blob: c813d1f896c2e85ea09e6115b155958fa5151d78 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaare4214502015-03-05 18:08:43 +010037#define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
Bram Moolenaare4214502015-03-05 18:08:43 +010040#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42static char *ctrl_x_msgs[] =
43{
44 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000046 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 N_(" Whole line completion (^L^N^P)"),
48 N_(" File name completion (^F^N^P)"),
49 N_(" Tag completion (^]^N^P)"),
50 N_(" Path pattern completion (^N^P)"),
51 N_(" Definition completion (^D^N^P)"),
52 NULL,
53 N_(" Dictionary completion (^K^N^P)"),
54 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000055 N_(" Command-line completion (^V^N^P)"),
56 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000057 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000058 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000059 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010060 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061};
62
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000063static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010064#ifdef FEAT_COMPL_FUNC
65static char e_complwin[] = N_("E839: Completion function changed window");
66static char e_compldel[] = N_("E840: Completion function deleted text");
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69/*
70 * Structure used to store one match for insert completion.
71 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000072typedef struct compl_S compl_T;
73struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000074{
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 compl_T *cp_next;
76 compl_T *cp_prev;
77 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000078 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000079 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000080 char_u *cp_fname; /* file containing the match, allocated when
81 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000082 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
83 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
Bram Moolenaar572cb562005-08-05 21:35:02 +000086#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#define FREE_FNAME (2)
88
89/*
90 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000091 * "compl_first_match" points to the start of the list.
92 * "compl_curr_match" points to the currently selected entry.
93 * "compl_shown_match" is different from compl_curr_match during
94 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000096static compl_T *compl_first_match = NULL;
97static compl_T *compl_curr_match = NULL;
98static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000100/* After using a cursor key <Enter> selects a match in the popup menu,
101 * otherwise it inserts a line break. */
102static int compl_enter_selects = FALSE;
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104/* When "compl_leader" is not NULL only matches that start with this string
105 * are used. */
106static char_u *compl_leader = NULL;
107
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000108static int compl_get_longest = FALSE; /* put longest common string
109 in compl_leader */
110
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200111static int compl_no_insert = FALSE; /* FALSE: select & insert
112 TRUE: noinsert */
113static int compl_no_select = FALSE; /* FALSE: select & insert
114 TRUE: noselect */
115
Bram Moolenaara6557602006-02-04 22:43:20 +0000116static int compl_used_match; /* Selected one of the matches. When
117 FALSE the match was edited or using
118 the longest common string. */
119
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000120static int compl_was_interrupted = FALSE; /* didn't finish finding
121 completions. */
122
123static int compl_restarting = FALSE; /* don't insert match */
124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125/* When the first completion is done "compl_started" is set. When it's
126 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000129/* Set when doing something for completion that may call edit() recursively,
130 * which is not allowed. */
131static int compl_busy = FALSE;
132
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static int compl_matches = 0;
134static char_u *compl_pattern = NULL;
135static int compl_direction = FORWARD;
136static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000137static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000138static pos_T compl_startpos;
139static colnr_T compl_col = 0; /* column where the text starts
140 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000141static char_u *compl_orig_text = NULL; /* text as it was before
142 * completion started */
143static int compl_cont_mode = 0;
144static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar82139082011-09-14 16:52:09 +0200146static int compl_opt_refresh_always = FALSE;
147
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100148static void ins_ctrl_x(void);
149static int has_compl_option(int dict_opt);
150static int ins_compl_accept_char(int c);
151static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
152static int ins_compl_equal(compl_T *match, char_u *str, int len);
153static void ins_compl_longest_match(compl_T *match);
154static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
155static int ins_compl_make_cyclic(void);
156static void ins_compl_upd_pum(void);
157static void ins_compl_del_pum(void);
158static int pum_wanted(void);
159static int pum_enough_matches(void);
160static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
161static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
162static char_u *find_line_end(char_u *ptr);
163static void ins_compl_free(void);
164static void ins_compl_clear(void);
165static int ins_compl_bs(void);
166static int ins_compl_need_restart(void);
167static void ins_compl_new_leader(void);
168static void ins_compl_addleader(int c);
169static int ins_compl_len(void);
170static void ins_compl_restart(void);
171static void ins_compl_set_original_text(char_u *str);
172static void ins_compl_addfrommatch(void);
173static int ins_compl_prep(int c);
174static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
175static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000176#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000179#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static int ins_compl_get_exp(pos_T *ini);
181static void ins_compl_delete(void);
182static void ins_compl_insert(void);
183static int ins_compl_next(int allow_get_expansion, int count, int insert_match);
184static int ins_compl_key2dir(int c);
185static int ins_compl_pum_key(int c);
186static int ins_compl_key2count(int c);
187static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100188static int ins_complete(int c, int enable_pum);
189static void show_pum(int save_w_wrow);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif /* FEAT_INS_EXPAND */
192
193#define BACKSPACE_CHAR 1
194#define BACKSPACE_WORD 2
195#define BACKSPACE_WORD_NOT_SPACE 3
196#define BACKSPACE_LINE 4
197
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100198static void ins_redraw(int ready);
199static void ins_ctrl_v(void);
200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
206static void start_arrow_with_change(pos_T *end_insert_pos, int change);
207static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000208#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void check_spell_redraw(void);
210static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000211static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
214static int echeck_abbr(int);
215static int replace_pop(void);
216static void replace_join(int off);
217static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void replace_flush(void);
222static void replace_do_bs(int limit_col);
223static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ins_reg(void);
228static void ins_ctrl_g(void);
229static void ins_ctrl_hat(void);
230static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int ins_start_select(int c);
235static void ins_insert(int replaceState);
236static void ins_ctrl_o(void);
237static void ins_shift(int c, int lastc);
238static void ins_del(void);
239static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_mouse(int c);
242static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000244#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000246#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_left(int end_change);
248static void ins_home(int c);
249static void ins_end(int c);
250static void ins_s_left(void);
251static void ins_right(int end_change);
252static void ins_s_right(void);
253static void ins_up(int startcol);
254static void ins_pageup(void);
255static void ins_down(int startcol);
256static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_tab(void);
261static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100270#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274static colnr_T Insstart_textlen; /* length of line when insert started */
275static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100276static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278static char_u *last_insert = NULL; /* the text of the previous insert,
279 K_SPECIAL and CSI are escaped */
280static int last_insert_skip; /* nr of chars in front of previous insert */
281static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000282static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284#ifdef FEAT_CINDENT
285static int can_cindent; /* may do cindenting on this line */
286#endif
287
288static int old_indent = 0; /* for ^^D command in insert mode */
289
290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000291static int revins_on; /* reverse insert mode on */
292static int revins_chars; /* how much to skip after edit */
293static int revins_legal; /* was the last char 'legal'? */
294static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static int ins_need_undo; /* call u_save() before inserting a
298 char. Set when edit() is called.
299 after that arrow_used is used. */
300
301static int did_add_space = FALSE; /* auto_format() added an extra space
302 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200303static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
304 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
306/*
307 * edit(): Start inserting text.
308 *
309 * "cmdchar" can be:
310 * 'i' normal insert command
311 * 'a' normal append command
312 * 'R' replace command
313 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
314 * but still only one <CR> is inserted. The <Esc> is not used for redo.
315 * 'g' "gI" command.
316 * 'V' "gR" command for Virtual Replace mode.
317 * 'v' "gr" command for single character Virtual Replace mode.
318 *
319 * This function is not called recursively. For CTRL-O commands, it returns
320 * and lets the caller handle the Normal-mode command.
321 *
322 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
323 */
324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100325edit(
326 int cmdchar,
327 int startln, /* if set, insert at start of line */
328 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329{
330 int c = 0;
331 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100332 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000333 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 int i;
336 int did_backspace = TRUE; /* previous char was backspace */
337#ifdef FEAT_CINDENT
338 int line_is_white = FALSE; /* line is empty before insert */
339#endif
340 linenr_T old_topline = 0; /* topline before insertion */
341#ifdef FEAT_DIFF
342 int old_topfill = -1;
343#endif
344 int inserted_space = FALSE; /* just inserted a space */
345 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000346 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000348 /* Remember whether editing was restarted after CTRL-O. */
349 did_restart_edit = restart_edit;
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 /* sleep before redrawing, needed for "CTRL-O :" that results in an
352 * error message */
353 check_for_delay(TRUE);
354
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100355 /* set Insstart_orig to Insstart */
356 update_Insstart_orig = TRUE;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef HAVE_SANDBOX
359 /* Don't allow inserting in the sandbox. */
360 if (sandbox != 0)
361 {
362 EMSG(_(e_sandbox));
363 return FALSE;
364 }
365#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000366 /* Don't allow changes in the buffer while editing the cmdline. The
367 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000368 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000369 {
370 EMSG(_(e_secure));
371 return FALSE;
372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000375 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000376 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000377 {
378 EMSG(_(e_secure));
379 return FALSE;
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 ins_compl_clear(); /* clear stuff for CTRL-X mode */
382#endif
383
Bram Moolenaar843ee412004-06-30 16:16:41 +0000384#ifdef FEAT_AUTOCMD
385 /*
386 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
387 */
388 if (cmdchar != 'r' && cmdchar != 'v')
389 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100390 pos_T save_cursor = curwin->w_cursor;
391
Bram Moolenaar1e015462005-09-25 22:16:38 +0000392# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000393 if (cmdchar == 'R')
394 ptr = (char_u *)"r";
395 else if (cmdchar == 'V')
396 ptr = (char_u *)"v";
397 else
398 ptr = (char_u *)"i";
399 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200400 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000401# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000402 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100403
Bram Moolenaar097c9922013-05-19 21:15:15 +0200404 /* Make sure the cursor didn't move. Do call check_cursor_col() in
405 * case the text was modified. Since Insert mode was not started yet
406 * a call to check_cursor_col() may move the cursor, especially with
407 * the "A" command, thus set State to avoid that. Also check that the
408 * line number is still valid (lines may have been deleted).
409 * Do not restore if v:char was set to a non-empty string. */
410 if (!equalpos(curwin->w_cursor, save_cursor)
411# ifdef FEAT_EVAL
412 && *get_vim_var_str(VV_CHAR) == NUL
413# endif
414 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100415 {
416 int save_state = State;
417
418 curwin->w_cursor = save_cursor;
419 State = INSERT;
420 check_cursor_col();
421 State = save_state;
422 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000423 }
424#endif
425
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200426#ifdef FEAT_CONCEAL
427 /* Check if the cursor line needs redrawing before changing State. If
428 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200429 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200430#endif
431
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#ifdef FEAT_MOUSE
433 /*
434 * When doing a paste with the middle mouse button, Insstart is set to
435 * where the paste started.
436 */
437 if (where_paste_started.lnum != 0)
438 Insstart = where_paste_started;
439 else
440#endif
441 {
442 Insstart = curwin->w_cursor;
443 if (startln)
444 Insstart.col = 0;
445 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000446 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 Insstart_blank_vcol = MAXCOL;
448 if (!did_ai)
449 ai_col = 0;
450
451 if (cmdchar != NUL && restart_edit == 0)
452 {
453 ResetRedobuff();
454 AppendNumberToRedobuff(count);
455#ifdef FEAT_VREPLACE
456 if (cmdchar == 'V' || cmdchar == 'v')
457 {
458 /* "gR" or "gr" command */
459 AppendCharToRedobuff('g');
460 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
461 }
462 else
463#endif
464 {
465 AppendCharToRedobuff(cmdchar);
466 if (cmdchar == 'g') /* "gI" command */
467 AppendCharToRedobuff('I');
468 else if (cmdchar == 'r') /* "r<CR>" command */
469 count = 1; /* insert only one <CR> */
470 }
471 }
472
473 if (cmdchar == 'R')
474 {
475#ifdef FEAT_FKMAP
476 if (p_fkmap && p_ri)
477 {
478 beep_flush();
479 EMSG(farsi_text_3); /* encoded in Farsi */
480 State = INSERT;
481 }
482 else
483#endif
484 State = REPLACE;
485 }
486#ifdef FEAT_VREPLACE
487 else if (cmdchar == 'V' || cmdchar == 'v')
488 {
489 State = VREPLACE;
490 replaceState = VREPLACE;
491 orig_line_count = curbuf->b_ml.ml_line_count;
492 vr_lines_changed = 1;
493 }
494#endif
495 else
496 State = INSERT;
497
498 stop_insert_mode = FALSE;
499
500 /*
501 * Need to recompute the cursor position, it might move when the cursor is
502 * on a TAB or special character.
503 */
504 curs_columns(TRUE);
505
506 /*
507 * Enable langmap or IME, indicated by 'iminsert'.
508 * Note that IME may enabled/disabled without us noticing here, thus the
509 * 'iminsert' value may not reflect what is actually used. It is updated
510 * when hitting <Esc>.
511 */
512 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
513 State |= LANGMAP;
514#ifdef USE_IM_CONTROL
515 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
516#endif
517
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#ifdef FEAT_MOUSE
519 setmouse();
520#endif
521#ifdef FEAT_CMDL_INFO
522 clear_showcmd();
523#endif
524#ifdef FEAT_RIGHTLEFT
525 /* there is no reverse replace mode */
526 revins_on = (State == INSERT && p_ri);
527 if (revins_on)
528 undisplay_dollar();
529 revins_chars = 0;
530 revins_legal = 0;
531 revins_scol = -1;
532#endif
533
534 /*
535 * Handle restarting Insert mode.
536 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
537 * restart_edit non-zero, and something in the stuff buffer.
538 */
539 if (restart_edit != 0 && stuff_empty())
540 {
541#ifdef FEAT_MOUSE
542 /*
543 * After a paste we consider text typed to be part of the insert for
544 * the pasted text. You can backspace over the pasted text too.
545 */
546 if (where_paste_started.lnum)
547 arrow_used = FALSE;
548 else
549#endif
550 arrow_used = TRUE;
551 restart_edit = 0;
552
553 /*
554 * If the cursor was after the end-of-line before the CTRL-O and it is
555 * now at the end-of-line, put it after the end-of-line (this is not
556 * correct in very rare cases).
557 * Also do this if curswant is greater than the current virtual
558 * column. Eg after "^O$" or "^O80|".
559 */
560 validate_virtcol();
561 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000562 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 || curwin->w_curswant > curwin->w_virtcol)
564 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
565 {
566 if (ptr[1] == NUL)
567 ++curwin->w_cursor.col;
568#ifdef FEAT_MBYTE
569 else if (has_mbyte)
570 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000571 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (ptr[i] == NUL)
573 curwin->w_cursor.col += i;
574 }
575#endif
576 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000577 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
579 else
580 arrow_used = FALSE;
581
582 /* we are in insert mode now, don't need to start it anymore */
583 need_start_insertmode = FALSE;
584
585 /* Need to save the line for undo before inserting the first char. */
586 ins_need_undo = TRUE;
587
588#ifdef FEAT_MOUSE
589 where_paste_started.lnum = 0;
590#endif
591#ifdef FEAT_CINDENT
592 can_cindent = TRUE;
593#endif
594#ifdef FEAT_FOLDING
595 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
596 * restarting. */
597 if (!p_im && did_restart_edit == 0)
598 foldOpenCursor();
599#endif
600
601 /*
602 * If 'showmode' is set, show the current (insert/replace/..) mode.
603 * A warning message for changing a readonly file is given here, before
604 * actually changing anything. It's put after the mode, if any.
605 */
606 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000607 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 i = showmode();
609
610 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000611 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613#ifdef CURSOR_SHAPE
614 ui_cursor_shape(); /* may show different cursor shape */
615#endif
616#ifdef FEAT_DIGRAPHS
617 do_digraph(-1); /* clear digraphs */
618#endif
619
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000620 /*
621 * Get the current length of the redo buffer, those characters have to be
622 * skipped if we want to get to the inserted characters.
623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 ptr = get_inserted();
625 if (ptr == NULL)
626 new_insert_skip = 0;
627 else
628 {
629 new_insert_skip = (int)STRLEN(ptr);
630 vim_free(ptr);
631 }
632
633 old_indent = 0;
634
635 /*
636 * Main loop in Insert mode: repeat until Insert mode is left.
637 */
638 for (;;)
639 {
640#ifdef FEAT_RIGHTLEFT
641 if (!revins_legal)
642 revins_scol = -1; /* reset on illegal motions */
643 else
644 revins_legal = 0;
645#endif
646 if (arrow_used) /* don't repeat insert when arrow key used */
647 count = 0;
648
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100649 if (update_Insstart_orig)
650 Insstart_orig = Insstart;
651
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 if (stop_insert_mode)
653 {
654 /* ":stopinsert" used or 'insertmode' reset */
655 count = 0;
656 goto doESCkey;
657 }
658
659 /* set curwin->w_curswant for next K_DOWN or K_UP */
660 if (!arrow_used)
661 curwin->w_set_curswant = TRUE;
662
663 /* If there is no typeahead may check for timestamps (e.g., for when a
664 * menu invoked a shell command). */
665 if (stuff_empty())
666 {
667 did_check_timestamps = FALSE;
668 if (need_check_timestamps)
669 check_timestamps(FALSE);
670 }
671
672 /*
673 * When emsg() was called msg_scroll will have been set.
674 */
675 msg_scroll = FALSE;
676
677#ifdef FEAT_GUI
678 /* When 'mousefocus' is set a mouse movement may have taken us to
679 * another window. "need_mouse_correct" may then be set because of an
680 * autocommand. */
681 if (need_mouse_correct)
682 gui_mouse_correct();
683#endif
684
685#ifdef FEAT_FOLDING
686 /* Open fold at the cursor line, according to 'foldopen'. */
687 if (fdo_flags & FDO_INSERT)
688 foldOpenCursor();
689 /* Close folds where the cursor isn't, according to 'foldclose' */
690 if (!char_avail())
691 foldCheckClose();
692#endif
693
694 /*
695 * If we inserted a character at the last position of the last line in
696 * the window, scroll the window one line up. This avoids an extra
697 * redraw.
698 * This is detected when the cursor column is smaller after inserting
699 * something.
700 * Don't do this when the topline changed already, it has
701 * already been adjusted (by insertchar() calling open_line())).
702 */
703 if (curbuf->b_mod_set
704 && curwin->w_p_wrap
705 && !did_backspace
706 && curwin->w_topline == old_topline
707#ifdef FEAT_DIFF
708 && curwin->w_topfill == old_topfill
709#endif
710 )
711 {
712 mincol = curwin->w_wcol;
713 validate_cursor_col();
714
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000715 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 && curwin->w_wrow == W_WINROW(curwin)
717 + curwin->w_height - 1 - p_so
718 && (curwin->w_cursor.lnum != curwin->w_topline
719#ifdef FEAT_DIFF
720 || curwin->w_topfill > 0
721#endif
722 ))
723 {
724#ifdef FEAT_DIFF
725 if (curwin->w_topfill > 0)
726 --curwin->w_topfill;
727 else
728#endif
729#ifdef FEAT_FOLDING
730 if (hasFolding(curwin->w_topline, NULL, &old_topline))
731 set_topline(curwin, old_topline + 1);
732 else
733#endif
734 set_topline(curwin, curwin->w_topline + 1);
735 }
736 }
737
738 /* May need to adjust w_topline to show the cursor. */
739 update_topline();
740
741 did_backspace = FALSE;
742
743 validate_cursor(); /* may set must_redraw */
744
745 /*
746 * Redraw the display when no characters are waiting.
747 * Also shows mode, ruler and positions cursor.
748 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000749 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
751#ifdef FEAT_SCROLLBIND
752 if (curwin->w_p_scb)
753 do_check_scrollbind(TRUE);
754#endif
755
Bram Moolenaar860cae12010-06-05 23:22:07 +0200756#ifdef FEAT_CURSORBIND
757 if (curwin->w_p_crb)
758 do_check_cursorbind();
759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 update_curswant();
761 old_topline = curwin->w_topline;
762#ifdef FEAT_DIFF
763 old_topfill = curwin->w_topfill;
764#endif
765
766#ifdef USE_ON_FLY_SCROLL
767 dont_scroll = FALSE; /* allow scrolling here */
768#endif
769
770 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000771 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100773 if (c != K_CURSORHOLD)
774 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200775
776 /* After using CTRL-G U the next cursor key will not break undo. */
777 if (dont_sync_undo == MAYBE)
778 dont_sync_undo = TRUE;
779 else
780 dont_sync_undo = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000781 do
782 {
783 c = safe_vgetc();
784 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000786#ifdef FEAT_AUTOCMD
787 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
788 did_cursorhold = TRUE;
789#endif
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#ifdef FEAT_RIGHTLEFT
792 if (p_hkmap && KeyTyped)
793 c = hkmap(c); /* Hebrew mode mapping */
794#endif
795#ifdef FEAT_FKMAP
796 if (p_fkmap && KeyTyped)
797 c = fkmap(c); /* Farsi mode mapping */
798#endif
799
800#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000801 /*
802 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000803 * and the cursor is still in the completed word. Only when there is
804 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000805 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000806 if (compl_started
807 && pum_wanted()
808 && curwin->w_cursor.col >= compl_col
809 && (compl_shown_match == NULL
810 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000811 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000812 /* BS: Delete one character from "compl_leader". */
813 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000814 && curwin->w_cursor.col > compl_col
815 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000816 continue;
817
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000818 /* When no match was selected or it was edited. */
819 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000820 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000821 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000822 * "compl_leader". Except when at the original match and
823 * there is nothing to add, CTRL-L works like CTRL-P then. */
824 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100825 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000826 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000827 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 {
829 ins_compl_addfrommatch();
830 continue;
831 }
832
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000833 /* A non-white character that fits in with the current
834 * completion: Add to "compl_leader". */
835 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000836 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100837#ifdef FEAT_AUTOCMD
838 /* Trigger InsertCharPre. */
839 char_u *str = do_insert_char_pre(c);
840 char_u *p;
841
842 if (str != NULL)
843 {
844 for (p = str; *p != NUL; mb_ptr_adv(p))
845 ins_compl_addleader(PTR2CHAR(p));
846 vim_free(str);
847 }
848 else
849#endif
850 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000851 continue;
852 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000853
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000854 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000855 * compl_enter_selects is set the Enter key does the same. */
856 if (c == Ctrl_Y || (compl_enter_selects
857 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000858 {
859 ins_compl_delete();
860 ins_compl_insert();
861 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000862 }
863 }
864
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
866 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000867 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000868 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000869 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#endif
871
Bram Moolenaar488c6512005-08-11 20:09:58 +0000872 /* CTRL-\ CTRL-N goes to Normal mode,
873 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
874 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (c == Ctrl_BSL)
876 {
877 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000878 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 ++no_mapping;
880 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000881 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 --no_mapping;
883 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000884 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000886 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 vungetc(c);
888 c = Ctrl_BSL;
889 }
890 else if (c == Ctrl_G && p_im)
891 continue;
892 else
893 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000894 if (c == Ctrl_O)
895 {
896 ins_ctrl_o();
897 ins_at_eol = FALSE; /* cursor keeps its column */
898 nomove = TRUE;
899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 count = 0;
901 goto doESCkey;
902 }
903 }
904
905#ifdef FEAT_DIGRAPHS
906 c = do_digraph(c);
907#endif
908
909#ifdef FEAT_INS_EXPAND
910 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
911 goto docomplete;
912#endif
913 if (c == Ctrl_V || c == Ctrl_Q)
914 {
915 ins_ctrl_v();
916 c = Ctrl_V; /* pretend CTRL-V is last typed character */
917 continue;
918 }
919
920#ifdef FEAT_CINDENT
921 if (cindent_on()
922# ifdef FEAT_INS_EXPAND
923 && ctrl_x_mode == 0
924# endif
925 )
926 {
927 /* A key name preceded by a bang means this key is not to be
928 * inserted. Skip ahead to the re-indenting below.
929 * A key name preceded by a star means that indenting has to be
930 * done before inserting the key. */
931 line_is_white = inindent(0);
932 if (in_cinkeys(c, '!', line_is_white))
933 goto force_cindent;
934 if (can_cindent && in_cinkeys(c, '*', line_is_white)
935 && stop_arrow() == OK)
936 do_c_expr_indent();
937 }
938#endif
939
940#ifdef FEAT_RIGHTLEFT
941 if (curwin->w_p_rl)
942 switch (c)
943 {
944 case K_LEFT: c = K_RIGHT; break;
945 case K_S_LEFT: c = K_S_RIGHT; break;
946 case K_C_LEFT: c = K_C_RIGHT; break;
947 case K_RIGHT: c = K_LEFT; break;
948 case K_S_RIGHT: c = K_S_LEFT; break;
949 case K_C_RIGHT: c = K_C_LEFT; break;
950 }
951#endif
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 /*
954 * If 'keymodel' contains "startsel", may start selection. If it
955 * does, a CTRL-O and c will be stuffed, we need to get these
956 * characters.
957 */
958 if (ins_start_select(c))
959 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 /*
962 * The big switch to handle a character in insert mode.
963 */
964 switch (c)
965 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 if (echeck_abbr(ESC + ABBR_OFF))
968 break;
969 /*FALLTHROUGH*/
970
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000971 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972#ifdef FEAT_CMDWIN
973 if (c == Ctrl_C && cmdwin_type != 0)
974 {
975 /* Close the cmdline window. */
976 cmdwin_result = K_IGNORE;
977 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000978 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 goto doESCkey;
980 }
981#endif
982
983#ifdef UNIX
984do_intr:
985#endif
986 /* when 'insertmode' set, and not halfway a mapping, don't leave
987 * Insert mode */
988 if (goto_im())
989 {
990 if (got_int)
991 {
992 (void)vgetc(); /* flush all buffers */
993 got_int = FALSE;
994 }
995 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200996 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 break;
998 }
999doESCkey:
1000 /*
1001 * This is the ONLY return from edit()!
1002 */
1003 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1004 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001005 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 o_lnum = curwin->w_cursor.lnum;
1007
Bram Moolenaar488c6512005-08-11 20:09:58 +00001008 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001009 {
1010#ifdef FEAT_AUTOCMD
1011 if (cmdchar != 'r' && cmdchar != 'v')
1012 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1013 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001014 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 continue;
1019
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001020 case Ctrl_Z: /* suspend when 'insertmode' set */
1021 if (!p_im)
1022 goto normalchar; /* insert CTRL-Z as normal char */
1023 stuffReadbuff((char_u *)":st\r");
1024 c = Ctrl_O;
1025 /*FALLTHROUGH*/
1026
1027 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001028#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001029 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001030 goto docomplete;
1031#endif
1032 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1033 break;
1034 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001035
1036#ifdef FEAT_VIRTUALEDIT
1037 /* don't move the cursor left when 'virtualedit' has "onemore". */
1038 if (ve_flags & VE_ONEMORE)
1039 {
1040 ins_at_eol = FALSE;
1041 nomove = TRUE;
1042 }
1043#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 count = 0;
1045 goto doESCkey;
1046
Bram Moolenaar572cb562005-08-05 21:35:02 +00001047 case K_INS: /* toggle insert/replace mode */
1048 case K_KINS:
1049 ins_insert(replaceState);
1050 break;
1051
1052 case K_SELECT: /* end of Select mode mapping - ignore */
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case K_HELP: /* Help key works like <ESC> <Help> */
1056 case K_F1:
1057 case K_XF1:
1058 stuffcharReadbuff(K_HELP);
1059 if (p_im)
1060 need_start_insertmode = TRUE;
1061 goto doESCkey;
1062
1063#ifdef FEAT_NETBEANS_INTG
1064 case K_F21: /* NetBeans command */
1065 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001066 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 --no_mapping;
1068 netbeans_keycommand(i);
1069 break;
1070#endif
1071
1072 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case NUL:
1074 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 /* For ^@ the trailing ESC will end the insert, unless there is an
1076 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1078 && c != Ctrl_A && !p_im)
1079 goto doESCkey; /* quit insert mode */
1080 inserted_space = FALSE;
1081 break;
1082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 ins_reg();
1085 auto_format(FALSE, TRUE);
1086 inserted_space = FALSE;
1087 break;
1088
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001089 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 ins_ctrl_g();
1091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case Ctrl_HAT: /* switch input mode and/or langmap */
1094 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 break;
1096
1097#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (!p_ari)
1100 goto normalchar;
1101 ins_ctrl_();
1102 break;
1103#endif
1104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001105 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1107 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1108 goto docomplete;
1109#endif
1110 /* FALLTHROUGH */
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113# ifdef FEAT_INS_EXPAND
1114 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1115 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 if (has_compl_option(FALSE))
1117 goto docomplete;
1118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120# endif
1121 ins_shift(c, lastc);
1122 auto_format(FALSE, TRUE);
1123 inserted_space = FALSE;
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 case K_KDEL:
1128 ins_del();
1129 auto_format(FALSE, TRUE);
1130 break;
1131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 case Ctrl_H:
1134 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1135 auto_format(FALSE, TRUE);
1136 break;
1137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1140 auto_format(FALSE, TRUE);
1141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001144# ifdef FEAT_COMPL_FUNC
1145 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001147 goto docomplete;
1148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1150 auto_format(FALSE, TRUE);
1151 inserted_space = FALSE;
1152 break;
1153
1154#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 case K_LEFTMOUSE_NM:
1157 case K_LEFTDRAG:
1158 case K_LEFTRELEASE:
1159 case K_LEFTRELEASE_NM:
1160 case K_MIDDLEMOUSE:
1161 case K_MIDDLEDRAG:
1162 case K_MIDDLERELEASE:
1163 case K_RIGHTMOUSE:
1164 case K_RIGHTDRAG:
1165 case K_RIGHTRELEASE:
1166 case K_X1MOUSE:
1167 case K_X1DRAG:
1168 case K_X1RELEASE:
1169 case K_X2MOUSE:
1170 case K_X2DRAG:
1171 case K_X2RELEASE:
1172 ins_mouse(c);
1173 break;
1174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001176 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 break;
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001180 ins_mousescroll(MSCR_UP);
1181 break;
1182
1183 case K_MOUSELEFT: /* Scroll wheel left */
1184 ins_mousescroll(MSCR_LEFT);
1185 break;
1186
1187 case K_MOUSERIGHT: /* Scroll wheel right */
1188 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 break;
1190#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001191#ifdef FEAT_GUI_TABLINE
1192 case K_TABLINE:
1193 case K_TABMENU:
1194 ins_tabline(c);
1195 break;
1196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 break;
1200
Bram Moolenaar754b5602006-02-09 23:53:20 +00001201#ifdef FEAT_AUTOCMD
1202 case K_CURSORHOLD: /* Didn't type something for a while. */
1203 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1204 did_cursorhold = TRUE;
1205 break;
1206#endif
1207
Bram Moolenaar4770d092006-01-12 23:22:24 +00001208#ifdef FEAT_GUI_W32
1209 /* On Win32 ignore <M-F4>, we get it when closing the window was
1210 * cancelled. */
1211 case K_F4:
1212 if (mod_mask != MOD_MASK_ALT)
1213 goto normalchar;
1214 break;
1215#endif
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#ifdef FEAT_GUI
1218 case K_VER_SCROLLBAR:
1219 ins_scroll();
1220 break;
1221
1222 case K_HOR_SCROLLBAR:
1223 ins_horscroll();
1224 break;
1225#endif
1226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 case K_S_HOME:
1230 case K_C_HOME:
1231 ins_home(c);
1232 break;
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 case K_S_END:
1237 case K_C_END:
1238 ins_end(c);
1239 break;
1240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001241 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001242 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1243 ins_s_left();
1244 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001245 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 break;
1247
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 case K_C_LEFT:
1250 ins_s_left();
1251 break;
1252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001254 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1255 ins_s_right();
1256 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001257 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 break;
1259
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001260 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 case K_C_RIGHT:
1262 ins_s_right();
1263 break;
1264
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001265 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001266#ifdef FEAT_INS_EXPAND
1267 if (pum_visible())
1268 goto docomplete;
1269#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001270 if (mod_mask & MOD_MASK_SHIFT)
1271 ins_pageup();
1272 else
1273 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 break;
1275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 case K_PAGEUP:
1278 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001279#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001280 if (pum_visible())
1281 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 ins_pageup();
1284 break;
1285
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001286 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001287#ifdef FEAT_INS_EXPAND
1288 if (pum_visible())
1289 goto docomplete;
1290#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001291 if (mod_mask & MOD_MASK_SHIFT)
1292 ins_pagedown();
1293 else
1294 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 break;
1296
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001297 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 case K_PAGEDOWN:
1299 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001300#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001301 if (pum_visible())
1302 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 ins_pagedown();
1305 break;
1306
1307#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 ins_drop();
1310 break;
1311#endif
1312
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001313 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 c = TAB;
1315 /* FALLTHROUGH */
1316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001317 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1319 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1320 goto docomplete;
1321#endif
1322 inserted_space = FALSE;
1323 if (ins_tab())
1324 goto normalchar; /* insert TAB as a normal char */
1325 auto_format(FALSE, TRUE);
1326 break;
1327
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001328 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 c = CAR;
1330 /* FALLTHROUGH */
1331 case CAR:
1332 case NL:
1333#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1334 /* In a quickfix window a <CR> jumps to the error under the
1335 * cursor. */
1336 if (bt_quickfix(curbuf) && c == CAR)
1337 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001338 if (curwin->w_llist_ref == NULL) /* quickfix window */
1339 do_cmdline_cmd((char_u *)".cc");
1340 else /* location list window */
1341 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 break;
1343 }
1344#endif
1345#ifdef FEAT_CMDWIN
1346 if (cmdwin_type != 0)
1347 {
1348 /* Execute the command in the cmdline window. */
1349 cmdwin_result = CAR;
1350 goto doESCkey;
1351 }
1352#endif
1353 if (ins_eol(c) && !p_im)
1354 goto doESCkey; /* out of memory */
1355 auto_format(FALSE, FALSE);
1356 inserted_space = FALSE;
1357 break;
1358
Bram Moolenaar860cae12010-06-05 23:22:07 +02001359#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361# ifdef FEAT_INS_EXPAND
1362 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1363 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001364 if (has_compl_option(TRUE))
1365 goto docomplete;
1366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368# endif
1369# ifdef FEAT_DIGRAPHS
1370 c = ins_digraph();
1371 if (c == NUL)
1372 break;
1373# endif
1374 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001378 case Ctrl_X: /* Enter CTRL-X mode */
1379 ins_ctrl_x();
1380 break;
1381
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (ctrl_x_mode != CTRL_X_TAGS)
1384 goto normalchar;
1385 goto docomplete;
1386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (ctrl_x_mode != CTRL_X_FILES)
1389 goto normalchar;
1390 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001391
1392 case 's': /* Spelling completion after ^X */
1393 case Ctrl_S:
1394 if (ctrl_x_mode != CTRL_X_SPELL)
1395 goto normalchar;
1396 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#endif
1398
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001399 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400#ifdef FEAT_INS_EXPAND
1401 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1402#endif
1403 {
1404 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1405 if (p_im)
1406 {
1407 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1408 break;
1409 goto doESCkey;
1410 }
1411 goto normalchar;
1412 }
1413#ifdef FEAT_INS_EXPAND
1414 /* FALLTHROUGH */
1415
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001416 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 case Ctrl_N:
1418 /* if 'complete' is empty then plain ^P is no longer special,
1419 * but it is under other ^X modes */
1420 if (*curbuf->b_p_cpt == NUL
1421 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001422 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 goto normalchar;
1424
1425docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001426 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001427 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001428 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001429 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001430 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001431 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 break;
1433#endif /* FEAT_INS_EXPAND */
1434
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001435 case Ctrl_Y: /* copy from previous line or scroll down */
1436 case Ctrl_E: /* copy from next line or scroll up */
1437 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 break;
1439
1440 default:
1441#ifdef UNIX
1442 if (c == intr_char) /* special interrupt char */
1443 goto do_intr;
1444#endif
1445
Bram Moolenaare659c952011-05-19 17:25:41 +02001446normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001448 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001450#ifdef FEAT_AUTOCMD
1451 if (!p_paste)
1452 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001453 /* Trigger InsertCharPre. */
1454 char_u *str = do_insert_char_pre(c);
1455 char_u *p;
1456
1457 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001458 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001459 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001460 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001461 /* Insert the new value of v:char literally. */
1462 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001463 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001464 c = PTR2CHAR(p);
1465 if (c == CAR || c == K_KENTER || c == NL)
1466 ins_eol(c);
1467 else
1468 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001469 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001470 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001471 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001472 vim_free(str);
1473 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001474 }
1475
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001476 /* If the new value is already inserted or an empty string
1477 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001478 if (c == NUL)
1479 break;
1480 }
1481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#ifdef FEAT_SMARTINDENT
1483 /* Try to perform smart-indenting. */
1484 ins_try_si(c);
1485#endif
1486
1487 if (c == ' ')
1488 {
1489 inserted_space = TRUE;
1490#ifdef FEAT_CINDENT
1491 if (inindent(0))
1492 can_cindent = FALSE;
1493#endif
1494 if (Insstart_blank_vcol == MAXCOL
1495 && curwin->w_cursor.lnum == Insstart.lnum)
1496 Insstart_blank_vcol = get_nolist_virtcol();
1497 }
1498
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001499 /* Insert a normal character and check for abbreviations on a
1500 * special character. Let CTRL-] expand abbreviations without
1501 * inserting it. */
1502 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503#ifdef FEAT_MBYTE
1504 /* Add ABBR_OFF for characters above 0x100, this is
1505 * what check_abbr() expects. */
1506 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1507#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001508 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
1510 insert_special(c, FALSE, FALSE);
1511#ifdef FEAT_RIGHTLEFT
1512 revins_legal++;
1513 revins_chars++;
1514#endif
1515 }
1516
1517 auto_format(FALSE, TRUE);
1518
1519#ifdef FEAT_FOLDING
1520 /* When inserting a character the cursor line must never be in a
1521 * closed fold. */
1522 foldOpenCursor();
1523#endif
1524 break;
1525 } /* end of switch (c) */
1526
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001527#ifdef FEAT_AUTOCMD
1528 /* If typed something may trigger CursorHoldI again. */
1529 if (c != K_CURSORHOLD)
1530 did_cursorhold = FALSE;
1531#endif
1532
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 /* If the cursor was moved we didn't just insert a space */
1534 if (arrow_used)
1535 inserted_space = FALSE;
1536
1537#ifdef FEAT_CINDENT
1538 if (can_cindent && cindent_on()
1539# ifdef FEAT_INS_EXPAND
1540 && ctrl_x_mode == 0
1541# endif
1542 )
1543 {
1544force_cindent:
1545 /*
1546 * Indent now if a key was typed that is in 'cinkeys'.
1547 */
1548 if (in_cinkeys(c, ' ', line_is_white))
1549 {
1550 if (stop_arrow() == OK)
1551 /* re-indent the current line */
1552 do_c_expr_indent();
1553 }
1554 }
1555#endif /* FEAT_CINDENT */
1556
1557 } /* for (;;) */
1558 /* NOTREACHED */
1559}
1560
1561/*
1562 * Redraw for Insert mode.
1563 * This is postponed until getting the next character to make '$' in the 'cpo'
1564 * option work correctly.
1565 * Only redraw when there are no characters available. This speeds up
1566 * inserting sequences of characters (e.g., for CTRL-R).
1567 */
1568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569ins_redraw(
1570 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001572#ifdef FEAT_CONCEAL
1573 linenr_T conceal_old_cursor_line = 0;
1574 linenr_T conceal_new_cursor_line = 0;
1575 int conceal_update_lines = FALSE;
1576#endif
1577
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001578 if (char_avail())
1579 return;
1580
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001581#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001582 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1583 * visible, the command might delete it. */
1584 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001585# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001586 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001587# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001588# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001589 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001590# endif
1591# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001592 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001593# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001594 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001595# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001596 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001597# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001598# ifdef FEAT_INS_EXPAND
1599 && !pum_visible()
1600# endif
1601 )
1602 {
1603# ifdef FEAT_SYN_HL
1604 /* Need to update the screen first, to make sure syntax
1605 * highlighting is correct after making a change (e.g., inserting
1606 * a "(". The autocommand may also require a redraw, so it's done
1607 * again below, unfortunately. */
1608 if (syntax_present(curwin) && must_redraw)
1609 update_screen(0);
1610# endif
1611# ifdef FEAT_AUTOCMD
1612 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001613 {
1614 /* Make sure curswant is correct, an autocommand may call
1615 * getcurpos(). */
1616 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001617 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001618 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001619# endif
1620# ifdef FEAT_CONCEAL
1621 if (curwin->w_p_cole > 0)
1622 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001623# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001625# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626 conceal_new_cursor_line = curwin->w_cursor.lnum;
1627 conceal_update_lines = TRUE;
1628 }
1629# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001630# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001631 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001632# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001633 }
1634#endif
1635
1636#ifdef FEAT_AUTOCMD
1637 /* Trigger TextChangedI if b_changedtick differs. */
1638 if (ready && has_textchangedI()
1639 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001640# ifdef FEAT_INS_EXPAND
1641 && !pum_visible()
1642# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001643 )
1644 {
1645 if (last_changedtick_buf == curbuf)
1646 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1647 last_changedtick_buf = curbuf;
1648 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001650#endif
1651
1652 if (must_redraw)
1653 update_screen(0);
1654 else if (clear_cmdline || redraw_cmdline)
1655 showmode(); /* clear cmdline and show mode */
1656# if defined(FEAT_CONCEAL)
1657 if ((conceal_update_lines
1658 && (conceal_old_cursor_line != conceal_new_cursor_line
1659 || conceal_cursor_line(curwin)))
1660 || need_cursor_line_redraw)
1661 {
1662 if (conceal_old_cursor_line != conceal_new_cursor_line)
1663 update_single_line(curwin, conceal_old_cursor_line);
1664 update_single_line(curwin, conceal_new_cursor_line == 0
1665 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1666 curwin->w_valid &= ~VALID_CROW;
1667 }
1668# endif
1669 showruler(FALSE);
1670 setcursor();
1671 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672}
1673
1674/*
1675 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1676 */
1677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001678ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001681 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001684 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
1686 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001689 did_putchar = TRUE;
1690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1692
1693#ifdef FEAT_CMDL_INFO
1694 add_to_showcmd_c(Ctrl_V);
1695#endif
1696
1697 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001698 if (did_putchar)
1699 /* when the line fits in 'columns' the '^' is at the start of the next
1700 * line and will not removed by the redraw */
1701 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#ifdef FEAT_CMDL_INFO
1703 clear_showcmd();
1704#endif
1705 insert_special(c, FALSE, TRUE);
1706#ifdef FEAT_RIGHTLEFT
1707 revins_chars++;
1708 revins_legal++;
1709#endif
1710}
1711
1712/*
1713 * Put a character directly onto the screen. It's not stored in a buffer.
1714 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1715 */
1716static int pc_status;
1717#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1718#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1719#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1720#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722static int pc_attr;
1723static int pc_row;
1724static int pc_col;
1725
1726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001727edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 int attr;
1730
1731 if (ScreenLines != NULL)
1732 {
1733 update_topline(); /* just in case w_topline isn't valid */
1734 validate_cursor();
1735 if (highlight)
1736 attr = hl_attr(HLF_8);
1737 else
1738 attr = 0;
1739 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1740 pc_col = W_WINCOL(curwin);
1741#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1742 pc_status = PC_STATUS_UNSET;
1743#endif
1744#ifdef FEAT_RIGHTLEFT
1745 if (curwin->w_p_rl)
1746 {
1747 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1748# ifdef FEAT_MBYTE
1749 if (has_mbyte)
1750 {
1751 int fix_col = mb_fix_col(pc_col, pc_row);
1752
1753 if (fix_col != pc_col)
1754 {
1755 screen_putchar(' ', pc_row, fix_col, attr);
1756 --curwin->w_wcol;
1757 pc_status = PC_STATUS_RIGHT;
1758 }
1759 }
1760# endif
1761 }
1762 else
1763#endif
1764 {
1765 pc_col += curwin->w_wcol;
1766#ifdef FEAT_MBYTE
1767 if (mb_lefthalve(pc_row, pc_col))
1768 pc_status = PC_STATUS_LEFT;
1769#endif
1770 }
1771
1772 /* save the character to be able to put it back */
1773#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1774 if (pc_status == PC_STATUS_UNSET)
1775#endif
1776 {
1777 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1778 pc_status = PC_STATUS_SET;
1779 }
1780 screen_putchar(c, pc_row, pc_col, attr);
1781 }
1782}
1783
1784/*
1785 * Undo the previous edit_putchar().
1786 */
1787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1791 {
1792#if defined(FEAT_MBYTE)
1793 if (pc_status == PC_STATUS_RIGHT)
1794 ++curwin->w_wcol;
1795 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1796 redrawWinline(curwin->w_cursor.lnum, FALSE);
1797 else
1798#endif
1799 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1800 }
1801}
1802
1803/*
1804 * Called when p_dollar is set: display a '$' at the end of the changed text
1805 * Only works when cursor is in the line that changes.
1806 */
1807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
1810 colnr_T save_col;
1811
1812 if (!redrawing())
1813 return;
1814
1815 cursor_off();
1816 save_col = curwin->w_cursor.col;
1817 curwin->w_cursor.col = col;
1818#ifdef FEAT_MBYTE
1819 if (has_mbyte)
1820 {
1821 char_u *p;
1822
1823 /* If on the last byte of a multi-byte move to the first byte. */
1824 p = ml_get_curline();
1825 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1826 }
1827#endif
1828 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1829 if (curwin->w_wcol < W_WIDTH(curwin))
1830 {
1831 edit_putchar('$', FALSE);
1832 dollar_vcol = curwin->w_virtcol;
1833 }
1834 curwin->w_cursor.col = save_col;
1835}
1836
1837/*
1838 * Call this function before moving the cursor from the normal insert position
1839 * in insert mode.
1840 */
1841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001842undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001844 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001846 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 redrawWinline(curwin->w_cursor.lnum, FALSE);
1848 }
1849}
1850
1851/*
1852 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1853 * Keep the cursor on the same character.
1854 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1855 * type == INDENT_DEC decrease indent (for CTRL-D)
1856 * type == INDENT_SET set indent to "amount"
1857 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1858 */
1859 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001860change_indent(
1861 int type,
1862 int amount,
1863 int round,
1864 int replaced, /* replaced character, put on replace stack */
1865 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 int vcol;
1868 int last_vcol;
1869 int insstart_less; /* reduction for Insstart.col */
1870 int new_cursor_col;
1871 int i;
1872 char_u *ptr;
1873 int save_p_list;
1874 int start_col;
1875 colnr_T vc;
1876#ifdef FEAT_VREPLACE
1877 colnr_T orig_col = 0; /* init for GCC */
1878 char_u *new_line, *orig_line = NULL; /* init for GCC */
1879
1880 /* VREPLACE mode needs to know what the line was like before changing */
1881 if (State & VREPLACE_FLAG)
1882 {
1883 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1884 orig_col = curwin->w_cursor.col;
1885 }
1886#endif
1887
1888 /* for the following tricks we don't want list mode */
1889 save_p_list = curwin->w_p_list;
1890 curwin->w_p_list = FALSE;
1891 vc = getvcol_nolist(&curwin->w_cursor);
1892 vcol = vc;
1893
1894 /*
1895 * For Replace mode we need to fix the replace stack later, which is only
1896 * possible when the cursor is in the indent. Remember the number of
1897 * characters before the cursor if it's possible.
1898 */
1899 start_col = curwin->w_cursor.col;
1900
1901 /* determine offset from first non-blank */
1902 new_cursor_col = curwin->w_cursor.col;
1903 beginline(BL_WHITE);
1904 new_cursor_col -= curwin->w_cursor.col;
1905
1906 insstart_less = curwin->w_cursor.col;
1907
1908 /*
1909 * If the cursor is in the indent, compute how many screen columns the
1910 * cursor is to the left of the first non-blank.
1911 */
1912 if (new_cursor_col < 0)
1913 vcol = get_indent() - vcol;
1914
1915 if (new_cursor_col > 0) /* can't fix replace stack */
1916 start_col = -1;
1917
1918 /*
1919 * Set the new indent. The cursor will be put on the first non-blank.
1920 */
1921 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001922 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 else
1924 {
1925#ifdef FEAT_VREPLACE
1926 int save_State = State;
1927
1928 /* Avoid being called recursively. */
1929 if (State & VREPLACE_FLAG)
1930 State = INSERT;
1931#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001932 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933#ifdef FEAT_VREPLACE
1934 State = save_State;
1935#endif
1936 }
1937 insstart_less -= curwin->w_cursor.col;
1938
1939 /*
1940 * Try to put cursor on same character.
1941 * If the cursor is at or after the first non-blank in the line,
1942 * compute the cursor column relative to the column of the first
1943 * non-blank character.
1944 * If we are not in insert mode, leave the cursor on the first non-blank.
1945 * If the cursor is before the first non-blank, position it relative
1946 * to the first non-blank, counted in screen columns.
1947 */
1948 if (new_cursor_col >= 0)
1949 {
1950 /*
1951 * When changing the indent while the cursor is touching it, reset
1952 * Insstart_col to 0.
1953 */
1954 if (new_cursor_col == 0)
1955 insstart_less = MAXCOL;
1956 new_cursor_col += curwin->w_cursor.col;
1957 }
1958 else if (!(State & INSERT))
1959 new_cursor_col = curwin->w_cursor.col;
1960 else
1961 {
1962 /*
1963 * Compute the screen column where the cursor should be.
1964 */
1965 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001966 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968 /*
1969 * Advance the cursor until we reach the right screen column.
1970 */
1971 vcol = last_vcol = 0;
1972 new_cursor_col = -1;
1973 ptr = ml_get_curline();
1974 while (vcol <= (int)curwin->w_virtcol)
1975 {
1976 last_vcol = vcol;
1977#ifdef FEAT_MBYTE
1978 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001979 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 else
1981#endif
1982 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001983 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 vcol = last_vcol;
1986
1987 /*
1988 * May need to insert spaces to be able to position the cursor on
1989 * the right screen column.
1990 */
1991 if (vcol != (int)curwin->w_virtcol)
1992 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001993 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001995 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (ptr != NULL)
1997 {
1998 new_cursor_col += i;
1999 ptr[i] = NUL;
2000 while (--i >= 0)
2001 ptr[i] = ' ';
2002 ins_str(ptr);
2003 vim_free(ptr);
2004 }
2005 }
2006
2007 /*
2008 * When changing the indent while the cursor is in it, reset
2009 * Insstart_col to 0.
2010 */
2011 insstart_less = MAXCOL;
2012 }
2013
2014 curwin->w_p_list = save_p_list;
2015
2016 if (new_cursor_col <= 0)
2017 curwin->w_cursor.col = 0;
2018 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002019 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 curwin->w_set_curswant = TRUE;
2021 changed_cline_bef_curs();
2022
2023 /*
2024 * May have to adjust the start of the insert.
2025 */
2026 if (State & INSERT)
2027 {
2028 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2029 {
2030 if ((int)Insstart.col <= insstart_less)
2031 Insstart.col = 0;
2032 else
2033 Insstart.col -= insstart_less;
2034 }
2035 if ((int)ai_col <= insstart_less)
2036 ai_col = 0;
2037 else
2038 ai_col -= insstart_less;
2039 }
2040
2041 /*
2042 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2043 * If the number of characters before the cursor decreased, need to pop a
2044 * few characters from the replace stack.
2045 * If the number of characters before the cursor increased, need to push a
2046 * few NULs onto the replace stack.
2047 */
2048 if (REPLACE_NORMAL(State) && start_col >= 0)
2049 {
2050 while (start_col > (int)curwin->w_cursor.col)
2051 {
2052 replace_join(0); /* remove a NUL from the replace stack */
2053 --start_col;
2054 }
2055 while (start_col < (int)curwin->w_cursor.col || replaced)
2056 {
2057 replace_push(NUL);
2058 if (replaced)
2059 {
2060 replace_push(replaced);
2061 replaced = NUL;
2062 }
2063 ++start_col;
2064 }
2065 }
2066
2067#ifdef FEAT_VREPLACE
2068 /*
2069 * For VREPLACE mode, we also have to fix the replace stack. In this case
2070 * it is always possible because we backspace over the whole line and then
2071 * put it back again the way we wanted it.
2072 */
2073 if (State & VREPLACE_FLAG)
2074 {
2075 /* If orig_line didn't allocate, just return. At least we did the job,
2076 * even if you can't backspace. */
2077 if (orig_line == NULL)
2078 return;
2079
2080 /* Save new line */
2081 new_line = vim_strsave(ml_get_curline());
2082 if (new_line == NULL)
2083 return;
2084
2085 /* We only put back the new line up to the cursor */
2086 new_line[curwin->w_cursor.col] = NUL;
2087
2088 /* Put back original line */
2089 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2090 curwin->w_cursor.col = orig_col;
2091
2092 /* Backspace from cursor to start of line */
2093 backspace_until_column(0);
2094
2095 /* Insert new stuff into line again */
2096 ins_bytes(new_line);
2097
2098 vim_free(new_line);
2099 }
2100#endif
2101}
2102
2103/*
2104 * Truncate the space at the end of a line. This is to be used only in an
2105 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2106 * modes.
2107 */
2108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002109truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 int i;
2112
2113 /* find start of trailing white space */
2114 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2115 {
2116 if (State & REPLACE_FLAG)
2117 replace_join(0); /* remove a NUL from the replace stack */
2118 }
2119 line[i + 1] = NUL;
2120}
2121
2122#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2123 || defined(FEAT_COMMENTS) || defined(PROTO)
2124/*
2125 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2126 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002127 * Will attempt not to go before "col" even when there is a composing
2128 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 */
2130 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002131backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133 while ((int)curwin->w_cursor.col > col)
2134 {
2135 curwin->w_cursor.col--;
2136 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002137 replace_do_bs(col);
2138 else if (!del_char_after_col(col))
2139 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
2141}
2142#endif
2143
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002144/*
2145 * Like del_char(), but make sure not to go before column "limit_col".
2146 * Only matters when there are composing characters.
2147 * Return TRUE when something was deleted.
2148 */
2149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002150del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002151{
2152#ifdef FEAT_MBYTE
2153 if (enc_utf8 && limit_col >= 0)
2154 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002155 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002156
2157 /* Make sure the cursor is at the start of a character, but
2158 * skip forward again when going too far back because of a
2159 * composing character. */
2160 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002161 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002162 {
2163 int l = utf_ptr2len(ml_get_cursor());
2164
2165 if (l == 0) /* end of line */
2166 break;
2167 curwin->w_cursor.col += l;
2168 }
2169 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2170 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002171 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002172 }
2173 else
2174#endif
2175 (void)del_char(FALSE);
2176 return TRUE;
2177}
2178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2180/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002181 * CTRL-X pressed in Insert mode.
2182 */
2183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002184ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002185{
2186 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2187 * CTRL-V works like CTRL-N */
2188 if (ctrl_x_mode != CTRL_X_CMDLINE)
2189 {
2190 /* if the next ^X<> won't ADD nothing, then reset
2191 * compl_cont_status */
2192 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002193 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002194 else
2195 compl_cont_status = 0;
2196 /* We're not sure which CTRL-X mode it will be yet */
2197 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2198 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2199 edit_submode_pre = NULL;
2200 showmode();
2201 }
2202}
2203
2204/*
2205 * Return TRUE if the 'dict' or 'tsr' option can be used.
2206 */
2207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002209{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002210 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002211# ifdef FEAT_SPELL
2212 && !curwin->w_p_spell
2213# endif
2214 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002215 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2216 {
2217 ctrl_x_mode = 0;
2218 edit_submode = NULL;
2219 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2220 : (char_u *)_("'thesaurus' option is empty"),
2221 hl_attr(HLF_E));
2222 if (emsg_silent == 0)
2223 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002224 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002225 setcursor();
2226 out_flush();
2227 ui_delay(2000L, FALSE);
2228 }
2229 return FALSE;
2230 }
2231 return TRUE;
2232}
2233
2234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2236 * This depends on the current mode.
2237 */
2238 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
2241 /* Always allow ^R - let it's results then be checked */
2242 if (c == Ctrl_R)
2243 return TRUE;
2244
Bram Moolenaare3226be2005-12-18 22:10:00 +00002245 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002246 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002247 return TRUE;
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 switch (ctrl_x_mode)
2250 {
2251 case 0: /* Not in any CTRL-X mode */
2252 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2253 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002254 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2256 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2257 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002258 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002259 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 case CTRL_X_SCROLL:
2261 return (c == Ctrl_Y || c == Ctrl_E);
2262 case CTRL_X_WHOLE_LINE:
2263 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2264 case CTRL_X_FILES:
2265 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2266 case CTRL_X_DICTIONARY:
2267 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2268 case CTRL_X_THESAURUS:
2269 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2270 case CTRL_X_TAGS:
2271 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2272#ifdef FEAT_FIND_ID
2273 case CTRL_X_PATH_PATTERNS:
2274 return (c == Ctrl_P || c == Ctrl_N);
2275 case CTRL_X_PATH_DEFINES:
2276 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2277#endif
2278 case CTRL_X_CMDLINE:
2279 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2280 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002281#ifdef FEAT_COMPL_FUNC
2282 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002283 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002284 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002285 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002286#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002287 case CTRL_X_SPELL:
2288 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002289 case CTRL_X_EVAL:
2290 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292 EMSG(_(e_internal));
2293 return FALSE;
2294}
2295
2296/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002297 * Return TRUE when character "c" is part of the item currently being
2298 * completed. Used to decide whether to abandon complete mode when the menu
2299 * is visible.
2300 */
2301 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002302ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002303{
2304 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2305 /* When expanding an identifier only accept identifier chars. */
2306 return vim_isIDc(c);
2307
2308 switch (ctrl_x_mode)
2309 {
2310 case CTRL_X_FILES:
2311 /* When expanding file name only accept file name chars. But not
2312 * path separators, so that "proto/<Tab>" expands files in
2313 * "proto", not "proto/" as a whole */
2314 return vim_isfilec(c) && !vim_ispathsep(c);
2315
2316 case CTRL_X_CMDLINE:
2317 case CTRL_X_OMNI:
2318 /* Command line and Omni completion can work with just about any
2319 * printable character, but do stop at white space. */
2320 return vim_isprintc(c) && !vim_iswhite(c);
2321
2322 case CTRL_X_WHOLE_LINE:
2323 /* For while line completion a space can be part of the line. */
2324 return vim_isprintc(c);
2325 }
2326 return vim_iswordc(c);
2327}
2328
2329/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002330 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002332 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 * the rest of the word to be in -- webb
2334 */
2335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002336ins_compl_add_infercase(
2337 char_u *str,
2338 int len,
2339 int icase,
2340 char_u *fname,
2341 int dir,
2342 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002344 char_u *p;
2345 int i, c;
2346 int actual_len; /* Take multi-byte characters */
2347 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002348 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002349 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 int has_lower = FALSE;
2351 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002353 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002355 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
Bram Moolenaara2993e12007-08-12 14:38:46 +00002357 /* Find actual length of completion. */
2358#ifdef FEAT_MBYTE
2359 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002361 p = str;
2362 actual_len = 0;
2363 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002365 mb_ptr_adv(p);
2366 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002369 else
2370#endif
2371 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
Bram Moolenaara2993e12007-08-12 14:38:46 +00002373 /* Find actual length of original text. */
2374#ifdef FEAT_MBYTE
2375 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002377 p = compl_orig_text;
2378 actual_compl_length = 0;
2379 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002381 mb_ptr_adv(p);
2382 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 }
2384 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002385 else
2386#endif
2387 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002389 /* "actual_len" may be smaller than "actual_compl_length" when using
2390 * thesaurus, only use the minimum when comparing. */
2391 min_len = actual_len < actual_compl_length
2392 ? actual_len : actual_compl_length;
2393
Bram Moolenaara2993e12007-08-12 14:38:46 +00002394 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002395 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002396 if (wca != NULL)
2397 {
2398 p = str;
2399 for (i = 0; i < actual_len; ++i)
2400#ifdef FEAT_MBYTE
2401 if (has_mbyte)
2402 wca[i] = mb_ptr2char_adv(&p);
2403 else
2404#endif
2405 wca[i] = *(p++);
2406
2407 /* Rule 1: Were any chars converted to lower? */
2408 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002409 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002410 {
2411#ifdef FEAT_MBYTE
2412 if (has_mbyte)
2413 c = mb_ptr2char_adv(&p);
2414 else
2415#endif
2416 c = *(p++);
2417 if (MB_ISLOWER(c))
2418 {
2419 has_lower = TRUE;
2420 if (MB_ISUPPER(wca[i]))
2421 {
2422 /* Rule 1 is satisfied. */
2423 for (i = actual_compl_length; i < actual_len; ++i)
2424 wca[i] = MB_TOLOWER(wca[i]);
2425 break;
2426 }
2427 }
2428 }
2429
2430 /*
2431 * Rule 2: No lower case, 2nd consecutive letter converted to
2432 * upper case.
2433 */
2434 if (!has_lower)
2435 {
2436 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002437 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002438 {
2439#ifdef FEAT_MBYTE
2440 if (has_mbyte)
2441 c = mb_ptr2char_adv(&p);
2442 else
2443#endif
2444 c = *(p++);
2445 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2446 {
2447 /* Rule 2 is satisfied. */
2448 for (i = actual_compl_length; i < actual_len; ++i)
2449 wca[i] = MB_TOUPPER(wca[i]);
2450 break;
2451 }
2452 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2453 }
2454 }
2455
2456 /* Copy the original case of the part we typed. */
2457 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002458 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002459 {
2460#ifdef FEAT_MBYTE
2461 if (has_mbyte)
2462 c = mb_ptr2char_adv(&p);
2463 else
2464#endif
2465 c = *(p++);
2466 if (MB_ISLOWER(c))
2467 wca[i] = MB_TOLOWER(wca[i]);
2468 else if (MB_ISUPPER(c))
2469 wca[i] = MB_TOUPPER(wca[i]);
2470 }
2471
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002472 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002473 * Generate encoding specific output from wide character array.
2474 * Multi-byte characters can occupy up to five bytes more than
2475 * ASCII characters, and we also need one byte for NUL, so stay
2476 * six bytes away from the edge of IObuff.
2477 */
2478 p = IObuff;
2479 i = 0;
2480 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2481#ifdef FEAT_MBYTE
2482 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002483 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002484 else
2485#endif
2486 *(p++) = wca[i++];
2487 *p = NUL;
2488
2489 vim_free(wca);
2490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002492 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2493 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002495 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496}
2497
2498/*
2499 * Add a match to the list of matches.
2500 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002501 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002502 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002505ins_compl_add(
2506 char_u *str,
2507 int len,
2508 int icase,
2509 char_u *fname,
2510 char_u **cptext, /* extra text for popup menu or NULL */
2511 int cdir,
2512 int flags,
2513 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002515 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002516 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
2518 ui_breakcheck();
2519 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002520 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (len < 0)
2522 len = (int)STRLEN(str);
2523
2524 /*
2525 * If the same match is already present, don't add it.
2526 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002527 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002529 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 do
2531 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002532 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002533 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002534 && match->cp_str[len] == NUL)
2535 return NOTDONE;
2536 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002537 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002540 /* Remove any popup menu before changing the list of matches. */
2541 ins_compl_del_pum();
2542
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 /*
2544 * Allocate a new match structure.
2545 * Copy the values to the new match structure.
2546 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002547 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002549 return FAIL;
2550 match->cp_number = -1;
2551 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002552 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002553 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002556 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002558 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002559
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002561 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2563 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002564 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002565 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002566 && compl_curr_match->cp_fname != NULL
2567 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002568 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002569 else if (fname != NULL)
2570 {
2571 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002572 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002575 match->cp_fname = NULL;
2576 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002577
2578 if (cptext != NULL)
2579 {
2580 int i;
2581
2582 for (i = 0; i < CPT_COUNT; ++i)
2583 if (cptext[i] != NULL && *cptext[i] != NUL)
2584 match->cp_text[i] = vim_strsave(cptext[i]);
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
2587 /*
2588 * Link the new match structure in the list of matches.
2589 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002590 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002591 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 else if (dir == FORWARD)
2593 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002594 match->cp_next = compl_curr_match->cp_next;
2595 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597 else /* BACKWARD */
2598 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002599 match->cp_next = compl_curr_match;
2600 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002602 if (match->cp_next)
2603 match->cp_next->cp_prev = match;
2604 if (match->cp_prev)
2605 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002607 compl_first_match = match;
2608 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002610 /*
2611 * Find the longest common string if still doing that.
2612 */
2613 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2614 ins_compl_longest_match(match);
2615
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 return OK;
2617}
2618
2619/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002620 * Return TRUE if "str[len]" matches with match->cp_str, considering
2621 * match->cp_icase.
2622 */
2623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002624ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002625{
2626 if (match->cp_icase)
2627 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2628 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2629}
2630
2631/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002632 * Reduce the longest common string for match "match".
2633 */
2634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002635ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002636{
2637 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002638 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002639 int had_match;
2640
2641 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002642 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002643 /* First match, use it as a whole. */
2644 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002645 if (compl_leader != NULL)
2646 {
2647 had_match = (curwin->w_cursor.col > compl_col);
2648 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002649 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002650 ins_redraw(FALSE);
2651
2652 /* When the match isn't there (to avoid matching itself) remove it
2653 * again after redrawing. */
2654 if (!had_match)
2655 ins_compl_delete();
2656 compl_used_match = FALSE;
2657 }
2658 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002659 else
2660 {
2661 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002662 p = compl_leader;
2663 s = match->cp_str;
2664 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002665 {
2666#ifdef FEAT_MBYTE
2667 if (has_mbyte)
2668 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002669 c1 = mb_ptr2char(p);
2670 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002671 }
2672 else
2673#endif
2674 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002675 c1 = *p;
2676 c2 = *s;
2677 }
2678 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2679 : (c1 != c2))
2680 break;
2681#ifdef FEAT_MBYTE
2682 if (has_mbyte)
2683 {
2684 mb_ptr_adv(p);
2685 mb_ptr_adv(s);
2686 }
2687 else
2688#endif
2689 {
2690 ++p;
2691 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002692 }
2693 }
2694
2695 if (*p != NUL)
2696 {
2697 /* Leader was shortened, need to change the inserted text. */
2698 *p = NUL;
2699 had_match = (curwin->w_cursor.col > compl_col);
2700 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002701 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002702 ins_redraw(FALSE);
2703
2704 /* When the match isn't there (to avoid matching itself) remove it
2705 * again after redrawing. */
2706 if (!had_match)
2707 ins_compl_delete();
2708 }
2709
2710 compl_used_match = FALSE;
2711 }
2712}
2713
2714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 * Add an array of matches to the list of matches.
2716 * Frees matches[].
2717 */
2718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002719ins_compl_add_matches(
2720 int num_matches,
2721 char_u **matches,
2722 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 int i;
2725 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002726 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
Bram Moolenaar572cb562005-08-05 21:35:02 +00002728 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002729 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002730 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002732 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 FreeWild(num_matches, matches);
2734}
2735
2736/* Make the completion list cyclic.
2737 * Return the number of matches (excluding the original).
2738 */
2739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002740ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002742 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 int count = 0;
2744
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002745 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 /*
2748 * Find the end of the list.
2749 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002750 match = compl_first_match;
2751 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002752 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002754 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 ++count;
2756 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002757 match->cp_next = compl_first_match;
2758 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
2760 return count;
2761}
2762
Bram Moolenaara94bc432006-03-10 21:42:59 +00002763/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002764 * Set variables that store noselect and noinsert behavior from the
2765 * 'completeopt' value.
2766 */
2767 void
2768completeopt_was_set()
2769{
2770 compl_no_insert = FALSE;
2771 compl_no_select = FALSE;
2772 if (strstr((char *)p_cot, "noselect") != NULL)
2773 compl_no_select = TRUE;
2774 if (strstr((char *)p_cot, "noinsert") != NULL)
2775 compl_no_insert = TRUE;
2776}
2777
2778/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002779 * Start completion for the complete() function.
2780 * "startcol" is where the matched text starts (1 is first column).
2781 * "list" is the list of matches.
2782 */
2783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002784set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002785{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002786 int save_w_wrow = curwin->w_wrow;
2787
Bram Moolenaara94bc432006-03-10 21:42:59 +00002788 /* If already doing completions stop it. */
2789 if (ctrl_x_mode != 0)
2790 ins_compl_prep(' ');
2791 ins_compl_clear();
2792
2793 if (stop_arrow() == FAIL)
2794 return;
2795
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002796 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002797 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002798 startcol = curwin->w_cursor.col;
2799 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002800 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002801 /* compl_pattern doesn't need to be set */
2802 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2803 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002804 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002805 return;
2806
Bram Moolenaare4214502015-03-05 18:08:43 +01002807 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002808
2809 ins_compl_add_list(list);
2810 compl_matches = ins_compl_make_cyclic();
2811 compl_started = TRUE;
2812 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002813 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002814
2815 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002816 if (compl_no_insert || compl_no_select)
2817 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002818 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002819 if (compl_no_select)
2820 /* Down/Up has no real effect. */
2821 ins_complete(K_UP, FALSE);
2822 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002823 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002824 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002825
2826 /* Lazily show the popup menu, unless we got interrupted. */
2827 if (!compl_interrupted)
2828 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002829 out_flush();
2830}
2831
2832
Bram Moolenaar9372a112005-12-06 19:59:18 +00002833/* "compl_match_array" points the currently displayed list of entries in the
2834 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002835static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002836static int compl_match_arraysize;
2837
2838/*
2839 * Update the screen and when there is any scrolling remove the popup menu.
2840 */
2841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002842ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002843{
2844 int h;
2845
2846 if (compl_match_array != NULL)
2847 {
2848 h = curwin->w_cline_height;
2849 update_screen(0);
2850 if (h != curwin->w_cline_height)
2851 ins_compl_del_pum();
2852 }
2853}
2854
2855/*
2856 * Remove any popup menu.
2857 */
2858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002859ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002860{
2861 if (compl_match_array != NULL)
2862 {
2863 pum_undisplay();
2864 vim_free(compl_match_array);
2865 compl_match_array = NULL;
2866 }
2867}
2868
2869/*
2870 * Return TRUE if the popup menu should be displayed.
2871 */
2872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002873pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002875 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002876 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002877 return FALSE;
2878
2879 /* The display looks bad on a B&W display. */
2880 if (t_colors < 8
2881#ifdef FEAT_GUI
2882 && !gui.in_use
2883#endif
2884 )
2885 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002886 return TRUE;
2887}
2888
2889/*
2890 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002891 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002892 */
2893 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002894pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002895{
2896 compl_T *compl;
2897 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002898
2899 /* Don't display the popup menu if there are no matches or there is only
2900 * one (ignoring the original text). */
2901 compl = compl_first_match;
2902 i = 0;
2903 do
2904 {
2905 if (compl == NULL
2906 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2907 break;
2908 compl = compl->cp_next;
2909 } while (compl != compl_first_match);
2910
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002911 if (strstr((char *)p_cot, "menuone") != NULL)
2912 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002913 return (i >= 2);
2914}
2915
2916/*
2917 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002918 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002919 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002921ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002922{
2923 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002924 compl_T *shown_compl = NULL;
2925 int did_find_shown_match = FALSE;
2926 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002927 int i;
2928 int cur = -1;
2929 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002930 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002931
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002932 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002933 return;
2934
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002935#if defined(FEAT_EVAL)
2936 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2937 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2938#endif
2939
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002940 /* Update the screen before drawing the popup menu over it. */
2941 update_screen(0);
2942
2943 if (compl_match_array == NULL)
2944 {
2945 /* Need to build the popup menu list. */
2946 compl_match_arraysize = 0;
2947 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002948 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002949 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002950 do
2951 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002952 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2953 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002954 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002955 ++compl_match_arraysize;
2956 compl = compl->cp_next;
2957 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002958 if (compl_match_arraysize == 0)
2959 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002960 compl_match_array = (pumitem_T *)alloc_clear(
2961 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002962 * compl_match_arraysize));
2963 if (compl_match_array != NULL)
2964 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002965 /* If the current match is the original text don't find the first
2966 * match after it, don't highlight anything. */
2967 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2968 shown_match_ok = TRUE;
2969
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002970 i = 0;
2971 compl = compl_first_match;
2972 do
2973 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002974 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2975 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002976 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002977 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002978 if (!shown_match_ok)
2979 {
2980 if (compl == compl_shown_match || did_find_shown_match)
2981 {
2982 /* This item is the shown match or this is the
2983 * first displayed item after the shown match. */
2984 compl_shown_match = compl;
2985 did_find_shown_match = TRUE;
2986 shown_match_ok = TRUE;
2987 }
2988 else
2989 /* Remember this displayed match for when the
2990 * shown match is just below it. */
2991 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002992 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002993 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002994
2995 if (compl->cp_text[CPT_ABBR] != NULL)
2996 compl_match_array[i].pum_text =
2997 compl->cp_text[CPT_ABBR];
2998 else
2999 compl_match_array[i].pum_text = compl->cp_str;
3000 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3001 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3002 if (compl->cp_text[CPT_MENU] != NULL)
3003 compl_match_array[i++].pum_extra =
3004 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003005 else
3006 compl_match_array[i++].pum_extra = compl->cp_fname;
3007 }
3008
3009 if (compl == compl_shown_match)
3010 {
3011 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003012
3013 /* When the original text is the shown match don't set
3014 * compl_shown_match. */
3015 if (compl->cp_flags & ORIGINAL_TEXT)
3016 shown_match_ok = TRUE;
3017
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003018 if (!shown_match_ok && shown_compl != NULL)
3019 {
3020 /* The shown match isn't displayed, set it to the
3021 * previously displayed match. */
3022 compl_shown_match = shown_compl;
3023 shown_match_ok = TRUE;
3024 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003025 }
3026 compl = compl->cp_next;
3027 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003028
3029 if (!shown_match_ok) /* no displayed match at all */
3030 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003031 }
3032 }
3033 else
3034 {
3035 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003036 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003037 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3038 || compl_match_array[i].pum_text
3039 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003040 {
3041 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003042 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003043 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003044 }
3045
3046 if (compl_match_array != NULL)
3047 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003048 /* In Replace mode when a $ is displayed at the end of the line only
3049 * part of the screen would be updated. We do need to redraw here. */
3050 dollar_vcol = -1;
3051
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003052 /* Compute the screen column of the start of the completed text.
3053 * Use the cursor to get all wrapping and other settings right. */
3054 col = curwin->w_cursor.col;
3055 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003056 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003057 curwin->w_cursor.col = col;
3058 }
3059}
3060
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061#define DICT_FIRST (1) /* use just first element in "dict" */
3062#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003063
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003065 * Add any identifiers that match the given pattern in the list of dictionary
3066 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003067 */
3068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003069ins_compl_dictionaries(
3070 char_u *dict_start,
3071 char_u *pat,
3072 int flags, /* DICT_FIRST and/or DICT_EXACT */
3073 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003075 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 char_u *ptr;
3077 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 char_u **files;
3080 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003082 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083
Bram Moolenaar0b238792006-03-02 22:49:12 +00003084 if (*dict == NUL)
3085 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003086#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003087 /* When 'dictionary' is empty and spell checking is enabled use
3088 * "spell". */
3089 if (!thesaurus && curwin->w_p_spell)
3090 dict = (char_u *)"spell";
3091 else
3092#endif
3093 return;
3094 }
3095
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003097 if (buf == NULL)
3098 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003099 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003100
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 /* If 'infercase' is set, don't use 'smartcase' here */
3102 save_p_scs = p_scs;
3103 if (curbuf->b_p_inf)
3104 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003105
3106 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3107 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003108 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003109 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003110 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003111 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003112 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003113
3114 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003115 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003116 len = STRLEN(pat_esc) + 10;
3117 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003118 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003119 {
3120 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003121 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003122 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003123 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003124 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3125 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003126 vim_free(ptr);
3127 }
3128 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003129 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003130 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003131 if (regmatch.regprog == NULL)
3132 goto theend;
3133 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003134
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3136 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003137 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 {
3139 /* copy one dictionary file name into buf */
3140 if (flags == DICT_EXACT)
3141 {
3142 count = 1;
3143 files = &dict;
3144 }
3145 else
3146 {
3147 /* Expand wildcards in the dictionary name, but do not allow
3148 * backticks (for security, the 'dict' option may have been set in
3149 * a modeline). */
3150 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003151# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003152 if (!thesaurus && STRCMP(buf, "spell") == 0)
3153 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003154 else
3155# endif
3156 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 || expand_wildcards(1, &buf, &count, &files,
3158 EW_FILE|EW_SILENT) != OK)
3159 count = 0;
3160 }
3161
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003162# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003163 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003165 /* Complete from active spelling. Skip "\<" in the pattern, we
3166 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003167 if (pat[0] == '\\' && pat[1] == '<')
3168 ptr = pat + 2;
3169 else
3170 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003171 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003173 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003174# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003175 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003176 {
3177 ins_compl_files(count, files, thesaurus, flags,
3178 &regmatch, buf, &dir);
3179 if (flags != DICT_EXACT)
3180 FreeWild(count, files);
3181 }
3182 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 break;
3184 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003185
3186theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003187 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003188 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 vim_free(buf);
3190}
3191
Bram Moolenaar0b238792006-03-02 22:49:12 +00003192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003193ins_compl_files(
3194 int count,
3195 char_u **files,
3196 int thesaurus,
3197 int flags,
3198 regmatch_T *regmatch,
3199 char_u *buf,
3200 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003201{
3202 char_u *ptr;
3203 int i;
3204 FILE *fp;
3205 int add_r;
3206
3207 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3208 {
3209 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3210 if (flags != DICT_EXACT)
3211 {
3212 vim_snprintf((char *)IObuff, IOSIZE,
3213 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003214 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003215 }
3216
3217 if (fp != NULL)
3218 {
3219 /*
3220 * Read dictionary file line by line.
3221 * Check each line for a match.
3222 */
3223 while (!got_int && !compl_interrupted
3224 && !vim_fgets(buf, LSIZE, fp))
3225 {
3226 ptr = buf;
3227 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3228 {
3229 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003230 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003231 ptr = find_line_end(ptr);
3232 else
3233 ptr = find_word_end(ptr);
3234 add_r = ins_compl_add_infercase(regmatch->startp[0],
3235 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003236 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003237 if (thesaurus)
3238 {
3239 char_u *wstart;
3240
3241 /*
3242 * Add the other matches on the line
3243 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003244 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003245 while (!got_int)
3246 {
3247 /* Find start of the next word. Skip white
3248 * space and punctuation. */
3249 ptr = find_word_start(ptr);
3250 if (*ptr == NUL || *ptr == NL)
3251 break;
3252 wstart = ptr;
3253
Bram Moolenaara2993e12007-08-12 14:38:46 +00003254 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003255#ifdef FEAT_MBYTE
3256 if (has_mbyte)
3257 /* Japanese words may have characters in
3258 * different classes, only separate words
3259 * with single-byte non-word characters. */
3260 while (*ptr != NUL)
3261 {
3262 int l = (*mb_ptr2len)(ptr);
3263
3264 if (l < 2 && !vim_iswordc(*ptr))
3265 break;
3266 ptr += l;
3267 }
3268 else
3269#endif
3270 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003271
3272 /* Add the word. Skip the regexp match. */
3273 if (wstart != regmatch->startp[0])
3274 add_r = ins_compl_add_infercase(wstart,
3275 (int)(ptr - wstart),
3276 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003277 }
3278 }
3279 if (add_r == OK)
3280 /* if dir was BACKWARD then honor it just once */
3281 *dir = FORWARD;
3282 else if (add_r == FAIL)
3283 break;
3284 /* avoid expensive call to vim_regexec() when at end
3285 * of line */
3286 if (*ptr == '\n' || got_int)
3287 break;
3288 }
3289 line_breakcheck();
3290 ins_compl_check_keys(50);
3291 }
3292 fclose(fp);
3293 }
3294 }
3295}
3296
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297/*
3298 * Find the start of the next word.
3299 * Returns a pointer to the first char of the word. Also stops at a NUL.
3300 */
3301 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
3304#ifdef FEAT_MBYTE
3305 if (has_mbyte)
3306 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003307 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 else
3309#endif
3310 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3311 ++ptr;
3312 return ptr;
3313}
3314
3315/*
3316 * Find the end of the word. Assumes it starts inside a word.
3317 * Returns a pointer to just after the word.
3318 */
3319 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003321{
3322#ifdef FEAT_MBYTE
3323 int start_class;
3324
3325 if (has_mbyte)
3326 {
3327 start_class = mb_get_class(ptr);
3328 if (start_class > 1)
3329 while (*ptr != NUL)
3330 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003331 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332 if (mb_get_class(ptr) != start_class)
3333 break;
3334 }
3335 }
3336 else
3337#endif
3338 while (vim_iswordc(*ptr))
3339 ++ptr;
3340 return ptr;
3341}
3342
3343/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003344 * Find the end of the line, omitting CR and NL at the end.
3345 * Returns a pointer to just after the line.
3346 */
3347 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003348find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003349{
3350 char_u *s;
3351
3352 s = ptr + STRLEN(ptr);
3353 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3354 --s;
3355 return s;
3356}
3357
3358/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 * Free the list of completions
3360 */
3361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003362ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003364 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003365 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003367 vim_free(compl_pattern);
3368 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003369 vim_free(compl_leader);
3370 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003372 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003373 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003374
3375 ins_compl_del_pum();
3376 pum_clear();
3377
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003378 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379 do
3380 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003381 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003382 compl_curr_match = compl_curr_match->cp_next;
3383 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003385 if (match->cp_flags & FREE_FNAME)
3386 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003387 for (i = 0; i < CPT_COUNT; ++i)
3388 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003390 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3391 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003392 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393}
3394
3395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003396ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003398 compl_cont_status = 0;
3399 compl_started = FALSE;
3400 compl_matches = 0;
3401 vim_free(compl_pattern);
3402 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003403 vim_free(compl_leader);
3404 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003406 vim_free(compl_orig_text);
3407 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003408 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003409 /* clear v:completed_item */
3410 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003411}
3412
3413/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003414 * Return TRUE when Insert completion is active.
3415 */
3416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003417ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003418{
3419 return compl_started;
3420}
3421
3422/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003423 * Delete one character before the cursor and show the subset of the matches
3424 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003425 * Returns the character to be used, NUL if the work is done and another char
3426 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003427 */
3428 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003429ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003430{
3431 char_u *line;
3432 char_u *p;
3433
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003434 line = ml_get_curline();
3435 p = line + curwin->w_cursor.col;
3436 mb_ptr_back(line, p);
3437
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003438 /* Stop completion when the whole word was deleted. For Omni completion
3439 * allow the word to be deleted, we won't match everything. */
3440 if ((int)(p - line) - (int)compl_col < 0
3441 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003442 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003443 return K_BS;
3444
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003445 /* Deleted more than what was used to find matches or didn't finish
3446 * finding all matches: need to look for matches all over again. */
3447 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003448 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003449 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003450
Bram Moolenaara6557602006-02-04 22:43:20 +00003451 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003452 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003453 if (compl_leader != NULL)
3454 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003455 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003456 if (compl_shown_match != NULL)
3457 /* Make sure current match is not a hidden item. */
3458 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003459 return NUL;
3460 }
3461 return K_BS;
3462}
Bram Moolenaara6557602006-02-04 22:43:20 +00003463
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003464/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003465 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3466 * be called.
3467 */
3468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003469ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003470{
3471 /* Return TRUE if we didn't complete finding matches or when the
3472 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3473 return compl_was_interrupted
3474 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3475 && compl_opt_refresh_always);
3476}
3477
3478/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003479 * Called after changing "compl_leader".
3480 * Show the popup menu with a different set of matches.
3481 * May also search for matches again if the previous search was interrupted.
3482 */
3483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003484ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003485{
3486 ins_compl_del_pum();
3487 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003488 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003489 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003490
3491 if (compl_started)
3492 ins_compl_set_original_text(compl_leader);
3493 else
3494 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003495#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003496 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003497#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003498 /*
3499 * Matches were cleared, need to search for them now. First display
3500 * the changed text before the cursor. Set "compl_restarting" to
3501 * avoid that the first match is inserted.
3502 */
3503 update_screen(0);
3504#ifdef FEAT_GUI
3505 if (gui.in_use)
3506 {
3507 /* Show the cursor after the match, not after the redrawn text. */
3508 setcursor();
3509 out_flush();
3510 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003511 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003512#endif
3513 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003514 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003515 compl_cont_status = 0;
3516 compl_restarting = FALSE;
3517 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003518
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003519 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003520
3521 /* Show the popup menu with a different set of matches. */
3522 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003523
3524 /* Don't let Enter select the original text when there is no popup menu. */
3525 if (compl_match_array == NULL)
3526 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003527}
3528
3529/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003530 * Return the length of the completion, from the completion start column to
3531 * the cursor column. Making sure it never goes below zero.
3532 */
3533 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003534ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003535{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003536 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003537
3538 if (off < 0)
3539 return 0;
3540 return off;
3541}
3542
3543/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003544 * Append one character to the match leader. May reduce the number of
3545 * matches.
3546 */
3547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003548ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003549{
3550#ifdef FEAT_MBYTE
3551 int cc;
3552
3553 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3554 {
3555 char_u buf[MB_MAXBYTES + 1];
3556
3557 (*mb_char2bytes)(c, buf);
3558 buf[cc] = NUL;
3559 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003560 if (compl_opt_refresh_always)
3561 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003562 }
3563 else
3564#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003565 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003566 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003567 if (compl_opt_refresh_always)
3568 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003569 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003570
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003571 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003572 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003573 ins_compl_restart();
3574
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003575 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003576 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003577 * break redo. */
3578 if (!compl_opt_refresh_always)
3579 {
3580 vim_free(compl_leader);
3581 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003582 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003583 if (compl_leader != NULL)
3584 ins_compl_new_leader();
3585 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003586}
3587
3588/*
3589 * Setup for finding completions again without leaving CTRL-X mode. Used when
3590 * BS or a key was typed while still searching for matches.
3591 */
3592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003593ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003594{
3595 ins_compl_free();
3596 compl_started = FALSE;
3597 compl_matches = 0;
3598 compl_cont_status = 0;
3599 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003600}
3601
3602/*
3603 * Set the first match, the original text.
3604 */
3605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003606ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003607{
3608 char_u *p;
3609
3610 /* Replace the original text entry. */
3611 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3612 {
3613 p = vim_strsave(str);
3614 if (p != NULL)
3615 {
3616 vim_free(compl_first_match->cp_str);
3617 compl_first_match->cp_str = p;
3618 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003619 }
3620}
3621
3622/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003623 * Append one character to the match leader. May reduce the number of
3624 * matches.
3625 */
3626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003627ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003628{
3629 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003630 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003631 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003632 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003633
3634 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003635 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003636 {
3637 /* When still at the original match use the first entry that matches
3638 * the leader. */
3639 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3640 {
3641 p = NULL;
3642 for (cp = compl_shown_match->cp_next; cp != NULL
3643 && cp != compl_first_match; cp = cp->cp_next)
3644 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003645 if (compl_leader == NULL
3646 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003647 (int)STRLEN(compl_leader)))
3648 {
3649 p = cp->cp_str;
3650 break;
3651 }
3652 }
3653 if (p == NULL || (int)STRLEN(p) <= len)
3654 return;
3655 }
3656 else
3657 return;
3658 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003659 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003660 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003661 ins_compl_addleader(c);
3662}
3663
3664/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003666 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003667 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003669 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003670ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671{
3672 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003674 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
3676 /* Forget any previous 'special' messages if this is actually
3677 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3678 */
3679 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3680 edit_submode_extra = NULL;
3681
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003682 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003683 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3684 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003685 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003687 /* Set "compl_get_longest" when finding the first matches. */
3688 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3689 || (ctrl_x_mode == 0 && !compl_started))
3690 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003691 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003692 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003693
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003694 }
3695
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3697 {
3698 /*
3699 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3700 * it will be yet. Now we decide.
3701 */
3702 switch (c)
3703 {
3704 case Ctrl_E:
3705 case Ctrl_Y:
3706 ctrl_x_mode = CTRL_X_SCROLL;
3707 if (!(State & REPLACE_FLAG))
3708 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3709 else
3710 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3711 edit_submode_pre = NULL;
3712 showmode();
3713 break;
3714 case Ctrl_L:
3715 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3716 break;
3717 case Ctrl_F:
3718 ctrl_x_mode = CTRL_X_FILES;
3719 break;
3720 case Ctrl_K:
3721 ctrl_x_mode = CTRL_X_DICTIONARY;
3722 break;
3723 case Ctrl_R:
3724 /* Simply allow ^R to happen without affecting ^X mode */
3725 break;
3726 case Ctrl_T:
3727 ctrl_x_mode = CTRL_X_THESAURUS;
3728 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003729#ifdef FEAT_COMPL_FUNC
3730 case Ctrl_U:
3731 ctrl_x_mode = CTRL_X_FUNCTION;
3732 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003733 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003734 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003735 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003736#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003737 case 's':
3738 case Ctrl_S:
3739 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003740#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003741 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003742 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003743 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003744#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003745 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 case Ctrl_RSB:
3747 ctrl_x_mode = CTRL_X_TAGS;
3748 break;
3749#ifdef FEAT_FIND_ID
3750 case Ctrl_I:
3751 case K_S_TAB:
3752 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3753 break;
3754 case Ctrl_D:
3755 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3756 break;
3757#endif
3758 case Ctrl_V:
3759 case Ctrl_Q:
3760 ctrl_x_mode = CTRL_X_CMDLINE;
3761 break;
3762 case Ctrl_P:
3763 case Ctrl_N:
3764 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3765 * just started ^X mode, or there were enough ^X's to cancel
3766 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3767 * do normal expansion when interrupting a different mode (say
3768 * ^X^F^X^P or ^P^X^X^P, see below)
3769 * nothing changes if interrupting mode 0, (eg, the flag
3770 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003771 if (!(compl_cont_status & CONT_INTRPT))
3772 compl_cont_status |= CONT_LOCAL;
3773 else if (compl_cont_mode != 0)
3774 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 /* FALLTHROUGH */
3776 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003777 /* If we have typed at least 2 ^X's... for modes != 0, we set
3778 * compl_cont_status = 0 (eg, as if we had just started ^X
3779 * mode).
3780 * For mode 0, we set "compl_cont_mode" to an impossible
3781 * value, in both cases ^X^X can be used to restart the same
3782 * mode (avoiding ADDING mode).
3783 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3784 * 'complete' and local ^P expansions respectively.
3785 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3786 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 if (c == Ctrl_X)
3788 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003789 if (compl_cont_mode != 0)
3790 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003791 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003792 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 }
3794 ctrl_x_mode = 0;
3795 edit_submode = NULL;
3796 showmode();
3797 break;
3798 }
3799 }
3800 else if (ctrl_x_mode != 0)
3801 {
3802 /* We're already in CTRL-X mode, do we stay in it? */
3803 if (!vim_is_ctrl_x_key(c))
3804 {
3805 if (ctrl_x_mode == CTRL_X_SCROLL)
3806 ctrl_x_mode = 0;
3807 else
3808 ctrl_x_mode = CTRL_X_FINISHED;
3809 edit_submode = NULL;
3810 }
3811 showmode();
3812 }
3813
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003814 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 {
3816 /* Show error message from attempted keyword completion (probably
3817 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003818 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003820 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3821 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 || ctrl_x_mode == CTRL_X_FINISHED)
3823 {
3824 /* Get here when we have finished typing a sequence of ^N and
3825 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003826 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003827 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 {
3829 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003830 * If any of the original typed text has been changed, eg when
3831 * ignorecase is set, we must add back-spaces to the redo
3832 * buffer. We add as few as necessary to delete just the part
3833 * of the original text that has changed.
3834 * When using the longest match, edited the match or used
3835 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003836 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003837 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3838 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003839 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003840 ptr = NULL;
3841 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003842 }
3843
3844#ifdef FEAT_CINDENT
3845 want_cindent = (can_cindent && cindent_on());
3846#endif
3847 /*
3848 * When completing whole lines: fix indent for 'cindent'.
3849 * Otherwise, break line if it's too long.
3850 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003851 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 {
3853#ifdef FEAT_CINDENT
3854 /* re-indent the current line */
3855 if (want_cindent)
3856 {
3857 do_c_expr_indent();
3858 want_cindent = FALSE; /* don't do it again */
3859 }
3860#endif
3861 }
3862 else
3863 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003864 int prev_col = curwin->w_cursor.col;
3865
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003867 if (prev_col > 0)
3868 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003869 if (stop_arrow() == OK)
3870 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003871 if (prev_col > 0
3872 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3873 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
3875
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003876 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003877 * the selection without inserting anything. When
3878 * compl_enter_selects is set the Enter key does the same. */
3879 if ((c == Ctrl_Y || (compl_enter_selects
3880 && (c == CAR || c == K_KENTER || c == NL)))
3881 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003882 retval = TRUE;
3883
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003884 /* CTRL-E means completion is Ended, go back to the typed text. */
3885 if (c == Ctrl_E)
3886 {
3887 ins_compl_delete();
3888 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003889 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003890 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003891 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003892 retval = TRUE;
3893 }
3894
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003895 auto_format(FALSE, TRUE);
3896
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003898 compl_started = FALSE;
3899 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003900 if (!shortmess(SHM_COMPLETIONMENU))
3901 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003903 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003904 if (edit_submode != NULL)
3905 {
3906 edit_submode = NULL;
3907 showmode();
3908 }
3909
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003910#ifdef FEAT_CMDWIN
3911 if (c == Ctrl_C && cmdwin_type != 0)
3912 /* Avoid the popup menu remains displayed when leaving the
3913 * command line window. */
3914 update_screen(0);
3915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916#ifdef FEAT_CINDENT
3917 /*
3918 * Indent now if a key was typed that is in 'cinkeys'.
3919 */
3920 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3921 do_c_expr_indent();
3922#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003923#ifdef FEAT_AUTOCMD
3924 /* Trigger the CompleteDone event to give scripts a chance to act
3925 * upon the completion. */
3926 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
3929 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003930#ifdef FEAT_AUTOCMD
3931 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3932 /* Trigger the CompleteDone event to give scripts a chance to act
3933 * upon the (possibly failed) completion. */
3934 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936
3937 /* reset continue_* if we left expansion-mode, if we stay they'll be
3938 * (re)set properly in ins_complete() */
3939 if (!vim_is_ctrl_x_key(c))
3940 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003941 compl_cont_status = 0;
3942 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003944
3945 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946}
3947
3948/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003949 * Fix the redo buffer for the completion leader replacing some of the typed
3950 * text. This inserts backspaces and appends the changed text.
3951 * "ptr" is the known leader text or NUL.
3952 */
3953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003954ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003955{
3956 int len;
3957 char_u *p;
3958 char_u *ptr = ptr_arg;
3959
3960 if (ptr == NULL)
3961 {
3962 if (compl_leader != NULL)
3963 ptr = compl_leader;
3964 else
3965 return; /* nothing to do */
3966 }
3967 if (compl_orig_text != NULL)
3968 {
3969 p = compl_orig_text;
3970 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3971 ;
3972#ifdef FEAT_MBYTE
3973 if (len > 0)
3974 len -= (*mb_head_off)(p, p + len);
3975#endif
3976 for (p += len; *p != NUL; mb_ptr_adv(p))
3977 AppendCharToRedobuff(K_BS);
3978 }
3979 else
3980 len = 0;
3981 if (ptr != NULL)
3982 AppendToRedobuffLit(ptr + len, -1);
3983}
3984
3985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003986 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3987 * (depending on flag) starting from buf and looking for a non-scanned
3988 * buffer (other than curbuf). curbuf is special, if it is called with
3989 * buf=curbuf then it has to be the first call for a given flag/expansion.
3990 *
3991 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3992 */
3993 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003994ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995{
3996#ifdef FEAT_WINDOWS
3997 static win_T *wp;
3998#endif
3999
4000 if (flag == 'w') /* just windows */
4001 {
4002#ifdef FEAT_WINDOWS
4003 if (buf == curbuf) /* first call for this flag/expansion */
4004 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004005 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 && wp->w_buffer->b_scanned)
4007 ;
4008 buf = wp->w_buffer;
4009#else
4010 buf = curbuf;
4011#endif
4012 }
4013 else
4014 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4015 * (unlisted buffers)
4016 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004017 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 && ((flag == 'U'
4019 ? buf->b_p_bl
4020 : (!buf->b_p_bl
4021 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004022 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 ;
4024 return buf;
4025}
4026
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004027#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004028static void expand_by_function(int type, char_u *base);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004029
4030/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004031 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004032 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004033 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004035expand_by_function(
4036 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4037 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004038{
Bram Moolenaar82139082011-09-14 16:52:09 +02004039 list_T *matchlist = NULL;
4040 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004041 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004042 char_u *funcname;
4043 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004044 win_T *curwin_save;
4045 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004046 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004047
Bram Moolenaare344bea2005-09-01 20:46:49 +00004048 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4049 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004050 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004051
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004052 /* Call 'completefunc' to obtain the list of matches. */
4053 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004054 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004055
Bram Moolenaare344bea2005-09-01 20:46:49 +00004056 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004057 curwin_save = curwin;
4058 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004059
4060 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004061 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004062 {
4063 switch (rettv.v_type)
4064 {
4065 case VAR_LIST:
4066 matchlist = rettv.vval.v_list;
4067 break;
4068 case VAR_DICT:
4069 matchdict = rettv.vval.v_dict;
4070 break;
4071 default:
4072 /* TODO: Give error message? */
4073 clear_tv(&rettv);
4074 break;
4075 }
4076 }
4077
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004078 if (curwin_save != curwin || curbuf_save != curbuf)
4079 {
4080 EMSG(_(e_complwin));
4081 goto theend;
4082 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004083 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004084 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004085 if (!equalpos(curwin->w_cursor, pos))
4086 {
4087 EMSG(_(e_compldel));
4088 goto theend;
4089 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004090
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004091 if (matchlist != NULL)
4092 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004093 else if (matchdict != NULL)
4094 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004095
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004096theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004097 if (matchdict != NULL)
4098 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004099 if (matchlist != NULL)
4100 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004101}
4102#endif /* FEAT_COMPL_FUNC */
4103
Bram Moolenaar39f05632006-03-19 22:15:26 +00004104#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004105/*
4106 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004107 */
4108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004109ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004110{
4111 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004112 int dir = compl_direction;
4113
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004114 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004115 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004116 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004117 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4118 /* if dir was BACKWARD then honor it just once */
4119 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004120 else if (did_emsg)
4121 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004122 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004123}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004124
4125/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004126 * Add completions from a dict.
4127 */
4128 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004129ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004130{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004131 dictitem_T *di_refresh;
4132 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004133
4134 /* Check for optional "refresh" item. */
4135 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004136 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4137 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004138 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004139 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004140
4141 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4142 compl_opt_refresh_always = TRUE;
4143 }
4144
4145 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004146 di_words = dict_find(dict, (char_u *)"words", 5);
4147 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4148 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004149}
4150
4151/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004152 * Add a match to the list of matches from a typeval_T.
4153 * If the given string is already in the list of completions, then return
4154 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4155 * maybe because alloc() returns NULL, then FAIL is returned.
4156 */
4157 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004158ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004159{
4160 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004161 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004162 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004163 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004164 char_u *(cptext[CPT_COUNT]);
4165
4166 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4167 {
4168 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4169 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4170 (char_u *)"abbr", FALSE);
4171 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4172 (char_u *)"menu", FALSE);
4173 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4174 (char_u *)"kind", FALSE);
4175 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4176 (char_u *)"info", FALSE);
4177 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4178 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004179 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004180 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004181 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4182 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004183 }
4184 else
4185 {
4186 word = get_tv_string_chk(tv);
4187 vim_memset(cptext, 0, sizeof(cptext));
4188 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004189 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004190 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004191 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004192}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004193#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004194
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004195/*
4196 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004197 * The search starts at position "ini" in curbuf and in the direction
4198 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004199 * When "compl_started" is FALSE start at that position, otherwise continue
4200 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004201 * This may return before finding all the matches.
4202 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 */
4204 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004205ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206{
4207 static pos_T first_match_pos;
4208 static pos_T last_match_pos;
4209 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210 static int found_all = FALSE; /* Found all matches of a
4211 certain type. */
4212 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213
Bram Moolenaar572cb562005-08-05 21:35:02 +00004214 pos_T *pos;
4215 char_u **matches;
4216 int save_p_scs;
4217 int save_p_ws;
4218 int save_p_ic;
4219 int i;
4220 int num_matches;
4221 int len;
4222 int found_new_match;
4223 int type = ctrl_x_mode;
4224 char_u *ptr;
4225 char_u *dict = NULL;
4226 int dict_f = 0;
4227 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004228 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004230 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
4232 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4233 ins_buf->b_scanned = 0;
4234 found_all = FALSE;
4235 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004236 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004237 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 last_match_pos = first_match_pos = *ini;
4239 }
4240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004242 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4244 for (;;)
4245 {
4246 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004247 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004249 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250 * or if found_all says this entry is done. For ^X^L only use the
4251 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004252 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004253 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 {
4255 found_all = FALSE;
4256 while (*e_cpt == ',' || *e_cpt == ' ')
4257 e_cpt++;
4258 if (*e_cpt == '.' && !curbuf->b_scanned)
4259 {
4260 ins_buf = curbuf;
4261 first_match_pos = *ini;
4262 /* So that ^N can match word immediately after cursor */
4263 if (ctrl_x_mode == 0)
4264 dec(&first_match_pos);
4265 last_match_pos = first_match_pos;
4266 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004267
4268 /* Remember the first match so that the loop stops when we
4269 * wrap and come back there a second time. */
4270 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
4272 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4273 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4274 {
4275 /* Scan a buffer, but not the current one. */
4276 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4277 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004278 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 first_match_pos.col = last_match_pos.col = 0;
4280 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4281 last_match_pos.lnum = 0;
4282 type = 0;
4283 }
4284 else /* unloaded buffer, scan like dictionary */
4285 {
4286 found_all = TRUE;
4287 if (ins_buf->b_fname == NULL)
4288 continue;
4289 type = CTRL_X_DICTIONARY;
4290 dict = ins_buf->b_fname;
4291 dict_f = DICT_EXACT;
4292 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004293 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 ins_buf->b_fname == NULL
4295 ? buf_spname(ins_buf)
4296 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004297 ? ins_buf->b_fname
4298 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004299 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 else if (*e_cpt == NUL)
4302 break;
4303 else
4304 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004305 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 type = -1;
4307 else if (*e_cpt == 'k' || *e_cpt == 's')
4308 {
4309 if (*e_cpt == 'k')
4310 type = CTRL_X_DICTIONARY;
4311 else
4312 type = CTRL_X_THESAURUS;
4313 if (*++e_cpt != ',' && *e_cpt != NUL)
4314 {
4315 dict = e_cpt;
4316 dict_f = DICT_FIRST;
4317 }
4318 }
4319#ifdef FEAT_FIND_ID
4320 else if (*e_cpt == 'i')
4321 type = CTRL_X_PATH_PATTERNS;
4322 else if (*e_cpt == 'd')
4323 type = CTRL_X_PATH_DEFINES;
4324#endif
4325 else if (*e_cpt == ']' || *e_cpt == 't')
4326 {
4327 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004328 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004329 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 }
4331 else
4332 type = -1;
4333
4334 /* in any case e_cpt is advanced to the next entry */
4335 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4336
4337 found_all = TRUE;
4338 if (type == -1)
4339 continue;
4340 }
4341 }
4342
4343 switch (type)
4344 {
4345 case -1:
4346 break;
4347#ifdef FEAT_FIND_ID
4348 case CTRL_X_PATH_PATTERNS:
4349 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004350 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004351 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4355 (linenr_T)1, (linenr_T)MAXLNUM);
4356 break;
4357#endif
4358
4359 case CTRL_X_DICTIONARY:
4360 case CTRL_X_THESAURUS:
4361 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004362 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 : (type == CTRL_X_THESAURUS
4364 ? (*curbuf->b_p_tsr == NUL
4365 ? p_tsr
4366 : curbuf->b_p_tsr)
4367 : (*curbuf->b_p_dict == NUL
4368 ? p_dict
4369 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004370 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004371 dict != NULL ? dict_f
4372 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 dict = NULL;
4374 break;
4375
4376 case CTRL_X_TAGS:
4377 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4378 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004381 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382 * of matches is found when compl_pattern is empty */
4383 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4385 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4386 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4387 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004388 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 p_ic = save_p_ic;
4391 break;
4392
4393 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004394 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4396 {
4397
4398 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004400 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 }
4402 break;
4403
4404 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405 if (expand_cmdline(&compl_xp, compl_pattern,
4406 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004408 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 break;
4410
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004411#ifdef FEAT_COMPL_FUNC
4412 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004413 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004414 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004415 break;
4416#endif
4417
Bram Moolenaar488c6512005-08-11 20:09:58 +00004418 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004419#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004420 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004421 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004422 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004423 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004424#endif
4425 break;
4426
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 default: /* normal ^P/^N and ^X^L */
4428 /*
4429 * If 'infercase' is set, don't use 'smartcase' here
4430 */
4431 save_p_scs = p_scs;
4432 if (ins_buf->b_p_inf)
4433 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004434
Bram Moolenaar361aa502014-01-23 22:45:58 +01004435 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 * end but never from the middle, thus setting nowrapscan in this
4437 * buffers is a good idea, on the other hand, we always set
4438 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4439 save_p_ws = p_ws;
4440 if (ins_buf != curbuf)
4441 p_ws = FALSE;
4442 else if (*e_cpt == '.')
4443 p_ws = TRUE;
4444 for (;;)
4445 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004446 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004448 ++msg_silent; /* Don't want messages for wrapscan. */
4449
Bram Moolenaare4214502015-03-05 18:08:43 +01004450 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4451 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004452 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004453 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004456 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004458 found_new_match = searchit(NULL, ins_buf, pos,
4459 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004461 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004462 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004463 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004465 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 first_match_pos = *pos;
4468 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004469 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 }
4471 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004472 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 found_new_match = FAIL;
4474 if (found_new_match == FAIL)
4475 {
4476 if (ins_buf == curbuf)
4477 found_all = TRUE;
4478 break;
4479 }
4480
4481 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 && ini->lnum == pos->lnum
4484 && ini->col == pos->col)
4485 continue;
4486 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004487 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
4491 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4492 continue;
4493 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4494 if (!p_paste)
4495 ptr = skipwhite(ptr);
4496 }
4497 len = (int)STRLEN(ptr);
4498 }
4499 else
4500 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004501 char_u *tmp_ptr = ptr;
4502
4503 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 /* Skip if already inside a word. */
4507 if (vim_iswordp(tmp_ptr))
4508 continue;
4509 /* Find start of next word. */
4510 tmp_ptr = find_word_start(tmp_ptr);
4511 }
4512 /* Find end of this word. */
4513 tmp_ptr = find_word_end(tmp_ptr);
4514 len = (int)(tmp_ptr - ptr);
4515
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004516 if ((compl_cont_status & CONT_ADDING)
4517 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 {
4519 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4520 {
4521 /* Try next line, if any. the new word will be
4522 * "join" as if the normal command "J" was used.
4523 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004524 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 * works -- Acevedo */
4526 STRNCPY(IObuff, ptr, len);
4527 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4528 tmp_ptr = ptr = skipwhite(ptr);
4529 /* Find start of next word. */
4530 tmp_ptr = find_word_start(tmp_ptr);
4531 /* Find end of next word. */
4532 tmp_ptr = find_word_end(tmp_ptr);
4533 if (tmp_ptr > ptr)
4534 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004535 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004537 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 IObuff[len++] = ' ';
4539 /* IObuf =~ "\k.* ", thus len >= 2 */
4540 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004541 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 || (vim_strchr(p_cpo, CPO_JOINSP)
4543 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004544 && (IObuff[len - 2] == '?'
4545 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 IObuff[len++] = ' ';
4547 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004548 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 if (tmp_ptr - ptr >= IOSIZE - len)
4550 tmp_ptr = ptr + IOSIZE - len - 1;
4551 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4552 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004553 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555 IObuff[len] = NUL;
4556 ptr = IObuff;
4557 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004558 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 continue;
4560 }
4561 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004562 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004563 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004564 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 {
4566 found_new_match = OK;
4567 break;
4568 }
4569 }
4570 p_scs = save_p_scs;
4571 p_ws = save_p_ws;
4572 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004573
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004574 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004575 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004576 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 found_new_match = OK;
4578
4579 /* break the loop for specialized modes (use 'complete' just for the
4580 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004581 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004582 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004583 {
4584 if (got_int)
4585 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004586 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004587 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004588 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004589
Bram Moolenaare4214502015-03-05 18:08:43 +01004590 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004591 || compl_interrupted)
4592 break;
4593 compl_started = TRUE;
4594 }
4595 else
4596 {
4597 /* Mark a buffer scanned when it has been scanned completely */
4598 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4599 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004601 compl_started = FALSE;
4602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004604 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
Bram Moolenaare4214502015-03-05 18:08:43 +01004606 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 && *e_cpt == NUL) /* Got to end of 'complete' */
4608 found_new_match = FAIL;
4609
4610 i = -1; /* total of matches, unknown */
4611 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004612 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 i = ins_compl_make_cyclic();
4614
4615 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004616 * just been made cyclic then we have to move compl_curr_match to the next
4617 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004618 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4619 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004620 if (compl_curr_match == NULL)
4621 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 return i;
4623}
4624
4625/* Delete the old text being completed. */
4626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004627ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628{
4629 int i;
4630
4631 /*
4632 * In insert mode: Delete the typed part.
4633 * In replace mode: Put the old characters back, if any.
4634 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004635 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 backspace_until_column(i);
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004637
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004638 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4639 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004641 /* clear v:completed_item */
4642 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643}
4644
4645/* Insert the new text being completed. */
4646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004647ins_compl_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004649 dict_T *dict;
4650
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004651 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004652 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4653 compl_used_match = FALSE;
4654 else
4655 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004656
4657 /* Set completed item. */
4658 /* { word, abbr, menu, kind, info } */
4659 dict = dict_alloc();
4660 if (dict != NULL)
4661 {
4662 dict_add_nr_str(dict, "word", 0L,
4663 EMPTY_IF_NULL(compl_shown_match->cp_str));
4664 dict_add_nr_str(dict, "abbr", 0L,
4665 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4666 dict_add_nr_str(dict, "menu", 0L,
4667 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4668 dict_add_nr_str(dict, "kind", 0L,
4669 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4670 dict_add_nr_str(dict, "info", 0L,
4671 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4672 }
4673 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674}
4675
4676/*
4677 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004678 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4679 * get more completions. If it is FALSE, then we just do nothing when there
4680 * are no more completions in a given direction. The latter case is used when
4681 * we are still in the middle of finding completions, to allow browsing
4682 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004683 * Return the total number of matches, or -1 if still unknown -- webb.
4684 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004685 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4686 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 *
4688 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004689 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4690 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 */
4692 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004693ins_compl_next(
4694 int allow_get_expansion,
4695 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004696 be at least 1 */
Bram Moolenaar7454a062016-01-30 15:14:10 +01004697 int insert_match) /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698{
4699 int num_matches = -1;
4700 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004701 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004702 compl_T *found_compl = NULL;
4703 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004704 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004705 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004706
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004707 /* When user complete function return -1 for findstart which is next
4708 * time of 'always', compl_shown_match become NULL. */
4709 if (compl_shown_match == NULL)
4710 return -1;
4711
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004712 if (compl_leader != NULL
4713 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004715 /* Set "compl_shown_match" to the actually shown match, it may differ
4716 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004717 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004718 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004719 && compl_shown_match->cp_next != NULL
4720 && compl_shown_match->cp_next != compl_first_match)
4721 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004722
4723 /* If we didn't find it searching forward, and compl_shows_dir is
4724 * backward, find the last match. */
4725 if (compl_shows_dir == BACKWARD
4726 && !ins_compl_equal(compl_shown_match,
4727 compl_leader, (int)STRLEN(compl_leader))
4728 && (compl_shown_match->cp_next == NULL
4729 || compl_shown_match->cp_next == compl_first_match))
4730 {
4731 while (!ins_compl_equal(compl_shown_match,
4732 compl_leader, (int)STRLEN(compl_leader))
4733 && compl_shown_match->cp_prev != NULL
4734 && compl_shown_match->cp_prev != compl_first_match)
4735 compl_shown_match = compl_shown_match->cp_prev;
4736 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004737 }
4738
4739 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004740 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 /* Delete old text to be replaced */
4742 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004743
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004744 /* When finding the longest common text we stick at the original text,
4745 * don't let CTRL-N or CTRL-P move to the first match. */
4746 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4747
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004748 /* When restarting the search don't insert the first match either. */
4749 if (compl_restarting)
4750 {
4751 advance = FALSE;
4752 compl_restarting = FALSE;
4753 }
4754
Bram Moolenaare3226be2005-12-18 22:10:00 +00004755 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4756 * around. */
4757 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004759 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004761 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004762 found_end = (compl_first_match != NULL
4763 && (compl_shown_match->cp_next == compl_first_match
4764 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004765 }
4766 else if (compl_shows_dir == BACKWARD
4767 && compl_shown_match->cp_prev != NULL)
4768 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004769 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004770 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004771 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
4773 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004774 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004775 if (!allow_get_expansion)
4776 {
4777 if (advance)
4778 {
4779 if (compl_shows_dir == BACKWARD)
4780 compl_pending -= todo + 1;
4781 else
4782 compl_pending += todo + 1;
4783 }
4784 return -1;
4785 }
4786
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004787 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004788 {
4789 if (compl_shows_dir == BACKWARD)
4790 --compl_pending;
4791 else
4792 ++compl_pending;
4793 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004794
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004795 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004796 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004797
4798 /* handle any pending completions */
4799 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004800 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004801 {
4802 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4803 {
4804 compl_shown_match = compl_shown_match->cp_next;
4805 --compl_pending;
4806 }
4807 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4808 {
4809 compl_shown_match = compl_shown_match->cp_prev;
4810 ++compl_pending;
4811 }
4812 else
4813 break;
4814 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004815 found_end = FALSE;
4816 }
4817 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4818 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004819 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004820 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004821 ++todo;
4822 else
4823 /* Remember a matching item. */
4824 found_compl = compl_shown_match;
4825
4826 /* Stop at the end of the list when we found a usable match. */
4827 if (found_end)
4828 {
4829 if (found_compl != NULL)
4830 {
4831 compl_shown_match = found_compl;
4832 break;
4833 }
4834 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004835 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
4837
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004838 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004839 if (compl_no_insert && !started)
4840 {
4841 ins_bytes(compl_orig_text + ins_compl_len());
4842 compl_used_match = FALSE;
4843 }
4844 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004845 {
4846 if (!compl_get_longest || compl_used_match)
4847 ins_compl_insert();
4848 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004849 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004850 }
4851 else
4852 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853
4854 if (!allow_get_expansion)
4855 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004856 /* may undisplay the popup menu first */
4857 ins_compl_upd_pum();
4858
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004859 /* redraw to show the user what was inserted */
4860 update_screen(0);
4861
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004862 /* display the updated popup menu */
4863 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004864#ifdef FEAT_GUI
4865 if (gui.in_use)
4866 {
4867 /* Show the cursor after the match, not after the redrawn text. */
4868 setcursor();
4869 out_flush();
4870 gui_update_cursor(FALSE, FALSE);
4871 }
4872#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004873
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 /* Delete old text to be replaced, since we're still searching and
4875 * don't want to match ourselves! */
4876 ins_compl_delete();
4877 }
4878
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004879 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004880 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004881 if (compl_no_insert && !started)
4882 compl_enter_selects = TRUE;
4883 else
4884 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004885
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 /*
4887 * Show the file name for the match (if any)
4888 * Truncate the file name to avoid a wait for return.
4889 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004890 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 {
4892 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004893 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 if (i <= 0)
4895 i = 0;
4896 else
4897 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004898 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 msg(IObuff);
4900 redraw_cmdline = FALSE; /* don't overwrite! */
4901 }
4902
4903 return num_matches;
4904}
4905
4906/*
4907 * Call this while finding completions, to check whether the user has hit a key
4908 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004909 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004911 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 */
4913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004914ins_compl_check_keys(int frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915{
4916 static int count = 0;
4917
4918 int c;
4919
4920 /* Don't check when reading keys from a script. That would break the test
4921 * scripts */
4922 if (using_script())
4923 return;
4924
4925 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004926 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 return;
4928 count = 0;
4929
Bram Moolenaara260a972006-06-23 19:36:29 +00004930 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4931 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 if (c != NUL)
4934 {
4935 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4936 {
4937 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004938 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004939 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4940 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004942 else
4943 {
4944 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004945 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004946 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004947 if (c != K_IGNORE)
4948 {
4949 /* Don't interrupt completion when the character wasn't typed,
4950 * e.g., when doing @q to replay keys. */
4951 if (c != Ctrl_R && KeyTyped)
4952 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004953
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004954 vungetc(c);
4955 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004956 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004958 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004959 {
4960 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4961
4962 compl_pending = 0;
4963 (void)ins_compl_next(FALSE, todo, TRUE);
4964 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004965}
4966
4967/*
4968 * Decide the direction of Insert mode complete from the key typed.
4969 * Returns BACKWARD or FORWARD.
4970 */
4971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004972ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004973{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004974 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02004975 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004976 return BACKWARD;
4977 return FORWARD;
4978}
4979
4980/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004981 * Return TRUE for keys that are used for completion only when the popup menu
4982 * is visible.
4983 */
4984 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004985ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004986{
4987 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004988 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4989 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004990}
4991
4992/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004993 * Decide the number of completions to move forward.
4994 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4995 */
4996 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004997ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004998{
4999 int h;
5000
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005001 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005002 {
5003 h = pum_get_height();
5004 if (h > 3)
5005 h -= 2; /* keep some context */
5006 return h;
5007 }
5008 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009}
5010
5011/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005012 * Return TRUE if completion with "c" should insert the match, FALSE if only
5013 * to change the currently selected completion.
5014 */
5015 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005016ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005017{
5018 switch (c)
5019 {
5020 case K_UP:
5021 case K_DOWN:
5022 case K_PAGEDOWN:
5023 case K_KPAGEDOWN:
5024 case K_S_DOWN:
5025 case K_PAGEUP:
5026 case K_KPAGEUP:
5027 case K_S_UP:
5028 return FALSE;
5029 }
5030 return TRUE;
5031}
5032
5033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 * Do Insert mode completion.
5035 * Called when character "c" was typed, which has a meaning for completion.
5036 * Returns OK if completion was done, FAIL if something failed (out of mem).
5037 */
5038 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005039ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005041 char_u *line;
5042 int startcol = 0; /* column where searched text starts */
5043 colnr_T curs_col; /* cursor column */
5044 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005045 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046
Bram Moolenaare3226be2005-12-18 22:10:00 +00005047 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005048 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 {
5050 /* First time we hit ^N or ^P (in a row, I mean) */
5051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 did_ai = FALSE;
5053#ifdef FEAT_SMARTINDENT
5054 did_si = FALSE;
5055 can_si = FALSE;
5056 can_si_back = FALSE;
5057#endif
5058 if (stop_arrow() == FAIL)
5059 return FAIL;
5060
5061 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005062 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005063 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005065 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005066 * "compl_startpos" to the cursor as a pattern to add a new word
5067 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005068 * "compl_startpos" is not in the same line as the cursor then fix it
5069 * (the line has been split because it was longer than 'tw'). if SOL
5070 * is set then skip the previous pattern, a word at the beginning of
5071 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005072 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5073 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
5075 /*
5076 * it is a continued search
5077 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005078 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5080 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5081 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005082 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 /* line (probably) wrapped, set compl_startpos to the
5085 * first non_blank in the line, if it is not a wordchar
5086 * include it to get a better pattern, but then we don't
5087 * want the "\\<" prefix, check it bellow */
5088 compl_col = (colnr_T)(skipwhite(line) - line);
5089 compl_startpos.col = compl_col;
5090 compl_startpos.lnum = curwin->w_cursor.lnum;
5091 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 else
5094 {
5095 /* S_IPOS was set when we inserted a word that was at the
5096 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005097 * mode but first we need to redefine compl_startpos */
5098 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005100 compl_cont_status |= CONT_SOL;
5101 compl_startpos.col = (colnr_T)(skipwhite(
5102 line + compl_length
5103 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005107 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005108 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005109 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005113 compl_cont_status &= ~CONT_SOL;
5114 compl_length = (IOSIZE - MIN_SPACE);
5115 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005117 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5118 if (compl_length < 1)
5119 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005121 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005122 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005124 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005127 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005131 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 compl_cont_status = 0;
5134 compl_cont_status |= CONT_N_ADDS;
5135 compl_startpos = curwin->w_cursor;
5136 startcol = (int)curs_col;
5137 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139
5140 /* Work out completion pattern and original text -- webb */
5141 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5142 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005143 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5145 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005146 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005148 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005150 compl_col += ++startcol;
5151 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154 compl_pattern = str_foldcase(line + compl_col,
5155 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 compl_pattern = vim_strnsave(line + compl_col,
5158 compl_length);
5159 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 return FAIL;
5161 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005162 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 char_u *prefix = (char_u *)"\\<";
5165
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005166 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005168 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 if (!vim_iswordp(line + compl_col)
5172 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 && (
5174#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#endif
5179 )))
5180 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005181 STRCPY((char *)compl_pattern, prefix);
5182 (void)quote_meta(compl_pattern + STRLEN(prefix),
5183 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005185 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190#endif
5191 )
5192 {
5193 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005194 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5195 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 compl_col += curs_col;
5198 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200 else
5201 {
5202#ifdef FEAT_MBYTE
5203 /* Search the point of change class of multibyte character
5204 * or not a word single byte character backward. */
5205 if (has_mbyte)
5206 {
5207 int base_class;
5208 int head_off;
5209
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005210 startcol -= (*mb_head_off)(line, line + startcol);
5211 base_class = mb_get_class(line + startcol);
5212 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005214 head_off = (*mb_head_off)(line, line + startcol);
5215 if (base_class != mb_get_class(line + startcol
5216 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
5220 }
5221 else
5222#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 compl_col += ++startcol;
5226 compl_length = (int)curs_col - startcol;
5227 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
5229 /* Only match word with at least two chars -- webb
5230 * there's no need to call quote_meta,
5231 * alloc(7) is enough -- Acevedo
5232 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005233 compl_pattern = alloc(7);
5234 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 STRCPY((char *)compl_pattern, "\\<");
5237 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5238 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
5240 else
5241 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005242 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005243 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 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,
5248 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250 }
5251 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005252 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005254 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005255 compl_length = (int)curs_col - (int)compl_col;
5256 if (compl_length < 0) /* cursor in indent: empty pattern */
5257 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005259 compl_pattern = str_foldcase(line + compl_col, compl_length,
5260 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005262 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5263 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 return FAIL;
5265 }
5266 else if (ctrl_x_mode == CTRL_X_FILES)
5267 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005268 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005269 if (startcol > 0)
5270 {
5271 char_u *p = line + startcol;
5272
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005273 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005274 while (p > line && vim_isfilec(PTR2CHAR(p)))
5275 mb_ptr_back(line, p);
5276 if (p == line && vim_isfilec(PTR2CHAR(p)))
5277 startcol = 0;
5278 else
5279 startcol = (int)(p - line) + 1;
5280 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005281
Bram Moolenaar0300e462013-09-08 16:03:45 +02005282 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 compl_length = (int)curs_col - startcol;
5284 compl_pattern = addstar(line + compl_col, compl_length,
5285 EXPAND_FILES);
5286 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 return FAIL;
5288 }
5289 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5290 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 compl_pattern = vim_strnsave(line, curs_col);
5292 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005294 set_cmd_context(&compl_xp, compl_pattern,
5295 (int)STRLEN(compl_pattern), curs_col);
5296 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5297 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005298 /* No completion possible, use an empty pattern to get a
5299 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005300 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005301 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005302 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5303 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005305 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005306 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005307#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005308 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005309 * Call user defined function 'completefunc' with "a:findstart"
5310 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005311 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005312 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005313 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005314 char_u *funcname;
5315 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005316 win_T *curwin_save;
5317 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005318
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005319 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005320 * string */
5321 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5322 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5323 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005324 {
5325 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5326 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005327 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005328 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005329
5330 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005331 args[1] = NULL;
5332 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005333 curwin_save = curwin;
5334 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005335 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005336 if (curwin_save != curwin || curbuf_save != curbuf)
5337 {
5338 EMSG(_(e_complwin));
5339 return FAIL;
5340 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005341 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005342 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005343 if (!equalpos(curwin->w_cursor, pos))
5344 {
5345 EMSG(_(e_compldel));
5346 return FAIL;
5347 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005348
Bram Moolenaar6110a002012-01-26 18:58:38 +01005349 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005350 * cancel the complete without an error.
5351 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005352 if (col == -2)
5353 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005354 if (col == -3)
5355 {
5356 ctrl_x_mode = 0;
5357 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005358 if (!shortmess(SHM_COMPLETIONMENU))
5359 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005360 return FAIL;
5361 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005362
Bram Moolenaar82139082011-09-14 16:52:09 +02005363 /*
5364 * Reset extended parameters of completion, when start new
5365 * completion.
5366 */
5367 compl_opt_refresh_always = FALSE;
5368
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005369 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005370 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005371 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005372 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005373 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 /* Setup variables for completion. Need to obtain "line" again,
5376 * it may have become invalid. */
5377 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005378 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005379 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5380 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005381#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 return FAIL;
5383 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005384 else if (ctrl_x_mode == CTRL_X_SPELL)
5385 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005386#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005387 if (spell_bad_len > 0)
5388 compl_col = curs_col - spell_bad_len;
5389 else
5390 compl_col = spell_word_start(startcol);
5391 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005392 {
5393 compl_length = 0;
5394 compl_col = curs_col;
5395 }
5396 else
5397 {
5398 spell_expand_check_cap(compl_col);
5399 compl_length = (int)curs_col - compl_col;
5400 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005401 /* Need to obtain "line" again, it may have become invalid. */
5402 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005403 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5404 if (compl_pattern == NULL)
5405#endif
5406 return FAIL;
5407 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005408 else
5409 {
5410 EMSG2(_(e_intern2), "ins_complete()");
5411 return FAIL;
5412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {
5416 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005417 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
5419 /* Insert a new line, keep indentation but ignore 'comments' */
5420#ifdef FEAT_COMMENTS
5421 char_u *old = curbuf->b_p_com;
5422
5423 curbuf->b_p_com = (char_u *)"";
5424#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005425 compl_startpos.lnum = curwin->w_cursor.lnum;
5426 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 ins_eol('\r');
5428#ifdef FEAT_COMMENTS
5429 curbuf->b_p_com = old;
5430#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005431 compl_length = 0;
5432 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
5434 }
5435 else
5436 {
5437 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005438 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 }
5440
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 if (compl_cont_status & CONT_LOCAL)
5442 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 else
5444 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5445
Bram Moolenaar62951b12011-09-21 18:23:05 +02005446 /* If any of the original typed text has been changed we need to fix
5447 * the redo buffer. */
5448 ins_compl_fixRedoBufForLeader(NULL);
5449
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005450 /* Always add completion for the original text. */
5451 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005452 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5453 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005454 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005456 vim_free(compl_pattern);
5457 compl_pattern = NULL;
5458 vim_free(compl_orig_text);
5459 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 return FAIL;
5461 }
5462
5463 /* showmode might reset the internal line pointers, so it must
5464 * be called before line = ml_get(), or when this address is no
5465 * longer needed. -- Acevedo.
5466 */
5467 edit_submode_extra = (char_u *)_("-- Searching...");
5468 edit_submode_highl = HLF_COUNT;
5469 showmode();
5470 edit_submode_extra = NULL;
5471 out_flush();
5472 }
5473
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005474 compl_shown_match = compl_curr_match;
5475 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005478 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005480 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005481 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005483 /* may undisplay the popup menu */
5484 ins_compl_upd_pum();
5485
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005486 if (n > 1) /* all matches have been found */
5487 compl_matches = n;
5488 compl_curr_match = compl_shown_match;
5489 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Bram Moolenaard68071d2006-05-02 22:08:30 +00005491 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5492 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 if (got_int && !global_busy)
5494 {
5495 (void)vgetc();
5496 got_int = FALSE;
5497 }
5498
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005499 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005500 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005502 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5503 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5505 edit_submode_highl = HLF_E;
5506 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5507 * because we couldn't expand anything at first place, but if we used
5508 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5509 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005510 if ( compl_length > 1
5511 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 || (ctrl_x_mode != 0
5513 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5514 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
5517
Bram Moolenaar572cb562005-08-05 21:35:02 +00005518 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005519 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005521 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
5523 if (edit_submode_extra == NULL)
5524 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005525 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
5527 edit_submode_extra = (char_u *)_("Back at original");
5528 edit_submode_highl = HLF_W;
5529 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005530 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 edit_submode_extra = (char_u *)_("Word from other line");
5533 edit_submode_highl = HLF_COUNT;
5534 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005535 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 edit_submode_extra = (char_u *)_("The only match");
5538 edit_submode_highl = HLF_COUNT;
5539 }
5540 else
5541 {
5542 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005543 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005545 int number = 0;
5546 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005548 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
5550 /* search backwards for the first valid (!= -1) number.
5551 * This should normally succeed already at the first loop
5552 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005553 for (match = compl_curr_match->cp_prev; match != NULL
5554 && match != compl_first_match;
5555 match = match->cp_prev)
5556 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005558 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 break;
5560 }
5561 if (match != NULL)
5562 /* go up and assign all numbers which are not assigned
5563 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005564 for (match = match->cp_next;
5565 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005566 match = match->cp_next)
5567 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 else /* BACKWARD */
5570 {
5571 /* search forwards (upwards) for the first valid (!= -1)
5572 * number. This should normally succeed already at the
5573 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005574 for (match = compl_curr_match->cp_next; match != NULL
5575 && match != compl_first_match;
5576 match = match->cp_next)
5577 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005579 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 break;
5581 }
5582 if (match != NULL)
5583 /* go down and assign all numbers which are not
5584 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005585 for (match = match->cp_prev; match
5586 && match->cp_number == -1;
5587 match = match->cp_prev)
5588 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
5590 }
5591
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005592 /* The match should always have a sequence number now, this is
5593 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005594 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005596 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5597 * Translations may need more than twice that. */
5598 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005600 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005601 vim_snprintf((char *)match_ref, sizeof(match_ref),
5602 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005603 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005605 vim_snprintf((char *)match_ref, sizeof(match_ref),
5606 _("match %d"),
5607 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 edit_submode_extra = match_ref;
5609 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005610 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 curs_columns(FALSE);
5612 }
5613 }
5614 }
5615
5616 /* Show a message about what (completion) mode we're in. */
5617 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005618 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005620 if (edit_submode_extra != NULL)
5621 {
5622 if (!p_smd)
5623 msg_attr(edit_submode_extra,
5624 edit_submode_highl < HLF_COUNT
5625 ? hl_attr(edit_submode_highl) : 0);
5626 }
5627 else
5628 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
Bram Moolenaard68071d2006-05-02 22:08:30 +00005631 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005632 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005633 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005634 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005635 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005636 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005637 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 return OK;
5640}
5641
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005642 static void
5643show_pum(int save_w_wrow)
5644{
5645 /* RedrawingDisabled may be set when invoked through complete(). */
5646 int n = RedrawingDisabled;
5647
5648 RedrawingDisabled = 0;
5649
5650 /* If the cursor moved we need to remove the pum first. */
5651 setcursor();
5652 if (save_w_wrow != curwin->w_wrow)
5653 ins_compl_del_pum();
5654
5655 ins_compl_show_pum();
5656 setcursor();
5657 RedrawingDisabled = n;
5658}
5659
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660/*
5661 * Looks in the first "len" chars. of "src" for search-metachars.
5662 * If dest is not NULL the chars. are copied there quoting (with
5663 * a backslash) the metachars, and dest would be NUL terminated.
5664 * Returns the length (needed) of dest
5665 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005666 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005667quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005669 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005671 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005672 {
5673 switch (*src)
5674 {
5675 case '.':
5676 case '*':
5677 case '[':
5678 if (ctrl_x_mode == CTRL_X_DICTIONARY
5679 || ctrl_x_mode == CTRL_X_THESAURUS)
5680 break;
5681 case '~':
5682 if (!p_magic) /* quote these only if magic is set */
5683 break;
5684 case '\\':
5685 if (ctrl_x_mode == CTRL_X_DICTIONARY
5686 || ctrl_x_mode == CTRL_X_THESAURUS)
5687 break;
5688 case '^': /* currently it's not needed. */
5689 case '$':
5690 m++;
5691 if (dest != NULL)
5692 *dest++ = '\\';
5693 break;
5694 }
5695 if (dest != NULL)
5696 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005697# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 /* Copy remaining bytes of a multibyte character. */
5699 if (has_mbyte)
5700 {
5701 int i, mb_len;
5702
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005703 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 if (mb_len > 0 && len >= mb_len)
5705 for (i = 0; i < mb_len; ++i)
5706 {
5707 --len;
5708 ++src;
5709 if (dest != NULL)
5710 *dest++ = *src;
5711 }
5712 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005713# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
5715 if (dest != NULL)
5716 *dest = NUL;
5717
5718 return m;
5719}
5720#endif /* FEAT_INS_EXPAND */
5721
5722/*
5723 * Next character is interpreted literally.
5724 * A one, two or three digit decimal number is interpreted as its byte value.
5725 * If one or two digits are entered, the next character is given to vungetc().
5726 * For Unicode a character > 255 may be returned.
5727 */
5728 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005729get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730{
5731 int cc;
5732 int nc;
5733 int i;
5734 int hex = FALSE;
5735 int octal = FALSE;
5736#ifdef FEAT_MBYTE
5737 int unicode = 0;
5738#endif
5739
5740 if (got_int)
5741 return Ctrl_C;
5742
5743#ifdef FEAT_GUI
5744 /*
5745 * In GUI there is no point inserting the internal code for a special key.
5746 * It is more useful to insert the string "<KEY>" instead. This would
5747 * probably be useful in a text window too, but it would not be
5748 * vi-compatible (maybe there should be an option for it?) -- webb
5749 */
5750 if (gui.in_use)
5751 ++allow_keys;
5752#endif
5753#ifdef USE_ON_FLY_SCROLL
5754 dont_scroll = TRUE; /* disallow scrolling here */
5755#endif
5756 ++no_mapping; /* don't map the next key hits */
5757 cc = 0;
5758 i = 0;
5759 for (;;)
5760 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005761 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762#ifdef FEAT_CMDL_INFO
5763 if (!(State & CMDLINE)
5764# ifdef FEAT_MBYTE
5765 && MB_BYTE2LEN_CHECK(nc) == 1
5766# endif
5767 )
5768 add_to_showcmd(nc);
5769#endif
5770 if (nc == 'x' || nc == 'X')
5771 hex = TRUE;
5772 else if (nc == 'o' || nc == 'O')
5773 octal = TRUE;
5774#ifdef FEAT_MBYTE
5775 else if (nc == 'u' || nc == 'U')
5776 unicode = nc;
5777#endif
5778 else
5779 {
5780 if (hex
5781#ifdef FEAT_MBYTE
5782 || unicode != 0
5783#endif
5784 )
5785 {
5786 if (!vim_isxdigit(nc))
5787 break;
5788 cc = cc * 16 + hex2nr(nc);
5789 }
5790 else if (octal)
5791 {
5792 if (nc < '0' || nc > '7')
5793 break;
5794 cc = cc * 8 + nc - '0';
5795 }
5796 else
5797 {
5798 if (!VIM_ISDIGIT(nc))
5799 break;
5800 cc = cc * 10 + nc - '0';
5801 }
5802
5803 ++i;
5804 }
5805
5806 if (cc > 255
5807#ifdef FEAT_MBYTE
5808 && unicode == 0
5809#endif
5810 )
5811 cc = 255; /* limit range to 0-255 */
5812 nc = 0;
5813
5814 if (hex) /* hex: up to two chars */
5815 {
5816 if (i >= 2)
5817 break;
5818 }
5819#ifdef FEAT_MBYTE
5820 else if (unicode) /* Unicode: up to four or eight chars */
5821 {
5822 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5823 break;
5824 }
5825#endif
5826 else if (i >= 3) /* decimal or octal: up to three chars */
5827 break;
5828 }
5829 if (i == 0) /* no number entered */
5830 {
5831 if (nc == K_ZERO) /* NUL is stored as NL */
5832 {
5833 cc = '\n';
5834 nc = 0;
5835 }
5836 else
5837 {
5838 cc = nc;
5839 nc = 0;
5840 }
5841 }
5842
5843 if (cc == 0) /* NUL is stored as NL */
5844 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005845#ifdef FEAT_MBYTE
5846 if (enc_dbcs && (cc & 0xff) == 0)
5847 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5848 second byte will cause trouble! */
5849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850
5851 --no_mapping;
5852#ifdef FEAT_GUI
5853 if (gui.in_use)
5854 --allow_keys;
5855#endif
5856 if (nc)
5857 vungetc(nc);
5858 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5859 return cc;
5860}
5861
5862/*
5863 * Insert character, taking care of special keys and mod_mask
5864 */
5865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005866insert_special(
5867 int c,
5868 int allow_modmask,
5869 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870{
5871 char_u *p;
5872 int len;
5873
5874 /*
5875 * Special function key, translate into "<Key>". Up to the last '>' is
5876 * inserted with ins_str(), so as not to replace characters in replace
5877 * mode.
5878 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5879 * unless 'allow_modmask' is TRUE.
5880 */
5881#ifdef MACOS
5882 /* Command-key never produces a normal key */
5883 if (mod_mask & MOD_MASK_CMD)
5884 allow_modmask = TRUE;
5885#endif
5886 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5887 {
5888 p = get_special_key_name(c, mod_mask);
5889 len = (int)STRLEN(p);
5890 c = p[len - 1];
5891 if (len > 2)
5892 {
5893 if (stop_arrow() == FAIL)
5894 return;
5895 p[len - 1] = NUL;
5896 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005897 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005898 ctrlv = FALSE;
5899 }
5900 }
5901 if (stop_arrow() == OK)
5902 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5903}
5904
5905/*
5906 * Special characters in this context are those that need processing other
5907 * than the simple insertion that can be performed here. This includes ESC
5908 * which terminates the insert, and CR/NL which need special processing to
5909 * open up a new line. This routine tries to optimize insertions performed by
5910 * the "redo", "undo" or "put" commands, so it needs to know when it should
5911 * stop and defer processing to the "normal" mechanism.
5912 * '0' and '^' are special, because they can be followed by CTRL-D.
5913 */
5914#ifdef EBCDIC
5915# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5916#else
5917# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5918#endif
5919
5920#ifdef FEAT_MBYTE
5921# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5922#else
5923# define WHITECHAR(cc) vim_iswhite(cc)
5924#endif
5925
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005926/*
5927 * "flags": INSCHAR_FORMAT - force formatting
5928 * INSCHAR_CTRLV - char typed just after CTRL-V
5929 * INSCHAR_NO_FEX - don't use 'formatexpr'
5930 *
5931 * NOTE: passes the flags value straight through to internal_format() which,
5932 * beside INSCHAR_FORMAT (above), is also looking for these:
5933 * INSCHAR_DO_COM - format comments
5934 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5935 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005937insertchar(
5938 int c, /* character to insert or NUL */
5939 int flags, /* INSCHAR_FORMAT, etc. */
5940 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 int textwidth;
5943#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005947 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005949 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951
5952 /*
5953 * Try to break the line in two or more pieces when:
5954 * - Always do this if we have been called to do formatting only.
5955 * - Always do this when 'formatoptions' has the 'a' flag and the line
5956 * ends in white space.
5957 * - Otherwise:
5958 * - Don't do this if inserting a blank
5959 * - Don't do this if an existing character is being replaced, unless
5960 * we're in VREPLACE mode.
5961 * - Do this if the cursor is not on the line where insert started
5962 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5963 * before the insert.
5964 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5965 * before 'textwidth'
5966 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005967 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005968 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005969 || (!vim_iswhite(c)
5970 && !((State & REPLACE_FLAG)
5971#ifdef FEAT_VREPLACE
5972 && !(State & VREPLACE_FLAG)
5973#endif
5974 && *ml_get_cursor() != NUL)
5975 && (curwin->w_cursor.lnum != Insstart.lnum
5976 || ((!has_format_option(FO_INS_LONG)
5977 || Insstart_textlen <= (colnr_T)textwidth)
5978 && (!fo_ins_blank
5979 || Insstart_blank_vcol <= (colnr_T)textwidth
5980 ))))))
5981 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005982 /* Format with 'formatexpr' when it's set. Use internal formatting
5983 * when 'formatexpr' isn't set or it returns non-zero. */
5984#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005985 int do_internal = TRUE;
5986 colnr_T virtcol = get_nolist_virtcol()
5987 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005988
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005989 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
5990 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005991 {
5992 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5993 /* It may be required to save for undo again, e.g. when setline()
5994 * was called. */
5995 ins_need_undo = TRUE;
5996 }
5997 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005999 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006001
Bram Moolenaar071d4272004-06-13 20:20:40 +00006002 if (c == NUL) /* only formatting was wanted */
6003 return;
6004
6005#ifdef FEAT_COMMENTS
6006 /* Check whether this character should end a comment. */
6007 if (did_ai && (int)c == end_comment_pending)
6008 {
6009 char_u *line;
6010 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6011 int middle_len, end_len;
6012 int i;
6013
6014 /*
6015 * Need to remove existing (middle) comment leader and insert end
6016 * comment leader. First, check what comment leader we can find.
6017 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006018 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006019 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6020 {
6021 /* Skip middle-comment string */
6022 while (*p && p[-1] != ':') /* find end of middle flags */
6023 ++p;
6024 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6025 /* Don't count trailing white space for middle_len */
6026 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6027 --middle_len;
6028
6029 /* Find the end-comment string */
6030 while (*p && p[-1] != ':') /* find end of end flags */
6031 ++p;
6032 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6033
6034 /* Skip white space before the cursor */
6035 i = curwin->w_cursor.col;
6036 while (--i >= 0 && vim_iswhite(line[i]))
6037 ;
6038 i++;
6039
6040 /* Skip to before the middle leader */
6041 i -= middle_len;
6042
6043 /* Check some expected things before we go on */
6044 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6045 {
6046 /* Backspace over all the stuff we want to replace */
6047 backspace_until_column(i);
6048
6049 /*
6050 * Insert the end-comment string, except for the last
6051 * character, which will get inserted as normal later.
6052 */
6053 ins_bytes_len(lead_end, end_len - 1);
6054 }
6055 }
6056 }
6057 end_comment_pending = NUL;
6058#endif
6059
6060 did_ai = FALSE;
6061#ifdef FEAT_SMARTINDENT
6062 did_si = FALSE;
6063 can_si = FALSE;
6064 can_si_back = FALSE;
6065#endif
6066
6067 /*
6068 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6069 * This speeds up normal text input considerably.
6070 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6071 * need to re-indent at a ':', or any other character (but not what
6072 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006073 * Don't do this when there an InsertCharPre autocommand is defined,
6074 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 */
6076#ifdef USE_ON_FLY_SCROLL
6077 dont_scroll = FALSE; /* allow scrolling here */
6078#endif
6079
6080 if ( !ISSPECIAL(c)
6081#ifdef FEAT_MBYTE
6082 && (!has_mbyte || (*mb_char2len)(c) == 1)
6083#endif
6084 && vpeekc() != NUL
6085 && !(State & REPLACE_FLAG)
6086#ifdef FEAT_CINDENT
6087 && !cindent_on()
6088#endif
6089#ifdef FEAT_RIGHTLEFT
6090 && !p_ri
6091#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006092#ifdef FEAT_AUTOCMD
6093 && !has_insertcharpre()
6094#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 )
6096 {
6097#define INPUT_BUFLEN 100
6098 char_u buf[INPUT_BUFLEN + 1];
6099 int i;
6100 colnr_T virtcol = 0;
6101
6102 buf[0] = c;
6103 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006104 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006105 virtcol = get_nolist_virtcol();
6106 /*
6107 * Stop the string when:
6108 * - no more chars available
6109 * - finding a special character (command key)
6110 * - buffer is full
6111 * - running into the 'textwidth' boundary
6112 * - need to check for abbreviation: A non-word char after a word-char
6113 */
6114 while ( (c = vpeekc()) != NUL
6115 && !ISSPECIAL(c)
6116#ifdef FEAT_MBYTE
6117 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6118#endif
6119 && i < INPUT_BUFLEN
6120 && (textwidth == 0
6121 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6122 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6123 {
6124#ifdef FEAT_RIGHTLEFT
6125 c = vgetc();
6126 if (p_hkmap && KeyTyped)
6127 c = hkmap(c); /* Hebrew mode mapping */
6128# ifdef FEAT_FKMAP
6129 if (p_fkmap && KeyTyped)
6130 c = fkmap(c); /* Farsi mode mapping */
6131# endif
6132 buf[i++] = c;
6133#else
6134 buf[i++] = vgetc();
6135#endif
6136 }
6137
6138#ifdef FEAT_DIGRAPHS
6139 do_digraph(-1); /* clear digraphs */
6140 do_digraph(buf[i-1]); /* may be the start of a digraph */
6141#endif
6142 buf[i] = NUL;
6143 ins_str(buf);
6144 if (flags & INSCHAR_CTRLV)
6145 {
6146 redo_literal(*buf);
6147 i = 1;
6148 }
6149 else
6150 i = 0;
6151 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006152 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006153 }
6154 else
6155 {
6156#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006157 int cc;
6158
Bram Moolenaar071d4272004-06-13 20:20:40 +00006159 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6160 {
6161 char_u buf[MB_MAXBYTES + 1];
6162
6163 (*mb_char2bytes)(c, buf);
6164 buf[cc] = NUL;
6165 ins_char_bytes(buf, cc);
6166 AppendCharToRedobuff(c);
6167 }
6168 else
6169#endif
6170 {
6171 ins_char(c);
6172 if (flags & INSCHAR_CTRLV)
6173 redo_literal(c);
6174 else
6175 AppendCharToRedobuff(c);
6176 }
6177 }
6178}
6179
6180/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006181 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006182 *
6183 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6184 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006185 */
6186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006187internal_format(
6188 int textwidth,
6189 int second_indent,
6190 int flags,
6191 int format_only,
6192 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006193{
6194 int cc;
6195 int save_char = NUL;
6196 int haveto_redraw = FALSE;
6197 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6198#ifdef FEAT_MBYTE
6199 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6200#endif
6201 int fo_white_par = has_format_option(FO_WHITE_PAR);
6202 int first_line = TRUE;
6203#ifdef FEAT_COMMENTS
6204 colnr_T leader_len;
6205 int no_leader = FALSE;
6206 int do_comments = (flags & INSCHAR_DO_COM);
6207#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006208#ifdef FEAT_LINEBREAK
6209 int has_lbr = curwin->w_p_lbr;
6210
6211 /* make sure win_lbr_chartabsize() counts correctly */
6212 curwin->w_p_lbr = FALSE;
6213#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214
6215 /*
6216 * When 'ai' is off we don't want a space under the cursor to be
6217 * deleted. Replace it with an 'x' temporarily.
6218 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006219 if (!curbuf->b_p_ai
6220#ifdef FEAT_VREPLACE
6221 && !(State & VREPLACE_FLAG)
6222#endif
6223 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006224 {
6225 cc = gchar_cursor();
6226 if (vim_iswhite(cc))
6227 {
6228 save_char = cc;
6229 pchar_cursor('x');
6230 }
6231 }
6232
6233 /*
6234 * Repeat breaking lines, until the current line is not too long.
6235 */
6236 while (!got_int)
6237 {
6238 int startcol; /* Cursor column at entry */
6239 int wantcol; /* column at textwidth border */
6240 int foundcol; /* column for start of spaces */
6241 int end_foundcol = 0; /* column for start of word */
6242 colnr_T len;
6243 colnr_T virtcol;
6244#ifdef FEAT_VREPLACE
6245 int orig_col = 0;
6246 char_u *saved_text = NULL;
6247#endif
6248 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006249 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006250
Bram Moolenaar97b98102009-11-17 16:41:01 +00006251 virtcol = get_nolist_virtcol()
6252 + char2cells(c != NUL ? c : gchar_cursor());
6253 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006254 break;
6255
6256#ifdef FEAT_COMMENTS
6257 if (no_leader)
6258 do_comments = FALSE;
6259 else if (!(flags & INSCHAR_FORMAT)
6260 && has_format_option(FO_WRAP_COMS))
6261 do_comments = TRUE;
6262
6263 /* Don't break until after the comment leader */
6264 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006265 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006266 else
6267 leader_len = 0;
6268
6269 /* If the line doesn't start with a comment leader, then don't
6270 * start one in a following broken line. Avoids that a %word
6271 * moved to the start of the next line causes all following lines
6272 * to start with %. */
6273 if (leader_len == 0)
6274 no_leader = TRUE;
6275#endif
6276 if (!(flags & INSCHAR_FORMAT)
6277#ifdef FEAT_COMMENTS
6278 && leader_len == 0
6279#endif
6280 && !has_format_option(FO_WRAP))
6281
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006283 if ((startcol = curwin->w_cursor.col) == 0)
6284 break;
6285
6286 /* find column of textwidth border */
6287 coladvance((colnr_T)textwidth);
6288 wantcol = curwin->w_cursor.col;
6289
Bram Moolenaar97b98102009-11-17 16:41:01 +00006290 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006291 foundcol = 0;
6292
6293 /*
6294 * Find position to break at.
6295 * Stop at first entered white when 'formatoptions' has 'v'
6296 */
6297 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006298 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006299 || curwin->w_cursor.lnum != Insstart.lnum
6300 || curwin->w_cursor.col >= Insstart.col)
6301 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006302 if (curwin->w_cursor.col == startcol && c != NUL)
6303 cc = c;
6304 else
6305 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006306 if (WHITECHAR(cc))
6307 {
6308 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006309 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006310
6311 /* find start of sequence of blanks */
6312 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6313 {
6314 dec_cursor();
6315 cc = gchar_cursor();
6316 }
6317 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6318 break; /* only spaces in front of text */
6319#ifdef FEAT_COMMENTS
6320 /* Don't break until after the comment leader */
6321 if (curwin->w_cursor.col < leader_len)
6322 break;
6323#endif
6324 if (has_format_option(FO_ONE_LETTER))
6325 {
6326 /* do not break after one-letter words */
6327 if (curwin->w_cursor.col == 0)
6328 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006329#ifdef FEAT_COMMENTS
6330 /* do not break "#a b" when 'tw' is 2 */
6331 if (curwin->w_cursor.col <= leader_len)
6332 break;
6333#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006334 col = curwin->w_cursor.col;
6335 dec_cursor();
6336 cc = gchar_cursor();
6337
6338 if (WHITECHAR(cc))
6339 continue; /* one-letter, continue */
6340 curwin->w_cursor.col = col;
6341 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006342
6343 inc_cursor();
6344
6345 end_foundcol = end_col + 1;
6346 foundcol = curwin->w_cursor.col;
6347 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006348 break;
6349 }
6350#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006351 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006352 {
6353 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006354 if (curwin->w_cursor.col != startcol)
6355 {
6356#ifdef FEAT_COMMENTS
6357 /* Don't break until after the comment leader */
6358 if (curwin->w_cursor.col < leader_len)
6359 break;
6360#endif
6361 col = curwin->w_cursor.col;
6362 inc_cursor();
6363 /* Don't change end_foundcol if already set. */
6364 if (foundcol != curwin->w_cursor.col)
6365 {
6366 foundcol = curwin->w_cursor.col;
6367 end_foundcol = foundcol;
6368 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6369 break;
6370 }
6371 curwin->w_cursor.col = col;
6372 }
6373
6374 if (curwin->w_cursor.col == 0)
6375 break;
6376
6377 col = curwin->w_cursor.col;
6378
6379 dec_cursor();
6380 cc = gchar_cursor();
6381
6382 if (WHITECHAR(cc))
6383 continue; /* break with space */
6384#ifdef FEAT_COMMENTS
6385 /* Don't break until after the comment leader */
6386 if (curwin->w_cursor.col < leader_len)
6387 break;
6388#endif
6389
6390 curwin->w_cursor.col = col;
6391
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006392 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006393 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006394 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6395 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006396 }
6397#endif
6398 if (curwin->w_cursor.col == 0)
6399 break;
6400 dec_cursor();
6401 }
6402
6403 if (foundcol == 0) /* no spaces, cannot break line */
6404 {
6405 curwin->w_cursor.col = startcol;
6406 break;
6407 }
6408
6409 /* Going to break the line, remove any "$" now. */
6410 undisplay_dollar();
6411
6412 /*
6413 * Offset between cursor position and line break is used by replace
6414 * stack functions. VREPLACE does not use this, and backspaces
6415 * over the text instead.
6416 */
6417#ifdef FEAT_VREPLACE
6418 if (State & VREPLACE_FLAG)
6419 orig_col = startcol; /* Will start backspacing from here */
6420 else
6421#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006422 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006423
6424 /*
6425 * adjust startcol for spaces that will be deleted and
6426 * characters that will remain on top line
6427 */
6428 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006429 while ((cc = gchar_cursor(), WHITECHAR(cc))
6430 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006431 inc_cursor();
6432 startcol -= curwin->w_cursor.col;
6433 if (startcol < 0)
6434 startcol = 0;
6435
6436#ifdef FEAT_VREPLACE
6437 if (State & VREPLACE_FLAG)
6438 {
6439 /*
6440 * In VREPLACE mode, we will backspace over the text to be
6441 * wrapped, so save a copy now to put on the next line.
6442 */
6443 saved_text = vim_strsave(ml_get_cursor());
6444 curwin->w_cursor.col = orig_col;
6445 if (saved_text == NULL)
6446 break; /* Can't do it, out of memory */
6447 saved_text[startcol] = NUL;
6448
6449 /* Backspace over characters that will move to the next line */
6450 if (!fo_white_par)
6451 backspace_until_column(foundcol);
6452 }
6453 else
6454#endif
6455 {
6456 /* put cursor after pos. to break line */
6457 if (!fo_white_par)
6458 curwin->w_cursor.col = foundcol;
6459 }
6460
6461 /*
6462 * Split the line just before the margin.
6463 * Only insert/delete lines, but don't really redraw the window.
6464 */
6465 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6466 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6467#ifdef FEAT_COMMENTS
6468 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006469 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006470#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006471 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6472 if (!(flags & INSCHAR_COM_LIST))
6473 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006474
6475 replace_offset = 0;
6476 if (first_line)
6477 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006478 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006480 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006481 * This section is for auto-wrap of numeric lists. When not
6482 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6483 * flag will be set and open_line() will handle it (as seen
6484 * above). The code here (and in get_number_indent()) will
6485 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006486 */
6487 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006488 second_indent =
6489 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006490 if (second_indent >= 0)
6491 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006492#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006493 if (State & VREPLACE_FLAG)
6494 change_indent(INDENT_SET, second_indent,
6495 FALSE, NUL, TRUE);
6496 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006497#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006498#ifdef FEAT_COMMENTS
6499 if (leader_len > 0 && second_indent - leader_len > 0)
6500 {
6501 int i;
6502 int padding = second_indent - leader_len;
6503
6504 /* We started at the first_line of a numbered list
6505 * that has a comment. the open_line() function has
6506 * inserted the proper comment leader and positioned
6507 * the cursor at the end of the split line. Now we
6508 * add the additional whitespace needed after the
6509 * comment leader for the numbered list. */
6510 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006511 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006512 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006513 }
6514 else
6515 {
6516#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006517 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006518#ifdef FEAT_COMMENTS
6519 }
6520#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006521 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006522 }
6523 first_line = FALSE;
6524 }
6525
6526#ifdef FEAT_VREPLACE
6527 if (State & VREPLACE_FLAG)
6528 {
6529 /*
6530 * In VREPLACE mode we have backspaced over the text to be
6531 * moved, now we re-insert it into the new line.
6532 */
6533 ins_bytes(saved_text);
6534 vim_free(saved_text);
6535 }
6536 else
6537#endif
6538 {
6539 /*
6540 * Check if cursor is not past the NUL off the line, cindent
6541 * may have added or removed indent.
6542 */
6543 curwin->w_cursor.col += startcol;
6544 len = (colnr_T)STRLEN(ml_get_curline());
6545 if (curwin->w_cursor.col > len)
6546 curwin->w_cursor.col = len;
6547 }
6548
6549 haveto_redraw = TRUE;
6550#ifdef FEAT_CINDENT
6551 can_cindent = TRUE;
6552#endif
6553 /* moved the cursor, don't autoindent or cindent now */
6554 did_ai = FALSE;
6555#ifdef FEAT_SMARTINDENT
6556 did_si = FALSE;
6557 can_si = FALSE;
6558 can_si_back = FALSE;
6559#endif
6560 line_breakcheck();
6561 }
6562
6563 if (save_char != NUL) /* put back space after cursor */
6564 pchar_cursor(save_char);
6565
Bram Moolenaar0026d472014-09-09 16:32:39 +02006566#ifdef FEAT_LINEBREAK
6567 curwin->w_p_lbr = has_lbr;
6568#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006569 if (!format_only && haveto_redraw)
6570 {
6571 update_topline();
6572 redraw_curbuf_later(VALID);
6573 }
6574}
6575
6576/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 * Called after inserting or deleting text: When 'formatoptions' includes the
6578 * 'a' flag format from the current line until the end of the paragraph.
6579 * Keep the cursor at the same position relative to the text.
6580 * The caller must have saved the cursor line for undo, following ones will be
6581 * saved here.
6582 */
6583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006584auto_format(
6585 int trailblank, /* when TRUE also format with trailing blank */
6586 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587{
6588 pos_T pos;
6589 colnr_T len;
6590 char_u *old;
6591 char_u *new, *pnew;
6592 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006593 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006594
6595 if (!has_format_option(FO_AUTO))
6596 return;
6597
6598 pos = curwin->w_cursor;
6599 old = ml_get_curline();
6600
6601 /* may remove added space */
6602 check_auto_format(FALSE);
6603
6604 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6605 * user might insert normal text next. Also skip formatting when "1" is
6606 * in 'formatoptions' and there is a single character before the cursor.
6607 * Otherwise the line would be broken and when typing another non-white
6608 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006609 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 if (*old != NUL && !trailblank && wasatend)
6611 {
6612 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006613 cc = gchar_cursor();
6614 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6615 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006616 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006617 cc = gchar_cursor();
6618 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 {
6620 curwin->w_cursor = pos;
6621 return;
6622 }
6623 curwin->w_cursor = pos;
6624 }
6625
6626#ifdef FEAT_COMMENTS
6627 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6628 * comments. */
6629 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006630 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631 return;
6632#endif
6633
6634 /*
6635 * May start formatting in a previous line, so that after "x" a word is
6636 * moved to the previous line if it fits there now. Only when this is not
6637 * the start of a paragraph.
6638 */
6639 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6640 {
6641 --curwin->w_cursor.lnum;
6642 if (u_save_cursor() == FAIL)
6643 return;
6644 }
6645
6646 /*
6647 * Do the formatting and restore the cursor position. "saved_cursor" will
6648 * be adjusted for the text formatting.
6649 */
6650 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006651 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 curwin->w_cursor = saved_cursor;
6653 saved_cursor.lnum = 0;
6654
6655 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6656 {
6657 /* "cannot happen" */
6658 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6659 coladvance((colnr_T)MAXCOL);
6660 }
6661 else
6662 check_cursor_col();
6663
6664 /* Insert mode: If the cursor is now after the end of the line while it
6665 * previously wasn't, the line was broken. Because of the rule above we
6666 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6667 * formatted. */
6668 if (!wasatend && has_format_option(FO_WHITE_PAR))
6669 {
6670 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006671 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 if (curwin->w_cursor.col == len)
6673 {
6674 pnew = vim_strnsave(new, len + 2);
6675 pnew[len] = ' ';
6676 pnew[len + 1] = NUL;
6677 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6678 /* remove the space later */
6679 did_add_space = TRUE;
6680 }
6681 else
6682 /* may remove added space */
6683 check_auto_format(FALSE);
6684 }
6685
6686 check_cursor();
6687}
6688
6689/*
6690 * When an extra space was added to continue a paragraph for auto-formatting,
6691 * delete it now. The space must be under the cursor, just after the insert
6692 * position.
6693 */
6694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006695check_auto_format(
6696 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006697{
6698 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006699 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700
6701 if (did_add_space)
6702 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006703 cc = gchar_cursor();
6704 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 /* Somehow the space was removed already. */
6706 did_add_space = FALSE;
6707 else
6708 {
6709 if (!end_insert)
6710 {
6711 inc_cursor();
6712 c = gchar_cursor();
6713 dec_cursor();
6714 }
6715 if (c != NUL)
6716 {
6717 /* The space is no longer at the end of the line, delete it. */
6718 del_char(FALSE);
6719 did_add_space = FALSE;
6720 }
6721 }
6722 }
6723}
6724
6725/*
6726 * Find out textwidth to be used for formatting:
6727 * if 'textwidth' option is set, use it
6728 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6729 * if invalid value, use 0.
6730 * Set default to window width (maximum 79) for "gq" operator.
6731 */
6732 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006733comp_textwidth(
6734 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735{
6736 int textwidth;
6737
6738 textwidth = curbuf->b_p_tw;
6739 if (textwidth == 0 && curbuf->b_p_wm)
6740 {
6741 /* The width is the window width minus 'wrapmargin' minus all the
6742 * things that add to the margin. */
6743 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6744#ifdef FEAT_CMDWIN
6745 if (cmdwin_type != 0)
6746 textwidth -= 1;
6747#endif
6748#ifdef FEAT_FOLDING
6749 textwidth -= curwin->w_p_fdc;
6750#endif
6751#ifdef FEAT_SIGNS
6752 if (curwin->w_buffer->b_signlist != NULL
6753# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01006754 || curwin->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755# endif
6756 )
6757 textwidth -= 1;
6758#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006759 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760 textwidth -= 8;
6761 }
6762 if (textwidth < 0)
6763 textwidth = 0;
6764 if (ff && textwidth == 0)
6765 {
6766 textwidth = W_WIDTH(curwin) - 1;
6767 if (textwidth > 79)
6768 textwidth = 79;
6769 }
6770 return textwidth;
6771}
6772
6773/*
6774 * Put a character in the redo buffer, for when just after a CTRL-V.
6775 */
6776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006777redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778{
6779 char_u buf[10];
6780
6781 /* Only digits need special treatment. Translate them into a string of
6782 * three digits. */
6783 if (VIM_ISDIGIT(c))
6784 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006785 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 AppendToRedobuff(buf);
6787 }
6788 else
6789 AppendCharToRedobuff(c);
6790}
6791
6792/*
6793 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006794 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006795 */
6796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006797start_arrow(
6798 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006799{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006800 start_arrow_common(end_insert_pos, TRUE);
6801}
6802
6803/*
6804 * Like start_arrow() but with end_change argument.
6805 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6806 */
6807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006808start_arrow_with_change(
6809 pos_T *end_insert_pos, /* can be NULL */
6810 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006811{
6812 start_arrow_common(end_insert_pos, end_change);
6813 if (!end_change)
6814 {
6815 AppendCharToRedobuff(Ctrl_G);
6816 AppendCharToRedobuff('U');
6817 }
6818}
6819
6820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821start_arrow_common(
6822 pos_T *end_insert_pos, /* can be NULL */
6823 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006824{
6825 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 {
6827 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006828 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006829 arrow_used = TRUE; /* this means we stopped the current insert */
6830 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006831#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006832 check_spell_redraw();
6833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834}
6835
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006836#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006837/*
6838 * If we skipped highlighting word at cursor, do it now.
6839 * It may be skipped again, thus reset spell_redraw_lnum first.
6840 */
6841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006842check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006843{
6844 if (spell_redraw_lnum != 0)
6845 {
6846 linenr_T lnum = spell_redraw_lnum;
6847
6848 spell_redraw_lnum = 0;
6849 redrawWinline(lnum, FALSE);
6850 }
6851}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006852
6853/*
6854 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6855 * spelled word, if there is one.
6856 */
6857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006858spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006859{
6860 pos_T tpos = curwin->w_cursor;
6861
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006862 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006863 if (curwin->w_cursor.col != tpos.col)
6864 start_arrow(&tpos);
6865}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006866#endif
6867
Bram Moolenaar071d4272004-06-13 20:20:40 +00006868/*
6869 * stop_arrow() is called before a change is made in insert mode.
6870 * If an arrow key has been used, start a new insertion.
6871 * Returns FAIL if undo is impossible, shouldn't insert then.
6872 */
6873 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006874stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875{
6876 if (arrow_used)
6877 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006878 Insstart = curwin->w_cursor; /* new insertion starts here */
6879 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6880 /* Don't update the original insert position when moved to the
6881 * right, except when nothing was inserted yet. */
6882 update_Insstart_orig = FALSE;
6883 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6884
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885 if (u_save_cursor() == OK)
6886 {
6887 arrow_used = FALSE;
6888 ins_need_undo = FALSE;
6889 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006890
Bram Moolenaar071d4272004-06-13 20:20:40 +00006891 ai_col = 0;
6892#ifdef FEAT_VREPLACE
6893 if (State & VREPLACE_FLAG)
6894 {
6895 orig_line_count = curbuf->b_ml.ml_line_count;
6896 vr_lines_changed = 1;
6897 }
6898#endif
6899 ResetRedobuff();
6900 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006901 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 }
6903 else if (ins_need_undo)
6904 {
6905 if (u_save_cursor() == OK)
6906 ins_need_undo = FALSE;
6907 }
6908
6909#ifdef FEAT_FOLDING
6910 /* Always open fold at the cursor line when inserting something. */
6911 foldOpenCursor();
6912#endif
6913
6914 return (arrow_used || ins_need_undo ? FAIL : OK);
6915}
6916
6917/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006918 * Do a few things to stop inserting.
6919 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6920 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 */
6922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006923stop_insert(
6924 pos_T *end_insert_pos,
6925 int esc, /* called by ins_esc() */
6926 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006928 int cc;
6929 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930
6931 stop_redo_ins();
6932 replace_flush(); /* abandon replace stack */
6933
6934 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006935 * Save the inserted text for later redo with ^@ and CTRL-A.
6936 * Don't do it when "restart_edit" was set and nothing was inserted,
6937 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006938 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006939 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006940 if (did_restart_edit == 0 || (ptr != NULL
6941 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006942 {
6943 vim_free(last_insert);
6944 last_insert = ptr;
6945 last_insert_skip = new_insert_skip;
6946 }
6947 else
6948 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006950 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 {
6952 /* Auto-format now. It may seem strange to do this when stopping an
6953 * insertion (or moving the cursor), but it's required when appending
6954 * a line and having it end in a space. But only do it when something
6955 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006956 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006957 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006958 pos_T tpos = curwin->w_cursor;
6959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 /* When the cursor is at the end of the line after a space the
6961 * formatting will move it to the following word. Avoid that by
6962 * moving the cursor onto the space. */
6963 cc = 'x';
6964 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6965 {
6966 dec_cursor();
6967 cc = gchar_cursor();
6968 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006969 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970 }
6971
6972 auto_format(TRUE, FALSE);
6973
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006974 if (vim_iswhite(cc))
6975 {
6976 if (gchar_cursor() != NUL)
6977 inc_cursor();
6978#ifdef FEAT_VIRTUALEDIT
6979 /* If the cursor is still at the same character, also keep
6980 * the "coladd". */
6981 if (gchar_cursor() == NUL
6982 && curwin->w_cursor.lnum == tpos.lnum
6983 && curwin->w_cursor.col == tpos.col)
6984 curwin->w_cursor.coladd = tpos.coladd;
6985#endif
6986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 }
6988
6989 /* If a space was inserted for auto-formatting, remove it now. */
6990 check_auto_format(TRUE);
6991
6992 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006993 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006994 * Do this when ESC was used or moving the cursor up/down.
6995 * Check for the old position still being valid, just in case the text
6996 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006997 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006998 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6999 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007001 pos_T tpos = curwin->w_cursor;
7002
7003 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007004 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007005 for (;;)
7006 {
7007 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7008 --curwin->w_cursor.col;
7009 cc = gchar_cursor();
7010 if (!vim_iswhite(cc))
7011 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007012 if (del_char(TRUE) == FAIL)
7013 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007014 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007015 if (curwin->w_cursor.lnum != tpos.lnum)
7016 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007017 else
7018 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007019 /* reset tpos, could have been invalidated in the loop above */
7020 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007021 tpos.col++;
7022 if (cc != NUL && gchar_pos(&tpos) == NUL)
7023 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007025
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 /* <C-S-Right> may have started Visual mode, adjust the position for
7027 * deleted characters. */
7028 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7029 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007030 int len = (int)STRLEN(ml_get_curline());
7031
7032 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007033 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007034 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007035#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 }
7041 }
7042 did_ai = FALSE;
7043#ifdef FEAT_SMARTINDENT
7044 did_si = FALSE;
7045 can_si = FALSE;
7046 can_si_back = FALSE;
7047#endif
7048
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007049 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7050 * now in a different buffer. */
7051 if (end_insert_pos != NULL)
7052 {
7053 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007054 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007055 curbuf->b_op_end = *end_insert_pos;
7056 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007057}
7058
7059/*
7060 * Set the last inserted text to a single character.
7061 * Used for the replace command.
7062 */
7063 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007064set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065{
7066 char_u *s;
7067
7068 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 if (last_insert != NULL)
7071 {
7072 s = last_insert;
7073 /* Use the CTRL-V only when entering a special char */
7074 if (c < ' ' || c == DEL)
7075 *s++ = Ctrl_V;
7076 s = add_char2buf(c, s);
7077 *s++ = ESC;
7078 *s++ = NUL;
7079 last_insert_skip = 0;
7080 }
7081}
7082
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007083#if defined(EXITFREE) || defined(PROTO)
7084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007085free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007086{
7087 vim_free(last_insert);
7088 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007089# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007090 vim_free(compl_orig_text);
7091 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007092# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007093}
7094#endif
7095
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096/*
7097 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7098 * and CSI. Handle multi-byte characters.
7099 * Returns a pointer to after the added bytes.
7100 */
7101 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007102add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103{
7104#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007105 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106 int i;
7107 int len;
7108
7109 len = (*mb_char2bytes)(c, temp);
7110 for (i = 0; i < len; ++i)
7111 {
7112 c = temp[i];
7113#endif
7114 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7115 if (c == K_SPECIAL)
7116 {
7117 *s++ = K_SPECIAL;
7118 *s++ = KS_SPECIAL;
7119 *s++ = KE_FILLER;
7120 }
7121#ifdef FEAT_GUI
7122 else if (c == CSI)
7123 {
7124 *s++ = CSI;
7125 *s++ = KS_EXTRA;
7126 *s++ = (int)KE_CSI;
7127 }
7128#endif
7129 else
7130 *s++ = c;
7131#ifdef FEAT_MBYTE
7132 }
7133#endif
7134 return s;
7135}
7136
7137/*
7138 * move cursor to start of line
7139 * if flags & BL_WHITE move to first non-white
7140 * if flags & BL_SOL move to first non-white if startofline is set,
7141 * otherwise keep "curswant" column
7142 * if flags & BL_FIX don't leave the cursor on a NUL.
7143 */
7144 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007145beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146{
7147 if ((flags & BL_SOL) && !p_sol)
7148 coladvance(curwin->w_curswant);
7149 else
7150 {
7151 curwin->w_cursor.col = 0;
7152#ifdef FEAT_VIRTUALEDIT
7153 curwin->w_cursor.coladd = 0;
7154#endif
7155
7156 if (flags & (BL_WHITE | BL_SOL))
7157 {
7158 char_u *ptr;
7159
7160 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7161 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7162 ++curwin->w_cursor.col;
7163 }
7164 curwin->w_set_curswant = TRUE;
7165 }
7166}
7167
7168/*
7169 * oneright oneleft cursor_down cursor_up
7170 *
7171 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007172 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007173 * Return OK when successful, FAIL when we hit a line of file boundary.
7174 */
7175
7176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007177oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178{
7179 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007180 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181
7182#ifdef FEAT_VIRTUALEDIT
7183 if (virtual_active())
7184 {
7185 pos_T prevpos = curwin->w_cursor;
7186
7187 /* Adjust for multi-wide char (excluding TAB) */
7188 ptr = ml_get_cursor();
7189 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007190# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007192# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007194# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195 ))
7196 ? ptr2cells(ptr) : 1));
7197 curwin->w_set_curswant = TRUE;
7198 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7199 return (prevpos.col != curwin->w_cursor.col
7200 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7201 }
7202#endif
7203
7204 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007205 if (*ptr == NUL)
7206 return FAIL; /* already at the very end */
7207
Bram Moolenaar071d4272004-06-13 20:20:40 +00007208#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007209 if (has_mbyte)
7210 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 else
7212#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007213 l = 1;
7214
7215 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7216 * contains "onemore". */
7217 if (ptr[l] == NUL
7218#ifdef FEAT_VIRTUALEDIT
7219 && (ve_flags & VE_ONEMORE) == 0
7220#endif
7221 )
7222 return FAIL;
7223 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007224
7225 curwin->w_set_curswant = TRUE;
7226 return OK;
7227}
7228
7229 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007230oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231{
7232#ifdef FEAT_VIRTUALEDIT
7233 if (virtual_active())
7234 {
7235 int width;
7236 int v = getviscol();
7237
7238 if (v == 0)
7239 return FAIL;
7240
7241# ifdef FEAT_LINEBREAK
7242 /* We might get stuck on 'showbreak', skip over it. */
7243 width = 1;
7244 for (;;)
7245 {
7246 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007247 /* getviscol() is slow, skip it when 'showbreak' is empty,
7248 * 'breakindent' is not set and there are no multi-byte
7249 * characters */
7250 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251# ifdef FEAT_MBYTE
7252 && !has_mbyte
7253# endif
7254 ) || getviscol() < v)
7255 break;
7256 ++width;
7257 }
7258# else
7259 coladvance(v - 1);
7260# endif
7261
7262 if (curwin->w_cursor.coladd == 1)
7263 {
7264 char_u *ptr;
7265
7266 /* Adjust for multi-wide char (not a TAB) */
7267 ptr = ml_get_cursor();
7268 if (*ptr != TAB && vim_isprintc(
7269# ifdef FEAT_MBYTE
7270 (*mb_ptr2char)(ptr)
7271# else
7272 *ptr
7273# endif
7274 ) && ptr2cells(ptr) > 1)
7275 curwin->w_cursor.coladd = 0;
7276 }
7277
7278 curwin->w_set_curswant = TRUE;
7279 return OK;
7280 }
7281#endif
7282
7283 if (curwin->w_cursor.col == 0)
7284 return FAIL;
7285
7286 curwin->w_set_curswant = TRUE;
7287 --curwin->w_cursor.col;
7288
7289#ifdef FEAT_MBYTE
7290 /* if the character on the left of the current cursor is a multi-byte
7291 * character, move to its first byte */
7292 if (has_mbyte)
7293 mb_adjust_cursor();
7294#endif
7295 return OK;
7296}
7297
7298 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007299cursor_up(
7300 long n,
7301 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302{
7303 linenr_T lnum;
7304
7305 if (n > 0)
7306 {
7307 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007308 /* This fails if the cursor is already in the first line or the count
7309 * is larger than the line number and '-' is in 'cpoptions' */
7310 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 return FAIL;
7312 if (n >= lnum)
7313 lnum = 1;
7314 else
7315#ifdef FEAT_FOLDING
7316 if (hasAnyFolding(curwin))
7317 {
7318 /*
7319 * Count each sequence of folded lines as one logical line.
7320 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007321 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322 (void)hasFolding(lnum, &lnum, NULL);
7323
7324 while (n--)
7325 {
7326 /* move up one line */
7327 --lnum;
7328 if (lnum <= 1)
7329 break;
7330 /* If we entered a fold, move to the beginning, unless in
7331 * Insert mode or when 'foldopen' contains "all": it will open
7332 * in a moment. */
7333 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7334 (void)hasFolding(lnum, &lnum, NULL);
7335 }
7336 if (lnum < 1)
7337 lnum = 1;
7338 }
7339 else
7340#endif
7341 lnum -= n;
7342 curwin->w_cursor.lnum = lnum;
7343 }
7344
7345 /* try to advance to the column we want to be at */
7346 coladvance(curwin->w_curswant);
7347
7348 if (upd_topline)
7349 update_topline(); /* make sure curwin->w_topline is valid */
7350
7351 return OK;
7352}
7353
7354/*
7355 * Cursor down a number of logical lines.
7356 */
7357 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007358cursor_down(
7359 long n,
7360 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007361{
7362 linenr_T lnum;
7363
7364 if (n > 0)
7365 {
7366 lnum = curwin->w_cursor.lnum;
7367#ifdef FEAT_FOLDING
7368 /* Move to last line of fold, will fail if it's the end-of-file. */
7369 (void)hasFolding(lnum, NULL, &lnum);
7370#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007371 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007372 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007373 if (lnum >= curbuf->b_ml.ml_line_count
7374 || (lnum + n > curbuf->b_ml.ml_line_count
7375 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007376 return FAIL;
7377 if (lnum + n >= curbuf->b_ml.ml_line_count)
7378 lnum = curbuf->b_ml.ml_line_count;
7379 else
7380#ifdef FEAT_FOLDING
7381 if (hasAnyFolding(curwin))
7382 {
7383 linenr_T last;
7384
7385 /* count each sequence of folded lines as one logical line */
7386 while (n--)
7387 {
7388 if (hasFolding(lnum, NULL, &last))
7389 lnum = last + 1;
7390 else
7391 ++lnum;
7392 if (lnum >= curbuf->b_ml.ml_line_count)
7393 break;
7394 }
7395 if (lnum > curbuf->b_ml.ml_line_count)
7396 lnum = curbuf->b_ml.ml_line_count;
7397 }
7398 else
7399#endif
7400 lnum += n;
7401 curwin->w_cursor.lnum = lnum;
7402 }
7403
7404 /* try to advance to the column we want to be at */
7405 coladvance(curwin->w_curswant);
7406
7407 if (upd_topline)
7408 update_topline(); /* make sure curwin->w_topline is valid */
7409
7410 return OK;
7411}
7412
7413/*
7414 * Stuff the last inserted text in the read buffer.
7415 * Last_insert actually is a copy of the redo buffer, so we
7416 * first have to remove the command.
7417 */
7418 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007419stuff_inserted(
7420 int c, /* Command character to be inserted */
7421 long count, /* Repeat this many times */
7422 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423{
7424 char_u *esc_ptr;
7425 char_u *ptr;
7426 char_u *last_ptr;
7427 char_u last = NUL;
7428
7429 ptr = get_last_insert();
7430 if (ptr == NULL)
7431 {
7432 EMSG(_(e_noinstext));
7433 return FAIL;
7434 }
7435
7436 /* may want to stuff the command character, to start Insert mode */
7437 if (c != NUL)
7438 stuffcharReadbuff(c);
7439 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7440 *esc_ptr = NUL; /* remove the ESC */
7441
7442 /* when the last char is either "0" or "^" it will be quoted if no ESC
7443 * comes after it OR if it will inserted more than once and "ptr"
7444 * starts with ^D. -- Acevedo
7445 */
7446 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7447 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7448 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7449 {
7450 last = *last_ptr;
7451 *last_ptr = NUL;
7452 }
7453
7454 do
7455 {
7456 stuffReadbuff(ptr);
7457 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7458 if (last)
7459 stuffReadbuff((char_u *)(last == '0'
7460 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7461 : IF_EB("\026^", CTRL_V_STR "^")));
7462 }
7463 while (--count > 0);
7464
7465 if (last)
7466 *last_ptr = last;
7467
7468 if (esc_ptr != NULL)
7469 *esc_ptr = ESC; /* put the ESC back */
7470
7471 /* may want to stuff a trailing ESC, to get out of Insert mode */
7472 if (!no_esc)
7473 stuffcharReadbuff(ESC);
7474
7475 return OK;
7476}
7477
7478 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007479get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007480{
7481 if (last_insert == NULL)
7482 return NULL;
7483 return last_insert + last_insert_skip;
7484}
7485
7486/*
7487 * Get last inserted string, and remove trailing <Esc>.
7488 * Returns pointer to allocated memory (must be freed) or NULL.
7489 */
7490 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007491get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007492{
7493 char_u *s;
7494 int len;
7495
7496 if (last_insert == NULL)
7497 return NULL;
7498 s = vim_strsave(last_insert + last_insert_skip);
7499 if (s != NULL)
7500 {
7501 len = (int)STRLEN(s);
7502 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7503 s[len - 1] = NUL;
7504 }
7505 return s;
7506}
7507
7508/*
7509 * Check the word in front of the cursor for an abbreviation.
7510 * Called when the non-id character "c" has been entered.
7511 * When an abbreviation is recognized it is removed from the text and
7512 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7513 */
7514 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007515echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516{
7517 /* Don't check for abbreviation in paste mode, when disabled and just
7518 * after moving around with cursor keys. */
7519 if (p_paste || no_abbr || arrow_used)
7520 return FALSE;
7521
7522 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7523 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7524}
7525
7526/*
7527 * replace-stack functions
7528 *
7529 * When replacing characters, the replaced characters are remembered for each
7530 * new character. This is used to re-insert the old text when backspacing.
7531 *
7532 * There is a NUL headed list of characters for each character that is
7533 * currently in the file after the insertion point. When BS is used, one NUL
7534 * headed list is put back for the deleted character.
7535 *
7536 * For a newline, there are two NUL headed lists. One contains the characters
7537 * that the NL replaced. The extra one stores the characters after the cursor
7538 * that were deleted (always white space).
7539 *
7540 * Replace_offset is normally 0, in which case replace_push will add a new
7541 * character at the end of the stack. If replace_offset is not 0, that many
7542 * characters will be left on the stack above the newly inserted character.
7543 */
7544
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007545static char_u *replace_stack = NULL;
7546static long replace_stack_nr = 0; /* next entry in replace stack */
7547static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007548
7549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007550replace_push(
7551 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552{
7553 char_u *p;
7554
7555 if (replace_stack_nr < replace_offset) /* nothing to do */
7556 return;
7557 if (replace_stack_len <= replace_stack_nr)
7558 {
7559 replace_stack_len += 50;
7560 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7561 if (p == NULL) /* out of memory */
7562 {
7563 replace_stack_len -= 50;
7564 return;
7565 }
7566 if (replace_stack != NULL)
7567 {
7568 mch_memmove(p, replace_stack,
7569 (size_t)(replace_stack_nr * sizeof(char_u)));
7570 vim_free(replace_stack);
7571 }
7572 replace_stack = p;
7573 }
7574 p = replace_stack + replace_stack_nr - replace_offset;
7575 if (replace_offset)
7576 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7577 *p = c;
7578 ++replace_stack_nr;
7579}
7580
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007581#if defined(FEAT_MBYTE) || defined(PROTO)
7582/*
7583 * Push a character onto the replace stack. Handles a multi-byte character in
7584 * reverse byte order, so that the first byte is popped off first.
7585 * Return the number of bytes done (includes composing characters).
7586 */
7587 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007588replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007589{
7590 int l = (*mb_ptr2len)(p);
7591 int j;
7592
7593 for (j = l - 1; j >= 0; --j)
7594 replace_push(p[j]);
7595 return l;
7596}
7597#endif
7598
Bram Moolenaar071d4272004-06-13 20:20:40 +00007599/*
7600 * Pop one item from the replace stack.
7601 * return -1 if stack empty
7602 * return replaced character or NUL otherwise
7603 */
7604 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007605replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007606{
7607 if (replace_stack_nr == 0)
7608 return -1;
7609 return (int)replace_stack[--replace_stack_nr];
7610}
7611
7612/*
7613 * Join the top two items on the replace stack. This removes to "off"'th NUL
7614 * encountered.
7615 */
7616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007617replace_join(
7618 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
7620 int i;
7621
7622 for (i = replace_stack_nr; --i >= 0; )
7623 if (replace_stack[i] == NUL && off-- <= 0)
7624 {
7625 --replace_stack_nr;
7626 mch_memmove(replace_stack + i, replace_stack + i + 1,
7627 (size_t)(replace_stack_nr - i));
7628 return;
7629 }
7630}
7631
7632/*
7633 * Pop bytes from the replace stack until a NUL is found, and insert them
7634 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7635 */
7636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007637replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007638{
7639 int cc;
7640 int oldState = State;
7641
7642 State = NORMAL; /* don't want REPLACE here */
7643 while ((cc = replace_pop()) > 0)
7644 {
7645#ifdef FEAT_MBYTE
7646 mb_replace_pop_ins(cc);
7647#else
7648 ins_char(cc);
7649#endif
7650 dec_cursor();
7651 }
7652 State = oldState;
7653}
7654
7655#ifdef FEAT_MBYTE
7656/*
7657 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7658 * indicates a multi-byte char, pop the other bytes too.
7659 */
7660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007661mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007662{
7663 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007664 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007665 int i;
7666 int c;
7667
7668 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7669 {
7670 buf[0] = cc;
7671 for (i = 1; i < n; ++i)
7672 buf[i] = replace_pop();
7673 ins_bytes_len(buf, n);
7674 }
7675 else
7676 ins_char(cc);
7677
7678 if (enc_utf8)
7679 /* Handle composing chars. */
7680 for (;;)
7681 {
7682 c = replace_pop();
7683 if (c == -1) /* stack empty */
7684 break;
7685 if ((n = MB_BYTE2LEN(c)) == 1)
7686 {
7687 /* Not a multi-byte char, put it back. */
7688 replace_push(c);
7689 break;
7690 }
7691 else
7692 {
7693 buf[0] = c;
7694 for (i = 1; i < n; ++i)
7695 buf[i] = replace_pop();
7696 if (utf_iscomposing(utf_ptr2char(buf)))
7697 ins_bytes_len(buf, n);
7698 else
7699 {
7700 /* Not a composing char, put it back. */
7701 for (i = n - 1; i >= 0; --i)
7702 replace_push(buf[i]);
7703 break;
7704 }
7705 }
7706 }
7707}
7708#endif
7709
7710/*
7711 * make the replace stack empty
7712 * (called when exiting replace mode)
7713 */
7714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007715replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716{
7717 vim_free(replace_stack);
7718 replace_stack = NULL;
7719 replace_stack_len = 0;
7720 replace_stack_nr = 0;
7721}
7722
7723/*
7724 * Handle doing a BS for one character.
7725 * cc < 0: replace stack empty, just move cursor
7726 * cc == 0: character was inserted, delete it
7727 * cc > 0: character was replaced, put cc (first byte of original char) back
7728 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007729 * When "limit_col" is >= 0, don't delete before this column. Matters when
7730 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 */
7732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007733replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007734{
7735 int cc;
7736#ifdef FEAT_VREPLACE
7737 int orig_len = 0;
7738 int ins_len;
7739 int orig_vcols = 0;
7740 colnr_T start_vcol;
7741 char_u *p;
7742 int i;
7743 int vcol;
7744#endif
7745
7746 cc = replace_pop();
7747 if (cc > 0)
7748 {
7749#ifdef FEAT_VREPLACE
7750 if (State & VREPLACE_FLAG)
7751 {
7752 /* Get the number of screen cells used by the character we are
7753 * going to delete. */
7754 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7755 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7756 }
7757#endif
7758#ifdef FEAT_MBYTE
7759 if (has_mbyte)
7760 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007761 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762# ifdef FEAT_VREPLACE
7763 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007764 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765# endif
7766 replace_push(cc);
7767 }
7768 else
7769#endif
7770 {
7771 pchar_cursor(cc);
7772#ifdef FEAT_VREPLACE
7773 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007774 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775#endif
7776 }
7777 replace_pop_ins();
7778
7779#ifdef FEAT_VREPLACE
7780 if (State & VREPLACE_FLAG)
7781 {
7782 /* Get the number of screen cells used by the inserted characters */
7783 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007784 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 vcol = start_vcol;
7786 for (i = 0; i < ins_len; ++i)
7787 {
7788 vcol += chartabsize(p + i, vcol);
7789#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007790 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007791#endif
7792 }
7793 vcol -= start_vcol;
7794
7795 /* Delete spaces that were inserted after the cursor to keep the
7796 * text aligned. */
7797 curwin->w_cursor.col += ins_len;
7798 while (vcol > orig_vcols && gchar_cursor() == ' ')
7799 {
7800 del_char(FALSE);
7801 ++orig_vcols;
7802 }
7803 curwin->w_cursor.col -= ins_len;
7804 }
7805#endif
7806
7807 /* mark the buffer as changed and prepare for displaying */
7808 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7809 }
7810 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007811 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007812}
7813
7814#ifdef FEAT_CINDENT
7815/*
7816 * Return TRUE if C-indenting is on.
7817 */
7818 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007819cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820{
7821 return (!p_paste && (curbuf->b_p_cin
7822# ifdef FEAT_EVAL
7823 || *curbuf->b_p_inde != NUL
7824# endif
7825 ));
7826}
7827#endif
7828
7829#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7830/*
7831 * Re-indent the current line, based on the current contents of it and the
7832 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7833 * confused what all the part that handles Control-T is doing that I'm not.
7834 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7835 */
7836
7837 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007838fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007840 int amount = get_the_indent();
7841
7842 if (amount >= 0)
7843 {
7844 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7845 if (linewhite(curwin->w_cursor.lnum))
7846 did_ai = TRUE; /* delete the indent if the line stays empty */
7847 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848}
7849
7850 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007851fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852{
7853 if (p_paste)
7854 return;
7855# ifdef FEAT_LISP
7856 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7857 fixthisline(get_lisp_indent);
7858# endif
7859# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7860 else
7861# endif
7862# ifdef FEAT_CINDENT
7863 if (cindent_on())
7864 do_c_expr_indent();
7865# endif
7866}
7867
7868#endif
7869
7870#ifdef FEAT_CINDENT
7871/*
7872 * return TRUE if 'cinkeys' contains the key "keytyped",
7873 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007874 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7876 *
7877 * "keytyped" can have a few special values:
7878 * KEY_OPEN_FORW
7879 * KEY_OPEN_BACK
7880 * KEY_COMPLETE just finished completion.
7881 *
7882 * If line_is_empty is TRUE accept keys with '0' before them.
7883 */
7884 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007885in_cinkeys(
7886 int keytyped,
7887 int when,
7888 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007889{
7890 char_u *look;
7891 int try_match;
7892 int try_match_word;
7893 char_u *p;
7894 char_u *line;
7895 int icase;
7896 int i;
7897
Bram Moolenaar30848942009-12-24 14:46:12 +00007898 if (keytyped == NUL)
7899 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7900 return FALSE;
7901
Bram Moolenaar071d4272004-06-13 20:20:40 +00007902#ifdef FEAT_EVAL
7903 if (*curbuf->b_p_inde != NUL)
7904 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7905 else
7906#endif
7907 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7908 while (*look)
7909 {
7910 /*
7911 * Find out if we want to try a match with this key, depending on
7912 * 'when' and a '*' or '!' before the key.
7913 */
7914 switch (when)
7915 {
7916 case '*': try_match = (*look == '*'); break;
7917 case '!': try_match = (*look == '!'); break;
7918 default: try_match = (*look != '*'); break;
7919 }
7920 if (*look == '*' || *look == '!')
7921 ++look;
7922
7923 /*
7924 * If there is a '0', only accept a match if the line is empty.
7925 * But may still match when typing last char of a word.
7926 */
7927 if (*look == '0')
7928 {
7929 try_match_word = try_match;
7930 if (!line_is_empty)
7931 try_match = FALSE;
7932 ++look;
7933 }
7934 else
7935 try_match_word = FALSE;
7936
7937 /*
7938 * does it look like a control character?
7939 */
7940 if (*look == '^'
7941#ifdef EBCDIC
7942 && (Ctrl_chr(look[1]) != 0)
7943#else
7944 && look[1] >= '?' && look[1] <= '_'
7945#endif
7946 )
7947 {
7948 if (try_match && keytyped == Ctrl_chr(look[1]))
7949 return TRUE;
7950 look += 2;
7951 }
7952 /*
7953 * 'o' means "o" command, open forward.
7954 * 'O' means "O" command, open backward.
7955 */
7956 else if (*look == 'o')
7957 {
7958 if (try_match && keytyped == KEY_OPEN_FORW)
7959 return TRUE;
7960 ++look;
7961 }
7962 else if (*look == 'O')
7963 {
7964 if (try_match && keytyped == KEY_OPEN_BACK)
7965 return TRUE;
7966 ++look;
7967 }
7968
7969 /*
7970 * 'e' means to check for "else" at start of line and just before the
7971 * cursor.
7972 */
7973 else if (*look == 'e')
7974 {
7975 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7976 {
7977 p = ml_get_curline();
7978 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7979 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7980 return TRUE;
7981 }
7982 ++look;
7983 }
7984
7985 /*
7986 * ':' only causes an indent if it is at the end of a label or case
7987 * statement, or when it was before typing the ':' (to fix
7988 * class::method for C++).
7989 */
7990 else if (*look == ':')
7991 {
7992 if (try_match && keytyped == ':')
7993 {
7994 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007995 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007997 /* Need to get the line again after cin_islabel(). */
7998 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 if (curwin->w_cursor.col > 2
8000 && p[curwin->w_cursor.col - 1] == ':'
8001 && p[curwin->w_cursor.col - 2] == ':')
8002 {
8003 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008004 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008005 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008006 p = ml_get_curline();
8007 p[curwin->w_cursor.col - 1] = ':';
8008 if (i)
8009 return TRUE;
8010 }
8011 }
8012 ++look;
8013 }
8014
8015
8016 /*
8017 * Is it a key in <>, maybe?
8018 */
8019 else if (*look == '<')
8020 {
8021 if (try_match)
8022 {
8023 /*
8024 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8025 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8026 * >, *, : and ! keys if they really really want to.
8027 */
8028 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8029 && keytyped == look[1])
8030 return TRUE;
8031
8032 if (keytyped == get_special_key_code(look + 1))
8033 return TRUE;
8034 }
8035 while (*look && *look != '>')
8036 look++;
8037 while (*look == '>')
8038 look++;
8039 }
8040
8041 /*
8042 * Is it a word: "=word"?
8043 */
8044 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8045 {
8046 ++look;
8047 if (*look == '~')
8048 {
8049 icase = TRUE;
8050 ++look;
8051 }
8052 else
8053 icase = FALSE;
8054 p = vim_strchr(look, ',');
8055 if (p == NULL)
8056 p = look + STRLEN(look);
8057 if ((try_match || try_match_word)
8058 && curwin->w_cursor.col >= (colnr_T)(p - look))
8059 {
8060 int match = FALSE;
8061
8062#ifdef FEAT_INS_EXPAND
8063 if (keytyped == KEY_COMPLETE)
8064 {
8065 char_u *s;
8066
8067 /* Just completed a word, check if it starts with "look".
8068 * search back for the start of a word. */
8069 line = ml_get_curline();
8070# ifdef FEAT_MBYTE
8071 if (has_mbyte)
8072 {
8073 char_u *n;
8074
8075 for (s = line + curwin->w_cursor.col; s > line; s = n)
8076 {
8077 n = mb_prevptr(line, s);
8078 if (!vim_iswordp(n))
8079 break;
8080 }
8081 }
8082 else
8083# endif
8084 for (s = line + curwin->w_cursor.col; s > line; --s)
8085 if (!vim_iswordc(s[-1]))
8086 break;
8087 if (s + (p - look) <= line + curwin->w_cursor.col
8088 && (icase
8089 ? MB_STRNICMP(s, look, p - look)
8090 : STRNCMP(s, look, p - look)) == 0)
8091 match = TRUE;
8092 }
8093 else
8094#endif
8095 /* TODO: multi-byte */
8096 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8097 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8098 {
8099 line = ml_get_cursor();
8100 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8101 || !vim_iswordc(line[-(p - look) - 1]))
8102 && (icase
8103 ? MB_STRNICMP(line - (p - look), look, p - look)
8104 : STRNCMP(line - (p - look), look, p - look))
8105 == 0)
8106 match = TRUE;
8107 }
8108 if (match && try_match_word && !try_match)
8109 {
8110 /* "0=word": Check if there are only blanks before the
8111 * word. */
8112 line = ml_get_curline();
8113 if ((int)(skipwhite(line) - line) !=
8114 (int)(curwin->w_cursor.col - (p - look)))
8115 match = FALSE;
8116 }
8117 if (match)
8118 return TRUE;
8119 }
8120 look = p;
8121 }
8122
8123 /*
8124 * ok, it's a boring generic character.
8125 */
8126 else
8127 {
8128 if (try_match && *look == keytyped)
8129 return TRUE;
8130 ++look;
8131 }
8132
8133 /*
8134 * Skip over ", ".
8135 */
8136 look = skip_to_option_part(look);
8137 }
8138 return FALSE;
8139}
8140#endif /* FEAT_CINDENT */
8141
8142#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8143/*
8144 * Map Hebrew keyboard when in hkmap mode.
8145 */
8146 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008147hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148{
8149 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8150 {
8151 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8152 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8153 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8154 static char_u map[26] =
8155 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8156 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8157 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8158 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8159 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8160 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8161 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8162 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8163 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8164
8165 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8166 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8167 /* '-1'='sofit' */
8168 else if (c == 'x')
8169 return 'X';
8170 else if (c == 'q')
8171 return '\''; /* {geresh}={'} */
8172 else if (c == 246)
8173 return ' '; /* \"o --> ' ' for a german keyboard */
8174 else if (c == 228)
8175 return ' '; /* \"a --> ' ' -- / -- */
8176 else if (c == 252)
8177 return ' '; /* \"u --> ' ' -- / -- */
8178#ifdef EBCDIC
8179 else if (islower(c))
8180#else
8181 /* NOTE: islower() does not do the right thing for us on Linux so we
8182 * do this the same was as 5.7 and previous, so it works correctly on
8183 * all systems. Specifically, the e.g. Delete and Arrow keys are
8184 * munged and won't work if e.g. searching for Hebrew text.
8185 */
8186 else if (c >= 'a' && c <= 'z')
8187#endif
8188 return (int)(map[CharOrdLow(c)] + p_aleph);
8189 else
8190 return c;
8191 }
8192 else
8193 {
8194 switch (c)
8195 {
8196 case '`': return ';';
8197 case '/': return '.';
8198 case '\'': return ',';
8199 case 'q': return '/';
8200 case 'w': return '\'';
8201
8202 /* Hebrew letters - set offset from 'a' */
8203 case ',': c = '{'; break;
8204 case '.': c = 'v'; break;
8205 case ';': c = 't'; break;
8206 default: {
8207 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8208
8209#ifdef EBCDIC
8210 /* see note about islower() above */
8211 if (!islower(c))
8212#else
8213 if (c < 'a' || c > 'z')
8214#endif
8215 return c;
8216 c = str[CharOrdLow(c)];
8217 break;
8218 }
8219 }
8220
8221 return (int)(CharOrdLow(c) + p_aleph);
8222 }
8223}
8224#endif
8225
8226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008227ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228{
8229 int need_redraw = FALSE;
8230 int regname;
8231 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008232 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233
8234 /*
8235 * If we are going to wait for a character, show a '"'.
8236 */
8237 pc_status = PC_STATUS_UNSET;
8238 if (redrawing() && !char_avail())
8239 {
8240 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008241 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242
8243 edit_putchar('"', TRUE);
8244#ifdef FEAT_CMDL_INFO
8245 add_to_showcmd_c(Ctrl_R);
8246#endif
8247 }
8248
8249#ifdef USE_ON_FLY_SCROLL
8250 dont_scroll = TRUE; /* disallow scrolling here */
8251#endif
8252
8253 /*
8254 * Don't map the register name. This also prevents the mode message to be
8255 * deleted when ESC is hit.
8256 */
8257 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008258 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8261 {
8262 /* Get a third key for literal register insertion */
8263 literally = regname;
8264#ifdef FEAT_CMDL_INFO
8265 add_to_showcmd_c(literally);
8266#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008267 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 }
8270 --no_mapping;
8271
8272#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008273 /* Don't call u_sync() while typing the expression or giving an error
8274 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 ++no_u_sync;
8276 if (regname == '=')
8277 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008278# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008280# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008281 /* Sync undo when evaluating the expression calls setline() or
8282 * append(), so that it can be undone separately. */
8283 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008284
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008286# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 /* Restore the Input Method. */
8288 if (im_on)
8289 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008290# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008292 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8293 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008294 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 else
8298 {
8299#endif
8300 if (literally == Ctrl_O || literally == Ctrl_P)
8301 {
8302 /* Append the command to the redo buffer. */
8303 AppendCharToRedobuff(Ctrl_R);
8304 AppendCharToRedobuff(literally);
8305 AppendCharToRedobuff(regname);
8306
8307 do_put(regname, BACKWARD, 1L,
8308 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8309 }
8310 else if (insert_reg(regname, literally) == FAIL)
8311 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008312 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 need_redraw = TRUE; /* remove the '"' */
8314 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008315 else if (stop_insert_mode)
8316 /* When the '=' register was used and a function was invoked that
8317 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8318 * insert anything, need to remove the '"' */
8319 need_redraw = TRUE;
8320
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321#ifdef FEAT_EVAL
8322 }
8323 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008324 if (u_sync_once == 1)
8325 ins_need_undo = TRUE;
8326 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327#endif
8328#ifdef FEAT_CMDL_INFO
8329 clear_showcmd();
8330#endif
8331
8332 /* If the inserted register is empty, we need to remove the '"' */
8333 if (need_redraw || stuff_empty())
8334 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008335
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008336 /* Disallow starting Visual mode here, would get a weird mode. */
8337 if (!vis_active && VIsual_active)
8338 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339}
8340
8341/*
8342 * CTRL-G commands in Insert mode.
8343 */
8344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008345ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008346{
8347 int c;
8348
8349#ifdef FEAT_INS_EXPAND
8350 /* Right after CTRL-X the cursor will be after the ruler. */
8351 setcursor();
8352#endif
8353
8354 /*
8355 * Don't map the second key. This also prevents the mode message to be
8356 * deleted when ESC is hit.
8357 */
8358 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008359 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 --no_mapping;
8361 switch (c)
8362 {
8363 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8364 case K_UP:
8365 case Ctrl_K:
8366 case 'k': ins_up(TRUE);
8367 break;
8368
8369 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8370 case K_DOWN:
8371 case Ctrl_J:
8372 case 'j': ins_down(TRUE);
8373 break;
8374
8375 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008376 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008378
8379 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008380 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008381 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008382 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008383 break;
8384
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008385 /* CTRL-G U: do not break undo with the next char */
8386 case 'U':
8387 /* Allow one left/right cursor movement with the next char,
8388 * without breaking undo. */
8389 dont_sync_undo = MAYBE;
8390 break;
8391
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008393 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 }
8395}
8396
8397/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008398 * CTRL-^ in Insert mode.
8399 */
8400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008401ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008402{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008403 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008404 {
8405 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8406 if (State & LANGMAP)
8407 {
8408 curbuf->b_p_iminsert = B_IMODE_NONE;
8409 State &= ~LANGMAP;
8410 }
8411 else
8412 {
8413 curbuf->b_p_iminsert = B_IMODE_LMAP;
8414 State |= LANGMAP;
8415#ifdef USE_IM_CONTROL
8416 im_set_active(FALSE);
8417#endif
8418 }
8419 }
8420#ifdef USE_IM_CONTROL
8421 else
8422 {
8423 /* There are no ":lmap" mappings, toggle IM */
8424 if (im_get_status())
8425 {
8426 curbuf->b_p_iminsert = B_IMODE_NONE;
8427 im_set_active(FALSE);
8428 }
8429 else
8430 {
8431 curbuf->b_p_iminsert = B_IMODE_IM;
8432 State &= ~LANGMAP;
8433 im_set_active(TRUE);
8434 }
8435 }
8436#endif
8437 set_iminsert_global();
8438 showmode();
8439#ifdef FEAT_GUI
8440 /* may show different cursor shape or color */
8441 if (gui.in_use)
8442 gui_update_cursor(TRUE, FALSE);
8443#endif
8444#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8445 /* Show/unshow value of 'keymap' in status lines. */
8446 status_redraw_curbuf();
8447#endif
8448}
8449
8450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 * Handle ESC in insert mode.
8452 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8453 * insert.
8454 */
8455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008456ins_esc(
8457 long *count,
8458 int cmdchar,
8459 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460{
8461 int temp;
8462 static int disabled_redraw = FALSE;
8463
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008464#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008465 check_spell_redraw();
8466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467#if defined(FEAT_HANGULIN)
8468# if defined(ESC_CHG_TO_ENG_MODE)
8469 hangul_input_state_set(0);
8470# endif
8471 if (composing_hangul)
8472 {
8473 push_raw_key(composing_hangul_buffer, 2);
8474 composing_hangul = 0;
8475 }
8476#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008477
8478 temp = curwin->w_cursor.col;
8479 if (disabled_redraw)
8480 {
8481 --RedrawingDisabled;
8482 disabled_redraw = FALSE;
8483 }
8484 if (!arrow_used)
8485 {
8486 /*
8487 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008488 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8489 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 */
8491 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008492 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493
8494 /*
8495 * Repeating insert may take a long time. Check for
8496 * interrupt now and then.
8497 */
8498 if (*count > 0)
8499 {
8500 line_breakcheck();
8501 if (got_int)
8502 *count = 0;
8503 }
8504
8505 if (--*count > 0) /* repeat what was typed */
8506 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008507 /* Vi repeats the insert without replacing characters. */
8508 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8509 State &= ~REPLACE_FLAG;
8510
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 (void)start_redo_ins();
8512 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008513 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 ++RedrawingDisabled;
8515 disabled_redraw = TRUE;
8516 return FALSE; /* repeat the insert */
8517 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008518 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 undisplay_dollar();
8520 }
8521
8522 /* When an autoindent was removed, curswant stays after the
8523 * indent */
8524 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8525 curwin->w_set_curswant = TRUE;
8526
8527 /* Remember the last Insert position in the '^ mark. */
8528 if (!cmdmod.keepjumps)
8529 curbuf->b_last_insert = curwin->w_cursor;
8530
8531 /*
8532 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008533 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008535 if (!nomove
8536 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537#ifdef FEAT_VIRTUALEDIT
8538 || curwin->w_cursor.coladd > 0
8539#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008540 )
8541 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008542 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543#ifdef FEAT_RIGHTLEFT
8544 && !revins_on
8545#endif
8546 )
8547 {
8548#ifdef FEAT_VIRTUALEDIT
8549 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8550 {
8551 oneleft();
8552 if (restart_edit != NUL)
8553 ++curwin->w_cursor.coladd;
8554 }
8555 else
8556#endif
8557 {
8558 --curwin->w_cursor.col;
8559#ifdef FEAT_MBYTE
8560 /* Correct cursor for multi-byte character. */
8561 if (has_mbyte)
8562 mb_adjust_cursor();
8563#endif
8564 }
8565 }
8566
8567#ifdef USE_IM_CONTROL
8568 /* Disable IM to allow typing English directly for Normal mode commands.
8569 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8570 * well). */
8571 if (!(State & LANGMAP))
8572 im_save_status(&curbuf->b_p_iminsert);
8573 im_set_active(FALSE);
8574#endif
8575
8576 State = NORMAL;
8577 /* need to position cursor again (e.g. when on a TAB ) */
8578 changed_cline_bef_curs();
8579
8580#ifdef FEAT_MOUSE
8581 setmouse();
8582#endif
8583#ifdef CURSOR_SHAPE
8584 ui_cursor_shape(); /* may show different cursor shape */
8585#endif
8586
8587 /*
8588 * When recording or for CTRL-O, need to display the new mode.
8589 * Otherwise remove the mode message.
8590 */
8591 if (Recording || restart_edit != NUL)
8592 showmode();
8593 else if (p_smd)
8594 MSG("");
8595
8596 return TRUE; /* exit Insert mode */
8597}
8598
8599#ifdef FEAT_RIGHTLEFT
8600/*
8601 * Toggle language: hkmap and revins_on.
8602 * Move to end of reverse inserted text.
8603 */
8604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008605ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606{
8607 if (revins_on && revins_chars && revins_scol >= 0)
8608 {
8609 while (gchar_cursor() != NUL && revins_chars--)
8610 ++curwin->w_cursor.col;
8611 }
8612 p_ri = !p_ri;
8613 revins_on = (State == INSERT && p_ri);
8614 if (revins_on)
8615 {
8616 revins_scol = curwin->w_cursor.col;
8617 revins_legal++;
8618 revins_chars = 0;
8619 undisplay_dollar();
8620 }
8621 else
8622 revins_scol = -1;
8623#ifdef FEAT_FKMAP
8624 if (p_altkeymap)
8625 {
8626 /*
8627 * to be consistent also for redo command, using '.'
8628 * set arrow_used to true and stop it - causing to redo
8629 * characters entered in one mode (normal/reverse insert).
8630 */
8631 arrow_used = TRUE;
8632 (void)stop_arrow();
8633 p_fkmap = curwin->w_p_rl ^ p_ri;
8634 if (p_fkmap && p_ri)
8635 State = INSERT;
8636 }
8637 else
8638#endif
8639 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8640 showmode();
8641}
8642#endif
8643
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644/*
8645 * If 'keymodel' contains "startsel", may start selection.
8646 * Returns TRUE when a CTRL-O and other keys stuffed.
8647 */
8648 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008649ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008650{
8651 if (km_startsel)
8652 switch (c)
8653 {
8654 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 case K_PAGEUP:
8657 case K_KPAGEUP:
8658 case K_PAGEDOWN:
8659 case K_KPAGEDOWN:
8660# ifdef MACOS
8661 case K_LEFT:
8662 case K_RIGHT:
8663 case K_UP:
8664 case K_DOWN:
8665 case K_END:
8666 case K_HOME:
8667# endif
8668 if (!(mod_mask & MOD_MASK_SHIFT))
8669 break;
8670 /* FALLTHROUGH */
8671 case K_S_LEFT:
8672 case K_S_RIGHT:
8673 case K_S_UP:
8674 case K_S_DOWN:
8675 case K_S_END:
8676 case K_S_HOME:
8677 /* Start selection right away, the cursor can move with
8678 * CTRL-O when beyond the end of the line. */
8679 start_selection();
8680
8681 /* Execute the key in (insert) Select mode. */
8682 stuffcharReadbuff(Ctrl_O);
8683 if (mod_mask)
8684 {
8685 char_u buf[4];
8686
8687 buf[0] = K_SPECIAL;
8688 buf[1] = KS_MODIFIER;
8689 buf[2] = mod_mask;
8690 buf[3] = NUL;
8691 stuffReadbuff(buf);
8692 }
8693 stuffcharReadbuff(c);
8694 return TRUE;
8695 }
8696 return FALSE;
8697}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698
8699/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008700 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008701 */
8702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008703ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008704{
8705#ifdef FEAT_FKMAP
8706 if (p_fkmap && p_ri)
8707 {
8708 beep_flush();
8709 EMSG(farsi_text_3); /* encoded in Farsi */
8710 return;
8711 }
8712#endif
8713
8714#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008715# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008716 set_vim_var_string(VV_INSERTMODE,
8717 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008718# ifdef FEAT_VREPLACE
8719 replaceState == VREPLACE ? "v" :
8720# endif
8721 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008722# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008723 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8724#endif
8725 if (State & REPLACE_FLAG)
8726 State = INSERT | (State & LANGMAP);
8727 else
8728 State = replaceState | (State & LANGMAP);
8729 AppendCharToRedobuff(K_INS);
8730 showmode();
8731#ifdef CURSOR_SHAPE
8732 ui_cursor_shape(); /* may show different cursor shape */
8733#endif
8734}
8735
8736/*
8737 * Pressed CTRL-O in Insert mode.
8738 */
8739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008740ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008741{
8742#ifdef FEAT_VREPLACE
8743 if (State & VREPLACE_FLAG)
8744 restart_edit = 'V';
8745 else
8746#endif
8747 if (State & REPLACE_FLAG)
8748 restart_edit = 'R';
8749 else
8750 restart_edit = 'I';
8751#ifdef FEAT_VIRTUALEDIT
8752 if (virtual_active())
8753 ins_at_eol = FALSE; /* cursor always keeps its column */
8754 else
8755#endif
8756 ins_at_eol = (gchar_cursor() == NUL);
8757}
8758
8759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 * If the cursor is on an indent, ^T/^D insert/delete one
8761 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008762 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 * with vi. But vi only supports ^T and ^D after an
8764 * autoindent, we support it everywhere.
8765 */
8766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008767ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768{
8769 if (stop_arrow() == FAIL)
8770 return;
8771 AppendCharToRedobuff(c);
8772
8773 /*
8774 * 0^D and ^^D: remove all indent.
8775 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008776 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8777 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778 {
8779 --curwin->w_cursor.col;
8780 (void)del_char(FALSE); /* delete the '^' or '0' */
8781 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8782 if (State & REPLACE_FLAG)
8783 replace_pop_ins();
8784 if (lastc == '^')
8785 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008786 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 }
8788 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008789 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790
8791 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8792 did_ai = FALSE;
8793#ifdef FEAT_SMARTINDENT
8794 did_si = FALSE;
8795 can_si = FALSE;
8796 can_si_back = FALSE;
8797#endif
8798#ifdef FEAT_CINDENT
8799 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8800#endif
8801}
8802
8803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008804ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805{
8806 int temp;
8807
8808 if (stop_arrow() == FAIL)
8809 return;
8810 if (gchar_cursor() == NUL) /* delete newline */
8811 {
8812 temp = curwin->w_cursor.col;
8813 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008814 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008815 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008816 else
8817 curwin->w_cursor.col = temp;
8818 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008819 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8820 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 did_ai = FALSE;
8822#ifdef FEAT_SMARTINDENT
8823 did_si = FALSE;
8824 can_si = FALSE;
8825 can_si_back = FALSE;
8826#endif
8827 AppendCharToRedobuff(K_DEL);
8828}
8829
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008830static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008831
8832/*
8833 * Delete one character for ins_bs().
8834 */
8835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008836ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008837{
8838 dec_cursor();
8839 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8840 if (State & REPLACE_FLAG)
8841 {
8842 /* Don't delete characters before the insert point when in
8843 * Replace mode */
8844 if (curwin->w_cursor.lnum != Insstart.lnum
8845 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008846 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008847 }
8848 else
8849 (void)del_char(FALSE);
8850}
8851
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852/*
8853 * Handle Backspace, delete-word and delete-line in Insert mode.
8854 * Return TRUE when backspace was actually used.
8855 */
8856 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008857ins_bs(
8858 int c,
8859 int mode,
8860 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861{
8862 linenr_T lnum;
8863 int cc;
8864 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008865 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 colnr_T mincol;
8867 int did_backspace = FALSE;
8868 int in_indent;
8869 int oldState;
8870#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008871 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872#endif
8873
8874 /*
8875 * can't delete anything in an empty file
8876 * can't backup past first character in buffer
8877 * can't backup past starting point unless 'backspace' > 1
8878 * can backup to a previous line if 'backspace' == 0
8879 */
8880 if ( bufempty()
8881 || (
8882#ifdef FEAT_RIGHTLEFT
8883 !revins_on &&
8884#endif
8885 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8886 || (!can_bs(BS_START)
8887 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008888 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8889 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008890 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8891 && curwin->w_cursor.col <= ai_col)
8892 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8893 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008894 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 return FALSE;
8896 }
8897
8898 if (stop_arrow() == FAIL)
8899 return FALSE;
8900 in_indent = inindent(0);
8901#ifdef FEAT_CINDENT
8902 if (in_indent)
8903 can_cindent = FALSE;
8904#endif
8905#ifdef FEAT_COMMENTS
8906 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8907#endif
8908#ifdef FEAT_RIGHTLEFT
8909 if (revins_on) /* put cursor after last inserted char */
8910 inc_cursor();
8911#endif
8912
8913#ifdef FEAT_VIRTUALEDIT
8914 /* Virtualedit:
8915 * BACKSPACE_CHAR eats a virtual space
8916 * BACKSPACE_WORD eats all coladd
8917 * BACKSPACE_LINE eats all coladd and keeps going
8918 */
8919 if (curwin->w_cursor.coladd > 0)
8920 {
8921 if (mode == BACKSPACE_CHAR)
8922 {
8923 --curwin->w_cursor.coladd;
8924 return TRUE;
8925 }
8926 if (mode == BACKSPACE_WORD)
8927 {
8928 curwin->w_cursor.coladd = 0;
8929 return TRUE;
8930 }
8931 curwin->w_cursor.coladd = 0;
8932 }
8933#endif
8934
8935 /*
8936 * delete newline!
8937 */
8938 if (curwin->w_cursor.col == 0)
8939 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008940 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008941 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942#ifdef FEAT_RIGHTLEFT
8943 || revins_on
8944#endif
8945 )
8946 {
8947 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8948 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8949 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008950 --Insstart.lnum;
8951 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952 }
8953 /*
8954 * In replace mode:
8955 * cc < 0: NL was inserted, delete it
8956 * cc >= 0: NL was replaced, put original characters back
8957 */
8958 cc = -1;
8959 if (State & REPLACE_FLAG)
8960 cc = replace_pop(); /* returns -1 if NL was inserted */
8961 /*
8962 * In replace mode, in the line we started replacing, we only move the
8963 * cursor.
8964 */
8965 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8966 {
8967 dec_cursor();
8968 }
8969 else
8970 {
8971#ifdef FEAT_VREPLACE
8972 if (!(State & VREPLACE_FLAG)
8973 || curwin->w_cursor.lnum > orig_line_count)
8974#endif
8975 {
8976 temp = gchar_cursor(); /* remember current char */
8977 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008978
8979 /* When "aw" is in 'formatoptions' we must delete the space at
8980 * the end of the line, otherwise the line will be broken
8981 * again when auto-formatting. */
8982 if (has_format_option(FO_AUTO)
8983 && has_format_option(FO_WHITE_PAR))
8984 {
8985 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8986 TRUE);
8987 int len;
8988
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008989 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008990 if (len > 0 && ptr[len - 1] == ' ')
8991 ptr[len - 1] = NUL;
8992 }
8993
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008994 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 if (temp == NUL && gchar_cursor() != NUL)
8996 inc_cursor();
8997 }
8998#ifdef FEAT_VREPLACE
8999 else
9000 dec_cursor();
9001#endif
9002
9003 /*
9004 * In REPLACE mode we have to put back the text that was replaced
9005 * by the NL. On the replace stack is first a NUL-terminated
9006 * sequence of characters that were deleted and then the
9007 * characters that NL replaced.
9008 */
9009 if (State & REPLACE_FLAG)
9010 {
9011 /*
9012 * Do the next ins_char() in NORMAL state, to
9013 * prevent ins_char() from replacing characters and
9014 * avoiding showmatch().
9015 */
9016 oldState = State;
9017 State = NORMAL;
9018 /*
9019 * restore characters (blanks) deleted after cursor
9020 */
9021 while (cc > 0)
9022 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009023 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024#ifdef FEAT_MBYTE
9025 mb_replace_pop_ins(cc);
9026#else
9027 ins_char(cc);
9028#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009029 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 cc = replace_pop();
9031 }
9032 /* restore the characters that NL replaced */
9033 replace_pop_ins();
9034 State = oldState;
9035 }
9036 }
9037 did_ai = FALSE;
9038 }
9039 else
9040 {
9041 /*
9042 * Delete character(s) before the cursor.
9043 */
9044#ifdef FEAT_RIGHTLEFT
9045 if (revins_on) /* put cursor on last inserted char */
9046 dec_cursor();
9047#endif
9048 mincol = 0;
9049 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009050 if (mode == BACKSPACE_LINE
9051 && (curbuf->b_p_ai
9052#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009053 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009054#endif
9055 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056#ifdef FEAT_RIGHTLEFT
9057 && !revins_on
9058#endif
9059 )
9060 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009061 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009063 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009065 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009066 }
9067
9068 /*
9069 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9070 */
9071 if ( mode == BACKSPACE_CHAR
9072 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009073 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009074 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 && (*(ml_get_cursor() - 1) == TAB
9076 || (*(ml_get_cursor() - 1) == ' '
9077 && (!*inserted_space_p
9078 || arrow_used))))))
9079 {
9080 int ts;
9081 colnr_T vcol;
9082 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009083 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084
9085 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009086 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009087 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009089 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 /* Compute the virtual column where we want to be. Since
9091 * 'showbreak' may get in the way, need to get the last column of
9092 * the previous character. */
9093 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009094 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 dec_cursor();
9096 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9097 inc_cursor();
9098 want_vcol = (want_vcol / ts) * ts;
9099
9100 /* delete characters until we are at or before want_vcol */
9101 while (vcol > want_vcol
9102 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009103 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
9105 /* insert extra spaces until we are at want_vcol */
9106 while (vcol < want_vcol)
9107 {
9108 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009109 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9110 && curwin->w_cursor.col < Insstart_orig.col)
9111 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112
9113#ifdef FEAT_VREPLACE
9114 if (State & VREPLACE_FLAG)
9115 ins_char(' ');
9116 else
9117#endif
9118 {
9119 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009120 if ((State & REPLACE_FLAG))
9121 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 }
9123 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9124 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009125
9126 /* If we are now back where we started delete one character. Can
9127 * happen when using 'sts' and 'linebreak'. */
9128 if (vcol >= start_vcol)
9129 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 }
9131
9132 /*
9133 * Delete upto starting point, start of line or previous word.
9134 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009135 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009137#ifdef FEAT_MBYTE
9138 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009140 if (has_mbyte)
9141 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009142#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009143 do
9144 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009145#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009146 if (!revins_on) /* put cursor on char to be deleted */
9147#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009149
9150 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009152 /* look multi-byte character class */
9153 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009155 prev_cclass = cclass;
9156 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009159
9160 /* start of word? */
9161 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9162 {
9163 mode = BACKSPACE_WORD_NOT_SPACE;
9164 temp = vim_iswordc(cc);
9165 }
9166 /* end of word? */
9167 else if (mode == BACKSPACE_WORD_NOT_SPACE
9168 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9169#ifdef FEAT_MBYTE
9170 || prev_cclass != cclass
9171#endif
9172 ))
9173 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009175 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009177 inc_cursor();
9178#ifdef FEAT_RIGHTLEFT
9179 else if (State & REPLACE_FLAG)
9180 dec_cursor();
9181#endif
9182 break;
9183 }
9184 if (State & REPLACE_FLAG)
9185 replace_do_bs(-1);
9186 else
9187 {
9188#ifdef FEAT_MBYTE
9189 if (enc_utf8 && p_deco)
9190 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9191#endif
9192 (void)del_char(FALSE);
9193#ifdef FEAT_MBYTE
9194 /*
9195 * If there are combining characters and 'delcombine' is set
9196 * move the cursor back. Don't back up before the base
9197 * character.
9198 */
9199 if (enc_utf8 && p_deco && cpc[0] != NUL)
9200 inc_cursor();
9201#endif
9202#ifdef FEAT_RIGHTLEFT
9203 if (revins_chars)
9204 {
9205 revins_chars--;
9206 revins_legal++;
9207 }
9208 if (revins_on && gchar_cursor() == NUL)
9209 break;
9210#endif
9211 }
9212 /* Just a single backspace?: */
9213 if (mode == BACKSPACE_CHAR)
9214 break;
9215 } while (
9216#ifdef FEAT_RIGHTLEFT
9217 revins_on ||
9218#endif
9219 (curwin->w_cursor.col > mincol
9220 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9221 || curwin->w_cursor.col != Insstart_orig.col)));
9222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009223 did_backspace = TRUE;
9224 }
9225#ifdef FEAT_SMARTINDENT
9226 did_si = FALSE;
9227 can_si = FALSE;
9228 can_si_back = FALSE;
9229#endif
9230 if (curwin->w_cursor.col <= 1)
9231 did_ai = FALSE;
9232 /*
9233 * It's a little strange to put backspaces into the redo
9234 * buffer, but it makes auto-indent a lot easier to deal
9235 * with.
9236 */
9237 AppendCharToRedobuff(c);
9238
9239 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009240 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9241 && curwin->w_cursor.col < Insstart_orig.col)
9242 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243
9244 /* vi behaviour: the cursor moves backward but the character that
9245 * was there remains visible
9246 * Vim behaviour: the cursor moves backward and the character that
9247 * was there is erased from the screen.
9248 * We can emulate the vi behaviour by pretending there is a dollar
9249 * displayed even when there isn't.
9250 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009251 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 dollar_vcol = curwin->w_virtcol;
9253
Bram Moolenaarce3be472008-01-14 19:12:28 +00009254#ifdef FEAT_FOLDING
9255 /* When deleting a char the cursor line must never be in a closed fold.
9256 * E.g., when 'foldmethod' is indent and deleting the first non-white
9257 * char before a Tab. */
9258 if (did_backspace)
9259 foldOpenCursor();
9260#endif
9261
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262 return did_backspace;
9263}
9264
9265#ifdef FEAT_MOUSE
9266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009267ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009268{
9269 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009270 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271
9272# ifdef FEAT_GUI
9273 /* When GUI is active, also move/paste when 'mouse' is empty */
9274 if (!gui.in_use)
9275# endif
9276 if (!mouse_has(MOUSE_INSERT))
9277 return;
9278
9279 undisplay_dollar();
9280 tpos = curwin->w_cursor;
9281 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9282 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009283#ifdef FEAT_WINDOWS
9284 win_T *new_curwin = curwin;
9285
9286 if (curwin != old_curwin && win_valid(old_curwin))
9287 {
9288 /* Mouse took us to another window. We need to go back to the
9289 * previous one to stop insert there properly. */
9290 curwin = old_curwin;
9291 curbuf = curwin->w_buffer;
9292 }
9293#endif
9294 start_arrow(curwin == old_curwin ? &tpos : NULL);
9295#ifdef FEAT_WINDOWS
9296 if (curwin != new_curwin && win_valid(new_curwin))
9297 {
9298 curwin = new_curwin;
9299 curbuf = curwin->w_buffer;
9300 }
9301#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302# ifdef FEAT_CINDENT
9303 can_cindent = TRUE;
9304# endif
9305 }
9306
9307#ifdef FEAT_WINDOWS
9308 /* redraw status lines (in case another window became active) */
9309 redraw_statuslines();
9310#endif
9311}
9312
9313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009314ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315{
9316 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009317# if defined(FEAT_WINDOWS)
9318 win_T *old_curwin = curwin;
9319# endif
9320# ifdef FEAT_INS_EXPAND
9321 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009322# endif
9323
9324 tpos = curwin->w_cursor;
9325
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009326# ifdef FEAT_WINDOWS
9327 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 {
9329 int row, col;
9330
9331 row = mouse_row;
9332 col = mouse_col;
9333
9334 /* find the window at the pointer coordinates */
9335 curwin = mouse_find_win(&row, &col);
9336 curbuf = curwin->w_buffer;
9337 }
9338 if (curwin == old_curwin)
9339# endif
9340 undisplay_dollar();
9341
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009342# ifdef FEAT_INS_EXPAND
9343 /* Don't scroll the window in which completion is being done. */
9344 if (!pum_visible()
9345# if defined(FEAT_WINDOWS)
9346 || curwin != old_curwin
9347# endif
9348 )
9349# endif
9350 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009351 if (dir == MSCR_DOWN || dir == MSCR_UP)
9352 {
9353 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9354 scroll_redraw(dir,
9355 (long)(curwin->w_botline - curwin->w_topline));
9356 else
9357 scroll_redraw(dir, 3L);
9358 }
9359#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009360 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009361 {
9362 int val, step = 6;
9363
9364 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9365 step = W_WIDTH(curwin);
9366 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9367 if (val < 0)
9368 val = 0;
9369 gui_do_horiz_scroll(val, TRUE);
9370 }
9371#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009372# ifdef FEAT_INS_EXPAND
9373 did_scroll = TRUE;
9374# endif
9375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009377# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 curwin->w_redr_status = TRUE;
9379
9380 curwin = old_curwin;
9381 curbuf = curwin->w_buffer;
9382# endif
9383
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009384# ifdef FEAT_INS_EXPAND
9385 /* The popup menu may overlay the window, need to redraw it.
9386 * TODO: Would be more efficient to only redraw the windows that are
9387 * overlapped by the popup menu. */
9388 if (pum_visible() && did_scroll)
9389 {
9390 redraw_all_later(NOT_VALID);
9391 ins_compl_show_pum();
9392 }
9393# endif
9394
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395 if (!equalpos(curwin->w_cursor, tpos))
9396 {
9397 start_arrow(&tpos);
9398# ifdef FEAT_CINDENT
9399 can_cindent = TRUE;
9400# endif
9401 }
9402}
9403#endif
9404
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009405#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009407ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009408{
9409 /* We will be leaving the current window, unless closing another tab. */
9410 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9411 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9412 {
9413 undisplay_dollar();
9414 start_arrow(&curwin->w_cursor);
9415# ifdef FEAT_CINDENT
9416 can_cindent = TRUE;
9417# endif
9418 }
9419
9420 if (c == K_TABLINE)
9421 goto_tabpage(current_tab);
9422 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009423 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009424 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009425 redraw_statuslines(); /* will redraw the tabline when needed */
9426 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009427}
9428#endif
9429
9430#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009432ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433{
9434 pos_T tpos;
9435
9436 undisplay_dollar();
9437 tpos = curwin->w_cursor;
9438 if (gui_do_scroll())
9439 {
9440 start_arrow(&tpos);
9441# ifdef FEAT_CINDENT
9442 can_cindent = TRUE;
9443# endif
9444 }
9445}
9446
9447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009448ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449{
9450 pos_T tpos;
9451
9452 undisplay_dollar();
9453 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009454 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009455 {
9456 start_arrow(&tpos);
9457# ifdef FEAT_CINDENT
9458 can_cindent = TRUE;
9459# endif
9460 }
9461}
9462#endif
9463
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465ins_left(
9466 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009467{
9468 pos_T tpos;
9469
9470#ifdef FEAT_FOLDING
9471 if ((fdo_flags & FDO_HOR) && KeyTyped)
9472 foldOpenCursor();
9473#endif
9474 undisplay_dollar();
9475 tpos = curwin->w_cursor;
9476 if (oneleft() == OK)
9477 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009478#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9479 /* Only call start_arrow() when not busy with preediting, it will
9480 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9481 if (!im_is_preediting())
9482#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009483 {
9484 start_arrow_with_change(&tpos, end_change);
9485 if (!end_change)
9486 AppendCharToRedobuff(K_LEFT);
9487 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488#ifdef FEAT_RIGHTLEFT
9489 /* If exit reversed string, position is fixed */
9490 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9491 revins_legal++;
9492 revins_chars++;
9493#endif
9494 }
9495
9496 /*
9497 * if 'whichwrap' set for cursor in insert mode may go to
9498 * previous line
9499 */
9500 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9501 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009502 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009503 start_arrow(&tpos);
9504 --(curwin->w_cursor.lnum);
9505 coladvance((colnr_T)MAXCOL);
9506 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9507 }
9508 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009509 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009510 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009511}
9512
9513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009514ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009515{
9516 pos_T tpos;
9517
9518#ifdef FEAT_FOLDING
9519 if ((fdo_flags & FDO_HOR) && KeyTyped)
9520 foldOpenCursor();
9521#endif
9522 undisplay_dollar();
9523 tpos = curwin->w_cursor;
9524 if (c == K_C_HOME)
9525 curwin->w_cursor.lnum = 1;
9526 curwin->w_cursor.col = 0;
9527#ifdef FEAT_VIRTUALEDIT
9528 curwin->w_cursor.coladd = 0;
9529#endif
9530 curwin->w_curswant = 0;
9531 start_arrow(&tpos);
9532}
9533
9534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009535ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536{
9537 pos_T tpos;
9538
9539#ifdef FEAT_FOLDING
9540 if ((fdo_flags & FDO_HOR) && KeyTyped)
9541 foldOpenCursor();
9542#endif
9543 undisplay_dollar();
9544 tpos = curwin->w_cursor;
9545 if (c == K_C_END)
9546 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9547 coladvance((colnr_T)MAXCOL);
9548 curwin->w_curswant = MAXCOL;
9549
9550 start_arrow(&tpos);
9551}
9552
9553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009554ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009555{
9556#ifdef FEAT_FOLDING
9557 if ((fdo_flags & FDO_HOR) && KeyTyped)
9558 foldOpenCursor();
9559#endif
9560 undisplay_dollar();
9561 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9562 {
9563 start_arrow(&curwin->w_cursor);
9564 (void)bck_word(1L, FALSE, FALSE);
9565 curwin->w_set_curswant = TRUE;
9566 }
9567 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009568 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569}
9570
9571 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009572ins_right(
9573 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574{
9575#ifdef FEAT_FOLDING
9576 if ((fdo_flags & FDO_HOR) && KeyTyped)
9577 foldOpenCursor();
9578#endif
9579 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009580 if (gchar_cursor() != NUL
9581#ifdef FEAT_VIRTUALEDIT
9582 || virtual_active()
9583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 )
9585 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009586 start_arrow_with_change(&curwin->w_cursor, end_change);
9587 if (!end_change)
9588 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 curwin->w_set_curswant = TRUE;
9590#ifdef FEAT_VIRTUALEDIT
9591 if (virtual_active())
9592 oneright();
9593 else
9594#endif
9595 {
9596#ifdef FEAT_MBYTE
9597 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009598 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 else
9600#endif
9601 ++curwin->w_cursor.col;
9602 }
9603
9604#ifdef FEAT_RIGHTLEFT
9605 revins_legal++;
9606 if (revins_chars)
9607 revins_chars--;
9608#endif
9609 }
9610 /* if 'whichwrap' set for cursor in insert mode, may move the
9611 * cursor to the next line */
9612 else if (vim_strchr(p_ww, ']') != NULL
9613 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9614 {
9615 start_arrow(&curwin->w_cursor);
9616 curwin->w_set_curswant = TRUE;
9617 ++curwin->w_cursor.lnum;
9618 curwin->w_cursor.col = 0;
9619 }
9620 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009621 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009622 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623}
9624
9625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009626ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009627{
9628#ifdef FEAT_FOLDING
9629 if ((fdo_flags & FDO_HOR) && KeyTyped)
9630 foldOpenCursor();
9631#endif
9632 undisplay_dollar();
9633 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9634 || gchar_cursor() != NUL)
9635 {
9636 start_arrow(&curwin->w_cursor);
9637 (void)fwd_word(1L, FALSE, 0);
9638 curwin->w_set_curswant = TRUE;
9639 }
9640 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009641 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642}
9643
9644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009645ins_up(
9646 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647{
9648 pos_T tpos;
9649 linenr_T old_topline = curwin->w_topline;
9650#ifdef FEAT_DIFF
9651 int old_topfill = curwin->w_topfill;
9652#endif
9653
9654 undisplay_dollar();
9655 tpos = curwin->w_cursor;
9656 if (cursor_up(1L, TRUE) == OK)
9657 {
9658 if (startcol)
9659 coladvance(getvcol_nolist(&Insstart));
9660 if (old_topline != curwin->w_topline
9661#ifdef FEAT_DIFF
9662 || old_topfill != curwin->w_topfill
9663#endif
9664 )
9665 redraw_later(VALID);
9666 start_arrow(&tpos);
9667#ifdef FEAT_CINDENT
9668 can_cindent = TRUE;
9669#endif
9670 }
9671 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009672 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673}
9674
9675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009676ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677{
9678 pos_T tpos;
9679
9680 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009681
9682#ifdef FEAT_WINDOWS
9683 if (mod_mask & MOD_MASK_CTRL)
9684 {
9685 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009686 if (first_tabpage->tp_next != NULL)
9687 {
9688 start_arrow(&curwin->w_cursor);
9689 goto_tabpage(-1);
9690 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009691 return;
9692 }
9693#endif
9694
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695 tpos = curwin->w_cursor;
9696 if (onepage(BACKWARD, 1L) == OK)
9697 {
9698 start_arrow(&tpos);
9699#ifdef FEAT_CINDENT
9700 can_cindent = TRUE;
9701#endif
9702 }
9703 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009704 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705}
9706
9707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009708ins_down(
9709 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710{
9711 pos_T tpos;
9712 linenr_T old_topline = curwin->w_topline;
9713#ifdef FEAT_DIFF
9714 int old_topfill = curwin->w_topfill;
9715#endif
9716
9717 undisplay_dollar();
9718 tpos = curwin->w_cursor;
9719 if (cursor_down(1L, TRUE) == OK)
9720 {
9721 if (startcol)
9722 coladvance(getvcol_nolist(&Insstart));
9723 if (old_topline != curwin->w_topline
9724#ifdef FEAT_DIFF
9725 || old_topfill != curwin->w_topfill
9726#endif
9727 )
9728 redraw_later(VALID);
9729 start_arrow(&tpos);
9730#ifdef FEAT_CINDENT
9731 can_cindent = TRUE;
9732#endif
9733 }
9734 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009735 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009736}
9737
9738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009739ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
9741 pos_T tpos;
9742
9743 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009744
9745#ifdef FEAT_WINDOWS
9746 if (mod_mask & MOD_MASK_CTRL)
9747 {
9748 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009749 if (first_tabpage->tp_next != NULL)
9750 {
9751 start_arrow(&curwin->w_cursor);
9752 goto_tabpage(0);
9753 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009754 return;
9755 }
9756#endif
9757
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758 tpos = curwin->w_cursor;
9759 if (onepage(FORWARD, 1L) == OK)
9760 {
9761 start_arrow(&tpos);
9762#ifdef FEAT_CINDENT
9763 can_cindent = TRUE;
9764#endif
9765 }
9766 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009767 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768}
9769
9770#ifdef FEAT_DND
9771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009772ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773{
9774 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9775}
9776#endif
9777
9778/*
9779 * Handle TAB in Insert or Replace mode.
9780 * Return TRUE when the TAB needs to be inserted like a normal character.
9781 */
9782 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009783ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
9785 int ind;
9786 int i;
9787 int temp;
9788
9789 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9790 Insstart_blank_vcol = get_nolist_virtcol();
9791 if (echeck_abbr(TAB + ABBR_OFF))
9792 return FALSE;
9793
9794 ind = inindent(0);
9795#ifdef FEAT_CINDENT
9796 if (ind)
9797 can_cindent = FALSE;
9798#endif
9799
9800 /*
9801 * When nothing special, insert TAB like a normal character
9802 */
9803 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009804 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009805 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806 return TRUE;
9807
9808 if (stop_arrow() == FAIL)
9809 return TRUE;
9810
9811 did_ai = FALSE;
9812#ifdef FEAT_SMARTINDENT
9813 did_si = FALSE;
9814 can_si = FALSE;
9815 can_si_back = FALSE;
9816#endif
9817 AppendToRedobuff((char_u *)"\t");
9818
9819 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009820 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009821 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9822 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 else /* otherwise use 'tabstop' */
9824 temp = (int)curbuf->b_p_ts;
9825 temp -= get_nolist_virtcol() % temp;
9826
9827 /*
9828 * Insert the first space with ins_char(). It will delete one char in
9829 * replace mode. Insert the rest with ins_str(); it will not delete any
9830 * chars. For VREPLACE mode, we use ins_char() for all characters.
9831 */
9832 ins_char(' ');
9833 while (--temp > 0)
9834 {
9835#ifdef FEAT_VREPLACE
9836 if (State & VREPLACE_FLAG)
9837 ins_char(' ');
9838 else
9839#endif
9840 {
9841 ins_str((char_u *)" ");
9842 if (State & REPLACE_FLAG) /* no char replaced */
9843 replace_push(NUL);
9844 }
9845 }
9846
9847 /*
9848 * When 'expandtab' not set: Replace spaces by TABs where possible.
9849 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009850 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851 {
9852 char_u *ptr;
9853#ifdef FEAT_VREPLACE
9854 char_u *saved_line = NULL; /* init for GCC */
9855 pos_T pos;
9856#endif
9857 pos_T fpos;
9858 pos_T *cursor;
9859 colnr_T want_vcol, vcol;
9860 int change_col = -1;
9861 int save_list = curwin->w_p_list;
9862
9863 /*
9864 * Get the current line. For VREPLACE mode, don't make real changes
9865 * yet, just work on a copy of the line.
9866 */
9867#ifdef FEAT_VREPLACE
9868 if (State & VREPLACE_FLAG)
9869 {
9870 pos = curwin->w_cursor;
9871 cursor = &pos;
9872 saved_line = vim_strsave(ml_get_curline());
9873 if (saved_line == NULL)
9874 return FALSE;
9875 ptr = saved_line + pos.col;
9876 }
9877 else
9878#endif
9879 {
9880 ptr = ml_get_cursor();
9881 cursor = &curwin->w_cursor;
9882 }
9883
9884 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9885 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9886 curwin->w_p_list = FALSE;
9887
9888 /* Find first white before the cursor */
9889 fpos = curwin->w_cursor;
9890 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9891 {
9892 --fpos.col;
9893 --ptr;
9894 }
9895
9896 /* In Replace mode, don't change characters before the insert point. */
9897 if ((State & REPLACE_FLAG)
9898 && fpos.lnum == Insstart.lnum
9899 && fpos.col < Insstart.col)
9900 {
9901 ptr += Insstart.col - fpos.col;
9902 fpos.col = Insstart.col;
9903 }
9904
9905 /* compute virtual column numbers of first white and cursor */
9906 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9907 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9908
Bram Moolenaar597a4222014-06-25 14:39:50 +02009909 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9910 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 while (vim_iswhite(*ptr))
9912 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009913 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009914 if (vcol + i > want_vcol)
9915 break;
9916 if (*ptr != TAB)
9917 {
9918 *ptr = TAB;
9919 if (change_col < 0)
9920 {
9921 change_col = fpos.col; /* Column of first change */
9922 /* May have to adjust Insstart */
9923 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9924 Insstart.col = fpos.col;
9925 }
9926 }
9927 ++fpos.col;
9928 ++ptr;
9929 vcol += i;
9930 }
9931
9932 if (change_col >= 0)
9933 {
9934 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009935 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936
9937 /* Skip over the spaces we need. */
9938 while (vcol < want_vcol && *ptr == ' ')
9939 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009940 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 ++ptr;
9942 ++repl_off;
9943 }
9944 if (vcol > want_vcol)
9945 {
9946 /* Must have a char with 'showbreak' just before it. */
9947 --ptr;
9948 --repl_off;
9949 }
9950 fpos.col += repl_off;
9951
9952 /* Delete following spaces. */
9953 i = cursor->col - fpos.col;
9954 if (i > 0)
9955 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009956 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 /* correct replace stack. */
9958 if ((State & REPLACE_FLAG)
9959#ifdef FEAT_VREPLACE
9960 && !(State & VREPLACE_FLAG)
9961#endif
9962 )
9963 for (temp = i; --temp >= 0; )
9964 replace_join(repl_off);
9965 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009966#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009967 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009968 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009969 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009970 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9971 (char_u *)"\t", 1);
9972 }
9973#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009974 cursor->col -= i;
9975
9976#ifdef FEAT_VREPLACE
9977 /*
9978 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9979 * backspacing over the changed spacing and then inserting the new
9980 * spacing.
9981 */
9982 if (State & VREPLACE_FLAG)
9983 {
9984 /* Backspace from real cursor to change_col */
9985 backspace_until_column(change_col);
9986
9987 /* Insert each char in saved_line from changed_col to
9988 * ptr-cursor */
9989 ins_bytes_len(saved_line + change_col,
9990 cursor->col - change_col);
9991 }
9992#endif
9993 }
9994
9995#ifdef FEAT_VREPLACE
9996 if (State & VREPLACE_FLAG)
9997 vim_free(saved_line);
9998#endif
9999 curwin->w_p_list = save_list;
10000 }
10001
10002 return FALSE;
10003}
10004
10005/*
10006 * Handle CR or NL in insert mode.
10007 * Return TRUE when out of memory or can't undo.
10008 */
10009 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010010ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011{
10012 int i;
10013
10014 if (echeck_abbr(c + ABBR_OFF))
10015 return FALSE;
10016 if (stop_arrow() == FAIL)
10017 return TRUE;
10018 undisplay_dollar();
10019
10020 /*
10021 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10022 * character under the cursor. Only push a NUL on the replace stack,
10023 * nothing to put back when the NL is deleted.
10024 */
10025 if ((State & REPLACE_FLAG)
10026#ifdef FEAT_VREPLACE
10027 && !(State & VREPLACE_FLAG)
10028#endif
10029 )
10030 replace_push(NUL);
10031
10032 /*
10033 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10034 * replacing the next line, so we push all of the characters left on the
10035 * line onto the replace stack. This is not done here though, it is done
10036 * in open_line().
10037 */
10038
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010039#ifdef FEAT_VIRTUALEDIT
10040 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10041 * CTRL-O). */
10042 if (virtual_active() && curwin->w_cursor.coladd > 0)
10043 coladvance(getviscol());
10044#endif
10045
Bram Moolenaar071d4272004-06-13 20:20:40 +000010046#ifdef FEAT_RIGHTLEFT
10047# ifdef FEAT_FKMAP
10048 if (p_altkeymap && p_fkmap)
10049 fkmap(NL);
10050# endif
10051 /* NL in reverse insert will always start in the end of
10052 * current line. */
10053 if (revins_on)
10054 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10055#endif
10056
10057 AppendToRedobuff(NL_STR);
10058 i = open_line(FORWARD,
10059#ifdef FEAT_COMMENTS
10060 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10061#endif
10062 0, old_indent);
10063 old_indent = 0;
10064#ifdef FEAT_CINDENT
10065 can_cindent = TRUE;
10066#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010067#ifdef FEAT_FOLDING
10068 /* When inserting a line the cursor line must never be in a closed fold. */
10069 foldOpenCursor();
10070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010071
10072 return (!i);
10073}
10074
10075#ifdef FEAT_DIGRAPHS
10076/*
10077 * Handle digraph in insert mode.
10078 * Returns character still to be inserted, or NUL when nothing remaining to be
10079 * done.
10080 */
10081 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010082ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083{
10084 int c;
10085 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010086 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087
10088 pc_status = PC_STATUS_UNSET;
10089 if (redrawing() && !char_avail())
10090 {
10091 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010092 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010093
10094 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010095 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010096#ifdef FEAT_CMDL_INFO
10097 add_to_showcmd_c(Ctrl_K);
10098#endif
10099 }
10100
10101#ifdef USE_ON_FLY_SCROLL
10102 dont_scroll = TRUE; /* disallow scrolling here */
10103#endif
10104
10105 /* don't map the digraph chars. This also prevents the
10106 * mode message to be deleted when ESC is hit */
10107 ++no_mapping;
10108 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010109 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110 --no_mapping;
10111 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010112 if (did_putchar)
10113 /* when the line fits in 'columns' the '?' is at the start of the next
10114 * line and will not be removed by the redraw */
10115 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010116
Bram Moolenaar071d4272004-06-13 20:20:40 +000010117 if (IS_SPECIAL(c) || mod_mask) /* special key */
10118 {
10119#ifdef FEAT_CMDL_INFO
10120 clear_showcmd();
10121#endif
10122 insert_special(c, TRUE, FALSE);
10123 return NUL;
10124 }
10125 if (c != ESC)
10126 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010127 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 if (redrawing() && !char_avail())
10129 {
10130 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010131 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132
10133 if (char2cells(c) == 1)
10134 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010135 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010137 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010138 }
10139#ifdef FEAT_CMDL_INFO
10140 add_to_showcmd_c(c);
10141#endif
10142 }
10143 ++no_mapping;
10144 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010145 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010146 --no_mapping;
10147 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010148 if (did_putchar)
10149 /* when the line fits in 'columns' the '?' is at the start of the
10150 * next line and will not be removed by a redraw */
10151 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152 if (cc != ESC)
10153 {
10154 AppendToRedobuff((char_u *)CTRL_V_STR);
10155 c = getdigraph(c, cc, TRUE);
10156#ifdef FEAT_CMDL_INFO
10157 clear_showcmd();
10158#endif
10159 return c;
10160 }
10161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010162#ifdef FEAT_CMDL_INFO
10163 clear_showcmd();
10164#endif
10165 return NUL;
10166}
10167#endif
10168
10169/*
10170 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10171 * Returns the char to be inserted, or NUL if none found.
10172 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010173 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010174ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175{
10176 int c;
10177 int temp;
10178 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010179 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180
10181 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10182 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010183 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 return NUL;
10185 }
10186
10187 /* try to advance to the cursor column */
10188 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010189 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 prev_ptr = ptr;
10191 validate_virtcol();
10192 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10193 {
10194 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010195 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010196 }
10197 if ((colnr_T)temp > curwin->w_virtcol)
10198 ptr = prev_ptr;
10199
10200#ifdef FEAT_MBYTE
10201 c = (*mb_ptr2char)(ptr);
10202#else
10203 c = *ptr;
10204#endif
10205 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010206 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010207 return c;
10208}
10209
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010210/*
10211 * CTRL-Y or CTRL-E typed in Insert mode.
10212 */
10213 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010214ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010215{
10216 int c = tc;
10217
10218#ifdef FEAT_INS_EXPAND
10219 if (ctrl_x_mode == CTRL_X_SCROLL)
10220 {
10221 if (c == Ctrl_Y)
10222 scrolldown_clamp();
10223 else
10224 scrollup_clamp();
10225 redraw_later(VALID);
10226 }
10227 else
10228#endif
10229 {
10230 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10231 if (c != NUL)
10232 {
10233 long tw_save;
10234
10235 /* The character must be taken literally, insert like it
10236 * was typed after a CTRL-V, and pretend 'textwidth'
10237 * wasn't set. Digits, 'o' and 'x' are special after a
10238 * CTRL-V, don't use it for these. */
10239 if (c < 256 && !isalnum(c))
10240 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10241 tw_save = curbuf->b_p_tw;
10242 curbuf->b_p_tw = -1;
10243 insert_special(c, TRUE, FALSE);
10244 curbuf->b_p_tw = tw_save;
10245#ifdef FEAT_RIGHTLEFT
10246 revins_chars++;
10247 revins_legal++;
10248#endif
10249 c = Ctrl_V; /* pretend CTRL-V is last character */
10250 auto_format(FALSE, TRUE);
10251 }
10252 }
10253 return c;
10254}
10255
Bram Moolenaar071d4272004-06-13 20:20:40 +000010256#ifdef FEAT_SMARTINDENT
10257/*
10258 * Try to do some very smart auto-indenting.
10259 * Used when inserting a "normal" character.
10260 */
10261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010262ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263{
10264 pos_T *pos, old_pos;
10265 char_u *ptr;
10266 int i;
10267 int temp;
10268
10269 /*
10270 * do some very smart indenting when entering '{' or '}'
10271 */
10272 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10273 {
10274 /*
10275 * for '}' set indent equal to indent of line containing matching '{'
10276 */
10277 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10278 {
10279 old_pos = curwin->w_cursor;
10280 /*
10281 * If the matching '{' has a ')' immediately before it (ignoring
10282 * white-space), then line up with the start of the line
10283 * containing the matching '(' if there is one. This handles the
10284 * case where an "if (..\n..) {" statement continues over multiple
10285 * lines -- webb
10286 */
10287 ptr = ml_get(pos->lnum);
10288 i = pos->col;
10289 if (i > 0) /* skip blanks before '{' */
10290 while (--i > 0 && vim_iswhite(ptr[i]))
10291 ;
10292 curwin->w_cursor.lnum = pos->lnum;
10293 curwin->w_cursor.col = i;
10294 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10295 curwin->w_cursor = *pos;
10296 i = get_indent();
10297 curwin->w_cursor = old_pos;
10298#ifdef FEAT_VREPLACE
10299 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010300 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010301 else
10302#endif
10303 (void)set_indent(i, SIN_CHANGED);
10304 }
10305 else if (curwin->w_cursor.col > 0)
10306 {
10307 /*
10308 * when inserting '{' after "O" reduce indent, but not
10309 * more than indent of previous line
10310 */
10311 temp = TRUE;
10312 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10313 {
10314 old_pos = curwin->w_cursor;
10315 i = get_indent();
10316 while (curwin->w_cursor.lnum > 1)
10317 {
10318 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10319
10320 /* ignore empty lines and lines starting with '#'. */
10321 if (*ptr != '#' && *ptr != NUL)
10322 break;
10323 }
10324 if (get_indent() >= i)
10325 temp = FALSE;
10326 curwin->w_cursor = old_pos;
10327 }
10328 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010329 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010330 }
10331 }
10332
10333 /*
10334 * set indent of '#' always to 0
10335 */
10336 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10337 {
10338 /* remember current indent for next line */
10339 old_indent = get_indent();
10340 (void)set_indent(0, SIN_CHANGED);
10341 }
10342
10343 /* Adjust ai_col, the char at this position can be deleted. */
10344 if (ai_col > curwin->w_cursor.col)
10345 ai_col = curwin->w_cursor.col;
10346}
10347#endif
10348
10349/*
10350 * Get the value that w_virtcol would have when 'list' is off.
10351 * Unless 'cpo' contains the 'L' flag.
10352 */
10353 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010354get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010355{
10356 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10357 return getvcol_nolist(&curwin->w_cursor);
10358 validate_virtcol();
10359 return curwin->w_virtcol;
10360}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010361
10362#ifdef FEAT_AUTOCMD
10363/*
10364 * Handle the InsertCharPre autocommand.
10365 * "c" is the character that was typed.
10366 * Return a pointer to allocated memory with the replacement string.
10367 * Return NULL to continue inserting "c".
10368 */
10369 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010370do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010371{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010372 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010373 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010374
10375 /* Return quickly when there is nothing to do. */
10376 if (!has_insertcharpre())
10377 return NULL;
10378
Bram Moolenaar704984a2012-06-01 14:57:51 +020010379#ifdef FEAT_MBYTE
10380 if (has_mbyte)
10381 buf[(*mb_char2bytes)(c, buf)] = NUL;
10382 else
10383#endif
10384 {
10385 buf[0] = c;
10386 buf[1] = NUL;
10387 }
10388
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010389 /* Lock the text to avoid weird things from happening. */
10390 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010391 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010392
Bram Moolenaar704984a2012-06-01 14:57:51 +020010393 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010394 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010395 {
10396 /* Get the value of v:char. It may be empty or more than one
10397 * character. Only use it when changed, otherwise continue with the
10398 * original character to avoid breaking autoindent. */
10399 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10400 res = vim_strsave(get_vim_var_str(VV_CHAR));
10401 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010402
10403 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10404 --textlock;
10405
10406 return res;
10407}
10408#endif