blob: 080e1ab319d3c60c2321064e3dc05849025a47e4 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaare4214502015-03-05 18:08:43 +010037#define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
Bram Moolenaare4214502015-03-05 18:08:43 +010040#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42static char *ctrl_x_msgs[] =
43{
44 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000046 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 N_(" Whole line completion (^L^N^P)"),
48 N_(" File name completion (^F^N^P)"),
49 N_(" Tag completion (^]^N^P)"),
50 N_(" Path pattern completion (^N^P)"),
51 N_(" Definition completion (^D^N^P)"),
52 NULL,
53 N_(" Dictionary completion (^K^N^P)"),
54 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000055 N_(" Command-line completion (^V^N^P)"),
56 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000057 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000058 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000059 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010060 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061};
62
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000063static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010064#ifdef FEAT_COMPL_FUNC
65static char e_complwin[] = N_("E839: Completion function changed window");
66static char e_compldel[] = N_("E840: Completion function deleted text");
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69/*
70 * Structure used to store one match for insert completion.
71 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000072typedef struct compl_S compl_T;
73struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000074{
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 compl_T *cp_next;
76 compl_T *cp_prev;
77 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000078 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000079 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000080 char_u *cp_fname; /* file containing the match, allocated when
81 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000082 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
83 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
Bram Moolenaar572cb562005-08-05 21:35:02 +000086#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#define FREE_FNAME (2)
88
89/*
90 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000091 * "compl_first_match" points to the start of the list.
92 * "compl_curr_match" points to the currently selected entry.
93 * "compl_shown_match" is different from compl_curr_match during
94 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000096static compl_T *compl_first_match = NULL;
97static compl_T *compl_curr_match = NULL;
98static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000100/* After using a cursor key <Enter> selects a match in the popup menu,
101 * otherwise it inserts a line break. */
102static int compl_enter_selects = FALSE;
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104/* When "compl_leader" is not NULL only matches that start with this string
105 * are used. */
106static char_u *compl_leader = NULL;
107
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000108static int compl_get_longest = FALSE; /* put longest common string
109 in compl_leader */
110
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200111static int compl_no_insert = FALSE; /* FALSE: select & insert
112 TRUE: noinsert */
113static int compl_no_select = FALSE; /* FALSE: select & insert
114 TRUE: noselect */
115
Bram Moolenaara6557602006-02-04 22:43:20 +0000116static int compl_used_match; /* Selected one of the matches. When
117 FALSE the match was edited or using
118 the longest common string. */
119
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000120static int compl_was_interrupted = FALSE; /* didn't finish finding
121 completions. */
122
123static int compl_restarting = FALSE; /* don't insert match */
124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125/* When the first completion is done "compl_started" is set. When it's
126 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000129/* Set when doing something for completion that may call edit() recursively,
130 * which is not allowed. */
131static int compl_busy = FALSE;
132
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static int compl_matches = 0;
134static char_u *compl_pattern = NULL;
135static int compl_direction = FORWARD;
136static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000137static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000138static pos_T compl_startpos;
139static colnr_T compl_col = 0; /* column where the text starts
140 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000141static char_u *compl_orig_text = NULL; /* text as it was before
142 * completion started */
143static int compl_cont_mode = 0;
144static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar82139082011-09-14 16:52:09 +0200146static int compl_opt_refresh_always = FALSE;
147
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100148static void ins_ctrl_x(void);
149static int has_compl_option(int dict_opt);
150static int ins_compl_accept_char(int c);
151static int ins_compl_add(char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup);
152static int ins_compl_equal(compl_T *match, char_u *str, int len);
153static void ins_compl_longest_match(compl_T *match);
154static void ins_compl_add_matches(int num_matches, char_u **matches, int icase);
155static int ins_compl_make_cyclic(void);
156static void ins_compl_upd_pum(void);
157static void ins_compl_del_pum(void);
158static int pum_wanted(void);
159static int pum_enough_matches(void);
160static void ins_compl_dictionaries(char_u *dict, char_u *pat, int flags, int thesaurus);
161static void ins_compl_files(int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir);
162static char_u *find_line_end(char_u *ptr);
163static void ins_compl_free(void);
164static void ins_compl_clear(void);
165static int ins_compl_bs(void);
166static int ins_compl_need_restart(void);
167static void ins_compl_new_leader(void);
168static void ins_compl_addleader(int c);
169static int ins_compl_len(void);
170static void ins_compl_restart(void);
171static void ins_compl_set_original_text(char_u *str);
172static void ins_compl_addfrommatch(void);
173static int ins_compl_prep(int c);
174static void ins_compl_fixRedoBufForLeader(char_u *ptr_arg);
175static buf_T *ins_compl_next_buf(buf_T *buf, int flag);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000176#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100177static void ins_compl_add_list(list_T *list);
178static void ins_compl_add_dict(dict_T *dict);
Bram Moolenaara94bc432006-03-10 21:42:59 +0000179#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100180static int ins_compl_get_exp(pos_T *ini);
181static void ins_compl_delete(void);
182static void ins_compl_insert(void);
183static int ins_compl_next(int allow_get_expansion, int count, int insert_match);
184static int ins_compl_key2dir(int c);
185static int ins_compl_pum_key(int c);
186static int ins_compl_key2count(int c);
187static int ins_compl_use_match(int c);
Bram Moolenaar8aefbe02016-02-23 20:13:16 +0100188static int ins_complete(int c, int enable_pum);
189static void show_pum(int save_w_wrow);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100190static unsigned quote_meta(char_u *dest, char_u *str, int len);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000191#endif /* FEAT_INS_EXPAND */
192
193#define BACKSPACE_CHAR 1
194#define BACKSPACE_WORD 2
195#define BACKSPACE_WORD_NOT_SPACE 3
196#define BACKSPACE_LINE 4
197
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100198static void ins_redraw(int ready);
199static void ins_ctrl_v(void);
200static void undisplay_dollar(void);
201static void insert_special(int, int, int);
202static void internal_format(int textwidth, int second_indent, int flags, int format_only, int c);
203static void check_auto_format(int);
204static void redo_literal(int c);
205static void start_arrow(pos_T *end_insert_pos);
206static void start_arrow_with_change(pos_T *end_insert_pos, int change);
207static void start_arrow_common(pos_T *end_insert_pos, int change);
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000208#ifdef FEAT_SPELL
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100209static void check_spell_redraw(void);
210static void spell_back_to_badword(void);
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000211static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000212#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100213static void stop_insert(pos_T *end_insert_pos, int esc, int nomove);
214static int echeck_abbr(int);
215static int replace_pop(void);
216static void replace_join(int off);
217static void replace_pop_ins(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218#ifdef FEAT_MBYTE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100219static void mb_replace_pop_ins(int cc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100221static void replace_flush(void);
222static void replace_do_bs(int limit_col);
223static int del_char_after_col(int limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224#ifdef FEAT_CINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100225static int cindent_on(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100227static void ins_reg(void);
228static void ins_ctrl_g(void);
229static void ins_ctrl_hat(void);
230static int ins_esc(long *count, int cmdchar, int nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231#ifdef FEAT_RIGHTLEFT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100232static void ins_ctrl_(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100234static int ins_start_select(int c);
235static void ins_insert(int replaceState);
236static void ins_ctrl_o(void);
237static void ins_shift(int c, int lastc);
238static void ins_del(void);
239static int ins_bs(int c, int mode, int *inserted_space_p);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#ifdef FEAT_MOUSE
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100241static void ins_mouse(int c);
242static void ins_mousescroll(int dir);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000243#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000244#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100245static void ins_tabline(int c);
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000246#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100247static void ins_left(int end_change);
248static void ins_home(int c);
249static void ins_end(int c);
250static void ins_s_left(void);
251static void ins_right(int end_change);
252static void ins_s_right(void);
253static void ins_up(int startcol);
254static void ins_pageup(void);
255static void ins_down(int startcol);
256static void ins_pagedown(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_DND
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100258static void ins_drop(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100260static int ins_tab(void);
261static int ins_eol(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262#ifdef FEAT_DIGRAPHS
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100263static int ins_digraph(void);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100265static int ins_ctrl_ey(int tc);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266#ifdef FEAT_SMARTINDENT
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100267static void ins_try_si(int c);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268#endif
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100269static colnr_T get_nolist_virtcol(void);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100270#ifdef FEAT_AUTOCMD
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100271static char_u *do_insert_char_pre(int c);
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274static colnr_T Insstart_textlen; /* length of line when insert started */
275static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100276static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277
278static char_u *last_insert = NULL; /* the text of the previous insert,
279 K_SPECIAL and CSI are escaped */
280static int last_insert_skip; /* nr of chars in front of previous insert */
281static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000282static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000283
284#ifdef FEAT_CINDENT
285static int can_cindent; /* may do cindenting on this line */
286#endif
287
288static int old_indent = 0; /* for ^^D command in insert mode */
289
290#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000291static int revins_on; /* reverse insert mode on */
292static int revins_chars; /* how much to skip after edit */
293static int revins_legal; /* was the last char 'legal'? */
294static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000295#endif
296
Bram Moolenaar071d4272004-06-13 20:20:40 +0000297static int ins_need_undo; /* call u_save() before inserting a
298 char. Set when edit() is called.
299 after that arrow_used is used. */
300
301static int did_add_space = FALSE; /* auto_format() added an extra space
302 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200303static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
304 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000305
306/*
307 * edit(): Start inserting text.
308 *
309 * "cmdchar" can be:
310 * 'i' normal insert command
311 * 'a' normal append command
312 * 'R' replace command
313 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
314 * but still only one <CR> is inserted. The <Esc> is not used for redo.
315 * 'g' "gI" command.
316 * 'V' "gR" command for Virtual Replace mode.
317 * 'v' "gr" command for single character Virtual Replace mode.
318 *
319 * This function is not called recursively. For CTRL-O commands, it returns
320 * and lets the caller handle the Normal-mode command.
321 *
322 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
323 */
324 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100325edit(
326 int cmdchar,
327 int startln, /* if set, insert at start of line */
328 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329{
330 int c = 0;
331 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100332 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000333 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 int i;
336 int did_backspace = TRUE; /* previous char was backspace */
337#ifdef FEAT_CINDENT
338 int line_is_white = FALSE; /* line is empty before insert */
339#endif
340 linenr_T old_topline = 0; /* topline before insertion */
341#ifdef FEAT_DIFF
342 int old_topfill = -1;
343#endif
344 int inserted_space = FALSE; /* just inserted a space */
345 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000346 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000348 /* Remember whether editing was restarted after CTRL-O. */
349 did_restart_edit = restart_edit;
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351 /* sleep before redrawing, needed for "CTRL-O :" that results in an
352 * error message */
353 check_for_delay(TRUE);
354
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100355 /* set Insstart_orig to Insstart */
356 update_Insstart_orig = TRUE;
357
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358#ifdef HAVE_SANDBOX
359 /* Don't allow inserting in the sandbox. */
360 if (sandbox != 0)
361 {
362 EMSG(_(e_sandbox));
363 return FALSE;
364 }
365#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000366 /* Don't allow changes in the buffer while editing the cmdline. The
367 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000368 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000369 {
370 EMSG(_(e_secure));
371 return FALSE;
372 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000373
374#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000375 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000376 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000377 {
378 EMSG(_(e_secure));
379 return FALSE;
380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000381 ins_compl_clear(); /* clear stuff for CTRL-X mode */
382#endif
383
Bram Moolenaar843ee412004-06-30 16:16:41 +0000384#ifdef FEAT_AUTOCMD
385 /*
386 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
387 */
388 if (cmdchar != 'r' && cmdchar != 'v')
389 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100390 pos_T save_cursor = curwin->w_cursor;
391
Bram Moolenaar1e015462005-09-25 22:16:38 +0000392# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000393 if (cmdchar == 'R')
394 ptr = (char_u *)"r";
395 else if (cmdchar == 'V')
396 ptr = (char_u *)"v";
397 else
398 ptr = (char_u *)"i";
399 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200400 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000401# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000402 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100403
Bram Moolenaar097c9922013-05-19 21:15:15 +0200404 /* Make sure the cursor didn't move. Do call check_cursor_col() in
405 * case the text was modified. Since Insert mode was not started yet
406 * a call to check_cursor_col() may move the cursor, especially with
407 * the "A" command, thus set State to avoid that. Also check that the
408 * line number is still valid (lines may have been deleted).
409 * Do not restore if v:char was set to a non-empty string. */
410 if (!equalpos(curwin->w_cursor, save_cursor)
411# ifdef FEAT_EVAL
412 && *get_vim_var_str(VV_CHAR) == NUL
413# endif
414 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100415 {
416 int save_state = State;
417
418 curwin->w_cursor = save_cursor;
419 State = INSERT;
420 check_cursor_col();
421 State = save_state;
422 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000423 }
424#endif
425
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200426#ifdef FEAT_CONCEAL
427 /* Check if the cursor line needs redrawing before changing State. If
428 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200429 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200430#endif
431
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432#ifdef FEAT_MOUSE
433 /*
434 * When doing a paste with the middle mouse button, Insstart is set to
435 * where the paste started.
436 */
437 if (where_paste_started.lnum != 0)
438 Insstart = where_paste_started;
439 else
440#endif
441 {
442 Insstart = curwin->w_cursor;
443 if (startln)
444 Insstart.col = 0;
445 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000446 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000447 Insstart_blank_vcol = MAXCOL;
448 if (!did_ai)
449 ai_col = 0;
450
451 if (cmdchar != NUL && restart_edit == 0)
452 {
453 ResetRedobuff();
454 AppendNumberToRedobuff(count);
455#ifdef FEAT_VREPLACE
456 if (cmdchar == 'V' || cmdchar == 'v')
457 {
458 /* "gR" or "gr" command */
459 AppendCharToRedobuff('g');
460 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
461 }
462 else
463#endif
464 {
465 AppendCharToRedobuff(cmdchar);
466 if (cmdchar == 'g') /* "gI" command */
467 AppendCharToRedobuff('I');
468 else if (cmdchar == 'r') /* "r<CR>" command */
469 count = 1; /* insert only one <CR> */
470 }
471 }
472
473 if (cmdchar == 'R')
474 {
475#ifdef FEAT_FKMAP
476 if (p_fkmap && p_ri)
477 {
478 beep_flush();
479 EMSG(farsi_text_3); /* encoded in Farsi */
480 State = INSERT;
481 }
482 else
483#endif
484 State = REPLACE;
485 }
486#ifdef FEAT_VREPLACE
487 else if (cmdchar == 'V' || cmdchar == 'v')
488 {
489 State = VREPLACE;
490 replaceState = VREPLACE;
491 orig_line_count = curbuf->b_ml.ml_line_count;
492 vr_lines_changed = 1;
493 }
494#endif
495 else
496 State = INSERT;
497
498 stop_insert_mode = FALSE;
499
500 /*
501 * Need to recompute the cursor position, it might move when the cursor is
502 * on a TAB or special character.
503 */
504 curs_columns(TRUE);
505
506 /*
507 * Enable langmap or IME, indicated by 'iminsert'.
508 * Note that IME may enabled/disabled without us noticing here, thus the
509 * 'iminsert' value may not reflect what is actually used. It is updated
510 * when hitting <Esc>.
511 */
512 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
513 State |= LANGMAP;
514#ifdef USE_IM_CONTROL
515 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
516#endif
517
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518#ifdef FEAT_MOUSE
519 setmouse();
520#endif
521#ifdef FEAT_CMDL_INFO
522 clear_showcmd();
523#endif
524#ifdef FEAT_RIGHTLEFT
525 /* there is no reverse replace mode */
526 revins_on = (State == INSERT && p_ri);
527 if (revins_on)
528 undisplay_dollar();
529 revins_chars = 0;
530 revins_legal = 0;
531 revins_scol = -1;
532#endif
533
534 /*
535 * Handle restarting Insert mode.
536 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
537 * restart_edit non-zero, and something in the stuff buffer.
538 */
539 if (restart_edit != 0 && stuff_empty())
540 {
541#ifdef FEAT_MOUSE
542 /*
543 * After a paste we consider text typed to be part of the insert for
544 * the pasted text. You can backspace over the pasted text too.
545 */
546 if (where_paste_started.lnum)
547 arrow_used = FALSE;
548 else
549#endif
550 arrow_used = TRUE;
551 restart_edit = 0;
552
553 /*
554 * If the cursor was after the end-of-line before the CTRL-O and it is
555 * now at the end-of-line, put it after the end-of-line (this is not
556 * correct in very rare cases).
557 * Also do this if curswant is greater than the current virtual
558 * column. Eg after "^O$" or "^O80|".
559 */
560 validate_virtcol();
561 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000562 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 || curwin->w_curswant > curwin->w_virtcol)
564 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
565 {
566 if (ptr[1] == NUL)
567 ++curwin->w_cursor.col;
568#ifdef FEAT_MBYTE
569 else if (has_mbyte)
570 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000571 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 if (ptr[i] == NUL)
573 curwin->w_cursor.col += i;
574 }
575#endif
576 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000577 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 }
579 else
580 arrow_used = FALSE;
581
582 /* we are in insert mode now, don't need to start it anymore */
583 need_start_insertmode = FALSE;
584
585 /* Need to save the line for undo before inserting the first char. */
586 ins_need_undo = TRUE;
587
588#ifdef FEAT_MOUSE
589 where_paste_started.lnum = 0;
590#endif
591#ifdef FEAT_CINDENT
592 can_cindent = TRUE;
593#endif
594#ifdef FEAT_FOLDING
595 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
596 * restarting. */
597 if (!p_im && did_restart_edit == 0)
598 foldOpenCursor();
599#endif
600
601 /*
602 * If 'showmode' is set, show the current (insert/replace/..) mode.
603 * A warning message for changing a readonly file is given here, before
604 * actually changing anything. It's put after the mode, if any.
605 */
606 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000607 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 i = showmode();
609
610 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000611 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612
613#ifdef CURSOR_SHAPE
614 ui_cursor_shape(); /* may show different cursor shape */
615#endif
616#ifdef FEAT_DIGRAPHS
617 do_digraph(-1); /* clear digraphs */
618#endif
619
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000620 /*
621 * Get the current length of the redo buffer, those characters have to be
622 * skipped if we want to get to the inserted characters.
623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000624 ptr = get_inserted();
625 if (ptr == NULL)
626 new_insert_skip = 0;
627 else
628 {
629 new_insert_skip = (int)STRLEN(ptr);
630 vim_free(ptr);
631 }
632
633 old_indent = 0;
634
635 /*
636 * Main loop in Insert mode: repeat until Insert mode is left.
637 */
638 for (;;)
639 {
640#ifdef FEAT_RIGHTLEFT
641 if (!revins_legal)
642 revins_scol = -1; /* reset on illegal motions */
643 else
644 revins_legal = 0;
645#endif
646 if (arrow_used) /* don't repeat insert when arrow key used */
647 count = 0;
648
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100649 if (update_Insstart_orig)
650 Insstart_orig = Insstart;
651
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 if (stop_insert_mode)
653 {
654 /* ":stopinsert" used or 'insertmode' reset */
655 count = 0;
656 goto doESCkey;
657 }
658
659 /* set curwin->w_curswant for next K_DOWN or K_UP */
660 if (!arrow_used)
661 curwin->w_set_curswant = TRUE;
662
663 /* If there is no typeahead may check for timestamps (e.g., for when a
664 * menu invoked a shell command). */
665 if (stuff_empty())
666 {
667 did_check_timestamps = FALSE;
668 if (need_check_timestamps)
669 check_timestamps(FALSE);
670 }
671
672 /*
673 * When emsg() was called msg_scroll will have been set.
674 */
675 msg_scroll = FALSE;
676
677#ifdef FEAT_GUI
678 /* When 'mousefocus' is set a mouse movement may have taken us to
679 * another window. "need_mouse_correct" may then be set because of an
680 * autocommand. */
681 if (need_mouse_correct)
682 gui_mouse_correct();
683#endif
684
685#ifdef FEAT_FOLDING
686 /* Open fold at the cursor line, according to 'foldopen'. */
687 if (fdo_flags & FDO_INSERT)
688 foldOpenCursor();
689 /* Close folds where the cursor isn't, according to 'foldclose' */
690 if (!char_avail())
691 foldCheckClose();
692#endif
693
694 /*
695 * If we inserted a character at the last position of the last line in
696 * the window, scroll the window one line up. This avoids an extra
697 * redraw.
698 * This is detected when the cursor column is smaller after inserting
699 * something.
700 * Don't do this when the topline changed already, it has
701 * already been adjusted (by insertchar() calling open_line())).
702 */
703 if (curbuf->b_mod_set
704 && curwin->w_p_wrap
705 && !did_backspace
706 && curwin->w_topline == old_topline
707#ifdef FEAT_DIFF
708 && curwin->w_topfill == old_topfill
709#endif
710 )
711 {
712 mincol = curwin->w_wcol;
713 validate_cursor_col();
714
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000715 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000716 && curwin->w_wrow == W_WINROW(curwin)
717 + curwin->w_height - 1 - p_so
718 && (curwin->w_cursor.lnum != curwin->w_topline
719#ifdef FEAT_DIFF
720 || curwin->w_topfill > 0
721#endif
722 ))
723 {
724#ifdef FEAT_DIFF
725 if (curwin->w_topfill > 0)
726 --curwin->w_topfill;
727 else
728#endif
729#ifdef FEAT_FOLDING
730 if (hasFolding(curwin->w_topline, NULL, &old_topline))
731 set_topline(curwin, old_topline + 1);
732 else
733#endif
734 set_topline(curwin, curwin->w_topline + 1);
735 }
736 }
737
738 /* May need to adjust w_topline to show the cursor. */
739 update_topline();
740
741 did_backspace = FALSE;
742
743 validate_cursor(); /* may set must_redraw */
744
745 /*
746 * Redraw the display when no characters are waiting.
747 * Also shows mode, ruler and positions cursor.
748 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000749 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000750
751#ifdef FEAT_SCROLLBIND
752 if (curwin->w_p_scb)
753 do_check_scrollbind(TRUE);
754#endif
755
Bram Moolenaar860cae12010-06-05 23:22:07 +0200756#ifdef FEAT_CURSORBIND
757 if (curwin->w_p_crb)
758 do_check_cursorbind();
759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760 update_curswant();
761 old_topline = curwin->w_topline;
762#ifdef FEAT_DIFF
763 old_topfill = curwin->w_topfill;
764#endif
765
766#ifdef USE_ON_FLY_SCROLL
767 dont_scroll = FALSE; /* allow scrolling here */
768#endif
769
770 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000771 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100773 if (c != K_CURSORHOLD)
774 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200775
776 /* After using CTRL-G U the next cursor key will not break undo. */
777 if (dont_sync_undo == MAYBE)
778 dont_sync_undo = TRUE;
779 else
780 dont_sync_undo = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000781 do
782 {
783 c = safe_vgetc();
784 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000786#ifdef FEAT_AUTOCMD
787 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
788 did_cursorhold = TRUE;
789#endif
790
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#ifdef FEAT_RIGHTLEFT
792 if (p_hkmap && KeyTyped)
793 c = hkmap(c); /* Hebrew mode mapping */
794#endif
795#ifdef FEAT_FKMAP
796 if (p_fkmap && KeyTyped)
797 c = fkmap(c); /* Farsi mode mapping */
798#endif
799
800#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000801 /*
802 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000803 * and the cursor is still in the completed word. Only when there is
804 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000805 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000806 if (compl_started
807 && pum_wanted()
808 && curwin->w_cursor.col >= compl_col
809 && (compl_shown_match == NULL
810 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000811 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000812 /* BS: Delete one character from "compl_leader". */
813 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000814 && curwin->w_cursor.col > compl_col
815 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000816 continue;
817
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000818 /* When no match was selected or it was edited. */
819 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000820 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000821 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000822 * "compl_leader". Except when at the original match and
823 * there is nothing to add, CTRL-L works like CTRL-P then. */
824 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100825 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000826 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000827 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000828 {
829 ins_compl_addfrommatch();
830 continue;
831 }
832
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000833 /* A non-white character that fits in with the current
834 * completion: Add to "compl_leader". */
835 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000836 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100837#ifdef FEAT_AUTOCMD
838 /* Trigger InsertCharPre. */
839 char_u *str = do_insert_char_pre(c);
840 char_u *p;
841
842 if (str != NULL)
843 {
844 for (p = str; *p != NUL; mb_ptr_adv(p))
845 ins_compl_addleader(PTR2CHAR(p));
846 vim_free(str);
847 }
848 else
849#endif
850 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000851 continue;
852 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000853
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000854 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000855 * compl_enter_selects is set the Enter key does the same. */
856 if (c == Ctrl_Y || (compl_enter_selects
857 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000858 {
859 ins_compl_delete();
860 ins_compl_insert();
861 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000862 }
863 }
864
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
866 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000867 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000868 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000869 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000870#endif
871
Bram Moolenaar488c6512005-08-11 20:09:58 +0000872 /* CTRL-\ CTRL-N goes to Normal mode,
873 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
874 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (c == Ctrl_BSL)
876 {
877 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000878 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000879 ++no_mapping;
880 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000881 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 --no_mapping;
883 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000884 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000885 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000886 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000887 vungetc(c);
888 c = Ctrl_BSL;
889 }
890 else if (c == Ctrl_G && p_im)
891 continue;
892 else
893 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000894 if (c == Ctrl_O)
895 {
896 ins_ctrl_o();
897 ins_at_eol = FALSE; /* cursor keeps its column */
898 nomove = TRUE;
899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 count = 0;
901 goto doESCkey;
902 }
903 }
904
905#ifdef FEAT_DIGRAPHS
906 c = do_digraph(c);
907#endif
908
909#ifdef FEAT_INS_EXPAND
910 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
911 goto docomplete;
912#endif
913 if (c == Ctrl_V || c == Ctrl_Q)
914 {
915 ins_ctrl_v();
916 c = Ctrl_V; /* pretend CTRL-V is last typed character */
917 continue;
918 }
919
920#ifdef FEAT_CINDENT
921 if (cindent_on()
922# ifdef FEAT_INS_EXPAND
923 && ctrl_x_mode == 0
924# endif
925 )
926 {
927 /* A key name preceded by a bang means this key is not to be
928 * inserted. Skip ahead to the re-indenting below.
929 * A key name preceded by a star means that indenting has to be
930 * done before inserting the key. */
931 line_is_white = inindent(0);
932 if (in_cinkeys(c, '!', line_is_white))
933 goto force_cindent;
934 if (can_cindent && in_cinkeys(c, '*', line_is_white)
935 && stop_arrow() == OK)
936 do_c_expr_indent();
937 }
938#endif
939
940#ifdef FEAT_RIGHTLEFT
941 if (curwin->w_p_rl)
942 switch (c)
943 {
944 case K_LEFT: c = K_RIGHT; break;
945 case K_S_LEFT: c = K_S_RIGHT; break;
946 case K_C_LEFT: c = K_C_RIGHT; break;
947 case K_RIGHT: c = K_LEFT; break;
948 case K_S_RIGHT: c = K_S_LEFT; break;
949 case K_C_RIGHT: c = K_C_LEFT; break;
950 }
951#endif
952
Bram Moolenaar071d4272004-06-13 20:20:40 +0000953 /*
954 * If 'keymodel' contains "startsel", may start selection. If it
955 * does, a CTRL-O and c will be stuffed, we need to get these
956 * characters.
957 */
958 if (ins_start_select(c))
959 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960
961 /*
962 * The big switch to handle a character in insert mode.
963 */
964 switch (c)
965 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000966 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000967 if (echeck_abbr(ESC + ABBR_OFF))
968 break;
969 /*FALLTHROUGH*/
970
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000971 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972#ifdef FEAT_CMDWIN
973 if (c == Ctrl_C && cmdwin_type != 0)
974 {
975 /* Close the cmdline window. */
976 cmdwin_result = K_IGNORE;
977 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000978 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 goto doESCkey;
980 }
981#endif
982
983#ifdef UNIX
984do_intr:
985#endif
986 /* when 'insertmode' set, and not halfway a mapping, don't leave
987 * Insert mode */
988 if (goto_im())
989 {
990 if (got_int)
991 {
992 (void)vgetc(); /* flush all buffers */
993 got_int = FALSE;
994 }
995 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200996 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000997 break;
998 }
999doESCkey:
1000 /*
1001 * This is the ONLY return from edit()!
1002 */
1003 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1004 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001005 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 o_lnum = curwin->w_cursor.lnum;
1007
Bram Moolenaar488c6512005-08-11 20:09:58 +00001008 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001009 {
1010#ifdef FEAT_AUTOCMD
1011 if (cmdchar != 'r' && cmdchar != 'v')
1012 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1013 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001014 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001018 continue;
1019
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001020 case Ctrl_Z: /* suspend when 'insertmode' set */
1021 if (!p_im)
1022 goto normalchar; /* insert CTRL-Z as normal char */
1023 stuffReadbuff((char_u *)":st\r");
1024 c = Ctrl_O;
1025 /*FALLTHROUGH*/
1026
1027 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001028#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001029 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001030 goto docomplete;
1031#endif
1032 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1033 break;
1034 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001035
1036#ifdef FEAT_VIRTUALEDIT
1037 /* don't move the cursor left when 'virtualedit' has "onemore". */
1038 if (ve_flags & VE_ONEMORE)
1039 {
1040 ins_at_eol = FALSE;
1041 nomove = TRUE;
1042 }
1043#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 count = 0;
1045 goto doESCkey;
1046
Bram Moolenaar572cb562005-08-05 21:35:02 +00001047 case K_INS: /* toggle insert/replace mode */
1048 case K_KINS:
1049 ins_insert(replaceState);
1050 break;
1051
1052 case K_SELECT: /* end of Select mode mapping - ignore */
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case K_HELP: /* Help key works like <ESC> <Help> */
1056 case K_F1:
1057 case K_XF1:
1058 stuffcharReadbuff(K_HELP);
1059 if (p_im)
1060 need_start_insertmode = TRUE;
1061 goto doESCkey;
1062
1063#ifdef FEAT_NETBEANS_INTG
1064 case K_F21: /* NetBeans command */
1065 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001066 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 --no_mapping;
1068 netbeans_keycommand(i);
1069 break;
1070#endif
1071
1072 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case NUL:
1074 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 /* For ^@ the trailing ESC will end the insert, unless there is an
1076 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1078 && c != Ctrl_A && !p_im)
1079 goto doESCkey; /* quit insert mode */
1080 inserted_space = FALSE;
1081 break;
1082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001084 ins_reg();
1085 auto_format(FALSE, TRUE);
1086 inserted_space = FALSE;
1087 break;
1088
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001089 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 ins_ctrl_g();
1091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case Ctrl_HAT: /* switch input mode and/or langmap */
1094 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 break;
1096
1097#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 if (!p_ari)
1100 goto normalchar;
1101 ins_ctrl_();
1102 break;
1103#endif
1104
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001105 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001106#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1107 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1108 goto docomplete;
1109#endif
1110 /* FALLTHROUGH */
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113# ifdef FEAT_INS_EXPAND
1114 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1115 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 if (has_compl_option(FALSE))
1117 goto docomplete;
1118 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 }
1120# endif
1121 ins_shift(c, lastc);
1122 auto_format(FALSE, TRUE);
1123 inserted_space = FALSE;
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001127 case K_KDEL:
1128 ins_del();
1129 auto_format(FALSE, TRUE);
1130 break;
1131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001133 case Ctrl_H:
1134 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1135 auto_format(FALSE, TRUE);
1136 break;
1137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1140 auto_format(FALSE, TRUE);
1141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001144# ifdef FEAT_COMPL_FUNC
1145 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001146 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001147 goto docomplete;
1148# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1150 auto_format(FALSE, TRUE);
1151 inserted_space = FALSE;
1152 break;
1153
1154#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 case K_LEFTMOUSE_NM:
1157 case K_LEFTDRAG:
1158 case K_LEFTRELEASE:
1159 case K_LEFTRELEASE_NM:
1160 case K_MIDDLEMOUSE:
1161 case K_MIDDLEDRAG:
1162 case K_MIDDLERELEASE:
1163 case K_RIGHTMOUSE:
1164 case K_RIGHTDRAG:
1165 case K_RIGHTRELEASE:
1166 case K_X1MOUSE:
1167 case K_X1DRAG:
1168 case K_X1RELEASE:
1169 case K_X2MOUSE:
1170 case K_X2DRAG:
1171 case K_X2RELEASE:
1172 ins_mouse(c);
1173 break;
1174
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001175 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001176 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177 break;
1178
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001179 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001180 ins_mousescroll(MSCR_UP);
1181 break;
1182
1183 case K_MOUSELEFT: /* Scroll wheel left */
1184 ins_mousescroll(MSCR_LEFT);
1185 break;
1186
1187 case K_MOUSERIGHT: /* Scroll wheel right */
1188 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 break;
1190#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001191#ifdef FEAT_GUI_TABLINE
1192 case K_TABLINE:
1193 case K_TABMENU:
1194 ins_tabline(c);
1195 break;
1196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001198 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 break;
1200
Bram Moolenaar754b5602006-02-09 23:53:20 +00001201#ifdef FEAT_AUTOCMD
1202 case K_CURSORHOLD: /* Didn't type something for a while. */
1203 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1204 did_cursorhold = TRUE;
1205 break;
1206#endif
1207
Bram Moolenaar4770d092006-01-12 23:22:24 +00001208#ifdef FEAT_GUI_W32
1209 /* On Win32 ignore <M-F4>, we get it when closing the window was
1210 * cancelled. */
1211 case K_F4:
1212 if (mod_mask != MOD_MASK_ALT)
1213 goto normalchar;
1214 break;
1215#endif
1216
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217#ifdef FEAT_GUI
1218 case K_VER_SCROLLBAR:
1219 ins_scroll();
1220 break;
1221
1222 case K_HOR_SCROLLBAR:
1223 ins_horscroll();
1224 break;
1225#endif
1226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001228 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 case K_S_HOME:
1230 case K_C_HOME:
1231 ins_home(c);
1232 break;
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 case K_S_END:
1237 case K_C_END:
1238 ins_end(c);
1239 break;
1240
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001241 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001242 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1243 ins_s_left();
1244 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001245 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 break;
1247
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 case K_C_LEFT:
1250 ins_s_left();
1251 break;
1252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001254 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1255 ins_s_right();
1256 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001257 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 break;
1259
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001260 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261 case K_C_RIGHT:
1262 ins_s_right();
1263 break;
1264
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001265 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001266#ifdef FEAT_INS_EXPAND
1267 if (pum_visible())
1268 goto docomplete;
1269#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001270 if (mod_mask & MOD_MASK_SHIFT)
1271 ins_pageup();
1272 else
1273 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 break;
1275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 case K_PAGEUP:
1278 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001279#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001280 if (pum_visible())
1281 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001282#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001283 ins_pageup();
1284 break;
1285
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001286 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001287#ifdef FEAT_INS_EXPAND
1288 if (pum_visible())
1289 goto docomplete;
1290#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001291 if (mod_mask & MOD_MASK_SHIFT)
1292 ins_pagedown();
1293 else
1294 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 break;
1296
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001297 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001298 case K_PAGEDOWN:
1299 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001300#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001301 if (pum_visible())
1302 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 ins_pagedown();
1305 break;
1306
1307#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 ins_drop();
1310 break;
1311#endif
1312
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001313 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 c = TAB;
1315 /* FALLTHROUGH */
1316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001317 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1319 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1320 goto docomplete;
1321#endif
1322 inserted_space = FALSE;
1323 if (ins_tab())
1324 goto normalchar; /* insert TAB as a normal char */
1325 auto_format(FALSE, TRUE);
1326 break;
1327
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001328 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001329 c = CAR;
1330 /* FALLTHROUGH */
1331 case CAR:
1332 case NL:
1333#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1334 /* In a quickfix window a <CR> jumps to the error under the
1335 * cursor. */
1336 if (bt_quickfix(curbuf) && c == CAR)
1337 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001338 if (curwin->w_llist_ref == NULL) /* quickfix window */
1339 do_cmdline_cmd((char_u *)".cc");
1340 else /* location list window */
1341 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 break;
1343 }
1344#endif
1345#ifdef FEAT_CMDWIN
1346 if (cmdwin_type != 0)
1347 {
1348 /* Execute the command in the cmdline window. */
1349 cmdwin_result = CAR;
1350 goto doESCkey;
1351 }
1352#endif
1353 if (ins_eol(c) && !p_im)
1354 goto doESCkey; /* out of memory */
1355 auto_format(FALSE, FALSE);
1356 inserted_space = FALSE;
1357 break;
1358
Bram Moolenaar860cae12010-06-05 23:22:07 +02001359#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361# ifdef FEAT_INS_EXPAND
1362 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1363 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001364 if (has_compl_option(TRUE))
1365 goto docomplete;
1366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 }
1368# endif
1369# ifdef FEAT_DIGRAPHS
1370 c = ins_digraph();
1371 if (c == NUL)
1372 break;
1373# endif
1374 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376
1377#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001378 case Ctrl_X: /* Enter CTRL-X mode */
1379 ins_ctrl_x();
1380 break;
1381
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (ctrl_x_mode != CTRL_X_TAGS)
1384 goto normalchar;
1385 goto docomplete;
1386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (ctrl_x_mode != CTRL_X_FILES)
1389 goto normalchar;
1390 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001391
1392 case 's': /* Spelling completion after ^X */
1393 case Ctrl_S:
1394 if (ctrl_x_mode != CTRL_X_SPELL)
1395 goto normalchar;
1396 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397#endif
1398
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001399 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400#ifdef FEAT_INS_EXPAND
1401 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1402#endif
1403 {
1404 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1405 if (p_im)
1406 {
1407 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1408 break;
1409 goto doESCkey;
1410 }
1411 goto normalchar;
1412 }
1413#ifdef FEAT_INS_EXPAND
1414 /* FALLTHROUGH */
1415
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001416 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 case Ctrl_N:
1418 /* if 'complete' is empty then plain ^P is no longer special,
1419 * but it is under other ^X modes */
1420 if (*curbuf->b_p_cpt == NUL
1421 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001422 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 goto normalchar;
1424
1425docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001426 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001427 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001428 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001429 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001430 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001431 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 break;
1433#endif /* FEAT_INS_EXPAND */
1434
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001435 case Ctrl_Y: /* copy from previous line or scroll down */
1436 case Ctrl_E: /* copy from next line or scroll up */
1437 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001438 break;
1439
1440 default:
1441#ifdef UNIX
1442 if (c == intr_char) /* special interrupt char */
1443 goto do_intr;
1444#endif
1445
Bram Moolenaare659c952011-05-19 17:25:41 +02001446normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001448 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001449 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001450#ifdef FEAT_AUTOCMD
1451 if (!p_paste)
1452 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001453 /* Trigger InsertCharPre. */
1454 char_u *str = do_insert_char_pre(c);
1455 char_u *p;
1456
1457 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001458 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001459 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001460 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001461 /* Insert the new value of v:char literally. */
1462 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001463 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001464 c = PTR2CHAR(p);
1465 if (c == CAR || c == K_KENTER || c == NL)
1466 ins_eol(c);
1467 else
1468 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001469 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001470 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001471 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001472 vim_free(str);
1473 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001474 }
1475
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001476 /* If the new value is already inserted or an empty string
1477 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001478 if (c == NUL)
1479 break;
1480 }
1481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482#ifdef FEAT_SMARTINDENT
1483 /* Try to perform smart-indenting. */
1484 ins_try_si(c);
1485#endif
1486
1487 if (c == ' ')
1488 {
1489 inserted_space = TRUE;
1490#ifdef FEAT_CINDENT
1491 if (inindent(0))
1492 can_cindent = FALSE;
1493#endif
1494 if (Insstart_blank_vcol == MAXCOL
1495 && curwin->w_cursor.lnum == Insstart.lnum)
1496 Insstart_blank_vcol = get_nolist_virtcol();
1497 }
1498
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001499 /* Insert a normal character and check for abbreviations on a
1500 * special character. Let CTRL-] expand abbreviations without
1501 * inserting it. */
1502 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503#ifdef FEAT_MBYTE
1504 /* Add ABBR_OFF for characters above 0x100, this is
1505 * what check_abbr() expects. */
1506 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1507#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001508 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001509 {
1510 insert_special(c, FALSE, FALSE);
1511#ifdef FEAT_RIGHTLEFT
1512 revins_legal++;
1513 revins_chars++;
1514#endif
1515 }
1516
1517 auto_format(FALSE, TRUE);
1518
1519#ifdef FEAT_FOLDING
1520 /* When inserting a character the cursor line must never be in a
1521 * closed fold. */
1522 foldOpenCursor();
1523#endif
1524 break;
1525 } /* end of switch (c) */
1526
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001527#ifdef FEAT_AUTOCMD
1528 /* If typed something may trigger CursorHoldI again. */
1529 if (c != K_CURSORHOLD)
1530 did_cursorhold = FALSE;
1531#endif
1532
Bram Moolenaar071d4272004-06-13 20:20:40 +00001533 /* If the cursor was moved we didn't just insert a space */
1534 if (arrow_used)
1535 inserted_space = FALSE;
1536
1537#ifdef FEAT_CINDENT
1538 if (can_cindent && cindent_on()
1539# ifdef FEAT_INS_EXPAND
1540 && ctrl_x_mode == 0
1541# endif
1542 )
1543 {
1544force_cindent:
1545 /*
1546 * Indent now if a key was typed that is in 'cinkeys'.
1547 */
1548 if (in_cinkeys(c, ' ', line_is_white))
1549 {
1550 if (stop_arrow() == OK)
1551 /* re-indent the current line */
1552 do_c_expr_indent();
1553 }
1554 }
1555#endif /* FEAT_CINDENT */
1556
1557 } /* for (;;) */
1558 /* NOTREACHED */
1559}
1560
1561/*
1562 * Redraw for Insert mode.
1563 * This is postponed until getting the next character to make '$' in the 'cpo'
1564 * option work correctly.
1565 * Only redraw when there are no characters available. This speeds up
1566 * inserting sequences of characters (e.g., for CTRL-R).
1567 */
1568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001569ins_redraw(
1570 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001572#ifdef FEAT_CONCEAL
1573 linenr_T conceal_old_cursor_line = 0;
1574 linenr_T conceal_new_cursor_line = 0;
1575 int conceal_update_lines = FALSE;
1576#endif
1577
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001578 if (char_avail())
1579 return;
1580
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001581#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001582 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1583 * visible, the command might delete it. */
1584 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001585# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001586 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001587# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001588# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001589 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001590# endif
1591# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001592 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001593# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001594 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001595# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001596 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001597# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001598# ifdef FEAT_INS_EXPAND
1599 && !pum_visible()
1600# endif
1601 )
1602 {
1603# ifdef FEAT_SYN_HL
1604 /* Need to update the screen first, to make sure syntax
1605 * highlighting is correct after making a change (e.g., inserting
1606 * a "(". The autocommand may also require a redraw, so it's done
1607 * again below, unfortunately. */
1608 if (syntax_present(curwin) && must_redraw)
1609 update_screen(0);
1610# endif
1611# ifdef FEAT_AUTOCMD
1612 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001613 {
1614 /* Make sure curswant is correct, an autocommand may call
1615 * getcurpos(). */
1616 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001617 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001618 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001619# endif
1620# ifdef FEAT_CONCEAL
1621 if (curwin->w_p_cole > 0)
1622 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001623# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001625# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626 conceal_new_cursor_line = curwin->w_cursor.lnum;
1627 conceal_update_lines = TRUE;
1628 }
1629# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001630# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001631 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001632# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001633 }
1634#endif
1635
1636#ifdef FEAT_AUTOCMD
1637 /* Trigger TextChangedI if b_changedtick differs. */
1638 if (ready && has_textchangedI()
1639 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001640# ifdef FEAT_INS_EXPAND
1641 && !pum_visible()
1642# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001643 )
1644 {
1645 if (last_changedtick_buf == curbuf)
1646 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1647 last_changedtick_buf = curbuf;
1648 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001650#endif
1651
1652 if (must_redraw)
1653 update_screen(0);
1654 else if (clear_cmdline || redraw_cmdline)
1655 showmode(); /* clear cmdline and show mode */
1656# if defined(FEAT_CONCEAL)
1657 if ((conceal_update_lines
1658 && (conceal_old_cursor_line != conceal_new_cursor_line
1659 || conceal_cursor_line(curwin)))
1660 || need_cursor_line_redraw)
1661 {
1662 if (conceal_old_cursor_line != conceal_new_cursor_line)
1663 update_single_line(curwin, conceal_old_cursor_line);
1664 update_single_line(curwin, conceal_new_cursor_line == 0
1665 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1666 curwin->w_valid &= ~VALID_CROW;
1667 }
1668# endif
1669 showruler(FALSE);
1670 setcursor();
1671 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672}
1673
1674/*
1675 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1676 */
1677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001678ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679{
1680 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001681 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682
1683 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001684 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685
1686 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001687 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001689 did_putchar = TRUE;
1690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1692
1693#ifdef FEAT_CMDL_INFO
1694 add_to_showcmd_c(Ctrl_V);
1695#endif
1696
1697 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001698 if (did_putchar)
1699 /* when the line fits in 'columns' the '^' is at the start of the next
1700 * line and will not removed by the redraw */
1701 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001702#ifdef FEAT_CMDL_INFO
1703 clear_showcmd();
1704#endif
1705 insert_special(c, FALSE, TRUE);
1706#ifdef FEAT_RIGHTLEFT
1707 revins_chars++;
1708 revins_legal++;
1709#endif
1710}
1711
1712/*
1713 * Put a character directly onto the screen. It's not stored in a buffer.
1714 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1715 */
1716static int pc_status;
1717#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1718#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1719#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1720#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001722static int pc_attr;
1723static int pc_row;
1724static int pc_col;
1725
1726 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001727edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001728{
1729 int attr;
1730
1731 if (ScreenLines != NULL)
1732 {
1733 update_topline(); /* just in case w_topline isn't valid */
1734 validate_cursor();
1735 if (highlight)
1736 attr = hl_attr(HLF_8);
1737 else
1738 attr = 0;
1739 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1740 pc_col = W_WINCOL(curwin);
1741#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1742 pc_status = PC_STATUS_UNSET;
1743#endif
1744#ifdef FEAT_RIGHTLEFT
1745 if (curwin->w_p_rl)
1746 {
1747 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1748# ifdef FEAT_MBYTE
1749 if (has_mbyte)
1750 {
1751 int fix_col = mb_fix_col(pc_col, pc_row);
1752
1753 if (fix_col != pc_col)
1754 {
1755 screen_putchar(' ', pc_row, fix_col, attr);
1756 --curwin->w_wcol;
1757 pc_status = PC_STATUS_RIGHT;
1758 }
1759 }
1760# endif
1761 }
1762 else
1763#endif
1764 {
1765 pc_col += curwin->w_wcol;
1766#ifdef FEAT_MBYTE
1767 if (mb_lefthalve(pc_row, pc_col))
1768 pc_status = PC_STATUS_LEFT;
1769#endif
1770 }
1771
1772 /* save the character to be able to put it back */
1773#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1774 if (pc_status == PC_STATUS_UNSET)
1775#endif
1776 {
1777 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1778 pc_status = PC_STATUS_SET;
1779 }
1780 screen_putchar(c, pc_row, pc_col, attr);
1781 }
1782}
1783
1784/*
1785 * Undo the previous edit_putchar().
1786 */
1787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
1790 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1791 {
1792#if defined(FEAT_MBYTE)
1793 if (pc_status == PC_STATUS_RIGHT)
1794 ++curwin->w_wcol;
1795 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1796 redrawWinline(curwin->w_cursor.lnum, FALSE);
1797 else
1798#endif
1799 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1800 }
1801}
1802
1803/*
1804 * Called when p_dollar is set: display a '$' at the end of the changed text
1805 * Only works when cursor is in the line that changes.
1806 */
1807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809{
1810 colnr_T save_col;
1811
1812 if (!redrawing())
1813 return;
1814
1815 cursor_off();
1816 save_col = curwin->w_cursor.col;
1817 curwin->w_cursor.col = col;
1818#ifdef FEAT_MBYTE
1819 if (has_mbyte)
1820 {
1821 char_u *p;
1822
1823 /* If on the last byte of a multi-byte move to the first byte. */
1824 p = ml_get_curline();
1825 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1826 }
1827#endif
1828 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1829 if (curwin->w_wcol < W_WIDTH(curwin))
1830 {
1831 edit_putchar('$', FALSE);
1832 dollar_vcol = curwin->w_virtcol;
1833 }
1834 curwin->w_cursor.col = save_col;
1835}
1836
1837/*
1838 * Call this function before moving the cursor from the normal insert position
1839 * in insert mode.
1840 */
1841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001842undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001844 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001845 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001846 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847 redrawWinline(curwin->w_cursor.lnum, FALSE);
1848 }
1849}
1850
1851/*
1852 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1853 * Keep the cursor on the same character.
1854 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1855 * type == INDENT_DEC decrease indent (for CTRL-D)
1856 * type == INDENT_SET set indent to "amount"
1857 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1858 */
1859 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001860change_indent(
1861 int type,
1862 int amount,
1863 int round,
1864 int replaced, /* replaced character, put on replace stack */
1865 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866{
1867 int vcol;
1868 int last_vcol;
1869 int insstart_less; /* reduction for Insstart.col */
1870 int new_cursor_col;
1871 int i;
1872 char_u *ptr;
1873 int save_p_list;
1874 int start_col;
1875 colnr_T vc;
1876#ifdef FEAT_VREPLACE
1877 colnr_T orig_col = 0; /* init for GCC */
1878 char_u *new_line, *orig_line = NULL; /* init for GCC */
1879
1880 /* VREPLACE mode needs to know what the line was like before changing */
1881 if (State & VREPLACE_FLAG)
1882 {
1883 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1884 orig_col = curwin->w_cursor.col;
1885 }
1886#endif
1887
1888 /* for the following tricks we don't want list mode */
1889 save_p_list = curwin->w_p_list;
1890 curwin->w_p_list = FALSE;
1891 vc = getvcol_nolist(&curwin->w_cursor);
1892 vcol = vc;
1893
1894 /*
1895 * For Replace mode we need to fix the replace stack later, which is only
1896 * possible when the cursor is in the indent. Remember the number of
1897 * characters before the cursor if it's possible.
1898 */
1899 start_col = curwin->w_cursor.col;
1900
1901 /* determine offset from first non-blank */
1902 new_cursor_col = curwin->w_cursor.col;
1903 beginline(BL_WHITE);
1904 new_cursor_col -= curwin->w_cursor.col;
1905
1906 insstart_less = curwin->w_cursor.col;
1907
1908 /*
1909 * If the cursor is in the indent, compute how many screen columns the
1910 * cursor is to the left of the first non-blank.
1911 */
1912 if (new_cursor_col < 0)
1913 vcol = get_indent() - vcol;
1914
1915 if (new_cursor_col > 0) /* can't fix replace stack */
1916 start_col = -1;
1917
1918 /*
1919 * Set the new indent. The cursor will be put on the first non-blank.
1920 */
1921 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001922 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 else
1924 {
1925#ifdef FEAT_VREPLACE
1926 int save_State = State;
1927
1928 /* Avoid being called recursively. */
1929 if (State & VREPLACE_FLAG)
1930 State = INSERT;
1931#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001932 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933#ifdef FEAT_VREPLACE
1934 State = save_State;
1935#endif
1936 }
1937 insstart_less -= curwin->w_cursor.col;
1938
1939 /*
1940 * Try to put cursor on same character.
1941 * If the cursor is at or after the first non-blank in the line,
1942 * compute the cursor column relative to the column of the first
1943 * non-blank character.
1944 * If we are not in insert mode, leave the cursor on the first non-blank.
1945 * If the cursor is before the first non-blank, position it relative
1946 * to the first non-blank, counted in screen columns.
1947 */
1948 if (new_cursor_col >= 0)
1949 {
1950 /*
1951 * When changing the indent while the cursor is touching it, reset
1952 * Insstart_col to 0.
1953 */
1954 if (new_cursor_col == 0)
1955 insstart_less = MAXCOL;
1956 new_cursor_col += curwin->w_cursor.col;
1957 }
1958 else if (!(State & INSERT))
1959 new_cursor_col = curwin->w_cursor.col;
1960 else
1961 {
1962 /*
1963 * Compute the screen column where the cursor should be.
1964 */
1965 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001966 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967
1968 /*
1969 * Advance the cursor until we reach the right screen column.
1970 */
1971 vcol = last_vcol = 0;
1972 new_cursor_col = -1;
1973 ptr = ml_get_curline();
1974 while (vcol <= (int)curwin->w_virtcol)
1975 {
1976 last_vcol = vcol;
1977#ifdef FEAT_MBYTE
1978 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001979 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 else
1981#endif
1982 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001983 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 }
1985 vcol = last_vcol;
1986
1987 /*
1988 * May need to insert spaces to be able to position the cursor on
1989 * the right screen column.
1990 */
1991 if (vcol != (int)curwin->w_virtcol)
1992 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001993 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001994 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001995 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 if (ptr != NULL)
1997 {
1998 new_cursor_col += i;
1999 ptr[i] = NUL;
2000 while (--i >= 0)
2001 ptr[i] = ' ';
2002 ins_str(ptr);
2003 vim_free(ptr);
2004 }
2005 }
2006
2007 /*
2008 * When changing the indent while the cursor is in it, reset
2009 * Insstart_col to 0.
2010 */
2011 insstart_less = MAXCOL;
2012 }
2013
2014 curwin->w_p_list = save_p_list;
2015
2016 if (new_cursor_col <= 0)
2017 curwin->w_cursor.col = 0;
2018 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002019 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002020 curwin->w_set_curswant = TRUE;
2021 changed_cline_bef_curs();
2022
2023 /*
2024 * May have to adjust the start of the insert.
2025 */
2026 if (State & INSERT)
2027 {
2028 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2029 {
2030 if ((int)Insstart.col <= insstart_less)
2031 Insstart.col = 0;
2032 else
2033 Insstart.col -= insstart_less;
2034 }
2035 if ((int)ai_col <= insstart_less)
2036 ai_col = 0;
2037 else
2038 ai_col -= insstart_less;
2039 }
2040
2041 /*
2042 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2043 * If the number of characters before the cursor decreased, need to pop a
2044 * few characters from the replace stack.
2045 * If the number of characters before the cursor increased, need to push a
2046 * few NULs onto the replace stack.
2047 */
2048 if (REPLACE_NORMAL(State) && start_col >= 0)
2049 {
2050 while (start_col > (int)curwin->w_cursor.col)
2051 {
2052 replace_join(0); /* remove a NUL from the replace stack */
2053 --start_col;
2054 }
2055 while (start_col < (int)curwin->w_cursor.col || replaced)
2056 {
2057 replace_push(NUL);
2058 if (replaced)
2059 {
2060 replace_push(replaced);
2061 replaced = NUL;
2062 }
2063 ++start_col;
2064 }
2065 }
2066
2067#ifdef FEAT_VREPLACE
2068 /*
2069 * For VREPLACE mode, we also have to fix the replace stack. In this case
2070 * it is always possible because we backspace over the whole line and then
2071 * put it back again the way we wanted it.
2072 */
2073 if (State & VREPLACE_FLAG)
2074 {
2075 /* If orig_line didn't allocate, just return. At least we did the job,
2076 * even if you can't backspace. */
2077 if (orig_line == NULL)
2078 return;
2079
2080 /* Save new line */
2081 new_line = vim_strsave(ml_get_curline());
2082 if (new_line == NULL)
2083 return;
2084
2085 /* We only put back the new line up to the cursor */
2086 new_line[curwin->w_cursor.col] = NUL;
2087
2088 /* Put back original line */
2089 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2090 curwin->w_cursor.col = orig_col;
2091
2092 /* Backspace from cursor to start of line */
2093 backspace_until_column(0);
2094
2095 /* Insert new stuff into line again */
2096 ins_bytes(new_line);
2097
2098 vim_free(new_line);
2099 }
2100#endif
2101}
2102
2103/*
2104 * Truncate the space at the end of a line. This is to be used only in an
2105 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2106 * modes.
2107 */
2108 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002109truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110{
2111 int i;
2112
2113 /* find start of trailing white space */
2114 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2115 {
2116 if (State & REPLACE_FLAG)
2117 replace_join(0); /* remove a NUL from the replace stack */
2118 }
2119 line[i + 1] = NUL;
2120}
2121
2122#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2123 || defined(FEAT_COMMENTS) || defined(PROTO)
2124/*
2125 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2126 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002127 * Will attempt not to go before "col" even when there is a composing
2128 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 */
2130 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002131backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002132{
2133 while ((int)curwin->w_cursor.col > col)
2134 {
2135 curwin->w_cursor.col--;
2136 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002137 replace_do_bs(col);
2138 else if (!del_char_after_col(col))
2139 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
2141}
2142#endif
2143
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002144/*
2145 * Like del_char(), but make sure not to go before column "limit_col".
2146 * Only matters when there are composing characters.
2147 * Return TRUE when something was deleted.
2148 */
2149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002150del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002151{
2152#ifdef FEAT_MBYTE
2153 if (enc_utf8 && limit_col >= 0)
2154 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002155 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002156
2157 /* Make sure the cursor is at the start of a character, but
2158 * skip forward again when going too far back because of a
2159 * composing character. */
2160 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002161 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002162 {
2163 int l = utf_ptr2len(ml_get_cursor());
2164
2165 if (l == 0) /* end of line */
2166 break;
2167 curwin->w_cursor.col += l;
2168 }
2169 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2170 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002171 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002172 }
2173 else
2174#endif
2175 (void)del_char(FALSE);
2176 return TRUE;
2177}
2178
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2180/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002181 * CTRL-X pressed in Insert mode.
2182 */
2183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002184ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002185{
2186 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2187 * CTRL-V works like CTRL-N */
2188 if (ctrl_x_mode != CTRL_X_CMDLINE)
2189 {
2190 /* if the next ^X<> won't ADD nothing, then reset
2191 * compl_cont_status */
2192 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002193 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002194 else
2195 compl_cont_status = 0;
2196 /* We're not sure which CTRL-X mode it will be yet */
2197 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2198 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2199 edit_submode_pre = NULL;
2200 showmode();
2201 }
2202}
2203
2204/*
2205 * Return TRUE if the 'dict' or 'tsr' option can be used.
2206 */
2207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002209{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002210 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002211# ifdef FEAT_SPELL
2212 && !curwin->w_p_spell
2213# endif
2214 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002215 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2216 {
2217 ctrl_x_mode = 0;
2218 edit_submode = NULL;
2219 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2220 : (char_u *)_("'thesaurus' option is empty"),
2221 hl_attr(HLF_E));
2222 if (emsg_silent == 0)
2223 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002224 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002225 setcursor();
2226 out_flush();
2227 ui_delay(2000L, FALSE);
2228 }
2229 return FALSE;
2230 }
2231 return TRUE;
2232}
2233
2234/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2236 * This depends on the current mode.
2237 */
2238 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240{
2241 /* Always allow ^R - let it's results then be checked */
2242 if (c == Ctrl_R)
2243 return TRUE;
2244
Bram Moolenaare3226be2005-12-18 22:10:00 +00002245 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002246 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002247 return TRUE;
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 switch (ctrl_x_mode)
2250 {
2251 case 0: /* Not in any CTRL-X mode */
2252 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2253 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002254 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2256 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2257 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002258 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002259 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 case CTRL_X_SCROLL:
2261 return (c == Ctrl_Y || c == Ctrl_E);
2262 case CTRL_X_WHOLE_LINE:
2263 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2264 case CTRL_X_FILES:
2265 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2266 case CTRL_X_DICTIONARY:
2267 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2268 case CTRL_X_THESAURUS:
2269 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2270 case CTRL_X_TAGS:
2271 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2272#ifdef FEAT_FIND_ID
2273 case CTRL_X_PATH_PATTERNS:
2274 return (c == Ctrl_P || c == Ctrl_N);
2275 case CTRL_X_PATH_DEFINES:
2276 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2277#endif
2278 case CTRL_X_CMDLINE:
2279 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2280 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002281#ifdef FEAT_COMPL_FUNC
2282 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002283 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002284 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002285 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002286#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002287 case CTRL_X_SPELL:
2288 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002289 case CTRL_X_EVAL:
2290 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292 EMSG(_(e_internal));
2293 return FALSE;
2294}
2295
2296/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002297 * Return TRUE when character "c" is part of the item currently being
2298 * completed. Used to decide whether to abandon complete mode when the menu
2299 * is visible.
2300 */
2301 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002302ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002303{
2304 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2305 /* When expanding an identifier only accept identifier chars. */
2306 return vim_isIDc(c);
2307
2308 switch (ctrl_x_mode)
2309 {
2310 case CTRL_X_FILES:
2311 /* When expanding file name only accept file name chars. But not
2312 * path separators, so that "proto/<Tab>" expands files in
2313 * "proto", not "proto/" as a whole */
2314 return vim_isfilec(c) && !vim_ispathsep(c);
2315
2316 case CTRL_X_CMDLINE:
2317 case CTRL_X_OMNI:
2318 /* Command line and Omni completion can work with just about any
2319 * printable character, but do stop at white space. */
2320 return vim_isprintc(c) && !vim_iswhite(c);
2321
2322 case CTRL_X_WHOLE_LINE:
2323 /* For while line completion a space can be part of the line. */
2324 return vim_isprintc(c);
2325 }
2326 return vim_iswordc(c);
2327}
2328
2329/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002330 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002332 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333 * the rest of the word to be in -- webb
2334 */
2335 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002336ins_compl_add_infercase(
2337 char_u *str,
2338 int len,
2339 int icase,
2340 char_u *fname,
2341 int dir,
2342 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002344 char_u *p;
2345 int i, c;
2346 int actual_len; /* Take multi-byte characters */
2347 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002348 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002349 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 int has_lower = FALSE;
2351 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002353 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002354 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002355 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
Bram Moolenaara2993e12007-08-12 14:38:46 +00002357 /* Find actual length of completion. */
2358#ifdef FEAT_MBYTE
2359 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002361 p = str;
2362 actual_len = 0;
2363 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002365 mb_ptr_adv(p);
2366 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002369 else
2370#endif
2371 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
Bram Moolenaara2993e12007-08-12 14:38:46 +00002373 /* Find actual length of original text. */
2374#ifdef FEAT_MBYTE
2375 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002377 p = compl_orig_text;
2378 actual_compl_length = 0;
2379 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002381 mb_ptr_adv(p);
2382 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 }
2384 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002385 else
2386#endif
2387 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002389 /* "actual_len" may be smaller than "actual_compl_length" when using
2390 * thesaurus, only use the minimum when comparing. */
2391 min_len = actual_len < actual_compl_length
2392 ? actual_len : actual_compl_length;
2393
Bram Moolenaara2993e12007-08-12 14:38:46 +00002394 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002395 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002396 if (wca != NULL)
2397 {
2398 p = str;
2399 for (i = 0; i < actual_len; ++i)
2400#ifdef FEAT_MBYTE
2401 if (has_mbyte)
2402 wca[i] = mb_ptr2char_adv(&p);
2403 else
2404#endif
2405 wca[i] = *(p++);
2406
2407 /* Rule 1: Were any chars converted to lower? */
2408 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002409 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002410 {
2411#ifdef FEAT_MBYTE
2412 if (has_mbyte)
2413 c = mb_ptr2char_adv(&p);
2414 else
2415#endif
2416 c = *(p++);
2417 if (MB_ISLOWER(c))
2418 {
2419 has_lower = TRUE;
2420 if (MB_ISUPPER(wca[i]))
2421 {
2422 /* Rule 1 is satisfied. */
2423 for (i = actual_compl_length; i < actual_len; ++i)
2424 wca[i] = MB_TOLOWER(wca[i]);
2425 break;
2426 }
2427 }
2428 }
2429
2430 /*
2431 * Rule 2: No lower case, 2nd consecutive letter converted to
2432 * upper case.
2433 */
2434 if (!has_lower)
2435 {
2436 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002437 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002438 {
2439#ifdef FEAT_MBYTE
2440 if (has_mbyte)
2441 c = mb_ptr2char_adv(&p);
2442 else
2443#endif
2444 c = *(p++);
2445 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2446 {
2447 /* Rule 2 is satisfied. */
2448 for (i = actual_compl_length; i < actual_len; ++i)
2449 wca[i] = MB_TOUPPER(wca[i]);
2450 break;
2451 }
2452 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2453 }
2454 }
2455
2456 /* Copy the original case of the part we typed. */
2457 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002458 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002459 {
2460#ifdef FEAT_MBYTE
2461 if (has_mbyte)
2462 c = mb_ptr2char_adv(&p);
2463 else
2464#endif
2465 c = *(p++);
2466 if (MB_ISLOWER(c))
2467 wca[i] = MB_TOLOWER(wca[i]);
2468 else if (MB_ISUPPER(c))
2469 wca[i] = MB_TOUPPER(wca[i]);
2470 }
2471
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002472 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002473 * Generate encoding specific output from wide character array.
2474 * Multi-byte characters can occupy up to five bytes more than
2475 * ASCII characters, and we also need one byte for NUL, so stay
2476 * six bytes away from the edge of IObuff.
2477 */
2478 p = IObuff;
2479 i = 0;
2480 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2481#ifdef FEAT_MBYTE
2482 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002483 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002484 else
2485#endif
2486 *(p++) = wca[i++];
2487 *p = NUL;
2488
2489 vim_free(wca);
2490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002492 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2493 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002495 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496}
2497
2498/*
2499 * Add a match to the list of matches.
2500 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002501 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002502 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002505ins_compl_add(
2506 char_u *str,
2507 int len,
2508 int icase,
2509 char_u *fname,
2510 char_u **cptext, /* extra text for popup menu or NULL */
2511 int cdir,
2512 int flags,
2513 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002515 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002516 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002517
2518 ui_breakcheck();
2519 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002520 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 if (len < 0)
2522 len = (int)STRLEN(str);
2523
2524 /*
2525 * If the same match is already present, don't add it.
2526 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002527 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002529 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002530 do
2531 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002532 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002533 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002534 && match->cp_str[len] == NUL)
2535 return NOTDONE;
2536 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002537 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002540 /* Remove any popup menu before changing the list of matches. */
2541 ins_compl_del_pum();
2542
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 /*
2544 * Allocate a new match structure.
2545 * Copy the values to the new match structure.
2546 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002547 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002549 return FAIL;
2550 match->cp_number = -1;
2551 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002552 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002553 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 {
2555 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002556 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002558 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002559
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002561 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002562 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2563 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002564 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002565 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002566 && compl_curr_match->cp_fname != NULL
2567 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002568 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002569 else if (fname != NULL)
2570 {
2571 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002572 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002573 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002574 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002575 match->cp_fname = NULL;
2576 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002577
2578 if (cptext != NULL)
2579 {
2580 int i;
2581
2582 for (i = 0; i < CPT_COUNT; ++i)
2583 if (cptext[i] != NULL && *cptext[i] != NUL)
2584 match->cp_text[i] = vim_strsave(cptext[i]);
2585 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
2587 /*
2588 * Link the new match structure in the list of matches.
2589 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002590 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002591 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 else if (dir == FORWARD)
2593 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002594 match->cp_next = compl_curr_match->cp_next;
2595 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 }
2597 else /* BACKWARD */
2598 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002599 match->cp_next = compl_curr_match;
2600 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002601 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002602 if (match->cp_next)
2603 match->cp_next->cp_prev = match;
2604 if (match->cp_prev)
2605 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002607 compl_first_match = match;
2608 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002610 /*
2611 * Find the longest common string if still doing that.
2612 */
2613 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2614 ins_compl_longest_match(match);
2615
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 return OK;
2617}
2618
2619/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002620 * Return TRUE if "str[len]" matches with match->cp_str, considering
2621 * match->cp_icase.
2622 */
2623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002624ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002625{
2626 if (match->cp_icase)
2627 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2628 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2629}
2630
2631/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002632 * Reduce the longest common string for match "match".
2633 */
2634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002635ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002636{
2637 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002638 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002639 int had_match;
2640
2641 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002642 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002643 /* First match, use it as a whole. */
2644 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002645 if (compl_leader != NULL)
2646 {
2647 had_match = (curwin->w_cursor.col > compl_col);
2648 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002649 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002650 ins_redraw(FALSE);
2651
2652 /* When the match isn't there (to avoid matching itself) remove it
2653 * again after redrawing. */
2654 if (!had_match)
2655 ins_compl_delete();
2656 compl_used_match = FALSE;
2657 }
2658 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002659 else
2660 {
2661 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002662 p = compl_leader;
2663 s = match->cp_str;
2664 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002665 {
2666#ifdef FEAT_MBYTE
2667 if (has_mbyte)
2668 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002669 c1 = mb_ptr2char(p);
2670 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002671 }
2672 else
2673#endif
2674 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002675 c1 = *p;
2676 c2 = *s;
2677 }
2678 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2679 : (c1 != c2))
2680 break;
2681#ifdef FEAT_MBYTE
2682 if (has_mbyte)
2683 {
2684 mb_ptr_adv(p);
2685 mb_ptr_adv(s);
2686 }
2687 else
2688#endif
2689 {
2690 ++p;
2691 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002692 }
2693 }
2694
2695 if (*p != NUL)
2696 {
2697 /* Leader was shortened, need to change the inserted text. */
2698 *p = NUL;
2699 had_match = (curwin->w_cursor.col > compl_col);
2700 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002701 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002702 ins_redraw(FALSE);
2703
2704 /* When the match isn't there (to avoid matching itself) remove it
2705 * again after redrawing. */
2706 if (!had_match)
2707 ins_compl_delete();
2708 }
2709
2710 compl_used_match = FALSE;
2711 }
2712}
2713
2714/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 * Add an array of matches to the list of matches.
2716 * Frees matches[].
2717 */
2718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002719ins_compl_add_matches(
2720 int num_matches,
2721 char_u **matches,
2722 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002723{
2724 int i;
2725 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002726 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727
Bram Moolenaar572cb562005-08-05 21:35:02 +00002728 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002729 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002730 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002732 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002733 FreeWild(num_matches, matches);
2734}
2735
2736/* Make the completion list cyclic.
2737 * Return the number of matches (excluding the original).
2738 */
2739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002740ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002742 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 int count = 0;
2744
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002745 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002746 {
2747 /*
2748 * Find the end of the list.
2749 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002750 match = compl_first_match;
2751 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002752 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002753 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002754 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755 ++count;
2756 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002757 match->cp_next = compl_first_match;
2758 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 }
2760 return count;
2761}
2762
Bram Moolenaara94bc432006-03-10 21:42:59 +00002763/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002764 * Set variables that store noselect and noinsert behavior from the
2765 * 'completeopt' value.
2766 */
2767 void
2768completeopt_was_set()
2769{
2770 compl_no_insert = FALSE;
2771 compl_no_select = FALSE;
2772 if (strstr((char *)p_cot, "noselect") != NULL)
2773 compl_no_select = TRUE;
2774 if (strstr((char *)p_cot, "noinsert") != NULL)
2775 compl_no_insert = TRUE;
2776}
2777
2778/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002779 * Start completion for the complete() function.
2780 * "startcol" is where the matched text starts (1 is first column).
2781 * "list" is the list of matches.
2782 */
2783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002784set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002785{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002786 int save_w_wrow = curwin->w_wrow;
2787
Bram Moolenaara94bc432006-03-10 21:42:59 +00002788 /* If already doing completions stop it. */
2789 if (ctrl_x_mode != 0)
2790 ins_compl_prep(' ');
2791 ins_compl_clear();
2792
2793 if (stop_arrow() == FAIL)
2794 return;
2795
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002796 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002797 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002798 startcol = curwin->w_cursor.col;
2799 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002800 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002801 /* compl_pattern doesn't need to be set */
2802 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2803 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002804 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002805 return;
2806
Bram Moolenaare4214502015-03-05 18:08:43 +01002807 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002808
2809 ins_compl_add_list(list);
2810 compl_matches = ins_compl_make_cyclic();
2811 compl_started = TRUE;
2812 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002813 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002814
2815 compl_curr_match = compl_first_match;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002816 if (compl_no_insert)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002817 ins_complete(K_DOWN, FALSE);
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002818 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002819 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar2cf69382015-07-17 13:42:23 +02002820 if (compl_no_select)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002821 ins_complete(Ctrl_P, FALSE);
2822
2823 /* Lazily show the popup menu, unless we got interrupted. */
2824 if (!compl_interrupted)
2825 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002826 out_flush();
2827}
2828
2829
Bram Moolenaar9372a112005-12-06 19:59:18 +00002830/* "compl_match_array" points the currently displayed list of entries in the
2831 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002832static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002833static int compl_match_arraysize;
2834
2835/*
2836 * Update the screen and when there is any scrolling remove the popup menu.
2837 */
2838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002839ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002840{
2841 int h;
2842
2843 if (compl_match_array != NULL)
2844 {
2845 h = curwin->w_cline_height;
2846 update_screen(0);
2847 if (h != curwin->w_cline_height)
2848 ins_compl_del_pum();
2849 }
2850}
2851
2852/*
2853 * Remove any popup menu.
2854 */
2855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002856ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002857{
2858 if (compl_match_array != NULL)
2859 {
2860 pum_undisplay();
2861 vim_free(compl_match_array);
2862 compl_match_array = NULL;
2863 }
2864}
2865
2866/*
2867 * Return TRUE if the popup menu should be displayed.
2868 */
2869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002870pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002871{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002872 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002873 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874 return FALSE;
2875
2876 /* The display looks bad on a B&W display. */
2877 if (t_colors < 8
2878#ifdef FEAT_GUI
2879 && !gui.in_use
2880#endif
2881 )
2882 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002883 return TRUE;
2884}
2885
2886/*
2887 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002888 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002889 */
2890 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002891pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002892{
2893 compl_T *compl;
2894 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002895
2896 /* Don't display the popup menu if there are no matches or there is only
2897 * one (ignoring the original text). */
2898 compl = compl_first_match;
2899 i = 0;
2900 do
2901 {
2902 if (compl == NULL
2903 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2904 break;
2905 compl = compl->cp_next;
2906 } while (compl != compl_first_match);
2907
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002908 if (strstr((char *)p_cot, "menuone") != NULL)
2909 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002910 return (i >= 2);
2911}
2912
2913/*
2914 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002915 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002916 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002918ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002919{
2920 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002921 compl_T *shown_compl = NULL;
2922 int did_find_shown_match = FALSE;
2923 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002924 int i;
2925 int cur = -1;
2926 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002927 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002928
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002929 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002930 return;
2931
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002932#if defined(FEAT_EVAL)
2933 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2934 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2935#endif
2936
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002937 /* Update the screen before drawing the popup menu over it. */
2938 update_screen(0);
2939
2940 if (compl_match_array == NULL)
2941 {
2942 /* Need to build the popup menu list. */
2943 compl_match_arraysize = 0;
2944 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002945 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002946 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002947 do
2948 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002949 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2950 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002951 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002952 ++compl_match_arraysize;
2953 compl = compl->cp_next;
2954 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002955 if (compl_match_arraysize == 0)
2956 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002957 compl_match_array = (pumitem_T *)alloc_clear(
2958 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002959 * compl_match_arraysize));
2960 if (compl_match_array != NULL)
2961 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002962 /* If the current match is the original text don't find the first
2963 * match after it, don't highlight anything. */
2964 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2965 shown_match_ok = TRUE;
2966
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002967 i = 0;
2968 compl = compl_first_match;
2969 do
2970 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002971 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2972 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002973 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002974 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002975 if (!shown_match_ok)
2976 {
2977 if (compl == compl_shown_match || did_find_shown_match)
2978 {
2979 /* This item is the shown match or this is the
2980 * first displayed item after the shown match. */
2981 compl_shown_match = compl;
2982 did_find_shown_match = TRUE;
2983 shown_match_ok = TRUE;
2984 }
2985 else
2986 /* Remember this displayed match for when the
2987 * shown match is just below it. */
2988 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002989 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002990 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002991
2992 if (compl->cp_text[CPT_ABBR] != NULL)
2993 compl_match_array[i].pum_text =
2994 compl->cp_text[CPT_ABBR];
2995 else
2996 compl_match_array[i].pum_text = compl->cp_str;
2997 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2998 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2999 if (compl->cp_text[CPT_MENU] != NULL)
3000 compl_match_array[i++].pum_extra =
3001 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003002 else
3003 compl_match_array[i++].pum_extra = compl->cp_fname;
3004 }
3005
3006 if (compl == compl_shown_match)
3007 {
3008 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003009
3010 /* When the original text is the shown match don't set
3011 * compl_shown_match. */
3012 if (compl->cp_flags & ORIGINAL_TEXT)
3013 shown_match_ok = TRUE;
3014
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003015 if (!shown_match_ok && shown_compl != NULL)
3016 {
3017 /* The shown match isn't displayed, set it to the
3018 * previously displayed match. */
3019 compl_shown_match = shown_compl;
3020 shown_match_ok = TRUE;
3021 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003022 }
3023 compl = compl->cp_next;
3024 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003025
3026 if (!shown_match_ok) /* no displayed match at all */
3027 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003028 }
3029 }
3030 else
3031 {
3032 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003033 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003034 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3035 || compl_match_array[i].pum_text
3036 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003037 {
3038 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003039 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003040 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003041 }
3042
3043 if (compl_match_array != NULL)
3044 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003045 /* In Replace mode when a $ is displayed at the end of the line only
3046 * part of the screen would be updated. We do need to redraw here. */
3047 dollar_vcol = -1;
3048
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003049 /* Compute the screen column of the start of the completed text.
3050 * Use the cursor to get all wrapping and other settings right. */
3051 col = curwin->w_cursor.col;
3052 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003053 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003054 curwin->w_cursor.col = col;
3055 }
3056}
3057
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058#define DICT_FIRST (1) /* use just first element in "dict" */
3059#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003060
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003062 * Add any identifiers that match the given pattern in the list of dictionary
3063 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 */
3065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003066ins_compl_dictionaries(
3067 char_u *dict_start,
3068 char_u *pat,
3069 int flags, /* DICT_FIRST and/or DICT_EXACT */
3070 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003071{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003072 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 char_u *ptr;
3074 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 char_u **files;
3077 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003079 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080
Bram Moolenaar0b238792006-03-02 22:49:12 +00003081 if (*dict == NUL)
3082 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003083#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003084 /* When 'dictionary' is empty and spell checking is enabled use
3085 * "spell". */
3086 if (!thesaurus && curwin->w_p_spell)
3087 dict = (char_u *)"spell";
3088 else
3089#endif
3090 return;
3091 }
3092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003094 if (buf == NULL)
3095 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003096 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003097
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 /* If 'infercase' is set, don't use 'smartcase' here */
3099 save_p_scs = p_scs;
3100 if (curbuf->b_p_inf)
3101 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003102
3103 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3104 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003105 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003106 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003107 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003108 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003109 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003110
3111 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003112 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003113 len = STRLEN(pat_esc) + 10;
3114 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003115 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003116 {
3117 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003118 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003119 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003120 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003121 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3122 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003123 vim_free(ptr);
3124 }
3125 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003126 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003127 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003128 if (regmatch.regprog == NULL)
3129 goto theend;
3130 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003131
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3133 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003134 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 {
3136 /* copy one dictionary file name into buf */
3137 if (flags == DICT_EXACT)
3138 {
3139 count = 1;
3140 files = &dict;
3141 }
3142 else
3143 {
3144 /* Expand wildcards in the dictionary name, but do not allow
3145 * backticks (for security, the 'dict' option may have been set in
3146 * a modeline). */
3147 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003148# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003149 if (!thesaurus && STRCMP(buf, "spell") == 0)
3150 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003151 else
3152# endif
3153 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 || expand_wildcards(1, &buf, &count, &files,
3155 EW_FILE|EW_SILENT) != OK)
3156 count = 0;
3157 }
3158
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003159# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003160 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003162 /* Complete from active spelling. Skip "\<" in the pattern, we
3163 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003164 if (pat[0] == '\\' && pat[1] == '<')
3165 ptr = pat + 2;
3166 else
3167 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003168 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003170 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003171# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003172 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003173 {
3174 ins_compl_files(count, files, thesaurus, flags,
3175 &regmatch, buf, &dir);
3176 if (flags != DICT_EXACT)
3177 FreeWild(count, files);
3178 }
3179 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003180 break;
3181 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003182
3183theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003185 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 vim_free(buf);
3187}
3188
Bram Moolenaar0b238792006-03-02 22:49:12 +00003189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003190ins_compl_files(
3191 int count,
3192 char_u **files,
3193 int thesaurus,
3194 int flags,
3195 regmatch_T *regmatch,
3196 char_u *buf,
3197 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003198{
3199 char_u *ptr;
3200 int i;
3201 FILE *fp;
3202 int add_r;
3203
3204 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3205 {
3206 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3207 if (flags != DICT_EXACT)
3208 {
3209 vim_snprintf((char *)IObuff, IOSIZE,
3210 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003211 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003212 }
3213
3214 if (fp != NULL)
3215 {
3216 /*
3217 * Read dictionary file line by line.
3218 * Check each line for a match.
3219 */
3220 while (!got_int && !compl_interrupted
3221 && !vim_fgets(buf, LSIZE, fp))
3222 {
3223 ptr = buf;
3224 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3225 {
3226 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003227 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003228 ptr = find_line_end(ptr);
3229 else
3230 ptr = find_word_end(ptr);
3231 add_r = ins_compl_add_infercase(regmatch->startp[0],
3232 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003233 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003234 if (thesaurus)
3235 {
3236 char_u *wstart;
3237
3238 /*
3239 * Add the other matches on the line
3240 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003241 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003242 while (!got_int)
3243 {
3244 /* Find start of the next word. Skip white
3245 * space and punctuation. */
3246 ptr = find_word_start(ptr);
3247 if (*ptr == NUL || *ptr == NL)
3248 break;
3249 wstart = ptr;
3250
Bram Moolenaara2993e12007-08-12 14:38:46 +00003251 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003252#ifdef FEAT_MBYTE
3253 if (has_mbyte)
3254 /* Japanese words may have characters in
3255 * different classes, only separate words
3256 * with single-byte non-word characters. */
3257 while (*ptr != NUL)
3258 {
3259 int l = (*mb_ptr2len)(ptr);
3260
3261 if (l < 2 && !vim_iswordc(*ptr))
3262 break;
3263 ptr += l;
3264 }
3265 else
3266#endif
3267 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003268
3269 /* Add the word. Skip the regexp match. */
3270 if (wstart != regmatch->startp[0])
3271 add_r = ins_compl_add_infercase(wstart,
3272 (int)(ptr - wstart),
3273 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003274 }
3275 }
3276 if (add_r == OK)
3277 /* if dir was BACKWARD then honor it just once */
3278 *dir = FORWARD;
3279 else if (add_r == FAIL)
3280 break;
3281 /* avoid expensive call to vim_regexec() when at end
3282 * of line */
3283 if (*ptr == '\n' || got_int)
3284 break;
3285 }
3286 line_breakcheck();
3287 ins_compl_check_keys(50);
3288 }
3289 fclose(fp);
3290 }
3291 }
3292}
3293
Bram Moolenaar071d4272004-06-13 20:20:40 +00003294/*
3295 * Find the start of the next word.
3296 * Returns a pointer to the first char of the word. Also stops at a NUL.
3297 */
3298 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003299find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300{
3301#ifdef FEAT_MBYTE
3302 if (has_mbyte)
3303 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003304 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003305 else
3306#endif
3307 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3308 ++ptr;
3309 return ptr;
3310}
3311
3312/*
3313 * Find the end of the word. Assumes it starts inside a word.
3314 * Returns a pointer to just after the word.
3315 */
3316 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003317find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318{
3319#ifdef FEAT_MBYTE
3320 int start_class;
3321
3322 if (has_mbyte)
3323 {
3324 start_class = mb_get_class(ptr);
3325 if (start_class > 1)
3326 while (*ptr != NUL)
3327 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003328 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329 if (mb_get_class(ptr) != start_class)
3330 break;
3331 }
3332 }
3333 else
3334#endif
3335 while (vim_iswordc(*ptr))
3336 ++ptr;
3337 return ptr;
3338}
3339
3340/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003341 * Find the end of the line, omitting CR and NL at the end.
3342 * Returns a pointer to just after the line.
3343 */
3344 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003345find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003346{
3347 char_u *s;
3348
3349 s = ptr + STRLEN(ptr);
3350 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3351 --s;
3352 return s;
3353}
3354
3355/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003356 * Free the list of completions
3357 */
3358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003359ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003361 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003362 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003364 vim_free(compl_pattern);
3365 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003366 vim_free(compl_leader);
3367 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003368
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003369 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003371
3372 ins_compl_del_pum();
3373 pum_clear();
3374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003375 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003376 do
3377 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003378 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003379 compl_curr_match = compl_curr_match->cp_next;
3380 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003382 if (match->cp_flags & FREE_FNAME)
3383 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003384 for (i = 0; i < CPT_COUNT; ++i)
3385 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003387 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3388 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003389 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390}
3391
3392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003393ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003394{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003395 compl_cont_status = 0;
3396 compl_started = FALSE;
3397 compl_matches = 0;
3398 vim_free(compl_pattern);
3399 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003400 vim_free(compl_leader);
3401 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003403 vim_free(compl_orig_text);
3404 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003405 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003406 /* clear v:completed_item */
3407 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003408}
3409
3410/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003411 * Return TRUE when Insert completion is active.
3412 */
3413 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003414ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003415{
3416 return compl_started;
3417}
3418
3419/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003420 * Delete one character before the cursor and show the subset of the matches
3421 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003422 * Returns the character to be used, NUL if the work is done and another char
3423 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003424 */
3425 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003426ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003427{
3428 char_u *line;
3429 char_u *p;
3430
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003431 line = ml_get_curline();
3432 p = line + curwin->w_cursor.col;
3433 mb_ptr_back(line, p);
3434
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003435 /* Stop completion when the whole word was deleted. For Omni completion
3436 * allow the word to be deleted, we won't match everything. */
3437 if ((int)(p - line) - (int)compl_col < 0
3438 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003439 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003440 return K_BS;
3441
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003442 /* Deleted more than what was used to find matches or didn't finish
3443 * finding all matches: need to look for matches all over again. */
3444 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003445 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003446 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003447
Bram Moolenaara6557602006-02-04 22:43:20 +00003448 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003449 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003450 if (compl_leader != NULL)
3451 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003452 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003453 if (compl_shown_match != NULL)
3454 /* Make sure current match is not a hidden item. */
3455 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003456 return NUL;
3457 }
3458 return K_BS;
3459}
Bram Moolenaara6557602006-02-04 22:43:20 +00003460
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003461/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003462 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3463 * be called.
3464 */
3465 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003466ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003467{
3468 /* Return TRUE if we didn't complete finding matches or when the
3469 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3470 return compl_was_interrupted
3471 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3472 && compl_opt_refresh_always);
3473}
3474
3475/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003476 * Called after changing "compl_leader".
3477 * Show the popup menu with a different set of matches.
3478 * May also search for matches again if the previous search was interrupted.
3479 */
3480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003481ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003482{
3483 ins_compl_del_pum();
3484 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003485 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003486 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003487
3488 if (compl_started)
3489 ins_compl_set_original_text(compl_leader);
3490 else
3491 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003492#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003493 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003494#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003495 /*
3496 * Matches were cleared, need to search for them now. First display
3497 * the changed text before the cursor. Set "compl_restarting" to
3498 * avoid that the first match is inserted.
3499 */
3500 update_screen(0);
3501#ifdef FEAT_GUI
3502 if (gui.in_use)
3503 {
3504 /* Show the cursor after the match, not after the redrawn text. */
3505 setcursor();
3506 out_flush();
3507 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003508 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003509#endif
3510 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003511 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003512 compl_cont_status = 0;
3513 compl_restarting = FALSE;
3514 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003515
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003516 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003517
3518 /* Show the popup menu with a different set of matches. */
3519 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003520
3521 /* Don't let Enter select the original text when there is no popup menu. */
3522 if (compl_match_array == NULL)
3523 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003524}
3525
3526/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003527 * Return the length of the completion, from the completion start column to
3528 * the cursor column. Making sure it never goes below zero.
3529 */
3530 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003531ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003532{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003533 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003534
3535 if (off < 0)
3536 return 0;
3537 return off;
3538}
3539
3540/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003541 * Append one character to the match leader. May reduce the number of
3542 * matches.
3543 */
3544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003545ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003546{
3547#ifdef FEAT_MBYTE
3548 int cc;
3549
3550 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3551 {
3552 char_u buf[MB_MAXBYTES + 1];
3553
3554 (*mb_char2bytes)(c, buf);
3555 buf[cc] = NUL;
3556 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003557 if (compl_opt_refresh_always)
3558 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003559 }
3560 else
3561#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003562 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003563 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003564 if (compl_opt_refresh_always)
3565 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003566 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003567
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003568 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003569 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003570 ins_compl_restart();
3571
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003572 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003573 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003574 * break redo. */
3575 if (!compl_opt_refresh_always)
3576 {
3577 vim_free(compl_leader);
3578 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003579 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003580 if (compl_leader != NULL)
3581 ins_compl_new_leader();
3582 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003583}
3584
3585/*
3586 * Setup for finding completions again without leaving CTRL-X mode. Used when
3587 * BS or a key was typed while still searching for matches.
3588 */
3589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003590ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003591{
3592 ins_compl_free();
3593 compl_started = FALSE;
3594 compl_matches = 0;
3595 compl_cont_status = 0;
3596 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003597}
3598
3599/*
3600 * Set the first match, the original text.
3601 */
3602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003603ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003604{
3605 char_u *p;
3606
3607 /* Replace the original text entry. */
3608 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3609 {
3610 p = vim_strsave(str);
3611 if (p != NULL)
3612 {
3613 vim_free(compl_first_match->cp_str);
3614 compl_first_match->cp_str = p;
3615 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003616 }
3617}
3618
3619/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003620 * Append one character to the match leader. May reduce the number of
3621 * matches.
3622 */
3623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003624ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003625{
3626 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003627 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003628 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003629 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003630
3631 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003632 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003633 {
3634 /* When still at the original match use the first entry that matches
3635 * the leader. */
3636 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3637 {
3638 p = NULL;
3639 for (cp = compl_shown_match->cp_next; cp != NULL
3640 && cp != compl_first_match; cp = cp->cp_next)
3641 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003642 if (compl_leader == NULL
3643 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003644 (int)STRLEN(compl_leader)))
3645 {
3646 p = cp->cp_str;
3647 break;
3648 }
3649 }
3650 if (p == NULL || (int)STRLEN(p) <= len)
3651 return;
3652 }
3653 else
3654 return;
3655 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003656 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003657 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003658 ins_compl_addleader(c);
3659}
3660
3661/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003663 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003664 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003667ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668{
3669 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003671 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672
3673 /* Forget any previous 'special' messages if this is actually
3674 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3675 */
3676 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3677 edit_submode_extra = NULL;
3678
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003679 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003680 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3681 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003682 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003684 /* Set "compl_get_longest" when finding the first matches. */
3685 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3686 || (ctrl_x_mode == 0 && !compl_started))
3687 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003688 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003689 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003690
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003691 }
3692
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3694 {
3695 /*
3696 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3697 * it will be yet. Now we decide.
3698 */
3699 switch (c)
3700 {
3701 case Ctrl_E:
3702 case Ctrl_Y:
3703 ctrl_x_mode = CTRL_X_SCROLL;
3704 if (!(State & REPLACE_FLAG))
3705 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3706 else
3707 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3708 edit_submode_pre = NULL;
3709 showmode();
3710 break;
3711 case Ctrl_L:
3712 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3713 break;
3714 case Ctrl_F:
3715 ctrl_x_mode = CTRL_X_FILES;
3716 break;
3717 case Ctrl_K:
3718 ctrl_x_mode = CTRL_X_DICTIONARY;
3719 break;
3720 case Ctrl_R:
3721 /* Simply allow ^R to happen without affecting ^X mode */
3722 break;
3723 case Ctrl_T:
3724 ctrl_x_mode = CTRL_X_THESAURUS;
3725 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003726#ifdef FEAT_COMPL_FUNC
3727 case Ctrl_U:
3728 ctrl_x_mode = CTRL_X_FUNCTION;
3729 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003730 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003731 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003732 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003733#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003734 case 's':
3735 case Ctrl_S:
3736 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003737#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003738 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003739 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003740 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003741#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003742 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 case Ctrl_RSB:
3744 ctrl_x_mode = CTRL_X_TAGS;
3745 break;
3746#ifdef FEAT_FIND_ID
3747 case Ctrl_I:
3748 case K_S_TAB:
3749 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3750 break;
3751 case Ctrl_D:
3752 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3753 break;
3754#endif
3755 case Ctrl_V:
3756 case Ctrl_Q:
3757 ctrl_x_mode = CTRL_X_CMDLINE;
3758 break;
3759 case Ctrl_P:
3760 case Ctrl_N:
3761 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3762 * just started ^X mode, or there were enough ^X's to cancel
3763 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3764 * do normal expansion when interrupting a different mode (say
3765 * ^X^F^X^P or ^P^X^X^P, see below)
3766 * nothing changes if interrupting mode 0, (eg, the flag
3767 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003768 if (!(compl_cont_status & CONT_INTRPT))
3769 compl_cont_status |= CONT_LOCAL;
3770 else if (compl_cont_mode != 0)
3771 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 /* FALLTHROUGH */
3773 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003774 /* If we have typed at least 2 ^X's... for modes != 0, we set
3775 * compl_cont_status = 0 (eg, as if we had just started ^X
3776 * mode).
3777 * For mode 0, we set "compl_cont_mode" to an impossible
3778 * value, in both cases ^X^X can be used to restart the same
3779 * mode (avoiding ADDING mode).
3780 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3781 * 'complete' and local ^P expansions respectively.
3782 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3783 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 if (c == Ctrl_X)
3785 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003786 if (compl_cont_mode != 0)
3787 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003788 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003789 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 }
3791 ctrl_x_mode = 0;
3792 edit_submode = NULL;
3793 showmode();
3794 break;
3795 }
3796 }
3797 else if (ctrl_x_mode != 0)
3798 {
3799 /* We're already in CTRL-X mode, do we stay in it? */
3800 if (!vim_is_ctrl_x_key(c))
3801 {
3802 if (ctrl_x_mode == CTRL_X_SCROLL)
3803 ctrl_x_mode = 0;
3804 else
3805 ctrl_x_mode = CTRL_X_FINISHED;
3806 edit_submode = NULL;
3807 }
3808 showmode();
3809 }
3810
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003811 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 {
3813 /* Show error message from attempted keyword completion (probably
3814 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003817 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3818 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 || ctrl_x_mode == CTRL_X_FINISHED)
3820 {
3821 /* Get here when we have finished typing a sequence of ^N and
3822 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003823 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003824 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 {
3826 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003827 * If any of the original typed text has been changed, eg when
3828 * ignorecase is set, we must add back-spaces to the redo
3829 * buffer. We add as few as necessary to delete just the part
3830 * of the original text that has changed.
3831 * When using the longest match, edited the match or used
3832 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003834 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3835 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003836 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003837 ptr = NULL;
3838 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 }
3840
3841#ifdef FEAT_CINDENT
3842 want_cindent = (can_cindent && cindent_on());
3843#endif
3844 /*
3845 * When completing whole lines: fix indent for 'cindent'.
3846 * Otherwise, break line if it's too long.
3847 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003848 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003849 {
3850#ifdef FEAT_CINDENT
3851 /* re-indent the current line */
3852 if (want_cindent)
3853 {
3854 do_c_expr_indent();
3855 want_cindent = FALSE; /* don't do it again */
3856 }
3857#endif
3858 }
3859 else
3860 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003861 int prev_col = curwin->w_cursor.col;
3862
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003864 if (prev_col > 0)
3865 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003866 if (stop_arrow() == OK)
3867 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003868 if (prev_col > 0
3869 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3870 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003871 }
3872
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003873 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003874 * the selection without inserting anything. When
3875 * compl_enter_selects is set the Enter key does the same. */
3876 if ((c == Ctrl_Y || (compl_enter_selects
3877 && (c == CAR || c == K_KENTER || c == NL)))
3878 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003879 retval = TRUE;
3880
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003881 /* CTRL-E means completion is Ended, go back to the typed text. */
3882 if (c == Ctrl_E)
3883 {
3884 ins_compl_delete();
3885 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003886 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003887 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003888 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003889 retval = TRUE;
3890 }
3891
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003892 auto_format(FALSE, TRUE);
3893
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003895 compl_started = FALSE;
3896 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003897 if (!shortmess(SHM_COMPLETIONMENU))
3898 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003900 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 if (edit_submode != NULL)
3902 {
3903 edit_submode = NULL;
3904 showmode();
3905 }
3906
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003907#ifdef FEAT_CMDWIN
3908 if (c == Ctrl_C && cmdwin_type != 0)
3909 /* Avoid the popup menu remains displayed when leaving the
3910 * command line window. */
3911 update_screen(0);
3912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913#ifdef FEAT_CINDENT
3914 /*
3915 * Indent now if a key was typed that is in 'cinkeys'.
3916 */
3917 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3918 do_c_expr_indent();
3919#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003920#ifdef FEAT_AUTOCMD
3921 /* Trigger the CompleteDone event to give scripts a chance to act
3922 * upon the completion. */
3923 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 }
3926 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003927#ifdef FEAT_AUTOCMD
3928 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3929 /* Trigger the CompleteDone event to give scripts a chance to act
3930 * upon the (possibly failed) completion. */
3931 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933
3934 /* reset continue_* if we left expansion-mode, if we stay they'll be
3935 * (re)set properly in ins_complete() */
3936 if (!vim_is_ctrl_x_key(c))
3937 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003938 compl_cont_status = 0;
3939 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003941
3942 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943}
3944
3945/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003946 * Fix the redo buffer for the completion leader replacing some of the typed
3947 * text. This inserts backspaces and appends the changed text.
3948 * "ptr" is the known leader text or NUL.
3949 */
3950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003951ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003952{
3953 int len;
3954 char_u *p;
3955 char_u *ptr = ptr_arg;
3956
3957 if (ptr == NULL)
3958 {
3959 if (compl_leader != NULL)
3960 ptr = compl_leader;
3961 else
3962 return; /* nothing to do */
3963 }
3964 if (compl_orig_text != NULL)
3965 {
3966 p = compl_orig_text;
3967 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3968 ;
3969#ifdef FEAT_MBYTE
3970 if (len > 0)
3971 len -= (*mb_head_off)(p, p + len);
3972#endif
3973 for (p += len; *p != NUL; mb_ptr_adv(p))
3974 AppendCharToRedobuff(K_BS);
3975 }
3976 else
3977 len = 0;
3978 if (ptr != NULL)
3979 AppendToRedobuffLit(ptr + len, -1);
3980}
3981
3982/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3984 * (depending on flag) starting from buf and looking for a non-scanned
3985 * buffer (other than curbuf). curbuf is special, if it is called with
3986 * buf=curbuf then it has to be the first call for a given flag/expansion.
3987 *
3988 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3989 */
3990 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003991ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
3993#ifdef FEAT_WINDOWS
3994 static win_T *wp;
3995#endif
3996
3997 if (flag == 'w') /* just windows */
3998 {
3999#ifdef FEAT_WINDOWS
4000 if (buf == curbuf) /* first call for this flag/expansion */
4001 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004002 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 && wp->w_buffer->b_scanned)
4004 ;
4005 buf = wp->w_buffer;
4006#else
4007 buf = curbuf;
4008#endif
4009 }
4010 else
4011 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4012 * (unlisted buffers)
4013 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004014 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 && ((flag == 'U'
4016 ? buf->b_p_bl
4017 : (!buf->b_p_bl
4018 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004019 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020 ;
4021 return buf;
4022}
4023
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004024#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004025static void expand_by_function(int type, char_u *base);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004026
4027/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004028 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004029 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004030 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004032expand_by_function(
4033 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4034 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004035{
Bram Moolenaar82139082011-09-14 16:52:09 +02004036 list_T *matchlist = NULL;
4037 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004038 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004039 char_u *funcname;
4040 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004041 win_T *curwin_save;
4042 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004043 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004044
Bram Moolenaare344bea2005-09-01 20:46:49 +00004045 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4046 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004047 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004048
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004049 /* Call 'completefunc' to obtain the list of matches. */
4050 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004051 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004052
Bram Moolenaare344bea2005-09-01 20:46:49 +00004053 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004054 curwin_save = curwin;
4055 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004056
4057 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004058 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004059 {
4060 switch (rettv.v_type)
4061 {
4062 case VAR_LIST:
4063 matchlist = rettv.vval.v_list;
4064 break;
4065 case VAR_DICT:
4066 matchdict = rettv.vval.v_dict;
4067 break;
4068 default:
4069 /* TODO: Give error message? */
4070 clear_tv(&rettv);
4071 break;
4072 }
4073 }
4074
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004075 if (curwin_save != curwin || curbuf_save != curbuf)
4076 {
4077 EMSG(_(e_complwin));
4078 goto theend;
4079 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004080 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004081 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004082 if (!equalpos(curwin->w_cursor, pos))
4083 {
4084 EMSG(_(e_compldel));
4085 goto theend;
4086 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004087
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004088 if (matchlist != NULL)
4089 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004090 else if (matchdict != NULL)
4091 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004092
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004093theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004094 if (matchdict != NULL)
4095 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004096 if (matchlist != NULL)
4097 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004098}
4099#endif /* FEAT_COMPL_FUNC */
4100
Bram Moolenaar39f05632006-03-19 22:15:26 +00004101#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004102/*
4103 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004104 */
4105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004106ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004107{
4108 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004109 int dir = compl_direction;
4110
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004111 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004112 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004113 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004114 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4115 /* if dir was BACKWARD then honor it just once */
4116 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004117 else if (did_emsg)
4118 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004119 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004120}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004121
4122/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004123 * Add completions from a dict.
4124 */
4125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004126ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004127{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004128 dictitem_T *di_refresh;
4129 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004130
4131 /* Check for optional "refresh" item. */
4132 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004133 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4134 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004135 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004136 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004137
4138 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4139 compl_opt_refresh_always = TRUE;
4140 }
4141
4142 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004143 di_words = dict_find(dict, (char_u *)"words", 5);
4144 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4145 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004146}
4147
4148/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004149 * Add a match to the list of matches from a typeval_T.
4150 * If the given string is already in the list of completions, then return
4151 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4152 * maybe because alloc() returns NULL, then FAIL is returned.
4153 */
4154 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004155ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004156{
4157 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004158 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004159 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004160 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004161 char_u *(cptext[CPT_COUNT]);
4162
4163 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4164 {
4165 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4166 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4167 (char_u *)"abbr", FALSE);
4168 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4169 (char_u *)"menu", FALSE);
4170 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4171 (char_u *)"kind", FALSE);
4172 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4173 (char_u *)"info", FALSE);
4174 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4175 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004176 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004177 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004178 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4179 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004180 }
4181 else
4182 {
4183 word = get_tv_string_chk(tv);
4184 vim_memset(cptext, 0, sizeof(cptext));
4185 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004186 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004187 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004188 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004189}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004190#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004192/*
4193 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004194 * The search starts at position "ini" in curbuf and in the direction
4195 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004196 * When "compl_started" is FALSE start at that position, otherwise continue
4197 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004198 * This may return before finding all the matches.
4199 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 */
4201 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004202ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203{
4204 static pos_T first_match_pos;
4205 static pos_T last_match_pos;
4206 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004207 static int found_all = FALSE; /* Found all matches of a
4208 certain type. */
4209 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
Bram Moolenaar572cb562005-08-05 21:35:02 +00004211 pos_T *pos;
4212 char_u **matches;
4213 int save_p_scs;
4214 int save_p_ws;
4215 int save_p_ic;
4216 int i;
4217 int num_matches;
4218 int len;
4219 int found_new_match;
4220 int type = ctrl_x_mode;
4221 char_u *ptr;
4222 char_u *dict = NULL;
4223 int dict_f = 0;
4224 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004225 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004227 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 {
4229 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4230 ins_buf->b_scanned = 0;
4231 found_all = FALSE;
4232 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004233 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004234 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 last_match_pos = first_match_pos = *ini;
4236 }
4237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004239 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4241 for (;;)
4242 {
4243 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004244 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 * or if found_all says this entry is done. For ^X^L only use the
4248 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004249 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004250 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251 {
4252 found_all = FALSE;
4253 while (*e_cpt == ',' || *e_cpt == ' ')
4254 e_cpt++;
4255 if (*e_cpt == '.' && !curbuf->b_scanned)
4256 {
4257 ins_buf = curbuf;
4258 first_match_pos = *ini;
4259 /* So that ^N can match word immediately after cursor */
4260 if (ctrl_x_mode == 0)
4261 dec(&first_match_pos);
4262 last_match_pos = first_match_pos;
4263 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004264
4265 /* Remember the first match so that the loop stops when we
4266 * wrap and come back there a second time. */
4267 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 }
4269 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4270 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4271 {
4272 /* Scan a buffer, but not the current one. */
4273 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4274 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 first_match_pos.col = last_match_pos.col = 0;
4277 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4278 last_match_pos.lnum = 0;
4279 type = 0;
4280 }
4281 else /* unloaded buffer, scan like dictionary */
4282 {
4283 found_all = TRUE;
4284 if (ins_buf->b_fname == NULL)
4285 continue;
4286 type = CTRL_X_DICTIONARY;
4287 dict = ins_buf->b_fname;
4288 dict_f = DICT_EXACT;
4289 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004290 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 ins_buf->b_fname == NULL
4292 ? buf_spname(ins_buf)
4293 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004294 ? ins_buf->b_fname
4295 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004296 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 else if (*e_cpt == NUL)
4299 break;
4300 else
4301 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004302 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 type = -1;
4304 else if (*e_cpt == 'k' || *e_cpt == 's')
4305 {
4306 if (*e_cpt == 'k')
4307 type = CTRL_X_DICTIONARY;
4308 else
4309 type = CTRL_X_THESAURUS;
4310 if (*++e_cpt != ',' && *e_cpt != NUL)
4311 {
4312 dict = e_cpt;
4313 dict_f = DICT_FIRST;
4314 }
4315 }
4316#ifdef FEAT_FIND_ID
4317 else if (*e_cpt == 'i')
4318 type = CTRL_X_PATH_PATTERNS;
4319 else if (*e_cpt == 'd')
4320 type = CTRL_X_PATH_DEFINES;
4321#endif
4322 else if (*e_cpt == ']' || *e_cpt == 't')
4323 {
4324 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004325 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004326 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 }
4328 else
4329 type = -1;
4330
4331 /* in any case e_cpt is advanced to the next entry */
4332 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4333
4334 found_all = TRUE;
4335 if (type == -1)
4336 continue;
4337 }
4338 }
4339
4340 switch (type)
4341 {
4342 case -1:
4343 break;
4344#ifdef FEAT_FIND_ID
4345 case CTRL_X_PATH_PATTERNS:
4346 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004347 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004348 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004350 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4352 (linenr_T)1, (linenr_T)MAXLNUM);
4353 break;
4354#endif
4355
4356 case CTRL_X_DICTIONARY:
4357 case CTRL_X_THESAURUS:
4358 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004359 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 : (type == CTRL_X_THESAURUS
4361 ? (*curbuf->b_p_tsr == NUL
4362 ? p_tsr
4363 : curbuf->b_p_tsr)
4364 : (*curbuf->b_p_dict == NUL
4365 ? p_dict
4366 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004367 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004368 dict != NULL ? dict_f
4369 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 dict = NULL;
4371 break;
4372
4373 case CTRL_X_TAGS:
4374 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4375 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004376 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004378 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379 * of matches is found when compl_pattern is empty */
4380 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4382 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4383 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4384 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004385 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 }
4387 p_ic = save_p_ic;
4388 break;
4389
4390 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004391 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4393 {
4394
4395 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004396 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004397 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 }
4399 break;
4400
4401 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004402 if (expand_cmdline(&compl_xp, compl_pattern,
4403 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004405 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406 break;
4407
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004408#ifdef FEAT_COMPL_FUNC
4409 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004410 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004411 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004412 break;
4413#endif
4414
Bram Moolenaar488c6512005-08-11 20:09:58 +00004415 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004416#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004417 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004418 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004419 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004420 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004421#endif
4422 break;
4423
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 default: /* normal ^P/^N and ^X^L */
4425 /*
4426 * If 'infercase' is set, don't use 'smartcase' here
4427 */
4428 save_p_scs = p_scs;
4429 if (ins_buf->b_p_inf)
4430 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431
Bram Moolenaar361aa502014-01-23 22:45:58 +01004432 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 * end but never from the middle, thus setting nowrapscan in this
4434 * buffers is a good idea, on the other hand, we always set
4435 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4436 save_p_ws = p_ws;
4437 if (ins_buf != curbuf)
4438 p_ws = FALSE;
4439 else if (*e_cpt == '.')
4440 p_ws = TRUE;
4441 for (;;)
4442 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004443 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004445 ++msg_silent; /* Don't want messages for wrapscan. */
4446
Bram Moolenaare4214502015-03-05 18:08:43 +01004447 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4448 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004449 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004450 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004451 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004453 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004454 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004455 found_new_match = searchit(NULL, ins_buf, pos,
4456 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004457 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004458 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004459 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004460 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004462 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 first_match_pos = *pos;
4465 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004466 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 }
4468 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004469 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 found_new_match = FAIL;
4471 if (found_new_match == FAIL)
4472 {
4473 if (ins_buf == curbuf)
4474 found_all = TRUE;
4475 break;
4476 }
4477
4478 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004479 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 && ini->lnum == pos->lnum
4481 && ini->col == pos->col)
4482 continue;
4483 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004484 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 {
4488 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4489 continue;
4490 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4491 if (!p_paste)
4492 ptr = skipwhite(ptr);
4493 }
4494 len = (int)STRLEN(ptr);
4495 }
4496 else
4497 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004498 char_u *tmp_ptr = ptr;
4499
4500 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004502 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 /* Skip if already inside a word. */
4504 if (vim_iswordp(tmp_ptr))
4505 continue;
4506 /* Find start of next word. */
4507 tmp_ptr = find_word_start(tmp_ptr);
4508 }
4509 /* Find end of this word. */
4510 tmp_ptr = find_word_end(tmp_ptr);
4511 len = (int)(tmp_ptr - ptr);
4512
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513 if ((compl_cont_status & CONT_ADDING)
4514 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 {
4516 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4517 {
4518 /* Try next line, if any. the new word will be
4519 * "join" as if the normal command "J" was used.
4520 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004521 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 * works -- Acevedo */
4523 STRNCPY(IObuff, ptr, len);
4524 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4525 tmp_ptr = ptr = skipwhite(ptr);
4526 /* Find start of next word. */
4527 tmp_ptr = find_word_start(tmp_ptr);
4528 /* Find end of next word. */
4529 tmp_ptr = find_word_end(tmp_ptr);
4530 if (tmp_ptr > ptr)
4531 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004532 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004534 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 IObuff[len++] = ' ';
4536 /* IObuf =~ "\k.* ", thus len >= 2 */
4537 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004538 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 || (vim_strchr(p_cpo, CPO_JOINSP)
4540 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004541 && (IObuff[len - 2] == '?'
4542 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 IObuff[len++] = ' ';
4544 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004545 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (tmp_ptr - ptr >= IOSIZE - len)
4547 tmp_ptr = ptr + IOSIZE - len - 1;
4548 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4549 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004550 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 }
4552 IObuff[len] = NUL;
4553 ptr = IObuff;
4554 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004555 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 continue;
4557 }
4558 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004559 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004560 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004561 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
4563 found_new_match = OK;
4564 break;
4565 }
4566 }
4567 p_scs = save_p_scs;
4568 p_ws = save_p_ws;
4569 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004570
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004571 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004572 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004573 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 found_new_match = OK;
4575
4576 /* break the loop for specialized modes (use 'complete' just for the
4577 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004578 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004579 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004580 {
4581 if (got_int)
4582 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004583 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004584 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004585 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004586
Bram Moolenaare4214502015-03-05 18:08:43 +01004587 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004588 || compl_interrupted)
4589 break;
4590 compl_started = TRUE;
4591 }
4592 else
4593 {
4594 /* Mark a buffer scanned when it has been scanned completely */
4595 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4596 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004598 compl_started = FALSE;
4599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004601 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602
Bram Moolenaare4214502015-03-05 18:08:43 +01004603 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 && *e_cpt == NUL) /* Got to end of 'complete' */
4605 found_new_match = FAIL;
4606
4607 i = -1; /* total of matches, unknown */
4608 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004609 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 i = ins_compl_make_cyclic();
4611
4612 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004613 * just been made cyclic then we have to move compl_curr_match to the next
4614 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004615 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4616 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004617 if (compl_curr_match == NULL)
4618 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 return i;
4620}
4621
4622/* Delete the old text being completed. */
4623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004624ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625{
4626 int i;
4627
4628 /*
4629 * In insert mode: Delete the typed part.
4630 * In replace mode: Put the old characters back, if any.
4631 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004632 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 backspace_until_column(i);
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004634
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004635 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4636 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004638 /* clear v:completed_item */
4639 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004640}
4641
4642/* Insert the new text being completed. */
4643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004644ins_compl_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004645{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004646 dict_T *dict;
4647
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004648 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004649 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4650 compl_used_match = FALSE;
4651 else
4652 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004653
4654 /* Set completed item. */
4655 /* { word, abbr, menu, kind, info } */
4656 dict = dict_alloc();
4657 if (dict != NULL)
4658 {
4659 dict_add_nr_str(dict, "word", 0L,
4660 EMPTY_IF_NULL(compl_shown_match->cp_str));
4661 dict_add_nr_str(dict, "abbr", 0L,
4662 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4663 dict_add_nr_str(dict, "menu", 0L,
4664 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4665 dict_add_nr_str(dict, "kind", 0L,
4666 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4667 dict_add_nr_str(dict, "info", 0L,
4668 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4669 }
4670 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671}
4672
4673/*
4674 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004675 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4676 * get more completions. If it is FALSE, then we just do nothing when there
4677 * are no more completions in a given direction. The latter case is used when
4678 * we are still in the middle of finding completions, to allow browsing
4679 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 * Return the total number of matches, or -1 if still unknown -- webb.
4681 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004682 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4683 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 *
4685 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004686 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4687 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 */
4689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004690ins_compl_next(
4691 int allow_get_expansion,
4692 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004693 be at least 1 */
Bram Moolenaar7454a062016-01-30 15:14:10 +01004694 int insert_match) /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695{
4696 int num_matches = -1;
4697 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004698 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004699 compl_T *found_compl = NULL;
4700 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004701 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004702 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004704 /* When user complete function return -1 for findstart which is next
4705 * time of 'always', compl_shown_match become NULL. */
4706 if (compl_shown_match == NULL)
4707 return -1;
4708
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004709 if (compl_leader != NULL
4710 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004712 /* Set "compl_shown_match" to the actually shown match, it may differ
4713 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004714 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004715 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004716 && compl_shown_match->cp_next != NULL
4717 && compl_shown_match->cp_next != compl_first_match)
4718 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004719
4720 /* If we didn't find it searching forward, and compl_shows_dir is
4721 * backward, find the last match. */
4722 if (compl_shows_dir == BACKWARD
4723 && !ins_compl_equal(compl_shown_match,
4724 compl_leader, (int)STRLEN(compl_leader))
4725 && (compl_shown_match->cp_next == NULL
4726 || compl_shown_match->cp_next == compl_first_match))
4727 {
4728 while (!ins_compl_equal(compl_shown_match,
4729 compl_leader, (int)STRLEN(compl_leader))
4730 && compl_shown_match->cp_prev != NULL
4731 && compl_shown_match->cp_prev != compl_first_match)
4732 compl_shown_match = compl_shown_match->cp_prev;
4733 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004734 }
4735
4736 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004737 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /* Delete old text to be replaced */
4739 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004740
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004741 /* When finding the longest common text we stick at the original text,
4742 * don't let CTRL-N or CTRL-P move to the first match. */
4743 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4744
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004745 /* When restarting the search don't insert the first match either. */
4746 if (compl_restarting)
4747 {
4748 advance = FALSE;
4749 compl_restarting = FALSE;
4750 }
4751
Bram Moolenaare3226be2005-12-18 22:10:00 +00004752 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4753 * around. */
4754 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004756 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004758 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004759 found_end = (compl_first_match != NULL
4760 && (compl_shown_match->cp_next == compl_first_match
4761 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004762 }
4763 else if (compl_shows_dir == BACKWARD
4764 && compl_shown_match->cp_prev != NULL)
4765 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004766 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004767 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004768 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 }
4770 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004771 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004772 if (!allow_get_expansion)
4773 {
4774 if (advance)
4775 {
4776 if (compl_shows_dir == BACKWARD)
4777 compl_pending -= todo + 1;
4778 else
4779 compl_pending += todo + 1;
4780 }
4781 return -1;
4782 }
4783
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004784 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004785 {
4786 if (compl_shows_dir == BACKWARD)
4787 --compl_pending;
4788 else
4789 ++compl_pending;
4790 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004791
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004792 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004793 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004794
4795 /* handle any pending completions */
4796 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004797 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004798 {
4799 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4800 {
4801 compl_shown_match = compl_shown_match->cp_next;
4802 --compl_pending;
4803 }
4804 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4805 {
4806 compl_shown_match = compl_shown_match->cp_prev;
4807 ++compl_pending;
4808 }
4809 else
4810 break;
4811 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004812 found_end = FALSE;
4813 }
4814 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4815 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004816 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004817 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004818 ++todo;
4819 else
4820 /* Remember a matching item. */
4821 found_compl = compl_shown_match;
4822
4823 /* Stop at the end of the list when we found a usable match. */
4824 if (found_end)
4825 {
4826 if (found_compl != NULL)
4827 {
4828 compl_shown_match = found_compl;
4829 break;
4830 }
4831 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004832 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
4834
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004835 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004836 if (compl_no_insert && !started)
4837 {
4838 ins_bytes(compl_orig_text + ins_compl_len());
4839 compl_used_match = FALSE;
4840 }
4841 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004842 {
4843 if (!compl_get_longest || compl_used_match)
4844 ins_compl_insert();
4845 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004846 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004847 }
4848 else
4849 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850
4851 if (!allow_get_expansion)
4852 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004853 /* may undisplay the popup menu first */
4854 ins_compl_upd_pum();
4855
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004856 /* redraw to show the user what was inserted */
4857 update_screen(0);
4858
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004859 /* display the updated popup menu */
4860 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004861#ifdef FEAT_GUI
4862 if (gui.in_use)
4863 {
4864 /* Show the cursor after the match, not after the redrawn text. */
4865 setcursor();
4866 out_flush();
4867 gui_update_cursor(FALSE, FALSE);
4868 }
4869#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004870
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 /* Delete old text to be replaced, since we're still searching and
4872 * don't want to match ourselves! */
4873 ins_compl_delete();
4874 }
4875
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004876 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004877 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004878 if (compl_no_insert && !started)
4879 compl_enter_selects = TRUE;
4880 else
4881 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004882
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 /*
4884 * Show the file name for the match (if any)
4885 * Truncate the file name to avoid a wait for return.
4886 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004887 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 {
4889 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004890 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 if (i <= 0)
4892 i = 0;
4893 else
4894 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004895 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896 msg(IObuff);
4897 redraw_cmdline = FALSE; /* don't overwrite! */
4898 }
4899
4900 return num_matches;
4901}
4902
4903/*
4904 * Call this while finding completions, to check whether the user has hit a key
4905 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004906 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004908 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 */
4910 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004911ins_compl_check_keys(int frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912{
4913 static int count = 0;
4914
4915 int c;
4916
4917 /* Don't check when reading keys from a script. That would break the test
4918 * scripts */
4919 if (using_script())
4920 return;
4921
4922 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004923 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004924 return;
4925 count = 0;
4926
Bram Moolenaara260a972006-06-23 19:36:29 +00004927 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4928 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 if (c != NUL)
4931 {
4932 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4933 {
4934 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004935 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004936 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4937 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004939 else
4940 {
4941 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004942 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004943 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004944 if (c != K_IGNORE)
4945 {
4946 /* Don't interrupt completion when the character wasn't typed,
4947 * e.g., when doing @q to replay keys. */
4948 if (c != Ctrl_R && KeyTyped)
4949 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004950
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004951 vungetc(c);
4952 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004955 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004956 {
4957 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4958
4959 compl_pending = 0;
4960 (void)ins_compl_next(FALSE, todo, TRUE);
4961 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004962}
4963
4964/*
4965 * Decide the direction of Insert mode complete from the key typed.
4966 * Returns BACKWARD or FORWARD.
4967 */
4968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004969ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004970{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004971 if (c == Ctrl_P || c == Ctrl_L
4972 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4973 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004974 return BACKWARD;
4975 return FORWARD;
4976}
4977
4978/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004979 * Return TRUE for keys that are used for completion only when the popup menu
4980 * is visible.
4981 */
4982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004983ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004984{
4985 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004986 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4987 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004988}
4989
4990/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004991 * Decide the number of completions to move forward.
4992 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4993 */
4994 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004995ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004996{
4997 int h;
4998
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004999 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005000 {
5001 h = pum_get_height();
5002 if (h > 3)
5003 h -= 2; /* keep some context */
5004 return h;
5005 }
5006 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007}
5008
5009/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005010 * Return TRUE if completion with "c" should insert the match, FALSE if only
5011 * to change the currently selected completion.
5012 */
5013 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005014ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005015{
5016 switch (c)
5017 {
5018 case K_UP:
5019 case K_DOWN:
5020 case K_PAGEDOWN:
5021 case K_KPAGEDOWN:
5022 case K_S_DOWN:
5023 case K_PAGEUP:
5024 case K_KPAGEUP:
5025 case K_S_UP:
5026 return FALSE;
5027 }
5028 return TRUE;
5029}
5030
5031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * Do Insert mode completion.
5033 * Called when character "c" was typed, which has a meaning for completion.
5034 * Returns OK if completion was done, FAIL if something failed (out of mem).
5035 */
5036 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005037ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005039 char_u *line;
5040 int startcol = 0; /* column where searched text starts */
5041 colnr_T curs_col; /* cursor column */
5042 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005043 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
Bram Moolenaare3226be2005-12-18 22:10:00 +00005045 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005046 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 {
5048 /* First time we hit ^N or ^P (in a row, I mean) */
5049
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 did_ai = FALSE;
5051#ifdef FEAT_SMARTINDENT
5052 did_si = FALSE;
5053 can_si = FALSE;
5054 can_si_back = FALSE;
5055#endif
5056 if (stop_arrow() == FAIL)
5057 return FAIL;
5058
5059 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005060 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005061 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005063 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005064 * "compl_startpos" to the cursor as a pattern to add a new word
5065 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005066 * "compl_startpos" is not in the same line as the cursor then fix it
5067 * (the line has been split because it was longer than 'tw'). if SOL
5068 * is set then skip the previous pattern, a word at the beginning of
5069 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005070 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5071 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 {
5073 /*
5074 * it is a continued search
5075 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005076 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5078 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5079 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005080 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005082 /* line (probably) wrapped, set compl_startpos to the
5083 * first non_blank in the line, if it is not a wordchar
5084 * include it to get a better pattern, but then we don't
5085 * want the "\\<" prefix, check it bellow */
5086 compl_col = (colnr_T)(skipwhite(line) - line);
5087 compl_startpos.col = compl_col;
5088 compl_startpos.lnum = curwin->w_cursor.lnum;
5089 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091 else
5092 {
5093 /* S_IPOS was set when we inserted a word that was at the
5094 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 * mode but first we need to redefine compl_startpos */
5096 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005098 compl_cont_status |= CONT_SOL;
5099 compl_startpos.col = (colnr_T)(skipwhite(
5100 line + compl_length
5101 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005106 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005107 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005108#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005109 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 compl_cont_status &= ~CONT_SOL;
5112 compl_length = (IOSIZE - MIN_SPACE);
5113 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005115 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5116 if (compl_length < 1)
5117 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005119 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005120 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005122 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 }
5124 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005127 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005131 compl_cont_status = 0;
5132 compl_cont_status |= CONT_N_ADDS;
5133 compl_startpos = curwin->w_cursor;
5134 startcol = (int)curs_col;
5135 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 }
5137
5138 /* Work out completion pattern and original text -- webb */
5139 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5143 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005144 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005146 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005148 compl_col += ++startcol;
5149 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 compl_pattern = str_foldcase(line + compl_col,
5153 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005155 compl_pattern = vim_strnsave(line + compl_col,
5156 compl_length);
5157 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 return FAIL;
5159 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 {
5162 char_u *prefix = (char_u *)"\\<";
5163
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005164 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005165 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005166 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 if (!vim_iswordp(line + compl_col)
5170 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 && (
5172#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176#endif
5177 )))
5178 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 STRCPY((char *)compl_pattern, prefix);
5180 (void)quote_meta(compl_pattern + STRLEN(prefix),
5181 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005183 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005185 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#endif
5189 )
5190 {
5191 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005192 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5193 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 compl_col += curs_col;
5196 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198 else
5199 {
5200#ifdef FEAT_MBYTE
5201 /* Search the point of change class of multibyte character
5202 * or not a word single byte character backward. */
5203 if (has_mbyte)
5204 {
5205 int base_class;
5206 int head_off;
5207
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005208 startcol -= (*mb_head_off)(line, line + startcol);
5209 base_class = mb_get_class(line + startcol);
5210 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005212 head_off = (*mb_head_off)(line, line + startcol);
5213 if (base_class != mb_get_class(line + startcol
5214 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005216 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 }
5218 }
5219 else
5220#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005221 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 compl_col += ++startcol;
5224 compl_length = (int)curs_col - startcol;
5225 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 {
5227 /* Only match word with at least two chars -- webb
5228 * there's no need to call quote_meta,
5229 * alloc(7) is enough -- Acevedo
5230 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005231 compl_pattern = alloc(7);
5232 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 STRCPY((char *)compl_pattern, "\\<");
5235 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5236 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005237 }
5238 else
5239 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005240 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005241 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005242 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 STRCPY((char *)compl_pattern, "\\<");
5245 (void)quote_meta(compl_pattern + 2, line + compl_col,
5246 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248 }
5249 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005250 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005252 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005253 compl_length = (int)curs_col - (int)compl_col;
5254 if (compl_length < 0) /* cursor in indent: empty pattern */
5255 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 compl_pattern = str_foldcase(line + compl_col, compl_length,
5258 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5261 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 return FAIL;
5263 }
5264 else if (ctrl_x_mode == CTRL_X_FILES)
5265 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005266 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005267 if (startcol > 0)
5268 {
5269 char_u *p = line + startcol;
5270
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005271 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005272 while (p > line && vim_isfilec(PTR2CHAR(p)))
5273 mb_ptr_back(line, p);
5274 if (p == line && vim_isfilec(PTR2CHAR(p)))
5275 startcol = 0;
5276 else
5277 startcol = (int)(p - line) + 1;
5278 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005279
Bram Moolenaar0300e462013-09-08 16:03:45 +02005280 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 compl_length = (int)curs_col - startcol;
5282 compl_pattern = addstar(line + compl_col, compl_length,
5283 EXPAND_FILES);
5284 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 return FAIL;
5286 }
5287 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5288 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005289 compl_pattern = vim_strnsave(line, curs_col);
5290 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005292 set_cmd_context(&compl_xp, compl_pattern,
5293 (int)STRLEN(compl_pattern), curs_col);
5294 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5295 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005296 /* No completion possible, use an empty pattern to get a
5297 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005298 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005299 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005300 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5301 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005303 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005304 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005305#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005306 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005307 * Call user defined function 'completefunc' with "a:findstart"
5308 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005309 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005310 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005311 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005312 char_u *funcname;
5313 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005314 win_T *curwin_save;
5315 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005316
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005317 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005318 * string */
5319 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5320 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5321 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005322 {
5323 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5324 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005325 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005326 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005327
5328 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005329 args[1] = NULL;
5330 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005331 curwin_save = curwin;
5332 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005333 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005334 if (curwin_save != curwin || curbuf_save != curbuf)
5335 {
5336 EMSG(_(e_complwin));
5337 return FAIL;
5338 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005339 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005340 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005341 if (!equalpos(curwin->w_cursor, pos))
5342 {
5343 EMSG(_(e_compldel));
5344 return FAIL;
5345 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005346
Bram Moolenaar6110a002012-01-26 18:58:38 +01005347 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005348 * cancel the complete without an error.
5349 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005350 if (col == -2)
5351 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005352 if (col == -3)
5353 {
5354 ctrl_x_mode = 0;
5355 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005356 if (!shortmess(SHM_COMPLETIONMENU))
5357 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005358 return FAIL;
5359 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005360
Bram Moolenaar82139082011-09-14 16:52:09 +02005361 /*
5362 * Reset extended parameters of completion, when start new
5363 * completion.
5364 */
5365 compl_opt_refresh_always = FALSE;
5366
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005367 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005368 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005369 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005370 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005371 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005372
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005373 /* Setup variables for completion. Need to obtain "line" again,
5374 * it may have become invalid. */
5375 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005376 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005377 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5378 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005379#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 return FAIL;
5381 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005382 else if (ctrl_x_mode == CTRL_X_SPELL)
5383 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005384#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005385 if (spell_bad_len > 0)
5386 compl_col = curs_col - spell_bad_len;
5387 else
5388 compl_col = spell_word_start(startcol);
5389 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005390 {
5391 compl_length = 0;
5392 compl_col = curs_col;
5393 }
5394 else
5395 {
5396 spell_expand_check_cap(compl_col);
5397 compl_length = (int)curs_col - compl_col;
5398 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005399 /* Need to obtain "line" again, it may have become invalid. */
5400 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005401 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5402 if (compl_pattern == NULL)
5403#endif
5404 return FAIL;
5405 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005406 else
5407 {
5408 EMSG2(_(e_intern2), "ins_complete()");
5409 return FAIL;
5410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005411
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
5414 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005415 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416 {
5417 /* Insert a new line, keep indentation but ignore 'comments' */
5418#ifdef FEAT_COMMENTS
5419 char_u *old = curbuf->b_p_com;
5420
5421 curbuf->b_p_com = (char_u *)"";
5422#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005423 compl_startpos.lnum = curwin->w_cursor.lnum;
5424 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425 ins_eol('\r');
5426#ifdef FEAT_COMMENTS
5427 curbuf->b_p_com = old;
5428#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005429 compl_length = 0;
5430 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 }
5432 }
5433 else
5434 {
5435 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 }
5438
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005439 if (compl_cont_status & CONT_LOCAL)
5440 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005441 else
5442 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5443
Bram Moolenaar62951b12011-09-21 18:23:05 +02005444 /* If any of the original typed text has been changed we need to fix
5445 * the redo buffer. */
5446 ins_compl_fixRedoBufForLeader(NULL);
5447
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005448 /* Always add completion for the original text. */
5449 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5451 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005452 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005454 vim_free(compl_pattern);
5455 compl_pattern = NULL;
5456 vim_free(compl_orig_text);
5457 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 return FAIL;
5459 }
5460
5461 /* showmode might reset the internal line pointers, so it must
5462 * be called before line = ml_get(), or when this address is no
5463 * longer needed. -- Acevedo.
5464 */
5465 edit_submode_extra = (char_u *)_("-- Searching...");
5466 edit_submode_highl = HLF_COUNT;
5467 showmode();
5468 edit_submode_extra = NULL;
5469 out_flush();
5470 }
5471
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005472 compl_shown_match = compl_curr_match;
5473 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474
5475 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005476 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005478 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005479 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005481 /* may undisplay the popup menu */
5482 ins_compl_upd_pum();
5483
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005484 if (n > 1) /* all matches have been found */
5485 compl_matches = n;
5486 compl_curr_match = compl_shown_match;
5487 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488
Bram Moolenaard68071d2006-05-02 22:08:30 +00005489 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5490 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 if (got_int && !global_busy)
5492 {
5493 (void)vgetc();
5494 got_int = FALSE;
5495 }
5496
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005497 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005498 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005500 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5501 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5503 edit_submode_highl = HLF_E;
5504 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5505 * because we couldn't expand anything at first place, but if we used
5506 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5507 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005508 if ( compl_length > 1
5509 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 || (ctrl_x_mode != 0
5511 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5512 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005513 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 }
5515
Bram Moolenaar572cb562005-08-05 21:35:02 +00005516 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005517 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005519 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
5521 if (edit_submode_extra == NULL)
5522 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005523 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 {
5525 edit_submode_extra = (char_u *)_("Back at original");
5526 edit_submode_highl = HLF_W;
5527 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005528 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 {
5530 edit_submode_extra = (char_u *)_("Word from other line");
5531 edit_submode_highl = HLF_COUNT;
5532 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005533 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 {
5535 edit_submode_extra = (char_u *)_("The only match");
5536 edit_submode_highl = HLF_COUNT;
5537 }
5538 else
5539 {
5540 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005541 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005543 int number = 0;
5544 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005546 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
5548 /* search backwards for the first valid (!= -1) number.
5549 * This should normally succeed already at the first loop
5550 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005551 for (match = compl_curr_match->cp_prev; match != NULL
5552 && match != compl_first_match;
5553 match = match->cp_prev)
5554 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005556 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 break;
5558 }
5559 if (match != NULL)
5560 /* go up and assign all numbers which are not assigned
5561 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005562 for (match = match->cp_next;
5563 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005564 match = match->cp_next)
5565 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 }
5567 else /* BACKWARD */
5568 {
5569 /* search forwards (upwards) for the first valid (!= -1)
5570 * number. This should normally succeed already at the
5571 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005572 for (match = compl_curr_match->cp_next; match != NULL
5573 && match != compl_first_match;
5574 match = match->cp_next)
5575 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005577 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 break;
5579 }
5580 if (match != NULL)
5581 /* go down and assign all numbers which are not
5582 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005583 for (match = match->cp_prev; match
5584 && match->cp_number == -1;
5585 match = match->cp_prev)
5586 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 }
5588 }
5589
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005590 /* The match should always have a sequence number now, this is
5591 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005592 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005594 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5595 * Translations may need more than twice that. */
5596 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005598 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005599 vim_snprintf((char *)match_ref, sizeof(match_ref),
5600 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005601 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005602 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005603 vim_snprintf((char *)match_ref, sizeof(match_ref),
5604 _("match %d"),
5605 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 edit_submode_extra = match_ref;
5607 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005608 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 curs_columns(FALSE);
5610 }
5611 }
5612 }
5613
5614 /* Show a message about what (completion) mode we're in. */
5615 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005616 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005618 if (edit_submode_extra != NULL)
5619 {
5620 if (!p_smd)
5621 msg_attr(edit_submode_extra,
5622 edit_submode_highl < HLF_COUNT
5623 ? hl_attr(edit_submode_highl) : 0);
5624 }
5625 else
5626 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005627 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005628
Bram Moolenaard68071d2006-05-02 22:08:30 +00005629 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005630 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005631 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005632 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005633 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005634 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005635 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005636
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 return OK;
5638}
5639
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005640 static void
5641show_pum(int save_w_wrow)
5642{
5643 /* RedrawingDisabled may be set when invoked through complete(). */
5644 int n = RedrawingDisabled;
5645
5646 RedrawingDisabled = 0;
5647
5648 /* If the cursor moved we need to remove the pum first. */
5649 setcursor();
5650 if (save_w_wrow != curwin->w_wrow)
5651 ins_compl_del_pum();
5652
5653 ins_compl_show_pum();
5654 setcursor();
5655 RedrawingDisabled = n;
5656}
5657
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658/*
5659 * Looks in the first "len" chars. of "src" for search-metachars.
5660 * If dest is not NULL the chars. are copied there quoting (with
5661 * a backslash) the metachars, and dest would be NUL terminated.
5662 * Returns the length (needed) of dest
5663 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005664 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005665quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005667 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005669 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005670 {
5671 switch (*src)
5672 {
5673 case '.':
5674 case '*':
5675 case '[':
5676 if (ctrl_x_mode == CTRL_X_DICTIONARY
5677 || ctrl_x_mode == CTRL_X_THESAURUS)
5678 break;
5679 case '~':
5680 if (!p_magic) /* quote these only if magic is set */
5681 break;
5682 case '\\':
5683 if (ctrl_x_mode == CTRL_X_DICTIONARY
5684 || ctrl_x_mode == CTRL_X_THESAURUS)
5685 break;
5686 case '^': /* currently it's not needed. */
5687 case '$':
5688 m++;
5689 if (dest != NULL)
5690 *dest++ = '\\';
5691 break;
5692 }
5693 if (dest != NULL)
5694 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005695# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 /* Copy remaining bytes of a multibyte character. */
5697 if (has_mbyte)
5698 {
5699 int i, mb_len;
5700
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005701 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005702 if (mb_len > 0 && len >= mb_len)
5703 for (i = 0; i < mb_len; ++i)
5704 {
5705 --len;
5706 ++src;
5707 if (dest != NULL)
5708 *dest++ = *src;
5709 }
5710 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005711# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 }
5713 if (dest != NULL)
5714 *dest = NUL;
5715
5716 return m;
5717}
5718#endif /* FEAT_INS_EXPAND */
5719
5720/*
5721 * Next character is interpreted literally.
5722 * A one, two or three digit decimal number is interpreted as its byte value.
5723 * If one or two digits are entered, the next character is given to vungetc().
5724 * For Unicode a character > 255 may be returned.
5725 */
5726 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005727get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728{
5729 int cc;
5730 int nc;
5731 int i;
5732 int hex = FALSE;
5733 int octal = FALSE;
5734#ifdef FEAT_MBYTE
5735 int unicode = 0;
5736#endif
5737
5738 if (got_int)
5739 return Ctrl_C;
5740
5741#ifdef FEAT_GUI
5742 /*
5743 * In GUI there is no point inserting the internal code for a special key.
5744 * It is more useful to insert the string "<KEY>" instead. This would
5745 * probably be useful in a text window too, but it would not be
5746 * vi-compatible (maybe there should be an option for it?) -- webb
5747 */
5748 if (gui.in_use)
5749 ++allow_keys;
5750#endif
5751#ifdef USE_ON_FLY_SCROLL
5752 dont_scroll = TRUE; /* disallow scrolling here */
5753#endif
5754 ++no_mapping; /* don't map the next key hits */
5755 cc = 0;
5756 i = 0;
5757 for (;;)
5758 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005759 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760#ifdef FEAT_CMDL_INFO
5761 if (!(State & CMDLINE)
5762# ifdef FEAT_MBYTE
5763 && MB_BYTE2LEN_CHECK(nc) == 1
5764# endif
5765 )
5766 add_to_showcmd(nc);
5767#endif
5768 if (nc == 'x' || nc == 'X')
5769 hex = TRUE;
5770 else if (nc == 'o' || nc == 'O')
5771 octal = TRUE;
5772#ifdef FEAT_MBYTE
5773 else if (nc == 'u' || nc == 'U')
5774 unicode = nc;
5775#endif
5776 else
5777 {
5778 if (hex
5779#ifdef FEAT_MBYTE
5780 || unicode != 0
5781#endif
5782 )
5783 {
5784 if (!vim_isxdigit(nc))
5785 break;
5786 cc = cc * 16 + hex2nr(nc);
5787 }
5788 else if (octal)
5789 {
5790 if (nc < '0' || nc > '7')
5791 break;
5792 cc = cc * 8 + nc - '0';
5793 }
5794 else
5795 {
5796 if (!VIM_ISDIGIT(nc))
5797 break;
5798 cc = cc * 10 + nc - '0';
5799 }
5800
5801 ++i;
5802 }
5803
5804 if (cc > 255
5805#ifdef FEAT_MBYTE
5806 && unicode == 0
5807#endif
5808 )
5809 cc = 255; /* limit range to 0-255 */
5810 nc = 0;
5811
5812 if (hex) /* hex: up to two chars */
5813 {
5814 if (i >= 2)
5815 break;
5816 }
5817#ifdef FEAT_MBYTE
5818 else if (unicode) /* Unicode: up to four or eight chars */
5819 {
5820 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5821 break;
5822 }
5823#endif
5824 else if (i >= 3) /* decimal or octal: up to three chars */
5825 break;
5826 }
5827 if (i == 0) /* no number entered */
5828 {
5829 if (nc == K_ZERO) /* NUL is stored as NL */
5830 {
5831 cc = '\n';
5832 nc = 0;
5833 }
5834 else
5835 {
5836 cc = nc;
5837 nc = 0;
5838 }
5839 }
5840
5841 if (cc == 0) /* NUL is stored as NL */
5842 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005843#ifdef FEAT_MBYTE
5844 if (enc_dbcs && (cc & 0xff) == 0)
5845 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5846 second byte will cause trouble! */
5847#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848
5849 --no_mapping;
5850#ifdef FEAT_GUI
5851 if (gui.in_use)
5852 --allow_keys;
5853#endif
5854 if (nc)
5855 vungetc(nc);
5856 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5857 return cc;
5858}
5859
5860/*
5861 * Insert character, taking care of special keys and mod_mask
5862 */
5863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005864insert_special(
5865 int c,
5866 int allow_modmask,
5867 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868{
5869 char_u *p;
5870 int len;
5871
5872 /*
5873 * Special function key, translate into "<Key>". Up to the last '>' is
5874 * inserted with ins_str(), so as not to replace characters in replace
5875 * mode.
5876 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5877 * unless 'allow_modmask' is TRUE.
5878 */
5879#ifdef MACOS
5880 /* Command-key never produces a normal key */
5881 if (mod_mask & MOD_MASK_CMD)
5882 allow_modmask = TRUE;
5883#endif
5884 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5885 {
5886 p = get_special_key_name(c, mod_mask);
5887 len = (int)STRLEN(p);
5888 c = p[len - 1];
5889 if (len > 2)
5890 {
5891 if (stop_arrow() == FAIL)
5892 return;
5893 p[len - 1] = NUL;
5894 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005895 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 ctrlv = FALSE;
5897 }
5898 }
5899 if (stop_arrow() == OK)
5900 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5901}
5902
5903/*
5904 * Special characters in this context are those that need processing other
5905 * than the simple insertion that can be performed here. This includes ESC
5906 * which terminates the insert, and CR/NL which need special processing to
5907 * open up a new line. This routine tries to optimize insertions performed by
5908 * the "redo", "undo" or "put" commands, so it needs to know when it should
5909 * stop and defer processing to the "normal" mechanism.
5910 * '0' and '^' are special, because they can be followed by CTRL-D.
5911 */
5912#ifdef EBCDIC
5913# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5914#else
5915# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5916#endif
5917
5918#ifdef FEAT_MBYTE
5919# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5920#else
5921# define WHITECHAR(cc) vim_iswhite(cc)
5922#endif
5923
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005924/*
5925 * "flags": INSCHAR_FORMAT - force formatting
5926 * INSCHAR_CTRLV - char typed just after CTRL-V
5927 * INSCHAR_NO_FEX - don't use 'formatexpr'
5928 *
5929 * NOTE: passes the flags value straight through to internal_format() which,
5930 * beside INSCHAR_FORMAT (above), is also looking for these:
5931 * INSCHAR_DO_COM - format comments
5932 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5933 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005935insertchar(
5936 int c, /* character to insert or NUL */
5937 int flags, /* INSCHAR_FORMAT, etc. */
5938 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 int textwidth;
5941#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005945 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005947 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005948 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
5950 /*
5951 * Try to break the line in two or more pieces when:
5952 * - Always do this if we have been called to do formatting only.
5953 * - Always do this when 'formatoptions' has the 'a' flag and the line
5954 * ends in white space.
5955 * - Otherwise:
5956 * - Don't do this if inserting a blank
5957 * - Don't do this if an existing character is being replaced, unless
5958 * we're in VREPLACE mode.
5959 * - Do this if the cursor is not on the line where insert started
5960 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5961 * before the insert.
5962 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5963 * before 'textwidth'
5964 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005965 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005966 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 || (!vim_iswhite(c)
5968 && !((State & REPLACE_FLAG)
5969#ifdef FEAT_VREPLACE
5970 && !(State & VREPLACE_FLAG)
5971#endif
5972 && *ml_get_cursor() != NUL)
5973 && (curwin->w_cursor.lnum != Insstart.lnum
5974 || ((!has_format_option(FO_INS_LONG)
5975 || Insstart_textlen <= (colnr_T)textwidth)
5976 && (!fo_ins_blank
5977 || Insstart_blank_vcol <= (colnr_T)textwidth
5978 ))))))
5979 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005980 /* Format with 'formatexpr' when it's set. Use internal formatting
5981 * when 'formatexpr' isn't set or it returns non-zero. */
5982#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005983 int do_internal = TRUE;
5984 colnr_T virtcol = get_nolist_virtcol()
5985 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005986
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005987 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
5988 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005989 {
5990 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5991 /* It may be required to save for undo again, e.g. when setline()
5992 * was called. */
5993 ins_need_undo = TRUE;
5994 }
5995 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005997 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 if (c == NUL) /* only formatting was wanted */
6001 return;
6002
6003#ifdef FEAT_COMMENTS
6004 /* Check whether this character should end a comment. */
6005 if (did_ai && (int)c == end_comment_pending)
6006 {
6007 char_u *line;
6008 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6009 int middle_len, end_len;
6010 int i;
6011
6012 /*
6013 * Need to remove existing (middle) comment leader and insert end
6014 * comment leader. First, check what comment leader we can find.
6015 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006016 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6018 {
6019 /* Skip middle-comment string */
6020 while (*p && p[-1] != ':') /* find end of middle flags */
6021 ++p;
6022 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6023 /* Don't count trailing white space for middle_len */
6024 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6025 --middle_len;
6026
6027 /* Find the end-comment string */
6028 while (*p && p[-1] != ':') /* find end of end flags */
6029 ++p;
6030 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6031
6032 /* Skip white space before the cursor */
6033 i = curwin->w_cursor.col;
6034 while (--i >= 0 && vim_iswhite(line[i]))
6035 ;
6036 i++;
6037
6038 /* Skip to before the middle leader */
6039 i -= middle_len;
6040
6041 /* Check some expected things before we go on */
6042 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6043 {
6044 /* Backspace over all the stuff we want to replace */
6045 backspace_until_column(i);
6046
6047 /*
6048 * Insert the end-comment string, except for the last
6049 * character, which will get inserted as normal later.
6050 */
6051 ins_bytes_len(lead_end, end_len - 1);
6052 }
6053 }
6054 }
6055 end_comment_pending = NUL;
6056#endif
6057
6058 did_ai = FALSE;
6059#ifdef FEAT_SMARTINDENT
6060 did_si = FALSE;
6061 can_si = FALSE;
6062 can_si_back = FALSE;
6063#endif
6064
6065 /*
6066 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6067 * This speeds up normal text input considerably.
6068 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6069 * need to re-indent at a ':', or any other character (but not what
6070 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006071 * Don't do this when there an InsertCharPre autocommand is defined,
6072 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006073 */
6074#ifdef USE_ON_FLY_SCROLL
6075 dont_scroll = FALSE; /* allow scrolling here */
6076#endif
6077
6078 if ( !ISSPECIAL(c)
6079#ifdef FEAT_MBYTE
6080 && (!has_mbyte || (*mb_char2len)(c) == 1)
6081#endif
6082 && vpeekc() != NUL
6083 && !(State & REPLACE_FLAG)
6084#ifdef FEAT_CINDENT
6085 && !cindent_on()
6086#endif
6087#ifdef FEAT_RIGHTLEFT
6088 && !p_ri
6089#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006090#ifdef FEAT_AUTOCMD
6091 && !has_insertcharpre()
6092#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006093 )
6094 {
6095#define INPUT_BUFLEN 100
6096 char_u buf[INPUT_BUFLEN + 1];
6097 int i;
6098 colnr_T virtcol = 0;
6099
6100 buf[0] = c;
6101 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006102 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 virtcol = get_nolist_virtcol();
6104 /*
6105 * Stop the string when:
6106 * - no more chars available
6107 * - finding a special character (command key)
6108 * - buffer is full
6109 * - running into the 'textwidth' boundary
6110 * - need to check for abbreviation: A non-word char after a word-char
6111 */
6112 while ( (c = vpeekc()) != NUL
6113 && !ISSPECIAL(c)
6114#ifdef FEAT_MBYTE
6115 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6116#endif
6117 && i < INPUT_BUFLEN
6118 && (textwidth == 0
6119 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6120 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6121 {
6122#ifdef FEAT_RIGHTLEFT
6123 c = vgetc();
6124 if (p_hkmap && KeyTyped)
6125 c = hkmap(c); /* Hebrew mode mapping */
6126# ifdef FEAT_FKMAP
6127 if (p_fkmap && KeyTyped)
6128 c = fkmap(c); /* Farsi mode mapping */
6129# endif
6130 buf[i++] = c;
6131#else
6132 buf[i++] = vgetc();
6133#endif
6134 }
6135
6136#ifdef FEAT_DIGRAPHS
6137 do_digraph(-1); /* clear digraphs */
6138 do_digraph(buf[i-1]); /* may be the start of a digraph */
6139#endif
6140 buf[i] = NUL;
6141 ins_str(buf);
6142 if (flags & INSCHAR_CTRLV)
6143 {
6144 redo_literal(*buf);
6145 i = 1;
6146 }
6147 else
6148 i = 0;
6149 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006150 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006151 }
6152 else
6153 {
6154#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006155 int cc;
6156
Bram Moolenaar071d4272004-06-13 20:20:40 +00006157 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6158 {
6159 char_u buf[MB_MAXBYTES + 1];
6160
6161 (*mb_char2bytes)(c, buf);
6162 buf[cc] = NUL;
6163 ins_char_bytes(buf, cc);
6164 AppendCharToRedobuff(c);
6165 }
6166 else
6167#endif
6168 {
6169 ins_char(c);
6170 if (flags & INSCHAR_CTRLV)
6171 redo_literal(c);
6172 else
6173 AppendCharToRedobuff(c);
6174 }
6175 }
6176}
6177
6178/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006179 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006180 *
6181 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6182 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006183 */
6184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006185internal_format(
6186 int textwidth,
6187 int second_indent,
6188 int flags,
6189 int format_only,
6190 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006191{
6192 int cc;
6193 int save_char = NUL;
6194 int haveto_redraw = FALSE;
6195 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6196#ifdef FEAT_MBYTE
6197 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6198#endif
6199 int fo_white_par = has_format_option(FO_WHITE_PAR);
6200 int first_line = TRUE;
6201#ifdef FEAT_COMMENTS
6202 colnr_T leader_len;
6203 int no_leader = FALSE;
6204 int do_comments = (flags & INSCHAR_DO_COM);
6205#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006206#ifdef FEAT_LINEBREAK
6207 int has_lbr = curwin->w_p_lbr;
6208
6209 /* make sure win_lbr_chartabsize() counts correctly */
6210 curwin->w_p_lbr = FALSE;
6211#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212
6213 /*
6214 * When 'ai' is off we don't want a space under the cursor to be
6215 * deleted. Replace it with an 'x' temporarily.
6216 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006217 if (!curbuf->b_p_ai
6218#ifdef FEAT_VREPLACE
6219 && !(State & VREPLACE_FLAG)
6220#endif
6221 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222 {
6223 cc = gchar_cursor();
6224 if (vim_iswhite(cc))
6225 {
6226 save_char = cc;
6227 pchar_cursor('x');
6228 }
6229 }
6230
6231 /*
6232 * Repeat breaking lines, until the current line is not too long.
6233 */
6234 while (!got_int)
6235 {
6236 int startcol; /* Cursor column at entry */
6237 int wantcol; /* column at textwidth border */
6238 int foundcol; /* column for start of spaces */
6239 int end_foundcol = 0; /* column for start of word */
6240 colnr_T len;
6241 colnr_T virtcol;
6242#ifdef FEAT_VREPLACE
6243 int orig_col = 0;
6244 char_u *saved_text = NULL;
6245#endif
6246 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006247 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006248
Bram Moolenaar97b98102009-11-17 16:41:01 +00006249 virtcol = get_nolist_virtcol()
6250 + char2cells(c != NUL ? c : gchar_cursor());
6251 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006252 break;
6253
6254#ifdef FEAT_COMMENTS
6255 if (no_leader)
6256 do_comments = FALSE;
6257 else if (!(flags & INSCHAR_FORMAT)
6258 && has_format_option(FO_WRAP_COMS))
6259 do_comments = TRUE;
6260
6261 /* Don't break until after the comment leader */
6262 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006263 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006264 else
6265 leader_len = 0;
6266
6267 /* If the line doesn't start with a comment leader, then don't
6268 * start one in a following broken line. Avoids that a %word
6269 * moved to the start of the next line causes all following lines
6270 * to start with %. */
6271 if (leader_len == 0)
6272 no_leader = TRUE;
6273#endif
6274 if (!(flags & INSCHAR_FORMAT)
6275#ifdef FEAT_COMMENTS
6276 && leader_len == 0
6277#endif
6278 && !has_format_option(FO_WRAP))
6279
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006280 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006281 if ((startcol = curwin->w_cursor.col) == 0)
6282 break;
6283
6284 /* find column of textwidth border */
6285 coladvance((colnr_T)textwidth);
6286 wantcol = curwin->w_cursor.col;
6287
Bram Moolenaar97b98102009-11-17 16:41:01 +00006288 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006289 foundcol = 0;
6290
6291 /*
6292 * Find position to break at.
6293 * Stop at first entered white when 'formatoptions' has 'v'
6294 */
6295 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006296 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006297 || curwin->w_cursor.lnum != Insstart.lnum
6298 || curwin->w_cursor.col >= Insstart.col)
6299 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006300 if (curwin->w_cursor.col == startcol && c != NUL)
6301 cc = c;
6302 else
6303 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006304 if (WHITECHAR(cc))
6305 {
6306 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006307 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006308
6309 /* find start of sequence of blanks */
6310 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6311 {
6312 dec_cursor();
6313 cc = gchar_cursor();
6314 }
6315 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6316 break; /* only spaces in front of text */
6317#ifdef FEAT_COMMENTS
6318 /* Don't break until after the comment leader */
6319 if (curwin->w_cursor.col < leader_len)
6320 break;
6321#endif
6322 if (has_format_option(FO_ONE_LETTER))
6323 {
6324 /* do not break after one-letter words */
6325 if (curwin->w_cursor.col == 0)
6326 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006327#ifdef FEAT_COMMENTS
6328 /* do not break "#a b" when 'tw' is 2 */
6329 if (curwin->w_cursor.col <= leader_len)
6330 break;
6331#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006332 col = curwin->w_cursor.col;
6333 dec_cursor();
6334 cc = gchar_cursor();
6335
6336 if (WHITECHAR(cc))
6337 continue; /* one-letter, continue */
6338 curwin->w_cursor.col = col;
6339 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006340
6341 inc_cursor();
6342
6343 end_foundcol = end_col + 1;
6344 foundcol = curwin->w_cursor.col;
6345 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006346 break;
6347 }
6348#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006349 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006350 {
6351 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006352 if (curwin->w_cursor.col != startcol)
6353 {
6354#ifdef FEAT_COMMENTS
6355 /* Don't break until after the comment leader */
6356 if (curwin->w_cursor.col < leader_len)
6357 break;
6358#endif
6359 col = curwin->w_cursor.col;
6360 inc_cursor();
6361 /* Don't change end_foundcol if already set. */
6362 if (foundcol != curwin->w_cursor.col)
6363 {
6364 foundcol = curwin->w_cursor.col;
6365 end_foundcol = foundcol;
6366 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6367 break;
6368 }
6369 curwin->w_cursor.col = col;
6370 }
6371
6372 if (curwin->w_cursor.col == 0)
6373 break;
6374
6375 col = curwin->w_cursor.col;
6376
6377 dec_cursor();
6378 cc = gchar_cursor();
6379
6380 if (WHITECHAR(cc))
6381 continue; /* break with space */
6382#ifdef FEAT_COMMENTS
6383 /* Don't break until after the comment leader */
6384 if (curwin->w_cursor.col < leader_len)
6385 break;
6386#endif
6387
6388 curwin->w_cursor.col = col;
6389
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006390 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006391 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006392 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6393 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006394 }
6395#endif
6396 if (curwin->w_cursor.col == 0)
6397 break;
6398 dec_cursor();
6399 }
6400
6401 if (foundcol == 0) /* no spaces, cannot break line */
6402 {
6403 curwin->w_cursor.col = startcol;
6404 break;
6405 }
6406
6407 /* Going to break the line, remove any "$" now. */
6408 undisplay_dollar();
6409
6410 /*
6411 * Offset between cursor position and line break is used by replace
6412 * stack functions. VREPLACE does not use this, and backspaces
6413 * over the text instead.
6414 */
6415#ifdef FEAT_VREPLACE
6416 if (State & VREPLACE_FLAG)
6417 orig_col = startcol; /* Will start backspacing from here */
6418 else
6419#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006420 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006421
6422 /*
6423 * adjust startcol for spaces that will be deleted and
6424 * characters that will remain on top line
6425 */
6426 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006427 while ((cc = gchar_cursor(), WHITECHAR(cc))
6428 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006429 inc_cursor();
6430 startcol -= curwin->w_cursor.col;
6431 if (startcol < 0)
6432 startcol = 0;
6433
6434#ifdef FEAT_VREPLACE
6435 if (State & VREPLACE_FLAG)
6436 {
6437 /*
6438 * In VREPLACE mode, we will backspace over the text to be
6439 * wrapped, so save a copy now to put on the next line.
6440 */
6441 saved_text = vim_strsave(ml_get_cursor());
6442 curwin->w_cursor.col = orig_col;
6443 if (saved_text == NULL)
6444 break; /* Can't do it, out of memory */
6445 saved_text[startcol] = NUL;
6446
6447 /* Backspace over characters that will move to the next line */
6448 if (!fo_white_par)
6449 backspace_until_column(foundcol);
6450 }
6451 else
6452#endif
6453 {
6454 /* put cursor after pos. to break line */
6455 if (!fo_white_par)
6456 curwin->w_cursor.col = foundcol;
6457 }
6458
6459 /*
6460 * Split the line just before the margin.
6461 * Only insert/delete lines, but don't really redraw the window.
6462 */
6463 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6464 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6465#ifdef FEAT_COMMENTS
6466 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006467 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006468#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006469 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6470 if (!(flags & INSCHAR_COM_LIST))
6471 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006472
6473 replace_offset = 0;
6474 if (first_line)
6475 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006476 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006477 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006478 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006479 * This section is for auto-wrap of numeric lists. When not
6480 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6481 * flag will be set and open_line() will handle it (as seen
6482 * above). The code here (and in get_number_indent()) will
6483 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006484 */
6485 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006486 second_indent =
6487 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006488 if (second_indent >= 0)
6489 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006490#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006491 if (State & VREPLACE_FLAG)
6492 change_indent(INDENT_SET, second_indent,
6493 FALSE, NUL, TRUE);
6494 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006495#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006496#ifdef FEAT_COMMENTS
6497 if (leader_len > 0 && second_indent - leader_len > 0)
6498 {
6499 int i;
6500 int padding = second_indent - leader_len;
6501
6502 /* We started at the first_line of a numbered list
6503 * that has a comment. the open_line() function has
6504 * inserted the proper comment leader and positioned
6505 * the cursor at the end of the split line. Now we
6506 * add the additional whitespace needed after the
6507 * comment leader for the numbered list. */
6508 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006509 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006510 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006511 }
6512 else
6513 {
6514#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006515 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006516#ifdef FEAT_COMMENTS
6517 }
6518#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006519 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006520 }
6521 first_line = FALSE;
6522 }
6523
6524#ifdef FEAT_VREPLACE
6525 if (State & VREPLACE_FLAG)
6526 {
6527 /*
6528 * In VREPLACE mode we have backspaced over the text to be
6529 * moved, now we re-insert it into the new line.
6530 */
6531 ins_bytes(saved_text);
6532 vim_free(saved_text);
6533 }
6534 else
6535#endif
6536 {
6537 /*
6538 * Check if cursor is not past the NUL off the line, cindent
6539 * may have added or removed indent.
6540 */
6541 curwin->w_cursor.col += startcol;
6542 len = (colnr_T)STRLEN(ml_get_curline());
6543 if (curwin->w_cursor.col > len)
6544 curwin->w_cursor.col = len;
6545 }
6546
6547 haveto_redraw = TRUE;
6548#ifdef FEAT_CINDENT
6549 can_cindent = TRUE;
6550#endif
6551 /* moved the cursor, don't autoindent or cindent now */
6552 did_ai = FALSE;
6553#ifdef FEAT_SMARTINDENT
6554 did_si = FALSE;
6555 can_si = FALSE;
6556 can_si_back = FALSE;
6557#endif
6558 line_breakcheck();
6559 }
6560
6561 if (save_char != NUL) /* put back space after cursor */
6562 pchar_cursor(save_char);
6563
Bram Moolenaar0026d472014-09-09 16:32:39 +02006564#ifdef FEAT_LINEBREAK
6565 curwin->w_p_lbr = has_lbr;
6566#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006567 if (!format_only && haveto_redraw)
6568 {
6569 update_topline();
6570 redraw_curbuf_later(VALID);
6571 }
6572}
6573
6574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 * Called after inserting or deleting text: When 'formatoptions' includes the
6576 * 'a' flag format from the current line until the end of the paragraph.
6577 * Keep the cursor at the same position relative to the text.
6578 * The caller must have saved the cursor line for undo, following ones will be
6579 * saved here.
6580 */
6581 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006582auto_format(
6583 int trailblank, /* when TRUE also format with trailing blank */
6584 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585{
6586 pos_T pos;
6587 colnr_T len;
6588 char_u *old;
6589 char_u *new, *pnew;
6590 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006591 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006592
6593 if (!has_format_option(FO_AUTO))
6594 return;
6595
6596 pos = curwin->w_cursor;
6597 old = ml_get_curline();
6598
6599 /* may remove added space */
6600 check_auto_format(FALSE);
6601
6602 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6603 * user might insert normal text next. Also skip formatting when "1" is
6604 * in 'formatoptions' and there is a single character before the cursor.
6605 * Otherwise the line would be broken and when typing another non-white
6606 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006607 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 if (*old != NUL && !trailblank && wasatend)
6609 {
6610 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006611 cc = gchar_cursor();
6612 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6613 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006615 cc = gchar_cursor();
6616 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 {
6618 curwin->w_cursor = pos;
6619 return;
6620 }
6621 curwin->w_cursor = pos;
6622 }
6623
6624#ifdef FEAT_COMMENTS
6625 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6626 * comments. */
6627 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006628 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629 return;
6630#endif
6631
6632 /*
6633 * May start formatting in a previous line, so that after "x" a word is
6634 * moved to the previous line if it fits there now. Only when this is not
6635 * the start of a paragraph.
6636 */
6637 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6638 {
6639 --curwin->w_cursor.lnum;
6640 if (u_save_cursor() == FAIL)
6641 return;
6642 }
6643
6644 /*
6645 * Do the formatting and restore the cursor position. "saved_cursor" will
6646 * be adjusted for the text formatting.
6647 */
6648 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006649 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006650 curwin->w_cursor = saved_cursor;
6651 saved_cursor.lnum = 0;
6652
6653 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6654 {
6655 /* "cannot happen" */
6656 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6657 coladvance((colnr_T)MAXCOL);
6658 }
6659 else
6660 check_cursor_col();
6661
6662 /* Insert mode: If the cursor is now after the end of the line while it
6663 * previously wasn't, the line was broken. Because of the rule above we
6664 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6665 * formatted. */
6666 if (!wasatend && has_format_option(FO_WHITE_PAR))
6667 {
6668 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006669 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 if (curwin->w_cursor.col == len)
6671 {
6672 pnew = vim_strnsave(new, len + 2);
6673 pnew[len] = ' ';
6674 pnew[len + 1] = NUL;
6675 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6676 /* remove the space later */
6677 did_add_space = TRUE;
6678 }
6679 else
6680 /* may remove added space */
6681 check_auto_format(FALSE);
6682 }
6683
6684 check_cursor();
6685}
6686
6687/*
6688 * When an extra space was added to continue a paragraph for auto-formatting,
6689 * delete it now. The space must be under the cursor, just after the insert
6690 * position.
6691 */
6692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006693check_auto_format(
6694 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695{
6696 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006697 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
6699 if (did_add_space)
6700 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006701 cc = gchar_cursor();
6702 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006703 /* Somehow the space was removed already. */
6704 did_add_space = FALSE;
6705 else
6706 {
6707 if (!end_insert)
6708 {
6709 inc_cursor();
6710 c = gchar_cursor();
6711 dec_cursor();
6712 }
6713 if (c != NUL)
6714 {
6715 /* The space is no longer at the end of the line, delete it. */
6716 del_char(FALSE);
6717 did_add_space = FALSE;
6718 }
6719 }
6720 }
6721}
6722
6723/*
6724 * Find out textwidth to be used for formatting:
6725 * if 'textwidth' option is set, use it
6726 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6727 * if invalid value, use 0.
6728 * Set default to window width (maximum 79) for "gq" operator.
6729 */
6730 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006731comp_textwidth(
6732 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006733{
6734 int textwidth;
6735
6736 textwidth = curbuf->b_p_tw;
6737 if (textwidth == 0 && curbuf->b_p_wm)
6738 {
6739 /* The width is the window width minus 'wrapmargin' minus all the
6740 * things that add to the margin. */
6741 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6742#ifdef FEAT_CMDWIN
6743 if (cmdwin_type != 0)
6744 textwidth -= 1;
6745#endif
6746#ifdef FEAT_FOLDING
6747 textwidth -= curwin->w_p_fdc;
6748#endif
6749#ifdef FEAT_SIGNS
6750 if (curwin->w_buffer->b_signlist != NULL
6751# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01006752 || curwin->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006753# endif
6754 )
6755 textwidth -= 1;
6756#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006757 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 textwidth -= 8;
6759 }
6760 if (textwidth < 0)
6761 textwidth = 0;
6762 if (ff && textwidth == 0)
6763 {
6764 textwidth = W_WIDTH(curwin) - 1;
6765 if (textwidth > 79)
6766 textwidth = 79;
6767 }
6768 return textwidth;
6769}
6770
6771/*
6772 * Put a character in the redo buffer, for when just after a CTRL-V.
6773 */
6774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006775redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776{
6777 char_u buf[10];
6778
6779 /* Only digits need special treatment. Translate them into a string of
6780 * three digits. */
6781 if (VIM_ISDIGIT(c))
6782 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006783 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006784 AppendToRedobuff(buf);
6785 }
6786 else
6787 AppendCharToRedobuff(c);
6788}
6789
6790/*
6791 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006792 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 */
6794 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006795start_arrow(
6796 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006798 start_arrow_common(end_insert_pos, TRUE);
6799}
6800
6801/*
6802 * Like start_arrow() but with end_change argument.
6803 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6804 */
6805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006806start_arrow_with_change(
6807 pos_T *end_insert_pos, /* can be NULL */
6808 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006809{
6810 start_arrow_common(end_insert_pos, end_change);
6811 if (!end_change)
6812 {
6813 AppendCharToRedobuff(Ctrl_G);
6814 AppendCharToRedobuff('U');
6815 }
6816}
6817
6818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006819start_arrow_common(
6820 pos_T *end_insert_pos, /* can be NULL */
6821 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006822{
6823 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 {
6825 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006826 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 arrow_used = TRUE; /* this means we stopped the current insert */
6828 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006829#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006830 check_spell_redraw();
6831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832}
6833
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006834#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006835/*
6836 * If we skipped highlighting word at cursor, do it now.
6837 * It may be skipped again, thus reset spell_redraw_lnum first.
6838 */
6839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006840check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006841{
6842 if (spell_redraw_lnum != 0)
6843 {
6844 linenr_T lnum = spell_redraw_lnum;
6845
6846 spell_redraw_lnum = 0;
6847 redrawWinline(lnum, FALSE);
6848 }
6849}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006850
6851/*
6852 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6853 * spelled word, if there is one.
6854 */
6855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006856spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006857{
6858 pos_T tpos = curwin->w_cursor;
6859
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006860 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006861 if (curwin->w_cursor.col != tpos.col)
6862 start_arrow(&tpos);
6863}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006864#endif
6865
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866/*
6867 * stop_arrow() is called before a change is made in insert mode.
6868 * If an arrow key has been used, start a new insertion.
6869 * Returns FAIL if undo is impossible, shouldn't insert then.
6870 */
6871 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006872stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873{
6874 if (arrow_used)
6875 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006876 Insstart = curwin->w_cursor; /* new insertion starts here */
6877 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6878 /* Don't update the original insert position when moved to the
6879 * right, except when nothing was inserted yet. */
6880 update_Insstart_orig = FALSE;
6881 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6882
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 if (u_save_cursor() == OK)
6884 {
6885 arrow_used = FALSE;
6886 ins_need_undo = FALSE;
6887 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006888
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889 ai_col = 0;
6890#ifdef FEAT_VREPLACE
6891 if (State & VREPLACE_FLAG)
6892 {
6893 orig_line_count = curbuf->b_ml.ml_line_count;
6894 vr_lines_changed = 1;
6895 }
6896#endif
6897 ResetRedobuff();
6898 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006899 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006900 }
6901 else if (ins_need_undo)
6902 {
6903 if (u_save_cursor() == OK)
6904 ins_need_undo = FALSE;
6905 }
6906
6907#ifdef FEAT_FOLDING
6908 /* Always open fold at the cursor line when inserting something. */
6909 foldOpenCursor();
6910#endif
6911
6912 return (arrow_used || ins_need_undo ? FAIL : OK);
6913}
6914
6915/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006916 * Do a few things to stop inserting.
6917 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6918 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006919 */
6920 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006921stop_insert(
6922 pos_T *end_insert_pos,
6923 int esc, /* called by ins_esc() */
6924 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006926 int cc;
6927 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928
6929 stop_redo_ins();
6930 replace_flush(); /* abandon replace stack */
6931
6932 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006933 * Save the inserted text for later redo with ^@ and CTRL-A.
6934 * Don't do it when "restart_edit" was set and nothing was inserted,
6935 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006937 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006938 if (did_restart_edit == 0 || (ptr != NULL
6939 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006940 {
6941 vim_free(last_insert);
6942 last_insert = ptr;
6943 last_insert_skip = new_insert_skip;
6944 }
6945 else
6946 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006948 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 {
6950 /* Auto-format now. It may seem strange to do this when stopping an
6951 * insertion (or moving the cursor), but it's required when appending
6952 * a line and having it end in a space. But only do it when something
6953 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006954 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006955 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006956 pos_T tpos = curwin->w_cursor;
6957
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 /* When the cursor is at the end of the line after a space the
6959 * formatting will move it to the following word. Avoid that by
6960 * moving the cursor onto the space. */
6961 cc = 'x';
6962 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6963 {
6964 dec_cursor();
6965 cc = gchar_cursor();
6966 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006967 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968 }
6969
6970 auto_format(TRUE, FALSE);
6971
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006972 if (vim_iswhite(cc))
6973 {
6974 if (gchar_cursor() != NUL)
6975 inc_cursor();
6976#ifdef FEAT_VIRTUALEDIT
6977 /* If the cursor is still at the same character, also keep
6978 * the "coladd". */
6979 if (gchar_cursor() == NUL
6980 && curwin->w_cursor.lnum == tpos.lnum
6981 && curwin->w_cursor.col == tpos.col)
6982 curwin->w_cursor.coladd = tpos.coladd;
6983#endif
6984 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006985 }
6986
6987 /* If a space was inserted for auto-formatting, remove it now. */
6988 check_auto_format(TRUE);
6989
6990 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006991 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006992 * Do this when ESC was used or moving the cursor up/down.
6993 * Check for the old position still being valid, just in case the text
6994 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006995 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006996 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6997 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006999 pos_T tpos = curwin->w_cursor;
7000
7001 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007002 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007003 for (;;)
7004 {
7005 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7006 --curwin->w_cursor.col;
7007 cc = gchar_cursor();
7008 if (!vim_iswhite(cc))
7009 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007010 if (del_char(TRUE) == FAIL)
7011 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007012 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007013 if (curwin->w_cursor.lnum != tpos.lnum)
7014 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007015 else
7016 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007017 /* reset tpos, could have been invalidated in the loop above */
7018 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007019 tpos.col++;
7020 if (cc != NUL && gchar_pos(&tpos) == NUL)
7021 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7022 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 /* <C-S-Right> may have started Visual mode, adjust the position for
7025 * deleted characters. */
7026 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7027 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007028 int len = (int)STRLEN(ml_get_curline());
7029
7030 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007032 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007033#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007036 }
7037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 }
7039 }
7040 did_ai = FALSE;
7041#ifdef FEAT_SMARTINDENT
7042 did_si = FALSE;
7043 can_si = FALSE;
7044 can_si_back = FALSE;
7045#endif
7046
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007047 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7048 * now in a different buffer. */
7049 if (end_insert_pos != NULL)
7050 {
7051 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007052 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007053 curbuf->b_op_end = *end_insert_pos;
7054 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055}
7056
7057/*
7058 * Set the last inserted text to a single character.
7059 * Used for the replace command.
7060 */
7061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007062set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063{
7064 char_u *s;
7065
7066 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007067 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 if (last_insert != NULL)
7069 {
7070 s = last_insert;
7071 /* Use the CTRL-V only when entering a special char */
7072 if (c < ' ' || c == DEL)
7073 *s++ = Ctrl_V;
7074 s = add_char2buf(c, s);
7075 *s++ = ESC;
7076 *s++ = NUL;
7077 last_insert_skip = 0;
7078 }
7079}
7080
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007081#if defined(EXITFREE) || defined(PROTO)
7082 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007083free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007084{
7085 vim_free(last_insert);
7086 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007087# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007088 vim_free(compl_orig_text);
7089 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007090# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007091}
7092#endif
7093
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094/*
7095 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7096 * and CSI. Handle multi-byte characters.
7097 * Returns a pointer to after the added bytes.
7098 */
7099 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007100add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007101{
7102#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007103 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007104 int i;
7105 int len;
7106
7107 len = (*mb_char2bytes)(c, temp);
7108 for (i = 0; i < len; ++i)
7109 {
7110 c = temp[i];
7111#endif
7112 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7113 if (c == K_SPECIAL)
7114 {
7115 *s++ = K_SPECIAL;
7116 *s++ = KS_SPECIAL;
7117 *s++ = KE_FILLER;
7118 }
7119#ifdef FEAT_GUI
7120 else if (c == CSI)
7121 {
7122 *s++ = CSI;
7123 *s++ = KS_EXTRA;
7124 *s++ = (int)KE_CSI;
7125 }
7126#endif
7127 else
7128 *s++ = c;
7129#ifdef FEAT_MBYTE
7130 }
7131#endif
7132 return s;
7133}
7134
7135/*
7136 * move cursor to start of line
7137 * if flags & BL_WHITE move to first non-white
7138 * if flags & BL_SOL move to first non-white if startofline is set,
7139 * otherwise keep "curswant" column
7140 * if flags & BL_FIX don't leave the cursor on a NUL.
7141 */
7142 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007143beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144{
7145 if ((flags & BL_SOL) && !p_sol)
7146 coladvance(curwin->w_curswant);
7147 else
7148 {
7149 curwin->w_cursor.col = 0;
7150#ifdef FEAT_VIRTUALEDIT
7151 curwin->w_cursor.coladd = 0;
7152#endif
7153
7154 if (flags & (BL_WHITE | BL_SOL))
7155 {
7156 char_u *ptr;
7157
7158 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7159 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7160 ++curwin->w_cursor.col;
7161 }
7162 curwin->w_set_curswant = TRUE;
7163 }
7164}
7165
7166/*
7167 * oneright oneleft cursor_down cursor_up
7168 *
7169 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007170 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 * Return OK when successful, FAIL when we hit a line of file boundary.
7172 */
7173
7174 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007175oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007176{
7177 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179
7180#ifdef FEAT_VIRTUALEDIT
7181 if (virtual_active())
7182 {
7183 pos_T prevpos = curwin->w_cursor;
7184
7185 /* Adjust for multi-wide char (excluding TAB) */
7186 ptr = ml_get_cursor();
7187 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007188# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007189 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007190# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007191 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007192# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 ))
7194 ? ptr2cells(ptr) : 1));
7195 curwin->w_set_curswant = TRUE;
7196 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7197 return (prevpos.col != curwin->w_cursor.col
7198 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7199 }
7200#endif
7201
7202 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007203 if (*ptr == NUL)
7204 return FAIL; /* already at the very end */
7205
Bram Moolenaar071d4272004-06-13 20:20:40 +00007206#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007207 if (has_mbyte)
7208 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007209 else
7210#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007211 l = 1;
7212
7213 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7214 * contains "onemore". */
7215 if (ptr[l] == NUL
7216#ifdef FEAT_VIRTUALEDIT
7217 && (ve_flags & VE_ONEMORE) == 0
7218#endif
7219 )
7220 return FAIL;
7221 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222
7223 curwin->w_set_curswant = TRUE;
7224 return OK;
7225}
7226
7227 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007228oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229{
7230#ifdef FEAT_VIRTUALEDIT
7231 if (virtual_active())
7232 {
7233 int width;
7234 int v = getviscol();
7235
7236 if (v == 0)
7237 return FAIL;
7238
7239# ifdef FEAT_LINEBREAK
7240 /* We might get stuck on 'showbreak', skip over it. */
7241 width = 1;
7242 for (;;)
7243 {
7244 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007245 /* getviscol() is slow, skip it when 'showbreak' is empty,
7246 * 'breakindent' is not set and there are no multi-byte
7247 * characters */
7248 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007249# ifdef FEAT_MBYTE
7250 && !has_mbyte
7251# endif
7252 ) || getviscol() < v)
7253 break;
7254 ++width;
7255 }
7256# else
7257 coladvance(v - 1);
7258# endif
7259
7260 if (curwin->w_cursor.coladd == 1)
7261 {
7262 char_u *ptr;
7263
7264 /* Adjust for multi-wide char (not a TAB) */
7265 ptr = ml_get_cursor();
7266 if (*ptr != TAB && vim_isprintc(
7267# ifdef FEAT_MBYTE
7268 (*mb_ptr2char)(ptr)
7269# else
7270 *ptr
7271# endif
7272 ) && ptr2cells(ptr) > 1)
7273 curwin->w_cursor.coladd = 0;
7274 }
7275
7276 curwin->w_set_curswant = TRUE;
7277 return OK;
7278 }
7279#endif
7280
7281 if (curwin->w_cursor.col == 0)
7282 return FAIL;
7283
7284 curwin->w_set_curswant = TRUE;
7285 --curwin->w_cursor.col;
7286
7287#ifdef FEAT_MBYTE
7288 /* if the character on the left of the current cursor is a multi-byte
7289 * character, move to its first byte */
7290 if (has_mbyte)
7291 mb_adjust_cursor();
7292#endif
7293 return OK;
7294}
7295
7296 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007297cursor_up(
7298 long n,
7299 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300{
7301 linenr_T lnum;
7302
7303 if (n > 0)
7304 {
7305 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007306 /* This fails if the cursor is already in the first line or the count
7307 * is larger than the line number and '-' is in 'cpoptions' */
7308 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007309 return FAIL;
7310 if (n >= lnum)
7311 lnum = 1;
7312 else
7313#ifdef FEAT_FOLDING
7314 if (hasAnyFolding(curwin))
7315 {
7316 /*
7317 * Count each sequence of folded lines as one logical line.
7318 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007319 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007320 (void)hasFolding(lnum, &lnum, NULL);
7321
7322 while (n--)
7323 {
7324 /* move up one line */
7325 --lnum;
7326 if (lnum <= 1)
7327 break;
7328 /* If we entered a fold, move to the beginning, unless in
7329 * Insert mode or when 'foldopen' contains "all": it will open
7330 * in a moment. */
7331 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7332 (void)hasFolding(lnum, &lnum, NULL);
7333 }
7334 if (lnum < 1)
7335 lnum = 1;
7336 }
7337 else
7338#endif
7339 lnum -= n;
7340 curwin->w_cursor.lnum = lnum;
7341 }
7342
7343 /* try to advance to the column we want to be at */
7344 coladvance(curwin->w_curswant);
7345
7346 if (upd_topline)
7347 update_topline(); /* make sure curwin->w_topline is valid */
7348
7349 return OK;
7350}
7351
7352/*
7353 * Cursor down a number of logical lines.
7354 */
7355 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007356cursor_down(
7357 long n,
7358 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359{
7360 linenr_T lnum;
7361
7362 if (n > 0)
7363 {
7364 lnum = curwin->w_cursor.lnum;
7365#ifdef FEAT_FOLDING
7366 /* Move to last line of fold, will fail if it's the end-of-file. */
7367 (void)hasFolding(lnum, NULL, &lnum);
7368#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007369 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007370 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007371 if (lnum >= curbuf->b_ml.ml_line_count
7372 || (lnum + n > curbuf->b_ml.ml_line_count
7373 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007374 return FAIL;
7375 if (lnum + n >= curbuf->b_ml.ml_line_count)
7376 lnum = curbuf->b_ml.ml_line_count;
7377 else
7378#ifdef FEAT_FOLDING
7379 if (hasAnyFolding(curwin))
7380 {
7381 linenr_T last;
7382
7383 /* count each sequence of folded lines as one logical line */
7384 while (n--)
7385 {
7386 if (hasFolding(lnum, NULL, &last))
7387 lnum = last + 1;
7388 else
7389 ++lnum;
7390 if (lnum >= curbuf->b_ml.ml_line_count)
7391 break;
7392 }
7393 if (lnum > curbuf->b_ml.ml_line_count)
7394 lnum = curbuf->b_ml.ml_line_count;
7395 }
7396 else
7397#endif
7398 lnum += n;
7399 curwin->w_cursor.lnum = lnum;
7400 }
7401
7402 /* try to advance to the column we want to be at */
7403 coladvance(curwin->w_curswant);
7404
7405 if (upd_topline)
7406 update_topline(); /* make sure curwin->w_topline is valid */
7407
7408 return OK;
7409}
7410
7411/*
7412 * Stuff the last inserted text in the read buffer.
7413 * Last_insert actually is a copy of the redo buffer, so we
7414 * first have to remove the command.
7415 */
7416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007417stuff_inserted(
7418 int c, /* Command character to be inserted */
7419 long count, /* Repeat this many times */
7420 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007421{
7422 char_u *esc_ptr;
7423 char_u *ptr;
7424 char_u *last_ptr;
7425 char_u last = NUL;
7426
7427 ptr = get_last_insert();
7428 if (ptr == NULL)
7429 {
7430 EMSG(_(e_noinstext));
7431 return FAIL;
7432 }
7433
7434 /* may want to stuff the command character, to start Insert mode */
7435 if (c != NUL)
7436 stuffcharReadbuff(c);
7437 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7438 *esc_ptr = NUL; /* remove the ESC */
7439
7440 /* when the last char is either "0" or "^" it will be quoted if no ESC
7441 * comes after it OR if it will inserted more than once and "ptr"
7442 * starts with ^D. -- Acevedo
7443 */
7444 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7445 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7446 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7447 {
7448 last = *last_ptr;
7449 *last_ptr = NUL;
7450 }
7451
7452 do
7453 {
7454 stuffReadbuff(ptr);
7455 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7456 if (last)
7457 stuffReadbuff((char_u *)(last == '0'
7458 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7459 : IF_EB("\026^", CTRL_V_STR "^")));
7460 }
7461 while (--count > 0);
7462
7463 if (last)
7464 *last_ptr = last;
7465
7466 if (esc_ptr != NULL)
7467 *esc_ptr = ESC; /* put the ESC back */
7468
7469 /* may want to stuff a trailing ESC, to get out of Insert mode */
7470 if (!no_esc)
7471 stuffcharReadbuff(ESC);
7472
7473 return OK;
7474}
7475
7476 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007477get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007478{
7479 if (last_insert == NULL)
7480 return NULL;
7481 return last_insert + last_insert_skip;
7482}
7483
7484/*
7485 * Get last inserted string, and remove trailing <Esc>.
7486 * Returns pointer to allocated memory (must be freed) or NULL.
7487 */
7488 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007489get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490{
7491 char_u *s;
7492 int len;
7493
7494 if (last_insert == NULL)
7495 return NULL;
7496 s = vim_strsave(last_insert + last_insert_skip);
7497 if (s != NULL)
7498 {
7499 len = (int)STRLEN(s);
7500 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7501 s[len - 1] = NUL;
7502 }
7503 return s;
7504}
7505
7506/*
7507 * Check the word in front of the cursor for an abbreviation.
7508 * Called when the non-id character "c" has been entered.
7509 * When an abbreviation is recognized it is removed from the text and
7510 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7511 */
7512 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007513echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007514{
7515 /* Don't check for abbreviation in paste mode, when disabled and just
7516 * after moving around with cursor keys. */
7517 if (p_paste || no_abbr || arrow_used)
7518 return FALSE;
7519
7520 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7521 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7522}
7523
7524/*
7525 * replace-stack functions
7526 *
7527 * When replacing characters, the replaced characters are remembered for each
7528 * new character. This is used to re-insert the old text when backspacing.
7529 *
7530 * There is a NUL headed list of characters for each character that is
7531 * currently in the file after the insertion point. When BS is used, one NUL
7532 * headed list is put back for the deleted character.
7533 *
7534 * For a newline, there are two NUL headed lists. One contains the characters
7535 * that the NL replaced. The extra one stores the characters after the cursor
7536 * that were deleted (always white space).
7537 *
7538 * Replace_offset is normally 0, in which case replace_push will add a new
7539 * character at the end of the stack. If replace_offset is not 0, that many
7540 * characters will be left on the stack above the newly inserted character.
7541 */
7542
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007543static char_u *replace_stack = NULL;
7544static long replace_stack_nr = 0; /* next entry in replace stack */
7545static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546
7547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548replace_push(
7549 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550{
7551 char_u *p;
7552
7553 if (replace_stack_nr < replace_offset) /* nothing to do */
7554 return;
7555 if (replace_stack_len <= replace_stack_nr)
7556 {
7557 replace_stack_len += 50;
7558 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7559 if (p == NULL) /* out of memory */
7560 {
7561 replace_stack_len -= 50;
7562 return;
7563 }
7564 if (replace_stack != NULL)
7565 {
7566 mch_memmove(p, replace_stack,
7567 (size_t)(replace_stack_nr * sizeof(char_u)));
7568 vim_free(replace_stack);
7569 }
7570 replace_stack = p;
7571 }
7572 p = replace_stack + replace_stack_nr - replace_offset;
7573 if (replace_offset)
7574 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7575 *p = c;
7576 ++replace_stack_nr;
7577}
7578
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007579#if defined(FEAT_MBYTE) || defined(PROTO)
7580/*
7581 * Push a character onto the replace stack. Handles a multi-byte character in
7582 * reverse byte order, so that the first byte is popped off first.
7583 * Return the number of bytes done (includes composing characters).
7584 */
7585 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007586replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007587{
7588 int l = (*mb_ptr2len)(p);
7589 int j;
7590
7591 for (j = l - 1; j >= 0; --j)
7592 replace_push(p[j]);
7593 return l;
7594}
7595#endif
7596
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597/*
7598 * Pop one item from the replace stack.
7599 * return -1 if stack empty
7600 * return replaced character or NUL otherwise
7601 */
7602 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007603replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604{
7605 if (replace_stack_nr == 0)
7606 return -1;
7607 return (int)replace_stack[--replace_stack_nr];
7608}
7609
7610/*
7611 * Join the top two items on the replace stack. This removes to "off"'th NUL
7612 * encountered.
7613 */
7614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007615replace_join(
7616 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007617{
7618 int i;
7619
7620 for (i = replace_stack_nr; --i >= 0; )
7621 if (replace_stack[i] == NUL && off-- <= 0)
7622 {
7623 --replace_stack_nr;
7624 mch_memmove(replace_stack + i, replace_stack + i + 1,
7625 (size_t)(replace_stack_nr - i));
7626 return;
7627 }
7628}
7629
7630/*
7631 * Pop bytes from the replace stack until a NUL is found, and insert them
7632 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7633 */
7634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007635replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007636{
7637 int cc;
7638 int oldState = State;
7639
7640 State = NORMAL; /* don't want REPLACE here */
7641 while ((cc = replace_pop()) > 0)
7642 {
7643#ifdef FEAT_MBYTE
7644 mb_replace_pop_ins(cc);
7645#else
7646 ins_char(cc);
7647#endif
7648 dec_cursor();
7649 }
7650 State = oldState;
7651}
7652
7653#ifdef FEAT_MBYTE
7654/*
7655 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7656 * indicates a multi-byte char, pop the other bytes too.
7657 */
7658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007659mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660{
7661 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007662 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 int i;
7664 int c;
7665
7666 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7667 {
7668 buf[0] = cc;
7669 for (i = 1; i < n; ++i)
7670 buf[i] = replace_pop();
7671 ins_bytes_len(buf, n);
7672 }
7673 else
7674 ins_char(cc);
7675
7676 if (enc_utf8)
7677 /* Handle composing chars. */
7678 for (;;)
7679 {
7680 c = replace_pop();
7681 if (c == -1) /* stack empty */
7682 break;
7683 if ((n = MB_BYTE2LEN(c)) == 1)
7684 {
7685 /* Not a multi-byte char, put it back. */
7686 replace_push(c);
7687 break;
7688 }
7689 else
7690 {
7691 buf[0] = c;
7692 for (i = 1; i < n; ++i)
7693 buf[i] = replace_pop();
7694 if (utf_iscomposing(utf_ptr2char(buf)))
7695 ins_bytes_len(buf, n);
7696 else
7697 {
7698 /* Not a composing char, put it back. */
7699 for (i = n - 1; i >= 0; --i)
7700 replace_push(buf[i]);
7701 break;
7702 }
7703 }
7704 }
7705}
7706#endif
7707
7708/*
7709 * make the replace stack empty
7710 * (called when exiting replace mode)
7711 */
7712 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007713replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714{
7715 vim_free(replace_stack);
7716 replace_stack = NULL;
7717 replace_stack_len = 0;
7718 replace_stack_nr = 0;
7719}
7720
7721/*
7722 * Handle doing a BS for one character.
7723 * cc < 0: replace stack empty, just move cursor
7724 * cc == 0: character was inserted, delete it
7725 * cc > 0: character was replaced, put cc (first byte of original char) back
7726 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007727 * When "limit_col" is >= 0, don't delete before this column. Matters when
7728 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007729 */
7730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007731replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732{
7733 int cc;
7734#ifdef FEAT_VREPLACE
7735 int orig_len = 0;
7736 int ins_len;
7737 int orig_vcols = 0;
7738 colnr_T start_vcol;
7739 char_u *p;
7740 int i;
7741 int vcol;
7742#endif
7743
7744 cc = replace_pop();
7745 if (cc > 0)
7746 {
7747#ifdef FEAT_VREPLACE
7748 if (State & VREPLACE_FLAG)
7749 {
7750 /* Get the number of screen cells used by the character we are
7751 * going to delete. */
7752 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7753 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7754 }
7755#endif
7756#ifdef FEAT_MBYTE
7757 if (has_mbyte)
7758 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007759 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760# ifdef FEAT_VREPLACE
7761 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007762 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007763# endif
7764 replace_push(cc);
7765 }
7766 else
7767#endif
7768 {
7769 pchar_cursor(cc);
7770#ifdef FEAT_VREPLACE
7771 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007772 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007773#endif
7774 }
7775 replace_pop_ins();
7776
7777#ifdef FEAT_VREPLACE
7778 if (State & VREPLACE_FLAG)
7779 {
7780 /* Get the number of screen cells used by the inserted characters */
7781 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007782 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 vcol = start_vcol;
7784 for (i = 0; i < ins_len; ++i)
7785 {
7786 vcol += chartabsize(p + i, vcol);
7787#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007788 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789#endif
7790 }
7791 vcol -= start_vcol;
7792
7793 /* Delete spaces that were inserted after the cursor to keep the
7794 * text aligned. */
7795 curwin->w_cursor.col += ins_len;
7796 while (vcol > orig_vcols && gchar_cursor() == ' ')
7797 {
7798 del_char(FALSE);
7799 ++orig_vcols;
7800 }
7801 curwin->w_cursor.col -= ins_len;
7802 }
7803#endif
7804
7805 /* mark the buffer as changed and prepare for displaying */
7806 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7807 }
7808 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007809 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810}
7811
7812#ifdef FEAT_CINDENT
7813/*
7814 * Return TRUE if C-indenting is on.
7815 */
7816 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007817cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818{
7819 return (!p_paste && (curbuf->b_p_cin
7820# ifdef FEAT_EVAL
7821 || *curbuf->b_p_inde != NUL
7822# endif
7823 ));
7824}
7825#endif
7826
7827#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7828/*
7829 * Re-indent the current line, based on the current contents of it and the
7830 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7831 * confused what all the part that handles Control-T is doing that I'm not.
7832 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7833 */
7834
7835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007836fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007838 int amount = get_the_indent();
7839
7840 if (amount >= 0)
7841 {
7842 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7843 if (linewhite(curwin->w_cursor.lnum))
7844 did_ai = TRUE; /* delete the indent if the line stays empty */
7845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846}
7847
7848 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007849fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850{
7851 if (p_paste)
7852 return;
7853# ifdef FEAT_LISP
7854 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7855 fixthisline(get_lisp_indent);
7856# endif
7857# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7858 else
7859# endif
7860# ifdef FEAT_CINDENT
7861 if (cindent_on())
7862 do_c_expr_indent();
7863# endif
7864}
7865
7866#endif
7867
7868#ifdef FEAT_CINDENT
7869/*
7870 * return TRUE if 'cinkeys' contains the key "keytyped",
7871 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007872 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7874 *
7875 * "keytyped" can have a few special values:
7876 * KEY_OPEN_FORW
7877 * KEY_OPEN_BACK
7878 * KEY_COMPLETE just finished completion.
7879 *
7880 * If line_is_empty is TRUE accept keys with '0' before them.
7881 */
7882 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007883in_cinkeys(
7884 int keytyped,
7885 int when,
7886 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887{
7888 char_u *look;
7889 int try_match;
7890 int try_match_word;
7891 char_u *p;
7892 char_u *line;
7893 int icase;
7894 int i;
7895
Bram Moolenaar30848942009-12-24 14:46:12 +00007896 if (keytyped == NUL)
7897 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7898 return FALSE;
7899
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900#ifdef FEAT_EVAL
7901 if (*curbuf->b_p_inde != NUL)
7902 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7903 else
7904#endif
7905 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7906 while (*look)
7907 {
7908 /*
7909 * Find out if we want to try a match with this key, depending on
7910 * 'when' and a '*' or '!' before the key.
7911 */
7912 switch (when)
7913 {
7914 case '*': try_match = (*look == '*'); break;
7915 case '!': try_match = (*look == '!'); break;
7916 default: try_match = (*look != '*'); break;
7917 }
7918 if (*look == '*' || *look == '!')
7919 ++look;
7920
7921 /*
7922 * If there is a '0', only accept a match if the line is empty.
7923 * But may still match when typing last char of a word.
7924 */
7925 if (*look == '0')
7926 {
7927 try_match_word = try_match;
7928 if (!line_is_empty)
7929 try_match = FALSE;
7930 ++look;
7931 }
7932 else
7933 try_match_word = FALSE;
7934
7935 /*
7936 * does it look like a control character?
7937 */
7938 if (*look == '^'
7939#ifdef EBCDIC
7940 && (Ctrl_chr(look[1]) != 0)
7941#else
7942 && look[1] >= '?' && look[1] <= '_'
7943#endif
7944 )
7945 {
7946 if (try_match && keytyped == Ctrl_chr(look[1]))
7947 return TRUE;
7948 look += 2;
7949 }
7950 /*
7951 * 'o' means "o" command, open forward.
7952 * 'O' means "O" command, open backward.
7953 */
7954 else if (*look == 'o')
7955 {
7956 if (try_match && keytyped == KEY_OPEN_FORW)
7957 return TRUE;
7958 ++look;
7959 }
7960 else if (*look == 'O')
7961 {
7962 if (try_match && keytyped == KEY_OPEN_BACK)
7963 return TRUE;
7964 ++look;
7965 }
7966
7967 /*
7968 * 'e' means to check for "else" at start of line and just before the
7969 * cursor.
7970 */
7971 else if (*look == 'e')
7972 {
7973 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7974 {
7975 p = ml_get_curline();
7976 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7977 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7978 return TRUE;
7979 }
7980 ++look;
7981 }
7982
7983 /*
7984 * ':' only causes an indent if it is at the end of a label or case
7985 * statement, or when it was before typing the ':' (to fix
7986 * class::method for C++).
7987 */
7988 else if (*look == ':')
7989 {
7990 if (try_match && keytyped == ':')
7991 {
7992 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007993 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007994 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007995 /* Need to get the line again after cin_islabel(). */
7996 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997 if (curwin->w_cursor.col > 2
7998 && p[curwin->w_cursor.col - 1] == ':'
7999 && p[curwin->w_cursor.col - 2] == ':')
8000 {
8001 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008002 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008003 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008004 p = ml_get_curline();
8005 p[curwin->w_cursor.col - 1] = ':';
8006 if (i)
8007 return TRUE;
8008 }
8009 }
8010 ++look;
8011 }
8012
8013
8014 /*
8015 * Is it a key in <>, maybe?
8016 */
8017 else if (*look == '<')
8018 {
8019 if (try_match)
8020 {
8021 /*
8022 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8023 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8024 * >, *, : and ! keys if they really really want to.
8025 */
8026 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8027 && keytyped == look[1])
8028 return TRUE;
8029
8030 if (keytyped == get_special_key_code(look + 1))
8031 return TRUE;
8032 }
8033 while (*look && *look != '>')
8034 look++;
8035 while (*look == '>')
8036 look++;
8037 }
8038
8039 /*
8040 * Is it a word: "=word"?
8041 */
8042 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8043 {
8044 ++look;
8045 if (*look == '~')
8046 {
8047 icase = TRUE;
8048 ++look;
8049 }
8050 else
8051 icase = FALSE;
8052 p = vim_strchr(look, ',');
8053 if (p == NULL)
8054 p = look + STRLEN(look);
8055 if ((try_match || try_match_word)
8056 && curwin->w_cursor.col >= (colnr_T)(p - look))
8057 {
8058 int match = FALSE;
8059
8060#ifdef FEAT_INS_EXPAND
8061 if (keytyped == KEY_COMPLETE)
8062 {
8063 char_u *s;
8064
8065 /* Just completed a word, check if it starts with "look".
8066 * search back for the start of a word. */
8067 line = ml_get_curline();
8068# ifdef FEAT_MBYTE
8069 if (has_mbyte)
8070 {
8071 char_u *n;
8072
8073 for (s = line + curwin->w_cursor.col; s > line; s = n)
8074 {
8075 n = mb_prevptr(line, s);
8076 if (!vim_iswordp(n))
8077 break;
8078 }
8079 }
8080 else
8081# endif
8082 for (s = line + curwin->w_cursor.col; s > line; --s)
8083 if (!vim_iswordc(s[-1]))
8084 break;
8085 if (s + (p - look) <= line + curwin->w_cursor.col
8086 && (icase
8087 ? MB_STRNICMP(s, look, p - look)
8088 : STRNCMP(s, look, p - look)) == 0)
8089 match = TRUE;
8090 }
8091 else
8092#endif
8093 /* TODO: multi-byte */
8094 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8095 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8096 {
8097 line = ml_get_cursor();
8098 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8099 || !vim_iswordc(line[-(p - look) - 1]))
8100 && (icase
8101 ? MB_STRNICMP(line - (p - look), look, p - look)
8102 : STRNCMP(line - (p - look), look, p - look))
8103 == 0)
8104 match = TRUE;
8105 }
8106 if (match && try_match_word && !try_match)
8107 {
8108 /* "0=word": Check if there are only blanks before the
8109 * word. */
8110 line = ml_get_curline();
8111 if ((int)(skipwhite(line) - line) !=
8112 (int)(curwin->w_cursor.col - (p - look)))
8113 match = FALSE;
8114 }
8115 if (match)
8116 return TRUE;
8117 }
8118 look = p;
8119 }
8120
8121 /*
8122 * ok, it's a boring generic character.
8123 */
8124 else
8125 {
8126 if (try_match && *look == keytyped)
8127 return TRUE;
8128 ++look;
8129 }
8130
8131 /*
8132 * Skip over ", ".
8133 */
8134 look = skip_to_option_part(look);
8135 }
8136 return FALSE;
8137}
8138#endif /* FEAT_CINDENT */
8139
8140#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8141/*
8142 * Map Hebrew keyboard when in hkmap mode.
8143 */
8144 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008145hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146{
8147 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8148 {
8149 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8150 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8151 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8152 static char_u map[26] =
8153 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8154 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8155 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8156 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8157 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8158 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8159 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8160 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8161 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8162
8163 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8164 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8165 /* '-1'='sofit' */
8166 else if (c == 'x')
8167 return 'X';
8168 else if (c == 'q')
8169 return '\''; /* {geresh}={'} */
8170 else if (c == 246)
8171 return ' '; /* \"o --> ' ' for a german keyboard */
8172 else if (c == 228)
8173 return ' '; /* \"a --> ' ' -- / -- */
8174 else if (c == 252)
8175 return ' '; /* \"u --> ' ' -- / -- */
8176#ifdef EBCDIC
8177 else if (islower(c))
8178#else
8179 /* NOTE: islower() does not do the right thing for us on Linux so we
8180 * do this the same was as 5.7 and previous, so it works correctly on
8181 * all systems. Specifically, the e.g. Delete and Arrow keys are
8182 * munged and won't work if e.g. searching for Hebrew text.
8183 */
8184 else if (c >= 'a' && c <= 'z')
8185#endif
8186 return (int)(map[CharOrdLow(c)] + p_aleph);
8187 else
8188 return c;
8189 }
8190 else
8191 {
8192 switch (c)
8193 {
8194 case '`': return ';';
8195 case '/': return '.';
8196 case '\'': return ',';
8197 case 'q': return '/';
8198 case 'w': return '\'';
8199
8200 /* Hebrew letters - set offset from 'a' */
8201 case ',': c = '{'; break;
8202 case '.': c = 'v'; break;
8203 case ';': c = 't'; break;
8204 default: {
8205 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8206
8207#ifdef EBCDIC
8208 /* see note about islower() above */
8209 if (!islower(c))
8210#else
8211 if (c < 'a' || c > 'z')
8212#endif
8213 return c;
8214 c = str[CharOrdLow(c)];
8215 break;
8216 }
8217 }
8218
8219 return (int)(CharOrdLow(c) + p_aleph);
8220 }
8221}
8222#endif
8223
8224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008225ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226{
8227 int need_redraw = FALSE;
8228 int regname;
8229 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008230 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231
8232 /*
8233 * If we are going to wait for a character, show a '"'.
8234 */
8235 pc_status = PC_STATUS_UNSET;
8236 if (redrawing() && !char_avail())
8237 {
8238 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008239 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240
8241 edit_putchar('"', TRUE);
8242#ifdef FEAT_CMDL_INFO
8243 add_to_showcmd_c(Ctrl_R);
8244#endif
8245 }
8246
8247#ifdef USE_ON_FLY_SCROLL
8248 dont_scroll = TRUE; /* disallow scrolling here */
8249#endif
8250
8251 /*
8252 * Don't map the register name. This also prevents the mode message to be
8253 * deleted when ESC is hit.
8254 */
8255 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008256 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8259 {
8260 /* Get a third key for literal register insertion */
8261 literally = regname;
8262#ifdef FEAT_CMDL_INFO
8263 add_to_showcmd_c(literally);
8264#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008265 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008266 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 }
8268 --no_mapping;
8269
8270#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008271 /* Don't call u_sync() while typing the expression or giving an error
8272 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 ++no_u_sync;
8274 if (regname == '=')
8275 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008276# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008278# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008279 /* Sync undo when evaluating the expression calls setline() or
8280 * append(), so that it can be undone separately. */
8281 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008282
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008284# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 /* Restore the Input Method. */
8286 if (im_on)
8287 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008288# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008290 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8291 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008292 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295 else
8296 {
8297#endif
8298 if (literally == Ctrl_O || literally == Ctrl_P)
8299 {
8300 /* Append the command to the redo buffer. */
8301 AppendCharToRedobuff(Ctrl_R);
8302 AppendCharToRedobuff(literally);
8303 AppendCharToRedobuff(regname);
8304
8305 do_put(regname, BACKWARD, 1L,
8306 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8307 }
8308 else if (insert_reg(regname, literally) == FAIL)
8309 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008310 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 need_redraw = TRUE; /* remove the '"' */
8312 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008313 else if (stop_insert_mode)
8314 /* When the '=' register was used and a function was invoked that
8315 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8316 * insert anything, need to remove the '"' */
8317 need_redraw = TRUE;
8318
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319#ifdef FEAT_EVAL
8320 }
8321 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008322 if (u_sync_once == 1)
8323 ins_need_undo = TRUE;
8324 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325#endif
8326#ifdef FEAT_CMDL_INFO
8327 clear_showcmd();
8328#endif
8329
8330 /* If the inserted register is empty, we need to remove the '"' */
8331 if (need_redraw || stuff_empty())
8332 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008333
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008334 /* Disallow starting Visual mode here, would get a weird mode. */
8335 if (!vis_active && VIsual_active)
8336 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337}
8338
8339/*
8340 * CTRL-G commands in Insert mode.
8341 */
8342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008343ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008344{
8345 int c;
8346
8347#ifdef FEAT_INS_EXPAND
8348 /* Right after CTRL-X the cursor will be after the ruler. */
8349 setcursor();
8350#endif
8351
8352 /*
8353 * Don't map the second key. This also prevents the mode message to be
8354 * deleted when ESC is hit.
8355 */
8356 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008357 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 --no_mapping;
8359 switch (c)
8360 {
8361 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8362 case K_UP:
8363 case Ctrl_K:
8364 case 'k': ins_up(TRUE);
8365 break;
8366
8367 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8368 case K_DOWN:
8369 case Ctrl_J:
8370 case 'j': ins_down(TRUE);
8371 break;
8372
8373 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008374 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008376
8377 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008378 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008379 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008380 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 break;
8382
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008383 /* CTRL-G U: do not break undo with the next char */
8384 case 'U':
8385 /* Allow one left/right cursor movement with the next char,
8386 * without breaking undo. */
8387 dont_sync_undo = MAYBE;
8388 break;
8389
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008391 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 }
8393}
8394
8395/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008396 * CTRL-^ in Insert mode.
8397 */
8398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008399ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008400{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008401 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008402 {
8403 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8404 if (State & LANGMAP)
8405 {
8406 curbuf->b_p_iminsert = B_IMODE_NONE;
8407 State &= ~LANGMAP;
8408 }
8409 else
8410 {
8411 curbuf->b_p_iminsert = B_IMODE_LMAP;
8412 State |= LANGMAP;
8413#ifdef USE_IM_CONTROL
8414 im_set_active(FALSE);
8415#endif
8416 }
8417 }
8418#ifdef USE_IM_CONTROL
8419 else
8420 {
8421 /* There are no ":lmap" mappings, toggle IM */
8422 if (im_get_status())
8423 {
8424 curbuf->b_p_iminsert = B_IMODE_NONE;
8425 im_set_active(FALSE);
8426 }
8427 else
8428 {
8429 curbuf->b_p_iminsert = B_IMODE_IM;
8430 State &= ~LANGMAP;
8431 im_set_active(TRUE);
8432 }
8433 }
8434#endif
8435 set_iminsert_global();
8436 showmode();
8437#ifdef FEAT_GUI
8438 /* may show different cursor shape or color */
8439 if (gui.in_use)
8440 gui_update_cursor(TRUE, FALSE);
8441#endif
8442#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8443 /* Show/unshow value of 'keymap' in status lines. */
8444 status_redraw_curbuf();
8445#endif
8446}
8447
8448/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 * Handle ESC in insert mode.
8450 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8451 * insert.
8452 */
8453 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008454ins_esc(
8455 long *count,
8456 int cmdchar,
8457 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458{
8459 int temp;
8460 static int disabled_redraw = FALSE;
8461
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008462#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008463 check_spell_redraw();
8464#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465#if defined(FEAT_HANGULIN)
8466# if defined(ESC_CHG_TO_ENG_MODE)
8467 hangul_input_state_set(0);
8468# endif
8469 if (composing_hangul)
8470 {
8471 push_raw_key(composing_hangul_buffer, 2);
8472 composing_hangul = 0;
8473 }
8474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475
8476 temp = curwin->w_cursor.col;
8477 if (disabled_redraw)
8478 {
8479 --RedrawingDisabled;
8480 disabled_redraw = FALSE;
8481 }
8482 if (!arrow_used)
8483 {
8484 /*
8485 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008486 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8487 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008488 */
8489 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008490 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491
8492 /*
8493 * Repeating insert may take a long time. Check for
8494 * interrupt now and then.
8495 */
8496 if (*count > 0)
8497 {
8498 line_breakcheck();
8499 if (got_int)
8500 *count = 0;
8501 }
8502
8503 if (--*count > 0) /* repeat what was typed */
8504 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008505 /* Vi repeats the insert without replacing characters. */
8506 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8507 State &= ~REPLACE_FLAG;
8508
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 (void)start_redo_ins();
8510 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008511 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 ++RedrawingDisabled;
8513 disabled_redraw = TRUE;
8514 return FALSE; /* repeat the insert */
8515 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008516 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 undisplay_dollar();
8518 }
8519
8520 /* When an autoindent was removed, curswant stays after the
8521 * indent */
8522 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8523 curwin->w_set_curswant = TRUE;
8524
8525 /* Remember the last Insert position in the '^ mark. */
8526 if (!cmdmod.keepjumps)
8527 curbuf->b_last_insert = curwin->w_cursor;
8528
8529 /*
8530 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008531 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008533 if (!nomove
8534 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535#ifdef FEAT_VIRTUALEDIT
8536 || curwin->w_cursor.coladd > 0
8537#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008538 )
8539 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008540 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541#ifdef FEAT_RIGHTLEFT
8542 && !revins_on
8543#endif
8544 )
8545 {
8546#ifdef FEAT_VIRTUALEDIT
8547 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8548 {
8549 oneleft();
8550 if (restart_edit != NUL)
8551 ++curwin->w_cursor.coladd;
8552 }
8553 else
8554#endif
8555 {
8556 --curwin->w_cursor.col;
8557#ifdef FEAT_MBYTE
8558 /* Correct cursor for multi-byte character. */
8559 if (has_mbyte)
8560 mb_adjust_cursor();
8561#endif
8562 }
8563 }
8564
8565#ifdef USE_IM_CONTROL
8566 /* Disable IM to allow typing English directly for Normal mode commands.
8567 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8568 * well). */
8569 if (!(State & LANGMAP))
8570 im_save_status(&curbuf->b_p_iminsert);
8571 im_set_active(FALSE);
8572#endif
8573
8574 State = NORMAL;
8575 /* need to position cursor again (e.g. when on a TAB ) */
8576 changed_cline_bef_curs();
8577
8578#ifdef FEAT_MOUSE
8579 setmouse();
8580#endif
8581#ifdef CURSOR_SHAPE
8582 ui_cursor_shape(); /* may show different cursor shape */
8583#endif
8584
8585 /*
8586 * When recording or for CTRL-O, need to display the new mode.
8587 * Otherwise remove the mode message.
8588 */
8589 if (Recording || restart_edit != NUL)
8590 showmode();
8591 else if (p_smd)
8592 MSG("");
8593
8594 return TRUE; /* exit Insert mode */
8595}
8596
8597#ifdef FEAT_RIGHTLEFT
8598/*
8599 * Toggle language: hkmap and revins_on.
8600 * Move to end of reverse inserted text.
8601 */
8602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008603ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008604{
8605 if (revins_on && revins_chars && revins_scol >= 0)
8606 {
8607 while (gchar_cursor() != NUL && revins_chars--)
8608 ++curwin->w_cursor.col;
8609 }
8610 p_ri = !p_ri;
8611 revins_on = (State == INSERT && p_ri);
8612 if (revins_on)
8613 {
8614 revins_scol = curwin->w_cursor.col;
8615 revins_legal++;
8616 revins_chars = 0;
8617 undisplay_dollar();
8618 }
8619 else
8620 revins_scol = -1;
8621#ifdef FEAT_FKMAP
8622 if (p_altkeymap)
8623 {
8624 /*
8625 * to be consistent also for redo command, using '.'
8626 * set arrow_used to true and stop it - causing to redo
8627 * characters entered in one mode (normal/reverse insert).
8628 */
8629 arrow_used = TRUE;
8630 (void)stop_arrow();
8631 p_fkmap = curwin->w_p_rl ^ p_ri;
8632 if (p_fkmap && p_ri)
8633 State = INSERT;
8634 }
8635 else
8636#endif
8637 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8638 showmode();
8639}
8640#endif
8641
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642/*
8643 * If 'keymodel' contains "startsel", may start selection.
8644 * Returns TRUE when a CTRL-O and other keys stuffed.
8645 */
8646 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008647ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648{
8649 if (km_startsel)
8650 switch (c)
8651 {
8652 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 case K_PAGEUP:
8655 case K_KPAGEUP:
8656 case K_PAGEDOWN:
8657 case K_KPAGEDOWN:
8658# ifdef MACOS
8659 case K_LEFT:
8660 case K_RIGHT:
8661 case K_UP:
8662 case K_DOWN:
8663 case K_END:
8664 case K_HOME:
8665# endif
8666 if (!(mod_mask & MOD_MASK_SHIFT))
8667 break;
8668 /* FALLTHROUGH */
8669 case K_S_LEFT:
8670 case K_S_RIGHT:
8671 case K_S_UP:
8672 case K_S_DOWN:
8673 case K_S_END:
8674 case K_S_HOME:
8675 /* Start selection right away, the cursor can move with
8676 * CTRL-O when beyond the end of the line. */
8677 start_selection();
8678
8679 /* Execute the key in (insert) Select mode. */
8680 stuffcharReadbuff(Ctrl_O);
8681 if (mod_mask)
8682 {
8683 char_u buf[4];
8684
8685 buf[0] = K_SPECIAL;
8686 buf[1] = KS_MODIFIER;
8687 buf[2] = mod_mask;
8688 buf[3] = NUL;
8689 stuffReadbuff(buf);
8690 }
8691 stuffcharReadbuff(c);
8692 return TRUE;
8693 }
8694 return FALSE;
8695}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696
8697/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008698 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008699 */
8700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008701ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008702{
8703#ifdef FEAT_FKMAP
8704 if (p_fkmap && p_ri)
8705 {
8706 beep_flush();
8707 EMSG(farsi_text_3); /* encoded in Farsi */
8708 return;
8709 }
8710#endif
8711
8712#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008713# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008714 set_vim_var_string(VV_INSERTMODE,
8715 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008716# ifdef FEAT_VREPLACE
8717 replaceState == VREPLACE ? "v" :
8718# endif
8719 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008720# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008721 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8722#endif
8723 if (State & REPLACE_FLAG)
8724 State = INSERT | (State & LANGMAP);
8725 else
8726 State = replaceState | (State & LANGMAP);
8727 AppendCharToRedobuff(K_INS);
8728 showmode();
8729#ifdef CURSOR_SHAPE
8730 ui_cursor_shape(); /* may show different cursor shape */
8731#endif
8732}
8733
8734/*
8735 * Pressed CTRL-O in Insert mode.
8736 */
8737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008738ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008739{
8740#ifdef FEAT_VREPLACE
8741 if (State & VREPLACE_FLAG)
8742 restart_edit = 'V';
8743 else
8744#endif
8745 if (State & REPLACE_FLAG)
8746 restart_edit = 'R';
8747 else
8748 restart_edit = 'I';
8749#ifdef FEAT_VIRTUALEDIT
8750 if (virtual_active())
8751 ins_at_eol = FALSE; /* cursor always keeps its column */
8752 else
8753#endif
8754 ins_at_eol = (gchar_cursor() == NUL);
8755}
8756
8757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 * If the cursor is on an indent, ^T/^D insert/delete one
8759 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008760 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 * with vi. But vi only supports ^T and ^D after an
8762 * autoindent, we support it everywhere.
8763 */
8764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008765ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766{
8767 if (stop_arrow() == FAIL)
8768 return;
8769 AppendCharToRedobuff(c);
8770
8771 /*
8772 * 0^D and ^^D: remove all indent.
8773 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008774 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8775 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 {
8777 --curwin->w_cursor.col;
8778 (void)del_char(FALSE); /* delete the '^' or '0' */
8779 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8780 if (State & REPLACE_FLAG)
8781 replace_pop_ins();
8782 if (lastc == '^')
8783 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008784 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 }
8786 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008787 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788
8789 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8790 did_ai = FALSE;
8791#ifdef FEAT_SMARTINDENT
8792 did_si = FALSE;
8793 can_si = FALSE;
8794 can_si_back = FALSE;
8795#endif
8796#ifdef FEAT_CINDENT
8797 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8798#endif
8799}
8800
8801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008802ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803{
8804 int temp;
8805
8806 if (stop_arrow() == FAIL)
8807 return;
8808 if (gchar_cursor() == NUL) /* delete newline */
8809 {
8810 temp = curwin->w_cursor.col;
8811 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008812 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008813 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008814 else
8815 curwin->w_cursor.col = temp;
8816 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008817 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8818 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 did_ai = FALSE;
8820#ifdef FEAT_SMARTINDENT
8821 did_si = FALSE;
8822 can_si = FALSE;
8823 can_si_back = FALSE;
8824#endif
8825 AppendCharToRedobuff(K_DEL);
8826}
8827
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008828static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008829
8830/*
8831 * Delete one character for ins_bs().
8832 */
8833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008834ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008835{
8836 dec_cursor();
8837 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8838 if (State & REPLACE_FLAG)
8839 {
8840 /* Don't delete characters before the insert point when in
8841 * Replace mode */
8842 if (curwin->w_cursor.lnum != Insstart.lnum
8843 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008844 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008845 }
8846 else
8847 (void)del_char(FALSE);
8848}
8849
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850/*
8851 * Handle Backspace, delete-word and delete-line in Insert mode.
8852 * Return TRUE when backspace was actually used.
8853 */
8854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008855ins_bs(
8856 int c,
8857 int mode,
8858 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859{
8860 linenr_T lnum;
8861 int cc;
8862 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008863 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008864 colnr_T mincol;
8865 int did_backspace = FALSE;
8866 int in_indent;
8867 int oldState;
8868#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008869 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870#endif
8871
8872 /*
8873 * can't delete anything in an empty file
8874 * can't backup past first character in buffer
8875 * can't backup past starting point unless 'backspace' > 1
8876 * can backup to a previous line if 'backspace' == 0
8877 */
8878 if ( bufempty()
8879 || (
8880#ifdef FEAT_RIGHTLEFT
8881 !revins_on &&
8882#endif
8883 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8884 || (!can_bs(BS_START)
8885 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008886 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8887 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8889 && curwin->w_cursor.col <= ai_col)
8890 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8891 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008892 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893 return FALSE;
8894 }
8895
8896 if (stop_arrow() == FAIL)
8897 return FALSE;
8898 in_indent = inindent(0);
8899#ifdef FEAT_CINDENT
8900 if (in_indent)
8901 can_cindent = FALSE;
8902#endif
8903#ifdef FEAT_COMMENTS
8904 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8905#endif
8906#ifdef FEAT_RIGHTLEFT
8907 if (revins_on) /* put cursor after last inserted char */
8908 inc_cursor();
8909#endif
8910
8911#ifdef FEAT_VIRTUALEDIT
8912 /* Virtualedit:
8913 * BACKSPACE_CHAR eats a virtual space
8914 * BACKSPACE_WORD eats all coladd
8915 * BACKSPACE_LINE eats all coladd and keeps going
8916 */
8917 if (curwin->w_cursor.coladd > 0)
8918 {
8919 if (mode == BACKSPACE_CHAR)
8920 {
8921 --curwin->w_cursor.coladd;
8922 return TRUE;
8923 }
8924 if (mode == BACKSPACE_WORD)
8925 {
8926 curwin->w_cursor.coladd = 0;
8927 return TRUE;
8928 }
8929 curwin->w_cursor.coladd = 0;
8930 }
8931#endif
8932
8933 /*
8934 * delete newline!
8935 */
8936 if (curwin->w_cursor.col == 0)
8937 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008938 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008939 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940#ifdef FEAT_RIGHTLEFT
8941 || revins_on
8942#endif
8943 )
8944 {
8945 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8946 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8947 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008948 --Insstart.lnum;
8949 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008950 }
8951 /*
8952 * In replace mode:
8953 * cc < 0: NL was inserted, delete it
8954 * cc >= 0: NL was replaced, put original characters back
8955 */
8956 cc = -1;
8957 if (State & REPLACE_FLAG)
8958 cc = replace_pop(); /* returns -1 if NL was inserted */
8959 /*
8960 * In replace mode, in the line we started replacing, we only move the
8961 * cursor.
8962 */
8963 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8964 {
8965 dec_cursor();
8966 }
8967 else
8968 {
8969#ifdef FEAT_VREPLACE
8970 if (!(State & VREPLACE_FLAG)
8971 || curwin->w_cursor.lnum > orig_line_count)
8972#endif
8973 {
8974 temp = gchar_cursor(); /* remember current char */
8975 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008976
8977 /* When "aw" is in 'formatoptions' we must delete the space at
8978 * the end of the line, otherwise the line will be broken
8979 * again when auto-formatting. */
8980 if (has_format_option(FO_AUTO)
8981 && has_format_option(FO_WHITE_PAR))
8982 {
8983 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8984 TRUE);
8985 int len;
8986
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008987 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008988 if (len > 0 && ptr[len - 1] == ' ')
8989 ptr[len - 1] = NUL;
8990 }
8991
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008992 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008993 if (temp == NUL && gchar_cursor() != NUL)
8994 inc_cursor();
8995 }
8996#ifdef FEAT_VREPLACE
8997 else
8998 dec_cursor();
8999#endif
9000
9001 /*
9002 * In REPLACE mode we have to put back the text that was replaced
9003 * by the NL. On the replace stack is first a NUL-terminated
9004 * sequence of characters that were deleted and then the
9005 * characters that NL replaced.
9006 */
9007 if (State & REPLACE_FLAG)
9008 {
9009 /*
9010 * Do the next ins_char() in NORMAL state, to
9011 * prevent ins_char() from replacing characters and
9012 * avoiding showmatch().
9013 */
9014 oldState = State;
9015 State = NORMAL;
9016 /*
9017 * restore characters (blanks) deleted after cursor
9018 */
9019 while (cc > 0)
9020 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009021 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009022#ifdef FEAT_MBYTE
9023 mb_replace_pop_ins(cc);
9024#else
9025 ins_char(cc);
9026#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009027 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009028 cc = replace_pop();
9029 }
9030 /* restore the characters that NL replaced */
9031 replace_pop_ins();
9032 State = oldState;
9033 }
9034 }
9035 did_ai = FALSE;
9036 }
9037 else
9038 {
9039 /*
9040 * Delete character(s) before the cursor.
9041 */
9042#ifdef FEAT_RIGHTLEFT
9043 if (revins_on) /* put cursor on last inserted char */
9044 dec_cursor();
9045#endif
9046 mincol = 0;
9047 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009048 if (mode == BACKSPACE_LINE
9049 && (curbuf->b_p_ai
9050#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009051 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009052#endif
9053 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054#ifdef FEAT_RIGHTLEFT
9055 && !revins_on
9056#endif
9057 )
9058 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009059 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009060 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009061 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009063 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 }
9065
9066 /*
9067 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9068 */
9069 if ( mode == BACKSPACE_CHAR
9070 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009071 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009072 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073 && (*(ml_get_cursor() - 1) == TAB
9074 || (*(ml_get_cursor() - 1) == ' '
9075 && (!*inserted_space_p
9076 || arrow_used))))))
9077 {
9078 int ts;
9079 colnr_T vcol;
9080 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009081 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082
9083 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009084 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009085 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009086 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009087 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 /* Compute the virtual column where we want to be. Since
9089 * 'showbreak' may get in the way, need to get the last column of
9090 * the previous character. */
9091 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009092 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009093 dec_cursor();
9094 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9095 inc_cursor();
9096 want_vcol = (want_vcol / ts) * ts;
9097
9098 /* delete characters until we are at or before want_vcol */
9099 while (vcol > want_vcol
9100 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009101 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102
9103 /* insert extra spaces until we are at want_vcol */
9104 while (vcol < want_vcol)
9105 {
9106 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009107 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9108 && curwin->w_cursor.col < Insstart_orig.col)
9109 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110
9111#ifdef FEAT_VREPLACE
9112 if (State & VREPLACE_FLAG)
9113 ins_char(' ');
9114 else
9115#endif
9116 {
9117 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009118 if ((State & REPLACE_FLAG))
9119 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 }
9121 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9122 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009123
9124 /* If we are now back where we started delete one character. Can
9125 * happen when using 'sts' and 'linebreak'. */
9126 if (vcol >= start_vcol)
9127 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 }
9129
9130 /*
9131 * Delete upto starting point, start of line or previous word.
9132 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009133 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009135#ifdef FEAT_MBYTE
9136 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009137
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009138 if (has_mbyte)
9139 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009141 do
9142 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009143#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009144 if (!revins_on) /* put cursor on char to be deleted */
9145#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009147
9148 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009150 /* look multi-byte character class */
9151 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009153 prev_cclass = cclass;
9154 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009157
9158 /* start of word? */
9159 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9160 {
9161 mode = BACKSPACE_WORD_NOT_SPACE;
9162 temp = vim_iswordc(cc);
9163 }
9164 /* end of word? */
9165 else if (mode == BACKSPACE_WORD_NOT_SPACE
9166 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9167#ifdef FEAT_MBYTE
9168 || prev_cclass != cclass
9169#endif
9170 ))
9171 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009173 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009175 inc_cursor();
9176#ifdef FEAT_RIGHTLEFT
9177 else if (State & REPLACE_FLAG)
9178 dec_cursor();
9179#endif
9180 break;
9181 }
9182 if (State & REPLACE_FLAG)
9183 replace_do_bs(-1);
9184 else
9185 {
9186#ifdef FEAT_MBYTE
9187 if (enc_utf8 && p_deco)
9188 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9189#endif
9190 (void)del_char(FALSE);
9191#ifdef FEAT_MBYTE
9192 /*
9193 * If there are combining characters and 'delcombine' is set
9194 * move the cursor back. Don't back up before the base
9195 * character.
9196 */
9197 if (enc_utf8 && p_deco && cpc[0] != NUL)
9198 inc_cursor();
9199#endif
9200#ifdef FEAT_RIGHTLEFT
9201 if (revins_chars)
9202 {
9203 revins_chars--;
9204 revins_legal++;
9205 }
9206 if (revins_on && gchar_cursor() == NUL)
9207 break;
9208#endif
9209 }
9210 /* Just a single backspace?: */
9211 if (mode == BACKSPACE_CHAR)
9212 break;
9213 } while (
9214#ifdef FEAT_RIGHTLEFT
9215 revins_on ||
9216#endif
9217 (curwin->w_cursor.col > mincol
9218 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9219 || curwin->w_cursor.col != Insstart_orig.col)));
9220 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221 did_backspace = TRUE;
9222 }
9223#ifdef FEAT_SMARTINDENT
9224 did_si = FALSE;
9225 can_si = FALSE;
9226 can_si_back = FALSE;
9227#endif
9228 if (curwin->w_cursor.col <= 1)
9229 did_ai = FALSE;
9230 /*
9231 * It's a little strange to put backspaces into the redo
9232 * buffer, but it makes auto-indent a lot easier to deal
9233 * with.
9234 */
9235 AppendCharToRedobuff(c);
9236
9237 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009238 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9239 && curwin->w_cursor.col < Insstart_orig.col)
9240 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009241
9242 /* vi behaviour: the cursor moves backward but the character that
9243 * was there remains visible
9244 * Vim behaviour: the cursor moves backward and the character that
9245 * was there is erased from the screen.
9246 * We can emulate the vi behaviour by pretending there is a dollar
9247 * displayed even when there isn't.
9248 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009249 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250 dollar_vcol = curwin->w_virtcol;
9251
Bram Moolenaarce3be472008-01-14 19:12:28 +00009252#ifdef FEAT_FOLDING
9253 /* When deleting a char the cursor line must never be in a closed fold.
9254 * E.g., when 'foldmethod' is indent and deleting the first non-white
9255 * char before a Tab. */
9256 if (did_backspace)
9257 foldOpenCursor();
9258#endif
9259
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 return did_backspace;
9261}
9262
9263#ifdef FEAT_MOUSE
9264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009265ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266{
9267 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009268 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269
9270# ifdef FEAT_GUI
9271 /* When GUI is active, also move/paste when 'mouse' is empty */
9272 if (!gui.in_use)
9273# endif
9274 if (!mouse_has(MOUSE_INSERT))
9275 return;
9276
9277 undisplay_dollar();
9278 tpos = curwin->w_cursor;
9279 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9280 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009281#ifdef FEAT_WINDOWS
9282 win_T *new_curwin = curwin;
9283
9284 if (curwin != old_curwin && win_valid(old_curwin))
9285 {
9286 /* Mouse took us to another window. We need to go back to the
9287 * previous one to stop insert there properly. */
9288 curwin = old_curwin;
9289 curbuf = curwin->w_buffer;
9290 }
9291#endif
9292 start_arrow(curwin == old_curwin ? &tpos : NULL);
9293#ifdef FEAT_WINDOWS
9294 if (curwin != new_curwin && win_valid(new_curwin))
9295 {
9296 curwin = new_curwin;
9297 curbuf = curwin->w_buffer;
9298 }
9299#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009300# ifdef FEAT_CINDENT
9301 can_cindent = TRUE;
9302# endif
9303 }
9304
9305#ifdef FEAT_WINDOWS
9306 /* redraw status lines (in case another window became active) */
9307 redraw_statuslines();
9308#endif
9309}
9310
9311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009312ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009313{
9314 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009315# if defined(FEAT_WINDOWS)
9316 win_T *old_curwin = curwin;
9317# endif
9318# ifdef FEAT_INS_EXPAND
9319 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320# endif
9321
9322 tpos = curwin->w_cursor;
9323
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009324# ifdef FEAT_WINDOWS
9325 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009326 {
9327 int row, col;
9328
9329 row = mouse_row;
9330 col = mouse_col;
9331
9332 /* find the window at the pointer coordinates */
9333 curwin = mouse_find_win(&row, &col);
9334 curbuf = curwin->w_buffer;
9335 }
9336 if (curwin == old_curwin)
9337# endif
9338 undisplay_dollar();
9339
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009340# ifdef FEAT_INS_EXPAND
9341 /* Don't scroll the window in which completion is being done. */
9342 if (!pum_visible()
9343# if defined(FEAT_WINDOWS)
9344 || curwin != old_curwin
9345# endif
9346 )
9347# endif
9348 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009349 if (dir == MSCR_DOWN || dir == MSCR_UP)
9350 {
9351 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9352 scroll_redraw(dir,
9353 (long)(curwin->w_botline - curwin->w_topline));
9354 else
9355 scroll_redraw(dir, 3L);
9356 }
9357#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009358 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009359 {
9360 int val, step = 6;
9361
9362 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9363 step = W_WIDTH(curwin);
9364 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9365 if (val < 0)
9366 val = 0;
9367 gui_do_horiz_scroll(val, TRUE);
9368 }
9369#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009370# ifdef FEAT_INS_EXPAND
9371 did_scroll = TRUE;
9372# endif
9373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009374
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009375# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009376 curwin->w_redr_status = TRUE;
9377
9378 curwin = old_curwin;
9379 curbuf = curwin->w_buffer;
9380# endif
9381
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009382# ifdef FEAT_INS_EXPAND
9383 /* The popup menu may overlay the window, need to redraw it.
9384 * TODO: Would be more efficient to only redraw the windows that are
9385 * overlapped by the popup menu. */
9386 if (pum_visible() && did_scroll)
9387 {
9388 redraw_all_later(NOT_VALID);
9389 ins_compl_show_pum();
9390 }
9391# endif
9392
Bram Moolenaar071d4272004-06-13 20:20:40 +00009393 if (!equalpos(curwin->w_cursor, tpos))
9394 {
9395 start_arrow(&tpos);
9396# ifdef FEAT_CINDENT
9397 can_cindent = TRUE;
9398# endif
9399 }
9400}
9401#endif
9402
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009403#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009405ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009406{
9407 /* We will be leaving the current window, unless closing another tab. */
9408 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9409 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9410 {
9411 undisplay_dollar();
9412 start_arrow(&curwin->w_cursor);
9413# ifdef FEAT_CINDENT
9414 can_cindent = TRUE;
9415# endif
9416 }
9417
9418 if (c == K_TABLINE)
9419 goto_tabpage(current_tab);
9420 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009421 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009422 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009423 redraw_statuslines(); /* will redraw the tabline when needed */
9424 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009425}
9426#endif
9427
9428#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009429 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009430ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431{
9432 pos_T tpos;
9433
9434 undisplay_dollar();
9435 tpos = curwin->w_cursor;
9436 if (gui_do_scroll())
9437 {
9438 start_arrow(&tpos);
9439# ifdef FEAT_CINDENT
9440 can_cindent = TRUE;
9441# endif
9442 }
9443}
9444
9445 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009446ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447{
9448 pos_T tpos;
9449
9450 undisplay_dollar();
9451 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009452 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009453 {
9454 start_arrow(&tpos);
9455# ifdef FEAT_CINDENT
9456 can_cindent = TRUE;
9457# endif
9458 }
9459}
9460#endif
9461
9462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009463ins_left(
9464 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009465{
9466 pos_T tpos;
9467
9468#ifdef FEAT_FOLDING
9469 if ((fdo_flags & FDO_HOR) && KeyTyped)
9470 foldOpenCursor();
9471#endif
9472 undisplay_dollar();
9473 tpos = curwin->w_cursor;
9474 if (oneleft() == OK)
9475 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009476#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9477 /* Only call start_arrow() when not busy with preediting, it will
9478 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9479 if (!im_is_preediting())
9480#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009481 {
9482 start_arrow_with_change(&tpos, end_change);
9483 if (!end_change)
9484 AppendCharToRedobuff(K_LEFT);
9485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486#ifdef FEAT_RIGHTLEFT
9487 /* If exit reversed string, position is fixed */
9488 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9489 revins_legal++;
9490 revins_chars++;
9491#endif
9492 }
9493
9494 /*
9495 * if 'whichwrap' set for cursor in insert mode may go to
9496 * previous line
9497 */
9498 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9499 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009500 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501 start_arrow(&tpos);
9502 --(curwin->w_cursor.lnum);
9503 coladvance((colnr_T)MAXCOL);
9504 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9505 }
9506 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009507 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009508 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009509}
9510
9511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009512ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513{
9514 pos_T tpos;
9515
9516#ifdef FEAT_FOLDING
9517 if ((fdo_flags & FDO_HOR) && KeyTyped)
9518 foldOpenCursor();
9519#endif
9520 undisplay_dollar();
9521 tpos = curwin->w_cursor;
9522 if (c == K_C_HOME)
9523 curwin->w_cursor.lnum = 1;
9524 curwin->w_cursor.col = 0;
9525#ifdef FEAT_VIRTUALEDIT
9526 curwin->w_cursor.coladd = 0;
9527#endif
9528 curwin->w_curswant = 0;
9529 start_arrow(&tpos);
9530}
9531
9532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009533ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535 pos_T tpos;
9536
9537#ifdef FEAT_FOLDING
9538 if ((fdo_flags & FDO_HOR) && KeyTyped)
9539 foldOpenCursor();
9540#endif
9541 undisplay_dollar();
9542 tpos = curwin->w_cursor;
9543 if (c == K_C_END)
9544 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9545 coladvance((colnr_T)MAXCOL);
9546 curwin->w_curswant = MAXCOL;
9547
9548 start_arrow(&tpos);
9549}
9550
9551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009552ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009553{
9554#ifdef FEAT_FOLDING
9555 if ((fdo_flags & FDO_HOR) && KeyTyped)
9556 foldOpenCursor();
9557#endif
9558 undisplay_dollar();
9559 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9560 {
9561 start_arrow(&curwin->w_cursor);
9562 (void)bck_word(1L, FALSE, FALSE);
9563 curwin->w_set_curswant = TRUE;
9564 }
9565 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009566 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009567}
9568
9569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009570ins_right(
9571 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009572{
9573#ifdef FEAT_FOLDING
9574 if ((fdo_flags & FDO_HOR) && KeyTyped)
9575 foldOpenCursor();
9576#endif
9577 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009578 if (gchar_cursor() != NUL
9579#ifdef FEAT_VIRTUALEDIT
9580 || virtual_active()
9581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 )
9583 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009584 start_arrow_with_change(&curwin->w_cursor, end_change);
9585 if (!end_change)
9586 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009587 curwin->w_set_curswant = TRUE;
9588#ifdef FEAT_VIRTUALEDIT
9589 if (virtual_active())
9590 oneright();
9591 else
9592#endif
9593 {
9594#ifdef FEAT_MBYTE
9595 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009596 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597 else
9598#endif
9599 ++curwin->w_cursor.col;
9600 }
9601
9602#ifdef FEAT_RIGHTLEFT
9603 revins_legal++;
9604 if (revins_chars)
9605 revins_chars--;
9606#endif
9607 }
9608 /* if 'whichwrap' set for cursor in insert mode, may move the
9609 * cursor to the next line */
9610 else if (vim_strchr(p_ww, ']') != NULL
9611 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9612 {
9613 start_arrow(&curwin->w_cursor);
9614 curwin->w_set_curswant = TRUE;
9615 ++curwin->w_cursor.lnum;
9616 curwin->w_cursor.col = 0;
9617 }
9618 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009619 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009620 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621}
9622
9623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009624ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625{
9626#ifdef FEAT_FOLDING
9627 if ((fdo_flags & FDO_HOR) && KeyTyped)
9628 foldOpenCursor();
9629#endif
9630 undisplay_dollar();
9631 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9632 || gchar_cursor() != NUL)
9633 {
9634 start_arrow(&curwin->w_cursor);
9635 (void)fwd_word(1L, FALSE, 0);
9636 curwin->w_set_curswant = TRUE;
9637 }
9638 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009639 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009640}
9641
9642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009643ins_up(
9644 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
9646 pos_T tpos;
9647 linenr_T old_topline = curwin->w_topline;
9648#ifdef FEAT_DIFF
9649 int old_topfill = curwin->w_topfill;
9650#endif
9651
9652 undisplay_dollar();
9653 tpos = curwin->w_cursor;
9654 if (cursor_up(1L, TRUE) == OK)
9655 {
9656 if (startcol)
9657 coladvance(getvcol_nolist(&Insstart));
9658 if (old_topline != curwin->w_topline
9659#ifdef FEAT_DIFF
9660 || old_topfill != curwin->w_topfill
9661#endif
9662 )
9663 redraw_later(VALID);
9664 start_arrow(&tpos);
9665#ifdef FEAT_CINDENT
9666 can_cindent = TRUE;
9667#endif
9668 }
9669 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009670 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009671}
9672
9673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009674ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675{
9676 pos_T tpos;
9677
9678 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009679
9680#ifdef FEAT_WINDOWS
9681 if (mod_mask & MOD_MASK_CTRL)
9682 {
9683 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009684 if (first_tabpage->tp_next != NULL)
9685 {
9686 start_arrow(&curwin->w_cursor);
9687 goto_tabpage(-1);
9688 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009689 return;
9690 }
9691#endif
9692
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693 tpos = curwin->w_cursor;
9694 if (onepage(BACKWARD, 1L) == OK)
9695 {
9696 start_arrow(&tpos);
9697#ifdef FEAT_CINDENT
9698 can_cindent = TRUE;
9699#endif
9700 }
9701 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009702 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703}
9704
9705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009706ins_down(
9707 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708{
9709 pos_T tpos;
9710 linenr_T old_topline = curwin->w_topline;
9711#ifdef FEAT_DIFF
9712 int old_topfill = curwin->w_topfill;
9713#endif
9714
9715 undisplay_dollar();
9716 tpos = curwin->w_cursor;
9717 if (cursor_down(1L, TRUE) == OK)
9718 {
9719 if (startcol)
9720 coladvance(getvcol_nolist(&Insstart));
9721 if (old_topline != curwin->w_topline
9722#ifdef FEAT_DIFF
9723 || old_topfill != curwin->w_topfill
9724#endif
9725 )
9726 redraw_later(VALID);
9727 start_arrow(&tpos);
9728#ifdef FEAT_CINDENT
9729 can_cindent = TRUE;
9730#endif
9731 }
9732 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009733 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734}
9735
9736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009737ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738{
9739 pos_T tpos;
9740
9741 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009742
9743#ifdef FEAT_WINDOWS
9744 if (mod_mask & MOD_MASK_CTRL)
9745 {
9746 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009747 if (first_tabpage->tp_next != NULL)
9748 {
9749 start_arrow(&curwin->w_cursor);
9750 goto_tabpage(0);
9751 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009752 return;
9753 }
9754#endif
9755
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756 tpos = curwin->w_cursor;
9757 if (onepage(FORWARD, 1L) == OK)
9758 {
9759 start_arrow(&tpos);
9760#ifdef FEAT_CINDENT
9761 can_cindent = TRUE;
9762#endif
9763 }
9764 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009765 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766}
9767
9768#ifdef FEAT_DND
9769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009770ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771{
9772 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9773}
9774#endif
9775
9776/*
9777 * Handle TAB in Insert or Replace mode.
9778 * Return TRUE when the TAB needs to be inserted like a normal character.
9779 */
9780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009781ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782{
9783 int ind;
9784 int i;
9785 int temp;
9786
9787 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9788 Insstart_blank_vcol = get_nolist_virtcol();
9789 if (echeck_abbr(TAB + ABBR_OFF))
9790 return FALSE;
9791
9792 ind = inindent(0);
9793#ifdef FEAT_CINDENT
9794 if (ind)
9795 can_cindent = FALSE;
9796#endif
9797
9798 /*
9799 * When nothing special, insert TAB like a normal character
9800 */
9801 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009802 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009803 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 return TRUE;
9805
9806 if (stop_arrow() == FAIL)
9807 return TRUE;
9808
9809 did_ai = FALSE;
9810#ifdef FEAT_SMARTINDENT
9811 did_si = FALSE;
9812 can_si = FALSE;
9813 can_si_back = FALSE;
9814#endif
9815 AppendToRedobuff((char_u *)"\t");
9816
9817 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009818 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009819 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9820 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821 else /* otherwise use 'tabstop' */
9822 temp = (int)curbuf->b_p_ts;
9823 temp -= get_nolist_virtcol() % temp;
9824
9825 /*
9826 * Insert the first space with ins_char(). It will delete one char in
9827 * replace mode. Insert the rest with ins_str(); it will not delete any
9828 * chars. For VREPLACE mode, we use ins_char() for all characters.
9829 */
9830 ins_char(' ');
9831 while (--temp > 0)
9832 {
9833#ifdef FEAT_VREPLACE
9834 if (State & VREPLACE_FLAG)
9835 ins_char(' ');
9836 else
9837#endif
9838 {
9839 ins_str((char_u *)" ");
9840 if (State & REPLACE_FLAG) /* no char replaced */
9841 replace_push(NUL);
9842 }
9843 }
9844
9845 /*
9846 * When 'expandtab' not set: Replace spaces by TABs where possible.
9847 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009848 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 {
9850 char_u *ptr;
9851#ifdef FEAT_VREPLACE
9852 char_u *saved_line = NULL; /* init for GCC */
9853 pos_T pos;
9854#endif
9855 pos_T fpos;
9856 pos_T *cursor;
9857 colnr_T want_vcol, vcol;
9858 int change_col = -1;
9859 int save_list = curwin->w_p_list;
9860
9861 /*
9862 * Get the current line. For VREPLACE mode, don't make real changes
9863 * yet, just work on a copy of the line.
9864 */
9865#ifdef FEAT_VREPLACE
9866 if (State & VREPLACE_FLAG)
9867 {
9868 pos = curwin->w_cursor;
9869 cursor = &pos;
9870 saved_line = vim_strsave(ml_get_curline());
9871 if (saved_line == NULL)
9872 return FALSE;
9873 ptr = saved_line + pos.col;
9874 }
9875 else
9876#endif
9877 {
9878 ptr = ml_get_cursor();
9879 cursor = &curwin->w_cursor;
9880 }
9881
9882 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9883 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9884 curwin->w_p_list = FALSE;
9885
9886 /* Find first white before the cursor */
9887 fpos = curwin->w_cursor;
9888 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9889 {
9890 --fpos.col;
9891 --ptr;
9892 }
9893
9894 /* In Replace mode, don't change characters before the insert point. */
9895 if ((State & REPLACE_FLAG)
9896 && fpos.lnum == Insstart.lnum
9897 && fpos.col < Insstart.col)
9898 {
9899 ptr += Insstart.col - fpos.col;
9900 fpos.col = Insstart.col;
9901 }
9902
9903 /* compute virtual column numbers of first white and cursor */
9904 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9905 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9906
Bram Moolenaar597a4222014-06-25 14:39:50 +02009907 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9908 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 while (vim_iswhite(*ptr))
9910 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009911 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912 if (vcol + i > want_vcol)
9913 break;
9914 if (*ptr != TAB)
9915 {
9916 *ptr = TAB;
9917 if (change_col < 0)
9918 {
9919 change_col = fpos.col; /* Column of first change */
9920 /* May have to adjust Insstart */
9921 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9922 Insstart.col = fpos.col;
9923 }
9924 }
9925 ++fpos.col;
9926 ++ptr;
9927 vcol += i;
9928 }
9929
9930 if (change_col >= 0)
9931 {
9932 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009933 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
9935 /* Skip over the spaces we need. */
9936 while (vcol < want_vcol && *ptr == ' ')
9937 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009938 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 ++ptr;
9940 ++repl_off;
9941 }
9942 if (vcol > want_vcol)
9943 {
9944 /* Must have a char with 'showbreak' just before it. */
9945 --ptr;
9946 --repl_off;
9947 }
9948 fpos.col += repl_off;
9949
9950 /* Delete following spaces. */
9951 i = cursor->col - fpos.col;
9952 if (i > 0)
9953 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009954 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009955 /* correct replace stack. */
9956 if ((State & REPLACE_FLAG)
9957#ifdef FEAT_VREPLACE
9958 && !(State & VREPLACE_FLAG)
9959#endif
9960 )
9961 for (temp = i; --temp >= 0; )
9962 replace_join(repl_off);
9963 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009964#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009965 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009966 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009967 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009968 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9969 (char_u *)"\t", 1);
9970 }
9971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 cursor->col -= i;
9973
9974#ifdef FEAT_VREPLACE
9975 /*
9976 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9977 * backspacing over the changed spacing and then inserting the new
9978 * spacing.
9979 */
9980 if (State & VREPLACE_FLAG)
9981 {
9982 /* Backspace from real cursor to change_col */
9983 backspace_until_column(change_col);
9984
9985 /* Insert each char in saved_line from changed_col to
9986 * ptr-cursor */
9987 ins_bytes_len(saved_line + change_col,
9988 cursor->col - change_col);
9989 }
9990#endif
9991 }
9992
9993#ifdef FEAT_VREPLACE
9994 if (State & VREPLACE_FLAG)
9995 vim_free(saved_line);
9996#endif
9997 curwin->w_p_list = save_list;
9998 }
9999
10000 return FALSE;
10001}
10002
10003/*
10004 * Handle CR or NL in insert mode.
10005 * Return TRUE when out of memory or can't undo.
10006 */
10007 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010008ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009{
10010 int i;
10011
10012 if (echeck_abbr(c + ABBR_OFF))
10013 return FALSE;
10014 if (stop_arrow() == FAIL)
10015 return TRUE;
10016 undisplay_dollar();
10017
10018 /*
10019 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10020 * character under the cursor. Only push a NUL on the replace stack,
10021 * nothing to put back when the NL is deleted.
10022 */
10023 if ((State & REPLACE_FLAG)
10024#ifdef FEAT_VREPLACE
10025 && !(State & VREPLACE_FLAG)
10026#endif
10027 )
10028 replace_push(NUL);
10029
10030 /*
10031 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10032 * replacing the next line, so we push all of the characters left on the
10033 * line onto the replace stack. This is not done here though, it is done
10034 * in open_line().
10035 */
10036
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010037#ifdef FEAT_VIRTUALEDIT
10038 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10039 * CTRL-O). */
10040 if (virtual_active() && curwin->w_cursor.coladd > 0)
10041 coladvance(getviscol());
10042#endif
10043
Bram Moolenaar071d4272004-06-13 20:20:40 +000010044#ifdef FEAT_RIGHTLEFT
10045# ifdef FEAT_FKMAP
10046 if (p_altkeymap && p_fkmap)
10047 fkmap(NL);
10048# endif
10049 /* NL in reverse insert will always start in the end of
10050 * current line. */
10051 if (revins_on)
10052 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10053#endif
10054
10055 AppendToRedobuff(NL_STR);
10056 i = open_line(FORWARD,
10057#ifdef FEAT_COMMENTS
10058 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10059#endif
10060 0, old_indent);
10061 old_indent = 0;
10062#ifdef FEAT_CINDENT
10063 can_cindent = TRUE;
10064#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010065#ifdef FEAT_FOLDING
10066 /* When inserting a line the cursor line must never be in a closed fold. */
10067 foldOpenCursor();
10068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069
10070 return (!i);
10071}
10072
10073#ifdef FEAT_DIGRAPHS
10074/*
10075 * Handle digraph in insert mode.
10076 * Returns character still to be inserted, or NUL when nothing remaining to be
10077 * done.
10078 */
10079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010080ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010081{
10082 int c;
10083 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010084 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010085
10086 pc_status = PC_STATUS_UNSET;
10087 if (redrawing() && !char_avail())
10088 {
10089 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010090 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010091
10092 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010093 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094#ifdef FEAT_CMDL_INFO
10095 add_to_showcmd_c(Ctrl_K);
10096#endif
10097 }
10098
10099#ifdef USE_ON_FLY_SCROLL
10100 dont_scroll = TRUE; /* disallow scrolling here */
10101#endif
10102
10103 /* don't map the digraph chars. This also prevents the
10104 * mode message to be deleted when ESC is hit */
10105 ++no_mapping;
10106 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010107 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 --no_mapping;
10109 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010110 if (did_putchar)
10111 /* when the line fits in 'columns' the '?' is at the start of the next
10112 * line and will not be removed by the redraw */
10113 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010114
Bram Moolenaar071d4272004-06-13 20:20:40 +000010115 if (IS_SPECIAL(c) || mod_mask) /* special key */
10116 {
10117#ifdef FEAT_CMDL_INFO
10118 clear_showcmd();
10119#endif
10120 insert_special(c, TRUE, FALSE);
10121 return NUL;
10122 }
10123 if (c != ESC)
10124 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010125 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 if (redrawing() && !char_avail())
10127 {
10128 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010129 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010130
10131 if (char2cells(c) == 1)
10132 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010133 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010135 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 }
10137#ifdef FEAT_CMDL_INFO
10138 add_to_showcmd_c(c);
10139#endif
10140 }
10141 ++no_mapping;
10142 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010143 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144 --no_mapping;
10145 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010146 if (did_putchar)
10147 /* when the line fits in 'columns' the '?' is at the start of the
10148 * next line and will not be removed by a redraw */
10149 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010150 if (cc != ESC)
10151 {
10152 AppendToRedobuff((char_u *)CTRL_V_STR);
10153 c = getdigraph(c, cc, TRUE);
10154#ifdef FEAT_CMDL_INFO
10155 clear_showcmd();
10156#endif
10157 return c;
10158 }
10159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010160#ifdef FEAT_CMDL_INFO
10161 clear_showcmd();
10162#endif
10163 return NUL;
10164}
10165#endif
10166
10167/*
10168 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10169 * Returns the char to be inserted, or NUL if none found.
10170 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010171 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010172ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010173{
10174 int c;
10175 int temp;
10176 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010177 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178
10179 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10180 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010181 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010182 return NUL;
10183 }
10184
10185 /* try to advance to the cursor column */
10186 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010187 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010188 prev_ptr = ptr;
10189 validate_virtcol();
10190 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10191 {
10192 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010193 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010194 }
10195 if ((colnr_T)temp > curwin->w_virtcol)
10196 ptr = prev_ptr;
10197
10198#ifdef FEAT_MBYTE
10199 c = (*mb_ptr2char)(ptr);
10200#else
10201 c = *ptr;
10202#endif
10203 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010204 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010205 return c;
10206}
10207
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010208/*
10209 * CTRL-Y or CTRL-E typed in Insert mode.
10210 */
10211 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010212ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010213{
10214 int c = tc;
10215
10216#ifdef FEAT_INS_EXPAND
10217 if (ctrl_x_mode == CTRL_X_SCROLL)
10218 {
10219 if (c == Ctrl_Y)
10220 scrolldown_clamp();
10221 else
10222 scrollup_clamp();
10223 redraw_later(VALID);
10224 }
10225 else
10226#endif
10227 {
10228 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10229 if (c != NUL)
10230 {
10231 long tw_save;
10232
10233 /* The character must be taken literally, insert like it
10234 * was typed after a CTRL-V, and pretend 'textwidth'
10235 * wasn't set. Digits, 'o' and 'x' are special after a
10236 * CTRL-V, don't use it for these. */
10237 if (c < 256 && !isalnum(c))
10238 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10239 tw_save = curbuf->b_p_tw;
10240 curbuf->b_p_tw = -1;
10241 insert_special(c, TRUE, FALSE);
10242 curbuf->b_p_tw = tw_save;
10243#ifdef FEAT_RIGHTLEFT
10244 revins_chars++;
10245 revins_legal++;
10246#endif
10247 c = Ctrl_V; /* pretend CTRL-V is last character */
10248 auto_format(FALSE, TRUE);
10249 }
10250 }
10251 return c;
10252}
10253
Bram Moolenaar071d4272004-06-13 20:20:40 +000010254#ifdef FEAT_SMARTINDENT
10255/*
10256 * Try to do some very smart auto-indenting.
10257 * Used when inserting a "normal" character.
10258 */
10259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010260ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010261{
10262 pos_T *pos, old_pos;
10263 char_u *ptr;
10264 int i;
10265 int temp;
10266
10267 /*
10268 * do some very smart indenting when entering '{' or '}'
10269 */
10270 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10271 {
10272 /*
10273 * for '}' set indent equal to indent of line containing matching '{'
10274 */
10275 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10276 {
10277 old_pos = curwin->w_cursor;
10278 /*
10279 * If the matching '{' has a ')' immediately before it (ignoring
10280 * white-space), then line up with the start of the line
10281 * containing the matching '(' if there is one. This handles the
10282 * case where an "if (..\n..) {" statement continues over multiple
10283 * lines -- webb
10284 */
10285 ptr = ml_get(pos->lnum);
10286 i = pos->col;
10287 if (i > 0) /* skip blanks before '{' */
10288 while (--i > 0 && vim_iswhite(ptr[i]))
10289 ;
10290 curwin->w_cursor.lnum = pos->lnum;
10291 curwin->w_cursor.col = i;
10292 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10293 curwin->w_cursor = *pos;
10294 i = get_indent();
10295 curwin->w_cursor = old_pos;
10296#ifdef FEAT_VREPLACE
10297 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010298 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 else
10300#endif
10301 (void)set_indent(i, SIN_CHANGED);
10302 }
10303 else if (curwin->w_cursor.col > 0)
10304 {
10305 /*
10306 * when inserting '{' after "O" reduce indent, but not
10307 * more than indent of previous line
10308 */
10309 temp = TRUE;
10310 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10311 {
10312 old_pos = curwin->w_cursor;
10313 i = get_indent();
10314 while (curwin->w_cursor.lnum > 1)
10315 {
10316 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10317
10318 /* ignore empty lines and lines starting with '#'. */
10319 if (*ptr != '#' && *ptr != NUL)
10320 break;
10321 }
10322 if (get_indent() >= i)
10323 temp = FALSE;
10324 curwin->w_cursor = old_pos;
10325 }
10326 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010327 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010328 }
10329 }
10330
10331 /*
10332 * set indent of '#' always to 0
10333 */
10334 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10335 {
10336 /* remember current indent for next line */
10337 old_indent = get_indent();
10338 (void)set_indent(0, SIN_CHANGED);
10339 }
10340
10341 /* Adjust ai_col, the char at this position can be deleted. */
10342 if (ai_col > curwin->w_cursor.col)
10343 ai_col = curwin->w_cursor.col;
10344}
10345#endif
10346
10347/*
10348 * Get the value that w_virtcol would have when 'list' is off.
10349 * Unless 'cpo' contains the 'L' flag.
10350 */
10351 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010352get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010353{
10354 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10355 return getvcol_nolist(&curwin->w_cursor);
10356 validate_virtcol();
10357 return curwin->w_virtcol;
10358}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010359
10360#ifdef FEAT_AUTOCMD
10361/*
10362 * Handle the InsertCharPre autocommand.
10363 * "c" is the character that was typed.
10364 * Return a pointer to allocated memory with the replacement string.
10365 * Return NULL to continue inserting "c".
10366 */
10367 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010368do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010369{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010370 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010371 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010372
10373 /* Return quickly when there is nothing to do. */
10374 if (!has_insertcharpre())
10375 return NULL;
10376
Bram Moolenaar704984a2012-06-01 14:57:51 +020010377#ifdef FEAT_MBYTE
10378 if (has_mbyte)
10379 buf[(*mb_char2bytes)(c, buf)] = NUL;
10380 else
10381#endif
10382 {
10383 buf[0] = c;
10384 buf[1] = NUL;
10385 }
10386
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010387 /* Lock the text to avoid weird things from happening. */
10388 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010389 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010390
Bram Moolenaar704984a2012-06-01 14:57:51 +020010391 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010392 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010393 {
10394 /* Get the value of v:char. It may be empty or more than one
10395 * character. Only use it when changed, otherwise continue with the
10396 * original character to avoid breaking autoindent. */
10397 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10398 res = vim_strsave(get_vim_var_str(VV_CHAR));
10399 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010400
10401 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10402 --textlock;
10403
10404 return res;
10405}
10406#endif