blob: 9e6cc3e8bf9eb61bc9a9c7bfc5cd8fe740db3f5f [file] [log] [blame]
Bram Moolenaaredf3f972016-08-29 22:49:24 +02001/* vi:set ts=8 sts=4 sw=4 noet:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002 *
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);
Bram Moolenaar472e8592016-10-15 17:06:47 +0200182static void ins_compl_insert(int in_compl_func);
183static int ins_compl_next(int allow_get_expansion, int count, int insert_match, int in_compl_func);
Bram Moolenaarf28dbce2016-01-29 22:03:47 +0100184static 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
Bram Moolenaarec2da362017-01-21 20:04:22 +0100312 * K_PS bracketed paste
Bram Moolenaar071d4272004-06-13 20:20:40 +0000313 * 'R' replace command
314 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
315 * but still only one <CR> is inserted. The <Esc> is not used for redo.
316 * 'g' "gI" command.
317 * 'V' "gR" command for Virtual Replace mode.
318 * 'v' "gr" command for single character Virtual Replace mode.
319 *
320 * This function is not called recursively. For CTRL-O commands, it returns
321 * and lets the caller handle the Normal-mode command.
322 *
323 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
324 */
325 int
Bram Moolenaar7454a062016-01-30 15:14:10 +0100326edit(
327 int cmdchar,
328 int startln, /* if set, insert at start of line */
329 long count)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330{
331 int c = 0;
332 char_u *ptr;
Bram Moolenaar418f81b2016-02-16 20:12:02 +0100333 int lastc = 0;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000334 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336 int i;
337 int did_backspace = TRUE; /* previous char was backspace */
338#ifdef FEAT_CINDENT
339 int line_is_white = FALSE; /* line is empty before insert */
340#endif
341 linenr_T old_topline = 0; /* topline before insertion */
342#ifdef FEAT_DIFF
343 int old_topfill = -1;
344#endif
345 int inserted_space = FALSE; /* just inserted a space */
346 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000347 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000348
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000349 /* Remember whether editing was restarted after CTRL-O. */
350 did_restart_edit = restart_edit;
351
Bram Moolenaar071d4272004-06-13 20:20:40 +0000352 /* sleep before redrawing, needed for "CTRL-O :" that results in an
353 * error message */
354 check_for_delay(TRUE);
355
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100356 /* set Insstart_orig to Insstart */
357 update_Insstart_orig = TRUE;
358
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359#ifdef HAVE_SANDBOX
360 /* Don't allow inserting in the sandbox. */
361 if (sandbox != 0)
362 {
363 EMSG(_(e_sandbox));
364 return FALSE;
365 }
366#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000367 /* Don't allow changes in the buffer while editing the cmdline. The
368 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000369 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000370 {
371 EMSG(_(e_secure));
372 return FALSE;
373 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000374
375#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000377 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000378 {
379 EMSG(_(e_secure));
380 return FALSE;
381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000382 ins_compl_clear(); /* clear stuff for CTRL-X mode */
383#endif
384
Bram Moolenaar843ee412004-06-30 16:16:41 +0000385#ifdef FEAT_AUTOCMD
386 /*
387 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
388 */
389 if (cmdchar != 'r' && cmdchar != 'v')
390 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100391 pos_T save_cursor = curwin->w_cursor;
392
Bram Moolenaar1e015462005-09-25 22:16:38 +0000393# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000394 if (cmdchar == 'R')
395 ptr = (char_u *)"r";
396 else if (cmdchar == 'V')
397 ptr = (char_u *)"v";
398 else
399 ptr = (char_u *)"i";
400 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200401 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000402# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000403 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100404
Bram Moolenaar097c9922013-05-19 21:15:15 +0200405 /* Make sure the cursor didn't move. Do call check_cursor_col() in
406 * case the text was modified. Since Insert mode was not started yet
407 * a call to check_cursor_col() may move the cursor, especially with
408 * the "A" command, thus set State to avoid that. Also check that the
409 * line number is still valid (lines may have been deleted).
410 * Do not restore if v:char was set to a non-empty string. */
411 if (!equalpos(curwin->w_cursor, save_cursor)
412# ifdef FEAT_EVAL
413 && *get_vim_var_str(VV_CHAR) == NUL
414# endif
415 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100416 {
417 int save_state = State;
418
419 curwin->w_cursor = save_cursor;
420 State = INSERT;
421 check_cursor_col();
422 State = save_state;
423 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000424 }
425#endif
426
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200427#ifdef FEAT_CONCEAL
428 /* Check if the cursor line needs redrawing before changing State. If
429 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200430 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200431#endif
432
Bram Moolenaar071d4272004-06-13 20:20:40 +0000433#ifdef FEAT_MOUSE
434 /*
435 * When doing a paste with the middle mouse button, Insstart is set to
436 * where the paste started.
437 */
438 if (where_paste_started.lnum != 0)
439 Insstart = where_paste_started;
440 else
441#endif
442 {
443 Insstart = curwin->w_cursor;
444 if (startln)
445 Insstart.col = 0;
446 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000447 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000448 Insstart_blank_vcol = MAXCOL;
449 if (!did_ai)
450 ai_col = 0;
451
452 if (cmdchar != NUL && restart_edit == 0)
453 {
454 ResetRedobuff();
455 AppendNumberToRedobuff(count);
456#ifdef FEAT_VREPLACE
457 if (cmdchar == 'V' || cmdchar == 'v')
458 {
459 /* "gR" or "gr" command */
460 AppendCharToRedobuff('g');
461 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
462 }
463 else
464#endif
465 {
Bram Moolenaar076e5022017-01-24 18:58:30 +0100466 if (cmdchar == K_PS)
467 AppendCharToRedobuff('a');
468 else
469 AppendCharToRedobuff(cmdchar);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000470 if (cmdchar == 'g') /* "gI" command */
471 AppendCharToRedobuff('I');
472 else if (cmdchar == 'r') /* "r<CR>" command */
473 count = 1; /* insert only one <CR> */
474 }
475 }
476
477 if (cmdchar == 'R')
478 {
479#ifdef FEAT_FKMAP
480 if (p_fkmap && p_ri)
481 {
482 beep_flush();
483 EMSG(farsi_text_3); /* encoded in Farsi */
484 State = INSERT;
485 }
486 else
487#endif
488 State = REPLACE;
489 }
490#ifdef FEAT_VREPLACE
491 else if (cmdchar == 'V' || cmdchar == 'v')
492 {
493 State = VREPLACE;
494 replaceState = VREPLACE;
495 orig_line_count = curbuf->b_ml.ml_line_count;
496 vr_lines_changed = 1;
497 }
498#endif
499 else
500 State = INSERT;
501
502 stop_insert_mode = FALSE;
503
504 /*
505 * Need to recompute the cursor position, it might move when the cursor is
506 * on a TAB or special character.
507 */
508 curs_columns(TRUE);
509
510 /*
511 * Enable langmap or IME, indicated by 'iminsert'.
512 * Note that IME may enabled/disabled without us noticing here, thus the
513 * 'iminsert' value may not reflect what is actually used. It is updated
514 * when hitting <Esc>.
515 */
516 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
517 State |= LANGMAP;
518#ifdef USE_IM_CONTROL
519 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
520#endif
521
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522#ifdef FEAT_MOUSE
523 setmouse();
524#endif
525#ifdef FEAT_CMDL_INFO
526 clear_showcmd();
527#endif
528#ifdef FEAT_RIGHTLEFT
529 /* there is no reverse replace mode */
530 revins_on = (State == INSERT && p_ri);
531 if (revins_on)
532 undisplay_dollar();
533 revins_chars = 0;
534 revins_legal = 0;
535 revins_scol = -1;
536#endif
537
538 /*
539 * Handle restarting Insert mode.
540 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
541 * restart_edit non-zero, and something in the stuff buffer.
542 */
543 if (restart_edit != 0 && stuff_empty())
544 {
545#ifdef FEAT_MOUSE
546 /*
547 * After a paste we consider text typed to be part of the insert for
548 * the pasted text. You can backspace over the pasted text too.
549 */
550 if (where_paste_started.lnum)
551 arrow_used = FALSE;
552 else
553#endif
554 arrow_used = TRUE;
555 restart_edit = 0;
556
557 /*
558 * If the cursor was after the end-of-line before the CTRL-O and it is
559 * now at the end-of-line, put it after the end-of-line (this is not
560 * correct in very rare cases).
561 * Also do this if curswant is greater than the current virtual
562 * column. Eg after "^O$" or "^O80|".
563 */
564 validate_virtcol();
565 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000566 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 || curwin->w_curswant > curwin->w_virtcol)
568 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
569 {
570 if (ptr[1] == NUL)
571 ++curwin->w_cursor.col;
572#ifdef FEAT_MBYTE
573 else if (has_mbyte)
574 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000575 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000576 if (ptr[i] == NUL)
577 curwin->w_cursor.col += i;
578 }
579#endif
580 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000581 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000582 }
583 else
584 arrow_used = FALSE;
585
586 /* we are in insert mode now, don't need to start it anymore */
587 need_start_insertmode = FALSE;
588
589 /* Need to save the line for undo before inserting the first char. */
590 ins_need_undo = TRUE;
591
592#ifdef FEAT_MOUSE
593 where_paste_started.lnum = 0;
594#endif
595#ifdef FEAT_CINDENT
596 can_cindent = TRUE;
597#endif
598#ifdef FEAT_FOLDING
599 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
600 * restarting. */
601 if (!p_im && did_restart_edit == 0)
602 foldOpenCursor();
603#endif
604
605 /*
606 * If 'showmode' is set, show the current (insert/replace/..) mode.
607 * A warning message for changing a readonly file is given here, before
608 * actually changing anything. It's put after the mode, if any.
609 */
610 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000611 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000612 i = showmode();
613
614 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000615 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616
617#ifdef CURSOR_SHAPE
618 ui_cursor_shape(); /* may show different cursor shape */
619#endif
620#ifdef FEAT_DIGRAPHS
621 do_digraph(-1); /* clear digraphs */
622#endif
623
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000624 /*
625 * Get the current length of the redo buffer, those characters have to be
626 * skipped if we want to get to the inserted characters.
627 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000628 ptr = get_inserted();
629 if (ptr == NULL)
630 new_insert_skip = 0;
631 else
632 {
633 new_insert_skip = (int)STRLEN(ptr);
634 vim_free(ptr);
635 }
636
637 old_indent = 0;
638
639 /*
640 * Main loop in Insert mode: repeat until Insert mode is left.
641 */
642 for (;;)
643 {
644#ifdef FEAT_RIGHTLEFT
645 if (!revins_legal)
646 revins_scol = -1; /* reset on illegal motions */
647 else
648 revins_legal = 0;
649#endif
650 if (arrow_used) /* don't repeat insert when arrow key used */
651 count = 0;
652
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100653 if (update_Insstart_orig)
654 Insstart_orig = Insstart;
655
Bram Moolenaar00672e12016-06-26 18:38:13 +0200656 if (stop_insert_mode
657#ifdef FEAT_INS_EXPAND
658 && !pum_visible()
659#endif
660 )
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 {
662 /* ":stopinsert" used or 'insertmode' reset */
663 count = 0;
664 goto doESCkey;
665 }
666
667 /* set curwin->w_curswant for next K_DOWN or K_UP */
668 if (!arrow_used)
669 curwin->w_set_curswant = TRUE;
670
671 /* If there is no typeahead may check for timestamps (e.g., for when a
672 * menu invoked a shell command). */
673 if (stuff_empty())
674 {
675 did_check_timestamps = FALSE;
676 if (need_check_timestamps)
677 check_timestamps(FALSE);
678 }
679
680 /*
681 * When emsg() was called msg_scroll will have been set.
682 */
683 msg_scroll = FALSE;
684
685#ifdef FEAT_GUI
686 /* When 'mousefocus' is set a mouse movement may have taken us to
687 * another window. "need_mouse_correct" may then be set because of an
688 * autocommand. */
689 if (need_mouse_correct)
690 gui_mouse_correct();
691#endif
692
693#ifdef FEAT_FOLDING
694 /* Open fold at the cursor line, according to 'foldopen'. */
695 if (fdo_flags & FDO_INSERT)
696 foldOpenCursor();
697 /* Close folds where the cursor isn't, according to 'foldclose' */
698 if (!char_avail())
699 foldCheckClose();
700#endif
701
702 /*
703 * If we inserted a character at the last position of the last line in
704 * the window, scroll the window one line up. This avoids an extra
705 * redraw.
706 * This is detected when the cursor column is smaller after inserting
707 * something.
708 * Don't do this when the topline changed already, it has
709 * already been adjusted (by insertchar() calling open_line())).
710 */
711 if (curbuf->b_mod_set
712 && curwin->w_p_wrap
713 && !did_backspace
714 && curwin->w_topline == old_topline
715#ifdef FEAT_DIFF
716 && curwin->w_topfill == old_topfill
717#endif
718 )
719 {
720 mincol = curwin->w_wcol;
721 validate_cursor_col();
722
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000723 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 && curwin->w_wrow == W_WINROW(curwin)
725 + curwin->w_height - 1 - p_so
726 && (curwin->w_cursor.lnum != curwin->w_topline
727#ifdef FEAT_DIFF
728 || curwin->w_topfill > 0
729#endif
730 ))
731 {
732#ifdef FEAT_DIFF
733 if (curwin->w_topfill > 0)
734 --curwin->w_topfill;
735 else
736#endif
737#ifdef FEAT_FOLDING
738 if (hasFolding(curwin->w_topline, NULL, &old_topline))
739 set_topline(curwin, old_topline + 1);
740 else
741#endif
742 set_topline(curwin, curwin->w_topline + 1);
743 }
744 }
745
746 /* May need to adjust w_topline to show the cursor. */
747 update_topline();
748
749 did_backspace = FALSE;
750
751 validate_cursor(); /* may set must_redraw */
752
753 /*
754 * Redraw the display when no characters are waiting.
755 * Also shows mode, ruler and positions cursor.
756 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000757 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758
759#ifdef FEAT_SCROLLBIND
760 if (curwin->w_p_scb)
761 do_check_scrollbind(TRUE);
762#endif
763
Bram Moolenaar860cae12010-06-05 23:22:07 +0200764#ifdef FEAT_CURSORBIND
765 if (curwin->w_p_crb)
766 do_check_cursorbind();
767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000768 update_curswant();
769 old_topline = curwin->w_topline;
770#ifdef FEAT_DIFF
771 old_topfill = curwin->w_topfill;
772#endif
773
774#ifdef USE_ON_FLY_SCROLL
775 dont_scroll = FALSE; /* allow scrolling here */
776#endif
777
778 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000779 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100781 if (c != K_CURSORHOLD)
782 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200783
784 /* After using CTRL-G U the next cursor key will not break undo. */
785 if (dont_sync_undo == MAYBE)
786 dont_sync_undo = TRUE;
787 else
788 dont_sync_undo = FALSE;
Bram Moolenaarec2da362017-01-21 20:04:22 +0100789 if (cmdchar == K_PS)
790 /* Got here from normal mode when bracketed paste started. */
791 c = K_PS;
792 else
793 do
794 {
795 c = safe_vgetc();
796 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000798#ifdef FEAT_AUTOCMD
799 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
800 did_cursorhold = TRUE;
801#endif
802
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803#ifdef FEAT_RIGHTLEFT
804 if (p_hkmap && KeyTyped)
805 c = hkmap(c); /* Hebrew mode mapping */
806#endif
807#ifdef FEAT_FKMAP
808 if (p_fkmap && KeyTyped)
809 c = fkmap(c); /* Farsi mode mapping */
810#endif
811
812#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000813 /*
814 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000815 * and the cursor is still in the completed word. Only when there is
816 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000817 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000818 if (compl_started
819 && pum_wanted()
820 && curwin->w_cursor.col >= compl_col
821 && (compl_shown_match == NULL
822 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000823 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000824 /* BS: Delete one character from "compl_leader". */
825 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000826 && curwin->w_cursor.col > compl_col
827 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000828 continue;
829
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000830 /* When no match was selected or it was edited. */
831 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000832 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000833 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000834 * "compl_leader". Except when at the original match and
835 * there is nothing to add, CTRL-L works like CTRL-P then. */
836 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100837 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000838 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000839 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 {
841 ins_compl_addfrommatch();
842 continue;
843 }
844
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000845 /* A non-white character that fits in with the current
846 * completion: Add to "compl_leader". */
847 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000848 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100849#ifdef FEAT_AUTOCMD
850 /* Trigger InsertCharPre. */
851 char_u *str = do_insert_char_pre(c);
852 char_u *p;
853
854 if (str != NULL)
855 {
856 for (p = str; *p != NUL; mb_ptr_adv(p))
857 ins_compl_addleader(PTR2CHAR(p));
858 vim_free(str);
859 }
860 else
861#endif
862 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000863 continue;
864 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000865
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000866 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000867 * compl_enter_selects is set the Enter key does the same. */
Bram Moolenaarcbd3bd62016-10-17 20:47:02 +0200868 if ((c == Ctrl_Y || (compl_enter_selects
869 && (c == CAR || c == K_KENTER || c == NL)))
870 && stop_arrow() == OK)
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000871 {
872 ins_compl_delete();
Bram Moolenaar472e8592016-10-15 17:06:47 +0200873 ins_compl_insert(FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000874 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000875 }
876 }
877
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
879 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000880 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000881 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000882 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000883#endif
884
Bram Moolenaar488c6512005-08-11 20:09:58 +0000885 /* CTRL-\ CTRL-N goes to Normal mode,
886 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
887 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 if (c == Ctrl_BSL)
889 {
890 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000891 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000892 ++no_mapping;
893 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000894 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895 --no_mapping;
896 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000897 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000899 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 vungetc(c);
901 c = Ctrl_BSL;
902 }
903 else if (c == Ctrl_G && p_im)
904 continue;
905 else
906 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000907 if (c == Ctrl_O)
908 {
909 ins_ctrl_o();
910 ins_at_eol = FALSE; /* cursor keeps its column */
911 nomove = TRUE;
912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 count = 0;
914 goto doESCkey;
915 }
916 }
917
918#ifdef FEAT_DIGRAPHS
919 c = do_digraph(c);
920#endif
921
922#ifdef FEAT_INS_EXPAND
923 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
924 goto docomplete;
925#endif
926 if (c == Ctrl_V || c == Ctrl_Q)
927 {
928 ins_ctrl_v();
929 c = Ctrl_V; /* pretend CTRL-V is last typed character */
930 continue;
931 }
932
933#ifdef FEAT_CINDENT
934 if (cindent_on()
935# ifdef FEAT_INS_EXPAND
936 && ctrl_x_mode == 0
937# endif
938 )
939 {
940 /* A key name preceded by a bang means this key is not to be
941 * inserted. Skip ahead to the re-indenting below.
942 * A key name preceded by a star means that indenting has to be
943 * done before inserting the key. */
944 line_is_white = inindent(0);
945 if (in_cinkeys(c, '!', line_is_white))
946 goto force_cindent;
947 if (can_cindent && in_cinkeys(c, '*', line_is_white)
948 && stop_arrow() == OK)
949 do_c_expr_indent();
950 }
951#endif
952
953#ifdef FEAT_RIGHTLEFT
954 if (curwin->w_p_rl)
955 switch (c)
956 {
957 case K_LEFT: c = K_RIGHT; break;
958 case K_S_LEFT: c = K_S_RIGHT; break;
959 case K_C_LEFT: c = K_C_RIGHT; break;
960 case K_RIGHT: c = K_LEFT; break;
961 case K_S_RIGHT: c = K_S_LEFT; break;
962 case K_C_RIGHT: c = K_C_LEFT; break;
963 }
964#endif
965
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 /*
967 * If 'keymodel' contains "startsel", may start selection. If it
968 * does, a CTRL-O and c will be stuffed, we need to get these
969 * characters.
970 */
971 if (ins_start_select(c))
972 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973
974 /*
975 * The big switch to handle a character in insert mode.
976 */
977 switch (c)
978 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000979 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000980 if (echeck_abbr(ESC + ABBR_OFF))
981 break;
982 /*FALLTHROUGH*/
983
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000984 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985#ifdef FEAT_CMDWIN
986 if (c == Ctrl_C && cmdwin_type != 0)
987 {
988 /* Close the cmdline window. */
989 cmdwin_result = K_IGNORE;
990 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000991 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000992 goto doESCkey;
993 }
994#endif
995
996#ifdef UNIX
997do_intr:
998#endif
999 /* when 'insertmode' set, and not halfway a mapping, don't leave
1000 * Insert mode */
1001 if (goto_im())
1002 {
1003 if (got_int)
1004 {
1005 (void)vgetc(); /* flush all buffers */
1006 got_int = FALSE;
1007 }
1008 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02001009 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 break;
1011 }
1012doESCkey:
1013 /*
1014 * This is the ONLY return from edit()!
1015 */
1016 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1017 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001018 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 o_lnum = curwin->w_cursor.lnum;
1020
Bram Moolenaar488c6512005-08-11 20:09:58 +00001021 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001022 {
1023#ifdef FEAT_AUTOCMD
1024 if (cmdchar != 'r' && cmdchar != 'v')
1025 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1026 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001027 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001028#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001030 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031 continue;
1032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 case Ctrl_Z: /* suspend when 'insertmode' set */
1034 if (!p_im)
1035 goto normalchar; /* insert CTRL-Z as normal char */
Bram Moolenaar25b0e6b2017-01-20 21:51:53 +01001036 do_cmdline_cmd((char_u *)"stop");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 c = Ctrl_O;
1038 /*FALLTHROUGH*/
1039
1040 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001041#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001042 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 goto docomplete;
1044#endif
1045 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1046 break;
1047 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001048
1049#ifdef FEAT_VIRTUALEDIT
1050 /* don't move the cursor left when 'virtualedit' has "onemore". */
1051 if (ve_flags & VE_ONEMORE)
1052 {
1053 ins_at_eol = FALSE;
1054 nomove = TRUE;
1055 }
1056#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 count = 0;
1058 goto doESCkey;
1059
Bram Moolenaar572cb562005-08-05 21:35:02 +00001060 case K_INS: /* toggle insert/replace mode */
1061 case K_KINS:
1062 ins_insert(replaceState);
1063 break;
1064
1065 case K_SELECT: /* end of Select mode mapping - ignore */
1066 break;
1067
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001068 case K_HELP: /* Help key works like <ESC> <Help> */
1069 case K_F1:
1070 case K_XF1:
1071 stuffcharReadbuff(K_HELP);
1072 if (p_im)
1073 need_start_insertmode = TRUE;
1074 goto doESCkey;
1075
1076#ifdef FEAT_NETBEANS_INTG
1077 case K_F21: /* NetBeans command */
1078 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001079 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 --no_mapping;
1081 netbeans_keycommand(i);
1082 break;
1083#endif
1084
1085 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086 case NUL:
1087 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 /* For ^@ the trailing ESC will end the insert, unless there is an
1089 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1091 && c != Ctrl_A && !p_im)
1092 goto doESCkey; /* quit insert mode */
1093 inserted_space = FALSE;
1094 break;
1095
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001096 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 ins_reg();
1098 auto_format(FALSE, TRUE);
1099 inserted_space = FALSE;
1100 break;
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103 ins_ctrl_g();
1104 break;
1105
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001106 case Ctrl_HAT: /* switch input mode and/or langmap */
1107 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 break;
1109
1110#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001111 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 if (!p_ari)
1113 goto normalchar;
1114 ins_ctrl_();
1115 break;
1116#endif
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1120 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1121 goto docomplete;
1122#endif
1123 /* FALLTHROUGH */
1124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001125 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001126# ifdef FEAT_INS_EXPAND
1127 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1128 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 if (has_compl_option(FALSE))
1130 goto docomplete;
1131 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 }
1133# endif
1134 ins_shift(c, lastc);
1135 auto_format(FALSE, TRUE);
1136 inserted_space = FALSE;
1137 break;
1138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001139 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001140 case K_KDEL:
1141 ins_del();
1142 auto_format(FALSE, TRUE);
1143 break;
1144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case Ctrl_H:
1147 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1148 auto_format(FALSE, TRUE);
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1153 auto_format(FALSE, TRUE);
1154 break;
1155
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001156 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001157# ifdef FEAT_COMPL_FUNC
1158 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001159 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001160 goto docomplete;
1161# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001162 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1163 auto_format(FALSE, TRUE);
1164 inserted_space = FALSE;
1165 break;
1166
1167#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001168 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 case K_LEFTMOUSE_NM:
1170 case K_LEFTDRAG:
1171 case K_LEFTRELEASE:
1172 case K_LEFTRELEASE_NM:
1173 case K_MIDDLEMOUSE:
1174 case K_MIDDLEDRAG:
1175 case K_MIDDLERELEASE:
1176 case K_RIGHTMOUSE:
1177 case K_RIGHTDRAG:
1178 case K_RIGHTRELEASE:
1179 case K_X1MOUSE:
1180 case K_X1DRAG:
1181 case K_X1RELEASE:
1182 case K_X2MOUSE:
1183 case K_X2DRAG:
1184 case K_X2RELEASE:
1185 ins_mouse(c);
1186 break;
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001189 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001193 ins_mousescroll(MSCR_UP);
1194 break;
1195
1196 case K_MOUSELEFT: /* Scroll wheel left */
1197 ins_mousescroll(MSCR_LEFT);
1198 break;
1199
1200 case K_MOUSERIGHT: /* Scroll wheel right */
1201 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 break;
1203#endif
Bram Moolenaarec2da362017-01-21 20:04:22 +01001204 case K_PS:
1205 bracketed_paste(PASTE_INSERT, FALSE, NULL);
1206 if (cmdchar == K_PS)
1207 /* invoked from normal mode, bail out */
1208 goto doESCkey;
1209 break;
1210 case K_PE:
1211 /* Got K_PE without K_PS, ignore. */
1212 break;
1213
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001214#ifdef FEAT_GUI_TABLINE
1215 case K_TABLINE:
1216 case K_TABMENU:
1217 ins_tabline(c);
1218 break;
1219#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001221 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 break;
1223
Bram Moolenaar754b5602006-02-09 23:53:20 +00001224#ifdef FEAT_AUTOCMD
1225 case K_CURSORHOLD: /* Didn't type something for a while. */
1226 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1227 did_cursorhold = TRUE;
1228 break;
1229#endif
1230
Bram Moolenaar4770d092006-01-12 23:22:24 +00001231#ifdef FEAT_GUI_W32
1232 /* On Win32 ignore <M-F4>, we get it when closing the window was
1233 * cancelled. */
1234 case K_F4:
1235 if (mod_mask != MOD_MASK_ALT)
1236 goto normalchar;
1237 break;
1238#endif
1239
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240#ifdef FEAT_GUI
1241 case K_VER_SCROLLBAR:
1242 ins_scroll();
1243 break;
1244
1245 case K_HOR_SCROLLBAR:
1246 ins_horscroll();
1247 break;
1248#endif
1249
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001250 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 case K_S_HOME:
1253 case K_C_HOME:
1254 ins_home(c);
1255 break;
1256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001257 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 case K_S_END:
1260 case K_C_END:
1261 ins_end(c);
1262 break;
1263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001264 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001265 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1266 ins_s_left();
1267 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001268 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 break;
1270
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001271 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 case K_C_LEFT:
1273 ins_s_left();
1274 break;
1275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001277 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1278 ins_s_right();
1279 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001280 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 break;
1282
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001283 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 case K_C_RIGHT:
1285 ins_s_right();
1286 break;
1287
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001288 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001289#ifdef FEAT_INS_EXPAND
1290 if (pum_visible())
1291 goto docomplete;
1292#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001293 if (mod_mask & MOD_MASK_SHIFT)
1294 ins_pageup();
1295 else
1296 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 break;
1298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 case K_PAGEUP:
1301 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001302#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001303 if (pum_visible())
1304 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001305#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 ins_pageup();
1307 break;
1308
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001309 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001310#ifdef FEAT_INS_EXPAND
1311 if (pum_visible())
1312 goto docomplete;
1313#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001314 if (mod_mask & MOD_MASK_SHIFT)
1315 ins_pagedown();
1316 else
1317 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318 break;
1319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001320 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321 case K_PAGEDOWN:
1322 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001323#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001324 if (pum_visible())
1325 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 ins_pagedown();
1328 break;
1329
1330#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001331 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 ins_drop();
1333 break;
1334#endif
1335
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001336 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 c = TAB;
1338 /* FALLTHROUGH */
1339
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001340 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001341#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1342 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1343 goto docomplete;
1344#endif
1345 inserted_space = FALSE;
1346 if (ins_tab())
1347 goto normalchar; /* insert TAB as a normal char */
1348 auto_format(FALSE, TRUE);
1349 break;
1350
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001351 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 c = CAR;
1353 /* FALLTHROUGH */
1354 case CAR:
1355 case NL:
1356#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1357 /* In a quickfix window a <CR> jumps to the error under the
1358 * cursor. */
1359 if (bt_quickfix(curbuf) && c == CAR)
1360 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001361 if (curwin->w_llist_ref == NULL) /* quickfix window */
1362 do_cmdline_cmd((char_u *)".cc");
1363 else /* location list window */
1364 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 break;
1366 }
1367#endif
1368#ifdef FEAT_CMDWIN
1369 if (cmdwin_type != 0)
1370 {
1371 /* Execute the command in the cmdline window. */
1372 cmdwin_result = CAR;
1373 goto doESCkey;
1374 }
1375#endif
1376 if (ins_eol(c) && !p_im)
1377 goto doESCkey; /* out of memory */
1378 auto_format(FALSE, FALSE);
1379 inserted_space = FALSE;
1380 break;
1381
Bram Moolenaar860cae12010-06-05 23:22:07 +02001382#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001383 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384# ifdef FEAT_INS_EXPAND
1385 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1386 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 if (has_compl_option(TRUE))
1388 goto docomplete;
1389 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391# endif
1392# ifdef FEAT_DIGRAPHS
1393 c = ins_digraph();
1394 if (c == NUL)
1395 break;
1396# endif
1397 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001399
1400#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001401 case Ctrl_X: /* Enter CTRL-X mode */
1402 ins_ctrl_x();
1403 break;
1404
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001405 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 if (ctrl_x_mode != CTRL_X_TAGS)
1407 goto normalchar;
1408 goto docomplete;
1409
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001410 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 if (ctrl_x_mode != CTRL_X_FILES)
1412 goto normalchar;
1413 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001414
1415 case 's': /* Spelling completion after ^X */
1416 case Ctrl_S:
1417 if (ctrl_x_mode != CTRL_X_SPELL)
1418 goto normalchar;
1419 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420#endif
1421
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001422 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423#ifdef FEAT_INS_EXPAND
1424 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1425#endif
1426 {
1427 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1428 if (p_im)
1429 {
1430 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1431 break;
1432 goto doESCkey;
1433 }
1434 goto normalchar;
1435 }
1436#ifdef FEAT_INS_EXPAND
1437 /* FALLTHROUGH */
1438
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001439 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 case Ctrl_N:
1441 /* if 'complete' is empty then plain ^P is no longer special,
1442 * but it is under other ^X modes */
1443 if (*curbuf->b_p_cpt == NUL
1444 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001445 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001446 goto normalchar;
1447
1448docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001449 compl_busy = TRUE;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001450 disable_fold_update++; /* don't redraw folds here */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01001451 if (ins_complete(c, TRUE) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001452 compl_cont_status = 0;
Bram Moolenaar429fcfb2016-04-14 16:22:04 +02001453 disable_fold_update--;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001454 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 break;
1456#endif /* FEAT_INS_EXPAND */
1457
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001458 case Ctrl_Y: /* copy from previous line or scroll down */
1459 case Ctrl_E: /* copy from next line or scroll up */
1460 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 break;
1462
1463 default:
1464#ifdef UNIX
1465 if (c == intr_char) /* special interrupt char */
1466 goto do_intr;
1467#endif
1468
Bram Moolenaare659c952011-05-19 17:25:41 +02001469normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001471 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001472 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001473#ifdef FEAT_AUTOCMD
1474 if (!p_paste)
1475 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001476 /* Trigger InsertCharPre. */
1477 char_u *str = do_insert_char_pre(c);
1478 char_u *p;
1479
1480 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001481 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001482 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001483 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001484 /* Insert the new value of v:char literally. */
1485 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001486 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001487 c = PTR2CHAR(p);
1488 if (c == CAR || c == K_KENTER || c == NL)
1489 ins_eol(c);
1490 else
1491 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001492 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001493 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001494 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001495 vim_free(str);
1496 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001497 }
1498
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001499 /* If the new value is already inserted or an empty string
1500 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001501 if (c == NUL)
1502 break;
1503 }
1504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505#ifdef FEAT_SMARTINDENT
1506 /* Try to perform smart-indenting. */
1507 ins_try_si(c);
1508#endif
1509
1510 if (c == ' ')
1511 {
1512 inserted_space = TRUE;
1513#ifdef FEAT_CINDENT
1514 if (inindent(0))
1515 can_cindent = FALSE;
1516#endif
1517 if (Insstart_blank_vcol == MAXCOL
1518 && curwin->w_cursor.lnum == Insstart.lnum)
1519 Insstart_blank_vcol = get_nolist_virtcol();
1520 }
1521
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001522 /* Insert a normal character and check for abbreviations on a
1523 * special character. Let CTRL-] expand abbreviations without
1524 * inserting it. */
1525 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526#ifdef FEAT_MBYTE
1527 /* Add ABBR_OFF for characters above 0x100, this is
1528 * what check_abbr() expects. */
1529 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1530#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001531 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001532 {
1533 insert_special(c, FALSE, FALSE);
1534#ifdef FEAT_RIGHTLEFT
1535 revins_legal++;
1536 revins_chars++;
1537#endif
1538 }
1539
1540 auto_format(FALSE, TRUE);
1541
1542#ifdef FEAT_FOLDING
1543 /* When inserting a character the cursor line must never be in a
1544 * closed fold. */
1545 foldOpenCursor();
1546#endif
1547 break;
1548 } /* end of switch (c) */
1549
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001550#ifdef FEAT_AUTOCMD
1551 /* If typed something may trigger CursorHoldI again. */
Bram Moolenaar245c4102016-04-20 17:37:41 +02001552 if (c != K_CURSORHOLD
1553# ifdef FEAT_COMPL_FUNC
1554 /* but not in CTRL-X mode, a script can't restore the state */
1555 && ctrl_x_mode == 0
1556# endif
1557 )
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001558 did_cursorhold = FALSE;
1559#endif
1560
Bram Moolenaar071d4272004-06-13 20:20:40 +00001561 /* If the cursor was moved we didn't just insert a space */
1562 if (arrow_used)
1563 inserted_space = FALSE;
1564
1565#ifdef FEAT_CINDENT
1566 if (can_cindent && cindent_on()
1567# ifdef FEAT_INS_EXPAND
1568 && ctrl_x_mode == 0
1569# endif
1570 )
1571 {
1572force_cindent:
1573 /*
1574 * Indent now if a key was typed that is in 'cinkeys'.
1575 */
1576 if (in_cinkeys(c, ' ', line_is_white))
1577 {
1578 if (stop_arrow() == OK)
1579 /* re-indent the current line */
1580 do_c_expr_indent();
1581 }
1582 }
1583#endif /* FEAT_CINDENT */
1584
1585 } /* for (;;) */
1586 /* NOTREACHED */
1587}
1588
1589/*
1590 * Redraw for Insert mode.
1591 * This is postponed until getting the next character to make '$' in the 'cpo'
1592 * option work correctly.
1593 * Only redraw when there are no characters available. This speeds up
1594 * inserting sequences of characters (e.g., for CTRL-R).
1595 */
1596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001597ins_redraw(
1598 int ready UNUSED) /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001600#ifdef FEAT_CONCEAL
1601 linenr_T conceal_old_cursor_line = 0;
1602 linenr_T conceal_new_cursor_line = 0;
1603 int conceal_update_lines = FALSE;
1604#endif
1605
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001606 if (char_avail())
1607 return;
1608
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001609#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001610 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1611 * visible, the command might delete it. */
1612 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001613# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001614 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001615# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001616# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001617 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001618# endif
1619# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001620 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001621# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001622 )
Bram Moolenaard1413d92016-03-02 21:51:56 +01001623# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001624 && !equalpos(last_cursormoved, curwin->w_cursor)
Bram Moolenaard1413d92016-03-02 21:51:56 +01001625# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001626# ifdef FEAT_INS_EXPAND
1627 && !pum_visible()
1628# endif
1629 )
1630 {
1631# ifdef FEAT_SYN_HL
1632 /* Need to update the screen first, to make sure syntax
1633 * highlighting is correct after making a change (e.g., inserting
1634 * a "(". The autocommand may also require a redraw, so it's done
1635 * again below, unfortunately. */
1636 if (syntax_present(curwin) && must_redraw)
1637 update_screen(0);
1638# endif
1639# ifdef FEAT_AUTOCMD
1640 if (has_cursormovedI())
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001641 {
1642 /* Make sure curswant is correct, an autocommand may call
1643 * getcurpos(). */
1644 update_curswant();
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001645 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
Bram Moolenaarf068dca2016-02-09 21:24:46 +01001646 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001647# endif
1648# ifdef FEAT_CONCEAL
1649 if (curwin->w_p_cole > 0)
1650 {
Bram Moolenaard1413d92016-03-02 21:51:56 +01001651# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001652 conceal_old_cursor_line = last_cursormoved.lnum;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001653# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001654 conceal_new_cursor_line = curwin->w_cursor.lnum;
1655 conceal_update_lines = TRUE;
1656 }
1657# endif
Bram Moolenaard1413d92016-03-02 21:51:56 +01001658# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001659 last_cursormoved = curwin->w_cursor;
Bram Moolenaard1413d92016-03-02 21:51:56 +01001660# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001661 }
1662#endif
1663
1664#ifdef FEAT_AUTOCMD
1665 /* Trigger TextChangedI if b_changedtick differs. */
1666 if (ready && has_textchangedI()
1667 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001668# ifdef FEAT_INS_EXPAND
1669 && !pum_visible()
1670# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001671 )
1672 {
1673 if (last_changedtick_buf == curbuf)
1674 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1675 last_changedtick_buf = curbuf;
1676 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001678#endif
1679
1680 if (must_redraw)
1681 update_screen(0);
1682 else if (clear_cmdline || redraw_cmdline)
1683 showmode(); /* clear cmdline and show mode */
1684# if defined(FEAT_CONCEAL)
1685 if ((conceal_update_lines
1686 && (conceal_old_cursor_line != conceal_new_cursor_line
1687 || conceal_cursor_line(curwin)))
1688 || need_cursor_line_redraw)
1689 {
1690 if (conceal_old_cursor_line != conceal_new_cursor_line)
1691 update_single_line(curwin, conceal_old_cursor_line);
1692 update_single_line(curwin, conceal_new_cursor_line == 0
1693 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1694 curwin->w_valid &= ~VALID_CROW;
1695 }
1696# endif
1697 showruler(FALSE);
1698 setcursor();
1699 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001700}
1701
1702/*
1703 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1704 */
1705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001706ins_ctrl_v(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001707{
1708 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001709 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001710
1711 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001712 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713
1714 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001715 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001717 did_putchar = TRUE;
1718 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001719 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1720
1721#ifdef FEAT_CMDL_INFO
1722 add_to_showcmd_c(Ctrl_V);
1723#endif
1724
1725 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001726 if (did_putchar)
1727 /* when the line fits in 'columns' the '^' is at the start of the next
1728 * line and will not removed by the redraw */
1729 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001730#ifdef FEAT_CMDL_INFO
1731 clear_showcmd();
1732#endif
1733 insert_special(c, FALSE, TRUE);
1734#ifdef FEAT_RIGHTLEFT
1735 revins_chars++;
1736 revins_legal++;
1737#endif
1738}
1739
1740/*
1741 * Put a character directly onto the screen. It's not stored in a buffer.
1742 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1743 */
1744static int pc_status;
1745#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1746#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1747#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1748#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750static int pc_attr;
1751static int pc_row;
1752static int pc_col;
1753
1754 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001755edit_putchar(int c, int highlight)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001756{
1757 int attr;
1758
1759 if (ScreenLines != NULL)
1760 {
1761 update_topline(); /* just in case w_topline isn't valid */
1762 validate_cursor();
1763 if (highlight)
1764 attr = hl_attr(HLF_8);
1765 else
1766 attr = 0;
1767 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1768 pc_col = W_WINCOL(curwin);
1769#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1770 pc_status = PC_STATUS_UNSET;
1771#endif
1772#ifdef FEAT_RIGHTLEFT
1773 if (curwin->w_p_rl)
1774 {
1775 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1776# ifdef FEAT_MBYTE
1777 if (has_mbyte)
1778 {
1779 int fix_col = mb_fix_col(pc_col, pc_row);
1780
1781 if (fix_col != pc_col)
1782 {
1783 screen_putchar(' ', pc_row, fix_col, attr);
1784 --curwin->w_wcol;
1785 pc_status = PC_STATUS_RIGHT;
1786 }
1787 }
1788# endif
1789 }
1790 else
1791#endif
1792 {
1793 pc_col += curwin->w_wcol;
1794#ifdef FEAT_MBYTE
1795 if (mb_lefthalve(pc_row, pc_col))
1796 pc_status = PC_STATUS_LEFT;
1797#endif
1798 }
1799
1800 /* save the character to be able to put it back */
1801#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1802 if (pc_status == PC_STATUS_UNSET)
1803#endif
1804 {
1805 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1806 pc_status = PC_STATUS_SET;
1807 }
1808 screen_putchar(c, pc_row, pc_col, attr);
1809 }
1810}
1811
1812/*
1813 * Undo the previous edit_putchar().
1814 */
1815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001816edit_unputchar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001817{
1818 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1819 {
1820#if defined(FEAT_MBYTE)
1821 if (pc_status == PC_STATUS_RIGHT)
1822 ++curwin->w_wcol;
1823 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1824 redrawWinline(curwin->w_cursor.lnum, FALSE);
1825 else
1826#endif
1827 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1828 }
1829}
1830
1831/*
1832 * Called when p_dollar is set: display a '$' at the end of the changed text
1833 * Only works when cursor is in the line that changes.
1834 */
1835 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001836display_dollar(colnr_T col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 colnr_T save_col;
1839
1840 if (!redrawing())
1841 return;
1842
1843 cursor_off();
1844 save_col = curwin->w_cursor.col;
1845 curwin->w_cursor.col = col;
1846#ifdef FEAT_MBYTE
1847 if (has_mbyte)
1848 {
1849 char_u *p;
1850
1851 /* If on the last byte of a multi-byte move to the first byte. */
1852 p = ml_get_curline();
1853 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1854 }
1855#endif
1856 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1857 if (curwin->w_wcol < W_WIDTH(curwin))
1858 {
1859 edit_putchar('$', FALSE);
1860 dollar_vcol = curwin->w_virtcol;
1861 }
1862 curwin->w_cursor.col = save_col;
1863}
1864
1865/*
1866 * Call this function before moving the cursor from the normal insert position
1867 * in insert mode.
1868 */
1869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001870undisplay_dollar(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001872 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001874 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 redrawWinline(curwin->w_cursor.lnum, FALSE);
1876 }
1877}
1878
1879/*
1880 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1881 * Keep the cursor on the same character.
1882 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1883 * type == INDENT_DEC decrease indent (for CTRL-D)
1884 * type == INDENT_SET set indent to "amount"
1885 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1886 */
1887 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001888change_indent(
1889 int type,
1890 int amount,
1891 int round,
1892 int replaced, /* replaced character, put on replace stack */
1893 int call_changed_bytes) /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894{
1895 int vcol;
1896 int last_vcol;
1897 int insstart_less; /* reduction for Insstart.col */
1898 int new_cursor_col;
1899 int i;
1900 char_u *ptr;
1901 int save_p_list;
1902 int start_col;
1903 colnr_T vc;
1904#ifdef FEAT_VREPLACE
1905 colnr_T orig_col = 0; /* init for GCC */
1906 char_u *new_line, *orig_line = NULL; /* init for GCC */
1907
1908 /* VREPLACE mode needs to know what the line was like before changing */
1909 if (State & VREPLACE_FLAG)
1910 {
1911 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1912 orig_col = curwin->w_cursor.col;
1913 }
1914#endif
1915
1916 /* for the following tricks we don't want list mode */
1917 save_p_list = curwin->w_p_list;
1918 curwin->w_p_list = FALSE;
1919 vc = getvcol_nolist(&curwin->w_cursor);
1920 vcol = vc;
1921
1922 /*
1923 * For Replace mode we need to fix the replace stack later, which is only
1924 * possible when the cursor is in the indent. Remember the number of
1925 * characters before the cursor if it's possible.
1926 */
1927 start_col = curwin->w_cursor.col;
1928
1929 /* determine offset from first non-blank */
1930 new_cursor_col = curwin->w_cursor.col;
1931 beginline(BL_WHITE);
1932 new_cursor_col -= curwin->w_cursor.col;
1933
1934 insstart_less = curwin->w_cursor.col;
1935
1936 /*
1937 * If the cursor is in the indent, compute how many screen columns the
1938 * cursor is to the left of the first non-blank.
1939 */
1940 if (new_cursor_col < 0)
1941 vcol = get_indent() - vcol;
1942
1943 if (new_cursor_col > 0) /* can't fix replace stack */
1944 start_col = -1;
1945
1946 /*
1947 * Set the new indent. The cursor will be put on the first non-blank.
1948 */
1949 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001950 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 else
1952 {
1953#ifdef FEAT_VREPLACE
1954 int save_State = State;
1955
1956 /* Avoid being called recursively. */
1957 if (State & VREPLACE_FLAG)
1958 State = INSERT;
1959#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001960 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961#ifdef FEAT_VREPLACE
1962 State = save_State;
1963#endif
1964 }
1965 insstart_less -= curwin->w_cursor.col;
1966
1967 /*
1968 * Try to put cursor on same character.
1969 * If the cursor is at or after the first non-blank in the line,
1970 * compute the cursor column relative to the column of the first
1971 * non-blank character.
1972 * If we are not in insert mode, leave the cursor on the first non-blank.
1973 * If the cursor is before the first non-blank, position it relative
1974 * to the first non-blank, counted in screen columns.
1975 */
1976 if (new_cursor_col >= 0)
1977 {
1978 /*
1979 * When changing the indent while the cursor is touching it, reset
1980 * Insstart_col to 0.
1981 */
1982 if (new_cursor_col == 0)
1983 insstart_less = MAXCOL;
1984 new_cursor_col += curwin->w_cursor.col;
1985 }
1986 else if (!(State & INSERT))
1987 new_cursor_col = curwin->w_cursor.col;
1988 else
1989 {
1990 /*
1991 * Compute the screen column where the cursor should be.
1992 */
1993 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001994 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995
1996 /*
1997 * Advance the cursor until we reach the right screen column.
1998 */
1999 vcol = last_vcol = 0;
2000 new_cursor_col = -1;
2001 ptr = ml_get_curline();
2002 while (vcol <= (int)curwin->w_virtcol)
2003 {
2004 last_vcol = vcol;
2005#ifdef FEAT_MBYTE
2006 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002007 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002008 else
2009#endif
2010 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02002011 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002012 }
2013 vcol = last_vcol;
2014
2015 /*
2016 * May need to insert spaces to be able to position the cursor on
2017 * the right screen column.
2018 */
2019 if (vcol != (int)curwin->w_virtcol)
2020 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002021 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002022 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002023 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002024 if (ptr != NULL)
2025 {
2026 new_cursor_col += i;
2027 ptr[i] = NUL;
2028 while (--i >= 0)
2029 ptr[i] = ' ';
2030 ins_str(ptr);
2031 vim_free(ptr);
2032 }
2033 }
2034
2035 /*
2036 * When changing the indent while the cursor is in it, reset
2037 * Insstart_col to 0.
2038 */
2039 insstart_less = MAXCOL;
2040 }
2041
2042 curwin->w_p_list = save_p_list;
2043
2044 if (new_cursor_col <= 0)
2045 curwin->w_cursor.col = 0;
2046 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002047 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002048 curwin->w_set_curswant = TRUE;
2049 changed_cline_bef_curs();
2050
2051 /*
2052 * May have to adjust the start of the insert.
2053 */
2054 if (State & INSERT)
2055 {
2056 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2057 {
2058 if ((int)Insstart.col <= insstart_less)
2059 Insstart.col = 0;
2060 else
2061 Insstart.col -= insstart_less;
2062 }
2063 if ((int)ai_col <= insstart_less)
2064 ai_col = 0;
2065 else
2066 ai_col -= insstart_less;
2067 }
2068
2069 /*
2070 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2071 * If the number of characters before the cursor decreased, need to pop a
2072 * few characters from the replace stack.
2073 * If the number of characters before the cursor increased, need to push a
2074 * few NULs onto the replace stack.
2075 */
2076 if (REPLACE_NORMAL(State) && start_col >= 0)
2077 {
2078 while (start_col > (int)curwin->w_cursor.col)
2079 {
2080 replace_join(0); /* remove a NUL from the replace stack */
2081 --start_col;
2082 }
2083 while (start_col < (int)curwin->w_cursor.col || replaced)
2084 {
2085 replace_push(NUL);
2086 if (replaced)
2087 {
2088 replace_push(replaced);
2089 replaced = NUL;
2090 }
2091 ++start_col;
2092 }
2093 }
2094
2095#ifdef FEAT_VREPLACE
2096 /*
2097 * For VREPLACE mode, we also have to fix the replace stack. In this case
2098 * it is always possible because we backspace over the whole line and then
2099 * put it back again the way we wanted it.
2100 */
2101 if (State & VREPLACE_FLAG)
2102 {
2103 /* If orig_line didn't allocate, just return. At least we did the job,
2104 * even if you can't backspace. */
2105 if (orig_line == NULL)
2106 return;
2107
2108 /* Save new line */
2109 new_line = vim_strsave(ml_get_curline());
2110 if (new_line == NULL)
2111 return;
2112
2113 /* We only put back the new line up to the cursor */
2114 new_line[curwin->w_cursor.col] = NUL;
2115
2116 /* Put back original line */
2117 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2118 curwin->w_cursor.col = orig_col;
2119
2120 /* Backspace from cursor to start of line */
2121 backspace_until_column(0);
2122
2123 /* Insert new stuff into line again */
2124 ins_bytes(new_line);
2125
2126 vim_free(new_line);
2127 }
2128#endif
2129}
2130
2131/*
2132 * Truncate the space at the end of a line. This is to be used only in an
2133 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2134 * modes.
2135 */
2136 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002137truncate_spaces(char_u *line)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002138{
2139 int i;
2140
2141 /* find start of trailing white space */
2142 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2143 {
2144 if (State & REPLACE_FLAG)
2145 replace_join(0); /* remove a NUL from the replace stack */
2146 }
2147 line[i + 1] = NUL;
2148}
2149
2150#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2151 || defined(FEAT_COMMENTS) || defined(PROTO)
2152/*
2153 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2154 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002155 * Will attempt not to go before "col" even when there is a composing
2156 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157 */
2158 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002159backspace_until_column(int col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160{
2161 while ((int)curwin->w_cursor.col > col)
2162 {
2163 curwin->w_cursor.col--;
2164 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002165 replace_do_bs(col);
2166 else if (!del_char_after_col(col))
2167 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 }
2169}
2170#endif
2171
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002172/*
2173 * Like del_char(), but make sure not to go before column "limit_col".
2174 * Only matters when there are composing characters.
2175 * Return TRUE when something was deleted.
2176 */
2177 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178del_char_after_col(int limit_col UNUSED)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002179{
2180#ifdef FEAT_MBYTE
2181 if (enc_utf8 && limit_col >= 0)
2182 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002183 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002184
2185 /* Make sure the cursor is at the start of a character, but
2186 * skip forward again when going too far back because of a
2187 * composing character. */
2188 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002189 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002190 {
2191 int l = utf_ptr2len(ml_get_cursor());
2192
2193 if (l == 0) /* end of line */
2194 break;
2195 curwin->w_cursor.col += l;
2196 }
2197 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2198 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002199 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002200 }
2201 else
2202#endif
2203 (void)del_char(FALSE);
2204 return TRUE;
2205}
2206
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2208/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002209 * CTRL-X pressed in Insert mode.
2210 */
2211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002212ins_ctrl_x(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002213{
2214 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2215 * CTRL-V works like CTRL-N */
2216 if (ctrl_x_mode != CTRL_X_CMDLINE)
2217 {
2218 /* if the next ^X<> won't ADD nothing, then reset
2219 * compl_cont_status */
2220 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002221 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002222 else
2223 compl_cont_status = 0;
2224 /* We're not sure which CTRL-X mode it will be yet */
2225 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2226 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2227 edit_submode_pre = NULL;
2228 showmode();
2229 }
2230}
2231
2232/*
2233 * Return TRUE if the 'dict' or 'tsr' option can be used.
2234 */
2235 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002236has_compl_option(int dict_opt)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002237{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002238 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002239# ifdef FEAT_SPELL
2240 && !curwin->w_p_spell
2241# endif
2242 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002243 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2244 {
2245 ctrl_x_mode = 0;
2246 edit_submode = NULL;
2247 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2248 : (char_u *)_("'thesaurus' option is empty"),
2249 hl_attr(HLF_E));
2250 if (emsg_silent == 0)
2251 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002252 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002253 setcursor();
2254 out_flush();
2255 ui_delay(2000L, FALSE);
2256 }
2257 return FALSE;
2258 }
2259 return TRUE;
2260}
2261
2262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2264 * This depends on the current mode.
2265 */
2266 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002267vim_is_ctrl_x_key(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268{
2269 /* Always allow ^R - let it's results then be checked */
2270 if (c == Ctrl_R)
2271 return TRUE;
2272
Bram Moolenaare3226be2005-12-18 22:10:00 +00002273 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002274 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002275 return TRUE;
2276
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 switch (ctrl_x_mode)
2278 {
2279 case 0: /* Not in any CTRL-X mode */
2280 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2281 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002282 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2284 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2285 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002286 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002287 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002288 case CTRL_X_SCROLL:
2289 return (c == Ctrl_Y || c == Ctrl_E);
2290 case CTRL_X_WHOLE_LINE:
2291 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2292 case CTRL_X_FILES:
2293 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2294 case CTRL_X_DICTIONARY:
2295 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2296 case CTRL_X_THESAURUS:
2297 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2298 case CTRL_X_TAGS:
2299 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2300#ifdef FEAT_FIND_ID
2301 case CTRL_X_PATH_PATTERNS:
2302 return (c == Ctrl_P || c == Ctrl_N);
2303 case CTRL_X_PATH_DEFINES:
2304 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2305#endif
2306 case CTRL_X_CMDLINE:
2307 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2308 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002309#ifdef FEAT_COMPL_FUNC
2310 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002311 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002312 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002313 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002314#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002315 case CTRL_X_SPELL:
2316 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002317 case CTRL_X_EVAL:
2318 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 }
Bram Moolenaar95f09602016-11-10 20:01:45 +01002320 internal_error("vim_is_ctrl_x_key()");
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 return FALSE;
2322}
2323
2324/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002325 * Return TRUE when character "c" is part of the item currently being
2326 * completed. Used to decide whether to abandon complete mode when the menu
2327 * is visible.
2328 */
2329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002330ins_compl_accept_char(int c)
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002331{
2332 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2333 /* When expanding an identifier only accept identifier chars. */
2334 return vim_isIDc(c);
2335
2336 switch (ctrl_x_mode)
2337 {
2338 case CTRL_X_FILES:
2339 /* When expanding file name only accept file name chars. But not
2340 * path separators, so that "proto/<Tab>" expands files in
2341 * "proto", not "proto/" as a whole */
2342 return vim_isfilec(c) && !vim_ispathsep(c);
2343
2344 case CTRL_X_CMDLINE:
2345 case CTRL_X_OMNI:
2346 /* Command line and Omni completion can work with just about any
2347 * printable character, but do stop at white space. */
2348 return vim_isprintc(c) && !vim_iswhite(c);
2349
2350 case CTRL_X_WHOLE_LINE:
2351 /* For while line completion a space can be part of the line. */
2352 return vim_isprintc(c);
2353 }
2354 return vim_iswordc(c);
2355}
2356
2357/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002358 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002359 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002360 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 * the rest of the word to be in -- webb
2362 */
2363 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002364ins_compl_add_infercase(
2365 char_u *str,
2366 int len,
2367 int icase,
2368 char_u *fname,
2369 int dir,
2370 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002372 char_u *p;
2373 int i, c;
2374 int actual_len; /* Take multi-byte characters */
2375 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002376 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002377 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378 int has_lower = FALSE;
2379 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002381 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002383 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384
Bram Moolenaara2993e12007-08-12 14:38:46 +00002385 /* Find actual length of completion. */
2386#ifdef FEAT_MBYTE
2387 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002389 p = str;
2390 actual_len = 0;
2391 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002392 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002393 mb_ptr_adv(p);
2394 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395 }
2396 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002397 else
2398#endif
2399 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400
Bram Moolenaara2993e12007-08-12 14:38:46 +00002401 /* Find actual length of original text. */
2402#ifdef FEAT_MBYTE
2403 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002405 p = compl_orig_text;
2406 actual_compl_length = 0;
2407 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002409 mb_ptr_adv(p);
2410 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 }
2412 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002413 else
2414#endif
2415 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002417 /* "actual_len" may be smaller than "actual_compl_length" when using
2418 * thesaurus, only use the minimum when comparing. */
2419 min_len = actual_len < actual_compl_length
2420 ? actual_len : actual_compl_length;
2421
Bram Moolenaara2993e12007-08-12 14:38:46 +00002422 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002423 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002424 if (wca != NULL)
2425 {
2426 p = str;
2427 for (i = 0; i < actual_len; ++i)
2428#ifdef FEAT_MBYTE
2429 if (has_mbyte)
2430 wca[i] = mb_ptr2char_adv(&p);
2431 else
2432#endif
2433 wca[i] = *(p++);
2434
2435 /* Rule 1: Were any chars converted to lower? */
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 (MB_ISLOWER(c))
2446 {
2447 has_lower = TRUE;
2448 if (MB_ISUPPER(wca[i]))
2449 {
2450 /* Rule 1 is satisfied. */
2451 for (i = actual_compl_length; i < actual_len; ++i)
2452 wca[i] = MB_TOLOWER(wca[i]);
2453 break;
2454 }
2455 }
2456 }
2457
2458 /*
2459 * Rule 2: No lower case, 2nd consecutive letter converted to
2460 * upper case.
2461 */
2462 if (!has_lower)
2463 {
2464 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002465 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002466 {
2467#ifdef FEAT_MBYTE
2468 if (has_mbyte)
2469 c = mb_ptr2char_adv(&p);
2470 else
2471#endif
2472 c = *(p++);
2473 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2474 {
2475 /* Rule 2 is satisfied. */
2476 for (i = actual_compl_length; i < actual_len; ++i)
2477 wca[i] = MB_TOUPPER(wca[i]);
2478 break;
2479 }
2480 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2481 }
2482 }
2483
2484 /* Copy the original case of the part we typed. */
2485 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002486 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002487 {
2488#ifdef FEAT_MBYTE
2489 if (has_mbyte)
2490 c = mb_ptr2char_adv(&p);
2491 else
2492#endif
2493 c = *(p++);
2494 if (MB_ISLOWER(c))
2495 wca[i] = MB_TOLOWER(wca[i]);
2496 else if (MB_ISUPPER(c))
2497 wca[i] = MB_TOUPPER(wca[i]);
2498 }
2499
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002500 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002501 * Generate encoding specific output from wide character array.
2502 * Multi-byte characters can occupy up to five bytes more than
2503 * ASCII characters, and we also need one byte for NUL, so stay
2504 * six bytes away from the edge of IObuff.
2505 */
2506 p = IObuff;
2507 i = 0;
2508 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2509#ifdef FEAT_MBYTE
2510 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002511 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002512 else
2513#endif
2514 *(p++) = wca[i++];
2515 *p = NUL;
2516
2517 vim_free(wca);
2518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002520 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2521 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002523 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524}
2525
2526/*
2527 * Add a match to the list of matches.
2528 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002529 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002530 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002532 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002533ins_compl_add(
2534 char_u *str,
2535 int len,
2536 int icase,
2537 char_u *fname,
2538 char_u **cptext, /* extra text for popup menu or NULL */
2539 int cdir,
2540 int flags,
2541 int adup) /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002543 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002544 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545
2546 ui_breakcheck();
2547 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002548 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (len < 0)
2550 len = (int)STRLEN(str);
2551
2552 /*
2553 * If the same match is already present, don't add it.
2554 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002555 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002557 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 do
2559 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002560 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002561 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002562 && match->cp_str[len] == NUL)
2563 return NOTDONE;
2564 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002565 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 }
2567
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002568 /* Remove any popup menu before changing the list of matches. */
2569 ins_compl_del_pum();
2570
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 /*
2572 * Allocate a new match structure.
2573 * Copy the values to the new match structure.
2574 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002575 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002577 return FAIL;
2578 match->cp_number = -1;
2579 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002580 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002581 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 {
2583 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002584 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002586 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002587
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002589 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2591 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002592 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002593 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002594 && compl_curr_match->cp_fname != NULL
2595 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002596 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002597 else if (fname != NULL)
2598 {
2599 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002600 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002603 match->cp_fname = NULL;
2604 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002605
2606 if (cptext != NULL)
2607 {
2608 int i;
2609
2610 for (i = 0; i < CPT_COUNT; ++i)
2611 if (cptext[i] != NULL && *cptext[i] != NUL)
2612 match->cp_text[i] = vim_strsave(cptext[i]);
2613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614
2615 /*
2616 * Link the new match structure in the list of matches.
2617 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002618 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002619 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 else if (dir == FORWARD)
2621 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002622 match->cp_next = compl_curr_match->cp_next;
2623 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625 else /* BACKWARD */
2626 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002627 match->cp_next = compl_curr_match;
2628 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002630 if (match->cp_next)
2631 match->cp_next->cp_prev = match;
2632 if (match->cp_prev)
2633 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002634 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002635 compl_first_match = match;
2636 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002638 /*
2639 * Find the longest common string if still doing that.
2640 */
2641 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2642 ins_compl_longest_match(match);
2643
Bram Moolenaar071d4272004-06-13 20:20:40 +00002644 return OK;
2645}
2646
2647/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002648 * Return TRUE if "str[len]" matches with match->cp_str, considering
2649 * match->cp_icase.
2650 */
2651 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002652ins_compl_equal(compl_T *match, char_u *str, int len)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002653{
2654 if (match->cp_icase)
2655 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2656 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2657}
2658
2659/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002660 * Reduce the longest common string for match "match".
2661 */
2662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002663ins_compl_longest_match(compl_T *match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002664{
2665 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002666 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002667 int had_match;
2668
2669 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002670 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002671 /* First match, use it as a whole. */
2672 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002673 if (compl_leader != NULL)
2674 {
2675 had_match = (curwin->w_cursor.col > compl_col);
2676 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002677 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002678 ins_redraw(FALSE);
2679
2680 /* When the match isn't there (to avoid matching itself) remove it
2681 * again after redrawing. */
2682 if (!had_match)
2683 ins_compl_delete();
2684 compl_used_match = FALSE;
2685 }
2686 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002687 else
2688 {
2689 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002690 p = compl_leader;
2691 s = match->cp_str;
2692 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002693 {
2694#ifdef FEAT_MBYTE
2695 if (has_mbyte)
2696 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002697 c1 = mb_ptr2char(p);
2698 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002699 }
2700 else
2701#endif
2702 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002703 c1 = *p;
2704 c2 = *s;
2705 }
2706 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2707 : (c1 != c2))
2708 break;
2709#ifdef FEAT_MBYTE
2710 if (has_mbyte)
2711 {
2712 mb_ptr_adv(p);
2713 mb_ptr_adv(s);
2714 }
2715 else
2716#endif
2717 {
2718 ++p;
2719 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002720 }
2721 }
2722
2723 if (*p != NUL)
2724 {
2725 /* Leader was shortened, need to change the inserted text. */
2726 *p = NUL;
2727 had_match = (curwin->w_cursor.col > compl_col);
2728 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002729 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002730 ins_redraw(FALSE);
2731
2732 /* When the match isn't there (to avoid matching itself) remove it
2733 * again after redrawing. */
2734 if (!had_match)
2735 ins_compl_delete();
2736 }
2737
2738 compl_used_match = FALSE;
2739 }
2740}
2741
2742/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 * Add an array of matches to the list of matches.
2744 * Frees matches[].
2745 */
2746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002747ins_compl_add_matches(
2748 int num_matches,
2749 char_u **matches,
2750 int icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751{
2752 int i;
2753 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002754 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002755
Bram Moolenaar572cb562005-08-05 21:35:02 +00002756 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002757 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002758 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002759 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002760 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002761 FreeWild(num_matches, matches);
2762}
2763
2764/* Make the completion list cyclic.
2765 * Return the number of matches (excluding the original).
2766 */
2767 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002768ins_compl_make_cyclic(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002769{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002770 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002771 int count = 0;
2772
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002773 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002774 {
2775 /*
2776 * Find the end of the list.
2777 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002778 match = compl_first_match;
2779 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002780 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002781 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002782 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002783 ++count;
2784 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002785 match->cp_next = compl_first_match;
2786 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002787 }
2788 return count;
2789}
2790
Bram Moolenaara94bc432006-03-10 21:42:59 +00002791/*
Bram Moolenaarc0200422016-04-20 12:02:02 +02002792 * Set variables that store noselect and noinsert behavior from the
2793 * 'completeopt' value.
2794 */
2795 void
Bram Moolenaarcf089462016-06-12 21:18:43 +02002796completeopt_was_set(void)
Bram Moolenaarc0200422016-04-20 12:02:02 +02002797{
2798 compl_no_insert = FALSE;
2799 compl_no_select = FALSE;
2800 if (strstr((char *)p_cot, "noselect") != NULL)
2801 compl_no_select = TRUE;
2802 if (strstr((char *)p_cot, "noinsert") != NULL)
2803 compl_no_insert = TRUE;
2804}
2805
2806/*
Bram Moolenaara94bc432006-03-10 21:42:59 +00002807 * Start completion for the complete() function.
2808 * "startcol" is where the matched text starts (1 is first column).
2809 * "list" is the list of matches.
2810 */
2811 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002812set_completion(colnr_T startcol, list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002813{
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002814 int save_w_wrow = curwin->w_wrow;
2815
Bram Moolenaara94bc432006-03-10 21:42:59 +00002816 /* If already doing completions stop it. */
2817 if (ctrl_x_mode != 0)
2818 ins_compl_prep(' ');
2819 ins_compl_clear();
2820
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002821 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002822 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002823 startcol = curwin->w_cursor.col;
2824 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002825 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002826 /* compl_pattern doesn't need to be set */
2827 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2828 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002829 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002830 return;
2831
Bram Moolenaare4214502015-03-05 18:08:43 +01002832 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002833
2834 ins_compl_add_list(list);
2835 compl_matches = ins_compl_make_cyclic();
2836 compl_started = TRUE;
2837 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002838 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002839
2840 compl_curr_match = compl_first_match;
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002841 if (compl_no_insert || compl_no_select)
2842 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002843 ins_complete(K_DOWN, FALSE);
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02002844 if (compl_no_select)
2845 /* Down/Up has no real effect. */
2846 ins_complete(K_UP, FALSE);
2847 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002848 else
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002849 ins_complete(Ctrl_N, FALSE);
Bram Moolenaar32b808a2016-07-09 21:57:20 +02002850 compl_enter_selects = compl_no_insert;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01002851
2852 /* Lazily show the popup menu, unless we got interrupted. */
2853 if (!compl_interrupted)
2854 show_pum(save_w_wrow);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002855 out_flush();
2856}
2857
2858
Bram Moolenaar9372a112005-12-06 19:59:18 +00002859/* "compl_match_array" points the currently displayed list of entries in the
2860 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002861static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002862static int compl_match_arraysize;
2863
2864/*
2865 * Update the screen and when there is any scrolling remove the popup menu.
2866 */
2867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002868ins_compl_upd_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002869{
2870 int h;
2871
2872 if (compl_match_array != NULL)
2873 {
2874 h = curwin->w_cline_height;
2875 update_screen(0);
2876 if (h != curwin->w_cline_height)
2877 ins_compl_del_pum();
2878 }
2879}
2880
2881/*
2882 * Remove any popup menu.
2883 */
2884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002885ins_compl_del_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002886{
2887 if (compl_match_array != NULL)
2888 {
2889 pum_undisplay();
2890 vim_free(compl_match_array);
2891 compl_match_array = NULL;
2892 }
2893}
2894
2895/*
2896 * Return TRUE if the popup menu should be displayed.
2897 */
2898 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002899pum_wanted(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002900{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002901 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002902 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002903 return FALSE;
2904
2905 /* The display looks bad on a B&W display. */
2906 if (t_colors < 8
2907#ifdef FEAT_GUI
2908 && !gui.in_use
2909#endif
2910 )
2911 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002912 return TRUE;
2913}
2914
2915/*
2916 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002917 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002918 */
2919 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920pum_enough_matches(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00002921{
2922 compl_T *compl;
2923 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002924
2925 /* Don't display the popup menu if there are no matches or there is only
2926 * one (ignoring the original text). */
2927 compl = compl_first_match;
2928 i = 0;
2929 do
2930 {
2931 if (compl == NULL
2932 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2933 break;
2934 compl = compl->cp_next;
2935 } while (compl != compl_first_match);
2936
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002937 if (strstr((char *)p_cot, "menuone") != NULL)
2938 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002939 return (i >= 2);
2940}
2941
2942/*
2943 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002944 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002945 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002947ins_compl_show_pum(void)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002948{
2949 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002950 compl_T *shown_compl = NULL;
2951 int did_find_shown_match = FALSE;
2952 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002953 int i;
2954 int cur = -1;
2955 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002956 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002957
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002958 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002959 return;
2960
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002961#if defined(FEAT_EVAL)
2962 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2963 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2964#endif
2965
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002966 /* Update the screen before drawing the popup menu over it. */
2967 update_screen(0);
2968
2969 if (compl_match_array == NULL)
2970 {
2971 /* Need to build the popup menu list. */
2972 compl_match_arraysize = 0;
2973 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002974 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002975 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002976 do
2977 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002978 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2979 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002980 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002981 ++compl_match_arraysize;
2982 compl = compl->cp_next;
2983 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002984 if (compl_match_arraysize == 0)
2985 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002986 compl_match_array = (pumitem_T *)alloc_clear(
2987 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002988 * compl_match_arraysize));
2989 if (compl_match_array != NULL)
2990 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002991 /* If the current match is the original text don't find the first
2992 * match after it, don't highlight anything. */
2993 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2994 shown_match_ok = TRUE;
2995
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002996 i = 0;
2997 compl = compl_first_match;
2998 do
2999 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003000 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
3001 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00003002 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003003 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003004 if (!shown_match_ok)
3005 {
3006 if (compl == compl_shown_match || did_find_shown_match)
3007 {
3008 /* This item is the shown match or this is the
3009 * first displayed item after the shown match. */
3010 compl_shown_match = compl;
3011 did_find_shown_match = TRUE;
3012 shown_match_ok = TRUE;
3013 }
3014 else
3015 /* Remember this displayed match for when the
3016 * shown match is just below it. */
3017 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003018 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003019 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00003020
3021 if (compl->cp_text[CPT_ABBR] != NULL)
3022 compl_match_array[i].pum_text =
3023 compl->cp_text[CPT_ABBR];
3024 else
3025 compl_match_array[i].pum_text = compl->cp_str;
3026 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
3027 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
3028 if (compl->cp_text[CPT_MENU] != NULL)
3029 compl_match_array[i++].pum_extra =
3030 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003031 else
3032 compl_match_array[i++].pum_extra = compl->cp_fname;
3033 }
3034
3035 if (compl == compl_shown_match)
3036 {
3037 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00003038
3039 /* When the original text is the shown match don't set
3040 * compl_shown_match. */
3041 if (compl->cp_flags & ORIGINAL_TEXT)
3042 shown_match_ok = TRUE;
3043
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003044 if (!shown_match_ok && shown_compl != NULL)
3045 {
3046 /* The shown match isn't displayed, set it to the
3047 * previously displayed match. */
3048 compl_shown_match = shown_compl;
3049 shown_match_ok = TRUE;
3050 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003051 }
3052 compl = compl->cp_next;
3053 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003054
3055 if (!shown_match_ok) /* no displayed match at all */
3056 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003057 }
3058 }
3059 else
3060 {
3061 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003062 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003063 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3064 || compl_match_array[i].pum_text
3065 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003066 {
3067 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003068 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003069 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003070 }
3071
3072 if (compl_match_array != NULL)
3073 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003074 /* In Replace mode when a $ is displayed at the end of the line only
3075 * part of the screen would be updated. We do need to redraw here. */
3076 dollar_vcol = -1;
3077
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003078 /* Compute the screen column of the start of the completed text.
3079 * Use the cursor to get all wrapping and other settings right. */
3080 col = curwin->w_cursor.col;
3081 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003082 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003083 curwin->w_cursor.col = col;
3084 }
3085}
3086
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087#define DICT_FIRST (1) /* use just first element in "dict" */
3088#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003089
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003091 * Add any identifiers that match the given pattern in the list of dictionary
3092 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 */
3094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003095ins_compl_dictionaries(
3096 char_u *dict_start,
3097 char_u *pat,
3098 int flags, /* DICT_FIRST and/or DICT_EXACT */
3099 int thesaurus) /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003100{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003101 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 char_u *ptr;
3103 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003104 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 char_u **files;
3106 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003107 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003108 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109
Bram Moolenaar0b238792006-03-02 22:49:12 +00003110 if (*dict == NUL)
3111 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003112#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003113 /* When 'dictionary' is empty and spell checking is enabled use
3114 * "spell". */
3115 if (!thesaurus && curwin->w_p_spell)
3116 dict = (char_u *)"spell";
3117 else
3118#endif
3119 return;
3120 }
3121
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003123 if (buf == NULL)
3124 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003125 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003126
Bram Moolenaar071d4272004-06-13 20:20:40 +00003127 /* If 'infercase' is set, don't use 'smartcase' here */
3128 save_p_scs = p_scs;
3129 if (curbuf->b_p_inf)
3130 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003131
3132 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3133 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003134 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003135 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003136 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003137 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003138 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003139
3140 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003141 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003142 len = STRLEN(pat_esc) + 10;
3143 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003144 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003145 {
3146 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003147 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003148 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003149 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003150 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3151 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003152 vim_free(ptr);
3153 }
3154 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003155 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003156 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003157 if (regmatch.regprog == NULL)
3158 goto theend;
3159 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003160
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3162 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003163 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 {
3165 /* copy one dictionary file name into buf */
3166 if (flags == DICT_EXACT)
3167 {
3168 count = 1;
3169 files = &dict;
3170 }
3171 else
3172 {
3173 /* Expand wildcards in the dictionary name, but do not allow
3174 * backticks (for security, the 'dict' option may have been set in
3175 * a modeline). */
3176 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003177# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003178 if (!thesaurus && STRCMP(buf, "spell") == 0)
3179 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003180 else
3181# endif
3182 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 || expand_wildcards(1, &buf, &count, &files,
3184 EW_FILE|EW_SILENT) != OK)
3185 count = 0;
3186 }
3187
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003188# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003189 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003190 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003191 /* Complete from active spelling. Skip "\<" in the pattern, we
3192 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003193 if (pat[0] == '\\' && pat[1] == '<')
3194 ptr = pat + 2;
3195 else
3196 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003197 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003198 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003199 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003200# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003201 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003202 {
3203 ins_compl_files(count, files, thesaurus, flags,
3204 &regmatch, buf, &dir);
3205 if (flags != DICT_EXACT)
3206 FreeWild(count, files);
3207 }
3208 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003209 break;
3210 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003211
3212theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003214 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003215 vim_free(buf);
3216}
3217
Bram Moolenaar0b238792006-03-02 22:49:12 +00003218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003219ins_compl_files(
3220 int count,
3221 char_u **files,
3222 int thesaurus,
3223 int flags,
3224 regmatch_T *regmatch,
3225 char_u *buf,
3226 int *dir)
Bram Moolenaar0b238792006-03-02 22:49:12 +00003227{
3228 char_u *ptr;
3229 int i;
3230 FILE *fp;
3231 int add_r;
3232
3233 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3234 {
3235 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3236 if (flags != DICT_EXACT)
3237 {
3238 vim_snprintf((char *)IObuff, IOSIZE,
3239 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003240 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003241 }
3242
3243 if (fp != NULL)
3244 {
3245 /*
3246 * Read dictionary file line by line.
3247 * Check each line for a match.
3248 */
3249 while (!got_int && !compl_interrupted
3250 && !vim_fgets(buf, LSIZE, fp))
3251 {
3252 ptr = buf;
3253 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3254 {
3255 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003256 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003257 ptr = find_line_end(ptr);
3258 else
3259 ptr = find_word_end(ptr);
3260 add_r = ins_compl_add_infercase(regmatch->startp[0],
3261 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003262 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003263 if (thesaurus)
3264 {
3265 char_u *wstart;
3266
3267 /*
3268 * Add the other matches on the line
3269 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003270 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003271 while (!got_int)
3272 {
3273 /* Find start of the next word. Skip white
3274 * space and punctuation. */
3275 ptr = find_word_start(ptr);
3276 if (*ptr == NUL || *ptr == NL)
3277 break;
3278 wstart = ptr;
3279
Bram Moolenaara2993e12007-08-12 14:38:46 +00003280 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003281#ifdef FEAT_MBYTE
3282 if (has_mbyte)
3283 /* Japanese words may have characters in
3284 * different classes, only separate words
3285 * with single-byte non-word characters. */
3286 while (*ptr != NUL)
3287 {
3288 int l = (*mb_ptr2len)(ptr);
3289
3290 if (l < 2 && !vim_iswordc(*ptr))
3291 break;
3292 ptr += l;
3293 }
3294 else
3295#endif
3296 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003297
3298 /* Add the word. Skip the regexp match. */
3299 if (wstart != regmatch->startp[0])
3300 add_r = ins_compl_add_infercase(wstart,
3301 (int)(ptr - wstart),
3302 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003303 }
3304 }
3305 if (add_r == OK)
3306 /* if dir was BACKWARD then honor it just once */
3307 *dir = FORWARD;
3308 else if (add_r == FAIL)
3309 break;
3310 /* avoid expensive call to vim_regexec() when at end
3311 * of line */
3312 if (*ptr == '\n' || got_int)
3313 break;
3314 }
3315 line_breakcheck();
Bram Moolenaar472e8592016-10-15 17:06:47 +02003316 ins_compl_check_keys(50, FALSE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003317 }
3318 fclose(fp);
3319 }
3320 }
3321}
3322
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323/*
3324 * Find the start of the next word.
3325 * Returns a pointer to the first char of the word. Also stops at a NUL.
3326 */
3327 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328find_word_start(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003329{
3330#ifdef FEAT_MBYTE
3331 if (has_mbyte)
3332 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003333 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 else
3335#endif
3336 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3337 ++ptr;
3338 return ptr;
3339}
3340
3341/*
3342 * Find the end of the word. Assumes it starts inside a word.
3343 * Returns a pointer to just after the word.
3344 */
3345 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003346find_word_end(char_u *ptr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348#ifdef FEAT_MBYTE
3349 int start_class;
3350
3351 if (has_mbyte)
3352 {
3353 start_class = mb_get_class(ptr);
3354 if (start_class > 1)
3355 while (*ptr != NUL)
3356 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003357 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003358 if (mb_get_class(ptr) != start_class)
3359 break;
3360 }
3361 }
3362 else
3363#endif
3364 while (vim_iswordc(*ptr))
3365 ++ptr;
3366 return ptr;
3367}
3368
3369/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003370 * Find the end of the line, omitting CR and NL at the end.
3371 * Returns a pointer to just after the line.
3372 */
3373 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003374find_line_end(char_u *ptr)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003375{
3376 char_u *s;
3377
3378 s = ptr + STRLEN(ptr);
3379 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3380 --s;
3381 return s;
3382}
3383
3384/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 * Free the list of completions
3386 */
3387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003388ins_compl_free(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003390 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003391 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003393 vim_free(compl_pattern);
3394 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003395 vim_free(compl_leader);
3396 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003398 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003400
3401 ins_compl_del_pum();
3402 pum_clear();
3403
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003404 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 do
3406 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003407 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003408 compl_curr_match = compl_curr_match->cp_next;
3409 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003410 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003411 if (match->cp_flags & FREE_FNAME)
3412 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003413 for (i = 0; i < CPT_COUNT; ++i)
3414 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003416 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3417 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003418 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419}
3420
3421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003422ins_compl_clear(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003423{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003424 compl_cont_status = 0;
3425 compl_started = FALSE;
3426 compl_matches = 0;
3427 vim_free(compl_pattern);
3428 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003429 vim_free(compl_leader);
3430 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003431 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003432 vim_free(compl_orig_text);
3433 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003434 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003435 /* clear v:completed_item */
3436 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437}
3438
3439/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003440 * Return TRUE when Insert completion is active.
3441 */
3442 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003443ins_compl_active(void)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003444{
3445 return compl_started;
3446}
3447
3448/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003449 * Delete one character before the cursor and show the subset of the matches
3450 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003451 * Returns the character to be used, NUL if the work is done and another char
3452 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003453 */
3454 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003455ins_compl_bs(void)
Bram Moolenaara6557602006-02-04 22:43:20 +00003456{
3457 char_u *line;
3458 char_u *p;
3459
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003460 line = ml_get_curline();
3461 p = line + curwin->w_cursor.col;
3462 mb_ptr_back(line, p);
3463
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003464 /* Stop completion when the whole word was deleted. For Omni completion
3465 * allow the word to be deleted, we won't match everything. */
3466 if ((int)(p - line) - (int)compl_col < 0
3467 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003468 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003469 return K_BS;
3470
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003471 /* Deleted more than what was used to find matches or didn't finish
3472 * finding all matches: need to look for matches all over again. */
3473 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003474 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003475 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003476
Bram Moolenaara6557602006-02-04 22:43:20 +00003477 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003478 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003479 if (compl_leader != NULL)
3480 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003481 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003482 if (compl_shown_match != NULL)
3483 /* Make sure current match is not a hidden item. */
3484 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003485 return NUL;
3486 }
3487 return K_BS;
3488}
Bram Moolenaara6557602006-02-04 22:43:20 +00003489
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003490/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003491 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3492 * be called.
3493 */
3494 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003495ins_compl_need_restart(void)
Bram Moolenaar82139082011-09-14 16:52:09 +02003496{
3497 /* Return TRUE if we didn't complete finding matches or when the
3498 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3499 return compl_was_interrupted
3500 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3501 && compl_opt_refresh_always);
3502}
3503
3504/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003505 * Called after changing "compl_leader".
3506 * Show the popup menu with a different set of matches.
3507 * May also search for matches again if the previous search was interrupted.
3508 */
3509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003510ins_compl_new_leader(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003511{
3512 ins_compl_del_pum();
3513 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003514 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003515 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003516
3517 if (compl_started)
3518 ins_compl_set_original_text(compl_leader);
3519 else
3520 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003521#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003522 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003523#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003524 /*
3525 * Matches were cleared, need to search for them now. First display
3526 * the changed text before the cursor. Set "compl_restarting" to
3527 * avoid that the first match is inserted.
3528 */
3529 update_screen(0);
3530#ifdef FEAT_GUI
3531 if (gui.in_use)
3532 {
3533 /* Show the cursor after the match, not after the redrawn text. */
3534 setcursor();
3535 out_flush();
3536 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003537 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003538#endif
3539 compl_restarting = TRUE;
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01003540 if (ins_complete(Ctrl_N, TRUE) == FAIL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003541 compl_cont_status = 0;
3542 compl_restarting = FALSE;
3543 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003544
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003545 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003546
3547 /* Show the popup menu with a different set of matches. */
3548 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003549
3550 /* Don't let Enter select the original text when there is no popup menu. */
3551 if (compl_match_array == NULL)
3552 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003553}
3554
3555/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003556 * Return the length of the completion, from the completion start column to
3557 * the cursor column. Making sure it never goes below zero.
3558 */
3559 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003560ins_compl_len(void)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003561{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003562 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003563
3564 if (off < 0)
3565 return 0;
3566 return off;
3567}
3568
3569/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003570 * Append one character to the match leader. May reduce the number of
3571 * matches.
3572 */
3573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003574ins_compl_addleader(int c)
Bram Moolenaara6557602006-02-04 22:43:20 +00003575{
3576#ifdef FEAT_MBYTE
3577 int cc;
3578
3579 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3580 {
3581 char_u buf[MB_MAXBYTES + 1];
3582
3583 (*mb_char2bytes)(c, buf);
3584 buf[cc] = NUL;
3585 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003586 if (compl_opt_refresh_always)
3587 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003588 }
3589 else
3590#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003591 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003592 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003593 if (compl_opt_refresh_always)
3594 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003595 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003596
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003597 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003598 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003599 ins_compl_restart();
3600
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003601 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003602 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003603 * break redo. */
3604 if (!compl_opt_refresh_always)
3605 {
3606 vim_free(compl_leader);
3607 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003608 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003609 if (compl_leader != NULL)
3610 ins_compl_new_leader();
3611 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003612}
3613
3614/*
3615 * Setup for finding completions again without leaving CTRL-X mode. Used when
3616 * BS or a key was typed while still searching for matches.
3617 */
3618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003619ins_compl_restart(void)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003620{
3621 ins_compl_free();
3622 compl_started = FALSE;
3623 compl_matches = 0;
3624 compl_cont_status = 0;
3625 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003626}
3627
3628/*
3629 * Set the first match, the original text.
3630 */
3631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003632ins_compl_set_original_text(char_u *str)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003633{
3634 char_u *p;
3635
3636 /* Replace the original text entry. */
3637 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3638 {
3639 p = vim_strsave(str);
3640 if (p != NULL)
3641 {
3642 vim_free(compl_first_match->cp_str);
3643 compl_first_match->cp_str = p;
3644 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003645 }
3646}
3647
3648/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003649 * Append one character to the match leader. May reduce the number of
3650 * matches.
3651 */
3652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003653ins_compl_addfrommatch(void)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003654{
3655 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003656 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003657 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003658 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003659
3660 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003661 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003662 {
3663 /* When still at the original match use the first entry that matches
3664 * the leader. */
3665 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3666 {
3667 p = NULL;
3668 for (cp = compl_shown_match->cp_next; cp != NULL
3669 && cp != compl_first_match; cp = cp->cp_next)
3670 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003671 if (compl_leader == NULL
3672 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003673 (int)STRLEN(compl_leader)))
3674 {
3675 p = cp->cp_str;
3676 break;
3677 }
3678 }
3679 if (p == NULL || (int)STRLEN(p) <= len)
3680 return;
3681 }
3682 else
3683 return;
3684 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003685 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003686 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003687 ins_compl_addleader(c);
3688}
3689
3690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003691 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003692 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003693 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003694 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003696ins_compl_prep(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697{
3698 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003700 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701
3702 /* Forget any previous 'special' messages if this is actually
3703 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3704 */
3705 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3706 edit_submode_extra = NULL;
3707
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003708 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003709 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3710 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003711 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003713 /* Set "compl_get_longest" when finding the first matches. */
3714 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3715 || (ctrl_x_mode == 0 && !compl_started))
3716 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003717 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003718 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003719
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003720 }
3721
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3723 {
3724 /*
3725 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3726 * it will be yet. Now we decide.
3727 */
3728 switch (c)
3729 {
3730 case Ctrl_E:
3731 case Ctrl_Y:
3732 ctrl_x_mode = CTRL_X_SCROLL;
3733 if (!(State & REPLACE_FLAG))
3734 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3735 else
3736 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3737 edit_submode_pre = NULL;
3738 showmode();
3739 break;
3740 case Ctrl_L:
3741 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3742 break;
3743 case Ctrl_F:
3744 ctrl_x_mode = CTRL_X_FILES;
3745 break;
3746 case Ctrl_K:
3747 ctrl_x_mode = CTRL_X_DICTIONARY;
3748 break;
3749 case Ctrl_R:
3750 /* Simply allow ^R to happen without affecting ^X mode */
3751 break;
3752 case Ctrl_T:
3753 ctrl_x_mode = CTRL_X_THESAURUS;
3754 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003755#ifdef FEAT_COMPL_FUNC
3756 case Ctrl_U:
3757 ctrl_x_mode = CTRL_X_FUNCTION;
3758 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003759 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003760 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003761 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003762#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003763 case 's':
3764 case Ctrl_S:
3765 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003766#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003767 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003768 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003769 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003770#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003771 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 case Ctrl_RSB:
3773 ctrl_x_mode = CTRL_X_TAGS;
3774 break;
3775#ifdef FEAT_FIND_ID
3776 case Ctrl_I:
3777 case K_S_TAB:
3778 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3779 break;
3780 case Ctrl_D:
3781 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3782 break;
3783#endif
3784 case Ctrl_V:
3785 case Ctrl_Q:
3786 ctrl_x_mode = CTRL_X_CMDLINE;
3787 break;
3788 case Ctrl_P:
3789 case Ctrl_N:
3790 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3791 * just started ^X mode, or there were enough ^X's to cancel
3792 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3793 * do normal expansion when interrupting a different mode (say
3794 * ^X^F^X^P or ^P^X^X^P, see below)
3795 * nothing changes if interrupting mode 0, (eg, the flag
3796 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003797 if (!(compl_cont_status & CONT_INTRPT))
3798 compl_cont_status |= CONT_LOCAL;
3799 else if (compl_cont_mode != 0)
3800 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 /* FALLTHROUGH */
3802 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003803 /* If we have typed at least 2 ^X's... for modes != 0, we set
3804 * compl_cont_status = 0 (eg, as if we had just started ^X
3805 * mode).
3806 * For mode 0, we set "compl_cont_mode" to an impossible
3807 * value, in both cases ^X^X can be used to restart the same
3808 * mode (avoiding ADDING mode).
3809 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3810 * 'complete' and local ^P expansions respectively.
3811 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3812 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (c == Ctrl_X)
3814 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003815 if (compl_cont_mode != 0)
3816 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003817 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003818 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 }
3820 ctrl_x_mode = 0;
3821 edit_submode = NULL;
3822 showmode();
3823 break;
3824 }
3825 }
3826 else if (ctrl_x_mode != 0)
3827 {
3828 /* We're already in CTRL-X mode, do we stay in it? */
3829 if (!vim_is_ctrl_x_key(c))
3830 {
3831 if (ctrl_x_mode == CTRL_X_SCROLL)
3832 ctrl_x_mode = 0;
3833 else
3834 ctrl_x_mode = CTRL_X_FINISHED;
3835 edit_submode = NULL;
3836 }
3837 showmode();
3838 }
3839
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003840 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 {
3842 /* Show error message from attempted keyword completion (probably
3843 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003844 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003845 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003846 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3847 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 || ctrl_x_mode == CTRL_X_FINISHED)
3849 {
3850 /* Get here when we have finished typing a sequence of ^N and
3851 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003852 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003853 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 {
3855 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003856 * If any of the original typed text has been changed, eg when
3857 * ignorecase is set, we must add back-spaces to the redo
3858 * buffer. We add as few as necessary to delete just the part
3859 * of the original text that has changed.
3860 * When using the longest match, edited the match or used
3861 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003863 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3864 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003865 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003866 ptr = NULL;
3867 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868 }
3869
3870#ifdef FEAT_CINDENT
3871 want_cindent = (can_cindent && cindent_on());
3872#endif
3873 /*
3874 * When completing whole lines: fix indent for 'cindent'.
3875 * Otherwise, break line if it's too long.
3876 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003877 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 {
3879#ifdef FEAT_CINDENT
3880 /* re-indent the current line */
3881 if (want_cindent)
3882 {
3883 do_c_expr_indent();
3884 want_cindent = FALSE; /* don't do it again */
3885 }
3886#endif
3887 }
3888 else
3889 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003890 int prev_col = curwin->w_cursor.col;
3891
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003893 if (prev_col > 0)
3894 dec_cursor();
Bram Moolenaar869e3522016-10-16 15:35:47 +02003895 /* only format when something was inserted */
Bram Moolenaar73fd4982016-12-09 19:36:56 +01003896 if (!arrow_used && !ins_need_undo && c != Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003898 if (prev_col > 0
3899 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3900 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 }
3902
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003903 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003904 * the selection without inserting anything. When
3905 * compl_enter_selects is set the Enter key does the same. */
3906 if ((c == Ctrl_Y || (compl_enter_selects
3907 && (c == CAR || c == K_KENTER || c == NL)))
3908 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003909 retval = TRUE;
3910
Bram Moolenaar47247282016-08-02 22:36:02 +02003911 /* CTRL-E means completion is Ended, go back to the typed text.
3912 * but only do this, if the Popup is still visible */
Bram Moolenaarc9fb77c2016-08-09 21:51:40 +02003913 if (c == Ctrl_E)
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003914 {
3915 ins_compl_delete();
3916 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003917 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003918 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003919 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003920 retval = TRUE;
3921 }
3922
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003923 auto_format(FALSE, TRUE);
3924
Bram Moolenaar071d4272004-06-13 20:20:40 +00003925 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003926 compl_started = FALSE;
3927 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003928 if (!shortmess(SHM_COMPLETIONMENU))
3929 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003931 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003932 if (edit_submode != NULL)
3933 {
3934 edit_submode = NULL;
3935 showmode();
3936 }
3937
Bram Moolenaar5f1fea22015-09-25 19:12:22 +02003938#ifdef FEAT_CMDWIN
3939 if (c == Ctrl_C && cmdwin_type != 0)
3940 /* Avoid the popup menu remains displayed when leaving the
3941 * command line window. */
3942 update_screen(0);
3943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944#ifdef FEAT_CINDENT
3945 /*
3946 * Indent now if a key was typed that is in 'cinkeys'.
3947 */
3948 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3949 do_c_expr_indent();
3950#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003951#ifdef FEAT_AUTOCMD
3952 /* Trigger the CompleteDone event to give scripts a chance to act
3953 * upon the completion. */
3954 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 }
3957 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003958#ifdef FEAT_AUTOCMD
3959 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3960 /* Trigger the CompleteDone event to give scripts a chance to act
3961 * upon the (possibly failed) completion. */
3962 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3963#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
3965 /* reset continue_* if we left expansion-mode, if we stay they'll be
3966 * (re)set properly in ins_complete() */
3967 if (!vim_is_ctrl_x_key(c))
3968 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003969 compl_cont_status = 0;
3970 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003972
3973 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974}
3975
3976/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003977 * Fix the redo buffer for the completion leader replacing some of the typed
3978 * text. This inserts backspaces and appends the changed text.
3979 * "ptr" is the known leader text or NUL.
3980 */
3981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003982ins_compl_fixRedoBufForLeader(char_u *ptr_arg)
Bram Moolenaar62951b12011-09-21 18:23:05 +02003983{
3984 int len;
3985 char_u *p;
3986 char_u *ptr = ptr_arg;
3987
3988 if (ptr == NULL)
3989 {
3990 if (compl_leader != NULL)
3991 ptr = compl_leader;
3992 else
3993 return; /* nothing to do */
3994 }
3995 if (compl_orig_text != NULL)
3996 {
3997 p = compl_orig_text;
3998 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3999 ;
4000#ifdef FEAT_MBYTE
4001 if (len > 0)
4002 len -= (*mb_head_off)(p, p + len);
4003#endif
4004 for (p += len; *p != NUL; mb_ptr_adv(p))
4005 AppendCharToRedobuff(K_BS);
4006 }
4007 else
4008 len = 0;
4009 if (ptr != NULL)
4010 AppendToRedobuffLit(ptr + len, -1);
4011}
4012
4013/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004014 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
4015 * (depending on flag) starting from buf and looking for a non-scanned
4016 * buffer (other than curbuf). curbuf is special, if it is called with
4017 * buf=curbuf then it has to be the first call for a given flag/expansion.
4018 *
4019 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
4020 */
4021 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004022ins_compl_next_buf(buf_T *buf, int flag)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023{
4024#ifdef FEAT_WINDOWS
4025 static win_T *wp;
4026#endif
4027
4028 if (flag == 'w') /* just windows */
4029 {
4030#ifdef FEAT_WINDOWS
4031 if (buf == curbuf) /* first call for this flag/expansion */
4032 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004033 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 && wp->w_buffer->b_scanned)
4035 ;
4036 buf = wp->w_buffer;
4037#else
4038 buf = curbuf;
4039#endif
4040 }
4041 else
4042 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4043 * (unlisted buffers)
4044 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004045 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 && ((flag == 'U'
4047 ? buf->b_p_bl
4048 : (!buf->b_p_bl
4049 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004050 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 ;
4052 return buf;
4053}
4054
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004055#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004056/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004057 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004058 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004059 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004061expand_by_function(
4062 int type, /* CTRL_X_OMNI or CTRL_X_FUNCTION */
4063 char_u *base)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004064{
Bram Moolenaar82139082011-09-14 16:52:09 +02004065 list_T *matchlist = NULL;
4066 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004067 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004068 char_u *funcname;
4069 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004070 win_T *curwin_save;
4071 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004072 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004073
Bram Moolenaare344bea2005-09-01 20:46:49 +00004074 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4075 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004076 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004077
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004078 /* Call 'completefunc' to obtain the list of matches. */
4079 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004080 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004081
Bram Moolenaare344bea2005-09-01 20:46:49 +00004082 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004083 curwin_save = curwin;
4084 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004085
4086 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004087 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004088 {
4089 switch (rettv.v_type)
4090 {
4091 case VAR_LIST:
4092 matchlist = rettv.vval.v_list;
4093 break;
4094 case VAR_DICT:
4095 matchdict = rettv.vval.v_dict;
4096 break;
4097 default:
4098 /* TODO: Give error message? */
4099 clear_tv(&rettv);
4100 break;
4101 }
4102 }
4103
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004104 if (curwin_save != curwin || curbuf_save != curbuf)
4105 {
4106 EMSG(_(e_complwin));
4107 goto theend;
4108 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004109 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004110 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004111 if (!equalpos(curwin->w_cursor, pos))
4112 {
4113 EMSG(_(e_compldel));
4114 goto theend;
4115 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004116
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004117 if (matchlist != NULL)
4118 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004119 else if (matchdict != NULL)
4120 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004121
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004122theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004123 if (matchdict != NULL)
4124 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004125 if (matchlist != NULL)
4126 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004127}
4128#endif /* FEAT_COMPL_FUNC */
4129
Bram Moolenaar39f05632006-03-19 22:15:26 +00004130#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004131/*
4132 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004133 */
4134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004135ins_compl_add_list(list_T *list)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004136{
4137 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004138 int dir = compl_direction;
4139
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004140 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004141 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004142 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004143 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4144 /* if dir was BACKWARD then honor it just once */
4145 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004146 else if (did_emsg)
4147 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004148 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004149}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004150
4151/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004152 * Add completions from a dict.
4153 */
4154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004155ins_compl_add_dict(dict_T *dict)
Bram Moolenaar82139082011-09-14 16:52:09 +02004156{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004157 dictitem_T *di_refresh;
4158 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004159
4160 /* Check for optional "refresh" item. */
4161 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004162 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4163 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004164 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004165 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004166
4167 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4168 compl_opt_refresh_always = TRUE;
4169 }
4170
4171 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004172 di_words = dict_find(dict, (char_u *)"words", 5);
4173 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4174 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004175}
4176
4177/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004178 * Add a match to the list of matches from a typeval_T.
4179 * If the given string is already in the list of completions, then return
4180 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4181 * maybe because alloc() returns NULL, then FAIL is returned.
4182 */
4183 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004184ins_compl_add_tv(typval_T *tv, int dir)
Bram Moolenaar39f05632006-03-19 22:15:26 +00004185{
4186 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004187 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004188 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004189 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004190 char_u *(cptext[CPT_COUNT]);
4191
4192 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4193 {
4194 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4195 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4196 (char_u *)"abbr", FALSE);
4197 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4198 (char_u *)"menu", FALSE);
4199 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4200 (char_u *)"kind", FALSE);
4201 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4202 (char_u *)"info", FALSE);
4203 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4204 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004205 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004206 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004207 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4208 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004209 }
4210 else
4211 {
4212 word = get_tv_string_chk(tv);
4213 vim_memset(cptext, 0, sizeof(cptext));
4214 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004215 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004216 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004217 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004218}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004219#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004220
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004221/*
4222 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004223 * The search starts at position "ini" in curbuf and in the direction
4224 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004225 * When "compl_started" is FALSE start at that position, otherwise continue
4226 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004227 * This may return before finding all the matches.
4228 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 */
4230 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004231ins_compl_get_exp(pos_T *ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232{
4233 static pos_T first_match_pos;
4234 static pos_T last_match_pos;
4235 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004236 static int found_all = FALSE; /* Found all matches of a
4237 certain type. */
4238 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239
Bram Moolenaar572cb562005-08-05 21:35:02 +00004240 pos_T *pos;
4241 char_u **matches;
4242 int save_p_scs;
4243 int save_p_ws;
4244 int save_p_ic;
4245 int i;
4246 int num_matches;
4247 int len;
4248 int found_new_match;
4249 int type = ctrl_x_mode;
4250 char_u *ptr;
4251 char_u *dict = NULL;
4252 int dict_f = 0;
4253 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004254 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004256 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 {
Bram Moolenaar29323592016-07-24 22:04:11 +02004258 FOR_ALL_BUFFERS(ins_buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 ins_buf->b_scanned = 0;
4260 found_all = FALSE;
4261 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004263 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 last_match_pos = first_match_pos = *ini;
4265 }
4266
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004267 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004268 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4270 for (;;)
4271 {
4272 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004273 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 * or if found_all says this entry is done. For ^X^L only use the
4277 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004278 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004279 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 {
4281 found_all = FALSE;
4282 while (*e_cpt == ',' || *e_cpt == ' ')
4283 e_cpt++;
4284 if (*e_cpt == '.' && !curbuf->b_scanned)
4285 {
4286 ins_buf = curbuf;
4287 first_match_pos = *ini;
4288 /* So that ^N can match word immediately after cursor */
4289 if (ctrl_x_mode == 0)
4290 dec(&first_match_pos);
4291 last_match_pos = first_match_pos;
4292 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004293
4294 /* Remember the first match so that the loop stops when we
4295 * wrap and come back there a second time. */
4296 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4299 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4300 {
4301 /* Scan a buffer, but not the current one. */
4302 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4303 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004304 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 first_match_pos.col = last_match_pos.col = 0;
4306 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4307 last_match_pos.lnum = 0;
4308 type = 0;
4309 }
4310 else /* unloaded buffer, scan like dictionary */
4311 {
4312 found_all = TRUE;
4313 if (ins_buf->b_fname == NULL)
4314 continue;
4315 type = CTRL_X_DICTIONARY;
4316 dict = ins_buf->b_fname;
4317 dict_f = DICT_EXACT;
4318 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004319 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 ins_buf->b_fname == NULL
4321 ? buf_spname(ins_buf)
4322 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004323 ? ins_buf->b_fname
4324 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004325 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 }
4327 else if (*e_cpt == NUL)
4328 break;
4329 else
4330 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004331 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 type = -1;
4333 else if (*e_cpt == 'k' || *e_cpt == 's')
4334 {
4335 if (*e_cpt == 'k')
4336 type = CTRL_X_DICTIONARY;
4337 else
4338 type = CTRL_X_THESAURUS;
4339 if (*++e_cpt != ',' && *e_cpt != NUL)
4340 {
4341 dict = e_cpt;
4342 dict_f = DICT_FIRST;
4343 }
4344 }
4345#ifdef FEAT_FIND_ID
4346 else if (*e_cpt == 'i')
4347 type = CTRL_X_PATH_PATTERNS;
4348 else if (*e_cpt == 'd')
4349 type = CTRL_X_PATH_DEFINES;
4350#endif
4351 else if (*e_cpt == ']' || *e_cpt == 't')
4352 {
4353 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004354 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004355 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 else
4358 type = -1;
4359
4360 /* in any case e_cpt is advanced to the next entry */
4361 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4362
4363 found_all = TRUE;
4364 if (type == -1)
4365 continue;
4366 }
4367 }
4368
4369 switch (type)
4370 {
4371 case -1:
4372 break;
4373#ifdef FEAT_FIND_ID
4374 case CTRL_X_PATH_PATTERNS:
4375 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004376 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004377 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4381 (linenr_T)1, (linenr_T)MAXLNUM);
4382 break;
4383#endif
4384
4385 case CTRL_X_DICTIONARY:
4386 case CTRL_X_THESAURUS:
4387 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004388 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 : (type == CTRL_X_THESAURUS
4390 ? (*curbuf->b_p_tsr == NUL
4391 ? p_tsr
4392 : curbuf->b_p_tsr)
4393 : (*curbuf->b_p_dict == NUL
4394 ? p_dict
4395 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004396 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004397 dict != NULL ? dict_f
4398 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 dict = NULL;
4400 break;
4401
4402 case CTRL_X_TAGS:
4403 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4404 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004405 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004407 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004408 * of matches is found when compl_pattern is empty */
4409 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4411 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4412 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4413 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004414 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 }
4416 p_ic = save_p_ic;
4417 break;
4418
4419 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4422 {
4423
4424 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004426 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 }
4428 break;
4429
4430 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004431 if (expand_cmdline(&compl_xp, compl_pattern,
4432 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004434 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 break;
4436
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004437#ifdef FEAT_COMPL_FUNC
4438 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004439 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004440 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004441 break;
4442#endif
4443
Bram Moolenaar488c6512005-08-11 20:09:58 +00004444 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004445#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004446 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004447 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004448 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004449 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004450#endif
4451 break;
4452
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 default: /* normal ^P/^N and ^X^L */
4454 /*
4455 * If 'infercase' is set, don't use 'smartcase' here
4456 */
4457 save_p_scs = p_scs;
4458 if (ins_buf->b_p_inf)
4459 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004460
Bram Moolenaar361aa502014-01-23 22:45:58 +01004461 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 * end but never from the middle, thus setting nowrapscan in this
4463 * buffers is a good idea, on the other hand, we always set
4464 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4465 save_p_ws = p_ws;
4466 if (ins_buf != curbuf)
4467 p_ws = FALSE;
4468 else if (*e_cpt == '.')
4469 p_ws = TRUE;
4470 for (;;)
4471 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004472 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004474 ++msg_silent; /* Don't want messages for wrapscan. */
4475
Bram Moolenaare4214502015-03-05 18:08:43 +01004476 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4477 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004478 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004479 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004482 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004484 found_new_match = searchit(NULL, ins_buf, pos,
4485 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004487 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004488 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004489 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004491 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004492 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 first_match_pos = *pos;
4494 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004495 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
4497 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004498 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 found_new_match = FAIL;
4500 if (found_new_match == FAIL)
4501 {
4502 if (ins_buf == curbuf)
4503 found_all = TRUE;
4504 break;
4505 }
4506
4507 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509 && ini->lnum == pos->lnum
4510 && ini->col == pos->col)
4511 continue;
4512 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004513 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004514 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004515 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
4517 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4518 continue;
4519 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4520 if (!p_paste)
4521 ptr = skipwhite(ptr);
4522 }
4523 len = (int)STRLEN(ptr);
4524 }
4525 else
4526 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004527 char_u *tmp_ptr = ptr;
4528
4529 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004531 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 /* Skip if already inside a word. */
4533 if (vim_iswordp(tmp_ptr))
4534 continue;
4535 /* Find start of next word. */
4536 tmp_ptr = find_word_start(tmp_ptr);
4537 }
4538 /* Find end of this word. */
4539 tmp_ptr = find_word_end(tmp_ptr);
4540 len = (int)(tmp_ptr - ptr);
4541
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004542 if ((compl_cont_status & CONT_ADDING)
4543 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
4545 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4546 {
4547 /* Try next line, if any. the new word will be
4548 * "join" as if the normal command "J" was used.
4549 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 * works -- Acevedo */
4552 STRNCPY(IObuff, ptr, len);
4553 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4554 tmp_ptr = ptr = skipwhite(ptr);
4555 /* Find start of next word. */
4556 tmp_ptr = find_word_start(tmp_ptr);
4557 /* Find end of next word. */
4558 tmp_ptr = find_word_end(tmp_ptr);
4559 if (tmp_ptr > ptr)
4560 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004561 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004563 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 IObuff[len++] = ' ';
4565 /* IObuf =~ "\k.* ", thus len >= 2 */
4566 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004567 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 || (vim_strchr(p_cpo, CPO_JOINSP)
4569 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004570 && (IObuff[len - 2] == '?'
4571 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 IObuff[len++] = ' ';
4573 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004574 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (tmp_ptr - ptr >= IOSIZE - len)
4576 tmp_ptr = ptr + IOSIZE - len - 1;
4577 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4578 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004579 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 }
4581 IObuff[len] = NUL;
4582 ptr = IObuff;
4583 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004584 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 continue;
4586 }
4587 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004588 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004589 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004590 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 {
4592 found_new_match = OK;
4593 break;
4594 }
4595 }
4596 p_scs = save_p_scs;
4597 p_ws = save_p_ws;
4598 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004599
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004600 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004601 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004602 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 found_new_match = OK;
4604
4605 /* break the loop for specialized modes (use 'complete' just for the
4606 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004607 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004608 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004609 {
4610 if (got_int)
4611 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004612 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004613 if (type != -1)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004614 ins_compl_check_keys(0, FALSE);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004615
Bram Moolenaare4214502015-03-05 18:08:43 +01004616 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004617 || compl_interrupted)
4618 break;
4619 compl_started = TRUE;
4620 }
4621 else
4622 {
4623 /* Mark a buffer scanned when it has been scanned completely */
4624 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4625 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004627 compl_started = FALSE;
4628 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004630 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631
Bram Moolenaare4214502015-03-05 18:08:43 +01004632 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 && *e_cpt == NUL) /* Got to end of 'complete' */
4634 found_new_match = FAIL;
4635
4636 i = -1; /* total of matches, unknown */
4637 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004638 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 i = ins_compl_make_cyclic();
4640
4641 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004642 * just been made cyclic then we have to move compl_curr_match to the next
4643 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004644 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4645 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004646 if (compl_curr_match == NULL)
4647 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 return i;
4649}
4650
4651/* Delete the old text being completed. */
4652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01004653ins_compl_delete(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004654{
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004655 int col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004656
4657 /*
4658 * In insert mode: Delete the typed part.
4659 * In replace mode: Put the old characters back, if any.
4660 */
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02004661 col = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
4662 if ((int)curwin->w_cursor.col > col)
4663 {
4664 if (stop_arrow() == FAIL)
4665 return;
4666 backspace_until_column(col);
4667 }
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004668
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004669 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4670 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004672 /* clear v:completed_item */
4673 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674}
4675
Bram Moolenaar472e8592016-10-15 17:06:47 +02004676/*
4677 * Insert the new text being completed.
4678 * "in_compl_func" is TRUE when called from complete_check().
4679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 static void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004681ins_compl_insert(int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004683 dict_T *dict;
4684
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004685 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004686 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4687 compl_used_match = FALSE;
4688 else
4689 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004690
4691 /* Set completed item. */
4692 /* { word, abbr, menu, kind, info } */
4693 dict = dict_alloc();
4694 if (dict != NULL)
4695 {
4696 dict_add_nr_str(dict, "word", 0L,
4697 EMPTY_IF_NULL(compl_shown_match->cp_str));
4698 dict_add_nr_str(dict, "abbr", 0L,
4699 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4700 dict_add_nr_str(dict, "menu", 0L,
4701 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4702 dict_add_nr_str(dict, "kind", 0L,
4703 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4704 dict_add_nr_str(dict, "info", 0L,
4705 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4706 }
4707 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar472e8592016-10-15 17:06:47 +02004708 if (!in_compl_func)
4709 compl_curr_match = compl_shown_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710}
4711
4712/*
4713 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004714 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4715 * get more completions. If it is FALSE, then we just do nothing when there
4716 * are no more completions in a given direction. The latter case is used when
4717 * we are still in the middle of finding completions, to allow browsing
4718 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 * Return the total number of matches, or -1 if still unknown -- webb.
4720 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004721 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4722 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 *
4724 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004725 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4726 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 */
4728 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004729ins_compl_next(
4730 int allow_get_expansion,
4731 int count, /* repeat completion this many times; should
Bram Moolenaare3226be2005-12-18 22:10:00 +00004732 be at least 1 */
Bram Moolenaar472e8592016-10-15 17:06:47 +02004733 int insert_match, /* Insert the newly selected match */
4734 int in_compl_func) /* called from complete_check() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735{
4736 int num_matches = -1;
4737 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004738 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004739 compl_T *found_compl = NULL;
4740 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004741 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004742 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004744 /* When user complete function return -1 for findstart which is next
4745 * time of 'always', compl_shown_match become NULL. */
4746 if (compl_shown_match == NULL)
4747 return -1;
4748
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004749 if (compl_leader != NULL
4750 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004752 /* Set "compl_shown_match" to the actually shown match, it may differ
4753 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004754 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004755 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004756 && compl_shown_match->cp_next != NULL
4757 && compl_shown_match->cp_next != compl_first_match)
4758 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004759
4760 /* If we didn't find it searching forward, and compl_shows_dir is
4761 * backward, find the last match. */
4762 if (compl_shows_dir == BACKWARD
4763 && !ins_compl_equal(compl_shown_match,
4764 compl_leader, (int)STRLEN(compl_leader))
4765 && (compl_shown_match->cp_next == NULL
4766 || compl_shown_match->cp_next == compl_first_match))
4767 {
4768 while (!ins_compl_equal(compl_shown_match,
4769 compl_leader, (int)STRLEN(compl_leader))
4770 && compl_shown_match->cp_prev != NULL
4771 && compl_shown_match->cp_prev != compl_first_match)
4772 compl_shown_match = compl_shown_match->cp_prev;
4773 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004774 }
4775
4776 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004777 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 /* Delete old text to be replaced */
4779 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004780
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004781 /* When finding the longest common text we stick at the original text,
4782 * don't let CTRL-N or CTRL-P move to the first match. */
4783 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4784
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004785 /* When restarting the search don't insert the first match either. */
4786 if (compl_restarting)
4787 {
4788 advance = FALSE;
4789 compl_restarting = FALSE;
4790 }
4791
Bram Moolenaare3226be2005-12-18 22:10:00 +00004792 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4793 * around. */
4794 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004796 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004798 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004799 found_end = (compl_first_match != NULL
4800 && (compl_shown_match->cp_next == compl_first_match
4801 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004802 }
4803 else if (compl_shows_dir == BACKWARD
4804 && compl_shown_match->cp_prev != NULL)
4805 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004806 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004807 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004808 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 }
4810 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004811 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004812 if (!allow_get_expansion)
4813 {
4814 if (advance)
4815 {
4816 if (compl_shows_dir == BACKWARD)
4817 compl_pending -= todo + 1;
4818 else
4819 compl_pending += todo + 1;
4820 }
4821 return -1;
4822 }
4823
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004824 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004825 {
4826 if (compl_shows_dir == BACKWARD)
4827 --compl_pending;
4828 else
4829 ++compl_pending;
4830 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004831
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004832 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004833 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004834
4835 /* handle any pending completions */
4836 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004837 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004838 {
4839 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4840 {
4841 compl_shown_match = compl_shown_match->cp_next;
4842 --compl_pending;
4843 }
4844 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4845 {
4846 compl_shown_match = compl_shown_match->cp_prev;
4847 ++compl_pending;
4848 }
4849 else
4850 break;
4851 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004852 found_end = FALSE;
4853 }
4854 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4855 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004856 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004857 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004858 ++todo;
4859 else
4860 /* Remember a matching item. */
4861 found_compl = compl_shown_match;
4862
4863 /* Stop at the end of the list when we found a usable match. */
4864 if (found_end)
4865 {
4866 if (found_compl != NULL)
4867 {
4868 compl_shown_match = found_compl;
4869 break;
4870 }
4871 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 }
4874
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004875 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004876 if (compl_no_insert && !started)
4877 {
4878 ins_bytes(compl_orig_text + ins_compl_len());
4879 compl_used_match = FALSE;
4880 }
4881 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004882 {
4883 if (!compl_get_longest || compl_used_match)
Bram Moolenaar472e8592016-10-15 17:06:47 +02004884 ins_compl_insert(in_compl_func);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004885 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004886 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004887 }
4888 else
4889 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890
4891 if (!allow_get_expansion)
4892 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004893 /* may undisplay the popup menu first */
4894 ins_compl_upd_pum();
4895
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004896 /* redraw to show the user what was inserted */
4897 update_screen(0);
4898
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004899 /* display the updated popup menu */
4900 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004901#ifdef FEAT_GUI
4902 if (gui.in_use)
4903 {
4904 /* Show the cursor after the match, not after the redrawn text. */
4905 setcursor();
4906 out_flush();
4907 gui_update_cursor(FALSE, FALSE);
4908 }
4909#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004910
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 /* Delete old text to be replaced, since we're still searching and
4912 * don't want to match ourselves! */
4913 ins_compl_delete();
4914 }
4915
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004916 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004917 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004918 if (compl_no_insert && !started)
4919 compl_enter_selects = TRUE;
4920 else
4921 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 /*
4924 * Show the file name for the match (if any)
4925 * Truncate the file name to avoid a wait for return.
4926 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004927 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 {
4929 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004930 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 if (i <= 0)
4932 i = 0;
4933 else
4934 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004935 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 msg(IObuff);
4937 redraw_cmdline = FALSE; /* don't overwrite! */
4938 }
4939
4940 return num_matches;
4941}
4942
4943/*
4944 * Call this while finding completions, to check whether the user has hit a key
4945 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004946 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004948 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar472e8592016-10-15 17:06:47 +02004949 * "in_compl_func" is TRUE when called from complete_check(), don't set
4950 * compl_curr_match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 */
4952 void
Bram Moolenaar472e8592016-10-15 17:06:47 +02004953ins_compl_check_keys(int frequency, int in_compl_func)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954{
4955 static int count = 0;
4956
4957 int c;
4958
4959 /* Don't check when reading keys from a script. That would break the test
4960 * scripts */
4961 if (using_script())
4962 return;
4963
4964 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004965 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 return;
4967 count = 0;
4968
Bram Moolenaara260a972006-06-23 19:36:29 +00004969 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4970 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 if (c != NUL)
4973 {
4974 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4975 {
4976 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004977 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004978 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
Bram Moolenaar472e8592016-10-15 17:06:47 +02004979 c != K_UP && c != K_DOWN, in_compl_func);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004981 else
4982 {
4983 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004984 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004985 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004986 if (c != K_IGNORE)
4987 {
4988 /* Don't interrupt completion when the character wasn't typed,
4989 * e.g., when doing @q to replay keys. */
4990 if (c != Ctrl_R && KeyTyped)
4991 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004992
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004993 vungetc(c);
4994 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004995 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004997 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004998 {
4999 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
5000
5001 compl_pending = 0;
Bram Moolenaar472e8592016-10-15 17:06:47 +02005002 (void)ins_compl_next(FALSE, todo, TRUE, in_compl_func);
Bram Moolenaara260a972006-06-23 19:36:29 +00005003 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00005004}
5005
5006/*
5007 * Decide the direction of Insert mode complete from the key typed.
5008 * Returns BACKWARD or FORWARD.
5009 */
5010 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005011ins_compl_key2dir(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005012{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005013 if (c == Ctrl_P || c == Ctrl_L
Bram Moolenaar8e42ae52016-04-20 16:39:19 +02005014 || c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP || c == K_UP)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005015 return BACKWARD;
5016 return FORWARD;
5017}
5018
5019/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005020 * Return TRUE for keys that are used for completion only when the popup menu
5021 * is visible.
5022 */
5023 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005024ins_compl_pum_key(int c)
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005025{
5026 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005027 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
5028 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00005029}
5030
5031/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00005032 * Decide the number of completions to move forward.
5033 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
5034 */
5035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005036ins_compl_key2count(int c)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005037{
5038 int h;
5039
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005040 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005041 {
5042 h = pum_get_height();
5043 if (h > 3)
5044 h -= 2; /* keep some context */
5045 return h;
5046 }
5047 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048}
5049
5050/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005051 * Return TRUE if completion with "c" should insert the match, FALSE if only
5052 * to change the currently selected completion.
5053 */
5054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005055ins_compl_use_match(int c)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005056{
5057 switch (c)
5058 {
5059 case K_UP:
5060 case K_DOWN:
5061 case K_PAGEDOWN:
5062 case K_KPAGEDOWN:
5063 case K_S_DOWN:
5064 case K_PAGEUP:
5065 case K_KPAGEUP:
5066 case K_S_UP:
5067 return FALSE;
5068 }
5069 return TRUE;
5070}
5071
5072/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073 * Do Insert mode completion.
5074 * Called when character "c" was typed, which has a meaning for completion.
5075 * Returns OK if completion was done, FAIL if something failed (out of mem).
5076 */
5077 static int
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005078ins_complete(int c, int enable_pum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005080 char_u *line;
5081 int startcol = 0; /* column where searched text starts */
5082 colnr_T curs_col; /* cursor column */
5083 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005084 int save_w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005085 int insert_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086
Bram Moolenaare3226be2005-12-18 22:10:00 +00005087 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005088 insert_match = ins_compl_use_match(c);
5089
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005090 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 {
5092 /* First time we hit ^N or ^P (in a row, I mean) */
5093
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 did_ai = FALSE;
5095#ifdef FEAT_SMARTINDENT
5096 did_si = FALSE;
5097 can_si = FALSE;
5098 can_si_back = FALSE;
5099#endif
5100 if (stop_arrow() == FAIL)
5101 return FAIL;
5102
5103 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005104 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005105 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005107 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005108 * "compl_startpos" to the cursor as a pattern to add a new word
5109 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005110 * "compl_startpos" is not in the same line as the cursor then fix it
5111 * (the line has been split because it was longer than 'tw'). if SOL
5112 * is set then skip the previous pattern, a word at the beginning of
5113 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005114 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5115 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 /*
5118 * it is a continued search
5119 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005120 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5122 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5123 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005124 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005126 /* line (probably) wrapped, set compl_startpos to the
5127 * first non_blank in the line, if it is not a wordchar
5128 * include it to get a better pattern, but then we don't
5129 * want the "\\<" prefix, check it bellow */
5130 compl_col = (colnr_T)(skipwhite(line) - line);
5131 compl_startpos.col = compl_col;
5132 compl_startpos.lnum = curwin->w_cursor.lnum;
5133 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 }
5135 else
5136 {
5137 /* S_IPOS was set when we inserted a word that was at the
5138 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 * mode but first we need to redefine compl_startpos */
5140 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005142 compl_cont_status |= CONT_SOL;
5143 compl_startpos.col = (colnr_T)(skipwhite(
5144 line + compl_length
5145 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005147 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005149 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005150 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005151 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005155 compl_cont_status &= ~CONT_SOL;
5156 compl_length = (IOSIZE - MIN_SPACE);
5157 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5160 if (compl_length < 1)
5161 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005163 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005164 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005166 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005167 }
5168 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 compl_cont_status = 0;
5176 compl_cont_status |= CONT_N_ADDS;
5177 compl_startpos = curwin->w_cursor;
5178 startcol = (int)curs_col;
5179 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
5181
5182 /* Work out completion pattern and original text -- webb */
5183 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5184 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005185 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5187 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005188 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005192 compl_col += ++startcol;
5193 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005196 compl_pattern = str_foldcase(line + compl_col,
5197 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005199 compl_pattern = vim_strnsave(line + compl_col,
5200 compl_length);
5201 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 return FAIL;
5203 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005204 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 {
5206 char_u *prefix = (char_u *)"\\<";
5207
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005208 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005210 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005211 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005213 if (!vim_iswordp(line + compl_col)
5214 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 && (
5216#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005217 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005219 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220#endif
5221 )))
5222 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 STRCPY((char *)compl_pattern, prefix);
5224 (void)quote_meta(compl_pattern + STRLEN(prefix),
5225 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005227 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005229 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005231 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232#endif
5233 )
5234 {
5235 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5237 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005239 compl_col += curs_col;
5240 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 }
5242 else
5243 {
5244#ifdef FEAT_MBYTE
5245 /* Search the point of change class of multibyte character
5246 * or not a word single byte character backward. */
5247 if (has_mbyte)
5248 {
5249 int base_class;
5250 int head_off;
5251
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005252 startcol -= (*mb_head_off)(line, line + startcol);
5253 base_class = mb_get_class(line + startcol);
5254 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005255 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005256 head_off = (*mb_head_off)(line, line + startcol);
5257 if (base_class != mb_get_class(line + startcol
5258 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 }
5262 }
5263 else
5264#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005265 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005267 compl_col += ++startcol;
5268 compl_length = (int)curs_col - startcol;
5269 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 {
5271 /* Only match word with at least two chars -- webb
5272 * there's no need to call quote_meta,
5273 * alloc(7) is enough -- Acevedo
5274 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 compl_pattern = alloc(7);
5276 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005278 STRCPY((char *)compl_pattern, "\\<");
5279 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5280 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005281 }
5282 else
5283 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005285 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005286 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 STRCPY((char *)compl_pattern, "\\<");
5289 (void)quote_meta(compl_pattern + 2, line + compl_col,
5290 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 }
5292 }
5293 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005294 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005296 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005297 compl_length = (int)curs_col - (int)compl_col;
5298 if (compl_length < 0) /* cursor in indent: empty pattern */
5299 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 compl_pattern = str_foldcase(line + compl_col, compl_length,
5302 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005304 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5305 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005306 return FAIL;
5307 }
5308 else if (ctrl_x_mode == CTRL_X_FILES)
5309 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005310 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005311 if (startcol > 0)
5312 {
5313 char_u *p = line + startcol;
5314
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005315 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005316 while (p > line && vim_isfilec(PTR2CHAR(p)))
5317 mb_ptr_back(line, p);
5318 if (p == line && vim_isfilec(PTR2CHAR(p)))
5319 startcol = 0;
5320 else
5321 startcol = (int)(p - line) + 1;
5322 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005323
Bram Moolenaar0300e462013-09-08 16:03:45 +02005324 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005325 compl_length = (int)curs_col - startcol;
5326 compl_pattern = addstar(line + compl_col, compl_length,
5327 EXPAND_FILES);
5328 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 return FAIL;
5330 }
5331 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5332 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005333 compl_pattern = vim_strnsave(line, curs_col);
5334 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 set_cmd_context(&compl_xp, compl_pattern,
Bram Moolenaar33a80ee2016-09-05 21:51:14 +02005337 (int)STRLEN(compl_pattern), curs_col, FALSE);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005338 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5339 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005340 /* No completion possible, use an empty pattern to get a
5341 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005342 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005343 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005344 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5345 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005347 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005348 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005349#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005350 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005351 * Call user defined function 'completefunc' with "a:findstart"
5352 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005353 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005354 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005355 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005356 char_u *funcname;
5357 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005358 win_T *curwin_save;
5359 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005360
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005361 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005362 * string */
5363 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5364 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5365 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005366 {
5367 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5368 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005369 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005370 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005371
5372 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005373 args[1] = NULL;
5374 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005375 curwin_save = curwin;
5376 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005377 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005378 if (curwin_save != curwin || curbuf_save != curbuf)
5379 {
5380 EMSG(_(e_complwin));
5381 return FAIL;
5382 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005383 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005384 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005385 if (!equalpos(curwin->w_cursor, pos))
5386 {
5387 EMSG(_(e_compldel));
5388 return FAIL;
5389 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005390
Bram Moolenaar6110a002012-01-26 18:58:38 +01005391 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005392 * cancel the complete without an error.
5393 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005394 if (col == -2)
5395 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005396 if (col == -3)
5397 {
5398 ctrl_x_mode = 0;
5399 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005400 if (!shortmess(SHM_COMPLETIONMENU))
5401 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005402 return FAIL;
5403 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005404
Bram Moolenaar82139082011-09-14 16:52:09 +02005405 /*
5406 * Reset extended parameters of completion, when start new
5407 * completion.
5408 */
5409 compl_opt_refresh_always = FALSE;
5410
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005411 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005412 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005413 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005414 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005415 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005416
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005417 /* Setup variables for completion. Need to obtain "line" again,
5418 * it may have become invalid. */
5419 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005420 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5422 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005423#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005424 return FAIL;
5425 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005426 else if (ctrl_x_mode == CTRL_X_SPELL)
5427 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005428#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005429 if (spell_bad_len > 0)
5430 compl_col = curs_col - spell_bad_len;
5431 else
5432 compl_col = spell_word_start(startcol);
5433 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005434 {
5435 compl_length = 0;
5436 compl_col = curs_col;
5437 }
5438 else
5439 {
5440 spell_expand_check_cap(compl_col);
5441 compl_length = (int)curs_col - compl_col;
5442 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005443 /* Need to obtain "line" again, it may have become invalid. */
5444 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005445 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5446 if (compl_pattern == NULL)
5447#endif
5448 return FAIL;
5449 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 else
5451 {
Bram Moolenaar95f09602016-11-10 20:01:45 +01005452 internal_error("ins_complete()");
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005453 return FAIL;
5454 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005456 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005459 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
5461 /* Insert a new line, keep indentation but ignore 'comments' */
5462#ifdef FEAT_COMMENTS
5463 char_u *old = curbuf->b_p_com;
5464
5465 curbuf->b_p_com = (char_u *)"";
5466#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005467 compl_startpos.lnum = curwin->w_cursor.lnum;
5468 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 ins_eol('\r');
5470#ifdef FEAT_COMMENTS
5471 curbuf->b_p_com = old;
5472#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005473 compl_length = 0;
5474 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 }
5476 }
5477 else
5478 {
5479 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005480 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 }
5482
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005483 if (compl_cont_status & CONT_LOCAL)
5484 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 else
5486 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5487
Bram Moolenaar62951b12011-09-21 18:23:05 +02005488 /* If any of the original typed text has been changed we need to fix
5489 * the redo buffer. */
5490 ins_compl_fixRedoBufForLeader(NULL);
5491
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005492 /* Always add completion for the original text. */
5493 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005494 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5495 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005496 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005498 vim_free(compl_pattern);
5499 compl_pattern = NULL;
5500 vim_free(compl_orig_text);
5501 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502 return FAIL;
5503 }
5504
5505 /* showmode might reset the internal line pointers, so it must
5506 * be called before line = ml_get(), or when this address is no
5507 * longer needed. -- Acevedo.
5508 */
5509 edit_submode_extra = (char_u *)_("-- Searching...");
5510 edit_submode_highl = HLF_COUNT;
5511 showmode();
5512 edit_submode_extra = NULL;
5513 out_flush();
5514 }
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005515 else if (insert_match && stop_arrow() == FAIL)
5516 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005518 compl_shown_match = compl_curr_match;
5519 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520
5521 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005522 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005523 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005524 save_w_wrow = curwin->w_wrow;
Bram Moolenaar9ec7fa82016-10-18 13:06:41 +02005525 n = ins_compl_next(TRUE, ins_compl_key2count(c), insert_match, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005527 /* may undisplay the popup menu */
5528 ins_compl_upd_pum();
5529
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005530 if (n > 1) /* all matches have been found */
5531 compl_matches = n;
5532 compl_curr_match = compl_shown_match;
5533 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
Bram Moolenaard68071d2006-05-02 22:08:30 +00005535 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5536 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 if (got_int && !global_busy)
5538 {
5539 (void)vgetc();
5540 got_int = FALSE;
5541 }
5542
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005543 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005544 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005546 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5547 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005548 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5549 edit_submode_highl = HLF_E;
5550 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5551 * because we couldn't expand anything at first place, but if we used
5552 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5553 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005554 if ( compl_length > 1
5555 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 || (ctrl_x_mode != 0
5557 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5558 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005559 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561
Bram Moolenaar572cb562005-08-05 21:35:02 +00005562 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005563 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005565 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
5567 if (edit_submode_extra == NULL)
5568 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005569 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570 {
5571 edit_submode_extra = (char_u *)_("Back at original");
5572 edit_submode_highl = HLF_W;
5573 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005574 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 {
5576 edit_submode_extra = (char_u *)_("Word from other line");
5577 edit_submode_highl = HLF_COUNT;
5578 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005579 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 {
5581 edit_submode_extra = (char_u *)_("The only match");
5582 edit_submode_highl = HLF_COUNT;
5583 }
5584 else
5585 {
5586 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005587 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005589 int number = 0;
5590 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005592 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005593 {
5594 /* search backwards for the first valid (!= -1) number.
5595 * This should normally succeed already at the first loop
5596 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005597 for (match = compl_curr_match->cp_prev; match != NULL
5598 && match != compl_first_match;
5599 match = match->cp_prev)
5600 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005601 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005602 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 break;
5604 }
5605 if (match != NULL)
5606 /* go up and assign all numbers which are not assigned
5607 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005608 for (match = match->cp_next;
5609 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005610 match = match->cp_next)
5611 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005612 }
5613 else /* BACKWARD */
5614 {
5615 /* search forwards (upwards) for the first valid (!= -1)
5616 * number. This should normally succeed already at the
5617 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005618 for (match = compl_curr_match->cp_next; match != NULL
5619 && match != compl_first_match;
5620 match = match->cp_next)
5621 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005622 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005623 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 break;
5625 }
5626 if (match != NULL)
5627 /* go down and assign all numbers which are not
5628 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005629 for (match = match->cp_prev; match
5630 && match->cp_number == -1;
5631 match = match->cp_prev)
5632 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 }
5634 }
5635
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005636 /* The match should always have a sequence number now, this is
5637 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005638 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005640 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5641 * Translations may need more than twice that. */
5642 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005644 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005645 vim_snprintf((char *)match_ref, sizeof(match_ref),
5646 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005647 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005648 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005649 vim_snprintf((char *)match_ref, sizeof(match_ref),
5650 _("match %d"),
5651 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 edit_submode_extra = match_ref;
5653 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005654 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 curs_columns(FALSE);
5656 }
5657 }
5658 }
5659
5660 /* Show a message about what (completion) mode we're in. */
5661 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005662 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005664 if (edit_submode_extra != NULL)
5665 {
5666 if (!p_smd)
5667 msg_attr(edit_submode_extra,
5668 edit_submode_highl < HLF_COUNT
5669 ? hl_attr(edit_submode_highl) : 0);
5670 }
5671 else
5672 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005674
Bram Moolenaard68071d2006-05-02 22:08:30 +00005675 /* Show the popup menu, unless we got interrupted. */
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005676 if (enable_pum && !compl_interrupted)
Bram Moolenaard68071d2006-05-02 22:08:30 +00005677 {
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005678 show_pum(save_w_wrow);
Bram Moolenaard68071d2006-05-02 22:08:30 +00005679 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005680 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005681 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005682
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 return OK;
5684}
5685
Bram Moolenaar8aefbe02016-02-23 20:13:16 +01005686 static void
5687show_pum(int save_w_wrow)
5688{
5689 /* RedrawingDisabled may be set when invoked through complete(). */
5690 int n = RedrawingDisabled;
5691
5692 RedrawingDisabled = 0;
5693
5694 /* If the cursor moved we need to remove the pum first. */
5695 setcursor();
5696 if (save_w_wrow != curwin->w_wrow)
5697 ins_compl_del_pum();
5698
5699 ins_compl_show_pum();
5700 setcursor();
5701 RedrawingDisabled = n;
5702}
5703
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704/*
5705 * Looks in the first "len" chars. of "src" for search-metachars.
5706 * If dest is not NULL the chars. are copied there quoting (with
5707 * a backslash) the metachars, and dest would be NUL terminated.
5708 * Returns the length (needed) of dest
5709 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005710 static unsigned
Bram Moolenaar7454a062016-01-30 15:14:10 +01005711quote_meta(char_u *dest, char_u *src, int len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005713 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005715 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 {
5717 switch (*src)
5718 {
5719 case '.':
5720 case '*':
5721 case '[':
5722 if (ctrl_x_mode == CTRL_X_DICTIONARY
5723 || ctrl_x_mode == CTRL_X_THESAURUS)
5724 break;
5725 case '~':
5726 if (!p_magic) /* quote these only if magic is set */
5727 break;
5728 case '\\':
5729 if (ctrl_x_mode == CTRL_X_DICTIONARY
5730 || ctrl_x_mode == CTRL_X_THESAURUS)
5731 break;
5732 case '^': /* currently it's not needed. */
5733 case '$':
5734 m++;
5735 if (dest != NULL)
5736 *dest++ = '\\';
5737 break;
5738 }
5739 if (dest != NULL)
5740 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005741# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005742 /* Copy remaining bytes of a multibyte character. */
5743 if (has_mbyte)
5744 {
5745 int i, mb_len;
5746
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005747 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005748 if (mb_len > 0 && len >= mb_len)
5749 for (i = 0; i < mb_len; ++i)
5750 {
5751 --len;
5752 ++src;
5753 if (dest != NULL)
5754 *dest++ = *src;
5755 }
5756 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005757# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758 }
5759 if (dest != NULL)
5760 *dest = NUL;
5761
5762 return m;
5763}
5764#endif /* FEAT_INS_EXPAND */
5765
5766/*
5767 * Next character is interpreted literally.
5768 * A one, two or three digit decimal number is interpreted as its byte value.
5769 * If one or two digits are entered, the next character is given to vungetc().
5770 * For Unicode a character > 255 may be returned.
5771 */
5772 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005773get_literal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774{
5775 int cc;
5776 int nc;
5777 int i;
5778 int hex = FALSE;
5779 int octal = FALSE;
5780#ifdef FEAT_MBYTE
5781 int unicode = 0;
5782#endif
5783
5784 if (got_int)
5785 return Ctrl_C;
5786
5787#ifdef FEAT_GUI
5788 /*
5789 * In GUI there is no point inserting the internal code for a special key.
5790 * It is more useful to insert the string "<KEY>" instead. This would
5791 * probably be useful in a text window too, but it would not be
5792 * vi-compatible (maybe there should be an option for it?) -- webb
5793 */
5794 if (gui.in_use)
5795 ++allow_keys;
5796#endif
5797#ifdef USE_ON_FLY_SCROLL
5798 dont_scroll = TRUE; /* disallow scrolling here */
5799#endif
5800 ++no_mapping; /* don't map the next key hits */
5801 cc = 0;
5802 i = 0;
5803 for (;;)
5804 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005805 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806#ifdef FEAT_CMDL_INFO
5807 if (!(State & CMDLINE)
5808# ifdef FEAT_MBYTE
5809 && MB_BYTE2LEN_CHECK(nc) == 1
5810# endif
5811 )
5812 add_to_showcmd(nc);
5813#endif
5814 if (nc == 'x' || nc == 'X')
5815 hex = TRUE;
5816 else if (nc == 'o' || nc == 'O')
5817 octal = TRUE;
5818#ifdef FEAT_MBYTE
5819 else if (nc == 'u' || nc == 'U')
5820 unicode = nc;
5821#endif
5822 else
5823 {
5824 if (hex
5825#ifdef FEAT_MBYTE
5826 || unicode != 0
5827#endif
5828 )
5829 {
5830 if (!vim_isxdigit(nc))
5831 break;
5832 cc = cc * 16 + hex2nr(nc);
5833 }
5834 else if (octal)
5835 {
5836 if (nc < '0' || nc > '7')
5837 break;
5838 cc = cc * 8 + nc - '0';
5839 }
5840 else
5841 {
5842 if (!VIM_ISDIGIT(nc))
5843 break;
5844 cc = cc * 10 + nc - '0';
5845 }
5846
5847 ++i;
5848 }
5849
5850 if (cc > 255
5851#ifdef FEAT_MBYTE
5852 && unicode == 0
5853#endif
5854 )
5855 cc = 255; /* limit range to 0-255 */
5856 nc = 0;
5857
5858 if (hex) /* hex: up to two chars */
5859 {
5860 if (i >= 2)
5861 break;
5862 }
5863#ifdef FEAT_MBYTE
5864 else if (unicode) /* Unicode: up to four or eight chars */
5865 {
5866 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5867 break;
5868 }
5869#endif
5870 else if (i >= 3) /* decimal or octal: up to three chars */
5871 break;
5872 }
5873 if (i == 0) /* no number entered */
5874 {
5875 if (nc == K_ZERO) /* NUL is stored as NL */
5876 {
5877 cc = '\n';
5878 nc = 0;
5879 }
5880 else
5881 {
5882 cc = nc;
5883 nc = 0;
5884 }
5885 }
5886
5887 if (cc == 0) /* NUL is stored as NL */
5888 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005889#ifdef FEAT_MBYTE
5890 if (enc_dbcs && (cc & 0xff) == 0)
5891 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5892 second byte will cause trouble! */
5893#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894
5895 --no_mapping;
5896#ifdef FEAT_GUI
5897 if (gui.in_use)
5898 --allow_keys;
5899#endif
5900 if (nc)
5901 vungetc(nc);
5902 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5903 return cc;
5904}
5905
5906/*
5907 * Insert character, taking care of special keys and mod_mask
5908 */
5909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005910insert_special(
5911 int c,
5912 int allow_modmask,
5913 int ctrlv) /* c was typed after CTRL-V */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914{
5915 char_u *p;
5916 int len;
5917
5918 /*
5919 * Special function key, translate into "<Key>". Up to the last '>' is
5920 * inserted with ins_str(), so as not to replace characters in replace
5921 * mode.
5922 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5923 * unless 'allow_modmask' is TRUE.
5924 */
5925#ifdef MACOS
5926 /* Command-key never produces a normal key */
5927 if (mod_mask & MOD_MASK_CMD)
5928 allow_modmask = TRUE;
5929#endif
5930 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5931 {
5932 p = get_special_key_name(c, mod_mask);
5933 len = (int)STRLEN(p);
5934 c = p[len - 1];
5935 if (len > 2)
5936 {
5937 if (stop_arrow() == FAIL)
5938 return;
5939 p[len - 1] = NUL;
5940 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005941 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 ctrlv = FALSE;
5943 }
5944 }
5945 if (stop_arrow() == OK)
5946 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5947}
5948
5949/*
5950 * Special characters in this context are those that need processing other
5951 * than the simple insertion that can be performed here. This includes ESC
5952 * which terminates the insert, and CR/NL which need special processing to
5953 * open up a new line. This routine tries to optimize insertions performed by
5954 * the "redo", "undo" or "put" commands, so it needs to know when it should
5955 * stop and defer processing to the "normal" mechanism.
5956 * '0' and '^' are special, because they can be followed by CTRL-D.
5957 */
5958#ifdef EBCDIC
5959# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5960#else
5961# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5962#endif
5963
5964#ifdef FEAT_MBYTE
5965# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5966#else
5967# define WHITECHAR(cc) vim_iswhite(cc)
5968#endif
5969
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005970/*
5971 * "flags": INSCHAR_FORMAT - force formatting
5972 * INSCHAR_CTRLV - char typed just after CTRL-V
5973 * INSCHAR_NO_FEX - don't use 'formatexpr'
5974 *
5975 * NOTE: passes the flags value straight through to internal_format() which,
5976 * beside INSCHAR_FORMAT (above), is also looking for these:
5977 * INSCHAR_DO_COM - format comments
5978 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5979 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01005981insertchar(
5982 int c, /* character to insert or NUL */
5983 int flags, /* INSCHAR_FORMAT, etc. */
5984 int second_indent) /* indent for second line if >= 0 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005985{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005986 int textwidth;
5987#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005991 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005992
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005993 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995
5996 /*
5997 * Try to break the line in two or more pieces when:
5998 * - Always do this if we have been called to do formatting only.
5999 * - Always do this when 'formatoptions' has the 'a' flag and the line
6000 * ends in white space.
6001 * - Otherwise:
6002 * - Don't do this if inserting a blank
6003 * - Don't do this if an existing character is being replaced, unless
6004 * we're in VREPLACE mode.
6005 * - Do this if the cursor is not on the line where insert started
6006 * or - 'formatoptions' doesn't have 'l' or the line was not too long
6007 * before the insert.
6008 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
6009 * before 'textwidth'
6010 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006011 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006012 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00006013 || (!vim_iswhite(c)
6014 && !((State & REPLACE_FLAG)
6015#ifdef FEAT_VREPLACE
6016 && !(State & VREPLACE_FLAG)
6017#endif
6018 && *ml_get_cursor() != NUL)
6019 && (curwin->w_cursor.lnum != Insstart.lnum
6020 || ((!has_format_option(FO_INS_LONG)
6021 || Insstart_textlen <= (colnr_T)textwidth)
6022 && (!fo_ins_blank
6023 || Insstart_blank_vcol <= (colnr_T)textwidth
6024 ))))))
6025 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006026 /* Format with 'formatexpr' when it's set. Use internal formatting
6027 * when 'formatexpr' isn't set or it returns non-zero. */
6028#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006029 int do_internal = TRUE;
6030 colnr_T virtcol = get_nolist_virtcol()
6031 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006032
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01006033 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
6034 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00006035 {
6036 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
6037 /* It may be required to save for undo again, e.g. when setline()
6038 * was called. */
6039 ins_need_undo = TRUE;
6040 }
6041 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006042#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006043 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006044 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006045
Bram Moolenaar071d4272004-06-13 20:20:40 +00006046 if (c == NUL) /* only formatting was wanted */
6047 return;
6048
6049#ifdef FEAT_COMMENTS
6050 /* Check whether this character should end a comment. */
6051 if (did_ai && (int)c == end_comment_pending)
6052 {
6053 char_u *line;
6054 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6055 int middle_len, end_len;
6056 int i;
6057
6058 /*
6059 * Need to remove existing (middle) comment leader and insert end
6060 * comment leader. First, check what comment leader we can find.
6061 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006062 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006063 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6064 {
6065 /* Skip middle-comment string */
6066 while (*p && p[-1] != ':') /* find end of middle flags */
6067 ++p;
6068 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6069 /* Don't count trailing white space for middle_len */
6070 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6071 --middle_len;
6072
6073 /* Find the end-comment string */
6074 while (*p && p[-1] != ':') /* find end of end flags */
6075 ++p;
6076 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6077
6078 /* Skip white space before the cursor */
6079 i = curwin->w_cursor.col;
6080 while (--i >= 0 && vim_iswhite(line[i]))
6081 ;
6082 i++;
6083
6084 /* Skip to before the middle leader */
6085 i -= middle_len;
6086
6087 /* Check some expected things before we go on */
6088 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6089 {
6090 /* Backspace over all the stuff we want to replace */
6091 backspace_until_column(i);
6092
6093 /*
6094 * Insert the end-comment string, except for the last
6095 * character, which will get inserted as normal later.
6096 */
6097 ins_bytes_len(lead_end, end_len - 1);
6098 }
6099 }
6100 }
6101 end_comment_pending = NUL;
6102#endif
6103
6104 did_ai = FALSE;
6105#ifdef FEAT_SMARTINDENT
6106 did_si = FALSE;
6107 can_si = FALSE;
6108 can_si_back = FALSE;
6109#endif
6110
6111 /*
6112 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6113 * This speeds up normal text input considerably.
6114 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6115 * need to re-indent at a ':', or any other character (but not what
6116 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006117 * Don't do this when there an InsertCharPre autocommand is defined,
6118 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006119 */
6120#ifdef USE_ON_FLY_SCROLL
6121 dont_scroll = FALSE; /* allow scrolling here */
6122#endif
6123
6124 if ( !ISSPECIAL(c)
6125#ifdef FEAT_MBYTE
6126 && (!has_mbyte || (*mb_char2len)(c) == 1)
6127#endif
6128 && vpeekc() != NUL
6129 && !(State & REPLACE_FLAG)
6130#ifdef FEAT_CINDENT
6131 && !cindent_on()
6132#endif
6133#ifdef FEAT_RIGHTLEFT
6134 && !p_ri
6135#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006136#ifdef FEAT_AUTOCMD
6137 && !has_insertcharpre()
6138#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 )
6140 {
6141#define INPUT_BUFLEN 100
6142 char_u buf[INPUT_BUFLEN + 1];
6143 int i;
6144 colnr_T virtcol = 0;
6145
6146 buf[0] = c;
6147 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006148 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 virtcol = get_nolist_virtcol();
6150 /*
6151 * Stop the string when:
6152 * - no more chars available
6153 * - finding a special character (command key)
6154 * - buffer is full
6155 * - running into the 'textwidth' boundary
6156 * - need to check for abbreviation: A non-word char after a word-char
6157 */
6158 while ( (c = vpeekc()) != NUL
6159 && !ISSPECIAL(c)
6160#ifdef FEAT_MBYTE
6161 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6162#endif
6163 && i < INPUT_BUFLEN
6164 && (textwidth == 0
6165 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6166 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6167 {
6168#ifdef FEAT_RIGHTLEFT
6169 c = vgetc();
6170 if (p_hkmap && KeyTyped)
6171 c = hkmap(c); /* Hebrew mode mapping */
6172# ifdef FEAT_FKMAP
6173 if (p_fkmap && KeyTyped)
6174 c = fkmap(c); /* Farsi mode mapping */
6175# endif
6176 buf[i++] = c;
6177#else
6178 buf[i++] = vgetc();
6179#endif
6180 }
6181
6182#ifdef FEAT_DIGRAPHS
6183 do_digraph(-1); /* clear digraphs */
6184 do_digraph(buf[i-1]); /* may be the start of a digraph */
6185#endif
6186 buf[i] = NUL;
6187 ins_str(buf);
6188 if (flags & INSCHAR_CTRLV)
6189 {
6190 redo_literal(*buf);
6191 i = 1;
6192 }
6193 else
6194 i = 0;
6195 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006196 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197 }
6198 else
6199 {
6200#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006201 int cc;
6202
Bram Moolenaar071d4272004-06-13 20:20:40 +00006203 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6204 {
6205 char_u buf[MB_MAXBYTES + 1];
6206
6207 (*mb_char2bytes)(c, buf);
6208 buf[cc] = NUL;
6209 ins_char_bytes(buf, cc);
6210 AppendCharToRedobuff(c);
6211 }
6212 else
6213#endif
6214 {
6215 ins_char(c);
6216 if (flags & INSCHAR_CTRLV)
6217 redo_literal(c);
6218 else
6219 AppendCharToRedobuff(c);
6220 }
6221 }
6222}
6223
6224/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006225 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006226 *
6227 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6228 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229 */
6230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006231internal_format(
6232 int textwidth,
6233 int second_indent,
6234 int flags,
6235 int format_only,
6236 int c) /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006237{
6238 int cc;
6239 int save_char = NUL;
6240 int haveto_redraw = FALSE;
6241 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6242#ifdef FEAT_MBYTE
6243 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6244#endif
6245 int fo_white_par = has_format_option(FO_WHITE_PAR);
6246 int first_line = TRUE;
6247#ifdef FEAT_COMMENTS
6248 colnr_T leader_len;
6249 int no_leader = FALSE;
6250 int do_comments = (flags & INSCHAR_DO_COM);
6251#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006252#ifdef FEAT_LINEBREAK
6253 int has_lbr = curwin->w_p_lbr;
6254
6255 /* make sure win_lbr_chartabsize() counts correctly */
6256 curwin->w_p_lbr = FALSE;
6257#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006258
6259 /*
6260 * When 'ai' is off we don't want a space under the cursor to be
6261 * deleted. Replace it with an 'x' temporarily.
6262 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006263 if (!curbuf->b_p_ai
6264#ifdef FEAT_VREPLACE
6265 && !(State & VREPLACE_FLAG)
6266#endif
6267 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006268 {
6269 cc = gchar_cursor();
6270 if (vim_iswhite(cc))
6271 {
6272 save_char = cc;
6273 pchar_cursor('x');
6274 }
6275 }
6276
6277 /*
6278 * Repeat breaking lines, until the current line is not too long.
6279 */
6280 while (!got_int)
6281 {
6282 int startcol; /* Cursor column at entry */
6283 int wantcol; /* column at textwidth border */
6284 int foundcol; /* column for start of spaces */
6285 int end_foundcol = 0; /* column for start of word */
6286 colnr_T len;
6287 colnr_T virtcol;
6288#ifdef FEAT_VREPLACE
6289 int orig_col = 0;
6290 char_u *saved_text = NULL;
6291#endif
6292 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006293 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006294
Bram Moolenaar97b98102009-11-17 16:41:01 +00006295 virtcol = get_nolist_virtcol()
6296 + char2cells(c != NUL ? c : gchar_cursor());
6297 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006298 break;
6299
6300#ifdef FEAT_COMMENTS
6301 if (no_leader)
6302 do_comments = FALSE;
6303 else if (!(flags & INSCHAR_FORMAT)
6304 && has_format_option(FO_WRAP_COMS))
6305 do_comments = TRUE;
6306
6307 /* Don't break until after the comment leader */
6308 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006309 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006310 else
6311 leader_len = 0;
6312
6313 /* If the line doesn't start with a comment leader, then don't
6314 * start one in a following broken line. Avoids that a %word
6315 * moved to the start of the next line causes all following lines
6316 * to start with %. */
6317 if (leader_len == 0)
6318 no_leader = TRUE;
6319#endif
6320 if (!(flags & INSCHAR_FORMAT)
6321#ifdef FEAT_COMMENTS
6322 && leader_len == 0
6323#endif
6324 && !has_format_option(FO_WRAP))
6325
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006326 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006327 if ((startcol = curwin->w_cursor.col) == 0)
6328 break;
6329
6330 /* find column of textwidth border */
6331 coladvance((colnr_T)textwidth);
6332 wantcol = curwin->w_cursor.col;
6333
Bram Moolenaar97b98102009-11-17 16:41:01 +00006334 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006335 foundcol = 0;
6336
6337 /*
6338 * Find position to break at.
6339 * Stop at first entered white when 'formatoptions' has 'v'
6340 */
6341 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006342 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006343 || curwin->w_cursor.lnum != Insstart.lnum
6344 || curwin->w_cursor.col >= Insstart.col)
6345 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006346 if (curwin->w_cursor.col == startcol && c != NUL)
6347 cc = c;
6348 else
6349 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006350 if (WHITECHAR(cc))
6351 {
6352 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006353 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006354
6355 /* find start of sequence of blanks */
6356 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6357 {
6358 dec_cursor();
6359 cc = gchar_cursor();
6360 }
6361 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6362 break; /* only spaces in front of text */
6363#ifdef FEAT_COMMENTS
6364 /* Don't break until after the comment leader */
6365 if (curwin->w_cursor.col < leader_len)
6366 break;
6367#endif
6368 if (has_format_option(FO_ONE_LETTER))
6369 {
6370 /* do not break after one-letter words */
6371 if (curwin->w_cursor.col == 0)
6372 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006373#ifdef FEAT_COMMENTS
6374 /* do not break "#a b" when 'tw' is 2 */
6375 if (curwin->w_cursor.col <= leader_len)
6376 break;
6377#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006378 col = curwin->w_cursor.col;
6379 dec_cursor();
6380 cc = gchar_cursor();
6381
6382 if (WHITECHAR(cc))
6383 continue; /* one-letter, continue */
6384 curwin->w_cursor.col = col;
6385 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006386
6387 inc_cursor();
6388
6389 end_foundcol = end_col + 1;
6390 foundcol = curwin->w_cursor.col;
6391 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006392 break;
6393 }
6394#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006395 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006396 {
6397 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006398 if (curwin->w_cursor.col != startcol)
6399 {
6400#ifdef FEAT_COMMENTS
6401 /* Don't break until after the comment leader */
6402 if (curwin->w_cursor.col < leader_len)
6403 break;
6404#endif
6405 col = curwin->w_cursor.col;
6406 inc_cursor();
6407 /* Don't change end_foundcol if already set. */
6408 if (foundcol != curwin->w_cursor.col)
6409 {
6410 foundcol = curwin->w_cursor.col;
6411 end_foundcol = foundcol;
6412 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6413 break;
6414 }
6415 curwin->w_cursor.col = col;
6416 }
6417
6418 if (curwin->w_cursor.col == 0)
6419 break;
6420
6421 col = curwin->w_cursor.col;
6422
6423 dec_cursor();
6424 cc = gchar_cursor();
6425
6426 if (WHITECHAR(cc))
6427 continue; /* break with space */
6428#ifdef FEAT_COMMENTS
6429 /* Don't break until after the comment leader */
6430 if (curwin->w_cursor.col < leader_len)
6431 break;
6432#endif
6433
6434 curwin->w_cursor.col = col;
6435
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006436 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006437 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006438 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6439 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006440 }
6441#endif
6442 if (curwin->w_cursor.col == 0)
6443 break;
6444 dec_cursor();
6445 }
6446
6447 if (foundcol == 0) /* no spaces, cannot break line */
6448 {
6449 curwin->w_cursor.col = startcol;
6450 break;
6451 }
6452
6453 /* Going to break the line, remove any "$" now. */
6454 undisplay_dollar();
6455
6456 /*
6457 * Offset between cursor position and line break is used by replace
6458 * stack functions. VREPLACE does not use this, and backspaces
6459 * over the text instead.
6460 */
6461#ifdef FEAT_VREPLACE
6462 if (State & VREPLACE_FLAG)
6463 orig_col = startcol; /* Will start backspacing from here */
6464 else
6465#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006466 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006467
6468 /*
6469 * adjust startcol for spaces that will be deleted and
6470 * characters that will remain on top line
6471 */
6472 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006473 while ((cc = gchar_cursor(), WHITECHAR(cc))
6474 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006475 inc_cursor();
6476 startcol -= curwin->w_cursor.col;
6477 if (startcol < 0)
6478 startcol = 0;
6479
6480#ifdef FEAT_VREPLACE
6481 if (State & VREPLACE_FLAG)
6482 {
6483 /*
6484 * In VREPLACE mode, we will backspace over the text to be
6485 * wrapped, so save a copy now to put on the next line.
6486 */
6487 saved_text = vim_strsave(ml_get_cursor());
6488 curwin->w_cursor.col = orig_col;
6489 if (saved_text == NULL)
6490 break; /* Can't do it, out of memory */
6491 saved_text[startcol] = NUL;
6492
6493 /* Backspace over characters that will move to the next line */
6494 if (!fo_white_par)
6495 backspace_until_column(foundcol);
6496 }
6497 else
6498#endif
6499 {
6500 /* put cursor after pos. to break line */
6501 if (!fo_white_par)
6502 curwin->w_cursor.col = foundcol;
6503 }
6504
6505 /*
6506 * Split the line just before the margin.
6507 * Only insert/delete lines, but don't really redraw the window.
6508 */
6509 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6510 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6511#ifdef FEAT_COMMENTS
6512 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006513 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006514#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006515 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6516 if (!(flags & INSCHAR_COM_LIST))
6517 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518
6519 replace_offset = 0;
6520 if (first_line)
6521 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006522 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006524 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006525 * This section is for auto-wrap of numeric lists. When not
6526 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6527 * flag will be set and open_line() will handle it (as seen
6528 * above). The code here (and in get_number_indent()) will
6529 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006530 */
6531 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006532 second_indent =
6533 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006534 if (second_indent >= 0)
6535 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006536#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006537 if (State & VREPLACE_FLAG)
6538 change_indent(INDENT_SET, second_indent,
6539 FALSE, NUL, TRUE);
6540 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006541#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006542#ifdef FEAT_COMMENTS
6543 if (leader_len > 0 && second_indent - leader_len > 0)
6544 {
6545 int i;
6546 int padding = second_indent - leader_len;
6547
6548 /* We started at the first_line of a numbered list
6549 * that has a comment. the open_line() function has
6550 * inserted the proper comment leader and positioned
6551 * the cursor at the end of the split line. Now we
6552 * add the additional whitespace needed after the
6553 * comment leader for the numbered list. */
6554 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006555 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006556 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006557 }
6558 else
6559 {
6560#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006561 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006562#ifdef FEAT_COMMENTS
6563 }
6564#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006565 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006566 }
6567 first_line = FALSE;
6568 }
6569
6570#ifdef FEAT_VREPLACE
6571 if (State & VREPLACE_FLAG)
6572 {
6573 /*
6574 * In VREPLACE mode we have backspaced over the text to be
6575 * moved, now we re-insert it into the new line.
6576 */
6577 ins_bytes(saved_text);
6578 vim_free(saved_text);
6579 }
6580 else
6581#endif
6582 {
6583 /*
6584 * Check if cursor is not past the NUL off the line, cindent
6585 * may have added or removed indent.
6586 */
6587 curwin->w_cursor.col += startcol;
6588 len = (colnr_T)STRLEN(ml_get_curline());
6589 if (curwin->w_cursor.col > len)
6590 curwin->w_cursor.col = len;
6591 }
6592
6593 haveto_redraw = TRUE;
6594#ifdef FEAT_CINDENT
6595 can_cindent = TRUE;
6596#endif
6597 /* moved the cursor, don't autoindent or cindent now */
6598 did_ai = FALSE;
6599#ifdef FEAT_SMARTINDENT
6600 did_si = FALSE;
6601 can_si = FALSE;
6602 can_si_back = FALSE;
6603#endif
6604 line_breakcheck();
6605 }
6606
6607 if (save_char != NUL) /* put back space after cursor */
6608 pchar_cursor(save_char);
6609
Bram Moolenaar0026d472014-09-09 16:32:39 +02006610#ifdef FEAT_LINEBREAK
6611 curwin->w_p_lbr = has_lbr;
6612#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006613 if (!format_only && haveto_redraw)
6614 {
6615 update_topline();
6616 redraw_curbuf_later(VALID);
6617 }
6618}
6619
6620/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 * Called after inserting or deleting text: When 'formatoptions' includes the
6622 * 'a' flag format from the current line until the end of the paragraph.
6623 * Keep the cursor at the same position relative to the text.
6624 * The caller must have saved the cursor line for undo, following ones will be
6625 * saved here.
6626 */
6627 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006628auto_format(
6629 int trailblank, /* when TRUE also format with trailing blank */
6630 int prev_line) /* may start in previous line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006631{
6632 pos_T pos;
6633 colnr_T len;
6634 char_u *old;
6635 char_u *new, *pnew;
6636 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006637 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638
6639 if (!has_format_option(FO_AUTO))
6640 return;
6641
6642 pos = curwin->w_cursor;
6643 old = ml_get_curline();
6644
6645 /* may remove added space */
6646 check_auto_format(FALSE);
6647
6648 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6649 * user might insert normal text next. Also skip formatting when "1" is
6650 * in 'formatoptions' and there is a single character before the cursor.
6651 * Otherwise the line would be broken and when typing another non-white
6652 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006653 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654 if (*old != NUL && !trailblank && wasatend)
6655 {
6656 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006657 cc = gchar_cursor();
6658 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6659 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006660 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006661 cc = gchar_cursor();
6662 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 {
6664 curwin->w_cursor = pos;
6665 return;
6666 }
6667 curwin->w_cursor = pos;
6668 }
6669
6670#ifdef FEAT_COMMENTS
6671 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6672 * comments. */
6673 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006674 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675 return;
6676#endif
6677
6678 /*
6679 * May start formatting in a previous line, so that after "x" a word is
6680 * moved to the previous line if it fits there now. Only when this is not
6681 * the start of a paragraph.
6682 */
6683 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6684 {
6685 --curwin->w_cursor.lnum;
6686 if (u_save_cursor() == FAIL)
6687 return;
6688 }
6689
6690 /*
6691 * Do the formatting and restore the cursor position. "saved_cursor" will
6692 * be adjusted for the text formatting.
6693 */
6694 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006695 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 curwin->w_cursor = saved_cursor;
6697 saved_cursor.lnum = 0;
6698
6699 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6700 {
6701 /* "cannot happen" */
6702 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6703 coladvance((colnr_T)MAXCOL);
6704 }
6705 else
6706 check_cursor_col();
6707
6708 /* Insert mode: If the cursor is now after the end of the line while it
6709 * previously wasn't, the line was broken. Because of the rule above we
6710 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6711 * formatted. */
6712 if (!wasatend && has_format_option(FO_WHITE_PAR))
6713 {
6714 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006715 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 if (curwin->w_cursor.col == len)
6717 {
6718 pnew = vim_strnsave(new, len + 2);
6719 pnew[len] = ' ';
6720 pnew[len + 1] = NUL;
6721 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6722 /* remove the space later */
6723 did_add_space = TRUE;
6724 }
6725 else
6726 /* may remove added space */
6727 check_auto_format(FALSE);
6728 }
6729
6730 check_cursor();
6731}
6732
6733/*
6734 * When an extra space was added to continue a paragraph for auto-formatting,
6735 * delete it now. The space must be under the cursor, just after the insert
6736 * position.
6737 */
6738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006739check_auto_format(
6740 int end_insert) /* TRUE when ending Insert mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006741{
6742 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006743 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744
6745 if (did_add_space)
6746 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006747 cc = gchar_cursor();
6748 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 /* Somehow the space was removed already. */
6750 did_add_space = FALSE;
6751 else
6752 {
6753 if (!end_insert)
6754 {
6755 inc_cursor();
6756 c = gchar_cursor();
6757 dec_cursor();
6758 }
6759 if (c != NUL)
6760 {
6761 /* The space is no longer at the end of the line, delete it. */
6762 del_char(FALSE);
6763 did_add_space = FALSE;
6764 }
6765 }
6766 }
6767}
6768
6769/*
6770 * Find out textwidth to be used for formatting:
6771 * if 'textwidth' option is set, use it
6772 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6773 * if invalid value, use 0.
6774 * Set default to window width (maximum 79) for "gq" operator.
6775 */
6776 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006777comp_textwidth(
6778 int ff) /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
6780 int textwidth;
6781
6782 textwidth = curbuf->b_p_tw;
6783 if (textwidth == 0 && curbuf->b_p_wm)
6784 {
6785 /* The width is the window width minus 'wrapmargin' minus all the
6786 * things that add to the margin. */
6787 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6788#ifdef FEAT_CMDWIN
6789 if (cmdwin_type != 0)
6790 textwidth -= 1;
6791#endif
6792#ifdef FEAT_FOLDING
6793 textwidth -= curwin->w_p_fdc;
6794#endif
6795#ifdef FEAT_SIGNS
Bram Moolenaar95ec9d62016-08-12 18:29:59 +02006796 if (signcolumn_on(curwin))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 textwidth -= 1;
6798#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006799 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006800 textwidth -= 8;
6801 }
6802 if (textwidth < 0)
6803 textwidth = 0;
6804 if (ff && textwidth == 0)
6805 {
6806 textwidth = W_WIDTH(curwin) - 1;
6807 if (textwidth > 79)
6808 textwidth = 79;
6809 }
6810 return textwidth;
6811}
6812
6813/*
6814 * Put a character in the redo buffer, for when just after a CTRL-V.
6815 */
6816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006817redo_literal(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006818{
6819 char_u buf[10];
6820
6821 /* Only digits need special treatment. Translate them into a string of
6822 * three digits. */
6823 if (VIM_ISDIGIT(c))
6824 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006825 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006826 AppendToRedobuff(buf);
6827 }
6828 else
6829 AppendCharToRedobuff(c);
6830}
6831
6832/*
6833 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006834 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 */
6836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006837start_arrow(
6838 pos_T *end_insert_pos) /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006840 start_arrow_common(end_insert_pos, TRUE);
6841}
6842
6843/*
6844 * Like start_arrow() but with end_change argument.
6845 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6846 */
6847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006848start_arrow_with_change(
6849 pos_T *end_insert_pos, /* can be NULL */
6850 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006851{
6852 start_arrow_common(end_insert_pos, end_change);
6853 if (!end_change)
6854 {
6855 AppendCharToRedobuff(Ctrl_G);
6856 AppendCharToRedobuff('U');
6857 }
6858}
6859
6860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006861start_arrow_common(
6862 pos_T *end_insert_pos, /* can be NULL */
6863 int end_change) /* end undoable change */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006864{
6865 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006866 {
6867 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006868 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 arrow_used = TRUE; /* this means we stopped the current insert */
6870 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006871#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006872 check_spell_redraw();
6873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874}
6875
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006876#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006877/*
6878 * If we skipped highlighting word at cursor, do it now.
6879 * It may be skipped again, thus reset spell_redraw_lnum first.
6880 */
6881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006882check_spell_redraw(void)
Bram Moolenaar217ad922005-03-20 22:37:15 +00006883{
6884 if (spell_redraw_lnum != 0)
6885 {
6886 linenr_T lnum = spell_redraw_lnum;
6887
6888 spell_redraw_lnum = 0;
6889 redrawWinline(lnum, FALSE);
6890 }
6891}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006892
6893/*
6894 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6895 * spelled word, if there is one.
6896 */
6897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006898spell_back_to_badword(void)
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006899{
6900 pos_T tpos = curwin->w_cursor;
6901
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006902 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006903 if (curwin->w_cursor.col != tpos.col)
6904 start_arrow(&tpos);
6905}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006906#endif
6907
Bram Moolenaar071d4272004-06-13 20:20:40 +00006908/*
6909 * stop_arrow() is called before a change is made in insert mode.
6910 * If an arrow key has been used, start a new insertion.
6911 * Returns FAIL if undo is impossible, shouldn't insert then.
6912 */
6913 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006914stop_arrow(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006915{
6916 if (arrow_used)
6917 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006918 Insstart = curwin->w_cursor; /* new insertion starts here */
6919 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6920 /* Don't update the original insert position when moved to the
6921 * right, except when nothing was inserted yet. */
6922 update_Insstart_orig = FALSE;
6923 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6924
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 if (u_save_cursor() == OK)
6926 {
6927 arrow_used = FALSE;
6928 ins_need_undo = FALSE;
6929 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006930
Bram Moolenaar071d4272004-06-13 20:20:40 +00006931 ai_col = 0;
6932#ifdef FEAT_VREPLACE
6933 if (State & VREPLACE_FLAG)
6934 {
6935 orig_line_count = curbuf->b_ml.ml_line_count;
6936 vr_lines_changed = 1;
6937 }
6938#endif
6939 ResetRedobuff();
6940 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006941 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 }
6943 else if (ins_need_undo)
6944 {
6945 if (u_save_cursor() == OK)
6946 ins_need_undo = FALSE;
6947 }
6948
6949#ifdef FEAT_FOLDING
6950 /* Always open fold at the cursor line when inserting something. */
6951 foldOpenCursor();
6952#endif
6953
6954 return (arrow_used || ins_need_undo ? FAIL : OK);
6955}
6956
6957/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006958 * Do a few things to stop inserting.
6959 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6960 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961 */
6962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006963stop_insert(
6964 pos_T *end_insert_pos,
6965 int esc, /* called by ins_esc() */
6966 int nomove) /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006968 int cc;
6969 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006970
6971 stop_redo_ins();
6972 replace_flush(); /* abandon replace stack */
6973
6974 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006975 * Save the inserted text for later redo with ^@ and CTRL-A.
6976 * Don't do it when "restart_edit" was set and nothing was inserted,
6977 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006978 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006979 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006980 if (did_restart_edit == 0 || (ptr != NULL
6981 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006982 {
6983 vim_free(last_insert);
6984 last_insert = ptr;
6985 last_insert_skip = new_insert_skip;
6986 }
6987 else
6988 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006990 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006991 {
6992 /* Auto-format now. It may seem strange to do this when stopping an
6993 * insertion (or moving the cursor), but it's required when appending
6994 * a line and having it end in a space. But only do it when something
6995 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006996 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006998 pos_T tpos = curwin->w_cursor;
6999
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 /* When the cursor is at the end of the line after a space the
7001 * formatting will move it to the following word. Avoid that by
7002 * moving the cursor onto the space. */
7003 cc = 'x';
7004 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
7005 {
7006 dec_cursor();
7007 cc = gchar_cursor();
7008 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007009 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 }
7011
7012 auto_format(TRUE, FALSE);
7013
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007014 if (vim_iswhite(cc))
7015 {
7016 if (gchar_cursor() != NUL)
7017 inc_cursor();
7018#ifdef FEAT_VIRTUALEDIT
7019 /* If the cursor is still at the same character, also keep
7020 * the "coladd". */
7021 if (gchar_cursor() == NUL
7022 && curwin->w_cursor.lnum == tpos.lnum
7023 && curwin->w_cursor.col == tpos.col)
7024 curwin->w_cursor.coladd = tpos.coladd;
7025#endif
7026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 }
7028
7029 /* If a space was inserted for auto-formatting, remove it now. */
7030 check_auto_format(TRUE);
7031
7032 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007033 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00007034 * Do this when ESC was used or moving the cursor up/down.
7035 * Check for the old position still being valid, just in case the text
7036 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01007037 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007038 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7039 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007041 pos_T tpos = curwin->w_cursor;
7042
7043 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007044 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007045 for (;;)
7046 {
7047 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7048 --curwin->w_cursor.col;
7049 cc = gchar_cursor();
7050 if (!vim_iswhite(cc))
7051 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007052 if (del_char(TRUE) == FAIL)
7053 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007054 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007055 if (curwin->w_cursor.lnum != tpos.lnum)
7056 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007057 else
7058 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007059 /* reset tpos, could have been invalidated in the loop above */
7060 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007061 tpos.col++;
7062 if (cc != NUL && gchar_pos(&tpos) == NUL)
7063 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 /* <C-S-Right> may have started Visual mode, adjust the position for
7067 * deleted characters. */
7068 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7069 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007070 int len = (int)STRLEN(ml_get_curline());
7071
7072 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007074 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007075#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007077#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 }
7079 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007080 }
7081 }
7082 did_ai = FALSE;
7083#ifdef FEAT_SMARTINDENT
7084 did_si = FALSE;
7085 can_si = FALSE;
7086 can_si_back = FALSE;
7087#endif
7088
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007089 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7090 * now in a different buffer. */
7091 if (end_insert_pos != NULL)
7092 {
7093 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007094 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007095 curbuf->b_op_end = *end_insert_pos;
7096 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007097}
7098
7099/*
7100 * Set the last inserted text to a single character.
7101 * Used for the replace command.
7102 */
7103 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007104set_last_insert(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007105{
7106 char_u *s;
7107
7108 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007109 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007110 if (last_insert != NULL)
7111 {
7112 s = last_insert;
7113 /* Use the CTRL-V only when entering a special char */
7114 if (c < ' ' || c == DEL)
7115 *s++ = Ctrl_V;
7116 s = add_char2buf(c, s);
7117 *s++ = ESC;
7118 *s++ = NUL;
7119 last_insert_skip = 0;
7120 }
7121}
7122
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007123#if defined(EXITFREE) || defined(PROTO)
7124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007125free_last_insert(void)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007126{
7127 vim_free(last_insert);
7128 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007129# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007130 vim_free(compl_orig_text);
7131 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007132# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007133}
7134#endif
7135
Bram Moolenaar071d4272004-06-13 20:20:40 +00007136/*
7137 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7138 * and CSI. Handle multi-byte characters.
7139 * Returns a pointer to after the added bytes.
7140 */
7141 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007142add_char2buf(int c, char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007143{
7144#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007145 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 int i;
7147 int len;
7148
7149 len = (*mb_char2bytes)(c, temp);
7150 for (i = 0; i < len; ++i)
7151 {
7152 c = temp[i];
7153#endif
7154 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7155 if (c == K_SPECIAL)
7156 {
7157 *s++ = K_SPECIAL;
7158 *s++ = KS_SPECIAL;
7159 *s++ = KE_FILLER;
7160 }
7161#ifdef FEAT_GUI
7162 else if (c == CSI)
7163 {
7164 *s++ = CSI;
7165 *s++ = KS_EXTRA;
7166 *s++ = (int)KE_CSI;
7167 }
7168#endif
7169 else
7170 *s++ = c;
7171#ifdef FEAT_MBYTE
7172 }
7173#endif
7174 return s;
7175}
7176
7177/*
7178 * move cursor to start of line
7179 * if flags & BL_WHITE move to first non-white
7180 * if flags & BL_SOL move to first non-white if startofline is set,
7181 * otherwise keep "curswant" column
7182 * if flags & BL_FIX don't leave the cursor on a NUL.
7183 */
7184 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007185beginline(int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
7187 if ((flags & BL_SOL) && !p_sol)
7188 coladvance(curwin->w_curswant);
7189 else
7190 {
7191 curwin->w_cursor.col = 0;
7192#ifdef FEAT_VIRTUALEDIT
7193 curwin->w_cursor.coladd = 0;
7194#endif
7195
7196 if (flags & (BL_WHITE | BL_SOL))
7197 {
7198 char_u *ptr;
7199
7200 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7201 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7202 ++curwin->w_cursor.col;
7203 }
7204 curwin->w_set_curswant = TRUE;
7205 }
7206}
7207
7208/*
7209 * oneright oneleft cursor_down cursor_up
7210 *
7211 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007212 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213 * Return OK when successful, FAIL when we hit a line of file boundary.
7214 */
7215
7216 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007217oneright(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007218{
7219 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007220 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007221
7222#ifdef FEAT_VIRTUALEDIT
7223 if (virtual_active())
7224 {
7225 pos_T prevpos = curwin->w_cursor;
7226
7227 /* Adjust for multi-wide char (excluding TAB) */
7228 ptr = ml_get_cursor();
7229 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007230# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007231 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007232# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007233 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007234# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007235 ))
7236 ? ptr2cells(ptr) : 1));
7237 curwin->w_set_curswant = TRUE;
7238 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7239 return (prevpos.col != curwin->w_cursor.col
7240 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7241 }
7242#endif
7243
7244 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007245 if (*ptr == NUL)
7246 return FAIL; /* already at the very end */
7247
Bram Moolenaar071d4272004-06-13 20:20:40 +00007248#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007249 if (has_mbyte)
7250 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 else
7252#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007253 l = 1;
7254
7255 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7256 * contains "onemore". */
7257 if (ptr[l] == NUL
7258#ifdef FEAT_VIRTUALEDIT
7259 && (ve_flags & VE_ONEMORE) == 0
7260#endif
7261 )
7262 return FAIL;
7263 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264
7265 curwin->w_set_curswant = TRUE;
7266 return OK;
7267}
7268
7269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007270oneleft(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007271{
7272#ifdef FEAT_VIRTUALEDIT
7273 if (virtual_active())
7274 {
7275 int width;
7276 int v = getviscol();
7277
7278 if (v == 0)
7279 return FAIL;
7280
7281# ifdef FEAT_LINEBREAK
7282 /* We might get stuck on 'showbreak', skip over it. */
7283 width = 1;
7284 for (;;)
7285 {
7286 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007287 /* getviscol() is slow, skip it when 'showbreak' is empty,
7288 * 'breakindent' is not set and there are no multi-byte
7289 * characters */
7290 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291# ifdef FEAT_MBYTE
7292 && !has_mbyte
7293# endif
7294 ) || getviscol() < v)
7295 break;
7296 ++width;
7297 }
7298# else
7299 coladvance(v - 1);
7300# endif
7301
7302 if (curwin->w_cursor.coladd == 1)
7303 {
7304 char_u *ptr;
7305
7306 /* Adjust for multi-wide char (not a TAB) */
7307 ptr = ml_get_cursor();
7308 if (*ptr != TAB && vim_isprintc(
7309# ifdef FEAT_MBYTE
7310 (*mb_ptr2char)(ptr)
7311# else
7312 *ptr
7313# endif
7314 ) && ptr2cells(ptr) > 1)
7315 curwin->w_cursor.coladd = 0;
7316 }
7317
7318 curwin->w_set_curswant = TRUE;
7319 return OK;
7320 }
7321#endif
7322
7323 if (curwin->w_cursor.col == 0)
7324 return FAIL;
7325
7326 curwin->w_set_curswant = TRUE;
7327 --curwin->w_cursor.col;
7328
7329#ifdef FEAT_MBYTE
7330 /* if the character on the left of the current cursor is a multi-byte
7331 * character, move to its first byte */
7332 if (has_mbyte)
7333 mb_adjust_cursor();
7334#endif
7335 return OK;
7336}
7337
7338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007339cursor_up(
7340 long n,
7341 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342{
7343 linenr_T lnum;
7344
7345 if (n > 0)
7346 {
7347 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007348 /* This fails if the cursor is already in the first line or the count
7349 * is larger than the line number and '-' is in 'cpoptions' */
7350 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007351 return FAIL;
7352 if (n >= lnum)
7353 lnum = 1;
7354 else
7355#ifdef FEAT_FOLDING
7356 if (hasAnyFolding(curwin))
7357 {
7358 /*
7359 * Count each sequence of folded lines as one logical line.
7360 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007361 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007362 (void)hasFolding(lnum, &lnum, NULL);
7363
7364 while (n--)
7365 {
7366 /* move up one line */
7367 --lnum;
7368 if (lnum <= 1)
7369 break;
7370 /* If we entered a fold, move to the beginning, unless in
7371 * Insert mode or when 'foldopen' contains "all": it will open
7372 * in a moment. */
7373 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7374 (void)hasFolding(lnum, &lnum, NULL);
7375 }
7376 if (lnum < 1)
7377 lnum = 1;
7378 }
7379 else
7380#endif
7381 lnum -= n;
7382 curwin->w_cursor.lnum = lnum;
7383 }
7384
7385 /* try to advance to the column we want to be at */
7386 coladvance(curwin->w_curswant);
7387
7388 if (upd_topline)
7389 update_topline(); /* make sure curwin->w_topline is valid */
7390
7391 return OK;
7392}
7393
7394/*
7395 * Cursor down a number of logical lines.
7396 */
7397 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007398cursor_down(
7399 long n,
7400 int upd_topline) /* When TRUE: update topline */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401{
7402 linenr_T lnum;
7403
7404 if (n > 0)
7405 {
7406 lnum = curwin->w_cursor.lnum;
7407#ifdef FEAT_FOLDING
7408 /* Move to last line of fold, will fail if it's the end-of-file. */
7409 (void)hasFolding(lnum, NULL, &lnum);
7410#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007411 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007412 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007413 if (lnum >= curbuf->b_ml.ml_line_count
7414 || (lnum + n > curbuf->b_ml.ml_line_count
7415 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007416 return FAIL;
7417 if (lnum + n >= curbuf->b_ml.ml_line_count)
7418 lnum = curbuf->b_ml.ml_line_count;
7419 else
7420#ifdef FEAT_FOLDING
7421 if (hasAnyFolding(curwin))
7422 {
7423 linenr_T last;
7424
7425 /* count each sequence of folded lines as one logical line */
7426 while (n--)
7427 {
7428 if (hasFolding(lnum, NULL, &last))
7429 lnum = last + 1;
7430 else
7431 ++lnum;
7432 if (lnum >= curbuf->b_ml.ml_line_count)
7433 break;
7434 }
7435 if (lnum > curbuf->b_ml.ml_line_count)
7436 lnum = curbuf->b_ml.ml_line_count;
7437 }
7438 else
7439#endif
7440 lnum += n;
7441 curwin->w_cursor.lnum = lnum;
7442 }
7443
7444 /* try to advance to the column we want to be at */
7445 coladvance(curwin->w_curswant);
7446
7447 if (upd_topline)
7448 update_topline(); /* make sure curwin->w_topline is valid */
7449
7450 return OK;
7451}
7452
7453/*
7454 * Stuff the last inserted text in the read buffer.
7455 * Last_insert actually is a copy of the redo buffer, so we
7456 * first have to remove the command.
7457 */
7458 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007459stuff_inserted(
7460 int c, /* Command character to be inserted */
7461 long count, /* Repeat this many times */
7462 int no_esc) /* Don't add an ESC at the end */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007463{
7464 char_u *esc_ptr;
7465 char_u *ptr;
7466 char_u *last_ptr;
7467 char_u last = NUL;
7468
7469 ptr = get_last_insert();
7470 if (ptr == NULL)
7471 {
7472 EMSG(_(e_noinstext));
7473 return FAIL;
7474 }
7475
7476 /* may want to stuff the command character, to start Insert mode */
7477 if (c != NUL)
7478 stuffcharReadbuff(c);
7479 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7480 *esc_ptr = NUL; /* remove the ESC */
7481
7482 /* when the last char is either "0" or "^" it will be quoted if no ESC
7483 * comes after it OR if it will inserted more than once and "ptr"
7484 * starts with ^D. -- Acevedo
7485 */
7486 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7487 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7488 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7489 {
7490 last = *last_ptr;
7491 *last_ptr = NUL;
7492 }
7493
7494 do
7495 {
7496 stuffReadbuff(ptr);
7497 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7498 if (last)
7499 stuffReadbuff((char_u *)(last == '0'
7500 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7501 : IF_EB("\026^", CTRL_V_STR "^")));
7502 }
7503 while (--count > 0);
7504
7505 if (last)
7506 *last_ptr = last;
7507
7508 if (esc_ptr != NULL)
7509 *esc_ptr = ESC; /* put the ESC back */
7510
7511 /* may want to stuff a trailing ESC, to get out of Insert mode */
7512 if (!no_esc)
7513 stuffcharReadbuff(ESC);
7514
7515 return OK;
7516}
7517
7518 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007519get_last_insert(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520{
7521 if (last_insert == NULL)
7522 return NULL;
7523 return last_insert + last_insert_skip;
7524}
7525
7526/*
7527 * Get last inserted string, and remove trailing <Esc>.
7528 * Returns pointer to allocated memory (must be freed) or NULL.
7529 */
7530 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007531get_last_insert_save(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007532{
7533 char_u *s;
7534 int len;
7535
7536 if (last_insert == NULL)
7537 return NULL;
7538 s = vim_strsave(last_insert + last_insert_skip);
7539 if (s != NULL)
7540 {
7541 len = (int)STRLEN(s);
7542 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7543 s[len - 1] = NUL;
7544 }
7545 return s;
7546}
7547
7548/*
7549 * Check the word in front of the cursor for an abbreviation.
7550 * Called when the non-id character "c" has been entered.
7551 * When an abbreviation is recognized it is removed from the text and
7552 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7553 */
7554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007555echeck_abbr(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556{
7557 /* Don't check for abbreviation in paste mode, when disabled and just
7558 * after moving around with cursor keys. */
7559 if (p_paste || no_abbr || arrow_used)
7560 return FALSE;
7561
7562 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7563 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7564}
7565
7566/*
7567 * replace-stack functions
7568 *
7569 * When replacing characters, the replaced characters are remembered for each
7570 * new character. This is used to re-insert the old text when backspacing.
7571 *
7572 * There is a NUL headed list of characters for each character that is
7573 * currently in the file after the insertion point. When BS is used, one NUL
7574 * headed list is put back for the deleted character.
7575 *
7576 * For a newline, there are two NUL headed lists. One contains the characters
7577 * that the NL replaced. The extra one stores the characters after the cursor
7578 * that were deleted (always white space).
7579 *
7580 * Replace_offset is normally 0, in which case replace_push will add a new
7581 * character at the end of the stack. If replace_offset is not 0, that many
7582 * characters will be left on the stack above the newly inserted character.
7583 */
7584
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007585static char_u *replace_stack = NULL;
7586static long replace_stack_nr = 0; /* next entry in replace stack */
7587static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588
7589 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007590replace_push(
7591 int c) /* character that is replaced (NUL is none) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007592{
7593 char_u *p;
7594
7595 if (replace_stack_nr < replace_offset) /* nothing to do */
7596 return;
7597 if (replace_stack_len <= replace_stack_nr)
7598 {
7599 replace_stack_len += 50;
7600 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7601 if (p == NULL) /* out of memory */
7602 {
7603 replace_stack_len -= 50;
7604 return;
7605 }
7606 if (replace_stack != NULL)
7607 {
7608 mch_memmove(p, replace_stack,
7609 (size_t)(replace_stack_nr * sizeof(char_u)));
7610 vim_free(replace_stack);
7611 }
7612 replace_stack = p;
7613 }
7614 p = replace_stack + replace_stack_nr - replace_offset;
7615 if (replace_offset)
7616 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7617 *p = c;
7618 ++replace_stack_nr;
7619}
7620
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007621#if defined(FEAT_MBYTE) || defined(PROTO)
7622/*
7623 * Push a character onto the replace stack. Handles a multi-byte character in
7624 * reverse byte order, so that the first byte is popped off first.
7625 * Return the number of bytes done (includes composing characters).
7626 */
7627 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007628replace_push_mb(char_u *p)
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007629{
7630 int l = (*mb_ptr2len)(p);
7631 int j;
7632
7633 for (j = l - 1; j >= 0; --j)
7634 replace_push(p[j]);
7635 return l;
7636}
7637#endif
7638
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639/*
7640 * Pop one item from the replace stack.
7641 * return -1 if stack empty
7642 * return replaced character or NUL otherwise
7643 */
7644 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007645replace_pop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007646{
7647 if (replace_stack_nr == 0)
7648 return -1;
7649 return (int)replace_stack[--replace_stack_nr];
7650}
7651
7652/*
7653 * Join the top two items on the replace stack. This removes to "off"'th NUL
7654 * encountered.
7655 */
7656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007657replace_join(
7658 int off) /* offset for which NUL to remove */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007659{
7660 int i;
7661
7662 for (i = replace_stack_nr; --i >= 0; )
7663 if (replace_stack[i] == NUL && off-- <= 0)
7664 {
7665 --replace_stack_nr;
7666 mch_memmove(replace_stack + i, replace_stack + i + 1,
7667 (size_t)(replace_stack_nr - i));
7668 return;
7669 }
7670}
7671
7672/*
7673 * Pop bytes from the replace stack until a NUL is found, and insert them
7674 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7675 */
7676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007677replace_pop_ins(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007678{
7679 int cc;
7680 int oldState = State;
7681
7682 State = NORMAL; /* don't want REPLACE here */
7683 while ((cc = replace_pop()) > 0)
7684 {
7685#ifdef FEAT_MBYTE
7686 mb_replace_pop_ins(cc);
7687#else
7688 ins_char(cc);
7689#endif
7690 dec_cursor();
7691 }
7692 State = oldState;
7693}
7694
7695#ifdef FEAT_MBYTE
7696/*
7697 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7698 * indicates a multi-byte char, pop the other bytes too.
7699 */
7700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007701mb_replace_pop_ins(int cc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007702{
7703 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007704 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 int i;
7706 int c;
7707
7708 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7709 {
7710 buf[0] = cc;
7711 for (i = 1; i < n; ++i)
7712 buf[i] = replace_pop();
7713 ins_bytes_len(buf, n);
7714 }
7715 else
7716 ins_char(cc);
7717
7718 if (enc_utf8)
7719 /* Handle composing chars. */
7720 for (;;)
7721 {
7722 c = replace_pop();
7723 if (c == -1) /* stack empty */
7724 break;
7725 if ((n = MB_BYTE2LEN(c)) == 1)
7726 {
7727 /* Not a multi-byte char, put it back. */
7728 replace_push(c);
7729 break;
7730 }
7731 else
7732 {
7733 buf[0] = c;
7734 for (i = 1; i < n; ++i)
7735 buf[i] = replace_pop();
7736 if (utf_iscomposing(utf_ptr2char(buf)))
7737 ins_bytes_len(buf, n);
7738 else
7739 {
7740 /* Not a composing char, put it back. */
7741 for (i = n - 1; i >= 0; --i)
7742 replace_push(buf[i]);
7743 break;
7744 }
7745 }
7746 }
7747}
7748#endif
7749
7750/*
7751 * make the replace stack empty
7752 * (called when exiting replace mode)
7753 */
7754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007755replace_flush(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756{
7757 vim_free(replace_stack);
7758 replace_stack = NULL;
7759 replace_stack_len = 0;
7760 replace_stack_nr = 0;
7761}
7762
7763/*
7764 * Handle doing a BS for one character.
7765 * cc < 0: replace stack empty, just move cursor
7766 * cc == 0: character was inserted, delete it
7767 * cc > 0: character was replaced, put cc (first byte of original char) back
7768 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007769 * When "limit_col" is >= 0, don't delete before this column. Matters when
7770 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771 */
7772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007773replace_do_bs(int limit_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774{
7775 int cc;
7776#ifdef FEAT_VREPLACE
7777 int orig_len = 0;
7778 int ins_len;
7779 int orig_vcols = 0;
7780 colnr_T start_vcol;
7781 char_u *p;
7782 int i;
7783 int vcol;
7784#endif
7785
7786 cc = replace_pop();
7787 if (cc > 0)
7788 {
7789#ifdef FEAT_VREPLACE
7790 if (State & VREPLACE_FLAG)
7791 {
7792 /* Get the number of screen cells used by the character we are
7793 * going to delete. */
7794 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7795 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7796 }
7797#endif
7798#ifdef FEAT_MBYTE
7799 if (has_mbyte)
7800 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007801 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007802# ifdef FEAT_VREPLACE
7803 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007804 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805# endif
7806 replace_push(cc);
7807 }
7808 else
7809#endif
7810 {
7811 pchar_cursor(cc);
7812#ifdef FEAT_VREPLACE
7813 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007814 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815#endif
7816 }
7817 replace_pop_ins();
7818
7819#ifdef FEAT_VREPLACE
7820 if (State & VREPLACE_FLAG)
7821 {
7822 /* Get the number of screen cells used by the inserted characters */
7823 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007824 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 vcol = start_vcol;
7826 for (i = 0; i < ins_len; ++i)
7827 {
7828 vcol += chartabsize(p + i, vcol);
7829#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007830 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007831#endif
7832 }
7833 vcol -= start_vcol;
7834
7835 /* Delete spaces that were inserted after the cursor to keep the
7836 * text aligned. */
7837 curwin->w_cursor.col += ins_len;
7838 while (vcol > orig_vcols && gchar_cursor() == ' ')
7839 {
7840 del_char(FALSE);
7841 ++orig_vcols;
7842 }
7843 curwin->w_cursor.col -= ins_len;
7844 }
7845#endif
7846
7847 /* mark the buffer as changed and prepare for displaying */
7848 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7849 }
7850 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007851 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852}
7853
7854#ifdef FEAT_CINDENT
7855/*
7856 * Return TRUE if C-indenting is on.
7857 */
7858 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007859cindent_on(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007860{
7861 return (!p_paste && (curbuf->b_p_cin
7862# ifdef FEAT_EVAL
7863 || *curbuf->b_p_inde != NUL
7864# endif
7865 ));
7866}
7867#endif
7868
7869#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7870/*
7871 * Re-indent the current line, based on the current contents of it and the
7872 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7873 * confused what all the part that handles Control-T is doing that I'm not.
7874 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7875 */
7876
7877 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007878fixthisline(int (*get_the_indent)(void))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007879{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007880 int amount = get_the_indent();
7881
7882 if (amount >= 0)
7883 {
7884 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7885 if (linewhite(curwin->w_cursor.lnum))
7886 did_ai = TRUE; /* delete the indent if the line stays empty */
7887 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888}
7889
7890 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007891fix_indent(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892{
7893 if (p_paste)
7894 return;
7895# ifdef FEAT_LISP
7896 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7897 fixthisline(get_lisp_indent);
7898# endif
7899# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7900 else
7901# endif
7902# ifdef FEAT_CINDENT
7903 if (cindent_on())
7904 do_c_expr_indent();
7905# endif
7906}
7907
7908#endif
7909
7910#ifdef FEAT_CINDENT
7911/*
7912 * return TRUE if 'cinkeys' contains the key "keytyped",
7913 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007914 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007915 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7916 *
7917 * "keytyped" can have a few special values:
7918 * KEY_OPEN_FORW
7919 * KEY_OPEN_BACK
7920 * KEY_COMPLETE just finished completion.
7921 *
7922 * If line_is_empty is TRUE accept keys with '0' before them.
7923 */
7924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007925in_cinkeys(
7926 int keytyped,
7927 int when,
7928 int line_is_empty)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929{
7930 char_u *look;
7931 int try_match;
7932 int try_match_word;
7933 char_u *p;
7934 char_u *line;
7935 int icase;
7936 int i;
7937
Bram Moolenaar30848942009-12-24 14:46:12 +00007938 if (keytyped == NUL)
7939 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7940 return FALSE;
7941
Bram Moolenaar071d4272004-06-13 20:20:40 +00007942#ifdef FEAT_EVAL
7943 if (*curbuf->b_p_inde != NUL)
7944 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7945 else
7946#endif
7947 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7948 while (*look)
7949 {
7950 /*
7951 * Find out if we want to try a match with this key, depending on
7952 * 'when' and a '*' or '!' before the key.
7953 */
7954 switch (when)
7955 {
7956 case '*': try_match = (*look == '*'); break;
7957 case '!': try_match = (*look == '!'); break;
7958 default: try_match = (*look != '*'); break;
7959 }
7960 if (*look == '*' || *look == '!')
7961 ++look;
7962
7963 /*
7964 * If there is a '0', only accept a match if the line is empty.
7965 * But may still match when typing last char of a word.
7966 */
7967 if (*look == '0')
7968 {
7969 try_match_word = try_match;
7970 if (!line_is_empty)
7971 try_match = FALSE;
7972 ++look;
7973 }
7974 else
7975 try_match_word = FALSE;
7976
7977 /*
7978 * does it look like a control character?
7979 */
7980 if (*look == '^'
7981#ifdef EBCDIC
7982 && (Ctrl_chr(look[1]) != 0)
7983#else
7984 && look[1] >= '?' && look[1] <= '_'
7985#endif
7986 )
7987 {
7988 if (try_match && keytyped == Ctrl_chr(look[1]))
7989 return TRUE;
7990 look += 2;
7991 }
7992 /*
7993 * 'o' means "o" command, open forward.
7994 * 'O' means "O" command, open backward.
7995 */
7996 else if (*look == 'o')
7997 {
7998 if (try_match && keytyped == KEY_OPEN_FORW)
7999 return TRUE;
8000 ++look;
8001 }
8002 else if (*look == 'O')
8003 {
8004 if (try_match && keytyped == KEY_OPEN_BACK)
8005 return TRUE;
8006 ++look;
8007 }
8008
8009 /*
8010 * 'e' means to check for "else" at start of line and just before the
8011 * cursor.
8012 */
8013 else if (*look == 'e')
8014 {
8015 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
8016 {
8017 p = ml_get_curline();
8018 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
8019 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
8020 return TRUE;
8021 }
8022 ++look;
8023 }
8024
8025 /*
8026 * ':' only causes an indent if it is at the end of a label or case
8027 * statement, or when it was before typing the ':' (to fix
8028 * class::method for C++).
8029 */
8030 else if (*look == ':')
8031 {
8032 if (try_match && keytyped == ':')
8033 {
8034 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008035 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008037 /* Need to get the line again after cin_islabel(). */
8038 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 if (curwin->w_cursor.col > 2
8040 && p[curwin->w_cursor.col - 1] == ':'
8041 && p[curwin->w_cursor.col - 2] == ':')
8042 {
8043 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008044 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008045 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046 p = ml_get_curline();
8047 p[curwin->w_cursor.col - 1] = ':';
8048 if (i)
8049 return TRUE;
8050 }
8051 }
8052 ++look;
8053 }
8054
8055
8056 /*
8057 * Is it a key in <>, maybe?
8058 */
8059 else if (*look == '<')
8060 {
8061 if (try_match)
8062 {
8063 /*
8064 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8065 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8066 * >, *, : and ! keys if they really really want to.
8067 */
8068 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8069 && keytyped == look[1])
8070 return TRUE;
8071
8072 if (keytyped == get_special_key_code(look + 1))
8073 return TRUE;
8074 }
8075 while (*look && *look != '>')
8076 look++;
8077 while (*look == '>')
8078 look++;
8079 }
8080
8081 /*
8082 * Is it a word: "=word"?
8083 */
8084 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8085 {
8086 ++look;
8087 if (*look == '~')
8088 {
8089 icase = TRUE;
8090 ++look;
8091 }
8092 else
8093 icase = FALSE;
8094 p = vim_strchr(look, ',');
8095 if (p == NULL)
8096 p = look + STRLEN(look);
8097 if ((try_match || try_match_word)
8098 && curwin->w_cursor.col >= (colnr_T)(p - look))
8099 {
8100 int match = FALSE;
8101
8102#ifdef FEAT_INS_EXPAND
8103 if (keytyped == KEY_COMPLETE)
8104 {
8105 char_u *s;
8106
8107 /* Just completed a word, check if it starts with "look".
8108 * search back for the start of a word. */
8109 line = ml_get_curline();
8110# ifdef FEAT_MBYTE
8111 if (has_mbyte)
8112 {
8113 char_u *n;
8114
8115 for (s = line + curwin->w_cursor.col; s > line; s = n)
8116 {
8117 n = mb_prevptr(line, s);
8118 if (!vim_iswordp(n))
8119 break;
8120 }
8121 }
8122 else
8123# endif
8124 for (s = line + curwin->w_cursor.col; s > line; --s)
8125 if (!vim_iswordc(s[-1]))
8126 break;
8127 if (s + (p - look) <= line + curwin->w_cursor.col
8128 && (icase
8129 ? MB_STRNICMP(s, look, p - look)
8130 : STRNCMP(s, look, p - look)) == 0)
8131 match = TRUE;
8132 }
8133 else
8134#endif
8135 /* TODO: multi-byte */
8136 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8137 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8138 {
8139 line = ml_get_cursor();
8140 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8141 || !vim_iswordc(line[-(p - look) - 1]))
8142 && (icase
8143 ? MB_STRNICMP(line - (p - look), look, p - look)
8144 : STRNCMP(line - (p - look), look, p - look))
8145 == 0)
8146 match = TRUE;
8147 }
8148 if (match && try_match_word && !try_match)
8149 {
8150 /* "0=word": Check if there are only blanks before the
8151 * word. */
8152 line = ml_get_curline();
8153 if ((int)(skipwhite(line) - line) !=
8154 (int)(curwin->w_cursor.col - (p - look)))
8155 match = FALSE;
8156 }
8157 if (match)
8158 return TRUE;
8159 }
8160 look = p;
8161 }
8162
8163 /*
8164 * ok, it's a boring generic character.
8165 */
8166 else
8167 {
8168 if (try_match && *look == keytyped)
8169 return TRUE;
8170 ++look;
8171 }
8172
8173 /*
8174 * Skip over ", ".
8175 */
8176 look = skip_to_option_part(look);
8177 }
8178 return FALSE;
8179}
8180#endif /* FEAT_CINDENT */
8181
8182#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8183/*
8184 * Map Hebrew keyboard when in hkmap mode.
8185 */
8186 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008187hkmap(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188{
8189 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8190 {
8191 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8192 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8193 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8194 static char_u map[26] =
8195 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8196 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8197 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8198 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8199 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8200 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8201 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8202 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8203 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8204
8205 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8206 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8207 /* '-1'='sofit' */
8208 else if (c == 'x')
8209 return 'X';
8210 else if (c == 'q')
8211 return '\''; /* {geresh}={'} */
8212 else if (c == 246)
8213 return ' '; /* \"o --> ' ' for a german keyboard */
8214 else if (c == 228)
8215 return ' '; /* \"a --> ' ' -- / -- */
8216 else if (c == 252)
8217 return ' '; /* \"u --> ' ' -- / -- */
8218#ifdef EBCDIC
8219 else if (islower(c))
8220#else
8221 /* NOTE: islower() does not do the right thing for us on Linux so we
8222 * do this the same was as 5.7 and previous, so it works correctly on
8223 * all systems. Specifically, the e.g. Delete and Arrow keys are
8224 * munged and won't work if e.g. searching for Hebrew text.
8225 */
8226 else if (c >= 'a' && c <= 'z')
8227#endif
8228 return (int)(map[CharOrdLow(c)] + p_aleph);
8229 else
8230 return c;
8231 }
8232 else
8233 {
8234 switch (c)
8235 {
8236 case '`': return ';';
8237 case '/': return '.';
8238 case '\'': return ',';
8239 case 'q': return '/';
8240 case 'w': return '\'';
8241
8242 /* Hebrew letters - set offset from 'a' */
8243 case ',': c = '{'; break;
8244 case '.': c = 'v'; break;
8245 case ';': c = 't'; break;
8246 default: {
8247 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8248
8249#ifdef EBCDIC
8250 /* see note about islower() above */
8251 if (!islower(c))
8252#else
8253 if (c < 'a' || c > 'z')
8254#endif
8255 return c;
8256 c = str[CharOrdLow(c)];
8257 break;
8258 }
8259 }
8260
8261 return (int)(CharOrdLow(c) + p_aleph);
8262 }
8263}
8264#endif
8265
8266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008267ins_reg(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268{
8269 int need_redraw = FALSE;
8270 int regname;
8271 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008272 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273
8274 /*
8275 * If we are going to wait for a character, show a '"'.
8276 */
8277 pc_status = PC_STATUS_UNSET;
8278 if (redrawing() && !char_avail())
8279 {
8280 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008281 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282
8283 edit_putchar('"', TRUE);
8284#ifdef FEAT_CMDL_INFO
8285 add_to_showcmd_c(Ctrl_R);
8286#endif
8287 }
8288
8289#ifdef USE_ON_FLY_SCROLL
8290 dont_scroll = TRUE; /* disallow scrolling here */
8291#endif
8292
8293 /*
8294 * Don't map the register name. This also prevents the mode message to be
8295 * deleted when ESC is hit.
8296 */
8297 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008298 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8301 {
8302 /* Get a third key for literal register insertion */
8303 literally = regname;
8304#ifdef FEAT_CMDL_INFO
8305 add_to_showcmd_c(literally);
8306#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008307 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 }
8310 --no_mapping;
8311
8312#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008313 /* Don't call u_sync() while typing the expression or giving an error
8314 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 ++no_u_sync;
8316 if (regname == '=')
8317 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008318# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008320# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008321 /* Sync undo when evaluating the expression calls setline() or
8322 * append(), so that it can be undone separately. */
8323 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008324
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008326# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008327 /* Restore the Input Method. */
8328 if (im_on)
8329 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008330# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008332 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8333 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008334 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 else
8338 {
8339#endif
8340 if (literally == Ctrl_O || literally == Ctrl_P)
8341 {
8342 /* Append the command to the redo buffer. */
8343 AppendCharToRedobuff(Ctrl_R);
8344 AppendCharToRedobuff(literally);
8345 AppendCharToRedobuff(regname);
8346
8347 do_put(regname, BACKWARD, 1L,
8348 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8349 }
8350 else if (insert_reg(regname, literally) == FAIL)
8351 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008352 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 need_redraw = TRUE; /* remove the '"' */
8354 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008355 else if (stop_insert_mode)
8356 /* When the '=' register was used and a function was invoked that
8357 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8358 * insert anything, need to remove the '"' */
8359 need_redraw = TRUE;
8360
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361#ifdef FEAT_EVAL
8362 }
8363 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008364 if (u_sync_once == 1)
8365 ins_need_undo = TRUE;
8366 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367#endif
8368#ifdef FEAT_CMDL_INFO
8369 clear_showcmd();
8370#endif
8371
8372 /* If the inserted register is empty, we need to remove the '"' */
8373 if (need_redraw || stuff_empty())
8374 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008375
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008376 /* Disallow starting Visual mode here, would get a weird mode. */
8377 if (!vis_active && VIsual_active)
8378 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008379}
8380
8381/*
8382 * CTRL-G commands in Insert mode.
8383 */
8384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008385ins_ctrl_g(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386{
8387 int c;
8388
8389#ifdef FEAT_INS_EXPAND
8390 /* Right after CTRL-X the cursor will be after the ruler. */
8391 setcursor();
8392#endif
8393
8394 /*
8395 * Don't map the second key. This also prevents the mode message to be
8396 * deleted when ESC is hit.
8397 */
8398 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008399 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 --no_mapping;
8401 switch (c)
8402 {
8403 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8404 case K_UP:
8405 case Ctrl_K:
8406 case 'k': ins_up(TRUE);
8407 break;
8408
8409 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8410 case K_DOWN:
8411 case Ctrl_J:
8412 case 'j': ins_down(TRUE);
8413 break;
8414
8415 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008416 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008418
8419 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008420 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008421 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008422 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 break;
8424
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008425 /* CTRL-G U: do not break undo with the next char */
8426 case 'U':
8427 /* Allow one left/right cursor movement with the next char,
8428 * without breaking undo. */
8429 dont_sync_undo = MAYBE;
8430 break;
8431
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008433 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 }
8435}
8436
8437/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008438 * CTRL-^ in Insert mode.
8439 */
8440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008441ins_ctrl_hat(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008442{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008443 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008444 {
8445 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8446 if (State & LANGMAP)
8447 {
8448 curbuf->b_p_iminsert = B_IMODE_NONE;
8449 State &= ~LANGMAP;
8450 }
8451 else
8452 {
8453 curbuf->b_p_iminsert = B_IMODE_LMAP;
8454 State |= LANGMAP;
8455#ifdef USE_IM_CONTROL
8456 im_set_active(FALSE);
8457#endif
8458 }
8459 }
8460#ifdef USE_IM_CONTROL
8461 else
8462 {
8463 /* There are no ":lmap" mappings, toggle IM */
8464 if (im_get_status())
8465 {
8466 curbuf->b_p_iminsert = B_IMODE_NONE;
8467 im_set_active(FALSE);
8468 }
8469 else
8470 {
8471 curbuf->b_p_iminsert = B_IMODE_IM;
8472 State &= ~LANGMAP;
8473 im_set_active(TRUE);
8474 }
8475 }
8476#endif
8477 set_iminsert_global();
8478 showmode();
8479#ifdef FEAT_GUI
8480 /* may show different cursor shape or color */
8481 if (gui.in_use)
8482 gui_update_cursor(TRUE, FALSE);
8483#endif
8484#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8485 /* Show/unshow value of 'keymap' in status lines. */
8486 status_redraw_curbuf();
8487#endif
8488}
8489
8490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008491 * Handle ESC in insert mode.
8492 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8493 * insert.
8494 */
8495 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008496ins_esc(
8497 long *count,
8498 int cmdchar,
8499 int nomove) /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008500{
8501 int temp;
8502 static int disabled_redraw = FALSE;
8503
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008504#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008505 check_spell_redraw();
8506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507#if defined(FEAT_HANGULIN)
8508# if defined(ESC_CHG_TO_ENG_MODE)
8509 hangul_input_state_set(0);
8510# endif
8511 if (composing_hangul)
8512 {
8513 push_raw_key(composing_hangul_buffer, 2);
8514 composing_hangul = 0;
8515 }
8516#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517
8518 temp = curwin->w_cursor.col;
8519 if (disabled_redraw)
8520 {
8521 --RedrawingDisabled;
8522 disabled_redraw = FALSE;
8523 }
8524 if (!arrow_used)
8525 {
8526 /*
8527 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008528 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8529 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 */
8531 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008532 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533
8534 /*
8535 * Repeating insert may take a long time. Check for
8536 * interrupt now and then.
8537 */
8538 if (*count > 0)
8539 {
8540 line_breakcheck();
8541 if (got_int)
8542 *count = 0;
8543 }
8544
8545 if (--*count > 0) /* repeat what was typed */
8546 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008547 /* Vi repeats the insert without replacing characters. */
8548 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8549 State &= ~REPLACE_FLAG;
8550
Bram Moolenaar071d4272004-06-13 20:20:40 +00008551 (void)start_redo_ins();
8552 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008553 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 ++RedrawingDisabled;
8555 disabled_redraw = TRUE;
8556 return FALSE; /* repeat the insert */
8557 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008558 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 undisplay_dollar();
8560 }
8561
8562 /* When an autoindent was removed, curswant stays after the
8563 * indent */
8564 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8565 curwin->w_set_curswant = TRUE;
8566
8567 /* Remember the last Insert position in the '^ mark. */
8568 if (!cmdmod.keepjumps)
8569 curbuf->b_last_insert = curwin->w_cursor;
8570
8571 /*
8572 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008573 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008575 if (!nomove
8576 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008577#ifdef FEAT_VIRTUALEDIT
8578 || curwin->w_cursor.coladd > 0
8579#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008580 )
8581 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008582 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583#ifdef FEAT_RIGHTLEFT
8584 && !revins_on
8585#endif
8586 )
8587 {
8588#ifdef FEAT_VIRTUALEDIT
8589 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8590 {
8591 oneleft();
8592 if (restart_edit != NUL)
8593 ++curwin->w_cursor.coladd;
8594 }
8595 else
8596#endif
8597 {
8598 --curwin->w_cursor.col;
8599#ifdef FEAT_MBYTE
8600 /* Correct cursor for multi-byte character. */
8601 if (has_mbyte)
8602 mb_adjust_cursor();
8603#endif
8604 }
8605 }
8606
8607#ifdef USE_IM_CONTROL
8608 /* Disable IM to allow typing English directly for Normal mode commands.
8609 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8610 * well). */
8611 if (!(State & LANGMAP))
8612 im_save_status(&curbuf->b_p_iminsert);
8613 im_set_active(FALSE);
8614#endif
8615
8616 State = NORMAL;
8617 /* need to position cursor again (e.g. when on a TAB ) */
8618 changed_cline_bef_curs();
8619
8620#ifdef FEAT_MOUSE
8621 setmouse();
8622#endif
8623#ifdef CURSOR_SHAPE
8624 ui_cursor_shape(); /* may show different cursor shape */
8625#endif
8626
8627 /*
8628 * When recording or for CTRL-O, need to display the new mode.
8629 * Otherwise remove the mode message.
8630 */
8631 if (Recording || restart_edit != NUL)
8632 showmode();
8633 else if (p_smd)
8634 MSG("");
8635
8636 return TRUE; /* exit Insert mode */
8637}
8638
8639#ifdef FEAT_RIGHTLEFT
8640/*
8641 * Toggle language: hkmap and revins_on.
8642 * Move to end of reverse inserted text.
8643 */
8644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008645ins_ctrl_(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646{
8647 if (revins_on && revins_chars && revins_scol >= 0)
8648 {
8649 while (gchar_cursor() != NUL && revins_chars--)
8650 ++curwin->w_cursor.col;
8651 }
8652 p_ri = !p_ri;
8653 revins_on = (State == INSERT && p_ri);
8654 if (revins_on)
8655 {
8656 revins_scol = curwin->w_cursor.col;
8657 revins_legal++;
8658 revins_chars = 0;
8659 undisplay_dollar();
8660 }
8661 else
8662 revins_scol = -1;
8663#ifdef FEAT_FKMAP
8664 if (p_altkeymap)
8665 {
8666 /*
8667 * to be consistent also for redo command, using '.'
8668 * set arrow_used to true and stop it - causing to redo
8669 * characters entered in one mode (normal/reverse insert).
8670 */
8671 arrow_used = TRUE;
8672 (void)stop_arrow();
8673 p_fkmap = curwin->w_p_rl ^ p_ri;
8674 if (p_fkmap && p_ri)
8675 State = INSERT;
8676 }
8677 else
8678#endif
8679 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8680 showmode();
8681}
8682#endif
8683
Bram Moolenaar071d4272004-06-13 20:20:40 +00008684/*
8685 * If 'keymodel' contains "startsel", may start selection.
8686 * Returns TRUE when a CTRL-O and other keys stuffed.
8687 */
8688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008689ins_start_select(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008690{
8691 if (km_startsel)
8692 switch (c)
8693 {
8694 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008695 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 case K_PAGEUP:
8697 case K_KPAGEUP:
8698 case K_PAGEDOWN:
8699 case K_KPAGEDOWN:
8700# ifdef MACOS
8701 case K_LEFT:
8702 case K_RIGHT:
8703 case K_UP:
8704 case K_DOWN:
8705 case K_END:
8706 case K_HOME:
8707# endif
8708 if (!(mod_mask & MOD_MASK_SHIFT))
8709 break;
8710 /* FALLTHROUGH */
8711 case K_S_LEFT:
8712 case K_S_RIGHT:
8713 case K_S_UP:
8714 case K_S_DOWN:
8715 case K_S_END:
8716 case K_S_HOME:
8717 /* Start selection right away, the cursor can move with
8718 * CTRL-O when beyond the end of the line. */
8719 start_selection();
8720
8721 /* Execute the key in (insert) Select mode. */
8722 stuffcharReadbuff(Ctrl_O);
8723 if (mod_mask)
8724 {
8725 char_u buf[4];
8726
8727 buf[0] = K_SPECIAL;
8728 buf[1] = KS_MODIFIER;
8729 buf[2] = mod_mask;
8730 buf[3] = NUL;
8731 stuffReadbuff(buf);
8732 }
8733 stuffcharReadbuff(c);
8734 return TRUE;
8735 }
8736 return FALSE;
8737}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738
8739/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008740 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008741 */
8742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008743ins_insert(int replaceState)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008744{
8745#ifdef FEAT_FKMAP
8746 if (p_fkmap && p_ri)
8747 {
8748 beep_flush();
8749 EMSG(farsi_text_3); /* encoded in Farsi */
8750 return;
8751 }
8752#endif
8753
8754#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008755# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008756 set_vim_var_string(VV_INSERTMODE,
8757 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008758# ifdef FEAT_VREPLACE
8759 replaceState == VREPLACE ? "v" :
8760# endif
8761 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008762# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008763 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8764#endif
8765 if (State & REPLACE_FLAG)
8766 State = INSERT | (State & LANGMAP);
8767 else
8768 State = replaceState | (State & LANGMAP);
8769 AppendCharToRedobuff(K_INS);
8770 showmode();
8771#ifdef CURSOR_SHAPE
8772 ui_cursor_shape(); /* may show different cursor shape */
8773#endif
8774}
8775
8776/*
8777 * Pressed CTRL-O in Insert mode.
8778 */
8779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008780ins_ctrl_o(void)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008781{
8782#ifdef FEAT_VREPLACE
8783 if (State & VREPLACE_FLAG)
8784 restart_edit = 'V';
8785 else
8786#endif
8787 if (State & REPLACE_FLAG)
8788 restart_edit = 'R';
8789 else
8790 restart_edit = 'I';
8791#ifdef FEAT_VIRTUALEDIT
8792 if (virtual_active())
8793 ins_at_eol = FALSE; /* cursor always keeps its column */
8794 else
8795#endif
8796 ins_at_eol = (gchar_cursor() == NUL);
8797}
8798
8799/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008800 * If the cursor is on an indent, ^T/^D insert/delete one
8801 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008802 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 * with vi. But vi only supports ^T and ^D after an
8804 * autoindent, we support it everywhere.
8805 */
8806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008807ins_shift(int c, int lastc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808{
8809 if (stop_arrow() == FAIL)
8810 return;
8811 AppendCharToRedobuff(c);
8812
8813 /*
8814 * 0^D and ^^D: remove all indent.
8815 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008816 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8817 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 {
8819 --curwin->w_cursor.col;
8820 (void)del_char(FALSE); /* delete the '^' or '0' */
8821 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8822 if (State & REPLACE_FLAG)
8823 replace_pop_ins();
8824 if (lastc == '^')
8825 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008826 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827 }
8828 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008829 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830
8831 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8832 did_ai = FALSE;
8833#ifdef FEAT_SMARTINDENT
8834 did_si = FALSE;
8835 can_si = FALSE;
8836 can_si_back = FALSE;
8837#endif
8838#ifdef FEAT_CINDENT
8839 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8840#endif
8841}
8842
8843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008844ins_del(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845{
8846 int temp;
8847
8848 if (stop_arrow() == FAIL)
8849 return;
8850 if (gchar_cursor() == NUL) /* delete newline */
8851 {
8852 temp = curwin->w_cursor.col;
8853 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008854 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008855 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 else
8857 curwin->w_cursor.col = temp;
8858 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008859 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8860 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 did_ai = FALSE;
8862#ifdef FEAT_SMARTINDENT
8863 did_si = FALSE;
8864 can_si = FALSE;
8865 can_si_back = FALSE;
8866#endif
8867 AppendCharToRedobuff(K_DEL);
8868}
8869
Bram Moolenaarf28dbce2016-01-29 22:03:47 +01008870static void ins_bs_one(colnr_T *vcolp);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008871
8872/*
8873 * Delete one character for ins_bs().
8874 */
8875 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008876ins_bs_one(colnr_T *vcolp)
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008877{
8878 dec_cursor();
8879 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8880 if (State & REPLACE_FLAG)
8881 {
8882 /* Don't delete characters before the insert point when in
8883 * Replace mode */
8884 if (curwin->w_cursor.lnum != Insstart.lnum
8885 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008886 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008887 }
8888 else
8889 (void)del_char(FALSE);
8890}
8891
Bram Moolenaar071d4272004-06-13 20:20:40 +00008892/*
8893 * Handle Backspace, delete-word and delete-line in Insert mode.
8894 * Return TRUE when backspace was actually used.
8895 */
8896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008897ins_bs(
8898 int c,
8899 int mode,
8900 int *inserted_space_p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901{
8902 linenr_T lnum;
8903 int cc;
8904 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008905 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 colnr_T mincol;
8907 int did_backspace = FALSE;
8908 int in_indent;
8909 int oldState;
8910#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008911 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912#endif
8913
8914 /*
8915 * can't delete anything in an empty file
8916 * can't backup past first character in buffer
8917 * can't backup past starting point unless 'backspace' > 1
8918 * can backup to a previous line if 'backspace' == 0
8919 */
8920 if ( bufempty()
8921 || (
8922#ifdef FEAT_RIGHTLEFT
8923 !revins_on &&
8924#endif
8925 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8926 || (!can_bs(BS_START)
8927 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008928 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8929 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8931 && curwin->w_cursor.col <= ai_col)
8932 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8933 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008934 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 return FALSE;
8936 }
8937
8938 if (stop_arrow() == FAIL)
8939 return FALSE;
8940 in_indent = inindent(0);
8941#ifdef FEAT_CINDENT
8942 if (in_indent)
8943 can_cindent = FALSE;
8944#endif
8945#ifdef FEAT_COMMENTS
8946 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8947#endif
8948#ifdef FEAT_RIGHTLEFT
8949 if (revins_on) /* put cursor after last inserted char */
8950 inc_cursor();
8951#endif
8952
8953#ifdef FEAT_VIRTUALEDIT
8954 /* Virtualedit:
8955 * BACKSPACE_CHAR eats a virtual space
8956 * BACKSPACE_WORD eats all coladd
8957 * BACKSPACE_LINE eats all coladd and keeps going
8958 */
8959 if (curwin->w_cursor.coladd > 0)
8960 {
8961 if (mode == BACKSPACE_CHAR)
8962 {
8963 --curwin->w_cursor.coladd;
8964 return TRUE;
8965 }
8966 if (mode == BACKSPACE_WORD)
8967 {
8968 curwin->w_cursor.coladd = 0;
8969 return TRUE;
8970 }
8971 curwin->w_cursor.coladd = 0;
8972 }
8973#endif
8974
8975 /*
8976 * delete newline!
8977 */
8978 if (curwin->w_cursor.col == 0)
8979 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008980 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008981 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008982#ifdef FEAT_RIGHTLEFT
8983 || revins_on
8984#endif
8985 )
8986 {
8987 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8988 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8989 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008990 --Insstart.lnum;
8991 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992 }
8993 /*
8994 * In replace mode:
8995 * cc < 0: NL was inserted, delete it
8996 * cc >= 0: NL was replaced, put original characters back
8997 */
8998 cc = -1;
8999 if (State & REPLACE_FLAG)
9000 cc = replace_pop(); /* returns -1 if NL was inserted */
9001 /*
9002 * In replace mode, in the line we started replacing, we only move the
9003 * cursor.
9004 */
9005 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
9006 {
9007 dec_cursor();
9008 }
9009 else
9010 {
9011#ifdef FEAT_VREPLACE
9012 if (!(State & VREPLACE_FLAG)
9013 || curwin->w_cursor.lnum > orig_line_count)
9014#endif
9015 {
9016 temp = gchar_cursor(); /* remember current char */
9017 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009018
9019 /* When "aw" is in 'formatoptions' we must delete the space at
9020 * the end of the line, otherwise the line will be broken
9021 * again when auto-formatting. */
9022 if (has_format_option(FO_AUTO)
9023 && has_format_option(FO_WHITE_PAR))
9024 {
9025 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9026 TRUE);
9027 int len;
9028
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009029 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009030 if (len > 0 && ptr[len - 1] == ' ')
9031 ptr[len - 1] = NUL;
9032 }
9033
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009034 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 if (temp == NUL && gchar_cursor() != NUL)
9036 inc_cursor();
9037 }
9038#ifdef FEAT_VREPLACE
9039 else
9040 dec_cursor();
9041#endif
9042
9043 /*
9044 * In REPLACE mode we have to put back the text that was replaced
9045 * by the NL. On the replace stack is first a NUL-terminated
9046 * sequence of characters that were deleted and then the
9047 * characters that NL replaced.
9048 */
9049 if (State & REPLACE_FLAG)
9050 {
9051 /*
9052 * Do the next ins_char() in NORMAL state, to
9053 * prevent ins_char() from replacing characters and
9054 * avoiding showmatch().
9055 */
9056 oldState = State;
9057 State = NORMAL;
9058 /*
9059 * restore characters (blanks) deleted after cursor
9060 */
9061 while (cc > 0)
9062 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009063 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064#ifdef FEAT_MBYTE
9065 mb_replace_pop_ins(cc);
9066#else
9067 ins_char(cc);
9068#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009069 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 cc = replace_pop();
9071 }
9072 /* restore the characters that NL replaced */
9073 replace_pop_ins();
9074 State = oldState;
9075 }
9076 }
9077 did_ai = FALSE;
9078 }
9079 else
9080 {
9081 /*
9082 * Delete character(s) before the cursor.
9083 */
9084#ifdef FEAT_RIGHTLEFT
9085 if (revins_on) /* put cursor on last inserted char */
9086 dec_cursor();
9087#endif
9088 mincol = 0;
9089 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009090 if (mode == BACKSPACE_LINE
9091 && (curbuf->b_p_ai
9092#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009093 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009094#endif
9095 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096#ifdef FEAT_RIGHTLEFT
9097 && !revins_on
9098#endif
9099 )
9100 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009101 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009103 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009105 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009106 }
9107
9108 /*
9109 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9110 */
9111 if ( mode == BACKSPACE_CHAR
9112 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009113 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009114 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 && (*(ml_get_cursor() - 1) == TAB
9116 || (*(ml_get_cursor() - 1) == ' '
9117 && (!*inserted_space_p
9118 || arrow_used))))))
9119 {
9120 int ts;
9121 colnr_T vcol;
9122 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009123 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009124
9125 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009126 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009127 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009128 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009129 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130 /* Compute the virtual column where we want to be. Since
9131 * 'showbreak' may get in the way, need to get the last column of
9132 * the previous character. */
9133 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009134 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135 dec_cursor();
9136 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9137 inc_cursor();
9138 want_vcol = (want_vcol / ts) * ts;
9139
9140 /* delete characters until we are at or before want_vcol */
9141 while (vcol > want_vcol
9142 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009143 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009144
9145 /* insert extra spaces until we are at want_vcol */
9146 while (vcol < want_vcol)
9147 {
9148 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009149 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9150 && curwin->w_cursor.col < Insstart_orig.col)
9151 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009152
9153#ifdef FEAT_VREPLACE
9154 if (State & VREPLACE_FLAG)
9155 ins_char(' ');
9156 else
9157#endif
9158 {
9159 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009160 if ((State & REPLACE_FLAG))
9161 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 }
9163 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9164 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009165
9166 /* If we are now back where we started delete one character. Can
9167 * happen when using 'sts' and 'linebreak'. */
9168 if (vcol >= start_vcol)
9169 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009170 }
9171
9172 /*
9173 * Delete upto starting point, start of line or previous word.
9174 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009175 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009177#ifdef FEAT_MBYTE
9178 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009179
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009180 if (has_mbyte)
9181 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009183 do
9184 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009186 if (!revins_on) /* put cursor on char to be deleted */
9187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009189
9190 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009192 /* look multi-byte character class */
9193 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009195 prev_cclass = cclass;
9196 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009199
9200 /* start of word? */
9201 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9202 {
9203 mode = BACKSPACE_WORD_NOT_SPACE;
9204 temp = vim_iswordc(cc);
9205 }
9206 /* end of word? */
9207 else if (mode == BACKSPACE_WORD_NOT_SPACE
9208 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9209#ifdef FEAT_MBYTE
9210 || prev_cclass != cclass
9211#endif
9212 ))
9213 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009215 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009216#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009217 inc_cursor();
9218#ifdef FEAT_RIGHTLEFT
9219 else if (State & REPLACE_FLAG)
9220 dec_cursor();
9221#endif
9222 break;
9223 }
9224 if (State & REPLACE_FLAG)
9225 replace_do_bs(-1);
9226 else
9227 {
9228#ifdef FEAT_MBYTE
9229 if (enc_utf8 && p_deco)
9230 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9231#endif
9232 (void)del_char(FALSE);
9233#ifdef FEAT_MBYTE
9234 /*
9235 * If there are combining characters and 'delcombine' is set
9236 * move the cursor back. Don't back up before the base
9237 * character.
9238 */
9239 if (enc_utf8 && p_deco && cpc[0] != NUL)
9240 inc_cursor();
9241#endif
9242#ifdef FEAT_RIGHTLEFT
9243 if (revins_chars)
9244 {
9245 revins_chars--;
9246 revins_legal++;
9247 }
9248 if (revins_on && gchar_cursor() == NUL)
9249 break;
9250#endif
9251 }
9252 /* Just a single backspace?: */
9253 if (mode == BACKSPACE_CHAR)
9254 break;
9255 } while (
9256#ifdef FEAT_RIGHTLEFT
9257 revins_on ||
9258#endif
9259 (curwin->w_cursor.col > mincol
9260 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9261 || curwin->w_cursor.col != Insstart_orig.col)));
9262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009263 did_backspace = TRUE;
9264 }
9265#ifdef FEAT_SMARTINDENT
9266 did_si = FALSE;
9267 can_si = FALSE;
9268 can_si_back = FALSE;
9269#endif
9270 if (curwin->w_cursor.col <= 1)
9271 did_ai = FALSE;
9272 /*
9273 * It's a little strange to put backspaces into the redo
9274 * buffer, but it makes auto-indent a lot easier to deal
9275 * with.
9276 */
9277 AppendCharToRedobuff(c);
9278
9279 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009280 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9281 && curwin->w_cursor.col < Insstart_orig.col)
9282 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283
9284 /* vi behaviour: the cursor moves backward but the character that
9285 * was there remains visible
9286 * Vim behaviour: the cursor moves backward and the character that
9287 * was there is erased from the screen.
9288 * We can emulate the vi behaviour by pretending there is a dollar
9289 * displayed even when there isn't.
9290 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009291 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 dollar_vcol = curwin->w_virtcol;
9293
Bram Moolenaarce3be472008-01-14 19:12:28 +00009294#ifdef FEAT_FOLDING
9295 /* When deleting a char the cursor line must never be in a closed fold.
9296 * E.g., when 'foldmethod' is indent and deleting the first non-white
9297 * char before a Tab. */
9298 if (did_backspace)
9299 foldOpenCursor();
9300#endif
9301
Bram Moolenaar071d4272004-06-13 20:20:40 +00009302 return did_backspace;
9303}
9304
9305#ifdef FEAT_MOUSE
9306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009307ins_mouse(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009310 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009311
9312# ifdef FEAT_GUI
9313 /* When GUI is active, also move/paste when 'mouse' is empty */
9314 if (!gui.in_use)
9315# endif
9316 if (!mouse_has(MOUSE_INSERT))
9317 return;
9318
9319 undisplay_dollar();
9320 tpos = curwin->w_cursor;
9321 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9322 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009323#ifdef FEAT_WINDOWS
9324 win_T *new_curwin = curwin;
9325
9326 if (curwin != old_curwin && win_valid(old_curwin))
9327 {
9328 /* Mouse took us to another window. We need to go back to the
9329 * previous one to stop insert there properly. */
9330 curwin = old_curwin;
9331 curbuf = curwin->w_buffer;
9332 }
9333#endif
9334 start_arrow(curwin == old_curwin ? &tpos : NULL);
9335#ifdef FEAT_WINDOWS
9336 if (curwin != new_curwin && win_valid(new_curwin))
9337 {
9338 curwin = new_curwin;
9339 curbuf = curwin->w_buffer;
9340 }
9341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009342# ifdef FEAT_CINDENT
9343 can_cindent = TRUE;
9344# endif
9345 }
9346
9347#ifdef FEAT_WINDOWS
9348 /* redraw status lines (in case another window became active) */
9349 redraw_statuslines();
9350#endif
9351}
9352
9353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009354ins_mousescroll(int dir)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355{
9356 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009357# if defined(FEAT_WINDOWS)
9358 win_T *old_curwin = curwin;
9359# endif
9360# ifdef FEAT_INS_EXPAND
9361 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009362# endif
9363
9364 tpos = curwin->w_cursor;
9365
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009366# ifdef FEAT_WINDOWS
9367 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009368 {
9369 int row, col;
9370
9371 row = mouse_row;
9372 col = mouse_col;
9373
9374 /* find the window at the pointer coordinates */
9375 curwin = mouse_find_win(&row, &col);
9376 curbuf = curwin->w_buffer;
9377 }
9378 if (curwin == old_curwin)
9379# endif
9380 undisplay_dollar();
9381
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009382# ifdef FEAT_INS_EXPAND
9383 /* Don't scroll the window in which completion is being done. */
9384 if (!pum_visible()
9385# if defined(FEAT_WINDOWS)
9386 || curwin != old_curwin
9387# endif
9388 )
9389# endif
9390 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009391 if (dir == MSCR_DOWN || dir == MSCR_UP)
9392 {
9393 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9394 scroll_redraw(dir,
9395 (long)(curwin->w_botline - curwin->w_topline));
9396 else
9397 scroll_redraw(dir, 3L);
9398 }
9399#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009400 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009401 {
9402 int val, step = 6;
9403
9404 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9405 step = W_WIDTH(curwin);
9406 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9407 if (val < 0)
9408 val = 0;
9409 gui_do_horiz_scroll(val, TRUE);
9410 }
9411#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009412# ifdef FEAT_INS_EXPAND
9413 did_scroll = TRUE;
9414# endif
9415 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009416
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009417# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 curwin->w_redr_status = TRUE;
9419
9420 curwin = old_curwin;
9421 curbuf = curwin->w_buffer;
9422# endif
9423
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009424# ifdef FEAT_INS_EXPAND
9425 /* The popup menu may overlay the window, need to redraw it.
9426 * TODO: Would be more efficient to only redraw the windows that are
9427 * overlapped by the popup menu. */
9428 if (pum_visible() && did_scroll)
9429 {
9430 redraw_all_later(NOT_VALID);
9431 ins_compl_show_pum();
9432 }
9433# endif
9434
Bram Moolenaar071d4272004-06-13 20:20:40 +00009435 if (!equalpos(curwin->w_cursor, tpos))
9436 {
9437 start_arrow(&tpos);
9438# ifdef FEAT_CINDENT
9439 can_cindent = TRUE;
9440# endif
9441 }
9442}
9443#endif
9444
Bram Moolenaarec2da362017-01-21 20:04:22 +01009445/*
9446 * Handle receiving P_PS: start paste mode. Inserts the following text up to
9447 * P_PE literally.
9448 * When "drop" is TRUE then consume the text and drop it.
9449 */
9450 int
9451bracketed_paste(paste_mode_T mode, int drop, garray_T *gap)
9452{
9453 int c;
9454 char_u buf[NUMBUFLEN + MB_MAXBYTES];
9455 int idx = 0;
9456 char_u *end = find_termcode((char_u *)"PE");
9457 int ret_char = -1;
9458 int save_allow_keys = allow_keys;
9459
9460 /* If the end code is too long we can't detect it, read everything. */
9461 if (STRLEN(end) >= NUMBUFLEN)
9462 end = NULL;
9463 ++no_mapping;
9464 allow_keys = 0;
9465 for (;;)
9466 {
9467 /* When the end is not defined read everything. */
9468 if (end == NULL && vpeekc() == NUL)
9469 break;
9470 c = plain_vgetc();
9471#ifdef FEAT_MBYTE
9472 if (has_mbyte)
9473 idx += (*mb_char2bytes)(c, buf + idx);
9474 else
9475#endif
9476 buf[idx++] = c;
9477 buf[idx] = NUL;
9478 if (end != NUL && STRNCMP(buf, end, idx) == 0)
9479 {
9480 if (end[idx] == NUL)
9481 break; /* Found the end of paste code. */
9482 continue;
9483 }
9484 if (!drop)
9485 {
9486 switch (mode)
9487 {
9488 case PASTE_CMDLINE:
9489 put_on_cmdline(buf, idx, TRUE);
9490 break;
9491
9492 case PASTE_EX:
9493 if (gap != NULL && ga_grow(gap, idx) == OK)
9494 {
9495 mch_memmove((char *)gap->ga_data + gap->ga_len,
9496 buf, (size_t)idx);
9497 gap->ga_len += idx;
9498 }
9499 break;
9500
9501 case PASTE_INSERT:
9502 if (stop_arrow() == OK)
9503 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009504 c = buf[0];
9505 if (idx == 1 && (c == CAR || c == K_KENTER || c == NL))
9506 ins_eol(c);
9507 else
Bram Moolenaar076e5022017-01-24 18:58:30 +01009508 {
Bram Moolenaar915350e2017-01-24 17:50:52 +01009509 ins_char_bytes(buf, idx);
Bram Moolenaar076e5022017-01-24 18:58:30 +01009510 AppendToRedobuffLit(buf, idx);
9511 }
Bram Moolenaarec2da362017-01-21 20:04:22 +01009512 }
9513 break;
9514
9515 case PASTE_ONE_CHAR:
9516 if (ret_char == -1)
9517 {
9518#ifdef FEAT_MBYTE
9519 if (has_mbyte)
9520 ret_char = (*mb_ptr2char)(buf);
9521 else
9522#endif
9523 ret_char = buf[0];
9524 }
9525 break;
9526 }
9527 }
9528 idx = 0;
9529 }
9530 --no_mapping;
9531 allow_keys = save_allow_keys;
9532
9533 return ret_char;
9534}
9535
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009536#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009538ins_tabline(int c)
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009539{
9540 /* We will be leaving the current window, unless closing another tab. */
9541 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9542 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9543 {
9544 undisplay_dollar();
9545 start_arrow(&curwin->w_cursor);
9546# ifdef FEAT_CINDENT
9547 can_cindent = TRUE;
9548# endif
9549 }
9550
9551 if (c == K_TABLINE)
9552 goto_tabpage(current_tab);
9553 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009554 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009555 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009556 redraw_statuslines(); /* will redraw the tabline when needed */
9557 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009558}
9559#endif
9560
9561#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009562 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009563ins_scroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009564{
9565 pos_T tpos;
9566
9567 undisplay_dollar();
9568 tpos = curwin->w_cursor;
9569 if (gui_do_scroll())
9570 {
9571 start_arrow(&tpos);
9572# ifdef FEAT_CINDENT
9573 can_cindent = TRUE;
9574# endif
9575 }
9576}
9577
9578 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009579ins_horscroll(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580{
9581 pos_T tpos;
9582
9583 undisplay_dollar();
9584 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009585 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586 {
9587 start_arrow(&tpos);
9588# ifdef FEAT_CINDENT
9589 can_cindent = TRUE;
9590# endif
9591 }
9592}
9593#endif
9594
9595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009596ins_left(
9597 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598{
9599 pos_T tpos;
9600
9601#ifdef FEAT_FOLDING
9602 if ((fdo_flags & FDO_HOR) && KeyTyped)
9603 foldOpenCursor();
9604#endif
9605 undisplay_dollar();
9606 tpos = curwin->w_cursor;
9607 if (oneleft() == OK)
9608 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009609#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9610 /* Only call start_arrow() when not busy with preediting, it will
9611 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9612 if (!im_is_preediting())
9613#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009614 {
9615 start_arrow_with_change(&tpos, end_change);
9616 if (!end_change)
9617 AppendCharToRedobuff(K_LEFT);
9618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619#ifdef FEAT_RIGHTLEFT
9620 /* If exit reversed string, position is fixed */
9621 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9622 revins_legal++;
9623 revins_chars++;
9624#endif
9625 }
9626
9627 /*
9628 * if 'whichwrap' set for cursor in insert mode may go to
9629 * previous line
9630 */
9631 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9632 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009633 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 start_arrow(&tpos);
9635 --(curwin->w_cursor.lnum);
9636 coladvance((colnr_T)MAXCOL);
9637 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9638 }
9639 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009640 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009641 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642}
9643
9644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009645ins_home(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646{
9647 pos_T tpos;
9648
9649#ifdef FEAT_FOLDING
9650 if ((fdo_flags & FDO_HOR) && KeyTyped)
9651 foldOpenCursor();
9652#endif
9653 undisplay_dollar();
9654 tpos = curwin->w_cursor;
9655 if (c == K_C_HOME)
9656 curwin->w_cursor.lnum = 1;
9657 curwin->w_cursor.col = 0;
9658#ifdef FEAT_VIRTUALEDIT
9659 curwin->w_cursor.coladd = 0;
9660#endif
9661 curwin->w_curswant = 0;
9662 start_arrow(&tpos);
9663}
9664
9665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009666ins_end(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
9668 pos_T tpos;
9669
9670#ifdef FEAT_FOLDING
9671 if ((fdo_flags & FDO_HOR) && KeyTyped)
9672 foldOpenCursor();
9673#endif
9674 undisplay_dollar();
9675 tpos = curwin->w_cursor;
9676 if (c == K_C_END)
9677 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9678 coladvance((colnr_T)MAXCOL);
9679 curwin->w_curswant = MAXCOL;
9680
9681 start_arrow(&tpos);
9682}
9683
9684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009685ins_s_left(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686{
9687#ifdef FEAT_FOLDING
9688 if ((fdo_flags & FDO_HOR) && KeyTyped)
9689 foldOpenCursor();
9690#endif
9691 undisplay_dollar();
9692 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9693 {
9694 start_arrow(&curwin->w_cursor);
9695 (void)bck_word(1L, FALSE, FALSE);
9696 curwin->w_set_curswant = TRUE;
9697 }
9698 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009699 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700}
9701
9702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009703ins_right(
9704 int end_change) /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706#ifdef FEAT_FOLDING
9707 if ((fdo_flags & FDO_HOR) && KeyTyped)
9708 foldOpenCursor();
9709#endif
9710 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009711 if (gchar_cursor() != NUL
9712#ifdef FEAT_VIRTUALEDIT
9713 || virtual_active()
9714#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 )
9716 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009717 start_arrow_with_change(&curwin->w_cursor, end_change);
9718 if (!end_change)
9719 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 curwin->w_set_curswant = TRUE;
9721#ifdef FEAT_VIRTUALEDIT
9722 if (virtual_active())
9723 oneright();
9724 else
9725#endif
9726 {
9727#ifdef FEAT_MBYTE
9728 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009729 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 else
9731#endif
9732 ++curwin->w_cursor.col;
9733 }
9734
9735#ifdef FEAT_RIGHTLEFT
9736 revins_legal++;
9737 if (revins_chars)
9738 revins_chars--;
9739#endif
9740 }
9741 /* if 'whichwrap' set for cursor in insert mode, may move the
9742 * cursor to the next line */
9743 else if (vim_strchr(p_ww, ']') != NULL
9744 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9745 {
9746 start_arrow(&curwin->w_cursor);
9747 curwin->w_set_curswant = TRUE;
9748 ++curwin->w_cursor.lnum;
9749 curwin->w_cursor.col = 0;
9750 }
9751 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009752 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009753 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754}
9755
9756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009757ins_s_right(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758{
9759#ifdef FEAT_FOLDING
9760 if ((fdo_flags & FDO_HOR) && KeyTyped)
9761 foldOpenCursor();
9762#endif
9763 undisplay_dollar();
9764 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9765 || gchar_cursor() != NUL)
9766 {
9767 start_arrow(&curwin->w_cursor);
9768 (void)fwd_word(1L, FALSE, 0);
9769 curwin->w_set_curswant = TRUE;
9770 }
9771 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009772 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773}
9774
9775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009776ins_up(
9777 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009778{
9779 pos_T tpos;
9780 linenr_T old_topline = curwin->w_topline;
9781#ifdef FEAT_DIFF
9782 int old_topfill = curwin->w_topfill;
9783#endif
9784
9785 undisplay_dollar();
9786 tpos = curwin->w_cursor;
9787 if (cursor_up(1L, TRUE) == OK)
9788 {
9789 if (startcol)
9790 coladvance(getvcol_nolist(&Insstart));
9791 if (old_topline != curwin->w_topline
9792#ifdef FEAT_DIFF
9793 || old_topfill != curwin->w_topfill
9794#endif
9795 )
9796 redraw_later(VALID);
9797 start_arrow(&tpos);
9798#ifdef FEAT_CINDENT
9799 can_cindent = TRUE;
9800#endif
9801 }
9802 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009803 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804}
9805
9806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009807ins_pageup(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808{
9809 pos_T tpos;
9810
9811 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009812
9813#ifdef FEAT_WINDOWS
9814 if (mod_mask & MOD_MASK_CTRL)
9815 {
9816 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009817 if (first_tabpage->tp_next != NULL)
9818 {
9819 start_arrow(&curwin->w_cursor);
9820 goto_tabpage(-1);
9821 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009822 return;
9823 }
9824#endif
9825
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 tpos = curwin->w_cursor;
9827 if (onepage(BACKWARD, 1L) == OK)
9828 {
9829 start_arrow(&tpos);
9830#ifdef FEAT_CINDENT
9831 can_cindent = TRUE;
9832#endif
9833 }
9834 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009835 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836}
9837
9838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009839ins_down(
9840 int startcol) /* when TRUE move to Insstart.col */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841{
9842 pos_T tpos;
9843 linenr_T old_topline = curwin->w_topline;
9844#ifdef FEAT_DIFF
9845 int old_topfill = curwin->w_topfill;
9846#endif
9847
9848 undisplay_dollar();
9849 tpos = curwin->w_cursor;
9850 if (cursor_down(1L, TRUE) == OK)
9851 {
9852 if (startcol)
9853 coladvance(getvcol_nolist(&Insstart));
9854 if (old_topline != curwin->w_topline
9855#ifdef FEAT_DIFF
9856 || old_topfill != curwin->w_topfill
9857#endif
9858 )
9859 redraw_later(VALID);
9860 start_arrow(&tpos);
9861#ifdef FEAT_CINDENT
9862 can_cindent = TRUE;
9863#endif
9864 }
9865 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009866 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867}
9868
9869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009870ins_pagedown(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871{
9872 pos_T tpos;
9873
9874 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009875
9876#ifdef FEAT_WINDOWS
9877 if (mod_mask & MOD_MASK_CTRL)
9878 {
9879 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009880 if (first_tabpage->tp_next != NULL)
9881 {
9882 start_arrow(&curwin->w_cursor);
9883 goto_tabpage(0);
9884 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009885 return;
9886 }
9887#endif
9888
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889 tpos = curwin->w_cursor;
9890 if (onepage(FORWARD, 1L) == OK)
9891 {
9892 start_arrow(&tpos);
9893#ifdef FEAT_CINDENT
9894 can_cindent = TRUE;
9895#endif
9896 }
9897 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009898 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009899}
9900
9901#ifdef FEAT_DND
9902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009903ins_drop(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904{
9905 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9906}
9907#endif
9908
9909/*
9910 * Handle TAB in Insert or Replace mode.
9911 * Return TRUE when the TAB needs to be inserted like a normal character.
9912 */
9913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009914ins_tab(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009915{
9916 int ind;
9917 int i;
9918 int temp;
9919
9920 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9921 Insstart_blank_vcol = get_nolist_virtcol();
9922 if (echeck_abbr(TAB + ABBR_OFF))
9923 return FALSE;
9924
9925 ind = inindent(0);
9926#ifdef FEAT_CINDENT
9927 if (ind)
9928 can_cindent = FALSE;
9929#endif
9930
9931 /*
9932 * When nothing special, insert TAB like a normal character
9933 */
9934 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009935 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009936 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009937 return TRUE;
9938
9939 if (stop_arrow() == FAIL)
9940 return TRUE;
9941
9942 did_ai = FALSE;
9943#ifdef FEAT_SMARTINDENT
9944 did_si = FALSE;
9945 can_si = FALSE;
9946 can_si_back = FALSE;
9947#endif
9948 AppendToRedobuff((char_u *)"\t");
9949
9950 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009951 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009952 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9953 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 else /* otherwise use 'tabstop' */
9955 temp = (int)curbuf->b_p_ts;
9956 temp -= get_nolist_virtcol() % temp;
9957
9958 /*
9959 * Insert the first space with ins_char(). It will delete one char in
9960 * replace mode. Insert the rest with ins_str(); it will not delete any
9961 * chars. For VREPLACE mode, we use ins_char() for all characters.
9962 */
9963 ins_char(' ');
9964 while (--temp > 0)
9965 {
9966#ifdef FEAT_VREPLACE
9967 if (State & VREPLACE_FLAG)
9968 ins_char(' ');
9969 else
9970#endif
9971 {
9972 ins_str((char_u *)" ");
9973 if (State & REPLACE_FLAG) /* no char replaced */
9974 replace_push(NUL);
9975 }
9976 }
9977
9978 /*
9979 * When 'expandtab' not set: Replace spaces by TABs where possible.
9980 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009981 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 {
9983 char_u *ptr;
9984#ifdef FEAT_VREPLACE
9985 char_u *saved_line = NULL; /* init for GCC */
9986 pos_T pos;
9987#endif
9988 pos_T fpos;
9989 pos_T *cursor;
9990 colnr_T want_vcol, vcol;
9991 int change_col = -1;
9992 int save_list = curwin->w_p_list;
9993
9994 /*
9995 * Get the current line. For VREPLACE mode, don't make real changes
9996 * yet, just work on a copy of the line.
9997 */
9998#ifdef FEAT_VREPLACE
9999 if (State & VREPLACE_FLAG)
10000 {
10001 pos = curwin->w_cursor;
10002 cursor = &pos;
10003 saved_line = vim_strsave(ml_get_curline());
10004 if (saved_line == NULL)
10005 return FALSE;
10006 ptr = saved_line + pos.col;
10007 }
10008 else
10009#endif
10010 {
10011 ptr = ml_get_cursor();
10012 cursor = &curwin->w_cursor;
10013 }
10014
10015 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
10016 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10017 curwin->w_p_list = FALSE;
10018
10019 /* Find first white before the cursor */
10020 fpos = curwin->w_cursor;
10021 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
10022 {
10023 --fpos.col;
10024 --ptr;
10025 }
10026
10027 /* In Replace mode, don't change characters before the insert point. */
10028 if ((State & REPLACE_FLAG)
10029 && fpos.lnum == Insstart.lnum
10030 && fpos.col < Insstart.col)
10031 {
10032 ptr += Insstart.col - fpos.col;
10033 fpos.col = Insstart.col;
10034 }
10035
10036 /* compute virtual column numbers of first white and cursor */
10037 getvcol(curwin, &fpos, &vcol, NULL, NULL);
10038 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
10039
Bram Moolenaar597a4222014-06-25 14:39:50 +020010040 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
10041 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010042 while (vim_iswhite(*ptr))
10043 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010044 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010045 if (vcol + i > want_vcol)
10046 break;
10047 if (*ptr != TAB)
10048 {
10049 *ptr = TAB;
10050 if (change_col < 0)
10051 {
10052 change_col = fpos.col; /* Column of first change */
10053 /* May have to adjust Insstart */
10054 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
10055 Insstart.col = fpos.col;
10056 }
10057 }
10058 ++fpos.col;
10059 ++ptr;
10060 vcol += i;
10061 }
10062
10063 if (change_col >= 0)
10064 {
10065 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010066 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010067
10068 /* Skip over the spaces we need. */
10069 while (vcol < want_vcol && *ptr == ' ')
10070 {
Bram Moolenaar597a4222014-06-25 14:39:50 +020010071 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010072 ++ptr;
10073 ++repl_off;
10074 }
10075 if (vcol > want_vcol)
10076 {
10077 /* Must have a char with 'showbreak' just before it. */
10078 --ptr;
10079 --repl_off;
10080 }
10081 fpos.col += repl_off;
10082
10083 /* Delete following spaces. */
10084 i = cursor->col - fpos.col;
10085 if (i > 0)
10086 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +000010087 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088 /* correct replace stack. */
10089 if ((State & REPLACE_FLAG)
10090#ifdef FEAT_VREPLACE
10091 && !(State & VREPLACE_FLAG)
10092#endif
10093 )
10094 for (temp = i; --temp >= 0; )
10095 replace_join(repl_off);
10096 }
Bram Moolenaar009b2592004-10-24 19:18:58 +000010097#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +020010098 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +000010099 {
Bram Moolenaar67c53842010-05-22 18:28:27 +020010100 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +000010101 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
10102 (char_u *)"\t", 1);
10103 }
10104#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010105 cursor->col -= i;
10106
10107#ifdef FEAT_VREPLACE
10108 /*
10109 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10110 * backspacing over the changed spacing and then inserting the new
10111 * spacing.
10112 */
10113 if (State & VREPLACE_FLAG)
10114 {
10115 /* Backspace from real cursor to change_col */
10116 backspace_until_column(change_col);
10117
10118 /* Insert each char in saved_line from changed_col to
10119 * ptr-cursor */
10120 ins_bytes_len(saved_line + change_col,
10121 cursor->col - change_col);
10122 }
10123#endif
10124 }
10125
10126#ifdef FEAT_VREPLACE
10127 if (State & VREPLACE_FLAG)
10128 vim_free(saved_line);
10129#endif
10130 curwin->w_p_list = save_list;
10131 }
10132
10133 return FALSE;
10134}
10135
10136/*
10137 * Handle CR or NL in insert mode.
10138 * Return TRUE when out of memory or can't undo.
10139 */
10140 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010141ins_eol(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010142{
10143 int i;
10144
10145 if (echeck_abbr(c + ABBR_OFF))
10146 return FALSE;
10147 if (stop_arrow() == FAIL)
10148 return TRUE;
10149 undisplay_dollar();
10150
10151 /*
10152 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10153 * character under the cursor. Only push a NUL on the replace stack,
10154 * nothing to put back when the NL is deleted.
10155 */
10156 if ((State & REPLACE_FLAG)
10157#ifdef FEAT_VREPLACE
10158 && !(State & VREPLACE_FLAG)
10159#endif
10160 )
10161 replace_push(NUL);
10162
10163 /*
10164 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10165 * replacing the next line, so we push all of the characters left on the
10166 * line onto the replace stack. This is not done here though, it is done
10167 * in open_line().
10168 */
10169
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010170#ifdef FEAT_VIRTUALEDIT
10171 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10172 * CTRL-O). */
10173 if (virtual_active() && curwin->w_cursor.coladd > 0)
10174 coladvance(getviscol());
10175#endif
10176
Bram Moolenaar071d4272004-06-13 20:20:40 +000010177#ifdef FEAT_RIGHTLEFT
10178# ifdef FEAT_FKMAP
10179 if (p_altkeymap && p_fkmap)
10180 fkmap(NL);
10181# endif
10182 /* NL in reverse insert will always start in the end of
10183 * current line. */
10184 if (revins_on)
10185 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10186#endif
10187
10188 AppendToRedobuff(NL_STR);
10189 i = open_line(FORWARD,
10190#ifdef FEAT_COMMENTS
10191 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10192#endif
10193 0, old_indent);
10194 old_indent = 0;
10195#ifdef FEAT_CINDENT
10196 can_cindent = TRUE;
10197#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010198#ifdef FEAT_FOLDING
10199 /* When inserting a line the cursor line must never be in a closed fold. */
10200 foldOpenCursor();
10201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010202
10203 return (!i);
10204}
10205
10206#ifdef FEAT_DIGRAPHS
10207/*
10208 * Handle digraph in insert mode.
10209 * Returns character still to be inserted, or NUL when nothing remaining to be
10210 * done.
10211 */
10212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010213ins_digraph(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214{
10215 int c;
10216 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010217 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010218
10219 pc_status = PC_STATUS_UNSET;
10220 if (redrawing() && !char_avail())
10221 {
10222 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010223 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010224
10225 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010226 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010227#ifdef FEAT_CMDL_INFO
10228 add_to_showcmd_c(Ctrl_K);
10229#endif
10230 }
10231
10232#ifdef USE_ON_FLY_SCROLL
10233 dont_scroll = TRUE; /* disallow scrolling here */
10234#endif
10235
10236 /* don't map the digraph chars. This also prevents the
10237 * mode message to be deleted when ESC is hit */
10238 ++no_mapping;
10239 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010240 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010241 --no_mapping;
10242 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010243 if (did_putchar)
10244 /* when the line fits in 'columns' the '?' is at the start of the next
10245 * line and will not be removed by the redraw */
10246 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010247
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 if (IS_SPECIAL(c) || mod_mask) /* special key */
10249 {
10250#ifdef FEAT_CMDL_INFO
10251 clear_showcmd();
10252#endif
10253 insert_special(c, TRUE, FALSE);
10254 return NUL;
10255 }
10256 if (c != ESC)
10257 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010258 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010259 if (redrawing() && !char_avail())
10260 {
10261 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010262 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010263
10264 if (char2cells(c) == 1)
10265 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010266 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010267 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010268 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 }
10270#ifdef FEAT_CMDL_INFO
10271 add_to_showcmd_c(c);
10272#endif
10273 }
10274 ++no_mapping;
10275 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010276 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 --no_mapping;
10278 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010279 if (did_putchar)
10280 /* when the line fits in 'columns' the '?' is at the start of the
10281 * next line and will not be removed by a redraw */
10282 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283 if (cc != ESC)
10284 {
10285 AppendToRedobuff((char_u *)CTRL_V_STR);
10286 c = getdigraph(c, cc, TRUE);
10287#ifdef FEAT_CMDL_INFO
10288 clear_showcmd();
10289#endif
10290 return c;
10291 }
10292 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010293#ifdef FEAT_CMDL_INFO
10294 clear_showcmd();
10295#endif
10296 return NUL;
10297}
10298#endif
10299
10300/*
10301 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10302 * Returns the char to be inserted, or NUL if none found.
10303 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010305ins_copychar(linenr_T lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306{
10307 int c;
10308 int temp;
10309 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010310 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010311
10312 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10313 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010314 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010315 return NUL;
10316 }
10317
10318 /* try to advance to the cursor column */
10319 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010320 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321 prev_ptr = ptr;
10322 validate_virtcol();
10323 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10324 {
10325 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010326 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 }
10328 if ((colnr_T)temp > curwin->w_virtcol)
10329 ptr = prev_ptr;
10330
10331#ifdef FEAT_MBYTE
10332 c = (*mb_ptr2char)(ptr);
10333#else
10334 c = *ptr;
10335#endif
10336 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010337 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010338 return c;
10339}
10340
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010341/*
10342 * CTRL-Y or CTRL-E typed in Insert mode.
10343 */
10344 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010010345ins_ctrl_ey(int tc)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010346{
10347 int c = tc;
10348
10349#ifdef FEAT_INS_EXPAND
10350 if (ctrl_x_mode == CTRL_X_SCROLL)
10351 {
10352 if (c == Ctrl_Y)
10353 scrolldown_clamp();
10354 else
10355 scrollup_clamp();
10356 redraw_later(VALID);
10357 }
10358 else
10359#endif
10360 {
10361 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10362 if (c != NUL)
10363 {
10364 long tw_save;
10365
10366 /* The character must be taken literally, insert like it
10367 * was typed after a CTRL-V, and pretend 'textwidth'
10368 * wasn't set. Digits, 'o' and 'x' are special after a
10369 * CTRL-V, don't use it for these. */
10370 if (c < 256 && !isalnum(c))
10371 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10372 tw_save = curbuf->b_p_tw;
10373 curbuf->b_p_tw = -1;
10374 insert_special(c, TRUE, FALSE);
10375 curbuf->b_p_tw = tw_save;
10376#ifdef FEAT_RIGHTLEFT
10377 revins_chars++;
10378 revins_legal++;
10379#endif
10380 c = Ctrl_V; /* pretend CTRL-V is last character */
10381 auto_format(FALSE, TRUE);
10382 }
10383 }
10384 return c;
10385}
10386
Bram Moolenaar071d4272004-06-13 20:20:40 +000010387#ifdef FEAT_SMARTINDENT
10388/*
10389 * Try to do some very smart auto-indenting.
10390 * Used when inserting a "normal" character.
10391 */
10392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010393ins_try_si(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394{
10395 pos_T *pos, old_pos;
10396 char_u *ptr;
10397 int i;
10398 int temp;
10399
10400 /*
10401 * do some very smart indenting when entering '{' or '}'
10402 */
10403 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10404 {
10405 /*
10406 * for '}' set indent equal to indent of line containing matching '{'
10407 */
10408 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10409 {
10410 old_pos = curwin->w_cursor;
10411 /*
10412 * If the matching '{' has a ')' immediately before it (ignoring
10413 * white-space), then line up with the start of the line
10414 * containing the matching '(' if there is one. This handles the
10415 * case where an "if (..\n..) {" statement continues over multiple
10416 * lines -- webb
10417 */
10418 ptr = ml_get(pos->lnum);
10419 i = pos->col;
10420 if (i > 0) /* skip blanks before '{' */
10421 while (--i > 0 && vim_iswhite(ptr[i]))
10422 ;
10423 curwin->w_cursor.lnum = pos->lnum;
10424 curwin->w_cursor.col = i;
10425 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10426 curwin->w_cursor = *pos;
10427 i = get_indent();
10428 curwin->w_cursor = old_pos;
10429#ifdef FEAT_VREPLACE
10430 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010431 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010432 else
10433#endif
10434 (void)set_indent(i, SIN_CHANGED);
10435 }
10436 else if (curwin->w_cursor.col > 0)
10437 {
10438 /*
10439 * when inserting '{' after "O" reduce indent, but not
10440 * more than indent of previous line
10441 */
10442 temp = TRUE;
10443 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10444 {
10445 old_pos = curwin->w_cursor;
10446 i = get_indent();
10447 while (curwin->w_cursor.lnum > 1)
10448 {
10449 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10450
10451 /* ignore empty lines and lines starting with '#'. */
10452 if (*ptr != '#' && *ptr != NUL)
10453 break;
10454 }
10455 if (get_indent() >= i)
10456 temp = FALSE;
10457 curwin->w_cursor = old_pos;
10458 }
10459 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010460 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010461 }
10462 }
10463
10464 /*
10465 * set indent of '#' always to 0
10466 */
10467 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10468 {
10469 /* remember current indent for next line */
10470 old_indent = get_indent();
10471 (void)set_indent(0, SIN_CHANGED);
10472 }
10473
10474 /* Adjust ai_col, the char at this position can be deleted. */
10475 if (ai_col > curwin->w_cursor.col)
10476 ai_col = curwin->w_cursor.col;
10477}
10478#endif
10479
10480/*
10481 * Get the value that w_virtcol would have when 'list' is off.
10482 * Unless 'cpo' contains the 'L' flag.
10483 */
10484 static colnr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010010485get_nolist_virtcol(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486{
10487 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10488 return getvcol_nolist(&curwin->w_cursor);
10489 validate_virtcol();
10490 return curwin->w_virtcol;
10491}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010492
10493#ifdef FEAT_AUTOCMD
10494/*
10495 * Handle the InsertCharPre autocommand.
10496 * "c" is the character that was typed.
10497 * Return a pointer to allocated memory with the replacement string.
10498 * Return NULL to continue inserting "c".
10499 */
10500 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010010501do_insert_char_pre(int c)
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010502{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010503 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010504 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010505
10506 /* Return quickly when there is nothing to do. */
10507 if (!has_insertcharpre())
10508 return NULL;
10509
Bram Moolenaar704984a2012-06-01 14:57:51 +020010510#ifdef FEAT_MBYTE
10511 if (has_mbyte)
10512 buf[(*mb_char2bytes)(c, buf)] = NUL;
10513 else
10514#endif
10515 {
10516 buf[0] = c;
10517 buf[1] = NUL;
10518 }
10519
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010520 /* Lock the text to avoid weird things from happening. */
10521 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010522 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010523
Bram Moolenaar704984a2012-06-01 14:57:51 +020010524 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010525 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010526 {
10527 /* Get the value of v:char. It may be empty or more than one
10528 * character. Only use it when changed, otherwise continue with the
10529 * original character to avoid breaking autoindent. */
10530 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10531 res = vim_strsave(get_vim_var_str(VV_CHAR));
10532 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010533
10534 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10535 --textlock;
10536
10537 return res;
10538}
10539#endif