blob: b48a2176f7bb9919de3494c7c3983385952030e5 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaare4214502015-03-05 18:08:43 +010037#define CTRL_X_EVAL 16 /* for builtin function complete() */
Bram Moolenaar071d4272004-06-13 20:20:40 +000038
Bram Moolenaar071d4272004-06-13 20:20:40 +000039#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
Bram Moolenaare4214502015-03-05 18:08:43 +010040#define CTRL_X_MODE_LINE_OR_EVAL(m) (m == CTRL_X_WHOLE_LINE || m == CTRL_X_EVAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
42static char *ctrl_x_msgs[] =
43{
44 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000045 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000046 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000047 N_(" Whole line completion (^L^N^P)"),
48 N_(" File name completion (^F^N^P)"),
49 N_(" Tag completion (^]^N^P)"),
50 N_(" Path pattern completion (^N^P)"),
51 N_(" Definition completion (^D^N^P)"),
52 NULL,
53 N_(" Dictionary completion (^K^N^P)"),
54 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000055 N_(" Command-line completion (^V^N^P)"),
56 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000057 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000058 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000059 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaare4214502015-03-05 18:08:43 +010060 NULL, /* CTRL_X_EVAL doesn't use msg. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000061};
62
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000063static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010064#ifdef FEAT_COMPL_FUNC
65static char e_complwin[] = N_("E839: Completion function changed window");
66static char e_compldel[] = N_("E840: Completion function deleted text");
67#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000068
69/*
70 * Structure used to store one match for insert completion.
71 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000072typedef struct compl_S compl_T;
73struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000074{
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 compl_T *cp_next;
76 compl_T *cp_prev;
77 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000078 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000079 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000080 char_u *cp_fname; /* file containing the match, allocated when
81 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000082 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
83 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084};
85
Bram Moolenaar572cb562005-08-05 21:35:02 +000086#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000087#define FREE_FNAME (2)
88
89/*
90 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000091 * "compl_first_match" points to the start of the list.
92 * "compl_curr_match" points to the currently selected entry.
93 * "compl_shown_match" is different from compl_curr_match during
94 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000095 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000096static compl_T *compl_first_match = NULL;
97static compl_T *compl_curr_match = NULL;
98static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000099
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000100/* After using a cursor key <Enter> selects a match in the popup menu,
101 * otherwise it inserts a line break. */
102static int compl_enter_selects = FALSE;
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104/* When "compl_leader" is not NULL only matches that start with this string
105 * are used. */
106static char_u *compl_leader = NULL;
107
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000108static int compl_get_longest = FALSE; /* put longest common string
109 in compl_leader */
110
Bram Moolenaarb6be1e22015-07-10 18:18:40 +0200111static int compl_no_insert = FALSE; /* FALSE: select & insert
112 TRUE: noinsert */
113static int compl_no_select = FALSE; /* FALSE: select & insert
114 TRUE: noselect */
115
Bram Moolenaara6557602006-02-04 22:43:20 +0000116static int compl_used_match; /* Selected one of the matches. When
117 FALSE the match was edited or using
118 the longest common string. */
119
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000120static int compl_was_interrupted = FALSE; /* didn't finish finding
121 completions. */
122
123static int compl_restarting = FALSE; /* don't insert match */
124
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000125/* When the first completion is done "compl_started" is set. When it's
126 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000127static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000128
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000129/* Set when doing something for completion that may call edit() recursively,
130 * which is not allowed. */
131static int compl_busy = FALSE;
132
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static int compl_matches = 0;
134static char_u *compl_pattern = NULL;
135static int compl_direction = FORWARD;
136static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000137static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000138static pos_T compl_startpos;
139static colnr_T compl_col = 0; /* column where the text starts
140 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000141static char_u *compl_orig_text = NULL; /* text as it was before
142 * completion started */
143static int compl_cont_mode = 0;
144static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145
Bram Moolenaar82139082011-09-14 16:52:09 +0200146static int compl_opt_refresh_always = FALSE;
147
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000148static void ins_ctrl_x __ARGS((void));
149static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000150static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000151static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000152static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000153static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000154static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000156static void ins_compl_upd_pum __ARGS((void));
157static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000158static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000159static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000160static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000161static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000162static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static void ins_compl_free __ARGS((void));
164static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000165static int ins_compl_bs __ARGS((void));
Bram Moolenaar82139082011-09-14 16:52:09 +0200166static int ins_compl_need_restart __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000167static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000168static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar82139082011-09-14 16:52:09 +0200169static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000170static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000171static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000172static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000173static int ins_compl_prep __ARGS((int c));
Bram Moolenaar62951b12011-09-21 18:23:05 +0200174static void ins_compl_fixRedoBufForLeader __ARGS((char_u *ptr_arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000176#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
177static void ins_compl_add_list __ARGS((list_T *list));
Bram Moolenaar82139082011-09-14 16:52:09 +0200178static void ins_compl_add_dict __ARGS((dict_T *dict));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000179#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000180static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181static void ins_compl_delete __ARGS((void));
182static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000183static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000184static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000185static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000186static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000187static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000188static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000189static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190#endif /* FEAT_INS_EXPAND */
191
192#define BACKSPACE_CHAR 1
193#define BACKSPACE_WORD 2
194#define BACKSPACE_WORD_NOT_SPACE 3
195#define BACKSPACE_LINE 4
196
Bram Moolenaar754b5602006-02-09 23:53:20 +0000197static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198static void ins_ctrl_v __ARGS((void));
199static void undisplay_dollar __ARGS((void));
200static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000201static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202static void check_auto_format __ARGS((int));
203static void redo_literal __ARGS((int c));
204static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000205#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000206static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000207static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000208static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000209#endif
Bram Moolenaarf332a652013-11-04 04:20:33 +0100210static void stop_insert __ARGS((pos_T *end_insert_pos, int esc, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000211static int echeck_abbr __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212static int replace_pop __ARGS((void));
213static void replace_join __ARGS((int off));
214static void replace_pop_ins __ARGS((void));
215#ifdef FEAT_MBYTE
216static void mb_replace_pop_ins __ARGS((int cc));
217#endif
218static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000219static void replace_do_bs __ARGS((int limit_col));
220static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000221#ifdef FEAT_CINDENT
222static int cindent_on __ARGS((void));
223#endif
224static void ins_reg __ARGS((void));
225static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000226static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000227static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#ifdef FEAT_RIGHTLEFT
229static void ins_ctrl_ __ARGS((void));
230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231static int ins_start_select __ARGS((int c));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000232static void ins_insert __ARGS((int replaceState));
233static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234static void ins_shift __ARGS((int c, int lastc));
235static void ins_del __ARGS((void));
236static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
237#ifdef FEAT_MOUSE
238static void ins_mouse __ARGS((int c));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200239static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000240#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000241#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
242static void ins_tabline __ARGS((int c));
243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000244static void ins_left __ARGS((void));
245static void ins_home __ARGS((int c));
246static void ins_end __ARGS((int c));
247static void ins_s_left __ARGS((void));
248static void ins_right __ARGS((void));
249static void ins_s_right __ARGS((void));
250static void ins_up __ARGS((int startcol));
251static void ins_pageup __ARGS((void));
252static void ins_down __ARGS((int startcol));
253static void ins_pagedown __ARGS((void));
254#ifdef FEAT_DND
255static void ins_drop __ARGS((void));
256#endif
257static int ins_tab __ARGS((void));
258static int ins_eol __ARGS((int c));
259#ifdef FEAT_DIGRAPHS
260static int ins_digraph __ARGS((void));
261#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000262static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000263#ifdef FEAT_SMARTINDENT
264static void ins_try_si __ARGS((int c));
265#endif
266static colnr_T get_nolist_virtcol __ARGS((void));
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100267#ifdef FEAT_AUTOCMD
268static char_u *do_insert_char_pre __ARGS((int c));
269#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000270
271static colnr_T Insstart_textlen; /* length of line when insert started */
272static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100273static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275static char_u *last_insert = NULL; /* the text of the previous insert,
276 K_SPECIAL and CSI are escaped */
277static int last_insert_skip; /* nr of chars in front of previous insert */
278static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000279static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280
281#ifdef FEAT_CINDENT
282static int can_cindent; /* may do cindenting on this line */
283#endif
284
285static int old_indent = 0; /* for ^^D command in insert mode */
286
287#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000288static int revins_on; /* reverse insert mode on */
289static int revins_chars; /* how much to skip after edit */
290static int revins_legal; /* was the last char 'legal'? */
291static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000292#endif
293
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294static int ins_need_undo; /* call u_save() before inserting a
295 char. Set when edit() is called.
296 after that arrow_used is used. */
297
298static int did_add_space = FALSE; /* auto_format() added an extra space
299 under the cursor */
300
301/*
302 * edit(): Start inserting text.
303 *
304 * "cmdchar" can be:
305 * 'i' normal insert command
306 * 'a' normal append command
307 * 'R' replace command
308 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
309 * but still only one <CR> is inserted. The <Esc> is not used for redo.
310 * 'g' "gI" command.
311 * 'V' "gR" command for Virtual Replace mode.
312 * 'v' "gr" command for single character Virtual Replace mode.
313 *
314 * This function is not called recursively. For CTRL-O commands, it returns
315 * and lets the caller handle the Normal-mode command.
316 *
317 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
318 */
319 int
320edit(cmdchar, startln, count)
321 int cmdchar;
322 int startln; /* if set, insert at start of line */
323 long count;
324{
325 int c = 0;
326 char_u *ptr;
327 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000328 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000329 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000330 int i;
331 int did_backspace = TRUE; /* previous char was backspace */
332#ifdef FEAT_CINDENT
333 int line_is_white = FALSE; /* line is empty before insert */
334#endif
335 linenr_T old_topline = 0; /* topline before insertion */
336#ifdef FEAT_DIFF
337 int old_topfill = -1;
338#endif
339 int inserted_space = FALSE; /* just inserted a space */
340 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000341 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000342
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000343 /* Remember whether editing was restarted after CTRL-O. */
344 did_restart_edit = restart_edit;
345
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346 /* sleep before redrawing, needed for "CTRL-O :" that results in an
347 * error message */
348 check_for_delay(TRUE);
349
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100350 /* set Insstart_orig to Insstart */
351 update_Insstart_orig = TRUE;
352
Bram Moolenaar071d4272004-06-13 20:20:40 +0000353#ifdef HAVE_SANDBOX
354 /* Don't allow inserting in the sandbox. */
355 if (sandbox != 0)
356 {
357 EMSG(_(e_sandbox));
358 return FALSE;
359 }
360#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000361 /* Don't allow changes in the buffer while editing the cmdline. The
362 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000363 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000364 {
365 EMSG(_(e_secure));
366 return FALSE;
367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368
369#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000370 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000371 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000372 {
373 EMSG(_(e_secure));
374 return FALSE;
375 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376 ins_compl_clear(); /* clear stuff for CTRL-X mode */
377#endif
378
Bram Moolenaar843ee412004-06-30 16:16:41 +0000379#ifdef FEAT_AUTOCMD
380 /*
381 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
382 */
383 if (cmdchar != 'r' && cmdchar != 'v')
384 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100385 pos_T save_cursor = curwin->w_cursor;
386
Bram Moolenaar1e015462005-09-25 22:16:38 +0000387# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000388 if (cmdchar == 'R')
389 ptr = (char_u *)"r";
390 else if (cmdchar == 'V')
391 ptr = (char_u *)"v";
392 else
393 ptr = (char_u *)"i";
394 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200395 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000396# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000397 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100398
Bram Moolenaar097c9922013-05-19 21:15:15 +0200399 /* Make sure the cursor didn't move. Do call check_cursor_col() in
400 * case the text was modified. Since Insert mode was not started yet
401 * a call to check_cursor_col() may move the cursor, especially with
402 * the "A" command, thus set State to avoid that. Also check that the
403 * line number is still valid (lines may have been deleted).
404 * Do not restore if v:char was set to a non-empty string. */
405 if (!equalpos(curwin->w_cursor, save_cursor)
406# ifdef FEAT_EVAL
407 && *get_vim_var_str(VV_CHAR) == NUL
408# endif
409 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100410 {
411 int save_state = State;
412
413 curwin->w_cursor = save_cursor;
414 State = INSERT;
415 check_cursor_col();
416 State = save_state;
417 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000418 }
419#endif
420
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200421#ifdef FEAT_CONCEAL
422 /* Check if the cursor line needs redrawing before changing State. If
423 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200424 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200425#endif
426
Bram Moolenaar071d4272004-06-13 20:20:40 +0000427#ifdef FEAT_MOUSE
428 /*
429 * When doing a paste with the middle mouse button, Insstart is set to
430 * where the paste started.
431 */
432 if (where_paste_started.lnum != 0)
433 Insstart = where_paste_started;
434 else
435#endif
436 {
437 Insstart = curwin->w_cursor;
438 if (startln)
439 Insstart.col = 0;
440 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000441 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000442 Insstart_blank_vcol = MAXCOL;
443 if (!did_ai)
444 ai_col = 0;
445
446 if (cmdchar != NUL && restart_edit == 0)
447 {
448 ResetRedobuff();
449 AppendNumberToRedobuff(count);
450#ifdef FEAT_VREPLACE
451 if (cmdchar == 'V' || cmdchar == 'v')
452 {
453 /* "gR" or "gr" command */
454 AppendCharToRedobuff('g');
455 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
456 }
457 else
458#endif
459 {
460 AppendCharToRedobuff(cmdchar);
461 if (cmdchar == 'g') /* "gI" command */
462 AppendCharToRedobuff('I');
463 else if (cmdchar == 'r') /* "r<CR>" command */
464 count = 1; /* insert only one <CR> */
465 }
466 }
467
468 if (cmdchar == 'R')
469 {
470#ifdef FEAT_FKMAP
471 if (p_fkmap && p_ri)
472 {
473 beep_flush();
474 EMSG(farsi_text_3); /* encoded in Farsi */
475 State = INSERT;
476 }
477 else
478#endif
479 State = REPLACE;
480 }
481#ifdef FEAT_VREPLACE
482 else if (cmdchar == 'V' || cmdchar == 'v')
483 {
484 State = VREPLACE;
485 replaceState = VREPLACE;
486 orig_line_count = curbuf->b_ml.ml_line_count;
487 vr_lines_changed = 1;
488 }
489#endif
490 else
491 State = INSERT;
492
493 stop_insert_mode = FALSE;
494
495 /*
496 * Need to recompute the cursor position, it might move when the cursor is
497 * on a TAB or special character.
498 */
499 curs_columns(TRUE);
500
501 /*
502 * Enable langmap or IME, indicated by 'iminsert'.
503 * Note that IME may enabled/disabled without us noticing here, thus the
504 * 'iminsert' value may not reflect what is actually used. It is updated
505 * when hitting <Esc>.
506 */
507 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
508 State |= LANGMAP;
509#ifdef USE_IM_CONTROL
510 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
511#endif
512
Bram Moolenaar071d4272004-06-13 20:20:40 +0000513#ifdef FEAT_MOUSE
514 setmouse();
515#endif
516#ifdef FEAT_CMDL_INFO
517 clear_showcmd();
518#endif
519#ifdef FEAT_RIGHTLEFT
520 /* there is no reverse replace mode */
521 revins_on = (State == INSERT && p_ri);
522 if (revins_on)
523 undisplay_dollar();
524 revins_chars = 0;
525 revins_legal = 0;
526 revins_scol = -1;
527#endif
528
529 /*
530 * Handle restarting Insert mode.
531 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
532 * restart_edit non-zero, and something in the stuff buffer.
533 */
534 if (restart_edit != 0 && stuff_empty())
535 {
536#ifdef FEAT_MOUSE
537 /*
538 * After a paste we consider text typed to be part of the insert for
539 * the pasted text. You can backspace over the pasted text too.
540 */
541 if (where_paste_started.lnum)
542 arrow_used = FALSE;
543 else
544#endif
545 arrow_used = TRUE;
546 restart_edit = 0;
547
548 /*
549 * If the cursor was after the end-of-line before the CTRL-O and it is
550 * now at the end-of-line, put it after the end-of-line (this is not
551 * correct in very rare cases).
552 * Also do this if curswant is greater than the current virtual
553 * column. Eg after "^O$" or "^O80|".
554 */
555 validate_virtcol();
556 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000557 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000558 || curwin->w_curswant > curwin->w_virtcol)
559 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
560 {
561 if (ptr[1] == NUL)
562 ++curwin->w_cursor.col;
563#ifdef FEAT_MBYTE
564 else if (has_mbyte)
565 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000566 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 if (ptr[i] == NUL)
568 curwin->w_cursor.col += i;
569 }
570#endif
571 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000572 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000573 }
574 else
575 arrow_used = FALSE;
576
577 /* we are in insert mode now, don't need to start it anymore */
578 need_start_insertmode = FALSE;
579
580 /* Need to save the line for undo before inserting the first char. */
581 ins_need_undo = TRUE;
582
583#ifdef FEAT_MOUSE
584 where_paste_started.lnum = 0;
585#endif
586#ifdef FEAT_CINDENT
587 can_cindent = TRUE;
588#endif
589#ifdef FEAT_FOLDING
590 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
591 * restarting. */
592 if (!p_im && did_restart_edit == 0)
593 foldOpenCursor();
594#endif
595
596 /*
597 * If 'showmode' is set, show the current (insert/replace/..) mode.
598 * A warning message for changing a readonly file is given here, before
599 * actually changing anything. It's put after the mode, if any.
600 */
601 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000602 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603 i = showmode();
604
605 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000606 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607
608#ifdef CURSOR_SHAPE
609 ui_cursor_shape(); /* may show different cursor shape */
610#endif
611#ifdef FEAT_DIGRAPHS
612 do_digraph(-1); /* clear digraphs */
613#endif
614
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000615 /*
616 * Get the current length of the redo buffer, those characters have to be
617 * skipped if we want to get to the inserted characters.
618 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000619 ptr = get_inserted();
620 if (ptr == NULL)
621 new_insert_skip = 0;
622 else
623 {
624 new_insert_skip = (int)STRLEN(ptr);
625 vim_free(ptr);
626 }
627
628 old_indent = 0;
629
630 /*
631 * Main loop in Insert mode: repeat until Insert mode is left.
632 */
633 for (;;)
634 {
635#ifdef FEAT_RIGHTLEFT
636 if (!revins_legal)
637 revins_scol = -1; /* reset on illegal motions */
638 else
639 revins_legal = 0;
640#endif
641 if (arrow_used) /* don't repeat insert when arrow key used */
642 count = 0;
643
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100644 if (update_Insstart_orig)
645 Insstart_orig = Insstart;
646
Bram Moolenaar071d4272004-06-13 20:20:40 +0000647 if (stop_insert_mode)
648 {
649 /* ":stopinsert" used or 'insertmode' reset */
650 count = 0;
651 goto doESCkey;
652 }
653
654 /* set curwin->w_curswant for next K_DOWN or K_UP */
655 if (!arrow_used)
656 curwin->w_set_curswant = TRUE;
657
658 /* If there is no typeahead may check for timestamps (e.g., for when a
659 * menu invoked a shell command). */
660 if (stuff_empty())
661 {
662 did_check_timestamps = FALSE;
663 if (need_check_timestamps)
664 check_timestamps(FALSE);
665 }
666
667 /*
668 * When emsg() was called msg_scroll will have been set.
669 */
670 msg_scroll = FALSE;
671
672#ifdef FEAT_GUI
673 /* When 'mousefocus' is set a mouse movement may have taken us to
674 * another window. "need_mouse_correct" may then be set because of an
675 * autocommand. */
676 if (need_mouse_correct)
677 gui_mouse_correct();
678#endif
679
680#ifdef FEAT_FOLDING
681 /* Open fold at the cursor line, according to 'foldopen'. */
682 if (fdo_flags & FDO_INSERT)
683 foldOpenCursor();
684 /* Close folds where the cursor isn't, according to 'foldclose' */
685 if (!char_avail())
686 foldCheckClose();
687#endif
688
689 /*
690 * If we inserted a character at the last position of the last line in
691 * the window, scroll the window one line up. This avoids an extra
692 * redraw.
693 * This is detected when the cursor column is smaller after inserting
694 * something.
695 * Don't do this when the topline changed already, it has
696 * already been adjusted (by insertchar() calling open_line())).
697 */
698 if (curbuf->b_mod_set
699 && curwin->w_p_wrap
700 && !did_backspace
701 && curwin->w_topline == old_topline
702#ifdef FEAT_DIFF
703 && curwin->w_topfill == old_topfill
704#endif
705 )
706 {
707 mincol = curwin->w_wcol;
708 validate_cursor_col();
709
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000710 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 && curwin->w_wrow == W_WINROW(curwin)
712 + curwin->w_height - 1 - p_so
713 && (curwin->w_cursor.lnum != curwin->w_topline
714#ifdef FEAT_DIFF
715 || curwin->w_topfill > 0
716#endif
717 ))
718 {
719#ifdef FEAT_DIFF
720 if (curwin->w_topfill > 0)
721 --curwin->w_topfill;
722 else
723#endif
724#ifdef FEAT_FOLDING
725 if (hasFolding(curwin->w_topline, NULL, &old_topline))
726 set_topline(curwin, old_topline + 1);
727 else
728#endif
729 set_topline(curwin, curwin->w_topline + 1);
730 }
731 }
732
733 /* May need to adjust w_topline to show the cursor. */
734 update_topline();
735
736 did_backspace = FALSE;
737
738 validate_cursor(); /* may set must_redraw */
739
740 /*
741 * Redraw the display when no characters are waiting.
742 * Also shows mode, ruler and positions cursor.
743 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000744 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000745
746#ifdef FEAT_SCROLLBIND
747 if (curwin->w_p_scb)
748 do_check_scrollbind(TRUE);
749#endif
750
Bram Moolenaar860cae12010-06-05 23:22:07 +0200751#ifdef FEAT_CURSORBIND
752 if (curwin->w_p_crb)
753 do_check_cursorbind();
754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 update_curswant();
756 old_topline = curwin->w_topline;
757#ifdef FEAT_DIFF
758 old_topfill = curwin->w_topfill;
759#endif
760
761#ifdef USE_ON_FLY_SCROLL
762 dont_scroll = FALSE; /* allow scrolling here */
763#endif
764
765 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000766 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100768 if (c != K_CURSORHOLD)
769 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000770 do
771 {
772 c = safe_vgetc();
773 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000774
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000775#ifdef FEAT_AUTOCMD
776 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
777 did_cursorhold = TRUE;
778#endif
779
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780#ifdef FEAT_RIGHTLEFT
781 if (p_hkmap && KeyTyped)
782 c = hkmap(c); /* Hebrew mode mapping */
783#endif
784#ifdef FEAT_FKMAP
785 if (p_fkmap && KeyTyped)
786 c = fkmap(c); /* Farsi mode mapping */
787#endif
788
789#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000790 /*
791 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000792 * and the cursor is still in the completed word. Only when there is
793 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000794 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000795 if (compl_started
796 && pum_wanted()
797 && curwin->w_cursor.col >= compl_col
798 && (compl_shown_match == NULL
799 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000800 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000801 /* BS: Delete one character from "compl_leader". */
802 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000803 && curwin->w_cursor.col > compl_col
804 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000805 continue;
806
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000807 /* When no match was selected or it was edited. */
808 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000809 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000810 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000811 * "compl_leader". Except when at the original match and
812 * there is nothing to add, CTRL-L works like CTRL-P then. */
813 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100814 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000815 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000816 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000817 {
818 ins_compl_addfrommatch();
819 continue;
820 }
821
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000822 /* A non-white character that fits in with the current
823 * completion: Add to "compl_leader". */
824 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000825 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100826#ifdef FEAT_AUTOCMD
827 /* Trigger InsertCharPre. */
828 char_u *str = do_insert_char_pre(c);
829 char_u *p;
830
831 if (str != NULL)
832 {
833 for (p = str; *p != NUL; mb_ptr_adv(p))
834 ins_compl_addleader(PTR2CHAR(p));
835 vim_free(str);
836 }
837 else
838#endif
839 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000840 continue;
841 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000842
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000843 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000844 * compl_enter_selects is set the Enter key does the same. */
845 if (c == Ctrl_Y || (compl_enter_selects
846 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000847 {
848 ins_compl_delete();
849 ins_compl_insert();
850 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000851 }
852 }
853
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
855 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000856 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000857 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000858 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859#endif
860
Bram Moolenaar488c6512005-08-11 20:09:58 +0000861 /* CTRL-\ CTRL-N goes to Normal mode,
862 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
863 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 if (c == Ctrl_BSL)
865 {
866 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000867 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000868 ++no_mapping;
869 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000870 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000871 --no_mapping;
872 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000873 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000875 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000876 vungetc(c);
877 c = Ctrl_BSL;
878 }
879 else if (c == Ctrl_G && p_im)
880 continue;
881 else
882 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000883 if (c == Ctrl_O)
884 {
885 ins_ctrl_o();
886 ins_at_eol = FALSE; /* cursor keeps its column */
887 nomove = TRUE;
888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 count = 0;
890 goto doESCkey;
891 }
892 }
893
894#ifdef FEAT_DIGRAPHS
895 c = do_digraph(c);
896#endif
897
898#ifdef FEAT_INS_EXPAND
899 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
900 goto docomplete;
901#endif
902 if (c == Ctrl_V || c == Ctrl_Q)
903 {
904 ins_ctrl_v();
905 c = Ctrl_V; /* pretend CTRL-V is last typed character */
906 continue;
907 }
908
909#ifdef FEAT_CINDENT
910 if (cindent_on()
911# ifdef FEAT_INS_EXPAND
912 && ctrl_x_mode == 0
913# endif
914 )
915 {
916 /* A key name preceded by a bang means this key is not to be
917 * inserted. Skip ahead to the re-indenting below.
918 * A key name preceded by a star means that indenting has to be
919 * done before inserting the key. */
920 line_is_white = inindent(0);
921 if (in_cinkeys(c, '!', line_is_white))
922 goto force_cindent;
923 if (can_cindent && in_cinkeys(c, '*', line_is_white)
924 && stop_arrow() == OK)
925 do_c_expr_indent();
926 }
927#endif
928
929#ifdef FEAT_RIGHTLEFT
930 if (curwin->w_p_rl)
931 switch (c)
932 {
933 case K_LEFT: c = K_RIGHT; break;
934 case K_S_LEFT: c = K_S_RIGHT; break;
935 case K_C_LEFT: c = K_C_RIGHT; break;
936 case K_RIGHT: c = K_LEFT; break;
937 case K_S_RIGHT: c = K_S_LEFT; break;
938 case K_C_RIGHT: c = K_C_LEFT; break;
939 }
940#endif
941
Bram Moolenaar071d4272004-06-13 20:20:40 +0000942 /*
943 * If 'keymodel' contains "startsel", may start selection. If it
944 * does, a CTRL-O and c will be stuffed, we need to get these
945 * characters.
946 */
947 if (ins_start_select(c))
948 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949
950 /*
951 * The big switch to handle a character in insert mode.
952 */
953 switch (c)
954 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 if (echeck_abbr(ESC + ABBR_OFF))
957 break;
958 /*FALLTHROUGH*/
959
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000960 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961#ifdef FEAT_CMDWIN
962 if (c == Ctrl_C && cmdwin_type != 0)
963 {
964 /* Close the cmdline window. */
965 cmdwin_result = K_IGNORE;
966 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000967 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000968 goto doESCkey;
969 }
970#endif
971
972#ifdef UNIX
973do_intr:
974#endif
975 /* when 'insertmode' set, and not halfway a mapping, don't leave
976 * Insert mode */
977 if (goto_im())
978 {
979 if (got_int)
980 {
981 (void)vgetc(); /* flush all buffers */
982 got_int = FALSE;
983 }
984 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200985 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 break;
987 }
988doESCkey:
989 /*
990 * This is the ONLY return from edit()!
991 */
992 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
993 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000994 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 o_lnum = curwin->w_cursor.lnum;
996
Bram Moolenaar488c6512005-08-11 20:09:58 +0000997 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000998 {
999#ifdef FEAT_AUTOCMD
1000 if (cmdchar != 'r' && cmdchar != 'v')
1001 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1002 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001003 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001007 continue;
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009 case Ctrl_Z: /* suspend when 'insertmode' set */
1010 if (!p_im)
1011 goto normalchar; /* insert CTRL-Z as normal char */
1012 stuffReadbuff((char_u *)":st\r");
1013 c = Ctrl_O;
1014 /*FALLTHROUGH*/
1015
1016 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001017#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001018 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001019 goto docomplete;
1020#endif
1021 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1022 break;
1023 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001024
1025#ifdef FEAT_VIRTUALEDIT
1026 /* don't move the cursor left when 'virtualedit' has "onemore". */
1027 if (ve_flags & VE_ONEMORE)
1028 {
1029 ins_at_eol = FALSE;
1030 nomove = TRUE;
1031 }
1032#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 count = 0;
1034 goto doESCkey;
1035
Bram Moolenaar572cb562005-08-05 21:35:02 +00001036 case K_INS: /* toggle insert/replace mode */
1037 case K_KINS:
1038 ins_insert(replaceState);
1039 break;
1040
1041 case K_SELECT: /* end of Select mode mapping - ignore */
1042 break;
1043
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044#ifdef FEAT_SNIFF
1045 case K_SNIFF: /* Sniff command received */
1046 stuffcharReadbuff(K_SNIFF);
1047 goto doESCkey;
1048#endif
1049
1050 case K_HELP: /* Help key works like <ESC> <Help> */
1051 case K_F1:
1052 case K_XF1:
1053 stuffcharReadbuff(K_HELP);
1054 if (p_im)
1055 need_start_insertmode = TRUE;
1056 goto doESCkey;
1057
1058#ifdef FEAT_NETBEANS_INTG
1059 case K_F21: /* NetBeans command */
1060 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001061 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001062 --no_mapping;
1063 netbeans_keycommand(i);
1064 break;
1065#endif
1066
1067 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 case NUL:
1069 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001070 /* For ^@ the trailing ESC will end the insert, unless there is an
1071 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1073 && c != Ctrl_A && !p_im)
1074 goto doESCkey; /* quit insert mode */
1075 inserted_space = FALSE;
1076 break;
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 ins_reg();
1080 auto_format(FALSE, TRUE);
1081 inserted_space = FALSE;
1082 break;
1083
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001084 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 ins_ctrl_g();
1086 break;
1087
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case Ctrl_HAT: /* switch input mode and/or langmap */
1089 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090 break;
1091
1092#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 if (!p_ari)
1095 goto normalchar;
1096 ins_ctrl_();
1097 break;
1098#endif
1099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1102 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1103 goto docomplete;
1104#endif
1105 /* FALLTHROUGH */
1106
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001107 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108# ifdef FEAT_INS_EXPAND
1109 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1110 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001111 if (has_compl_option(FALSE))
1112 goto docomplete;
1113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 }
1115# endif
1116 ins_shift(c, lastc);
1117 auto_format(FALSE, TRUE);
1118 inserted_space = FALSE;
1119 break;
1120
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001121 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 case K_KDEL:
1123 ins_del();
1124 auto_format(FALSE, TRUE);
1125 break;
1126
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001127 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 case Ctrl_H:
1129 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1130 auto_format(FALSE, TRUE);
1131 break;
1132
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1135 auto_format(FALSE, TRUE);
1136 break;
1137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001139# ifdef FEAT_COMPL_FUNC
1140 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001142 goto docomplete;
1143# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1145 auto_format(FALSE, TRUE);
1146 inserted_space = FALSE;
1147 break;
1148
1149#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001150 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001151 case K_LEFTMOUSE_NM:
1152 case K_LEFTDRAG:
1153 case K_LEFTRELEASE:
1154 case K_LEFTRELEASE_NM:
1155 case K_MIDDLEMOUSE:
1156 case K_MIDDLEDRAG:
1157 case K_MIDDLERELEASE:
1158 case K_RIGHTMOUSE:
1159 case K_RIGHTDRAG:
1160 case K_RIGHTRELEASE:
1161 case K_X1MOUSE:
1162 case K_X1DRAG:
1163 case K_X1RELEASE:
1164 case K_X2MOUSE:
1165 case K_X2DRAG:
1166 case K_X2RELEASE:
1167 ins_mouse(c);
1168 break;
1169
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001170 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001171 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001175 ins_mousescroll(MSCR_UP);
1176 break;
1177
1178 case K_MOUSELEFT: /* Scroll wheel left */
1179 ins_mousescroll(MSCR_LEFT);
1180 break;
1181
1182 case K_MOUSERIGHT: /* Scroll wheel right */
1183 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001184 break;
1185#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001186#ifdef FEAT_GUI_TABLINE
1187 case K_TABLINE:
1188 case K_TABMENU:
1189 ins_tabline(c);
1190 break;
1191#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001193 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 break;
1195
Bram Moolenaar754b5602006-02-09 23:53:20 +00001196#ifdef FEAT_AUTOCMD
1197 case K_CURSORHOLD: /* Didn't type something for a while. */
1198 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1199 did_cursorhold = TRUE;
1200 break;
1201#endif
1202
Bram Moolenaar4770d092006-01-12 23:22:24 +00001203#ifdef FEAT_GUI_W32
1204 /* On Win32 ignore <M-F4>, we get it when closing the window was
1205 * cancelled. */
1206 case K_F4:
1207 if (mod_mask != MOD_MASK_ALT)
1208 goto normalchar;
1209 break;
1210#endif
1211
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212#ifdef FEAT_GUI
1213 case K_VER_SCROLLBAR:
1214 ins_scroll();
1215 break;
1216
1217 case K_HOR_SCROLLBAR:
1218 ins_horscroll();
1219 break;
1220#endif
1221
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001222 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 case K_S_HOME:
1225 case K_C_HOME:
1226 ins_home(c);
1227 break;
1228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 case K_S_END:
1232 case K_C_END:
1233 ins_end(c);
1234 break;
1235
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001236 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001237 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1238 ins_s_left();
1239 else
1240 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 break;
1242
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001243 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 case K_C_LEFT:
1245 ins_s_left();
1246 break;
1247
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001249 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1250 ins_s_right();
1251 else
1252 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 break;
1254
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001255 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 case K_C_RIGHT:
1257 ins_s_right();
1258 break;
1259
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001260 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001261#ifdef FEAT_INS_EXPAND
1262 if (pum_visible())
1263 goto docomplete;
1264#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001265 if (mod_mask & MOD_MASK_SHIFT)
1266 ins_pageup();
1267 else
1268 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 break;
1270
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001271 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001272 case K_PAGEUP:
1273 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001274#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001275 if (pum_visible())
1276 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 ins_pageup();
1279 break;
1280
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001281 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001282#ifdef FEAT_INS_EXPAND
1283 if (pum_visible())
1284 goto docomplete;
1285#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001286 if (mod_mask & MOD_MASK_SHIFT)
1287 ins_pagedown();
1288 else
1289 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 break;
1291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293 case K_PAGEDOWN:
1294 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001295#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001296 if (pum_visible())
1297 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001298#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 ins_pagedown();
1300 break;
1301
1302#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 ins_drop();
1305 break;
1306#endif
1307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 c = TAB;
1310 /* FALLTHROUGH */
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1314 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1315 goto docomplete;
1316#endif
1317 inserted_space = FALSE;
1318 if (ins_tab())
1319 goto normalchar; /* insert TAB as a normal char */
1320 auto_format(FALSE, TRUE);
1321 break;
1322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001323 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001324 c = CAR;
1325 /* FALLTHROUGH */
1326 case CAR:
1327 case NL:
1328#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1329 /* In a quickfix window a <CR> jumps to the error under the
1330 * cursor. */
1331 if (bt_quickfix(curbuf) && c == CAR)
1332 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001333 if (curwin->w_llist_ref == NULL) /* quickfix window */
1334 do_cmdline_cmd((char_u *)".cc");
1335 else /* location list window */
1336 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337 break;
1338 }
1339#endif
1340#ifdef FEAT_CMDWIN
1341 if (cmdwin_type != 0)
1342 {
1343 /* Execute the command in the cmdline window. */
1344 cmdwin_result = CAR;
1345 goto doESCkey;
1346 }
1347#endif
1348 if (ins_eol(c) && !p_im)
1349 goto doESCkey; /* out of memory */
1350 auto_format(FALSE, FALSE);
1351 inserted_space = FALSE;
1352 break;
1353
Bram Moolenaar860cae12010-06-05 23:22:07 +02001354#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001355 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356# ifdef FEAT_INS_EXPAND
1357 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1358 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001359 if (has_compl_option(TRUE))
1360 goto docomplete;
1361 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 }
1363# endif
1364# ifdef FEAT_DIGRAPHS
1365 c = ins_digraph();
1366 if (c == NUL)
1367 break;
1368# endif
1369 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371
1372#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001373 case Ctrl_X: /* Enter CTRL-X mode */
1374 ins_ctrl_x();
1375 break;
1376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001377 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if (ctrl_x_mode != CTRL_X_TAGS)
1379 goto normalchar;
1380 goto docomplete;
1381
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 if (ctrl_x_mode != CTRL_X_FILES)
1384 goto normalchar;
1385 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001386
1387 case 's': /* Spelling completion after ^X */
1388 case Ctrl_S:
1389 if (ctrl_x_mode != CTRL_X_SPELL)
1390 goto normalchar;
1391 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392#endif
1393
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001394 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395#ifdef FEAT_INS_EXPAND
1396 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1397#endif
1398 {
1399 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1400 if (p_im)
1401 {
1402 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1403 break;
1404 goto doESCkey;
1405 }
1406 goto normalchar;
1407 }
1408#ifdef FEAT_INS_EXPAND
1409 /* FALLTHROUGH */
1410
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 case Ctrl_N:
1413 /* if 'complete' is empty then plain ^P is no longer special,
1414 * but it is under other ^X modes */
1415 if (*curbuf->b_p_cpt == NUL
1416 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001417 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 goto normalchar;
1419
1420docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001421 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001423 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001424 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 break;
1426#endif /* FEAT_INS_EXPAND */
1427
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001428 case Ctrl_Y: /* copy from previous line or scroll down */
1429 case Ctrl_E: /* copy from next line or scroll up */
1430 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 break;
1432
1433 default:
1434#ifdef UNIX
1435 if (c == intr_char) /* special interrupt char */
1436 goto do_intr;
1437#endif
1438
Bram Moolenaare659c952011-05-19 17:25:41 +02001439normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001441 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001442 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001443#ifdef FEAT_AUTOCMD
1444 if (!p_paste)
1445 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001446 /* Trigger InsertCharPre. */
1447 char_u *str = do_insert_char_pre(c);
1448 char_u *p;
1449
1450 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001451 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001452 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001453 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001454 /* Insert the new value of v:char literally. */
1455 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001456 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001457 c = PTR2CHAR(p);
1458 if (c == CAR || c == K_KENTER || c == NL)
1459 ins_eol(c);
1460 else
1461 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001462 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001463 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001464 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001465 vim_free(str);
1466 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001467 }
1468
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001469 /* If the new value is already inserted or an empty string
1470 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001471 if (c == NUL)
1472 break;
1473 }
1474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475#ifdef FEAT_SMARTINDENT
1476 /* Try to perform smart-indenting. */
1477 ins_try_si(c);
1478#endif
1479
1480 if (c == ' ')
1481 {
1482 inserted_space = TRUE;
1483#ifdef FEAT_CINDENT
1484 if (inindent(0))
1485 can_cindent = FALSE;
1486#endif
1487 if (Insstart_blank_vcol == MAXCOL
1488 && curwin->w_cursor.lnum == Insstart.lnum)
1489 Insstart_blank_vcol = get_nolist_virtcol();
1490 }
1491
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001492 /* Insert a normal character and check for abbreviations on a
1493 * special character. Let CTRL-] expand abbreviations without
1494 * inserting it. */
1495 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496#ifdef FEAT_MBYTE
1497 /* Add ABBR_OFF for characters above 0x100, this is
1498 * what check_abbr() expects. */
1499 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1500#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001501 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001502 {
1503 insert_special(c, FALSE, FALSE);
1504#ifdef FEAT_RIGHTLEFT
1505 revins_legal++;
1506 revins_chars++;
1507#endif
1508 }
1509
1510 auto_format(FALSE, TRUE);
1511
1512#ifdef FEAT_FOLDING
1513 /* When inserting a character the cursor line must never be in a
1514 * closed fold. */
1515 foldOpenCursor();
1516#endif
1517 break;
1518 } /* end of switch (c) */
1519
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001520#ifdef FEAT_AUTOCMD
1521 /* If typed something may trigger CursorHoldI again. */
1522 if (c != K_CURSORHOLD)
1523 did_cursorhold = FALSE;
1524#endif
1525
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 /* If the cursor was moved we didn't just insert a space */
1527 if (arrow_used)
1528 inserted_space = FALSE;
1529
1530#ifdef FEAT_CINDENT
1531 if (can_cindent && cindent_on()
1532# ifdef FEAT_INS_EXPAND
1533 && ctrl_x_mode == 0
1534# endif
1535 )
1536 {
1537force_cindent:
1538 /*
1539 * Indent now if a key was typed that is in 'cinkeys'.
1540 */
1541 if (in_cinkeys(c, ' ', line_is_white))
1542 {
1543 if (stop_arrow() == OK)
1544 /* re-indent the current line */
1545 do_c_expr_indent();
1546 }
1547 }
1548#endif /* FEAT_CINDENT */
1549
1550 } /* for (;;) */
1551 /* NOTREACHED */
1552}
1553
1554/*
1555 * Redraw for Insert mode.
1556 * This is postponed until getting the next character to make '$' in the 'cpo'
1557 * option work correctly.
1558 * Only redraw when there are no characters available. This speeds up
1559 * inserting sequences of characters (e.g., for CTRL-R).
1560 */
1561 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001562ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001563 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001564{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001565#ifdef FEAT_CONCEAL
1566 linenr_T conceal_old_cursor_line = 0;
1567 linenr_T conceal_new_cursor_line = 0;
1568 int conceal_update_lines = FALSE;
1569#endif
1570
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001571 if (char_avail())
1572 return;
1573
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001574#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001575 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1576 * visible, the command might delete it. */
1577 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001578# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001579 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001580# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001581# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001582 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001583# endif
1584# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001585 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001586# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001587 )
1588 && !equalpos(last_cursormoved, curwin->w_cursor)
1589# ifdef FEAT_INS_EXPAND
1590 && !pum_visible()
1591# endif
1592 )
1593 {
1594# ifdef FEAT_SYN_HL
1595 /* Need to update the screen first, to make sure syntax
1596 * highlighting is correct after making a change (e.g., inserting
1597 * a "(". The autocommand may also require a redraw, so it's done
1598 * again below, unfortunately. */
1599 if (syntax_present(curwin) && must_redraw)
1600 update_screen(0);
1601# endif
1602# ifdef FEAT_AUTOCMD
1603 if (has_cursormovedI())
1604 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1605# endif
1606# ifdef FEAT_CONCEAL
1607 if (curwin->w_p_cole > 0)
1608 {
1609 conceal_old_cursor_line = last_cursormoved.lnum;
1610 conceal_new_cursor_line = curwin->w_cursor.lnum;
1611 conceal_update_lines = TRUE;
1612 }
1613# endif
1614 last_cursormoved = curwin->w_cursor;
1615 }
1616#endif
1617
1618#ifdef FEAT_AUTOCMD
1619 /* Trigger TextChangedI if b_changedtick differs. */
1620 if (ready && has_textchangedI()
1621 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001622# ifdef FEAT_INS_EXPAND
1623 && !pum_visible()
1624# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001625 )
1626 {
1627 if (last_changedtick_buf == curbuf)
1628 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1629 last_changedtick_buf = curbuf;
1630 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001631 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001632#endif
1633
1634 if (must_redraw)
1635 update_screen(0);
1636 else if (clear_cmdline || redraw_cmdline)
1637 showmode(); /* clear cmdline and show mode */
1638# if defined(FEAT_CONCEAL)
1639 if ((conceal_update_lines
1640 && (conceal_old_cursor_line != conceal_new_cursor_line
1641 || conceal_cursor_line(curwin)))
1642 || need_cursor_line_redraw)
1643 {
1644 if (conceal_old_cursor_line != conceal_new_cursor_line)
1645 update_single_line(curwin, conceal_old_cursor_line);
1646 update_single_line(curwin, conceal_new_cursor_line == 0
1647 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1648 curwin->w_valid &= ~VALID_CROW;
1649 }
1650# endif
1651 showruler(FALSE);
1652 setcursor();
1653 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001654}
1655
1656/*
1657 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1658 */
1659 static void
1660ins_ctrl_v()
1661{
1662 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001663 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664
1665 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001666 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667
1668 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001669 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001671 did_putchar = TRUE;
1672 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1674
1675#ifdef FEAT_CMDL_INFO
1676 add_to_showcmd_c(Ctrl_V);
1677#endif
1678
1679 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001680 if (did_putchar)
1681 /* when the line fits in 'columns' the '^' is at the start of the next
1682 * line and will not removed by the redraw */
1683 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684#ifdef FEAT_CMDL_INFO
1685 clear_showcmd();
1686#endif
1687 insert_special(c, FALSE, TRUE);
1688#ifdef FEAT_RIGHTLEFT
1689 revins_chars++;
1690 revins_legal++;
1691#endif
1692}
1693
1694/*
1695 * Put a character directly onto the screen. It's not stored in a buffer.
1696 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1697 */
1698static int pc_status;
1699#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1700#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1701#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1702#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001703static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001704static int pc_attr;
1705static int pc_row;
1706static int pc_col;
1707
1708 void
1709edit_putchar(c, highlight)
1710 int c;
1711 int highlight;
1712{
1713 int attr;
1714
1715 if (ScreenLines != NULL)
1716 {
1717 update_topline(); /* just in case w_topline isn't valid */
1718 validate_cursor();
1719 if (highlight)
1720 attr = hl_attr(HLF_8);
1721 else
1722 attr = 0;
1723 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1724 pc_col = W_WINCOL(curwin);
1725#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1726 pc_status = PC_STATUS_UNSET;
1727#endif
1728#ifdef FEAT_RIGHTLEFT
1729 if (curwin->w_p_rl)
1730 {
1731 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1732# ifdef FEAT_MBYTE
1733 if (has_mbyte)
1734 {
1735 int fix_col = mb_fix_col(pc_col, pc_row);
1736
1737 if (fix_col != pc_col)
1738 {
1739 screen_putchar(' ', pc_row, fix_col, attr);
1740 --curwin->w_wcol;
1741 pc_status = PC_STATUS_RIGHT;
1742 }
1743 }
1744# endif
1745 }
1746 else
1747#endif
1748 {
1749 pc_col += curwin->w_wcol;
1750#ifdef FEAT_MBYTE
1751 if (mb_lefthalve(pc_row, pc_col))
1752 pc_status = PC_STATUS_LEFT;
1753#endif
1754 }
1755
1756 /* save the character to be able to put it back */
1757#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1758 if (pc_status == PC_STATUS_UNSET)
1759#endif
1760 {
1761 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1762 pc_status = PC_STATUS_SET;
1763 }
1764 screen_putchar(c, pc_row, pc_col, attr);
1765 }
1766}
1767
1768/*
1769 * Undo the previous edit_putchar().
1770 */
1771 void
1772edit_unputchar()
1773{
1774 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1775 {
1776#if defined(FEAT_MBYTE)
1777 if (pc_status == PC_STATUS_RIGHT)
1778 ++curwin->w_wcol;
1779 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1780 redrawWinline(curwin->w_cursor.lnum, FALSE);
1781 else
1782#endif
1783 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1784 }
1785}
1786
1787/*
1788 * Called when p_dollar is set: display a '$' at the end of the changed text
1789 * Only works when cursor is in the line that changes.
1790 */
1791 void
1792display_dollar(col)
1793 colnr_T col;
1794{
1795 colnr_T save_col;
1796
1797 if (!redrawing())
1798 return;
1799
1800 cursor_off();
1801 save_col = curwin->w_cursor.col;
1802 curwin->w_cursor.col = col;
1803#ifdef FEAT_MBYTE
1804 if (has_mbyte)
1805 {
1806 char_u *p;
1807
1808 /* If on the last byte of a multi-byte move to the first byte. */
1809 p = ml_get_curline();
1810 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1811 }
1812#endif
1813 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1814 if (curwin->w_wcol < W_WIDTH(curwin))
1815 {
1816 edit_putchar('$', FALSE);
1817 dollar_vcol = curwin->w_virtcol;
1818 }
1819 curwin->w_cursor.col = save_col;
1820}
1821
1822/*
1823 * Call this function before moving the cursor from the normal insert position
1824 * in insert mode.
1825 */
1826 static void
1827undisplay_dollar()
1828{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001829 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001830 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001831 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 redrawWinline(curwin->w_cursor.lnum, FALSE);
1833 }
1834}
1835
1836/*
1837 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1838 * Keep the cursor on the same character.
1839 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1840 * type == INDENT_DEC decrease indent (for CTRL-D)
1841 * type == INDENT_SET set indent to "amount"
1842 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1843 */
1844 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001845change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int type;
1847 int amount;
1848 int round;
1849 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001850 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851{
1852 int vcol;
1853 int last_vcol;
1854 int insstart_less; /* reduction for Insstart.col */
1855 int new_cursor_col;
1856 int i;
1857 char_u *ptr;
1858 int save_p_list;
1859 int start_col;
1860 colnr_T vc;
1861#ifdef FEAT_VREPLACE
1862 colnr_T orig_col = 0; /* init for GCC */
1863 char_u *new_line, *orig_line = NULL; /* init for GCC */
1864
1865 /* VREPLACE mode needs to know what the line was like before changing */
1866 if (State & VREPLACE_FLAG)
1867 {
1868 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1869 orig_col = curwin->w_cursor.col;
1870 }
1871#endif
1872
1873 /* for the following tricks we don't want list mode */
1874 save_p_list = curwin->w_p_list;
1875 curwin->w_p_list = FALSE;
1876 vc = getvcol_nolist(&curwin->w_cursor);
1877 vcol = vc;
1878
1879 /*
1880 * For Replace mode we need to fix the replace stack later, which is only
1881 * possible when the cursor is in the indent. Remember the number of
1882 * characters before the cursor if it's possible.
1883 */
1884 start_col = curwin->w_cursor.col;
1885
1886 /* determine offset from first non-blank */
1887 new_cursor_col = curwin->w_cursor.col;
1888 beginline(BL_WHITE);
1889 new_cursor_col -= curwin->w_cursor.col;
1890
1891 insstart_less = curwin->w_cursor.col;
1892
1893 /*
1894 * If the cursor is in the indent, compute how many screen columns the
1895 * cursor is to the left of the first non-blank.
1896 */
1897 if (new_cursor_col < 0)
1898 vcol = get_indent() - vcol;
1899
1900 if (new_cursor_col > 0) /* can't fix replace stack */
1901 start_col = -1;
1902
1903 /*
1904 * Set the new indent. The cursor will be put on the first non-blank.
1905 */
1906 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001907 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 else
1909 {
1910#ifdef FEAT_VREPLACE
1911 int save_State = State;
1912
1913 /* Avoid being called recursively. */
1914 if (State & VREPLACE_FLAG)
1915 State = INSERT;
1916#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001917 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918#ifdef FEAT_VREPLACE
1919 State = save_State;
1920#endif
1921 }
1922 insstart_less -= curwin->w_cursor.col;
1923
1924 /*
1925 * Try to put cursor on same character.
1926 * If the cursor is at or after the first non-blank in the line,
1927 * compute the cursor column relative to the column of the first
1928 * non-blank character.
1929 * If we are not in insert mode, leave the cursor on the first non-blank.
1930 * If the cursor is before the first non-blank, position it relative
1931 * to the first non-blank, counted in screen columns.
1932 */
1933 if (new_cursor_col >= 0)
1934 {
1935 /*
1936 * When changing the indent while the cursor is touching it, reset
1937 * Insstart_col to 0.
1938 */
1939 if (new_cursor_col == 0)
1940 insstart_less = MAXCOL;
1941 new_cursor_col += curwin->w_cursor.col;
1942 }
1943 else if (!(State & INSERT))
1944 new_cursor_col = curwin->w_cursor.col;
1945 else
1946 {
1947 /*
1948 * Compute the screen column where the cursor should be.
1949 */
1950 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001951 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952
1953 /*
1954 * Advance the cursor until we reach the right screen column.
1955 */
1956 vcol = last_vcol = 0;
1957 new_cursor_col = -1;
1958 ptr = ml_get_curline();
1959 while (vcol <= (int)curwin->w_virtcol)
1960 {
1961 last_vcol = vcol;
1962#ifdef FEAT_MBYTE
1963 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001964 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 else
1966#endif
1967 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001968 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970 vcol = last_vcol;
1971
1972 /*
1973 * May need to insert spaces to be able to position the cursor on
1974 * the right screen column.
1975 */
1976 if (vcol != (int)curwin->w_virtcol)
1977 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001978 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001980 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001981 if (ptr != NULL)
1982 {
1983 new_cursor_col += i;
1984 ptr[i] = NUL;
1985 while (--i >= 0)
1986 ptr[i] = ' ';
1987 ins_str(ptr);
1988 vim_free(ptr);
1989 }
1990 }
1991
1992 /*
1993 * When changing the indent while the cursor is in it, reset
1994 * Insstart_col to 0.
1995 */
1996 insstart_less = MAXCOL;
1997 }
1998
1999 curwin->w_p_list = save_p_list;
2000
2001 if (new_cursor_col <= 0)
2002 curwin->w_cursor.col = 0;
2003 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002004 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 curwin->w_set_curswant = TRUE;
2006 changed_cline_bef_curs();
2007
2008 /*
2009 * May have to adjust the start of the insert.
2010 */
2011 if (State & INSERT)
2012 {
2013 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2014 {
2015 if ((int)Insstart.col <= insstart_less)
2016 Insstart.col = 0;
2017 else
2018 Insstart.col -= insstart_less;
2019 }
2020 if ((int)ai_col <= insstart_less)
2021 ai_col = 0;
2022 else
2023 ai_col -= insstart_less;
2024 }
2025
2026 /*
2027 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2028 * If the number of characters before the cursor decreased, need to pop a
2029 * few characters from the replace stack.
2030 * If the number of characters before the cursor increased, need to push a
2031 * few NULs onto the replace stack.
2032 */
2033 if (REPLACE_NORMAL(State) && start_col >= 0)
2034 {
2035 while (start_col > (int)curwin->w_cursor.col)
2036 {
2037 replace_join(0); /* remove a NUL from the replace stack */
2038 --start_col;
2039 }
2040 while (start_col < (int)curwin->w_cursor.col || replaced)
2041 {
2042 replace_push(NUL);
2043 if (replaced)
2044 {
2045 replace_push(replaced);
2046 replaced = NUL;
2047 }
2048 ++start_col;
2049 }
2050 }
2051
2052#ifdef FEAT_VREPLACE
2053 /*
2054 * For VREPLACE mode, we also have to fix the replace stack. In this case
2055 * it is always possible because we backspace over the whole line and then
2056 * put it back again the way we wanted it.
2057 */
2058 if (State & VREPLACE_FLAG)
2059 {
2060 /* If orig_line didn't allocate, just return. At least we did the job,
2061 * even if you can't backspace. */
2062 if (orig_line == NULL)
2063 return;
2064
2065 /* Save new line */
2066 new_line = vim_strsave(ml_get_curline());
2067 if (new_line == NULL)
2068 return;
2069
2070 /* We only put back the new line up to the cursor */
2071 new_line[curwin->w_cursor.col] = NUL;
2072
2073 /* Put back original line */
2074 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2075 curwin->w_cursor.col = orig_col;
2076
2077 /* Backspace from cursor to start of line */
2078 backspace_until_column(0);
2079
2080 /* Insert new stuff into line again */
2081 ins_bytes(new_line);
2082
2083 vim_free(new_line);
2084 }
2085#endif
2086}
2087
2088/*
2089 * Truncate the space at the end of a line. This is to be used only in an
2090 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2091 * modes.
2092 */
2093 void
2094truncate_spaces(line)
2095 char_u *line;
2096{
2097 int i;
2098
2099 /* find start of trailing white space */
2100 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2101 {
2102 if (State & REPLACE_FLAG)
2103 replace_join(0); /* remove a NUL from the replace stack */
2104 }
2105 line[i + 1] = NUL;
2106}
2107
2108#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2109 || defined(FEAT_COMMENTS) || defined(PROTO)
2110/*
2111 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2112 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002113 * Will attempt not to go before "col" even when there is a composing
2114 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 */
2116 void
2117backspace_until_column(col)
2118 int col;
2119{
2120 while ((int)curwin->w_cursor.col > col)
2121 {
2122 curwin->w_cursor.col--;
2123 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002124 replace_do_bs(col);
2125 else if (!del_char_after_col(col))
2126 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002127 }
2128}
2129#endif
2130
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002131/*
2132 * Like del_char(), but make sure not to go before column "limit_col".
2133 * Only matters when there are composing characters.
2134 * Return TRUE when something was deleted.
2135 */
2136 static int
2137del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002138 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002139{
2140#ifdef FEAT_MBYTE
2141 if (enc_utf8 && limit_col >= 0)
2142 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002143 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002144
2145 /* Make sure the cursor is at the start of a character, but
2146 * skip forward again when going too far back because of a
2147 * composing character. */
2148 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002149 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002150 {
2151 int l = utf_ptr2len(ml_get_cursor());
2152
2153 if (l == 0) /* end of line */
2154 break;
2155 curwin->w_cursor.col += l;
2156 }
2157 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2158 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002159 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002160 }
2161 else
2162#endif
2163 (void)del_char(FALSE);
2164 return TRUE;
2165}
2166
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2168/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002169 * CTRL-X pressed in Insert mode.
2170 */
2171 static void
2172ins_ctrl_x()
2173{
2174 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2175 * CTRL-V works like CTRL-N */
2176 if (ctrl_x_mode != CTRL_X_CMDLINE)
2177 {
2178 /* if the next ^X<> won't ADD nothing, then reset
2179 * compl_cont_status */
2180 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002181 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002182 else
2183 compl_cont_status = 0;
2184 /* We're not sure which CTRL-X mode it will be yet */
2185 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2186 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2187 edit_submode_pre = NULL;
2188 showmode();
2189 }
2190}
2191
2192/*
2193 * Return TRUE if the 'dict' or 'tsr' option can be used.
2194 */
2195 static int
2196has_compl_option(dict_opt)
2197 int dict_opt;
2198{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002199 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002200# ifdef FEAT_SPELL
2201 && !curwin->w_p_spell
2202# endif
2203 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002204 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2205 {
2206 ctrl_x_mode = 0;
2207 edit_submode = NULL;
2208 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2209 : (char_u *)_("'thesaurus' option is empty"),
2210 hl_attr(HLF_E));
2211 if (emsg_silent == 0)
2212 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002213 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002214 setcursor();
2215 out_flush();
2216 ui_delay(2000L, FALSE);
2217 }
2218 return FALSE;
2219 }
2220 return TRUE;
2221}
2222
2223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002224 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2225 * This depends on the current mode.
2226 */
2227 int
2228vim_is_ctrl_x_key(c)
2229 int c;
2230{
2231 /* Always allow ^R - let it's results then be checked */
2232 if (c == Ctrl_R)
2233 return TRUE;
2234
Bram Moolenaare3226be2005-12-18 22:10:00 +00002235 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002236 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002237 return TRUE;
2238
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 switch (ctrl_x_mode)
2240 {
2241 case 0: /* Not in any CTRL-X mode */
2242 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2243 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002244 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2246 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2247 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002248 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002249 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002250 case CTRL_X_SCROLL:
2251 return (c == Ctrl_Y || c == Ctrl_E);
2252 case CTRL_X_WHOLE_LINE:
2253 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2254 case CTRL_X_FILES:
2255 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2256 case CTRL_X_DICTIONARY:
2257 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2258 case CTRL_X_THESAURUS:
2259 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2260 case CTRL_X_TAGS:
2261 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2262#ifdef FEAT_FIND_ID
2263 case CTRL_X_PATH_PATTERNS:
2264 return (c == Ctrl_P || c == Ctrl_N);
2265 case CTRL_X_PATH_DEFINES:
2266 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2267#endif
2268 case CTRL_X_CMDLINE:
2269 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2270 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002271#ifdef FEAT_COMPL_FUNC
2272 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002273 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002274 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002275 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002276#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002277 case CTRL_X_SPELL:
2278 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002279 case CTRL_X_EVAL:
2280 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 }
2282 EMSG(_(e_internal));
2283 return FALSE;
2284}
2285
2286/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002287 * Return TRUE when character "c" is part of the item currently being
2288 * completed. Used to decide whether to abandon complete mode when the menu
2289 * is visible.
2290 */
2291 static int
2292ins_compl_accept_char(c)
2293 int c;
2294{
2295 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2296 /* When expanding an identifier only accept identifier chars. */
2297 return vim_isIDc(c);
2298
2299 switch (ctrl_x_mode)
2300 {
2301 case CTRL_X_FILES:
2302 /* When expanding file name only accept file name chars. But not
2303 * path separators, so that "proto/<Tab>" expands files in
2304 * "proto", not "proto/" as a whole */
2305 return vim_isfilec(c) && !vim_ispathsep(c);
2306
2307 case CTRL_X_CMDLINE:
2308 case CTRL_X_OMNI:
2309 /* Command line and Omni completion can work with just about any
2310 * printable character, but do stop at white space. */
2311 return vim_isprintc(c) && !vim_iswhite(c);
2312
2313 case CTRL_X_WHOLE_LINE:
2314 /* For while line completion a space can be part of the line. */
2315 return vim_isprintc(c);
2316 }
2317 return vim_iswordc(c);
2318}
2319
2320/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002321 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002323 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 * the rest of the word to be in -- webb
2325 */
2326 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002327ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 char_u *str;
2329 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002330 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 char_u *fname;
2332 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002333 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002335 char_u *p;
2336 int i, c;
2337 int actual_len; /* Take multi-byte characters */
2338 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002339 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002340 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 int has_lower = FALSE;
2342 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002344 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002345 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002346 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
Bram Moolenaara2993e12007-08-12 14:38:46 +00002348 /* Find actual length of completion. */
2349#ifdef FEAT_MBYTE
2350 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002352 p = str;
2353 actual_len = 0;
2354 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002356 mb_ptr_adv(p);
2357 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002360 else
2361#endif
2362 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
Bram Moolenaara2993e12007-08-12 14:38:46 +00002364 /* Find actual length of original text. */
2365#ifdef FEAT_MBYTE
2366 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002368 p = compl_orig_text;
2369 actual_compl_length = 0;
2370 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002372 mb_ptr_adv(p);
2373 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
2375 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002376 else
2377#endif
2378 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002380 /* "actual_len" may be smaller than "actual_compl_length" when using
2381 * thesaurus, only use the minimum when comparing. */
2382 min_len = actual_len < actual_compl_length
2383 ? actual_len : actual_compl_length;
2384
Bram Moolenaara2993e12007-08-12 14:38:46 +00002385 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002386 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002387 if (wca != NULL)
2388 {
2389 p = str;
2390 for (i = 0; i < actual_len; ++i)
2391#ifdef FEAT_MBYTE
2392 if (has_mbyte)
2393 wca[i] = mb_ptr2char_adv(&p);
2394 else
2395#endif
2396 wca[i] = *(p++);
2397
2398 /* Rule 1: Were any chars converted to lower? */
2399 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002400 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002401 {
2402#ifdef FEAT_MBYTE
2403 if (has_mbyte)
2404 c = mb_ptr2char_adv(&p);
2405 else
2406#endif
2407 c = *(p++);
2408 if (MB_ISLOWER(c))
2409 {
2410 has_lower = TRUE;
2411 if (MB_ISUPPER(wca[i]))
2412 {
2413 /* Rule 1 is satisfied. */
2414 for (i = actual_compl_length; i < actual_len; ++i)
2415 wca[i] = MB_TOLOWER(wca[i]);
2416 break;
2417 }
2418 }
2419 }
2420
2421 /*
2422 * Rule 2: No lower case, 2nd consecutive letter converted to
2423 * upper case.
2424 */
2425 if (!has_lower)
2426 {
2427 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002428 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002429 {
2430#ifdef FEAT_MBYTE
2431 if (has_mbyte)
2432 c = mb_ptr2char_adv(&p);
2433 else
2434#endif
2435 c = *(p++);
2436 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2437 {
2438 /* Rule 2 is satisfied. */
2439 for (i = actual_compl_length; i < actual_len; ++i)
2440 wca[i] = MB_TOUPPER(wca[i]);
2441 break;
2442 }
2443 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2444 }
2445 }
2446
2447 /* Copy the original case of the part we typed. */
2448 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002449 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002450 {
2451#ifdef FEAT_MBYTE
2452 if (has_mbyte)
2453 c = mb_ptr2char_adv(&p);
2454 else
2455#endif
2456 c = *(p++);
2457 if (MB_ISLOWER(c))
2458 wca[i] = MB_TOLOWER(wca[i]);
2459 else if (MB_ISUPPER(c))
2460 wca[i] = MB_TOUPPER(wca[i]);
2461 }
2462
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002463 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002464 * Generate encoding specific output from wide character array.
2465 * Multi-byte characters can occupy up to five bytes more than
2466 * ASCII characters, and we also need one byte for NUL, so stay
2467 * six bytes away from the edge of IObuff.
2468 */
2469 p = IObuff;
2470 i = 0;
2471 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2472#ifdef FEAT_MBYTE
2473 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002474 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002475 else
2476#endif
2477 *(p++) = wca[i++];
2478 *p = NUL;
2479
2480 vim_free(wca);
2481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002483 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2484 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002486 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487}
2488
2489/*
2490 * Add a match to the list of matches.
2491 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002492 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002493 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002495 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002496ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 char_u *str;
2498 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002499 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002501 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002502 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002503 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002504 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002506 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002507 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508
2509 ui_breakcheck();
2510 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002511 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 if (len < 0)
2513 len = (int)STRLEN(str);
2514
2515 /*
2516 * If the same match is already present, don't add it.
2517 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002518 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002520 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 do
2522 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002523 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002524 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002525 && match->cp_str[len] == NUL)
2526 return NOTDONE;
2527 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002528 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 }
2530
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002531 /* Remove any popup menu before changing the list of matches. */
2532 ins_compl_del_pum();
2533
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 /*
2535 * Allocate a new match structure.
2536 * Copy the values to the new match structure.
2537 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002538 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002540 return FAIL;
2541 match->cp_number = -1;
2542 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002543 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002544 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 {
2546 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002547 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002549 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002550
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002552 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2554 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002555 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002556 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002557 && compl_curr_match->cp_fname != NULL
2558 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002559 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002560 else if (fname != NULL)
2561 {
2562 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002563 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002564 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 match->cp_fname = NULL;
2567 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002568
2569 if (cptext != NULL)
2570 {
2571 int i;
2572
2573 for (i = 0; i < CPT_COUNT; ++i)
2574 if (cptext[i] != NULL && *cptext[i] != NUL)
2575 match->cp_text[i] = vim_strsave(cptext[i]);
2576 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002577
2578 /*
2579 * Link the new match structure in the list of matches.
2580 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002581 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002582 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 else if (dir == FORWARD)
2584 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002585 match->cp_next = compl_curr_match->cp_next;
2586 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587 }
2588 else /* BACKWARD */
2589 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002590 match->cp_next = compl_curr_match;
2591 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002593 if (match->cp_next)
2594 match->cp_next->cp_prev = match;
2595 if (match->cp_prev)
2596 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002598 compl_first_match = match;
2599 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002601 /*
2602 * Find the longest common string if still doing that.
2603 */
2604 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2605 ins_compl_longest_match(match);
2606
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 return OK;
2608}
2609
2610/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002611 * Return TRUE if "str[len]" matches with match->cp_str, considering
2612 * match->cp_icase.
2613 */
2614 static int
2615ins_compl_equal(match, str, len)
2616 compl_T *match;
2617 char_u *str;
2618 int len;
2619{
2620 if (match->cp_icase)
2621 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2622 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2623}
2624
2625/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002626 * Reduce the longest common string for match "match".
2627 */
2628 static void
2629ins_compl_longest_match(match)
2630 compl_T *match;
2631{
2632 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002633 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002634 int had_match;
2635
2636 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002637 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002638 /* First match, use it as a whole. */
2639 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002640 if (compl_leader != NULL)
2641 {
2642 had_match = (curwin->w_cursor.col > compl_col);
2643 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002644 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002645 ins_redraw(FALSE);
2646
2647 /* When the match isn't there (to avoid matching itself) remove it
2648 * again after redrawing. */
2649 if (!had_match)
2650 ins_compl_delete();
2651 compl_used_match = FALSE;
2652 }
2653 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002654 else
2655 {
2656 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002657 p = compl_leader;
2658 s = match->cp_str;
2659 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002660 {
2661#ifdef FEAT_MBYTE
2662 if (has_mbyte)
2663 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002664 c1 = mb_ptr2char(p);
2665 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002666 }
2667 else
2668#endif
2669 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002670 c1 = *p;
2671 c2 = *s;
2672 }
2673 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2674 : (c1 != c2))
2675 break;
2676#ifdef FEAT_MBYTE
2677 if (has_mbyte)
2678 {
2679 mb_ptr_adv(p);
2680 mb_ptr_adv(s);
2681 }
2682 else
2683#endif
2684 {
2685 ++p;
2686 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002687 }
2688 }
2689
2690 if (*p != NUL)
2691 {
2692 /* Leader was shortened, need to change the inserted text. */
2693 *p = NUL;
2694 had_match = (curwin->w_cursor.col > compl_col);
2695 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002696 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002697 ins_redraw(FALSE);
2698
2699 /* When the match isn't there (to avoid matching itself) remove it
2700 * again after redrawing. */
2701 if (!had_match)
2702 ins_compl_delete();
2703 }
2704
2705 compl_used_match = FALSE;
2706 }
2707}
2708
2709/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 * Add an array of matches to the list of matches.
2711 * Frees matches[].
2712 */
2713 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002714ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 int num_matches;
2716 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002717 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002718{
2719 int i;
2720 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002721 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722
Bram Moolenaar572cb562005-08-05 21:35:02 +00002723 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002724 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002725 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002727 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728 FreeWild(num_matches, matches);
2729}
2730
2731/* Make the completion list cyclic.
2732 * Return the number of matches (excluding the original).
2733 */
2734 static int
2735ins_compl_make_cyclic()
2736{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002737 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 int count = 0;
2739
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002740 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
2742 /*
2743 * Find the end of the list.
2744 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002745 match = compl_first_match;
2746 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002747 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002749 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002750 ++count;
2751 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002752 match->cp_next = compl_first_match;
2753 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002754 }
2755 return count;
2756}
2757
Bram Moolenaara94bc432006-03-10 21:42:59 +00002758/*
2759 * Start completion for the complete() function.
2760 * "startcol" is where the matched text starts (1 is first column).
2761 * "list" is the list of matches.
2762 */
2763 void
2764set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002765 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002766 list_T *list;
2767{
2768 /* If already doing completions stop it. */
2769 if (ctrl_x_mode != 0)
2770 ins_compl_prep(' ');
2771 ins_compl_clear();
2772
2773 if (stop_arrow() == FAIL)
2774 return;
2775
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002776 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002777 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002778 startcol = curwin->w_cursor.col;
2779 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002780 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002781 /* compl_pattern doesn't need to be set */
2782 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2783 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002784 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002785 return;
2786
Bram Moolenaare4214502015-03-05 18:08:43 +01002787 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002788
2789 ins_compl_add_list(list);
2790 compl_matches = ins_compl_make_cyclic();
2791 compl_started = TRUE;
2792 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002793 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002794
2795 compl_curr_match = compl_first_match;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002796 if (compl_no_insert)
Bram Moolenaar2cf69382015-07-17 13:42:23 +02002797 ins_complete(K_DOWN);
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002798 else
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002799 ins_complete(Ctrl_N);
Bram Moolenaar2cf69382015-07-17 13:42:23 +02002800 if (compl_no_select)
2801 ins_complete(Ctrl_P);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002802 out_flush();
2803}
2804
2805
Bram Moolenaar9372a112005-12-06 19:59:18 +00002806/* "compl_match_array" points the currently displayed list of entries in the
2807 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002808static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002809static int compl_match_arraysize;
2810
2811/*
2812 * Update the screen and when there is any scrolling remove the popup menu.
2813 */
2814 static void
2815ins_compl_upd_pum()
2816{
2817 int h;
2818
2819 if (compl_match_array != NULL)
2820 {
2821 h = curwin->w_cline_height;
2822 update_screen(0);
2823 if (h != curwin->w_cline_height)
2824 ins_compl_del_pum();
2825 }
2826}
2827
2828/*
2829 * Remove any popup menu.
2830 */
2831 static void
2832ins_compl_del_pum()
2833{
2834 if (compl_match_array != NULL)
2835 {
2836 pum_undisplay();
2837 vim_free(compl_match_array);
2838 compl_match_array = NULL;
2839 }
2840}
2841
2842/*
2843 * Return TRUE if the popup menu should be displayed.
2844 */
2845 static int
2846pum_wanted()
2847{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002848 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002849 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002850 return FALSE;
2851
2852 /* The display looks bad on a B&W display. */
2853 if (t_colors < 8
2854#ifdef FEAT_GUI
2855 && !gui.in_use
2856#endif
2857 )
2858 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002859 return TRUE;
2860}
2861
2862/*
2863 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002864 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002865 */
2866 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002867pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002868{
2869 compl_T *compl;
2870 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002871
2872 /* Don't display the popup menu if there are no matches or there is only
2873 * one (ignoring the original text). */
2874 compl = compl_first_match;
2875 i = 0;
2876 do
2877 {
2878 if (compl == NULL
2879 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2880 break;
2881 compl = compl->cp_next;
2882 } while (compl != compl_first_match);
2883
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002884 if (strstr((char *)p_cot, "menuone") != NULL)
2885 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002886 return (i >= 2);
2887}
2888
2889/*
2890 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002891 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002892 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002893 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002894ins_compl_show_pum()
2895{
2896 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002897 compl_T *shown_compl = NULL;
2898 int did_find_shown_match = FALSE;
2899 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002900 int i;
2901 int cur = -1;
2902 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002903 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002904
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002905 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002906 return;
2907
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002908#if defined(FEAT_EVAL)
2909 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2910 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2911#endif
2912
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002913 /* Update the screen before drawing the popup menu over it. */
2914 update_screen(0);
2915
2916 if (compl_match_array == NULL)
2917 {
2918 /* Need to build the popup menu list. */
2919 compl_match_arraysize = 0;
2920 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002921 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002922 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002923 do
2924 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002925 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2926 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002927 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002928 ++compl_match_arraysize;
2929 compl = compl->cp_next;
2930 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002931 if (compl_match_arraysize == 0)
2932 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002933 compl_match_array = (pumitem_T *)alloc_clear(
2934 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002935 * compl_match_arraysize));
2936 if (compl_match_array != NULL)
2937 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002938 /* If the current match is the original text don't find the first
2939 * match after it, don't highlight anything. */
2940 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2941 shown_match_ok = TRUE;
2942
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002943 i = 0;
2944 compl = compl_first_match;
2945 do
2946 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002947 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2948 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002949 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002950 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002951 if (!shown_match_ok)
2952 {
2953 if (compl == compl_shown_match || did_find_shown_match)
2954 {
2955 /* This item is the shown match or this is the
2956 * first displayed item after the shown match. */
2957 compl_shown_match = compl;
2958 did_find_shown_match = TRUE;
2959 shown_match_ok = TRUE;
2960 }
2961 else
2962 /* Remember this displayed match for when the
2963 * shown match is just below it. */
2964 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002965 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002966 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002967
2968 if (compl->cp_text[CPT_ABBR] != NULL)
2969 compl_match_array[i].pum_text =
2970 compl->cp_text[CPT_ABBR];
2971 else
2972 compl_match_array[i].pum_text = compl->cp_str;
2973 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2974 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2975 if (compl->cp_text[CPT_MENU] != NULL)
2976 compl_match_array[i++].pum_extra =
2977 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002978 else
2979 compl_match_array[i++].pum_extra = compl->cp_fname;
2980 }
2981
2982 if (compl == compl_shown_match)
2983 {
2984 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002985
2986 /* When the original text is the shown match don't set
2987 * compl_shown_match. */
2988 if (compl->cp_flags & ORIGINAL_TEXT)
2989 shown_match_ok = TRUE;
2990
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002991 if (!shown_match_ok && shown_compl != NULL)
2992 {
2993 /* The shown match isn't displayed, set it to the
2994 * previously displayed match. */
2995 compl_shown_match = shown_compl;
2996 shown_match_ok = TRUE;
2997 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002998 }
2999 compl = compl->cp_next;
3000 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003001
3002 if (!shown_match_ok) /* no displayed match at all */
3003 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003004 }
3005 }
3006 else
3007 {
3008 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003009 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003010 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3011 || compl_match_array[i].pum_text
3012 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003013 {
3014 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003015 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003016 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003017 }
3018
3019 if (compl_match_array != NULL)
3020 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003021 /* In Replace mode when a $ is displayed at the end of the line only
3022 * part of the screen would be updated. We do need to redraw here. */
3023 dollar_vcol = -1;
3024
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003025 /* Compute the screen column of the start of the completed text.
3026 * Use the cursor to get all wrapping and other settings right. */
3027 col = curwin->w_cursor.col;
3028 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003029 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003030 curwin->w_cursor.col = col;
3031 }
3032}
3033
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034#define DICT_FIRST (1) /* use just first element in "dict" */
3035#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003036
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003038 * Add any identifiers that match the given pattern in the list of dictionary
3039 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003040 */
3041 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00003042ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
3043 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003045 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003046 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003048 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 char_u *ptr;
3050 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 char_u **files;
3053 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003055 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056
Bram Moolenaar0b238792006-03-02 22:49:12 +00003057 if (*dict == NUL)
3058 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003059#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003060 /* When 'dictionary' is empty and spell checking is enabled use
3061 * "spell". */
3062 if (!thesaurus && curwin->w_p_spell)
3063 dict = (char_u *)"spell";
3064 else
3065#endif
3066 return;
3067 }
3068
Bram Moolenaar071d4272004-06-13 20:20:40 +00003069 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003070 if (buf == NULL)
3071 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003072 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003073
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 /* If 'infercase' is set, don't use 'smartcase' here */
3075 save_p_scs = p_scs;
3076 if (curbuf->b_p_inf)
3077 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003078
3079 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3080 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003081 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003082 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003083 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003084 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003085 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003086
3087 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003088 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003089 len = STRLEN(pat_esc) + 10;
3090 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003091 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003092 {
3093 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003094 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003095 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003096 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003097 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3098 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003099 vim_free(ptr);
3100 }
3101 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003102 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003103 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003104 if (regmatch.regprog == NULL)
3105 goto theend;
3106 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003107
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3109 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003110 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 {
3112 /* copy one dictionary file name into buf */
3113 if (flags == DICT_EXACT)
3114 {
3115 count = 1;
3116 files = &dict;
3117 }
3118 else
3119 {
3120 /* Expand wildcards in the dictionary name, but do not allow
3121 * backticks (for security, the 'dict' option may have been set in
3122 * a modeline). */
3123 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003124# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003125 if (!thesaurus && STRCMP(buf, "spell") == 0)
3126 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003127 else
3128# endif
3129 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 || expand_wildcards(1, &buf, &count, &files,
3131 EW_FILE|EW_SILENT) != OK)
3132 count = 0;
3133 }
3134
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003135# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003136 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003138 /* Complete from active spelling. Skip "\<" in the pattern, we
3139 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003140 if (pat[0] == '\\' && pat[1] == '<')
3141 ptr = pat + 2;
3142 else
3143 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003144 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003146 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003147# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003148 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003149 {
3150 ins_compl_files(count, files, thesaurus, flags,
3151 &regmatch, buf, &dir);
3152 if (flags != DICT_EXACT)
3153 FreeWild(count, files);
3154 }
3155 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156 break;
3157 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003158
3159theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003160 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003161 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003162 vim_free(buf);
3163}
3164
Bram Moolenaar0b238792006-03-02 22:49:12 +00003165 static void
3166ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3167 int count;
3168 char_u **files;
3169 int thesaurus;
3170 int flags;
3171 regmatch_T *regmatch;
3172 char_u *buf;
3173 int *dir;
3174{
3175 char_u *ptr;
3176 int i;
3177 FILE *fp;
3178 int add_r;
3179
3180 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3181 {
3182 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3183 if (flags != DICT_EXACT)
3184 {
3185 vim_snprintf((char *)IObuff, IOSIZE,
3186 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003187 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003188 }
3189
3190 if (fp != NULL)
3191 {
3192 /*
3193 * Read dictionary file line by line.
3194 * Check each line for a match.
3195 */
3196 while (!got_int && !compl_interrupted
3197 && !vim_fgets(buf, LSIZE, fp))
3198 {
3199 ptr = buf;
3200 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3201 {
3202 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003203 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003204 ptr = find_line_end(ptr);
3205 else
3206 ptr = find_word_end(ptr);
3207 add_r = ins_compl_add_infercase(regmatch->startp[0],
3208 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003209 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003210 if (thesaurus)
3211 {
3212 char_u *wstart;
3213
3214 /*
3215 * Add the other matches on the line
3216 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003217 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003218 while (!got_int)
3219 {
3220 /* Find start of the next word. Skip white
3221 * space and punctuation. */
3222 ptr = find_word_start(ptr);
3223 if (*ptr == NUL || *ptr == NL)
3224 break;
3225 wstart = ptr;
3226
Bram Moolenaara2993e12007-08-12 14:38:46 +00003227 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003228#ifdef FEAT_MBYTE
3229 if (has_mbyte)
3230 /* Japanese words may have characters in
3231 * different classes, only separate words
3232 * with single-byte non-word characters. */
3233 while (*ptr != NUL)
3234 {
3235 int l = (*mb_ptr2len)(ptr);
3236
3237 if (l < 2 && !vim_iswordc(*ptr))
3238 break;
3239 ptr += l;
3240 }
3241 else
3242#endif
3243 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003244
3245 /* Add the word. Skip the regexp match. */
3246 if (wstart != regmatch->startp[0])
3247 add_r = ins_compl_add_infercase(wstart,
3248 (int)(ptr - wstart),
3249 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003250 }
3251 }
3252 if (add_r == OK)
3253 /* if dir was BACKWARD then honor it just once */
3254 *dir = FORWARD;
3255 else if (add_r == FAIL)
3256 break;
3257 /* avoid expensive call to vim_regexec() when at end
3258 * of line */
3259 if (*ptr == '\n' || got_int)
3260 break;
3261 }
3262 line_breakcheck();
3263 ins_compl_check_keys(50);
3264 }
3265 fclose(fp);
3266 }
3267 }
3268}
3269
Bram Moolenaar071d4272004-06-13 20:20:40 +00003270/*
3271 * Find the start of the next word.
3272 * Returns a pointer to the first char of the word. Also stops at a NUL.
3273 */
3274 char_u *
3275find_word_start(ptr)
3276 char_u *ptr;
3277{
3278#ifdef FEAT_MBYTE
3279 if (has_mbyte)
3280 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003281 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003282 else
3283#endif
3284 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3285 ++ptr;
3286 return ptr;
3287}
3288
3289/*
3290 * Find the end of the word. Assumes it starts inside a word.
3291 * Returns a pointer to just after the word.
3292 */
3293 char_u *
3294find_word_end(ptr)
3295 char_u *ptr;
3296{
3297#ifdef FEAT_MBYTE
3298 int start_class;
3299
3300 if (has_mbyte)
3301 {
3302 start_class = mb_get_class(ptr);
3303 if (start_class > 1)
3304 while (*ptr != NUL)
3305 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003306 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 if (mb_get_class(ptr) != start_class)
3308 break;
3309 }
3310 }
3311 else
3312#endif
3313 while (vim_iswordc(*ptr))
3314 ++ptr;
3315 return ptr;
3316}
3317
3318/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003319 * Find the end of the line, omitting CR and NL at the end.
3320 * Returns a pointer to just after the line.
3321 */
3322 static char_u *
3323find_line_end(ptr)
3324 char_u *ptr;
3325{
3326 char_u *s;
3327
3328 s = ptr + STRLEN(ptr);
3329 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3330 --s;
3331 return s;
3332}
3333
3334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003335 * Free the list of completions
3336 */
3337 static void
3338ins_compl_free()
3339{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003340 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003341 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003342
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003343 vim_free(compl_pattern);
3344 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003345 vim_free(compl_leader);
3346 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003348 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003350
3351 ins_compl_del_pum();
3352 pum_clear();
3353
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003354 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003355 do
3356 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003357 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003358 compl_curr_match = compl_curr_match->cp_next;
3359 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003361 if (match->cp_flags & FREE_FNAME)
3362 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003363 for (i = 0; i < CPT_COUNT; ++i)
3364 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003366 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3367 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003368 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369}
3370
3371 static void
3372ins_compl_clear()
3373{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003374 compl_cont_status = 0;
3375 compl_started = FALSE;
3376 compl_matches = 0;
3377 vim_free(compl_pattern);
3378 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003379 vim_free(compl_leader);
3380 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003381 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003382 vim_free(compl_orig_text);
3383 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003384 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003385 /* clear v:completed_item */
3386 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387}
3388
3389/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003390 * Return TRUE when Insert completion is active.
3391 */
3392 int
3393ins_compl_active()
3394{
3395 return compl_started;
3396}
3397
3398/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003399 * Delete one character before the cursor and show the subset of the matches
3400 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003401 * Returns the character to be used, NUL if the work is done and another char
3402 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003403 */
3404 static int
3405ins_compl_bs()
3406{
3407 char_u *line;
3408 char_u *p;
3409
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003410 line = ml_get_curline();
3411 p = line + curwin->w_cursor.col;
3412 mb_ptr_back(line, p);
3413
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003414 /* Stop completion when the whole word was deleted. For Omni completion
3415 * allow the word to be deleted, we won't match everything. */
3416 if ((int)(p - line) - (int)compl_col < 0
3417 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003418 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003419 return K_BS;
3420
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003421 /* Deleted more than what was used to find matches or didn't finish
3422 * finding all matches: need to look for matches all over again. */
3423 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003424 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003425 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003426
Bram Moolenaara6557602006-02-04 22:43:20 +00003427 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003428 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003429 if (compl_leader != NULL)
3430 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003431 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003432 if (compl_shown_match != NULL)
3433 /* Make sure current match is not a hidden item. */
3434 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003435 return NUL;
3436 }
3437 return K_BS;
3438}
Bram Moolenaara6557602006-02-04 22:43:20 +00003439
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003440/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003441 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3442 * be called.
3443 */
3444 static int
3445ins_compl_need_restart()
3446{
3447 /* Return TRUE if we didn't complete finding matches or when the
3448 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3449 return compl_was_interrupted
3450 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3451 && compl_opt_refresh_always);
3452}
3453
3454/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003455 * Called after changing "compl_leader".
3456 * Show the popup menu with a different set of matches.
3457 * May also search for matches again if the previous search was interrupted.
3458 */
3459 static void
3460ins_compl_new_leader()
3461{
3462 ins_compl_del_pum();
3463 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003464 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003465 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003466
3467 if (compl_started)
3468 ins_compl_set_original_text(compl_leader);
3469 else
3470 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003471#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003472 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003473#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003474 /*
3475 * Matches were cleared, need to search for them now. First display
3476 * the changed text before the cursor. Set "compl_restarting" to
3477 * avoid that the first match is inserted.
3478 */
3479 update_screen(0);
3480#ifdef FEAT_GUI
3481 if (gui.in_use)
3482 {
3483 /* Show the cursor after the match, not after the redrawn text. */
3484 setcursor();
3485 out_flush();
3486 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003487 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003488#endif
3489 compl_restarting = TRUE;
3490 if (ins_complete(Ctrl_N) == FAIL)
3491 compl_cont_status = 0;
3492 compl_restarting = FALSE;
3493 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003494
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003495 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003496
3497 /* Show the popup menu with a different set of matches. */
3498 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003499
3500 /* Don't let Enter select the original text when there is no popup menu. */
3501 if (compl_match_array == NULL)
3502 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003503}
3504
3505/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003506 * Return the length of the completion, from the completion start column to
3507 * the cursor column. Making sure it never goes below zero.
3508 */
3509 static int
3510ins_compl_len()
3511{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003512 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003513
3514 if (off < 0)
3515 return 0;
3516 return off;
3517}
3518
3519/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003520 * Append one character to the match leader. May reduce the number of
3521 * matches.
3522 */
3523 static void
3524ins_compl_addleader(c)
3525 int c;
3526{
3527#ifdef FEAT_MBYTE
3528 int cc;
3529
3530 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3531 {
3532 char_u buf[MB_MAXBYTES + 1];
3533
3534 (*mb_char2bytes)(c, buf);
3535 buf[cc] = NUL;
3536 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003537 if (compl_opt_refresh_always)
3538 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003539 }
3540 else
3541#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003542 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003543 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003544 if (compl_opt_refresh_always)
3545 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003546 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003547
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003548 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003549 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003550 ins_compl_restart();
3551
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003552 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003553 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003554 * break redo. */
3555 if (!compl_opt_refresh_always)
3556 {
3557 vim_free(compl_leader);
3558 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003559 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003560 if (compl_leader != NULL)
3561 ins_compl_new_leader();
3562 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003563}
3564
3565/*
3566 * Setup for finding completions again without leaving CTRL-X mode. Used when
3567 * BS or a key was typed while still searching for matches.
3568 */
3569 static void
3570ins_compl_restart()
3571{
3572 ins_compl_free();
3573 compl_started = FALSE;
3574 compl_matches = 0;
3575 compl_cont_status = 0;
3576 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003577}
3578
3579/*
3580 * Set the first match, the original text.
3581 */
3582 static void
3583ins_compl_set_original_text(str)
3584 char_u *str;
3585{
3586 char_u *p;
3587
3588 /* Replace the original text entry. */
3589 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3590 {
3591 p = vim_strsave(str);
3592 if (p != NULL)
3593 {
3594 vim_free(compl_first_match->cp_str);
3595 compl_first_match->cp_str = p;
3596 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003597 }
3598}
3599
3600/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003601 * Append one character to the match leader. May reduce the number of
3602 * matches.
3603 */
3604 static void
3605ins_compl_addfrommatch()
3606{
3607 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003608 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003609 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003610 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003611
3612 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003613 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003614 {
3615 /* When still at the original match use the first entry that matches
3616 * the leader. */
3617 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3618 {
3619 p = NULL;
3620 for (cp = compl_shown_match->cp_next; cp != NULL
3621 && cp != compl_first_match; cp = cp->cp_next)
3622 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003623 if (compl_leader == NULL
3624 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003625 (int)STRLEN(compl_leader)))
3626 {
3627 p = cp->cp_str;
3628 break;
3629 }
3630 }
3631 if (p == NULL || (int)STRLEN(p) <= len)
3632 return;
3633 }
3634 else
3635 return;
3636 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003637 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003638 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003639 ins_compl_addleader(c);
3640}
3641
3642/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003644 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003645 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003647 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648ins_compl_prep(c)
3649 int c;
3650{
3651 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003653 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654
3655 /* Forget any previous 'special' messages if this is actually
3656 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3657 */
3658 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3659 edit_submode_extra = NULL;
3660
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003661 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003662 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3663 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003664 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003666 /* Set "compl_get_longest" when finding the first matches. */
3667 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3668 || (ctrl_x_mode == 0 && !compl_started))
3669 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003670 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003671 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003672
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003673 }
3674
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003675 compl_no_insert = FALSE;
3676 compl_no_select = FALSE;
3677 if (strstr((char *)p_cot, "noselect") != NULL)
3678 compl_no_select = TRUE;
3679 if (strstr((char *)p_cot, "noinsert") != NULL)
3680 compl_no_insert = TRUE;
3681
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3683 {
3684 /*
3685 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3686 * it will be yet. Now we decide.
3687 */
3688 switch (c)
3689 {
3690 case Ctrl_E:
3691 case Ctrl_Y:
3692 ctrl_x_mode = CTRL_X_SCROLL;
3693 if (!(State & REPLACE_FLAG))
3694 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3695 else
3696 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3697 edit_submode_pre = NULL;
3698 showmode();
3699 break;
3700 case Ctrl_L:
3701 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3702 break;
3703 case Ctrl_F:
3704 ctrl_x_mode = CTRL_X_FILES;
3705 break;
3706 case Ctrl_K:
3707 ctrl_x_mode = CTRL_X_DICTIONARY;
3708 break;
3709 case Ctrl_R:
3710 /* Simply allow ^R to happen without affecting ^X mode */
3711 break;
3712 case Ctrl_T:
3713 ctrl_x_mode = CTRL_X_THESAURUS;
3714 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003715#ifdef FEAT_COMPL_FUNC
3716 case Ctrl_U:
3717 ctrl_x_mode = CTRL_X_FUNCTION;
3718 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003719 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003720 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003721 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003722#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003723 case 's':
3724 case Ctrl_S:
3725 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003726#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003727 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003728 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003729 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003730#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003731 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 case Ctrl_RSB:
3733 ctrl_x_mode = CTRL_X_TAGS;
3734 break;
3735#ifdef FEAT_FIND_ID
3736 case Ctrl_I:
3737 case K_S_TAB:
3738 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3739 break;
3740 case Ctrl_D:
3741 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3742 break;
3743#endif
3744 case Ctrl_V:
3745 case Ctrl_Q:
3746 ctrl_x_mode = CTRL_X_CMDLINE;
3747 break;
3748 case Ctrl_P:
3749 case Ctrl_N:
3750 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3751 * just started ^X mode, or there were enough ^X's to cancel
3752 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3753 * do normal expansion when interrupting a different mode (say
3754 * ^X^F^X^P or ^P^X^X^P, see below)
3755 * nothing changes if interrupting mode 0, (eg, the flag
3756 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003757 if (!(compl_cont_status & CONT_INTRPT))
3758 compl_cont_status |= CONT_LOCAL;
3759 else if (compl_cont_mode != 0)
3760 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 /* FALLTHROUGH */
3762 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003763 /* If we have typed at least 2 ^X's... for modes != 0, we set
3764 * compl_cont_status = 0 (eg, as if we had just started ^X
3765 * mode).
3766 * For mode 0, we set "compl_cont_mode" to an impossible
3767 * value, in both cases ^X^X can be used to restart the same
3768 * mode (avoiding ADDING mode).
3769 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3770 * 'complete' and local ^P expansions respectively.
3771 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3772 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 if (c == Ctrl_X)
3774 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003775 if (compl_cont_mode != 0)
3776 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003777 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003778 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780 ctrl_x_mode = 0;
3781 edit_submode = NULL;
3782 showmode();
3783 break;
3784 }
3785 }
3786 else if (ctrl_x_mode != 0)
3787 {
3788 /* We're already in CTRL-X mode, do we stay in it? */
3789 if (!vim_is_ctrl_x_key(c))
3790 {
3791 if (ctrl_x_mode == CTRL_X_SCROLL)
3792 ctrl_x_mode = 0;
3793 else
3794 ctrl_x_mode = CTRL_X_FINISHED;
3795 edit_submode = NULL;
3796 }
3797 showmode();
3798 }
3799
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003800 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003801 {
3802 /* Show error message from attempted keyword completion (probably
3803 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003804 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003806 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3807 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 || ctrl_x_mode == CTRL_X_FINISHED)
3809 {
3810 /* Get here when we have finished typing a sequence of ^N and
3811 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003812 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003813 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003814 {
3815 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003816 * If any of the original typed text has been changed, eg when
3817 * ignorecase is set, we must add back-spaces to the redo
3818 * buffer. We add as few as necessary to delete just the part
3819 * of the original text that has changed.
3820 * When using the longest match, edited the match or used
3821 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003823 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3824 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003825 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003826 ptr = NULL;
3827 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829
3830#ifdef FEAT_CINDENT
3831 want_cindent = (can_cindent && cindent_on());
3832#endif
3833 /*
3834 * When completing whole lines: fix indent for 'cindent'.
3835 * Otherwise, break line if it's too long.
3836 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003837 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 {
3839#ifdef FEAT_CINDENT
3840 /* re-indent the current line */
3841 if (want_cindent)
3842 {
3843 do_c_expr_indent();
3844 want_cindent = FALSE; /* don't do it again */
3845 }
3846#endif
3847 }
3848 else
3849 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003850 int prev_col = curwin->w_cursor.col;
3851
Bram Moolenaar071d4272004-06-13 20:20:40 +00003852 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003853 if (prev_col > 0)
3854 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003855 if (stop_arrow() == OK)
3856 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003857 if (prev_col > 0
3858 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3859 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 }
3861
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003862 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003863 * the selection without inserting anything. When
3864 * compl_enter_selects is set the Enter key does the same. */
3865 if ((c == Ctrl_Y || (compl_enter_selects
3866 && (c == CAR || c == K_KENTER || c == NL)))
3867 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003868 retval = TRUE;
3869
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003870 /* CTRL-E means completion is Ended, go back to the typed text. */
3871 if (c == Ctrl_E)
3872 {
3873 ins_compl_delete();
3874 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003875 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003876 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003877 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003878 retval = TRUE;
3879 }
3880
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003881 auto_format(FALSE, TRUE);
3882
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003884 compl_started = FALSE;
3885 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003886 if (!shortmess(SHM_COMPLETIONMENU))
3887 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003889 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 if (edit_submode != NULL)
3891 {
3892 edit_submode = NULL;
3893 showmode();
3894 }
3895
3896#ifdef FEAT_CINDENT
3897 /*
3898 * Indent now if a key was typed that is in 'cinkeys'.
3899 */
3900 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3901 do_c_expr_indent();
3902#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003903#ifdef FEAT_AUTOCMD
3904 /* Trigger the CompleteDone event to give scripts a chance to act
3905 * upon the completion. */
3906 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3907#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 }
3909 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003910#ifdef FEAT_AUTOCMD
3911 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3912 /* Trigger the CompleteDone event to give scripts a chance to act
3913 * upon the (possibly failed) completion. */
3914 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916
3917 /* reset continue_* if we left expansion-mode, if we stay they'll be
3918 * (re)set properly in ins_complete() */
3919 if (!vim_is_ctrl_x_key(c))
3920 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 compl_cont_status = 0;
3922 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003924
3925 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926}
3927
3928/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003929 * Fix the redo buffer for the completion leader replacing some of the typed
3930 * text. This inserts backspaces and appends the changed text.
3931 * "ptr" is the known leader text or NUL.
3932 */
3933 static void
3934ins_compl_fixRedoBufForLeader(ptr_arg)
3935 char_u *ptr_arg;
3936{
3937 int len;
3938 char_u *p;
3939 char_u *ptr = ptr_arg;
3940
3941 if (ptr == NULL)
3942 {
3943 if (compl_leader != NULL)
3944 ptr = compl_leader;
3945 else
3946 return; /* nothing to do */
3947 }
3948 if (compl_orig_text != NULL)
3949 {
3950 p = compl_orig_text;
3951 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3952 ;
3953#ifdef FEAT_MBYTE
3954 if (len > 0)
3955 len -= (*mb_head_off)(p, p + len);
3956#endif
3957 for (p += len; *p != NUL; mb_ptr_adv(p))
3958 AppendCharToRedobuff(K_BS);
3959 }
3960 else
3961 len = 0;
3962 if (ptr != NULL)
3963 AppendToRedobuffLit(ptr + len, -1);
3964}
3965
3966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3968 * (depending on flag) starting from buf and looking for a non-scanned
3969 * buffer (other than curbuf). curbuf is special, if it is called with
3970 * buf=curbuf then it has to be the first call for a given flag/expansion.
3971 *
3972 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3973 */
3974 static buf_T *
3975ins_compl_next_buf(buf, flag)
3976 buf_T *buf;
3977 int flag;
3978{
3979#ifdef FEAT_WINDOWS
3980 static win_T *wp;
3981#endif
3982
3983 if (flag == 'w') /* just windows */
3984 {
3985#ifdef FEAT_WINDOWS
3986 if (buf == curbuf) /* first call for this flag/expansion */
3987 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003988 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989 && wp->w_buffer->b_scanned)
3990 ;
3991 buf = wp->w_buffer;
3992#else
3993 buf = curbuf;
3994#endif
3995 }
3996 else
3997 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3998 * (unlisted buffers)
3999 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004000 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004001 && ((flag == 'U'
4002 ? buf->b_p_bl
4003 : (!buf->b_p_bl
4004 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004005 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 ;
4007 return buf;
4008}
4009
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004010#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004011static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004012
4013/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004014 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004015 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004016 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004017 static void
4018expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004019 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004020 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004021{
Bram Moolenaar82139082011-09-14 16:52:09 +02004022 list_T *matchlist = NULL;
4023 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004024 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004025 char_u *funcname;
4026 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004027 win_T *curwin_save;
4028 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004029 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004030
Bram Moolenaare344bea2005-09-01 20:46:49 +00004031 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4032 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004033 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004034
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004035 /* Call 'completefunc' to obtain the list of matches. */
4036 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004037 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004038
Bram Moolenaare344bea2005-09-01 20:46:49 +00004039 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004040 curwin_save = curwin;
4041 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004042
4043 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004044 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004045 {
4046 switch (rettv.v_type)
4047 {
4048 case VAR_LIST:
4049 matchlist = rettv.vval.v_list;
4050 break;
4051 case VAR_DICT:
4052 matchdict = rettv.vval.v_dict;
4053 break;
4054 default:
4055 /* TODO: Give error message? */
4056 clear_tv(&rettv);
4057 break;
4058 }
4059 }
4060
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004061 if (curwin_save != curwin || curbuf_save != curbuf)
4062 {
4063 EMSG(_(e_complwin));
4064 goto theend;
4065 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004066 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004067 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004068 if (!equalpos(curwin->w_cursor, pos))
4069 {
4070 EMSG(_(e_compldel));
4071 goto theend;
4072 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004073
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004074 if (matchlist != NULL)
4075 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004076 else if (matchdict != NULL)
4077 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004078
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004079theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004080 if (matchdict != NULL)
4081 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004082 if (matchlist != NULL)
4083 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004084}
4085#endif /* FEAT_COMPL_FUNC */
4086
Bram Moolenaar39f05632006-03-19 22:15:26 +00004087#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004088/*
4089 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004090 */
4091 static void
4092ins_compl_add_list(list)
4093 list_T *list;
4094{
4095 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004096 int dir = compl_direction;
4097
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004098 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004099 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004100 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004101 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4102 /* if dir was BACKWARD then honor it just once */
4103 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004104 else if (did_emsg)
4105 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004106 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004107}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004108
4109/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004110 * Add completions from a dict.
4111 */
4112 static void
4113ins_compl_add_dict(dict)
4114 dict_T *dict;
4115{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004116 dictitem_T *di_refresh;
4117 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004118
4119 /* Check for optional "refresh" item. */
4120 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004121 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4122 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004123 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004124 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004125
4126 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4127 compl_opt_refresh_always = TRUE;
4128 }
4129
4130 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004131 di_words = dict_find(dict, (char_u *)"words", 5);
4132 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4133 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004134}
4135
4136/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004137 * Add a match to the list of matches from a typeval_T.
4138 * If the given string is already in the list of completions, then return
4139 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4140 * maybe because alloc() returns NULL, then FAIL is returned.
4141 */
4142 int
4143ins_compl_add_tv(tv, dir)
4144 typval_T *tv;
4145 int dir;
4146{
4147 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004148 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004149 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004150 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004151 char_u *(cptext[CPT_COUNT]);
4152
4153 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4154 {
4155 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4156 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4157 (char_u *)"abbr", FALSE);
4158 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4159 (char_u *)"menu", FALSE);
4160 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4161 (char_u *)"kind", FALSE);
4162 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4163 (char_u *)"info", FALSE);
4164 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4165 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004166 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004167 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004168 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4169 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004170 }
4171 else
4172 {
4173 word = get_tv_string_chk(tv);
4174 vim_memset(cptext, 0, sizeof(cptext));
4175 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004176 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004177 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004178 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004179}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004180#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004181
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004182/*
4183 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004184 * The search starts at position "ini" in curbuf and in the direction
4185 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004186 * When "compl_started" is FALSE start at that position, otherwise continue
4187 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004188 * This may return before finding all the matches.
4189 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 */
4191 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004192ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194{
4195 static pos_T first_match_pos;
4196 static pos_T last_match_pos;
4197 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004198 static int found_all = FALSE; /* Found all matches of a
4199 certain type. */
4200 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004201
Bram Moolenaar572cb562005-08-05 21:35:02 +00004202 pos_T *pos;
4203 char_u **matches;
4204 int save_p_scs;
4205 int save_p_ws;
4206 int save_p_ic;
4207 int i;
4208 int num_matches;
4209 int len;
4210 int found_new_match;
4211 int type = ctrl_x_mode;
4212 char_u *ptr;
4213 char_u *dict = NULL;
4214 int dict_f = 0;
4215 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004216 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004218 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 {
4220 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4221 ins_buf->b_scanned = 0;
4222 found_all = FALSE;
4223 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004224 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004225 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 last_match_pos = first_match_pos = *ini;
4227 }
4228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004229 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004230 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4232 for (;;)
4233 {
4234 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004235 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004237 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 * or if found_all says this entry is done. For ^X^L only use the
4239 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004240 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004241 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 {
4243 found_all = FALSE;
4244 while (*e_cpt == ',' || *e_cpt == ' ')
4245 e_cpt++;
4246 if (*e_cpt == '.' && !curbuf->b_scanned)
4247 {
4248 ins_buf = curbuf;
4249 first_match_pos = *ini;
4250 /* So that ^N can match word immediately after cursor */
4251 if (ctrl_x_mode == 0)
4252 dec(&first_match_pos);
4253 last_match_pos = first_match_pos;
4254 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004255
4256 /* Remember the first match so that the loop stops when we
4257 * wrap and come back there a second time. */
4258 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4261 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4262 {
4263 /* Scan a buffer, but not the current one. */
4264 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4265 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004266 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 first_match_pos.col = last_match_pos.col = 0;
4268 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4269 last_match_pos.lnum = 0;
4270 type = 0;
4271 }
4272 else /* unloaded buffer, scan like dictionary */
4273 {
4274 found_all = TRUE;
4275 if (ins_buf->b_fname == NULL)
4276 continue;
4277 type = CTRL_X_DICTIONARY;
4278 dict = ins_buf->b_fname;
4279 dict_f = DICT_EXACT;
4280 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004281 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 ins_buf->b_fname == NULL
4283 ? buf_spname(ins_buf)
4284 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004285 ? ins_buf->b_fname
4286 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004287 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 }
4289 else if (*e_cpt == NUL)
4290 break;
4291 else
4292 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004293 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294 type = -1;
4295 else if (*e_cpt == 'k' || *e_cpt == 's')
4296 {
4297 if (*e_cpt == 'k')
4298 type = CTRL_X_DICTIONARY;
4299 else
4300 type = CTRL_X_THESAURUS;
4301 if (*++e_cpt != ',' && *e_cpt != NUL)
4302 {
4303 dict = e_cpt;
4304 dict_f = DICT_FIRST;
4305 }
4306 }
4307#ifdef FEAT_FIND_ID
4308 else if (*e_cpt == 'i')
4309 type = CTRL_X_PATH_PATTERNS;
4310 else if (*e_cpt == 'd')
4311 type = CTRL_X_PATH_DEFINES;
4312#endif
4313 else if (*e_cpt == ']' || *e_cpt == 't')
4314 {
4315 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004316 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004317 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 }
4319 else
4320 type = -1;
4321
4322 /* in any case e_cpt is advanced to the next entry */
4323 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4324
4325 found_all = TRUE;
4326 if (type == -1)
4327 continue;
4328 }
4329 }
4330
4331 switch (type)
4332 {
4333 case -1:
4334 break;
4335#ifdef FEAT_FIND_ID
4336 case CTRL_X_PATH_PATTERNS:
4337 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004338 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004339 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004341 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4343 (linenr_T)1, (linenr_T)MAXLNUM);
4344 break;
4345#endif
4346
4347 case CTRL_X_DICTIONARY:
4348 case CTRL_X_THESAURUS:
4349 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004350 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 : (type == CTRL_X_THESAURUS
4352 ? (*curbuf->b_p_tsr == NUL
4353 ? p_tsr
4354 : curbuf->b_p_tsr)
4355 : (*curbuf->b_p_dict == NUL
4356 ? p_dict
4357 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004358 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004359 dict != NULL ? dict_f
4360 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 dict = NULL;
4362 break;
4363
4364 case CTRL_X_TAGS:
4365 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4366 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004367 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004369 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004370 * of matches is found when compl_pattern is empty */
4371 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4373 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4374 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4375 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004376 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 }
4378 p_ic = save_p_ic;
4379 break;
4380
4381 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4384 {
4385
4386 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004388 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 }
4390 break;
4391
4392 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393 if (expand_cmdline(&compl_xp, compl_pattern,
4394 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004396 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 break;
4398
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004399#ifdef FEAT_COMPL_FUNC
4400 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004401 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004402 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004403 break;
4404#endif
4405
Bram Moolenaar488c6512005-08-11 20:09:58 +00004406 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004407#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004408 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004409 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004410 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004411 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004412#endif
4413 break;
4414
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 default: /* normal ^P/^N and ^X^L */
4416 /*
4417 * If 'infercase' is set, don't use 'smartcase' here
4418 */
4419 save_p_scs = p_scs;
4420 if (ins_buf->b_p_inf)
4421 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004422
Bram Moolenaar361aa502014-01-23 22:45:58 +01004423 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 * end but never from the middle, thus setting nowrapscan in this
4425 * buffers is a good idea, on the other hand, we always set
4426 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4427 save_p_ws = p_ws;
4428 if (ins_buf != curbuf)
4429 p_ws = FALSE;
4430 else if (*e_cpt == '.')
4431 p_ws = TRUE;
4432 for (;;)
4433 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004434 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004436 ++msg_silent; /* Don't want messages for wrapscan. */
4437
Bram Moolenaare4214502015-03-05 18:08:43 +01004438 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4439 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004440 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004441 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004442 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004444 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004446 found_new_match = searchit(NULL, ins_buf, pos,
4447 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004448 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004449 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004450 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004451 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004453 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 first_match_pos = *pos;
4456 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004457 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 }
4459 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004460 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 found_new_match = FAIL;
4462 if (found_new_match == FAIL)
4463 {
4464 if (ins_buf == curbuf)
4465 found_all = TRUE;
4466 break;
4467 }
4468
4469 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 && ini->lnum == pos->lnum
4472 && ini->col == pos->col)
4473 continue;
4474 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004475 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004477 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
4479 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4480 continue;
4481 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4482 if (!p_paste)
4483 ptr = skipwhite(ptr);
4484 }
4485 len = (int)STRLEN(ptr);
4486 }
4487 else
4488 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004489 char_u *tmp_ptr = ptr;
4490
4491 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004493 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 /* Skip if already inside a word. */
4495 if (vim_iswordp(tmp_ptr))
4496 continue;
4497 /* Find start of next word. */
4498 tmp_ptr = find_word_start(tmp_ptr);
4499 }
4500 /* Find end of this word. */
4501 tmp_ptr = find_word_end(tmp_ptr);
4502 len = (int)(tmp_ptr - ptr);
4503
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504 if ((compl_cont_status & CONT_ADDING)
4505 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 {
4507 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4508 {
4509 /* Try next line, if any. the new word will be
4510 * "join" as if the normal command "J" was used.
4511 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 * works -- Acevedo */
4514 STRNCPY(IObuff, ptr, len);
4515 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4516 tmp_ptr = ptr = skipwhite(ptr);
4517 /* Find start of next word. */
4518 tmp_ptr = find_word_start(tmp_ptr);
4519 /* Find end of next word. */
4520 tmp_ptr = find_word_end(tmp_ptr);
4521 if (tmp_ptr > ptr)
4522 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004523 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004525 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 IObuff[len++] = ' ';
4527 /* IObuf =~ "\k.* ", thus len >= 2 */
4528 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004529 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004530 || (vim_strchr(p_cpo, CPO_JOINSP)
4531 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004532 && (IObuff[len - 2] == '?'
4533 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 IObuff[len++] = ' ';
4535 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004536 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537 if (tmp_ptr - ptr >= IOSIZE - len)
4538 tmp_ptr = ptr + IOSIZE - len - 1;
4539 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4540 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004541 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 }
4543 IObuff[len] = NUL;
4544 ptr = IObuff;
4545 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004546 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 continue;
4548 }
4549 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004550 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004551 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004552 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 {
4554 found_new_match = OK;
4555 break;
4556 }
4557 }
4558 p_scs = save_p_scs;
4559 p_ws = save_p_ws;
4560 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004561
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004563 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004564 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 found_new_match = OK;
4566
4567 /* break the loop for specialized modes (use 'complete' just for the
4568 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004569 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004571 {
4572 if (got_int)
4573 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004574 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004575 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004576 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004577
Bram Moolenaare4214502015-03-05 18:08:43 +01004578 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004579 || compl_interrupted)
4580 break;
4581 compl_started = TRUE;
4582 }
4583 else
4584 {
4585 /* Mark a buffer scanned when it has been scanned completely */
4586 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4587 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004589 compl_started = FALSE;
4590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004592 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
Bram Moolenaare4214502015-03-05 18:08:43 +01004594 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 && *e_cpt == NUL) /* Got to end of 'complete' */
4596 found_new_match = FAIL;
4597
4598 i = -1; /* total of matches, unknown */
4599 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004600 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 i = ins_compl_make_cyclic();
4602
4603 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004604 * just been made cyclic then we have to move compl_curr_match to the next
4605 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004606 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4607 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004608 if (compl_curr_match == NULL)
4609 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610 return i;
4611}
4612
4613/* Delete the old text being completed. */
4614 static void
4615ins_compl_delete()
4616{
4617 int i;
4618
4619 /*
4620 * In insert mode: Delete the typed part.
4621 * In replace mode: Put the old characters back, if any.
4622 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004623 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 backspace_until_column(i);
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004625
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004626 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4627 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004628 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004629 /* clear v:completed_item */
4630 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631}
4632
4633/* Insert the new text being completed. */
4634 static void
4635ins_compl_insert()
4636{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004637 dict_T *dict;
4638
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004639 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004640 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4641 compl_used_match = FALSE;
4642 else
4643 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004644
4645 /* Set completed item. */
4646 /* { word, abbr, menu, kind, info } */
4647 dict = dict_alloc();
4648 if (dict != NULL)
4649 {
4650 dict_add_nr_str(dict, "word", 0L,
4651 EMPTY_IF_NULL(compl_shown_match->cp_str));
4652 dict_add_nr_str(dict, "abbr", 0L,
4653 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4654 dict_add_nr_str(dict, "menu", 0L,
4655 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4656 dict_add_nr_str(dict, "kind", 0L,
4657 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4658 dict_add_nr_str(dict, "info", 0L,
4659 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4660 }
4661 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662}
4663
4664/*
4665 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004666 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4667 * get more completions. If it is FALSE, then we just do nothing when there
4668 * are no more completions in a given direction. The latter case is used when
4669 * we are still in the middle of finding completions, to allow browsing
4670 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004671 * Return the total number of matches, or -1 if still unknown -- webb.
4672 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004673 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4674 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 *
4676 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004677 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4678 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004679 */
4680 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004681ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004683 int count; /* repeat completion this many times; should
4684 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004685 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686{
4687 int num_matches = -1;
4688 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004689 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004690 compl_T *found_compl = NULL;
4691 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004692 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004693 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004695 /* When user complete function return -1 for findstart which is next
4696 * time of 'always', compl_shown_match become NULL. */
4697 if (compl_shown_match == NULL)
4698 return -1;
4699
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004700 if (compl_leader != NULL
4701 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004703 /* Set "compl_shown_match" to the actually shown match, it may differ
4704 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004705 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004706 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004707 && compl_shown_match->cp_next != NULL
4708 && compl_shown_match->cp_next != compl_first_match)
4709 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004710
4711 /* If we didn't find it searching forward, and compl_shows_dir is
4712 * backward, find the last match. */
4713 if (compl_shows_dir == BACKWARD
4714 && !ins_compl_equal(compl_shown_match,
4715 compl_leader, (int)STRLEN(compl_leader))
4716 && (compl_shown_match->cp_next == NULL
4717 || compl_shown_match->cp_next == compl_first_match))
4718 {
4719 while (!ins_compl_equal(compl_shown_match,
4720 compl_leader, (int)STRLEN(compl_leader))
4721 && compl_shown_match->cp_prev != NULL
4722 && compl_shown_match->cp_prev != compl_first_match)
4723 compl_shown_match = compl_shown_match->cp_prev;
4724 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004725 }
4726
4727 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004728 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 /* Delete old text to be replaced */
4730 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004731
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004732 /* When finding the longest common text we stick at the original text,
4733 * don't let CTRL-N or CTRL-P move to the first match. */
4734 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4735
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004736 /* When restarting the search don't insert the first match either. */
4737 if (compl_restarting)
4738 {
4739 advance = FALSE;
4740 compl_restarting = FALSE;
4741 }
4742
Bram Moolenaare3226be2005-12-18 22:10:00 +00004743 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4744 * around. */
4745 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004747 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004749 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004750 found_end = (compl_first_match != NULL
4751 && (compl_shown_match->cp_next == compl_first_match
4752 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004753 }
4754 else if (compl_shows_dir == BACKWARD
4755 && compl_shown_match->cp_prev != NULL)
4756 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004757 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004758 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004759 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004762 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004763 if (!allow_get_expansion)
4764 {
4765 if (advance)
4766 {
4767 if (compl_shows_dir == BACKWARD)
4768 compl_pending -= todo + 1;
4769 else
4770 compl_pending += todo + 1;
4771 }
4772 return -1;
4773 }
4774
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004775 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004776 {
4777 if (compl_shows_dir == BACKWARD)
4778 --compl_pending;
4779 else
4780 ++compl_pending;
4781 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004782
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004783 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004784 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004785
4786 /* handle any pending completions */
4787 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004788 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004789 {
4790 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4791 {
4792 compl_shown_match = compl_shown_match->cp_next;
4793 --compl_pending;
4794 }
4795 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4796 {
4797 compl_shown_match = compl_shown_match->cp_prev;
4798 ++compl_pending;
4799 }
4800 else
4801 break;
4802 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004803 found_end = FALSE;
4804 }
4805 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4806 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004807 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004808 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004809 ++todo;
4810 else
4811 /* Remember a matching item. */
4812 found_compl = compl_shown_match;
4813
4814 /* Stop at the end of the list when we found a usable match. */
4815 if (found_end)
4816 {
4817 if (found_compl != NULL)
4818 {
4819 compl_shown_match = found_compl;
4820 break;
4821 }
4822 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 }
4825
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004826 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004827 if (compl_no_insert && !started)
4828 {
4829 ins_bytes(compl_orig_text + ins_compl_len());
4830 compl_used_match = FALSE;
4831 }
4832 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004833 {
4834 if (!compl_get_longest || compl_used_match)
4835 ins_compl_insert();
4836 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004837 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004838 }
4839 else
4840 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841
4842 if (!allow_get_expansion)
4843 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004844 /* may undisplay the popup menu first */
4845 ins_compl_upd_pum();
4846
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004847 /* redraw to show the user what was inserted */
4848 update_screen(0);
4849
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004850 /* display the updated popup menu */
4851 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004852#ifdef FEAT_GUI
4853 if (gui.in_use)
4854 {
4855 /* Show the cursor after the match, not after the redrawn text. */
4856 setcursor();
4857 out_flush();
4858 gui_update_cursor(FALSE, FALSE);
4859 }
4860#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004861
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 /* Delete old text to be replaced, since we're still searching and
4863 * don't want to match ourselves! */
4864 ins_compl_delete();
4865 }
4866
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004867 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004868 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004869 if (compl_no_insert && !started)
4870 compl_enter_selects = TRUE;
4871 else
4872 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004873
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 /*
4875 * Show the file name for the match (if any)
4876 * Truncate the file name to avoid a wait for return.
4877 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004878 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 {
4880 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004881 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 if (i <= 0)
4883 i = 0;
4884 else
4885 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004886 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 msg(IObuff);
4888 redraw_cmdline = FALSE; /* don't overwrite! */
4889 }
4890
4891 return num_matches;
4892}
4893
4894/*
4895 * Call this while finding completions, to check whether the user has hit a key
4896 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004897 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004899 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 */
4901 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004902ins_compl_check_keys(frequency)
4903 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904{
4905 static int count = 0;
4906
4907 int c;
4908
4909 /* Don't check when reading keys from a script. That would break the test
4910 * scripts */
4911 if (using_script())
4912 return;
4913
4914 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004915 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 return;
4917 count = 0;
4918
Bram Moolenaara260a972006-06-23 19:36:29 +00004919 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4920 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 if (c != NUL)
4923 {
4924 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4925 {
4926 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004927 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004928 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4929 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004931 else
4932 {
4933 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004934 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004935 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004936 if (c != K_IGNORE)
4937 {
4938 /* Don't interrupt completion when the character wasn't typed,
4939 * e.g., when doing @q to replay keys. */
4940 if (c != Ctrl_R && KeyTyped)
4941 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004942
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004943 vungetc(c);
4944 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004945 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004947 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004948 {
4949 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4950
4951 compl_pending = 0;
4952 (void)ins_compl_next(FALSE, todo, TRUE);
4953 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004954}
4955
4956/*
4957 * Decide the direction of Insert mode complete from the key typed.
4958 * Returns BACKWARD or FORWARD.
4959 */
4960 static int
4961ins_compl_key2dir(c)
4962 int c;
4963{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004964 if (c == Ctrl_P || c == Ctrl_L
4965 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4966 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004967 return BACKWARD;
4968 return FORWARD;
4969}
4970
4971/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004972 * Return TRUE for keys that are used for completion only when the popup menu
4973 * is visible.
4974 */
4975 static int
4976ins_compl_pum_key(c)
4977 int c;
4978{
4979 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004980 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4981 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004982}
4983
4984/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004985 * Decide the number of completions to move forward.
4986 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4987 */
4988 static int
4989ins_compl_key2count(c)
4990 int c;
4991{
4992 int h;
4993
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004994 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004995 {
4996 h = pum_get_height();
4997 if (h > 3)
4998 h -= 2; /* keep some context */
4999 return h;
5000 }
5001 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002}
5003
5004/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005005 * Return TRUE if completion with "c" should insert the match, FALSE if only
5006 * to change the currently selected completion.
5007 */
5008 static int
5009ins_compl_use_match(c)
5010 int c;
5011{
5012 switch (c)
5013 {
5014 case K_UP:
5015 case K_DOWN:
5016 case K_PAGEDOWN:
5017 case K_KPAGEDOWN:
5018 case K_S_DOWN:
5019 case K_PAGEUP:
5020 case K_KPAGEUP:
5021 case K_S_UP:
5022 return FALSE;
5023 }
5024 return TRUE;
5025}
5026
5027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 * Do Insert mode completion.
5029 * Called when character "c" was typed, which has a meaning for completion.
5030 * Returns OK if completion was done, FAIL if something failed (out of mem).
5031 */
5032 static int
5033ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005034 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005036 char_u *line;
5037 int startcol = 0; /* column where searched text starts */
5038 colnr_T curs_col; /* cursor column */
5039 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005040 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041
Bram Moolenaare3226be2005-12-18 22:10:00 +00005042 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005043 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 {
5045 /* First time we hit ^N or ^P (in a row, I mean) */
5046
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 did_ai = FALSE;
5048#ifdef FEAT_SMARTINDENT
5049 did_si = FALSE;
5050 can_si = FALSE;
5051 can_si_back = FALSE;
5052#endif
5053 if (stop_arrow() == FAIL)
5054 return FAIL;
5055
5056 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005057 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005058 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005060 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005061 * "compl_startpos" to the cursor as a pattern to add a new word
5062 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005063 * "compl_startpos" is not in the same line as the cursor then fix it
5064 * (the line has been split because it was longer than 'tw'). if SOL
5065 * is set then skip the previous pattern, a word at the beginning of
5066 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005067 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5068 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 {
5070 /*
5071 * it is a continued search
5072 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005073 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5075 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5076 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005077 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005079 /* line (probably) wrapped, set compl_startpos to the
5080 * first non_blank in the line, if it is not a wordchar
5081 * include it to get a better pattern, but then we don't
5082 * want the "\\<" prefix, check it bellow */
5083 compl_col = (colnr_T)(skipwhite(line) - line);
5084 compl_startpos.col = compl_col;
5085 compl_startpos.lnum = curwin->w_cursor.lnum;
5086 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 }
5088 else
5089 {
5090 /* S_IPOS was set when we inserted a word that was at the
5091 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005092 * mode but first we need to redefine compl_startpos */
5093 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 compl_cont_status |= CONT_SOL;
5096 compl_startpos.col = (colnr_T)(skipwhite(
5097 line + compl_length
5098 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005100 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005103 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005104 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005106 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005108 compl_cont_status &= ~CONT_SOL;
5109 compl_length = (IOSIZE - MIN_SPACE);
5110 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5113 if (compl_length < 1)
5114 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005116 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005117 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005119 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005122 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005124 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005126 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005128 compl_cont_status = 0;
5129 compl_cont_status |= CONT_N_ADDS;
5130 compl_startpos = curwin->w_cursor;
5131 startcol = (int)curs_col;
5132 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134
5135 /* Work out completion pattern and original text -- webb */
5136 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5137 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005138 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005143 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005145 compl_col += ++startcol;
5146 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 }
5148 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005149 compl_pattern = str_foldcase(line + compl_col,
5150 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 compl_pattern = vim_strnsave(line + compl_col,
5153 compl_length);
5154 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 return FAIL;
5156 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
5159 char_u *prefix = (char_u *)"\\<";
5160
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005161 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005162 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005163 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005164 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005166 if (!vim_iswordp(line + compl_col)
5167 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 && (
5169#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005170 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005172 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173#endif
5174 )))
5175 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 STRCPY((char *)compl_pattern, prefix);
5177 (void)quote_meta(compl_pattern + STRLEN(prefix),
5178 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005182 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005184 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185#endif
5186 )
5187 {
5188 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5190 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005192 compl_col += curs_col;
5193 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 }
5195 else
5196 {
5197#ifdef FEAT_MBYTE
5198 /* Search the point of change class of multibyte character
5199 * or not a word single byte character backward. */
5200 if (has_mbyte)
5201 {
5202 int base_class;
5203 int head_off;
5204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005205 startcol -= (*mb_head_off)(line, line + startcol);
5206 base_class = mb_get_class(line + startcol);
5207 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005209 head_off = (*mb_head_off)(line, line + startcol);
5210 if (base_class != mb_get_class(line + startcol
5211 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005213 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 }
5215 }
5216 else
5217#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005220 compl_col += ++startcol;
5221 compl_length = (int)curs_col - startcol;
5222 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005223 {
5224 /* Only match word with at least two chars -- webb
5225 * there's no need to call quote_meta,
5226 * alloc(7) is enough -- Acevedo
5227 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 compl_pattern = alloc(7);
5229 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005231 STRCPY((char *)compl_pattern, "\\<");
5232 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5233 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 }
5235 else
5236 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005237 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005238 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005239 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 STRCPY((char *)compl_pattern, "\\<");
5242 (void)quote_meta(compl_pattern + 2, line + compl_col,
5243 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245 }
5246 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005247 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005249 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005250 compl_length = (int)curs_col - (int)compl_col;
5251 if (compl_length < 0) /* cursor in indent: empty pattern */
5252 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005254 compl_pattern = str_foldcase(line + compl_col, compl_length,
5255 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005256 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5258 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 return FAIL;
5260 }
5261 else if (ctrl_x_mode == CTRL_X_FILES)
5262 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005263 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005264 if (startcol > 0)
5265 {
5266 char_u *p = line + startcol;
5267
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005268 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005269 while (p > line && vim_isfilec(PTR2CHAR(p)))
5270 mb_ptr_back(line, p);
5271 if (p == line && vim_isfilec(PTR2CHAR(p)))
5272 startcol = 0;
5273 else
5274 startcol = (int)(p - line) + 1;
5275 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005276
Bram Moolenaar0300e462013-09-08 16:03:45 +02005277 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005278 compl_length = (int)curs_col - startcol;
5279 compl_pattern = addstar(line + compl_col, compl_length,
5280 EXPAND_FILES);
5281 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 return FAIL;
5283 }
5284 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5285 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005286 compl_pattern = vim_strnsave(line, curs_col);
5287 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005289 set_cmd_context(&compl_xp, compl_pattern,
5290 (int)STRLEN(compl_pattern), curs_col);
5291 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5292 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005293 /* No completion possible, use an empty pattern to get a
5294 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005295 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005296 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005297 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5298 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005300 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005301 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005302#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005303 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005304 * Call user defined function 'completefunc' with "a:findstart"
5305 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005306 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005307 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005308 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005309 char_u *funcname;
5310 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005311 win_T *curwin_save;
5312 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005313
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005314 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005315 * string */
5316 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5317 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5318 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005319 {
5320 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5321 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005322 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005323 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005324
5325 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005326 args[1] = NULL;
5327 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005328 curwin_save = curwin;
5329 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005330 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005331 if (curwin_save != curwin || curbuf_save != curbuf)
5332 {
5333 EMSG(_(e_complwin));
5334 return FAIL;
5335 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005336 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005337 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005338 if (!equalpos(curwin->w_cursor, pos))
5339 {
5340 EMSG(_(e_compldel));
5341 return FAIL;
5342 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005343
Bram Moolenaar6110a002012-01-26 18:58:38 +01005344 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005345 * cancel the complete without an error.
5346 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005347 if (col == -2)
5348 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005349 if (col == -3)
5350 {
5351 ctrl_x_mode = 0;
5352 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005353 if (!shortmess(SHM_COMPLETIONMENU))
5354 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005355 return FAIL;
5356 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005357
Bram Moolenaar82139082011-09-14 16:52:09 +02005358 /*
5359 * Reset extended parameters of completion, when start new
5360 * completion.
5361 */
5362 compl_opt_refresh_always = FALSE;
5363
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005364 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005365 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005366 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005367 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005368 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005369
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005370 /* Setup variables for completion. Need to obtain "line" again,
5371 * it may have become invalid. */
5372 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005373 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005374 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5375 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005376#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005377 return FAIL;
5378 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005379 else if (ctrl_x_mode == CTRL_X_SPELL)
5380 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005381#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005382 if (spell_bad_len > 0)
5383 compl_col = curs_col - spell_bad_len;
5384 else
5385 compl_col = spell_word_start(startcol);
5386 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005387 {
5388 compl_length = 0;
5389 compl_col = curs_col;
5390 }
5391 else
5392 {
5393 spell_expand_check_cap(compl_col);
5394 compl_length = (int)curs_col - compl_col;
5395 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005396 /* Need to obtain "line" again, it may have become invalid. */
5397 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005398 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5399 if (compl_pattern == NULL)
5400#endif
5401 return FAIL;
5402 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 else
5404 {
5405 EMSG2(_(e_intern2), "ins_complete()");
5406 return FAIL;
5407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 {
5411 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005412 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413 {
5414 /* Insert a new line, keep indentation but ignore 'comments' */
5415#ifdef FEAT_COMMENTS
5416 char_u *old = curbuf->b_p_com;
5417
5418 curbuf->b_p_com = (char_u *)"";
5419#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005420 compl_startpos.lnum = curwin->w_cursor.lnum;
5421 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 ins_eol('\r');
5423#ifdef FEAT_COMMENTS
5424 curbuf->b_p_com = old;
5425#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005426 compl_length = 0;
5427 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 }
5429 }
5430 else
5431 {
5432 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005433 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 }
5435
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 if (compl_cont_status & CONT_LOCAL)
5437 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 else
5439 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5440
Bram Moolenaar62951b12011-09-21 18:23:05 +02005441 /* If any of the original typed text has been changed we need to fix
5442 * the redo buffer. */
5443 ins_compl_fixRedoBufForLeader(NULL);
5444
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005445 /* Always add completion for the original text. */
5446 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005447 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5448 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005449 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005451 vim_free(compl_pattern);
5452 compl_pattern = NULL;
5453 vim_free(compl_orig_text);
5454 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 return FAIL;
5456 }
5457
5458 /* showmode might reset the internal line pointers, so it must
5459 * be called before line = ml_get(), or when this address is no
5460 * longer needed. -- Acevedo.
5461 */
5462 edit_submode_extra = (char_u *)_("-- Searching...");
5463 edit_submode_highl = HLF_COUNT;
5464 showmode();
5465 edit_submode_extra = NULL;
5466 out_flush();
5467 }
5468
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005469 compl_shown_match = compl_curr_match;
5470 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471
5472 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005473 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005475 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005476 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005478 /* may undisplay the popup menu */
5479 ins_compl_upd_pum();
5480
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005481 if (n > 1) /* all matches have been found */
5482 compl_matches = n;
5483 compl_curr_match = compl_shown_match;
5484 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485
Bram Moolenaard68071d2006-05-02 22:08:30 +00005486 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5487 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 if (got_int && !global_busy)
5489 {
5490 (void)vgetc();
5491 got_int = FALSE;
5492 }
5493
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005494 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005495 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005497 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5498 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5500 edit_submode_highl = HLF_E;
5501 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5502 * because we couldn't expand anything at first place, but if we used
5503 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5504 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005505 if ( compl_length > 1
5506 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 || (ctrl_x_mode != 0
5508 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5509 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005510 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 }
5512
Bram Moolenaar572cb562005-08-05 21:35:02 +00005513 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005514 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005516 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517
5518 if (edit_submode_extra == NULL)
5519 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005520 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 {
5522 edit_submode_extra = (char_u *)_("Back at original");
5523 edit_submode_highl = HLF_W;
5524 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005525 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
5527 edit_submode_extra = (char_u *)_("Word from other line");
5528 edit_submode_highl = HLF_COUNT;
5529 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005530 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 edit_submode_extra = (char_u *)_("The only match");
5533 edit_submode_highl = HLF_COUNT;
5534 }
5535 else
5536 {
5537 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005538 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005540 int number = 0;
5541 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005543 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
5545 /* search backwards for the first valid (!= -1) number.
5546 * This should normally succeed already at the first loop
5547 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005548 for (match = compl_curr_match->cp_prev; match != NULL
5549 && match != compl_first_match;
5550 match = match->cp_prev)
5551 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005553 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 break;
5555 }
5556 if (match != NULL)
5557 /* go up and assign all numbers which are not assigned
5558 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005559 for (match = match->cp_next;
5560 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005561 match = match->cp_next)
5562 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563 }
5564 else /* BACKWARD */
5565 {
5566 /* search forwards (upwards) for the first valid (!= -1)
5567 * number. This should normally succeed already at the
5568 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005569 for (match = compl_curr_match->cp_next; match != NULL
5570 && match != compl_first_match;
5571 match = match->cp_next)
5572 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005574 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005575 break;
5576 }
5577 if (match != NULL)
5578 /* go down and assign all numbers which are not
5579 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005580 for (match = match->cp_prev; match
5581 && match->cp_number == -1;
5582 match = match->cp_prev)
5583 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005584 }
5585 }
5586
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005587 /* The match should always have a sequence number now, this is
5588 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005589 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005591 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5592 * Translations may need more than twice that. */
5593 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005595 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005596 vim_snprintf((char *)match_ref, sizeof(match_ref),
5597 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005598 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005600 vim_snprintf((char *)match_ref, sizeof(match_ref),
5601 _("match %d"),
5602 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 edit_submode_extra = match_ref;
5604 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005605 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 curs_columns(FALSE);
5607 }
5608 }
5609 }
5610
5611 /* Show a message about what (completion) mode we're in. */
5612 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005613 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005615 if (edit_submode_extra != NULL)
5616 {
5617 if (!p_smd)
5618 msg_attr(edit_submode_extra,
5619 edit_submode_highl < HLF_COUNT
5620 ? hl_attr(edit_submode_highl) : 0);
5621 }
5622 else
5623 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625
Bram Moolenaard68071d2006-05-02 22:08:30 +00005626 /* Show the popup menu, unless we got interrupted. */
5627 if (!compl_interrupted)
5628 {
5629 /* RedrawingDisabled may be set when invoked through complete(). */
5630 n = RedrawingDisabled;
5631 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005632
5633 /* If the cursor moved we need to remove the pum first. */
5634 setcursor();
5635 if (save_w_wrow != curwin->w_wrow)
5636 ins_compl_del_pum();
5637
Bram Moolenaard68071d2006-05-02 22:08:30 +00005638 ins_compl_show_pum();
5639 setcursor();
5640 RedrawingDisabled = n;
5641 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005642 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005643 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005644
Bram Moolenaar071d4272004-06-13 20:20:40 +00005645 return OK;
5646}
5647
5648/*
5649 * Looks in the first "len" chars. of "src" for search-metachars.
5650 * If dest is not NULL the chars. are copied there quoting (with
5651 * a backslash) the metachars, and dest would be NUL terminated.
5652 * Returns the length (needed) of dest
5653 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005654 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655quote_meta(dest, src, len)
5656 char_u *dest;
5657 char_u *src;
5658 int len;
5659{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005660 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005662 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005663 {
5664 switch (*src)
5665 {
5666 case '.':
5667 case '*':
5668 case '[':
5669 if (ctrl_x_mode == CTRL_X_DICTIONARY
5670 || ctrl_x_mode == CTRL_X_THESAURUS)
5671 break;
5672 case '~':
5673 if (!p_magic) /* quote these only if magic is set */
5674 break;
5675 case '\\':
5676 if (ctrl_x_mode == CTRL_X_DICTIONARY
5677 || ctrl_x_mode == CTRL_X_THESAURUS)
5678 break;
5679 case '^': /* currently it's not needed. */
5680 case '$':
5681 m++;
5682 if (dest != NULL)
5683 *dest++ = '\\';
5684 break;
5685 }
5686 if (dest != NULL)
5687 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005688# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 /* Copy remaining bytes of a multibyte character. */
5690 if (has_mbyte)
5691 {
5692 int i, mb_len;
5693
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005694 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 if (mb_len > 0 && len >= mb_len)
5696 for (i = 0; i < mb_len; ++i)
5697 {
5698 --len;
5699 ++src;
5700 if (dest != NULL)
5701 *dest++ = *src;
5702 }
5703 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005704# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706 if (dest != NULL)
5707 *dest = NUL;
5708
5709 return m;
5710}
5711#endif /* FEAT_INS_EXPAND */
5712
5713/*
5714 * Next character is interpreted literally.
5715 * A one, two or three digit decimal number is interpreted as its byte value.
5716 * If one or two digits are entered, the next character is given to vungetc().
5717 * For Unicode a character > 255 may be returned.
5718 */
5719 int
5720get_literal()
5721{
5722 int cc;
5723 int nc;
5724 int i;
5725 int hex = FALSE;
5726 int octal = FALSE;
5727#ifdef FEAT_MBYTE
5728 int unicode = 0;
5729#endif
5730
5731 if (got_int)
5732 return Ctrl_C;
5733
5734#ifdef FEAT_GUI
5735 /*
5736 * In GUI there is no point inserting the internal code for a special key.
5737 * It is more useful to insert the string "<KEY>" instead. This would
5738 * probably be useful in a text window too, but it would not be
5739 * vi-compatible (maybe there should be an option for it?) -- webb
5740 */
5741 if (gui.in_use)
5742 ++allow_keys;
5743#endif
5744#ifdef USE_ON_FLY_SCROLL
5745 dont_scroll = TRUE; /* disallow scrolling here */
5746#endif
5747 ++no_mapping; /* don't map the next key hits */
5748 cc = 0;
5749 i = 0;
5750 for (;;)
5751 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005752 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753#ifdef FEAT_CMDL_INFO
5754 if (!(State & CMDLINE)
5755# ifdef FEAT_MBYTE
5756 && MB_BYTE2LEN_CHECK(nc) == 1
5757# endif
5758 )
5759 add_to_showcmd(nc);
5760#endif
5761 if (nc == 'x' || nc == 'X')
5762 hex = TRUE;
5763 else if (nc == 'o' || nc == 'O')
5764 octal = TRUE;
5765#ifdef FEAT_MBYTE
5766 else if (nc == 'u' || nc == 'U')
5767 unicode = nc;
5768#endif
5769 else
5770 {
5771 if (hex
5772#ifdef FEAT_MBYTE
5773 || unicode != 0
5774#endif
5775 )
5776 {
5777 if (!vim_isxdigit(nc))
5778 break;
5779 cc = cc * 16 + hex2nr(nc);
5780 }
5781 else if (octal)
5782 {
5783 if (nc < '0' || nc > '7')
5784 break;
5785 cc = cc * 8 + nc - '0';
5786 }
5787 else
5788 {
5789 if (!VIM_ISDIGIT(nc))
5790 break;
5791 cc = cc * 10 + nc - '0';
5792 }
5793
5794 ++i;
5795 }
5796
5797 if (cc > 255
5798#ifdef FEAT_MBYTE
5799 && unicode == 0
5800#endif
5801 )
5802 cc = 255; /* limit range to 0-255 */
5803 nc = 0;
5804
5805 if (hex) /* hex: up to two chars */
5806 {
5807 if (i >= 2)
5808 break;
5809 }
5810#ifdef FEAT_MBYTE
5811 else if (unicode) /* Unicode: up to four or eight chars */
5812 {
5813 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5814 break;
5815 }
5816#endif
5817 else if (i >= 3) /* decimal or octal: up to three chars */
5818 break;
5819 }
5820 if (i == 0) /* no number entered */
5821 {
5822 if (nc == K_ZERO) /* NUL is stored as NL */
5823 {
5824 cc = '\n';
5825 nc = 0;
5826 }
5827 else
5828 {
5829 cc = nc;
5830 nc = 0;
5831 }
5832 }
5833
5834 if (cc == 0) /* NUL is stored as NL */
5835 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005836#ifdef FEAT_MBYTE
5837 if (enc_dbcs && (cc & 0xff) == 0)
5838 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5839 second byte will cause trouble! */
5840#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841
5842 --no_mapping;
5843#ifdef FEAT_GUI
5844 if (gui.in_use)
5845 --allow_keys;
5846#endif
5847 if (nc)
5848 vungetc(nc);
5849 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5850 return cc;
5851}
5852
5853/*
5854 * Insert character, taking care of special keys and mod_mask
5855 */
5856 static void
5857insert_special(c, allow_modmask, ctrlv)
5858 int c;
5859 int allow_modmask;
5860 int ctrlv; /* c was typed after CTRL-V */
5861{
5862 char_u *p;
5863 int len;
5864
5865 /*
5866 * Special function key, translate into "<Key>". Up to the last '>' is
5867 * inserted with ins_str(), so as not to replace characters in replace
5868 * mode.
5869 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5870 * unless 'allow_modmask' is TRUE.
5871 */
5872#ifdef MACOS
5873 /* Command-key never produces a normal key */
5874 if (mod_mask & MOD_MASK_CMD)
5875 allow_modmask = TRUE;
5876#endif
5877 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5878 {
5879 p = get_special_key_name(c, mod_mask);
5880 len = (int)STRLEN(p);
5881 c = p[len - 1];
5882 if (len > 2)
5883 {
5884 if (stop_arrow() == FAIL)
5885 return;
5886 p[len - 1] = NUL;
5887 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005888 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005889 ctrlv = FALSE;
5890 }
5891 }
5892 if (stop_arrow() == OK)
5893 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5894}
5895
5896/*
5897 * Special characters in this context are those that need processing other
5898 * than the simple insertion that can be performed here. This includes ESC
5899 * which terminates the insert, and CR/NL which need special processing to
5900 * open up a new line. This routine tries to optimize insertions performed by
5901 * the "redo", "undo" or "put" commands, so it needs to know when it should
5902 * stop and defer processing to the "normal" mechanism.
5903 * '0' and '^' are special, because they can be followed by CTRL-D.
5904 */
5905#ifdef EBCDIC
5906# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5907#else
5908# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5909#endif
5910
5911#ifdef FEAT_MBYTE
5912# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5913#else
5914# define WHITECHAR(cc) vim_iswhite(cc)
5915#endif
5916
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005917/*
5918 * "flags": INSCHAR_FORMAT - force formatting
5919 * INSCHAR_CTRLV - char typed just after CTRL-V
5920 * INSCHAR_NO_FEX - don't use 'formatexpr'
5921 *
5922 * NOTE: passes the flags value straight through to internal_format() which,
5923 * beside INSCHAR_FORMAT (above), is also looking for these:
5924 * INSCHAR_DO_COM - format comments
5925 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5926 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005927 void
5928insertchar(c, flags, second_indent)
5929 int c; /* character to insert or NUL */
5930 int flags; /* INSCHAR_FORMAT, etc. */
5931 int second_indent; /* indent for second line if >= 0 */
5932{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 int textwidth;
5934#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005935 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005938 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005939
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005940 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942
5943 /*
5944 * Try to break the line in two or more pieces when:
5945 * - Always do this if we have been called to do formatting only.
5946 * - Always do this when 'formatoptions' has the 'a' flag and the line
5947 * ends in white space.
5948 * - Otherwise:
5949 * - Don't do this if inserting a blank
5950 * - Don't do this if an existing character is being replaced, unless
5951 * we're in VREPLACE mode.
5952 * - Do this if the cursor is not on the line where insert started
5953 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5954 * before the insert.
5955 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5956 * before 'textwidth'
5957 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005958 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005959 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005960 || (!vim_iswhite(c)
5961 && !((State & REPLACE_FLAG)
5962#ifdef FEAT_VREPLACE
5963 && !(State & VREPLACE_FLAG)
5964#endif
5965 && *ml_get_cursor() != NUL)
5966 && (curwin->w_cursor.lnum != Insstart.lnum
5967 || ((!has_format_option(FO_INS_LONG)
5968 || Insstart_textlen <= (colnr_T)textwidth)
5969 && (!fo_ins_blank
5970 || Insstart_blank_vcol <= (colnr_T)textwidth
5971 ))))))
5972 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005973 /* Format with 'formatexpr' when it's set. Use internal formatting
5974 * when 'formatexpr' isn't set or it returns non-zero. */
5975#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005976 int do_internal = TRUE;
5977 colnr_T virtcol = get_nolist_virtcol()
5978 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005979
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005980 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
5981 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005982 {
5983 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5984 /* It may be required to save for undo again, e.g. when setline()
5985 * was called. */
5986 ins_need_undo = TRUE;
5987 }
5988 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005989#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005990 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005991 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005992
Bram Moolenaar071d4272004-06-13 20:20:40 +00005993 if (c == NUL) /* only formatting was wanted */
5994 return;
5995
5996#ifdef FEAT_COMMENTS
5997 /* Check whether this character should end a comment. */
5998 if (did_ai && (int)c == end_comment_pending)
5999 {
6000 char_u *line;
6001 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6002 int middle_len, end_len;
6003 int i;
6004
6005 /*
6006 * Need to remove existing (middle) comment leader and insert end
6007 * comment leader. First, check what comment leader we can find.
6008 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006009 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6011 {
6012 /* Skip middle-comment string */
6013 while (*p && p[-1] != ':') /* find end of middle flags */
6014 ++p;
6015 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6016 /* Don't count trailing white space for middle_len */
6017 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6018 --middle_len;
6019
6020 /* Find the end-comment string */
6021 while (*p && p[-1] != ':') /* find end of end flags */
6022 ++p;
6023 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6024
6025 /* Skip white space before the cursor */
6026 i = curwin->w_cursor.col;
6027 while (--i >= 0 && vim_iswhite(line[i]))
6028 ;
6029 i++;
6030
6031 /* Skip to before the middle leader */
6032 i -= middle_len;
6033
6034 /* Check some expected things before we go on */
6035 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6036 {
6037 /* Backspace over all the stuff we want to replace */
6038 backspace_until_column(i);
6039
6040 /*
6041 * Insert the end-comment string, except for the last
6042 * character, which will get inserted as normal later.
6043 */
6044 ins_bytes_len(lead_end, end_len - 1);
6045 }
6046 }
6047 }
6048 end_comment_pending = NUL;
6049#endif
6050
6051 did_ai = FALSE;
6052#ifdef FEAT_SMARTINDENT
6053 did_si = FALSE;
6054 can_si = FALSE;
6055 can_si_back = FALSE;
6056#endif
6057
6058 /*
6059 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6060 * This speeds up normal text input considerably.
6061 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6062 * need to re-indent at a ':', or any other character (but not what
6063 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006064 * Don't do this when there an InsertCharPre autocommand is defined,
6065 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006066 */
6067#ifdef USE_ON_FLY_SCROLL
6068 dont_scroll = FALSE; /* allow scrolling here */
6069#endif
6070
6071 if ( !ISSPECIAL(c)
6072#ifdef FEAT_MBYTE
6073 && (!has_mbyte || (*mb_char2len)(c) == 1)
6074#endif
6075 && vpeekc() != NUL
6076 && !(State & REPLACE_FLAG)
6077#ifdef FEAT_CINDENT
6078 && !cindent_on()
6079#endif
6080#ifdef FEAT_RIGHTLEFT
6081 && !p_ri
6082#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006083#ifdef FEAT_AUTOCMD
6084 && !has_insertcharpre()
6085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006086 )
6087 {
6088#define INPUT_BUFLEN 100
6089 char_u buf[INPUT_BUFLEN + 1];
6090 int i;
6091 colnr_T virtcol = 0;
6092
6093 buf[0] = c;
6094 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006095 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 virtcol = get_nolist_virtcol();
6097 /*
6098 * Stop the string when:
6099 * - no more chars available
6100 * - finding a special character (command key)
6101 * - buffer is full
6102 * - running into the 'textwidth' boundary
6103 * - need to check for abbreviation: A non-word char after a word-char
6104 */
6105 while ( (c = vpeekc()) != NUL
6106 && !ISSPECIAL(c)
6107#ifdef FEAT_MBYTE
6108 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6109#endif
6110 && i < INPUT_BUFLEN
6111 && (textwidth == 0
6112 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6113 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6114 {
6115#ifdef FEAT_RIGHTLEFT
6116 c = vgetc();
6117 if (p_hkmap && KeyTyped)
6118 c = hkmap(c); /* Hebrew mode mapping */
6119# ifdef FEAT_FKMAP
6120 if (p_fkmap && KeyTyped)
6121 c = fkmap(c); /* Farsi mode mapping */
6122# endif
6123 buf[i++] = c;
6124#else
6125 buf[i++] = vgetc();
6126#endif
6127 }
6128
6129#ifdef FEAT_DIGRAPHS
6130 do_digraph(-1); /* clear digraphs */
6131 do_digraph(buf[i-1]); /* may be the start of a digraph */
6132#endif
6133 buf[i] = NUL;
6134 ins_str(buf);
6135 if (flags & INSCHAR_CTRLV)
6136 {
6137 redo_literal(*buf);
6138 i = 1;
6139 }
6140 else
6141 i = 0;
6142 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006143 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006144 }
6145 else
6146 {
6147#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006148 int cc;
6149
Bram Moolenaar071d4272004-06-13 20:20:40 +00006150 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6151 {
6152 char_u buf[MB_MAXBYTES + 1];
6153
6154 (*mb_char2bytes)(c, buf);
6155 buf[cc] = NUL;
6156 ins_char_bytes(buf, cc);
6157 AppendCharToRedobuff(c);
6158 }
6159 else
6160#endif
6161 {
6162 ins_char(c);
6163 if (flags & INSCHAR_CTRLV)
6164 redo_literal(c);
6165 else
6166 AppendCharToRedobuff(c);
6167 }
6168 }
6169}
6170
6171/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006172 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006173 *
6174 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6175 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006176 */
6177 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006178internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006179 int textwidth;
6180 int second_indent;
6181 int flags;
6182 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006183 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006184{
6185 int cc;
6186 int save_char = NUL;
6187 int haveto_redraw = FALSE;
6188 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6189#ifdef FEAT_MBYTE
6190 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6191#endif
6192 int fo_white_par = has_format_option(FO_WHITE_PAR);
6193 int first_line = TRUE;
6194#ifdef FEAT_COMMENTS
6195 colnr_T leader_len;
6196 int no_leader = FALSE;
6197 int do_comments = (flags & INSCHAR_DO_COM);
6198#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006199#ifdef FEAT_LINEBREAK
6200 int has_lbr = curwin->w_p_lbr;
6201
6202 /* make sure win_lbr_chartabsize() counts correctly */
6203 curwin->w_p_lbr = FALSE;
6204#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006205
6206 /*
6207 * When 'ai' is off we don't want a space under the cursor to be
6208 * deleted. Replace it with an 'x' temporarily.
6209 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006210 if (!curbuf->b_p_ai
6211#ifdef FEAT_VREPLACE
6212 && !(State & VREPLACE_FLAG)
6213#endif
6214 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006215 {
6216 cc = gchar_cursor();
6217 if (vim_iswhite(cc))
6218 {
6219 save_char = cc;
6220 pchar_cursor('x');
6221 }
6222 }
6223
6224 /*
6225 * Repeat breaking lines, until the current line is not too long.
6226 */
6227 while (!got_int)
6228 {
6229 int startcol; /* Cursor column at entry */
6230 int wantcol; /* column at textwidth border */
6231 int foundcol; /* column for start of spaces */
6232 int end_foundcol = 0; /* column for start of word */
6233 colnr_T len;
6234 colnr_T virtcol;
6235#ifdef FEAT_VREPLACE
6236 int orig_col = 0;
6237 char_u *saved_text = NULL;
6238#endif
6239 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006240 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006241
Bram Moolenaar97b98102009-11-17 16:41:01 +00006242 virtcol = get_nolist_virtcol()
6243 + char2cells(c != NUL ? c : gchar_cursor());
6244 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006245 break;
6246
6247#ifdef FEAT_COMMENTS
6248 if (no_leader)
6249 do_comments = FALSE;
6250 else if (!(flags & INSCHAR_FORMAT)
6251 && has_format_option(FO_WRAP_COMS))
6252 do_comments = TRUE;
6253
6254 /* Don't break until after the comment leader */
6255 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006256 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006257 else
6258 leader_len = 0;
6259
6260 /* If the line doesn't start with a comment leader, then don't
6261 * start one in a following broken line. Avoids that a %word
6262 * moved to the start of the next line causes all following lines
6263 * to start with %. */
6264 if (leader_len == 0)
6265 no_leader = TRUE;
6266#endif
6267 if (!(flags & INSCHAR_FORMAT)
6268#ifdef FEAT_COMMENTS
6269 && leader_len == 0
6270#endif
6271 && !has_format_option(FO_WRAP))
6272
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006273 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006274 if ((startcol = curwin->w_cursor.col) == 0)
6275 break;
6276
6277 /* find column of textwidth border */
6278 coladvance((colnr_T)textwidth);
6279 wantcol = curwin->w_cursor.col;
6280
Bram Moolenaar97b98102009-11-17 16:41:01 +00006281 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282 foundcol = 0;
6283
6284 /*
6285 * Find position to break at.
6286 * Stop at first entered white when 'formatoptions' has 'v'
6287 */
6288 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006289 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006290 || curwin->w_cursor.lnum != Insstart.lnum
6291 || curwin->w_cursor.col >= Insstart.col)
6292 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006293 if (curwin->w_cursor.col == startcol && c != NUL)
6294 cc = c;
6295 else
6296 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006297 if (WHITECHAR(cc))
6298 {
6299 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006300 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006301
6302 /* find start of sequence of blanks */
6303 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6304 {
6305 dec_cursor();
6306 cc = gchar_cursor();
6307 }
6308 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6309 break; /* only spaces in front of text */
6310#ifdef FEAT_COMMENTS
6311 /* Don't break until after the comment leader */
6312 if (curwin->w_cursor.col < leader_len)
6313 break;
6314#endif
6315 if (has_format_option(FO_ONE_LETTER))
6316 {
6317 /* do not break after one-letter words */
6318 if (curwin->w_cursor.col == 0)
6319 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006320#ifdef FEAT_COMMENTS
6321 /* do not break "#a b" when 'tw' is 2 */
6322 if (curwin->w_cursor.col <= leader_len)
6323 break;
6324#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006325 col = curwin->w_cursor.col;
6326 dec_cursor();
6327 cc = gchar_cursor();
6328
6329 if (WHITECHAR(cc))
6330 continue; /* one-letter, continue */
6331 curwin->w_cursor.col = col;
6332 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006333
6334 inc_cursor();
6335
6336 end_foundcol = end_col + 1;
6337 foundcol = curwin->w_cursor.col;
6338 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006339 break;
6340 }
6341#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006342 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006343 {
6344 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006345 if (curwin->w_cursor.col != startcol)
6346 {
6347#ifdef FEAT_COMMENTS
6348 /* Don't break until after the comment leader */
6349 if (curwin->w_cursor.col < leader_len)
6350 break;
6351#endif
6352 col = curwin->w_cursor.col;
6353 inc_cursor();
6354 /* Don't change end_foundcol if already set. */
6355 if (foundcol != curwin->w_cursor.col)
6356 {
6357 foundcol = curwin->w_cursor.col;
6358 end_foundcol = foundcol;
6359 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6360 break;
6361 }
6362 curwin->w_cursor.col = col;
6363 }
6364
6365 if (curwin->w_cursor.col == 0)
6366 break;
6367
6368 col = curwin->w_cursor.col;
6369
6370 dec_cursor();
6371 cc = gchar_cursor();
6372
6373 if (WHITECHAR(cc))
6374 continue; /* break with space */
6375#ifdef FEAT_COMMENTS
6376 /* Don't break until after the comment leader */
6377 if (curwin->w_cursor.col < leader_len)
6378 break;
6379#endif
6380
6381 curwin->w_cursor.col = col;
6382
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006383 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006384 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006385 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6386 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006387 }
6388#endif
6389 if (curwin->w_cursor.col == 0)
6390 break;
6391 dec_cursor();
6392 }
6393
6394 if (foundcol == 0) /* no spaces, cannot break line */
6395 {
6396 curwin->w_cursor.col = startcol;
6397 break;
6398 }
6399
6400 /* Going to break the line, remove any "$" now. */
6401 undisplay_dollar();
6402
6403 /*
6404 * Offset between cursor position and line break is used by replace
6405 * stack functions. VREPLACE does not use this, and backspaces
6406 * over the text instead.
6407 */
6408#ifdef FEAT_VREPLACE
6409 if (State & VREPLACE_FLAG)
6410 orig_col = startcol; /* Will start backspacing from here */
6411 else
6412#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006413 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006414
6415 /*
6416 * adjust startcol for spaces that will be deleted and
6417 * characters that will remain on top line
6418 */
6419 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006420 while ((cc = gchar_cursor(), WHITECHAR(cc))
6421 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006422 inc_cursor();
6423 startcol -= curwin->w_cursor.col;
6424 if (startcol < 0)
6425 startcol = 0;
6426
6427#ifdef FEAT_VREPLACE
6428 if (State & VREPLACE_FLAG)
6429 {
6430 /*
6431 * In VREPLACE mode, we will backspace over the text to be
6432 * wrapped, so save a copy now to put on the next line.
6433 */
6434 saved_text = vim_strsave(ml_get_cursor());
6435 curwin->w_cursor.col = orig_col;
6436 if (saved_text == NULL)
6437 break; /* Can't do it, out of memory */
6438 saved_text[startcol] = NUL;
6439
6440 /* Backspace over characters that will move to the next line */
6441 if (!fo_white_par)
6442 backspace_until_column(foundcol);
6443 }
6444 else
6445#endif
6446 {
6447 /* put cursor after pos. to break line */
6448 if (!fo_white_par)
6449 curwin->w_cursor.col = foundcol;
6450 }
6451
6452 /*
6453 * Split the line just before the margin.
6454 * Only insert/delete lines, but don't really redraw the window.
6455 */
6456 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6457 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6458#ifdef FEAT_COMMENTS
6459 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006460 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006461#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006462 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6463 if (!(flags & INSCHAR_COM_LIST))
6464 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006465
6466 replace_offset = 0;
6467 if (first_line)
6468 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006469 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006470 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006471 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006472 * This section is for auto-wrap of numeric lists. When not
6473 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6474 * flag will be set and open_line() will handle it (as seen
6475 * above). The code here (and in get_number_indent()) will
6476 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006477 */
6478 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006479 second_indent =
6480 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006481 if (second_indent >= 0)
6482 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006483#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006484 if (State & VREPLACE_FLAG)
6485 change_indent(INDENT_SET, second_indent,
6486 FALSE, NUL, TRUE);
6487 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006488#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006489#ifdef FEAT_COMMENTS
6490 if (leader_len > 0 && second_indent - leader_len > 0)
6491 {
6492 int i;
6493 int padding = second_indent - leader_len;
6494
6495 /* We started at the first_line of a numbered list
6496 * that has a comment. the open_line() function has
6497 * inserted the proper comment leader and positioned
6498 * the cursor at the end of the split line. Now we
6499 * add the additional whitespace needed after the
6500 * comment leader for the numbered list. */
6501 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006502 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006503 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006504 }
6505 else
6506 {
6507#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006508 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006509#ifdef FEAT_COMMENTS
6510 }
6511#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006512 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006513 }
6514 first_line = FALSE;
6515 }
6516
6517#ifdef FEAT_VREPLACE
6518 if (State & VREPLACE_FLAG)
6519 {
6520 /*
6521 * In VREPLACE mode we have backspaced over the text to be
6522 * moved, now we re-insert it into the new line.
6523 */
6524 ins_bytes(saved_text);
6525 vim_free(saved_text);
6526 }
6527 else
6528#endif
6529 {
6530 /*
6531 * Check if cursor is not past the NUL off the line, cindent
6532 * may have added or removed indent.
6533 */
6534 curwin->w_cursor.col += startcol;
6535 len = (colnr_T)STRLEN(ml_get_curline());
6536 if (curwin->w_cursor.col > len)
6537 curwin->w_cursor.col = len;
6538 }
6539
6540 haveto_redraw = TRUE;
6541#ifdef FEAT_CINDENT
6542 can_cindent = TRUE;
6543#endif
6544 /* moved the cursor, don't autoindent or cindent now */
6545 did_ai = FALSE;
6546#ifdef FEAT_SMARTINDENT
6547 did_si = FALSE;
6548 can_si = FALSE;
6549 can_si_back = FALSE;
6550#endif
6551 line_breakcheck();
6552 }
6553
6554 if (save_char != NUL) /* put back space after cursor */
6555 pchar_cursor(save_char);
6556
Bram Moolenaar0026d472014-09-09 16:32:39 +02006557#ifdef FEAT_LINEBREAK
6558 curwin->w_p_lbr = has_lbr;
6559#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006560 if (!format_only && haveto_redraw)
6561 {
6562 update_topline();
6563 redraw_curbuf_later(VALID);
6564 }
6565}
6566
6567/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 * Called after inserting or deleting text: When 'formatoptions' includes the
6569 * 'a' flag format from the current line until the end of the paragraph.
6570 * Keep the cursor at the same position relative to the text.
6571 * The caller must have saved the cursor line for undo, following ones will be
6572 * saved here.
6573 */
6574 void
6575auto_format(trailblank, prev_line)
6576 int trailblank; /* when TRUE also format with trailing blank */
6577 int prev_line; /* may start in previous line */
6578{
6579 pos_T pos;
6580 colnr_T len;
6581 char_u *old;
6582 char_u *new, *pnew;
6583 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006584 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585
6586 if (!has_format_option(FO_AUTO))
6587 return;
6588
6589 pos = curwin->w_cursor;
6590 old = ml_get_curline();
6591
6592 /* may remove added space */
6593 check_auto_format(FALSE);
6594
6595 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6596 * user might insert normal text next. Also skip formatting when "1" is
6597 * in 'formatoptions' and there is a single character before the cursor.
6598 * Otherwise the line would be broken and when typing another non-white
6599 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006600 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 if (*old != NUL && !trailblank && wasatend)
6602 {
6603 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006604 cc = gchar_cursor();
6605 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6606 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006607 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006608 cc = gchar_cursor();
6609 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610 {
6611 curwin->w_cursor = pos;
6612 return;
6613 }
6614 curwin->w_cursor = pos;
6615 }
6616
6617#ifdef FEAT_COMMENTS
6618 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6619 * comments. */
6620 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006621 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 return;
6623#endif
6624
6625 /*
6626 * May start formatting in a previous line, so that after "x" a word is
6627 * moved to the previous line if it fits there now. Only when this is not
6628 * the start of a paragraph.
6629 */
6630 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6631 {
6632 --curwin->w_cursor.lnum;
6633 if (u_save_cursor() == FAIL)
6634 return;
6635 }
6636
6637 /*
6638 * Do the formatting and restore the cursor position. "saved_cursor" will
6639 * be adjusted for the text formatting.
6640 */
6641 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006642 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 curwin->w_cursor = saved_cursor;
6644 saved_cursor.lnum = 0;
6645
6646 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6647 {
6648 /* "cannot happen" */
6649 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6650 coladvance((colnr_T)MAXCOL);
6651 }
6652 else
6653 check_cursor_col();
6654
6655 /* Insert mode: If the cursor is now after the end of the line while it
6656 * previously wasn't, the line was broken. Because of the rule above we
6657 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6658 * formatted. */
6659 if (!wasatend && has_format_option(FO_WHITE_PAR))
6660 {
6661 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006662 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006663 if (curwin->w_cursor.col == len)
6664 {
6665 pnew = vim_strnsave(new, len + 2);
6666 pnew[len] = ' ';
6667 pnew[len + 1] = NUL;
6668 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6669 /* remove the space later */
6670 did_add_space = TRUE;
6671 }
6672 else
6673 /* may remove added space */
6674 check_auto_format(FALSE);
6675 }
6676
6677 check_cursor();
6678}
6679
6680/*
6681 * When an extra space was added to continue a paragraph for auto-formatting,
6682 * delete it now. The space must be under the cursor, just after the insert
6683 * position.
6684 */
6685 static void
6686check_auto_format(end_insert)
6687 int end_insert; /* TRUE when ending Insert mode */
6688{
6689 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006690 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691
6692 if (did_add_space)
6693 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006694 cc = gchar_cursor();
6695 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696 /* Somehow the space was removed already. */
6697 did_add_space = FALSE;
6698 else
6699 {
6700 if (!end_insert)
6701 {
6702 inc_cursor();
6703 c = gchar_cursor();
6704 dec_cursor();
6705 }
6706 if (c != NUL)
6707 {
6708 /* The space is no longer at the end of the line, delete it. */
6709 del_char(FALSE);
6710 did_add_space = FALSE;
6711 }
6712 }
6713 }
6714}
6715
6716/*
6717 * Find out textwidth to be used for formatting:
6718 * if 'textwidth' option is set, use it
6719 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6720 * if invalid value, use 0.
6721 * Set default to window width (maximum 79) for "gq" operator.
6722 */
6723 int
6724comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006725 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006726{
6727 int textwidth;
6728
6729 textwidth = curbuf->b_p_tw;
6730 if (textwidth == 0 && curbuf->b_p_wm)
6731 {
6732 /* The width is the window width minus 'wrapmargin' minus all the
6733 * things that add to the margin. */
6734 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6735#ifdef FEAT_CMDWIN
6736 if (cmdwin_type != 0)
6737 textwidth -= 1;
6738#endif
6739#ifdef FEAT_FOLDING
6740 textwidth -= curwin->w_p_fdc;
6741#endif
6742#ifdef FEAT_SIGNS
6743 if (curwin->w_buffer->b_signlist != NULL
6744# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01006745 || curwin->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746# endif
6747 )
6748 textwidth -= 1;
6749#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006750 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 textwidth -= 8;
6752 }
6753 if (textwidth < 0)
6754 textwidth = 0;
6755 if (ff && textwidth == 0)
6756 {
6757 textwidth = W_WIDTH(curwin) - 1;
6758 if (textwidth > 79)
6759 textwidth = 79;
6760 }
6761 return textwidth;
6762}
6763
6764/*
6765 * Put a character in the redo buffer, for when just after a CTRL-V.
6766 */
6767 static void
6768redo_literal(c)
6769 int c;
6770{
6771 char_u buf[10];
6772
6773 /* Only digits need special treatment. Translate them into a string of
6774 * three digits. */
6775 if (VIM_ISDIGIT(c))
6776 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006777 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 AppendToRedobuff(buf);
6779 }
6780 else
6781 AppendCharToRedobuff(c);
6782}
6783
6784/*
6785 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006786 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006787 */
6788 static void
6789start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006790 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006791{
6792 if (!arrow_used) /* something has been inserted */
6793 {
6794 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006795 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796 arrow_used = TRUE; /* this means we stopped the current insert */
6797 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006798#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006799 check_spell_redraw();
6800#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801}
6802
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006803#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006804/*
6805 * If we skipped highlighting word at cursor, do it now.
6806 * It may be skipped again, thus reset spell_redraw_lnum first.
6807 */
6808 static void
6809check_spell_redraw()
6810{
6811 if (spell_redraw_lnum != 0)
6812 {
6813 linenr_T lnum = spell_redraw_lnum;
6814
6815 spell_redraw_lnum = 0;
6816 redrawWinline(lnum, FALSE);
6817 }
6818}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006819
6820/*
6821 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6822 * spelled word, if there is one.
6823 */
6824 static void
6825spell_back_to_badword()
6826{
6827 pos_T tpos = curwin->w_cursor;
6828
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006829 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006830 if (curwin->w_cursor.col != tpos.col)
6831 start_arrow(&tpos);
6832}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006833#endif
6834
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835/*
6836 * stop_arrow() is called before a change is made in insert mode.
6837 * If an arrow key has been used, start a new insertion.
6838 * Returns FAIL if undo is impossible, shouldn't insert then.
6839 */
6840 int
6841stop_arrow()
6842{
6843 if (arrow_used)
6844 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006845 Insstart = curwin->w_cursor; /* new insertion starts here */
6846 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6847 /* Don't update the original insert position when moved to the
6848 * right, except when nothing was inserted yet. */
6849 update_Insstart_orig = FALSE;
6850 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6851
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 if (u_save_cursor() == OK)
6853 {
6854 arrow_used = FALSE;
6855 ins_need_undo = FALSE;
6856 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006857
Bram Moolenaar071d4272004-06-13 20:20:40 +00006858 ai_col = 0;
6859#ifdef FEAT_VREPLACE
6860 if (State & VREPLACE_FLAG)
6861 {
6862 orig_line_count = curbuf->b_ml.ml_line_count;
6863 vr_lines_changed = 1;
6864 }
6865#endif
6866 ResetRedobuff();
6867 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006868 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 }
6870 else if (ins_need_undo)
6871 {
6872 if (u_save_cursor() == OK)
6873 ins_need_undo = FALSE;
6874 }
6875
6876#ifdef FEAT_FOLDING
6877 /* Always open fold at the cursor line when inserting something. */
6878 foldOpenCursor();
6879#endif
6880
6881 return (arrow_used || ins_need_undo ? FAIL : OK);
6882}
6883
6884/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006885 * Do a few things to stop inserting.
6886 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6887 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 */
6889 static void
Bram Moolenaarf332a652013-11-04 04:20:33 +01006890stop_insert(end_insert_pos, esc, nomove)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006891 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006892 int esc; /* called by ins_esc() */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006893 int nomove; /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006895 int cc;
6896 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006897
6898 stop_redo_ins();
6899 replace_flush(); /* abandon replace stack */
6900
6901 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006902 * Save the inserted text for later redo with ^@ and CTRL-A.
6903 * Don't do it when "restart_edit" was set and nothing was inserted,
6904 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006905 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006906 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006907 if (did_restart_edit == 0 || (ptr != NULL
6908 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006909 {
6910 vim_free(last_insert);
6911 last_insert = ptr;
6912 last_insert_skip = new_insert_skip;
6913 }
6914 else
6915 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006917 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 {
6919 /* Auto-format now. It may seem strange to do this when stopping an
6920 * insertion (or moving the cursor), but it's required when appending
6921 * a line and having it end in a space. But only do it when something
6922 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006923 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006924 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006925 pos_T tpos = curwin->w_cursor;
6926
Bram Moolenaar071d4272004-06-13 20:20:40 +00006927 /* When the cursor is at the end of the line after a space the
6928 * formatting will move it to the following word. Avoid that by
6929 * moving the cursor onto the space. */
6930 cc = 'x';
6931 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6932 {
6933 dec_cursor();
6934 cc = gchar_cursor();
6935 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006936 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006937 }
6938
6939 auto_format(TRUE, FALSE);
6940
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006941 if (vim_iswhite(cc))
6942 {
6943 if (gchar_cursor() != NUL)
6944 inc_cursor();
6945#ifdef FEAT_VIRTUALEDIT
6946 /* If the cursor is still at the same character, also keep
6947 * the "coladd". */
6948 if (gchar_cursor() == NUL
6949 && curwin->w_cursor.lnum == tpos.lnum
6950 && curwin->w_cursor.col == tpos.col)
6951 curwin->w_cursor.coladd = tpos.coladd;
6952#endif
6953 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006954 }
6955
6956 /* If a space was inserted for auto-formatting, remove it now. */
6957 check_auto_format(TRUE);
6958
6959 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006960 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006961 * Do this when ESC was used or moving the cursor up/down.
6962 * Check for the old position still being valid, just in case the text
6963 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006964 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006965 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6966 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006967 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006968 pos_T tpos = curwin->w_cursor;
6969
6970 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006971 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006972 for (;;)
6973 {
6974 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6975 --curwin->w_cursor.col;
6976 cc = gchar_cursor();
6977 if (!vim_iswhite(cc))
6978 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006979 if (del_char(TRUE) == FAIL)
6980 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006981 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006982 if (curwin->w_cursor.lnum != tpos.lnum)
6983 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01006984 else
6985 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01006986 /* reset tpos, could have been invalidated in the loop above */
6987 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01006988 tpos.col++;
6989 if (cc != NUL && gchar_pos(&tpos) == NUL)
6990 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006992
Bram Moolenaar071d4272004-06-13 20:20:40 +00006993 /* <C-S-Right> may have started Visual mode, adjust the position for
6994 * deleted characters. */
6995 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6996 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006997 int len = (int)STRLEN(ml_get_curline());
6998
6999 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007000 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007001 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007002#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007003 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 }
7006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007007 }
7008 }
7009 did_ai = FALSE;
7010#ifdef FEAT_SMARTINDENT
7011 did_si = FALSE;
7012 can_si = FALSE;
7013 can_si_back = FALSE;
7014#endif
7015
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007016 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7017 * now in a different buffer. */
7018 if (end_insert_pos != NULL)
7019 {
7020 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007021 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007022 curbuf->b_op_end = *end_insert_pos;
7023 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024}
7025
7026/*
7027 * Set the last inserted text to a single character.
7028 * Used for the replace command.
7029 */
7030 void
7031set_last_insert(c)
7032 int c;
7033{
7034 char_u *s;
7035
7036 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007037 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 if (last_insert != NULL)
7039 {
7040 s = last_insert;
7041 /* Use the CTRL-V only when entering a special char */
7042 if (c < ' ' || c == DEL)
7043 *s++ = Ctrl_V;
7044 s = add_char2buf(c, s);
7045 *s++ = ESC;
7046 *s++ = NUL;
7047 last_insert_skip = 0;
7048 }
7049}
7050
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007051#if defined(EXITFREE) || defined(PROTO)
7052 void
7053free_last_insert()
7054{
7055 vim_free(last_insert);
7056 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007057# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007058 vim_free(compl_orig_text);
7059 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007060# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007061}
7062#endif
7063
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064/*
7065 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7066 * and CSI. Handle multi-byte characters.
7067 * Returns a pointer to after the added bytes.
7068 */
7069 char_u *
7070add_char2buf(c, s)
7071 int c;
7072 char_u *s;
7073{
7074#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007075 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 int i;
7077 int len;
7078
7079 len = (*mb_char2bytes)(c, temp);
7080 for (i = 0; i < len; ++i)
7081 {
7082 c = temp[i];
7083#endif
7084 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7085 if (c == K_SPECIAL)
7086 {
7087 *s++ = K_SPECIAL;
7088 *s++ = KS_SPECIAL;
7089 *s++ = KE_FILLER;
7090 }
7091#ifdef FEAT_GUI
7092 else if (c == CSI)
7093 {
7094 *s++ = CSI;
7095 *s++ = KS_EXTRA;
7096 *s++ = (int)KE_CSI;
7097 }
7098#endif
7099 else
7100 *s++ = c;
7101#ifdef FEAT_MBYTE
7102 }
7103#endif
7104 return s;
7105}
7106
7107/*
7108 * move cursor to start of line
7109 * if flags & BL_WHITE move to first non-white
7110 * if flags & BL_SOL move to first non-white if startofline is set,
7111 * otherwise keep "curswant" column
7112 * if flags & BL_FIX don't leave the cursor on a NUL.
7113 */
7114 void
7115beginline(flags)
7116 int flags;
7117{
7118 if ((flags & BL_SOL) && !p_sol)
7119 coladvance(curwin->w_curswant);
7120 else
7121 {
7122 curwin->w_cursor.col = 0;
7123#ifdef FEAT_VIRTUALEDIT
7124 curwin->w_cursor.coladd = 0;
7125#endif
7126
7127 if (flags & (BL_WHITE | BL_SOL))
7128 {
7129 char_u *ptr;
7130
7131 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7132 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7133 ++curwin->w_cursor.col;
7134 }
7135 curwin->w_set_curswant = TRUE;
7136 }
7137}
7138
7139/*
7140 * oneright oneleft cursor_down cursor_up
7141 *
7142 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007143 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007144 * Return OK when successful, FAIL when we hit a line of file boundary.
7145 */
7146
7147 int
7148oneright()
7149{
7150 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007151 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007152
7153#ifdef FEAT_VIRTUALEDIT
7154 if (virtual_active())
7155 {
7156 pos_T prevpos = curwin->w_cursor;
7157
7158 /* Adjust for multi-wide char (excluding TAB) */
7159 ptr = ml_get_cursor();
7160 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007161# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007162 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007163# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007165# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007166 ))
7167 ? ptr2cells(ptr) : 1));
7168 curwin->w_set_curswant = TRUE;
7169 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7170 return (prevpos.col != curwin->w_cursor.col
7171 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7172 }
7173#endif
7174
7175 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007176 if (*ptr == NUL)
7177 return FAIL; /* already at the very end */
7178
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007180 if (has_mbyte)
7181 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 else
7183#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007184 l = 1;
7185
7186 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7187 * contains "onemore". */
7188 if (ptr[l] == NUL
7189#ifdef FEAT_VIRTUALEDIT
7190 && (ve_flags & VE_ONEMORE) == 0
7191#endif
7192 )
7193 return FAIL;
7194 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007195
7196 curwin->w_set_curswant = TRUE;
7197 return OK;
7198}
7199
7200 int
7201oneleft()
7202{
7203#ifdef FEAT_VIRTUALEDIT
7204 if (virtual_active())
7205 {
7206 int width;
7207 int v = getviscol();
7208
7209 if (v == 0)
7210 return FAIL;
7211
7212# ifdef FEAT_LINEBREAK
7213 /* We might get stuck on 'showbreak', skip over it. */
7214 width = 1;
7215 for (;;)
7216 {
7217 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007218 /* getviscol() is slow, skip it when 'showbreak' is empty,
7219 * 'breakindent' is not set and there are no multi-byte
7220 * characters */
7221 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007222# ifdef FEAT_MBYTE
7223 && !has_mbyte
7224# endif
7225 ) || getviscol() < v)
7226 break;
7227 ++width;
7228 }
7229# else
7230 coladvance(v - 1);
7231# endif
7232
7233 if (curwin->w_cursor.coladd == 1)
7234 {
7235 char_u *ptr;
7236
7237 /* Adjust for multi-wide char (not a TAB) */
7238 ptr = ml_get_cursor();
7239 if (*ptr != TAB && vim_isprintc(
7240# ifdef FEAT_MBYTE
7241 (*mb_ptr2char)(ptr)
7242# else
7243 *ptr
7244# endif
7245 ) && ptr2cells(ptr) > 1)
7246 curwin->w_cursor.coladd = 0;
7247 }
7248
7249 curwin->w_set_curswant = TRUE;
7250 return OK;
7251 }
7252#endif
7253
7254 if (curwin->w_cursor.col == 0)
7255 return FAIL;
7256
7257 curwin->w_set_curswant = TRUE;
7258 --curwin->w_cursor.col;
7259
7260#ifdef FEAT_MBYTE
7261 /* if the character on the left of the current cursor is a multi-byte
7262 * character, move to its first byte */
7263 if (has_mbyte)
7264 mb_adjust_cursor();
7265#endif
7266 return OK;
7267}
7268
7269 int
7270cursor_up(n, upd_topline)
7271 long n;
7272 int upd_topline; /* When TRUE: update topline */
7273{
7274 linenr_T lnum;
7275
7276 if (n > 0)
7277 {
7278 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007279 /* This fails if the cursor is already in the first line or the count
7280 * is larger than the line number and '-' is in 'cpoptions' */
7281 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282 return FAIL;
7283 if (n >= lnum)
7284 lnum = 1;
7285 else
7286#ifdef FEAT_FOLDING
7287 if (hasAnyFolding(curwin))
7288 {
7289 /*
7290 * Count each sequence of folded lines as one logical line.
7291 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007292 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 (void)hasFolding(lnum, &lnum, NULL);
7294
7295 while (n--)
7296 {
7297 /* move up one line */
7298 --lnum;
7299 if (lnum <= 1)
7300 break;
7301 /* If we entered a fold, move to the beginning, unless in
7302 * Insert mode or when 'foldopen' contains "all": it will open
7303 * in a moment. */
7304 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7305 (void)hasFolding(lnum, &lnum, NULL);
7306 }
7307 if (lnum < 1)
7308 lnum = 1;
7309 }
7310 else
7311#endif
7312 lnum -= n;
7313 curwin->w_cursor.lnum = lnum;
7314 }
7315
7316 /* try to advance to the column we want to be at */
7317 coladvance(curwin->w_curswant);
7318
7319 if (upd_topline)
7320 update_topline(); /* make sure curwin->w_topline is valid */
7321
7322 return OK;
7323}
7324
7325/*
7326 * Cursor down a number of logical lines.
7327 */
7328 int
7329cursor_down(n, upd_topline)
7330 long n;
7331 int upd_topline; /* When TRUE: update topline */
7332{
7333 linenr_T lnum;
7334
7335 if (n > 0)
7336 {
7337 lnum = curwin->w_cursor.lnum;
7338#ifdef FEAT_FOLDING
7339 /* Move to last line of fold, will fail if it's the end-of-file. */
7340 (void)hasFolding(lnum, NULL, &lnum);
7341#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007342 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007343 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007344 if (lnum >= curbuf->b_ml.ml_line_count
7345 || (lnum + n > curbuf->b_ml.ml_line_count
7346 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007347 return FAIL;
7348 if (lnum + n >= curbuf->b_ml.ml_line_count)
7349 lnum = curbuf->b_ml.ml_line_count;
7350 else
7351#ifdef FEAT_FOLDING
7352 if (hasAnyFolding(curwin))
7353 {
7354 linenr_T last;
7355
7356 /* count each sequence of folded lines as one logical line */
7357 while (n--)
7358 {
7359 if (hasFolding(lnum, NULL, &last))
7360 lnum = last + 1;
7361 else
7362 ++lnum;
7363 if (lnum >= curbuf->b_ml.ml_line_count)
7364 break;
7365 }
7366 if (lnum > curbuf->b_ml.ml_line_count)
7367 lnum = curbuf->b_ml.ml_line_count;
7368 }
7369 else
7370#endif
7371 lnum += n;
7372 curwin->w_cursor.lnum = lnum;
7373 }
7374
7375 /* try to advance to the column we want to be at */
7376 coladvance(curwin->w_curswant);
7377
7378 if (upd_topline)
7379 update_topline(); /* make sure curwin->w_topline is valid */
7380
7381 return OK;
7382}
7383
7384/*
7385 * Stuff the last inserted text in the read buffer.
7386 * Last_insert actually is a copy of the redo buffer, so we
7387 * first have to remove the command.
7388 */
7389 int
7390stuff_inserted(c, count, no_esc)
7391 int c; /* Command character to be inserted */
7392 long count; /* Repeat this many times */
7393 int no_esc; /* Don't add an ESC at the end */
7394{
7395 char_u *esc_ptr;
7396 char_u *ptr;
7397 char_u *last_ptr;
7398 char_u last = NUL;
7399
7400 ptr = get_last_insert();
7401 if (ptr == NULL)
7402 {
7403 EMSG(_(e_noinstext));
7404 return FAIL;
7405 }
7406
7407 /* may want to stuff the command character, to start Insert mode */
7408 if (c != NUL)
7409 stuffcharReadbuff(c);
7410 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7411 *esc_ptr = NUL; /* remove the ESC */
7412
7413 /* when the last char is either "0" or "^" it will be quoted if no ESC
7414 * comes after it OR if it will inserted more than once and "ptr"
7415 * starts with ^D. -- Acevedo
7416 */
7417 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7418 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7419 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7420 {
7421 last = *last_ptr;
7422 *last_ptr = NUL;
7423 }
7424
7425 do
7426 {
7427 stuffReadbuff(ptr);
7428 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7429 if (last)
7430 stuffReadbuff((char_u *)(last == '0'
7431 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7432 : IF_EB("\026^", CTRL_V_STR "^")));
7433 }
7434 while (--count > 0);
7435
7436 if (last)
7437 *last_ptr = last;
7438
7439 if (esc_ptr != NULL)
7440 *esc_ptr = ESC; /* put the ESC back */
7441
7442 /* may want to stuff a trailing ESC, to get out of Insert mode */
7443 if (!no_esc)
7444 stuffcharReadbuff(ESC);
7445
7446 return OK;
7447}
7448
7449 char_u *
7450get_last_insert()
7451{
7452 if (last_insert == NULL)
7453 return NULL;
7454 return last_insert + last_insert_skip;
7455}
7456
7457/*
7458 * Get last inserted string, and remove trailing <Esc>.
7459 * Returns pointer to allocated memory (must be freed) or NULL.
7460 */
7461 char_u *
7462get_last_insert_save()
7463{
7464 char_u *s;
7465 int len;
7466
7467 if (last_insert == NULL)
7468 return NULL;
7469 s = vim_strsave(last_insert + last_insert_skip);
7470 if (s != NULL)
7471 {
7472 len = (int)STRLEN(s);
7473 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7474 s[len - 1] = NUL;
7475 }
7476 return s;
7477}
7478
7479/*
7480 * Check the word in front of the cursor for an abbreviation.
7481 * Called when the non-id character "c" has been entered.
7482 * When an abbreviation is recognized it is removed from the text and
7483 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7484 */
7485 static int
7486echeck_abbr(c)
7487 int c;
7488{
7489 /* Don't check for abbreviation in paste mode, when disabled and just
7490 * after moving around with cursor keys. */
7491 if (p_paste || no_abbr || arrow_used)
7492 return FALSE;
7493
7494 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7495 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7496}
7497
7498/*
7499 * replace-stack functions
7500 *
7501 * When replacing characters, the replaced characters are remembered for each
7502 * new character. This is used to re-insert the old text when backspacing.
7503 *
7504 * There is a NUL headed list of characters for each character that is
7505 * currently in the file after the insertion point. When BS is used, one NUL
7506 * headed list is put back for the deleted character.
7507 *
7508 * For a newline, there are two NUL headed lists. One contains the characters
7509 * that the NL replaced. The extra one stores the characters after the cursor
7510 * that were deleted (always white space).
7511 *
7512 * Replace_offset is normally 0, in which case replace_push will add a new
7513 * character at the end of the stack. If replace_offset is not 0, that many
7514 * characters will be left on the stack above the newly inserted character.
7515 */
7516
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007517static char_u *replace_stack = NULL;
7518static long replace_stack_nr = 0; /* next entry in replace stack */
7519static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520
7521 void
7522replace_push(c)
7523 int c; /* character that is replaced (NUL is none) */
7524{
7525 char_u *p;
7526
7527 if (replace_stack_nr < replace_offset) /* nothing to do */
7528 return;
7529 if (replace_stack_len <= replace_stack_nr)
7530 {
7531 replace_stack_len += 50;
7532 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7533 if (p == NULL) /* out of memory */
7534 {
7535 replace_stack_len -= 50;
7536 return;
7537 }
7538 if (replace_stack != NULL)
7539 {
7540 mch_memmove(p, replace_stack,
7541 (size_t)(replace_stack_nr * sizeof(char_u)));
7542 vim_free(replace_stack);
7543 }
7544 replace_stack = p;
7545 }
7546 p = replace_stack + replace_stack_nr - replace_offset;
7547 if (replace_offset)
7548 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7549 *p = c;
7550 ++replace_stack_nr;
7551}
7552
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007553#if defined(FEAT_MBYTE) || defined(PROTO)
7554/*
7555 * Push a character onto the replace stack. Handles a multi-byte character in
7556 * reverse byte order, so that the first byte is popped off first.
7557 * Return the number of bytes done (includes composing characters).
7558 */
7559 int
7560replace_push_mb(p)
7561 char_u *p;
7562{
7563 int l = (*mb_ptr2len)(p);
7564 int j;
7565
7566 for (j = l - 1; j >= 0; --j)
7567 replace_push(p[j]);
7568 return l;
7569}
7570#endif
7571
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572/*
7573 * Pop one item from the replace stack.
7574 * return -1 if stack empty
7575 * return replaced character or NUL otherwise
7576 */
7577 static int
7578replace_pop()
7579{
7580 if (replace_stack_nr == 0)
7581 return -1;
7582 return (int)replace_stack[--replace_stack_nr];
7583}
7584
7585/*
7586 * Join the top two items on the replace stack. This removes to "off"'th NUL
7587 * encountered.
7588 */
7589 static void
7590replace_join(off)
7591 int off; /* offset for which NUL to remove */
7592{
7593 int i;
7594
7595 for (i = replace_stack_nr; --i >= 0; )
7596 if (replace_stack[i] == NUL && off-- <= 0)
7597 {
7598 --replace_stack_nr;
7599 mch_memmove(replace_stack + i, replace_stack + i + 1,
7600 (size_t)(replace_stack_nr - i));
7601 return;
7602 }
7603}
7604
7605/*
7606 * Pop bytes from the replace stack until a NUL is found, and insert them
7607 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7608 */
7609 static void
7610replace_pop_ins()
7611{
7612 int cc;
7613 int oldState = State;
7614
7615 State = NORMAL; /* don't want REPLACE here */
7616 while ((cc = replace_pop()) > 0)
7617 {
7618#ifdef FEAT_MBYTE
7619 mb_replace_pop_ins(cc);
7620#else
7621 ins_char(cc);
7622#endif
7623 dec_cursor();
7624 }
7625 State = oldState;
7626}
7627
7628#ifdef FEAT_MBYTE
7629/*
7630 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7631 * indicates a multi-byte char, pop the other bytes too.
7632 */
7633 static void
7634mb_replace_pop_ins(cc)
7635 int cc;
7636{
7637 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007638 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 int i;
7640 int c;
7641
7642 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7643 {
7644 buf[0] = cc;
7645 for (i = 1; i < n; ++i)
7646 buf[i] = replace_pop();
7647 ins_bytes_len(buf, n);
7648 }
7649 else
7650 ins_char(cc);
7651
7652 if (enc_utf8)
7653 /* Handle composing chars. */
7654 for (;;)
7655 {
7656 c = replace_pop();
7657 if (c == -1) /* stack empty */
7658 break;
7659 if ((n = MB_BYTE2LEN(c)) == 1)
7660 {
7661 /* Not a multi-byte char, put it back. */
7662 replace_push(c);
7663 break;
7664 }
7665 else
7666 {
7667 buf[0] = c;
7668 for (i = 1; i < n; ++i)
7669 buf[i] = replace_pop();
7670 if (utf_iscomposing(utf_ptr2char(buf)))
7671 ins_bytes_len(buf, n);
7672 else
7673 {
7674 /* Not a composing char, put it back. */
7675 for (i = n - 1; i >= 0; --i)
7676 replace_push(buf[i]);
7677 break;
7678 }
7679 }
7680 }
7681}
7682#endif
7683
7684/*
7685 * make the replace stack empty
7686 * (called when exiting replace mode)
7687 */
7688 static void
7689replace_flush()
7690{
7691 vim_free(replace_stack);
7692 replace_stack = NULL;
7693 replace_stack_len = 0;
7694 replace_stack_nr = 0;
7695}
7696
7697/*
7698 * Handle doing a BS for one character.
7699 * cc < 0: replace stack empty, just move cursor
7700 * cc == 0: character was inserted, delete it
7701 * cc > 0: character was replaced, put cc (first byte of original char) back
7702 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007703 * When "limit_col" is >= 0, don't delete before this column. Matters when
7704 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705 */
7706 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007707replace_do_bs(limit_col)
7708 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007709{
7710 int cc;
7711#ifdef FEAT_VREPLACE
7712 int orig_len = 0;
7713 int ins_len;
7714 int orig_vcols = 0;
7715 colnr_T start_vcol;
7716 char_u *p;
7717 int i;
7718 int vcol;
7719#endif
7720
7721 cc = replace_pop();
7722 if (cc > 0)
7723 {
7724#ifdef FEAT_VREPLACE
7725 if (State & VREPLACE_FLAG)
7726 {
7727 /* Get the number of screen cells used by the character we are
7728 * going to delete. */
7729 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7730 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7731 }
7732#endif
7733#ifdef FEAT_MBYTE
7734 if (has_mbyte)
7735 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007736 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737# ifdef FEAT_VREPLACE
7738 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007739 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740# endif
7741 replace_push(cc);
7742 }
7743 else
7744#endif
7745 {
7746 pchar_cursor(cc);
7747#ifdef FEAT_VREPLACE
7748 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007749 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007750#endif
7751 }
7752 replace_pop_ins();
7753
7754#ifdef FEAT_VREPLACE
7755 if (State & VREPLACE_FLAG)
7756 {
7757 /* Get the number of screen cells used by the inserted characters */
7758 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007759 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007760 vcol = start_vcol;
7761 for (i = 0; i < ins_len; ++i)
7762 {
7763 vcol += chartabsize(p + i, vcol);
7764#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007765 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766#endif
7767 }
7768 vcol -= start_vcol;
7769
7770 /* Delete spaces that were inserted after the cursor to keep the
7771 * text aligned. */
7772 curwin->w_cursor.col += ins_len;
7773 while (vcol > orig_vcols && gchar_cursor() == ' ')
7774 {
7775 del_char(FALSE);
7776 ++orig_vcols;
7777 }
7778 curwin->w_cursor.col -= ins_len;
7779 }
7780#endif
7781
7782 /* mark the buffer as changed and prepare for displaying */
7783 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7784 }
7785 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007786 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007787}
7788
7789#ifdef FEAT_CINDENT
7790/*
7791 * Return TRUE if C-indenting is on.
7792 */
7793 static int
7794cindent_on()
7795{
7796 return (!p_paste && (curbuf->b_p_cin
7797# ifdef FEAT_EVAL
7798 || *curbuf->b_p_inde != NUL
7799# endif
7800 ));
7801}
7802#endif
7803
7804#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7805/*
7806 * Re-indent the current line, based on the current contents of it and the
7807 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7808 * confused what all the part that handles Control-T is doing that I'm not.
7809 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7810 */
7811
7812 void
7813fixthisline(get_the_indent)
7814 int (*get_the_indent) __ARGS((void));
7815{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007816 int amount = get_the_indent();
7817
7818 if (amount >= 0)
7819 {
7820 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7821 if (linewhite(curwin->w_cursor.lnum))
7822 did_ai = TRUE; /* delete the indent if the line stays empty */
7823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824}
7825
7826 void
7827fix_indent()
7828{
7829 if (p_paste)
7830 return;
7831# ifdef FEAT_LISP
7832 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7833 fixthisline(get_lisp_indent);
7834# endif
7835# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7836 else
7837# endif
7838# ifdef FEAT_CINDENT
7839 if (cindent_on())
7840 do_c_expr_indent();
7841# endif
7842}
7843
7844#endif
7845
7846#ifdef FEAT_CINDENT
7847/*
7848 * return TRUE if 'cinkeys' contains the key "keytyped",
7849 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007850 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7852 *
7853 * "keytyped" can have a few special values:
7854 * KEY_OPEN_FORW
7855 * KEY_OPEN_BACK
7856 * KEY_COMPLETE just finished completion.
7857 *
7858 * If line_is_empty is TRUE accept keys with '0' before them.
7859 */
7860 int
7861in_cinkeys(keytyped, when, line_is_empty)
7862 int keytyped;
7863 int when;
7864 int line_is_empty;
7865{
7866 char_u *look;
7867 int try_match;
7868 int try_match_word;
7869 char_u *p;
7870 char_u *line;
7871 int icase;
7872 int i;
7873
Bram Moolenaar30848942009-12-24 14:46:12 +00007874 if (keytyped == NUL)
7875 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7876 return FALSE;
7877
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878#ifdef FEAT_EVAL
7879 if (*curbuf->b_p_inde != NUL)
7880 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7881 else
7882#endif
7883 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7884 while (*look)
7885 {
7886 /*
7887 * Find out if we want to try a match with this key, depending on
7888 * 'when' and a '*' or '!' before the key.
7889 */
7890 switch (when)
7891 {
7892 case '*': try_match = (*look == '*'); break;
7893 case '!': try_match = (*look == '!'); break;
7894 default: try_match = (*look != '*'); break;
7895 }
7896 if (*look == '*' || *look == '!')
7897 ++look;
7898
7899 /*
7900 * If there is a '0', only accept a match if the line is empty.
7901 * But may still match when typing last char of a word.
7902 */
7903 if (*look == '0')
7904 {
7905 try_match_word = try_match;
7906 if (!line_is_empty)
7907 try_match = FALSE;
7908 ++look;
7909 }
7910 else
7911 try_match_word = FALSE;
7912
7913 /*
7914 * does it look like a control character?
7915 */
7916 if (*look == '^'
7917#ifdef EBCDIC
7918 && (Ctrl_chr(look[1]) != 0)
7919#else
7920 && look[1] >= '?' && look[1] <= '_'
7921#endif
7922 )
7923 {
7924 if (try_match && keytyped == Ctrl_chr(look[1]))
7925 return TRUE;
7926 look += 2;
7927 }
7928 /*
7929 * 'o' means "o" command, open forward.
7930 * 'O' means "O" command, open backward.
7931 */
7932 else if (*look == 'o')
7933 {
7934 if (try_match && keytyped == KEY_OPEN_FORW)
7935 return TRUE;
7936 ++look;
7937 }
7938 else if (*look == 'O')
7939 {
7940 if (try_match && keytyped == KEY_OPEN_BACK)
7941 return TRUE;
7942 ++look;
7943 }
7944
7945 /*
7946 * 'e' means to check for "else" at start of line and just before the
7947 * cursor.
7948 */
7949 else if (*look == 'e')
7950 {
7951 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7952 {
7953 p = ml_get_curline();
7954 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7955 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7956 return TRUE;
7957 }
7958 ++look;
7959 }
7960
7961 /*
7962 * ':' only causes an indent if it is at the end of a label or case
7963 * statement, or when it was before typing the ':' (to fix
7964 * class::method for C++).
7965 */
7966 else if (*look == ':')
7967 {
7968 if (try_match && keytyped == ':')
7969 {
7970 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007971 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007973 /* Need to get the line again after cin_islabel(). */
7974 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007975 if (curwin->w_cursor.col > 2
7976 && p[curwin->w_cursor.col - 1] == ':'
7977 && p[curwin->w_cursor.col - 2] == ':')
7978 {
7979 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007980 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007981 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982 p = ml_get_curline();
7983 p[curwin->w_cursor.col - 1] = ':';
7984 if (i)
7985 return TRUE;
7986 }
7987 }
7988 ++look;
7989 }
7990
7991
7992 /*
7993 * Is it a key in <>, maybe?
7994 */
7995 else if (*look == '<')
7996 {
7997 if (try_match)
7998 {
7999 /*
8000 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8001 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8002 * >, *, : and ! keys if they really really want to.
8003 */
8004 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8005 && keytyped == look[1])
8006 return TRUE;
8007
8008 if (keytyped == get_special_key_code(look + 1))
8009 return TRUE;
8010 }
8011 while (*look && *look != '>')
8012 look++;
8013 while (*look == '>')
8014 look++;
8015 }
8016
8017 /*
8018 * Is it a word: "=word"?
8019 */
8020 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8021 {
8022 ++look;
8023 if (*look == '~')
8024 {
8025 icase = TRUE;
8026 ++look;
8027 }
8028 else
8029 icase = FALSE;
8030 p = vim_strchr(look, ',');
8031 if (p == NULL)
8032 p = look + STRLEN(look);
8033 if ((try_match || try_match_word)
8034 && curwin->w_cursor.col >= (colnr_T)(p - look))
8035 {
8036 int match = FALSE;
8037
8038#ifdef FEAT_INS_EXPAND
8039 if (keytyped == KEY_COMPLETE)
8040 {
8041 char_u *s;
8042
8043 /* Just completed a word, check if it starts with "look".
8044 * search back for the start of a word. */
8045 line = ml_get_curline();
8046# ifdef FEAT_MBYTE
8047 if (has_mbyte)
8048 {
8049 char_u *n;
8050
8051 for (s = line + curwin->w_cursor.col; s > line; s = n)
8052 {
8053 n = mb_prevptr(line, s);
8054 if (!vim_iswordp(n))
8055 break;
8056 }
8057 }
8058 else
8059# endif
8060 for (s = line + curwin->w_cursor.col; s > line; --s)
8061 if (!vim_iswordc(s[-1]))
8062 break;
8063 if (s + (p - look) <= line + curwin->w_cursor.col
8064 && (icase
8065 ? MB_STRNICMP(s, look, p - look)
8066 : STRNCMP(s, look, p - look)) == 0)
8067 match = TRUE;
8068 }
8069 else
8070#endif
8071 /* TODO: multi-byte */
8072 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8073 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8074 {
8075 line = ml_get_cursor();
8076 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8077 || !vim_iswordc(line[-(p - look) - 1]))
8078 && (icase
8079 ? MB_STRNICMP(line - (p - look), look, p - look)
8080 : STRNCMP(line - (p - look), look, p - look))
8081 == 0)
8082 match = TRUE;
8083 }
8084 if (match && try_match_word && !try_match)
8085 {
8086 /* "0=word": Check if there are only blanks before the
8087 * word. */
8088 line = ml_get_curline();
8089 if ((int)(skipwhite(line) - line) !=
8090 (int)(curwin->w_cursor.col - (p - look)))
8091 match = FALSE;
8092 }
8093 if (match)
8094 return TRUE;
8095 }
8096 look = p;
8097 }
8098
8099 /*
8100 * ok, it's a boring generic character.
8101 */
8102 else
8103 {
8104 if (try_match && *look == keytyped)
8105 return TRUE;
8106 ++look;
8107 }
8108
8109 /*
8110 * Skip over ", ".
8111 */
8112 look = skip_to_option_part(look);
8113 }
8114 return FALSE;
8115}
8116#endif /* FEAT_CINDENT */
8117
8118#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8119/*
8120 * Map Hebrew keyboard when in hkmap mode.
8121 */
8122 int
8123hkmap(c)
8124 int c;
8125{
8126 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8127 {
8128 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8129 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8130 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8131 static char_u map[26] =
8132 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8133 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8134 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8135 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8136 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8137 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8138 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8139 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8140 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8141
8142 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8143 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8144 /* '-1'='sofit' */
8145 else if (c == 'x')
8146 return 'X';
8147 else if (c == 'q')
8148 return '\''; /* {geresh}={'} */
8149 else if (c == 246)
8150 return ' '; /* \"o --> ' ' for a german keyboard */
8151 else if (c == 228)
8152 return ' '; /* \"a --> ' ' -- / -- */
8153 else if (c == 252)
8154 return ' '; /* \"u --> ' ' -- / -- */
8155#ifdef EBCDIC
8156 else if (islower(c))
8157#else
8158 /* NOTE: islower() does not do the right thing for us on Linux so we
8159 * do this the same was as 5.7 and previous, so it works correctly on
8160 * all systems. Specifically, the e.g. Delete and Arrow keys are
8161 * munged and won't work if e.g. searching for Hebrew text.
8162 */
8163 else if (c >= 'a' && c <= 'z')
8164#endif
8165 return (int)(map[CharOrdLow(c)] + p_aleph);
8166 else
8167 return c;
8168 }
8169 else
8170 {
8171 switch (c)
8172 {
8173 case '`': return ';';
8174 case '/': return '.';
8175 case '\'': return ',';
8176 case 'q': return '/';
8177 case 'w': return '\'';
8178
8179 /* Hebrew letters - set offset from 'a' */
8180 case ',': c = '{'; break;
8181 case '.': c = 'v'; break;
8182 case ';': c = 't'; break;
8183 default: {
8184 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8185
8186#ifdef EBCDIC
8187 /* see note about islower() above */
8188 if (!islower(c))
8189#else
8190 if (c < 'a' || c > 'z')
8191#endif
8192 return c;
8193 c = str[CharOrdLow(c)];
8194 break;
8195 }
8196 }
8197
8198 return (int)(CharOrdLow(c) + p_aleph);
8199 }
8200}
8201#endif
8202
8203 static void
8204ins_reg()
8205{
8206 int need_redraw = FALSE;
8207 int regname;
8208 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008209 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210
8211 /*
8212 * If we are going to wait for a character, show a '"'.
8213 */
8214 pc_status = PC_STATUS_UNSET;
8215 if (redrawing() && !char_avail())
8216 {
8217 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008218 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219
8220 edit_putchar('"', TRUE);
8221#ifdef FEAT_CMDL_INFO
8222 add_to_showcmd_c(Ctrl_R);
8223#endif
8224 }
8225
8226#ifdef USE_ON_FLY_SCROLL
8227 dont_scroll = TRUE; /* disallow scrolling here */
8228#endif
8229
8230 /*
8231 * Don't map the register name. This also prevents the mode message to be
8232 * deleted when ESC is hit.
8233 */
8234 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008235 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8238 {
8239 /* Get a third key for literal register insertion */
8240 literally = regname;
8241#ifdef FEAT_CMDL_INFO
8242 add_to_showcmd_c(literally);
8243#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008244 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 }
8247 --no_mapping;
8248
8249#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008250 /* Don't call u_sync() while typing the expression or giving an error
8251 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 ++no_u_sync;
8253 if (regname == '=')
8254 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008255# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008257# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008258 /* Sync undo when evaluating the expression calls setline() or
8259 * append(), so that it can be undone separately. */
8260 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008261
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008263# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 /* Restore the Input Method. */
8265 if (im_on)
8266 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008267# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008268 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008269 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8270 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008271 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008273 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 else
8275 {
8276#endif
8277 if (literally == Ctrl_O || literally == Ctrl_P)
8278 {
8279 /* Append the command to the redo buffer. */
8280 AppendCharToRedobuff(Ctrl_R);
8281 AppendCharToRedobuff(literally);
8282 AppendCharToRedobuff(regname);
8283
8284 do_put(regname, BACKWARD, 1L,
8285 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8286 }
8287 else if (insert_reg(regname, literally) == FAIL)
8288 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008289 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 need_redraw = TRUE; /* remove the '"' */
8291 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008292 else if (stop_insert_mode)
8293 /* When the '=' register was used and a function was invoked that
8294 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8295 * insert anything, need to remove the '"' */
8296 need_redraw = TRUE;
8297
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298#ifdef FEAT_EVAL
8299 }
8300 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008301 if (u_sync_once == 1)
8302 ins_need_undo = TRUE;
8303 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304#endif
8305#ifdef FEAT_CMDL_INFO
8306 clear_showcmd();
8307#endif
8308
8309 /* If the inserted register is empty, we need to remove the '"' */
8310 if (need_redraw || stuff_empty())
8311 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008312
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008313 /* Disallow starting Visual mode here, would get a weird mode. */
8314 if (!vis_active && VIsual_active)
8315 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316}
8317
8318/*
8319 * CTRL-G commands in Insert mode.
8320 */
8321 static void
8322ins_ctrl_g()
8323{
8324 int c;
8325
8326#ifdef FEAT_INS_EXPAND
8327 /* Right after CTRL-X the cursor will be after the ruler. */
8328 setcursor();
8329#endif
8330
8331 /*
8332 * Don't map the second key. This also prevents the mode message to be
8333 * deleted when ESC is hit.
8334 */
8335 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008336 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 --no_mapping;
8338 switch (c)
8339 {
8340 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8341 case K_UP:
8342 case Ctrl_K:
8343 case 'k': ins_up(TRUE);
8344 break;
8345
8346 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8347 case K_DOWN:
8348 case Ctrl_J:
8349 case 'j': ins_down(TRUE);
8350 break;
8351
8352 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008353 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008355
8356 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008357 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008358 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008359 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 break;
8361
8362 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008363 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 }
8365}
8366
8367/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008368 * CTRL-^ in Insert mode.
8369 */
8370 static void
8371ins_ctrl_hat()
8372{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008373 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008374 {
8375 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8376 if (State & LANGMAP)
8377 {
8378 curbuf->b_p_iminsert = B_IMODE_NONE;
8379 State &= ~LANGMAP;
8380 }
8381 else
8382 {
8383 curbuf->b_p_iminsert = B_IMODE_LMAP;
8384 State |= LANGMAP;
8385#ifdef USE_IM_CONTROL
8386 im_set_active(FALSE);
8387#endif
8388 }
8389 }
8390#ifdef USE_IM_CONTROL
8391 else
8392 {
8393 /* There are no ":lmap" mappings, toggle IM */
8394 if (im_get_status())
8395 {
8396 curbuf->b_p_iminsert = B_IMODE_NONE;
8397 im_set_active(FALSE);
8398 }
8399 else
8400 {
8401 curbuf->b_p_iminsert = B_IMODE_IM;
8402 State &= ~LANGMAP;
8403 im_set_active(TRUE);
8404 }
8405 }
8406#endif
8407 set_iminsert_global();
8408 showmode();
8409#ifdef FEAT_GUI
8410 /* may show different cursor shape or color */
8411 if (gui.in_use)
8412 gui_update_cursor(TRUE, FALSE);
8413#endif
8414#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8415 /* Show/unshow value of 'keymap' in status lines. */
8416 status_redraw_curbuf();
8417#endif
8418}
8419
8420/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 * Handle ESC in insert mode.
8422 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8423 * insert.
8424 */
8425 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008426ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427 long *count;
8428 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008429 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430{
8431 int temp;
8432 static int disabled_redraw = FALSE;
8433
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008434#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008435 check_spell_redraw();
8436#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008437#if defined(FEAT_HANGULIN)
8438# if defined(ESC_CHG_TO_ENG_MODE)
8439 hangul_input_state_set(0);
8440# endif
8441 if (composing_hangul)
8442 {
8443 push_raw_key(composing_hangul_buffer, 2);
8444 composing_hangul = 0;
8445 }
8446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447
8448 temp = curwin->w_cursor.col;
8449 if (disabled_redraw)
8450 {
8451 --RedrawingDisabled;
8452 disabled_redraw = FALSE;
8453 }
8454 if (!arrow_used)
8455 {
8456 /*
8457 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008458 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8459 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008460 */
8461 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008462 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463
8464 /*
8465 * Repeating insert may take a long time. Check for
8466 * interrupt now and then.
8467 */
8468 if (*count > 0)
8469 {
8470 line_breakcheck();
8471 if (got_int)
8472 *count = 0;
8473 }
8474
8475 if (--*count > 0) /* repeat what was typed */
8476 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008477 /* Vi repeats the insert without replacing characters. */
8478 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8479 State &= ~REPLACE_FLAG;
8480
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 (void)start_redo_ins();
8482 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008483 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 ++RedrawingDisabled;
8485 disabled_redraw = TRUE;
8486 return FALSE; /* repeat the insert */
8487 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008488 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 undisplay_dollar();
8490 }
8491
8492 /* When an autoindent was removed, curswant stays after the
8493 * indent */
8494 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8495 curwin->w_set_curswant = TRUE;
8496
8497 /* Remember the last Insert position in the '^ mark. */
8498 if (!cmdmod.keepjumps)
8499 curbuf->b_last_insert = curwin->w_cursor;
8500
8501 /*
8502 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008503 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008505 if (!nomove
8506 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507#ifdef FEAT_VIRTUALEDIT
8508 || curwin->w_cursor.coladd > 0
8509#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008510 )
8511 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008512 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513#ifdef FEAT_RIGHTLEFT
8514 && !revins_on
8515#endif
8516 )
8517 {
8518#ifdef FEAT_VIRTUALEDIT
8519 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8520 {
8521 oneleft();
8522 if (restart_edit != NUL)
8523 ++curwin->w_cursor.coladd;
8524 }
8525 else
8526#endif
8527 {
8528 --curwin->w_cursor.col;
8529#ifdef FEAT_MBYTE
8530 /* Correct cursor for multi-byte character. */
8531 if (has_mbyte)
8532 mb_adjust_cursor();
8533#endif
8534 }
8535 }
8536
8537#ifdef USE_IM_CONTROL
8538 /* Disable IM to allow typing English directly for Normal mode commands.
8539 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8540 * well). */
8541 if (!(State & LANGMAP))
8542 im_save_status(&curbuf->b_p_iminsert);
8543 im_set_active(FALSE);
8544#endif
8545
8546 State = NORMAL;
8547 /* need to position cursor again (e.g. when on a TAB ) */
8548 changed_cline_bef_curs();
8549
8550#ifdef FEAT_MOUSE
8551 setmouse();
8552#endif
8553#ifdef CURSOR_SHAPE
8554 ui_cursor_shape(); /* may show different cursor shape */
8555#endif
8556
8557 /*
8558 * When recording or for CTRL-O, need to display the new mode.
8559 * Otherwise remove the mode message.
8560 */
8561 if (Recording || restart_edit != NUL)
8562 showmode();
8563 else if (p_smd)
8564 MSG("");
8565
8566 return TRUE; /* exit Insert mode */
8567}
8568
8569#ifdef FEAT_RIGHTLEFT
8570/*
8571 * Toggle language: hkmap and revins_on.
8572 * Move to end of reverse inserted text.
8573 */
8574 static void
8575ins_ctrl_()
8576{
8577 if (revins_on && revins_chars && revins_scol >= 0)
8578 {
8579 while (gchar_cursor() != NUL && revins_chars--)
8580 ++curwin->w_cursor.col;
8581 }
8582 p_ri = !p_ri;
8583 revins_on = (State == INSERT && p_ri);
8584 if (revins_on)
8585 {
8586 revins_scol = curwin->w_cursor.col;
8587 revins_legal++;
8588 revins_chars = 0;
8589 undisplay_dollar();
8590 }
8591 else
8592 revins_scol = -1;
8593#ifdef FEAT_FKMAP
8594 if (p_altkeymap)
8595 {
8596 /*
8597 * to be consistent also for redo command, using '.'
8598 * set arrow_used to true and stop it - causing to redo
8599 * characters entered in one mode (normal/reverse insert).
8600 */
8601 arrow_used = TRUE;
8602 (void)stop_arrow();
8603 p_fkmap = curwin->w_p_rl ^ p_ri;
8604 if (p_fkmap && p_ri)
8605 State = INSERT;
8606 }
8607 else
8608#endif
8609 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8610 showmode();
8611}
8612#endif
8613
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614/*
8615 * If 'keymodel' contains "startsel", may start selection.
8616 * Returns TRUE when a CTRL-O and other keys stuffed.
8617 */
8618 static int
8619ins_start_select(c)
8620 int c;
8621{
8622 if (km_startsel)
8623 switch (c)
8624 {
8625 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 case K_PAGEUP:
8628 case K_KPAGEUP:
8629 case K_PAGEDOWN:
8630 case K_KPAGEDOWN:
8631# ifdef MACOS
8632 case K_LEFT:
8633 case K_RIGHT:
8634 case K_UP:
8635 case K_DOWN:
8636 case K_END:
8637 case K_HOME:
8638# endif
8639 if (!(mod_mask & MOD_MASK_SHIFT))
8640 break;
8641 /* FALLTHROUGH */
8642 case K_S_LEFT:
8643 case K_S_RIGHT:
8644 case K_S_UP:
8645 case K_S_DOWN:
8646 case K_S_END:
8647 case K_S_HOME:
8648 /* Start selection right away, the cursor can move with
8649 * CTRL-O when beyond the end of the line. */
8650 start_selection();
8651
8652 /* Execute the key in (insert) Select mode. */
8653 stuffcharReadbuff(Ctrl_O);
8654 if (mod_mask)
8655 {
8656 char_u buf[4];
8657
8658 buf[0] = K_SPECIAL;
8659 buf[1] = KS_MODIFIER;
8660 buf[2] = mod_mask;
8661 buf[3] = NUL;
8662 stuffReadbuff(buf);
8663 }
8664 stuffcharReadbuff(c);
8665 return TRUE;
8666 }
8667 return FALSE;
8668}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669
8670/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008671 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008672 */
8673 static void
8674ins_insert(replaceState)
8675 int replaceState;
8676{
8677#ifdef FEAT_FKMAP
8678 if (p_fkmap && p_ri)
8679 {
8680 beep_flush();
8681 EMSG(farsi_text_3); /* encoded in Farsi */
8682 return;
8683 }
8684#endif
8685
8686#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008687# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008688 set_vim_var_string(VV_INSERTMODE,
8689 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008690# ifdef FEAT_VREPLACE
8691 replaceState == VREPLACE ? "v" :
8692# endif
8693 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008694# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008695 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8696#endif
8697 if (State & REPLACE_FLAG)
8698 State = INSERT | (State & LANGMAP);
8699 else
8700 State = replaceState | (State & LANGMAP);
8701 AppendCharToRedobuff(K_INS);
8702 showmode();
8703#ifdef CURSOR_SHAPE
8704 ui_cursor_shape(); /* may show different cursor shape */
8705#endif
8706}
8707
8708/*
8709 * Pressed CTRL-O in Insert mode.
8710 */
8711 static void
8712ins_ctrl_o()
8713{
8714#ifdef FEAT_VREPLACE
8715 if (State & VREPLACE_FLAG)
8716 restart_edit = 'V';
8717 else
8718#endif
8719 if (State & REPLACE_FLAG)
8720 restart_edit = 'R';
8721 else
8722 restart_edit = 'I';
8723#ifdef FEAT_VIRTUALEDIT
8724 if (virtual_active())
8725 ins_at_eol = FALSE; /* cursor always keeps its column */
8726 else
8727#endif
8728 ins_at_eol = (gchar_cursor() == NUL);
8729}
8730
8731/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 * If the cursor is on an indent, ^T/^D insert/delete one
8733 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008734 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 * with vi. But vi only supports ^T and ^D after an
8736 * autoindent, we support it everywhere.
8737 */
8738 static void
8739ins_shift(c, lastc)
8740 int c;
8741 int lastc;
8742{
8743 if (stop_arrow() == FAIL)
8744 return;
8745 AppendCharToRedobuff(c);
8746
8747 /*
8748 * 0^D and ^^D: remove all indent.
8749 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008750 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8751 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752 {
8753 --curwin->w_cursor.col;
8754 (void)del_char(FALSE); /* delete the '^' or '0' */
8755 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8756 if (State & REPLACE_FLAG)
8757 replace_pop_ins();
8758 if (lastc == '^')
8759 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008760 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761 }
8762 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008763 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764
8765 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8766 did_ai = FALSE;
8767#ifdef FEAT_SMARTINDENT
8768 did_si = FALSE;
8769 can_si = FALSE;
8770 can_si_back = FALSE;
8771#endif
8772#ifdef FEAT_CINDENT
8773 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8774#endif
8775}
8776
8777 static void
8778ins_del()
8779{
8780 int temp;
8781
8782 if (stop_arrow() == FAIL)
8783 return;
8784 if (gchar_cursor() == NUL) /* delete newline */
8785 {
8786 temp = curwin->w_cursor.col;
8787 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008788 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008789 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008790 else
8791 curwin->w_cursor.col = temp;
8792 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008793 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8794 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008795 did_ai = FALSE;
8796#ifdef FEAT_SMARTINDENT
8797 did_si = FALSE;
8798 can_si = FALSE;
8799 can_si_back = FALSE;
8800#endif
8801 AppendCharToRedobuff(K_DEL);
8802}
8803
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008804static void ins_bs_one __ARGS((colnr_T *vcolp));
8805
8806/*
8807 * Delete one character for ins_bs().
8808 */
8809 static void
8810ins_bs_one(vcolp)
8811 colnr_T *vcolp;
8812{
8813 dec_cursor();
8814 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8815 if (State & REPLACE_FLAG)
8816 {
8817 /* Don't delete characters before the insert point when in
8818 * Replace mode */
8819 if (curwin->w_cursor.lnum != Insstart.lnum
8820 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008821 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008822 }
8823 else
8824 (void)del_char(FALSE);
8825}
8826
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827/*
8828 * Handle Backspace, delete-word and delete-line in Insert mode.
8829 * Return TRUE when backspace was actually used.
8830 */
8831 static int
8832ins_bs(c, mode, inserted_space_p)
8833 int c;
8834 int mode;
8835 int *inserted_space_p;
8836{
8837 linenr_T lnum;
8838 int cc;
8839 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008840 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 colnr_T mincol;
8842 int did_backspace = FALSE;
8843 int in_indent;
8844 int oldState;
8845#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008846 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847#endif
8848
8849 /*
8850 * can't delete anything in an empty file
8851 * can't backup past first character in buffer
8852 * can't backup past starting point unless 'backspace' > 1
8853 * can backup to a previous line if 'backspace' == 0
8854 */
8855 if ( bufempty()
8856 || (
8857#ifdef FEAT_RIGHTLEFT
8858 !revins_on &&
8859#endif
8860 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8861 || (!can_bs(BS_START)
8862 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008863 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8864 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8866 && curwin->w_cursor.col <= ai_col)
8867 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8868 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008869 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 return FALSE;
8871 }
8872
8873 if (stop_arrow() == FAIL)
8874 return FALSE;
8875 in_indent = inindent(0);
8876#ifdef FEAT_CINDENT
8877 if (in_indent)
8878 can_cindent = FALSE;
8879#endif
8880#ifdef FEAT_COMMENTS
8881 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8882#endif
8883#ifdef FEAT_RIGHTLEFT
8884 if (revins_on) /* put cursor after last inserted char */
8885 inc_cursor();
8886#endif
8887
8888#ifdef FEAT_VIRTUALEDIT
8889 /* Virtualedit:
8890 * BACKSPACE_CHAR eats a virtual space
8891 * BACKSPACE_WORD eats all coladd
8892 * BACKSPACE_LINE eats all coladd and keeps going
8893 */
8894 if (curwin->w_cursor.coladd > 0)
8895 {
8896 if (mode == BACKSPACE_CHAR)
8897 {
8898 --curwin->w_cursor.coladd;
8899 return TRUE;
8900 }
8901 if (mode == BACKSPACE_WORD)
8902 {
8903 curwin->w_cursor.coladd = 0;
8904 return TRUE;
8905 }
8906 curwin->w_cursor.coladd = 0;
8907 }
8908#endif
8909
8910 /*
8911 * delete newline!
8912 */
8913 if (curwin->w_cursor.col == 0)
8914 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008915 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008916 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917#ifdef FEAT_RIGHTLEFT
8918 || revins_on
8919#endif
8920 )
8921 {
8922 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8923 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8924 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008925 --Insstart.lnum;
8926 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 }
8928 /*
8929 * In replace mode:
8930 * cc < 0: NL was inserted, delete it
8931 * cc >= 0: NL was replaced, put original characters back
8932 */
8933 cc = -1;
8934 if (State & REPLACE_FLAG)
8935 cc = replace_pop(); /* returns -1 if NL was inserted */
8936 /*
8937 * In replace mode, in the line we started replacing, we only move the
8938 * cursor.
8939 */
8940 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8941 {
8942 dec_cursor();
8943 }
8944 else
8945 {
8946#ifdef FEAT_VREPLACE
8947 if (!(State & VREPLACE_FLAG)
8948 || curwin->w_cursor.lnum > orig_line_count)
8949#endif
8950 {
8951 temp = gchar_cursor(); /* remember current char */
8952 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008953
8954 /* When "aw" is in 'formatoptions' we must delete the space at
8955 * the end of the line, otherwise the line will be broken
8956 * again when auto-formatting. */
8957 if (has_format_option(FO_AUTO)
8958 && has_format_option(FO_WHITE_PAR))
8959 {
8960 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8961 TRUE);
8962 int len;
8963
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008964 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008965 if (len > 0 && ptr[len - 1] == ' ')
8966 ptr[len - 1] = NUL;
8967 }
8968
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008969 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 if (temp == NUL && gchar_cursor() != NUL)
8971 inc_cursor();
8972 }
8973#ifdef FEAT_VREPLACE
8974 else
8975 dec_cursor();
8976#endif
8977
8978 /*
8979 * In REPLACE mode we have to put back the text that was replaced
8980 * by the NL. On the replace stack is first a NUL-terminated
8981 * sequence of characters that were deleted and then the
8982 * characters that NL replaced.
8983 */
8984 if (State & REPLACE_FLAG)
8985 {
8986 /*
8987 * Do the next ins_char() in NORMAL state, to
8988 * prevent ins_char() from replacing characters and
8989 * avoiding showmatch().
8990 */
8991 oldState = State;
8992 State = NORMAL;
8993 /*
8994 * restore characters (blanks) deleted after cursor
8995 */
8996 while (cc > 0)
8997 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008998 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008999#ifdef FEAT_MBYTE
9000 mb_replace_pop_ins(cc);
9001#else
9002 ins_char(cc);
9003#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009004 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005 cc = replace_pop();
9006 }
9007 /* restore the characters that NL replaced */
9008 replace_pop_ins();
9009 State = oldState;
9010 }
9011 }
9012 did_ai = FALSE;
9013 }
9014 else
9015 {
9016 /*
9017 * Delete character(s) before the cursor.
9018 */
9019#ifdef FEAT_RIGHTLEFT
9020 if (revins_on) /* put cursor on last inserted char */
9021 dec_cursor();
9022#endif
9023 mincol = 0;
9024 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009025 if (mode == BACKSPACE_LINE
9026 && (curbuf->b_p_ai
9027#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009028 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009029#endif
9030 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031#ifdef FEAT_RIGHTLEFT
9032 && !revins_on
9033#endif
9034 )
9035 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009036 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009038 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009039 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009040 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041 }
9042
9043 /*
9044 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9045 */
9046 if ( mode == BACKSPACE_CHAR
9047 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009048 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009049 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050 && (*(ml_get_cursor() - 1) == TAB
9051 || (*(ml_get_cursor() - 1) == ' '
9052 && (!*inserted_space_p
9053 || arrow_used))))))
9054 {
9055 int ts;
9056 colnr_T vcol;
9057 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009058 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059
9060 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009061 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009062 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009064 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 /* Compute the virtual column where we want to be. Since
9066 * 'showbreak' may get in the way, need to get the last column of
9067 * the previous character. */
9068 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009069 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 dec_cursor();
9071 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9072 inc_cursor();
9073 want_vcol = (want_vcol / ts) * ts;
9074
9075 /* delete characters until we are at or before want_vcol */
9076 while (vcol > want_vcol
9077 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009078 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079
9080 /* insert extra spaces until we are at want_vcol */
9081 while (vcol < want_vcol)
9082 {
9083 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009084 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9085 && curwin->w_cursor.col < Insstart_orig.col)
9086 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087
9088#ifdef FEAT_VREPLACE
9089 if (State & VREPLACE_FLAG)
9090 ins_char(' ');
9091 else
9092#endif
9093 {
9094 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009095 if ((State & REPLACE_FLAG))
9096 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009097 }
9098 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9099 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009100
9101 /* If we are now back where we started delete one character. Can
9102 * happen when using 'sts' and 'linebreak'. */
9103 if (vcol >= start_vcol)
9104 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 }
9106
9107 /*
9108 * Delete upto starting point, start of line or previous word.
9109 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009110 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009111 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009112#ifdef FEAT_MBYTE
9113 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009115 if (has_mbyte)
9116 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009117#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009118 do
9119 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009121 if (!revins_on) /* put cursor on char to be deleted */
9122#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009123 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009124
9125 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009126#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009127 /* look multi-byte character class */
9128 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009130 prev_cclass = cclass;
9131 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009133#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009134
9135 /* start of word? */
9136 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9137 {
9138 mode = BACKSPACE_WORD_NOT_SPACE;
9139 temp = vim_iswordc(cc);
9140 }
9141 /* end of word? */
9142 else if (mode == BACKSPACE_WORD_NOT_SPACE
9143 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9144#ifdef FEAT_MBYTE
9145 || prev_cclass != cclass
9146#endif
9147 ))
9148 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009150 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009152 inc_cursor();
9153#ifdef FEAT_RIGHTLEFT
9154 else if (State & REPLACE_FLAG)
9155 dec_cursor();
9156#endif
9157 break;
9158 }
9159 if (State & REPLACE_FLAG)
9160 replace_do_bs(-1);
9161 else
9162 {
9163#ifdef FEAT_MBYTE
9164 if (enc_utf8 && p_deco)
9165 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9166#endif
9167 (void)del_char(FALSE);
9168#ifdef FEAT_MBYTE
9169 /*
9170 * If there are combining characters and 'delcombine' is set
9171 * move the cursor back. Don't back up before the base
9172 * character.
9173 */
9174 if (enc_utf8 && p_deco && cpc[0] != NUL)
9175 inc_cursor();
9176#endif
9177#ifdef FEAT_RIGHTLEFT
9178 if (revins_chars)
9179 {
9180 revins_chars--;
9181 revins_legal++;
9182 }
9183 if (revins_on && gchar_cursor() == NUL)
9184 break;
9185#endif
9186 }
9187 /* Just a single backspace?: */
9188 if (mode == BACKSPACE_CHAR)
9189 break;
9190 } while (
9191#ifdef FEAT_RIGHTLEFT
9192 revins_on ||
9193#endif
9194 (curwin->w_cursor.col > mincol
9195 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9196 || curwin->w_cursor.col != Insstart_orig.col)));
9197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198 did_backspace = TRUE;
9199 }
9200#ifdef FEAT_SMARTINDENT
9201 did_si = FALSE;
9202 can_si = FALSE;
9203 can_si_back = FALSE;
9204#endif
9205 if (curwin->w_cursor.col <= 1)
9206 did_ai = FALSE;
9207 /*
9208 * It's a little strange to put backspaces into the redo
9209 * buffer, but it makes auto-indent a lot easier to deal
9210 * with.
9211 */
9212 AppendCharToRedobuff(c);
9213
9214 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009215 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9216 && curwin->w_cursor.col < Insstart_orig.col)
9217 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218
9219 /* vi behaviour: the cursor moves backward but the character that
9220 * was there remains visible
9221 * Vim behaviour: the cursor moves backward and the character that
9222 * was there is erased from the screen.
9223 * We can emulate the vi behaviour by pretending there is a dollar
9224 * displayed even when there isn't.
9225 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009226 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009227 dollar_vcol = curwin->w_virtcol;
9228
Bram Moolenaarce3be472008-01-14 19:12:28 +00009229#ifdef FEAT_FOLDING
9230 /* When deleting a char the cursor line must never be in a closed fold.
9231 * E.g., when 'foldmethod' is indent and deleting the first non-white
9232 * char before a Tab. */
9233 if (did_backspace)
9234 foldOpenCursor();
9235#endif
9236
Bram Moolenaar071d4272004-06-13 20:20:40 +00009237 return did_backspace;
9238}
9239
9240#ifdef FEAT_MOUSE
9241 static void
9242ins_mouse(c)
9243 int c;
9244{
9245 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009246 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247
9248# ifdef FEAT_GUI
9249 /* When GUI is active, also move/paste when 'mouse' is empty */
9250 if (!gui.in_use)
9251# endif
9252 if (!mouse_has(MOUSE_INSERT))
9253 return;
9254
9255 undisplay_dollar();
9256 tpos = curwin->w_cursor;
9257 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9258 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009259#ifdef FEAT_WINDOWS
9260 win_T *new_curwin = curwin;
9261
9262 if (curwin != old_curwin && win_valid(old_curwin))
9263 {
9264 /* Mouse took us to another window. We need to go back to the
9265 * previous one to stop insert there properly. */
9266 curwin = old_curwin;
9267 curbuf = curwin->w_buffer;
9268 }
9269#endif
9270 start_arrow(curwin == old_curwin ? &tpos : NULL);
9271#ifdef FEAT_WINDOWS
9272 if (curwin != new_curwin && win_valid(new_curwin))
9273 {
9274 curwin = new_curwin;
9275 curbuf = curwin->w_buffer;
9276 }
9277#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278# ifdef FEAT_CINDENT
9279 can_cindent = TRUE;
9280# endif
9281 }
9282
9283#ifdef FEAT_WINDOWS
9284 /* redraw status lines (in case another window became active) */
9285 redraw_statuslines();
9286#endif
9287}
9288
9289 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009290ins_mousescroll(dir)
9291 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292{
9293 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009294# if defined(FEAT_WINDOWS)
9295 win_T *old_curwin = curwin;
9296# endif
9297# ifdef FEAT_INS_EXPAND
9298 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299# endif
9300
9301 tpos = curwin->w_cursor;
9302
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009303# ifdef FEAT_WINDOWS
9304 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009305 {
9306 int row, col;
9307
9308 row = mouse_row;
9309 col = mouse_col;
9310
9311 /* find the window at the pointer coordinates */
9312 curwin = mouse_find_win(&row, &col);
9313 curbuf = curwin->w_buffer;
9314 }
9315 if (curwin == old_curwin)
9316# endif
9317 undisplay_dollar();
9318
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009319# ifdef FEAT_INS_EXPAND
9320 /* Don't scroll the window in which completion is being done. */
9321 if (!pum_visible()
9322# if defined(FEAT_WINDOWS)
9323 || curwin != old_curwin
9324# endif
9325 )
9326# endif
9327 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009328 if (dir == MSCR_DOWN || dir == MSCR_UP)
9329 {
9330 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9331 scroll_redraw(dir,
9332 (long)(curwin->w_botline - curwin->w_topline));
9333 else
9334 scroll_redraw(dir, 3L);
9335 }
9336#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009337 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009338 {
9339 int val, step = 6;
9340
9341 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9342 step = W_WIDTH(curwin);
9343 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9344 if (val < 0)
9345 val = 0;
9346 gui_do_horiz_scroll(val, TRUE);
9347 }
9348#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009349# ifdef FEAT_INS_EXPAND
9350 did_scroll = TRUE;
9351# endif
9352 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009353
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009354# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355 curwin->w_redr_status = TRUE;
9356
9357 curwin = old_curwin;
9358 curbuf = curwin->w_buffer;
9359# endif
9360
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009361# ifdef FEAT_INS_EXPAND
9362 /* The popup menu may overlay the window, need to redraw it.
9363 * TODO: Would be more efficient to only redraw the windows that are
9364 * overlapped by the popup menu. */
9365 if (pum_visible() && did_scroll)
9366 {
9367 redraw_all_later(NOT_VALID);
9368 ins_compl_show_pum();
9369 }
9370# endif
9371
Bram Moolenaar071d4272004-06-13 20:20:40 +00009372 if (!equalpos(curwin->w_cursor, tpos))
9373 {
9374 start_arrow(&tpos);
9375# ifdef FEAT_CINDENT
9376 can_cindent = TRUE;
9377# endif
9378 }
9379}
9380#endif
9381
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009382#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009383 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009384ins_tabline(c)
9385 int c;
9386{
9387 /* We will be leaving the current window, unless closing another tab. */
9388 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9389 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9390 {
9391 undisplay_dollar();
9392 start_arrow(&curwin->w_cursor);
9393# ifdef FEAT_CINDENT
9394 can_cindent = TRUE;
9395# endif
9396 }
9397
9398 if (c == K_TABLINE)
9399 goto_tabpage(current_tab);
9400 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009401 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009402 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009403 redraw_statuslines(); /* will redraw the tabline when needed */
9404 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009405}
9406#endif
9407
9408#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009409 void
9410ins_scroll()
9411{
9412 pos_T tpos;
9413
9414 undisplay_dollar();
9415 tpos = curwin->w_cursor;
9416 if (gui_do_scroll())
9417 {
9418 start_arrow(&tpos);
9419# ifdef FEAT_CINDENT
9420 can_cindent = TRUE;
9421# endif
9422 }
9423}
9424
9425 void
9426ins_horscroll()
9427{
9428 pos_T tpos;
9429
9430 undisplay_dollar();
9431 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009432 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009433 {
9434 start_arrow(&tpos);
9435# ifdef FEAT_CINDENT
9436 can_cindent = TRUE;
9437# endif
9438 }
9439}
9440#endif
9441
9442 static void
9443ins_left()
9444{
9445 pos_T tpos;
9446
9447#ifdef FEAT_FOLDING
9448 if ((fdo_flags & FDO_HOR) && KeyTyped)
9449 foldOpenCursor();
9450#endif
9451 undisplay_dollar();
9452 tpos = curwin->w_cursor;
9453 if (oneleft() == OK)
9454 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009455#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9456 /* Only call start_arrow() when not busy with preediting, it will
9457 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9458 if (!im_is_preediting())
9459#endif
9460 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009461#ifdef FEAT_RIGHTLEFT
9462 /* If exit reversed string, position is fixed */
9463 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9464 revins_legal++;
9465 revins_chars++;
9466#endif
9467 }
9468
9469 /*
9470 * if 'whichwrap' set for cursor in insert mode may go to
9471 * previous line
9472 */
9473 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9474 {
9475 start_arrow(&tpos);
9476 --(curwin->w_cursor.lnum);
9477 coladvance((colnr_T)MAXCOL);
9478 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9479 }
9480 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009481 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009482}
9483
9484 static void
9485ins_home(c)
9486 int c;
9487{
9488 pos_T tpos;
9489
9490#ifdef FEAT_FOLDING
9491 if ((fdo_flags & FDO_HOR) && KeyTyped)
9492 foldOpenCursor();
9493#endif
9494 undisplay_dollar();
9495 tpos = curwin->w_cursor;
9496 if (c == K_C_HOME)
9497 curwin->w_cursor.lnum = 1;
9498 curwin->w_cursor.col = 0;
9499#ifdef FEAT_VIRTUALEDIT
9500 curwin->w_cursor.coladd = 0;
9501#endif
9502 curwin->w_curswant = 0;
9503 start_arrow(&tpos);
9504}
9505
9506 static void
9507ins_end(c)
9508 int c;
9509{
9510 pos_T tpos;
9511
9512#ifdef FEAT_FOLDING
9513 if ((fdo_flags & FDO_HOR) && KeyTyped)
9514 foldOpenCursor();
9515#endif
9516 undisplay_dollar();
9517 tpos = curwin->w_cursor;
9518 if (c == K_C_END)
9519 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9520 coladvance((colnr_T)MAXCOL);
9521 curwin->w_curswant = MAXCOL;
9522
9523 start_arrow(&tpos);
9524}
9525
9526 static void
9527ins_s_left()
9528{
9529#ifdef FEAT_FOLDING
9530 if ((fdo_flags & FDO_HOR) && KeyTyped)
9531 foldOpenCursor();
9532#endif
9533 undisplay_dollar();
9534 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9535 {
9536 start_arrow(&curwin->w_cursor);
9537 (void)bck_word(1L, FALSE, FALSE);
9538 curwin->w_set_curswant = TRUE;
9539 }
9540 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009541 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542}
9543
9544 static void
9545ins_right()
9546{
9547#ifdef FEAT_FOLDING
9548 if ((fdo_flags & FDO_HOR) && KeyTyped)
9549 foldOpenCursor();
9550#endif
9551 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009552 if (gchar_cursor() != NUL
9553#ifdef FEAT_VIRTUALEDIT
9554 || virtual_active()
9555#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556 )
9557 {
9558 start_arrow(&curwin->w_cursor);
9559 curwin->w_set_curswant = TRUE;
9560#ifdef FEAT_VIRTUALEDIT
9561 if (virtual_active())
9562 oneright();
9563 else
9564#endif
9565 {
9566#ifdef FEAT_MBYTE
9567 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009568 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 else
9570#endif
9571 ++curwin->w_cursor.col;
9572 }
9573
9574#ifdef FEAT_RIGHTLEFT
9575 revins_legal++;
9576 if (revins_chars)
9577 revins_chars--;
9578#endif
9579 }
9580 /* if 'whichwrap' set for cursor in insert mode, may move the
9581 * cursor to the next line */
9582 else if (vim_strchr(p_ww, ']') != NULL
9583 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9584 {
9585 start_arrow(&curwin->w_cursor);
9586 curwin->w_set_curswant = TRUE;
9587 ++curwin->w_cursor.lnum;
9588 curwin->w_cursor.col = 0;
9589 }
9590 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009591 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592}
9593
9594 static void
9595ins_s_right()
9596{
9597#ifdef FEAT_FOLDING
9598 if ((fdo_flags & FDO_HOR) && KeyTyped)
9599 foldOpenCursor();
9600#endif
9601 undisplay_dollar();
9602 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9603 || gchar_cursor() != NUL)
9604 {
9605 start_arrow(&curwin->w_cursor);
9606 (void)fwd_word(1L, FALSE, 0);
9607 curwin->w_set_curswant = TRUE;
9608 }
9609 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009610 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611}
9612
9613 static void
9614ins_up(startcol)
9615 int startcol; /* when TRUE move to Insstart.col */
9616{
9617 pos_T tpos;
9618 linenr_T old_topline = curwin->w_topline;
9619#ifdef FEAT_DIFF
9620 int old_topfill = curwin->w_topfill;
9621#endif
9622
9623 undisplay_dollar();
9624 tpos = curwin->w_cursor;
9625 if (cursor_up(1L, TRUE) == OK)
9626 {
9627 if (startcol)
9628 coladvance(getvcol_nolist(&Insstart));
9629 if (old_topline != curwin->w_topline
9630#ifdef FEAT_DIFF
9631 || old_topfill != curwin->w_topfill
9632#endif
9633 )
9634 redraw_later(VALID);
9635 start_arrow(&tpos);
9636#ifdef FEAT_CINDENT
9637 can_cindent = TRUE;
9638#endif
9639 }
9640 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009641 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642}
9643
9644 static void
9645ins_pageup()
9646{
9647 pos_T tpos;
9648
9649 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009650
9651#ifdef FEAT_WINDOWS
9652 if (mod_mask & MOD_MASK_CTRL)
9653 {
9654 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009655 if (first_tabpage->tp_next != NULL)
9656 {
9657 start_arrow(&curwin->w_cursor);
9658 goto_tabpage(-1);
9659 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009660 return;
9661 }
9662#endif
9663
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 tpos = curwin->w_cursor;
9665 if (onepage(BACKWARD, 1L) == OK)
9666 {
9667 start_arrow(&tpos);
9668#ifdef FEAT_CINDENT
9669 can_cindent = TRUE;
9670#endif
9671 }
9672 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009673 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009674}
9675
9676 static void
9677ins_down(startcol)
9678 int startcol; /* when TRUE move to Insstart.col */
9679{
9680 pos_T tpos;
9681 linenr_T old_topline = curwin->w_topline;
9682#ifdef FEAT_DIFF
9683 int old_topfill = curwin->w_topfill;
9684#endif
9685
9686 undisplay_dollar();
9687 tpos = curwin->w_cursor;
9688 if (cursor_down(1L, TRUE) == OK)
9689 {
9690 if (startcol)
9691 coladvance(getvcol_nolist(&Insstart));
9692 if (old_topline != curwin->w_topline
9693#ifdef FEAT_DIFF
9694 || old_topfill != curwin->w_topfill
9695#endif
9696 )
9697 redraw_later(VALID);
9698 start_arrow(&tpos);
9699#ifdef FEAT_CINDENT
9700 can_cindent = TRUE;
9701#endif
9702 }
9703 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009704 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705}
9706
9707 static void
9708ins_pagedown()
9709{
9710 pos_T tpos;
9711
9712 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009713
9714#ifdef FEAT_WINDOWS
9715 if (mod_mask & MOD_MASK_CTRL)
9716 {
9717 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009718 if (first_tabpage->tp_next != NULL)
9719 {
9720 start_arrow(&curwin->w_cursor);
9721 goto_tabpage(0);
9722 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009723 return;
9724 }
9725#endif
9726
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 tpos = curwin->w_cursor;
9728 if (onepage(FORWARD, 1L) == OK)
9729 {
9730 start_arrow(&tpos);
9731#ifdef FEAT_CINDENT
9732 can_cindent = TRUE;
9733#endif
9734 }
9735 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009736 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009737}
9738
9739#ifdef FEAT_DND
9740 static void
9741ins_drop()
9742{
9743 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9744}
9745#endif
9746
9747/*
9748 * Handle TAB in Insert or Replace mode.
9749 * Return TRUE when the TAB needs to be inserted like a normal character.
9750 */
9751 static int
9752ins_tab()
9753{
9754 int ind;
9755 int i;
9756 int temp;
9757
9758 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9759 Insstart_blank_vcol = get_nolist_virtcol();
9760 if (echeck_abbr(TAB + ABBR_OFF))
9761 return FALSE;
9762
9763 ind = inindent(0);
9764#ifdef FEAT_CINDENT
9765 if (ind)
9766 can_cindent = FALSE;
9767#endif
9768
9769 /*
9770 * When nothing special, insert TAB like a normal character
9771 */
9772 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009773 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009774 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 return TRUE;
9776
9777 if (stop_arrow() == FAIL)
9778 return TRUE;
9779
9780 did_ai = FALSE;
9781#ifdef FEAT_SMARTINDENT
9782 did_si = FALSE;
9783 can_si = FALSE;
9784 can_si_back = FALSE;
9785#endif
9786 AppendToRedobuff((char_u *)"\t");
9787
9788 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009789 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009790 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9791 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 else /* otherwise use 'tabstop' */
9793 temp = (int)curbuf->b_p_ts;
9794 temp -= get_nolist_virtcol() % temp;
9795
9796 /*
9797 * Insert the first space with ins_char(). It will delete one char in
9798 * replace mode. Insert the rest with ins_str(); it will not delete any
9799 * chars. For VREPLACE mode, we use ins_char() for all characters.
9800 */
9801 ins_char(' ');
9802 while (--temp > 0)
9803 {
9804#ifdef FEAT_VREPLACE
9805 if (State & VREPLACE_FLAG)
9806 ins_char(' ');
9807 else
9808#endif
9809 {
9810 ins_str((char_u *)" ");
9811 if (State & REPLACE_FLAG) /* no char replaced */
9812 replace_push(NUL);
9813 }
9814 }
9815
9816 /*
9817 * When 'expandtab' not set: Replace spaces by TABs where possible.
9818 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009819 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009820 {
9821 char_u *ptr;
9822#ifdef FEAT_VREPLACE
9823 char_u *saved_line = NULL; /* init for GCC */
9824 pos_T pos;
9825#endif
9826 pos_T fpos;
9827 pos_T *cursor;
9828 colnr_T want_vcol, vcol;
9829 int change_col = -1;
9830 int save_list = curwin->w_p_list;
9831
9832 /*
9833 * Get the current line. For VREPLACE mode, don't make real changes
9834 * yet, just work on a copy of the line.
9835 */
9836#ifdef FEAT_VREPLACE
9837 if (State & VREPLACE_FLAG)
9838 {
9839 pos = curwin->w_cursor;
9840 cursor = &pos;
9841 saved_line = vim_strsave(ml_get_curline());
9842 if (saved_line == NULL)
9843 return FALSE;
9844 ptr = saved_line + pos.col;
9845 }
9846 else
9847#endif
9848 {
9849 ptr = ml_get_cursor();
9850 cursor = &curwin->w_cursor;
9851 }
9852
9853 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9854 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9855 curwin->w_p_list = FALSE;
9856
9857 /* Find first white before the cursor */
9858 fpos = curwin->w_cursor;
9859 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9860 {
9861 --fpos.col;
9862 --ptr;
9863 }
9864
9865 /* In Replace mode, don't change characters before the insert point. */
9866 if ((State & REPLACE_FLAG)
9867 && fpos.lnum == Insstart.lnum
9868 && fpos.col < Insstart.col)
9869 {
9870 ptr += Insstart.col - fpos.col;
9871 fpos.col = Insstart.col;
9872 }
9873
9874 /* compute virtual column numbers of first white and cursor */
9875 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9876 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9877
Bram Moolenaar597a4222014-06-25 14:39:50 +02009878 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9879 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 while (vim_iswhite(*ptr))
9881 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009882 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009883 if (vcol + i > want_vcol)
9884 break;
9885 if (*ptr != TAB)
9886 {
9887 *ptr = TAB;
9888 if (change_col < 0)
9889 {
9890 change_col = fpos.col; /* Column of first change */
9891 /* May have to adjust Insstart */
9892 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9893 Insstart.col = fpos.col;
9894 }
9895 }
9896 ++fpos.col;
9897 ++ptr;
9898 vcol += i;
9899 }
9900
9901 if (change_col >= 0)
9902 {
9903 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009904 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905
9906 /* Skip over the spaces we need. */
9907 while (vcol < want_vcol && *ptr == ' ')
9908 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009909 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009910 ++ptr;
9911 ++repl_off;
9912 }
9913 if (vcol > want_vcol)
9914 {
9915 /* Must have a char with 'showbreak' just before it. */
9916 --ptr;
9917 --repl_off;
9918 }
9919 fpos.col += repl_off;
9920
9921 /* Delete following spaces. */
9922 i = cursor->col - fpos.col;
9923 if (i > 0)
9924 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009925 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009926 /* correct replace stack. */
9927 if ((State & REPLACE_FLAG)
9928#ifdef FEAT_VREPLACE
9929 && !(State & VREPLACE_FLAG)
9930#endif
9931 )
9932 for (temp = i; --temp >= 0; )
9933 replace_join(repl_off);
9934 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009935#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009936 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009937 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009938 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009939 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9940 (char_u *)"\t", 1);
9941 }
9942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 cursor->col -= i;
9944
9945#ifdef FEAT_VREPLACE
9946 /*
9947 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9948 * backspacing over the changed spacing and then inserting the new
9949 * spacing.
9950 */
9951 if (State & VREPLACE_FLAG)
9952 {
9953 /* Backspace from real cursor to change_col */
9954 backspace_until_column(change_col);
9955
9956 /* Insert each char in saved_line from changed_col to
9957 * ptr-cursor */
9958 ins_bytes_len(saved_line + change_col,
9959 cursor->col - change_col);
9960 }
9961#endif
9962 }
9963
9964#ifdef FEAT_VREPLACE
9965 if (State & VREPLACE_FLAG)
9966 vim_free(saved_line);
9967#endif
9968 curwin->w_p_list = save_list;
9969 }
9970
9971 return FALSE;
9972}
9973
9974/*
9975 * Handle CR or NL in insert mode.
9976 * Return TRUE when out of memory or can't undo.
9977 */
9978 static int
9979ins_eol(c)
9980 int c;
9981{
9982 int i;
9983
9984 if (echeck_abbr(c + ABBR_OFF))
9985 return FALSE;
9986 if (stop_arrow() == FAIL)
9987 return TRUE;
9988 undisplay_dollar();
9989
9990 /*
9991 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9992 * character under the cursor. Only push a NUL on the replace stack,
9993 * nothing to put back when the NL is deleted.
9994 */
9995 if ((State & REPLACE_FLAG)
9996#ifdef FEAT_VREPLACE
9997 && !(State & VREPLACE_FLAG)
9998#endif
9999 )
10000 replace_push(NUL);
10001
10002 /*
10003 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10004 * replacing the next line, so we push all of the characters left on the
10005 * line onto the replace stack. This is not done here though, it is done
10006 * in open_line().
10007 */
10008
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010009#ifdef FEAT_VIRTUALEDIT
10010 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10011 * CTRL-O). */
10012 if (virtual_active() && curwin->w_cursor.coladd > 0)
10013 coladvance(getviscol());
10014#endif
10015
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016#ifdef FEAT_RIGHTLEFT
10017# ifdef FEAT_FKMAP
10018 if (p_altkeymap && p_fkmap)
10019 fkmap(NL);
10020# endif
10021 /* NL in reverse insert will always start in the end of
10022 * current line. */
10023 if (revins_on)
10024 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10025#endif
10026
10027 AppendToRedobuff(NL_STR);
10028 i = open_line(FORWARD,
10029#ifdef FEAT_COMMENTS
10030 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10031#endif
10032 0, old_indent);
10033 old_indent = 0;
10034#ifdef FEAT_CINDENT
10035 can_cindent = TRUE;
10036#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010037#ifdef FEAT_FOLDING
10038 /* When inserting a line the cursor line must never be in a closed fold. */
10039 foldOpenCursor();
10040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010041
10042 return (!i);
10043}
10044
10045#ifdef FEAT_DIGRAPHS
10046/*
10047 * Handle digraph in insert mode.
10048 * Returns character still to be inserted, or NUL when nothing remaining to be
10049 * done.
10050 */
10051 static int
10052ins_digraph()
10053{
10054 int c;
10055 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010056 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057
10058 pc_status = PC_STATUS_UNSET;
10059 if (redrawing() && !char_avail())
10060 {
10061 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010062 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010063
10064 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010065 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010066#ifdef FEAT_CMDL_INFO
10067 add_to_showcmd_c(Ctrl_K);
10068#endif
10069 }
10070
10071#ifdef USE_ON_FLY_SCROLL
10072 dont_scroll = TRUE; /* disallow scrolling here */
10073#endif
10074
10075 /* don't map the digraph chars. This also prevents the
10076 * mode message to be deleted when ESC is hit */
10077 ++no_mapping;
10078 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010079 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010080 --no_mapping;
10081 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010082 if (did_putchar)
10083 /* when the line fits in 'columns' the '?' is at the start of the next
10084 * line and will not be removed by the redraw */
10085 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010086
Bram Moolenaar071d4272004-06-13 20:20:40 +000010087 if (IS_SPECIAL(c) || mod_mask) /* special key */
10088 {
10089#ifdef FEAT_CMDL_INFO
10090 clear_showcmd();
10091#endif
10092 insert_special(c, TRUE, FALSE);
10093 return NUL;
10094 }
10095 if (c != ESC)
10096 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010097 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010098 if (redrawing() && !char_avail())
10099 {
10100 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010101 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010102
10103 if (char2cells(c) == 1)
10104 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010105 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010106 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010107 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010108 }
10109#ifdef FEAT_CMDL_INFO
10110 add_to_showcmd_c(c);
10111#endif
10112 }
10113 ++no_mapping;
10114 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010115 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116 --no_mapping;
10117 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010118 if (did_putchar)
10119 /* when the line fits in 'columns' the '?' is at the start of the
10120 * next line and will not be removed by a redraw */
10121 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010122 if (cc != ESC)
10123 {
10124 AppendToRedobuff((char_u *)CTRL_V_STR);
10125 c = getdigraph(c, cc, TRUE);
10126#ifdef FEAT_CMDL_INFO
10127 clear_showcmd();
10128#endif
10129 return c;
10130 }
10131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010132#ifdef FEAT_CMDL_INFO
10133 clear_showcmd();
10134#endif
10135 return NUL;
10136}
10137#endif
10138
10139/*
10140 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10141 * Returns the char to be inserted, or NUL if none found.
10142 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010143 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000010144ins_copychar(lnum)
10145 linenr_T lnum;
10146{
10147 int c;
10148 int temp;
10149 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010150 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151
10152 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10153 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010154 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 return NUL;
10156 }
10157
10158 /* try to advance to the cursor column */
10159 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010160 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 prev_ptr = ptr;
10162 validate_virtcol();
10163 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10164 {
10165 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010166 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010167 }
10168 if ((colnr_T)temp > curwin->w_virtcol)
10169 ptr = prev_ptr;
10170
10171#ifdef FEAT_MBYTE
10172 c = (*mb_ptr2char)(ptr);
10173#else
10174 c = *ptr;
10175#endif
10176 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010177 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010178 return c;
10179}
10180
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010181/*
10182 * CTRL-Y or CTRL-E typed in Insert mode.
10183 */
10184 static int
10185ins_ctrl_ey(tc)
10186 int tc;
10187{
10188 int c = tc;
10189
10190#ifdef FEAT_INS_EXPAND
10191 if (ctrl_x_mode == CTRL_X_SCROLL)
10192 {
10193 if (c == Ctrl_Y)
10194 scrolldown_clamp();
10195 else
10196 scrollup_clamp();
10197 redraw_later(VALID);
10198 }
10199 else
10200#endif
10201 {
10202 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10203 if (c != NUL)
10204 {
10205 long tw_save;
10206
10207 /* The character must be taken literally, insert like it
10208 * was typed after a CTRL-V, and pretend 'textwidth'
10209 * wasn't set. Digits, 'o' and 'x' are special after a
10210 * CTRL-V, don't use it for these. */
10211 if (c < 256 && !isalnum(c))
10212 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10213 tw_save = curbuf->b_p_tw;
10214 curbuf->b_p_tw = -1;
10215 insert_special(c, TRUE, FALSE);
10216 curbuf->b_p_tw = tw_save;
10217#ifdef FEAT_RIGHTLEFT
10218 revins_chars++;
10219 revins_legal++;
10220#endif
10221 c = Ctrl_V; /* pretend CTRL-V is last character */
10222 auto_format(FALSE, TRUE);
10223 }
10224 }
10225 return c;
10226}
10227
Bram Moolenaar071d4272004-06-13 20:20:40 +000010228#ifdef FEAT_SMARTINDENT
10229/*
10230 * Try to do some very smart auto-indenting.
10231 * Used when inserting a "normal" character.
10232 */
10233 static void
10234ins_try_si(c)
10235 int c;
10236{
10237 pos_T *pos, old_pos;
10238 char_u *ptr;
10239 int i;
10240 int temp;
10241
10242 /*
10243 * do some very smart indenting when entering '{' or '}'
10244 */
10245 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10246 {
10247 /*
10248 * for '}' set indent equal to indent of line containing matching '{'
10249 */
10250 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10251 {
10252 old_pos = curwin->w_cursor;
10253 /*
10254 * If the matching '{' has a ')' immediately before it (ignoring
10255 * white-space), then line up with the start of the line
10256 * containing the matching '(' if there is one. This handles the
10257 * case where an "if (..\n..) {" statement continues over multiple
10258 * lines -- webb
10259 */
10260 ptr = ml_get(pos->lnum);
10261 i = pos->col;
10262 if (i > 0) /* skip blanks before '{' */
10263 while (--i > 0 && vim_iswhite(ptr[i]))
10264 ;
10265 curwin->w_cursor.lnum = pos->lnum;
10266 curwin->w_cursor.col = i;
10267 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10268 curwin->w_cursor = *pos;
10269 i = get_indent();
10270 curwin->w_cursor = old_pos;
10271#ifdef FEAT_VREPLACE
10272 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010273 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274 else
10275#endif
10276 (void)set_indent(i, SIN_CHANGED);
10277 }
10278 else if (curwin->w_cursor.col > 0)
10279 {
10280 /*
10281 * when inserting '{' after "O" reduce indent, but not
10282 * more than indent of previous line
10283 */
10284 temp = TRUE;
10285 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10286 {
10287 old_pos = curwin->w_cursor;
10288 i = get_indent();
10289 while (curwin->w_cursor.lnum > 1)
10290 {
10291 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10292
10293 /* ignore empty lines and lines starting with '#'. */
10294 if (*ptr != '#' && *ptr != NUL)
10295 break;
10296 }
10297 if (get_indent() >= i)
10298 temp = FALSE;
10299 curwin->w_cursor = old_pos;
10300 }
10301 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010302 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010303 }
10304 }
10305
10306 /*
10307 * set indent of '#' always to 0
10308 */
10309 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10310 {
10311 /* remember current indent for next line */
10312 old_indent = get_indent();
10313 (void)set_indent(0, SIN_CHANGED);
10314 }
10315
10316 /* Adjust ai_col, the char at this position can be deleted. */
10317 if (ai_col > curwin->w_cursor.col)
10318 ai_col = curwin->w_cursor.col;
10319}
10320#endif
10321
10322/*
10323 * Get the value that w_virtcol would have when 'list' is off.
10324 * Unless 'cpo' contains the 'L' flag.
10325 */
10326 static colnr_T
10327get_nolist_virtcol()
10328{
10329 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10330 return getvcol_nolist(&curwin->w_cursor);
10331 validate_virtcol();
10332 return curwin->w_virtcol;
10333}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010334
10335#ifdef FEAT_AUTOCMD
10336/*
10337 * Handle the InsertCharPre autocommand.
10338 * "c" is the character that was typed.
10339 * Return a pointer to allocated memory with the replacement string.
10340 * Return NULL to continue inserting "c".
10341 */
10342 static char_u *
10343do_insert_char_pre(c)
10344 int c;
10345{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010346 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010347 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010348
10349 /* Return quickly when there is nothing to do. */
10350 if (!has_insertcharpre())
10351 return NULL;
10352
Bram Moolenaar704984a2012-06-01 14:57:51 +020010353#ifdef FEAT_MBYTE
10354 if (has_mbyte)
10355 buf[(*mb_char2bytes)(c, buf)] = NUL;
10356 else
10357#endif
10358 {
10359 buf[0] = c;
10360 buf[1] = NUL;
10361 }
10362
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010363 /* Lock the text to avoid weird things from happening. */
10364 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010365 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010366
Bram Moolenaar704984a2012-06-01 14:57:51 +020010367 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010368 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010369 {
10370 /* Get the value of v:char. It may be empty or more than one
10371 * character. Only use it when changed, otherwise continue with the
10372 * original character to avoid breaking autoindent. */
10373 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10374 res = vim_strsave(get_vim_var_str(VV_CHAR));
10375 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010376
10377 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10378 --textlock;
10379
10380 return res;
10381}
10382#endif