blob: 086a5b99eeb6b6b71c2246c6b0198f0f370a3fd9 [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
985 vim_beep();
986 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 {
2213 vim_beep();
2214 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)
2797 {
2798 if (!compl_no_select)
2799 ins_complete(K_DOWN);
2800 }
2801 else
2802 {
2803 ins_complete(Ctrl_N);
2804 if (compl_no_select)
2805 ins_complete(Ctrl_P);
2806 }
Bram Moolenaara94bc432006-03-10 21:42:59 +00002807 out_flush();
2808}
2809
2810
Bram Moolenaar9372a112005-12-06 19:59:18 +00002811/* "compl_match_array" points the currently displayed list of entries in the
2812 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002813static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002814static int compl_match_arraysize;
2815
2816/*
2817 * Update the screen and when there is any scrolling remove the popup menu.
2818 */
2819 static void
2820ins_compl_upd_pum()
2821{
2822 int h;
2823
2824 if (compl_match_array != NULL)
2825 {
2826 h = curwin->w_cline_height;
2827 update_screen(0);
2828 if (h != curwin->w_cline_height)
2829 ins_compl_del_pum();
2830 }
2831}
2832
2833/*
2834 * Remove any popup menu.
2835 */
2836 static void
2837ins_compl_del_pum()
2838{
2839 if (compl_match_array != NULL)
2840 {
2841 pum_undisplay();
2842 vim_free(compl_match_array);
2843 compl_match_array = NULL;
2844 }
2845}
2846
2847/*
2848 * Return TRUE if the popup menu should be displayed.
2849 */
2850 static int
2851pum_wanted()
2852{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002853 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002854 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002855 return FALSE;
2856
2857 /* The display looks bad on a B&W display. */
2858 if (t_colors < 8
2859#ifdef FEAT_GUI
2860 && !gui.in_use
2861#endif
2862 )
2863 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002864 return TRUE;
2865}
2866
2867/*
2868 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002869 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002870 */
2871 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002872pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002873{
2874 compl_T *compl;
2875 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002876
2877 /* Don't display the popup menu if there are no matches or there is only
2878 * one (ignoring the original text). */
2879 compl = compl_first_match;
2880 i = 0;
2881 do
2882 {
2883 if (compl == NULL
2884 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2885 break;
2886 compl = compl->cp_next;
2887 } while (compl != compl_first_match);
2888
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002889 if (strstr((char *)p_cot, "menuone") != NULL)
2890 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002891 return (i >= 2);
2892}
2893
2894/*
2895 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002896 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002897 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002898 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002899ins_compl_show_pum()
2900{
2901 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002902 compl_T *shown_compl = NULL;
2903 int did_find_shown_match = FALSE;
2904 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002905 int i;
2906 int cur = -1;
2907 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002908 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002909
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002910 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002911 return;
2912
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002913#if defined(FEAT_EVAL)
2914 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2915 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2916#endif
2917
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002918 /* Update the screen before drawing the popup menu over it. */
2919 update_screen(0);
2920
2921 if (compl_match_array == NULL)
2922 {
2923 /* Need to build the popup menu list. */
2924 compl_match_arraysize = 0;
2925 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002926 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002927 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002928 do
2929 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002930 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2931 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002932 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002933 ++compl_match_arraysize;
2934 compl = compl->cp_next;
2935 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002936 if (compl_match_arraysize == 0)
2937 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002938 compl_match_array = (pumitem_T *)alloc_clear(
2939 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002940 * compl_match_arraysize));
2941 if (compl_match_array != NULL)
2942 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002943 /* If the current match is the original text don't find the first
2944 * match after it, don't highlight anything. */
2945 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2946 shown_match_ok = TRUE;
2947
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002948 i = 0;
2949 compl = compl_first_match;
2950 do
2951 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002952 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2953 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002954 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002955 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002956 if (!shown_match_ok)
2957 {
2958 if (compl == compl_shown_match || did_find_shown_match)
2959 {
2960 /* This item is the shown match or this is the
2961 * first displayed item after the shown match. */
2962 compl_shown_match = compl;
2963 did_find_shown_match = TRUE;
2964 shown_match_ok = TRUE;
2965 }
2966 else
2967 /* Remember this displayed match for when the
2968 * shown match is just below it. */
2969 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002970 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002971 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002972
2973 if (compl->cp_text[CPT_ABBR] != NULL)
2974 compl_match_array[i].pum_text =
2975 compl->cp_text[CPT_ABBR];
2976 else
2977 compl_match_array[i].pum_text = compl->cp_str;
2978 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2979 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2980 if (compl->cp_text[CPT_MENU] != NULL)
2981 compl_match_array[i++].pum_extra =
2982 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002983 else
2984 compl_match_array[i++].pum_extra = compl->cp_fname;
2985 }
2986
2987 if (compl == compl_shown_match)
2988 {
2989 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002990
2991 /* When the original text is the shown match don't set
2992 * compl_shown_match. */
2993 if (compl->cp_flags & ORIGINAL_TEXT)
2994 shown_match_ok = TRUE;
2995
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002996 if (!shown_match_ok && shown_compl != NULL)
2997 {
2998 /* The shown match isn't displayed, set it to the
2999 * previously displayed match. */
3000 compl_shown_match = shown_compl;
3001 shown_match_ok = TRUE;
3002 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003003 }
3004 compl = compl->cp_next;
3005 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003006
3007 if (!shown_match_ok) /* no displayed match at all */
3008 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003009 }
3010 }
3011 else
3012 {
3013 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003014 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003015 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3016 || compl_match_array[i].pum_text
3017 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003018 {
3019 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003020 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003021 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003022 }
3023
3024 if (compl_match_array != NULL)
3025 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003026 /* In Replace mode when a $ is displayed at the end of the line only
3027 * part of the screen would be updated. We do need to redraw here. */
3028 dollar_vcol = -1;
3029
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003030 /* Compute the screen column of the start of the completed text.
3031 * Use the cursor to get all wrapping and other settings right. */
3032 col = curwin->w_cursor.col;
3033 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003034 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003035 curwin->w_cursor.col = col;
3036 }
3037}
3038
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039#define DICT_FIRST (1) /* use just first element in "dict" */
3040#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003041
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003043 * Add any identifiers that match the given pattern in the list of dictionary
3044 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 */
3046 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00003047ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
3048 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003050 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003051 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003053 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 char_u *ptr;
3055 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003056 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057 char_u **files;
3058 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003060 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061
Bram Moolenaar0b238792006-03-02 22:49:12 +00003062 if (*dict == NUL)
3063 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003064#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003065 /* When 'dictionary' is empty and spell checking is enabled use
3066 * "spell". */
3067 if (!thesaurus && curwin->w_p_spell)
3068 dict = (char_u *)"spell";
3069 else
3070#endif
3071 return;
3072 }
3073
Bram Moolenaar071d4272004-06-13 20:20:40 +00003074 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003075 if (buf == NULL)
3076 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003077 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003078
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 /* If 'infercase' is set, don't use 'smartcase' here */
3080 save_p_scs = p_scs;
3081 if (curbuf->b_p_inf)
3082 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003083
3084 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3085 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003086 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003087 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003088 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003089 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003090 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003091
3092 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003093 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003094 len = STRLEN(pat_esc) + 10;
3095 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003096 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003097 {
3098 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003099 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003100 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003101 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003102 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3103 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003104 vim_free(ptr);
3105 }
3106 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003107 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003108 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003109 if (regmatch.regprog == NULL)
3110 goto theend;
3111 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003112
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3114 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003115 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 {
3117 /* copy one dictionary file name into buf */
3118 if (flags == DICT_EXACT)
3119 {
3120 count = 1;
3121 files = &dict;
3122 }
3123 else
3124 {
3125 /* Expand wildcards in the dictionary name, but do not allow
3126 * backticks (for security, the 'dict' option may have been set in
3127 * a modeline). */
3128 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003129# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003130 if (!thesaurus && STRCMP(buf, "spell") == 0)
3131 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003132 else
3133# endif
3134 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003135 || expand_wildcards(1, &buf, &count, &files,
3136 EW_FILE|EW_SILENT) != OK)
3137 count = 0;
3138 }
3139
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003140# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003141 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003142 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003143 /* Complete from active spelling. Skip "\<" in the pattern, we
3144 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003145 if (pat[0] == '\\' && pat[1] == '<')
3146 ptr = pat + 2;
3147 else
3148 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003149 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003151 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003152# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003153 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003154 {
3155 ins_compl_files(count, files, thesaurus, flags,
3156 &regmatch, buf, &dir);
3157 if (flags != DICT_EXACT)
3158 FreeWild(count, files);
3159 }
3160 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 break;
3162 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003163
3164theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003166 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003167 vim_free(buf);
3168}
3169
Bram Moolenaar0b238792006-03-02 22:49:12 +00003170 static void
3171ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3172 int count;
3173 char_u **files;
3174 int thesaurus;
3175 int flags;
3176 regmatch_T *regmatch;
3177 char_u *buf;
3178 int *dir;
3179{
3180 char_u *ptr;
3181 int i;
3182 FILE *fp;
3183 int add_r;
3184
3185 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3186 {
3187 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3188 if (flags != DICT_EXACT)
3189 {
3190 vim_snprintf((char *)IObuff, IOSIZE,
3191 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003192 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003193 }
3194
3195 if (fp != NULL)
3196 {
3197 /*
3198 * Read dictionary file line by line.
3199 * Check each line for a match.
3200 */
3201 while (!got_int && !compl_interrupted
3202 && !vim_fgets(buf, LSIZE, fp))
3203 {
3204 ptr = buf;
3205 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3206 {
3207 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003208 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003209 ptr = find_line_end(ptr);
3210 else
3211 ptr = find_word_end(ptr);
3212 add_r = ins_compl_add_infercase(regmatch->startp[0],
3213 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003214 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003215 if (thesaurus)
3216 {
3217 char_u *wstart;
3218
3219 /*
3220 * Add the other matches on the line
3221 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003222 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003223 while (!got_int)
3224 {
3225 /* Find start of the next word. Skip white
3226 * space and punctuation. */
3227 ptr = find_word_start(ptr);
3228 if (*ptr == NUL || *ptr == NL)
3229 break;
3230 wstart = ptr;
3231
Bram Moolenaara2993e12007-08-12 14:38:46 +00003232 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003233#ifdef FEAT_MBYTE
3234 if (has_mbyte)
3235 /* Japanese words may have characters in
3236 * different classes, only separate words
3237 * with single-byte non-word characters. */
3238 while (*ptr != NUL)
3239 {
3240 int l = (*mb_ptr2len)(ptr);
3241
3242 if (l < 2 && !vim_iswordc(*ptr))
3243 break;
3244 ptr += l;
3245 }
3246 else
3247#endif
3248 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003249
3250 /* Add the word. Skip the regexp match. */
3251 if (wstart != regmatch->startp[0])
3252 add_r = ins_compl_add_infercase(wstart,
3253 (int)(ptr - wstart),
3254 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003255 }
3256 }
3257 if (add_r == OK)
3258 /* if dir was BACKWARD then honor it just once */
3259 *dir = FORWARD;
3260 else if (add_r == FAIL)
3261 break;
3262 /* avoid expensive call to vim_regexec() when at end
3263 * of line */
3264 if (*ptr == '\n' || got_int)
3265 break;
3266 }
3267 line_breakcheck();
3268 ins_compl_check_keys(50);
3269 }
3270 fclose(fp);
3271 }
3272 }
3273}
3274
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275/*
3276 * Find the start of the next word.
3277 * Returns a pointer to the first char of the word. Also stops at a NUL.
3278 */
3279 char_u *
3280find_word_start(ptr)
3281 char_u *ptr;
3282{
3283#ifdef FEAT_MBYTE
3284 if (has_mbyte)
3285 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003286 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 else
3288#endif
3289 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3290 ++ptr;
3291 return ptr;
3292}
3293
3294/*
3295 * Find the end of the word. Assumes it starts inside a word.
3296 * Returns a pointer to just after the word.
3297 */
3298 char_u *
3299find_word_end(ptr)
3300 char_u *ptr;
3301{
3302#ifdef FEAT_MBYTE
3303 int start_class;
3304
3305 if (has_mbyte)
3306 {
3307 start_class = mb_get_class(ptr);
3308 if (start_class > 1)
3309 while (*ptr != NUL)
3310 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003311 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312 if (mb_get_class(ptr) != start_class)
3313 break;
3314 }
3315 }
3316 else
3317#endif
3318 while (vim_iswordc(*ptr))
3319 ++ptr;
3320 return ptr;
3321}
3322
3323/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003324 * Find the end of the line, omitting CR and NL at the end.
3325 * Returns a pointer to just after the line.
3326 */
3327 static char_u *
3328find_line_end(ptr)
3329 char_u *ptr;
3330{
3331 char_u *s;
3332
3333 s = ptr + STRLEN(ptr);
3334 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3335 --s;
3336 return s;
3337}
3338
3339/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 * Free the list of completions
3341 */
3342 static void
3343ins_compl_free()
3344{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003345 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003346 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003348 vim_free(compl_pattern);
3349 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003350 vim_free(compl_leader);
3351 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003353 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003355
3356 ins_compl_del_pum();
3357 pum_clear();
3358
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003359 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003360 do
3361 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003362 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003363 compl_curr_match = compl_curr_match->cp_next;
3364 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003366 if (match->cp_flags & FREE_FNAME)
3367 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003368 for (i = 0; i < CPT_COUNT; ++i)
3369 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003371 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3372 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003373 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003374}
3375
3376 static void
3377ins_compl_clear()
3378{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003379 compl_cont_status = 0;
3380 compl_started = FALSE;
3381 compl_matches = 0;
3382 vim_free(compl_pattern);
3383 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003384 vim_free(compl_leader);
3385 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003387 vim_free(compl_orig_text);
3388 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003389 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003390 /* clear v:completed_item */
3391 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392}
3393
3394/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003395 * Return TRUE when Insert completion is active.
3396 */
3397 int
3398ins_compl_active()
3399{
3400 return compl_started;
3401}
3402
3403/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003404 * Delete one character before the cursor and show the subset of the matches
3405 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003406 * Returns the character to be used, NUL if the work is done and another char
3407 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003408 */
3409 static int
3410ins_compl_bs()
3411{
3412 char_u *line;
3413 char_u *p;
3414
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003415 line = ml_get_curline();
3416 p = line + curwin->w_cursor.col;
3417 mb_ptr_back(line, p);
3418
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003419 /* Stop completion when the whole word was deleted. For Omni completion
3420 * allow the word to be deleted, we won't match everything. */
3421 if ((int)(p - line) - (int)compl_col < 0
3422 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003423 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003424 return K_BS;
3425
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003426 /* Deleted more than what was used to find matches or didn't finish
3427 * finding all matches: need to look for matches all over again. */
3428 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003429 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003430 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003431
Bram Moolenaara6557602006-02-04 22:43:20 +00003432 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003433 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003434 if (compl_leader != NULL)
3435 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003436 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003437 if (compl_shown_match != NULL)
3438 /* Make sure current match is not a hidden item. */
3439 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003440 return NUL;
3441 }
3442 return K_BS;
3443}
Bram Moolenaara6557602006-02-04 22:43:20 +00003444
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003445/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003446 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3447 * be called.
3448 */
3449 static int
3450ins_compl_need_restart()
3451{
3452 /* Return TRUE if we didn't complete finding matches or when the
3453 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3454 return compl_was_interrupted
3455 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3456 && compl_opt_refresh_always);
3457}
3458
3459/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003460 * Called after changing "compl_leader".
3461 * Show the popup menu with a different set of matches.
3462 * May also search for matches again if the previous search was interrupted.
3463 */
3464 static void
3465ins_compl_new_leader()
3466{
3467 ins_compl_del_pum();
3468 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003469 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003470 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003471
3472 if (compl_started)
3473 ins_compl_set_original_text(compl_leader);
3474 else
3475 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003476#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003477 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003478#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003479 /*
3480 * Matches were cleared, need to search for them now. First display
3481 * the changed text before the cursor. Set "compl_restarting" to
3482 * avoid that the first match is inserted.
3483 */
3484 update_screen(0);
3485#ifdef FEAT_GUI
3486 if (gui.in_use)
3487 {
3488 /* Show the cursor after the match, not after the redrawn text. */
3489 setcursor();
3490 out_flush();
3491 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003492 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003493#endif
3494 compl_restarting = TRUE;
3495 if (ins_complete(Ctrl_N) == FAIL)
3496 compl_cont_status = 0;
3497 compl_restarting = FALSE;
3498 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003499
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003500 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003501
3502 /* Show the popup menu with a different set of matches. */
3503 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003504
3505 /* Don't let Enter select the original text when there is no popup menu. */
3506 if (compl_match_array == NULL)
3507 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003508}
3509
3510/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003511 * Return the length of the completion, from the completion start column to
3512 * the cursor column. Making sure it never goes below zero.
3513 */
3514 static int
3515ins_compl_len()
3516{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003517 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003518
3519 if (off < 0)
3520 return 0;
3521 return off;
3522}
3523
3524/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003525 * Append one character to the match leader. May reduce the number of
3526 * matches.
3527 */
3528 static void
3529ins_compl_addleader(c)
3530 int c;
3531{
3532#ifdef FEAT_MBYTE
3533 int cc;
3534
3535 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3536 {
3537 char_u buf[MB_MAXBYTES + 1];
3538
3539 (*mb_char2bytes)(c, buf);
3540 buf[cc] = NUL;
3541 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003542 if (compl_opt_refresh_always)
3543 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003544 }
3545 else
3546#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003547 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003548 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003549 if (compl_opt_refresh_always)
3550 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003551 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003552
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003553 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003554 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003555 ins_compl_restart();
3556
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003557 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003558 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003559 * break redo. */
3560 if (!compl_opt_refresh_always)
3561 {
3562 vim_free(compl_leader);
3563 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003564 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003565 if (compl_leader != NULL)
3566 ins_compl_new_leader();
3567 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003568}
3569
3570/*
3571 * Setup for finding completions again without leaving CTRL-X mode. Used when
3572 * BS or a key was typed while still searching for matches.
3573 */
3574 static void
3575ins_compl_restart()
3576{
3577 ins_compl_free();
3578 compl_started = FALSE;
3579 compl_matches = 0;
3580 compl_cont_status = 0;
3581 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003582}
3583
3584/*
3585 * Set the first match, the original text.
3586 */
3587 static void
3588ins_compl_set_original_text(str)
3589 char_u *str;
3590{
3591 char_u *p;
3592
3593 /* Replace the original text entry. */
3594 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3595 {
3596 p = vim_strsave(str);
3597 if (p != NULL)
3598 {
3599 vim_free(compl_first_match->cp_str);
3600 compl_first_match->cp_str = p;
3601 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003602 }
3603}
3604
3605/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003606 * Append one character to the match leader. May reduce the number of
3607 * matches.
3608 */
3609 static void
3610ins_compl_addfrommatch()
3611{
3612 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003613 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003614 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003615 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003616
3617 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003618 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003619 {
3620 /* When still at the original match use the first entry that matches
3621 * the leader. */
3622 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3623 {
3624 p = NULL;
3625 for (cp = compl_shown_match->cp_next; cp != NULL
3626 && cp != compl_first_match; cp = cp->cp_next)
3627 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003628 if (compl_leader == NULL
3629 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003630 (int)STRLEN(compl_leader)))
3631 {
3632 p = cp->cp_str;
3633 break;
3634 }
3635 }
3636 if (p == NULL || (int)STRLEN(p) <= len)
3637 return;
3638 }
3639 else
3640 return;
3641 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003642 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003643 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003644 ins_compl_addleader(c);
3645}
3646
3647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003649 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003650 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003652 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653ins_compl_prep(c)
3654 int c;
3655{
3656 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003658 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659
3660 /* Forget any previous 'special' messages if this is actually
3661 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3662 */
3663 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3664 edit_submode_extra = NULL;
3665
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003666 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003667 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3668 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003669 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003671 /* Set "compl_get_longest" when finding the first matches. */
3672 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3673 || (ctrl_x_mode == 0 && !compl_started))
3674 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003675 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003676 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003677
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003678 }
3679
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003680 compl_no_insert = FALSE;
3681 compl_no_select = FALSE;
3682 if (strstr((char *)p_cot, "noselect") != NULL)
3683 compl_no_select = TRUE;
3684 if (strstr((char *)p_cot, "noinsert") != NULL)
3685 compl_no_insert = TRUE;
3686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3688 {
3689 /*
3690 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3691 * it will be yet. Now we decide.
3692 */
3693 switch (c)
3694 {
3695 case Ctrl_E:
3696 case Ctrl_Y:
3697 ctrl_x_mode = CTRL_X_SCROLL;
3698 if (!(State & REPLACE_FLAG))
3699 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3700 else
3701 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3702 edit_submode_pre = NULL;
3703 showmode();
3704 break;
3705 case Ctrl_L:
3706 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3707 break;
3708 case Ctrl_F:
3709 ctrl_x_mode = CTRL_X_FILES;
3710 break;
3711 case Ctrl_K:
3712 ctrl_x_mode = CTRL_X_DICTIONARY;
3713 break;
3714 case Ctrl_R:
3715 /* Simply allow ^R to happen without affecting ^X mode */
3716 break;
3717 case Ctrl_T:
3718 ctrl_x_mode = CTRL_X_THESAURUS;
3719 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003720#ifdef FEAT_COMPL_FUNC
3721 case Ctrl_U:
3722 ctrl_x_mode = CTRL_X_FUNCTION;
3723 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003724 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003725 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003726 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003727#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003728 case 's':
3729 case Ctrl_S:
3730 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003731#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003732 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003733 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003734 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003735#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003736 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 case Ctrl_RSB:
3738 ctrl_x_mode = CTRL_X_TAGS;
3739 break;
3740#ifdef FEAT_FIND_ID
3741 case Ctrl_I:
3742 case K_S_TAB:
3743 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3744 break;
3745 case Ctrl_D:
3746 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3747 break;
3748#endif
3749 case Ctrl_V:
3750 case Ctrl_Q:
3751 ctrl_x_mode = CTRL_X_CMDLINE;
3752 break;
3753 case Ctrl_P:
3754 case Ctrl_N:
3755 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3756 * just started ^X mode, or there were enough ^X's to cancel
3757 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3758 * do normal expansion when interrupting a different mode (say
3759 * ^X^F^X^P or ^P^X^X^P, see below)
3760 * nothing changes if interrupting mode 0, (eg, the flag
3761 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003762 if (!(compl_cont_status & CONT_INTRPT))
3763 compl_cont_status |= CONT_LOCAL;
3764 else if (compl_cont_mode != 0)
3765 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 /* FALLTHROUGH */
3767 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003768 /* If we have typed at least 2 ^X's... for modes != 0, we set
3769 * compl_cont_status = 0 (eg, as if we had just started ^X
3770 * mode).
3771 * For mode 0, we set "compl_cont_mode" to an impossible
3772 * value, in both cases ^X^X can be used to restart the same
3773 * mode (avoiding ADDING mode).
3774 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3775 * 'complete' and local ^P expansions respectively.
3776 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3777 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (c == Ctrl_X)
3779 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003780 if (compl_cont_mode != 0)
3781 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003783 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
3785 ctrl_x_mode = 0;
3786 edit_submode = NULL;
3787 showmode();
3788 break;
3789 }
3790 }
3791 else if (ctrl_x_mode != 0)
3792 {
3793 /* We're already in CTRL-X mode, do we stay in it? */
3794 if (!vim_is_ctrl_x_key(c))
3795 {
3796 if (ctrl_x_mode == CTRL_X_SCROLL)
3797 ctrl_x_mode = 0;
3798 else
3799 ctrl_x_mode = CTRL_X_FINISHED;
3800 edit_submode = NULL;
3801 }
3802 showmode();
3803 }
3804
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003805 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
3807 /* Show error message from attempted keyword completion (probably
3808 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003809 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003811 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3812 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 || ctrl_x_mode == CTRL_X_FINISHED)
3814 {
3815 /* Get here when we have finished typing a sequence of ^N and
3816 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003817 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003818 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 {
3820 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003821 * If any of the original typed text has been changed, eg when
3822 * ignorecase is set, we must add back-spaces to the redo
3823 * buffer. We add as few as necessary to delete just the part
3824 * of the original text that has changed.
3825 * When using the longest match, edited the match or used
3826 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003828 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3829 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003830 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003831 ptr = NULL;
3832 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833 }
3834
3835#ifdef FEAT_CINDENT
3836 want_cindent = (can_cindent && cindent_on());
3837#endif
3838 /*
3839 * When completing whole lines: fix indent for 'cindent'.
3840 * Otherwise, break line if it's too long.
3841 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003842 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003843 {
3844#ifdef FEAT_CINDENT
3845 /* re-indent the current line */
3846 if (want_cindent)
3847 {
3848 do_c_expr_indent();
3849 want_cindent = FALSE; /* don't do it again */
3850 }
3851#endif
3852 }
3853 else
3854 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003855 int prev_col = curwin->w_cursor.col;
3856
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003858 if (prev_col > 0)
3859 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 if (stop_arrow() == OK)
3861 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003862 if (prev_col > 0
3863 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3864 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003867 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003868 * the selection without inserting anything. When
3869 * compl_enter_selects is set the Enter key does the same. */
3870 if ((c == Ctrl_Y || (compl_enter_selects
3871 && (c == CAR || c == K_KENTER || c == NL)))
3872 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003873 retval = TRUE;
3874
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003875 /* CTRL-E means completion is Ended, go back to the typed text. */
3876 if (c == Ctrl_E)
3877 {
3878 ins_compl_delete();
3879 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003880 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003881 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003882 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003883 retval = TRUE;
3884 }
3885
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003886 auto_format(FALSE, TRUE);
3887
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003889 compl_started = FALSE;
3890 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003891 if (!shortmess(SHM_COMPLETIONMENU))
3892 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003894 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895 if (edit_submode != NULL)
3896 {
3897 edit_submode = NULL;
3898 showmode();
3899 }
3900
3901#ifdef FEAT_CINDENT
3902 /*
3903 * Indent now if a key was typed that is in 'cinkeys'.
3904 */
3905 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3906 do_c_expr_indent();
3907#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003908#ifdef FEAT_AUTOCMD
3909 /* Trigger the CompleteDone event to give scripts a chance to act
3910 * upon the completion. */
3911 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 }
3914 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003915#ifdef FEAT_AUTOCMD
3916 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3917 /* Trigger the CompleteDone event to give scripts a chance to act
3918 * upon the (possibly failed) completion. */
3919 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003921
3922 /* reset continue_* if we left expansion-mode, if we stay they'll be
3923 * (re)set properly in ins_complete() */
3924 if (!vim_is_ctrl_x_key(c))
3925 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003926 compl_cont_status = 0;
3927 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003929
3930 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931}
3932
3933/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003934 * Fix the redo buffer for the completion leader replacing some of the typed
3935 * text. This inserts backspaces and appends the changed text.
3936 * "ptr" is the known leader text or NUL.
3937 */
3938 static void
3939ins_compl_fixRedoBufForLeader(ptr_arg)
3940 char_u *ptr_arg;
3941{
3942 int len;
3943 char_u *p;
3944 char_u *ptr = ptr_arg;
3945
3946 if (ptr == NULL)
3947 {
3948 if (compl_leader != NULL)
3949 ptr = compl_leader;
3950 else
3951 return; /* nothing to do */
3952 }
3953 if (compl_orig_text != NULL)
3954 {
3955 p = compl_orig_text;
3956 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3957 ;
3958#ifdef FEAT_MBYTE
3959 if (len > 0)
3960 len -= (*mb_head_off)(p, p + len);
3961#endif
3962 for (p += len; *p != NUL; mb_ptr_adv(p))
3963 AppendCharToRedobuff(K_BS);
3964 }
3965 else
3966 len = 0;
3967 if (ptr != NULL)
3968 AppendToRedobuffLit(ptr + len, -1);
3969}
3970
3971/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3973 * (depending on flag) starting from buf and looking for a non-scanned
3974 * buffer (other than curbuf). curbuf is special, if it is called with
3975 * buf=curbuf then it has to be the first call for a given flag/expansion.
3976 *
3977 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3978 */
3979 static buf_T *
3980ins_compl_next_buf(buf, flag)
3981 buf_T *buf;
3982 int flag;
3983{
3984#ifdef FEAT_WINDOWS
3985 static win_T *wp;
3986#endif
3987
3988 if (flag == 'w') /* just windows */
3989 {
3990#ifdef FEAT_WINDOWS
3991 if (buf == curbuf) /* first call for this flag/expansion */
3992 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003993 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994 && wp->w_buffer->b_scanned)
3995 ;
3996 buf = wp->w_buffer;
3997#else
3998 buf = curbuf;
3999#endif
4000 }
4001 else
4002 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4003 * (unlisted buffers)
4004 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004005 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 && ((flag == 'U'
4007 ? buf->b_p_bl
4008 : (!buf->b_p_bl
4009 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004010 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 ;
4012 return buf;
4013}
4014
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004015#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004016static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004017
4018/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004019 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004020 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004021 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004022 static void
4023expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004024 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004025 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004026{
Bram Moolenaar82139082011-09-14 16:52:09 +02004027 list_T *matchlist = NULL;
4028 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004029 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004030 char_u *funcname;
4031 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004032 win_T *curwin_save;
4033 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004034 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004035
Bram Moolenaare344bea2005-09-01 20:46:49 +00004036 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4037 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004038 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004039
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004040 /* Call 'completefunc' to obtain the list of matches. */
4041 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004042 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004043
Bram Moolenaare344bea2005-09-01 20:46:49 +00004044 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004045 curwin_save = curwin;
4046 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004047
4048 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004049 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004050 {
4051 switch (rettv.v_type)
4052 {
4053 case VAR_LIST:
4054 matchlist = rettv.vval.v_list;
4055 break;
4056 case VAR_DICT:
4057 matchdict = rettv.vval.v_dict;
4058 break;
4059 default:
4060 /* TODO: Give error message? */
4061 clear_tv(&rettv);
4062 break;
4063 }
4064 }
4065
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004066 if (curwin_save != curwin || curbuf_save != curbuf)
4067 {
4068 EMSG(_(e_complwin));
4069 goto theend;
4070 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004071 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004072 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004073 if (!equalpos(curwin->w_cursor, pos))
4074 {
4075 EMSG(_(e_compldel));
4076 goto theend;
4077 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004078
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004079 if (matchlist != NULL)
4080 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004081 else if (matchdict != NULL)
4082 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004083
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004084theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004085 if (matchdict != NULL)
4086 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004087 if (matchlist != NULL)
4088 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004089}
4090#endif /* FEAT_COMPL_FUNC */
4091
Bram Moolenaar39f05632006-03-19 22:15:26 +00004092#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004093/*
4094 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004095 */
4096 static void
4097ins_compl_add_list(list)
4098 list_T *list;
4099{
4100 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004101 int dir = compl_direction;
4102
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004103 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004104 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004105 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004106 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4107 /* if dir was BACKWARD then honor it just once */
4108 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004109 else if (did_emsg)
4110 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004111 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004112}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004113
4114/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004115 * Add completions from a dict.
4116 */
4117 static void
4118ins_compl_add_dict(dict)
4119 dict_T *dict;
4120{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004121 dictitem_T *di_refresh;
4122 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004123
4124 /* Check for optional "refresh" item. */
4125 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004126 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4127 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004128 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004129 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004130
4131 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4132 compl_opt_refresh_always = TRUE;
4133 }
4134
4135 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004136 di_words = dict_find(dict, (char_u *)"words", 5);
4137 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4138 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004139}
4140
4141/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004142 * Add a match to the list of matches from a typeval_T.
4143 * If the given string is already in the list of completions, then return
4144 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4145 * maybe because alloc() returns NULL, then FAIL is returned.
4146 */
4147 int
4148ins_compl_add_tv(tv, dir)
4149 typval_T *tv;
4150 int dir;
4151{
4152 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004153 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004154 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004155 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004156 char_u *(cptext[CPT_COUNT]);
4157
4158 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4159 {
4160 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4161 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4162 (char_u *)"abbr", FALSE);
4163 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4164 (char_u *)"menu", FALSE);
4165 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4166 (char_u *)"kind", FALSE);
4167 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4168 (char_u *)"info", FALSE);
4169 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4170 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004171 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004172 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004173 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4174 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004175 }
4176 else
4177 {
4178 word = get_tv_string_chk(tv);
4179 vim_memset(cptext, 0, sizeof(cptext));
4180 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004181 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004182 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004183 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004184}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004185#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004186
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004187/*
4188 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004189 * The search starts at position "ini" in curbuf and in the direction
4190 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004191 * When "compl_started" is FALSE start at that position, otherwise continue
4192 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193 * This may return before finding all the matches.
4194 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 */
4196 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004197ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199{
4200 static pos_T first_match_pos;
4201 static pos_T last_match_pos;
4202 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004203 static int found_all = FALSE; /* Found all matches of a
4204 certain type. */
4205 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206
Bram Moolenaar572cb562005-08-05 21:35:02 +00004207 pos_T *pos;
4208 char_u **matches;
4209 int save_p_scs;
4210 int save_p_ws;
4211 int save_p_ic;
4212 int i;
4213 int num_matches;
4214 int len;
4215 int found_new_match;
4216 int type = ctrl_x_mode;
4217 char_u *ptr;
4218 char_u *dict = NULL;
4219 int dict_f = 0;
4220 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004221 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
4225 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4226 ins_buf->b_scanned = 0;
4227 found_all = FALSE;
4228 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004229 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004230 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 last_match_pos = first_match_pos = *ini;
4232 }
4233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004234 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004235 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4237 for (;;)
4238 {
4239 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004240 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 * or if found_all says this entry is done. For ^X^L only use the
4244 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004245 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 {
4248 found_all = FALSE;
4249 while (*e_cpt == ',' || *e_cpt == ' ')
4250 e_cpt++;
4251 if (*e_cpt == '.' && !curbuf->b_scanned)
4252 {
4253 ins_buf = curbuf;
4254 first_match_pos = *ini;
4255 /* So that ^N can match word immediately after cursor */
4256 if (ctrl_x_mode == 0)
4257 dec(&first_match_pos);
4258 last_match_pos = first_match_pos;
4259 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004260
4261 /* Remember the first match so that the loop stops when we
4262 * wrap and come back there a second time. */
4263 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 }
4265 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4266 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4267 {
4268 /* Scan a buffer, but not the current one. */
4269 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4270 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 first_match_pos.col = last_match_pos.col = 0;
4273 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4274 last_match_pos.lnum = 0;
4275 type = 0;
4276 }
4277 else /* unloaded buffer, scan like dictionary */
4278 {
4279 found_all = TRUE;
4280 if (ins_buf->b_fname == NULL)
4281 continue;
4282 type = CTRL_X_DICTIONARY;
4283 dict = ins_buf->b_fname;
4284 dict_f = DICT_EXACT;
4285 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004286 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 ins_buf->b_fname == NULL
4288 ? buf_spname(ins_buf)
4289 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004290 ? ins_buf->b_fname
4291 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004292 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294 else if (*e_cpt == NUL)
4295 break;
4296 else
4297 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004298 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 type = -1;
4300 else if (*e_cpt == 'k' || *e_cpt == 's')
4301 {
4302 if (*e_cpt == 'k')
4303 type = CTRL_X_DICTIONARY;
4304 else
4305 type = CTRL_X_THESAURUS;
4306 if (*++e_cpt != ',' && *e_cpt != NUL)
4307 {
4308 dict = e_cpt;
4309 dict_f = DICT_FIRST;
4310 }
4311 }
4312#ifdef FEAT_FIND_ID
4313 else if (*e_cpt == 'i')
4314 type = CTRL_X_PATH_PATTERNS;
4315 else if (*e_cpt == 'd')
4316 type = CTRL_X_PATH_DEFINES;
4317#endif
4318 else if (*e_cpt == ']' || *e_cpt == 't')
4319 {
4320 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004321 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004322 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
4324 else
4325 type = -1;
4326
4327 /* in any case e_cpt is advanced to the next entry */
4328 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4329
4330 found_all = TRUE;
4331 if (type == -1)
4332 continue;
4333 }
4334 }
4335
4336 switch (type)
4337 {
4338 case -1:
4339 break;
4340#ifdef FEAT_FIND_ID
4341 case CTRL_X_PATH_PATTERNS:
4342 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004343 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004346 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4348 (linenr_T)1, (linenr_T)MAXLNUM);
4349 break;
4350#endif
4351
4352 case CTRL_X_DICTIONARY:
4353 case CTRL_X_THESAURUS:
4354 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004355 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 : (type == CTRL_X_THESAURUS
4357 ? (*curbuf->b_p_tsr == NUL
4358 ? p_tsr
4359 : curbuf->b_p_tsr)
4360 : (*curbuf->b_p_dict == NUL
4361 ? p_dict
4362 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004363 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004364 dict != NULL ? dict_f
4365 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 dict = NULL;
4367 break;
4368
4369 case CTRL_X_TAGS:
4370 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4371 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004372 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004374 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004375 * of matches is found when compl_pattern is empty */
4376 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4378 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4379 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4380 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004381 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 }
4383 p_ic = save_p_ic;
4384 break;
4385
4386 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4389 {
4390
4391 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004393 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 }
4395 break;
4396
4397 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 if (expand_cmdline(&compl_xp, compl_pattern,
4399 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004401 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 break;
4403
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004404#ifdef FEAT_COMPL_FUNC
4405 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004406 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004407 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004408 break;
4409#endif
4410
Bram Moolenaar488c6512005-08-11 20:09:58 +00004411 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004412#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004413 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004414 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004415 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004416 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004417#endif
4418 break;
4419
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 default: /* normal ^P/^N and ^X^L */
4421 /*
4422 * If 'infercase' is set, don't use 'smartcase' here
4423 */
4424 save_p_scs = p_scs;
4425 if (ins_buf->b_p_inf)
4426 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427
Bram Moolenaar361aa502014-01-23 22:45:58 +01004428 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 * end but never from the middle, thus setting nowrapscan in this
4430 * buffers is a good idea, on the other hand, we always set
4431 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4432 save_p_ws = p_ws;
4433 if (ins_buf != curbuf)
4434 p_ws = FALSE;
4435 else if (*e_cpt == '.')
4436 p_ws = TRUE;
4437 for (;;)
4438 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004439 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004441 ++msg_silent; /* Don't want messages for wrapscan. */
4442
Bram Moolenaare4214502015-03-05 18:08:43 +01004443 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4444 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004445 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004446 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004449 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004451 found_new_match = searchit(NULL, ins_buf, pos,
4452 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004453 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004454 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004455 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004456 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004458 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 first_match_pos = *pos;
4461 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004462 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004465 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 found_new_match = FAIL;
4467 if (found_new_match == FAIL)
4468 {
4469 if (ins_buf == curbuf)
4470 found_all = TRUE;
4471 break;
4472 }
4473
4474 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004475 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 && ini->lnum == pos->lnum
4477 && ini->col == pos->col)
4478 continue;
4479 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004480 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 {
4484 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4485 continue;
4486 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4487 if (!p_paste)
4488 ptr = skipwhite(ptr);
4489 }
4490 len = (int)STRLEN(ptr);
4491 }
4492 else
4493 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004494 char_u *tmp_ptr = ptr;
4495
4496 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004498 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 /* Skip if already inside a word. */
4500 if (vim_iswordp(tmp_ptr))
4501 continue;
4502 /* Find start of next word. */
4503 tmp_ptr = find_word_start(tmp_ptr);
4504 }
4505 /* Find end of this word. */
4506 tmp_ptr = find_word_end(tmp_ptr);
4507 len = (int)(tmp_ptr - ptr);
4508
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004509 if ((compl_cont_status & CONT_ADDING)
4510 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
4512 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4513 {
4514 /* Try next line, if any. the new word will be
4515 * "join" as if the normal command "J" was used.
4516 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004517 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 * works -- Acevedo */
4519 STRNCPY(IObuff, ptr, len);
4520 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4521 tmp_ptr = ptr = skipwhite(ptr);
4522 /* Find start of next word. */
4523 tmp_ptr = find_word_start(tmp_ptr);
4524 /* Find end of next word. */
4525 tmp_ptr = find_word_end(tmp_ptr);
4526 if (tmp_ptr > ptr)
4527 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004528 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004530 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 IObuff[len++] = ' ';
4532 /* IObuf =~ "\k.* ", thus len >= 2 */
4533 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004534 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 || (vim_strchr(p_cpo, CPO_JOINSP)
4536 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004537 && (IObuff[len - 2] == '?'
4538 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 IObuff[len++] = ' ';
4540 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004541 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 if (tmp_ptr - ptr >= IOSIZE - len)
4543 tmp_ptr = ptr + IOSIZE - len - 1;
4544 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4545 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004546 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 }
4548 IObuff[len] = NUL;
4549 ptr = IObuff;
4550 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004551 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 continue;
4553 }
4554 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004555 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004556 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004557 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 {
4559 found_new_match = OK;
4560 break;
4561 }
4562 }
4563 p_scs = save_p_scs;
4564 p_ws = save_p_ws;
4565 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004566
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004567 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004568 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004569 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 found_new_match = OK;
4571
4572 /* break the loop for specialized modes (use 'complete' just for the
4573 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004574 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004575 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004576 {
4577 if (got_int)
4578 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004579 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004580 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004581 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004582
Bram Moolenaare4214502015-03-05 18:08:43 +01004583 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004584 || compl_interrupted)
4585 break;
4586 compl_started = TRUE;
4587 }
4588 else
4589 {
4590 /* Mark a buffer scanned when it has been scanned completely */
4591 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4592 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004594 compl_started = FALSE;
4595 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004597 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
Bram Moolenaare4214502015-03-05 18:08:43 +01004599 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004600 && *e_cpt == NUL) /* Got to end of 'complete' */
4601 found_new_match = FAIL;
4602
4603 i = -1; /* total of matches, unknown */
4604 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004605 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004606 i = ins_compl_make_cyclic();
4607
4608 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004609 * just been made cyclic then we have to move compl_curr_match to the next
4610 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004611 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4612 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004613 if (compl_curr_match == NULL)
4614 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 return i;
4616}
4617
4618/* Delete the old text being completed. */
4619 static void
4620ins_compl_delete()
4621{
4622 int i;
4623
4624 /*
4625 * In insert mode: Delete the typed part.
4626 * In replace mode: Put the old characters back, if any.
4627 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004628 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 backspace_until_column(i);
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004630
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004631 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4632 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004634 /* clear v:completed_item */
4635 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636}
4637
4638/* Insert the new text being completed. */
4639 static void
4640ins_compl_insert()
4641{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004642 dict_T *dict;
4643
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004644 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004645 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4646 compl_used_match = FALSE;
4647 else
4648 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004649
4650 /* Set completed item. */
4651 /* { word, abbr, menu, kind, info } */
4652 dict = dict_alloc();
4653 if (dict != NULL)
4654 {
4655 dict_add_nr_str(dict, "word", 0L,
4656 EMPTY_IF_NULL(compl_shown_match->cp_str));
4657 dict_add_nr_str(dict, "abbr", 0L,
4658 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4659 dict_add_nr_str(dict, "menu", 0L,
4660 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4661 dict_add_nr_str(dict, "kind", 0L,
4662 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4663 dict_add_nr_str(dict, "info", 0L,
4664 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4665 }
4666 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667}
4668
4669/*
4670 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004671 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4672 * get more completions. If it is FALSE, then we just do nothing when there
4673 * are no more completions in a given direction. The latter case is used when
4674 * we are still in the middle of finding completions, to allow browsing
4675 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 * Return the total number of matches, or -1 if still unknown -- webb.
4677 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004678 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4679 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 *
4681 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004682 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4683 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 */
4685 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004686ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004688 int count; /* repeat completion this many times; should
4689 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004690 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
4692 int num_matches = -1;
4693 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004694 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004695 compl_T *found_compl = NULL;
4696 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004697 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004698 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004700 /* When user complete function return -1 for findstart which is next
4701 * time of 'always', compl_shown_match become NULL. */
4702 if (compl_shown_match == NULL)
4703 return -1;
4704
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004705 if (compl_leader != NULL
4706 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004708 /* Set "compl_shown_match" to the actually shown match, it may differ
4709 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004710 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004711 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004712 && compl_shown_match->cp_next != NULL
4713 && compl_shown_match->cp_next != compl_first_match)
4714 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004715
4716 /* If we didn't find it searching forward, and compl_shows_dir is
4717 * backward, find the last match. */
4718 if (compl_shows_dir == BACKWARD
4719 && !ins_compl_equal(compl_shown_match,
4720 compl_leader, (int)STRLEN(compl_leader))
4721 && (compl_shown_match->cp_next == NULL
4722 || compl_shown_match->cp_next == compl_first_match))
4723 {
4724 while (!ins_compl_equal(compl_shown_match,
4725 compl_leader, (int)STRLEN(compl_leader))
4726 && compl_shown_match->cp_prev != NULL
4727 && compl_shown_match->cp_prev != compl_first_match)
4728 compl_shown_match = compl_shown_match->cp_prev;
4729 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004730 }
4731
4732 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004733 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 /* Delete old text to be replaced */
4735 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004736
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004737 /* When finding the longest common text we stick at the original text,
4738 * don't let CTRL-N or CTRL-P move to the first match. */
4739 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4740
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004741 /* When restarting the search don't insert the first match either. */
4742 if (compl_restarting)
4743 {
4744 advance = FALSE;
4745 compl_restarting = FALSE;
4746 }
4747
Bram Moolenaare3226be2005-12-18 22:10:00 +00004748 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4749 * around. */
4750 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004752 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004754 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004755 found_end = (compl_first_match != NULL
4756 && (compl_shown_match->cp_next == compl_first_match
4757 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004758 }
4759 else if (compl_shows_dir == BACKWARD
4760 && compl_shown_match->cp_prev != NULL)
4761 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004762 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004763 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004764 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 }
4766 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004767 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004768 if (!allow_get_expansion)
4769 {
4770 if (advance)
4771 {
4772 if (compl_shows_dir == BACKWARD)
4773 compl_pending -= todo + 1;
4774 else
4775 compl_pending += todo + 1;
4776 }
4777 return -1;
4778 }
4779
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004780 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004781 {
4782 if (compl_shows_dir == BACKWARD)
4783 --compl_pending;
4784 else
4785 ++compl_pending;
4786 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004787
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004788 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004789 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004790
4791 /* handle any pending completions */
4792 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004793 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004794 {
4795 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4796 {
4797 compl_shown_match = compl_shown_match->cp_next;
4798 --compl_pending;
4799 }
4800 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4801 {
4802 compl_shown_match = compl_shown_match->cp_prev;
4803 ++compl_pending;
4804 }
4805 else
4806 break;
4807 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004808 found_end = FALSE;
4809 }
4810 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4811 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004812 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004813 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004814 ++todo;
4815 else
4816 /* Remember a matching item. */
4817 found_compl = compl_shown_match;
4818
4819 /* Stop at the end of the list when we found a usable match. */
4820 if (found_end)
4821 {
4822 if (found_compl != NULL)
4823 {
4824 compl_shown_match = found_compl;
4825 break;
4826 }
4827 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004831 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004832 if (compl_no_insert && !started)
4833 {
4834 ins_bytes(compl_orig_text + ins_compl_len());
4835 compl_used_match = FALSE;
4836 }
4837 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004838 {
4839 if (!compl_get_longest || compl_used_match)
4840 ins_compl_insert();
4841 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004842 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004843 }
4844 else
4845 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846
4847 if (!allow_get_expansion)
4848 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004849 /* may undisplay the popup menu first */
4850 ins_compl_upd_pum();
4851
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004852 /* redraw to show the user what was inserted */
4853 update_screen(0);
4854
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004855 /* display the updated popup menu */
4856 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004857#ifdef FEAT_GUI
4858 if (gui.in_use)
4859 {
4860 /* Show the cursor after the match, not after the redrawn text. */
4861 setcursor();
4862 out_flush();
4863 gui_update_cursor(FALSE, FALSE);
4864 }
4865#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004866
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 /* Delete old text to be replaced, since we're still searching and
4868 * don't want to match ourselves! */
4869 ins_compl_delete();
4870 }
4871
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004872 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004873 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004874 if (compl_no_insert && !started)
4875 compl_enter_selects = TRUE;
4876 else
4877 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004878
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 /*
4880 * Show the file name for the match (if any)
4881 * Truncate the file name to avoid a wait for return.
4882 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004883 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 {
4885 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004886 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 if (i <= 0)
4888 i = 0;
4889 else
4890 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004891 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 msg(IObuff);
4893 redraw_cmdline = FALSE; /* don't overwrite! */
4894 }
4895
4896 return num_matches;
4897}
4898
4899/*
4900 * Call this while finding completions, to check whether the user has hit a key
4901 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004902 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004904 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 */
4906 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004907ins_compl_check_keys(frequency)
4908 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909{
4910 static int count = 0;
4911
4912 int c;
4913
4914 /* Don't check when reading keys from a script. That would break the test
4915 * scripts */
4916 if (using_script())
4917 return;
4918
4919 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004920 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 return;
4922 count = 0;
4923
Bram Moolenaara260a972006-06-23 19:36:29 +00004924 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4925 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 if (c != NUL)
4928 {
4929 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4930 {
4931 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004932 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004933 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4934 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004936 else
4937 {
4938 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004939 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004940 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004941 if (c != K_IGNORE)
4942 {
4943 /* Don't interrupt completion when the character wasn't typed,
4944 * e.g., when doing @q to replay keys. */
4945 if (c != Ctrl_R && KeyTyped)
4946 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004947
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004948 vungetc(c);
4949 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004952 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004953 {
4954 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4955
4956 compl_pending = 0;
4957 (void)ins_compl_next(FALSE, todo, TRUE);
4958 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004959}
4960
4961/*
4962 * Decide the direction of Insert mode complete from the key typed.
4963 * Returns BACKWARD or FORWARD.
4964 */
4965 static int
4966ins_compl_key2dir(c)
4967 int c;
4968{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004969 if (c == Ctrl_P || c == Ctrl_L
4970 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4971 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004972 return BACKWARD;
4973 return FORWARD;
4974}
4975
4976/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004977 * Return TRUE for keys that are used for completion only when the popup menu
4978 * is visible.
4979 */
4980 static int
4981ins_compl_pum_key(c)
4982 int c;
4983{
4984 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004985 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4986 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004987}
4988
4989/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004990 * Decide the number of completions to move forward.
4991 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4992 */
4993 static int
4994ins_compl_key2count(c)
4995 int c;
4996{
4997 int h;
4998
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004999 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005000 {
5001 h = pum_get_height();
5002 if (h > 3)
5003 h -= 2; /* keep some context */
5004 return h;
5005 }
5006 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007}
5008
5009/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005010 * Return TRUE if completion with "c" should insert the match, FALSE if only
5011 * to change the currently selected completion.
5012 */
5013 static int
5014ins_compl_use_match(c)
5015 int c;
5016{
5017 switch (c)
5018 {
5019 case K_UP:
5020 case K_DOWN:
5021 case K_PAGEDOWN:
5022 case K_KPAGEDOWN:
5023 case K_S_DOWN:
5024 case K_PAGEUP:
5025 case K_KPAGEUP:
5026 case K_S_UP:
5027 return FALSE;
5028 }
5029 return TRUE;
5030}
5031
5032/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 * Do Insert mode completion.
5034 * Called when character "c" was typed, which has a meaning for completion.
5035 * Returns OK if completion was done, FAIL if something failed (out of mem).
5036 */
5037 static int
5038ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005039 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005041 char_u *line;
5042 int startcol = 0; /* column where searched text starts */
5043 colnr_T curs_col; /* cursor column */
5044 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005045 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046
Bram Moolenaare3226be2005-12-18 22:10:00 +00005047 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005048 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 {
5050 /* First time we hit ^N or ^P (in a row, I mean) */
5051
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 did_ai = FALSE;
5053#ifdef FEAT_SMARTINDENT
5054 did_si = FALSE;
5055 can_si = FALSE;
5056 can_si_back = FALSE;
5057#endif
5058 if (stop_arrow() == FAIL)
5059 return FAIL;
5060
5061 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005062 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005063 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005065 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005066 * "compl_startpos" to the cursor as a pattern to add a new word
5067 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005068 * "compl_startpos" is not in the same line as the cursor then fix it
5069 * (the line has been split because it was longer than 'tw'). if SOL
5070 * is set then skip the previous pattern, a word at the beginning of
5071 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005072 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5073 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
5075 /*
5076 * it is a continued search
5077 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005078 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5080 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5081 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005082 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 /* line (probably) wrapped, set compl_startpos to the
5085 * first non_blank in the line, if it is not a wordchar
5086 * include it to get a better pattern, but then we don't
5087 * want the "\\<" prefix, check it bellow */
5088 compl_col = (colnr_T)(skipwhite(line) - line);
5089 compl_startpos.col = compl_col;
5090 compl_startpos.lnum = curwin->w_cursor.lnum;
5091 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 }
5093 else
5094 {
5095 /* S_IPOS was set when we inserted a word that was at the
5096 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005097 * mode but first we need to redefine compl_startpos */
5098 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005100 compl_cont_status |= CONT_SOL;
5101 compl_startpos.col = (colnr_T)(skipwhite(
5102 line + compl_length
5103 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005107 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005108 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005109 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005113 compl_cont_status &= ~CONT_SOL;
5114 compl_length = (IOSIZE - MIN_SPACE);
5115 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005117 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5118 if (compl_length < 1)
5119 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005121 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005122 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005124 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
5126 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005127 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005131 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 compl_cont_status = 0;
5134 compl_cont_status |= CONT_N_ADDS;
5135 compl_startpos = curwin->w_cursor;
5136 startcol = (int)curs_col;
5137 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139
5140 /* Work out completion pattern and original text -- webb */
5141 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5142 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005143 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5145 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005146 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005148 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005150 compl_col += ++startcol;
5151 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154 compl_pattern = str_foldcase(line + compl_col,
5155 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 compl_pattern = vim_strnsave(line + compl_col,
5158 compl_length);
5159 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 return FAIL;
5161 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005162 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 char_u *prefix = (char_u *)"\\<";
5165
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005166 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005168 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 if (!vim_iswordp(line + compl_col)
5172 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 && (
5174#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178#endif
5179 )))
5180 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005181 STRCPY((char *)compl_pattern, prefix);
5182 (void)quote_meta(compl_pattern + STRLEN(prefix),
5183 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005185 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190#endif
5191 )
5192 {
5193 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005194 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5195 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 compl_col += curs_col;
5198 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200 else
5201 {
5202#ifdef FEAT_MBYTE
5203 /* Search the point of change class of multibyte character
5204 * or not a word single byte character backward. */
5205 if (has_mbyte)
5206 {
5207 int base_class;
5208 int head_off;
5209
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005210 startcol -= (*mb_head_off)(line, line + startcol);
5211 base_class = mb_get_class(line + startcol);
5212 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005213 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005214 head_off = (*mb_head_off)(line, line + startcol);
5215 if (base_class != mb_get_class(line + startcol
5216 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 }
5220 }
5221 else
5222#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 compl_col += ++startcol;
5226 compl_length = (int)curs_col - startcol;
5227 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 {
5229 /* Only match word with at least two chars -- webb
5230 * there's no need to call quote_meta,
5231 * alloc(7) is enough -- Acevedo
5232 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005233 compl_pattern = alloc(7);
5234 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005236 STRCPY((char *)compl_pattern, "\\<");
5237 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5238 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 }
5240 else
5241 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005242 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005243 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005244 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005246 STRCPY((char *)compl_pattern, "\\<");
5247 (void)quote_meta(compl_pattern + 2, line + compl_col,
5248 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250 }
5251 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005252 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005254 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005255 compl_length = (int)curs_col - (int)compl_col;
5256 if (compl_length < 0) /* cursor in indent: empty pattern */
5257 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005259 compl_pattern = str_foldcase(line + compl_col, compl_length,
5260 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005262 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5263 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 return FAIL;
5265 }
5266 else if (ctrl_x_mode == CTRL_X_FILES)
5267 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005268 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005269 if (startcol > 0)
5270 {
5271 char_u *p = line + startcol;
5272
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005273 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005274 while (p > line && vim_isfilec(PTR2CHAR(p)))
5275 mb_ptr_back(line, p);
5276 if (p == line && vim_isfilec(PTR2CHAR(p)))
5277 startcol = 0;
5278 else
5279 startcol = (int)(p - line) + 1;
5280 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005281
Bram Moolenaar0300e462013-09-08 16:03:45 +02005282 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005283 compl_length = (int)curs_col - startcol;
5284 compl_pattern = addstar(line + compl_col, compl_length,
5285 EXPAND_FILES);
5286 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005287 return FAIL;
5288 }
5289 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5290 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 compl_pattern = vim_strnsave(line, curs_col);
5292 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005294 set_cmd_context(&compl_xp, compl_pattern,
5295 (int)STRLEN(compl_pattern), curs_col);
5296 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5297 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005298 /* No completion possible, use an empty pattern to get a
5299 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005300 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005301 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005302 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5303 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005305 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005306 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005307#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005308 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005309 * Call user defined function 'completefunc' with "a:findstart"
5310 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005311 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005312 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005313 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005314 char_u *funcname;
5315 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005316 win_T *curwin_save;
5317 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005318
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005319 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005320 * string */
5321 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5322 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5323 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005324 {
5325 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5326 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005327 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005328 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005329
5330 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005331 args[1] = NULL;
5332 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005333 curwin_save = curwin;
5334 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005335 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005336 if (curwin_save != curwin || curbuf_save != curbuf)
5337 {
5338 EMSG(_(e_complwin));
5339 return FAIL;
5340 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005341 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005342 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005343 if (!equalpos(curwin->w_cursor, pos))
5344 {
5345 EMSG(_(e_compldel));
5346 return FAIL;
5347 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005348
Bram Moolenaar6110a002012-01-26 18:58:38 +01005349 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005350 * cancel the complete without an error.
5351 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005352 if (col == -2)
5353 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005354 if (col == -3)
5355 {
5356 ctrl_x_mode = 0;
5357 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005358 if (!shortmess(SHM_COMPLETIONMENU))
5359 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005360 return FAIL;
5361 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005362
Bram Moolenaar82139082011-09-14 16:52:09 +02005363 /*
5364 * Reset extended parameters of completion, when start new
5365 * completion.
5366 */
5367 compl_opt_refresh_always = FALSE;
5368
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005369 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005370 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005371 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005372 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005373 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005374
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005375 /* Setup variables for completion. Need to obtain "line" again,
5376 * it may have become invalid. */
5377 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005378 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005379 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5380 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005381#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 return FAIL;
5383 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005384 else if (ctrl_x_mode == CTRL_X_SPELL)
5385 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005386#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005387 if (spell_bad_len > 0)
5388 compl_col = curs_col - spell_bad_len;
5389 else
5390 compl_col = spell_word_start(startcol);
5391 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005392 {
5393 compl_length = 0;
5394 compl_col = curs_col;
5395 }
5396 else
5397 {
5398 spell_expand_check_cap(compl_col);
5399 compl_length = (int)curs_col - compl_col;
5400 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005401 /* Need to obtain "line" again, it may have become invalid. */
5402 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005403 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5404 if (compl_pattern == NULL)
5405#endif
5406 return FAIL;
5407 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005408 else
5409 {
5410 EMSG2(_(e_intern2), "ins_complete()");
5411 return FAIL;
5412 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005414 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 {
5416 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005417 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 {
5419 /* Insert a new line, keep indentation but ignore 'comments' */
5420#ifdef FEAT_COMMENTS
5421 char_u *old = curbuf->b_p_com;
5422
5423 curbuf->b_p_com = (char_u *)"";
5424#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005425 compl_startpos.lnum = curwin->w_cursor.lnum;
5426 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 ins_eol('\r');
5428#ifdef FEAT_COMMENTS
5429 curbuf->b_p_com = old;
5430#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005431 compl_length = 0;
5432 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 }
5434 }
5435 else
5436 {
5437 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005438 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 }
5440
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 if (compl_cont_status & CONT_LOCAL)
5442 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 else
5444 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5445
Bram Moolenaar62951b12011-09-21 18:23:05 +02005446 /* If any of the original typed text has been changed we need to fix
5447 * the redo buffer. */
5448 ins_compl_fixRedoBufForLeader(NULL);
5449
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005450 /* Always add completion for the original text. */
5451 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005452 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5453 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005454 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005456 vim_free(compl_pattern);
5457 compl_pattern = NULL;
5458 vim_free(compl_orig_text);
5459 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 return FAIL;
5461 }
5462
5463 /* showmode might reset the internal line pointers, so it must
5464 * be called before line = ml_get(), or when this address is no
5465 * longer needed. -- Acevedo.
5466 */
5467 edit_submode_extra = (char_u *)_("-- Searching...");
5468 edit_submode_highl = HLF_COUNT;
5469 showmode();
5470 edit_submode_extra = NULL;
5471 out_flush();
5472 }
5473
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005474 compl_shown_match = compl_curr_match;
5475 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005476
5477 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005478 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005479 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005480 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005481 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005483 /* may undisplay the popup menu */
5484 ins_compl_upd_pum();
5485
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005486 if (n > 1) /* all matches have been found */
5487 compl_matches = n;
5488 compl_curr_match = compl_shown_match;
5489 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490
Bram Moolenaard68071d2006-05-02 22:08:30 +00005491 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5492 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005493 if (got_int && !global_busy)
5494 {
5495 (void)vgetc();
5496 got_int = FALSE;
5497 }
5498
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005499 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005500 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005502 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5503 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5505 edit_submode_highl = HLF_E;
5506 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5507 * because we couldn't expand anything at first place, but if we used
5508 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5509 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005510 if ( compl_length > 1
5511 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512 || (ctrl_x_mode != 0
5513 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5514 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516 }
5517
Bram Moolenaar572cb562005-08-05 21:35:02 +00005518 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005519 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005521 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522
5523 if (edit_submode_extra == NULL)
5524 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005525 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
5527 edit_submode_extra = (char_u *)_("Back at original");
5528 edit_submode_highl = HLF_W;
5529 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005530 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 edit_submode_extra = (char_u *)_("Word from other line");
5533 edit_submode_highl = HLF_COUNT;
5534 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005535 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 edit_submode_extra = (char_u *)_("The only match");
5538 edit_submode_highl = HLF_COUNT;
5539 }
5540 else
5541 {
5542 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005543 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005545 int number = 0;
5546 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005548 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
5550 /* search backwards for the first valid (!= -1) number.
5551 * This should normally succeed already at the first loop
5552 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005553 for (match = compl_curr_match->cp_prev; match != NULL
5554 && match != compl_first_match;
5555 match = match->cp_prev)
5556 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005558 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 break;
5560 }
5561 if (match != NULL)
5562 /* go up and assign all numbers which are not assigned
5563 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005564 for (match = match->cp_next;
5565 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005566 match = match->cp_next)
5567 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 }
5569 else /* BACKWARD */
5570 {
5571 /* search forwards (upwards) for the first valid (!= -1)
5572 * number. This should normally succeed already at the
5573 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005574 for (match = compl_curr_match->cp_next; match != NULL
5575 && match != compl_first_match;
5576 match = match->cp_next)
5577 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005579 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 break;
5581 }
5582 if (match != NULL)
5583 /* go down and assign all numbers which are not
5584 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005585 for (match = match->cp_prev; match
5586 && match->cp_number == -1;
5587 match = match->cp_prev)
5588 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 }
5590 }
5591
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005592 /* The match should always have a sequence number now, this is
5593 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005594 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005596 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5597 * Translations may need more than twice that. */
5598 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005600 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005601 vim_snprintf((char *)match_ref, sizeof(match_ref),
5602 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005603 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005605 vim_snprintf((char *)match_ref, sizeof(match_ref),
5606 _("match %d"),
5607 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 edit_submode_extra = match_ref;
5609 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005610 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611 curs_columns(FALSE);
5612 }
5613 }
5614 }
5615
5616 /* Show a message about what (completion) mode we're in. */
5617 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005618 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005619 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005620 if (edit_submode_extra != NULL)
5621 {
5622 if (!p_smd)
5623 msg_attr(edit_submode_extra,
5624 edit_submode_highl < HLF_COUNT
5625 ? hl_attr(edit_submode_highl) : 0);
5626 }
5627 else
5628 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005629 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630
Bram Moolenaard68071d2006-05-02 22:08:30 +00005631 /* Show the popup menu, unless we got interrupted. */
5632 if (!compl_interrupted)
5633 {
5634 /* RedrawingDisabled may be set when invoked through complete(). */
5635 n = RedrawingDisabled;
5636 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005637
5638 /* If the cursor moved we need to remove the pum first. */
5639 setcursor();
5640 if (save_w_wrow != curwin->w_wrow)
5641 ins_compl_del_pum();
5642
Bram Moolenaard68071d2006-05-02 22:08:30 +00005643 ins_compl_show_pum();
5644 setcursor();
5645 RedrawingDisabled = n;
5646 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005647 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005648 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005649
Bram Moolenaar071d4272004-06-13 20:20:40 +00005650 return OK;
5651}
5652
5653/*
5654 * Looks in the first "len" chars. of "src" for search-metachars.
5655 * If dest is not NULL the chars. are copied there quoting (with
5656 * a backslash) the metachars, and dest would be NUL terminated.
5657 * Returns the length (needed) of dest
5658 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005659 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660quote_meta(dest, src, len)
5661 char_u *dest;
5662 char_u *src;
5663 int len;
5664{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005665 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005666
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005667 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005668 {
5669 switch (*src)
5670 {
5671 case '.':
5672 case '*':
5673 case '[':
5674 if (ctrl_x_mode == CTRL_X_DICTIONARY
5675 || ctrl_x_mode == CTRL_X_THESAURUS)
5676 break;
5677 case '~':
5678 if (!p_magic) /* quote these only if magic is set */
5679 break;
5680 case '\\':
5681 if (ctrl_x_mode == CTRL_X_DICTIONARY
5682 || ctrl_x_mode == CTRL_X_THESAURUS)
5683 break;
5684 case '^': /* currently it's not needed. */
5685 case '$':
5686 m++;
5687 if (dest != NULL)
5688 *dest++ = '\\';
5689 break;
5690 }
5691 if (dest != NULL)
5692 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005693# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 /* Copy remaining bytes of a multibyte character. */
5695 if (has_mbyte)
5696 {
5697 int i, mb_len;
5698
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005699 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 if (mb_len > 0 && len >= mb_len)
5701 for (i = 0; i < mb_len; ++i)
5702 {
5703 --len;
5704 ++src;
5705 if (dest != NULL)
5706 *dest++ = *src;
5707 }
5708 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005709# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005710 }
5711 if (dest != NULL)
5712 *dest = NUL;
5713
5714 return m;
5715}
5716#endif /* FEAT_INS_EXPAND */
5717
5718/*
5719 * Next character is interpreted literally.
5720 * A one, two or three digit decimal number is interpreted as its byte value.
5721 * If one or two digits are entered, the next character is given to vungetc().
5722 * For Unicode a character > 255 may be returned.
5723 */
5724 int
5725get_literal()
5726{
5727 int cc;
5728 int nc;
5729 int i;
5730 int hex = FALSE;
5731 int octal = FALSE;
5732#ifdef FEAT_MBYTE
5733 int unicode = 0;
5734#endif
5735
5736 if (got_int)
5737 return Ctrl_C;
5738
5739#ifdef FEAT_GUI
5740 /*
5741 * In GUI there is no point inserting the internal code for a special key.
5742 * It is more useful to insert the string "<KEY>" instead. This would
5743 * probably be useful in a text window too, but it would not be
5744 * vi-compatible (maybe there should be an option for it?) -- webb
5745 */
5746 if (gui.in_use)
5747 ++allow_keys;
5748#endif
5749#ifdef USE_ON_FLY_SCROLL
5750 dont_scroll = TRUE; /* disallow scrolling here */
5751#endif
5752 ++no_mapping; /* don't map the next key hits */
5753 cc = 0;
5754 i = 0;
5755 for (;;)
5756 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005757 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758#ifdef FEAT_CMDL_INFO
5759 if (!(State & CMDLINE)
5760# ifdef FEAT_MBYTE
5761 && MB_BYTE2LEN_CHECK(nc) == 1
5762# endif
5763 )
5764 add_to_showcmd(nc);
5765#endif
5766 if (nc == 'x' || nc == 'X')
5767 hex = TRUE;
5768 else if (nc == 'o' || nc == 'O')
5769 octal = TRUE;
5770#ifdef FEAT_MBYTE
5771 else if (nc == 'u' || nc == 'U')
5772 unicode = nc;
5773#endif
5774 else
5775 {
5776 if (hex
5777#ifdef FEAT_MBYTE
5778 || unicode != 0
5779#endif
5780 )
5781 {
5782 if (!vim_isxdigit(nc))
5783 break;
5784 cc = cc * 16 + hex2nr(nc);
5785 }
5786 else if (octal)
5787 {
5788 if (nc < '0' || nc > '7')
5789 break;
5790 cc = cc * 8 + nc - '0';
5791 }
5792 else
5793 {
5794 if (!VIM_ISDIGIT(nc))
5795 break;
5796 cc = cc * 10 + nc - '0';
5797 }
5798
5799 ++i;
5800 }
5801
5802 if (cc > 255
5803#ifdef FEAT_MBYTE
5804 && unicode == 0
5805#endif
5806 )
5807 cc = 255; /* limit range to 0-255 */
5808 nc = 0;
5809
5810 if (hex) /* hex: up to two chars */
5811 {
5812 if (i >= 2)
5813 break;
5814 }
5815#ifdef FEAT_MBYTE
5816 else if (unicode) /* Unicode: up to four or eight chars */
5817 {
5818 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5819 break;
5820 }
5821#endif
5822 else if (i >= 3) /* decimal or octal: up to three chars */
5823 break;
5824 }
5825 if (i == 0) /* no number entered */
5826 {
5827 if (nc == K_ZERO) /* NUL is stored as NL */
5828 {
5829 cc = '\n';
5830 nc = 0;
5831 }
5832 else
5833 {
5834 cc = nc;
5835 nc = 0;
5836 }
5837 }
5838
5839 if (cc == 0) /* NUL is stored as NL */
5840 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005841#ifdef FEAT_MBYTE
5842 if (enc_dbcs && (cc & 0xff) == 0)
5843 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5844 second byte will cause trouble! */
5845#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
5847 --no_mapping;
5848#ifdef FEAT_GUI
5849 if (gui.in_use)
5850 --allow_keys;
5851#endif
5852 if (nc)
5853 vungetc(nc);
5854 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5855 return cc;
5856}
5857
5858/*
5859 * Insert character, taking care of special keys and mod_mask
5860 */
5861 static void
5862insert_special(c, allow_modmask, ctrlv)
5863 int c;
5864 int allow_modmask;
5865 int ctrlv; /* c was typed after CTRL-V */
5866{
5867 char_u *p;
5868 int len;
5869
5870 /*
5871 * Special function key, translate into "<Key>". Up to the last '>' is
5872 * inserted with ins_str(), so as not to replace characters in replace
5873 * mode.
5874 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5875 * unless 'allow_modmask' is TRUE.
5876 */
5877#ifdef MACOS
5878 /* Command-key never produces a normal key */
5879 if (mod_mask & MOD_MASK_CMD)
5880 allow_modmask = TRUE;
5881#endif
5882 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5883 {
5884 p = get_special_key_name(c, mod_mask);
5885 len = (int)STRLEN(p);
5886 c = p[len - 1];
5887 if (len > 2)
5888 {
5889 if (stop_arrow() == FAIL)
5890 return;
5891 p[len - 1] = NUL;
5892 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005893 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 ctrlv = FALSE;
5895 }
5896 }
5897 if (stop_arrow() == OK)
5898 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5899}
5900
5901/*
5902 * Special characters in this context are those that need processing other
5903 * than the simple insertion that can be performed here. This includes ESC
5904 * which terminates the insert, and CR/NL which need special processing to
5905 * open up a new line. This routine tries to optimize insertions performed by
5906 * the "redo", "undo" or "put" commands, so it needs to know when it should
5907 * stop and defer processing to the "normal" mechanism.
5908 * '0' and '^' are special, because they can be followed by CTRL-D.
5909 */
5910#ifdef EBCDIC
5911# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5912#else
5913# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5914#endif
5915
5916#ifdef FEAT_MBYTE
5917# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5918#else
5919# define WHITECHAR(cc) vim_iswhite(cc)
5920#endif
5921
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005922/*
5923 * "flags": INSCHAR_FORMAT - force formatting
5924 * INSCHAR_CTRLV - char typed just after CTRL-V
5925 * INSCHAR_NO_FEX - don't use 'formatexpr'
5926 *
5927 * NOTE: passes the flags value straight through to internal_format() which,
5928 * beside INSCHAR_FORMAT (above), is also looking for these:
5929 * INSCHAR_DO_COM - format comments
5930 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005932 void
5933insertchar(c, flags, second_indent)
5934 int c; /* character to insert or NUL */
5935 int flags; /* INSCHAR_FORMAT, etc. */
5936 int second_indent; /* indent for second line if >= 0 */
5937{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005938 int textwidth;
5939#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005940 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005943 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005944
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005945 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947
5948 /*
5949 * Try to break the line in two or more pieces when:
5950 * - Always do this if we have been called to do formatting only.
5951 * - Always do this when 'formatoptions' has the 'a' flag and the line
5952 * ends in white space.
5953 * - Otherwise:
5954 * - Don't do this if inserting a blank
5955 * - Don't do this if an existing character is being replaced, unless
5956 * we're in VREPLACE mode.
5957 * - Do this if the cursor is not on the line where insert started
5958 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5959 * before the insert.
5960 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5961 * before 'textwidth'
5962 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005963 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005964 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005965 || (!vim_iswhite(c)
5966 && !((State & REPLACE_FLAG)
5967#ifdef FEAT_VREPLACE
5968 && !(State & VREPLACE_FLAG)
5969#endif
5970 && *ml_get_cursor() != NUL)
5971 && (curwin->w_cursor.lnum != Insstart.lnum
5972 || ((!has_format_option(FO_INS_LONG)
5973 || Insstart_textlen <= (colnr_T)textwidth)
5974 && (!fo_ins_blank
5975 || Insstart_blank_vcol <= (colnr_T)textwidth
5976 ))))))
5977 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005978 /* Format with 'formatexpr' when it's set. Use internal formatting
5979 * when 'formatexpr' isn't set or it returns non-zero. */
5980#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005981 int do_internal = TRUE;
5982 colnr_T virtcol = get_nolist_virtcol()
5983 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005984
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005985 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
5986 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005987 {
5988 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5989 /* It may be required to save for undo again, e.g. when setline()
5990 * was called. */
5991 ins_need_undo = TRUE;
5992 }
5993 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005994#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005995 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005996 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005997
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 if (c == NUL) /* only formatting was wanted */
5999 return;
6000
6001#ifdef FEAT_COMMENTS
6002 /* Check whether this character should end a comment. */
6003 if (did_ai && (int)c == end_comment_pending)
6004 {
6005 char_u *line;
6006 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6007 int middle_len, end_len;
6008 int i;
6009
6010 /*
6011 * Need to remove existing (middle) comment leader and insert end
6012 * comment leader. First, check what comment leader we can find.
6013 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006014 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006015 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6016 {
6017 /* Skip middle-comment string */
6018 while (*p && p[-1] != ':') /* find end of middle flags */
6019 ++p;
6020 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6021 /* Don't count trailing white space for middle_len */
6022 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6023 --middle_len;
6024
6025 /* Find the end-comment string */
6026 while (*p && p[-1] != ':') /* find end of end flags */
6027 ++p;
6028 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6029
6030 /* Skip white space before the cursor */
6031 i = curwin->w_cursor.col;
6032 while (--i >= 0 && vim_iswhite(line[i]))
6033 ;
6034 i++;
6035
6036 /* Skip to before the middle leader */
6037 i -= middle_len;
6038
6039 /* Check some expected things before we go on */
6040 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6041 {
6042 /* Backspace over all the stuff we want to replace */
6043 backspace_until_column(i);
6044
6045 /*
6046 * Insert the end-comment string, except for the last
6047 * character, which will get inserted as normal later.
6048 */
6049 ins_bytes_len(lead_end, end_len - 1);
6050 }
6051 }
6052 }
6053 end_comment_pending = NUL;
6054#endif
6055
6056 did_ai = FALSE;
6057#ifdef FEAT_SMARTINDENT
6058 did_si = FALSE;
6059 can_si = FALSE;
6060 can_si_back = FALSE;
6061#endif
6062
6063 /*
6064 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6065 * This speeds up normal text input considerably.
6066 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6067 * need to re-indent at a ':', or any other character (but not what
6068 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006069 * Don't do this when there an InsertCharPre autocommand is defined,
6070 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006071 */
6072#ifdef USE_ON_FLY_SCROLL
6073 dont_scroll = FALSE; /* allow scrolling here */
6074#endif
6075
6076 if ( !ISSPECIAL(c)
6077#ifdef FEAT_MBYTE
6078 && (!has_mbyte || (*mb_char2len)(c) == 1)
6079#endif
6080 && vpeekc() != NUL
6081 && !(State & REPLACE_FLAG)
6082#ifdef FEAT_CINDENT
6083 && !cindent_on()
6084#endif
6085#ifdef FEAT_RIGHTLEFT
6086 && !p_ri
6087#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006088#ifdef FEAT_AUTOCMD
6089 && !has_insertcharpre()
6090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006091 )
6092 {
6093#define INPUT_BUFLEN 100
6094 char_u buf[INPUT_BUFLEN + 1];
6095 int i;
6096 colnr_T virtcol = 0;
6097
6098 buf[0] = c;
6099 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006100 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006101 virtcol = get_nolist_virtcol();
6102 /*
6103 * Stop the string when:
6104 * - no more chars available
6105 * - finding a special character (command key)
6106 * - buffer is full
6107 * - running into the 'textwidth' boundary
6108 * - need to check for abbreviation: A non-word char after a word-char
6109 */
6110 while ( (c = vpeekc()) != NUL
6111 && !ISSPECIAL(c)
6112#ifdef FEAT_MBYTE
6113 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6114#endif
6115 && i < INPUT_BUFLEN
6116 && (textwidth == 0
6117 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6118 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6119 {
6120#ifdef FEAT_RIGHTLEFT
6121 c = vgetc();
6122 if (p_hkmap && KeyTyped)
6123 c = hkmap(c); /* Hebrew mode mapping */
6124# ifdef FEAT_FKMAP
6125 if (p_fkmap && KeyTyped)
6126 c = fkmap(c); /* Farsi mode mapping */
6127# endif
6128 buf[i++] = c;
6129#else
6130 buf[i++] = vgetc();
6131#endif
6132 }
6133
6134#ifdef FEAT_DIGRAPHS
6135 do_digraph(-1); /* clear digraphs */
6136 do_digraph(buf[i-1]); /* may be the start of a digraph */
6137#endif
6138 buf[i] = NUL;
6139 ins_str(buf);
6140 if (flags & INSCHAR_CTRLV)
6141 {
6142 redo_literal(*buf);
6143 i = 1;
6144 }
6145 else
6146 i = 0;
6147 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006148 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006149 }
6150 else
6151 {
6152#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006153 int cc;
6154
Bram Moolenaar071d4272004-06-13 20:20:40 +00006155 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6156 {
6157 char_u buf[MB_MAXBYTES + 1];
6158
6159 (*mb_char2bytes)(c, buf);
6160 buf[cc] = NUL;
6161 ins_char_bytes(buf, cc);
6162 AppendCharToRedobuff(c);
6163 }
6164 else
6165#endif
6166 {
6167 ins_char(c);
6168 if (flags & INSCHAR_CTRLV)
6169 redo_literal(c);
6170 else
6171 AppendCharToRedobuff(c);
6172 }
6173 }
6174}
6175
6176/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006177 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006178 *
6179 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6180 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006181 */
6182 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006183internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006184 int textwidth;
6185 int second_indent;
6186 int flags;
6187 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006188 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006189{
6190 int cc;
6191 int save_char = NUL;
6192 int haveto_redraw = FALSE;
6193 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6194#ifdef FEAT_MBYTE
6195 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6196#endif
6197 int fo_white_par = has_format_option(FO_WHITE_PAR);
6198 int first_line = TRUE;
6199#ifdef FEAT_COMMENTS
6200 colnr_T leader_len;
6201 int no_leader = FALSE;
6202 int do_comments = (flags & INSCHAR_DO_COM);
6203#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006204#ifdef FEAT_LINEBREAK
6205 int has_lbr = curwin->w_p_lbr;
6206
6207 /* make sure win_lbr_chartabsize() counts correctly */
6208 curwin->w_p_lbr = FALSE;
6209#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006210
6211 /*
6212 * When 'ai' is off we don't want a space under the cursor to be
6213 * deleted. Replace it with an 'x' temporarily.
6214 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006215 if (!curbuf->b_p_ai
6216#ifdef FEAT_VREPLACE
6217 && !(State & VREPLACE_FLAG)
6218#endif
6219 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006220 {
6221 cc = gchar_cursor();
6222 if (vim_iswhite(cc))
6223 {
6224 save_char = cc;
6225 pchar_cursor('x');
6226 }
6227 }
6228
6229 /*
6230 * Repeat breaking lines, until the current line is not too long.
6231 */
6232 while (!got_int)
6233 {
6234 int startcol; /* Cursor column at entry */
6235 int wantcol; /* column at textwidth border */
6236 int foundcol; /* column for start of spaces */
6237 int end_foundcol = 0; /* column for start of word */
6238 colnr_T len;
6239 colnr_T virtcol;
6240#ifdef FEAT_VREPLACE
6241 int orig_col = 0;
6242 char_u *saved_text = NULL;
6243#endif
6244 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006245 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006246
Bram Moolenaar97b98102009-11-17 16:41:01 +00006247 virtcol = get_nolist_virtcol()
6248 + char2cells(c != NUL ? c : gchar_cursor());
6249 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006250 break;
6251
6252#ifdef FEAT_COMMENTS
6253 if (no_leader)
6254 do_comments = FALSE;
6255 else if (!(flags & INSCHAR_FORMAT)
6256 && has_format_option(FO_WRAP_COMS))
6257 do_comments = TRUE;
6258
6259 /* Don't break until after the comment leader */
6260 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006261 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006262 else
6263 leader_len = 0;
6264
6265 /* If the line doesn't start with a comment leader, then don't
6266 * start one in a following broken line. Avoids that a %word
6267 * moved to the start of the next line causes all following lines
6268 * to start with %. */
6269 if (leader_len == 0)
6270 no_leader = TRUE;
6271#endif
6272 if (!(flags & INSCHAR_FORMAT)
6273#ifdef FEAT_COMMENTS
6274 && leader_len == 0
6275#endif
6276 && !has_format_option(FO_WRAP))
6277
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006278 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006279 if ((startcol = curwin->w_cursor.col) == 0)
6280 break;
6281
6282 /* find column of textwidth border */
6283 coladvance((colnr_T)textwidth);
6284 wantcol = curwin->w_cursor.col;
6285
Bram Moolenaar97b98102009-11-17 16:41:01 +00006286 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006287 foundcol = 0;
6288
6289 /*
6290 * Find position to break at.
6291 * Stop at first entered white when 'formatoptions' has 'v'
6292 */
6293 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006294 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006295 || curwin->w_cursor.lnum != Insstart.lnum
6296 || curwin->w_cursor.col >= Insstart.col)
6297 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006298 if (curwin->w_cursor.col == startcol && c != NUL)
6299 cc = c;
6300 else
6301 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006302 if (WHITECHAR(cc))
6303 {
6304 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006305 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006306
6307 /* find start of sequence of blanks */
6308 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6309 {
6310 dec_cursor();
6311 cc = gchar_cursor();
6312 }
6313 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6314 break; /* only spaces in front of text */
6315#ifdef FEAT_COMMENTS
6316 /* Don't break until after the comment leader */
6317 if (curwin->w_cursor.col < leader_len)
6318 break;
6319#endif
6320 if (has_format_option(FO_ONE_LETTER))
6321 {
6322 /* do not break after one-letter words */
6323 if (curwin->w_cursor.col == 0)
6324 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006325#ifdef FEAT_COMMENTS
6326 /* do not break "#a b" when 'tw' is 2 */
6327 if (curwin->w_cursor.col <= leader_len)
6328 break;
6329#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006330 col = curwin->w_cursor.col;
6331 dec_cursor();
6332 cc = gchar_cursor();
6333
6334 if (WHITECHAR(cc))
6335 continue; /* one-letter, continue */
6336 curwin->w_cursor.col = col;
6337 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006338
6339 inc_cursor();
6340
6341 end_foundcol = end_col + 1;
6342 foundcol = curwin->w_cursor.col;
6343 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006344 break;
6345 }
6346#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006347 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006348 {
6349 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006350 if (curwin->w_cursor.col != startcol)
6351 {
6352#ifdef FEAT_COMMENTS
6353 /* Don't break until after the comment leader */
6354 if (curwin->w_cursor.col < leader_len)
6355 break;
6356#endif
6357 col = curwin->w_cursor.col;
6358 inc_cursor();
6359 /* Don't change end_foundcol if already set. */
6360 if (foundcol != curwin->w_cursor.col)
6361 {
6362 foundcol = curwin->w_cursor.col;
6363 end_foundcol = foundcol;
6364 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6365 break;
6366 }
6367 curwin->w_cursor.col = col;
6368 }
6369
6370 if (curwin->w_cursor.col == 0)
6371 break;
6372
6373 col = curwin->w_cursor.col;
6374
6375 dec_cursor();
6376 cc = gchar_cursor();
6377
6378 if (WHITECHAR(cc))
6379 continue; /* break with space */
6380#ifdef FEAT_COMMENTS
6381 /* Don't break until after the comment leader */
6382 if (curwin->w_cursor.col < leader_len)
6383 break;
6384#endif
6385
6386 curwin->w_cursor.col = col;
6387
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006388 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006389 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006390 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6391 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006392 }
6393#endif
6394 if (curwin->w_cursor.col == 0)
6395 break;
6396 dec_cursor();
6397 }
6398
6399 if (foundcol == 0) /* no spaces, cannot break line */
6400 {
6401 curwin->w_cursor.col = startcol;
6402 break;
6403 }
6404
6405 /* Going to break the line, remove any "$" now. */
6406 undisplay_dollar();
6407
6408 /*
6409 * Offset between cursor position and line break is used by replace
6410 * stack functions. VREPLACE does not use this, and backspaces
6411 * over the text instead.
6412 */
6413#ifdef FEAT_VREPLACE
6414 if (State & VREPLACE_FLAG)
6415 orig_col = startcol; /* Will start backspacing from here */
6416 else
6417#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006418 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006419
6420 /*
6421 * adjust startcol for spaces that will be deleted and
6422 * characters that will remain on top line
6423 */
6424 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006425 while ((cc = gchar_cursor(), WHITECHAR(cc))
6426 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006427 inc_cursor();
6428 startcol -= curwin->w_cursor.col;
6429 if (startcol < 0)
6430 startcol = 0;
6431
6432#ifdef FEAT_VREPLACE
6433 if (State & VREPLACE_FLAG)
6434 {
6435 /*
6436 * In VREPLACE mode, we will backspace over the text to be
6437 * wrapped, so save a copy now to put on the next line.
6438 */
6439 saved_text = vim_strsave(ml_get_cursor());
6440 curwin->w_cursor.col = orig_col;
6441 if (saved_text == NULL)
6442 break; /* Can't do it, out of memory */
6443 saved_text[startcol] = NUL;
6444
6445 /* Backspace over characters that will move to the next line */
6446 if (!fo_white_par)
6447 backspace_until_column(foundcol);
6448 }
6449 else
6450#endif
6451 {
6452 /* put cursor after pos. to break line */
6453 if (!fo_white_par)
6454 curwin->w_cursor.col = foundcol;
6455 }
6456
6457 /*
6458 * Split the line just before the margin.
6459 * Only insert/delete lines, but don't really redraw the window.
6460 */
6461 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6462 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6463#ifdef FEAT_COMMENTS
6464 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006465 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006466#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006467 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6468 if (!(flags & INSCHAR_COM_LIST))
6469 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006470
6471 replace_offset = 0;
6472 if (first_line)
6473 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006474 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006475 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006476 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006477 * This section is for auto-wrap of numeric lists. When not
6478 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6479 * flag will be set and open_line() will handle it (as seen
6480 * above). The code here (and in get_number_indent()) will
6481 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006482 */
6483 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006484 second_indent =
6485 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006486 if (second_indent >= 0)
6487 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006488#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006489 if (State & VREPLACE_FLAG)
6490 change_indent(INDENT_SET, second_indent,
6491 FALSE, NUL, TRUE);
6492 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006493#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006494#ifdef FEAT_COMMENTS
6495 if (leader_len > 0 && second_indent - leader_len > 0)
6496 {
6497 int i;
6498 int padding = second_indent - leader_len;
6499
6500 /* We started at the first_line of a numbered list
6501 * that has a comment. the open_line() function has
6502 * inserted the proper comment leader and positioned
6503 * the cursor at the end of the split line. Now we
6504 * add the additional whitespace needed after the
6505 * comment leader for the numbered list. */
6506 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006507 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006508 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006509 }
6510 else
6511 {
6512#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006513 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006514#ifdef FEAT_COMMENTS
6515 }
6516#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006517 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006518 }
6519 first_line = FALSE;
6520 }
6521
6522#ifdef FEAT_VREPLACE
6523 if (State & VREPLACE_FLAG)
6524 {
6525 /*
6526 * In VREPLACE mode we have backspaced over the text to be
6527 * moved, now we re-insert it into the new line.
6528 */
6529 ins_bytes(saved_text);
6530 vim_free(saved_text);
6531 }
6532 else
6533#endif
6534 {
6535 /*
6536 * Check if cursor is not past the NUL off the line, cindent
6537 * may have added or removed indent.
6538 */
6539 curwin->w_cursor.col += startcol;
6540 len = (colnr_T)STRLEN(ml_get_curline());
6541 if (curwin->w_cursor.col > len)
6542 curwin->w_cursor.col = len;
6543 }
6544
6545 haveto_redraw = TRUE;
6546#ifdef FEAT_CINDENT
6547 can_cindent = TRUE;
6548#endif
6549 /* moved the cursor, don't autoindent or cindent now */
6550 did_ai = FALSE;
6551#ifdef FEAT_SMARTINDENT
6552 did_si = FALSE;
6553 can_si = FALSE;
6554 can_si_back = FALSE;
6555#endif
6556 line_breakcheck();
6557 }
6558
6559 if (save_char != NUL) /* put back space after cursor */
6560 pchar_cursor(save_char);
6561
Bram Moolenaar0026d472014-09-09 16:32:39 +02006562#ifdef FEAT_LINEBREAK
6563 curwin->w_p_lbr = has_lbr;
6564#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006565 if (!format_only && haveto_redraw)
6566 {
6567 update_topline();
6568 redraw_curbuf_later(VALID);
6569 }
6570}
6571
6572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 * Called after inserting or deleting text: When 'formatoptions' includes the
6574 * 'a' flag format from the current line until the end of the paragraph.
6575 * Keep the cursor at the same position relative to the text.
6576 * The caller must have saved the cursor line for undo, following ones will be
6577 * saved here.
6578 */
6579 void
6580auto_format(trailblank, prev_line)
6581 int trailblank; /* when TRUE also format with trailing blank */
6582 int prev_line; /* may start in previous line */
6583{
6584 pos_T pos;
6585 colnr_T len;
6586 char_u *old;
6587 char_u *new, *pnew;
6588 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006589 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590
6591 if (!has_format_option(FO_AUTO))
6592 return;
6593
6594 pos = curwin->w_cursor;
6595 old = ml_get_curline();
6596
6597 /* may remove added space */
6598 check_auto_format(FALSE);
6599
6600 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6601 * user might insert normal text next. Also skip formatting when "1" is
6602 * in 'formatoptions' and there is a single character before the cursor.
6603 * Otherwise the line would be broken and when typing another non-white
6604 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006605 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 if (*old != NUL && !trailblank && wasatend)
6607 {
6608 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006609 cc = gchar_cursor();
6610 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6611 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006612 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006613 cc = gchar_cursor();
6614 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006615 {
6616 curwin->w_cursor = pos;
6617 return;
6618 }
6619 curwin->w_cursor = pos;
6620 }
6621
6622#ifdef FEAT_COMMENTS
6623 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6624 * comments. */
6625 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006626 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627 return;
6628#endif
6629
6630 /*
6631 * May start formatting in a previous line, so that after "x" a word is
6632 * moved to the previous line if it fits there now. Only when this is not
6633 * the start of a paragraph.
6634 */
6635 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6636 {
6637 --curwin->w_cursor.lnum;
6638 if (u_save_cursor() == FAIL)
6639 return;
6640 }
6641
6642 /*
6643 * Do the formatting and restore the cursor position. "saved_cursor" will
6644 * be adjusted for the text formatting.
6645 */
6646 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006647 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648 curwin->w_cursor = saved_cursor;
6649 saved_cursor.lnum = 0;
6650
6651 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6652 {
6653 /* "cannot happen" */
6654 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6655 coladvance((colnr_T)MAXCOL);
6656 }
6657 else
6658 check_cursor_col();
6659
6660 /* Insert mode: If the cursor is now after the end of the line while it
6661 * previously wasn't, the line was broken. Because of the rule above we
6662 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6663 * formatted. */
6664 if (!wasatend && has_format_option(FO_WHITE_PAR))
6665 {
6666 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006667 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668 if (curwin->w_cursor.col == len)
6669 {
6670 pnew = vim_strnsave(new, len + 2);
6671 pnew[len] = ' ';
6672 pnew[len + 1] = NUL;
6673 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6674 /* remove the space later */
6675 did_add_space = TRUE;
6676 }
6677 else
6678 /* may remove added space */
6679 check_auto_format(FALSE);
6680 }
6681
6682 check_cursor();
6683}
6684
6685/*
6686 * When an extra space was added to continue a paragraph for auto-formatting,
6687 * delete it now. The space must be under the cursor, just after the insert
6688 * position.
6689 */
6690 static void
6691check_auto_format(end_insert)
6692 int end_insert; /* TRUE when ending Insert mode */
6693{
6694 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006695 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696
6697 if (did_add_space)
6698 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006699 cc = gchar_cursor();
6700 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 /* Somehow the space was removed already. */
6702 did_add_space = FALSE;
6703 else
6704 {
6705 if (!end_insert)
6706 {
6707 inc_cursor();
6708 c = gchar_cursor();
6709 dec_cursor();
6710 }
6711 if (c != NUL)
6712 {
6713 /* The space is no longer at the end of the line, delete it. */
6714 del_char(FALSE);
6715 did_add_space = FALSE;
6716 }
6717 }
6718 }
6719}
6720
6721/*
6722 * Find out textwidth to be used for formatting:
6723 * if 'textwidth' option is set, use it
6724 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6725 * if invalid value, use 0.
6726 * Set default to window width (maximum 79) for "gq" operator.
6727 */
6728 int
6729comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006730 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731{
6732 int textwidth;
6733
6734 textwidth = curbuf->b_p_tw;
6735 if (textwidth == 0 && curbuf->b_p_wm)
6736 {
6737 /* The width is the window width minus 'wrapmargin' minus all the
6738 * things that add to the margin. */
6739 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6740#ifdef FEAT_CMDWIN
6741 if (cmdwin_type != 0)
6742 textwidth -= 1;
6743#endif
6744#ifdef FEAT_FOLDING
6745 textwidth -= curwin->w_p_fdc;
6746#endif
6747#ifdef FEAT_SIGNS
6748 if (curwin->w_buffer->b_signlist != NULL
6749# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01006750 || curwin->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751# endif
6752 )
6753 textwidth -= 1;
6754#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006755 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756 textwidth -= 8;
6757 }
6758 if (textwidth < 0)
6759 textwidth = 0;
6760 if (ff && textwidth == 0)
6761 {
6762 textwidth = W_WIDTH(curwin) - 1;
6763 if (textwidth > 79)
6764 textwidth = 79;
6765 }
6766 return textwidth;
6767}
6768
6769/*
6770 * Put a character in the redo buffer, for when just after a CTRL-V.
6771 */
6772 static void
6773redo_literal(c)
6774 int c;
6775{
6776 char_u buf[10];
6777
6778 /* Only digits need special treatment. Translate them into a string of
6779 * three digits. */
6780 if (VIM_ISDIGIT(c))
6781 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006782 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006783 AppendToRedobuff(buf);
6784 }
6785 else
6786 AppendCharToRedobuff(c);
6787}
6788
6789/*
6790 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006791 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006792 */
6793 static void
6794start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006795 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006796{
6797 if (!arrow_used) /* something has been inserted */
6798 {
6799 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006800 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801 arrow_used = TRUE; /* this means we stopped the current insert */
6802 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006803#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006804 check_spell_redraw();
6805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806}
6807
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006808#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006809/*
6810 * If we skipped highlighting word at cursor, do it now.
6811 * It may be skipped again, thus reset spell_redraw_lnum first.
6812 */
6813 static void
6814check_spell_redraw()
6815{
6816 if (spell_redraw_lnum != 0)
6817 {
6818 linenr_T lnum = spell_redraw_lnum;
6819
6820 spell_redraw_lnum = 0;
6821 redrawWinline(lnum, FALSE);
6822 }
6823}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006824
6825/*
6826 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6827 * spelled word, if there is one.
6828 */
6829 static void
6830spell_back_to_badword()
6831{
6832 pos_T tpos = curwin->w_cursor;
6833
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006834 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006835 if (curwin->w_cursor.col != tpos.col)
6836 start_arrow(&tpos);
6837}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006838#endif
6839
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840/*
6841 * stop_arrow() is called before a change is made in insert mode.
6842 * If an arrow key has been used, start a new insertion.
6843 * Returns FAIL if undo is impossible, shouldn't insert then.
6844 */
6845 int
6846stop_arrow()
6847{
6848 if (arrow_used)
6849 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006850 Insstart = curwin->w_cursor; /* new insertion starts here */
6851 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6852 /* Don't update the original insert position when moved to the
6853 * right, except when nothing was inserted yet. */
6854 update_Insstart_orig = FALSE;
6855 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6856
Bram Moolenaar071d4272004-06-13 20:20:40 +00006857 if (u_save_cursor() == OK)
6858 {
6859 arrow_used = FALSE;
6860 ins_need_undo = FALSE;
6861 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006862
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 ai_col = 0;
6864#ifdef FEAT_VREPLACE
6865 if (State & VREPLACE_FLAG)
6866 {
6867 orig_line_count = curbuf->b_ml.ml_line_count;
6868 vr_lines_changed = 1;
6869 }
6870#endif
6871 ResetRedobuff();
6872 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006873 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006874 }
6875 else if (ins_need_undo)
6876 {
6877 if (u_save_cursor() == OK)
6878 ins_need_undo = FALSE;
6879 }
6880
6881#ifdef FEAT_FOLDING
6882 /* Always open fold at the cursor line when inserting something. */
6883 foldOpenCursor();
6884#endif
6885
6886 return (arrow_used || ins_need_undo ? FAIL : OK);
6887}
6888
6889/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006890 * Do a few things to stop inserting.
6891 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6892 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 */
6894 static void
Bram Moolenaarf332a652013-11-04 04:20:33 +01006895stop_insert(end_insert_pos, esc, nomove)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006896 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006897 int esc; /* called by ins_esc() */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006898 int nomove; /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006899{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006900 int cc;
6901 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902
6903 stop_redo_ins();
6904 replace_flush(); /* abandon replace stack */
6905
6906 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006907 * Save the inserted text for later redo with ^@ and CTRL-A.
6908 * Don't do it when "restart_edit" was set and nothing was inserted,
6909 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006910 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006911 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006912 if (did_restart_edit == 0 || (ptr != NULL
6913 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006914 {
6915 vim_free(last_insert);
6916 last_insert = ptr;
6917 last_insert_skip = new_insert_skip;
6918 }
6919 else
6920 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006922 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 {
6924 /* Auto-format now. It may seem strange to do this when stopping an
6925 * insertion (or moving the cursor), but it's required when appending
6926 * a line and having it end in a space. But only do it when something
6927 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006928 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006930 pos_T tpos = curwin->w_cursor;
6931
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 /* When the cursor is at the end of the line after a space the
6933 * formatting will move it to the following word. Avoid that by
6934 * moving the cursor onto the space. */
6935 cc = 'x';
6936 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6937 {
6938 dec_cursor();
6939 cc = gchar_cursor();
6940 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006941 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 }
6943
6944 auto_format(TRUE, FALSE);
6945
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006946 if (vim_iswhite(cc))
6947 {
6948 if (gchar_cursor() != NUL)
6949 inc_cursor();
6950#ifdef FEAT_VIRTUALEDIT
6951 /* If the cursor is still at the same character, also keep
6952 * the "coladd". */
6953 if (gchar_cursor() == NUL
6954 && curwin->w_cursor.lnum == tpos.lnum
6955 && curwin->w_cursor.col == tpos.col)
6956 curwin->w_cursor.coladd = tpos.coladd;
6957#endif
6958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 }
6960
6961 /* If a space was inserted for auto-formatting, remove it now. */
6962 check_auto_format(TRUE);
6963
6964 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006965 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006966 * Do this when ESC was used or moving the cursor up/down.
6967 * Check for the old position still being valid, just in case the text
6968 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006969 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006970 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6971 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006973 pos_T tpos = curwin->w_cursor;
6974
6975 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006976 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006977 for (;;)
6978 {
6979 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6980 --curwin->w_cursor.col;
6981 cc = gchar_cursor();
6982 if (!vim_iswhite(cc))
6983 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006984 if (del_char(TRUE) == FAIL)
6985 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006986 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006987 if (curwin->w_cursor.lnum != tpos.lnum)
6988 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01006989 else
6990 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01006991 /* reset tpos, could have been invalidated in the loop above */
6992 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01006993 tpos.col++;
6994 if (cc != NUL && gchar_pos(&tpos) == NUL)
6995 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6996 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006997
Bram Moolenaar071d4272004-06-13 20:20:40 +00006998 /* <C-S-Right> may have started Visual mode, adjust the position for
6999 * deleted characters. */
7000 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7001 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007002 int len = (int)STRLEN(ml_get_curline());
7003
7004 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007005 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007006 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007007#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007008 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007010 }
7011 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007012 }
7013 }
7014 did_ai = FALSE;
7015#ifdef FEAT_SMARTINDENT
7016 did_si = FALSE;
7017 can_si = FALSE;
7018 can_si_back = FALSE;
7019#endif
7020
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007021 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7022 * now in a different buffer. */
7023 if (end_insert_pos != NULL)
7024 {
7025 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007026 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007027 curbuf->b_op_end = *end_insert_pos;
7028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029}
7030
7031/*
7032 * Set the last inserted text to a single character.
7033 * Used for the replace command.
7034 */
7035 void
7036set_last_insert(c)
7037 int c;
7038{
7039 char_u *s;
7040
7041 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007043 if (last_insert != NULL)
7044 {
7045 s = last_insert;
7046 /* Use the CTRL-V only when entering a special char */
7047 if (c < ' ' || c == DEL)
7048 *s++ = Ctrl_V;
7049 s = add_char2buf(c, s);
7050 *s++ = ESC;
7051 *s++ = NUL;
7052 last_insert_skip = 0;
7053 }
7054}
7055
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007056#if defined(EXITFREE) || defined(PROTO)
7057 void
7058free_last_insert()
7059{
7060 vim_free(last_insert);
7061 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007062# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007063 vim_free(compl_orig_text);
7064 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007065# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007066}
7067#endif
7068
Bram Moolenaar071d4272004-06-13 20:20:40 +00007069/*
7070 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7071 * and CSI. Handle multi-byte characters.
7072 * Returns a pointer to after the added bytes.
7073 */
7074 char_u *
7075add_char2buf(c, s)
7076 int c;
7077 char_u *s;
7078{
7079#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007080 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007081 int i;
7082 int len;
7083
7084 len = (*mb_char2bytes)(c, temp);
7085 for (i = 0; i < len; ++i)
7086 {
7087 c = temp[i];
7088#endif
7089 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7090 if (c == K_SPECIAL)
7091 {
7092 *s++ = K_SPECIAL;
7093 *s++ = KS_SPECIAL;
7094 *s++ = KE_FILLER;
7095 }
7096#ifdef FEAT_GUI
7097 else if (c == CSI)
7098 {
7099 *s++ = CSI;
7100 *s++ = KS_EXTRA;
7101 *s++ = (int)KE_CSI;
7102 }
7103#endif
7104 else
7105 *s++ = c;
7106#ifdef FEAT_MBYTE
7107 }
7108#endif
7109 return s;
7110}
7111
7112/*
7113 * move cursor to start of line
7114 * if flags & BL_WHITE move to first non-white
7115 * if flags & BL_SOL move to first non-white if startofline is set,
7116 * otherwise keep "curswant" column
7117 * if flags & BL_FIX don't leave the cursor on a NUL.
7118 */
7119 void
7120beginline(flags)
7121 int flags;
7122{
7123 if ((flags & BL_SOL) && !p_sol)
7124 coladvance(curwin->w_curswant);
7125 else
7126 {
7127 curwin->w_cursor.col = 0;
7128#ifdef FEAT_VIRTUALEDIT
7129 curwin->w_cursor.coladd = 0;
7130#endif
7131
7132 if (flags & (BL_WHITE | BL_SOL))
7133 {
7134 char_u *ptr;
7135
7136 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7137 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7138 ++curwin->w_cursor.col;
7139 }
7140 curwin->w_set_curswant = TRUE;
7141 }
7142}
7143
7144/*
7145 * oneright oneleft cursor_down cursor_up
7146 *
7147 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007148 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007149 * Return OK when successful, FAIL when we hit a line of file boundary.
7150 */
7151
7152 int
7153oneright()
7154{
7155 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007157
7158#ifdef FEAT_VIRTUALEDIT
7159 if (virtual_active())
7160 {
7161 pos_T prevpos = curwin->w_cursor;
7162
7163 /* Adjust for multi-wide char (excluding TAB) */
7164 ptr = ml_get_cursor();
7165 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007166# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007167 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007168# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007169 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007170# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007171 ))
7172 ? ptr2cells(ptr) : 1));
7173 curwin->w_set_curswant = TRUE;
7174 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7175 return (prevpos.col != curwin->w_cursor.col
7176 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7177 }
7178#endif
7179
7180 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007181 if (*ptr == NUL)
7182 return FAIL; /* already at the very end */
7183
Bram Moolenaar071d4272004-06-13 20:20:40 +00007184#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007185 if (has_mbyte)
7186 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187 else
7188#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007189 l = 1;
7190
7191 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7192 * contains "onemore". */
7193 if (ptr[l] == NUL
7194#ifdef FEAT_VIRTUALEDIT
7195 && (ve_flags & VE_ONEMORE) == 0
7196#endif
7197 )
7198 return FAIL;
7199 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007200
7201 curwin->w_set_curswant = TRUE;
7202 return OK;
7203}
7204
7205 int
7206oneleft()
7207{
7208#ifdef FEAT_VIRTUALEDIT
7209 if (virtual_active())
7210 {
7211 int width;
7212 int v = getviscol();
7213
7214 if (v == 0)
7215 return FAIL;
7216
7217# ifdef FEAT_LINEBREAK
7218 /* We might get stuck on 'showbreak', skip over it. */
7219 width = 1;
7220 for (;;)
7221 {
7222 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007223 /* getviscol() is slow, skip it when 'showbreak' is empty,
7224 * 'breakindent' is not set and there are no multi-byte
7225 * characters */
7226 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227# ifdef FEAT_MBYTE
7228 && !has_mbyte
7229# endif
7230 ) || getviscol() < v)
7231 break;
7232 ++width;
7233 }
7234# else
7235 coladvance(v - 1);
7236# endif
7237
7238 if (curwin->w_cursor.coladd == 1)
7239 {
7240 char_u *ptr;
7241
7242 /* Adjust for multi-wide char (not a TAB) */
7243 ptr = ml_get_cursor();
7244 if (*ptr != TAB && vim_isprintc(
7245# ifdef FEAT_MBYTE
7246 (*mb_ptr2char)(ptr)
7247# else
7248 *ptr
7249# endif
7250 ) && ptr2cells(ptr) > 1)
7251 curwin->w_cursor.coladd = 0;
7252 }
7253
7254 curwin->w_set_curswant = TRUE;
7255 return OK;
7256 }
7257#endif
7258
7259 if (curwin->w_cursor.col == 0)
7260 return FAIL;
7261
7262 curwin->w_set_curswant = TRUE;
7263 --curwin->w_cursor.col;
7264
7265#ifdef FEAT_MBYTE
7266 /* if the character on the left of the current cursor is a multi-byte
7267 * character, move to its first byte */
7268 if (has_mbyte)
7269 mb_adjust_cursor();
7270#endif
7271 return OK;
7272}
7273
7274 int
7275cursor_up(n, upd_topline)
7276 long n;
7277 int upd_topline; /* When TRUE: update topline */
7278{
7279 linenr_T lnum;
7280
7281 if (n > 0)
7282 {
7283 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007284 /* This fails if the cursor is already in the first line or the count
7285 * is larger than the line number and '-' is in 'cpoptions' */
7286 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 return FAIL;
7288 if (n >= lnum)
7289 lnum = 1;
7290 else
7291#ifdef FEAT_FOLDING
7292 if (hasAnyFolding(curwin))
7293 {
7294 /*
7295 * Count each sequence of folded lines as one logical line.
7296 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007297 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007298 (void)hasFolding(lnum, &lnum, NULL);
7299
7300 while (n--)
7301 {
7302 /* move up one line */
7303 --lnum;
7304 if (lnum <= 1)
7305 break;
7306 /* If we entered a fold, move to the beginning, unless in
7307 * Insert mode or when 'foldopen' contains "all": it will open
7308 * in a moment. */
7309 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7310 (void)hasFolding(lnum, &lnum, NULL);
7311 }
7312 if (lnum < 1)
7313 lnum = 1;
7314 }
7315 else
7316#endif
7317 lnum -= n;
7318 curwin->w_cursor.lnum = lnum;
7319 }
7320
7321 /* try to advance to the column we want to be at */
7322 coladvance(curwin->w_curswant);
7323
7324 if (upd_topline)
7325 update_topline(); /* make sure curwin->w_topline is valid */
7326
7327 return OK;
7328}
7329
7330/*
7331 * Cursor down a number of logical lines.
7332 */
7333 int
7334cursor_down(n, upd_topline)
7335 long n;
7336 int upd_topline; /* When TRUE: update topline */
7337{
7338 linenr_T lnum;
7339
7340 if (n > 0)
7341 {
7342 lnum = curwin->w_cursor.lnum;
7343#ifdef FEAT_FOLDING
7344 /* Move to last line of fold, will fail if it's the end-of-file. */
7345 (void)hasFolding(lnum, NULL, &lnum);
7346#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007347 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007348 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007349 if (lnum >= curbuf->b_ml.ml_line_count
7350 || (lnum + n > curbuf->b_ml.ml_line_count
7351 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352 return FAIL;
7353 if (lnum + n >= curbuf->b_ml.ml_line_count)
7354 lnum = curbuf->b_ml.ml_line_count;
7355 else
7356#ifdef FEAT_FOLDING
7357 if (hasAnyFolding(curwin))
7358 {
7359 linenr_T last;
7360
7361 /* count each sequence of folded lines as one logical line */
7362 while (n--)
7363 {
7364 if (hasFolding(lnum, NULL, &last))
7365 lnum = last + 1;
7366 else
7367 ++lnum;
7368 if (lnum >= curbuf->b_ml.ml_line_count)
7369 break;
7370 }
7371 if (lnum > curbuf->b_ml.ml_line_count)
7372 lnum = curbuf->b_ml.ml_line_count;
7373 }
7374 else
7375#endif
7376 lnum += n;
7377 curwin->w_cursor.lnum = lnum;
7378 }
7379
7380 /* try to advance to the column we want to be at */
7381 coladvance(curwin->w_curswant);
7382
7383 if (upd_topline)
7384 update_topline(); /* make sure curwin->w_topline is valid */
7385
7386 return OK;
7387}
7388
7389/*
7390 * Stuff the last inserted text in the read buffer.
7391 * Last_insert actually is a copy of the redo buffer, so we
7392 * first have to remove the command.
7393 */
7394 int
7395stuff_inserted(c, count, no_esc)
7396 int c; /* Command character to be inserted */
7397 long count; /* Repeat this many times */
7398 int no_esc; /* Don't add an ESC at the end */
7399{
7400 char_u *esc_ptr;
7401 char_u *ptr;
7402 char_u *last_ptr;
7403 char_u last = NUL;
7404
7405 ptr = get_last_insert();
7406 if (ptr == NULL)
7407 {
7408 EMSG(_(e_noinstext));
7409 return FAIL;
7410 }
7411
7412 /* may want to stuff the command character, to start Insert mode */
7413 if (c != NUL)
7414 stuffcharReadbuff(c);
7415 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7416 *esc_ptr = NUL; /* remove the ESC */
7417
7418 /* when the last char is either "0" or "^" it will be quoted if no ESC
7419 * comes after it OR if it will inserted more than once and "ptr"
7420 * starts with ^D. -- Acevedo
7421 */
7422 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7423 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7424 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7425 {
7426 last = *last_ptr;
7427 *last_ptr = NUL;
7428 }
7429
7430 do
7431 {
7432 stuffReadbuff(ptr);
7433 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7434 if (last)
7435 stuffReadbuff((char_u *)(last == '0'
7436 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7437 : IF_EB("\026^", CTRL_V_STR "^")));
7438 }
7439 while (--count > 0);
7440
7441 if (last)
7442 *last_ptr = last;
7443
7444 if (esc_ptr != NULL)
7445 *esc_ptr = ESC; /* put the ESC back */
7446
7447 /* may want to stuff a trailing ESC, to get out of Insert mode */
7448 if (!no_esc)
7449 stuffcharReadbuff(ESC);
7450
7451 return OK;
7452}
7453
7454 char_u *
7455get_last_insert()
7456{
7457 if (last_insert == NULL)
7458 return NULL;
7459 return last_insert + last_insert_skip;
7460}
7461
7462/*
7463 * Get last inserted string, and remove trailing <Esc>.
7464 * Returns pointer to allocated memory (must be freed) or NULL.
7465 */
7466 char_u *
7467get_last_insert_save()
7468{
7469 char_u *s;
7470 int len;
7471
7472 if (last_insert == NULL)
7473 return NULL;
7474 s = vim_strsave(last_insert + last_insert_skip);
7475 if (s != NULL)
7476 {
7477 len = (int)STRLEN(s);
7478 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7479 s[len - 1] = NUL;
7480 }
7481 return s;
7482}
7483
7484/*
7485 * Check the word in front of the cursor for an abbreviation.
7486 * Called when the non-id character "c" has been entered.
7487 * When an abbreviation is recognized it is removed from the text and
7488 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7489 */
7490 static int
7491echeck_abbr(c)
7492 int c;
7493{
7494 /* Don't check for abbreviation in paste mode, when disabled and just
7495 * after moving around with cursor keys. */
7496 if (p_paste || no_abbr || arrow_used)
7497 return FALSE;
7498
7499 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7500 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7501}
7502
7503/*
7504 * replace-stack functions
7505 *
7506 * When replacing characters, the replaced characters are remembered for each
7507 * new character. This is used to re-insert the old text when backspacing.
7508 *
7509 * There is a NUL headed list of characters for each character that is
7510 * currently in the file after the insertion point. When BS is used, one NUL
7511 * headed list is put back for the deleted character.
7512 *
7513 * For a newline, there are two NUL headed lists. One contains the characters
7514 * that the NL replaced. The extra one stores the characters after the cursor
7515 * that were deleted (always white space).
7516 *
7517 * Replace_offset is normally 0, in which case replace_push will add a new
7518 * character at the end of the stack. If replace_offset is not 0, that many
7519 * characters will be left on the stack above the newly inserted character.
7520 */
7521
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007522static char_u *replace_stack = NULL;
7523static long replace_stack_nr = 0; /* next entry in replace stack */
7524static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007525
7526 void
7527replace_push(c)
7528 int c; /* character that is replaced (NUL is none) */
7529{
7530 char_u *p;
7531
7532 if (replace_stack_nr < replace_offset) /* nothing to do */
7533 return;
7534 if (replace_stack_len <= replace_stack_nr)
7535 {
7536 replace_stack_len += 50;
7537 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7538 if (p == NULL) /* out of memory */
7539 {
7540 replace_stack_len -= 50;
7541 return;
7542 }
7543 if (replace_stack != NULL)
7544 {
7545 mch_memmove(p, replace_stack,
7546 (size_t)(replace_stack_nr * sizeof(char_u)));
7547 vim_free(replace_stack);
7548 }
7549 replace_stack = p;
7550 }
7551 p = replace_stack + replace_stack_nr - replace_offset;
7552 if (replace_offset)
7553 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7554 *p = c;
7555 ++replace_stack_nr;
7556}
7557
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007558#if defined(FEAT_MBYTE) || defined(PROTO)
7559/*
7560 * Push a character onto the replace stack. Handles a multi-byte character in
7561 * reverse byte order, so that the first byte is popped off first.
7562 * Return the number of bytes done (includes composing characters).
7563 */
7564 int
7565replace_push_mb(p)
7566 char_u *p;
7567{
7568 int l = (*mb_ptr2len)(p);
7569 int j;
7570
7571 for (j = l - 1; j >= 0; --j)
7572 replace_push(p[j]);
7573 return l;
7574}
7575#endif
7576
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577/*
7578 * Pop one item from the replace stack.
7579 * return -1 if stack empty
7580 * return replaced character or NUL otherwise
7581 */
7582 static int
7583replace_pop()
7584{
7585 if (replace_stack_nr == 0)
7586 return -1;
7587 return (int)replace_stack[--replace_stack_nr];
7588}
7589
7590/*
7591 * Join the top two items on the replace stack. This removes to "off"'th NUL
7592 * encountered.
7593 */
7594 static void
7595replace_join(off)
7596 int off; /* offset for which NUL to remove */
7597{
7598 int i;
7599
7600 for (i = replace_stack_nr; --i >= 0; )
7601 if (replace_stack[i] == NUL && off-- <= 0)
7602 {
7603 --replace_stack_nr;
7604 mch_memmove(replace_stack + i, replace_stack + i + 1,
7605 (size_t)(replace_stack_nr - i));
7606 return;
7607 }
7608}
7609
7610/*
7611 * Pop bytes from the replace stack until a NUL is found, and insert them
7612 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7613 */
7614 static void
7615replace_pop_ins()
7616{
7617 int cc;
7618 int oldState = State;
7619
7620 State = NORMAL; /* don't want REPLACE here */
7621 while ((cc = replace_pop()) > 0)
7622 {
7623#ifdef FEAT_MBYTE
7624 mb_replace_pop_ins(cc);
7625#else
7626 ins_char(cc);
7627#endif
7628 dec_cursor();
7629 }
7630 State = oldState;
7631}
7632
7633#ifdef FEAT_MBYTE
7634/*
7635 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7636 * indicates a multi-byte char, pop the other bytes too.
7637 */
7638 static void
7639mb_replace_pop_ins(cc)
7640 int cc;
7641{
7642 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007643 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007644 int i;
7645 int c;
7646
7647 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7648 {
7649 buf[0] = cc;
7650 for (i = 1; i < n; ++i)
7651 buf[i] = replace_pop();
7652 ins_bytes_len(buf, n);
7653 }
7654 else
7655 ins_char(cc);
7656
7657 if (enc_utf8)
7658 /* Handle composing chars. */
7659 for (;;)
7660 {
7661 c = replace_pop();
7662 if (c == -1) /* stack empty */
7663 break;
7664 if ((n = MB_BYTE2LEN(c)) == 1)
7665 {
7666 /* Not a multi-byte char, put it back. */
7667 replace_push(c);
7668 break;
7669 }
7670 else
7671 {
7672 buf[0] = c;
7673 for (i = 1; i < n; ++i)
7674 buf[i] = replace_pop();
7675 if (utf_iscomposing(utf_ptr2char(buf)))
7676 ins_bytes_len(buf, n);
7677 else
7678 {
7679 /* Not a composing char, put it back. */
7680 for (i = n - 1; i >= 0; --i)
7681 replace_push(buf[i]);
7682 break;
7683 }
7684 }
7685 }
7686}
7687#endif
7688
7689/*
7690 * make the replace stack empty
7691 * (called when exiting replace mode)
7692 */
7693 static void
7694replace_flush()
7695{
7696 vim_free(replace_stack);
7697 replace_stack = NULL;
7698 replace_stack_len = 0;
7699 replace_stack_nr = 0;
7700}
7701
7702/*
7703 * Handle doing a BS for one character.
7704 * cc < 0: replace stack empty, just move cursor
7705 * cc == 0: character was inserted, delete it
7706 * cc > 0: character was replaced, put cc (first byte of original char) back
7707 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007708 * When "limit_col" is >= 0, don't delete before this column. Matters when
7709 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007710 */
7711 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007712replace_do_bs(limit_col)
7713 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007714{
7715 int cc;
7716#ifdef FEAT_VREPLACE
7717 int orig_len = 0;
7718 int ins_len;
7719 int orig_vcols = 0;
7720 colnr_T start_vcol;
7721 char_u *p;
7722 int i;
7723 int vcol;
7724#endif
7725
7726 cc = replace_pop();
7727 if (cc > 0)
7728 {
7729#ifdef FEAT_VREPLACE
7730 if (State & VREPLACE_FLAG)
7731 {
7732 /* Get the number of screen cells used by the character we are
7733 * going to delete. */
7734 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7735 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7736 }
7737#endif
7738#ifdef FEAT_MBYTE
7739 if (has_mbyte)
7740 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007741 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742# ifdef FEAT_VREPLACE
7743 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007744 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745# endif
7746 replace_push(cc);
7747 }
7748 else
7749#endif
7750 {
7751 pchar_cursor(cc);
7752#ifdef FEAT_VREPLACE
7753 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007754 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755#endif
7756 }
7757 replace_pop_ins();
7758
7759#ifdef FEAT_VREPLACE
7760 if (State & VREPLACE_FLAG)
7761 {
7762 /* Get the number of screen cells used by the inserted characters */
7763 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007764 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007765 vcol = start_vcol;
7766 for (i = 0; i < ins_len; ++i)
7767 {
7768 vcol += chartabsize(p + i, vcol);
7769#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007770 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007771#endif
7772 }
7773 vcol -= start_vcol;
7774
7775 /* Delete spaces that were inserted after the cursor to keep the
7776 * text aligned. */
7777 curwin->w_cursor.col += ins_len;
7778 while (vcol > orig_vcols && gchar_cursor() == ' ')
7779 {
7780 del_char(FALSE);
7781 ++orig_vcols;
7782 }
7783 curwin->w_cursor.col -= ins_len;
7784 }
7785#endif
7786
7787 /* mark the buffer as changed and prepare for displaying */
7788 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7789 }
7790 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007791 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792}
7793
7794#ifdef FEAT_CINDENT
7795/*
7796 * Return TRUE if C-indenting is on.
7797 */
7798 static int
7799cindent_on()
7800{
7801 return (!p_paste && (curbuf->b_p_cin
7802# ifdef FEAT_EVAL
7803 || *curbuf->b_p_inde != NUL
7804# endif
7805 ));
7806}
7807#endif
7808
7809#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7810/*
7811 * Re-indent the current line, based on the current contents of it and the
7812 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7813 * confused what all the part that handles Control-T is doing that I'm not.
7814 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7815 */
7816
7817 void
7818fixthisline(get_the_indent)
7819 int (*get_the_indent) __ARGS((void));
7820{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007821 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822 if (linewhite(curwin->w_cursor.lnum))
7823 did_ai = TRUE; /* delete the indent if the line stays empty */
7824}
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 {
8271 vim_beep();
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 {
8289 vim_beep();
8290 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. */
8363 default: vim_beep();
8364 }
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 Moolenaar071d4272004-06-13 20:20:40 +00008789 vim_beep();
8790 else
8791 curwin->w_cursor.col = temp;
8792 }
8793 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8794 vim_beep();
8795 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 {
8869 vim_beep();
8870 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
9481 vim_beep();
9482}
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
9541 vim_beep();
9542}
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
9591 vim_beep();
9592}
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
9610 vim_beep();
9611}
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
9641 vim_beep();
9642}
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
9673 vim_beep();
9674}
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
9704 vim_beep();
9705}
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
9736 vim_beep();
9737}
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 {
10154 vim_beep();
10155 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)
10177 vim_beep();
10178 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