blob: 73ed472d7e32eaf3183af1c0ba8f7e8b3545262a [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. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001529 if (c != K_CURSORHOLD
1530# ifdef FEAT_COMPL_FUNC
1531 /* but not in CTRL-X mode, a script can't restore the state */
1532 && ctrl_x_mode == 0
1533# endif
1534 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001535 did_cursorhold = FALSE;
1536#endif
1537
Bram Moolenaar071d4272004-06-13 20:20:40 +00001538 /* If the cursor was moved we didn't just insert a space */
1539 if (arrow_used)
1540 inserted_space = FALSE;
1541
1542#ifdef FEAT_CINDENT
1543 if (can_cindent && cindent_on()
1544# ifdef FEAT_INS_EXPAND
1545 && ctrl_x_mode == 0
1546# endif
1547 )
1548 {
1549force_cindent:
1550 /*
1551 * Indent now if a key was typed that is in 'cinkeys'.
1552 */
1553 if (in_cinkeys(c, ' ', line_is_white))
1554 {
1555 if (stop_arrow() == OK)
1556 /* re-indent the current line */
1557 do_c_expr_indent();
1558 }
1559 }
1560#endif /* FEAT_CINDENT */
1561
1562 } /* for (;;) */
1563 /* NOTREACHED */
1564}
1565
1566/*
1567 * Redraw for Insert mode.
1568 * This is postponed until getting the next character to make '$' in the 'cpo'
1569 * option work correctly.
1570 * Only redraw when there are no characters available. This speeds up
1571 * inserting sequences of characters (e.g., for CTRL-R).
1572 */
1573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001574ins_redraw(
1575 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001576{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001577#ifdef FEAT_CONCEAL
1578 linenr_T conceal_old_cursor_line = 0;
1579 linenr_T conceal_new_cursor_line = 0;
1580 int conceal_update_lines = FALSE;
1581#endif
1582
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001583 if (char_avail())
1584 return;
1585
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001586#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001587 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1588 * visible, the command might delete it. */
1589 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001590# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001591 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001592# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001593# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001594 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001595# endif
1596# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001597 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001598# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001599 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001600# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001601 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001602# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001603# ifdef FEAT_INS_EXPAND
1604 && !pum_visible()
1605# endif
1606 )
1607 {
1608# ifdef FEAT_SYN_HL
1609 /* Need to update the screen first, to make sure syntax
1610 * highlighting is correct after making a change (e.g., inserting
1611 * a "(". The autocommand may also require a redraw, so it's done
1612 * again below, unfortunately. */
1613 if (syntax_present(curwin) && must_redraw)
1614 update_screen(0);
1615# endif
1616# ifdef FEAT_AUTOCMD
1617 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001618 {
1619 /* Make sure curswant is correct, an autocommand may call
1620 * getcurpos(). */
1621 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001622 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001623 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624# endif
1625# ifdef FEAT_CONCEAL
1626 if (curwin->w_p_cole > 0)
1627 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001628# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001629 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001630# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001631 conceal_new_cursor_line = curwin->w_cursor.lnum;
1632 conceal_update_lines = TRUE;
1633 }
1634# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001635# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001636 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001637# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001638 }
1639#endif
1640
1641#ifdef FEAT_AUTOCMD
1642 /* Trigger TextChangedI if b_changedtick differs. */
1643 if (ready && has_textchangedI()
1644 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001645# ifdef FEAT_INS_EXPAND
1646 && !pum_visible()
1647# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001648 )
1649 {
1650 if (last_changedtick_buf == curbuf)
1651 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1652 last_changedtick_buf = curbuf;
1653 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001655#endif
1656
1657 if (must_redraw)
1658 update_screen(0);
1659 else if (clear_cmdline || redraw_cmdline)
1660 showmode(); /* clear cmdline and show mode */
1661# if defined(FEAT_CONCEAL)
1662 if ((conceal_update_lines
1663 && (conceal_old_cursor_line != conceal_new_cursor_line
1664 || conceal_cursor_line(curwin)))
1665 || need_cursor_line_redraw)
1666 {
1667 if (conceal_old_cursor_line != conceal_new_cursor_line)
1668 update_single_line(curwin, conceal_old_cursor_line);
1669 update_single_line(curwin, conceal_new_cursor_line == 0
1670 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1671 curwin->w_valid &= ~VALID_CROW;
1672 }
1673# endif
1674 showruler(FALSE);
1675 setcursor();
1676 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677}
1678
1679/*
1680 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1681 */
1682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001683ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684{
1685 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001686 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687
1688 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001689 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690
1691 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001692 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001694 did_putchar = TRUE;
1695 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001696 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1697
1698#ifdef FEAT_CMDL_INFO
1699 add_to_showcmd_c(Ctrl_V);
1700#endif
1701
1702 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001703 if (did_putchar)
1704 /* when the line fits in 'columns' the '^' is at the start of the next
1705 * line and will not removed by the redraw */
1706 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707#ifdef FEAT_CMDL_INFO
1708 clear_showcmd();
1709#endif
1710 insert_special(c, FALSE, TRUE);
1711#ifdef FEAT_RIGHTLEFT
1712 revins_chars++;
1713 revins_legal++;
1714#endif
1715}
1716
1717/*
1718 * Put a character directly onto the screen. It's not stored in a buffer.
1719 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1720 */
1721static int pc_status;
1722#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1723#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1724#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1725#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001726static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001727static int pc_attr;
1728static int pc_row;
1729static int pc_col;
1730
1731 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001732edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733{
1734 int attr;
1735
1736 if (ScreenLines != NULL)
1737 {
1738 update_topline(); /* just in case w_topline isn't valid */
1739 validate_cursor();
1740 if (highlight)
1741 attr = hl_attr(HLF_8);
1742 else
1743 attr = 0;
1744 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1745 pc_col = W_WINCOL(curwin);
1746#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1747 pc_status = PC_STATUS_UNSET;
1748#endif
1749#ifdef FEAT_RIGHTLEFT
1750 if (curwin->w_p_rl)
1751 {
1752 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1753# ifdef FEAT_MBYTE
1754 if (has_mbyte)
1755 {
1756 int fix_col = mb_fix_col(pc_col, pc_row);
1757
1758 if (fix_col != pc_col)
1759 {
1760 screen_putchar(' ', pc_row, fix_col, attr);
1761 --curwin->w_wcol;
1762 pc_status = PC_STATUS_RIGHT;
1763 }
1764 }
1765# endif
1766 }
1767 else
1768#endif
1769 {
1770 pc_col += curwin->w_wcol;
1771#ifdef FEAT_MBYTE
1772 if (mb_lefthalve(pc_row, pc_col))
1773 pc_status = PC_STATUS_LEFT;
1774#endif
1775 }
1776
1777 /* save the character to be able to put it back */
1778#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1779 if (pc_status == PC_STATUS_UNSET)
1780#endif
1781 {
1782 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1783 pc_status = PC_STATUS_SET;
1784 }
1785 screen_putchar(c, pc_row, pc_col, attr);
1786 }
1787}
1788
1789/*
1790 * Undo the previous edit_putchar().
1791 */
1792 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001793edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794{
1795 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1796 {
1797#if defined(FEAT_MBYTE)
1798 if (pc_status == PC_STATUS_RIGHT)
1799 ++curwin->w_wcol;
1800 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1801 redrawWinline(curwin->w_cursor.lnum, FALSE);
1802 else
1803#endif
1804 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1805 }
1806}
1807
1808/*
1809 * Called when p_dollar is set: display a '$' at the end of the changed text
1810 * Only works when cursor is in the line that changes.
1811 */
1812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001813display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 colnr_T save_col;
1816
1817 if (!redrawing())
1818 return;
1819
1820 cursor_off();
1821 save_col = curwin->w_cursor.col;
1822 curwin->w_cursor.col = col;
1823#ifdef FEAT_MBYTE
1824 if (has_mbyte)
1825 {
1826 char_u *p;
1827
1828 /* If on the last byte of a multi-byte move to the first byte. */
1829 p = ml_get_curline();
1830 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1831 }
1832#endif
1833 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1834 if (curwin->w_wcol < W_WIDTH(curwin))
1835 {
1836 edit_putchar('$', FALSE);
1837 dollar_vcol = curwin->w_virtcol;
1838 }
1839 curwin->w_cursor.col = save_col;
1840}
1841
1842/*
1843 * Call this function before moving the cursor from the normal insert position
1844 * in insert mode.
1845 */
1846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001847undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001849 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001851 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 redrawWinline(curwin->w_cursor.lnum, FALSE);
1853 }
1854}
1855
1856/*
1857 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1858 * Keep the cursor on the same character.
1859 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1860 * type == INDENT_DEC decrease indent (for CTRL-D)
1861 * type == INDENT_SET set indent to "amount"
1862 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1863 */
1864 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001865change_indent(
1866 int type,
1867 int amount,
1868 int round,
1869 int replaced, /* replaced character, put on replace stack */
1870 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871{
1872 int vcol;
1873 int last_vcol;
1874 int insstart_less; /* reduction for Insstart.col */
1875 int new_cursor_col;
1876 int i;
1877 char_u *ptr;
1878 int save_p_list;
1879 int start_col;
1880 colnr_T vc;
1881#ifdef FEAT_VREPLACE
1882 colnr_T orig_col = 0; /* init for GCC */
1883 char_u *new_line, *orig_line = NULL; /* init for GCC */
1884
1885 /* VREPLACE mode needs to know what the line was like before changing */
1886 if (State & VREPLACE_FLAG)
1887 {
1888 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1889 orig_col = curwin->w_cursor.col;
1890 }
1891#endif
1892
1893 /* for the following tricks we don't want list mode */
1894 save_p_list = curwin->w_p_list;
1895 curwin->w_p_list = FALSE;
1896 vc = getvcol_nolist(&curwin->w_cursor);
1897 vcol = vc;
1898
1899 /*
1900 * For Replace mode we need to fix the replace stack later, which is only
1901 * possible when the cursor is in the indent. Remember the number of
1902 * characters before the cursor if it's possible.
1903 */
1904 start_col = curwin->w_cursor.col;
1905
1906 /* determine offset from first non-blank */
1907 new_cursor_col = curwin->w_cursor.col;
1908 beginline(BL_WHITE);
1909 new_cursor_col -= curwin->w_cursor.col;
1910
1911 insstart_less = curwin->w_cursor.col;
1912
1913 /*
1914 * If the cursor is in the indent, compute how many screen columns the
1915 * cursor is to the left of the first non-blank.
1916 */
1917 if (new_cursor_col < 0)
1918 vcol = get_indent() - vcol;
1919
1920 if (new_cursor_col > 0) /* can't fix replace stack */
1921 start_col = -1;
1922
1923 /*
1924 * Set the new indent. The cursor will be put on the first non-blank.
1925 */
1926 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001927 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 else
1929 {
1930#ifdef FEAT_VREPLACE
1931 int save_State = State;
1932
1933 /* Avoid being called recursively. */
1934 if (State & VREPLACE_FLAG)
1935 State = INSERT;
1936#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001937 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938#ifdef FEAT_VREPLACE
1939 State = save_State;
1940#endif
1941 }
1942 insstart_less -= curwin->w_cursor.col;
1943
1944 /*
1945 * Try to put cursor on same character.
1946 * If the cursor is at or after the first non-blank in the line,
1947 * compute the cursor column relative to the column of the first
1948 * non-blank character.
1949 * If we are not in insert mode, leave the cursor on the first non-blank.
1950 * If the cursor is before the first non-blank, position it relative
1951 * to the first non-blank, counted in screen columns.
1952 */
1953 if (new_cursor_col >= 0)
1954 {
1955 /*
1956 * When changing the indent while the cursor is touching it, reset
1957 * Insstart_col to 0.
1958 */
1959 if (new_cursor_col == 0)
1960 insstart_less = MAXCOL;
1961 new_cursor_col += curwin->w_cursor.col;
1962 }
1963 else if (!(State & INSERT))
1964 new_cursor_col = curwin->w_cursor.col;
1965 else
1966 {
1967 /*
1968 * Compute the screen column where the cursor should be.
1969 */
1970 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001971 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972
1973 /*
1974 * Advance the cursor until we reach the right screen column.
1975 */
1976 vcol = last_vcol = 0;
1977 new_cursor_col = -1;
1978 ptr = ml_get_curline();
1979 while (vcol <= (int)curwin->w_virtcol)
1980 {
1981 last_vcol = vcol;
1982#ifdef FEAT_MBYTE
1983 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001984 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 else
1986#endif
1987 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001988 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 }
1990 vcol = last_vcol;
1991
1992 /*
1993 * May need to insert spaces to be able to position the cursor on
1994 * the right screen column.
1995 */
1996 if (vcol != (int)curwin->w_virtcol)
1997 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001998 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002000 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001 if (ptr != NULL)
2002 {
2003 new_cursor_col += i;
2004 ptr[i] = NUL;
2005 while (--i >= 0)
2006 ptr[i] = ' ';
2007 ins_str(ptr);
2008 vim_free(ptr);
2009 }
2010 }
2011
2012 /*
2013 * When changing the indent while the cursor is in it, reset
2014 * Insstart_col to 0.
2015 */
2016 insstart_less = MAXCOL;
2017 }
2018
2019 curwin->w_p_list = save_p_list;
2020
2021 if (new_cursor_col <= 0)
2022 curwin->w_cursor.col = 0;
2023 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002024 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002025 curwin->w_set_curswant = TRUE;
2026 changed_cline_bef_curs();
2027
2028 /*
2029 * May have to adjust the start of the insert.
2030 */
2031 if (State & INSERT)
2032 {
2033 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2034 {
2035 if ((int)Insstart.col <= insstart_less)
2036 Insstart.col = 0;
2037 else
2038 Insstart.col -= insstart_less;
2039 }
2040 if ((int)ai_col <= insstart_less)
2041 ai_col = 0;
2042 else
2043 ai_col -= insstart_less;
2044 }
2045
2046 /*
2047 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2048 * If the number of characters before the cursor decreased, need to pop a
2049 * few characters from the replace stack.
2050 * If the number of characters before the cursor increased, need to push a
2051 * few NULs onto the replace stack.
2052 */
2053 if (REPLACE_NORMAL(State) && start_col >= 0)
2054 {
2055 while (start_col > (int)curwin->w_cursor.col)
2056 {
2057 replace_join(0); /* remove a NUL from the replace stack */
2058 --start_col;
2059 }
2060 while (start_col < (int)curwin->w_cursor.col || replaced)
2061 {
2062 replace_push(NUL);
2063 if (replaced)
2064 {
2065 replace_push(replaced);
2066 replaced = NUL;
2067 }
2068 ++start_col;
2069 }
2070 }
2071
2072#ifdef FEAT_VREPLACE
2073 /*
2074 * For VREPLACE mode, we also have to fix the replace stack. In this case
2075 * it is always possible because we backspace over the whole line and then
2076 * put it back again the way we wanted it.
2077 */
2078 if (State & VREPLACE_FLAG)
2079 {
2080 /* If orig_line didn't allocate, just return. At least we did the job,
2081 * even if you can't backspace. */
2082 if (orig_line == NULL)
2083 return;
2084
2085 /* Save new line */
2086 new_line = vim_strsave(ml_get_curline());
2087 if (new_line == NULL)
2088 return;
2089
2090 /* We only put back the new line up to the cursor */
2091 new_line[curwin->w_cursor.col] = NUL;
2092
2093 /* Put back original line */
2094 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2095 curwin->w_cursor.col = orig_col;
2096
2097 /* Backspace from cursor to start of line */
2098 backspace_until_column(0);
2099
2100 /* Insert new stuff into line again */
2101 ins_bytes(new_line);
2102
2103 vim_free(new_line);
2104 }
2105#endif
2106}
2107
2108/*
2109 * Truncate the space at the end of a line. This is to be used only in an
2110 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2111 * modes.
2112 */
2113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002114truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115{
2116 int i;
2117
2118 /* find start of trailing white space */
2119 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2120 {
2121 if (State & REPLACE_FLAG)
2122 replace_join(0); /* remove a NUL from the replace stack */
2123 }
2124 line[i + 1] = NUL;
2125}
2126
2127#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2128 || defined(FEAT_COMMENTS) || defined(PROTO)
2129/*
2130 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2131 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002132 * Will attempt not to go before "col" even when there is a composing
2133 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002134 */
2135 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002136backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137{
2138 while ((int)curwin->w_cursor.col > col)
2139 {
2140 curwin->w_cursor.col--;
2141 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002142 replace_do_bs(col);
2143 else if (!del_char_after_col(col))
2144 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 }
2146}
2147#endif
2148
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002149/*
2150 * Like del_char(), but make sure not to go before column "limit_col".
2151 * Only matters when there are composing characters.
2152 * Return TRUE when something was deleted.
2153 */
2154 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002155del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002156{
2157#ifdef FEAT_MBYTE
2158 if (enc_utf8 && limit_col >= 0)
2159 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002160 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002161
2162 /* Make sure the cursor is at the start of a character, but
2163 * skip forward again when going too far back because of a
2164 * composing character. */
2165 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002166 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002167 {
2168 int l = utf_ptr2len(ml_get_cursor());
2169
2170 if (l == 0) /* end of line */
2171 break;
2172 curwin->w_cursor.col += l;
2173 }
2174 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2175 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002176 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002177 }
2178 else
2179#endif
2180 (void)del_char(FALSE);
2181 return TRUE;
2182}
2183
Bram Moolenaar071d4272004-06-13 20:20:40 +00002184#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2185/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002186 * CTRL-X pressed in Insert mode.
2187 */
2188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002189ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002190{
2191 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2192 * CTRL-V works like CTRL-N */
2193 if (ctrl_x_mode != CTRL_X_CMDLINE)
2194 {
2195 /* if the next ^X<> won't ADD nothing, then reset
2196 * compl_cont_status */
2197 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002198 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002199 else
2200 compl_cont_status = 0;
2201 /* We're not sure which CTRL-X mode it will be yet */
2202 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2203 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2204 edit_submode_pre = NULL;
2205 showmode();
2206 }
2207}
2208
2209/*
2210 * Return TRUE if the 'dict' or 'tsr' option can be used.
2211 */
2212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002213has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002214{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002215 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002216# ifdef FEAT_SPELL
2217 && !curwin->w_p_spell
2218# endif
2219 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002220 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2221 {
2222 ctrl_x_mode = 0;
2223 edit_submode = NULL;
2224 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2225 : (char_u *)_("'thesaurus' option is empty"),
2226 hl_attr(HLF_E));
2227 if (emsg_silent == 0)
2228 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002229 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002230 setcursor();
2231 out_flush();
2232 ui_delay(2000L, FALSE);
2233 }
2234 return FALSE;
2235 }
2236 return TRUE;
2237}
2238
2239/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2241 * This depends on the current mode.
2242 */
2243 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002244vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245{
2246 /* Always allow ^R - let it's results then be checked */
2247 if (c == Ctrl_R)
2248 return TRUE;
2249
Bram Moolenaare3226be2005-12-18 22:10:00 +00002250 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002251 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002252 return TRUE;
2253
Bram Moolenaar071d4272004-06-13 20:20:40 +00002254 switch (ctrl_x_mode)
2255 {
2256 case 0: /* Not in any CTRL-X mode */
2257 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2258 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002259 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2261 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2262 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002263 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002264 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 case CTRL_X_SCROLL:
2266 return (c == Ctrl_Y || c == Ctrl_E);
2267 case CTRL_X_WHOLE_LINE:
2268 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2269 case CTRL_X_FILES:
2270 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2271 case CTRL_X_DICTIONARY:
2272 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2273 case CTRL_X_THESAURUS:
2274 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2275 case CTRL_X_TAGS:
2276 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2277#ifdef FEAT_FIND_ID
2278 case CTRL_X_PATH_PATTERNS:
2279 return (c == Ctrl_P || c == Ctrl_N);
2280 case CTRL_X_PATH_DEFINES:
2281 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2282#endif
2283 case CTRL_X_CMDLINE:
2284 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2285 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002286#ifdef FEAT_COMPL_FUNC
2287 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002288 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002289 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002290 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002291#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002292 case CTRL_X_SPELL:
2293 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002294 case CTRL_X_EVAL:
2295 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 }
2297 EMSG(_(e_internal));
2298 return FALSE;
2299}
2300
2301/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002302 * Return TRUE when character "c" is part of the item currently being
2303 * completed. Used to decide whether to abandon complete mode when the menu
2304 * is visible.
2305 */
2306 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002307ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002308{
2309 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2310 /* When expanding an identifier only accept identifier chars. */
2311 return vim_isIDc(c);
2312
2313 switch (ctrl_x_mode)
2314 {
2315 case CTRL_X_FILES:
2316 /* When expanding file name only accept file name chars. But not
2317 * path separators, so that "proto/<Tab>" expands files in
2318 * "proto", not "proto/" as a whole */
2319 return vim_isfilec(c) && !vim_ispathsep(c);
2320
2321 case CTRL_X_CMDLINE:
2322 case CTRL_X_OMNI:
2323 /* Command line and Omni completion can work with just about any
2324 * printable character, but do stop at white space. */
2325 return vim_isprintc(c) && !vim_iswhite(c);
2326
2327 case CTRL_X_WHOLE_LINE:
2328 /* For while line completion a space can be part of the line. */
2329 return vim_isprintc(c);
2330 }
2331 return vim_iswordc(c);
2332}
2333
2334/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002335 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002337 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 * the rest of the word to be in -- webb
2339 */
2340 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002341ins_compl_add_infercase(
2342 char_u *str,
2343 int len,
2344 int icase,
2345 char_u *fname,
2346 int dir,
2347 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002349 char_u *p;
2350 int i, c;
2351 int actual_len; /* Take multi-byte characters */
2352 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002353 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002354 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 int has_lower = FALSE;
2356 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002358 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002360 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361
Bram Moolenaara2993e12007-08-12 14:38:46 +00002362 /* Find actual length of completion. */
2363#ifdef FEAT_MBYTE
2364 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002366 p = str;
2367 actual_len = 0;
2368 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002370 mb_ptr_adv(p);
2371 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372 }
2373 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002374 else
2375#endif
2376 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377
Bram Moolenaara2993e12007-08-12 14:38:46 +00002378 /* Find actual length of original text. */
2379#ifdef FEAT_MBYTE
2380 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002382 p = compl_orig_text;
2383 actual_compl_length = 0;
2384 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002386 mb_ptr_adv(p);
2387 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 }
2389 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002390 else
2391#endif
2392 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002394 /* "actual_len" may be smaller than "actual_compl_length" when using
2395 * thesaurus, only use the minimum when comparing. */
2396 min_len = actual_len < actual_compl_length
2397 ? actual_len : actual_compl_length;
2398
Bram Moolenaara2993e12007-08-12 14:38:46 +00002399 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002400 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002401 if (wca != NULL)
2402 {
2403 p = str;
2404 for (i = 0; i < actual_len; ++i)
2405#ifdef FEAT_MBYTE
2406 if (has_mbyte)
2407 wca[i] = mb_ptr2char_adv(&p);
2408 else
2409#endif
2410 wca[i] = *(p++);
2411
2412 /* Rule 1: Were any chars converted to lower? */
2413 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002414 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002415 {
2416#ifdef FEAT_MBYTE
2417 if (has_mbyte)
2418 c = mb_ptr2char_adv(&p);
2419 else
2420#endif
2421 c = *(p++);
2422 if (MB_ISLOWER(c))
2423 {
2424 has_lower = TRUE;
2425 if (MB_ISUPPER(wca[i]))
2426 {
2427 /* Rule 1 is satisfied. */
2428 for (i = actual_compl_length; i < actual_len; ++i)
2429 wca[i] = MB_TOLOWER(wca[i]);
2430 break;
2431 }
2432 }
2433 }
2434
2435 /*
2436 * Rule 2: No lower case, 2nd consecutive letter converted to
2437 * upper case.
2438 */
2439 if (!has_lower)
2440 {
2441 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002442 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002443 {
2444#ifdef FEAT_MBYTE
2445 if (has_mbyte)
2446 c = mb_ptr2char_adv(&p);
2447 else
2448#endif
2449 c = *(p++);
2450 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2451 {
2452 /* Rule 2 is satisfied. */
2453 for (i = actual_compl_length; i < actual_len; ++i)
2454 wca[i] = MB_TOUPPER(wca[i]);
2455 break;
2456 }
2457 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2458 }
2459 }
2460
2461 /* Copy the original case of the part we typed. */
2462 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002463 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002464 {
2465#ifdef FEAT_MBYTE
2466 if (has_mbyte)
2467 c = mb_ptr2char_adv(&p);
2468 else
2469#endif
2470 c = *(p++);
2471 if (MB_ISLOWER(c))
2472 wca[i] = MB_TOLOWER(wca[i]);
2473 else if (MB_ISUPPER(c))
2474 wca[i] = MB_TOUPPER(wca[i]);
2475 }
2476
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002477 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002478 * Generate encoding specific output from wide character array.
2479 * Multi-byte characters can occupy up to five bytes more than
2480 * ASCII characters, and we also need one byte for NUL, so stay
2481 * six bytes away from the edge of IObuff.
2482 */
2483 p = IObuff;
2484 i = 0;
2485 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2486#ifdef FEAT_MBYTE
2487 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002488 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002489 else
2490#endif
2491 *(p++) = wca[i++];
2492 *p = NUL;
2493
2494 vim_free(wca);
2495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002497 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2498 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002500 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501}
2502
2503/*
2504 * Add a match to the list of matches.
2505 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002506 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002507 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002509 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002510ins_compl_add(
2511 char_u *str,
2512 int len,
2513 int icase,
2514 char_u *fname,
2515 char_u **cptext, /* extra text for popup menu or NULL */
2516 int cdir,
2517 int flags,
2518 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002520 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002521 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522
2523 ui_breakcheck();
2524 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002525 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 if (len < 0)
2527 len = (int)STRLEN(str);
2528
2529 /*
2530 * If the same match is already present, don't add it.
2531 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002532 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002534 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 do
2536 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002537 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002538 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002539 && match->cp_str[len] == NUL)
2540 return NOTDONE;
2541 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002542 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
2544
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002545 /* Remove any popup menu before changing the list of matches. */
2546 ins_compl_del_pum();
2547
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 /*
2549 * Allocate a new match structure.
2550 * Copy the values to the new match structure.
2551 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002552 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002554 return FAIL;
2555 match->cp_number = -1;
2556 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002557 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002558 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002561 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002563 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002564
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2568 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002569 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002570 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002571 && compl_curr_match->cp_fname != NULL
2572 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002573 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002574 else if (fname != NULL)
2575 {
2576 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002577 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002578 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002579 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002580 match->cp_fname = NULL;
2581 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002582
2583 if (cptext != NULL)
2584 {
2585 int i;
2586
2587 for (i = 0; i < CPT_COUNT; ++i)
2588 if (cptext[i] != NULL && *cptext[i] != NUL)
2589 match->cp_text[i] = vim_strsave(cptext[i]);
2590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591
2592 /*
2593 * Link the new match structure in the list of matches.
2594 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002595 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002596 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 else if (dir == FORWARD)
2598 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002599 match->cp_next = compl_curr_match->cp_next;
2600 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
2602 else /* BACKWARD */
2603 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002604 match->cp_next = compl_curr_match;
2605 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002607 if (match->cp_next)
2608 match->cp_next->cp_prev = match;
2609 if (match->cp_prev)
2610 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002612 compl_first_match = match;
2613 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002615 /*
2616 * Find the longest common string if still doing that.
2617 */
2618 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2619 ins_compl_longest_match(match);
2620
Bram Moolenaar071d4272004-06-13 20:20:40 +00002621 return OK;
2622}
2623
2624/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002625 * Return TRUE if "str[len]" matches with match->cp_str, considering
2626 * match->cp_icase.
2627 */
2628 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002629ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002630{
2631 if (match->cp_icase)
2632 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2633 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2634}
2635
2636/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002637 * Reduce the longest common string for match "match".
2638 */
2639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002640ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002641{
2642 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002643 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002644 int had_match;
2645
2646 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002647 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002648 /* First match, use it as a whole. */
2649 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002650 if (compl_leader != NULL)
2651 {
2652 had_match = (curwin->w_cursor.col > compl_col);
2653 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002654 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002655 ins_redraw(FALSE);
2656
2657 /* When the match isn't there (to avoid matching itself) remove it
2658 * again after redrawing. */
2659 if (!had_match)
2660 ins_compl_delete();
2661 compl_used_match = FALSE;
2662 }
2663 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002664 else
2665 {
2666 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002667 p = compl_leader;
2668 s = match->cp_str;
2669 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002670 {
2671#ifdef FEAT_MBYTE
2672 if (has_mbyte)
2673 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002674 c1 = mb_ptr2char(p);
2675 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002676 }
2677 else
2678#endif
2679 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002680 c1 = *p;
2681 c2 = *s;
2682 }
2683 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2684 : (c1 != c2))
2685 break;
2686#ifdef FEAT_MBYTE
2687 if (has_mbyte)
2688 {
2689 mb_ptr_adv(p);
2690 mb_ptr_adv(s);
2691 }
2692 else
2693#endif
2694 {
2695 ++p;
2696 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002697 }
2698 }
2699
2700 if (*p != NUL)
2701 {
2702 /* Leader was shortened, need to change the inserted text. */
2703 *p = NUL;
2704 had_match = (curwin->w_cursor.col > compl_col);
2705 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002706 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002707 ins_redraw(FALSE);
2708
2709 /* When the match isn't there (to avoid matching itself) remove it
2710 * again after redrawing. */
2711 if (!had_match)
2712 ins_compl_delete();
2713 }
2714
2715 compl_used_match = FALSE;
2716 }
2717}
2718
2719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 * Add an array of matches to the list of matches.
2721 * Frees matches[].
2722 */
2723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002724ins_compl_add_matches(
2725 int num_matches,
2726 char_u **matches,
2727 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 int i;
2730 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002731 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar572cb562005-08-05 21:35:02 +00002733 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002734 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002735 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002737 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 FreeWild(num_matches, matches);
2739}
2740
2741/* Make the completion list cyclic.
2742 * Return the number of matches (excluding the original).
2743 */
2744 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002745ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002747 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 int count = 0;
2749
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002750 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 {
2752 /*
2753 * Find the end of the list.
2754 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002755 match = compl_first_match;
2756 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002757 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002759 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 ++count;
2761 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002762 match->cp_next = compl_first_match;
2763 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
2765 return count;
2766}
2767
Bram Moolenaara94bc432006-03-10 21:42:59 +00002768/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002769 * Set variables that store noselect and noinsert behavior from the
2770 * 'completeopt' value.
2771 */
2772 void
2773completeopt_was_set()
2774{
2775 compl_no_insert = FALSE;
2776 compl_no_select = FALSE;
2777 if (strstr((char *)p_cot, "noselect") != NULL)
2778 compl_no_select = TRUE;
2779 if (strstr((char *)p_cot, "noinsert") != NULL)
2780 compl_no_insert = TRUE;
2781}
2782
2783/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002784 * Start completion for the complete() function.
2785 * "startcol" is where the matched text starts (1 is first column).
2786 * "list" is the list of matches.
2787 */
2788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002789set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002790{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002791 int save_w_wrow = curwin->w_wrow;
2792
Bram Moolenaara94bc432006-03-10 21:42:59 +00002793 /* If already doing completions stop it. */
2794 if (ctrl_x_mode != 0)
2795 ins_compl_prep(' ');
2796 ins_compl_clear();
2797
2798 if (stop_arrow() == FAIL)
2799 return;
2800
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002801 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002802 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002803 startcol = curwin->w_cursor.col;
2804 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002805 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002806 /* compl_pattern doesn't need to be set */
2807 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2808 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002809 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002810 return;
2811
Bram Moolenaare4214502015-03-05 18:08:43 +01002812 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002813
2814 ins_compl_add_list(list);
2815 compl_matches = ins_compl_make_cyclic();
2816 compl_started = TRUE;
2817 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002818 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002819
2820 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002821 if (compl_no_insert || compl_no_select)
2822 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002823 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002824 if (compl_no_select)
2825 /* Down/Up has no real effect. */
2826 ins_complete(K_UP, FALSE);
2827 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002828 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002829 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002830
2831 /* Lazily show the popup menu, unless we got interrupted. */
2832 if (!compl_interrupted)
2833 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002834 out_flush();
2835}
2836
2837
Bram Moolenaar9372a112005-12-06 19:59:18 +00002838/* "compl_match_array" points the currently displayed list of entries in the
2839 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002840static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002841static int compl_match_arraysize;
2842
2843/*
2844 * Update the screen and when there is any scrolling remove the popup menu.
2845 */
2846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002847ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002848{
2849 int h;
2850
2851 if (compl_match_array != NULL)
2852 {
2853 h = curwin->w_cline_height;
2854 update_screen(0);
2855 if (h != curwin->w_cline_height)
2856 ins_compl_del_pum();
2857 }
2858}
2859
2860/*
2861 * Remove any popup menu.
2862 */
2863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002864ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002865{
2866 if (compl_match_array != NULL)
2867 {
2868 pum_undisplay();
2869 vim_free(compl_match_array);
2870 compl_match_array = NULL;
2871 }
2872}
2873
2874/*
2875 * Return TRUE if the popup menu should be displayed.
2876 */
2877 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002878pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002879{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002880 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002881 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002882 return FALSE;
2883
2884 /* The display looks bad on a B&W display. */
2885 if (t_colors < 8
2886#ifdef FEAT_GUI
2887 && !gui.in_use
2888#endif
2889 )
2890 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002891 return TRUE;
2892}
2893
2894/*
2895 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002896 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002897 */
2898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002899pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002900{
2901 compl_T *compl;
2902 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002903
2904 /* Don't display the popup menu if there are no matches or there is only
2905 * one (ignoring the original text). */
2906 compl = compl_first_match;
2907 i = 0;
2908 do
2909 {
2910 if (compl == NULL
2911 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2912 break;
2913 compl = compl->cp_next;
2914 } while (compl != compl_first_match);
2915
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002916 if (strstr((char *)p_cot, "menuone") != NULL)
2917 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002918 return (i >= 2);
2919}
2920
2921/*
2922 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002923 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002924 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002925 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002927{
2928 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002929 compl_T *shown_compl = NULL;
2930 int did_find_shown_match = FALSE;
2931 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002932 int i;
2933 int cur = -1;
2934 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002935 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002936
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002937 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002938 return;
2939
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002940#if defined(FEAT_EVAL)
2941 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2942 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2943#endif
2944
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002945 /* Update the screen before drawing the popup menu over it. */
2946 update_screen(0);
2947
2948 if (compl_match_array == NULL)
2949 {
2950 /* Need to build the popup menu list. */
2951 compl_match_arraysize = 0;
2952 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002953 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002954 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002955 do
2956 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002957 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2958 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002959 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002960 ++compl_match_arraysize;
2961 compl = compl->cp_next;
2962 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002963 if (compl_match_arraysize == 0)
2964 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002965 compl_match_array = (pumitem_T *)alloc_clear(
2966 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002967 * compl_match_arraysize));
2968 if (compl_match_array != NULL)
2969 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002970 /* If the current match is the original text don't find the first
2971 * match after it, don't highlight anything. */
2972 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2973 shown_match_ok = TRUE;
2974
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002975 i = 0;
2976 compl = compl_first_match;
2977 do
2978 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002979 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2980 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002981 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002982 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002983 if (!shown_match_ok)
2984 {
2985 if (compl == compl_shown_match || did_find_shown_match)
2986 {
2987 /* This item is the shown match or this is the
2988 * first displayed item after the shown match. */
2989 compl_shown_match = compl;
2990 did_find_shown_match = TRUE;
2991 shown_match_ok = TRUE;
2992 }
2993 else
2994 /* Remember this displayed match for when the
2995 * shown match is just below it. */
2996 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002997 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002998 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002999
3000 if (compl->cp_text[CPT_ABBR] != NULL)
3001 compl_match_array[i].pum_text =
3002 compl->cp_text[CPT_ABBR];
3003 else
3004 compl_match_array[i].pum_text = compl->cp_str;
3005 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3006 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3007 if (compl->cp_text[CPT_MENU] != NULL)
3008 compl_match_array[i++].pum_extra =
3009 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003010 else
3011 compl_match_array[i++].pum_extra = compl->cp_fname;
3012 }
3013
3014 if (compl == compl_shown_match)
3015 {
3016 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003017
3018 /* When the original text is the shown match don't set
3019 * compl_shown_match. */
3020 if (compl->cp_flags & ORIGINAL_TEXT)
3021 shown_match_ok = TRUE;
3022
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003023 if (!shown_match_ok && shown_compl != NULL)
3024 {
3025 /* The shown match isn't displayed, set it to the
3026 * previously displayed match. */
3027 compl_shown_match = shown_compl;
3028 shown_match_ok = TRUE;
3029 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003030 }
3031 compl = compl->cp_next;
3032 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003033
3034 if (!shown_match_ok) /* no displayed match at all */
3035 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003036 }
3037 }
3038 else
3039 {
3040 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003041 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003042 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3043 || compl_match_array[i].pum_text
3044 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003045 {
3046 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003047 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003048 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003049 }
3050
3051 if (compl_match_array != NULL)
3052 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003053 /* In Replace mode when a $ is displayed at the end of the line only
3054 * part of the screen would be updated. We do need to redraw here. */
3055 dollar_vcol = -1;
3056
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003057 /* Compute the screen column of the start of the completed text.
3058 * Use the cursor to get all wrapping and other settings right. */
3059 col = curwin->w_cursor.col;
3060 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003061 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003062 curwin->w_cursor.col = col;
3063 }
3064}
3065
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066#define DICT_FIRST (1) /* use just first element in "dict" */
3067#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003070 * Add any identifiers that match the given pattern in the list of dictionary
3071 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 */
3073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003074ins_compl_dictionaries(
3075 char_u *dict_start,
3076 char_u *pat,
3077 int flags, /* DICT_FIRST and/or DICT_EXACT */
3078 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003080 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003081 char_u *ptr;
3082 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 char_u **files;
3085 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003087 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088
Bram Moolenaar0b238792006-03-02 22:49:12 +00003089 if (*dict == NUL)
3090 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003091#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003092 /* When 'dictionary' is empty and spell checking is enabled use
3093 * "spell". */
3094 if (!thesaurus && curwin->w_p_spell)
3095 dict = (char_u *)"spell";
3096 else
3097#endif
3098 return;
3099 }
3100
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003102 if (buf == NULL)
3103 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003104 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003105
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 /* If 'infercase' is set, don't use 'smartcase' here */
3107 save_p_scs = p_scs;
3108 if (curbuf->b_p_inf)
3109 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003110
3111 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3112 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003113 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003114 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003115 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003116 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003117 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003118
3119 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003120 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003121 len = STRLEN(pat_esc) + 10;
3122 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003123 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003124 {
3125 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003126 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003127 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003128 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003129 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3130 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003131 vim_free(ptr);
3132 }
3133 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003134 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003135 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003136 if (regmatch.regprog == NULL)
3137 goto theend;
3138 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003139
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3141 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003142 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 {
3144 /* copy one dictionary file name into buf */
3145 if (flags == DICT_EXACT)
3146 {
3147 count = 1;
3148 files = &dict;
3149 }
3150 else
3151 {
3152 /* Expand wildcards in the dictionary name, but do not allow
3153 * backticks (for security, the 'dict' option may have been set in
3154 * a modeline). */
3155 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003156# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003157 if (!thesaurus && STRCMP(buf, "spell") == 0)
3158 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003159 else
3160# endif
3161 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 || expand_wildcards(1, &buf, &count, &files,
3163 EW_FILE|EW_SILENT) != OK)
3164 count = 0;
3165 }
3166
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003167# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003168 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003170 /* Complete from active spelling. Skip "\<" in the pattern, we
3171 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003172 if (pat[0] == '\\' && pat[1] == '<')
3173 ptr = pat + 2;
3174 else
3175 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003176 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003178 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003179# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003180 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003181 {
3182 ins_compl_files(count, files, thesaurus, flags,
3183 &regmatch, buf, &dir);
3184 if (flags != DICT_EXACT)
3185 FreeWild(count, files);
3186 }
3187 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 break;
3189 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003190
3191theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003193 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 vim_free(buf);
3195}
3196
Bram Moolenaar0b238792006-03-02 22:49:12 +00003197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003198ins_compl_files(
3199 int count,
3200 char_u **files,
3201 int thesaurus,
3202 int flags,
3203 regmatch_T *regmatch,
3204 char_u *buf,
3205 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003206{
3207 char_u *ptr;
3208 int i;
3209 FILE *fp;
3210 int add_r;
3211
3212 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3213 {
3214 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3215 if (flags != DICT_EXACT)
3216 {
3217 vim_snprintf((char *)IObuff, IOSIZE,
3218 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003219 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003220 }
3221
3222 if (fp != NULL)
3223 {
3224 /*
3225 * Read dictionary file line by line.
3226 * Check each line for a match.
3227 */
3228 while (!got_int && !compl_interrupted
3229 && !vim_fgets(buf, LSIZE, fp))
3230 {
3231 ptr = buf;
3232 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3233 {
3234 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003235 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003236 ptr = find_line_end(ptr);
3237 else
3238 ptr = find_word_end(ptr);
3239 add_r = ins_compl_add_infercase(regmatch->startp[0],
3240 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003241 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003242 if (thesaurus)
3243 {
3244 char_u *wstart;
3245
3246 /*
3247 * Add the other matches on the line
3248 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003249 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003250 while (!got_int)
3251 {
3252 /* Find start of the next word. Skip white
3253 * space and punctuation. */
3254 ptr = find_word_start(ptr);
3255 if (*ptr == NUL || *ptr == NL)
3256 break;
3257 wstart = ptr;
3258
Bram Moolenaara2993e12007-08-12 14:38:46 +00003259 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003260#ifdef FEAT_MBYTE
3261 if (has_mbyte)
3262 /* Japanese words may have characters in
3263 * different classes, only separate words
3264 * with single-byte non-word characters. */
3265 while (*ptr != NUL)
3266 {
3267 int l = (*mb_ptr2len)(ptr);
3268
3269 if (l < 2 && !vim_iswordc(*ptr))
3270 break;
3271 ptr += l;
3272 }
3273 else
3274#endif
3275 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003276
3277 /* Add the word. Skip the regexp match. */
3278 if (wstart != regmatch->startp[0])
3279 add_r = ins_compl_add_infercase(wstart,
3280 (int)(ptr - wstart),
3281 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003282 }
3283 }
3284 if (add_r == OK)
3285 /* if dir was BACKWARD then honor it just once */
3286 *dir = FORWARD;
3287 else if (add_r == FAIL)
3288 break;
3289 /* avoid expensive call to vim_regexec() when at end
3290 * of line */
3291 if (*ptr == '\n' || got_int)
3292 break;
3293 }
3294 line_breakcheck();
3295 ins_compl_check_keys(50);
3296 }
3297 fclose(fp);
3298 }
3299 }
3300}
3301
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302/*
3303 * Find the start of the next word.
3304 * Returns a pointer to the first char of the word. Also stops at a NUL.
3305 */
3306 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003307find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308{
3309#ifdef FEAT_MBYTE
3310 if (has_mbyte)
3311 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003312 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 else
3314#endif
3315 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3316 ++ptr;
3317 return ptr;
3318}
3319
3320/*
3321 * Find the end of the word. Assumes it starts inside a word.
3322 * Returns a pointer to just after the word.
3323 */
3324 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003325find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003326{
3327#ifdef FEAT_MBYTE
3328 int start_class;
3329
3330 if (has_mbyte)
3331 {
3332 start_class = mb_get_class(ptr);
3333 if (start_class > 1)
3334 while (*ptr != NUL)
3335 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003336 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337 if (mb_get_class(ptr) != start_class)
3338 break;
3339 }
3340 }
3341 else
3342#endif
3343 while (vim_iswordc(*ptr))
3344 ++ptr;
3345 return ptr;
3346}
3347
3348/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003349 * Find the end of the line, omitting CR and NL at the end.
3350 * Returns a pointer to just after the line.
3351 */
3352 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003353find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003354{
3355 char_u *s;
3356
3357 s = ptr + STRLEN(ptr);
3358 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3359 --s;
3360 return s;
3361}
3362
3363/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003364 * Free the list of completions
3365 */
3366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003367ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003369 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003370 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003372 vim_free(compl_pattern);
3373 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003374 vim_free(compl_leader);
3375 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003377 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003379
3380 ins_compl_del_pum();
3381 pum_clear();
3382
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003383 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 do
3385 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003386 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003387 compl_curr_match = compl_curr_match->cp_next;
3388 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003390 if (match->cp_flags & FREE_FNAME)
3391 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003392 for (i = 0; i < CPT_COUNT; ++i)
3393 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003395 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3396 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003397 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003398}
3399
3400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003401ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003403 compl_cont_status = 0;
3404 compl_started = FALSE;
3405 compl_matches = 0;
3406 vim_free(compl_pattern);
3407 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003408 vim_free(compl_leader);
3409 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003411 vim_free(compl_orig_text);
3412 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003413 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003414 /* clear v:completed_item */
3415 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003416}
3417
3418/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003419 * Return TRUE when Insert completion is active.
3420 */
3421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003422ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003423{
3424 return compl_started;
3425}
3426
3427/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003428 * Delete one character before the cursor and show the subset of the matches
3429 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003430 * Returns the character to be used, NUL if the work is done and another char
3431 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003432 */
3433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003434ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003435{
3436 char_u *line;
3437 char_u *p;
3438
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003439 line = ml_get_curline();
3440 p = line + curwin->w_cursor.col;
3441 mb_ptr_back(line, p);
3442
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003443 /* Stop completion when the whole word was deleted. For Omni completion
3444 * allow the word to be deleted, we won't match everything. */
3445 if ((int)(p - line) - (int)compl_col < 0
3446 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003447 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003448 return K_BS;
3449
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003450 /* Deleted more than what was used to find matches or didn't finish
3451 * finding all matches: need to look for matches all over again. */
3452 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003453 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003454 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003455
Bram Moolenaara6557602006-02-04 22:43:20 +00003456 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003457 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003458 if (compl_leader != NULL)
3459 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003460 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003461 if (compl_shown_match != NULL)
3462 /* Make sure current match is not a hidden item. */
3463 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003464 return NUL;
3465 }
3466 return K_BS;
3467}
Bram Moolenaara6557602006-02-04 22:43:20 +00003468
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003469/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003470 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3471 * be called.
3472 */
3473 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003474ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003475{
3476 /* Return TRUE if we didn't complete finding matches or when the
3477 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3478 return compl_was_interrupted
3479 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3480 && compl_opt_refresh_always);
3481}
3482
3483/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003484 * Called after changing "compl_leader".
3485 * Show the popup menu with a different set of matches.
3486 * May also search for matches again if the previous search was interrupted.
3487 */
3488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003489ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003490{
3491 ins_compl_del_pum();
3492 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003493 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003494 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003495
3496 if (compl_started)
3497 ins_compl_set_original_text(compl_leader);
3498 else
3499 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003500#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003501 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003502#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003503 /*
3504 * Matches were cleared, need to search for them now. First display
3505 * the changed text before the cursor. Set "compl_restarting" to
3506 * avoid that the first match is inserted.
3507 */
3508 update_screen(0);
3509#ifdef FEAT_GUI
3510 if (gui.in_use)
3511 {
3512 /* Show the cursor after the match, not after the redrawn text. */
3513 setcursor();
3514 out_flush();
3515 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003516 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003517#endif
3518 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003519 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003520 compl_cont_status = 0;
3521 compl_restarting = FALSE;
3522 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003523
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003524 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003525
3526 /* Show the popup menu with a different set of matches. */
3527 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003528
3529 /* Don't let Enter select the original text when there is no popup menu. */
3530 if (compl_match_array == NULL)
3531 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003532}
3533
3534/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003535 * Return the length of the completion, from the completion start column to
3536 * the cursor column. Making sure it never goes below zero.
3537 */
3538 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003539ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003540{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003541 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003542
3543 if (off < 0)
3544 return 0;
3545 return off;
3546}
3547
3548/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003549 * Append one character to the match leader. May reduce the number of
3550 * matches.
3551 */
3552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003553ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003554{
3555#ifdef FEAT_MBYTE
3556 int cc;
3557
3558 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3559 {
3560 char_u buf[MB_MAXBYTES + 1];
3561
3562 (*mb_char2bytes)(c, buf);
3563 buf[cc] = NUL;
3564 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003565 if (compl_opt_refresh_always)
3566 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003567 }
3568 else
3569#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003570 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003571 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003572 if (compl_opt_refresh_always)
3573 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003574 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003575
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003576 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003577 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003578 ins_compl_restart();
3579
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003580 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003581 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003582 * break redo. */
3583 if (!compl_opt_refresh_always)
3584 {
3585 vim_free(compl_leader);
3586 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003587 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003588 if (compl_leader != NULL)
3589 ins_compl_new_leader();
3590 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003591}
3592
3593/*
3594 * Setup for finding completions again without leaving CTRL-X mode. Used when
3595 * BS or a key was typed while still searching for matches.
3596 */
3597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003598ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003599{
3600 ins_compl_free();
3601 compl_started = FALSE;
3602 compl_matches = 0;
3603 compl_cont_status = 0;
3604 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003605}
3606
3607/*
3608 * Set the first match, the original text.
3609 */
3610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003611ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003612{
3613 char_u *p;
3614
3615 /* Replace the original text entry. */
3616 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3617 {
3618 p = vim_strsave(str);
3619 if (p != NULL)
3620 {
3621 vim_free(compl_first_match->cp_str);
3622 compl_first_match->cp_str = p;
3623 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003624 }
3625}
3626
3627/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003628 * Append one character to the match leader. May reduce the number of
3629 * matches.
3630 */
3631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003633{
3634 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003635 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003636 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003637 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003638
3639 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003640 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003641 {
3642 /* When still at the original match use the first entry that matches
3643 * the leader. */
3644 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3645 {
3646 p = NULL;
3647 for (cp = compl_shown_match->cp_next; cp != NULL
3648 && cp != compl_first_match; cp = cp->cp_next)
3649 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003650 if (compl_leader == NULL
3651 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003652 (int)STRLEN(compl_leader)))
3653 {
3654 p = cp->cp_str;
3655 break;
3656 }
3657 }
3658 if (p == NULL || (int)STRLEN(p) <= len)
3659 return;
3660 }
3661 else
3662 return;
3663 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003664 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003665 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003666 ins_compl_addleader(c);
3667}
3668
3669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003671 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003672 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003673 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003675ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003676{
3677 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003679 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680
3681 /* Forget any previous 'special' messages if this is actually
3682 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3683 */
3684 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3685 edit_submode_extra = NULL;
3686
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003687 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003688 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3689 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003690 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003692 /* Set "compl_get_longest" when finding the first matches. */
3693 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3694 || (ctrl_x_mode == 0 && !compl_started))
3695 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003696 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003697 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003698
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003699 }
3700
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3702 {
3703 /*
3704 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3705 * it will be yet. Now we decide.
3706 */
3707 switch (c)
3708 {
3709 case Ctrl_E:
3710 case Ctrl_Y:
3711 ctrl_x_mode = CTRL_X_SCROLL;
3712 if (!(State & REPLACE_FLAG))
3713 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3714 else
3715 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3716 edit_submode_pre = NULL;
3717 showmode();
3718 break;
3719 case Ctrl_L:
3720 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3721 break;
3722 case Ctrl_F:
3723 ctrl_x_mode = CTRL_X_FILES;
3724 break;
3725 case Ctrl_K:
3726 ctrl_x_mode = CTRL_X_DICTIONARY;
3727 break;
3728 case Ctrl_R:
3729 /* Simply allow ^R to happen without affecting ^X mode */
3730 break;
3731 case Ctrl_T:
3732 ctrl_x_mode = CTRL_X_THESAURUS;
3733 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003734#ifdef FEAT_COMPL_FUNC
3735 case Ctrl_U:
3736 ctrl_x_mode = CTRL_X_FUNCTION;
3737 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003738 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003739 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003740 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003741#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003742 case 's':
3743 case Ctrl_S:
3744 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003745#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003746 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003747 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003748 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003749#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003750 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 case Ctrl_RSB:
3752 ctrl_x_mode = CTRL_X_TAGS;
3753 break;
3754#ifdef FEAT_FIND_ID
3755 case Ctrl_I:
3756 case K_S_TAB:
3757 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3758 break;
3759 case Ctrl_D:
3760 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3761 break;
3762#endif
3763 case Ctrl_V:
3764 case Ctrl_Q:
3765 ctrl_x_mode = CTRL_X_CMDLINE;
3766 break;
3767 case Ctrl_P:
3768 case Ctrl_N:
3769 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3770 * just started ^X mode, or there were enough ^X's to cancel
3771 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3772 * do normal expansion when interrupting a different mode (say
3773 * ^X^F^X^P or ^P^X^X^P, see below)
3774 * nothing changes if interrupting mode 0, (eg, the flag
3775 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003776 if (!(compl_cont_status & CONT_INTRPT))
3777 compl_cont_status |= CONT_LOCAL;
3778 else if (compl_cont_mode != 0)
3779 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 /* FALLTHROUGH */
3781 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003782 /* If we have typed at least 2 ^X's... for modes != 0, we set
3783 * compl_cont_status = 0 (eg, as if we had just started ^X
3784 * mode).
3785 * For mode 0, we set "compl_cont_mode" to an impossible
3786 * value, in both cases ^X^X can be used to restart the same
3787 * mode (avoiding ADDING mode).
3788 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3789 * 'complete' and local ^P expansions respectively.
3790 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3791 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 if (c == Ctrl_X)
3793 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003794 if (compl_cont_mode != 0)
3795 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003797 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 }
3799 ctrl_x_mode = 0;
3800 edit_submode = NULL;
3801 showmode();
3802 break;
3803 }
3804 }
3805 else if (ctrl_x_mode != 0)
3806 {
3807 /* We're already in CTRL-X mode, do we stay in it? */
3808 if (!vim_is_ctrl_x_key(c))
3809 {
3810 if (ctrl_x_mode == CTRL_X_SCROLL)
3811 ctrl_x_mode = 0;
3812 else
3813 ctrl_x_mode = CTRL_X_FINISHED;
3814 edit_submode = NULL;
3815 }
3816 showmode();
3817 }
3818
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003819 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 {
3821 /* Show error message from attempted keyword completion (probably
3822 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003823 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003825 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3826 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 || ctrl_x_mode == CTRL_X_FINISHED)
3828 {
3829 /* Get here when we have finished typing a sequence of ^N and
3830 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003831 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003832 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 {
3834 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003835 * If any of the original typed text has been changed, eg when
3836 * ignorecase is set, we must add back-spaces to the redo
3837 * buffer. We add as few as necessary to delete just the part
3838 * of the original text that has changed.
3839 * When using the longest match, edited the match or used
3840 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003842 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3843 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003844 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003845 ptr = NULL;
3846 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 }
3848
3849#ifdef FEAT_CINDENT
3850 want_cindent = (can_cindent && cindent_on());
3851#endif
3852 /*
3853 * When completing whole lines: fix indent for 'cindent'.
3854 * Otherwise, break line if it's too long.
3855 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003856 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 {
3858#ifdef FEAT_CINDENT
3859 /* re-indent the current line */
3860 if (want_cindent)
3861 {
3862 do_c_expr_indent();
3863 want_cindent = FALSE; /* don't do it again */
3864 }
3865#endif
3866 }
3867 else
3868 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003869 int prev_col = curwin->w_cursor.col;
3870
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003872 if (prev_col > 0)
3873 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 if (stop_arrow() == OK)
3875 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003876 if (prev_col > 0
3877 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3878 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
3880
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003881 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003882 * the selection without inserting anything. When
3883 * compl_enter_selects is set the Enter key does the same. */
3884 if ((c == Ctrl_Y || (compl_enter_selects
3885 && (c == CAR || c == K_KENTER || c == NL)))
3886 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003887 retval = TRUE;
3888
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003889 /* CTRL-E means completion is Ended, go back to the typed text. */
3890 if (c == Ctrl_E)
3891 {
3892 ins_compl_delete();
3893 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003894 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003895 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003896 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003897 retval = TRUE;
3898 }
3899
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003900 auto_format(FALSE, TRUE);
3901
Bram Moolenaar071d4272004-06-13 20:20:40 +00003902 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003903 compl_started = FALSE;
3904 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003905 if (!shortmess(SHM_COMPLETIONMENU))
3906 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003908 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 if (edit_submode != NULL)
3910 {
3911 edit_submode = NULL;
3912 showmode();
3913 }
3914
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003915#ifdef FEAT_CMDWIN
3916 if (c == Ctrl_C && cmdwin_type != 0)
3917 /* Avoid the popup menu remains displayed when leaving the
3918 * command line window. */
3919 update_screen(0);
3920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921#ifdef FEAT_CINDENT
3922 /*
3923 * Indent now if a key was typed that is in 'cinkeys'.
3924 */
3925 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3926 do_c_expr_indent();
3927#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003928#ifdef FEAT_AUTOCMD
3929 /* Trigger the CompleteDone event to give scripts a chance to act
3930 * upon the completion. */
3931 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
3934 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003935#ifdef FEAT_AUTOCMD
3936 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3937 /* Trigger the CompleteDone event to give scripts a chance to act
3938 * upon the (possibly failed) completion. */
3939 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941
3942 /* reset continue_* if we left expansion-mode, if we stay they'll be
3943 * (re)set properly in ins_complete() */
3944 if (!vim_is_ctrl_x_key(c))
3945 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003946 compl_cont_status = 0;
3947 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003949
3950 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951}
3952
3953/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003954 * Fix the redo buffer for the completion leader replacing some of the typed
3955 * text. This inserts backspaces and appends the changed text.
3956 * "ptr" is the known leader text or NUL.
3957 */
3958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003959ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003960{
3961 int len;
3962 char_u *p;
3963 char_u *ptr = ptr_arg;
3964
3965 if (ptr == NULL)
3966 {
3967 if (compl_leader != NULL)
3968 ptr = compl_leader;
3969 else
3970 return; /* nothing to do */
3971 }
3972 if (compl_orig_text != NULL)
3973 {
3974 p = compl_orig_text;
3975 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3976 ;
3977#ifdef FEAT_MBYTE
3978 if (len > 0)
3979 len -= (*mb_head_off)(p, p + len);
3980#endif
3981 for (p += len; *p != NUL; mb_ptr_adv(p))
3982 AppendCharToRedobuff(K_BS);
3983 }
3984 else
3985 len = 0;
3986 if (ptr != NULL)
3987 AppendToRedobuffLit(ptr + len, -1);
3988}
3989
3990/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3992 * (depending on flag) starting from buf and looking for a non-scanned
3993 * buffer (other than curbuf). curbuf is special, if it is called with
3994 * buf=curbuf then it has to be the first call for a given flag/expansion.
3995 *
3996 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3997 */
3998 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003999ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004000{
4001#ifdef FEAT_WINDOWS
4002 static win_T *wp;
4003#endif
4004
4005 if (flag == 'w') /* just windows */
4006 {
4007#ifdef FEAT_WINDOWS
4008 if (buf == curbuf) /* first call for this flag/expansion */
4009 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004010 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 && wp->w_buffer->b_scanned)
4012 ;
4013 buf = wp->w_buffer;
4014#else
4015 buf = curbuf;
4016#endif
4017 }
4018 else
4019 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4020 * (unlisted buffers)
4021 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004022 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023 && ((flag == 'U'
4024 ? buf->b_p_bl
4025 : (!buf->b_p_bl
4026 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004027 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 ;
4029 return buf;
4030}
4031
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004032#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004033static void expand_by_function(int type, char_u *base);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004034
4035/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004036 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004037 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004038 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004040expand_by_function(
4041 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4042 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004043{
Bram Moolenaar82139082011-09-14 16:52:09 +02004044 list_T *matchlist = NULL;
4045 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004046 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004047 char_u *funcname;
4048 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004049 win_T *curwin_save;
4050 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004051 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004052
Bram Moolenaare344bea2005-09-01 20:46:49 +00004053 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4054 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004055 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004056
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004057 /* Call 'completefunc' to obtain the list of matches. */
4058 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004059 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004060
Bram Moolenaare344bea2005-09-01 20:46:49 +00004061 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004062 curwin_save = curwin;
4063 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004064
4065 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004066 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004067 {
4068 switch (rettv.v_type)
4069 {
4070 case VAR_LIST:
4071 matchlist = rettv.vval.v_list;
4072 break;
4073 case VAR_DICT:
4074 matchdict = rettv.vval.v_dict;
4075 break;
4076 default:
4077 /* TODO: Give error message? */
4078 clear_tv(&rettv);
4079 break;
4080 }
4081 }
4082
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004083 if (curwin_save != curwin || curbuf_save != curbuf)
4084 {
4085 EMSG(_(e_complwin));
4086 goto theend;
4087 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004088 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004089 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004090 if (!equalpos(curwin->w_cursor, pos))
4091 {
4092 EMSG(_(e_compldel));
4093 goto theend;
4094 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004095
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004096 if (matchlist != NULL)
4097 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004098 else if (matchdict != NULL)
4099 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004100
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004101theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004102 if (matchdict != NULL)
4103 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004104 if (matchlist != NULL)
4105 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004106}
4107#endif /* FEAT_COMPL_FUNC */
4108
Bram Moolenaar39f05632006-03-19 22:15:26 +00004109#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004110/*
4111 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004112 */
4113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004114ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004115{
4116 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004117 int dir = compl_direction;
4118
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004119 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004120 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004121 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004122 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4123 /* if dir was BACKWARD then honor it just once */
4124 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004125 else if (did_emsg)
4126 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004127 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004128}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004129
4130/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004131 * Add completions from a dict.
4132 */
4133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004134ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004135{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004136 dictitem_T *di_refresh;
4137 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004138
4139 /* Check for optional "refresh" item. */
4140 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004141 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4142 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004143 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004144 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004145
4146 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4147 compl_opt_refresh_always = TRUE;
4148 }
4149
4150 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004151 di_words = dict_find(dict, (char_u *)"words", 5);
4152 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4153 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004154}
4155
4156/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004157 * Add a match to the list of matches from a typeval_T.
4158 * If the given string is already in the list of completions, then return
4159 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4160 * maybe because alloc() returns NULL, then FAIL is returned.
4161 */
4162 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004163ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004164{
4165 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004166 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004167 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004168 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004169 char_u *(cptext[CPT_COUNT]);
4170
4171 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4172 {
4173 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4174 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4175 (char_u *)"abbr", FALSE);
4176 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4177 (char_u *)"menu", FALSE);
4178 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4179 (char_u *)"kind", FALSE);
4180 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4181 (char_u *)"info", FALSE);
4182 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4183 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004184 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004185 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004186 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4187 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004188 }
4189 else
4190 {
4191 word = get_tv_string_chk(tv);
4192 vim_memset(cptext, 0, sizeof(cptext));
4193 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004194 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004195 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004196 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004197}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004198#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004200/*
4201 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004202 * The search starts at position "ini" in curbuf and in the direction
4203 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004204 * When "compl_started" is FALSE start at that position, otherwise continue
4205 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004206 * This may return before finding all the matches.
4207 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 */
4209 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004210ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211{
4212 static pos_T first_match_pos;
4213 static pos_T last_match_pos;
4214 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 static int found_all = FALSE; /* Found all matches of a
4216 certain type. */
4217 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
Bram Moolenaar572cb562005-08-05 21:35:02 +00004219 pos_T *pos;
4220 char_u **matches;
4221 int save_p_scs;
4222 int save_p_ws;
4223 int save_p_ic;
4224 int i;
4225 int num_matches;
4226 int len;
4227 int found_new_match;
4228 int type = ctrl_x_mode;
4229 char_u *ptr;
4230 char_u *dict = NULL;
4231 int dict_f = 0;
4232 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004233 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004235 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 {
4237 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4238 ins_buf->b_scanned = 0;
4239 found_all = FALSE;
4240 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004242 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 last_match_pos = first_match_pos = *ini;
4244 }
4245
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004247 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4249 for (;;)
4250 {
4251 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004252 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004254 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 * or if found_all says this entry is done. For ^X^L only use the
4256 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004257 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004258 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
4260 found_all = FALSE;
4261 while (*e_cpt == ',' || *e_cpt == ' ')
4262 e_cpt++;
4263 if (*e_cpt == '.' && !curbuf->b_scanned)
4264 {
4265 ins_buf = curbuf;
4266 first_match_pos = *ini;
4267 /* So that ^N can match word immediately after cursor */
4268 if (ctrl_x_mode == 0)
4269 dec(&first_match_pos);
4270 last_match_pos = first_match_pos;
4271 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004272
4273 /* Remember the first match so that the loop stops when we
4274 * wrap and come back there a second time. */
4275 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4278 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4279 {
4280 /* Scan a buffer, but not the current one. */
4281 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4282 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004283 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 first_match_pos.col = last_match_pos.col = 0;
4285 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4286 last_match_pos.lnum = 0;
4287 type = 0;
4288 }
4289 else /* unloaded buffer, scan like dictionary */
4290 {
4291 found_all = TRUE;
4292 if (ins_buf->b_fname == NULL)
4293 continue;
4294 type = CTRL_X_DICTIONARY;
4295 dict = ins_buf->b_fname;
4296 dict_f = DICT_EXACT;
4297 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004298 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 ins_buf->b_fname == NULL
4300 ? buf_spname(ins_buf)
4301 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004302 ? ins_buf->b_fname
4303 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004304 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 else if (*e_cpt == NUL)
4307 break;
4308 else
4309 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004310 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 type = -1;
4312 else if (*e_cpt == 'k' || *e_cpt == 's')
4313 {
4314 if (*e_cpt == 'k')
4315 type = CTRL_X_DICTIONARY;
4316 else
4317 type = CTRL_X_THESAURUS;
4318 if (*++e_cpt != ',' && *e_cpt != NUL)
4319 {
4320 dict = e_cpt;
4321 dict_f = DICT_FIRST;
4322 }
4323 }
4324#ifdef FEAT_FIND_ID
4325 else if (*e_cpt == 'i')
4326 type = CTRL_X_PATH_PATTERNS;
4327 else if (*e_cpt == 'd')
4328 type = CTRL_X_PATH_DEFINES;
4329#endif
4330 else if (*e_cpt == ']' || *e_cpt == 't')
4331 {
4332 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004333 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004334 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336 else
4337 type = -1;
4338
4339 /* in any case e_cpt is advanced to the next entry */
4340 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4341
4342 found_all = TRUE;
4343 if (type == -1)
4344 continue;
4345 }
4346 }
4347
4348 switch (type)
4349 {
4350 case -1:
4351 break;
4352#ifdef FEAT_FIND_ID
4353 case CTRL_X_PATH_PATTERNS:
4354 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004355 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004356 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004358 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4360 (linenr_T)1, (linenr_T)MAXLNUM);
4361 break;
4362#endif
4363
4364 case CTRL_X_DICTIONARY:
4365 case CTRL_X_THESAURUS:
4366 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004367 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 : (type == CTRL_X_THESAURUS
4369 ? (*curbuf->b_p_tsr == NUL
4370 ? p_tsr
4371 : curbuf->b_p_tsr)
4372 : (*curbuf->b_p_dict == NUL
4373 ? p_dict
4374 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004375 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004376 dict != NULL ? dict_f
4377 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 dict = NULL;
4379 break;
4380
4381 case CTRL_X_TAGS:
4382 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4383 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004386 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 * of matches is found when compl_pattern is empty */
4388 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4390 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4391 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4392 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004393 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 p_ic = save_p_ic;
4396 break;
4397
4398 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4401 {
4402
4403 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004404 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004405 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 }
4407 break;
4408
4409 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004410 if (expand_cmdline(&compl_xp, compl_pattern,
4411 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004412 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004413 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 break;
4415
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004416#ifdef FEAT_COMPL_FUNC
4417 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004418 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004419 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004420 break;
4421#endif
4422
Bram Moolenaar488c6512005-08-11 20:09:58 +00004423 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004424#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004425 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004426 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004427 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004428 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004429#endif
4430 break;
4431
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 default: /* normal ^P/^N and ^X^L */
4433 /*
4434 * If 'infercase' is set, don't use 'smartcase' here
4435 */
4436 save_p_scs = p_scs;
4437 if (ins_buf->b_p_inf)
4438 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004439
Bram Moolenaar361aa502014-01-23 22:45:58 +01004440 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 * end but never from the middle, thus setting nowrapscan in this
4442 * buffers is a good idea, on the other hand, we always set
4443 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4444 save_p_ws = p_ws;
4445 if (ins_buf != curbuf)
4446 p_ws = FALSE;
4447 else if (*e_cpt == '.')
4448 p_ws = TRUE;
4449 for (;;)
4450 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004451 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004453 ++msg_silent; /* Don't want messages for wrapscan. */
4454
Bram Moolenaare4214502015-03-05 18:08:43 +01004455 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4456 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004457 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004458 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004461 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004463 found_new_match = searchit(NULL, ins_buf, pos,
4464 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004465 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004466 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004467 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004468 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004469 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004470 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004471 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 first_match_pos = *pos;
4473 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004474 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 }
4476 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004477 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 found_new_match = FAIL;
4479 if (found_new_match == FAIL)
4480 {
4481 if (ins_buf == curbuf)
4482 found_all = TRUE;
4483 break;
4484 }
4485
4486 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004487 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 && ini->lnum == pos->lnum
4489 && ini->col == pos->col)
4490 continue;
4491 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004492 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004494 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 {
4496 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4497 continue;
4498 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4499 if (!p_paste)
4500 ptr = skipwhite(ptr);
4501 }
4502 len = (int)STRLEN(ptr);
4503 }
4504 else
4505 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004506 char_u *tmp_ptr = ptr;
4507
4508 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 /* Skip if already inside a word. */
4512 if (vim_iswordp(tmp_ptr))
4513 continue;
4514 /* Find start of next word. */
4515 tmp_ptr = find_word_start(tmp_ptr);
4516 }
4517 /* Find end of this word. */
4518 tmp_ptr = find_word_end(tmp_ptr);
4519 len = (int)(tmp_ptr - ptr);
4520
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004521 if ((compl_cont_status & CONT_ADDING)
4522 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
4524 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4525 {
4526 /* Try next line, if any. the new word will be
4527 * "join" as if the normal command "J" was used.
4528 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004529 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 * works -- Acevedo */
4531 STRNCPY(IObuff, ptr, len);
4532 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4533 tmp_ptr = ptr = skipwhite(ptr);
4534 /* Find start of next word. */
4535 tmp_ptr = find_word_start(tmp_ptr);
4536 /* Find end of next word. */
4537 tmp_ptr = find_word_end(tmp_ptr);
4538 if (tmp_ptr > ptr)
4539 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004540 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004542 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 IObuff[len++] = ' ';
4544 /* IObuf =~ "\k.* ", thus len >= 2 */
4545 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004546 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 || (vim_strchr(p_cpo, CPO_JOINSP)
4548 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004549 && (IObuff[len - 2] == '?'
4550 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 IObuff[len++] = ' ';
4552 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004553 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 if (tmp_ptr - ptr >= IOSIZE - len)
4555 tmp_ptr = ptr + IOSIZE - len - 1;
4556 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4557 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004558 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 }
4560 IObuff[len] = NUL;
4561 ptr = IObuff;
4562 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 continue;
4565 }
4566 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004567 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004568 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004569 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 {
4571 found_new_match = OK;
4572 break;
4573 }
4574 }
4575 p_scs = save_p_scs;
4576 p_ws = save_p_ws;
4577 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004578
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004579 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004580 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004581 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 found_new_match = OK;
4583
4584 /* break the loop for specialized modes (use 'complete' just for the
4585 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004586 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004587 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004588 {
4589 if (got_int)
4590 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004591 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004592 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004593 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004594
Bram Moolenaare4214502015-03-05 18:08:43 +01004595 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004596 || compl_interrupted)
4597 break;
4598 compl_started = TRUE;
4599 }
4600 else
4601 {
4602 /* Mark a buffer scanned when it has been scanned completely */
4603 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4604 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004606 compl_started = FALSE;
4607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004609 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610
Bram Moolenaare4214502015-03-05 18:08:43 +01004611 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 && *e_cpt == NUL) /* Got to end of 'complete' */
4613 found_new_match = FAIL;
4614
4615 i = -1; /* total of matches, unknown */
4616 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004617 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 i = ins_compl_make_cyclic();
4619
4620 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004621 * just been made cyclic then we have to move compl_curr_match to the next
4622 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004623 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4624 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004625 if (compl_curr_match == NULL)
4626 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 return i;
4628}
4629
4630/* Delete the old text being completed. */
4631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004632ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633{
4634 int i;
4635
4636 /*
4637 * In insert mode: Delete the typed part.
4638 * In replace mode: Put the old characters back, if any.
4639 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004640 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 backspace_until_column(i);
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004642
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004643 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4644 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004646 /* clear v:completed_item */
4647 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648}
4649
4650/* Insert the new text being completed. */
4651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004652ins_compl_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004653{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004654 dict_T *dict;
4655
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004656 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004657 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4658 compl_used_match = FALSE;
4659 else
4660 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004661
4662 /* Set completed item. */
4663 /* { word, abbr, menu, kind, info } */
4664 dict = dict_alloc();
4665 if (dict != NULL)
4666 {
4667 dict_add_nr_str(dict, "word", 0L,
4668 EMPTY_IF_NULL(compl_shown_match->cp_str));
4669 dict_add_nr_str(dict, "abbr", 0L,
4670 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4671 dict_add_nr_str(dict, "menu", 0L,
4672 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4673 dict_add_nr_str(dict, "kind", 0L,
4674 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4675 dict_add_nr_str(dict, "info", 0L,
4676 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4677 }
4678 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679}
4680
4681/*
4682 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004683 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4684 * get more completions. If it is FALSE, then we just do nothing when there
4685 * are no more completions in a given direction. The latter case is used when
4686 * we are still in the middle of finding completions, to allow browsing
4687 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 * Return the total number of matches, or -1 if still unknown -- webb.
4689 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004690 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4691 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 *
4693 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004694 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4695 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696 */
4697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004698ins_compl_next(
4699 int allow_get_expansion,
4700 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004701 be at least 1 */
Bram Moolenaar7454a062016-01-30 15:14:10 +01004702 int insert_match) /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703{
4704 int num_matches = -1;
4705 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004706 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004707 compl_T *found_compl = NULL;
4708 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004709 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004710 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004712 /* When user complete function return -1 for findstart which is next
4713 * time of 'always', compl_shown_match become NULL. */
4714 if (compl_shown_match == NULL)
4715 return -1;
4716
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004717 if (compl_leader != NULL
4718 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004720 /* Set "compl_shown_match" to the actually shown match, it may differ
4721 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004722 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004723 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004724 && compl_shown_match->cp_next != NULL
4725 && compl_shown_match->cp_next != compl_first_match)
4726 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004727
4728 /* If we didn't find it searching forward, and compl_shows_dir is
4729 * backward, find the last match. */
4730 if (compl_shows_dir == BACKWARD
4731 && !ins_compl_equal(compl_shown_match,
4732 compl_leader, (int)STRLEN(compl_leader))
4733 && (compl_shown_match->cp_next == NULL
4734 || compl_shown_match->cp_next == compl_first_match))
4735 {
4736 while (!ins_compl_equal(compl_shown_match,
4737 compl_leader, (int)STRLEN(compl_leader))
4738 && compl_shown_match->cp_prev != NULL
4739 && compl_shown_match->cp_prev != compl_first_match)
4740 compl_shown_match = compl_shown_match->cp_prev;
4741 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004742 }
4743
4744 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004745 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 /* Delete old text to be replaced */
4747 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004748
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004749 /* When finding the longest common text we stick at the original text,
4750 * don't let CTRL-N or CTRL-P move to the first match. */
4751 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4752
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004753 /* When restarting the search don't insert the first match either. */
4754 if (compl_restarting)
4755 {
4756 advance = FALSE;
4757 compl_restarting = FALSE;
4758 }
4759
Bram Moolenaare3226be2005-12-18 22:10:00 +00004760 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4761 * around. */
4762 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004764 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004766 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004767 found_end = (compl_first_match != NULL
4768 && (compl_shown_match->cp_next == compl_first_match
4769 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004770 }
4771 else if (compl_shows_dir == BACKWARD
4772 && compl_shown_match->cp_prev != NULL)
4773 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004774 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004775 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004776 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004779 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004780 if (!allow_get_expansion)
4781 {
4782 if (advance)
4783 {
4784 if (compl_shows_dir == BACKWARD)
4785 compl_pending -= todo + 1;
4786 else
4787 compl_pending += todo + 1;
4788 }
4789 return -1;
4790 }
4791
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004792 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004793 {
4794 if (compl_shows_dir == BACKWARD)
4795 --compl_pending;
4796 else
4797 ++compl_pending;
4798 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004799
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004800 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004801 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004802
4803 /* handle any pending completions */
4804 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004805 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004806 {
4807 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4808 {
4809 compl_shown_match = compl_shown_match->cp_next;
4810 --compl_pending;
4811 }
4812 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4813 {
4814 compl_shown_match = compl_shown_match->cp_prev;
4815 ++compl_pending;
4816 }
4817 else
4818 break;
4819 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004820 found_end = FALSE;
4821 }
4822 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4823 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004824 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004825 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004826 ++todo;
4827 else
4828 /* Remember a matching item. */
4829 found_compl = compl_shown_match;
4830
4831 /* Stop at the end of the list when we found a usable match. */
4832 if (found_end)
4833 {
4834 if (found_compl != NULL)
4835 {
4836 compl_shown_match = found_compl;
4837 break;
4838 }
4839 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 }
4842
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004843 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004844 if (compl_no_insert && !started)
4845 {
4846 ins_bytes(compl_orig_text + ins_compl_len());
4847 compl_used_match = FALSE;
4848 }
4849 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004850 {
4851 if (!compl_get_longest || compl_used_match)
4852 ins_compl_insert();
4853 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004854 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004855 }
4856 else
4857 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858
4859 if (!allow_get_expansion)
4860 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004861 /* may undisplay the popup menu first */
4862 ins_compl_upd_pum();
4863
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004864 /* redraw to show the user what was inserted */
4865 update_screen(0);
4866
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004867 /* display the updated popup menu */
4868 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004869#ifdef FEAT_GUI
4870 if (gui.in_use)
4871 {
4872 /* Show the cursor after the match, not after the redrawn text. */
4873 setcursor();
4874 out_flush();
4875 gui_update_cursor(FALSE, FALSE);
4876 }
4877#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004878
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 /* Delete old text to be replaced, since we're still searching and
4880 * don't want to match ourselves! */
4881 ins_compl_delete();
4882 }
4883
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004884 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004885 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004886 if (compl_no_insert && !started)
4887 compl_enter_selects = TRUE;
4888 else
4889 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004890
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 /*
4892 * Show the file name for the match (if any)
4893 * Truncate the file name to avoid a wait for return.
4894 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004895 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 {
4897 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004898 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 if (i <= 0)
4900 i = 0;
4901 else
4902 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004903 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 msg(IObuff);
4905 redraw_cmdline = FALSE; /* don't overwrite! */
4906 }
4907
4908 return num_matches;
4909}
4910
4911/*
4912 * Call this while finding completions, to check whether the user has hit a key
4913 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004914 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004916 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 */
4918 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004919ins_compl_check_keys(int frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920{
4921 static int count = 0;
4922
4923 int c;
4924
4925 /* Don't check when reading keys from a script. That would break the test
4926 * scripts */
4927 if (using_script())
4928 return;
4929
4930 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004931 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 return;
4933 count = 0;
4934
Bram Moolenaara260a972006-06-23 19:36:29 +00004935 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4936 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 if (c != NUL)
4939 {
4940 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4941 {
4942 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004943 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004944 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4945 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004947 else
4948 {
4949 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004950 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004951 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004952 if (c != K_IGNORE)
4953 {
4954 /* Don't interrupt completion when the character wasn't typed,
4955 * e.g., when doing @q to replay keys. */
4956 if (c != Ctrl_R && KeyTyped)
4957 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004958
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004959 vungetc(c);
4960 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004963 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004964 {
4965 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4966
4967 compl_pending = 0;
4968 (void)ins_compl_next(FALSE, todo, TRUE);
4969 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004970}
4971
4972/*
4973 * Decide the direction of Insert mode complete from the key typed.
4974 * Returns BACKWARD or FORWARD.
4975 */
4976 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004977ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004978{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004979 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02004980 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004981 return BACKWARD;
4982 return FORWARD;
4983}
4984
4985/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004986 * Return TRUE for keys that are used for completion only when the popup menu
4987 * is visible.
4988 */
4989 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004990ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004991{
4992 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004993 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4994 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004995}
4996
4997/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004998 * Decide the number of completions to move forward.
4999 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5000 */
5001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005002ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005003{
5004 int h;
5005
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005006 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005007 {
5008 h = pum_get_height();
5009 if (h > 3)
5010 h -= 2; /* keep some context */
5011 return h;
5012 }
5013 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014}
5015
5016/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005017 * Return TRUE if completion with "c" should insert the match, FALSE if only
5018 * to change the currently selected completion.
5019 */
5020 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005021ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005022{
5023 switch (c)
5024 {
5025 case K_UP:
5026 case K_DOWN:
5027 case K_PAGEDOWN:
5028 case K_KPAGEDOWN:
5029 case K_S_DOWN:
5030 case K_PAGEUP:
5031 case K_KPAGEUP:
5032 case K_S_UP:
5033 return FALSE;
5034 }
5035 return TRUE;
5036}
5037
5038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 * Do Insert mode completion.
5040 * Called when character "c" was typed, which has a meaning for completion.
5041 * Returns OK if completion was done, FAIL if something failed (out of mem).
5042 */
5043 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005044ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005046 char_u *line;
5047 int startcol = 0; /* column where searched text starts */
5048 colnr_T curs_col; /* cursor column */
5049 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005050 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
Bram Moolenaare3226be2005-12-18 22:10:00 +00005052 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005053 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
5055 /* First time we hit ^N or ^P (in a row, I mean) */
5056
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 did_ai = FALSE;
5058#ifdef FEAT_SMARTINDENT
5059 did_si = FALSE;
5060 can_si = FALSE;
5061 can_si_back = FALSE;
5062#endif
5063 if (stop_arrow() == FAIL)
5064 return FAIL;
5065
5066 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005067 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005068 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005070 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005071 * "compl_startpos" to the cursor as a pattern to add a new word
5072 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005073 * "compl_startpos" is not in the same line as the cursor then fix it
5074 * (the line has been split because it was longer than 'tw'). if SOL
5075 * is set then skip the previous pattern, a word at the beginning of
5076 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005077 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5078 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 /*
5081 * it is a continued search
5082 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005083 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5085 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5086 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005087 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005089 /* line (probably) wrapped, set compl_startpos to the
5090 * first non_blank in the line, if it is not a wordchar
5091 * include it to get a better pattern, but then we don't
5092 * want the "\\<" prefix, check it bellow */
5093 compl_col = (colnr_T)(skipwhite(line) - line);
5094 compl_startpos.col = compl_col;
5095 compl_startpos.lnum = curwin->w_cursor.lnum;
5096 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 else
5099 {
5100 /* S_IPOS was set when we inserted a word that was at the
5101 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 * mode but first we need to redefine compl_startpos */
5103 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 compl_cont_status |= CONT_SOL;
5106 compl_startpos.col = (colnr_T)(skipwhite(
5107 line + compl_length
5108 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005110 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005113 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005114 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005118 compl_cont_status &= ~CONT_SOL;
5119 compl_length = (IOSIZE - MIN_SPACE);
5120 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005122 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5123 if (compl_length < 1)
5124 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005126 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005127 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005132 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005134 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005136 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005138 compl_cont_status = 0;
5139 compl_cont_status |= CONT_N_ADDS;
5140 compl_startpos = curwin->w_cursor;
5141 startcol = (int)curs_col;
5142 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144
5145 /* Work out completion pattern and original text -- webb */
5146 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5147 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005148 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5150 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005151 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005155 compl_col += ++startcol;
5156 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 compl_pattern = str_foldcase(line + compl_col,
5160 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005162 compl_pattern = vim_strnsave(line + compl_col,
5163 compl_length);
5164 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 return FAIL;
5166 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
5169 char_u *prefix = (char_u *)"\\<";
5170
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005171 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005172 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005173 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005174 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 if (!vim_iswordp(line + compl_col)
5177 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 && (
5179#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005182 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183#endif
5184 )))
5185 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 STRCPY((char *)compl_pattern, prefix);
5187 (void)quote_meta(compl_pattern + STRLEN(prefix),
5188 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005192 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005194 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195#endif
5196 )
5197 {
5198 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005199 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5200 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005202 compl_col += curs_col;
5203 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
5205 else
5206 {
5207#ifdef FEAT_MBYTE
5208 /* Search the point of change class of multibyte character
5209 * or not a word single byte character backward. */
5210 if (has_mbyte)
5211 {
5212 int base_class;
5213 int head_off;
5214
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005215 startcol -= (*mb_head_off)(line, line + startcol);
5216 base_class = mb_get_class(line + startcol);
5217 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005219 head_off = (*mb_head_off)(line, line + startcol);
5220 if (base_class != mb_get_class(line + startcol
5221 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
5225 }
5226 else
5227#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005230 compl_col += ++startcol;
5231 compl_length = (int)curs_col - startcol;
5232 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
5234 /* Only match word with at least two chars -- webb
5235 * there's no need to call quote_meta,
5236 * alloc(7) is enough -- Acevedo
5237 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005238 compl_pattern = alloc(7);
5239 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 STRCPY((char *)compl_pattern, "\\<");
5242 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5243 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245 else
5246 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005247 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005248 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005251 STRCPY((char *)compl_pattern, "\\<");
5252 (void)quote_meta(compl_pattern + 2, line + compl_col,
5253 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255 }
5256 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005257 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005259 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260 compl_length = (int)curs_col - (int)compl_col;
5261 if (compl_length < 0) /* cursor in indent: empty pattern */
5262 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264 compl_pattern = str_foldcase(line + compl_col, compl_length,
5265 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005267 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5268 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 return FAIL;
5270 }
5271 else if (ctrl_x_mode == CTRL_X_FILES)
5272 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005273 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005274 if (startcol > 0)
5275 {
5276 char_u *p = line + startcol;
5277
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005278 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005279 while (p > line && vim_isfilec(PTR2CHAR(p)))
5280 mb_ptr_back(line, p);
5281 if (p == line && vim_isfilec(PTR2CHAR(p)))
5282 startcol = 0;
5283 else
5284 startcol = (int)(p - line) + 1;
5285 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005286
Bram Moolenaar0300e462013-09-08 16:03:45 +02005287 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 compl_length = (int)curs_col - startcol;
5289 compl_pattern = addstar(line + compl_col, compl_length,
5290 EXPAND_FILES);
5291 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 return FAIL;
5293 }
5294 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5295 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 compl_pattern = vim_strnsave(line, curs_col);
5297 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 set_cmd_context(&compl_xp, compl_pattern,
5300 (int)STRLEN(compl_pattern), curs_col);
5301 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5302 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005303 /* No completion possible, use an empty pattern to get a
5304 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005305 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005306 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005307 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5308 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005310 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005311 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005312#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005313 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005314 * Call user defined function 'completefunc' with "a:findstart"
5315 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005316 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005317 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005318 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005319 char_u *funcname;
5320 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005321 win_T *curwin_save;
5322 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005323
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005324 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005325 * string */
5326 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5327 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5328 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005329 {
5330 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5331 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005332 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005333 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005334
5335 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005336 args[1] = NULL;
5337 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005338 curwin_save = curwin;
5339 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005340 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005341 if (curwin_save != curwin || curbuf_save != curbuf)
5342 {
5343 EMSG(_(e_complwin));
5344 return FAIL;
5345 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005346 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005347 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005348 if (!equalpos(curwin->w_cursor, pos))
5349 {
5350 EMSG(_(e_compldel));
5351 return FAIL;
5352 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005353
Bram Moolenaar6110a002012-01-26 18:58:38 +01005354 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005355 * cancel the complete without an error.
5356 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005357 if (col == -2)
5358 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005359 if (col == -3)
5360 {
5361 ctrl_x_mode = 0;
5362 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005363 if (!shortmess(SHM_COMPLETIONMENU))
5364 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005365 return FAIL;
5366 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005367
Bram Moolenaar82139082011-09-14 16:52:09 +02005368 /*
5369 * Reset extended parameters of completion, when start new
5370 * completion.
5371 */
5372 compl_opt_refresh_always = FALSE;
5373
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005374 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005375 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005376 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005377 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005378 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005379
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 /* Setup variables for completion. Need to obtain "line" again,
5381 * it may have become invalid. */
5382 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005383 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005384 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5385 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005386#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005387 return FAIL;
5388 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005389 else if (ctrl_x_mode == CTRL_X_SPELL)
5390 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005391#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005392 if (spell_bad_len > 0)
5393 compl_col = curs_col - spell_bad_len;
5394 else
5395 compl_col = spell_word_start(startcol);
5396 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005397 {
5398 compl_length = 0;
5399 compl_col = curs_col;
5400 }
5401 else
5402 {
5403 spell_expand_check_cap(compl_col);
5404 compl_length = (int)curs_col - compl_col;
5405 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005406 /* Need to obtain "line" again, it may have become invalid. */
5407 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005408 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5409 if (compl_pattern == NULL)
5410#endif
5411 return FAIL;
5412 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 else
5414 {
5415 EMSG2(_(e_intern2), "ins_complete()");
5416 return FAIL;
5417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005419 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {
5421 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005422 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 {
5424 /* Insert a new line, keep indentation but ignore 'comments' */
5425#ifdef FEAT_COMMENTS
5426 char_u *old = curbuf->b_p_com;
5427
5428 curbuf->b_p_com = (char_u *)"";
5429#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005430 compl_startpos.lnum = curwin->w_cursor.lnum;
5431 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 ins_eol('\r');
5433#ifdef FEAT_COMMENTS
5434 curbuf->b_p_com = old;
5435#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 compl_length = 0;
5437 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
5439 }
5440 else
5441 {
5442 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005443 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005446 if (compl_cont_status & CONT_LOCAL)
5447 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 else
5449 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5450
Bram Moolenaar62951b12011-09-21 18:23:05 +02005451 /* If any of the original typed text has been changed we need to fix
5452 * the redo buffer. */
5453 ins_compl_fixRedoBufForLeader(NULL);
5454
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005455 /* Always add completion for the original text. */
5456 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005457 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5458 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005459 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005461 vim_free(compl_pattern);
5462 compl_pattern = NULL;
5463 vim_free(compl_orig_text);
5464 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 return FAIL;
5466 }
5467
5468 /* showmode might reset the internal line pointers, so it must
5469 * be called before line = ml_get(), or when this address is no
5470 * longer needed. -- Acevedo.
5471 */
5472 edit_submode_extra = (char_u *)_("-- Searching...");
5473 edit_submode_highl = HLF_COUNT;
5474 showmode();
5475 edit_submode_extra = NULL;
5476 out_flush();
5477 }
5478
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 compl_shown_match = compl_curr_match;
5480 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
5482 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005483 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005485 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005486 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005488 /* may undisplay the popup menu */
5489 ins_compl_upd_pum();
5490
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005491 if (n > 1) /* all matches have been found */
5492 compl_matches = n;
5493 compl_curr_match = compl_shown_match;
5494 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495
Bram Moolenaard68071d2006-05-02 22:08:30 +00005496 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5497 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 if (got_int && !global_busy)
5499 {
5500 (void)vgetc();
5501 got_int = FALSE;
5502 }
5503
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005504 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005505 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005507 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5508 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5510 edit_submode_highl = HLF_E;
5511 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5512 * because we couldn't expand anything at first place, but if we used
5513 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5514 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 if ( compl_length > 1
5516 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 || (ctrl_x_mode != 0
5518 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5519 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005520 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 }
5522
Bram Moolenaar572cb562005-08-05 21:35:02 +00005523 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005524 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005526 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
5528 if (edit_submode_extra == NULL)
5529 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005530 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 edit_submode_extra = (char_u *)_("Back at original");
5533 edit_submode_highl = HLF_W;
5534 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005535 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 edit_submode_extra = (char_u *)_("Word from other line");
5538 edit_submode_highl = HLF_COUNT;
5539 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005540 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542 edit_submode_extra = (char_u *)_("The only match");
5543 edit_submode_highl = HLF_COUNT;
5544 }
5545 else
5546 {
5547 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005548 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005550 int number = 0;
5551 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005553 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {
5555 /* search backwards for the first valid (!= -1) number.
5556 * This should normally succeed already at the first loop
5557 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005558 for (match = compl_curr_match->cp_prev; match != NULL
5559 && match != compl_first_match;
5560 match = match->cp_prev)
5561 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005563 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 break;
5565 }
5566 if (match != NULL)
5567 /* go up and assign all numbers which are not assigned
5568 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005569 for (match = match->cp_next;
5570 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005571 match = match->cp_next)
5572 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574 else /* BACKWARD */
5575 {
5576 /* search forwards (upwards) for the first valid (!= -1)
5577 * number. This should normally succeed already at the
5578 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005579 for (match = compl_curr_match->cp_next; match != NULL
5580 && match != compl_first_match;
5581 match = match->cp_next)
5582 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005584 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 break;
5586 }
5587 if (match != NULL)
5588 /* go down and assign all numbers which are not
5589 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005590 for (match = match->cp_prev; match
5591 && match->cp_number == -1;
5592 match = match->cp_prev)
5593 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 }
5596
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005597 /* The match should always have a sequence number now, this is
5598 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005599 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005601 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5602 * Translations may need more than twice that. */
5603 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005605 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005606 vim_snprintf((char *)match_ref, sizeof(match_ref),
5607 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005608 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005610 vim_snprintf((char *)match_ref, sizeof(match_ref),
5611 _("match %d"),
5612 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 edit_submode_extra = match_ref;
5614 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005615 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 curs_columns(FALSE);
5617 }
5618 }
5619 }
5620
5621 /* Show a message about what (completion) mode we're in. */
5622 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005623 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005625 if (edit_submode_extra != NULL)
5626 {
5627 if (!p_smd)
5628 msg_attr(edit_submode_extra,
5629 edit_submode_highl < HLF_COUNT
5630 ? hl_attr(edit_submode_highl) : 0);
5631 }
5632 else
5633 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
Bram Moolenaard68071d2006-05-02 22:08:30 +00005636 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005637 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005638 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005639 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005640 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005641 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005642 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005643
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644 return OK;
5645}
5646
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005647 static void
5648show_pum(int save_w_wrow)
5649{
5650 /* RedrawingDisabled may be set when invoked through complete(). */
5651 int n = RedrawingDisabled;
5652
5653 RedrawingDisabled = 0;
5654
5655 /* If the cursor moved we need to remove the pum first. */
5656 setcursor();
5657 if (save_w_wrow != curwin->w_wrow)
5658 ins_compl_del_pum();
5659
5660 ins_compl_show_pum();
5661 setcursor();
5662 RedrawingDisabled = n;
5663}
5664
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665/*
5666 * Looks in the first "len" chars. of "src" for search-metachars.
5667 * If dest is not NULL the chars. are copied there quoting (with
5668 * a backslash) the metachars, and dest would be NUL terminated.
5669 * Returns the length (needed) of dest
5670 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005671 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005672quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005674 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005676 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 {
5678 switch (*src)
5679 {
5680 case '.':
5681 case '*':
5682 case '[':
5683 if (ctrl_x_mode == CTRL_X_DICTIONARY
5684 || ctrl_x_mode == CTRL_X_THESAURUS)
5685 break;
5686 case '~':
5687 if (!p_magic) /* quote these only if magic is set */
5688 break;
5689 case '\\':
5690 if (ctrl_x_mode == CTRL_X_DICTIONARY
5691 || ctrl_x_mode == CTRL_X_THESAURUS)
5692 break;
5693 case '^': /* currently it's not needed. */
5694 case '$':
5695 m++;
5696 if (dest != NULL)
5697 *dest++ = '\\';
5698 break;
5699 }
5700 if (dest != NULL)
5701 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005702# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 /* Copy remaining bytes of a multibyte character. */
5704 if (has_mbyte)
5705 {
5706 int i, mb_len;
5707
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005708 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 if (mb_len > 0 && len >= mb_len)
5710 for (i = 0; i < mb_len; ++i)
5711 {
5712 --len;
5713 ++src;
5714 if (dest != NULL)
5715 *dest++ = *src;
5716 }
5717 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005718# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720 if (dest != NULL)
5721 *dest = NUL;
5722
5723 return m;
5724}
5725#endif /* FEAT_INS_EXPAND */
5726
5727/*
5728 * Next character is interpreted literally.
5729 * A one, two or three digit decimal number is interpreted as its byte value.
5730 * If one or two digits are entered, the next character is given to vungetc().
5731 * For Unicode a character > 255 may be returned.
5732 */
5733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005734get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 int cc;
5737 int nc;
5738 int i;
5739 int hex = FALSE;
5740 int octal = FALSE;
5741#ifdef FEAT_MBYTE
5742 int unicode = 0;
5743#endif
5744
5745 if (got_int)
5746 return Ctrl_C;
5747
5748#ifdef FEAT_GUI
5749 /*
5750 * In GUI there is no point inserting the internal code for a special key.
5751 * It is more useful to insert the string "<KEY>" instead. This would
5752 * probably be useful in a text window too, but it would not be
5753 * vi-compatible (maybe there should be an option for it?) -- webb
5754 */
5755 if (gui.in_use)
5756 ++allow_keys;
5757#endif
5758#ifdef USE_ON_FLY_SCROLL
5759 dont_scroll = TRUE; /* disallow scrolling here */
5760#endif
5761 ++no_mapping; /* don't map the next key hits */
5762 cc = 0;
5763 i = 0;
5764 for (;;)
5765 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005766 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767#ifdef FEAT_CMDL_INFO
5768 if (!(State & CMDLINE)
5769# ifdef FEAT_MBYTE
5770 && MB_BYTE2LEN_CHECK(nc) == 1
5771# endif
5772 )
5773 add_to_showcmd(nc);
5774#endif
5775 if (nc == 'x' || nc == 'X')
5776 hex = TRUE;
5777 else if (nc == 'o' || nc == 'O')
5778 octal = TRUE;
5779#ifdef FEAT_MBYTE
5780 else if (nc == 'u' || nc == 'U')
5781 unicode = nc;
5782#endif
5783 else
5784 {
5785 if (hex
5786#ifdef FEAT_MBYTE
5787 || unicode != 0
5788#endif
5789 )
5790 {
5791 if (!vim_isxdigit(nc))
5792 break;
5793 cc = cc * 16 + hex2nr(nc);
5794 }
5795 else if (octal)
5796 {
5797 if (nc < '0' || nc > '7')
5798 break;
5799 cc = cc * 8 + nc - '0';
5800 }
5801 else
5802 {
5803 if (!VIM_ISDIGIT(nc))
5804 break;
5805 cc = cc * 10 + nc - '0';
5806 }
5807
5808 ++i;
5809 }
5810
5811 if (cc > 255
5812#ifdef FEAT_MBYTE
5813 && unicode == 0
5814#endif
5815 )
5816 cc = 255; /* limit range to 0-255 */
5817 nc = 0;
5818
5819 if (hex) /* hex: up to two chars */
5820 {
5821 if (i >= 2)
5822 break;
5823 }
5824#ifdef FEAT_MBYTE
5825 else if (unicode) /* Unicode: up to four or eight chars */
5826 {
5827 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5828 break;
5829 }
5830#endif
5831 else if (i >= 3) /* decimal or octal: up to three chars */
5832 break;
5833 }
5834 if (i == 0) /* no number entered */
5835 {
5836 if (nc == K_ZERO) /* NUL is stored as NL */
5837 {
5838 cc = '\n';
5839 nc = 0;
5840 }
5841 else
5842 {
5843 cc = nc;
5844 nc = 0;
5845 }
5846 }
5847
5848 if (cc == 0) /* NUL is stored as NL */
5849 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005850#ifdef FEAT_MBYTE
5851 if (enc_dbcs && (cc & 0xff) == 0)
5852 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5853 second byte will cause trouble! */
5854#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855
5856 --no_mapping;
5857#ifdef FEAT_GUI
5858 if (gui.in_use)
5859 --allow_keys;
5860#endif
5861 if (nc)
5862 vungetc(nc);
5863 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5864 return cc;
5865}
5866
5867/*
5868 * Insert character, taking care of special keys and mod_mask
5869 */
5870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005871insert_special(
5872 int c,
5873 int allow_modmask,
5874 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875{
5876 char_u *p;
5877 int len;
5878
5879 /*
5880 * Special function key, translate into "<Key>". Up to the last '>' is
5881 * inserted with ins_str(), so as not to replace characters in replace
5882 * mode.
5883 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5884 * unless 'allow_modmask' is TRUE.
5885 */
5886#ifdef MACOS
5887 /* Command-key never produces a normal key */
5888 if (mod_mask & MOD_MASK_CMD)
5889 allow_modmask = TRUE;
5890#endif
5891 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5892 {
5893 p = get_special_key_name(c, mod_mask);
5894 len = (int)STRLEN(p);
5895 c = p[len - 1];
5896 if (len > 2)
5897 {
5898 if (stop_arrow() == FAIL)
5899 return;
5900 p[len - 1] = NUL;
5901 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005902 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 ctrlv = FALSE;
5904 }
5905 }
5906 if (stop_arrow() == OK)
5907 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5908}
5909
5910/*
5911 * Special characters in this context are those that need processing other
5912 * than the simple insertion that can be performed here. This includes ESC
5913 * which terminates the insert, and CR/NL which need special processing to
5914 * open up a new line. This routine tries to optimize insertions performed by
5915 * the "redo", "undo" or "put" commands, so it needs to know when it should
5916 * stop and defer processing to the "normal" mechanism.
5917 * '0' and '^' are special, because they can be followed by CTRL-D.
5918 */
5919#ifdef EBCDIC
5920# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5921#else
5922# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5923#endif
5924
5925#ifdef FEAT_MBYTE
5926# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5927#else
5928# define WHITECHAR(cc) vim_iswhite(cc)
5929#endif
5930
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005931/*
5932 * "flags": INSCHAR_FORMAT - force formatting
5933 * INSCHAR_CTRLV - char typed just after CTRL-V
5934 * INSCHAR_NO_FEX - don't use 'formatexpr'
5935 *
5936 * NOTE: passes the flags value straight through to internal_format() which,
5937 * beside INSCHAR_FORMAT (above), is also looking for these:
5938 * INSCHAR_DO_COM - format comments
5939 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5940 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005942insertchar(
5943 int c, /* character to insert or NUL */
5944 int flags, /* INSCHAR_FORMAT, etc. */
5945 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 int textwidth;
5948#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005952 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005953
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005954 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005955 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005956
5957 /*
5958 * Try to break the line in two or more pieces when:
5959 * - Always do this if we have been called to do formatting only.
5960 * - Always do this when 'formatoptions' has the 'a' flag and the line
5961 * ends in white space.
5962 * - Otherwise:
5963 * - Don't do this if inserting a blank
5964 * - Don't do this if an existing character is being replaced, unless
5965 * we're in VREPLACE mode.
5966 * - Do this if the cursor is not on the line where insert started
5967 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5968 * before the insert.
5969 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5970 * before 'textwidth'
5971 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005972 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005973 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005974 || (!vim_iswhite(c)
5975 && !((State & REPLACE_FLAG)
5976#ifdef FEAT_VREPLACE
5977 && !(State & VREPLACE_FLAG)
5978#endif
5979 && *ml_get_cursor() != NUL)
5980 && (curwin->w_cursor.lnum != Insstart.lnum
5981 || ((!has_format_option(FO_INS_LONG)
5982 || Insstart_textlen <= (colnr_T)textwidth)
5983 && (!fo_ins_blank
5984 || Insstart_blank_vcol <= (colnr_T)textwidth
5985 ))))))
5986 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005987 /* Format with 'formatexpr' when it's set. Use internal formatting
5988 * when 'formatexpr' isn't set or it returns non-zero. */
5989#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005990 int do_internal = TRUE;
5991 colnr_T virtcol = get_nolist_virtcol()
5992 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005993
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005994 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
5995 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005996 {
5997 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5998 /* It may be required to save for undo again, e.g. when setline()
5999 * was called. */
6000 ins_need_undo = TRUE;
6001 }
6002 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006004 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006005 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006006
Bram Moolenaar071d4272004-06-13 20:20:40 +00006007 if (c == NUL) /* only formatting was wanted */
6008 return;
6009
6010#ifdef FEAT_COMMENTS
6011 /* Check whether this character should end a comment. */
6012 if (did_ai && (int)c == end_comment_pending)
6013 {
6014 char_u *line;
6015 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6016 int middle_len, end_len;
6017 int i;
6018
6019 /*
6020 * Need to remove existing (middle) comment leader and insert end
6021 * comment leader. First, check what comment leader we can find.
6022 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006023 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006024 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6025 {
6026 /* Skip middle-comment string */
6027 while (*p && p[-1] != ':') /* find end of middle flags */
6028 ++p;
6029 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6030 /* Don't count trailing white space for middle_len */
6031 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6032 --middle_len;
6033
6034 /* Find the end-comment string */
6035 while (*p && p[-1] != ':') /* find end of end flags */
6036 ++p;
6037 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6038
6039 /* Skip white space before the cursor */
6040 i = curwin->w_cursor.col;
6041 while (--i >= 0 && vim_iswhite(line[i]))
6042 ;
6043 i++;
6044
6045 /* Skip to before the middle leader */
6046 i -= middle_len;
6047
6048 /* Check some expected things before we go on */
6049 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6050 {
6051 /* Backspace over all the stuff we want to replace */
6052 backspace_until_column(i);
6053
6054 /*
6055 * Insert the end-comment string, except for the last
6056 * character, which will get inserted as normal later.
6057 */
6058 ins_bytes_len(lead_end, end_len - 1);
6059 }
6060 }
6061 }
6062 end_comment_pending = NUL;
6063#endif
6064
6065 did_ai = FALSE;
6066#ifdef FEAT_SMARTINDENT
6067 did_si = FALSE;
6068 can_si = FALSE;
6069 can_si_back = FALSE;
6070#endif
6071
6072 /*
6073 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6074 * This speeds up normal text input considerably.
6075 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6076 * need to re-indent at a ':', or any other character (but not what
6077 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006078 * Don't do this when there an InsertCharPre autocommand is defined,
6079 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006080 */
6081#ifdef USE_ON_FLY_SCROLL
6082 dont_scroll = FALSE; /* allow scrolling here */
6083#endif
6084
6085 if ( !ISSPECIAL(c)
6086#ifdef FEAT_MBYTE
6087 && (!has_mbyte || (*mb_char2len)(c) == 1)
6088#endif
6089 && vpeekc() != NUL
6090 && !(State & REPLACE_FLAG)
6091#ifdef FEAT_CINDENT
6092 && !cindent_on()
6093#endif
6094#ifdef FEAT_RIGHTLEFT
6095 && !p_ri
6096#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006097#ifdef FEAT_AUTOCMD
6098 && !has_insertcharpre()
6099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006100 )
6101 {
6102#define INPUT_BUFLEN 100
6103 char_u buf[INPUT_BUFLEN + 1];
6104 int i;
6105 colnr_T virtcol = 0;
6106
6107 buf[0] = c;
6108 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006109 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006110 virtcol = get_nolist_virtcol();
6111 /*
6112 * Stop the string when:
6113 * - no more chars available
6114 * - finding a special character (command key)
6115 * - buffer is full
6116 * - running into the 'textwidth' boundary
6117 * - need to check for abbreviation: A non-word char after a word-char
6118 */
6119 while ( (c = vpeekc()) != NUL
6120 && !ISSPECIAL(c)
6121#ifdef FEAT_MBYTE
6122 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6123#endif
6124 && i < INPUT_BUFLEN
6125 && (textwidth == 0
6126 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6127 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6128 {
6129#ifdef FEAT_RIGHTLEFT
6130 c = vgetc();
6131 if (p_hkmap && KeyTyped)
6132 c = hkmap(c); /* Hebrew mode mapping */
6133# ifdef FEAT_FKMAP
6134 if (p_fkmap && KeyTyped)
6135 c = fkmap(c); /* Farsi mode mapping */
6136# endif
6137 buf[i++] = c;
6138#else
6139 buf[i++] = vgetc();
6140#endif
6141 }
6142
6143#ifdef FEAT_DIGRAPHS
6144 do_digraph(-1); /* clear digraphs */
6145 do_digraph(buf[i-1]); /* may be the start of a digraph */
6146#endif
6147 buf[i] = NUL;
6148 ins_str(buf);
6149 if (flags & INSCHAR_CTRLV)
6150 {
6151 redo_literal(*buf);
6152 i = 1;
6153 }
6154 else
6155 i = 0;
6156 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006157 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158 }
6159 else
6160 {
6161#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006162 int cc;
6163
Bram Moolenaar071d4272004-06-13 20:20:40 +00006164 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6165 {
6166 char_u buf[MB_MAXBYTES + 1];
6167
6168 (*mb_char2bytes)(c, buf);
6169 buf[cc] = NUL;
6170 ins_char_bytes(buf, cc);
6171 AppendCharToRedobuff(c);
6172 }
6173 else
6174#endif
6175 {
6176 ins_char(c);
6177 if (flags & INSCHAR_CTRLV)
6178 redo_literal(c);
6179 else
6180 AppendCharToRedobuff(c);
6181 }
6182 }
6183}
6184
6185/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006186 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006187 *
6188 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6189 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006190 */
6191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006192internal_format(
6193 int textwidth,
6194 int second_indent,
6195 int flags,
6196 int format_only,
6197 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006198{
6199 int cc;
6200 int save_char = NUL;
6201 int haveto_redraw = FALSE;
6202 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6203#ifdef FEAT_MBYTE
6204 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6205#endif
6206 int fo_white_par = has_format_option(FO_WHITE_PAR);
6207 int first_line = TRUE;
6208#ifdef FEAT_COMMENTS
6209 colnr_T leader_len;
6210 int no_leader = FALSE;
6211 int do_comments = (flags & INSCHAR_DO_COM);
6212#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006213#ifdef FEAT_LINEBREAK
6214 int has_lbr = curwin->w_p_lbr;
6215
6216 /* make sure win_lbr_chartabsize() counts correctly */
6217 curwin->w_p_lbr = FALSE;
6218#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006219
6220 /*
6221 * When 'ai' is off we don't want a space under the cursor to be
6222 * deleted. Replace it with an 'x' temporarily.
6223 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006224 if (!curbuf->b_p_ai
6225#ifdef FEAT_VREPLACE
6226 && !(State & VREPLACE_FLAG)
6227#endif
6228 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229 {
6230 cc = gchar_cursor();
6231 if (vim_iswhite(cc))
6232 {
6233 save_char = cc;
6234 pchar_cursor('x');
6235 }
6236 }
6237
6238 /*
6239 * Repeat breaking lines, until the current line is not too long.
6240 */
6241 while (!got_int)
6242 {
6243 int startcol; /* Cursor column at entry */
6244 int wantcol; /* column at textwidth border */
6245 int foundcol; /* column for start of spaces */
6246 int end_foundcol = 0; /* column for start of word */
6247 colnr_T len;
6248 colnr_T virtcol;
6249#ifdef FEAT_VREPLACE
6250 int orig_col = 0;
6251 char_u *saved_text = NULL;
6252#endif
6253 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006254 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006255
Bram Moolenaar97b98102009-11-17 16:41:01 +00006256 virtcol = get_nolist_virtcol()
6257 + char2cells(c != NUL ? c : gchar_cursor());
6258 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006259 break;
6260
6261#ifdef FEAT_COMMENTS
6262 if (no_leader)
6263 do_comments = FALSE;
6264 else if (!(flags & INSCHAR_FORMAT)
6265 && has_format_option(FO_WRAP_COMS))
6266 do_comments = TRUE;
6267
6268 /* Don't break until after the comment leader */
6269 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006270 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006271 else
6272 leader_len = 0;
6273
6274 /* If the line doesn't start with a comment leader, then don't
6275 * start one in a following broken line. Avoids that a %word
6276 * moved to the start of the next line causes all following lines
6277 * to start with %. */
6278 if (leader_len == 0)
6279 no_leader = TRUE;
6280#endif
6281 if (!(flags & INSCHAR_FORMAT)
6282#ifdef FEAT_COMMENTS
6283 && leader_len == 0
6284#endif
6285 && !has_format_option(FO_WRAP))
6286
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006287 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006288 if ((startcol = curwin->w_cursor.col) == 0)
6289 break;
6290
6291 /* find column of textwidth border */
6292 coladvance((colnr_T)textwidth);
6293 wantcol = curwin->w_cursor.col;
6294
Bram Moolenaar97b98102009-11-17 16:41:01 +00006295 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006296 foundcol = 0;
6297
6298 /*
6299 * Find position to break at.
6300 * Stop at first entered white when 'formatoptions' has 'v'
6301 */
6302 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006303 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006304 || curwin->w_cursor.lnum != Insstart.lnum
6305 || curwin->w_cursor.col >= Insstart.col)
6306 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006307 if (curwin->w_cursor.col == startcol && c != NUL)
6308 cc = c;
6309 else
6310 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006311 if (WHITECHAR(cc))
6312 {
6313 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006314 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006315
6316 /* find start of sequence of blanks */
6317 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6318 {
6319 dec_cursor();
6320 cc = gchar_cursor();
6321 }
6322 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6323 break; /* only spaces in front of text */
6324#ifdef FEAT_COMMENTS
6325 /* Don't break until after the comment leader */
6326 if (curwin->w_cursor.col < leader_len)
6327 break;
6328#endif
6329 if (has_format_option(FO_ONE_LETTER))
6330 {
6331 /* do not break after one-letter words */
6332 if (curwin->w_cursor.col == 0)
6333 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006334#ifdef FEAT_COMMENTS
6335 /* do not break "#a b" when 'tw' is 2 */
6336 if (curwin->w_cursor.col <= leader_len)
6337 break;
6338#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006339 col = curwin->w_cursor.col;
6340 dec_cursor();
6341 cc = gchar_cursor();
6342
6343 if (WHITECHAR(cc))
6344 continue; /* one-letter, continue */
6345 curwin->w_cursor.col = col;
6346 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006347
6348 inc_cursor();
6349
6350 end_foundcol = end_col + 1;
6351 foundcol = curwin->w_cursor.col;
6352 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006353 break;
6354 }
6355#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006356 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006357 {
6358 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006359 if (curwin->w_cursor.col != startcol)
6360 {
6361#ifdef FEAT_COMMENTS
6362 /* Don't break until after the comment leader */
6363 if (curwin->w_cursor.col < leader_len)
6364 break;
6365#endif
6366 col = curwin->w_cursor.col;
6367 inc_cursor();
6368 /* Don't change end_foundcol if already set. */
6369 if (foundcol != curwin->w_cursor.col)
6370 {
6371 foundcol = curwin->w_cursor.col;
6372 end_foundcol = foundcol;
6373 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6374 break;
6375 }
6376 curwin->w_cursor.col = col;
6377 }
6378
6379 if (curwin->w_cursor.col == 0)
6380 break;
6381
6382 col = curwin->w_cursor.col;
6383
6384 dec_cursor();
6385 cc = gchar_cursor();
6386
6387 if (WHITECHAR(cc))
6388 continue; /* break with space */
6389#ifdef FEAT_COMMENTS
6390 /* Don't break until after the comment leader */
6391 if (curwin->w_cursor.col < leader_len)
6392 break;
6393#endif
6394
6395 curwin->w_cursor.col = col;
6396
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006397 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006398 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006399 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6400 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006401 }
6402#endif
6403 if (curwin->w_cursor.col == 0)
6404 break;
6405 dec_cursor();
6406 }
6407
6408 if (foundcol == 0) /* no spaces, cannot break line */
6409 {
6410 curwin->w_cursor.col = startcol;
6411 break;
6412 }
6413
6414 /* Going to break the line, remove any "$" now. */
6415 undisplay_dollar();
6416
6417 /*
6418 * Offset between cursor position and line break is used by replace
6419 * stack functions. VREPLACE does not use this, and backspaces
6420 * over the text instead.
6421 */
6422#ifdef FEAT_VREPLACE
6423 if (State & VREPLACE_FLAG)
6424 orig_col = startcol; /* Will start backspacing from here */
6425 else
6426#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006427 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006428
6429 /*
6430 * adjust startcol for spaces that will be deleted and
6431 * characters that will remain on top line
6432 */
6433 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006434 while ((cc = gchar_cursor(), WHITECHAR(cc))
6435 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006436 inc_cursor();
6437 startcol -= curwin->w_cursor.col;
6438 if (startcol < 0)
6439 startcol = 0;
6440
6441#ifdef FEAT_VREPLACE
6442 if (State & VREPLACE_FLAG)
6443 {
6444 /*
6445 * In VREPLACE mode, we will backspace over the text to be
6446 * wrapped, so save a copy now to put on the next line.
6447 */
6448 saved_text = vim_strsave(ml_get_cursor());
6449 curwin->w_cursor.col = orig_col;
6450 if (saved_text == NULL)
6451 break; /* Can't do it, out of memory */
6452 saved_text[startcol] = NUL;
6453
6454 /* Backspace over characters that will move to the next line */
6455 if (!fo_white_par)
6456 backspace_until_column(foundcol);
6457 }
6458 else
6459#endif
6460 {
6461 /* put cursor after pos. to break line */
6462 if (!fo_white_par)
6463 curwin->w_cursor.col = foundcol;
6464 }
6465
6466 /*
6467 * Split the line just before the margin.
6468 * Only insert/delete lines, but don't really redraw the window.
6469 */
6470 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6471 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6472#ifdef FEAT_COMMENTS
6473 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006474 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006475#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006476 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6477 if (!(flags & INSCHAR_COM_LIST))
6478 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006479
6480 replace_offset = 0;
6481 if (first_line)
6482 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006483 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006484 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006485 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006486 * This section is for auto-wrap of numeric lists. When not
6487 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6488 * flag will be set and open_line() will handle it (as seen
6489 * above). The code here (and in get_number_indent()) will
6490 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006491 */
6492 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006493 second_indent =
6494 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006495 if (second_indent >= 0)
6496 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006497#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006498 if (State & VREPLACE_FLAG)
6499 change_indent(INDENT_SET, second_indent,
6500 FALSE, NUL, TRUE);
6501 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006502#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006503#ifdef FEAT_COMMENTS
6504 if (leader_len > 0 && second_indent - leader_len > 0)
6505 {
6506 int i;
6507 int padding = second_indent - leader_len;
6508
6509 /* We started at the first_line of a numbered list
6510 * that has a comment. the open_line() function has
6511 * inserted the proper comment leader and positioned
6512 * the cursor at the end of the split line. Now we
6513 * add the additional whitespace needed after the
6514 * comment leader for the numbered list. */
6515 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006516 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006517 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006518 }
6519 else
6520 {
6521#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006522 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006523#ifdef FEAT_COMMENTS
6524 }
6525#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006526 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006527 }
6528 first_line = FALSE;
6529 }
6530
6531#ifdef FEAT_VREPLACE
6532 if (State & VREPLACE_FLAG)
6533 {
6534 /*
6535 * In VREPLACE mode we have backspaced over the text to be
6536 * moved, now we re-insert it into the new line.
6537 */
6538 ins_bytes(saved_text);
6539 vim_free(saved_text);
6540 }
6541 else
6542#endif
6543 {
6544 /*
6545 * Check if cursor is not past the NUL off the line, cindent
6546 * may have added or removed indent.
6547 */
6548 curwin->w_cursor.col += startcol;
6549 len = (colnr_T)STRLEN(ml_get_curline());
6550 if (curwin->w_cursor.col > len)
6551 curwin->w_cursor.col = len;
6552 }
6553
6554 haveto_redraw = TRUE;
6555#ifdef FEAT_CINDENT
6556 can_cindent = TRUE;
6557#endif
6558 /* moved the cursor, don't autoindent or cindent now */
6559 did_ai = FALSE;
6560#ifdef FEAT_SMARTINDENT
6561 did_si = FALSE;
6562 can_si = FALSE;
6563 can_si_back = FALSE;
6564#endif
6565 line_breakcheck();
6566 }
6567
6568 if (save_char != NUL) /* put back space after cursor */
6569 pchar_cursor(save_char);
6570
Bram Moolenaar0026d472014-09-09 16:32:39 +02006571#ifdef FEAT_LINEBREAK
6572 curwin->w_p_lbr = has_lbr;
6573#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006574 if (!format_only && haveto_redraw)
6575 {
6576 update_topline();
6577 redraw_curbuf_later(VALID);
6578 }
6579}
6580
6581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582 * Called after inserting or deleting text: When 'formatoptions' includes the
6583 * 'a' flag format from the current line until the end of the paragraph.
6584 * Keep the cursor at the same position relative to the text.
6585 * The caller must have saved the cursor line for undo, following ones will be
6586 * saved here.
6587 */
6588 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006589auto_format(
6590 int trailblank, /* when TRUE also format with trailing blank */
6591 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592{
6593 pos_T pos;
6594 colnr_T len;
6595 char_u *old;
6596 char_u *new, *pnew;
6597 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006598 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006599
6600 if (!has_format_option(FO_AUTO))
6601 return;
6602
6603 pos = curwin->w_cursor;
6604 old = ml_get_curline();
6605
6606 /* may remove added space */
6607 check_auto_format(FALSE);
6608
6609 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6610 * user might insert normal text next. Also skip formatting when "1" is
6611 * in 'formatoptions' and there is a single character before the cursor.
6612 * Otherwise the line would be broken and when typing another non-white
6613 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006614 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 if (*old != NUL && !trailblank && wasatend)
6616 {
6617 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006618 cc = gchar_cursor();
6619 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6620 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006622 cc = gchar_cursor();
6623 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 {
6625 curwin->w_cursor = pos;
6626 return;
6627 }
6628 curwin->w_cursor = pos;
6629 }
6630
6631#ifdef FEAT_COMMENTS
6632 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6633 * comments. */
6634 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006635 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636 return;
6637#endif
6638
6639 /*
6640 * May start formatting in a previous line, so that after "x" a word is
6641 * moved to the previous line if it fits there now. Only when this is not
6642 * the start of a paragraph.
6643 */
6644 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6645 {
6646 --curwin->w_cursor.lnum;
6647 if (u_save_cursor() == FAIL)
6648 return;
6649 }
6650
6651 /*
6652 * Do the formatting and restore the cursor position. "saved_cursor" will
6653 * be adjusted for the text formatting.
6654 */
6655 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006656 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006657 curwin->w_cursor = saved_cursor;
6658 saved_cursor.lnum = 0;
6659
6660 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6661 {
6662 /* "cannot happen" */
6663 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6664 coladvance((colnr_T)MAXCOL);
6665 }
6666 else
6667 check_cursor_col();
6668
6669 /* Insert mode: If the cursor is now after the end of the line while it
6670 * previously wasn't, the line was broken. Because of the rule above we
6671 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6672 * formatted. */
6673 if (!wasatend && has_format_option(FO_WHITE_PAR))
6674 {
6675 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006676 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677 if (curwin->w_cursor.col == len)
6678 {
6679 pnew = vim_strnsave(new, len + 2);
6680 pnew[len] = ' ';
6681 pnew[len + 1] = NUL;
6682 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6683 /* remove the space later */
6684 did_add_space = TRUE;
6685 }
6686 else
6687 /* may remove added space */
6688 check_auto_format(FALSE);
6689 }
6690
6691 check_cursor();
6692}
6693
6694/*
6695 * When an extra space was added to continue a paragraph for auto-formatting,
6696 * delete it now. The space must be under the cursor, just after the insert
6697 * position.
6698 */
6699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006700check_auto_format(
6701 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702{
6703 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006704 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705
6706 if (did_add_space)
6707 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006708 cc = gchar_cursor();
6709 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 /* Somehow the space was removed already. */
6711 did_add_space = FALSE;
6712 else
6713 {
6714 if (!end_insert)
6715 {
6716 inc_cursor();
6717 c = gchar_cursor();
6718 dec_cursor();
6719 }
6720 if (c != NUL)
6721 {
6722 /* The space is no longer at the end of the line, delete it. */
6723 del_char(FALSE);
6724 did_add_space = FALSE;
6725 }
6726 }
6727 }
6728}
6729
6730/*
6731 * Find out textwidth to be used for formatting:
6732 * if 'textwidth' option is set, use it
6733 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6734 * if invalid value, use 0.
6735 * Set default to window width (maximum 79) for "gq" operator.
6736 */
6737 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006738comp_textwidth(
6739 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740{
6741 int textwidth;
6742
6743 textwidth = curbuf->b_p_tw;
6744 if (textwidth == 0 && curbuf->b_p_wm)
6745 {
6746 /* The width is the window width minus 'wrapmargin' minus all the
6747 * things that add to the margin. */
6748 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6749#ifdef FEAT_CMDWIN
6750 if (cmdwin_type != 0)
6751 textwidth -= 1;
6752#endif
6753#ifdef FEAT_FOLDING
6754 textwidth -= curwin->w_p_fdc;
6755#endif
6756#ifdef FEAT_SIGNS
6757 if (curwin->w_buffer->b_signlist != NULL
6758# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01006759 || curwin->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760# endif
6761 )
6762 textwidth -= 1;
6763#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006764 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006765 textwidth -= 8;
6766 }
6767 if (textwidth < 0)
6768 textwidth = 0;
6769 if (ff && textwidth == 0)
6770 {
6771 textwidth = W_WIDTH(curwin) - 1;
6772 if (textwidth > 79)
6773 textwidth = 79;
6774 }
6775 return textwidth;
6776}
6777
6778/*
6779 * Put a character in the redo buffer, for when just after a CTRL-V.
6780 */
6781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006782redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783{
6784 char_u buf[10];
6785
6786 /* Only digits need special treatment. Translate them into a string of
6787 * three digits. */
6788 if (VIM_ISDIGIT(c))
6789 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006790 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791 AppendToRedobuff(buf);
6792 }
6793 else
6794 AppendCharToRedobuff(c);
6795}
6796
6797/*
6798 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006799 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 */
6801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006802start_arrow(
6803 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006805 start_arrow_common(end_insert_pos, TRUE);
6806}
6807
6808/*
6809 * Like start_arrow() but with end_change argument.
6810 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6811 */
6812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006813start_arrow_with_change(
6814 pos_T *end_insert_pos, /* can be NULL */
6815 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006816{
6817 start_arrow_common(end_insert_pos, end_change);
6818 if (!end_change)
6819 {
6820 AppendCharToRedobuff(Ctrl_G);
6821 AppendCharToRedobuff('U');
6822 }
6823}
6824
6825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006826start_arrow_common(
6827 pos_T *end_insert_pos, /* can be NULL */
6828 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006829{
6830 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 {
6832 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006833 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 arrow_used = TRUE; /* this means we stopped the current insert */
6835 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006836#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006837 check_spell_redraw();
6838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839}
6840
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006841#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006842/*
6843 * If we skipped highlighting word at cursor, do it now.
6844 * It may be skipped again, thus reset spell_redraw_lnum first.
6845 */
6846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006847check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006848{
6849 if (spell_redraw_lnum != 0)
6850 {
6851 linenr_T lnum = spell_redraw_lnum;
6852
6853 spell_redraw_lnum = 0;
6854 redrawWinline(lnum, FALSE);
6855 }
6856}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006857
6858/*
6859 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6860 * spelled word, if there is one.
6861 */
6862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006863spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006864{
6865 pos_T tpos = curwin->w_cursor;
6866
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006867 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006868 if (curwin->w_cursor.col != tpos.col)
6869 start_arrow(&tpos);
6870}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006871#endif
6872
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873/*
6874 * stop_arrow() is called before a change is made in insert mode.
6875 * If an arrow key has been used, start a new insertion.
6876 * Returns FAIL if undo is impossible, shouldn't insert then.
6877 */
6878 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006879stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880{
6881 if (arrow_used)
6882 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006883 Insstart = curwin->w_cursor; /* new insertion starts here */
6884 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6885 /* Don't update the original insert position when moved to the
6886 * right, except when nothing was inserted yet. */
6887 update_Insstart_orig = FALSE;
6888 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6889
Bram Moolenaar071d4272004-06-13 20:20:40 +00006890 if (u_save_cursor() == OK)
6891 {
6892 arrow_used = FALSE;
6893 ins_need_undo = FALSE;
6894 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006895
Bram Moolenaar071d4272004-06-13 20:20:40 +00006896 ai_col = 0;
6897#ifdef FEAT_VREPLACE
6898 if (State & VREPLACE_FLAG)
6899 {
6900 orig_line_count = curbuf->b_ml.ml_line_count;
6901 vr_lines_changed = 1;
6902 }
6903#endif
6904 ResetRedobuff();
6905 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006906 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907 }
6908 else if (ins_need_undo)
6909 {
6910 if (u_save_cursor() == OK)
6911 ins_need_undo = FALSE;
6912 }
6913
6914#ifdef FEAT_FOLDING
6915 /* Always open fold at the cursor line when inserting something. */
6916 foldOpenCursor();
6917#endif
6918
6919 return (arrow_used || ins_need_undo ? FAIL : OK);
6920}
6921
6922/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006923 * Do a few things to stop inserting.
6924 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6925 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 */
6927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006928stop_insert(
6929 pos_T *end_insert_pos,
6930 int esc, /* called by ins_esc() */
6931 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006933 int cc;
6934 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935
6936 stop_redo_ins();
6937 replace_flush(); /* abandon replace stack */
6938
6939 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006940 * Save the inserted text for later redo with ^@ and CTRL-A.
6941 * Don't do it when "restart_edit" was set and nothing was inserted,
6942 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006944 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006945 if (did_restart_edit == 0 || (ptr != NULL
6946 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006947 {
6948 vim_free(last_insert);
6949 last_insert = ptr;
6950 last_insert_skip = new_insert_skip;
6951 }
6952 else
6953 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006955 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 {
6957 /* Auto-format now. It may seem strange to do this when stopping an
6958 * insertion (or moving the cursor), but it's required when appending
6959 * a line and having it end in a space. But only do it when something
6960 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006961 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006963 pos_T tpos = curwin->w_cursor;
6964
Bram Moolenaar071d4272004-06-13 20:20:40 +00006965 /* When the cursor is at the end of the line after a space the
6966 * formatting will move it to the following word. Avoid that by
6967 * moving the cursor onto the space. */
6968 cc = 'x';
6969 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6970 {
6971 dec_cursor();
6972 cc = gchar_cursor();
6973 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006974 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975 }
6976
6977 auto_format(TRUE, FALSE);
6978
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006979 if (vim_iswhite(cc))
6980 {
6981 if (gchar_cursor() != NUL)
6982 inc_cursor();
6983#ifdef FEAT_VIRTUALEDIT
6984 /* If the cursor is still at the same character, also keep
6985 * the "coladd". */
6986 if (gchar_cursor() == NUL
6987 && curwin->w_cursor.lnum == tpos.lnum
6988 && curwin->w_cursor.col == tpos.col)
6989 curwin->w_cursor.coladd = tpos.coladd;
6990#endif
6991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992 }
6993
6994 /* If a space was inserted for auto-formatting, remove it now. */
6995 check_auto_format(TRUE);
6996
6997 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006998 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006999 * Do this when ESC was used or moving the cursor up/down.
7000 * Check for the old position still being valid, just in case the text
7001 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007002 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007003 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7004 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007006 pos_T tpos = curwin->w_cursor;
7007
7008 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007009 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007010 for (;;)
7011 {
7012 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7013 --curwin->w_cursor.col;
7014 cc = gchar_cursor();
7015 if (!vim_iswhite(cc))
7016 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007017 if (del_char(TRUE) == FAIL)
7018 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007019 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007020 if (curwin->w_cursor.lnum != tpos.lnum)
7021 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007022 else
7023 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007024 /* reset tpos, could have been invalidated in the loop above */
7025 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007026 tpos.col++;
7027 if (cc != NUL && gchar_pos(&tpos) == NUL)
7028 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 /* <C-S-Right> may have started Visual mode, adjust the position for
7032 * deleted characters. */
7033 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7034 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007035 int len = (int)STRLEN(ml_get_curline());
7036
7037 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007039 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007040#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007041 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 }
7044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 }
7046 }
7047 did_ai = FALSE;
7048#ifdef FEAT_SMARTINDENT
7049 did_si = FALSE;
7050 can_si = FALSE;
7051 can_si_back = FALSE;
7052#endif
7053
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007054 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7055 * now in a different buffer. */
7056 if (end_insert_pos != NULL)
7057 {
7058 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007059 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007060 curbuf->b_op_end = *end_insert_pos;
7061 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062}
7063
7064/*
7065 * Set the last inserted text to a single character.
7066 * Used for the replace command.
7067 */
7068 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007069set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070{
7071 char_u *s;
7072
7073 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 if (last_insert != NULL)
7076 {
7077 s = last_insert;
7078 /* Use the CTRL-V only when entering a special char */
7079 if (c < ' ' || c == DEL)
7080 *s++ = Ctrl_V;
7081 s = add_char2buf(c, s);
7082 *s++ = ESC;
7083 *s++ = NUL;
7084 last_insert_skip = 0;
7085 }
7086}
7087
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007088#if defined(EXITFREE) || defined(PROTO)
7089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007090free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007091{
7092 vim_free(last_insert);
7093 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007094# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007095 vim_free(compl_orig_text);
7096 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007097# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007098}
7099#endif
7100
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101/*
7102 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7103 * and CSI. Handle multi-byte characters.
7104 * Returns a pointer to after the added bytes.
7105 */
7106 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007107add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007108{
7109#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007110 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 int i;
7112 int len;
7113
7114 len = (*mb_char2bytes)(c, temp);
7115 for (i = 0; i < len; ++i)
7116 {
7117 c = temp[i];
7118#endif
7119 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7120 if (c == K_SPECIAL)
7121 {
7122 *s++ = K_SPECIAL;
7123 *s++ = KS_SPECIAL;
7124 *s++ = KE_FILLER;
7125 }
7126#ifdef FEAT_GUI
7127 else if (c == CSI)
7128 {
7129 *s++ = CSI;
7130 *s++ = KS_EXTRA;
7131 *s++ = (int)KE_CSI;
7132 }
7133#endif
7134 else
7135 *s++ = c;
7136#ifdef FEAT_MBYTE
7137 }
7138#endif
7139 return s;
7140}
7141
7142/*
7143 * move cursor to start of line
7144 * if flags & BL_WHITE move to first non-white
7145 * if flags & BL_SOL move to first non-white if startofline is set,
7146 * otherwise keep "curswant" column
7147 * if flags & BL_FIX don't leave the cursor on a NUL.
7148 */
7149 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007150beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151{
7152 if ((flags & BL_SOL) && !p_sol)
7153 coladvance(curwin->w_curswant);
7154 else
7155 {
7156 curwin->w_cursor.col = 0;
7157#ifdef FEAT_VIRTUALEDIT
7158 curwin->w_cursor.coladd = 0;
7159#endif
7160
7161 if (flags & (BL_WHITE | BL_SOL))
7162 {
7163 char_u *ptr;
7164
7165 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7166 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7167 ++curwin->w_cursor.col;
7168 }
7169 curwin->w_set_curswant = TRUE;
7170 }
7171}
7172
7173/*
7174 * oneright oneleft cursor_down cursor_up
7175 *
7176 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007177 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 * Return OK when successful, FAIL when we hit a line of file boundary.
7179 */
7180
7181 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007182oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183{
7184 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186
7187#ifdef FEAT_VIRTUALEDIT
7188 if (virtual_active())
7189 {
7190 pos_T prevpos = curwin->w_cursor;
7191
7192 /* Adjust for multi-wide char (excluding TAB) */
7193 ptr = ml_get_cursor();
7194 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007195# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007197# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007199# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200 ))
7201 ? ptr2cells(ptr) : 1));
7202 curwin->w_set_curswant = TRUE;
7203 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7204 return (prevpos.col != curwin->w_cursor.col
7205 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7206 }
7207#endif
7208
7209 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007210 if (*ptr == NUL)
7211 return FAIL; /* already at the very end */
7212
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007214 if (has_mbyte)
7215 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216 else
7217#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007218 l = 1;
7219
7220 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7221 * contains "onemore". */
7222 if (ptr[l] == NUL
7223#ifdef FEAT_VIRTUALEDIT
7224 && (ve_flags & VE_ONEMORE) == 0
7225#endif
7226 )
7227 return FAIL;
7228 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229
7230 curwin->w_set_curswant = TRUE;
7231 return OK;
7232}
7233
7234 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007235oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236{
7237#ifdef FEAT_VIRTUALEDIT
7238 if (virtual_active())
7239 {
7240 int width;
7241 int v = getviscol();
7242
7243 if (v == 0)
7244 return FAIL;
7245
7246# ifdef FEAT_LINEBREAK
7247 /* We might get stuck on 'showbreak', skip over it. */
7248 width = 1;
7249 for (;;)
7250 {
7251 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007252 /* getviscol() is slow, skip it when 'showbreak' is empty,
7253 * 'breakindent' is not set and there are no multi-byte
7254 * characters */
7255 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007256# ifdef FEAT_MBYTE
7257 && !has_mbyte
7258# endif
7259 ) || getviscol() < v)
7260 break;
7261 ++width;
7262 }
7263# else
7264 coladvance(v - 1);
7265# endif
7266
7267 if (curwin->w_cursor.coladd == 1)
7268 {
7269 char_u *ptr;
7270
7271 /* Adjust for multi-wide char (not a TAB) */
7272 ptr = ml_get_cursor();
7273 if (*ptr != TAB && vim_isprintc(
7274# ifdef FEAT_MBYTE
7275 (*mb_ptr2char)(ptr)
7276# else
7277 *ptr
7278# endif
7279 ) && ptr2cells(ptr) > 1)
7280 curwin->w_cursor.coladd = 0;
7281 }
7282
7283 curwin->w_set_curswant = TRUE;
7284 return OK;
7285 }
7286#endif
7287
7288 if (curwin->w_cursor.col == 0)
7289 return FAIL;
7290
7291 curwin->w_set_curswant = TRUE;
7292 --curwin->w_cursor.col;
7293
7294#ifdef FEAT_MBYTE
7295 /* if the character on the left of the current cursor is a multi-byte
7296 * character, move to its first byte */
7297 if (has_mbyte)
7298 mb_adjust_cursor();
7299#endif
7300 return OK;
7301}
7302
7303 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007304cursor_up(
7305 long n,
7306 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007307{
7308 linenr_T lnum;
7309
7310 if (n > 0)
7311 {
7312 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007313 /* This fails if the cursor is already in the first line or the count
7314 * is larger than the line number and '-' is in 'cpoptions' */
7315 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007316 return FAIL;
7317 if (n >= lnum)
7318 lnum = 1;
7319 else
7320#ifdef FEAT_FOLDING
7321 if (hasAnyFolding(curwin))
7322 {
7323 /*
7324 * Count each sequence of folded lines as one logical line.
7325 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007326 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007327 (void)hasFolding(lnum, &lnum, NULL);
7328
7329 while (n--)
7330 {
7331 /* move up one line */
7332 --lnum;
7333 if (lnum <= 1)
7334 break;
7335 /* If we entered a fold, move to the beginning, unless in
7336 * Insert mode or when 'foldopen' contains "all": it will open
7337 * in a moment. */
7338 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7339 (void)hasFolding(lnum, &lnum, NULL);
7340 }
7341 if (lnum < 1)
7342 lnum = 1;
7343 }
7344 else
7345#endif
7346 lnum -= n;
7347 curwin->w_cursor.lnum = lnum;
7348 }
7349
7350 /* try to advance to the column we want to be at */
7351 coladvance(curwin->w_curswant);
7352
7353 if (upd_topline)
7354 update_topline(); /* make sure curwin->w_topline is valid */
7355
7356 return OK;
7357}
7358
7359/*
7360 * Cursor down a number of logical lines.
7361 */
7362 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007363cursor_down(
7364 long n,
7365 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366{
7367 linenr_T lnum;
7368
7369 if (n > 0)
7370 {
7371 lnum = curwin->w_cursor.lnum;
7372#ifdef FEAT_FOLDING
7373 /* Move to last line of fold, will fail if it's the end-of-file. */
7374 (void)hasFolding(lnum, NULL, &lnum);
7375#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007376 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007377 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007378 if (lnum >= curbuf->b_ml.ml_line_count
7379 || (lnum + n > curbuf->b_ml.ml_line_count
7380 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007381 return FAIL;
7382 if (lnum + n >= curbuf->b_ml.ml_line_count)
7383 lnum = curbuf->b_ml.ml_line_count;
7384 else
7385#ifdef FEAT_FOLDING
7386 if (hasAnyFolding(curwin))
7387 {
7388 linenr_T last;
7389
7390 /* count each sequence of folded lines as one logical line */
7391 while (n--)
7392 {
7393 if (hasFolding(lnum, NULL, &last))
7394 lnum = last + 1;
7395 else
7396 ++lnum;
7397 if (lnum >= curbuf->b_ml.ml_line_count)
7398 break;
7399 }
7400 if (lnum > curbuf->b_ml.ml_line_count)
7401 lnum = curbuf->b_ml.ml_line_count;
7402 }
7403 else
7404#endif
7405 lnum += n;
7406 curwin->w_cursor.lnum = lnum;
7407 }
7408
7409 /* try to advance to the column we want to be at */
7410 coladvance(curwin->w_curswant);
7411
7412 if (upd_topline)
7413 update_topline(); /* make sure curwin->w_topline is valid */
7414
7415 return OK;
7416}
7417
7418/*
7419 * Stuff the last inserted text in the read buffer.
7420 * Last_insert actually is a copy of the redo buffer, so we
7421 * first have to remove the command.
7422 */
7423 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007424stuff_inserted(
7425 int c, /* Command character to be inserted */
7426 long count, /* Repeat this many times */
7427 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007428{
7429 char_u *esc_ptr;
7430 char_u *ptr;
7431 char_u *last_ptr;
7432 char_u last = NUL;
7433
7434 ptr = get_last_insert();
7435 if (ptr == NULL)
7436 {
7437 EMSG(_(e_noinstext));
7438 return FAIL;
7439 }
7440
7441 /* may want to stuff the command character, to start Insert mode */
7442 if (c != NUL)
7443 stuffcharReadbuff(c);
7444 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7445 *esc_ptr = NUL; /* remove the ESC */
7446
7447 /* when the last char is either "0" or "^" it will be quoted if no ESC
7448 * comes after it OR if it will inserted more than once and "ptr"
7449 * starts with ^D. -- Acevedo
7450 */
7451 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7452 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7453 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7454 {
7455 last = *last_ptr;
7456 *last_ptr = NUL;
7457 }
7458
7459 do
7460 {
7461 stuffReadbuff(ptr);
7462 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7463 if (last)
7464 stuffReadbuff((char_u *)(last == '0'
7465 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7466 : IF_EB("\026^", CTRL_V_STR "^")));
7467 }
7468 while (--count > 0);
7469
7470 if (last)
7471 *last_ptr = last;
7472
7473 if (esc_ptr != NULL)
7474 *esc_ptr = ESC; /* put the ESC back */
7475
7476 /* may want to stuff a trailing ESC, to get out of Insert mode */
7477 if (!no_esc)
7478 stuffcharReadbuff(ESC);
7479
7480 return OK;
7481}
7482
7483 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007484get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485{
7486 if (last_insert == NULL)
7487 return NULL;
7488 return last_insert + last_insert_skip;
7489}
7490
7491/*
7492 * Get last inserted string, and remove trailing <Esc>.
7493 * Returns pointer to allocated memory (must be freed) or NULL.
7494 */
7495 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007496get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007497{
7498 char_u *s;
7499 int len;
7500
7501 if (last_insert == NULL)
7502 return NULL;
7503 s = vim_strsave(last_insert + last_insert_skip);
7504 if (s != NULL)
7505 {
7506 len = (int)STRLEN(s);
7507 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7508 s[len - 1] = NUL;
7509 }
7510 return s;
7511}
7512
7513/*
7514 * Check the word in front of the cursor for an abbreviation.
7515 * Called when the non-id character "c" has been entered.
7516 * When an abbreviation is recognized it is removed from the text and
7517 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7518 */
7519 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007520echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521{
7522 /* Don't check for abbreviation in paste mode, when disabled and just
7523 * after moving around with cursor keys. */
7524 if (p_paste || no_abbr || arrow_used)
7525 return FALSE;
7526
7527 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7528 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7529}
7530
7531/*
7532 * replace-stack functions
7533 *
7534 * When replacing characters, the replaced characters are remembered for each
7535 * new character. This is used to re-insert the old text when backspacing.
7536 *
7537 * There is a NUL headed list of characters for each character that is
7538 * currently in the file after the insertion point. When BS is used, one NUL
7539 * headed list is put back for the deleted character.
7540 *
7541 * For a newline, there are two NUL headed lists. One contains the characters
7542 * that the NL replaced. The extra one stores the characters after the cursor
7543 * that were deleted (always white space).
7544 *
7545 * Replace_offset is normally 0, in which case replace_push will add a new
7546 * character at the end of the stack. If replace_offset is not 0, that many
7547 * characters will be left on the stack above the newly inserted character.
7548 */
7549
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007550static char_u *replace_stack = NULL;
7551static long replace_stack_nr = 0; /* next entry in replace stack */
7552static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007553
7554 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007555replace_push(
7556 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007557{
7558 char_u *p;
7559
7560 if (replace_stack_nr < replace_offset) /* nothing to do */
7561 return;
7562 if (replace_stack_len <= replace_stack_nr)
7563 {
7564 replace_stack_len += 50;
7565 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7566 if (p == NULL) /* out of memory */
7567 {
7568 replace_stack_len -= 50;
7569 return;
7570 }
7571 if (replace_stack != NULL)
7572 {
7573 mch_memmove(p, replace_stack,
7574 (size_t)(replace_stack_nr * sizeof(char_u)));
7575 vim_free(replace_stack);
7576 }
7577 replace_stack = p;
7578 }
7579 p = replace_stack + replace_stack_nr - replace_offset;
7580 if (replace_offset)
7581 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7582 *p = c;
7583 ++replace_stack_nr;
7584}
7585
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007586#if defined(FEAT_MBYTE) || defined(PROTO)
7587/*
7588 * Push a character onto the replace stack. Handles a multi-byte character in
7589 * reverse byte order, so that the first byte is popped off first.
7590 * Return the number of bytes done (includes composing characters).
7591 */
7592 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007593replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007594{
7595 int l = (*mb_ptr2len)(p);
7596 int j;
7597
7598 for (j = l - 1; j >= 0; --j)
7599 replace_push(p[j]);
7600 return l;
7601}
7602#endif
7603
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604/*
7605 * Pop one item from the replace stack.
7606 * return -1 if stack empty
7607 * return replaced character or NUL otherwise
7608 */
7609 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007610replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007611{
7612 if (replace_stack_nr == 0)
7613 return -1;
7614 return (int)replace_stack[--replace_stack_nr];
7615}
7616
7617/*
7618 * Join the top two items on the replace stack. This removes to "off"'th NUL
7619 * encountered.
7620 */
7621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007622replace_join(
7623 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624{
7625 int i;
7626
7627 for (i = replace_stack_nr; --i >= 0; )
7628 if (replace_stack[i] == NUL && off-- <= 0)
7629 {
7630 --replace_stack_nr;
7631 mch_memmove(replace_stack + i, replace_stack + i + 1,
7632 (size_t)(replace_stack_nr - i));
7633 return;
7634 }
7635}
7636
7637/*
7638 * Pop bytes from the replace stack until a NUL is found, and insert them
7639 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7640 */
7641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007642replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643{
7644 int cc;
7645 int oldState = State;
7646
7647 State = NORMAL; /* don't want REPLACE here */
7648 while ((cc = replace_pop()) > 0)
7649 {
7650#ifdef FEAT_MBYTE
7651 mb_replace_pop_ins(cc);
7652#else
7653 ins_char(cc);
7654#endif
7655 dec_cursor();
7656 }
7657 State = oldState;
7658}
7659
7660#ifdef FEAT_MBYTE
7661/*
7662 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7663 * indicates a multi-byte char, pop the other bytes too.
7664 */
7665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007666mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007667{
7668 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007669 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 int i;
7671 int c;
7672
7673 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7674 {
7675 buf[0] = cc;
7676 for (i = 1; i < n; ++i)
7677 buf[i] = replace_pop();
7678 ins_bytes_len(buf, n);
7679 }
7680 else
7681 ins_char(cc);
7682
7683 if (enc_utf8)
7684 /* Handle composing chars. */
7685 for (;;)
7686 {
7687 c = replace_pop();
7688 if (c == -1) /* stack empty */
7689 break;
7690 if ((n = MB_BYTE2LEN(c)) == 1)
7691 {
7692 /* Not a multi-byte char, put it back. */
7693 replace_push(c);
7694 break;
7695 }
7696 else
7697 {
7698 buf[0] = c;
7699 for (i = 1; i < n; ++i)
7700 buf[i] = replace_pop();
7701 if (utf_iscomposing(utf_ptr2char(buf)))
7702 ins_bytes_len(buf, n);
7703 else
7704 {
7705 /* Not a composing char, put it back. */
7706 for (i = n - 1; i >= 0; --i)
7707 replace_push(buf[i]);
7708 break;
7709 }
7710 }
7711 }
7712}
7713#endif
7714
7715/*
7716 * make the replace stack empty
7717 * (called when exiting replace mode)
7718 */
7719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007720replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721{
7722 vim_free(replace_stack);
7723 replace_stack = NULL;
7724 replace_stack_len = 0;
7725 replace_stack_nr = 0;
7726}
7727
7728/*
7729 * Handle doing a BS for one character.
7730 * cc < 0: replace stack empty, just move cursor
7731 * cc == 0: character was inserted, delete it
7732 * cc > 0: character was replaced, put cc (first byte of original char) back
7733 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007734 * When "limit_col" is >= 0, don't delete before this column. Matters when
7735 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 */
7737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007738replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739{
7740 int cc;
7741#ifdef FEAT_VREPLACE
7742 int orig_len = 0;
7743 int ins_len;
7744 int orig_vcols = 0;
7745 colnr_T start_vcol;
7746 char_u *p;
7747 int i;
7748 int vcol;
7749#endif
7750
7751 cc = replace_pop();
7752 if (cc > 0)
7753 {
7754#ifdef FEAT_VREPLACE
7755 if (State & VREPLACE_FLAG)
7756 {
7757 /* Get the number of screen cells used by the character we are
7758 * going to delete. */
7759 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7760 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7761 }
7762#endif
7763#ifdef FEAT_MBYTE
7764 if (has_mbyte)
7765 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007766 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007767# ifdef FEAT_VREPLACE
7768 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007769 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007770# endif
7771 replace_push(cc);
7772 }
7773 else
7774#endif
7775 {
7776 pchar_cursor(cc);
7777#ifdef FEAT_VREPLACE
7778 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007779 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780#endif
7781 }
7782 replace_pop_ins();
7783
7784#ifdef FEAT_VREPLACE
7785 if (State & VREPLACE_FLAG)
7786 {
7787 /* Get the number of screen cells used by the inserted characters */
7788 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007789 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007790 vcol = start_vcol;
7791 for (i = 0; i < ins_len; ++i)
7792 {
7793 vcol += chartabsize(p + i, vcol);
7794#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007795 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007796#endif
7797 }
7798 vcol -= start_vcol;
7799
7800 /* Delete spaces that were inserted after the cursor to keep the
7801 * text aligned. */
7802 curwin->w_cursor.col += ins_len;
7803 while (vcol > orig_vcols && gchar_cursor() == ' ')
7804 {
7805 del_char(FALSE);
7806 ++orig_vcols;
7807 }
7808 curwin->w_cursor.col -= ins_len;
7809 }
7810#endif
7811
7812 /* mark the buffer as changed and prepare for displaying */
7813 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7814 }
7815 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007816 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007817}
7818
7819#ifdef FEAT_CINDENT
7820/*
7821 * Return TRUE if C-indenting is on.
7822 */
7823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007824cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825{
7826 return (!p_paste && (curbuf->b_p_cin
7827# ifdef FEAT_EVAL
7828 || *curbuf->b_p_inde != NUL
7829# endif
7830 ));
7831}
7832#endif
7833
7834#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7835/*
7836 * Re-indent the current line, based on the current contents of it and the
7837 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7838 * confused what all the part that handles Control-T is doing that I'm not.
7839 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7840 */
7841
7842 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007843fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007845 int amount = get_the_indent();
7846
7847 if (amount >= 0)
7848 {
7849 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7850 if (linewhite(curwin->w_cursor.lnum))
7851 did_ai = TRUE; /* delete the indent if the line stays empty */
7852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853}
7854
7855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007856fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007857{
7858 if (p_paste)
7859 return;
7860# ifdef FEAT_LISP
7861 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7862 fixthisline(get_lisp_indent);
7863# endif
7864# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7865 else
7866# endif
7867# ifdef FEAT_CINDENT
7868 if (cindent_on())
7869 do_c_expr_indent();
7870# endif
7871}
7872
7873#endif
7874
7875#ifdef FEAT_CINDENT
7876/*
7877 * return TRUE if 'cinkeys' contains the key "keytyped",
7878 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007879 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7881 *
7882 * "keytyped" can have a few special values:
7883 * KEY_OPEN_FORW
7884 * KEY_OPEN_BACK
7885 * KEY_COMPLETE just finished completion.
7886 *
7887 * If line_is_empty is TRUE accept keys with '0' before them.
7888 */
7889 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007890in_cinkeys(
7891 int keytyped,
7892 int when,
7893 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894{
7895 char_u *look;
7896 int try_match;
7897 int try_match_word;
7898 char_u *p;
7899 char_u *line;
7900 int icase;
7901 int i;
7902
Bram Moolenaar30848942009-12-24 14:46:12 +00007903 if (keytyped == NUL)
7904 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7905 return FALSE;
7906
Bram Moolenaar071d4272004-06-13 20:20:40 +00007907#ifdef FEAT_EVAL
7908 if (*curbuf->b_p_inde != NUL)
7909 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7910 else
7911#endif
7912 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7913 while (*look)
7914 {
7915 /*
7916 * Find out if we want to try a match with this key, depending on
7917 * 'when' and a '*' or '!' before the key.
7918 */
7919 switch (when)
7920 {
7921 case '*': try_match = (*look == '*'); break;
7922 case '!': try_match = (*look == '!'); break;
7923 default: try_match = (*look != '*'); break;
7924 }
7925 if (*look == '*' || *look == '!')
7926 ++look;
7927
7928 /*
7929 * If there is a '0', only accept a match if the line is empty.
7930 * But may still match when typing last char of a word.
7931 */
7932 if (*look == '0')
7933 {
7934 try_match_word = try_match;
7935 if (!line_is_empty)
7936 try_match = FALSE;
7937 ++look;
7938 }
7939 else
7940 try_match_word = FALSE;
7941
7942 /*
7943 * does it look like a control character?
7944 */
7945 if (*look == '^'
7946#ifdef EBCDIC
7947 && (Ctrl_chr(look[1]) != 0)
7948#else
7949 && look[1] >= '?' && look[1] <= '_'
7950#endif
7951 )
7952 {
7953 if (try_match && keytyped == Ctrl_chr(look[1]))
7954 return TRUE;
7955 look += 2;
7956 }
7957 /*
7958 * 'o' means "o" command, open forward.
7959 * 'O' means "O" command, open backward.
7960 */
7961 else if (*look == 'o')
7962 {
7963 if (try_match && keytyped == KEY_OPEN_FORW)
7964 return TRUE;
7965 ++look;
7966 }
7967 else if (*look == 'O')
7968 {
7969 if (try_match && keytyped == KEY_OPEN_BACK)
7970 return TRUE;
7971 ++look;
7972 }
7973
7974 /*
7975 * 'e' means to check for "else" at start of line and just before the
7976 * cursor.
7977 */
7978 else if (*look == 'e')
7979 {
7980 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7981 {
7982 p = ml_get_curline();
7983 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7984 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7985 return TRUE;
7986 }
7987 ++look;
7988 }
7989
7990 /*
7991 * ':' only causes an indent if it is at the end of a label or case
7992 * statement, or when it was before typing the ':' (to fix
7993 * class::method for C++).
7994 */
7995 else if (*look == ':')
7996 {
7997 if (try_match && keytyped == ':')
7998 {
7999 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008000 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008001 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008002 /* Need to get the line again after cin_islabel(). */
8003 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 if (curwin->w_cursor.col > 2
8005 && p[curwin->w_cursor.col - 1] == ':'
8006 && p[curwin->w_cursor.col - 2] == ':')
8007 {
8008 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008009 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008010 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008011 p = ml_get_curline();
8012 p[curwin->w_cursor.col - 1] = ':';
8013 if (i)
8014 return TRUE;
8015 }
8016 }
8017 ++look;
8018 }
8019
8020
8021 /*
8022 * Is it a key in <>, maybe?
8023 */
8024 else if (*look == '<')
8025 {
8026 if (try_match)
8027 {
8028 /*
8029 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8030 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8031 * >, *, : and ! keys if they really really want to.
8032 */
8033 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8034 && keytyped == look[1])
8035 return TRUE;
8036
8037 if (keytyped == get_special_key_code(look + 1))
8038 return TRUE;
8039 }
8040 while (*look && *look != '>')
8041 look++;
8042 while (*look == '>')
8043 look++;
8044 }
8045
8046 /*
8047 * Is it a word: "=word"?
8048 */
8049 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8050 {
8051 ++look;
8052 if (*look == '~')
8053 {
8054 icase = TRUE;
8055 ++look;
8056 }
8057 else
8058 icase = FALSE;
8059 p = vim_strchr(look, ',');
8060 if (p == NULL)
8061 p = look + STRLEN(look);
8062 if ((try_match || try_match_word)
8063 && curwin->w_cursor.col >= (colnr_T)(p - look))
8064 {
8065 int match = FALSE;
8066
8067#ifdef FEAT_INS_EXPAND
8068 if (keytyped == KEY_COMPLETE)
8069 {
8070 char_u *s;
8071
8072 /* Just completed a word, check if it starts with "look".
8073 * search back for the start of a word. */
8074 line = ml_get_curline();
8075# ifdef FEAT_MBYTE
8076 if (has_mbyte)
8077 {
8078 char_u *n;
8079
8080 for (s = line + curwin->w_cursor.col; s > line; s = n)
8081 {
8082 n = mb_prevptr(line, s);
8083 if (!vim_iswordp(n))
8084 break;
8085 }
8086 }
8087 else
8088# endif
8089 for (s = line + curwin->w_cursor.col; s > line; --s)
8090 if (!vim_iswordc(s[-1]))
8091 break;
8092 if (s + (p - look) <= line + curwin->w_cursor.col
8093 && (icase
8094 ? MB_STRNICMP(s, look, p - look)
8095 : STRNCMP(s, look, p - look)) == 0)
8096 match = TRUE;
8097 }
8098 else
8099#endif
8100 /* TODO: multi-byte */
8101 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8102 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8103 {
8104 line = ml_get_cursor();
8105 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8106 || !vim_iswordc(line[-(p - look) - 1]))
8107 && (icase
8108 ? MB_STRNICMP(line - (p - look), look, p - look)
8109 : STRNCMP(line - (p - look), look, p - look))
8110 == 0)
8111 match = TRUE;
8112 }
8113 if (match && try_match_word && !try_match)
8114 {
8115 /* "0=word": Check if there are only blanks before the
8116 * word. */
8117 line = ml_get_curline();
8118 if ((int)(skipwhite(line) - line) !=
8119 (int)(curwin->w_cursor.col - (p - look)))
8120 match = FALSE;
8121 }
8122 if (match)
8123 return TRUE;
8124 }
8125 look = p;
8126 }
8127
8128 /*
8129 * ok, it's a boring generic character.
8130 */
8131 else
8132 {
8133 if (try_match && *look == keytyped)
8134 return TRUE;
8135 ++look;
8136 }
8137
8138 /*
8139 * Skip over ", ".
8140 */
8141 look = skip_to_option_part(look);
8142 }
8143 return FALSE;
8144}
8145#endif /* FEAT_CINDENT */
8146
8147#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8148/*
8149 * Map Hebrew keyboard when in hkmap mode.
8150 */
8151 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008152hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153{
8154 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8155 {
8156 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8157 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8158 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8159 static char_u map[26] =
8160 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8161 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8162 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8163 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8164 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8165 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8166 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8167 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8168 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8169
8170 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8171 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8172 /* '-1'='sofit' */
8173 else if (c == 'x')
8174 return 'X';
8175 else if (c == 'q')
8176 return '\''; /* {geresh}={'} */
8177 else if (c == 246)
8178 return ' '; /* \"o --> ' ' for a german keyboard */
8179 else if (c == 228)
8180 return ' '; /* \"a --> ' ' -- / -- */
8181 else if (c == 252)
8182 return ' '; /* \"u --> ' ' -- / -- */
8183#ifdef EBCDIC
8184 else if (islower(c))
8185#else
8186 /* NOTE: islower() does not do the right thing for us on Linux so we
8187 * do this the same was as 5.7 and previous, so it works correctly on
8188 * all systems. Specifically, the e.g. Delete and Arrow keys are
8189 * munged and won't work if e.g. searching for Hebrew text.
8190 */
8191 else if (c >= 'a' && c <= 'z')
8192#endif
8193 return (int)(map[CharOrdLow(c)] + p_aleph);
8194 else
8195 return c;
8196 }
8197 else
8198 {
8199 switch (c)
8200 {
8201 case '`': return ';';
8202 case '/': return '.';
8203 case '\'': return ',';
8204 case 'q': return '/';
8205 case 'w': return '\'';
8206
8207 /* Hebrew letters - set offset from 'a' */
8208 case ',': c = '{'; break;
8209 case '.': c = 'v'; break;
8210 case ';': c = 't'; break;
8211 default: {
8212 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8213
8214#ifdef EBCDIC
8215 /* see note about islower() above */
8216 if (!islower(c))
8217#else
8218 if (c < 'a' || c > 'z')
8219#endif
8220 return c;
8221 c = str[CharOrdLow(c)];
8222 break;
8223 }
8224 }
8225
8226 return (int)(CharOrdLow(c) + p_aleph);
8227 }
8228}
8229#endif
8230
8231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008232ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233{
8234 int need_redraw = FALSE;
8235 int regname;
8236 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008237 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
8239 /*
8240 * If we are going to wait for a character, show a '"'.
8241 */
8242 pc_status = PC_STATUS_UNSET;
8243 if (redrawing() && !char_avail())
8244 {
8245 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008246 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247
8248 edit_putchar('"', TRUE);
8249#ifdef FEAT_CMDL_INFO
8250 add_to_showcmd_c(Ctrl_R);
8251#endif
8252 }
8253
8254#ifdef USE_ON_FLY_SCROLL
8255 dont_scroll = TRUE; /* disallow scrolling here */
8256#endif
8257
8258 /*
8259 * Don't map the register name. This also prevents the mode message to be
8260 * deleted when ESC is hit.
8261 */
8262 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008263 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8266 {
8267 /* Get a third key for literal register insertion */
8268 literally = regname;
8269#ifdef FEAT_CMDL_INFO
8270 add_to_showcmd_c(literally);
8271#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008272 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 }
8275 --no_mapping;
8276
8277#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008278 /* Don't call u_sync() while typing the expression or giving an error
8279 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 ++no_u_sync;
8281 if (regname == '=')
8282 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008283# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008285# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008286 /* Sync undo when evaluating the expression calls setline() or
8287 * append(), so that it can be undone separately. */
8288 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008289
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008291# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 /* Restore the Input Method. */
8293 if (im_on)
8294 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008295# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008297 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8298 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008299 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008301 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 else
8303 {
8304#endif
8305 if (literally == Ctrl_O || literally == Ctrl_P)
8306 {
8307 /* Append the command to the redo buffer. */
8308 AppendCharToRedobuff(Ctrl_R);
8309 AppendCharToRedobuff(literally);
8310 AppendCharToRedobuff(regname);
8311
8312 do_put(regname, BACKWARD, 1L,
8313 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8314 }
8315 else if (insert_reg(regname, literally) == FAIL)
8316 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008317 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 need_redraw = TRUE; /* remove the '"' */
8319 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008320 else if (stop_insert_mode)
8321 /* When the '=' register was used and a function was invoked that
8322 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8323 * insert anything, need to remove the '"' */
8324 need_redraw = TRUE;
8325
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326#ifdef FEAT_EVAL
8327 }
8328 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008329 if (u_sync_once == 1)
8330 ins_need_undo = TRUE;
8331 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332#endif
8333#ifdef FEAT_CMDL_INFO
8334 clear_showcmd();
8335#endif
8336
8337 /* If the inserted register is empty, we need to remove the '"' */
8338 if (need_redraw || stuff_empty())
8339 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008340
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008341 /* Disallow starting Visual mode here, would get a weird mode. */
8342 if (!vis_active && VIsual_active)
8343 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344}
8345
8346/*
8347 * CTRL-G commands in Insert mode.
8348 */
8349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008350ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351{
8352 int c;
8353
8354#ifdef FEAT_INS_EXPAND
8355 /* Right after CTRL-X the cursor will be after the ruler. */
8356 setcursor();
8357#endif
8358
8359 /*
8360 * Don't map the second key. This also prevents the mode message to be
8361 * deleted when ESC is hit.
8362 */
8363 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008364 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 --no_mapping;
8366 switch (c)
8367 {
8368 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8369 case K_UP:
8370 case Ctrl_K:
8371 case 'k': ins_up(TRUE);
8372 break;
8373
8374 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8375 case K_DOWN:
8376 case Ctrl_J:
8377 case 'j': ins_down(TRUE);
8378 break;
8379
8380 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008381 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008383
8384 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008385 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008386 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008387 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 break;
8389
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008390 /* CTRL-G U: do not break undo with the next char */
8391 case 'U':
8392 /* Allow one left/right cursor movement with the next char,
8393 * without breaking undo. */
8394 dont_sync_undo = MAYBE;
8395 break;
8396
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008398 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 }
8400}
8401
8402/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008403 * CTRL-^ in Insert mode.
8404 */
8405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008406ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008407{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008408 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008409 {
8410 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8411 if (State & LANGMAP)
8412 {
8413 curbuf->b_p_iminsert = B_IMODE_NONE;
8414 State &= ~LANGMAP;
8415 }
8416 else
8417 {
8418 curbuf->b_p_iminsert = B_IMODE_LMAP;
8419 State |= LANGMAP;
8420#ifdef USE_IM_CONTROL
8421 im_set_active(FALSE);
8422#endif
8423 }
8424 }
8425#ifdef USE_IM_CONTROL
8426 else
8427 {
8428 /* There are no ":lmap" mappings, toggle IM */
8429 if (im_get_status())
8430 {
8431 curbuf->b_p_iminsert = B_IMODE_NONE;
8432 im_set_active(FALSE);
8433 }
8434 else
8435 {
8436 curbuf->b_p_iminsert = B_IMODE_IM;
8437 State &= ~LANGMAP;
8438 im_set_active(TRUE);
8439 }
8440 }
8441#endif
8442 set_iminsert_global();
8443 showmode();
8444#ifdef FEAT_GUI
8445 /* may show different cursor shape or color */
8446 if (gui.in_use)
8447 gui_update_cursor(TRUE, FALSE);
8448#endif
8449#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8450 /* Show/unshow value of 'keymap' in status lines. */
8451 status_redraw_curbuf();
8452#endif
8453}
8454
8455/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 * Handle ESC in insert mode.
8457 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8458 * insert.
8459 */
8460 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008461ins_esc(
8462 long *count,
8463 int cmdchar,
8464 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465{
8466 int temp;
8467 static int disabled_redraw = FALSE;
8468
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008469#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008470 check_spell_redraw();
8471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472#if defined(FEAT_HANGULIN)
8473# if defined(ESC_CHG_TO_ENG_MODE)
8474 hangul_input_state_set(0);
8475# endif
8476 if (composing_hangul)
8477 {
8478 push_raw_key(composing_hangul_buffer, 2);
8479 composing_hangul = 0;
8480 }
8481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482
8483 temp = curwin->w_cursor.col;
8484 if (disabled_redraw)
8485 {
8486 --RedrawingDisabled;
8487 disabled_redraw = FALSE;
8488 }
8489 if (!arrow_used)
8490 {
8491 /*
8492 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008493 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8494 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008495 */
8496 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008497 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008498
8499 /*
8500 * Repeating insert may take a long time. Check for
8501 * interrupt now and then.
8502 */
8503 if (*count > 0)
8504 {
8505 line_breakcheck();
8506 if (got_int)
8507 *count = 0;
8508 }
8509
8510 if (--*count > 0) /* repeat what was typed */
8511 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008512 /* Vi repeats the insert without replacing characters. */
8513 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8514 State &= ~REPLACE_FLAG;
8515
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 (void)start_redo_ins();
8517 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008518 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 ++RedrawingDisabled;
8520 disabled_redraw = TRUE;
8521 return FALSE; /* repeat the insert */
8522 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008523 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 undisplay_dollar();
8525 }
8526
8527 /* When an autoindent was removed, curswant stays after the
8528 * indent */
8529 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8530 curwin->w_set_curswant = TRUE;
8531
8532 /* Remember the last Insert position in the '^ mark. */
8533 if (!cmdmod.keepjumps)
8534 curbuf->b_last_insert = curwin->w_cursor;
8535
8536 /*
8537 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008538 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008540 if (!nomove
8541 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542#ifdef FEAT_VIRTUALEDIT
8543 || curwin->w_cursor.coladd > 0
8544#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008545 )
8546 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008547 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548#ifdef FEAT_RIGHTLEFT
8549 && !revins_on
8550#endif
8551 )
8552 {
8553#ifdef FEAT_VIRTUALEDIT
8554 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8555 {
8556 oneleft();
8557 if (restart_edit != NUL)
8558 ++curwin->w_cursor.coladd;
8559 }
8560 else
8561#endif
8562 {
8563 --curwin->w_cursor.col;
8564#ifdef FEAT_MBYTE
8565 /* Correct cursor for multi-byte character. */
8566 if (has_mbyte)
8567 mb_adjust_cursor();
8568#endif
8569 }
8570 }
8571
8572#ifdef USE_IM_CONTROL
8573 /* Disable IM to allow typing English directly for Normal mode commands.
8574 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8575 * well). */
8576 if (!(State & LANGMAP))
8577 im_save_status(&curbuf->b_p_iminsert);
8578 im_set_active(FALSE);
8579#endif
8580
8581 State = NORMAL;
8582 /* need to position cursor again (e.g. when on a TAB ) */
8583 changed_cline_bef_curs();
8584
8585#ifdef FEAT_MOUSE
8586 setmouse();
8587#endif
8588#ifdef CURSOR_SHAPE
8589 ui_cursor_shape(); /* may show different cursor shape */
8590#endif
8591
8592 /*
8593 * When recording or for CTRL-O, need to display the new mode.
8594 * Otherwise remove the mode message.
8595 */
8596 if (Recording || restart_edit != NUL)
8597 showmode();
8598 else if (p_smd)
8599 MSG("");
8600
8601 return TRUE; /* exit Insert mode */
8602}
8603
8604#ifdef FEAT_RIGHTLEFT
8605/*
8606 * Toggle language: hkmap and revins_on.
8607 * Move to end of reverse inserted text.
8608 */
8609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008610ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611{
8612 if (revins_on && revins_chars && revins_scol >= 0)
8613 {
8614 while (gchar_cursor() != NUL && revins_chars--)
8615 ++curwin->w_cursor.col;
8616 }
8617 p_ri = !p_ri;
8618 revins_on = (State == INSERT && p_ri);
8619 if (revins_on)
8620 {
8621 revins_scol = curwin->w_cursor.col;
8622 revins_legal++;
8623 revins_chars = 0;
8624 undisplay_dollar();
8625 }
8626 else
8627 revins_scol = -1;
8628#ifdef FEAT_FKMAP
8629 if (p_altkeymap)
8630 {
8631 /*
8632 * to be consistent also for redo command, using '.'
8633 * set arrow_used to true and stop it - causing to redo
8634 * characters entered in one mode (normal/reverse insert).
8635 */
8636 arrow_used = TRUE;
8637 (void)stop_arrow();
8638 p_fkmap = curwin->w_p_rl ^ p_ri;
8639 if (p_fkmap && p_ri)
8640 State = INSERT;
8641 }
8642 else
8643#endif
8644 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8645 showmode();
8646}
8647#endif
8648
Bram Moolenaar071d4272004-06-13 20:20:40 +00008649/*
8650 * If 'keymodel' contains "startsel", may start selection.
8651 * Returns TRUE when a CTRL-O and other keys stuffed.
8652 */
8653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008654ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008655{
8656 if (km_startsel)
8657 switch (c)
8658 {
8659 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008660 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008661 case K_PAGEUP:
8662 case K_KPAGEUP:
8663 case K_PAGEDOWN:
8664 case K_KPAGEDOWN:
8665# ifdef MACOS
8666 case K_LEFT:
8667 case K_RIGHT:
8668 case K_UP:
8669 case K_DOWN:
8670 case K_END:
8671 case K_HOME:
8672# endif
8673 if (!(mod_mask & MOD_MASK_SHIFT))
8674 break;
8675 /* FALLTHROUGH */
8676 case K_S_LEFT:
8677 case K_S_RIGHT:
8678 case K_S_UP:
8679 case K_S_DOWN:
8680 case K_S_END:
8681 case K_S_HOME:
8682 /* Start selection right away, the cursor can move with
8683 * CTRL-O when beyond the end of the line. */
8684 start_selection();
8685
8686 /* Execute the key in (insert) Select mode. */
8687 stuffcharReadbuff(Ctrl_O);
8688 if (mod_mask)
8689 {
8690 char_u buf[4];
8691
8692 buf[0] = K_SPECIAL;
8693 buf[1] = KS_MODIFIER;
8694 buf[2] = mod_mask;
8695 buf[3] = NUL;
8696 stuffReadbuff(buf);
8697 }
8698 stuffcharReadbuff(c);
8699 return TRUE;
8700 }
8701 return FALSE;
8702}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703
8704/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008705 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008706 */
8707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008708ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008709{
8710#ifdef FEAT_FKMAP
8711 if (p_fkmap && p_ri)
8712 {
8713 beep_flush();
8714 EMSG(farsi_text_3); /* encoded in Farsi */
8715 return;
8716 }
8717#endif
8718
8719#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008720# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008721 set_vim_var_string(VV_INSERTMODE,
8722 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008723# ifdef FEAT_VREPLACE
8724 replaceState == VREPLACE ? "v" :
8725# endif
8726 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008727# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008728 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8729#endif
8730 if (State & REPLACE_FLAG)
8731 State = INSERT | (State & LANGMAP);
8732 else
8733 State = replaceState | (State & LANGMAP);
8734 AppendCharToRedobuff(K_INS);
8735 showmode();
8736#ifdef CURSOR_SHAPE
8737 ui_cursor_shape(); /* may show different cursor shape */
8738#endif
8739}
8740
8741/*
8742 * Pressed CTRL-O in Insert mode.
8743 */
8744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008745ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008746{
8747#ifdef FEAT_VREPLACE
8748 if (State & VREPLACE_FLAG)
8749 restart_edit = 'V';
8750 else
8751#endif
8752 if (State & REPLACE_FLAG)
8753 restart_edit = 'R';
8754 else
8755 restart_edit = 'I';
8756#ifdef FEAT_VIRTUALEDIT
8757 if (virtual_active())
8758 ins_at_eol = FALSE; /* cursor always keeps its column */
8759 else
8760#endif
8761 ins_at_eol = (gchar_cursor() == NUL);
8762}
8763
8764/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 * If the cursor is on an indent, ^T/^D insert/delete one
8766 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008767 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 * with vi. But vi only supports ^T and ^D after an
8769 * autoindent, we support it everywhere.
8770 */
8771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008772ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008773{
8774 if (stop_arrow() == FAIL)
8775 return;
8776 AppendCharToRedobuff(c);
8777
8778 /*
8779 * 0^D and ^^D: remove all indent.
8780 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008781 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8782 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783 {
8784 --curwin->w_cursor.col;
8785 (void)del_char(FALSE); /* delete the '^' or '0' */
8786 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8787 if (State & REPLACE_FLAG)
8788 replace_pop_ins();
8789 if (lastc == '^')
8790 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008791 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008792 }
8793 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008794 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795
8796 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8797 did_ai = FALSE;
8798#ifdef FEAT_SMARTINDENT
8799 did_si = FALSE;
8800 can_si = FALSE;
8801 can_si_back = FALSE;
8802#endif
8803#ifdef FEAT_CINDENT
8804 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8805#endif
8806}
8807
8808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008809ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810{
8811 int temp;
8812
8813 if (stop_arrow() == FAIL)
8814 return;
8815 if (gchar_cursor() == NUL) /* delete newline */
8816 {
8817 temp = curwin->w_cursor.col;
8818 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008819 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008820 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 else
8822 curwin->w_cursor.col = temp;
8823 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008824 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8825 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008826 did_ai = FALSE;
8827#ifdef FEAT_SMARTINDENT
8828 did_si = FALSE;
8829 can_si = FALSE;
8830 can_si_back = FALSE;
8831#endif
8832 AppendCharToRedobuff(K_DEL);
8833}
8834
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008835static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008836
8837/*
8838 * Delete one character for ins_bs().
8839 */
8840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008841ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008842{
8843 dec_cursor();
8844 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8845 if (State & REPLACE_FLAG)
8846 {
8847 /* Don't delete characters before the insert point when in
8848 * Replace mode */
8849 if (curwin->w_cursor.lnum != Insstart.lnum
8850 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008851 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008852 }
8853 else
8854 (void)del_char(FALSE);
8855}
8856
Bram Moolenaar071d4272004-06-13 20:20:40 +00008857/*
8858 * Handle Backspace, delete-word and delete-line in Insert mode.
8859 * Return TRUE when backspace was actually used.
8860 */
8861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008862ins_bs(
8863 int c,
8864 int mode,
8865 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866{
8867 linenr_T lnum;
8868 int cc;
8869 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008870 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008871 colnr_T mincol;
8872 int did_backspace = FALSE;
8873 int in_indent;
8874 int oldState;
8875#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008876 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877#endif
8878
8879 /*
8880 * can't delete anything in an empty file
8881 * can't backup past first character in buffer
8882 * can't backup past starting point unless 'backspace' > 1
8883 * can backup to a previous line if 'backspace' == 0
8884 */
8885 if ( bufempty()
8886 || (
8887#ifdef FEAT_RIGHTLEFT
8888 !revins_on &&
8889#endif
8890 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8891 || (!can_bs(BS_START)
8892 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008893 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8894 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8896 && curwin->w_cursor.col <= ai_col)
8897 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8898 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008899 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 return FALSE;
8901 }
8902
8903 if (stop_arrow() == FAIL)
8904 return FALSE;
8905 in_indent = inindent(0);
8906#ifdef FEAT_CINDENT
8907 if (in_indent)
8908 can_cindent = FALSE;
8909#endif
8910#ifdef FEAT_COMMENTS
8911 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8912#endif
8913#ifdef FEAT_RIGHTLEFT
8914 if (revins_on) /* put cursor after last inserted char */
8915 inc_cursor();
8916#endif
8917
8918#ifdef FEAT_VIRTUALEDIT
8919 /* Virtualedit:
8920 * BACKSPACE_CHAR eats a virtual space
8921 * BACKSPACE_WORD eats all coladd
8922 * BACKSPACE_LINE eats all coladd and keeps going
8923 */
8924 if (curwin->w_cursor.coladd > 0)
8925 {
8926 if (mode == BACKSPACE_CHAR)
8927 {
8928 --curwin->w_cursor.coladd;
8929 return TRUE;
8930 }
8931 if (mode == BACKSPACE_WORD)
8932 {
8933 curwin->w_cursor.coladd = 0;
8934 return TRUE;
8935 }
8936 curwin->w_cursor.coladd = 0;
8937 }
8938#endif
8939
8940 /*
8941 * delete newline!
8942 */
8943 if (curwin->w_cursor.col == 0)
8944 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008945 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008946 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947#ifdef FEAT_RIGHTLEFT
8948 || revins_on
8949#endif
8950 )
8951 {
8952 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8953 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8954 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008955 --Insstart.lnum;
8956 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
8958 /*
8959 * In replace mode:
8960 * cc < 0: NL was inserted, delete it
8961 * cc >= 0: NL was replaced, put original characters back
8962 */
8963 cc = -1;
8964 if (State & REPLACE_FLAG)
8965 cc = replace_pop(); /* returns -1 if NL was inserted */
8966 /*
8967 * In replace mode, in the line we started replacing, we only move the
8968 * cursor.
8969 */
8970 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8971 {
8972 dec_cursor();
8973 }
8974 else
8975 {
8976#ifdef FEAT_VREPLACE
8977 if (!(State & VREPLACE_FLAG)
8978 || curwin->w_cursor.lnum > orig_line_count)
8979#endif
8980 {
8981 temp = gchar_cursor(); /* remember current char */
8982 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008983
8984 /* When "aw" is in 'formatoptions' we must delete the space at
8985 * the end of the line, otherwise the line will be broken
8986 * again when auto-formatting. */
8987 if (has_format_option(FO_AUTO)
8988 && has_format_option(FO_WHITE_PAR))
8989 {
8990 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8991 TRUE);
8992 int len;
8993
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008994 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008995 if (len > 0 && ptr[len - 1] == ' ')
8996 ptr[len - 1] = NUL;
8997 }
8998
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008999 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009000 if (temp == NUL && gchar_cursor() != NUL)
9001 inc_cursor();
9002 }
9003#ifdef FEAT_VREPLACE
9004 else
9005 dec_cursor();
9006#endif
9007
9008 /*
9009 * In REPLACE mode we have to put back the text that was replaced
9010 * by the NL. On the replace stack is first a NUL-terminated
9011 * sequence of characters that were deleted and then the
9012 * characters that NL replaced.
9013 */
9014 if (State & REPLACE_FLAG)
9015 {
9016 /*
9017 * Do the next ins_char() in NORMAL state, to
9018 * prevent ins_char() from replacing characters and
9019 * avoiding showmatch().
9020 */
9021 oldState = State;
9022 State = NORMAL;
9023 /*
9024 * restore characters (blanks) deleted after cursor
9025 */
9026 while (cc > 0)
9027 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009028 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029#ifdef FEAT_MBYTE
9030 mb_replace_pop_ins(cc);
9031#else
9032 ins_char(cc);
9033#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009034 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 cc = replace_pop();
9036 }
9037 /* restore the characters that NL replaced */
9038 replace_pop_ins();
9039 State = oldState;
9040 }
9041 }
9042 did_ai = FALSE;
9043 }
9044 else
9045 {
9046 /*
9047 * Delete character(s) before the cursor.
9048 */
9049#ifdef FEAT_RIGHTLEFT
9050 if (revins_on) /* put cursor on last inserted char */
9051 dec_cursor();
9052#endif
9053 mincol = 0;
9054 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009055 if (mode == BACKSPACE_LINE
9056 && (curbuf->b_p_ai
9057#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009058 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009059#endif
9060 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061#ifdef FEAT_RIGHTLEFT
9062 && !revins_on
9063#endif
9064 )
9065 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009066 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009068 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009070 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009071 }
9072
9073 /*
9074 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9075 */
9076 if ( mode == BACKSPACE_CHAR
9077 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009078 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009079 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 && (*(ml_get_cursor() - 1) == TAB
9081 || (*(ml_get_cursor() - 1) == ' '
9082 && (!*inserted_space_p
9083 || arrow_used))))))
9084 {
9085 int ts;
9086 colnr_T vcol;
9087 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009088 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
9090 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009091 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009092 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009094 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009095 /* Compute the virtual column where we want to be. Since
9096 * 'showbreak' may get in the way, need to get the last column of
9097 * the previous character. */
9098 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009099 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009100 dec_cursor();
9101 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9102 inc_cursor();
9103 want_vcol = (want_vcol / ts) * ts;
9104
9105 /* delete characters until we are at or before want_vcol */
9106 while (vcol > want_vcol
9107 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009108 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109
9110 /* insert extra spaces until we are at want_vcol */
9111 while (vcol < want_vcol)
9112 {
9113 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009114 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9115 && curwin->w_cursor.col < Insstart_orig.col)
9116 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117
9118#ifdef FEAT_VREPLACE
9119 if (State & VREPLACE_FLAG)
9120 ins_char(' ');
9121 else
9122#endif
9123 {
9124 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009125 if ((State & REPLACE_FLAG))
9126 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009127 }
9128 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9129 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009130
9131 /* If we are now back where we started delete one character. Can
9132 * happen when using 'sts' and 'linebreak'. */
9133 if (vcol >= start_vcol)
9134 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 }
9136
9137 /*
9138 * Delete upto starting point, start of line or previous word.
9139 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009140 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009142#ifdef FEAT_MBYTE
9143 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009145 if (has_mbyte)
9146 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009148 do
9149 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009151 if (!revins_on) /* put cursor on char to be deleted */
9152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009154
9155 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009157 /* look multi-byte character class */
9158 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009160 prev_cclass = cclass;
9161 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009164
9165 /* start of word? */
9166 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9167 {
9168 mode = BACKSPACE_WORD_NOT_SPACE;
9169 temp = vim_iswordc(cc);
9170 }
9171 /* end of word? */
9172 else if (mode == BACKSPACE_WORD_NOT_SPACE
9173 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9174#ifdef FEAT_MBYTE
9175 || prev_cclass != cclass
9176#endif
9177 ))
9178 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009180 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009182 inc_cursor();
9183#ifdef FEAT_RIGHTLEFT
9184 else if (State & REPLACE_FLAG)
9185 dec_cursor();
9186#endif
9187 break;
9188 }
9189 if (State & REPLACE_FLAG)
9190 replace_do_bs(-1);
9191 else
9192 {
9193#ifdef FEAT_MBYTE
9194 if (enc_utf8 && p_deco)
9195 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9196#endif
9197 (void)del_char(FALSE);
9198#ifdef FEAT_MBYTE
9199 /*
9200 * If there are combining characters and 'delcombine' is set
9201 * move the cursor back. Don't back up before the base
9202 * character.
9203 */
9204 if (enc_utf8 && p_deco && cpc[0] != NUL)
9205 inc_cursor();
9206#endif
9207#ifdef FEAT_RIGHTLEFT
9208 if (revins_chars)
9209 {
9210 revins_chars--;
9211 revins_legal++;
9212 }
9213 if (revins_on && gchar_cursor() == NUL)
9214 break;
9215#endif
9216 }
9217 /* Just a single backspace?: */
9218 if (mode == BACKSPACE_CHAR)
9219 break;
9220 } while (
9221#ifdef FEAT_RIGHTLEFT
9222 revins_on ||
9223#endif
9224 (curwin->w_cursor.col > mincol
9225 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9226 || curwin->w_cursor.col != Insstart_orig.col)));
9227 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009228 did_backspace = TRUE;
9229 }
9230#ifdef FEAT_SMARTINDENT
9231 did_si = FALSE;
9232 can_si = FALSE;
9233 can_si_back = FALSE;
9234#endif
9235 if (curwin->w_cursor.col <= 1)
9236 did_ai = FALSE;
9237 /*
9238 * It's a little strange to put backspaces into the redo
9239 * buffer, but it makes auto-indent a lot easier to deal
9240 * with.
9241 */
9242 AppendCharToRedobuff(c);
9243
9244 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009245 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9246 && curwin->w_cursor.col < Insstart_orig.col)
9247 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248
9249 /* vi behaviour: the cursor moves backward but the character that
9250 * was there remains visible
9251 * Vim behaviour: the cursor moves backward and the character that
9252 * was there is erased from the screen.
9253 * We can emulate the vi behaviour by pretending there is a dollar
9254 * displayed even when there isn't.
9255 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009256 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 dollar_vcol = curwin->w_virtcol;
9258
Bram Moolenaarce3be472008-01-14 19:12:28 +00009259#ifdef FEAT_FOLDING
9260 /* When deleting a char the cursor line must never be in a closed fold.
9261 * E.g., when 'foldmethod' is indent and deleting the first non-white
9262 * char before a Tab. */
9263 if (did_backspace)
9264 foldOpenCursor();
9265#endif
9266
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 return did_backspace;
9268}
9269
9270#ifdef FEAT_MOUSE
9271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009272ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273{
9274 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009275 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276
9277# ifdef FEAT_GUI
9278 /* When GUI is active, also move/paste when 'mouse' is empty */
9279 if (!gui.in_use)
9280# endif
9281 if (!mouse_has(MOUSE_INSERT))
9282 return;
9283
9284 undisplay_dollar();
9285 tpos = curwin->w_cursor;
9286 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9287 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009288#ifdef FEAT_WINDOWS
9289 win_T *new_curwin = curwin;
9290
9291 if (curwin != old_curwin && win_valid(old_curwin))
9292 {
9293 /* Mouse took us to another window. We need to go back to the
9294 * previous one to stop insert there properly. */
9295 curwin = old_curwin;
9296 curbuf = curwin->w_buffer;
9297 }
9298#endif
9299 start_arrow(curwin == old_curwin ? &tpos : NULL);
9300#ifdef FEAT_WINDOWS
9301 if (curwin != new_curwin && win_valid(new_curwin))
9302 {
9303 curwin = new_curwin;
9304 curbuf = curwin->w_buffer;
9305 }
9306#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009307# ifdef FEAT_CINDENT
9308 can_cindent = TRUE;
9309# endif
9310 }
9311
9312#ifdef FEAT_WINDOWS
9313 /* redraw status lines (in case another window became active) */
9314 redraw_statuslines();
9315#endif
9316}
9317
9318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009319ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320{
9321 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009322# if defined(FEAT_WINDOWS)
9323 win_T *old_curwin = curwin;
9324# endif
9325# ifdef FEAT_INS_EXPAND
9326 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327# endif
9328
9329 tpos = curwin->w_cursor;
9330
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009331# ifdef FEAT_WINDOWS
9332 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009333 {
9334 int row, col;
9335
9336 row = mouse_row;
9337 col = mouse_col;
9338
9339 /* find the window at the pointer coordinates */
9340 curwin = mouse_find_win(&row, &col);
9341 curbuf = curwin->w_buffer;
9342 }
9343 if (curwin == old_curwin)
9344# endif
9345 undisplay_dollar();
9346
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009347# ifdef FEAT_INS_EXPAND
9348 /* Don't scroll the window in which completion is being done. */
9349 if (!pum_visible()
9350# if defined(FEAT_WINDOWS)
9351 || curwin != old_curwin
9352# endif
9353 )
9354# endif
9355 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009356 if (dir == MSCR_DOWN || dir == MSCR_UP)
9357 {
9358 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9359 scroll_redraw(dir,
9360 (long)(curwin->w_botline - curwin->w_topline));
9361 else
9362 scroll_redraw(dir, 3L);
9363 }
9364#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009365 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009366 {
9367 int val, step = 6;
9368
9369 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9370 step = W_WIDTH(curwin);
9371 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9372 if (val < 0)
9373 val = 0;
9374 gui_do_horiz_scroll(val, TRUE);
9375 }
9376#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009377# ifdef FEAT_INS_EXPAND
9378 did_scroll = TRUE;
9379# endif
9380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009381
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009382# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009383 curwin->w_redr_status = TRUE;
9384
9385 curwin = old_curwin;
9386 curbuf = curwin->w_buffer;
9387# endif
9388
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009389# ifdef FEAT_INS_EXPAND
9390 /* The popup menu may overlay the window, need to redraw it.
9391 * TODO: Would be more efficient to only redraw the windows that are
9392 * overlapped by the popup menu. */
9393 if (pum_visible() && did_scroll)
9394 {
9395 redraw_all_later(NOT_VALID);
9396 ins_compl_show_pum();
9397 }
9398# endif
9399
Bram Moolenaar071d4272004-06-13 20:20:40 +00009400 if (!equalpos(curwin->w_cursor, tpos))
9401 {
9402 start_arrow(&tpos);
9403# ifdef FEAT_CINDENT
9404 can_cindent = TRUE;
9405# endif
9406 }
9407}
9408#endif
9409
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009410#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009412ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009413{
9414 /* We will be leaving the current window, unless closing another tab. */
9415 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9416 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9417 {
9418 undisplay_dollar();
9419 start_arrow(&curwin->w_cursor);
9420# ifdef FEAT_CINDENT
9421 can_cindent = TRUE;
9422# endif
9423 }
9424
9425 if (c == K_TABLINE)
9426 goto_tabpage(current_tab);
9427 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009428 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009429 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009430 redraw_statuslines(); /* will redraw the tabline when needed */
9431 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009432}
9433#endif
9434
9435#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009437ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009438{
9439 pos_T tpos;
9440
9441 undisplay_dollar();
9442 tpos = curwin->w_cursor;
9443 if (gui_do_scroll())
9444 {
9445 start_arrow(&tpos);
9446# ifdef FEAT_CINDENT
9447 can_cindent = TRUE;
9448# endif
9449 }
9450}
9451
9452 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009453ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454{
9455 pos_T tpos;
9456
9457 undisplay_dollar();
9458 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009459 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009460 {
9461 start_arrow(&tpos);
9462# ifdef FEAT_CINDENT
9463 can_cindent = TRUE;
9464# endif
9465 }
9466}
9467#endif
9468
9469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009470ins_left(
9471 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009472{
9473 pos_T tpos;
9474
9475#ifdef FEAT_FOLDING
9476 if ((fdo_flags & FDO_HOR) && KeyTyped)
9477 foldOpenCursor();
9478#endif
9479 undisplay_dollar();
9480 tpos = curwin->w_cursor;
9481 if (oneleft() == OK)
9482 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009483#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9484 /* Only call start_arrow() when not busy with preediting, it will
9485 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9486 if (!im_is_preediting())
9487#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009488 {
9489 start_arrow_with_change(&tpos, end_change);
9490 if (!end_change)
9491 AppendCharToRedobuff(K_LEFT);
9492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493#ifdef FEAT_RIGHTLEFT
9494 /* If exit reversed string, position is fixed */
9495 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9496 revins_legal++;
9497 revins_chars++;
9498#endif
9499 }
9500
9501 /*
9502 * if 'whichwrap' set for cursor in insert mode may go to
9503 * previous line
9504 */
9505 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9506 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009507 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508 start_arrow(&tpos);
9509 --(curwin->w_cursor.lnum);
9510 coladvance((colnr_T)MAXCOL);
9511 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9512 }
9513 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009514 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009515 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009516}
9517
9518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009519ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520{
9521 pos_T tpos;
9522
9523#ifdef FEAT_FOLDING
9524 if ((fdo_flags & FDO_HOR) && KeyTyped)
9525 foldOpenCursor();
9526#endif
9527 undisplay_dollar();
9528 tpos = curwin->w_cursor;
9529 if (c == K_C_HOME)
9530 curwin->w_cursor.lnum = 1;
9531 curwin->w_cursor.col = 0;
9532#ifdef FEAT_VIRTUALEDIT
9533 curwin->w_cursor.coladd = 0;
9534#endif
9535 curwin->w_curswant = 0;
9536 start_arrow(&tpos);
9537}
9538
9539 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009540ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009541{
9542 pos_T tpos;
9543
9544#ifdef FEAT_FOLDING
9545 if ((fdo_flags & FDO_HOR) && KeyTyped)
9546 foldOpenCursor();
9547#endif
9548 undisplay_dollar();
9549 tpos = curwin->w_cursor;
9550 if (c == K_C_END)
9551 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9552 coladvance((colnr_T)MAXCOL);
9553 curwin->w_curswant = MAXCOL;
9554
9555 start_arrow(&tpos);
9556}
9557
9558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009559ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560{
9561#ifdef FEAT_FOLDING
9562 if ((fdo_flags & FDO_HOR) && KeyTyped)
9563 foldOpenCursor();
9564#endif
9565 undisplay_dollar();
9566 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9567 {
9568 start_arrow(&curwin->w_cursor);
9569 (void)bck_word(1L, FALSE, FALSE);
9570 curwin->w_set_curswant = TRUE;
9571 }
9572 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009573 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574}
9575
9576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009577ins_right(
9578 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579{
9580#ifdef FEAT_FOLDING
9581 if ((fdo_flags & FDO_HOR) && KeyTyped)
9582 foldOpenCursor();
9583#endif
9584 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009585 if (gchar_cursor() != NUL
9586#ifdef FEAT_VIRTUALEDIT
9587 || virtual_active()
9588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 )
9590 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009591 start_arrow_with_change(&curwin->w_cursor, end_change);
9592 if (!end_change)
9593 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594 curwin->w_set_curswant = TRUE;
9595#ifdef FEAT_VIRTUALEDIT
9596 if (virtual_active())
9597 oneright();
9598 else
9599#endif
9600 {
9601#ifdef FEAT_MBYTE
9602 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009603 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604 else
9605#endif
9606 ++curwin->w_cursor.col;
9607 }
9608
9609#ifdef FEAT_RIGHTLEFT
9610 revins_legal++;
9611 if (revins_chars)
9612 revins_chars--;
9613#endif
9614 }
9615 /* if 'whichwrap' set for cursor in insert mode, may move the
9616 * cursor to the next line */
9617 else if (vim_strchr(p_ww, ']') != NULL
9618 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9619 {
9620 start_arrow(&curwin->w_cursor);
9621 curwin->w_set_curswant = TRUE;
9622 ++curwin->w_cursor.lnum;
9623 curwin->w_cursor.col = 0;
9624 }
9625 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009626 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009627 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628}
9629
9630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009631ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
9633#ifdef FEAT_FOLDING
9634 if ((fdo_flags & FDO_HOR) && KeyTyped)
9635 foldOpenCursor();
9636#endif
9637 undisplay_dollar();
9638 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9639 || gchar_cursor() != NUL)
9640 {
9641 start_arrow(&curwin->w_cursor);
9642 (void)fwd_word(1L, FALSE, 0);
9643 curwin->w_set_curswant = TRUE;
9644 }
9645 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009646 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647}
9648
9649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009650ins_up(
9651 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009652{
9653 pos_T tpos;
9654 linenr_T old_topline = curwin->w_topline;
9655#ifdef FEAT_DIFF
9656 int old_topfill = curwin->w_topfill;
9657#endif
9658
9659 undisplay_dollar();
9660 tpos = curwin->w_cursor;
9661 if (cursor_up(1L, TRUE) == OK)
9662 {
9663 if (startcol)
9664 coladvance(getvcol_nolist(&Insstart));
9665 if (old_topline != curwin->w_topline
9666#ifdef FEAT_DIFF
9667 || old_topfill != curwin->w_topfill
9668#endif
9669 )
9670 redraw_later(VALID);
9671 start_arrow(&tpos);
9672#ifdef FEAT_CINDENT
9673 can_cindent = TRUE;
9674#endif
9675 }
9676 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009677 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678}
9679
9680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009681ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682{
9683 pos_T tpos;
9684
9685 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009686
9687#ifdef FEAT_WINDOWS
9688 if (mod_mask & MOD_MASK_CTRL)
9689 {
9690 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009691 if (first_tabpage->tp_next != NULL)
9692 {
9693 start_arrow(&curwin->w_cursor);
9694 goto_tabpage(-1);
9695 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009696 return;
9697 }
9698#endif
9699
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700 tpos = curwin->w_cursor;
9701 if (onepage(BACKWARD, 1L) == OK)
9702 {
9703 start_arrow(&tpos);
9704#ifdef FEAT_CINDENT
9705 can_cindent = TRUE;
9706#endif
9707 }
9708 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009709 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710}
9711
9712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009713ins_down(
9714 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715{
9716 pos_T tpos;
9717 linenr_T old_topline = curwin->w_topline;
9718#ifdef FEAT_DIFF
9719 int old_topfill = curwin->w_topfill;
9720#endif
9721
9722 undisplay_dollar();
9723 tpos = curwin->w_cursor;
9724 if (cursor_down(1L, TRUE) == OK)
9725 {
9726 if (startcol)
9727 coladvance(getvcol_nolist(&Insstart));
9728 if (old_topline != curwin->w_topline
9729#ifdef FEAT_DIFF
9730 || old_topfill != curwin->w_topfill
9731#endif
9732 )
9733 redraw_later(VALID);
9734 start_arrow(&tpos);
9735#ifdef FEAT_CINDENT
9736 can_cindent = TRUE;
9737#endif
9738 }
9739 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009740 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741}
9742
9743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009744ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745{
9746 pos_T tpos;
9747
9748 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009749
9750#ifdef FEAT_WINDOWS
9751 if (mod_mask & MOD_MASK_CTRL)
9752 {
9753 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009754 if (first_tabpage->tp_next != NULL)
9755 {
9756 start_arrow(&curwin->w_cursor);
9757 goto_tabpage(0);
9758 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009759 return;
9760 }
9761#endif
9762
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 tpos = curwin->w_cursor;
9764 if (onepage(FORWARD, 1L) == OK)
9765 {
9766 start_arrow(&tpos);
9767#ifdef FEAT_CINDENT
9768 can_cindent = TRUE;
9769#endif
9770 }
9771 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009772 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773}
9774
9775#ifdef FEAT_DND
9776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009777ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9780}
9781#endif
9782
9783/*
9784 * Handle TAB in Insert or Replace mode.
9785 * Return TRUE when the TAB needs to be inserted like a normal character.
9786 */
9787 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009788ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009789{
9790 int ind;
9791 int i;
9792 int temp;
9793
9794 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9795 Insstart_blank_vcol = get_nolist_virtcol();
9796 if (echeck_abbr(TAB + ABBR_OFF))
9797 return FALSE;
9798
9799 ind = inindent(0);
9800#ifdef FEAT_CINDENT
9801 if (ind)
9802 can_cindent = FALSE;
9803#endif
9804
9805 /*
9806 * When nothing special, insert TAB like a normal character
9807 */
9808 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009809 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009810 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 return TRUE;
9812
9813 if (stop_arrow() == FAIL)
9814 return TRUE;
9815
9816 did_ai = FALSE;
9817#ifdef FEAT_SMARTINDENT
9818 did_si = FALSE;
9819 can_si = FALSE;
9820 can_si_back = FALSE;
9821#endif
9822 AppendToRedobuff((char_u *)"\t");
9823
9824 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009825 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009826 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9827 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 else /* otherwise use 'tabstop' */
9829 temp = (int)curbuf->b_p_ts;
9830 temp -= get_nolist_virtcol() % temp;
9831
9832 /*
9833 * Insert the first space with ins_char(). It will delete one char in
9834 * replace mode. Insert the rest with ins_str(); it will not delete any
9835 * chars. For VREPLACE mode, we use ins_char() for all characters.
9836 */
9837 ins_char(' ');
9838 while (--temp > 0)
9839 {
9840#ifdef FEAT_VREPLACE
9841 if (State & VREPLACE_FLAG)
9842 ins_char(' ');
9843 else
9844#endif
9845 {
9846 ins_str((char_u *)" ");
9847 if (State & REPLACE_FLAG) /* no char replaced */
9848 replace_push(NUL);
9849 }
9850 }
9851
9852 /*
9853 * When 'expandtab' not set: Replace spaces by TABs where possible.
9854 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009855 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 {
9857 char_u *ptr;
9858#ifdef FEAT_VREPLACE
9859 char_u *saved_line = NULL; /* init for GCC */
9860 pos_T pos;
9861#endif
9862 pos_T fpos;
9863 pos_T *cursor;
9864 colnr_T want_vcol, vcol;
9865 int change_col = -1;
9866 int save_list = curwin->w_p_list;
9867
9868 /*
9869 * Get the current line. For VREPLACE mode, don't make real changes
9870 * yet, just work on a copy of the line.
9871 */
9872#ifdef FEAT_VREPLACE
9873 if (State & VREPLACE_FLAG)
9874 {
9875 pos = curwin->w_cursor;
9876 cursor = &pos;
9877 saved_line = vim_strsave(ml_get_curline());
9878 if (saved_line == NULL)
9879 return FALSE;
9880 ptr = saved_line + pos.col;
9881 }
9882 else
9883#endif
9884 {
9885 ptr = ml_get_cursor();
9886 cursor = &curwin->w_cursor;
9887 }
9888
9889 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9890 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9891 curwin->w_p_list = FALSE;
9892
9893 /* Find first white before the cursor */
9894 fpos = curwin->w_cursor;
9895 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9896 {
9897 --fpos.col;
9898 --ptr;
9899 }
9900
9901 /* In Replace mode, don't change characters before the insert point. */
9902 if ((State & REPLACE_FLAG)
9903 && fpos.lnum == Insstart.lnum
9904 && fpos.col < Insstart.col)
9905 {
9906 ptr += Insstart.col - fpos.col;
9907 fpos.col = Insstart.col;
9908 }
9909
9910 /* compute virtual column numbers of first white and cursor */
9911 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9912 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9913
Bram Moolenaar597a4222014-06-25 14:39:50 +02009914 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9915 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 while (vim_iswhite(*ptr))
9917 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009918 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919 if (vcol + i > want_vcol)
9920 break;
9921 if (*ptr != TAB)
9922 {
9923 *ptr = TAB;
9924 if (change_col < 0)
9925 {
9926 change_col = fpos.col; /* Column of first change */
9927 /* May have to adjust Insstart */
9928 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9929 Insstart.col = fpos.col;
9930 }
9931 }
9932 ++fpos.col;
9933 ++ptr;
9934 vcol += i;
9935 }
9936
9937 if (change_col >= 0)
9938 {
9939 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009940 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
9942 /* Skip over the spaces we need. */
9943 while (vcol < want_vcol && *ptr == ' ')
9944 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009945 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009946 ++ptr;
9947 ++repl_off;
9948 }
9949 if (vcol > want_vcol)
9950 {
9951 /* Must have a char with 'showbreak' just before it. */
9952 --ptr;
9953 --repl_off;
9954 }
9955 fpos.col += repl_off;
9956
9957 /* Delete following spaces. */
9958 i = cursor->col - fpos.col;
9959 if (i > 0)
9960 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009961 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 /* correct replace stack. */
9963 if ((State & REPLACE_FLAG)
9964#ifdef FEAT_VREPLACE
9965 && !(State & VREPLACE_FLAG)
9966#endif
9967 )
9968 for (temp = i; --temp >= 0; )
9969 replace_join(repl_off);
9970 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009971#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009972 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009973 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009974 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009975 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9976 (char_u *)"\t", 1);
9977 }
9978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 cursor->col -= i;
9980
9981#ifdef FEAT_VREPLACE
9982 /*
9983 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9984 * backspacing over the changed spacing and then inserting the new
9985 * spacing.
9986 */
9987 if (State & VREPLACE_FLAG)
9988 {
9989 /* Backspace from real cursor to change_col */
9990 backspace_until_column(change_col);
9991
9992 /* Insert each char in saved_line from changed_col to
9993 * ptr-cursor */
9994 ins_bytes_len(saved_line + change_col,
9995 cursor->col - change_col);
9996 }
9997#endif
9998 }
9999
10000#ifdef FEAT_VREPLACE
10001 if (State & VREPLACE_FLAG)
10002 vim_free(saved_line);
10003#endif
10004 curwin->w_p_list = save_list;
10005 }
10006
10007 return FALSE;
10008}
10009
10010/*
10011 * Handle CR or NL in insert mode.
10012 * Return TRUE when out of memory or can't undo.
10013 */
10014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010015ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016{
10017 int i;
10018
10019 if (echeck_abbr(c + ABBR_OFF))
10020 return FALSE;
10021 if (stop_arrow() == FAIL)
10022 return TRUE;
10023 undisplay_dollar();
10024
10025 /*
10026 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10027 * character under the cursor. Only push a NUL on the replace stack,
10028 * nothing to put back when the NL is deleted.
10029 */
10030 if ((State & REPLACE_FLAG)
10031#ifdef FEAT_VREPLACE
10032 && !(State & VREPLACE_FLAG)
10033#endif
10034 )
10035 replace_push(NUL);
10036
10037 /*
10038 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10039 * replacing the next line, so we push all of the characters left on the
10040 * line onto the replace stack. This is not done here though, it is done
10041 * in open_line().
10042 */
10043
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010044#ifdef FEAT_VIRTUALEDIT
10045 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10046 * CTRL-O). */
10047 if (virtual_active() && curwin->w_cursor.coladd > 0)
10048 coladvance(getviscol());
10049#endif
10050
Bram Moolenaar071d4272004-06-13 20:20:40 +000010051#ifdef FEAT_RIGHTLEFT
10052# ifdef FEAT_FKMAP
10053 if (p_altkeymap && p_fkmap)
10054 fkmap(NL);
10055# endif
10056 /* NL in reverse insert will always start in the end of
10057 * current line. */
10058 if (revins_on)
10059 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10060#endif
10061
10062 AppendToRedobuff(NL_STR);
10063 i = open_line(FORWARD,
10064#ifdef FEAT_COMMENTS
10065 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10066#endif
10067 0, old_indent);
10068 old_indent = 0;
10069#ifdef FEAT_CINDENT
10070 can_cindent = TRUE;
10071#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010072#ifdef FEAT_FOLDING
10073 /* When inserting a line the cursor line must never be in a closed fold. */
10074 foldOpenCursor();
10075#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010076
10077 return (!i);
10078}
10079
10080#ifdef FEAT_DIGRAPHS
10081/*
10082 * Handle digraph in insert mode.
10083 * Returns character still to be inserted, or NUL when nothing remaining to be
10084 * done.
10085 */
10086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010087ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088{
10089 int c;
10090 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010091 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010092
10093 pc_status = PC_STATUS_UNSET;
10094 if (redrawing() && !char_avail())
10095 {
10096 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010097 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098
10099 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010100 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010101#ifdef FEAT_CMDL_INFO
10102 add_to_showcmd_c(Ctrl_K);
10103#endif
10104 }
10105
10106#ifdef USE_ON_FLY_SCROLL
10107 dont_scroll = TRUE; /* disallow scrolling here */
10108#endif
10109
10110 /* don't map the digraph chars. This also prevents the
10111 * mode message to be deleted when ESC is hit */
10112 ++no_mapping;
10113 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010114 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 --no_mapping;
10116 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010117 if (did_putchar)
10118 /* when the line fits in 'columns' the '?' is at the start of the next
10119 * line and will not be removed by the redraw */
10120 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010121
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 if (IS_SPECIAL(c) || mod_mask) /* special key */
10123 {
10124#ifdef FEAT_CMDL_INFO
10125 clear_showcmd();
10126#endif
10127 insert_special(c, TRUE, FALSE);
10128 return NUL;
10129 }
10130 if (c != ESC)
10131 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010132 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 if (redrawing() && !char_avail())
10134 {
10135 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010136 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010137
10138 if (char2cells(c) == 1)
10139 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010140 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010141 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010142 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 }
10144#ifdef FEAT_CMDL_INFO
10145 add_to_showcmd_c(c);
10146#endif
10147 }
10148 ++no_mapping;
10149 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010150 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 --no_mapping;
10152 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010153 if (did_putchar)
10154 /* when the line fits in 'columns' the '?' is at the start of the
10155 * next line and will not be removed by a redraw */
10156 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 if (cc != ESC)
10158 {
10159 AppendToRedobuff((char_u *)CTRL_V_STR);
10160 c = getdigraph(c, cc, TRUE);
10161#ifdef FEAT_CMDL_INFO
10162 clear_showcmd();
10163#endif
10164 return c;
10165 }
10166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167#ifdef FEAT_CMDL_INFO
10168 clear_showcmd();
10169#endif
10170 return NUL;
10171}
10172#endif
10173
10174/*
10175 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10176 * Returns the char to be inserted, or NUL if none found.
10177 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010178 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010179ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180{
10181 int c;
10182 int temp;
10183 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010184 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185
10186 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10187 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010188 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010189 return NUL;
10190 }
10191
10192 /* try to advance to the cursor column */
10193 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010194 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 prev_ptr = ptr;
10196 validate_virtcol();
10197 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10198 {
10199 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010200 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010201 }
10202 if ((colnr_T)temp > curwin->w_virtcol)
10203 ptr = prev_ptr;
10204
10205#ifdef FEAT_MBYTE
10206 c = (*mb_ptr2char)(ptr);
10207#else
10208 c = *ptr;
10209#endif
10210 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010211 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010212 return c;
10213}
10214
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010215/*
10216 * CTRL-Y or CTRL-E typed in Insert mode.
10217 */
10218 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010219ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010220{
10221 int c = tc;
10222
10223#ifdef FEAT_INS_EXPAND
10224 if (ctrl_x_mode == CTRL_X_SCROLL)
10225 {
10226 if (c == Ctrl_Y)
10227 scrolldown_clamp();
10228 else
10229 scrollup_clamp();
10230 redraw_later(VALID);
10231 }
10232 else
10233#endif
10234 {
10235 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10236 if (c != NUL)
10237 {
10238 long tw_save;
10239
10240 /* The character must be taken literally, insert like it
10241 * was typed after a CTRL-V, and pretend 'textwidth'
10242 * wasn't set. Digits, 'o' and 'x' are special after a
10243 * CTRL-V, don't use it for these. */
10244 if (c < 256 && !isalnum(c))
10245 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10246 tw_save = curbuf->b_p_tw;
10247 curbuf->b_p_tw = -1;
10248 insert_special(c, TRUE, FALSE);
10249 curbuf->b_p_tw = tw_save;
10250#ifdef FEAT_RIGHTLEFT
10251 revins_chars++;
10252 revins_legal++;
10253#endif
10254 c = Ctrl_V; /* pretend CTRL-V is last character */
10255 auto_format(FALSE, TRUE);
10256 }
10257 }
10258 return c;
10259}
10260
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261#ifdef FEAT_SMARTINDENT
10262/*
10263 * Try to do some very smart auto-indenting.
10264 * Used when inserting a "normal" character.
10265 */
10266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010267ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010268{
10269 pos_T *pos, old_pos;
10270 char_u *ptr;
10271 int i;
10272 int temp;
10273
10274 /*
10275 * do some very smart indenting when entering '{' or '}'
10276 */
10277 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10278 {
10279 /*
10280 * for '}' set indent equal to indent of line containing matching '{'
10281 */
10282 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10283 {
10284 old_pos = curwin->w_cursor;
10285 /*
10286 * If the matching '{' has a ')' immediately before it (ignoring
10287 * white-space), then line up with the start of the line
10288 * containing the matching '(' if there is one. This handles the
10289 * case where an "if (..\n..) {" statement continues over multiple
10290 * lines -- webb
10291 */
10292 ptr = ml_get(pos->lnum);
10293 i = pos->col;
10294 if (i > 0) /* skip blanks before '{' */
10295 while (--i > 0 && vim_iswhite(ptr[i]))
10296 ;
10297 curwin->w_cursor.lnum = pos->lnum;
10298 curwin->w_cursor.col = i;
10299 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10300 curwin->w_cursor = *pos;
10301 i = get_indent();
10302 curwin->w_cursor = old_pos;
10303#ifdef FEAT_VREPLACE
10304 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010305 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 else
10307#endif
10308 (void)set_indent(i, SIN_CHANGED);
10309 }
10310 else if (curwin->w_cursor.col > 0)
10311 {
10312 /*
10313 * when inserting '{' after "O" reduce indent, but not
10314 * more than indent of previous line
10315 */
10316 temp = TRUE;
10317 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10318 {
10319 old_pos = curwin->w_cursor;
10320 i = get_indent();
10321 while (curwin->w_cursor.lnum > 1)
10322 {
10323 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10324
10325 /* ignore empty lines and lines starting with '#'. */
10326 if (*ptr != '#' && *ptr != NUL)
10327 break;
10328 }
10329 if (get_indent() >= i)
10330 temp = FALSE;
10331 curwin->w_cursor = old_pos;
10332 }
10333 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010334 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 }
10336 }
10337
10338 /*
10339 * set indent of '#' always to 0
10340 */
10341 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10342 {
10343 /* remember current indent for next line */
10344 old_indent = get_indent();
10345 (void)set_indent(0, SIN_CHANGED);
10346 }
10347
10348 /* Adjust ai_col, the char at this position can be deleted. */
10349 if (ai_col > curwin->w_cursor.col)
10350 ai_col = curwin->w_cursor.col;
10351}
10352#endif
10353
10354/*
10355 * Get the value that w_virtcol would have when 'list' is off.
10356 * Unless 'cpo' contains the 'L' flag.
10357 */
10358 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010359get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010360{
10361 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10362 return getvcol_nolist(&curwin->w_cursor);
10363 validate_virtcol();
10364 return curwin->w_virtcol;
10365}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010366
10367#ifdef FEAT_AUTOCMD
10368/*
10369 * Handle the InsertCharPre autocommand.
10370 * "c" is the character that was typed.
10371 * Return a pointer to allocated memory with the replacement string.
10372 * Return NULL to continue inserting "c".
10373 */
10374 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010375do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010376{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010377 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010378 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010379
10380 /* Return quickly when there is nothing to do. */
10381 if (!has_insertcharpre())
10382 return NULL;
10383
Bram Moolenaar704984a2012-06-01 14:57:51 +020010384#ifdef FEAT_MBYTE
10385 if (has_mbyte)
10386 buf[(*mb_char2bytes)(c, buf)] = NUL;
10387 else
10388#endif
10389 {
10390 buf[0] = c;
10391 buf[1] = NUL;
10392 }
10393
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010394 /* Lock the text to avoid weird things from happening. */
10395 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010396 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010397
Bram Moolenaar704984a2012-06-01 14:57:51 +020010398 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010399 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010400 {
10401 /* Get the value of v:char. It may be empty or more than one
10402 * character. Only use it when changed, otherwise continue with the
10403 * original character to avoid breaking autoindent. */
10404 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10405 res = vim_strsave(get_vim_var_str(VV_CHAR));
10406 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010407
10408 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10409 --textlock;
10410
10411 return res;
10412}
10413#endif