blob: f4a835485cb4df42160a4f28564f7c857c006aa7 [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/*
2764 * Start completion for the complete() function.
2765 * "startcol" is where the matched text starts (1 is first column).
2766 * "list" is the list of matches.
2767 */
2768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002769set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002770{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002771 int save_w_wrow = curwin->w_wrow;
2772
Bram Moolenaara94bc432006-03-10 21:42:59 +00002773 /* If already doing completions stop it. */
2774 if (ctrl_x_mode != 0)
2775 ins_compl_prep(' ');
2776 ins_compl_clear();
2777
2778 if (stop_arrow() == FAIL)
2779 return;
2780
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002781 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002782 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002783 startcol = curwin->w_cursor.col;
2784 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002785 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002786 /* compl_pattern doesn't need to be set */
2787 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2788 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002789 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002790 return;
2791
Bram Moolenaare4214502015-03-05 18:08:43 +01002792 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002793
2794 ins_compl_add_list(list);
2795 compl_matches = ins_compl_make_cyclic();
2796 compl_started = TRUE;
2797 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002798 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002799
2800 compl_curr_match = compl_first_match;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002801 if (compl_no_insert)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002802 ins_complete(K_DOWN, FALSE);
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002803 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002804 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar2cf69382015-07-17 13:42:23 +02002805 if (compl_no_select)
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002806 ins_complete(Ctrl_P, FALSE);
2807
2808 /* Lazily show the popup menu, unless we got interrupted. */
2809 if (!compl_interrupted)
2810 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002811 out_flush();
2812}
2813
2814
Bram Moolenaar9372a112005-12-06 19:59:18 +00002815/* "compl_match_array" points the currently displayed list of entries in the
2816 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002817static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002818static int compl_match_arraysize;
2819
2820/*
2821 * Update the screen and when there is any scrolling remove the popup menu.
2822 */
2823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002824ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002825{
2826 int h;
2827
2828 if (compl_match_array != NULL)
2829 {
2830 h = curwin->w_cline_height;
2831 update_screen(0);
2832 if (h != curwin->w_cline_height)
2833 ins_compl_del_pum();
2834 }
2835}
2836
2837/*
2838 * Remove any popup menu.
2839 */
2840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002841ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002842{
2843 if (compl_match_array != NULL)
2844 {
2845 pum_undisplay();
2846 vim_free(compl_match_array);
2847 compl_match_array = NULL;
2848 }
2849}
2850
2851/*
2852 * Return TRUE if the popup menu should be displayed.
2853 */
2854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002855pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002856{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002857 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002858 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002859 return FALSE;
2860
2861 /* The display looks bad on a B&W display. */
2862 if (t_colors < 8
2863#ifdef FEAT_GUI
2864 && !gui.in_use
2865#endif
2866 )
2867 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002868 return TRUE;
2869}
2870
2871/*
2872 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002873 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002874 */
2875 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002876pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002877{
2878 compl_T *compl;
2879 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002880
2881 /* Don't display the popup menu if there are no matches or there is only
2882 * one (ignoring the original text). */
2883 compl = compl_first_match;
2884 i = 0;
2885 do
2886 {
2887 if (compl == NULL
2888 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2889 break;
2890 compl = compl->cp_next;
2891 } while (compl != compl_first_match);
2892
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002893 if (strstr((char *)p_cot, "menuone") != NULL)
2894 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002895 return (i >= 2);
2896}
2897
2898/*
2899 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002900 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002901 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002902 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002903ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002904{
2905 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002906 compl_T *shown_compl = NULL;
2907 int did_find_shown_match = FALSE;
2908 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002909 int i;
2910 int cur = -1;
2911 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002912 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002913
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002914 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002915 return;
2916
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002917#if defined(FEAT_EVAL)
2918 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2919 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2920#endif
2921
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002922 /* Update the screen before drawing the popup menu over it. */
2923 update_screen(0);
2924
2925 if (compl_match_array == NULL)
2926 {
2927 /* Need to build the popup menu list. */
2928 compl_match_arraysize = 0;
2929 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002930 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002931 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002932 do
2933 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002934 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2935 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002936 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002937 ++compl_match_arraysize;
2938 compl = compl->cp_next;
2939 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002940 if (compl_match_arraysize == 0)
2941 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002942 compl_match_array = (pumitem_T *)alloc_clear(
2943 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002944 * compl_match_arraysize));
2945 if (compl_match_array != NULL)
2946 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002947 /* If the current match is the original text don't find the first
2948 * match after it, don't highlight anything. */
2949 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2950 shown_match_ok = TRUE;
2951
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002952 i = 0;
2953 compl = compl_first_match;
2954 do
2955 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002956 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2957 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002958 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002959 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002960 if (!shown_match_ok)
2961 {
2962 if (compl == compl_shown_match || did_find_shown_match)
2963 {
2964 /* This item is the shown match or this is the
2965 * first displayed item after the shown match. */
2966 compl_shown_match = compl;
2967 did_find_shown_match = TRUE;
2968 shown_match_ok = TRUE;
2969 }
2970 else
2971 /* Remember this displayed match for when the
2972 * shown match is just below it. */
2973 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002974 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002975 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002976
2977 if (compl->cp_text[CPT_ABBR] != NULL)
2978 compl_match_array[i].pum_text =
2979 compl->cp_text[CPT_ABBR];
2980 else
2981 compl_match_array[i].pum_text = compl->cp_str;
2982 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2983 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2984 if (compl->cp_text[CPT_MENU] != NULL)
2985 compl_match_array[i++].pum_extra =
2986 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002987 else
2988 compl_match_array[i++].pum_extra = compl->cp_fname;
2989 }
2990
2991 if (compl == compl_shown_match)
2992 {
2993 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002994
2995 /* When the original text is the shown match don't set
2996 * compl_shown_match. */
2997 if (compl->cp_flags & ORIGINAL_TEXT)
2998 shown_match_ok = TRUE;
2999
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003000 if (!shown_match_ok && shown_compl != NULL)
3001 {
3002 /* The shown match isn't displayed, set it to the
3003 * previously displayed match. */
3004 compl_shown_match = shown_compl;
3005 shown_match_ok = TRUE;
3006 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003007 }
3008 compl = compl->cp_next;
3009 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003010
3011 if (!shown_match_ok) /* no displayed match at all */
3012 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003013 }
3014 }
3015 else
3016 {
3017 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003018 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003019 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3020 || compl_match_array[i].pum_text
3021 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003022 {
3023 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003024 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003025 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003026 }
3027
3028 if (compl_match_array != NULL)
3029 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003030 /* In Replace mode when a $ is displayed at the end of the line only
3031 * part of the screen would be updated. We do need to redraw here. */
3032 dollar_vcol = -1;
3033
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003034 /* Compute the screen column of the start of the completed text.
3035 * Use the cursor to get all wrapping and other settings right. */
3036 col = curwin->w_cursor.col;
3037 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003038 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003039 curwin->w_cursor.col = col;
3040 }
3041}
3042
Bram Moolenaar071d4272004-06-13 20:20:40 +00003043#define DICT_FIRST (1) /* use just first element in "dict" */
3044#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003045
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003047 * Add any identifiers that match the given pattern in the list of dictionary
3048 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 */
3050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003051ins_compl_dictionaries(
3052 char_u *dict_start,
3053 char_u *pat,
3054 int flags, /* DICT_FIRST and/or DICT_EXACT */
3055 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003057 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003058 char_u *ptr;
3059 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003060 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 char_u **files;
3062 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003063 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003064 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003065
Bram Moolenaar0b238792006-03-02 22:49:12 +00003066 if (*dict == NUL)
3067 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003068#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003069 /* When 'dictionary' is empty and spell checking is enabled use
3070 * "spell". */
3071 if (!thesaurus && curwin->w_p_spell)
3072 dict = (char_u *)"spell";
3073 else
3074#endif
3075 return;
3076 }
3077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003079 if (buf == NULL)
3080 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003081 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003082
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 /* If 'infercase' is set, don't use 'smartcase' here */
3084 save_p_scs = p_scs;
3085 if (curbuf->b_p_inf)
3086 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003087
3088 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3089 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003090 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003091 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003092 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003093 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003094 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003095
3096 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003097 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003098 len = STRLEN(pat_esc) + 10;
3099 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003100 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003101 {
3102 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003103 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003104 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003105 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003106 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3107 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003108 vim_free(ptr);
3109 }
3110 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003111 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003112 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003113 if (regmatch.regprog == NULL)
3114 goto theend;
3115 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003116
Bram Moolenaar071d4272004-06-13 20:20:40 +00003117 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3118 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003119 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003120 {
3121 /* copy one dictionary file name into buf */
3122 if (flags == DICT_EXACT)
3123 {
3124 count = 1;
3125 files = &dict;
3126 }
3127 else
3128 {
3129 /* Expand wildcards in the dictionary name, but do not allow
3130 * backticks (for security, the 'dict' option may have been set in
3131 * a modeline). */
3132 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003133# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003134 if (!thesaurus && STRCMP(buf, "spell") == 0)
3135 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003136 else
3137# endif
3138 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003139 || expand_wildcards(1, &buf, &count, &files,
3140 EW_FILE|EW_SILENT) != OK)
3141 count = 0;
3142 }
3143
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003144# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003145 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003146 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003147 /* Complete from active spelling. Skip "\<" in the pattern, we
3148 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003149 if (pat[0] == '\\' && pat[1] == '<')
3150 ptr = pat + 2;
3151 else
3152 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003153 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003155 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003156# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003157 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003158 {
3159 ins_compl_files(count, files, thesaurus, flags,
3160 &regmatch, buf, &dir);
3161 if (flags != DICT_EXACT)
3162 FreeWild(count, files);
3163 }
3164 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 break;
3166 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003167
3168theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003170 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 vim_free(buf);
3172}
3173
Bram Moolenaar0b238792006-03-02 22:49:12 +00003174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003175ins_compl_files(
3176 int count,
3177 char_u **files,
3178 int thesaurus,
3179 int flags,
3180 regmatch_T *regmatch,
3181 char_u *buf,
3182 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003183{
3184 char_u *ptr;
3185 int i;
3186 FILE *fp;
3187 int add_r;
3188
3189 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3190 {
3191 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3192 if (flags != DICT_EXACT)
3193 {
3194 vim_snprintf((char *)IObuff, IOSIZE,
3195 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003196 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003197 }
3198
3199 if (fp != NULL)
3200 {
3201 /*
3202 * Read dictionary file line by line.
3203 * Check each line for a match.
3204 */
3205 while (!got_int && !compl_interrupted
3206 && !vim_fgets(buf, LSIZE, fp))
3207 {
3208 ptr = buf;
3209 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3210 {
3211 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003212 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003213 ptr = find_line_end(ptr);
3214 else
3215 ptr = find_word_end(ptr);
3216 add_r = ins_compl_add_infercase(regmatch->startp[0],
3217 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003218 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003219 if (thesaurus)
3220 {
3221 char_u *wstart;
3222
3223 /*
3224 * Add the other matches on the line
3225 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003226 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003227 while (!got_int)
3228 {
3229 /* Find start of the next word. Skip white
3230 * space and punctuation. */
3231 ptr = find_word_start(ptr);
3232 if (*ptr == NUL || *ptr == NL)
3233 break;
3234 wstart = ptr;
3235
Bram Moolenaara2993e12007-08-12 14:38:46 +00003236 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003237#ifdef FEAT_MBYTE
3238 if (has_mbyte)
3239 /* Japanese words may have characters in
3240 * different classes, only separate words
3241 * with single-byte non-word characters. */
3242 while (*ptr != NUL)
3243 {
3244 int l = (*mb_ptr2len)(ptr);
3245
3246 if (l < 2 && !vim_iswordc(*ptr))
3247 break;
3248 ptr += l;
3249 }
3250 else
3251#endif
3252 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003253
3254 /* Add the word. Skip the regexp match. */
3255 if (wstart != regmatch->startp[0])
3256 add_r = ins_compl_add_infercase(wstart,
3257 (int)(ptr - wstart),
3258 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003259 }
3260 }
3261 if (add_r == OK)
3262 /* if dir was BACKWARD then honor it just once */
3263 *dir = FORWARD;
3264 else if (add_r == FAIL)
3265 break;
3266 /* avoid expensive call to vim_regexec() when at end
3267 * of line */
3268 if (*ptr == '\n' || got_int)
3269 break;
3270 }
3271 line_breakcheck();
3272 ins_compl_check_keys(50);
3273 }
3274 fclose(fp);
3275 }
3276 }
3277}
3278
Bram Moolenaar071d4272004-06-13 20:20:40 +00003279/*
3280 * Find the start of the next word.
3281 * Returns a pointer to the first char of the word. Also stops at a NUL.
3282 */
3283 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003284find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285{
3286#ifdef FEAT_MBYTE
3287 if (has_mbyte)
3288 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003289 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290 else
3291#endif
3292 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3293 ++ptr;
3294 return ptr;
3295}
3296
3297/*
3298 * Find the end of the word. Assumes it starts inside a word.
3299 * Returns a pointer to just after the word.
3300 */
3301 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303{
3304#ifdef FEAT_MBYTE
3305 int start_class;
3306
3307 if (has_mbyte)
3308 {
3309 start_class = mb_get_class(ptr);
3310 if (start_class > 1)
3311 while (*ptr != NUL)
3312 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003313 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003314 if (mb_get_class(ptr) != start_class)
3315 break;
3316 }
3317 }
3318 else
3319#endif
3320 while (vim_iswordc(*ptr))
3321 ++ptr;
3322 return ptr;
3323}
3324
3325/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003326 * Find the end of the line, omitting CR and NL at the end.
3327 * Returns a pointer to just after the line.
3328 */
3329 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003330find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003331{
3332 char_u *s;
3333
3334 s = ptr + STRLEN(ptr);
3335 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3336 --s;
3337 return s;
3338}
3339
3340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 * Free the list of completions
3342 */
3343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003344ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003346 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003347 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003348
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003349 vim_free(compl_pattern);
3350 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003351 vim_free(compl_leader);
3352 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003354 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003356
3357 ins_compl_del_pum();
3358 pum_clear();
3359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003360 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361 do
3362 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003363 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003364 compl_curr_match = compl_curr_match->cp_next;
3365 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003367 if (match->cp_flags & FREE_FNAME)
3368 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003369 for (i = 0; i < CPT_COUNT; ++i)
3370 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003372 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3373 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003374 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375}
3376
3377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003378ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003380 compl_cont_status = 0;
3381 compl_started = FALSE;
3382 compl_matches = 0;
3383 vim_free(compl_pattern);
3384 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003385 vim_free(compl_leader);
3386 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003388 vim_free(compl_orig_text);
3389 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003390 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003391 /* clear v:completed_item */
3392 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393}
3394
3395/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003396 * Return TRUE when Insert completion is active.
3397 */
3398 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003399ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003400{
3401 return compl_started;
3402}
3403
3404/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003405 * Delete one character before the cursor and show the subset of the matches
3406 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003407 * Returns the character to be used, NUL if the work is done and another char
3408 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003409 */
3410 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003411ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003412{
3413 char_u *line;
3414 char_u *p;
3415
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003416 line = ml_get_curline();
3417 p = line + curwin->w_cursor.col;
3418 mb_ptr_back(line, p);
3419
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003420 /* Stop completion when the whole word was deleted. For Omni completion
3421 * allow the word to be deleted, we won't match everything. */
3422 if ((int)(p - line) - (int)compl_col < 0
3423 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003424 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003425 return K_BS;
3426
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003427 /* Deleted more than what was used to find matches or didn't finish
3428 * finding all matches: need to look for matches all over again. */
3429 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003430 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003431 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003432
Bram Moolenaara6557602006-02-04 22:43:20 +00003433 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003434 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003435 if (compl_leader != NULL)
3436 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003437 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003438 if (compl_shown_match != NULL)
3439 /* Make sure current match is not a hidden item. */
3440 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003441 return NUL;
3442 }
3443 return K_BS;
3444}
Bram Moolenaara6557602006-02-04 22:43:20 +00003445
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003446/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003447 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3448 * be called.
3449 */
3450 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003451ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003452{
3453 /* Return TRUE if we didn't complete finding matches or when the
3454 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3455 return compl_was_interrupted
3456 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3457 && compl_opt_refresh_always);
3458}
3459
3460/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003461 * Called after changing "compl_leader".
3462 * Show the popup menu with a different set of matches.
3463 * May also search for matches again if the previous search was interrupted.
3464 */
3465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003466ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003467{
3468 ins_compl_del_pum();
3469 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003470 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003471 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003472
3473 if (compl_started)
3474 ins_compl_set_original_text(compl_leader);
3475 else
3476 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003477#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003478 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003479#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003480 /*
3481 * Matches were cleared, need to search for them now. First display
3482 * the changed text before the cursor. Set "compl_restarting" to
3483 * avoid that the first match is inserted.
3484 */
3485 update_screen(0);
3486#ifdef FEAT_GUI
3487 if (gui.in_use)
3488 {
3489 /* Show the cursor after the match, not after the redrawn text. */
3490 setcursor();
3491 out_flush();
3492 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003493 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003494#endif
3495 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003496 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003497 compl_cont_status = 0;
3498 compl_restarting = FALSE;
3499 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003500
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003501 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003502
3503 /* Show the popup menu with a different set of matches. */
3504 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003505
3506 /* Don't let Enter select the original text when there is no popup menu. */
3507 if (compl_match_array == NULL)
3508 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003509}
3510
3511/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003512 * Return the length of the completion, from the completion start column to
3513 * the cursor column. Making sure it never goes below zero.
3514 */
3515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003516ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003517{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003518 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003519
3520 if (off < 0)
3521 return 0;
3522 return off;
3523}
3524
3525/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003526 * Append one character to the match leader. May reduce the number of
3527 * matches.
3528 */
3529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003530ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003531{
3532#ifdef FEAT_MBYTE
3533 int cc;
3534
3535 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3536 {
3537 char_u buf[MB_MAXBYTES + 1];
3538
3539 (*mb_char2bytes)(c, buf);
3540 buf[cc] = NUL;
3541 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003542 if (compl_opt_refresh_always)
3543 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003544 }
3545 else
3546#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003547 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003548 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003549 if (compl_opt_refresh_always)
3550 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003551 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003552
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003553 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003554 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003555 ins_compl_restart();
3556
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003557 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003558 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003559 * break redo. */
3560 if (!compl_opt_refresh_always)
3561 {
3562 vim_free(compl_leader);
3563 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003564 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003565 if (compl_leader != NULL)
3566 ins_compl_new_leader();
3567 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003568}
3569
3570/*
3571 * Setup for finding completions again without leaving CTRL-X mode. Used when
3572 * BS or a key was typed while still searching for matches.
3573 */
3574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003575ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003576{
3577 ins_compl_free();
3578 compl_started = FALSE;
3579 compl_matches = 0;
3580 compl_cont_status = 0;
3581 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003582}
3583
3584/*
3585 * Set the first match, the original text.
3586 */
3587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003588ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003589{
3590 char_u *p;
3591
3592 /* Replace the original text entry. */
3593 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3594 {
3595 p = vim_strsave(str);
3596 if (p != NULL)
3597 {
3598 vim_free(compl_first_match->cp_str);
3599 compl_first_match->cp_str = p;
3600 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003601 }
3602}
3603
3604/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003605 * Append one character to the match leader. May reduce the number of
3606 * matches.
3607 */
3608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003609ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003610{
3611 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003612 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003613 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003614 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003615
3616 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003617 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003618 {
3619 /* When still at the original match use the first entry that matches
3620 * the leader. */
3621 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3622 {
3623 p = NULL;
3624 for (cp = compl_shown_match->cp_next; cp != NULL
3625 && cp != compl_first_match; cp = cp->cp_next)
3626 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003627 if (compl_leader == NULL
3628 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003629 (int)STRLEN(compl_leader)))
3630 {
3631 p = cp->cp_str;
3632 break;
3633 }
3634 }
3635 if (p == NULL || (int)STRLEN(p) <= len)
3636 return;
3637 }
3638 else
3639 return;
3640 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003641 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003642 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003643 ins_compl_addleader(c);
3644}
3645
3646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003648 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003649 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003651 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003652ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653{
3654 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003656 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657
3658 /* Forget any previous 'special' messages if this is actually
3659 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3660 */
3661 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3662 edit_submode_extra = NULL;
3663
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003664 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003665 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3666 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003667 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003669 /* Set "compl_get_longest" when finding the first matches. */
3670 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3671 || (ctrl_x_mode == 0 && !compl_started))
3672 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003673 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003674 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003675
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003676 }
3677
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003678 compl_no_insert = FALSE;
3679 compl_no_select = FALSE;
3680 if (strstr((char *)p_cot, "noselect") != NULL)
3681 compl_no_select = TRUE;
3682 if (strstr((char *)p_cot, "noinsert") != NULL)
3683 compl_no_insert = TRUE;
3684
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3686 {
3687 /*
3688 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3689 * it will be yet. Now we decide.
3690 */
3691 switch (c)
3692 {
3693 case Ctrl_E:
3694 case Ctrl_Y:
3695 ctrl_x_mode = CTRL_X_SCROLL;
3696 if (!(State & REPLACE_FLAG))
3697 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3698 else
3699 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3700 edit_submode_pre = NULL;
3701 showmode();
3702 break;
3703 case Ctrl_L:
3704 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3705 break;
3706 case Ctrl_F:
3707 ctrl_x_mode = CTRL_X_FILES;
3708 break;
3709 case Ctrl_K:
3710 ctrl_x_mode = CTRL_X_DICTIONARY;
3711 break;
3712 case Ctrl_R:
3713 /* Simply allow ^R to happen without affecting ^X mode */
3714 break;
3715 case Ctrl_T:
3716 ctrl_x_mode = CTRL_X_THESAURUS;
3717 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003718#ifdef FEAT_COMPL_FUNC
3719 case Ctrl_U:
3720 ctrl_x_mode = CTRL_X_FUNCTION;
3721 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003722 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003723 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003724 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003725#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003726 case 's':
3727 case Ctrl_S:
3728 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003729#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003730 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003731 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003732 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003733#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003734 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 case Ctrl_RSB:
3736 ctrl_x_mode = CTRL_X_TAGS;
3737 break;
3738#ifdef FEAT_FIND_ID
3739 case Ctrl_I:
3740 case K_S_TAB:
3741 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3742 break;
3743 case Ctrl_D:
3744 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3745 break;
3746#endif
3747 case Ctrl_V:
3748 case Ctrl_Q:
3749 ctrl_x_mode = CTRL_X_CMDLINE;
3750 break;
3751 case Ctrl_P:
3752 case Ctrl_N:
3753 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3754 * just started ^X mode, or there were enough ^X's to cancel
3755 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3756 * do normal expansion when interrupting a different mode (say
3757 * ^X^F^X^P or ^P^X^X^P, see below)
3758 * nothing changes if interrupting mode 0, (eg, the flag
3759 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003760 if (!(compl_cont_status & CONT_INTRPT))
3761 compl_cont_status |= CONT_LOCAL;
3762 else if (compl_cont_mode != 0)
3763 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003764 /* FALLTHROUGH */
3765 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003766 /* If we have typed at least 2 ^X's... for modes != 0, we set
3767 * compl_cont_status = 0 (eg, as if we had just started ^X
3768 * mode).
3769 * For mode 0, we set "compl_cont_mode" to an impossible
3770 * value, in both cases ^X^X can be used to restart the same
3771 * mode (avoiding ADDING mode).
3772 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3773 * 'complete' and local ^P expansions respectively.
3774 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3775 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 if (c == Ctrl_X)
3777 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003778 if (compl_cont_mode != 0)
3779 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003781 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
3783 ctrl_x_mode = 0;
3784 edit_submode = NULL;
3785 showmode();
3786 break;
3787 }
3788 }
3789 else if (ctrl_x_mode != 0)
3790 {
3791 /* We're already in CTRL-X mode, do we stay in it? */
3792 if (!vim_is_ctrl_x_key(c))
3793 {
3794 if (ctrl_x_mode == CTRL_X_SCROLL)
3795 ctrl_x_mode = 0;
3796 else
3797 ctrl_x_mode = CTRL_X_FINISHED;
3798 edit_submode = NULL;
3799 }
3800 showmode();
3801 }
3802
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003803 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 {
3805 /* Show error message from attempted keyword completion (probably
3806 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003809 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3810 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 || ctrl_x_mode == CTRL_X_FINISHED)
3812 {
3813 /* Get here when we have finished typing a sequence of ^N and
3814 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003816 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 {
3818 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003819 * If any of the original typed text has been changed, eg when
3820 * ignorecase is set, we must add back-spaces to the redo
3821 * buffer. We add as few as necessary to delete just the part
3822 * of the original text that has changed.
3823 * When using the longest match, edited the match or used
3824 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003825 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003826 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3827 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003828 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003829 ptr = NULL;
3830 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832
3833#ifdef FEAT_CINDENT
3834 want_cindent = (can_cindent && cindent_on());
3835#endif
3836 /*
3837 * When completing whole lines: fix indent for 'cindent'.
3838 * Otherwise, break line if it's too long.
3839 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003840 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
3842#ifdef FEAT_CINDENT
3843 /* re-indent the current line */
3844 if (want_cindent)
3845 {
3846 do_c_expr_indent();
3847 want_cindent = FALSE; /* don't do it again */
3848 }
3849#endif
3850 }
3851 else
3852 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003853 int prev_col = curwin->w_cursor.col;
3854
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003856 if (prev_col > 0)
3857 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 if (stop_arrow() == OK)
3859 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003860 if (prev_col > 0
3861 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3862 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003863 }
3864
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003865 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003866 * the selection without inserting anything. When
3867 * compl_enter_selects is set the Enter key does the same. */
3868 if ((c == Ctrl_Y || (compl_enter_selects
3869 && (c == CAR || c == K_KENTER || c == NL)))
3870 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003871 retval = TRUE;
3872
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003873 /* CTRL-E means completion is Ended, go back to the typed text. */
3874 if (c == Ctrl_E)
3875 {
3876 ins_compl_delete();
3877 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003878 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003879 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003880 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003881 retval = TRUE;
3882 }
3883
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003884 auto_format(FALSE, TRUE);
3885
Bram Moolenaar071d4272004-06-13 20:20:40 +00003886 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003887 compl_started = FALSE;
3888 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003889 if (!shortmess(SHM_COMPLETIONMENU))
3890 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003892 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 if (edit_submode != NULL)
3894 {
3895 edit_submode = NULL;
3896 showmode();
3897 }
3898
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003899#ifdef FEAT_CMDWIN
3900 if (c == Ctrl_C && cmdwin_type != 0)
3901 /* Avoid the popup menu remains displayed when leaving the
3902 * command line window. */
3903 update_screen(0);
3904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003905#ifdef FEAT_CINDENT
3906 /*
3907 * Indent now if a key was typed that is in 'cinkeys'.
3908 */
3909 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3910 do_c_expr_indent();
3911#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003912#ifdef FEAT_AUTOCMD
3913 /* Trigger the CompleteDone event to give scripts a chance to act
3914 * upon the completion. */
3915 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 }
3918 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003919#ifdef FEAT_AUTOCMD
3920 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3921 /* Trigger the CompleteDone event to give scripts a chance to act
3922 * upon the (possibly failed) completion. */
3923 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925
3926 /* reset continue_* if we left expansion-mode, if we stay they'll be
3927 * (re)set properly in ins_complete() */
3928 if (!vim_is_ctrl_x_key(c))
3929 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003930 compl_cont_status = 0;
3931 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003933
3934 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935}
3936
3937/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003938 * Fix the redo buffer for the completion leader replacing some of the typed
3939 * text. This inserts backspaces and appends the changed text.
3940 * "ptr" is the known leader text or NUL.
3941 */
3942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003943ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003944{
3945 int len;
3946 char_u *p;
3947 char_u *ptr = ptr_arg;
3948
3949 if (ptr == NULL)
3950 {
3951 if (compl_leader != NULL)
3952 ptr = compl_leader;
3953 else
3954 return; /* nothing to do */
3955 }
3956 if (compl_orig_text != NULL)
3957 {
3958 p = compl_orig_text;
3959 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3960 ;
3961#ifdef FEAT_MBYTE
3962 if (len > 0)
3963 len -= (*mb_head_off)(p, p + len);
3964#endif
3965 for (p += len; *p != NUL; mb_ptr_adv(p))
3966 AppendCharToRedobuff(K_BS);
3967 }
3968 else
3969 len = 0;
3970 if (ptr != NULL)
3971 AppendToRedobuffLit(ptr + len, -1);
3972}
3973
3974/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3976 * (depending on flag) starting from buf and looking for a non-scanned
3977 * buffer (other than curbuf). curbuf is special, if it is called with
3978 * buf=curbuf then it has to be the first call for a given flag/expansion.
3979 *
3980 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3981 */
3982 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003983ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003984{
3985#ifdef FEAT_WINDOWS
3986 static win_T *wp;
3987#endif
3988
3989 if (flag == 'w') /* just windows */
3990 {
3991#ifdef FEAT_WINDOWS
3992 if (buf == curbuf) /* first call for this flag/expansion */
3993 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003994 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 && wp->w_buffer->b_scanned)
3996 ;
3997 buf = wp->w_buffer;
3998#else
3999 buf = curbuf;
4000#endif
4001 }
4002 else
4003 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4004 * (unlisted buffers)
4005 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004006 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004007 && ((flag == 'U'
4008 ? buf->b_p_bl
4009 : (!buf->b_p_bl
4010 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004011 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 ;
4013 return buf;
4014}
4015
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004016#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01004017static void expand_by_function(int type, char_u *base);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004018
4019/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004020 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004021 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004022 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004024expand_by_function(
4025 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4026 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004027{
Bram Moolenaar82139082011-09-14 16:52:09 +02004028 list_T *matchlist = NULL;
4029 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004030 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004031 char_u *funcname;
4032 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004033 win_T *curwin_save;
4034 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004035 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004036
Bram Moolenaare344bea2005-09-01 20:46:49 +00004037 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4038 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004039 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004040
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004041 /* Call 'completefunc' to obtain the list of matches. */
4042 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004043 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004044
Bram Moolenaare344bea2005-09-01 20:46:49 +00004045 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004046 curwin_save = curwin;
4047 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004048
4049 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004050 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004051 {
4052 switch (rettv.v_type)
4053 {
4054 case VAR_LIST:
4055 matchlist = rettv.vval.v_list;
4056 break;
4057 case VAR_DICT:
4058 matchdict = rettv.vval.v_dict;
4059 break;
4060 default:
4061 /* TODO: Give error message? */
4062 clear_tv(&rettv);
4063 break;
4064 }
4065 }
4066
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004067 if (curwin_save != curwin || curbuf_save != curbuf)
4068 {
4069 EMSG(_(e_complwin));
4070 goto theend;
4071 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004072 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004073 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004074 if (!equalpos(curwin->w_cursor, pos))
4075 {
4076 EMSG(_(e_compldel));
4077 goto theend;
4078 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004079
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004080 if (matchlist != NULL)
4081 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004082 else if (matchdict != NULL)
4083 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004084
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004085theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004086 if (matchdict != NULL)
4087 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004088 if (matchlist != NULL)
4089 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004090}
4091#endif /* FEAT_COMPL_FUNC */
4092
Bram Moolenaar39f05632006-03-19 22:15:26 +00004093#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004094/*
4095 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004096 */
4097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004098ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004099{
4100 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004101 int dir = compl_direction;
4102
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004103 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004104 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004105 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004106 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4107 /* if dir was BACKWARD then honor it just once */
4108 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004109 else if (did_emsg)
4110 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004111 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004112}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004113
4114/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004115 * Add completions from a dict.
4116 */
4117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004118ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004119{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004120 dictitem_T *di_refresh;
4121 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004122
4123 /* Check for optional "refresh" item. */
4124 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004125 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4126 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004127 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004128 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004129
4130 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4131 compl_opt_refresh_always = TRUE;
4132 }
4133
4134 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004135 di_words = dict_find(dict, (char_u *)"words", 5);
4136 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4137 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004138}
4139
4140/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004141 * Add a match to the list of matches from a typeval_T.
4142 * If the given string is already in the list of completions, then return
4143 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4144 * maybe because alloc() returns NULL, then FAIL is returned.
4145 */
4146 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004147ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004148{
4149 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004150 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004151 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004152 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004153 char_u *(cptext[CPT_COUNT]);
4154
4155 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4156 {
4157 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4158 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4159 (char_u *)"abbr", FALSE);
4160 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4161 (char_u *)"menu", FALSE);
4162 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4163 (char_u *)"kind", FALSE);
4164 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4165 (char_u *)"info", FALSE);
4166 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4167 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004168 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004169 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004170 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4171 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004172 }
4173 else
4174 {
4175 word = get_tv_string_chk(tv);
4176 vim_memset(cptext, 0, sizeof(cptext));
4177 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004178 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004179 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004180 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004181}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004182#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004183
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004184/*
4185 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004186 * The search starts at position "ini" in curbuf and in the direction
4187 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004188 * When "compl_started" is FALSE start at that position, otherwise continue
4189 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004190 * This may return before finding all the matches.
4191 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 */
4193 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004194ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195{
4196 static pos_T first_match_pos;
4197 static pos_T last_match_pos;
4198 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004199 static int found_all = FALSE; /* Found all matches of a
4200 certain type. */
4201 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202
Bram Moolenaar572cb562005-08-05 21:35:02 +00004203 pos_T *pos;
4204 char_u **matches;
4205 int save_p_scs;
4206 int save_p_ws;
4207 int save_p_ic;
4208 int i;
4209 int num_matches;
4210 int len;
4211 int found_new_match;
4212 int type = ctrl_x_mode;
4213 char_u *ptr;
4214 char_u *dict = NULL;
4215 int dict_f = 0;
4216 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004217 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004219 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
4221 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4222 ins_buf->b_scanned = 0;
4223 found_all = FALSE;
4224 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004225 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004226 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 last_match_pos = first_match_pos = *ini;
4228 }
4229
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004230 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004231 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4233 for (;;)
4234 {
4235 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004236 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 * or if found_all says this entry is done. For ^X^L only use the
4240 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004241 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 {
4244 found_all = FALSE;
4245 while (*e_cpt == ',' || *e_cpt == ' ')
4246 e_cpt++;
4247 if (*e_cpt == '.' && !curbuf->b_scanned)
4248 {
4249 ins_buf = curbuf;
4250 first_match_pos = *ini;
4251 /* So that ^N can match word immediately after cursor */
4252 if (ctrl_x_mode == 0)
4253 dec(&first_match_pos);
4254 last_match_pos = first_match_pos;
4255 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004256
4257 /* Remember the first match so that the loop stops when we
4258 * wrap and come back there a second time. */
4259 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 }
4261 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4262 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4263 {
4264 /* Scan a buffer, but not the current one. */
4265 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4266 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004267 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 first_match_pos.col = last_match_pos.col = 0;
4269 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4270 last_match_pos.lnum = 0;
4271 type = 0;
4272 }
4273 else /* unloaded buffer, scan like dictionary */
4274 {
4275 found_all = TRUE;
4276 if (ins_buf->b_fname == NULL)
4277 continue;
4278 type = CTRL_X_DICTIONARY;
4279 dict = ins_buf->b_fname;
4280 dict_f = DICT_EXACT;
4281 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004282 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 ins_buf->b_fname == NULL
4284 ? buf_spname(ins_buf)
4285 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004286 ? ins_buf->b_fname
4287 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004288 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 else if (*e_cpt == NUL)
4291 break;
4292 else
4293 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004294 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 type = -1;
4296 else if (*e_cpt == 'k' || *e_cpt == 's')
4297 {
4298 if (*e_cpt == 'k')
4299 type = CTRL_X_DICTIONARY;
4300 else
4301 type = CTRL_X_THESAURUS;
4302 if (*++e_cpt != ',' && *e_cpt != NUL)
4303 {
4304 dict = e_cpt;
4305 dict_f = DICT_FIRST;
4306 }
4307 }
4308#ifdef FEAT_FIND_ID
4309 else if (*e_cpt == 'i')
4310 type = CTRL_X_PATH_PATTERNS;
4311 else if (*e_cpt == 'd')
4312 type = CTRL_X_PATH_DEFINES;
4313#endif
4314 else if (*e_cpt == ']' || *e_cpt == 't')
4315 {
4316 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004317 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004318 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 }
4320 else
4321 type = -1;
4322
4323 /* in any case e_cpt is advanced to the next entry */
4324 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4325
4326 found_all = TRUE;
4327 if (type == -1)
4328 continue;
4329 }
4330 }
4331
4332 switch (type)
4333 {
4334 case -1:
4335 break;
4336#ifdef FEAT_FIND_ID
4337 case CTRL_X_PATH_PATTERNS:
4338 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004339 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004342 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4344 (linenr_T)1, (linenr_T)MAXLNUM);
4345 break;
4346#endif
4347
4348 case CTRL_X_DICTIONARY:
4349 case CTRL_X_THESAURUS:
4350 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004351 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 : (type == CTRL_X_THESAURUS
4353 ? (*curbuf->b_p_tsr == NUL
4354 ? p_tsr
4355 : curbuf->b_p_tsr)
4356 : (*curbuf->b_p_dict == NUL
4357 ? p_dict
4358 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004359 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004360 dict != NULL ? dict_f
4361 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 dict = NULL;
4363 break;
4364
4365 case CTRL_X_TAGS:
4366 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4367 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004368 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004370 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004371 * of matches is found when compl_pattern is empty */
4372 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4374 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4375 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4376 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004377 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 }
4379 p_ic = save_p_ic;
4380 break;
4381
4382 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004383 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4385 {
4386
4387 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004388 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004389 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 }
4391 break;
4392
4393 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004394 if (expand_cmdline(&compl_xp, compl_pattern,
4395 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004397 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 break;
4399
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004400#ifdef FEAT_COMPL_FUNC
4401 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004402 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004403 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004404 break;
4405#endif
4406
Bram Moolenaar488c6512005-08-11 20:09:58 +00004407 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004408#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004409 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004410 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004411 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004412 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004413#endif
4414 break;
4415
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 default: /* normal ^P/^N and ^X^L */
4417 /*
4418 * If 'infercase' is set, don't use 'smartcase' here
4419 */
4420 save_p_scs = p_scs;
4421 if (ins_buf->b_p_inf)
4422 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004423
Bram Moolenaar361aa502014-01-23 22:45:58 +01004424 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 * end but never from the middle, thus setting nowrapscan in this
4426 * buffers is a good idea, on the other hand, we always set
4427 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4428 save_p_ws = p_ws;
4429 if (ins_buf != curbuf)
4430 p_ws = FALSE;
4431 else if (*e_cpt == '.')
4432 p_ws = TRUE;
4433 for (;;)
4434 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004435 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004437 ++msg_silent; /* Don't want messages for wrapscan. */
4438
Bram Moolenaare4214502015-03-05 18:08:43 +01004439 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4440 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004441 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004442 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004443 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004445 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004447 found_new_match = searchit(NULL, ins_buf, pos,
4448 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004449 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004450 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004451 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004452 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004454 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 first_match_pos = *pos;
4457 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004458 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 }
4460 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004461 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 found_new_match = FAIL;
4463 if (found_new_match == FAIL)
4464 {
4465 if (ins_buf == curbuf)
4466 found_all = TRUE;
4467 break;
4468 }
4469
4470 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004471 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 && ini->lnum == pos->lnum
4473 && ini->col == pos->col)
4474 continue;
4475 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004476 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004478 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
4480 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4481 continue;
4482 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4483 if (!p_paste)
4484 ptr = skipwhite(ptr);
4485 }
4486 len = (int)STRLEN(ptr);
4487 }
4488 else
4489 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004490 char_u *tmp_ptr = ptr;
4491
4492 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004494 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 /* Skip if already inside a word. */
4496 if (vim_iswordp(tmp_ptr))
4497 continue;
4498 /* Find start of next word. */
4499 tmp_ptr = find_word_start(tmp_ptr);
4500 }
4501 /* Find end of this word. */
4502 tmp_ptr = find_word_end(tmp_ptr);
4503 len = (int)(tmp_ptr - ptr);
4504
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004505 if ((compl_cont_status & CONT_ADDING)
4506 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 {
4508 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4509 {
4510 /* Try next line, if any. the new word will be
4511 * "join" as if the normal command "J" was used.
4512 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 * works -- Acevedo */
4515 STRNCPY(IObuff, ptr, len);
4516 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4517 tmp_ptr = ptr = skipwhite(ptr);
4518 /* Find start of next word. */
4519 tmp_ptr = find_word_start(tmp_ptr);
4520 /* Find end of next word. */
4521 tmp_ptr = find_word_end(tmp_ptr);
4522 if (tmp_ptr > ptr)
4523 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004524 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004526 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 IObuff[len++] = ' ';
4528 /* IObuf =~ "\k.* ", thus len >= 2 */
4529 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004530 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 || (vim_strchr(p_cpo, CPO_JOINSP)
4532 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004533 && (IObuff[len - 2] == '?'
4534 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 IObuff[len++] = ' ';
4536 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004537 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 if (tmp_ptr - ptr >= IOSIZE - len)
4539 tmp_ptr = ptr + IOSIZE - len - 1;
4540 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4541 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004542 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543 }
4544 IObuff[len] = NUL;
4545 ptr = IObuff;
4546 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548 continue;
4549 }
4550 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004551 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004552 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004553 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 {
4555 found_new_match = OK;
4556 break;
4557 }
4558 }
4559 p_scs = save_p_scs;
4560 p_ws = save_p_ws;
4561 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004562
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004564 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004565 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 found_new_match = OK;
4567
4568 /* break the loop for specialized modes (use 'complete' just for the
4569 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004570 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004571 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004572 {
4573 if (got_int)
4574 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004575 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004576 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004577 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004578
Bram Moolenaare4214502015-03-05 18:08:43 +01004579 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004580 || compl_interrupted)
4581 break;
4582 compl_started = TRUE;
4583 }
4584 else
4585 {
4586 /* Mark a buffer scanned when it has been scanned completely */
4587 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4588 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004590 compl_started = FALSE;
4591 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594
Bram Moolenaare4214502015-03-05 18:08:43 +01004595 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 && *e_cpt == NUL) /* Got to end of 'complete' */
4597 found_new_match = FAIL;
4598
4599 i = -1; /* total of matches, unknown */
4600 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004601 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004602 i = ins_compl_make_cyclic();
4603
4604 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004605 * just been made cyclic then we have to move compl_curr_match to the next
4606 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004607 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4608 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004609 if (compl_curr_match == NULL)
4610 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 return i;
4612}
4613
4614/* Delete the old text being completed. */
4615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004616ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617{
4618 int i;
4619
4620 /*
4621 * In insert mode: Delete the typed part.
4622 * In replace mode: Put the old characters back, if any.
4623 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004624 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 backspace_until_column(i);
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004626
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004627 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4628 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004630 /* clear v:completed_item */
4631 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632}
4633
4634/* Insert the new text being completed. */
4635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004636ins_compl_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004638 dict_T *dict;
4639
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004640 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004641 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4642 compl_used_match = FALSE;
4643 else
4644 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004645
4646 /* Set completed item. */
4647 /* { word, abbr, menu, kind, info } */
4648 dict = dict_alloc();
4649 if (dict != NULL)
4650 {
4651 dict_add_nr_str(dict, "word", 0L,
4652 EMPTY_IF_NULL(compl_shown_match->cp_str));
4653 dict_add_nr_str(dict, "abbr", 0L,
4654 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4655 dict_add_nr_str(dict, "menu", 0L,
4656 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4657 dict_add_nr_str(dict, "kind", 0L,
4658 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4659 dict_add_nr_str(dict, "info", 0L,
4660 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4661 }
4662 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663}
4664
4665/*
4666 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004667 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4668 * get more completions. If it is FALSE, then we just do nothing when there
4669 * are no more completions in a given direction. The latter case is used when
4670 * we are still in the middle of finding completions, to allow browsing
4671 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 * Return the total number of matches, or -1 if still unknown -- webb.
4673 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004674 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4675 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 *
4677 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004678 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4679 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 */
4681 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004682ins_compl_next(
4683 int allow_get_expansion,
4684 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004685 be at least 1 */
Bram Moolenaar7454a062016-01-30 15:14:10 +01004686 int insert_match) /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687{
4688 int num_matches = -1;
4689 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004690 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004691 compl_T *found_compl = NULL;
4692 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004693 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004694 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004696 /* When user complete function return -1 for findstart which is next
4697 * time of 'always', compl_shown_match become NULL. */
4698 if (compl_shown_match == NULL)
4699 return -1;
4700
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004701 if (compl_leader != NULL
4702 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004704 /* Set "compl_shown_match" to the actually shown match, it may differ
4705 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004706 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004707 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004708 && compl_shown_match->cp_next != NULL
4709 && compl_shown_match->cp_next != compl_first_match)
4710 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004711
4712 /* If we didn't find it searching forward, and compl_shows_dir is
4713 * backward, find the last match. */
4714 if (compl_shows_dir == BACKWARD
4715 && !ins_compl_equal(compl_shown_match,
4716 compl_leader, (int)STRLEN(compl_leader))
4717 && (compl_shown_match->cp_next == NULL
4718 || compl_shown_match->cp_next == compl_first_match))
4719 {
4720 while (!ins_compl_equal(compl_shown_match,
4721 compl_leader, (int)STRLEN(compl_leader))
4722 && compl_shown_match->cp_prev != NULL
4723 && compl_shown_match->cp_prev != compl_first_match)
4724 compl_shown_match = compl_shown_match->cp_prev;
4725 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004726 }
4727
4728 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004729 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730 /* Delete old text to be replaced */
4731 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004732
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004733 /* When finding the longest common text we stick at the original text,
4734 * don't let CTRL-N or CTRL-P move to the first match. */
4735 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4736
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004737 /* When restarting the search don't insert the first match either. */
4738 if (compl_restarting)
4739 {
4740 advance = FALSE;
4741 compl_restarting = FALSE;
4742 }
4743
Bram Moolenaare3226be2005-12-18 22:10:00 +00004744 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4745 * around. */
4746 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004748 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004750 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004751 found_end = (compl_first_match != NULL
4752 && (compl_shown_match->cp_next == compl_first_match
4753 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004754 }
4755 else if (compl_shows_dir == BACKWARD
4756 && compl_shown_match->cp_prev != NULL)
4757 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004758 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004759 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004760 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004763 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004764 if (!allow_get_expansion)
4765 {
4766 if (advance)
4767 {
4768 if (compl_shows_dir == BACKWARD)
4769 compl_pending -= todo + 1;
4770 else
4771 compl_pending += todo + 1;
4772 }
4773 return -1;
4774 }
4775
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004776 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004777 {
4778 if (compl_shows_dir == BACKWARD)
4779 --compl_pending;
4780 else
4781 ++compl_pending;
4782 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004783
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004784 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004785 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004786
4787 /* handle any pending completions */
4788 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004789 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004790 {
4791 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4792 {
4793 compl_shown_match = compl_shown_match->cp_next;
4794 --compl_pending;
4795 }
4796 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4797 {
4798 compl_shown_match = compl_shown_match->cp_prev;
4799 ++compl_pending;
4800 }
4801 else
4802 break;
4803 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004804 found_end = FALSE;
4805 }
4806 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4807 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004808 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004809 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004810 ++todo;
4811 else
4812 /* Remember a matching item. */
4813 found_compl = compl_shown_match;
4814
4815 /* Stop at the end of the list when we found a usable match. */
4816 if (found_end)
4817 {
4818 if (found_compl != NULL)
4819 {
4820 compl_shown_match = found_compl;
4821 break;
4822 }
4823 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
4826
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004827 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004828 if (compl_no_insert && !started)
4829 {
4830 ins_bytes(compl_orig_text + ins_compl_len());
4831 compl_used_match = FALSE;
4832 }
4833 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004834 {
4835 if (!compl_get_longest || compl_used_match)
4836 ins_compl_insert();
4837 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004838 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004839 }
4840 else
4841 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842
4843 if (!allow_get_expansion)
4844 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004845 /* may undisplay the popup menu first */
4846 ins_compl_upd_pum();
4847
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004848 /* redraw to show the user what was inserted */
4849 update_screen(0);
4850
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004851 /* display the updated popup menu */
4852 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004853#ifdef FEAT_GUI
4854 if (gui.in_use)
4855 {
4856 /* Show the cursor after the match, not after the redrawn text. */
4857 setcursor();
4858 out_flush();
4859 gui_update_cursor(FALSE, FALSE);
4860 }
4861#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004862
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 /* Delete old text to be replaced, since we're still searching and
4864 * don't want to match ourselves! */
4865 ins_compl_delete();
4866 }
4867
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004868 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004869 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004870 if (compl_no_insert && !started)
4871 compl_enter_selects = TRUE;
4872 else
4873 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004874
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 /*
4876 * Show the file name for the match (if any)
4877 * Truncate the file name to avoid a wait for return.
4878 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004879 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004882 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 if (i <= 0)
4884 i = 0;
4885 else
4886 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004887 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004888 msg(IObuff);
4889 redraw_cmdline = FALSE; /* don't overwrite! */
4890 }
4891
4892 return num_matches;
4893}
4894
4895/*
4896 * Call this while finding completions, to check whether the user has hit a key
4897 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004898 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004900 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 */
4902 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004903ins_compl_check_keys(int frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
4905 static int count = 0;
4906
4907 int c;
4908
4909 /* Don't check when reading keys from a script. That would break the test
4910 * scripts */
4911 if (using_script())
4912 return;
4913
4914 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004915 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 return;
4917 count = 0;
4918
Bram Moolenaara260a972006-06-23 19:36:29 +00004919 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4920 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (c != NUL)
4923 {
4924 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4925 {
4926 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004927 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004928 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4929 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004931 else
4932 {
4933 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004934 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004935 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004936 if (c != K_IGNORE)
4937 {
4938 /* Don't interrupt completion when the character wasn't typed,
4939 * e.g., when doing @q to replay keys. */
4940 if (c != Ctrl_R && KeyTyped)
4941 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004942
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004943 vungetc(c);
4944 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004947 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004948 {
4949 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4950
4951 compl_pending = 0;
4952 (void)ins_compl_next(FALSE, todo, TRUE);
4953 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004954}
4955
4956/*
4957 * Decide the direction of Insert mode complete from the key typed.
4958 * Returns BACKWARD or FORWARD.
4959 */
4960 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004961ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004962{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004963 if (c == Ctrl_P || c == Ctrl_L
4964 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4965 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004966 return BACKWARD;
4967 return FORWARD;
4968}
4969
4970/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004971 * Return TRUE for keys that are used for completion only when the popup menu
4972 * is visible.
4973 */
4974 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004975ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004976{
4977 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004978 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4979 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004980}
4981
4982/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004983 * Decide the number of completions to move forward.
4984 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4985 */
4986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004987ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004988{
4989 int h;
4990
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004991 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004992 {
4993 h = pum_get_height();
4994 if (h > 3)
4995 h -= 2; /* keep some context */
4996 return h;
4997 }
4998 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999}
5000
5001/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005002 * Return TRUE if completion with "c" should insert the match, FALSE if only
5003 * to change the currently selected completion.
5004 */
5005 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005006ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005007{
5008 switch (c)
5009 {
5010 case K_UP:
5011 case K_DOWN:
5012 case K_PAGEDOWN:
5013 case K_KPAGEDOWN:
5014 case K_S_DOWN:
5015 case K_PAGEUP:
5016 case K_KPAGEUP:
5017 case K_S_UP:
5018 return FALSE;
5019 }
5020 return TRUE;
5021}
5022
5023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 * Do Insert mode completion.
5025 * Called when character "c" was typed, which has a meaning for completion.
5026 * Returns OK if completion was done, FAIL if something failed (out of mem).
5027 */
5028 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005029ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005031 char_u *line;
5032 int startcol = 0; /* column where searched text starts */
5033 colnr_T curs_col; /* cursor column */
5034 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005035 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036
Bram Moolenaare3226be2005-12-18 22:10:00 +00005037 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005038 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
5040 /* First time we hit ^N or ^P (in a row, I mean) */
5041
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 did_ai = FALSE;
5043#ifdef FEAT_SMARTINDENT
5044 did_si = FALSE;
5045 can_si = FALSE;
5046 can_si_back = FALSE;
5047#endif
5048 if (stop_arrow() == FAIL)
5049 return FAIL;
5050
5051 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005052 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005053 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005055 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005056 * "compl_startpos" to the cursor as a pattern to add a new word
5057 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005058 * "compl_startpos" is not in the same line as the cursor then fix it
5059 * (the line has been split because it was longer than 'tw'). if SOL
5060 * is set then skip the previous pattern, a word at the beginning of
5061 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005062 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5063 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
5065 /*
5066 * it is a continued search
5067 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005068 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5070 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5071 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005072 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005074 /* line (probably) wrapped, set compl_startpos to the
5075 * first non_blank in the line, if it is not a wordchar
5076 * include it to get a better pattern, but then we don't
5077 * want the "\\<" prefix, check it bellow */
5078 compl_col = (colnr_T)(skipwhite(line) - line);
5079 compl_startpos.col = compl_col;
5080 compl_startpos.lnum = curwin->w_cursor.lnum;
5081 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083 else
5084 {
5085 /* S_IPOS was set when we inserted a word that was at the
5086 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005087 * mode but first we need to redefine compl_startpos */
5088 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005090 compl_cont_status |= CONT_SOL;
5091 compl_startpos.col = (colnr_T)(skipwhite(
5092 line + compl_length
5093 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005097 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005098 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005099 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005101 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 compl_cont_status &= ~CONT_SOL;
5104 compl_length = (IOSIZE - MIN_SPACE);
5105 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005107 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5108 if (compl_length < 1)
5109 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005111 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005114 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005117 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005119 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005123 compl_cont_status = 0;
5124 compl_cont_status |= CONT_N_ADDS;
5125 compl_startpos = curwin->w_cursor;
5126 startcol = (int)curs_col;
5127 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 }
5129
5130 /* Work out completion pattern and original text -- webb */
5131 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5132 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5135 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005136 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005138 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005140 compl_col += ++startcol;
5141 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 }
5143 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005144 compl_pattern = str_foldcase(line + compl_col,
5145 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005147 compl_pattern = vim_strnsave(line + compl_col,
5148 compl_length);
5149 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 return FAIL;
5151 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 {
5154 char_u *prefix = (char_u *)"\\<";
5155
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005156 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005158 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005161 if (!vim_iswordp(line + compl_col)
5162 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 && (
5164#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005165 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168#endif
5169 )))
5170 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 STRCPY((char *)compl_pattern, prefix);
5172 (void)quote_meta(compl_pattern + STRLEN(prefix),
5173 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180#endif
5181 )
5182 {
5183 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005184 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5185 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 compl_col += curs_col;
5188 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
5190 else
5191 {
5192#ifdef FEAT_MBYTE
5193 /* Search the point of change class of multibyte character
5194 * or not a word single byte character backward. */
5195 if (has_mbyte)
5196 {
5197 int base_class;
5198 int head_off;
5199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005200 startcol -= (*mb_head_off)(line, line + startcol);
5201 base_class = mb_get_class(line + startcol);
5202 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005204 head_off = (*mb_head_off)(line, line + startcol);
5205 if (base_class != mb_get_class(line + startcol
5206 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005208 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 }
5210 }
5211 else
5212#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005213 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005215 compl_col += ++startcol;
5216 compl_length = (int)curs_col - startcol;
5217 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
5219 /* Only match word with at least two chars -- webb
5220 * there's no need to call quote_meta,
5221 * alloc(7) is enough -- Acevedo
5222 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 compl_pattern = alloc(7);
5224 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005226 STRCPY((char *)compl_pattern, "\\<");
5227 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5228 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 }
5230 else
5231 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005232 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005233 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005234 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 STRCPY((char *)compl_pattern, "\\<");
5237 (void)quote_meta(compl_pattern + 2, line + compl_col,
5238 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
5240 }
5241 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005242 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005244 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005245 compl_length = (int)curs_col - (int)compl_col;
5246 if (compl_length < 0) /* cursor in indent: empty pattern */
5247 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 compl_pattern = str_foldcase(line + compl_col, compl_length,
5250 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005251 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005252 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5253 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 return FAIL;
5255 }
5256 else if (ctrl_x_mode == CTRL_X_FILES)
5257 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005258 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005259 if (startcol > 0)
5260 {
5261 char_u *p = line + startcol;
5262
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005263 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005264 while (p > line && vim_isfilec(PTR2CHAR(p)))
5265 mb_ptr_back(line, p);
5266 if (p == line && vim_isfilec(PTR2CHAR(p)))
5267 startcol = 0;
5268 else
5269 startcol = (int)(p - line) + 1;
5270 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005271
Bram Moolenaar0300e462013-09-08 16:03:45 +02005272 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005273 compl_length = (int)curs_col - startcol;
5274 compl_pattern = addstar(line + compl_col, compl_length,
5275 EXPAND_FILES);
5276 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 return FAIL;
5278 }
5279 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5280 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 compl_pattern = vim_strnsave(line, curs_col);
5282 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 set_cmd_context(&compl_xp, compl_pattern,
5285 (int)STRLEN(compl_pattern), curs_col);
5286 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5287 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005288 /* No completion possible, use an empty pattern to get a
5289 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005290 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005291 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005292 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5293 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005295 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005296 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005297#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005298 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005299 * Call user defined function 'completefunc' with "a:findstart"
5300 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005301 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005302 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005303 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005304 char_u *funcname;
5305 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005306 win_T *curwin_save;
5307 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005308
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005309 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005310 * string */
5311 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5312 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5313 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005314 {
5315 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5316 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005317 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005318 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005319
5320 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005321 args[1] = NULL;
5322 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005323 curwin_save = curwin;
5324 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005325 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005326 if (curwin_save != curwin || curbuf_save != curbuf)
5327 {
5328 EMSG(_(e_complwin));
5329 return FAIL;
5330 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005331 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005332 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005333 if (!equalpos(curwin->w_cursor, pos))
5334 {
5335 EMSG(_(e_compldel));
5336 return FAIL;
5337 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005338
Bram Moolenaar6110a002012-01-26 18:58:38 +01005339 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005340 * cancel the complete without an error.
5341 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005342 if (col == -2)
5343 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005344 if (col == -3)
5345 {
5346 ctrl_x_mode = 0;
5347 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005348 if (!shortmess(SHM_COMPLETIONMENU))
5349 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005350 return FAIL;
5351 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005352
Bram Moolenaar82139082011-09-14 16:52:09 +02005353 /*
5354 * Reset extended parameters of completion, when start new
5355 * completion.
5356 */
5357 compl_opt_refresh_always = FALSE;
5358
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005359 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005360 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005361 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005362 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005363 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005364
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 /* Setup variables for completion. Need to obtain "line" again,
5366 * it may have become invalid. */
5367 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005368 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005369 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5370 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005371#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 return FAIL;
5373 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005374 else if (ctrl_x_mode == CTRL_X_SPELL)
5375 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005376#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005377 if (spell_bad_len > 0)
5378 compl_col = curs_col - spell_bad_len;
5379 else
5380 compl_col = spell_word_start(startcol);
5381 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005382 {
5383 compl_length = 0;
5384 compl_col = curs_col;
5385 }
5386 else
5387 {
5388 spell_expand_check_cap(compl_col);
5389 compl_length = (int)curs_col - compl_col;
5390 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005391 /* Need to obtain "line" again, it may have become invalid. */
5392 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005393 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5394 if (compl_pattern == NULL)
5395#endif
5396 return FAIL;
5397 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005398 else
5399 {
5400 EMSG2(_(e_intern2), "ins_complete()");
5401 return FAIL;
5402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005403
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005404 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 {
5406 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005407 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 {
5409 /* Insert a new line, keep indentation but ignore 'comments' */
5410#ifdef FEAT_COMMENTS
5411 char_u *old = curbuf->b_p_com;
5412
5413 curbuf->b_p_com = (char_u *)"";
5414#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005415 compl_startpos.lnum = curwin->w_cursor.lnum;
5416 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 ins_eol('\r');
5418#ifdef FEAT_COMMENTS
5419 curbuf->b_p_com = old;
5420#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 compl_length = 0;
5422 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 }
5424 }
5425 else
5426 {
5427 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005428 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 }
5430
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005431 if (compl_cont_status & CONT_LOCAL)
5432 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 else
5434 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5435
Bram Moolenaar62951b12011-09-21 18:23:05 +02005436 /* If any of the original typed text has been changed we need to fix
5437 * the redo buffer. */
5438 ins_compl_fixRedoBufForLeader(NULL);
5439
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005440 /* Always add completion for the original text. */
5441 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005442 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5443 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005444 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005446 vim_free(compl_pattern);
5447 compl_pattern = NULL;
5448 vim_free(compl_orig_text);
5449 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 return FAIL;
5451 }
5452
5453 /* showmode might reset the internal line pointers, so it must
5454 * be called before line = ml_get(), or when this address is no
5455 * longer needed. -- Acevedo.
5456 */
5457 edit_submode_extra = (char_u *)_("-- Searching...");
5458 edit_submode_highl = HLF_COUNT;
5459 showmode();
5460 edit_submode_extra = NULL;
5461 out_flush();
5462 }
5463
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005464 compl_shown_match = compl_curr_match;
5465 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466
5467 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005468 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005470 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005471 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005472
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005473 /* may undisplay the popup menu */
5474 ins_compl_upd_pum();
5475
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005476 if (n > 1) /* all matches have been found */
5477 compl_matches = n;
5478 compl_curr_match = compl_shown_match;
5479 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
Bram Moolenaard68071d2006-05-02 22:08:30 +00005481 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5482 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 if (got_int && !global_busy)
5484 {
5485 (void)vgetc();
5486 got_int = FALSE;
5487 }
5488
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005489 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005490 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005491 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005492 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5493 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5495 edit_submode_highl = HLF_E;
5496 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5497 * because we couldn't expand anything at first place, but if we used
5498 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5499 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005500 if ( compl_length > 1
5501 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 || (ctrl_x_mode != 0
5503 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5504 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005505 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 }
5507
Bram Moolenaar572cb562005-08-05 21:35:02 +00005508 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005509 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005511 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
5513 if (edit_submode_extra == NULL)
5514 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005515 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 {
5517 edit_submode_extra = (char_u *)_("Back at original");
5518 edit_submode_highl = HLF_W;
5519 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005520 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 {
5522 edit_submode_extra = (char_u *)_("Word from other line");
5523 edit_submode_highl = HLF_COUNT;
5524 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005525 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
5527 edit_submode_extra = (char_u *)_("The only match");
5528 edit_submode_highl = HLF_COUNT;
5529 }
5530 else
5531 {
5532 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005533 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005535 int number = 0;
5536 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005538 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
5540 /* search backwards for the first valid (!= -1) number.
5541 * This should normally succeed already at the first loop
5542 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005543 for (match = compl_curr_match->cp_prev; match != NULL
5544 && match != compl_first_match;
5545 match = match->cp_prev)
5546 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005548 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 break;
5550 }
5551 if (match != NULL)
5552 /* go up and assign all numbers which are not assigned
5553 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005554 for (match = match->cp_next;
5555 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005556 match = match->cp_next)
5557 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
5559 else /* BACKWARD */
5560 {
5561 /* search forwards (upwards) for the first valid (!= -1)
5562 * number. This should normally succeed already at the
5563 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005564 for (match = compl_curr_match->cp_next; match != NULL
5565 && match != compl_first_match;
5566 match = match->cp_next)
5567 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005569 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 break;
5571 }
5572 if (match != NULL)
5573 /* go down and assign all numbers which are not
5574 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005575 for (match = match->cp_prev; match
5576 && match->cp_number == -1;
5577 match = match->cp_prev)
5578 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 }
5580 }
5581
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005582 /* The match should always have a sequence number now, this is
5583 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005584 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005586 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5587 * Translations may need more than twice that. */
5588 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005590 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005591 vim_snprintf((char *)match_ref, sizeof(match_ref),
5592 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005593 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005595 vim_snprintf((char *)match_ref, sizeof(match_ref),
5596 _("match %d"),
5597 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 edit_submode_extra = match_ref;
5599 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005600 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 curs_columns(FALSE);
5602 }
5603 }
5604 }
5605
5606 /* Show a message about what (completion) mode we're in. */
5607 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005608 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005610 if (edit_submode_extra != NULL)
5611 {
5612 if (!p_smd)
5613 msg_attr(edit_submode_extra,
5614 edit_submode_highl < HLF_COUNT
5615 ? hl_attr(edit_submode_highl) : 0);
5616 }
5617 else
5618 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620
Bram Moolenaard68071d2006-05-02 22:08:30 +00005621 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005622 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005623 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005624 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005625 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005626 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005627 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005628
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 return OK;
5630}
5631
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005632 static void
5633show_pum(int save_w_wrow)
5634{
5635 /* RedrawingDisabled may be set when invoked through complete(). */
5636 int n = RedrawingDisabled;
5637
5638 RedrawingDisabled = 0;
5639
5640 /* If the cursor moved we need to remove the pum first. */
5641 setcursor();
5642 if (save_w_wrow != curwin->w_wrow)
5643 ins_compl_del_pum();
5644
5645 ins_compl_show_pum();
5646 setcursor();
5647 RedrawingDisabled = n;
5648}
5649
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650/*
5651 * Looks in the first "len" chars. of "src" for search-metachars.
5652 * If dest is not NULL the chars. are copied there quoting (with
5653 * a backslash) the metachars, and dest would be NUL terminated.
5654 * Returns the length (needed) of dest
5655 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005656 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005657quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005659 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005661 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 {
5663 switch (*src)
5664 {
5665 case '.':
5666 case '*':
5667 case '[':
5668 if (ctrl_x_mode == CTRL_X_DICTIONARY
5669 || ctrl_x_mode == CTRL_X_THESAURUS)
5670 break;
5671 case '~':
5672 if (!p_magic) /* quote these only if magic is set */
5673 break;
5674 case '\\':
5675 if (ctrl_x_mode == CTRL_X_DICTIONARY
5676 || ctrl_x_mode == CTRL_X_THESAURUS)
5677 break;
5678 case '^': /* currently it's not needed. */
5679 case '$':
5680 m++;
5681 if (dest != NULL)
5682 *dest++ = '\\';
5683 break;
5684 }
5685 if (dest != NULL)
5686 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005687# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 /* Copy remaining bytes of a multibyte character. */
5689 if (has_mbyte)
5690 {
5691 int i, mb_len;
5692
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005693 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 if (mb_len > 0 && len >= mb_len)
5695 for (i = 0; i < mb_len; ++i)
5696 {
5697 --len;
5698 ++src;
5699 if (dest != NULL)
5700 *dest++ = *src;
5701 }
5702 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005703# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 }
5705 if (dest != NULL)
5706 *dest = NUL;
5707
5708 return m;
5709}
5710#endif /* FEAT_INS_EXPAND */
5711
5712/*
5713 * Next character is interpreted literally.
5714 * A one, two or three digit decimal number is interpreted as its byte value.
5715 * If one or two digits are entered, the next character is given to vungetc().
5716 * For Unicode a character > 255 may be returned.
5717 */
5718 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005719get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
5721 int cc;
5722 int nc;
5723 int i;
5724 int hex = FALSE;
5725 int octal = FALSE;
5726#ifdef FEAT_MBYTE
5727 int unicode = 0;
5728#endif
5729
5730 if (got_int)
5731 return Ctrl_C;
5732
5733#ifdef FEAT_GUI
5734 /*
5735 * In GUI there is no point inserting the internal code for a special key.
5736 * It is more useful to insert the string "<KEY>" instead. This would
5737 * probably be useful in a text window too, but it would not be
5738 * vi-compatible (maybe there should be an option for it?) -- webb
5739 */
5740 if (gui.in_use)
5741 ++allow_keys;
5742#endif
5743#ifdef USE_ON_FLY_SCROLL
5744 dont_scroll = TRUE; /* disallow scrolling here */
5745#endif
5746 ++no_mapping; /* don't map the next key hits */
5747 cc = 0;
5748 i = 0;
5749 for (;;)
5750 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005751 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005752#ifdef FEAT_CMDL_INFO
5753 if (!(State & CMDLINE)
5754# ifdef FEAT_MBYTE
5755 && MB_BYTE2LEN_CHECK(nc) == 1
5756# endif
5757 )
5758 add_to_showcmd(nc);
5759#endif
5760 if (nc == 'x' || nc == 'X')
5761 hex = TRUE;
5762 else if (nc == 'o' || nc == 'O')
5763 octal = TRUE;
5764#ifdef FEAT_MBYTE
5765 else if (nc == 'u' || nc == 'U')
5766 unicode = nc;
5767#endif
5768 else
5769 {
5770 if (hex
5771#ifdef FEAT_MBYTE
5772 || unicode != 0
5773#endif
5774 )
5775 {
5776 if (!vim_isxdigit(nc))
5777 break;
5778 cc = cc * 16 + hex2nr(nc);
5779 }
5780 else if (octal)
5781 {
5782 if (nc < '0' || nc > '7')
5783 break;
5784 cc = cc * 8 + nc - '0';
5785 }
5786 else
5787 {
5788 if (!VIM_ISDIGIT(nc))
5789 break;
5790 cc = cc * 10 + nc - '0';
5791 }
5792
5793 ++i;
5794 }
5795
5796 if (cc > 255
5797#ifdef FEAT_MBYTE
5798 && unicode == 0
5799#endif
5800 )
5801 cc = 255; /* limit range to 0-255 */
5802 nc = 0;
5803
5804 if (hex) /* hex: up to two chars */
5805 {
5806 if (i >= 2)
5807 break;
5808 }
5809#ifdef FEAT_MBYTE
5810 else if (unicode) /* Unicode: up to four or eight chars */
5811 {
5812 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5813 break;
5814 }
5815#endif
5816 else if (i >= 3) /* decimal or octal: up to three chars */
5817 break;
5818 }
5819 if (i == 0) /* no number entered */
5820 {
5821 if (nc == K_ZERO) /* NUL is stored as NL */
5822 {
5823 cc = '\n';
5824 nc = 0;
5825 }
5826 else
5827 {
5828 cc = nc;
5829 nc = 0;
5830 }
5831 }
5832
5833 if (cc == 0) /* NUL is stored as NL */
5834 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005835#ifdef FEAT_MBYTE
5836 if (enc_dbcs && (cc & 0xff) == 0)
5837 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5838 second byte will cause trouble! */
5839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
5841 --no_mapping;
5842#ifdef FEAT_GUI
5843 if (gui.in_use)
5844 --allow_keys;
5845#endif
5846 if (nc)
5847 vungetc(nc);
5848 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5849 return cc;
5850}
5851
5852/*
5853 * Insert character, taking care of special keys and mod_mask
5854 */
5855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005856insert_special(
5857 int c,
5858 int allow_modmask,
5859 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860{
5861 char_u *p;
5862 int len;
5863
5864 /*
5865 * Special function key, translate into "<Key>". Up to the last '>' is
5866 * inserted with ins_str(), so as not to replace characters in replace
5867 * mode.
5868 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5869 * unless 'allow_modmask' is TRUE.
5870 */
5871#ifdef MACOS
5872 /* Command-key never produces a normal key */
5873 if (mod_mask & MOD_MASK_CMD)
5874 allow_modmask = TRUE;
5875#endif
5876 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5877 {
5878 p = get_special_key_name(c, mod_mask);
5879 len = (int)STRLEN(p);
5880 c = p[len - 1];
5881 if (len > 2)
5882 {
5883 if (stop_arrow() == FAIL)
5884 return;
5885 p[len - 1] = NUL;
5886 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005887 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005888 ctrlv = FALSE;
5889 }
5890 }
5891 if (stop_arrow() == OK)
5892 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5893}
5894
5895/*
5896 * Special characters in this context are those that need processing other
5897 * than the simple insertion that can be performed here. This includes ESC
5898 * which terminates the insert, and CR/NL which need special processing to
5899 * open up a new line. This routine tries to optimize insertions performed by
5900 * the "redo", "undo" or "put" commands, so it needs to know when it should
5901 * stop and defer processing to the "normal" mechanism.
5902 * '0' and '^' are special, because they can be followed by CTRL-D.
5903 */
5904#ifdef EBCDIC
5905# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5906#else
5907# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5908#endif
5909
5910#ifdef FEAT_MBYTE
5911# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5912#else
5913# define WHITECHAR(cc) vim_iswhite(cc)
5914#endif
5915
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005916/*
5917 * "flags": INSCHAR_FORMAT - force formatting
5918 * INSCHAR_CTRLV - char typed just after CTRL-V
5919 * INSCHAR_NO_FEX - don't use 'formatexpr'
5920 *
5921 * NOTE: passes the flags value straight through to internal_format() which,
5922 * beside INSCHAR_FORMAT (above), is also looking for these:
5923 * INSCHAR_DO_COM - format comments
5924 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5925 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005926 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005927insertchar(
5928 int c, /* character to insert or NUL */
5929 int flags, /* INSCHAR_FORMAT, etc. */
5930 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005931{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 int textwidth;
5933#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005937 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005939 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941
5942 /*
5943 * Try to break the line in two or more pieces when:
5944 * - Always do this if we have been called to do formatting only.
5945 * - Always do this when 'formatoptions' has the 'a' flag and the line
5946 * ends in white space.
5947 * - Otherwise:
5948 * - Don't do this if inserting a blank
5949 * - Don't do this if an existing character is being replaced, unless
5950 * we're in VREPLACE mode.
5951 * - Do this if the cursor is not on the line where insert started
5952 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5953 * before the insert.
5954 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5955 * before 'textwidth'
5956 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005957 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005958 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005959 || (!vim_iswhite(c)
5960 && !((State & REPLACE_FLAG)
5961#ifdef FEAT_VREPLACE
5962 && !(State & VREPLACE_FLAG)
5963#endif
5964 && *ml_get_cursor() != NUL)
5965 && (curwin->w_cursor.lnum != Insstart.lnum
5966 || ((!has_format_option(FO_INS_LONG)
5967 || Insstart_textlen <= (colnr_T)textwidth)
5968 && (!fo_ins_blank
5969 || Insstart_blank_vcol <= (colnr_T)textwidth
5970 ))))))
5971 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005972 /* Format with 'formatexpr' when it's set. Use internal formatting
5973 * when 'formatexpr' isn't set or it returns non-zero. */
5974#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005975 int do_internal = TRUE;
5976 colnr_T virtcol = get_nolist_virtcol()
5977 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005978
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005979 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
5980 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005981 {
5982 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5983 /* It may be required to save for undo again, e.g. when setline()
5984 * was called. */
5985 ins_need_undo = TRUE;
5986 }
5987 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005989 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005991
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992 if (c == NUL) /* only formatting was wanted */
5993 return;
5994
5995#ifdef FEAT_COMMENTS
5996 /* Check whether this character should end a comment. */
5997 if (did_ai && (int)c == end_comment_pending)
5998 {
5999 char_u *line;
6000 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6001 int middle_len, end_len;
6002 int i;
6003
6004 /*
6005 * Need to remove existing (middle) comment leader and insert end
6006 * comment leader. First, check what comment leader we can find.
6007 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006008 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006009 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6010 {
6011 /* Skip middle-comment string */
6012 while (*p && p[-1] != ':') /* find end of middle flags */
6013 ++p;
6014 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6015 /* Don't count trailing white space for middle_len */
6016 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6017 --middle_len;
6018
6019 /* Find the end-comment string */
6020 while (*p && p[-1] != ':') /* find end of end flags */
6021 ++p;
6022 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6023
6024 /* Skip white space before the cursor */
6025 i = curwin->w_cursor.col;
6026 while (--i >= 0 && vim_iswhite(line[i]))
6027 ;
6028 i++;
6029
6030 /* Skip to before the middle leader */
6031 i -= middle_len;
6032
6033 /* Check some expected things before we go on */
6034 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6035 {
6036 /* Backspace over all the stuff we want to replace */
6037 backspace_until_column(i);
6038
6039 /*
6040 * Insert the end-comment string, except for the last
6041 * character, which will get inserted as normal later.
6042 */
6043 ins_bytes_len(lead_end, end_len - 1);
6044 }
6045 }
6046 }
6047 end_comment_pending = NUL;
6048#endif
6049
6050 did_ai = FALSE;
6051#ifdef FEAT_SMARTINDENT
6052 did_si = FALSE;
6053 can_si = FALSE;
6054 can_si_back = FALSE;
6055#endif
6056
6057 /*
6058 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6059 * This speeds up normal text input considerably.
6060 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6061 * need to re-indent at a ':', or any other character (but not what
6062 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006063 * Don't do this when there an InsertCharPre autocommand is defined,
6064 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006065 */
6066#ifdef USE_ON_FLY_SCROLL
6067 dont_scroll = FALSE; /* allow scrolling here */
6068#endif
6069
6070 if ( !ISSPECIAL(c)
6071#ifdef FEAT_MBYTE
6072 && (!has_mbyte || (*mb_char2len)(c) == 1)
6073#endif
6074 && vpeekc() != NUL
6075 && !(State & REPLACE_FLAG)
6076#ifdef FEAT_CINDENT
6077 && !cindent_on()
6078#endif
6079#ifdef FEAT_RIGHTLEFT
6080 && !p_ri
6081#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006082#ifdef FEAT_AUTOCMD
6083 && !has_insertcharpre()
6084#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006085 )
6086 {
6087#define INPUT_BUFLEN 100
6088 char_u buf[INPUT_BUFLEN + 1];
6089 int i;
6090 colnr_T virtcol = 0;
6091
6092 buf[0] = c;
6093 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006094 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006095 virtcol = get_nolist_virtcol();
6096 /*
6097 * Stop the string when:
6098 * - no more chars available
6099 * - finding a special character (command key)
6100 * - buffer is full
6101 * - running into the 'textwidth' boundary
6102 * - need to check for abbreviation: A non-word char after a word-char
6103 */
6104 while ( (c = vpeekc()) != NUL
6105 && !ISSPECIAL(c)
6106#ifdef FEAT_MBYTE
6107 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6108#endif
6109 && i < INPUT_BUFLEN
6110 && (textwidth == 0
6111 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6112 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6113 {
6114#ifdef FEAT_RIGHTLEFT
6115 c = vgetc();
6116 if (p_hkmap && KeyTyped)
6117 c = hkmap(c); /* Hebrew mode mapping */
6118# ifdef FEAT_FKMAP
6119 if (p_fkmap && KeyTyped)
6120 c = fkmap(c); /* Farsi mode mapping */
6121# endif
6122 buf[i++] = c;
6123#else
6124 buf[i++] = vgetc();
6125#endif
6126 }
6127
6128#ifdef FEAT_DIGRAPHS
6129 do_digraph(-1); /* clear digraphs */
6130 do_digraph(buf[i-1]); /* may be the start of a digraph */
6131#endif
6132 buf[i] = NUL;
6133 ins_str(buf);
6134 if (flags & INSCHAR_CTRLV)
6135 {
6136 redo_literal(*buf);
6137 i = 1;
6138 }
6139 else
6140 i = 0;
6141 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006142 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006143 }
6144 else
6145 {
6146#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006147 int cc;
6148
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6150 {
6151 char_u buf[MB_MAXBYTES + 1];
6152
6153 (*mb_char2bytes)(c, buf);
6154 buf[cc] = NUL;
6155 ins_char_bytes(buf, cc);
6156 AppendCharToRedobuff(c);
6157 }
6158 else
6159#endif
6160 {
6161 ins_char(c);
6162 if (flags & INSCHAR_CTRLV)
6163 redo_literal(c);
6164 else
6165 AppendCharToRedobuff(c);
6166 }
6167 }
6168}
6169
6170/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006171 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006172 *
6173 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6174 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175 */
6176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006177internal_format(
6178 int textwidth,
6179 int second_indent,
6180 int flags,
6181 int format_only,
6182 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006183{
6184 int cc;
6185 int save_char = NUL;
6186 int haveto_redraw = FALSE;
6187 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6188#ifdef FEAT_MBYTE
6189 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6190#endif
6191 int fo_white_par = has_format_option(FO_WHITE_PAR);
6192 int first_line = TRUE;
6193#ifdef FEAT_COMMENTS
6194 colnr_T leader_len;
6195 int no_leader = FALSE;
6196 int do_comments = (flags & INSCHAR_DO_COM);
6197#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006198#ifdef FEAT_LINEBREAK
6199 int has_lbr = curwin->w_p_lbr;
6200
6201 /* make sure win_lbr_chartabsize() counts correctly */
6202 curwin->w_p_lbr = FALSE;
6203#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006204
6205 /*
6206 * When 'ai' is off we don't want a space under the cursor to be
6207 * deleted. Replace it with an 'x' temporarily.
6208 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006209 if (!curbuf->b_p_ai
6210#ifdef FEAT_VREPLACE
6211 && !(State & VREPLACE_FLAG)
6212#endif
6213 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006214 {
6215 cc = gchar_cursor();
6216 if (vim_iswhite(cc))
6217 {
6218 save_char = cc;
6219 pchar_cursor('x');
6220 }
6221 }
6222
6223 /*
6224 * Repeat breaking lines, until the current line is not too long.
6225 */
6226 while (!got_int)
6227 {
6228 int startcol; /* Cursor column at entry */
6229 int wantcol; /* column at textwidth border */
6230 int foundcol; /* column for start of spaces */
6231 int end_foundcol = 0; /* column for start of word */
6232 colnr_T len;
6233 colnr_T virtcol;
6234#ifdef FEAT_VREPLACE
6235 int orig_col = 0;
6236 char_u *saved_text = NULL;
6237#endif
6238 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006239 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006240
Bram Moolenaar97b98102009-11-17 16:41:01 +00006241 virtcol = get_nolist_virtcol()
6242 + char2cells(c != NUL ? c : gchar_cursor());
6243 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006244 break;
6245
6246#ifdef FEAT_COMMENTS
6247 if (no_leader)
6248 do_comments = FALSE;
6249 else if (!(flags & INSCHAR_FORMAT)
6250 && has_format_option(FO_WRAP_COMS))
6251 do_comments = TRUE;
6252
6253 /* Don't break until after the comment leader */
6254 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006255 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006256 else
6257 leader_len = 0;
6258
6259 /* If the line doesn't start with a comment leader, then don't
6260 * start one in a following broken line. Avoids that a %word
6261 * moved to the start of the next line causes all following lines
6262 * to start with %. */
6263 if (leader_len == 0)
6264 no_leader = TRUE;
6265#endif
6266 if (!(flags & INSCHAR_FORMAT)
6267#ifdef FEAT_COMMENTS
6268 && leader_len == 0
6269#endif
6270 && !has_format_option(FO_WRAP))
6271
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006272 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006273 if ((startcol = curwin->w_cursor.col) == 0)
6274 break;
6275
6276 /* find column of textwidth border */
6277 coladvance((colnr_T)textwidth);
6278 wantcol = curwin->w_cursor.col;
6279
Bram Moolenaar97b98102009-11-17 16:41:01 +00006280 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006281 foundcol = 0;
6282
6283 /*
6284 * Find position to break at.
6285 * Stop at first entered white when 'formatoptions' has 'v'
6286 */
6287 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006288 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006289 || curwin->w_cursor.lnum != Insstart.lnum
6290 || curwin->w_cursor.col >= Insstart.col)
6291 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006292 if (curwin->w_cursor.col == startcol && c != NUL)
6293 cc = c;
6294 else
6295 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006296 if (WHITECHAR(cc))
6297 {
6298 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006299 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006300
6301 /* find start of sequence of blanks */
6302 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6303 {
6304 dec_cursor();
6305 cc = gchar_cursor();
6306 }
6307 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6308 break; /* only spaces in front of text */
6309#ifdef FEAT_COMMENTS
6310 /* Don't break until after the comment leader */
6311 if (curwin->w_cursor.col < leader_len)
6312 break;
6313#endif
6314 if (has_format_option(FO_ONE_LETTER))
6315 {
6316 /* do not break after one-letter words */
6317 if (curwin->w_cursor.col == 0)
6318 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006319#ifdef FEAT_COMMENTS
6320 /* do not break "#a b" when 'tw' is 2 */
6321 if (curwin->w_cursor.col <= leader_len)
6322 break;
6323#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006324 col = curwin->w_cursor.col;
6325 dec_cursor();
6326 cc = gchar_cursor();
6327
6328 if (WHITECHAR(cc))
6329 continue; /* one-letter, continue */
6330 curwin->w_cursor.col = col;
6331 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006332
6333 inc_cursor();
6334
6335 end_foundcol = end_col + 1;
6336 foundcol = curwin->w_cursor.col;
6337 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006338 break;
6339 }
6340#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006341 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006342 {
6343 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006344 if (curwin->w_cursor.col != startcol)
6345 {
6346#ifdef FEAT_COMMENTS
6347 /* Don't break until after the comment leader */
6348 if (curwin->w_cursor.col < leader_len)
6349 break;
6350#endif
6351 col = curwin->w_cursor.col;
6352 inc_cursor();
6353 /* Don't change end_foundcol if already set. */
6354 if (foundcol != curwin->w_cursor.col)
6355 {
6356 foundcol = curwin->w_cursor.col;
6357 end_foundcol = foundcol;
6358 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6359 break;
6360 }
6361 curwin->w_cursor.col = col;
6362 }
6363
6364 if (curwin->w_cursor.col == 0)
6365 break;
6366
6367 col = curwin->w_cursor.col;
6368
6369 dec_cursor();
6370 cc = gchar_cursor();
6371
6372 if (WHITECHAR(cc))
6373 continue; /* break with space */
6374#ifdef FEAT_COMMENTS
6375 /* Don't break until after the comment leader */
6376 if (curwin->w_cursor.col < leader_len)
6377 break;
6378#endif
6379
6380 curwin->w_cursor.col = col;
6381
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006382 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006383 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006384 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6385 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006386 }
6387#endif
6388 if (curwin->w_cursor.col == 0)
6389 break;
6390 dec_cursor();
6391 }
6392
6393 if (foundcol == 0) /* no spaces, cannot break line */
6394 {
6395 curwin->w_cursor.col = startcol;
6396 break;
6397 }
6398
6399 /* Going to break the line, remove any "$" now. */
6400 undisplay_dollar();
6401
6402 /*
6403 * Offset between cursor position and line break is used by replace
6404 * stack functions. VREPLACE does not use this, and backspaces
6405 * over the text instead.
6406 */
6407#ifdef FEAT_VREPLACE
6408 if (State & VREPLACE_FLAG)
6409 orig_col = startcol; /* Will start backspacing from here */
6410 else
6411#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006412 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006413
6414 /*
6415 * adjust startcol for spaces that will be deleted and
6416 * characters that will remain on top line
6417 */
6418 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006419 while ((cc = gchar_cursor(), WHITECHAR(cc))
6420 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006421 inc_cursor();
6422 startcol -= curwin->w_cursor.col;
6423 if (startcol < 0)
6424 startcol = 0;
6425
6426#ifdef FEAT_VREPLACE
6427 if (State & VREPLACE_FLAG)
6428 {
6429 /*
6430 * In VREPLACE mode, we will backspace over the text to be
6431 * wrapped, so save a copy now to put on the next line.
6432 */
6433 saved_text = vim_strsave(ml_get_cursor());
6434 curwin->w_cursor.col = orig_col;
6435 if (saved_text == NULL)
6436 break; /* Can't do it, out of memory */
6437 saved_text[startcol] = NUL;
6438
6439 /* Backspace over characters that will move to the next line */
6440 if (!fo_white_par)
6441 backspace_until_column(foundcol);
6442 }
6443 else
6444#endif
6445 {
6446 /* put cursor after pos. to break line */
6447 if (!fo_white_par)
6448 curwin->w_cursor.col = foundcol;
6449 }
6450
6451 /*
6452 * Split the line just before the margin.
6453 * Only insert/delete lines, but don't really redraw the window.
6454 */
6455 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6456 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6457#ifdef FEAT_COMMENTS
6458 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006459 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006460#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006461 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6462 if (!(flags & INSCHAR_COM_LIST))
6463 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006464
6465 replace_offset = 0;
6466 if (first_line)
6467 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006468 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006469 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006470 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006471 * This section is for auto-wrap of numeric lists. When not
6472 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6473 * flag will be set and open_line() will handle it (as seen
6474 * above). The code here (and in get_number_indent()) will
6475 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006476 */
6477 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006478 second_indent =
6479 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006480 if (second_indent >= 0)
6481 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006482#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006483 if (State & VREPLACE_FLAG)
6484 change_indent(INDENT_SET, second_indent,
6485 FALSE, NUL, TRUE);
6486 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006487#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006488#ifdef FEAT_COMMENTS
6489 if (leader_len > 0 && second_indent - leader_len > 0)
6490 {
6491 int i;
6492 int padding = second_indent - leader_len;
6493
6494 /* We started at the first_line of a numbered list
6495 * that has a comment. the open_line() function has
6496 * inserted the proper comment leader and positioned
6497 * the cursor at the end of the split line. Now we
6498 * add the additional whitespace needed after the
6499 * comment leader for the numbered list. */
6500 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006501 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006502 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006503 }
6504 else
6505 {
6506#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006507 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006508#ifdef FEAT_COMMENTS
6509 }
6510#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006511 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006512 }
6513 first_line = FALSE;
6514 }
6515
6516#ifdef FEAT_VREPLACE
6517 if (State & VREPLACE_FLAG)
6518 {
6519 /*
6520 * In VREPLACE mode we have backspaced over the text to be
6521 * moved, now we re-insert it into the new line.
6522 */
6523 ins_bytes(saved_text);
6524 vim_free(saved_text);
6525 }
6526 else
6527#endif
6528 {
6529 /*
6530 * Check if cursor is not past the NUL off the line, cindent
6531 * may have added or removed indent.
6532 */
6533 curwin->w_cursor.col += startcol;
6534 len = (colnr_T)STRLEN(ml_get_curline());
6535 if (curwin->w_cursor.col > len)
6536 curwin->w_cursor.col = len;
6537 }
6538
6539 haveto_redraw = TRUE;
6540#ifdef FEAT_CINDENT
6541 can_cindent = TRUE;
6542#endif
6543 /* moved the cursor, don't autoindent or cindent now */
6544 did_ai = FALSE;
6545#ifdef FEAT_SMARTINDENT
6546 did_si = FALSE;
6547 can_si = FALSE;
6548 can_si_back = FALSE;
6549#endif
6550 line_breakcheck();
6551 }
6552
6553 if (save_char != NUL) /* put back space after cursor */
6554 pchar_cursor(save_char);
6555
Bram Moolenaar0026d472014-09-09 16:32:39 +02006556#ifdef FEAT_LINEBREAK
6557 curwin->w_p_lbr = has_lbr;
6558#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006559 if (!format_only && haveto_redraw)
6560 {
6561 update_topline();
6562 redraw_curbuf_later(VALID);
6563 }
6564}
6565
6566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 * Called after inserting or deleting text: When 'formatoptions' includes the
6568 * 'a' flag format from the current line until the end of the paragraph.
6569 * Keep the cursor at the same position relative to the text.
6570 * The caller must have saved the cursor line for undo, following ones will be
6571 * saved here.
6572 */
6573 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006574auto_format(
6575 int trailblank, /* when TRUE also format with trailing blank */
6576 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577{
6578 pos_T pos;
6579 colnr_T len;
6580 char_u *old;
6581 char_u *new, *pnew;
6582 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006583 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006584
6585 if (!has_format_option(FO_AUTO))
6586 return;
6587
6588 pos = curwin->w_cursor;
6589 old = ml_get_curline();
6590
6591 /* may remove added space */
6592 check_auto_format(FALSE);
6593
6594 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6595 * user might insert normal text next. Also skip formatting when "1" is
6596 * in 'formatoptions' and there is a single character before the cursor.
6597 * Otherwise the line would be broken and when typing another non-white
6598 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006599 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006600 if (*old != NUL && !trailblank && wasatend)
6601 {
6602 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006603 cc = gchar_cursor();
6604 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6605 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006607 cc = gchar_cursor();
6608 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 {
6610 curwin->w_cursor = pos;
6611 return;
6612 }
6613 curwin->w_cursor = pos;
6614 }
6615
6616#ifdef FEAT_COMMENTS
6617 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6618 * comments. */
6619 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006620 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 return;
6622#endif
6623
6624 /*
6625 * May start formatting in a previous line, so that after "x" a word is
6626 * moved to the previous line if it fits there now. Only when this is not
6627 * the start of a paragraph.
6628 */
6629 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6630 {
6631 --curwin->w_cursor.lnum;
6632 if (u_save_cursor() == FAIL)
6633 return;
6634 }
6635
6636 /*
6637 * Do the formatting and restore the cursor position. "saved_cursor" will
6638 * be adjusted for the text formatting.
6639 */
6640 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006641 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 curwin->w_cursor = saved_cursor;
6643 saved_cursor.lnum = 0;
6644
6645 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6646 {
6647 /* "cannot happen" */
6648 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6649 coladvance((colnr_T)MAXCOL);
6650 }
6651 else
6652 check_cursor_col();
6653
6654 /* Insert mode: If the cursor is now after the end of the line while it
6655 * previously wasn't, the line was broken. Because of the rule above we
6656 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6657 * formatted. */
6658 if (!wasatend && has_format_option(FO_WHITE_PAR))
6659 {
6660 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006661 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006662 if (curwin->w_cursor.col == len)
6663 {
6664 pnew = vim_strnsave(new, len + 2);
6665 pnew[len] = ' ';
6666 pnew[len + 1] = NUL;
6667 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6668 /* remove the space later */
6669 did_add_space = TRUE;
6670 }
6671 else
6672 /* may remove added space */
6673 check_auto_format(FALSE);
6674 }
6675
6676 check_cursor();
6677}
6678
6679/*
6680 * When an extra space was added to continue a paragraph for auto-formatting,
6681 * delete it now. The space must be under the cursor, just after the insert
6682 * position.
6683 */
6684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006685check_auto_format(
6686 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687{
6688 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006689 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006690
6691 if (did_add_space)
6692 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006693 cc = gchar_cursor();
6694 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006695 /* Somehow the space was removed already. */
6696 did_add_space = FALSE;
6697 else
6698 {
6699 if (!end_insert)
6700 {
6701 inc_cursor();
6702 c = gchar_cursor();
6703 dec_cursor();
6704 }
6705 if (c != NUL)
6706 {
6707 /* The space is no longer at the end of the line, delete it. */
6708 del_char(FALSE);
6709 did_add_space = FALSE;
6710 }
6711 }
6712 }
6713}
6714
6715/*
6716 * Find out textwidth to be used for formatting:
6717 * if 'textwidth' option is set, use it
6718 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6719 * if invalid value, use 0.
6720 * Set default to window width (maximum 79) for "gq" operator.
6721 */
6722 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006723comp_textwidth(
6724 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725{
6726 int textwidth;
6727
6728 textwidth = curbuf->b_p_tw;
6729 if (textwidth == 0 && curbuf->b_p_wm)
6730 {
6731 /* The width is the window width minus 'wrapmargin' minus all the
6732 * things that add to the margin. */
6733 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6734#ifdef FEAT_CMDWIN
6735 if (cmdwin_type != 0)
6736 textwidth -= 1;
6737#endif
6738#ifdef FEAT_FOLDING
6739 textwidth -= curwin->w_p_fdc;
6740#endif
6741#ifdef FEAT_SIGNS
6742 if (curwin->w_buffer->b_signlist != NULL
6743# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01006744 || curwin->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745# endif
6746 )
6747 textwidth -= 1;
6748#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006749 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 textwidth -= 8;
6751 }
6752 if (textwidth < 0)
6753 textwidth = 0;
6754 if (ff && textwidth == 0)
6755 {
6756 textwidth = W_WIDTH(curwin) - 1;
6757 if (textwidth > 79)
6758 textwidth = 79;
6759 }
6760 return textwidth;
6761}
6762
6763/*
6764 * Put a character in the redo buffer, for when just after a CTRL-V.
6765 */
6766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006767redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768{
6769 char_u buf[10];
6770
6771 /* Only digits need special treatment. Translate them into a string of
6772 * three digits. */
6773 if (VIM_ISDIGIT(c))
6774 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006775 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006776 AppendToRedobuff(buf);
6777 }
6778 else
6779 AppendCharToRedobuff(c);
6780}
6781
6782/*
6783 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006784 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 */
6786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006787start_arrow(
6788 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006790 start_arrow_common(end_insert_pos, TRUE);
6791}
6792
6793/*
6794 * Like start_arrow() but with end_change argument.
6795 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6796 */
6797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006798start_arrow_with_change(
6799 pos_T *end_insert_pos, /* can be NULL */
6800 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006801{
6802 start_arrow_common(end_insert_pos, end_change);
6803 if (!end_change)
6804 {
6805 AppendCharToRedobuff(Ctrl_G);
6806 AppendCharToRedobuff('U');
6807 }
6808}
6809
6810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006811start_arrow_common(
6812 pos_T *end_insert_pos, /* can be NULL */
6813 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006814{
6815 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006816 {
6817 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006818 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006819 arrow_used = TRUE; /* this means we stopped the current insert */
6820 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006821#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006822 check_spell_redraw();
6823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824}
6825
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006826#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006827/*
6828 * If we skipped highlighting word at cursor, do it now.
6829 * It may be skipped again, thus reset spell_redraw_lnum first.
6830 */
6831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006832check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006833{
6834 if (spell_redraw_lnum != 0)
6835 {
6836 linenr_T lnum = spell_redraw_lnum;
6837
6838 spell_redraw_lnum = 0;
6839 redrawWinline(lnum, FALSE);
6840 }
6841}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006842
6843/*
6844 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6845 * spelled word, if there is one.
6846 */
6847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006848spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006849{
6850 pos_T tpos = curwin->w_cursor;
6851
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006852 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006853 if (curwin->w_cursor.col != tpos.col)
6854 start_arrow(&tpos);
6855}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006856#endif
6857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858/*
6859 * stop_arrow() is called before a change is made in insert mode.
6860 * If an arrow key has been used, start a new insertion.
6861 * Returns FAIL if undo is impossible, shouldn't insert then.
6862 */
6863 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006864stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865{
6866 if (arrow_used)
6867 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006868 Insstart = curwin->w_cursor; /* new insertion starts here */
6869 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6870 /* Don't update the original insert position when moved to the
6871 * right, except when nothing was inserted yet. */
6872 update_Insstart_orig = FALSE;
6873 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6874
Bram Moolenaar071d4272004-06-13 20:20:40 +00006875 if (u_save_cursor() == OK)
6876 {
6877 arrow_used = FALSE;
6878 ins_need_undo = FALSE;
6879 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006880
Bram Moolenaar071d4272004-06-13 20:20:40 +00006881 ai_col = 0;
6882#ifdef FEAT_VREPLACE
6883 if (State & VREPLACE_FLAG)
6884 {
6885 orig_line_count = curbuf->b_ml.ml_line_count;
6886 vr_lines_changed = 1;
6887 }
6888#endif
6889 ResetRedobuff();
6890 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006891 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006892 }
6893 else if (ins_need_undo)
6894 {
6895 if (u_save_cursor() == OK)
6896 ins_need_undo = FALSE;
6897 }
6898
6899#ifdef FEAT_FOLDING
6900 /* Always open fold at the cursor line when inserting something. */
6901 foldOpenCursor();
6902#endif
6903
6904 return (arrow_used || ins_need_undo ? FAIL : OK);
6905}
6906
6907/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006908 * Do a few things to stop inserting.
6909 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6910 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 */
6912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006913stop_insert(
6914 pos_T *end_insert_pos,
6915 int esc, /* called by ins_esc() */
6916 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006918 int cc;
6919 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920
6921 stop_redo_ins();
6922 replace_flush(); /* abandon replace stack */
6923
6924 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006925 * Save the inserted text for later redo with ^@ and CTRL-A.
6926 * Don't do it when "restart_edit" was set and nothing was inserted,
6927 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006929 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006930 if (did_restart_edit == 0 || (ptr != NULL
6931 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006932 {
6933 vim_free(last_insert);
6934 last_insert = ptr;
6935 last_insert_skip = new_insert_skip;
6936 }
6937 else
6938 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006939
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006940 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 {
6942 /* Auto-format now. It may seem strange to do this when stopping an
6943 * insertion (or moving the cursor), but it's required when appending
6944 * a line and having it end in a space. But only do it when something
6945 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006946 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006948 pos_T tpos = curwin->w_cursor;
6949
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 /* When the cursor is at the end of the line after a space the
6951 * formatting will move it to the following word. Avoid that by
6952 * moving the cursor onto the space. */
6953 cc = 'x';
6954 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6955 {
6956 dec_cursor();
6957 cc = gchar_cursor();
6958 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006959 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960 }
6961
6962 auto_format(TRUE, FALSE);
6963
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006964 if (vim_iswhite(cc))
6965 {
6966 if (gchar_cursor() != NUL)
6967 inc_cursor();
6968#ifdef FEAT_VIRTUALEDIT
6969 /* If the cursor is still at the same character, also keep
6970 * the "coladd". */
6971 if (gchar_cursor() == NUL
6972 && curwin->w_cursor.lnum == tpos.lnum
6973 && curwin->w_cursor.col == tpos.col)
6974 curwin->w_cursor.coladd = tpos.coladd;
6975#endif
6976 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977 }
6978
6979 /* If a space was inserted for auto-formatting, remove it now. */
6980 check_auto_format(TRUE);
6981
6982 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006983 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006984 * Do this when ESC was used or moving the cursor up/down.
6985 * Check for the old position still being valid, just in case the text
6986 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006987 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006988 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6989 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006990 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006991 pos_T tpos = curwin->w_cursor;
6992
6993 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006994 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006995 for (;;)
6996 {
6997 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6998 --curwin->w_cursor.col;
6999 cc = gchar_cursor();
7000 if (!vim_iswhite(cc))
7001 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007002 if (del_char(TRUE) == FAIL)
7003 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007004 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007005 if (curwin->w_cursor.lnum != tpos.lnum)
7006 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007007 else
7008 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007009 /* reset tpos, could have been invalidated in the loop above */
7010 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007011 tpos.col++;
7012 if (cc != NUL && gchar_pos(&tpos) == NUL)
7013 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7014 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007015
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 /* <C-S-Right> may have started Visual mode, adjust the position for
7017 * deleted characters. */
7018 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7019 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007020 int len = (int)STRLEN(ml_get_curline());
7021
7022 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007023 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007024 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007025#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007026 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 }
7029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030 }
7031 }
7032 did_ai = FALSE;
7033#ifdef FEAT_SMARTINDENT
7034 did_si = FALSE;
7035 can_si = FALSE;
7036 can_si_back = FALSE;
7037#endif
7038
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007039 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7040 * now in a different buffer. */
7041 if (end_insert_pos != NULL)
7042 {
7043 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007044 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007045 curbuf->b_op_end = *end_insert_pos;
7046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047}
7048
7049/*
7050 * Set the last inserted text to a single character.
7051 * Used for the replace command.
7052 */
7053 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007054set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055{
7056 char_u *s;
7057
7058 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060 if (last_insert != NULL)
7061 {
7062 s = last_insert;
7063 /* Use the CTRL-V only when entering a special char */
7064 if (c < ' ' || c == DEL)
7065 *s++ = Ctrl_V;
7066 s = add_char2buf(c, s);
7067 *s++ = ESC;
7068 *s++ = NUL;
7069 last_insert_skip = 0;
7070 }
7071}
7072
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007073#if defined(EXITFREE) || defined(PROTO)
7074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007075free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007076{
7077 vim_free(last_insert);
7078 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007079# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007080 vim_free(compl_orig_text);
7081 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007082# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007083}
7084#endif
7085
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086/*
7087 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7088 * and CSI. Handle multi-byte characters.
7089 * Returns a pointer to after the added bytes.
7090 */
7091 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007092add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093{
7094#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007095 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007096 int i;
7097 int len;
7098
7099 len = (*mb_char2bytes)(c, temp);
7100 for (i = 0; i < len; ++i)
7101 {
7102 c = temp[i];
7103#endif
7104 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7105 if (c == K_SPECIAL)
7106 {
7107 *s++ = K_SPECIAL;
7108 *s++ = KS_SPECIAL;
7109 *s++ = KE_FILLER;
7110 }
7111#ifdef FEAT_GUI
7112 else if (c == CSI)
7113 {
7114 *s++ = CSI;
7115 *s++ = KS_EXTRA;
7116 *s++ = (int)KE_CSI;
7117 }
7118#endif
7119 else
7120 *s++ = c;
7121#ifdef FEAT_MBYTE
7122 }
7123#endif
7124 return s;
7125}
7126
7127/*
7128 * move cursor to start of line
7129 * if flags & BL_WHITE move to first non-white
7130 * if flags & BL_SOL move to first non-white if startofline is set,
7131 * otherwise keep "curswant" column
7132 * if flags & BL_FIX don't leave the cursor on a NUL.
7133 */
7134 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007135beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136{
7137 if ((flags & BL_SOL) && !p_sol)
7138 coladvance(curwin->w_curswant);
7139 else
7140 {
7141 curwin->w_cursor.col = 0;
7142#ifdef FEAT_VIRTUALEDIT
7143 curwin->w_cursor.coladd = 0;
7144#endif
7145
7146 if (flags & (BL_WHITE | BL_SOL))
7147 {
7148 char_u *ptr;
7149
7150 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7151 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7152 ++curwin->w_cursor.col;
7153 }
7154 curwin->w_set_curswant = TRUE;
7155 }
7156}
7157
7158/*
7159 * oneright oneleft cursor_down cursor_up
7160 *
7161 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007162 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007163 * Return OK when successful, FAIL when we hit a line of file boundary.
7164 */
7165
7166 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007167oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007168{
7169 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007170 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171
7172#ifdef FEAT_VIRTUALEDIT
7173 if (virtual_active())
7174 {
7175 pos_T prevpos = curwin->w_cursor;
7176
7177 /* Adjust for multi-wide char (excluding TAB) */
7178 ptr = ml_get_cursor();
7179 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007180# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007182# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007183 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007184# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 ))
7186 ? ptr2cells(ptr) : 1));
7187 curwin->w_set_curswant = TRUE;
7188 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7189 return (prevpos.col != curwin->w_cursor.col
7190 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7191 }
7192#endif
7193
7194 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007195 if (*ptr == NUL)
7196 return FAIL; /* already at the very end */
7197
Bram Moolenaar071d4272004-06-13 20:20:40 +00007198#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007199 if (has_mbyte)
7200 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 else
7202#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007203 l = 1;
7204
7205 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7206 * contains "onemore". */
7207 if (ptr[l] == NUL
7208#ifdef FEAT_VIRTUALEDIT
7209 && (ve_flags & VE_ONEMORE) == 0
7210#endif
7211 )
7212 return FAIL;
7213 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214
7215 curwin->w_set_curswant = TRUE;
7216 return OK;
7217}
7218
7219 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007220oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221{
7222#ifdef FEAT_VIRTUALEDIT
7223 if (virtual_active())
7224 {
7225 int width;
7226 int v = getviscol();
7227
7228 if (v == 0)
7229 return FAIL;
7230
7231# ifdef FEAT_LINEBREAK
7232 /* We might get stuck on 'showbreak', skip over it. */
7233 width = 1;
7234 for (;;)
7235 {
7236 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007237 /* getviscol() is slow, skip it when 'showbreak' is empty,
7238 * 'breakindent' is not set and there are no multi-byte
7239 * characters */
7240 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007241# ifdef FEAT_MBYTE
7242 && !has_mbyte
7243# endif
7244 ) || getviscol() < v)
7245 break;
7246 ++width;
7247 }
7248# else
7249 coladvance(v - 1);
7250# endif
7251
7252 if (curwin->w_cursor.coladd == 1)
7253 {
7254 char_u *ptr;
7255
7256 /* Adjust for multi-wide char (not a TAB) */
7257 ptr = ml_get_cursor();
7258 if (*ptr != TAB && vim_isprintc(
7259# ifdef FEAT_MBYTE
7260 (*mb_ptr2char)(ptr)
7261# else
7262 *ptr
7263# endif
7264 ) && ptr2cells(ptr) > 1)
7265 curwin->w_cursor.coladd = 0;
7266 }
7267
7268 curwin->w_set_curswant = TRUE;
7269 return OK;
7270 }
7271#endif
7272
7273 if (curwin->w_cursor.col == 0)
7274 return FAIL;
7275
7276 curwin->w_set_curswant = TRUE;
7277 --curwin->w_cursor.col;
7278
7279#ifdef FEAT_MBYTE
7280 /* if the character on the left of the current cursor is a multi-byte
7281 * character, move to its first byte */
7282 if (has_mbyte)
7283 mb_adjust_cursor();
7284#endif
7285 return OK;
7286}
7287
7288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007289cursor_up(
7290 long n,
7291 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292{
7293 linenr_T lnum;
7294
7295 if (n > 0)
7296 {
7297 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007298 /* This fails if the cursor is already in the first line or the count
7299 * is larger than the line number and '-' is in 'cpoptions' */
7300 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301 return FAIL;
7302 if (n >= lnum)
7303 lnum = 1;
7304 else
7305#ifdef FEAT_FOLDING
7306 if (hasAnyFolding(curwin))
7307 {
7308 /*
7309 * Count each sequence of folded lines as one logical line.
7310 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007311 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312 (void)hasFolding(lnum, &lnum, NULL);
7313
7314 while (n--)
7315 {
7316 /* move up one line */
7317 --lnum;
7318 if (lnum <= 1)
7319 break;
7320 /* If we entered a fold, move to the beginning, unless in
7321 * Insert mode or when 'foldopen' contains "all": it will open
7322 * in a moment. */
7323 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7324 (void)hasFolding(lnum, &lnum, NULL);
7325 }
7326 if (lnum < 1)
7327 lnum = 1;
7328 }
7329 else
7330#endif
7331 lnum -= n;
7332 curwin->w_cursor.lnum = lnum;
7333 }
7334
7335 /* try to advance to the column we want to be at */
7336 coladvance(curwin->w_curswant);
7337
7338 if (upd_topline)
7339 update_topline(); /* make sure curwin->w_topline is valid */
7340
7341 return OK;
7342}
7343
7344/*
7345 * Cursor down a number of logical lines.
7346 */
7347 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007348cursor_down(
7349 long n,
7350 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351{
7352 linenr_T lnum;
7353
7354 if (n > 0)
7355 {
7356 lnum = curwin->w_cursor.lnum;
7357#ifdef FEAT_FOLDING
7358 /* Move to last line of fold, will fail if it's the end-of-file. */
7359 (void)hasFolding(lnum, NULL, &lnum);
7360#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007361 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007362 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007363 if (lnum >= curbuf->b_ml.ml_line_count
7364 || (lnum + n > curbuf->b_ml.ml_line_count
7365 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 return FAIL;
7367 if (lnum + n >= curbuf->b_ml.ml_line_count)
7368 lnum = curbuf->b_ml.ml_line_count;
7369 else
7370#ifdef FEAT_FOLDING
7371 if (hasAnyFolding(curwin))
7372 {
7373 linenr_T last;
7374
7375 /* count each sequence of folded lines as one logical line */
7376 while (n--)
7377 {
7378 if (hasFolding(lnum, NULL, &last))
7379 lnum = last + 1;
7380 else
7381 ++lnum;
7382 if (lnum >= curbuf->b_ml.ml_line_count)
7383 break;
7384 }
7385 if (lnum > curbuf->b_ml.ml_line_count)
7386 lnum = curbuf->b_ml.ml_line_count;
7387 }
7388 else
7389#endif
7390 lnum += n;
7391 curwin->w_cursor.lnum = lnum;
7392 }
7393
7394 /* try to advance to the column we want to be at */
7395 coladvance(curwin->w_curswant);
7396
7397 if (upd_topline)
7398 update_topline(); /* make sure curwin->w_topline is valid */
7399
7400 return OK;
7401}
7402
7403/*
7404 * Stuff the last inserted text in the read buffer.
7405 * Last_insert actually is a copy of the redo buffer, so we
7406 * first have to remove the command.
7407 */
7408 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007409stuff_inserted(
7410 int c, /* Command character to be inserted */
7411 long count, /* Repeat this many times */
7412 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007413{
7414 char_u *esc_ptr;
7415 char_u *ptr;
7416 char_u *last_ptr;
7417 char_u last = NUL;
7418
7419 ptr = get_last_insert();
7420 if (ptr == NULL)
7421 {
7422 EMSG(_(e_noinstext));
7423 return FAIL;
7424 }
7425
7426 /* may want to stuff the command character, to start Insert mode */
7427 if (c != NUL)
7428 stuffcharReadbuff(c);
7429 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7430 *esc_ptr = NUL; /* remove the ESC */
7431
7432 /* when the last char is either "0" or "^" it will be quoted if no ESC
7433 * comes after it OR if it will inserted more than once and "ptr"
7434 * starts with ^D. -- Acevedo
7435 */
7436 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7437 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7438 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7439 {
7440 last = *last_ptr;
7441 *last_ptr = NUL;
7442 }
7443
7444 do
7445 {
7446 stuffReadbuff(ptr);
7447 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7448 if (last)
7449 stuffReadbuff((char_u *)(last == '0'
7450 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7451 : IF_EB("\026^", CTRL_V_STR "^")));
7452 }
7453 while (--count > 0);
7454
7455 if (last)
7456 *last_ptr = last;
7457
7458 if (esc_ptr != NULL)
7459 *esc_ptr = ESC; /* put the ESC back */
7460
7461 /* may want to stuff a trailing ESC, to get out of Insert mode */
7462 if (!no_esc)
7463 stuffcharReadbuff(ESC);
7464
7465 return OK;
7466}
7467
7468 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007469get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007470{
7471 if (last_insert == NULL)
7472 return NULL;
7473 return last_insert + last_insert_skip;
7474}
7475
7476/*
7477 * Get last inserted string, and remove trailing <Esc>.
7478 * Returns pointer to allocated memory (must be freed) or NULL.
7479 */
7480 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007481get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482{
7483 char_u *s;
7484 int len;
7485
7486 if (last_insert == NULL)
7487 return NULL;
7488 s = vim_strsave(last_insert + last_insert_skip);
7489 if (s != NULL)
7490 {
7491 len = (int)STRLEN(s);
7492 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7493 s[len - 1] = NUL;
7494 }
7495 return s;
7496}
7497
7498/*
7499 * Check the word in front of the cursor for an abbreviation.
7500 * Called when the non-id character "c" has been entered.
7501 * When an abbreviation is recognized it is removed from the text and
7502 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7503 */
7504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007505echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007506{
7507 /* Don't check for abbreviation in paste mode, when disabled and just
7508 * after moving around with cursor keys. */
7509 if (p_paste || no_abbr || arrow_used)
7510 return FALSE;
7511
7512 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7513 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7514}
7515
7516/*
7517 * replace-stack functions
7518 *
7519 * When replacing characters, the replaced characters are remembered for each
7520 * new character. This is used to re-insert the old text when backspacing.
7521 *
7522 * There is a NUL headed list of characters for each character that is
7523 * currently in the file after the insertion point. When BS is used, one NUL
7524 * headed list is put back for the deleted character.
7525 *
7526 * For a newline, there are two NUL headed lists. One contains the characters
7527 * that the NL replaced. The extra one stores the characters after the cursor
7528 * that were deleted (always white space).
7529 *
7530 * Replace_offset is normally 0, in which case replace_push will add a new
7531 * character at the end of the stack. If replace_offset is not 0, that many
7532 * characters will be left on the stack above the newly inserted character.
7533 */
7534
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007535static char_u *replace_stack = NULL;
7536static long replace_stack_nr = 0; /* next entry in replace stack */
7537static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007538
7539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007540replace_push(
7541 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542{
7543 char_u *p;
7544
7545 if (replace_stack_nr < replace_offset) /* nothing to do */
7546 return;
7547 if (replace_stack_len <= replace_stack_nr)
7548 {
7549 replace_stack_len += 50;
7550 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7551 if (p == NULL) /* out of memory */
7552 {
7553 replace_stack_len -= 50;
7554 return;
7555 }
7556 if (replace_stack != NULL)
7557 {
7558 mch_memmove(p, replace_stack,
7559 (size_t)(replace_stack_nr * sizeof(char_u)));
7560 vim_free(replace_stack);
7561 }
7562 replace_stack = p;
7563 }
7564 p = replace_stack + replace_stack_nr - replace_offset;
7565 if (replace_offset)
7566 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7567 *p = c;
7568 ++replace_stack_nr;
7569}
7570
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007571#if defined(FEAT_MBYTE) || defined(PROTO)
7572/*
7573 * Push a character onto the replace stack. Handles a multi-byte character in
7574 * reverse byte order, so that the first byte is popped off first.
7575 * Return the number of bytes done (includes composing characters).
7576 */
7577 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007578replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007579{
7580 int l = (*mb_ptr2len)(p);
7581 int j;
7582
7583 for (j = l - 1; j >= 0; --j)
7584 replace_push(p[j]);
7585 return l;
7586}
7587#endif
7588
Bram Moolenaar071d4272004-06-13 20:20:40 +00007589/*
7590 * Pop one item from the replace stack.
7591 * return -1 if stack empty
7592 * return replaced character or NUL otherwise
7593 */
7594 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007595replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007596{
7597 if (replace_stack_nr == 0)
7598 return -1;
7599 return (int)replace_stack[--replace_stack_nr];
7600}
7601
7602/*
7603 * Join the top two items on the replace stack. This removes to "off"'th NUL
7604 * encountered.
7605 */
7606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007607replace_join(
7608 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007609{
7610 int i;
7611
7612 for (i = replace_stack_nr; --i >= 0; )
7613 if (replace_stack[i] == NUL && off-- <= 0)
7614 {
7615 --replace_stack_nr;
7616 mch_memmove(replace_stack + i, replace_stack + i + 1,
7617 (size_t)(replace_stack_nr - i));
7618 return;
7619 }
7620}
7621
7622/*
7623 * Pop bytes from the replace stack until a NUL is found, and insert them
7624 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7625 */
7626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007627replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007628{
7629 int cc;
7630 int oldState = State;
7631
7632 State = NORMAL; /* don't want REPLACE here */
7633 while ((cc = replace_pop()) > 0)
7634 {
7635#ifdef FEAT_MBYTE
7636 mb_replace_pop_ins(cc);
7637#else
7638 ins_char(cc);
7639#endif
7640 dec_cursor();
7641 }
7642 State = oldState;
7643}
7644
7645#ifdef FEAT_MBYTE
7646/*
7647 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7648 * indicates a multi-byte char, pop the other bytes too.
7649 */
7650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007651mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007652{
7653 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007654 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007655 int i;
7656 int c;
7657
7658 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7659 {
7660 buf[0] = cc;
7661 for (i = 1; i < n; ++i)
7662 buf[i] = replace_pop();
7663 ins_bytes_len(buf, n);
7664 }
7665 else
7666 ins_char(cc);
7667
7668 if (enc_utf8)
7669 /* Handle composing chars. */
7670 for (;;)
7671 {
7672 c = replace_pop();
7673 if (c == -1) /* stack empty */
7674 break;
7675 if ((n = MB_BYTE2LEN(c)) == 1)
7676 {
7677 /* Not a multi-byte char, put it back. */
7678 replace_push(c);
7679 break;
7680 }
7681 else
7682 {
7683 buf[0] = c;
7684 for (i = 1; i < n; ++i)
7685 buf[i] = replace_pop();
7686 if (utf_iscomposing(utf_ptr2char(buf)))
7687 ins_bytes_len(buf, n);
7688 else
7689 {
7690 /* Not a composing char, put it back. */
7691 for (i = n - 1; i >= 0; --i)
7692 replace_push(buf[i]);
7693 break;
7694 }
7695 }
7696 }
7697}
7698#endif
7699
7700/*
7701 * make the replace stack empty
7702 * (called when exiting replace mode)
7703 */
7704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007705replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706{
7707 vim_free(replace_stack);
7708 replace_stack = NULL;
7709 replace_stack_len = 0;
7710 replace_stack_nr = 0;
7711}
7712
7713/*
7714 * Handle doing a BS for one character.
7715 * cc < 0: replace stack empty, just move cursor
7716 * cc == 0: character was inserted, delete it
7717 * cc > 0: character was replaced, put cc (first byte of original char) back
7718 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007719 * When "limit_col" is >= 0, don't delete before this column. Matters when
7720 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007721 */
7722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007723replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007724{
7725 int cc;
7726#ifdef FEAT_VREPLACE
7727 int orig_len = 0;
7728 int ins_len;
7729 int orig_vcols = 0;
7730 colnr_T start_vcol;
7731 char_u *p;
7732 int i;
7733 int vcol;
7734#endif
7735
7736 cc = replace_pop();
7737 if (cc > 0)
7738 {
7739#ifdef FEAT_VREPLACE
7740 if (State & VREPLACE_FLAG)
7741 {
7742 /* Get the number of screen cells used by the character we are
7743 * going to delete. */
7744 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7745 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7746 }
7747#endif
7748#ifdef FEAT_MBYTE
7749 if (has_mbyte)
7750 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007751 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752# ifdef FEAT_VREPLACE
7753 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007754 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755# endif
7756 replace_push(cc);
7757 }
7758 else
7759#endif
7760 {
7761 pchar_cursor(cc);
7762#ifdef FEAT_VREPLACE
7763 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007764 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765#endif
7766 }
7767 replace_pop_ins();
7768
7769#ifdef FEAT_VREPLACE
7770 if (State & VREPLACE_FLAG)
7771 {
7772 /* Get the number of screen cells used by the inserted characters */
7773 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007774 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 vcol = start_vcol;
7776 for (i = 0; i < ins_len; ++i)
7777 {
7778 vcol += chartabsize(p + i, vcol);
7779#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007780 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007781#endif
7782 }
7783 vcol -= start_vcol;
7784
7785 /* Delete spaces that were inserted after the cursor to keep the
7786 * text aligned. */
7787 curwin->w_cursor.col += ins_len;
7788 while (vcol > orig_vcols && gchar_cursor() == ' ')
7789 {
7790 del_char(FALSE);
7791 ++orig_vcols;
7792 }
7793 curwin->w_cursor.col -= ins_len;
7794 }
7795#endif
7796
7797 /* mark the buffer as changed and prepare for displaying */
7798 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7799 }
7800 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007801 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802}
7803
7804#ifdef FEAT_CINDENT
7805/*
7806 * Return TRUE if C-indenting is on.
7807 */
7808 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007809cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007810{
7811 return (!p_paste && (curbuf->b_p_cin
7812# ifdef FEAT_EVAL
7813 || *curbuf->b_p_inde != NUL
7814# endif
7815 ));
7816}
7817#endif
7818
7819#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7820/*
7821 * Re-indent the current line, based on the current contents of it and the
7822 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7823 * confused what all the part that handles Control-T is doing that I'm not.
7824 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7825 */
7826
7827 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007828fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007830 int amount = get_the_indent();
7831
7832 if (amount >= 0)
7833 {
7834 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7835 if (linewhite(curwin->w_cursor.lnum))
7836 did_ai = TRUE; /* delete the indent if the line stays empty */
7837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838}
7839
7840 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007841fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842{
7843 if (p_paste)
7844 return;
7845# ifdef FEAT_LISP
7846 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7847 fixthisline(get_lisp_indent);
7848# endif
7849# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7850 else
7851# endif
7852# ifdef FEAT_CINDENT
7853 if (cindent_on())
7854 do_c_expr_indent();
7855# endif
7856}
7857
7858#endif
7859
7860#ifdef FEAT_CINDENT
7861/*
7862 * return TRUE if 'cinkeys' contains the key "keytyped",
7863 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007864 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007865 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7866 *
7867 * "keytyped" can have a few special values:
7868 * KEY_OPEN_FORW
7869 * KEY_OPEN_BACK
7870 * KEY_COMPLETE just finished completion.
7871 *
7872 * If line_is_empty is TRUE accept keys with '0' before them.
7873 */
7874 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007875in_cinkeys(
7876 int keytyped,
7877 int when,
7878 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
7880 char_u *look;
7881 int try_match;
7882 int try_match_word;
7883 char_u *p;
7884 char_u *line;
7885 int icase;
7886 int i;
7887
Bram Moolenaar30848942009-12-24 14:46:12 +00007888 if (keytyped == NUL)
7889 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7890 return FALSE;
7891
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892#ifdef FEAT_EVAL
7893 if (*curbuf->b_p_inde != NUL)
7894 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7895 else
7896#endif
7897 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7898 while (*look)
7899 {
7900 /*
7901 * Find out if we want to try a match with this key, depending on
7902 * 'when' and a '*' or '!' before the key.
7903 */
7904 switch (when)
7905 {
7906 case '*': try_match = (*look == '*'); break;
7907 case '!': try_match = (*look == '!'); break;
7908 default: try_match = (*look != '*'); break;
7909 }
7910 if (*look == '*' || *look == '!')
7911 ++look;
7912
7913 /*
7914 * If there is a '0', only accept a match if the line is empty.
7915 * But may still match when typing last char of a word.
7916 */
7917 if (*look == '0')
7918 {
7919 try_match_word = try_match;
7920 if (!line_is_empty)
7921 try_match = FALSE;
7922 ++look;
7923 }
7924 else
7925 try_match_word = FALSE;
7926
7927 /*
7928 * does it look like a control character?
7929 */
7930 if (*look == '^'
7931#ifdef EBCDIC
7932 && (Ctrl_chr(look[1]) != 0)
7933#else
7934 && look[1] >= '?' && look[1] <= '_'
7935#endif
7936 )
7937 {
7938 if (try_match && keytyped == Ctrl_chr(look[1]))
7939 return TRUE;
7940 look += 2;
7941 }
7942 /*
7943 * 'o' means "o" command, open forward.
7944 * 'O' means "O" command, open backward.
7945 */
7946 else if (*look == 'o')
7947 {
7948 if (try_match && keytyped == KEY_OPEN_FORW)
7949 return TRUE;
7950 ++look;
7951 }
7952 else if (*look == 'O')
7953 {
7954 if (try_match && keytyped == KEY_OPEN_BACK)
7955 return TRUE;
7956 ++look;
7957 }
7958
7959 /*
7960 * 'e' means to check for "else" at start of line and just before the
7961 * cursor.
7962 */
7963 else if (*look == 'e')
7964 {
7965 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7966 {
7967 p = ml_get_curline();
7968 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7969 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7970 return TRUE;
7971 }
7972 ++look;
7973 }
7974
7975 /*
7976 * ':' only causes an indent if it is at the end of a label or case
7977 * statement, or when it was before typing the ':' (to fix
7978 * class::method for C++).
7979 */
7980 else if (*look == ':')
7981 {
7982 if (try_match && keytyped == ':')
7983 {
7984 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007985 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007987 /* Need to get the line again after cin_islabel(). */
7988 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989 if (curwin->w_cursor.col > 2
7990 && p[curwin->w_cursor.col - 1] == ':'
7991 && p[curwin->w_cursor.col - 2] == ':')
7992 {
7993 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007994 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007995 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 p = ml_get_curline();
7997 p[curwin->w_cursor.col - 1] = ':';
7998 if (i)
7999 return TRUE;
8000 }
8001 }
8002 ++look;
8003 }
8004
8005
8006 /*
8007 * Is it a key in <>, maybe?
8008 */
8009 else if (*look == '<')
8010 {
8011 if (try_match)
8012 {
8013 /*
8014 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8015 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8016 * >, *, : and ! keys if they really really want to.
8017 */
8018 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8019 && keytyped == look[1])
8020 return TRUE;
8021
8022 if (keytyped == get_special_key_code(look + 1))
8023 return TRUE;
8024 }
8025 while (*look && *look != '>')
8026 look++;
8027 while (*look == '>')
8028 look++;
8029 }
8030
8031 /*
8032 * Is it a word: "=word"?
8033 */
8034 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8035 {
8036 ++look;
8037 if (*look == '~')
8038 {
8039 icase = TRUE;
8040 ++look;
8041 }
8042 else
8043 icase = FALSE;
8044 p = vim_strchr(look, ',');
8045 if (p == NULL)
8046 p = look + STRLEN(look);
8047 if ((try_match || try_match_word)
8048 && curwin->w_cursor.col >= (colnr_T)(p - look))
8049 {
8050 int match = FALSE;
8051
8052#ifdef FEAT_INS_EXPAND
8053 if (keytyped == KEY_COMPLETE)
8054 {
8055 char_u *s;
8056
8057 /* Just completed a word, check if it starts with "look".
8058 * search back for the start of a word. */
8059 line = ml_get_curline();
8060# ifdef FEAT_MBYTE
8061 if (has_mbyte)
8062 {
8063 char_u *n;
8064
8065 for (s = line + curwin->w_cursor.col; s > line; s = n)
8066 {
8067 n = mb_prevptr(line, s);
8068 if (!vim_iswordp(n))
8069 break;
8070 }
8071 }
8072 else
8073# endif
8074 for (s = line + curwin->w_cursor.col; s > line; --s)
8075 if (!vim_iswordc(s[-1]))
8076 break;
8077 if (s + (p - look) <= line + curwin->w_cursor.col
8078 && (icase
8079 ? MB_STRNICMP(s, look, p - look)
8080 : STRNCMP(s, look, p - look)) == 0)
8081 match = TRUE;
8082 }
8083 else
8084#endif
8085 /* TODO: multi-byte */
8086 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8087 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8088 {
8089 line = ml_get_cursor();
8090 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8091 || !vim_iswordc(line[-(p - look) - 1]))
8092 && (icase
8093 ? MB_STRNICMP(line - (p - look), look, p - look)
8094 : STRNCMP(line - (p - look), look, p - look))
8095 == 0)
8096 match = TRUE;
8097 }
8098 if (match && try_match_word && !try_match)
8099 {
8100 /* "0=word": Check if there are only blanks before the
8101 * word. */
8102 line = ml_get_curline();
8103 if ((int)(skipwhite(line) - line) !=
8104 (int)(curwin->w_cursor.col - (p - look)))
8105 match = FALSE;
8106 }
8107 if (match)
8108 return TRUE;
8109 }
8110 look = p;
8111 }
8112
8113 /*
8114 * ok, it's a boring generic character.
8115 */
8116 else
8117 {
8118 if (try_match && *look == keytyped)
8119 return TRUE;
8120 ++look;
8121 }
8122
8123 /*
8124 * Skip over ", ".
8125 */
8126 look = skip_to_option_part(look);
8127 }
8128 return FALSE;
8129}
8130#endif /* FEAT_CINDENT */
8131
8132#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8133/*
8134 * Map Hebrew keyboard when in hkmap mode.
8135 */
8136 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008137hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138{
8139 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8140 {
8141 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8142 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8143 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8144 static char_u map[26] =
8145 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8146 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8147 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8148 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8149 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8150 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8151 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8152 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8153 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8154
8155 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8156 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8157 /* '-1'='sofit' */
8158 else if (c == 'x')
8159 return 'X';
8160 else if (c == 'q')
8161 return '\''; /* {geresh}={'} */
8162 else if (c == 246)
8163 return ' '; /* \"o --> ' ' for a german keyboard */
8164 else if (c == 228)
8165 return ' '; /* \"a --> ' ' -- / -- */
8166 else if (c == 252)
8167 return ' '; /* \"u --> ' ' -- / -- */
8168#ifdef EBCDIC
8169 else if (islower(c))
8170#else
8171 /* NOTE: islower() does not do the right thing for us on Linux so we
8172 * do this the same was as 5.7 and previous, so it works correctly on
8173 * all systems. Specifically, the e.g. Delete and Arrow keys are
8174 * munged and won't work if e.g. searching for Hebrew text.
8175 */
8176 else if (c >= 'a' && c <= 'z')
8177#endif
8178 return (int)(map[CharOrdLow(c)] + p_aleph);
8179 else
8180 return c;
8181 }
8182 else
8183 {
8184 switch (c)
8185 {
8186 case '`': return ';';
8187 case '/': return '.';
8188 case '\'': return ',';
8189 case 'q': return '/';
8190 case 'w': return '\'';
8191
8192 /* Hebrew letters - set offset from 'a' */
8193 case ',': c = '{'; break;
8194 case '.': c = 'v'; break;
8195 case ';': c = 't'; break;
8196 default: {
8197 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8198
8199#ifdef EBCDIC
8200 /* see note about islower() above */
8201 if (!islower(c))
8202#else
8203 if (c < 'a' || c > 'z')
8204#endif
8205 return c;
8206 c = str[CharOrdLow(c)];
8207 break;
8208 }
8209 }
8210
8211 return (int)(CharOrdLow(c) + p_aleph);
8212 }
8213}
8214#endif
8215
8216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008217ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218{
8219 int need_redraw = FALSE;
8220 int regname;
8221 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008222 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223
8224 /*
8225 * If we are going to wait for a character, show a '"'.
8226 */
8227 pc_status = PC_STATUS_UNSET;
8228 if (redrawing() && !char_avail())
8229 {
8230 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008231 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232
8233 edit_putchar('"', TRUE);
8234#ifdef FEAT_CMDL_INFO
8235 add_to_showcmd_c(Ctrl_R);
8236#endif
8237 }
8238
8239#ifdef USE_ON_FLY_SCROLL
8240 dont_scroll = TRUE; /* disallow scrolling here */
8241#endif
8242
8243 /*
8244 * Don't map the register name. This also prevents the mode message to be
8245 * deleted when ESC is hit.
8246 */
8247 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008248 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8251 {
8252 /* Get a third key for literal register insertion */
8253 literally = regname;
8254#ifdef FEAT_CMDL_INFO
8255 add_to_showcmd_c(literally);
8256#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008257 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008258 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 }
8260 --no_mapping;
8261
8262#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008263 /* Don't call u_sync() while typing the expression or giving an error
8264 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 ++no_u_sync;
8266 if (regname == '=')
8267 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008268# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008270# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008271 /* Sync undo when evaluating the expression calls setline() or
8272 * append(), so that it can be undone separately. */
8273 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008274
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008276# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 /* Restore the Input Method. */
8278 if (im_on)
8279 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008280# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008282 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8283 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008284 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 else
8288 {
8289#endif
8290 if (literally == Ctrl_O || literally == Ctrl_P)
8291 {
8292 /* Append the command to the redo buffer. */
8293 AppendCharToRedobuff(Ctrl_R);
8294 AppendCharToRedobuff(literally);
8295 AppendCharToRedobuff(regname);
8296
8297 do_put(regname, BACKWARD, 1L,
8298 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8299 }
8300 else if (insert_reg(regname, literally) == FAIL)
8301 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008302 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 need_redraw = TRUE; /* remove the '"' */
8304 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008305 else if (stop_insert_mode)
8306 /* When the '=' register was used and a function was invoked that
8307 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8308 * insert anything, need to remove the '"' */
8309 need_redraw = TRUE;
8310
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311#ifdef FEAT_EVAL
8312 }
8313 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008314 if (u_sync_once == 1)
8315 ins_need_undo = TRUE;
8316 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317#endif
8318#ifdef FEAT_CMDL_INFO
8319 clear_showcmd();
8320#endif
8321
8322 /* If the inserted register is empty, we need to remove the '"' */
8323 if (need_redraw || stuff_empty())
8324 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008325
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008326 /* Disallow starting Visual mode here, would get a weird mode. */
8327 if (!vis_active && VIsual_active)
8328 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008329}
8330
8331/*
8332 * CTRL-G commands in Insert mode.
8333 */
8334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008335ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336{
8337 int c;
8338
8339#ifdef FEAT_INS_EXPAND
8340 /* Right after CTRL-X the cursor will be after the ruler. */
8341 setcursor();
8342#endif
8343
8344 /*
8345 * Don't map the second key. This also prevents the mode message to be
8346 * deleted when ESC is hit.
8347 */
8348 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008349 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008350 --no_mapping;
8351 switch (c)
8352 {
8353 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8354 case K_UP:
8355 case Ctrl_K:
8356 case 'k': ins_up(TRUE);
8357 break;
8358
8359 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8360 case K_DOWN:
8361 case Ctrl_J:
8362 case 'j': ins_down(TRUE);
8363 break;
8364
8365 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008366 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008368
8369 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008370 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008371 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008372 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 break;
8374
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008375 /* CTRL-G U: do not break undo with the next char */
8376 case 'U':
8377 /* Allow one left/right cursor movement with the next char,
8378 * without breaking undo. */
8379 dont_sync_undo = MAYBE;
8380 break;
8381
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008383 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 }
8385}
8386
8387/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008388 * CTRL-^ in Insert mode.
8389 */
8390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008391ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008392{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008393 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008394 {
8395 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8396 if (State & LANGMAP)
8397 {
8398 curbuf->b_p_iminsert = B_IMODE_NONE;
8399 State &= ~LANGMAP;
8400 }
8401 else
8402 {
8403 curbuf->b_p_iminsert = B_IMODE_LMAP;
8404 State |= LANGMAP;
8405#ifdef USE_IM_CONTROL
8406 im_set_active(FALSE);
8407#endif
8408 }
8409 }
8410#ifdef USE_IM_CONTROL
8411 else
8412 {
8413 /* There are no ":lmap" mappings, toggle IM */
8414 if (im_get_status())
8415 {
8416 curbuf->b_p_iminsert = B_IMODE_NONE;
8417 im_set_active(FALSE);
8418 }
8419 else
8420 {
8421 curbuf->b_p_iminsert = B_IMODE_IM;
8422 State &= ~LANGMAP;
8423 im_set_active(TRUE);
8424 }
8425 }
8426#endif
8427 set_iminsert_global();
8428 showmode();
8429#ifdef FEAT_GUI
8430 /* may show different cursor shape or color */
8431 if (gui.in_use)
8432 gui_update_cursor(TRUE, FALSE);
8433#endif
8434#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8435 /* Show/unshow value of 'keymap' in status lines. */
8436 status_redraw_curbuf();
8437#endif
8438}
8439
8440/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008441 * Handle ESC in insert mode.
8442 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8443 * insert.
8444 */
8445 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008446ins_esc(
8447 long *count,
8448 int cmdchar,
8449 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450{
8451 int temp;
8452 static int disabled_redraw = FALSE;
8453
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008454#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008455 check_spell_redraw();
8456#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008457#if defined(FEAT_HANGULIN)
8458# if defined(ESC_CHG_TO_ENG_MODE)
8459 hangul_input_state_set(0);
8460# endif
8461 if (composing_hangul)
8462 {
8463 push_raw_key(composing_hangul_buffer, 2);
8464 composing_hangul = 0;
8465 }
8466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467
8468 temp = curwin->w_cursor.col;
8469 if (disabled_redraw)
8470 {
8471 --RedrawingDisabled;
8472 disabled_redraw = FALSE;
8473 }
8474 if (!arrow_used)
8475 {
8476 /*
8477 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008478 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8479 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 */
8481 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008482 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483
8484 /*
8485 * Repeating insert may take a long time. Check for
8486 * interrupt now and then.
8487 */
8488 if (*count > 0)
8489 {
8490 line_breakcheck();
8491 if (got_int)
8492 *count = 0;
8493 }
8494
8495 if (--*count > 0) /* repeat what was typed */
8496 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008497 /* Vi repeats the insert without replacing characters. */
8498 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8499 State &= ~REPLACE_FLAG;
8500
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 (void)start_redo_ins();
8502 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008503 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 ++RedrawingDisabled;
8505 disabled_redraw = TRUE;
8506 return FALSE; /* repeat the insert */
8507 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008508 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 undisplay_dollar();
8510 }
8511
8512 /* When an autoindent was removed, curswant stays after the
8513 * indent */
8514 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8515 curwin->w_set_curswant = TRUE;
8516
8517 /* Remember the last Insert position in the '^ mark. */
8518 if (!cmdmod.keepjumps)
8519 curbuf->b_last_insert = curwin->w_cursor;
8520
8521 /*
8522 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008523 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008525 if (!nomove
8526 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527#ifdef FEAT_VIRTUALEDIT
8528 || curwin->w_cursor.coladd > 0
8529#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008530 )
8531 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008532 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533#ifdef FEAT_RIGHTLEFT
8534 && !revins_on
8535#endif
8536 )
8537 {
8538#ifdef FEAT_VIRTUALEDIT
8539 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8540 {
8541 oneleft();
8542 if (restart_edit != NUL)
8543 ++curwin->w_cursor.coladd;
8544 }
8545 else
8546#endif
8547 {
8548 --curwin->w_cursor.col;
8549#ifdef FEAT_MBYTE
8550 /* Correct cursor for multi-byte character. */
8551 if (has_mbyte)
8552 mb_adjust_cursor();
8553#endif
8554 }
8555 }
8556
8557#ifdef USE_IM_CONTROL
8558 /* Disable IM to allow typing English directly for Normal mode commands.
8559 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8560 * well). */
8561 if (!(State & LANGMAP))
8562 im_save_status(&curbuf->b_p_iminsert);
8563 im_set_active(FALSE);
8564#endif
8565
8566 State = NORMAL;
8567 /* need to position cursor again (e.g. when on a TAB ) */
8568 changed_cline_bef_curs();
8569
8570#ifdef FEAT_MOUSE
8571 setmouse();
8572#endif
8573#ifdef CURSOR_SHAPE
8574 ui_cursor_shape(); /* may show different cursor shape */
8575#endif
8576
8577 /*
8578 * When recording or for CTRL-O, need to display the new mode.
8579 * Otherwise remove the mode message.
8580 */
8581 if (Recording || restart_edit != NUL)
8582 showmode();
8583 else if (p_smd)
8584 MSG("");
8585
8586 return TRUE; /* exit Insert mode */
8587}
8588
8589#ifdef FEAT_RIGHTLEFT
8590/*
8591 * Toggle language: hkmap and revins_on.
8592 * Move to end of reverse inserted text.
8593 */
8594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008595ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596{
8597 if (revins_on && revins_chars && revins_scol >= 0)
8598 {
8599 while (gchar_cursor() != NUL && revins_chars--)
8600 ++curwin->w_cursor.col;
8601 }
8602 p_ri = !p_ri;
8603 revins_on = (State == INSERT && p_ri);
8604 if (revins_on)
8605 {
8606 revins_scol = curwin->w_cursor.col;
8607 revins_legal++;
8608 revins_chars = 0;
8609 undisplay_dollar();
8610 }
8611 else
8612 revins_scol = -1;
8613#ifdef FEAT_FKMAP
8614 if (p_altkeymap)
8615 {
8616 /*
8617 * to be consistent also for redo command, using '.'
8618 * set arrow_used to true and stop it - causing to redo
8619 * characters entered in one mode (normal/reverse insert).
8620 */
8621 arrow_used = TRUE;
8622 (void)stop_arrow();
8623 p_fkmap = curwin->w_p_rl ^ p_ri;
8624 if (p_fkmap && p_ri)
8625 State = INSERT;
8626 }
8627 else
8628#endif
8629 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8630 showmode();
8631}
8632#endif
8633
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634/*
8635 * If 'keymodel' contains "startsel", may start selection.
8636 * Returns TRUE when a CTRL-O and other keys stuffed.
8637 */
8638 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008639ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640{
8641 if (km_startsel)
8642 switch (c)
8643 {
8644 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 case K_PAGEUP:
8647 case K_KPAGEUP:
8648 case K_PAGEDOWN:
8649 case K_KPAGEDOWN:
8650# ifdef MACOS
8651 case K_LEFT:
8652 case K_RIGHT:
8653 case K_UP:
8654 case K_DOWN:
8655 case K_END:
8656 case K_HOME:
8657# endif
8658 if (!(mod_mask & MOD_MASK_SHIFT))
8659 break;
8660 /* FALLTHROUGH */
8661 case K_S_LEFT:
8662 case K_S_RIGHT:
8663 case K_S_UP:
8664 case K_S_DOWN:
8665 case K_S_END:
8666 case K_S_HOME:
8667 /* Start selection right away, the cursor can move with
8668 * CTRL-O when beyond the end of the line. */
8669 start_selection();
8670
8671 /* Execute the key in (insert) Select mode. */
8672 stuffcharReadbuff(Ctrl_O);
8673 if (mod_mask)
8674 {
8675 char_u buf[4];
8676
8677 buf[0] = K_SPECIAL;
8678 buf[1] = KS_MODIFIER;
8679 buf[2] = mod_mask;
8680 buf[3] = NUL;
8681 stuffReadbuff(buf);
8682 }
8683 stuffcharReadbuff(c);
8684 return TRUE;
8685 }
8686 return FALSE;
8687}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688
8689/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008690 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008691 */
8692 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008693ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008694{
8695#ifdef FEAT_FKMAP
8696 if (p_fkmap && p_ri)
8697 {
8698 beep_flush();
8699 EMSG(farsi_text_3); /* encoded in Farsi */
8700 return;
8701 }
8702#endif
8703
8704#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008705# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008706 set_vim_var_string(VV_INSERTMODE,
8707 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008708# ifdef FEAT_VREPLACE
8709 replaceState == VREPLACE ? "v" :
8710# endif
8711 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008712# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008713 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8714#endif
8715 if (State & REPLACE_FLAG)
8716 State = INSERT | (State & LANGMAP);
8717 else
8718 State = replaceState | (State & LANGMAP);
8719 AppendCharToRedobuff(K_INS);
8720 showmode();
8721#ifdef CURSOR_SHAPE
8722 ui_cursor_shape(); /* may show different cursor shape */
8723#endif
8724}
8725
8726/*
8727 * Pressed CTRL-O in Insert mode.
8728 */
8729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008730ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008731{
8732#ifdef FEAT_VREPLACE
8733 if (State & VREPLACE_FLAG)
8734 restart_edit = 'V';
8735 else
8736#endif
8737 if (State & REPLACE_FLAG)
8738 restart_edit = 'R';
8739 else
8740 restart_edit = 'I';
8741#ifdef FEAT_VIRTUALEDIT
8742 if (virtual_active())
8743 ins_at_eol = FALSE; /* cursor always keeps its column */
8744 else
8745#endif
8746 ins_at_eol = (gchar_cursor() == NUL);
8747}
8748
8749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 * If the cursor is on an indent, ^T/^D insert/delete one
8751 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008752 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753 * with vi. But vi only supports ^T and ^D after an
8754 * autoindent, we support it everywhere.
8755 */
8756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008757ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758{
8759 if (stop_arrow() == FAIL)
8760 return;
8761 AppendCharToRedobuff(c);
8762
8763 /*
8764 * 0^D and ^^D: remove all indent.
8765 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008766 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8767 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 {
8769 --curwin->w_cursor.col;
8770 (void)del_char(FALSE); /* delete the '^' or '0' */
8771 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8772 if (State & REPLACE_FLAG)
8773 replace_pop_ins();
8774 if (lastc == '^')
8775 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008776 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 }
8778 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008779 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780
8781 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8782 did_ai = FALSE;
8783#ifdef FEAT_SMARTINDENT
8784 did_si = FALSE;
8785 can_si = FALSE;
8786 can_si_back = FALSE;
8787#endif
8788#ifdef FEAT_CINDENT
8789 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8790#endif
8791}
8792
8793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008794ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795{
8796 int temp;
8797
8798 if (stop_arrow() == FAIL)
8799 return;
8800 if (gchar_cursor() == NUL) /* delete newline */
8801 {
8802 temp = curwin->w_cursor.col;
8803 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008804 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008805 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806 else
8807 curwin->w_cursor.col = temp;
8808 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008809 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8810 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 did_ai = FALSE;
8812#ifdef FEAT_SMARTINDENT
8813 did_si = FALSE;
8814 can_si = FALSE;
8815 can_si_back = FALSE;
8816#endif
8817 AppendCharToRedobuff(K_DEL);
8818}
8819
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008820static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008821
8822/*
8823 * Delete one character for ins_bs().
8824 */
8825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008826ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008827{
8828 dec_cursor();
8829 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8830 if (State & REPLACE_FLAG)
8831 {
8832 /* Don't delete characters before the insert point when in
8833 * Replace mode */
8834 if (curwin->w_cursor.lnum != Insstart.lnum
8835 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008836 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008837 }
8838 else
8839 (void)del_char(FALSE);
8840}
8841
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842/*
8843 * Handle Backspace, delete-word and delete-line in Insert mode.
8844 * Return TRUE when backspace was actually used.
8845 */
8846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008847ins_bs(
8848 int c,
8849 int mode,
8850 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851{
8852 linenr_T lnum;
8853 int cc;
8854 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008855 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 colnr_T mincol;
8857 int did_backspace = FALSE;
8858 int in_indent;
8859 int oldState;
8860#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008861 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862#endif
8863
8864 /*
8865 * can't delete anything in an empty file
8866 * can't backup past first character in buffer
8867 * can't backup past starting point unless 'backspace' > 1
8868 * can backup to a previous line if 'backspace' == 0
8869 */
8870 if ( bufempty()
8871 || (
8872#ifdef FEAT_RIGHTLEFT
8873 !revins_on &&
8874#endif
8875 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8876 || (!can_bs(BS_START)
8877 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008878 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8879 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8881 && curwin->w_cursor.col <= ai_col)
8882 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8883 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008884 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 return FALSE;
8886 }
8887
8888 if (stop_arrow() == FAIL)
8889 return FALSE;
8890 in_indent = inindent(0);
8891#ifdef FEAT_CINDENT
8892 if (in_indent)
8893 can_cindent = FALSE;
8894#endif
8895#ifdef FEAT_COMMENTS
8896 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8897#endif
8898#ifdef FEAT_RIGHTLEFT
8899 if (revins_on) /* put cursor after last inserted char */
8900 inc_cursor();
8901#endif
8902
8903#ifdef FEAT_VIRTUALEDIT
8904 /* Virtualedit:
8905 * BACKSPACE_CHAR eats a virtual space
8906 * BACKSPACE_WORD eats all coladd
8907 * BACKSPACE_LINE eats all coladd and keeps going
8908 */
8909 if (curwin->w_cursor.coladd > 0)
8910 {
8911 if (mode == BACKSPACE_CHAR)
8912 {
8913 --curwin->w_cursor.coladd;
8914 return TRUE;
8915 }
8916 if (mode == BACKSPACE_WORD)
8917 {
8918 curwin->w_cursor.coladd = 0;
8919 return TRUE;
8920 }
8921 curwin->w_cursor.coladd = 0;
8922 }
8923#endif
8924
8925 /*
8926 * delete newline!
8927 */
8928 if (curwin->w_cursor.col == 0)
8929 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008930 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008931 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932#ifdef FEAT_RIGHTLEFT
8933 || revins_on
8934#endif
8935 )
8936 {
8937 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8938 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8939 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008940 --Insstart.lnum;
8941 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 }
8943 /*
8944 * In replace mode:
8945 * cc < 0: NL was inserted, delete it
8946 * cc >= 0: NL was replaced, put original characters back
8947 */
8948 cc = -1;
8949 if (State & REPLACE_FLAG)
8950 cc = replace_pop(); /* returns -1 if NL was inserted */
8951 /*
8952 * In replace mode, in the line we started replacing, we only move the
8953 * cursor.
8954 */
8955 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8956 {
8957 dec_cursor();
8958 }
8959 else
8960 {
8961#ifdef FEAT_VREPLACE
8962 if (!(State & VREPLACE_FLAG)
8963 || curwin->w_cursor.lnum > orig_line_count)
8964#endif
8965 {
8966 temp = gchar_cursor(); /* remember current char */
8967 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008968
8969 /* When "aw" is in 'formatoptions' we must delete the space at
8970 * the end of the line, otherwise the line will be broken
8971 * again when auto-formatting. */
8972 if (has_format_option(FO_AUTO)
8973 && has_format_option(FO_WHITE_PAR))
8974 {
8975 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8976 TRUE);
8977 int len;
8978
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008979 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008980 if (len > 0 && ptr[len - 1] == ' ')
8981 ptr[len - 1] = NUL;
8982 }
8983
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008984 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 if (temp == NUL && gchar_cursor() != NUL)
8986 inc_cursor();
8987 }
8988#ifdef FEAT_VREPLACE
8989 else
8990 dec_cursor();
8991#endif
8992
8993 /*
8994 * In REPLACE mode we have to put back the text that was replaced
8995 * by the NL. On the replace stack is first a NUL-terminated
8996 * sequence of characters that were deleted and then the
8997 * characters that NL replaced.
8998 */
8999 if (State & REPLACE_FLAG)
9000 {
9001 /*
9002 * Do the next ins_char() in NORMAL state, to
9003 * prevent ins_char() from replacing characters and
9004 * avoiding showmatch().
9005 */
9006 oldState = State;
9007 State = NORMAL;
9008 /*
9009 * restore characters (blanks) deleted after cursor
9010 */
9011 while (cc > 0)
9012 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009013 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009014#ifdef FEAT_MBYTE
9015 mb_replace_pop_ins(cc);
9016#else
9017 ins_char(cc);
9018#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009019 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009020 cc = replace_pop();
9021 }
9022 /* restore the characters that NL replaced */
9023 replace_pop_ins();
9024 State = oldState;
9025 }
9026 }
9027 did_ai = FALSE;
9028 }
9029 else
9030 {
9031 /*
9032 * Delete character(s) before the cursor.
9033 */
9034#ifdef FEAT_RIGHTLEFT
9035 if (revins_on) /* put cursor on last inserted char */
9036 dec_cursor();
9037#endif
9038 mincol = 0;
9039 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009040 if (mode == BACKSPACE_LINE
9041 && (curbuf->b_p_ai
9042#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009043 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009044#endif
9045 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046#ifdef FEAT_RIGHTLEFT
9047 && !revins_on
9048#endif
9049 )
9050 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009051 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009053 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009054 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009055 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 }
9057
9058 /*
9059 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9060 */
9061 if ( mode == BACKSPACE_CHAR
9062 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009063 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009064 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 && (*(ml_get_cursor() - 1) == TAB
9066 || (*(ml_get_cursor() - 1) == ' '
9067 && (!*inserted_space_p
9068 || arrow_used))))))
9069 {
9070 int ts;
9071 colnr_T vcol;
9072 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009073 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074
9075 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009076 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009077 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009079 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080 /* Compute the virtual column where we want to be. Since
9081 * 'showbreak' may get in the way, need to get the last column of
9082 * the previous character. */
9083 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009084 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085 dec_cursor();
9086 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9087 inc_cursor();
9088 want_vcol = (want_vcol / ts) * ts;
9089
9090 /* delete characters until we are at or before want_vcol */
9091 while (vcol > want_vcol
9092 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009093 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094
9095 /* insert extra spaces until we are at want_vcol */
9096 while (vcol < want_vcol)
9097 {
9098 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009099 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9100 && curwin->w_cursor.col < Insstart_orig.col)
9101 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102
9103#ifdef FEAT_VREPLACE
9104 if (State & VREPLACE_FLAG)
9105 ins_char(' ');
9106 else
9107#endif
9108 {
9109 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009110 if ((State & REPLACE_FLAG))
9111 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 }
9113 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9114 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009115
9116 /* If we are now back where we started delete one character. Can
9117 * happen when using 'sts' and 'linebreak'. */
9118 if (vcol >= start_vcol)
9119 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 }
9121
9122 /*
9123 * Delete upto starting point, start of line or previous word.
9124 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009125 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009127#ifdef FEAT_MBYTE
9128 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009130 if (has_mbyte)
9131 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009133 do
9134 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009136 if (!revins_on) /* put cursor on char to be deleted */
9137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009139
9140 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009142 /* look multi-byte character class */
9143 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009145 prev_cclass = cclass;
9146 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009149
9150 /* start of word? */
9151 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9152 {
9153 mode = BACKSPACE_WORD_NOT_SPACE;
9154 temp = vim_iswordc(cc);
9155 }
9156 /* end of word? */
9157 else if (mode == BACKSPACE_WORD_NOT_SPACE
9158 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9159#ifdef FEAT_MBYTE
9160 || prev_cclass != cclass
9161#endif
9162 ))
9163 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009164#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009165 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009166#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009167 inc_cursor();
9168#ifdef FEAT_RIGHTLEFT
9169 else if (State & REPLACE_FLAG)
9170 dec_cursor();
9171#endif
9172 break;
9173 }
9174 if (State & REPLACE_FLAG)
9175 replace_do_bs(-1);
9176 else
9177 {
9178#ifdef FEAT_MBYTE
9179 if (enc_utf8 && p_deco)
9180 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9181#endif
9182 (void)del_char(FALSE);
9183#ifdef FEAT_MBYTE
9184 /*
9185 * If there are combining characters and 'delcombine' is set
9186 * move the cursor back. Don't back up before the base
9187 * character.
9188 */
9189 if (enc_utf8 && p_deco && cpc[0] != NUL)
9190 inc_cursor();
9191#endif
9192#ifdef FEAT_RIGHTLEFT
9193 if (revins_chars)
9194 {
9195 revins_chars--;
9196 revins_legal++;
9197 }
9198 if (revins_on && gchar_cursor() == NUL)
9199 break;
9200#endif
9201 }
9202 /* Just a single backspace?: */
9203 if (mode == BACKSPACE_CHAR)
9204 break;
9205 } while (
9206#ifdef FEAT_RIGHTLEFT
9207 revins_on ||
9208#endif
9209 (curwin->w_cursor.col > mincol
9210 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9211 || curwin->w_cursor.col != Insstart_orig.col)));
9212 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009213 did_backspace = TRUE;
9214 }
9215#ifdef FEAT_SMARTINDENT
9216 did_si = FALSE;
9217 can_si = FALSE;
9218 can_si_back = FALSE;
9219#endif
9220 if (curwin->w_cursor.col <= 1)
9221 did_ai = FALSE;
9222 /*
9223 * It's a little strange to put backspaces into the redo
9224 * buffer, but it makes auto-indent a lot easier to deal
9225 * with.
9226 */
9227 AppendCharToRedobuff(c);
9228
9229 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009230 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9231 && curwin->w_cursor.col < Insstart_orig.col)
9232 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233
9234 /* vi behaviour: the cursor moves backward but the character that
9235 * was there remains visible
9236 * Vim behaviour: the cursor moves backward and the character that
9237 * was there is erased from the screen.
9238 * We can emulate the vi behaviour by pretending there is a dollar
9239 * displayed even when there isn't.
9240 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009241 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242 dollar_vcol = curwin->w_virtcol;
9243
Bram Moolenaarce3be472008-01-14 19:12:28 +00009244#ifdef FEAT_FOLDING
9245 /* When deleting a char the cursor line must never be in a closed fold.
9246 * E.g., when 'foldmethod' is indent and deleting the first non-white
9247 * char before a Tab. */
9248 if (did_backspace)
9249 foldOpenCursor();
9250#endif
9251
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252 return did_backspace;
9253}
9254
9255#ifdef FEAT_MOUSE
9256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009257ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009258{
9259 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009260 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261
9262# ifdef FEAT_GUI
9263 /* When GUI is active, also move/paste when 'mouse' is empty */
9264 if (!gui.in_use)
9265# endif
9266 if (!mouse_has(MOUSE_INSERT))
9267 return;
9268
9269 undisplay_dollar();
9270 tpos = curwin->w_cursor;
9271 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9272 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009273#ifdef FEAT_WINDOWS
9274 win_T *new_curwin = curwin;
9275
9276 if (curwin != old_curwin && win_valid(old_curwin))
9277 {
9278 /* Mouse took us to another window. We need to go back to the
9279 * previous one to stop insert there properly. */
9280 curwin = old_curwin;
9281 curbuf = curwin->w_buffer;
9282 }
9283#endif
9284 start_arrow(curwin == old_curwin ? &tpos : NULL);
9285#ifdef FEAT_WINDOWS
9286 if (curwin != new_curwin && win_valid(new_curwin))
9287 {
9288 curwin = new_curwin;
9289 curbuf = curwin->w_buffer;
9290 }
9291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292# ifdef FEAT_CINDENT
9293 can_cindent = TRUE;
9294# endif
9295 }
9296
9297#ifdef FEAT_WINDOWS
9298 /* redraw status lines (in case another window became active) */
9299 redraw_statuslines();
9300#endif
9301}
9302
9303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009304ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305{
9306 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009307# if defined(FEAT_WINDOWS)
9308 win_T *old_curwin = curwin;
9309# endif
9310# ifdef FEAT_INS_EXPAND
9311 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312# endif
9313
9314 tpos = curwin->w_cursor;
9315
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009316# ifdef FEAT_WINDOWS
9317 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 {
9319 int row, col;
9320
9321 row = mouse_row;
9322 col = mouse_col;
9323
9324 /* find the window at the pointer coordinates */
9325 curwin = mouse_find_win(&row, &col);
9326 curbuf = curwin->w_buffer;
9327 }
9328 if (curwin == old_curwin)
9329# endif
9330 undisplay_dollar();
9331
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009332# ifdef FEAT_INS_EXPAND
9333 /* Don't scroll the window in which completion is being done. */
9334 if (!pum_visible()
9335# if defined(FEAT_WINDOWS)
9336 || curwin != old_curwin
9337# endif
9338 )
9339# endif
9340 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009341 if (dir == MSCR_DOWN || dir == MSCR_UP)
9342 {
9343 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9344 scroll_redraw(dir,
9345 (long)(curwin->w_botline - curwin->w_topline));
9346 else
9347 scroll_redraw(dir, 3L);
9348 }
9349#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009350 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009351 {
9352 int val, step = 6;
9353
9354 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9355 step = W_WIDTH(curwin);
9356 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9357 if (val < 0)
9358 val = 0;
9359 gui_do_horiz_scroll(val, TRUE);
9360 }
9361#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009362# ifdef FEAT_INS_EXPAND
9363 did_scroll = TRUE;
9364# endif
9365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009366
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009367# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 curwin->w_redr_status = TRUE;
9369
9370 curwin = old_curwin;
9371 curbuf = curwin->w_buffer;
9372# endif
9373
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009374# ifdef FEAT_INS_EXPAND
9375 /* The popup menu may overlay the window, need to redraw it.
9376 * TODO: Would be more efficient to only redraw the windows that are
9377 * overlapped by the popup menu. */
9378 if (pum_visible() && did_scroll)
9379 {
9380 redraw_all_later(NOT_VALID);
9381 ins_compl_show_pum();
9382 }
9383# endif
9384
Bram Moolenaar071d4272004-06-13 20:20:40 +00009385 if (!equalpos(curwin->w_cursor, tpos))
9386 {
9387 start_arrow(&tpos);
9388# ifdef FEAT_CINDENT
9389 can_cindent = TRUE;
9390# endif
9391 }
9392}
9393#endif
9394
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009395#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009397ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009398{
9399 /* We will be leaving the current window, unless closing another tab. */
9400 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9401 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9402 {
9403 undisplay_dollar();
9404 start_arrow(&curwin->w_cursor);
9405# ifdef FEAT_CINDENT
9406 can_cindent = TRUE;
9407# endif
9408 }
9409
9410 if (c == K_TABLINE)
9411 goto_tabpage(current_tab);
9412 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009413 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009414 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009415 redraw_statuslines(); /* will redraw the tabline when needed */
9416 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009417}
9418#endif
9419
9420#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009421 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009422ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423{
9424 pos_T tpos;
9425
9426 undisplay_dollar();
9427 tpos = curwin->w_cursor;
9428 if (gui_do_scroll())
9429 {
9430 start_arrow(&tpos);
9431# ifdef FEAT_CINDENT
9432 can_cindent = TRUE;
9433# endif
9434 }
9435}
9436
9437 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009438ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439{
9440 pos_T tpos;
9441
9442 undisplay_dollar();
9443 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009444 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009445 {
9446 start_arrow(&tpos);
9447# ifdef FEAT_CINDENT
9448 can_cindent = TRUE;
9449# endif
9450 }
9451}
9452#endif
9453
9454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009455ins_left(
9456 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009457{
9458 pos_T tpos;
9459
9460#ifdef FEAT_FOLDING
9461 if ((fdo_flags & FDO_HOR) && KeyTyped)
9462 foldOpenCursor();
9463#endif
9464 undisplay_dollar();
9465 tpos = curwin->w_cursor;
9466 if (oneleft() == OK)
9467 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009468#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9469 /* Only call start_arrow() when not busy with preediting, it will
9470 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9471 if (!im_is_preediting())
9472#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009473 {
9474 start_arrow_with_change(&tpos, end_change);
9475 if (!end_change)
9476 AppendCharToRedobuff(K_LEFT);
9477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009478#ifdef FEAT_RIGHTLEFT
9479 /* If exit reversed string, position is fixed */
9480 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9481 revins_legal++;
9482 revins_chars++;
9483#endif
9484 }
9485
9486 /*
9487 * if 'whichwrap' set for cursor in insert mode may go to
9488 * previous line
9489 */
9490 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9491 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009492 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009493 start_arrow(&tpos);
9494 --(curwin->w_cursor.lnum);
9495 coladvance((colnr_T)MAXCOL);
9496 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9497 }
9498 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009499 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009500 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009501}
9502
9503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009504ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009505{
9506 pos_T tpos;
9507
9508#ifdef FEAT_FOLDING
9509 if ((fdo_flags & FDO_HOR) && KeyTyped)
9510 foldOpenCursor();
9511#endif
9512 undisplay_dollar();
9513 tpos = curwin->w_cursor;
9514 if (c == K_C_HOME)
9515 curwin->w_cursor.lnum = 1;
9516 curwin->w_cursor.col = 0;
9517#ifdef FEAT_VIRTUALEDIT
9518 curwin->w_cursor.coladd = 0;
9519#endif
9520 curwin->w_curswant = 0;
9521 start_arrow(&tpos);
9522}
9523
9524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009525ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526{
9527 pos_T tpos;
9528
9529#ifdef FEAT_FOLDING
9530 if ((fdo_flags & FDO_HOR) && KeyTyped)
9531 foldOpenCursor();
9532#endif
9533 undisplay_dollar();
9534 tpos = curwin->w_cursor;
9535 if (c == K_C_END)
9536 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9537 coladvance((colnr_T)MAXCOL);
9538 curwin->w_curswant = MAXCOL;
9539
9540 start_arrow(&tpos);
9541}
9542
9543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009544ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546#ifdef FEAT_FOLDING
9547 if ((fdo_flags & FDO_HOR) && KeyTyped)
9548 foldOpenCursor();
9549#endif
9550 undisplay_dollar();
9551 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9552 {
9553 start_arrow(&curwin->w_cursor);
9554 (void)bck_word(1L, FALSE, FALSE);
9555 curwin->w_set_curswant = TRUE;
9556 }
9557 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009558 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559}
9560
9561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009562ins_right(
9563 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565#ifdef FEAT_FOLDING
9566 if ((fdo_flags & FDO_HOR) && KeyTyped)
9567 foldOpenCursor();
9568#endif
9569 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009570 if (gchar_cursor() != NUL
9571#ifdef FEAT_VIRTUALEDIT
9572 || virtual_active()
9573#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009574 )
9575 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009576 start_arrow_with_change(&curwin->w_cursor, end_change);
9577 if (!end_change)
9578 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009579 curwin->w_set_curswant = TRUE;
9580#ifdef FEAT_VIRTUALEDIT
9581 if (virtual_active())
9582 oneright();
9583 else
9584#endif
9585 {
9586#ifdef FEAT_MBYTE
9587 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009588 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 else
9590#endif
9591 ++curwin->w_cursor.col;
9592 }
9593
9594#ifdef FEAT_RIGHTLEFT
9595 revins_legal++;
9596 if (revins_chars)
9597 revins_chars--;
9598#endif
9599 }
9600 /* if 'whichwrap' set for cursor in insert mode, may move the
9601 * cursor to the next line */
9602 else if (vim_strchr(p_ww, ']') != NULL
9603 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9604 {
9605 start_arrow(&curwin->w_cursor);
9606 curwin->w_set_curswant = TRUE;
9607 ++curwin->w_cursor.lnum;
9608 curwin->w_cursor.col = 0;
9609 }
9610 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009611 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009612 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009613}
9614
9615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009616ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617{
9618#ifdef FEAT_FOLDING
9619 if ((fdo_flags & FDO_HOR) && KeyTyped)
9620 foldOpenCursor();
9621#endif
9622 undisplay_dollar();
9623 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9624 || gchar_cursor() != NUL)
9625 {
9626 start_arrow(&curwin->w_cursor);
9627 (void)fwd_word(1L, FALSE, 0);
9628 curwin->w_set_curswant = TRUE;
9629 }
9630 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009631 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632}
9633
9634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009635ins_up(
9636 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637{
9638 pos_T tpos;
9639 linenr_T old_topline = curwin->w_topline;
9640#ifdef FEAT_DIFF
9641 int old_topfill = curwin->w_topfill;
9642#endif
9643
9644 undisplay_dollar();
9645 tpos = curwin->w_cursor;
9646 if (cursor_up(1L, TRUE) == OK)
9647 {
9648 if (startcol)
9649 coladvance(getvcol_nolist(&Insstart));
9650 if (old_topline != curwin->w_topline
9651#ifdef FEAT_DIFF
9652 || old_topfill != curwin->w_topfill
9653#endif
9654 )
9655 redraw_later(VALID);
9656 start_arrow(&tpos);
9657#ifdef FEAT_CINDENT
9658 can_cindent = TRUE;
9659#endif
9660 }
9661 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009662 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663}
9664
9665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009666ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
9668 pos_T tpos;
9669
9670 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009671
9672#ifdef FEAT_WINDOWS
9673 if (mod_mask & MOD_MASK_CTRL)
9674 {
9675 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009676 if (first_tabpage->tp_next != NULL)
9677 {
9678 start_arrow(&curwin->w_cursor);
9679 goto_tabpage(-1);
9680 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009681 return;
9682 }
9683#endif
9684
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 tpos = curwin->w_cursor;
9686 if (onepage(BACKWARD, 1L) == OK)
9687 {
9688 start_arrow(&tpos);
9689#ifdef FEAT_CINDENT
9690 can_cindent = TRUE;
9691#endif
9692 }
9693 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009694 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695}
9696
9697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009698ins_down(
9699 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700{
9701 pos_T tpos;
9702 linenr_T old_topline = curwin->w_topline;
9703#ifdef FEAT_DIFF
9704 int old_topfill = curwin->w_topfill;
9705#endif
9706
9707 undisplay_dollar();
9708 tpos = curwin->w_cursor;
9709 if (cursor_down(1L, TRUE) == OK)
9710 {
9711 if (startcol)
9712 coladvance(getvcol_nolist(&Insstart));
9713 if (old_topline != curwin->w_topline
9714#ifdef FEAT_DIFF
9715 || old_topfill != curwin->w_topfill
9716#endif
9717 )
9718 redraw_later(VALID);
9719 start_arrow(&tpos);
9720#ifdef FEAT_CINDENT
9721 can_cindent = TRUE;
9722#endif
9723 }
9724 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009725 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726}
9727
9728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009729ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730{
9731 pos_T tpos;
9732
9733 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009734
9735#ifdef FEAT_WINDOWS
9736 if (mod_mask & MOD_MASK_CTRL)
9737 {
9738 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009739 if (first_tabpage->tp_next != NULL)
9740 {
9741 start_arrow(&curwin->w_cursor);
9742 goto_tabpage(0);
9743 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009744 return;
9745 }
9746#endif
9747
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 tpos = curwin->w_cursor;
9749 if (onepage(FORWARD, 1L) == OK)
9750 {
9751 start_arrow(&tpos);
9752#ifdef FEAT_CINDENT
9753 can_cindent = TRUE;
9754#endif
9755 }
9756 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009757 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758}
9759
9760#ifdef FEAT_DND
9761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009762ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763{
9764 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9765}
9766#endif
9767
9768/*
9769 * Handle TAB in Insert or Replace mode.
9770 * Return TRUE when the TAB needs to be inserted like a normal character.
9771 */
9772 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009773ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774{
9775 int ind;
9776 int i;
9777 int temp;
9778
9779 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9780 Insstart_blank_vcol = get_nolist_virtcol();
9781 if (echeck_abbr(TAB + ABBR_OFF))
9782 return FALSE;
9783
9784 ind = inindent(0);
9785#ifdef FEAT_CINDENT
9786 if (ind)
9787 can_cindent = FALSE;
9788#endif
9789
9790 /*
9791 * When nothing special, insert TAB like a normal character
9792 */
9793 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009794 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009795 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 return TRUE;
9797
9798 if (stop_arrow() == FAIL)
9799 return TRUE;
9800
9801 did_ai = FALSE;
9802#ifdef FEAT_SMARTINDENT
9803 did_si = FALSE;
9804 can_si = FALSE;
9805 can_si_back = FALSE;
9806#endif
9807 AppendToRedobuff((char_u *)"\t");
9808
9809 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009810 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009811 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9812 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 else /* otherwise use 'tabstop' */
9814 temp = (int)curbuf->b_p_ts;
9815 temp -= get_nolist_virtcol() % temp;
9816
9817 /*
9818 * Insert the first space with ins_char(). It will delete one char in
9819 * replace mode. Insert the rest with ins_str(); it will not delete any
9820 * chars. For VREPLACE mode, we use ins_char() for all characters.
9821 */
9822 ins_char(' ');
9823 while (--temp > 0)
9824 {
9825#ifdef FEAT_VREPLACE
9826 if (State & VREPLACE_FLAG)
9827 ins_char(' ');
9828 else
9829#endif
9830 {
9831 ins_str((char_u *)" ");
9832 if (State & REPLACE_FLAG) /* no char replaced */
9833 replace_push(NUL);
9834 }
9835 }
9836
9837 /*
9838 * When 'expandtab' not set: Replace spaces by TABs where possible.
9839 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009840 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 {
9842 char_u *ptr;
9843#ifdef FEAT_VREPLACE
9844 char_u *saved_line = NULL; /* init for GCC */
9845 pos_T pos;
9846#endif
9847 pos_T fpos;
9848 pos_T *cursor;
9849 colnr_T want_vcol, vcol;
9850 int change_col = -1;
9851 int save_list = curwin->w_p_list;
9852
9853 /*
9854 * Get the current line. For VREPLACE mode, don't make real changes
9855 * yet, just work on a copy of the line.
9856 */
9857#ifdef FEAT_VREPLACE
9858 if (State & VREPLACE_FLAG)
9859 {
9860 pos = curwin->w_cursor;
9861 cursor = &pos;
9862 saved_line = vim_strsave(ml_get_curline());
9863 if (saved_line == NULL)
9864 return FALSE;
9865 ptr = saved_line + pos.col;
9866 }
9867 else
9868#endif
9869 {
9870 ptr = ml_get_cursor();
9871 cursor = &curwin->w_cursor;
9872 }
9873
9874 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9875 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9876 curwin->w_p_list = FALSE;
9877
9878 /* Find first white before the cursor */
9879 fpos = curwin->w_cursor;
9880 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9881 {
9882 --fpos.col;
9883 --ptr;
9884 }
9885
9886 /* In Replace mode, don't change characters before the insert point. */
9887 if ((State & REPLACE_FLAG)
9888 && fpos.lnum == Insstart.lnum
9889 && fpos.col < Insstart.col)
9890 {
9891 ptr += Insstart.col - fpos.col;
9892 fpos.col = Insstart.col;
9893 }
9894
9895 /* compute virtual column numbers of first white and cursor */
9896 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9897 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9898
Bram Moolenaar597a4222014-06-25 14:39:50 +02009899 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9900 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901 while (vim_iswhite(*ptr))
9902 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009903 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904 if (vcol + i > want_vcol)
9905 break;
9906 if (*ptr != TAB)
9907 {
9908 *ptr = TAB;
9909 if (change_col < 0)
9910 {
9911 change_col = fpos.col; /* Column of first change */
9912 /* May have to adjust Insstart */
9913 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9914 Insstart.col = fpos.col;
9915 }
9916 }
9917 ++fpos.col;
9918 ++ptr;
9919 vcol += i;
9920 }
9921
9922 if (change_col >= 0)
9923 {
9924 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009925 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926
9927 /* Skip over the spaces we need. */
9928 while (vcol < want_vcol && *ptr == ' ')
9929 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009930 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009931 ++ptr;
9932 ++repl_off;
9933 }
9934 if (vcol > want_vcol)
9935 {
9936 /* Must have a char with 'showbreak' just before it. */
9937 --ptr;
9938 --repl_off;
9939 }
9940 fpos.col += repl_off;
9941
9942 /* Delete following spaces. */
9943 i = cursor->col - fpos.col;
9944 if (i > 0)
9945 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009946 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 /* correct replace stack. */
9948 if ((State & REPLACE_FLAG)
9949#ifdef FEAT_VREPLACE
9950 && !(State & VREPLACE_FLAG)
9951#endif
9952 )
9953 for (temp = i; --temp >= 0; )
9954 replace_join(repl_off);
9955 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009956#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009957 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009958 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009959 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009960 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9961 (char_u *)"\t", 1);
9962 }
9963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 cursor->col -= i;
9965
9966#ifdef FEAT_VREPLACE
9967 /*
9968 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9969 * backspacing over the changed spacing and then inserting the new
9970 * spacing.
9971 */
9972 if (State & VREPLACE_FLAG)
9973 {
9974 /* Backspace from real cursor to change_col */
9975 backspace_until_column(change_col);
9976
9977 /* Insert each char in saved_line from changed_col to
9978 * ptr-cursor */
9979 ins_bytes_len(saved_line + change_col,
9980 cursor->col - change_col);
9981 }
9982#endif
9983 }
9984
9985#ifdef FEAT_VREPLACE
9986 if (State & VREPLACE_FLAG)
9987 vim_free(saved_line);
9988#endif
9989 curwin->w_p_list = save_list;
9990 }
9991
9992 return FALSE;
9993}
9994
9995/*
9996 * Handle CR or NL in insert mode.
9997 * Return TRUE when out of memory or can't undo.
9998 */
9999 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010000ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001{
10002 int i;
10003
10004 if (echeck_abbr(c + ABBR_OFF))
10005 return FALSE;
10006 if (stop_arrow() == FAIL)
10007 return TRUE;
10008 undisplay_dollar();
10009
10010 /*
10011 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10012 * character under the cursor. Only push a NUL on the replace stack,
10013 * nothing to put back when the NL is deleted.
10014 */
10015 if ((State & REPLACE_FLAG)
10016#ifdef FEAT_VREPLACE
10017 && !(State & VREPLACE_FLAG)
10018#endif
10019 )
10020 replace_push(NUL);
10021
10022 /*
10023 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10024 * replacing the next line, so we push all of the characters left on the
10025 * line onto the replace stack. This is not done here though, it is done
10026 * in open_line().
10027 */
10028
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010029#ifdef FEAT_VIRTUALEDIT
10030 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10031 * CTRL-O). */
10032 if (virtual_active() && curwin->w_cursor.coladd > 0)
10033 coladvance(getviscol());
10034#endif
10035
Bram Moolenaar071d4272004-06-13 20:20:40 +000010036#ifdef FEAT_RIGHTLEFT
10037# ifdef FEAT_FKMAP
10038 if (p_altkeymap && p_fkmap)
10039 fkmap(NL);
10040# endif
10041 /* NL in reverse insert will always start in the end of
10042 * current line. */
10043 if (revins_on)
10044 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10045#endif
10046
10047 AppendToRedobuff(NL_STR);
10048 i = open_line(FORWARD,
10049#ifdef FEAT_COMMENTS
10050 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10051#endif
10052 0, old_indent);
10053 old_indent = 0;
10054#ifdef FEAT_CINDENT
10055 can_cindent = TRUE;
10056#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010057#ifdef FEAT_FOLDING
10058 /* When inserting a line the cursor line must never be in a closed fold. */
10059 foldOpenCursor();
10060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061
10062 return (!i);
10063}
10064
10065#ifdef FEAT_DIGRAPHS
10066/*
10067 * Handle digraph in insert mode.
10068 * Returns character still to be inserted, or NUL when nothing remaining to be
10069 * done.
10070 */
10071 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010072ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010073{
10074 int c;
10075 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010076 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010077
10078 pc_status = PC_STATUS_UNSET;
10079 if (redrawing() && !char_avail())
10080 {
10081 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010082 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083
10084 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010085 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086#ifdef FEAT_CMDL_INFO
10087 add_to_showcmd_c(Ctrl_K);
10088#endif
10089 }
10090
10091#ifdef USE_ON_FLY_SCROLL
10092 dont_scroll = TRUE; /* disallow scrolling here */
10093#endif
10094
10095 /* don't map the digraph chars. This also prevents the
10096 * mode message to be deleted when ESC is hit */
10097 ++no_mapping;
10098 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010099 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010100 --no_mapping;
10101 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010102 if (did_putchar)
10103 /* when the line fits in 'columns' the '?' is at the start of the next
10104 * line and will not be removed by the redraw */
10105 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010106
Bram Moolenaar071d4272004-06-13 20:20:40 +000010107 if (IS_SPECIAL(c) || mod_mask) /* special key */
10108 {
10109#ifdef FEAT_CMDL_INFO
10110 clear_showcmd();
10111#endif
10112 insert_special(c, TRUE, FALSE);
10113 return NUL;
10114 }
10115 if (c != ESC)
10116 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010117 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010118 if (redrawing() && !char_avail())
10119 {
10120 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010121 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122
10123 if (char2cells(c) == 1)
10124 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010125 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010126 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010127 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010128 }
10129#ifdef FEAT_CMDL_INFO
10130 add_to_showcmd_c(c);
10131#endif
10132 }
10133 ++no_mapping;
10134 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010135 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010136 --no_mapping;
10137 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010138 if (did_putchar)
10139 /* when the line fits in 'columns' the '?' is at the start of the
10140 * next line and will not be removed by a redraw */
10141 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142 if (cc != ESC)
10143 {
10144 AppendToRedobuff((char_u *)CTRL_V_STR);
10145 c = getdigraph(c, cc, TRUE);
10146#ifdef FEAT_CMDL_INFO
10147 clear_showcmd();
10148#endif
10149 return c;
10150 }
10151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010152#ifdef FEAT_CMDL_INFO
10153 clear_showcmd();
10154#endif
10155 return NUL;
10156}
10157#endif
10158
10159/*
10160 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10161 * Returns the char to be inserted, or NUL if none found.
10162 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010163 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010164ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010165{
10166 int c;
10167 int temp;
10168 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010169 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010170
10171 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10172 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010173 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010174 return NUL;
10175 }
10176
10177 /* try to advance to the cursor column */
10178 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010179 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010180 prev_ptr = ptr;
10181 validate_virtcol();
10182 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10183 {
10184 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010185 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 }
10187 if ((colnr_T)temp > curwin->w_virtcol)
10188 ptr = prev_ptr;
10189
10190#ifdef FEAT_MBYTE
10191 c = (*mb_ptr2char)(ptr);
10192#else
10193 c = *ptr;
10194#endif
10195 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010196 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197 return c;
10198}
10199
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010200/*
10201 * CTRL-Y or CTRL-E typed in Insert mode.
10202 */
10203 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010204ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010205{
10206 int c = tc;
10207
10208#ifdef FEAT_INS_EXPAND
10209 if (ctrl_x_mode == CTRL_X_SCROLL)
10210 {
10211 if (c == Ctrl_Y)
10212 scrolldown_clamp();
10213 else
10214 scrollup_clamp();
10215 redraw_later(VALID);
10216 }
10217 else
10218#endif
10219 {
10220 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10221 if (c != NUL)
10222 {
10223 long tw_save;
10224
10225 /* The character must be taken literally, insert like it
10226 * was typed after a CTRL-V, and pretend 'textwidth'
10227 * wasn't set. Digits, 'o' and 'x' are special after a
10228 * CTRL-V, don't use it for these. */
10229 if (c < 256 && !isalnum(c))
10230 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10231 tw_save = curbuf->b_p_tw;
10232 curbuf->b_p_tw = -1;
10233 insert_special(c, TRUE, FALSE);
10234 curbuf->b_p_tw = tw_save;
10235#ifdef FEAT_RIGHTLEFT
10236 revins_chars++;
10237 revins_legal++;
10238#endif
10239 c = Ctrl_V; /* pretend CTRL-V is last character */
10240 auto_format(FALSE, TRUE);
10241 }
10242 }
10243 return c;
10244}
10245
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246#ifdef FEAT_SMARTINDENT
10247/*
10248 * Try to do some very smart auto-indenting.
10249 * Used when inserting a "normal" character.
10250 */
10251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010252ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010253{
10254 pos_T *pos, old_pos;
10255 char_u *ptr;
10256 int i;
10257 int temp;
10258
10259 /*
10260 * do some very smart indenting when entering '{' or '}'
10261 */
10262 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10263 {
10264 /*
10265 * for '}' set indent equal to indent of line containing matching '{'
10266 */
10267 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10268 {
10269 old_pos = curwin->w_cursor;
10270 /*
10271 * If the matching '{' has a ')' immediately before it (ignoring
10272 * white-space), then line up with the start of the line
10273 * containing the matching '(' if there is one. This handles the
10274 * case where an "if (..\n..) {" statement continues over multiple
10275 * lines -- webb
10276 */
10277 ptr = ml_get(pos->lnum);
10278 i = pos->col;
10279 if (i > 0) /* skip blanks before '{' */
10280 while (--i > 0 && vim_iswhite(ptr[i]))
10281 ;
10282 curwin->w_cursor.lnum = pos->lnum;
10283 curwin->w_cursor.col = i;
10284 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10285 curwin->w_cursor = *pos;
10286 i = get_indent();
10287 curwin->w_cursor = old_pos;
10288#ifdef FEAT_VREPLACE
10289 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010290 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010291 else
10292#endif
10293 (void)set_indent(i, SIN_CHANGED);
10294 }
10295 else if (curwin->w_cursor.col > 0)
10296 {
10297 /*
10298 * when inserting '{' after "O" reduce indent, but not
10299 * more than indent of previous line
10300 */
10301 temp = TRUE;
10302 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10303 {
10304 old_pos = curwin->w_cursor;
10305 i = get_indent();
10306 while (curwin->w_cursor.lnum > 1)
10307 {
10308 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10309
10310 /* ignore empty lines and lines starting with '#'. */
10311 if (*ptr != '#' && *ptr != NUL)
10312 break;
10313 }
10314 if (get_indent() >= i)
10315 temp = FALSE;
10316 curwin->w_cursor = old_pos;
10317 }
10318 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010319 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010320 }
10321 }
10322
10323 /*
10324 * set indent of '#' always to 0
10325 */
10326 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10327 {
10328 /* remember current indent for next line */
10329 old_indent = get_indent();
10330 (void)set_indent(0, SIN_CHANGED);
10331 }
10332
10333 /* Adjust ai_col, the char at this position can be deleted. */
10334 if (ai_col > curwin->w_cursor.col)
10335 ai_col = curwin->w_cursor.col;
10336}
10337#endif
10338
10339/*
10340 * Get the value that w_virtcol would have when 'list' is off.
10341 * Unless 'cpo' contains the 'L' flag.
10342 */
10343 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010344get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345{
10346 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10347 return getvcol_nolist(&curwin->w_cursor);
10348 validate_virtcol();
10349 return curwin->w_virtcol;
10350}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010351
10352#ifdef FEAT_AUTOCMD
10353/*
10354 * Handle the InsertCharPre autocommand.
10355 * "c" is the character that was typed.
10356 * Return a pointer to allocated memory with the replacement string.
10357 * Return NULL to continue inserting "c".
10358 */
10359 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010360do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010361{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010362 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010363 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010364
10365 /* Return quickly when there is nothing to do. */
10366 if (!has_insertcharpre())
10367 return NULL;
10368
Bram Moolenaar704984a2012-06-01 14:57:51 +020010369#ifdef FEAT_MBYTE
10370 if (has_mbyte)
10371 buf[(*mb_char2bytes)(c, buf)] = NUL;
10372 else
10373#endif
10374 {
10375 buf[0] = c;
10376 buf[1] = NUL;
10377 }
10378
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010379 /* Lock the text to avoid weird things from happening. */
10380 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010381 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010382
Bram Moolenaar704984a2012-06-01 14:57:51 +020010383 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010384 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010385 {
10386 /* Get the value of v:char. It may be empty or more than one
10387 * character. Only use it when changed, otherwise continue with the
10388 * original character to avoid breaking autoindent. */
10389 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10390 res = vim_strsave(get_vim_var_str(VV_CHAR));
10391 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010392
10393 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10394 --textlock;
10395
10396 return res;
10397}
10398#endif