blob: db4a079e40b632c4a541e79c2821213f179bddb2 [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 Moolenaar8b5f65a2015-09-01 19:26:12 +0200205static void start_arrow_with_change __ARGS((pos_T *end_insert_pos, int change));
206static void start_arrow_common __ARGS((pos_T *end_insert_pos, int change));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000207#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000208static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000209static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000210static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000211#endif
Bram Moolenaarf332a652013-11-04 04:20:33 +0100212static void stop_insert __ARGS((pos_T *end_insert_pos, int esc, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213static int echeck_abbr __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214static int replace_pop __ARGS((void));
215static void replace_join __ARGS((int off));
216static void replace_pop_ins __ARGS((void));
217#ifdef FEAT_MBYTE
218static void mb_replace_pop_ins __ARGS((int cc));
219#endif
220static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000221static void replace_do_bs __ARGS((int limit_col));
222static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223#ifdef FEAT_CINDENT
224static int cindent_on __ARGS((void));
225#endif
226static void ins_reg __ARGS((void));
227static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000228static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000229static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000230#ifdef FEAT_RIGHTLEFT
231static void ins_ctrl_ __ARGS((void));
232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233static int ins_start_select __ARGS((int c));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000234static void ins_insert __ARGS((int replaceState));
235static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236static void ins_shift __ARGS((int c, int lastc));
237static void ins_del __ARGS((void));
238static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
239#ifdef FEAT_MOUSE
240static void ins_mouse __ARGS((int c));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200241static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000243#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
244static void ins_tabline __ARGS((int c));
245#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200246static void ins_left __ARGS((int end_change));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000247static void ins_home __ARGS((int c));
248static void ins_end __ARGS((int c));
249static void ins_s_left __ARGS((void));
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200250static void ins_right __ARGS((int end_change));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251static void ins_s_right __ARGS((void));
252static void ins_up __ARGS((int startcol));
253static void ins_pageup __ARGS((void));
254static void ins_down __ARGS((int startcol));
255static void ins_pagedown __ARGS((void));
256#ifdef FEAT_DND
257static void ins_drop __ARGS((void));
258#endif
259static int ins_tab __ARGS((void));
260static int ins_eol __ARGS((int c));
261#ifdef FEAT_DIGRAPHS
262static int ins_digraph __ARGS((void));
263#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000264static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265#ifdef FEAT_SMARTINDENT
266static void ins_try_si __ARGS((int c));
267#endif
268static colnr_T get_nolist_virtcol __ARGS((void));
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100269#ifdef FEAT_AUTOCMD
270static char_u *do_insert_char_pre __ARGS((int c));
271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
273static colnr_T Insstart_textlen; /* length of line when insert started */
274static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100275static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000276
277static char_u *last_insert = NULL; /* the text of the previous insert,
278 K_SPECIAL and CSI are escaped */
279static int last_insert_skip; /* nr of chars in front of previous insert */
280static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000281static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000282
283#ifdef FEAT_CINDENT
284static int can_cindent; /* may do cindenting on this line */
285#endif
286
287static int old_indent = 0; /* for ^^D command in insert mode */
288
289#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000290static int revins_on; /* reverse insert mode on */
291static int revins_chars; /* how much to skip after edit */
292static int revins_legal; /* was the last char 'legal'? */
293static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000294#endif
295
Bram Moolenaar071d4272004-06-13 20:20:40 +0000296static int ins_need_undo; /* call u_save() before inserting a
297 char. Set when edit() is called.
298 after that arrow_used is used. */
299
300static int did_add_space = FALSE; /* auto_format() added an extra space
301 under the cursor */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200302static int dont_sync_undo = FALSE; /* CTRL-G U prevents syncing undo for
303 the next left/right cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000304
305/*
306 * edit(): Start inserting text.
307 *
308 * "cmdchar" can be:
309 * 'i' normal insert command
310 * 'a' normal append command
311 * 'R' replace command
312 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
313 * but still only one <CR> is inserted. The <Esc> is not used for redo.
314 * 'g' "gI" command.
315 * 'V' "gR" command for Virtual Replace mode.
316 * 'v' "gr" command for single character Virtual Replace mode.
317 *
318 * This function is not called recursively. For CTRL-O commands, it returns
319 * and lets the caller handle the Normal-mode command.
320 *
321 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
322 */
323 int
324edit(cmdchar, startln, count)
325 int cmdchar;
326 int startln; /* if set, insert at start of line */
327 long count;
328{
329 int c = 0;
330 char_u *ptr;
331 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000332 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000333 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334 int i;
335 int did_backspace = TRUE; /* previous char was backspace */
336#ifdef FEAT_CINDENT
337 int line_is_white = FALSE; /* line is empty before insert */
338#endif
339 linenr_T old_topline = 0; /* topline before insertion */
340#ifdef FEAT_DIFF
341 int old_topfill = -1;
342#endif
343 int inserted_space = FALSE; /* just inserted a space */
344 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000345 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000347 /* Remember whether editing was restarted after CTRL-O. */
348 did_restart_edit = restart_edit;
349
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350 /* sleep before redrawing, needed for "CTRL-O :" that results in an
351 * error message */
352 check_for_delay(TRUE);
353
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100354 /* set Insstart_orig to Insstart */
355 update_Insstart_orig = TRUE;
356
Bram Moolenaar071d4272004-06-13 20:20:40 +0000357#ifdef HAVE_SANDBOX
358 /* Don't allow inserting in the sandbox. */
359 if (sandbox != 0)
360 {
361 EMSG(_(e_sandbox));
362 return FALSE;
363 }
364#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000365 /* Don't allow changes in the buffer while editing the cmdline. The
366 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000367 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000368 {
369 EMSG(_(e_secure));
370 return FALSE;
371 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372
373#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000374 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000375 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000376 {
377 EMSG(_(e_secure));
378 return FALSE;
379 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380 ins_compl_clear(); /* clear stuff for CTRL-X mode */
381#endif
382
Bram Moolenaar843ee412004-06-30 16:16:41 +0000383#ifdef FEAT_AUTOCMD
384 /*
385 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
386 */
387 if (cmdchar != 'r' && cmdchar != 'v')
388 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100389 pos_T save_cursor = curwin->w_cursor;
390
Bram Moolenaar1e015462005-09-25 22:16:38 +0000391# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000392 if (cmdchar == 'R')
393 ptr = (char_u *)"r";
394 else if (cmdchar == 'V')
395 ptr = (char_u *)"v";
396 else
397 ptr = (char_u *)"i";
398 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200399 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000400# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000401 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100402
Bram Moolenaar097c9922013-05-19 21:15:15 +0200403 /* Make sure the cursor didn't move. Do call check_cursor_col() in
404 * case the text was modified. Since Insert mode was not started yet
405 * a call to check_cursor_col() may move the cursor, especially with
406 * the "A" command, thus set State to avoid that. Also check that the
407 * line number is still valid (lines may have been deleted).
408 * Do not restore if v:char was set to a non-empty string. */
409 if (!equalpos(curwin->w_cursor, save_cursor)
410# ifdef FEAT_EVAL
411 && *get_vim_var_str(VV_CHAR) == NUL
412# endif
413 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100414 {
415 int save_state = State;
416
417 curwin->w_cursor = save_cursor;
418 State = INSERT;
419 check_cursor_col();
420 State = save_state;
421 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000422 }
423#endif
424
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200425#ifdef FEAT_CONCEAL
426 /* Check if the cursor line needs redrawing before changing State. If
427 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200428 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200429#endif
430
Bram Moolenaar071d4272004-06-13 20:20:40 +0000431#ifdef FEAT_MOUSE
432 /*
433 * When doing a paste with the middle mouse button, Insstart is set to
434 * where the paste started.
435 */
436 if (where_paste_started.lnum != 0)
437 Insstart = where_paste_started;
438 else
439#endif
440 {
441 Insstart = curwin->w_cursor;
442 if (startln)
443 Insstart.col = 0;
444 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000445 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000446 Insstart_blank_vcol = MAXCOL;
447 if (!did_ai)
448 ai_col = 0;
449
450 if (cmdchar != NUL && restart_edit == 0)
451 {
452 ResetRedobuff();
453 AppendNumberToRedobuff(count);
454#ifdef FEAT_VREPLACE
455 if (cmdchar == 'V' || cmdchar == 'v')
456 {
457 /* "gR" or "gr" command */
458 AppendCharToRedobuff('g');
459 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
460 }
461 else
462#endif
463 {
464 AppendCharToRedobuff(cmdchar);
465 if (cmdchar == 'g') /* "gI" command */
466 AppendCharToRedobuff('I');
467 else if (cmdchar == 'r') /* "r<CR>" command */
468 count = 1; /* insert only one <CR> */
469 }
470 }
471
472 if (cmdchar == 'R')
473 {
474#ifdef FEAT_FKMAP
475 if (p_fkmap && p_ri)
476 {
477 beep_flush();
478 EMSG(farsi_text_3); /* encoded in Farsi */
479 State = INSERT;
480 }
481 else
482#endif
483 State = REPLACE;
484 }
485#ifdef FEAT_VREPLACE
486 else if (cmdchar == 'V' || cmdchar == 'v')
487 {
488 State = VREPLACE;
489 replaceState = VREPLACE;
490 orig_line_count = curbuf->b_ml.ml_line_count;
491 vr_lines_changed = 1;
492 }
493#endif
494 else
495 State = INSERT;
496
497 stop_insert_mode = FALSE;
498
499 /*
500 * Need to recompute the cursor position, it might move when the cursor is
501 * on a TAB or special character.
502 */
503 curs_columns(TRUE);
504
505 /*
506 * Enable langmap or IME, indicated by 'iminsert'.
507 * Note that IME may enabled/disabled without us noticing here, thus the
508 * 'iminsert' value may not reflect what is actually used. It is updated
509 * when hitting <Esc>.
510 */
511 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
512 State |= LANGMAP;
513#ifdef USE_IM_CONTROL
514 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
515#endif
516
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517#ifdef FEAT_MOUSE
518 setmouse();
519#endif
520#ifdef FEAT_CMDL_INFO
521 clear_showcmd();
522#endif
523#ifdef FEAT_RIGHTLEFT
524 /* there is no reverse replace mode */
525 revins_on = (State == INSERT && p_ri);
526 if (revins_on)
527 undisplay_dollar();
528 revins_chars = 0;
529 revins_legal = 0;
530 revins_scol = -1;
531#endif
532
533 /*
534 * Handle restarting Insert mode.
535 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
536 * restart_edit non-zero, and something in the stuff buffer.
537 */
538 if (restart_edit != 0 && stuff_empty())
539 {
540#ifdef FEAT_MOUSE
541 /*
542 * After a paste we consider text typed to be part of the insert for
543 * the pasted text. You can backspace over the pasted text too.
544 */
545 if (where_paste_started.lnum)
546 arrow_used = FALSE;
547 else
548#endif
549 arrow_used = TRUE;
550 restart_edit = 0;
551
552 /*
553 * If the cursor was after the end-of-line before the CTRL-O and it is
554 * now at the end-of-line, put it after the end-of-line (this is not
555 * correct in very rare cases).
556 * Also do this if curswant is greater than the current virtual
557 * column. Eg after "^O$" or "^O80|".
558 */
559 validate_virtcol();
560 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000561 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 || curwin->w_curswant > curwin->w_virtcol)
563 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
564 {
565 if (ptr[1] == NUL)
566 ++curwin->w_cursor.col;
567#ifdef FEAT_MBYTE
568 else if (has_mbyte)
569 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000570 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 if (ptr[i] == NUL)
572 curwin->w_cursor.col += i;
573 }
574#endif
575 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000576 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000577 }
578 else
579 arrow_used = FALSE;
580
581 /* we are in insert mode now, don't need to start it anymore */
582 need_start_insertmode = FALSE;
583
584 /* Need to save the line for undo before inserting the first char. */
585 ins_need_undo = TRUE;
586
587#ifdef FEAT_MOUSE
588 where_paste_started.lnum = 0;
589#endif
590#ifdef FEAT_CINDENT
591 can_cindent = TRUE;
592#endif
593#ifdef FEAT_FOLDING
594 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
595 * restarting. */
596 if (!p_im && did_restart_edit == 0)
597 foldOpenCursor();
598#endif
599
600 /*
601 * If 'showmode' is set, show the current (insert/replace/..) mode.
602 * A warning message for changing a readonly file is given here, before
603 * actually changing anything. It's put after the mode, if any.
604 */
605 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000606 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 i = showmode();
608
609 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000610 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611
612#ifdef CURSOR_SHAPE
613 ui_cursor_shape(); /* may show different cursor shape */
614#endif
615#ifdef FEAT_DIGRAPHS
616 do_digraph(-1); /* clear digraphs */
617#endif
618
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000619 /*
620 * Get the current length of the redo buffer, those characters have to be
621 * skipped if we want to get to the inserted characters.
622 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 ptr = get_inserted();
624 if (ptr == NULL)
625 new_insert_skip = 0;
626 else
627 {
628 new_insert_skip = (int)STRLEN(ptr);
629 vim_free(ptr);
630 }
631
632 old_indent = 0;
633
634 /*
635 * Main loop in Insert mode: repeat until Insert mode is left.
636 */
637 for (;;)
638 {
639#ifdef FEAT_RIGHTLEFT
640 if (!revins_legal)
641 revins_scol = -1; /* reset on illegal motions */
642 else
643 revins_legal = 0;
644#endif
645 if (arrow_used) /* don't repeat insert when arrow key used */
646 count = 0;
647
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100648 if (update_Insstart_orig)
649 Insstart_orig = Insstart;
650
Bram Moolenaar071d4272004-06-13 20:20:40 +0000651 if (stop_insert_mode)
652 {
653 /* ":stopinsert" used or 'insertmode' reset */
654 count = 0;
655 goto doESCkey;
656 }
657
658 /* set curwin->w_curswant for next K_DOWN or K_UP */
659 if (!arrow_used)
660 curwin->w_set_curswant = TRUE;
661
662 /* If there is no typeahead may check for timestamps (e.g., for when a
663 * menu invoked a shell command). */
664 if (stuff_empty())
665 {
666 did_check_timestamps = FALSE;
667 if (need_check_timestamps)
668 check_timestamps(FALSE);
669 }
670
671 /*
672 * When emsg() was called msg_scroll will have been set.
673 */
674 msg_scroll = FALSE;
675
676#ifdef FEAT_GUI
677 /* When 'mousefocus' is set a mouse movement may have taken us to
678 * another window. "need_mouse_correct" may then be set because of an
679 * autocommand. */
680 if (need_mouse_correct)
681 gui_mouse_correct();
682#endif
683
684#ifdef FEAT_FOLDING
685 /* Open fold at the cursor line, according to 'foldopen'. */
686 if (fdo_flags & FDO_INSERT)
687 foldOpenCursor();
688 /* Close folds where the cursor isn't, according to 'foldclose' */
689 if (!char_avail())
690 foldCheckClose();
691#endif
692
693 /*
694 * If we inserted a character at the last position of the last line in
695 * the window, scroll the window one line up. This avoids an extra
696 * redraw.
697 * This is detected when the cursor column is smaller after inserting
698 * something.
699 * Don't do this when the topline changed already, it has
700 * already been adjusted (by insertchar() calling open_line())).
701 */
702 if (curbuf->b_mod_set
703 && curwin->w_p_wrap
704 && !did_backspace
705 && curwin->w_topline == old_topline
706#ifdef FEAT_DIFF
707 && curwin->w_topfill == old_topfill
708#endif
709 )
710 {
711 mincol = curwin->w_wcol;
712 validate_cursor_col();
713
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000714 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715 && curwin->w_wrow == W_WINROW(curwin)
716 + curwin->w_height - 1 - p_so
717 && (curwin->w_cursor.lnum != curwin->w_topline
718#ifdef FEAT_DIFF
719 || curwin->w_topfill > 0
720#endif
721 ))
722 {
723#ifdef FEAT_DIFF
724 if (curwin->w_topfill > 0)
725 --curwin->w_topfill;
726 else
727#endif
728#ifdef FEAT_FOLDING
729 if (hasFolding(curwin->w_topline, NULL, &old_topline))
730 set_topline(curwin, old_topline + 1);
731 else
732#endif
733 set_topline(curwin, curwin->w_topline + 1);
734 }
735 }
736
737 /* May need to adjust w_topline to show the cursor. */
738 update_topline();
739
740 did_backspace = FALSE;
741
742 validate_cursor(); /* may set must_redraw */
743
744 /*
745 * Redraw the display when no characters are waiting.
746 * Also shows mode, ruler and positions cursor.
747 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000748 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749
750#ifdef FEAT_SCROLLBIND
751 if (curwin->w_p_scb)
752 do_check_scrollbind(TRUE);
753#endif
754
Bram Moolenaar860cae12010-06-05 23:22:07 +0200755#ifdef FEAT_CURSORBIND
756 if (curwin->w_p_crb)
757 do_check_cursorbind();
758#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 update_curswant();
760 old_topline = curwin->w_topline;
761#ifdef FEAT_DIFF
762 old_topfill = curwin->w_topfill;
763#endif
764
765#ifdef USE_ON_FLY_SCROLL
766 dont_scroll = FALSE; /* allow scrolling here */
767#endif
768
769 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000770 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 */
Bram Moolenaar6c5bdb72015-03-13 13:24:23 +0100772 if (c != K_CURSORHOLD)
773 lastc = c; /* remember the previous char for CTRL-D */
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +0200774
775 /* After using CTRL-G U the next cursor key will not break undo. */
776 if (dont_sync_undo == MAYBE)
777 dont_sync_undo = TRUE;
778 else
779 dont_sync_undo = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000780 do
781 {
782 c = safe_vgetc();
783 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000784
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000785#ifdef FEAT_AUTOCMD
786 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
787 did_cursorhold = TRUE;
788#endif
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790#ifdef FEAT_RIGHTLEFT
791 if (p_hkmap && KeyTyped)
792 c = hkmap(c); /* Hebrew mode mapping */
793#endif
794#ifdef FEAT_FKMAP
795 if (p_fkmap && KeyTyped)
796 c = fkmap(c); /* Farsi mode mapping */
797#endif
798
799#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000800 /*
801 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000802 * and the cursor is still in the completed word. Only when there is
803 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000804 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000805 if (compl_started
806 && pum_wanted()
807 && curwin->w_cursor.col >= compl_col
808 && (compl_shown_match == NULL
809 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000810 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000811 /* BS: Delete one character from "compl_leader". */
812 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000813 && curwin->w_cursor.col > compl_col
814 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000815 continue;
816
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000817 /* When no match was selected or it was edited. */
818 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000819 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000820 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000821 * "compl_leader". Except when at the original match and
822 * there is nothing to add, CTRL-L works like CTRL-P then. */
823 if (c == Ctrl_L
Bram Moolenaare4214502015-03-05 18:08:43 +0100824 && (!CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000825 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000826 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000827 {
828 ins_compl_addfrommatch();
829 continue;
830 }
831
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000832 /* A non-white character that fits in with the current
833 * completion: Add to "compl_leader". */
834 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000835 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100836#ifdef FEAT_AUTOCMD
837 /* Trigger InsertCharPre. */
838 char_u *str = do_insert_char_pre(c);
839 char_u *p;
840
841 if (str != NULL)
842 {
843 for (p = str; *p != NUL; mb_ptr_adv(p))
844 ins_compl_addleader(PTR2CHAR(p));
845 vim_free(str);
846 }
847 else
848#endif
849 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000850 continue;
851 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000852
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000853 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000854 * compl_enter_selects is set the Enter key does the same. */
855 if (c == Ctrl_Y || (compl_enter_selects
856 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000857 {
858 ins_compl_delete();
859 ins_compl_insert();
860 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000861 }
862 }
863
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
865 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000866 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000867 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000868 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869#endif
870
Bram Moolenaar488c6512005-08-11 20:09:58 +0000871 /* CTRL-\ CTRL-N goes to Normal mode,
872 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
873 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000874 if (c == Ctrl_BSL)
875 {
876 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000877 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 ++no_mapping;
879 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000880 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000881 --no_mapping;
882 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000883 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000884 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000885 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 vungetc(c);
887 c = Ctrl_BSL;
888 }
889 else if (c == Ctrl_G && p_im)
890 continue;
891 else
892 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000893 if (c == Ctrl_O)
894 {
895 ins_ctrl_o();
896 ins_at_eol = FALSE; /* cursor keeps its column */
897 nomove = TRUE;
898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899 count = 0;
900 goto doESCkey;
901 }
902 }
903
904#ifdef FEAT_DIGRAPHS
905 c = do_digraph(c);
906#endif
907
908#ifdef FEAT_INS_EXPAND
909 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
910 goto docomplete;
911#endif
912 if (c == Ctrl_V || c == Ctrl_Q)
913 {
914 ins_ctrl_v();
915 c = Ctrl_V; /* pretend CTRL-V is last typed character */
916 continue;
917 }
918
919#ifdef FEAT_CINDENT
920 if (cindent_on()
921# ifdef FEAT_INS_EXPAND
922 && ctrl_x_mode == 0
923# endif
924 )
925 {
926 /* A key name preceded by a bang means this key is not to be
927 * inserted. Skip ahead to the re-indenting below.
928 * A key name preceded by a star means that indenting has to be
929 * done before inserting the key. */
930 line_is_white = inindent(0);
931 if (in_cinkeys(c, '!', line_is_white))
932 goto force_cindent;
933 if (can_cindent && in_cinkeys(c, '*', line_is_white)
934 && stop_arrow() == OK)
935 do_c_expr_indent();
936 }
937#endif
938
939#ifdef FEAT_RIGHTLEFT
940 if (curwin->w_p_rl)
941 switch (c)
942 {
943 case K_LEFT: c = K_RIGHT; break;
944 case K_S_LEFT: c = K_S_RIGHT; break;
945 case K_C_LEFT: c = K_C_RIGHT; break;
946 case K_RIGHT: c = K_LEFT; break;
947 case K_S_RIGHT: c = K_S_LEFT; break;
948 case K_C_RIGHT: c = K_C_LEFT; break;
949 }
950#endif
951
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 /*
953 * If 'keymodel' contains "startsel", may start selection. If it
954 * does, a CTRL-O and c will be stuffed, we need to get these
955 * characters.
956 */
957 if (ins_start_select(c))
958 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959
960 /*
961 * The big switch to handle a character in insert mode.
962 */
963 switch (c)
964 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000965 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000966 if (echeck_abbr(ESC + ABBR_OFF))
967 break;
968 /*FALLTHROUGH*/
969
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000970 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971#ifdef FEAT_CMDWIN
972 if (c == Ctrl_C && cmdwin_type != 0)
973 {
974 /* Close the cmdline window. */
975 cmdwin_result = K_IGNORE;
976 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000977 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000978 goto doESCkey;
979 }
980#endif
981
982#ifdef UNIX
983do_intr:
984#endif
985 /* when 'insertmode' set, and not halfway a mapping, don't leave
986 * Insert mode */
987 if (goto_im())
988 {
989 if (got_int)
990 {
991 (void)vgetc(); /* flush all buffers */
992 got_int = FALSE;
993 }
994 else
Bram Moolenaar165bc692015-07-21 17:53:25 +0200995 vim_beep(BO_IM);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 break;
997 }
998doESCkey:
999 /*
1000 * This is the ONLY return from edit()!
1001 */
1002 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
1003 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +00001004 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005 o_lnum = curwin->w_cursor.lnum;
1006
Bram Moolenaar488c6512005-08-11 20:09:58 +00001007 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +00001008 {
1009#ifdef FEAT_AUTOCMD
1010 if (cmdchar != 'r' && cmdchar != 'v')
1011 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
1012 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +00001013 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +00001014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 continue;
1018
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001019 case Ctrl_Z: /* suspend when 'insertmode' set */
1020 if (!p_im)
1021 goto normalchar; /* insert CTRL-Z as normal char */
1022 stuffReadbuff((char_u *)":st\r");
1023 c = Ctrl_O;
1024 /*FALLTHROUGH*/
1025
1026 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001027#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001028 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001029 goto docomplete;
1030#endif
1031 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1032 break;
1033 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001034
1035#ifdef FEAT_VIRTUALEDIT
1036 /* don't move the cursor left when 'virtualedit' has "onemore". */
1037 if (ve_flags & VE_ONEMORE)
1038 {
1039 ins_at_eol = FALSE;
1040 nomove = TRUE;
1041 }
1042#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 count = 0;
1044 goto doESCkey;
1045
Bram Moolenaar572cb562005-08-05 21:35:02 +00001046 case K_INS: /* toggle insert/replace mode */
1047 case K_KINS:
1048 ins_insert(replaceState);
1049 break;
1050
1051 case K_SELECT: /* end of Select mode mapping - ignore */
1052 break;
1053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054#ifdef FEAT_SNIFF
1055 case K_SNIFF: /* Sniff command received */
1056 stuffcharReadbuff(K_SNIFF);
1057 goto doESCkey;
1058#endif
1059
1060 case K_HELP: /* Help key works like <ESC> <Help> */
1061 case K_F1:
1062 case K_XF1:
1063 stuffcharReadbuff(K_HELP);
1064 if (p_im)
1065 need_start_insertmode = TRUE;
1066 goto doESCkey;
1067
1068#ifdef FEAT_NETBEANS_INTG
1069 case K_F21: /* NetBeans command */
1070 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001071 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 --no_mapping;
1073 netbeans_keycommand(i);
1074 break;
1075#endif
1076
1077 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 case NUL:
1079 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 /* For ^@ the trailing ESC will end the insert, unless there is an
1081 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1083 && c != Ctrl_A && !p_im)
1084 goto doESCkey; /* quit insert mode */
1085 inserted_space = FALSE;
1086 break;
1087
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 ins_reg();
1090 auto_format(FALSE, TRUE);
1091 inserted_space = FALSE;
1092 break;
1093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 ins_ctrl_g();
1096 break;
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case Ctrl_HAT: /* switch input mode and/or langmap */
1099 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 break;
1101
1102#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001103 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 if (!p_ari)
1105 goto normalchar;
1106 ins_ctrl_();
1107 break;
1108#endif
1109
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001110 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001111#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1112 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1113 goto docomplete;
1114#endif
1115 /* FALLTHROUGH */
1116
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001117 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118# ifdef FEAT_INS_EXPAND
1119 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1120 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001121 if (has_compl_option(FALSE))
1122 goto docomplete;
1123 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 }
1125# endif
1126 ins_shift(c, lastc);
1127 auto_format(FALSE, TRUE);
1128 inserted_space = FALSE;
1129 break;
1130
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001131 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 case K_KDEL:
1133 ins_del();
1134 auto_format(FALSE, TRUE);
1135 break;
1136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 case Ctrl_H:
1139 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1140 auto_format(FALSE, TRUE);
1141 break;
1142
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001143 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001144 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1145 auto_format(FALSE, TRUE);
1146 break;
1147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001149# ifdef FEAT_COMPL_FUNC
1150 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001152 goto docomplete;
1153# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1155 auto_format(FALSE, TRUE);
1156 inserted_space = FALSE;
1157 break;
1158
1159#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001160 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 case K_LEFTMOUSE_NM:
1162 case K_LEFTDRAG:
1163 case K_LEFTRELEASE:
1164 case K_LEFTRELEASE_NM:
1165 case K_MIDDLEMOUSE:
1166 case K_MIDDLEDRAG:
1167 case K_MIDDLERELEASE:
1168 case K_RIGHTMOUSE:
1169 case K_RIGHTDRAG:
1170 case K_RIGHTRELEASE:
1171 case K_X1MOUSE:
1172 case K_X1DRAG:
1173 case K_X1RELEASE:
1174 case K_X2MOUSE:
1175 case K_X2DRAG:
1176 case K_X2RELEASE:
1177 ins_mouse(c);
1178 break;
1179
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001181 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 break;
1183
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001184 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001185 ins_mousescroll(MSCR_UP);
1186 break;
1187
1188 case K_MOUSELEFT: /* Scroll wheel left */
1189 ins_mousescroll(MSCR_LEFT);
1190 break;
1191
1192 case K_MOUSERIGHT: /* Scroll wheel right */
1193 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 break;
1195#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001196#ifdef FEAT_GUI_TABLINE
1197 case K_TABLINE:
1198 case K_TABMENU:
1199 ins_tabline(c);
1200 break;
1201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001203 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 break;
1205
Bram Moolenaar754b5602006-02-09 23:53:20 +00001206#ifdef FEAT_AUTOCMD
1207 case K_CURSORHOLD: /* Didn't type something for a while. */
1208 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1209 did_cursorhold = TRUE;
1210 break;
1211#endif
1212
Bram Moolenaar4770d092006-01-12 23:22:24 +00001213#ifdef FEAT_GUI_W32
1214 /* On Win32 ignore <M-F4>, we get it when closing the window was
1215 * cancelled. */
1216 case K_F4:
1217 if (mod_mask != MOD_MASK_ALT)
1218 goto normalchar;
1219 break;
1220#endif
1221
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222#ifdef FEAT_GUI
1223 case K_VER_SCROLLBAR:
1224 ins_scroll();
1225 break;
1226
1227 case K_HOR_SCROLLBAR:
1228 ins_horscroll();
1229 break;
1230#endif
1231
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001232 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 case K_S_HOME:
1235 case K_C_HOME:
1236 ins_home(c);
1237 break;
1238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001239 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001240 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 case K_S_END:
1242 case K_C_END:
1243 ins_end(c);
1244 break;
1245
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001246 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001247 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1248 ins_s_left();
1249 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001250 ins_left(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 break;
1252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 case K_C_LEFT:
1255 ins_s_left();
1256 break;
1257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001258 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001259 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1260 ins_s_right();
1261 else
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02001262 ins_right(dont_sync_undo == FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 break;
1264
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001265 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 case K_C_RIGHT:
1267 ins_s_right();
1268 break;
1269
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001270 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001271#ifdef FEAT_INS_EXPAND
1272 if (pum_visible())
1273 goto docomplete;
1274#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001275 if (mod_mask & MOD_MASK_SHIFT)
1276 ins_pageup();
1277 else
1278 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 break;
1280
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001281 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 case K_PAGEUP:
1283 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001284#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001285 if (pum_visible())
1286 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001287#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 ins_pageup();
1289 break;
1290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001292#ifdef FEAT_INS_EXPAND
1293 if (pum_visible())
1294 goto docomplete;
1295#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001296 if (mod_mask & MOD_MASK_SHIFT)
1297 ins_pagedown();
1298 else
1299 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 break;
1301
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001302 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 case K_PAGEDOWN:
1304 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001305#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001306 if (pum_visible())
1307 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001308#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 ins_pagedown();
1310 break;
1311
1312#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001313 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314 ins_drop();
1315 break;
1316#endif
1317
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001318 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 c = TAB;
1320 /* FALLTHROUGH */
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1324 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1325 goto docomplete;
1326#endif
1327 inserted_space = FALSE;
1328 if (ins_tab())
1329 goto normalchar; /* insert TAB as a normal char */
1330 auto_format(FALSE, TRUE);
1331 break;
1332
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001333 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 c = CAR;
1335 /* FALLTHROUGH */
1336 case CAR:
1337 case NL:
1338#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1339 /* In a quickfix window a <CR> jumps to the error under the
1340 * cursor. */
1341 if (bt_quickfix(curbuf) && c == CAR)
1342 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001343 if (curwin->w_llist_ref == NULL) /* quickfix window */
1344 do_cmdline_cmd((char_u *)".cc");
1345 else /* location list window */
1346 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 break;
1348 }
1349#endif
1350#ifdef FEAT_CMDWIN
1351 if (cmdwin_type != 0)
1352 {
1353 /* Execute the command in the cmdline window. */
1354 cmdwin_result = CAR;
1355 goto doESCkey;
1356 }
1357#endif
1358 if (ins_eol(c) && !p_im)
1359 goto doESCkey; /* out of memory */
1360 auto_format(FALSE, FALSE);
1361 inserted_space = FALSE;
1362 break;
1363
Bram Moolenaar860cae12010-06-05 23:22:07 +02001364#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001365 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366# ifdef FEAT_INS_EXPAND
1367 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1368 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001369 if (has_compl_option(TRUE))
1370 goto docomplete;
1371 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 }
1373# endif
1374# ifdef FEAT_DIGRAPHS
1375 c = ins_digraph();
1376 if (c == NUL)
1377 break;
1378# endif
1379 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001381
1382#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001383 case Ctrl_X: /* Enter CTRL-X mode */
1384 ins_ctrl_x();
1385 break;
1386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001387 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (ctrl_x_mode != CTRL_X_TAGS)
1389 goto normalchar;
1390 goto docomplete;
1391
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001392 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001393 if (ctrl_x_mode != CTRL_X_FILES)
1394 goto normalchar;
1395 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001396
1397 case 's': /* Spelling completion after ^X */
1398 case Ctrl_S:
1399 if (ctrl_x_mode != CTRL_X_SPELL)
1400 goto normalchar;
1401 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402#endif
1403
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001404 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405#ifdef FEAT_INS_EXPAND
1406 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1407#endif
1408 {
1409 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1410 if (p_im)
1411 {
1412 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1413 break;
1414 goto doESCkey;
1415 }
1416 goto normalchar;
1417 }
1418#ifdef FEAT_INS_EXPAND
1419 /* FALLTHROUGH */
1420
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001421 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 case Ctrl_N:
1423 /* if 'complete' is empty then plain ^P is no longer special,
1424 * but it is under other ^X modes */
1425 if (*curbuf->b_p_cpt == NUL
1426 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001427 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 goto normalchar;
1429
1430docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001431 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001433 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001434 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 break;
1436#endif /* FEAT_INS_EXPAND */
1437
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001438 case Ctrl_Y: /* copy from previous line or scroll down */
1439 case Ctrl_E: /* copy from next line or scroll up */
1440 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441 break;
1442
1443 default:
1444#ifdef UNIX
1445 if (c == intr_char) /* special interrupt char */
1446 goto do_intr;
1447#endif
1448
Bram Moolenaare659c952011-05-19 17:25:41 +02001449normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001451 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001452 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001453#ifdef FEAT_AUTOCMD
1454 if (!p_paste)
1455 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001456 /* Trigger InsertCharPre. */
1457 char_u *str = do_insert_char_pre(c);
1458 char_u *p;
1459
1460 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001461 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001462 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001463 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001464 /* Insert the new value of v:char literally. */
1465 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001466 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001467 c = PTR2CHAR(p);
1468 if (c == CAR || c == K_KENTER || c == NL)
1469 ins_eol(c);
1470 else
1471 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001472 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001473 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001474 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001475 vim_free(str);
1476 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001477 }
1478
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001479 /* If the new value is already inserted or an empty string
1480 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001481 if (c == NUL)
1482 break;
1483 }
1484#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485#ifdef FEAT_SMARTINDENT
1486 /* Try to perform smart-indenting. */
1487 ins_try_si(c);
1488#endif
1489
1490 if (c == ' ')
1491 {
1492 inserted_space = TRUE;
1493#ifdef FEAT_CINDENT
1494 if (inindent(0))
1495 can_cindent = FALSE;
1496#endif
1497 if (Insstart_blank_vcol == MAXCOL
1498 && curwin->w_cursor.lnum == Insstart.lnum)
1499 Insstart_blank_vcol = get_nolist_virtcol();
1500 }
1501
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001502 /* Insert a normal character and check for abbreviations on a
1503 * special character. Let CTRL-] expand abbreviations without
1504 * inserting it. */
1505 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506#ifdef FEAT_MBYTE
1507 /* Add ABBR_OFF for characters above 0x100, this is
1508 * what check_abbr() expects. */
1509 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1510#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001511 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 {
1513 insert_special(c, FALSE, FALSE);
1514#ifdef FEAT_RIGHTLEFT
1515 revins_legal++;
1516 revins_chars++;
1517#endif
1518 }
1519
1520 auto_format(FALSE, TRUE);
1521
1522#ifdef FEAT_FOLDING
1523 /* When inserting a character the cursor line must never be in a
1524 * closed fold. */
1525 foldOpenCursor();
1526#endif
1527 break;
1528 } /* end of switch (c) */
1529
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001530#ifdef FEAT_AUTOCMD
1531 /* If typed something may trigger CursorHoldI again. */
1532 if (c != K_CURSORHOLD)
1533 did_cursorhold = FALSE;
1534#endif
1535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 /* If the cursor was moved we didn't just insert a space */
1537 if (arrow_used)
1538 inserted_space = FALSE;
1539
1540#ifdef FEAT_CINDENT
1541 if (can_cindent && cindent_on()
1542# ifdef FEAT_INS_EXPAND
1543 && ctrl_x_mode == 0
1544# endif
1545 )
1546 {
1547force_cindent:
1548 /*
1549 * Indent now if a key was typed that is in 'cinkeys'.
1550 */
1551 if (in_cinkeys(c, ' ', line_is_white))
1552 {
1553 if (stop_arrow() == OK)
1554 /* re-indent the current line */
1555 do_c_expr_indent();
1556 }
1557 }
1558#endif /* FEAT_CINDENT */
1559
1560 } /* for (;;) */
1561 /* NOTREACHED */
1562}
1563
1564/*
1565 * Redraw for Insert mode.
1566 * This is postponed until getting the next character to make '$' in the 'cpo'
1567 * option work correctly.
1568 * Only redraw when there are no characters available. This speeds up
1569 * inserting sequences of characters (e.g., for CTRL-R).
1570 */
1571 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001572ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001573 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001574{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001575#ifdef FEAT_CONCEAL
1576 linenr_T conceal_old_cursor_line = 0;
1577 linenr_T conceal_new_cursor_line = 0;
1578 int conceal_update_lines = FALSE;
1579#endif
1580
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001581 if (char_avail())
1582 return;
1583
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001584#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001585 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1586 * visible, the command might delete it. */
1587 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001588# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001589 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001590# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001591# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001592 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001593# endif
1594# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001595 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001596# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001597 )
1598 && !equalpos(last_cursormoved, curwin->w_cursor)
1599# ifdef FEAT_INS_EXPAND
1600 && !pum_visible()
1601# endif
1602 )
1603 {
1604# ifdef FEAT_SYN_HL
1605 /* Need to update the screen first, to make sure syntax
1606 * highlighting is correct after making a change (e.g., inserting
1607 * a "(". The autocommand may also require a redraw, so it's done
1608 * again below, unfortunately. */
1609 if (syntax_present(curwin) && must_redraw)
1610 update_screen(0);
1611# endif
1612# ifdef FEAT_AUTOCMD
1613 if (has_cursormovedI())
1614 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1615# endif
1616# ifdef FEAT_CONCEAL
1617 if (curwin->w_p_cole > 0)
1618 {
1619 conceal_old_cursor_line = last_cursormoved.lnum;
1620 conceal_new_cursor_line = curwin->w_cursor.lnum;
1621 conceal_update_lines = TRUE;
1622 }
1623# endif
1624 last_cursormoved = curwin->w_cursor;
1625 }
1626#endif
1627
1628#ifdef FEAT_AUTOCMD
1629 /* Trigger TextChangedI if b_changedtick differs. */
1630 if (ready && has_textchangedI()
1631 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001632# ifdef FEAT_INS_EXPAND
1633 && !pum_visible()
1634# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001635 )
1636 {
1637 if (last_changedtick_buf == curbuf)
1638 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1639 last_changedtick_buf = curbuf;
1640 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001642#endif
1643
1644 if (must_redraw)
1645 update_screen(0);
1646 else if (clear_cmdline || redraw_cmdline)
1647 showmode(); /* clear cmdline and show mode */
1648# if defined(FEAT_CONCEAL)
1649 if ((conceal_update_lines
1650 && (conceal_old_cursor_line != conceal_new_cursor_line
1651 || conceal_cursor_line(curwin)))
1652 || need_cursor_line_redraw)
1653 {
1654 if (conceal_old_cursor_line != conceal_new_cursor_line)
1655 update_single_line(curwin, conceal_old_cursor_line);
1656 update_single_line(curwin, conceal_new_cursor_line == 0
1657 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1658 curwin->w_valid &= ~VALID_CROW;
1659 }
1660# endif
1661 showruler(FALSE);
1662 setcursor();
1663 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664}
1665
1666/*
1667 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1668 */
1669 static void
1670ins_ctrl_v()
1671{
1672 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001673 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674
1675 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001676 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001677
1678 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001679 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001681 did_putchar = TRUE;
1682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1684
1685#ifdef FEAT_CMDL_INFO
1686 add_to_showcmd_c(Ctrl_V);
1687#endif
1688
1689 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001690 if (did_putchar)
1691 /* when the line fits in 'columns' the '^' is at the start of the next
1692 * line and will not removed by the redraw */
1693 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694#ifdef FEAT_CMDL_INFO
1695 clear_showcmd();
1696#endif
1697 insert_special(c, FALSE, TRUE);
1698#ifdef FEAT_RIGHTLEFT
1699 revins_chars++;
1700 revins_legal++;
1701#endif
1702}
1703
1704/*
1705 * Put a character directly onto the screen. It's not stored in a buffer.
1706 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1707 */
1708static int pc_status;
1709#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1710#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1711#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1712#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001713static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001714static int pc_attr;
1715static int pc_row;
1716static int pc_col;
1717
1718 void
1719edit_putchar(c, highlight)
1720 int c;
1721 int highlight;
1722{
1723 int attr;
1724
1725 if (ScreenLines != NULL)
1726 {
1727 update_topline(); /* just in case w_topline isn't valid */
1728 validate_cursor();
1729 if (highlight)
1730 attr = hl_attr(HLF_8);
1731 else
1732 attr = 0;
1733 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1734 pc_col = W_WINCOL(curwin);
1735#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1736 pc_status = PC_STATUS_UNSET;
1737#endif
1738#ifdef FEAT_RIGHTLEFT
1739 if (curwin->w_p_rl)
1740 {
1741 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1742# ifdef FEAT_MBYTE
1743 if (has_mbyte)
1744 {
1745 int fix_col = mb_fix_col(pc_col, pc_row);
1746
1747 if (fix_col != pc_col)
1748 {
1749 screen_putchar(' ', pc_row, fix_col, attr);
1750 --curwin->w_wcol;
1751 pc_status = PC_STATUS_RIGHT;
1752 }
1753 }
1754# endif
1755 }
1756 else
1757#endif
1758 {
1759 pc_col += curwin->w_wcol;
1760#ifdef FEAT_MBYTE
1761 if (mb_lefthalve(pc_row, pc_col))
1762 pc_status = PC_STATUS_LEFT;
1763#endif
1764 }
1765
1766 /* save the character to be able to put it back */
1767#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1768 if (pc_status == PC_STATUS_UNSET)
1769#endif
1770 {
1771 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1772 pc_status = PC_STATUS_SET;
1773 }
1774 screen_putchar(c, pc_row, pc_col, attr);
1775 }
1776}
1777
1778/*
1779 * Undo the previous edit_putchar().
1780 */
1781 void
1782edit_unputchar()
1783{
1784 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1785 {
1786#if defined(FEAT_MBYTE)
1787 if (pc_status == PC_STATUS_RIGHT)
1788 ++curwin->w_wcol;
1789 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1790 redrawWinline(curwin->w_cursor.lnum, FALSE);
1791 else
1792#endif
1793 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1794 }
1795}
1796
1797/*
1798 * Called when p_dollar is set: display a '$' at the end of the changed text
1799 * Only works when cursor is in the line that changes.
1800 */
1801 void
1802display_dollar(col)
1803 colnr_T col;
1804{
1805 colnr_T save_col;
1806
1807 if (!redrawing())
1808 return;
1809
1810 cursor_off();
1811 save_col = curwin->w_cursor.col;
1812 curwin->w_cursor.col = col;
1813#ifdef FEAT_MBYTE
1814 if (has_mbyte)
1815 {
1816 char_u *p;
1817
1818 /* If on the last byte of a multi-byte move to the first byte. */
1819 p = ml_get_curline();
1820 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1821 }
1822#endif
1823 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1824 if (curwin->w_wcol < W_WIDTH(curwin))
1825 {
1826 edit_putchar('$', FALSE);
1827 dollar_vcol = curwin->w_virtcol;
1828 }
1829 curwin->w_cursor.col = save_col;
1830}
1831
1832/*
1833 * Call this function before moving the cursor from the normal insert position
1834 * in insert mode.
1835 */
1836 static void
1837undisplay_dollar()
1838{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001839 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001841 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842 redrawWinline(curwin->w_cursor.lnum, FALSE);
1843 }
1844}
1845
1846/*
1847 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1848 * Keep the cursor on the same character.
1849 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1850 * type == INDENT_DEC decrease indent (for CTRL-D)
1851 * type == INDENT_SET set indent to "amount"
1852 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1853 */
1854 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001855change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 int type;
1857 int amount;
1858 int round;
1859 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001860 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861{
1862 int vcol;
1863 int last_vcol;
1864 int insstart_less; /* reduction for Insstart.col */
1865 int new_cursor_col;
1866 int i;
1867 char_u *ptr;
1868 int save_p_list;
1869 int start_col;
1870 colnr_T vc;
1871#ifdef FEAT_VREPLACE
1872 colnr_T orig_col = 0; /* init for GCC */
1873 char_u *new_line, *orig_line = NULL; /* init for GCC */
1874
1875 /* VREPLACE mode needs to know what the line was like before changing */
1876 if (State & VREPLACE_FLAG)
1877 {
1878 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1879 orig_col = curwin->w_cursor.col;
1880 }
1881#endif
1882
1883 /* for the following tricks we don't want list mode */
1884 save_p_list = curwin->w_p_list;
1885 curwin->w_p_list = FALSE;
1886 vc = getvcol_nolist(&curwin->w_cursor);
1887 vcol = vc;
1888
1889 /*
1890 * For Replace mode we need to fix the replace stack later, which is only
1891 * possible when the cursor is in the indent. Remember the number of
1892 * characters before the cursor if it's possible.
1893 */
1894 start_col = curwin->w_cursor.col;
1895
1896 /* determine offset from first non-blank */
1897 new_cursor_col = curwin->w_cursor.col;
1898 beginline(BL_WHITE);
1899 new_cursor_col -= curwin->w_cursor.col;
1900
1901 insstart_less = curwin->w_cursor.col;
1902
1903 /*
1904 * If the cursor is in the indent, compute how many screen columns the
1905 * cursor is to the left of the first non-blank.
1906 */
1907 if (new_cursor_col < 0)
1908 vcol = get_indent() - vcol;
1909
1910 if (new_cursor_col > 0) /* can't fix replace stack */
1911 start_col = -1;
1912
1913 /*
1914 * Set the new indent. The cursor will be put on the first non-blank.
1915 */
1916 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001917 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 else
1919 {
1920#ifdef FEAT_VREPLACE
1921 int save_State = State;
1922
1923 /* Avoid being called recursively. */
1924 if (State & VREPLACE_FLAG)
1925 State = INSERT;
1926#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001927 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928#ifdef FEAT_VREPLACE
1929 State = save_State;
1930#endif
1931 }
1932 insstart_less -= curwin->w_cursor.col;
1933
1934 /*
1935 * Try to put cursor on same character.
1936 * If the cursor is at or after the first non-blank in the line,
1937 * compute the cursor column relative to the column of the first
1938 * non-blank character.
1939 * If we are not in insert mode, leave the cursor on the first non-blank.
1940 * If the cursor is before the first non-blank, position it relative
1941 * to the first non-blank, counted in screen columns.
1942 */
1943 if (new_cursor_col >= 0)
1944 {
1945 /*
1946 * When changing the indent while the cursor is touching it, reset
1947 * Insstart_col to 0.
1948 */
1949 if (new_cursor_col == 0)
1950 insstart_less = MAXCOL;
1951 new_cursor_col += curwin->w_cursor.col;
1952 }
1953 else if (!(State & INSERT))
1954 new_cursor_col = curwin->w_cursor.col;
1955 else
1956 {
1957 /*
1958 * Compute the screen column where the cursor should be.
1959 */
1960 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001961 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962
1963 /*
1964 * Advance the cursor until we reach the right screen column.
1965 */
1966 vcol = last_vcol = 0;
1967 new_cursor_col = -1;
1968 ptr = ml_get_curline();
1969 while (vcol <= (int)curwin->w_virtcol)
1970 {
1971 last_vcol = vcol;
1972#ifdef FEAT_MBYTE
1973 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001974 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001975 else
1976#endif
1977 ++new_cursor_col;
Bram Moolenaar597a4222014-06-25 14:39:50 +02001978 vcol += lbr_chartabsize(ptr, ptr + new_cursor_col, (colnr_T)vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001979 }
1980 vcol = last_vcol;
1981
1982 /*
1983 * May need to insert spaces to be able to position the cursor on
1984 * the right screen column.
1985 */
1986 if (vcol != (int)curwin->w_virtcol)
1987 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001988 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001989 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001990 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 if (ptr != NULL)
1992 {
1993 new_cursor_col += i;
1994 ptr[i] = NUL;
1995 while (--i >= 0)
1996 ptr[i] = ' ';
1997 ins_str(ptr);
1998 vim_free(ptr);
1999 }
2000 }
2001
2002 /*
2003 * When changing the indent while the cursor is in it, reset
2004 * Insstart_col to 0.
2005 */
2006 insstart_less = MAXCOL;
2007 }
2008
2009 curwin->w_p_list = save_p_list;
2010
2011 if (new_cursor_col <= 0)
2012 curwin->w_cursor.col = 0;
2013 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002014 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 curwin->w_set_curswant = TRUE;
2016 changed_cline_bef_curs();
2017
2018 /*
2019 * May have to adjust the start of the insert.
2020 */
2021 if (State & INSERT)
2022 {
2023 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2024 {
2025 if ((int)Insstart.col <= insstart_less)
2026 Insstart.col = 0;
2027 else
2028 Insstart.col -= insstart_less;
2029 }
2030 if ((int)ai_col <= insstart_less)
2031 ai_col = 0;
2032 else
2033 ai_col -= insstart_less;
2034 }
2035
2036 /*
2037 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2038 * If the number of characters before the cursor decreased, need to pop a
2039 * few characters from the replace stack.
2040 * If the number of characters before the cursor increased, need to push a
2041 * few NULs onto the replace stack.
2042 */
2043 if (REPLACE_NORMAL(State) && start_col >= 0)
2044 {
2045 while (start_col > (int)curwin->w_cursor.col)
2046 {
2047 replace_join(0); /* remove a NUL from the replace stack */
2048 --start_col;
2049 }
2050 while (start_col < (int)curwin->w_cursor.col || replaced)
2051 {
2052 replace_push(NUL);
2053 if (replaced)
2054 {
2055 replace_push(replaced);
2056 replaced = NUL;
2057 }
2058 ++start_col;
2059 }
2060 }
2061
2062#ifdef FEAT_VREPLACE
2063 /*
2064 * For VREPLACE mode, we also have to fix the replace stack. In this case
2065 * it is always possible because we backspace over the whole line and then
2066 * put it back again the way we wanted it.
2067 */
2068 if (State & VREPLACE_FLAG)
2069 {
2070 /* If orig_line didn't allocate, just return. At least we did the job,
2071 * even if you can't backspace. */
2072 if (orig_line == NULL)
2073 return;
2074
2075 /* Save new line */
2076 new_line = vim_strsave(ml_get_curline());
2077 if (new_line == NULL)
2078 return;
2079
2080 /* We only put back the new line up to the cursor */
2081 new_line[curwin->w_cursor.col] = NUL;
2082
2083 /* Put back original line */
2084 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2085 curwin->w_cursor.col = orig_col;
2086
2087 /* Backspace from cursor to start of line */
2088 backspace_until_column(0);
2089
2090 /* Insert new stuff into line again */
2091 ins_bytes(new_line);
2092
2093 vim_free(new_line);
2094 }
2095#endif
2096}
2097
2098/*
2099 * Truncate the space at the end of a line. This is to be used only in an
2100 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2101 * modes.
2102 */
2103 void
2104truncate_spaces(line)
2105 char_u *line;
2106{
2107 int i;
2108
2109 /* find start of trailing white space */
2110 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2111 {
2112 if (State & REPLACE_FLAG)
2113 replace_join(0); /* remove a NUL from the replace stack */
2114 }
2115 line[i + 1] = NUL;
2116}
2117
2118#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2119 || defined(FEAT_COMMENTS) || defined(PROTO)
2120/*
2121 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2122 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002123 * Will attempt not to go before "col" even when there is a composing
2124 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002125 */
2126 void
2127backspace_until_column(col)
2128 int col;
2129{
2130 while ((int)curwin->w_cursor.col > col)
2131 {
2132 curwin->w_cursor.col--;
2133 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002134 replace_do_bs(col);
2135 else if (!del_char_after_col(col))
2136 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002137 }
2138}
2139#endif
2140
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002141/*
2142 * Like del_char(), but make sure not to go before column "limit_col".
2143 * Only matters when there are composing characters.
2144 * Return TRUE when something was deleted.
2145 */
2146 static int
2147del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002148 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002149{
2150#ifdef FEAT_MBYTE
2151 if (enc_utf8 && limit_col >= 0)
2152 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002153 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002154
2155 /* Make sure the cursor is at the start of a character, but
2156 * skip forward again when going too far back because of a
2157 * composing character. */
2158 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002159 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002160 {
2161 int l = utf_ptr2len(ml_get_cursor());
2162
2163 if (l == 0) /* end of line */
2164 break;
2165 curwin->w_cursor.col += l;
2166 }
2167 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2168 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002169 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002170 }
2171 else
2172#endif
2173 (void)del_char(FALSE);
2174 return TRUE;
2175}
2176
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2178/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002179 * CTRL-X pressed in Insert mode.
2180 */
2181 static void
2182ins_ctrl_x()
2183{
2184 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2185 * CTRL-V works like CTRL-N */
2186 if (ctrl_x_mode != CTRL_X_CMDLINE)
2187 {
2188 /* if the next ^X<> won't ADD nothing, then reset
2189 * compl_cont_status */
2190 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002191 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002192 else
2193 compl_cont_status = 0;
2194 /* We're not sure which CTRL-X mode it will be yet */
2195 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2196 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2197 edit_submode_pre = NULL;
2198 showmode();
2199 }
2200}
2201
2202/*
2203 * Return TRUE if the 'dict' or 'tsr' option can be used.
2204 */
2205 static int
2206has_compl_option(dict_opt)
2207 int dict_opt;
2208{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002209 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002210# ifdef FEAT_SPELL
2211 && !curwin->w_p_spell
2212# endif
2213 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002214 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2215 {
2216 ctrl_x_mode = 0;
2217 edit_submode = NULL;
2218 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2219 : (char_u *)_("'thesaurus' option is empty"),
2220 hl_attr(HLF_E));
2221 if (emsg_silent == 0)
2222 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02002223 vim_beep(BO_COMPL);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002224 setcursor();
2225 out_flush();
2226 ui_delay(2000L, FALSE);
2227 }
2228 return FALSE;
2229 }
2230 return TRUE;
2231}
2232
2233/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2235 * This depends on the current mode.
2236 */
2237 int
2238vim_is_ctrl_x_key(c)
2239 int c;
2240{
2241 /* Always allow ^R - let it's results then be checked */
2242 if (c == Ctrl_R)
2243 return TRUE;
2244
Bram Moolenaare3226be2005-12-18 22:10:00 +00002245 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002246 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002247 return TRUE;
2248
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249 switch (ctrl_x_mode)
2250 {
2251 case 0: /* Not in any CTRL-X mode */
2252 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2253 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002254 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2256 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2257 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002258 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002259 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 case CTRL_X_SCROLL:
2261 return (c == Ctrl_Y || c == Ctrl_E);
2262 case CTRL_X_WHOLE_LINE:
2263 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2264 case CTRL_X_FILES:
2265 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2266 case CTRL_X_DICTIONARY:
2267 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2268 case CTRL_X_THESAURUS:
2269 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2270 case CTRL_X_TAGS:
2271 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2272#ifdef FEAT_FIND_ID
2273 case CTRL_X_PATH_PATTERNS:
2274 return (c == Ctrl_P || c == Ctrl_N);
2275 case CTRL_X_PATH_DEFINES:
2276 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2277#endif
2278 case CTRL_X_CMDLINE:
2279 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2280 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002281#ifdef FEAT_COMPL_FUNC
2282 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002283 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002284 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002285 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002286#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002287 case CTRL_X_SPELL:
2288 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare4214502015-03-05 18:08:43 +01002289 case CTRL_X_EVAL:
2290 return (c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 }
2292 EMSG(_(e_internal));
2293 return FALSE;
2294}
2295
2296/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002297 * Return TRUE when character "c" is part of the item currently being
2298 * completed. Used to decide whether to abandon complete mode when the menu
2299 * is visible.
2300 */
2301 static int
2302ins_compl_accept_char(c)
2303 int c;
2304{
2305 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2306 /* When expanding an identifier only accept identifier chars. */
2307 return vim_isIDc(c);
2308
2309 switch (ctrl_x_mode)
2310 {
2311 case CTRL_X_FILES:
2312 /* When expanding file name only accept file name chars. But not
2313 * path separators, so that "proto/<Tab>" expands files in
2314 * "proto", not "proto/" as a whole */
2315 return vim_isfilec(c) && !vim_ispathsep(c);
2316
2317 case CTRL_X_CMDLINE:
2318 case CTRL_X_OMNI:
2319 /* Command line and Omni completion can work with just about any
2320 * printable character, but do stop at white space. */
2321 return vim_isprintc(c) && !vim_iswhite(c);
2322
2323 case CTRL_X_WHOLE_LINE:
2324 /* For while line completion a space can be part of the line. */
2325 return vim_isprintc(c);
2326 }
2327 return vim_iswordc(c);
2328}
2329
2330/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002331 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002333 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 * the rest of the word to be in -- webb
2335 */
2336 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002337ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 char_u *str;
2339 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002340 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 char_u *fname;
2342 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002343 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002345 char_u *p;
2346 int i, c;
2347 int actual_len; /* Take multi-byte characters */
2348 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002349 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002350 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 int has_lower = FALSE;
2352 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002354 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002356 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357
Bram Moolenaara2993e12007-08-12 14:38:46 +00002358 /* Find actual length of completion. */
2359#ifdef FEAT_MBYTE
2360 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002362 p = str;
2363 actual_len = 0;
2364 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002366 mb_ptr_adv(p);
2367 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 }
2369 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002370 else
2371#endif
2372 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373
Bram Moolenaara2993e12007-08-12 14:38:46 +00002374 /* Find actual length of original text. */
2375#ifdef FEAT_MBYTE
2376 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002378 p = compl_orig_text;
2379 actual_compl_length = 0;
2380 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002382 mb_ptr_adv(p);
2383 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002384 }
2385 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002386 else
2387#endif
2388 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002390 /* "actual_len" may be smaller than "actual_compl_length" when using
2391 * thesaurus, only use the minimum when comparing. */
2392 min_len = actual_len < actual_compl_length
2393 ? actual_len : actual_compl_length;
2394
Bram Moolenaara2993e12007-08-12 14:38:46 +00002395 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002396 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002397 if (wca != NULL)
2398 {
2399 p = str;
2400 for (i = 0; i < actual_len; ++i)
2401#ifdef FEAT_MBYTE
2402 if (has_mbyte)
2403 wca[i] = mb_ptr2char_adv(&p);
2404 else
2405#endif
2406 wca[i] = *(p++);
2407
2408 /* Rule 1: Were any chars converted to lower? */
2409 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002410 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002411 {
2412#ifdef FEAT_MBYTE
2413 if (has_mbyte)
2414 c = mb_ptr2char_adv(&p);
2415 else
2416#endif
2417 c = *(p++);
2418 if (MB_ISLOWER(c))
2419 {
2420 has_lower = TRUE;
2421 if (MB_ISUPPER(wca[i]))
2422 {
2423 /* Rule 1 is satisfied. */
2424 for (i = actual_compl_length; i < actual_len; ++i)
2425 wca[i] = MB_TOLOWER(wca[i]);
2426 break;
2427 }
2428 }
2429 }
2430
2431 /*
2432 * Rule 2: No lower case, 2nd consecutive letter converted to
2433 * upper case.
2434 */
2435 if (!has_lower)
2436 {
2437 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002438 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002439 {
2440#ifdef FEAT_MBYTE
2441 if (has_mbyte)
2442 c = mb_ptr2char_adv(&p);
2443 else
2444#endif
2445 c = *(p++);
2446 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2447 {
2448 /* Rule 2 is satisfied. */
2449 for (i = actual_compl_length; i < actual_len; ++i)
2450 wca[i] = MB_TOUPPER(wca[i]);
2451 break;
2452 }
2453 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2454 }
2455 }
2456
2457 /* Copy the original case of the part we typed. */
2458 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002459 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002460 {
2461#ifdef FEAT_MBYTE
2462 if (has_mbyte)
2463 c = mb_ptr2char_adv(&p);
2464 else
2465#endif
2466 c = *(p++);
2467 if (MB_ISLOWER(c))
2468 wca[i] = MB_TOLOWER(wca[i]);
2469 else if (MB_ISUPPER(c))
2470 wca[i] = MB_TOUPPER(wca[i]);
2471 }
2472
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002473 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002474 * Generate encoding specific output from wide character array.
2475 * Multi-byte characters can occupy up to five bytes more than
2476 * ASCII characters, and we also need one byte for NUL, so stay
2477 * six bytes away from the edge of IObuff.
2478 */
2479 p = IObuff;
2480 i = 0;
2481 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2482#ifdef FEAT_MBYTE
2483 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002484 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002485 else
2486#endif
2487 *(p++) = wca[i++];
2488 *p = NUL;
2489
2490 vim_free(wca);
2491 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002493 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2494 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002496 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497}
2498
2499/*
2500 * Add a match to the list of matches.
2501 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002502 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002503 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002505 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002506ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 char_u *str;
2508 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002509 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002511 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002512 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002513 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002514 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002516 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002517 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518
2519 ui_breakcheck();
2520 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002521 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 if (len < 0)
2523 len = (int)STRLEN(str);
2524
2525 /*
2526 * If the same match is already present, don't add it.
2527 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002528 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002530 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 do
2532 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002533 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002534 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002535 && match->cp_str[len] == NUL)
2536 return NOTDONE;
2537 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002538 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 }
2540
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002541 /* Remove any popup menu before changing the list of matches. */
2542 ins_compl_del_pum();
2543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 /*
2545 * Allocate a new match structure.
2546 * Copy the values to the new match structure.
2547 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002548 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002550 return FAIL;
2551 match->cp_number = -1;
2552 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002553 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002554 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 {
2556 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002557 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002559 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002560
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002562 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2564 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002565 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002566 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002567 && compl_curr_match->cp_fname != NULL
2568 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002569 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002570 else if (fname != NULL)
2571 {
2572 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002573 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002575 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002576 match->cp_fname = NULL;
2577 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002578
2579 if (cptext != NULL)
2580 {
2581 int i;
2582
2583 for (i = 0; i < CPT_COUNT; ++i)
2584 if (cptext[i] != NULL && *cptext[i] != NUL)
2585 match->cp_text[i] = vim_strsave(cptext[i]);
2586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002587
2588 /*
2589 * Link the new match structure in the list of matches.
2590 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002591 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002592 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 else if (dir == FORWARD)
2594 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002595 match->cp_next = compl_curr_match->cp_next;
2596 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 }
2598 else /* BACKWARD */
2599 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002600 match->cp_next = compl_curr_match;
2601 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002602 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002603 if (match->cp_next)
2604 match->cp_next->cp_prev = match;
2605 if (match->cp_prev)
2606 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002608 compl_first_match = match;
2609 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002611 /*
2612 * Find the longest common string if still doing that.
2613 */
2614 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2615 ins_compl_longest_match(match);
2616
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 return OK;
2618}
2619
2620/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002621 * Return TRUE if "str[len]" matches with match->cp_str, considering
2622 * match->cp_icase.
2623 */
2624 static int
2625ins_compl_equal(match, str, len)
2626 compl_T *match;
2627 char_u *str;
2628 int len;
2629{
2630 if (match->cp_icase)
2631 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2632 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2633}
2634
2635/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002636 * Reduce the longest common string for match "match".
2637 */
2638 static void
2639ins_compl_longest_match(match)
2640 compl_T *match;
2641{
2642 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002643 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002644 int had_match;
2645
2646 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002647 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002648 /* First match, use it as a whole. */
2649 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002650 if (compl_leader != NULL)
2651 {
2652 had_match = (curwin->w_cursor.col > compl_col);
2653 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002654 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002655 ins_redraw(FALSE);
2656
2657 /* When the match isn't there (to avoid matching itself) remove it
2658 * again after redrawing. */
2659 if (!had_match)
2660 ins_compl_delete();
2661 compl_used_match = FALSE;
2662 }
2663 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002664 else
2665 {
2666 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002667 p = compl_leader;
2668 s = match->cp_str;
2669 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002670 {
2671#ifdef FEAT_MBYTE
2672 if (has_mbyte)
2673 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002674 c1 = mb_ptr2char(p);
2675 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002676 }
2677 else
2678#endif
2679 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002680 c1 = *p;
2681 c2 = *s;
2682 }
2683 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2684 : (c1 != c2))
2685 break;
2686#ifdef FEAT_MBYTE
2687 if (has_mbyte)
2688 {
2689 mb_ptr_adv(p);
2690 mb_ptr_adv(s);
2691 }
2692 else
2693#endif
2694 {
2695 ++p;
2696 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002697 }
2698 }
2699
2700 if (*p != NUL)
2701 {
2702 /* Leader was shortened, need to change the inserted text. */
2703 *p = NUL;
2704 had_match = (curwin->w_cursor.col > compl_col);
2705 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002706 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002707 ins_redraw(FALSE);
2708
2709 /* When the match isn't there (to avoid matching itself) remove it
2710 * again after redrawing. */
2711 if (!had_match)
2712 ins_compl_delete();
2713 }
2714
2715 compl_used_match = FALSE;
2716 }
2717}
2718
2719/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002720 * Add an array of matches to the list of matches.
2721 * Frees matches[].
2722 */
2723 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002724ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 int num_matches;
2726 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002727 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002728{
2729 int i;
2730 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002731 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732
Bram Moolenaar572cb562005-08-05 21:35:02 +00002733 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002734 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002735 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002737 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 FreeWild(num_matches, matches);
2739}
2740
2741/* Make the completion list cyclic.
2742 * Return the number of matches (excluding the original).
2743 */
2744 static int
2745ins_compl_make_cyclic()
2746{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002747 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002748 int count = 0;
2749
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002750 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002751 {
2752 /*
2753 * Find the end of the list.
2754 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002755 match = compl_first_match;
2756 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002757 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002758 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002759 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002760 ++count;
2761 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002762 match->cp_next = compl_first_match;
2763 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002764 }
2765 return count;
2766}
2767
Bram Moolenaara94bc432006-03-10 21:42:59 +00002768/*
2769 * Start completion for the complete() function.
2770 * "startcol" is where the matched text starts (1 is first column).
2771 * "list" is the list of matches.
2772 */
2773 void
2774set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002775 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002776 list_T *list;
2777{
2778 /* If already doing completions stop it. */
2779 if (ctrl_x_mode != 0)
2780 ins_compl_prep(' ');
2781 ins_compl_clear();
2782
2783 if (stop_arrow() == FAIL)
2784 return;
2785
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002786 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002787 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002788 startcol = curwin->w_cursor.col;
2789 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002790 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002791 /* compl_pattern doesn't need to be set */
2792 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2793 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002794 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002795 return;
2796
Bram Moolenaare4214502015-03-05 18:08:43 +01002797 ctrl_x_mode = CTRL_X_EVAL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002798
2799 ins_compl_add_list(list);
2800 compl_matches = ins_compl_make_cyclic();
2801 compl_started = TRUE;
2802 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002803 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002804
2805 compl_curr_match = compl_first_match;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002806 if (compl_no_insert)
Bram Moolenaar2cf69382015-07-17 13:42:23 +02002807 ins_complete(K_DOWN);
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002808 else
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02002809 ins_complete(Ctrl_N);
Bram Moolenaar2cf69382015-07-17 13:42:23 +02002810 if (compl_no_select)
2811 ins_complete(Ctrl_P);
Bram Moolenaara94bc432006-03-10 21:42:59 +00002812 out_flush();
2813}
2814
2815
Bram Moolenaar9372a112005-12-06 19:59:18 +00002816/* "compl_match_array" points the currently displayed list of entries in the
2817 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002818static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002819static int compl_match_arraysize;
2820
2821/*
2822 * Update the screen and when there is any scrolling remove the popup menu.
2823 */
2824 static void
2825ins_compl_upd_pum()
2826{
2827 int h;
2828
2829 if (compl_match_array != NULL)
2830 {
2831 h = curwin->w_cline_height;
2832 update_screen(0);
2833 if (h != curwin->w_cline_height)
2834 ins_compl_del_pum();
2835 }
2836}
2837
2838/*
2839 * Remove any popup menu.
2840 */
2841 static void
2842ins_compl_del_pum()
2843{
2844 if (compl_match_array != NULL)
2845 {
2846 pum_undisplay();
2847 vim_free(compl_match_array);
2848 compl_match_array = NULL;
2849 }
2850}
2851
2852/*
2853 * Return TRUE if the popup menu should be displayed.
2854 */
2855 static int
2856pum_wanted()
2857{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002858 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002859 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002860 return FALSE;
2861
2862 /* The display looks bad on a B&W display. */
2863 if (t_colors < 8
2864#ifdef FEAT_GUI
2865 && !gui.in_use
2866#endif
2867 )
2868 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002869 return TRUE;
2870}
2871
2872/*
2873 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002874 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002875 */
2876 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002877pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002878{
2879 compl_T *compl;
2880 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002881
2882 /* Don't display the popup menu if there are no matches or there is only
2883 * one (ignoring the original text). */
2884 compl = compl_first_match;
2885 i = 0;
2886 do
2887 {
2888 if (compl == NULL
2889 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2890 break;
2891 compl = compl->cp_next;
2892 } while (compl != compl_first_match);
2893
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002894 if (strstr((char *)p_cot, "menuone") != NULL)
2895 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002896 return (i >= 2);
2897}
2898
2899/*
2900 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002901 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002902 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002903 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002904ins_compl_show_pum()
2905{
2906 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002907 compl_T *shown_compl = NULL;
2908 int did_find_shown_match = FALSE;
2909 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002910 int i;
2911 int cur = -1;
2912 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002913 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002914
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002915 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002916 return;
2917
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002918#if defined(FEAT_EVAL)
2919 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2920 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2921#endif
2922
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002923 /* Update the screen before drawing the popup menu over it. */
2924 update_screen(0);
2925
2926 if (compl_match_array == NULL)
2927 {
2928 /* Need to build the popup menu list. */
2929 compl_match_arraysize = 0;
2930 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002931 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002932 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002933 do
2934 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002935 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2936 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002937 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002938 ++compl_match_arraysize;
2939 compl = compl->cp_next;
2940 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002941 if (compl_match_arraysize == 0)
2942 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002943 compl_match_array = (pumitem_T *)alloc_clear(
2944 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002945 * compl_match_arraysize));
2946 if (compl_match_array != NULL)
2947 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002948 /* If the current match is the original text don't find the first
2949 * match after it, don't highlight anything. */
2950 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2951 shown_match_ok = TRUE;
2952
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002953 i = 0;
2954 compl = compl_first_match;
2955 do
2956 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002957 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2958 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002959 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002960 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002961 if (!shown_match_ok)
2962 {
2963 if (compl == compl_shown_match || did_find_shown_match)
2964 {
2965 /* This item is the shown match or this is the
2966 * first displayed item after the shown match. */
2967 compl_shown_match = compl;
2968 did_find_shown_match = TRUE;
2969 shown_match_ok = TRUE;
2970 }
2971 else
2972 /* Remember this displayed match for when the
2973 * shown match is just below it. */
2974 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002975 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002976 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002977
2978 if (compl->cp_text[CPT_ABBR] != NULL)
2979 compl_match_array[i].pum_text =
2980 compl->cp_text[CPT_ABBR];
2981 else
2982 compl_match_array[i].pum_text = compl->cp_str;
2983 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2984 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2985 if (compl->cp_text[CPT_MENU] != NULL)
2986 compl_match_array[i++].pum_extra =
2987 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002988 else
2989 compl_match_array[i++].pum_extra = compl->cp_fname;
2990 }
2991
2992 if (compl == compl_shown_match)
2993 {
2994 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002995
2996 /* When the original text is the shown match don't set
2997 * compl_shown_match. */
2998 if (compl->cp_flags & ORIGINAL_TEXT)
2999 shown_match_ok = TRUE;
3000
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003001 if (!shown_match_ok && shown_compl != NULL)
3002 {
3003 /* The shown match isn't displayed, set it to the
3004 * previously displayed match. */
3005 compl_shown_match = shown_compl;
3006 shown_match_ok = TRUE;
3007 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003008 }
3009 compl = compl->cp_next;
3010 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003011
3012 if (!shown_match_ok) /* no displayed match at all */
3013 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003014 }
3015 }
3016 else
3017 {
3018 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00003019 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00003020 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3021 || compl_match_array[i].pum_text
3022 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003023 {
3024 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003025 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003026 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003027 }
3028
3029 if (compl_match_array != NULL)
3030 {
Bram Moolenaar478c46e2015-03-31 19:18:00 +02003031 /* In Replace mode when a $ is displayed at the end of the line only
3032 * part of the screen would be updated. We do need to redraw here. */
3033 dollar_vcol = -1;
3034
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003035 /* Compute the screen column of the start of the completed text.
3036 * Use the cursor to get all wrapping and other settings right. */
3037 col = curwin->w_cursor.col;
3038 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003039 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003040 curwin->w_cursor.col = col;
3041 }
3042}
3043
Bram Moolenaar071d4272004-06-13 20:20:40 +00003044#define DICT_FIRST (1) /* use just first element in "dict" */
3045#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003046
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003048 * Add any identifiers that match the given pattern in the list of dictionary
3049 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 */
3051 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00003052ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
3053 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003055 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003056 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003057{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003058 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 char_u *ptr;
3060 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003062 char_u **files;
3063 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003065 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003066
Bram Moolenaar0b238792006-03-02 22:49:12 +00003067 if (*dict == NUL)
3068 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003069#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003070 /* When 'dictionary' is empty and spell checking is enabled use
3071 * "spell". */
3072 if (!thesaurus && curwin->w_p_spell)
3073 dict = (char_u *)"spell";
3074 else
3075#endif
3076 return;
3077 }
3078
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003080 if (buf == NULL)
3081 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003082 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 /* If 'infercase' is set, don't use 'smartcase' here */
3085 save_p_scs = p_scs;
3086 if (curbuf->b_p_inf)
3087 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003088
3089 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3090 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003091 * pattern. Also need to double backslashes. */
Bram Moolenaare4214502015-03-05 18:08:43 +01003092 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003093 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003094 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003095 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003096
3097 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003098 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003099 len = STRLEN(pat_esc) + 10;
3100 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003101 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003102 {
3103 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003104 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003105 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003106 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003107 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3108 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003109 vim_free(ptr);
3110 }
3111 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003112 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003113 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003114 if (regmatch.regprog == NULL)
3115 goto theend;
3116 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003117
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3119 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003120 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 {
3122 /* copy one dictionary file name into buf */
3123 if (flags == DICT_EXACT)
3124 {
3125 count = 1;
3126 files = &dict;
3127 }
3128 else
3129 {
3130 /* Expand wildcards in the dictionary name, but do not allow
3131 * backticks (for security, the 'dict' option may have been set in
3132 * a modeline). */
3133 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003134# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003135 if (!thesaurus && STRCMP(buf, "spell") == 0)
3136 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003137 else
3138# endif
3139 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 || expand_wildcards(1, &buf, &count, &files,
3141 EW_FILE|EW_SILENT) != OK)
3142 count = 0;
3143 }
3144
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003145# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003146 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003148 /* Complete from active spelling. Skip "\<" in the pattern, we
3149 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003150 if (pat[0] == '\\' && pat[1] == '<')
3151 ptr = pat + 2;
3152 else
3153 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003154 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003156 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003157# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003158 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003159 {
3160 ins_compl_files(count, files, thesaurus, flags,
3161 &regmatch, buf, &dir);
3162 if (flags != DICT_EXACT)
3163 FreeWild(count, files);
3164 }
3165 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 break;
3167 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003168
3169theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003171 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 vim_free(buf);
3173}
3174
Bram Moolenaar0b238792006-03-02 22:49:12 +00003175 static void
3176ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3177 int count;
3178 char_u **files;
3179 int thesaurus;
3180 int flags;
3181 regmatch_T *regmatch;
3182 char_u *buf;
3183 int *dir;
3184{
3185 char_u *ptr;
3186 int i;
3187 FILE *fp;
3188 int add_r;
3189
3190 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3191 {
3192 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3193 if (flags != DICT_EXACT)
3194 {
3195 vim_snprintf((char *)IObuff, IOSIZE,
3196 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003197 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003198 }
3199
3200 if (fp != NULL)
3201 {
3202 /*
3203 * Read dictionary file line by line.
3204 * Check each line for a match.
3205 */
3206 while (!got_int && !compl_interrupted
3207 && !vim_fgets(buf, LSIZE, fp))
3208 {
3209 ptr = buf;
3210 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3211 {
3212 ptr = regmatch->startp[0];
Bram Moolenaare4214502015-03-05 18:08:43 +01003213 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar0b238792006-03-02 22:49:12 +00003214 ptr = find_line_end(ptr);
3215 else
3216 ptr = find_word_end(ptr);
3217 add_r = ins_compl_add_infercase(regmatch->startp[0],
3218 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003219 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003220 if (thesaurus)
3221 {
3222 char_u *wstart;
3223
3224 /*
3225 * Add the other matches on the line
3226 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003227 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003228 while (!got_int)
3229 {
3230 /* Find start of the next word. Skip white
3231 * space and punctuation. */
3232 ptr = find_word_start(ptr);
3233 if (*ptr == NUL || *ptr == NL)
3234 break;
3235 wstart = ptr;
3236
Bram Moolenaara2993e12007-08-12 14:38:46 +00003237 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003238#ifdef FEAT_MBYTE
3239 if (has_mbyte)
3240 /* Japanese words may have characters in
3241 * different classes, only separate words
3242 * with single-byte non-word characters. */
3243 while (*ptr != NUL)
3244 {
3245 int l = (*mb_ptr2len)(ptr);
3246
3247 if (l < 2 && !vim_iswordc(*ptr))
3248 break;
3249 ptr += l;
3250 }
3251 else
3252#endif
3253 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003254
3255 /* Add the word. Skip the regexp match. */
3256 if (wstart != regmatch->startp[0])
3257 add_r = ins_compl_add_infercase(wstart,
3258 (int)(ptr - wstart),
3259 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003260 }
3261 }
3262 if (add_r == OK)
3263 /* if dir was BACKWARD then honor it just once */
3264 *dir = FORWARD;
3265 else if (add_r == FAIL)
3266 break;
3267 /* avoid expensive call to vim_regexec() when at end
3268 * of line */
3269 if (*ptr == '\n' || got_int)
3270 break;
3271 }
3272 line_breakcheck();
3273 ins_compl_check_keys(50);
3274 }
3275 fclose(fp);
3276 }
3277 }
3278}
3279
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280/*
3281 * Find the start of the next word.
3282 * Returns a pointer to the first char of the word. Also stops at a NUL.
3283 */
3284 char_u *
3285find_word_start(ptr)
3286 char_u *ptr;
3287{
3288#ifdef FEAT_MBYTE
3289 if (has_mbyte)
3290 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003291 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 else
3293#endif
3294 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3295 ++ptr;
3296 return ptr;
3297}
3298
3299/*
3300 * Find the end of the word. Assumes it starts inside a word.
3301 * Returns a pointer to just after the word.
3302 */
3303 char_u *
3304find_word_end(ptr)
3305 char_u *ptr;
3306{
3307#ifdef FEAT_MBYTE
3308 int start_class;
3309
3310 if (has_mbyte)
3311 {
3312 start_class = mb_get_class(ptr);
3313 if (start_class > 1)
3314 while (*ptr != NUL)
3315 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003316 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 if (mb_get_class(ptr) != start_class)
3318 break;
3319 }
3320 }
3321 else
3322#endif
3323 while (vim_iswordc(*ptr))
3324 ++ptr;
3325 return ptr;
3326}
3327
3328/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003329 * Find the end of the line, omitting CR and NL at the end.
3330 * Returns a pointer to just after the line.
3331 */
3332 static char_u *
3333find_line_end(ptr)
3334 char_u *ptr;
3335{
3336 char_u *s;
3337
3338 s = ptr + STRLEN(ptr);
3339 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3340 --s;
3341 return s;
3342}
3343
3344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 * Free the list of completions
3346 */
3347 static void
3348ins_compl_free()
3349{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003350 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003351 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003352
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003353 vim_free(compl_pattern);
3354 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003355 vim_free(compl_leader);
3356 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003358 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003360
3361 ins_compl_del_pum();
3362 pum_clear();
3363
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003364 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003365 do
3366 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003367 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003368 compl_curr_match = compl_curr_match->cp_next;
3369 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003371 if (match->cp_flags & FREE_FNAME)
3372 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003373 for (i = 0; i < CPT_COUNT; ++i)
3374 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003375 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003376 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3377 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003378 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003379}
3380
3381 static void
3382ins_compl_clear()
3383{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003384 compl_cont_status = 0;
3385 compl_started = FALSE;
3386 compl_matches = 0;
3387 vim_free(compl_pattern);
3388 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003389 vim_free(compl_leader);
3390 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003392 vim_free(compl_orig_text);
3393 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003394 compl_enter_selects = FALSE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02003395 /* clear v:completed_item */
3396 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397}
3398
3399/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003400 * Return TRUE when Insert completion is active.
3401 */
3402 int
3403ins_compl_active()
3404{
3405 return compl_started;
3406}
3407
3408/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003409 * Delete one character before the cursor and show the subset of the matches
3410 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003411 * Returns the character to be used, NUL if the work is done and another char
3412 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003413 */
3414 static int
3415ins_compl_bs()
3416{
3417 char_u *line;
3418 char_u *p;
3419
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003420 line = ml_get_curline();
3421 p = line + curwin->w_cursor.col;
3422 mb_ptr_back(line, p);
3423
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003424 /* Stop completion when the whole word was deleted. For Omni completion
3425 * allow the word to be deleted, we won't match everything. */
3426 if ((int)(p - line) - (int)compl_col < 0
3427 || ((int)(p - line) - (int)compl_col == 0
Bram Moolenaare4214502015-03-05 18:08:43 +01003428 && ctrl_x_mode != CTRL_X_OMNI) || ctrl_x_mode == CTRL_X_EVAL)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003429 return K_BS;
3430
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003431 /* Deleted more than what was used to find matches or didn't finish
3432 * finding all matches: need to look for matches all over again. */
3433 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003434 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003435 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003436
Bram Moolenaara6557602006-02-04 22:43:20 +00003437 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003438 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003439 if (compl_leader != NULL)
3440 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003441 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003442 if (compl_shown_match != NULL)
3443 /* Make sure current match is not a hidden item. */
3444 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003445 return NUL;
3446 }
3447 return K_BS;
3448}
Bram Moolenaara6557602006-02-04 22:43:20 +00003449
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003450/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003451 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3452 * be called.
3453 */
3454 static int
3455ins_compl_need_restart()
3456{
3457 /* Return TRUE if we didn't complete finding matches or when the
3458 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3459 return compl_was_interrupted
3460 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3461 && compl_opt_refresh_always);
3462}
3463
3464/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003465 * Called after changing "compl_leader".
3466 * Show the popup menu with a different set of matches.
3467 * May also search for matches again if the previous search was interrupted.
3468 */
3469 static void
3470ins_compl_new_leader()
3471{
3472 ins_compl_del_pum();
3473 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003474 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003475 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003476
3477 if (compl_started)
3478 ins_compl_set_original_text(compl_leader);
3479 else
3480 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003481#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003482 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003483#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003484 /*
3485 * Matches were cleared, need to search for them now. First display
3486 * the changed text before the cursor. Set "compl_restarting" to
3487 * avoid that the first match is inserted.
3488 */
3489 update_screen(0);
3490#ifdef FEAT_GUI
3491 if (gui.in_use)
3492 {
3493 /* Show the cursor after the match, not after the redrawn text. */
3494 setcursor();
3495 out_flush();
3496 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003497 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003498#endif
3499 compl_restarting = TRUE;
3500 if (ins_complete(Ctrl_N) == FAIL)
3501 compl_cont_status = 0;
3502 compl_restarting = FALSE;
3503 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003504
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003505 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003506
3507 /* Show the popup menu with a different set of matches. */
3508 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003509
3510 /* Don't let Enter select the original text when there is no popup menu. */
3511 if (compl_match_array == NULL)
3512 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003513}
3514
3515/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003516 * Return the length of the completion, from the completion start column to
3517 * the cursor column. Making sure it never goes below zero.
3518 */
3519 static int
3520ins_compl_len()
3521{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003522 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003523
3524 if (off < 0)
3525 return 0;
3526 return off;
3527}
3528
3529/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003530 * Append one character to the match leader. May reduce the number of
3531 * matches.
3532 */
3533 static void
3534ins_compl_addleader(c)
3535 int c;
3536{
3537#ifdef FEAT_MBYTE
3538 int cc;
3539
3540 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3541 {
3542 char_u buf[MB_MAXBYTES + 1];
3543
3544 (*mb_char2bytes)(c, buf);
3545 buf[cc] = NUL;
3546 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003547 if (compl_opt_refresh_always)
3548 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003549 }
3550 else
3551#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003552 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003553 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003554 if (compl_opt_refresh_always)
3555 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003556 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003557
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003558 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003559 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003560 ins_compl_restart();
3561
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003562 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003563 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003564 * break redo. */
3565 if (!compl_opt_refresh_always)
3566 {
3567 vim_free(compl_leader);
3568 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003569 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003570 if (compl_leader != NULL)
3571 ins_compl_new_leader();
3572 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003573}
3574
3575/*
3576 * Setup for finding completions again without leaving CTRL-X mode. Used when
3577 * BS or a key was typed while still searching for matches.
3578 */
3579 static void
3580ins_compl_restart()
3581{
3582 ins_compl_free();
3583 compl_started = FALSE;
3584 compl_matches = 0;
3585 compl_cont_status = 0;
3586 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003587}
3588
3589/*
3590 * Set the first match, the original text.
3591 */
3592 static void
3593ins_compl_set_original_text(str)
3594 char_u *str;
3595{
3596 char_u *p;
3597
3598 /* Replace the original text entry. */
3599 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3600 {
3601 p = vim_strsave(str);
3602 if (p != NULL)
3603 {
3604 vim_free(compl_first_match->cp_str);
3605 compl_first_match->cp_str = p;
3606 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003607 }
3608}
3609
3610/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003611 * Append one character to the match leader. May reduce the number of
3612 * matches.
3613 */
3614 static void
3615ins_compl_addfrommatch()
3616{
3617 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003618 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003619 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003620 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003621
3622 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003623 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003624 {
3625 /* When still at the original match use the first entry that matches
3626 * the leader. */
3627 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3628 {
3629 p = NULL;
3630 for (cp = compl_shown_match->cp_next; cp != NULL
3631 && cp != compl_first_match; cp = cp->cp_next)
3632 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003633 if (compl_leader == NULL
3634 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003635 (int)STRLEN(compl_leader)))
3636 {
3637 p = cp->cp_str;
3638 break;
3639 }
3640 }
3641 if (p == NULL || (int)STRLEN(p) <= len)
3642 return;
3643 }
3644 else
3645 return;
3646 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003647 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003648 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003649 ins_compl_addleader(c);
3650}
3651
3652/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003654 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003655 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003657 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658ins_compl_prep(c)
3659 int c;
3660{
3661 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003663 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
3665 /* Forget any previous 'special' messages if this is actually
3666 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3667 */
3668 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3669 edit_submode_extra = NULL;
3670
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003671 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003672 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3673 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003674 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003676 /* Set "compl_get_longest" when finding the first matches. */
3677 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3678 || (ctrl_x_mode == 0 && !compl_started))
3679 {
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003680 compl_get_longest = (strstr((char *)p_cot, "longest") != NULL);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003681 compl_used_match = TRUE;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003682
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003683 }
3684
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02003685 compl_no_insert = FALSE;
3686 compl_no_select = FALSE;
3687 if (strstr((char *)p_cot, "noselect") != NULL)
3688 compl_no_select = TRUE;
3689 if (strstr((char *)p_cot, "noinsert") != NULL)
3690 compl_no_insert = TRUE;
3691
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3693 {
3694 /*
3695 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3696 * it will be yet. Now we decide.
3697 */
3698 switch (c)
3699 {
3700 case Ctrl_E:
3701 case Ctrl_Y:
3702 ctrl_x_mode = CTRL_X_SCROLL;
3703 if (!(State & REPLACE_FLAG))
3704 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3705 else
3706 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3707 edit_submode_pre = NULL;
3708 showmode();
3709 break;
3710 case Ctrl_L:
3711 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3712 break;
3713 case Ctrl_F:
3714 ctrl_x_mode = CTRL_X_FILES;
3715 break;
3716 case Ctrl_K:
3717 ctrl_x_mode = CTRL_X_DICTIONARY;
3718 break;
3719 case Ctrl_R:
3720 /* Simply allow ^R to happen without affecting ^X mode */
3721 break;
3722 case Ctrl_T:
3723 ctrl_x_mode = CTRL_X_THESAURUS;
3724 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003725#ifdef FEAT_COMPL_FUNC
3726 case Ctrl_U:
3727 ctrl_x_mode = CTRL_X_FUNCTION;
3728 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003729 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003730 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003731 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003732#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003733 case 's':
3734 case Ctrl_S:
3735 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003736#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003737 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003738 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003739 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003740#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003741 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 case Ctrl_RSB:
3743 ctrl_x_mode = CTRL_X_TAGS;
3744 break;
3745#ifdef FEAT_FIND_ID
3746 case Ctrl_I:
3747 case K_S_TAB:
3748 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3749 break;
3750 case Ctrl_D:
3751 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3752 break;
3753#endif
3754 case Ctrl_V:
3755 case Ctrl_Q:
3756 ctrl_x_mode = CTRL_X_CMDLINE;
3757 break;
3758 case Ctrl_P:
3759 case Ctrl_N:
3760 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3761 * just started ^X mode, or there were enough ^X's to cancel
3762 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3763 * do normal expansion when interrupting a different mode (say
3764 * ^X^F^X^P or ^P^X^X^P, see below)
3765 * nothing changes if interrupting mode 0, (eg, the flag
3766 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003767 if (!(compl_cont_status & CONT_INTRPT))
3768 compl_cont_status |= CONT_LOCAL;
3769 else if (compl_cont_mode != 0)
3770 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 /* FALLTHROUGH */
3772 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003773 /* If we have typed at least 2 ^X's... for modes != 0, we set
3774 * compl_cont_status = 0 (eg, as if we had just started ^X
3775 * mode).
3776 * For mode 0, we set "compl_cont_mode" to an impossible
3777 * value, in both cases ^X^X can be used to restart the same
3778 * mode (avoiding ADDING mode).
3779 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3780 * 'complete' and local ^P expansions respectively.
3781 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3782 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 if (c == Ctrl_X)
3784 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003785 if (compl_cont_mode != 0)
3786 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003788 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 }
3790 ctrl_x_mode = 0;
3791 edit_submode = NULL;
3792 showmode();
3793 break;
3794 }
3795 }
3796 else if (ctrl_x_mode != 0)
3797 {
3798 /* We're already in CTRL-X mode, do we stay in it? */
3799 if (!vim_is_ctrl_x_key(c))
3800 {
3801 if (ctrl_x_mode == CTRL_X_SCROLL)
3802 ctrl_x_mode = 0;
3803 else
3804 ctrl_x_mode = CTRL_X_FINISHED;
3805 edit_submode = NULL;
3806 }
3807 showmode();
3808 }
3809
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003810 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 {
3812 /* Show error message from attempted keyword completion (probably
3813 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003814 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003816 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3817 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 || ctrl_x_mode == CTRL_X_FINISHED)
3819 {
3820 /* Get here when we have finished typing a sequence of ^N and
3821 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003822 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003823 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003824 {
3825 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003826 * If any of the original typed text has been changed, eg when
3827 * ignorecase is set, we must add back-spaces to the redo
3828 * buffer. We add as few as necessary to delete just the part
3829 * of the original text that has changed.
3830 * When using the longest match, edited the match or used
3831 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003833 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3834 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003835 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003836 ptr = NULL;
3837 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838 }
3839
3840#ifdef FEAT_CINDENT
3841 want_cindent = (can_cindent && cindent_on());
3842#endif
3843 /*
3844 * When completing whole lines: fix indent for 'cindent'.
3845 * Otherwise, break line if it's too long.
3846 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003847 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003848 {
3849#ifdef FEAT_CINDENT
3850 /* re-indent the current line */
3851 if (want_cindent)
3852 {
3853 do_c_expr_indent();
3854 want_cindent = FALSE; /* don't do it again */
3855 }
3856#endif
3857 }
3858 else
3859 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003860 int prev_col = curwin->w_cursor.col;
3861
Bram Moolenaar071d4272004-06-13 20:20:40 +00003862 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003863 if (prev_col > 0)
3864 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 if (stop_arrow() == OK)
3866 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003867 if (prev_col > 0
3868 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3869 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 }
3871
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003872 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003873 * the selection without inserting anything. When
3874 * compl_enter_selects is set the Enter key does the same. */
3875 if ((c == Ctrl_Y || (compl_enter_selects
3876 && (c == CAR || c == K_KENTER || c == NL)))
3877 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003878 retval = TRUE;
3879
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003880 /* CTRL-E means completion is Ended, go back to the typed text. */
3881 if (c == Ctrl_E)
3882 {
3883 ins_compl_delete();
3884 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003885 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003886 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003887 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003888 retval = TRUE;
3889 }
3890
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003891 auto_format(FALSE, TRUE);
3892
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003894 compl_started = FALSE;
3895 compl_matches = 0;
Bram Moolenaarf1924a92014-07-16 14:42:46 +02003896 if (!shortmess(SHM_COMPLETIONMENU))
3897 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003899 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 if (edit_submode != NULL)
3901 {
3902 edit_submode = NULL;
3903 showmode();
3904 }
3905
3906#ifdef FEAT_CINDENT
3907 /*
3908 * Indent now if a key was typed that is in 'cinkeys'.
3909 */
3910 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3911 do_c_expr_indent();
3912#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003913#ifdef FEAT_AUTOCMD
3914 /* Trigger the CompleteDone event to give scripts a chance to act
3915 * upon the completion. */
3916 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 }
3919 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003920#ifdef FEAT_AUTOCMD
3921 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3922 /* Trigger the CompleteDone event to give scripts a chance to act
3923 * upon the (possibly failed) completion. */
3924 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3925#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926
3927 /* reset continue_* if we left expansion-mode, if we stay they'll be
3928 * (re)set properly in ins_complete() */
3929 if (!vim_is_ctrl_x_key(c))
3930 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003931 compl_cont_status = 0;
3932 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003934
3935 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003936}
3937
3938/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003939 * Fix the redo buffer for the completion leader replacing some of the typed
3940 * text. This inserts backspaces and appends the changed text.
3941 * "ptr" is the known leader text or NUL.
3942 */
3943 static void
3944ins_compl_fixRedoBufForLeader(ptr_arg)
3945 char_u *ptr_arg;
3946{
3947 int len;
3948 char_u *p;
3949 char_u *ptr = ptr_arg;
3950
3951 if (ptr == NULL)
3952 {
3953 if (compl_leader != NULL)
3954 ptr = compl_leader;
3955 else
3956 return; /* nothing to do */
3957 }
3958 if (compl_orig_text != NULL)
3959 {
3960 p = compl_orig_text;
3961 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3962 ;
3963#ifdef FEAT_MBYTE
3964 if (len > 0)
3965 len -= (*mb_head_off)(p, p + len);
3966#endif
3967 for (p += len; *p != NUL; mb_ptr_adv(p))
3968 AppendCharToRedobuff(K_BS);
3969 }
3970 else
3971 len = 0;
3972 if (ptr != NULL)
3973 AppendToRedobuffLit(ptr + len, -1);
3974}
3975
3976/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3978 * (depending on flag) starting from buf and looking for a non-scanned
3979 * buffer (other than curbuf). curbuf is special, if it is called with
3980 * buf=curbuf then it has to be the first call for a given flag/expansion.
3981 *
3982 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3983 */
3984 static buf_T *
3985ins_compl_next_buf(buf, flag)
3986 buf_T *buf;
3987 int flag;
3988{
3989#ifdef FEAT_WINDOWS
3990 static win_T *wp;
3991#endif
3992
3993 if (flag == 'w') /* just windows */
3994 {
3995#ifdef FEAT_WINDOWS
3996 if (buf == curbuf) /* first call for this flag/expansion */
3997 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003998 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 && wp->w_buffer->b_scanned)
4000 ;
4001 buf = wp->w_buffer;
4002#else
4003 buf = curbuf;
4004#endif
4005 }
4006 else
4007 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
4008 * (unlisted buffers)
4009 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00004010 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 && ((flag == 'U'
4012 ? buf->b_p_bl
4013 : (!buf->b_p_bl
4014 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004015 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 ;
4017 return buf;
4018}
4019
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004020#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004021static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004022
4023/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004024 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00004025 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004026 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004027 static void
4028expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004029 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004030 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004031{
Bram Moolenaar82139082011-09-14 16:52:09 +02004032 list_T *matchlist = NULL;
4033 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004034 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00004035 char_u *funcname;
4036 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004037 win_T *curwin_save;
4038 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004039 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004040
Bram Moolenaare344bea2005-09-01 20:46:49 +00004041 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4042 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004043 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004044
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004045 /* Call 'completefunc' to obtain the list of matches. */
4046 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004047 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004048
Bram Moolenaare344bea2005-09-01 20:46:49 +00004049 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004050 curwin_save = curwin;
4051 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004052
4053 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004054 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004055 {
4056 switch (rettv.v_type)
4057 {
4058 case VAR_LIST:
4059 matchlist = rettv.vval.v_list;
4060 break;
4061 case VAR_DICT:
4062 matchdict = rettv.vval.v_dict;
4063 break;
4064 default:
4065 /* TODO: Give error message? */
4066 clear_tv(&rettv);
4067 break;
4068 }
4069 }
4070
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004071 if (curwin_save != curwin || curbuf_save != curbuf)
4072 {
4073 EMSG(_(e_complwin));
4074 goto theend;
4075 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004076 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02004077 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004078 if (!equalpos(curwin->w_cursor, pos))
4079 {
4080 EMSG(_(e_compldel));
4081 goto theend;
4082 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004083
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004084 if (matchlist != NULL)
4085 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004086 else if (matchdict != NULL)
4087 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004088
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004089theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004090 if (matchdict != NULL)
4091 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004092 if (matchlist != NULL)
4093 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004094}
4095#endif /* FEAT_COMPL_FUNC */
4096
Bram Moolenaar39f05632006-03-19 22:15:26 +00004097#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004098/*
4099 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004100 */
4101 static void
4102ins_compl_add_list(list)
4103 list_T *list;
4104{
4105 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004106 int dir = compl_direction;
4107
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004108 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004109 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004110 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004111 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4112 /* if dir was BACKWARD then honor it just once */
4113 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004114 else if (did_emsg)
4115 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004116 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004117}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004118
4119/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004120 * Add completions from a dict.
4121 */
4122 static void
4123ins_compl_add_dict(dict)
4124 dict_T *dict;
4125{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004126 dictitem_T *di_refresh;
4127 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004128
4129 /* Check for optional "refresh" item. */
4130 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004131 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4132 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004133 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004134 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004135
4136 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4137 compl_opt_refresh_always = TRUE;
4138 }
4139
4140 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004141 di_words = dict_find(dict, (char_u *)"words", 5);
4142 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4143 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004144}
4145
4146/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004147 * Add a match to the list of matches from a typeval_T.
4148 * If the given string is already in the list of completions, then return
4149 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4150 * maybe because alloc() returns NULL, then FAIL is returned.
4151 */
4152 int
4153ins_compl_add_tv(tv, dir)
4154 typval_T *tv;
4155 int dir;
4156{
4157 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004158 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004159 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004160 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004161 char_u *(cptext[CPT_COUNT]);
4162
4163 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4164 {
4165 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4166 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4167 (char_u *)"abbr", FALSE);
4168 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4169 (char_u *)"menu", FALSE);
4170 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4171 (char_u *)"kind", FALSE);
4172 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4173 (char_u *)"info", FALSE);
4174 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4175 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004176 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004177 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004178 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4179 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004180 }
4181 else
4182 {
4183 word = get_tv_string_chk(tv);
4184 vim_memset(cptext, 0, sizeof(cptext));
4185 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004186 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004187 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004188 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004189}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004190#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004192/*
4193 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004194 * The search starts at position "ini" in curbuf and in the direction
4195 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004196 * When "compl_started" is FALSE start at that position, otherwise continue
4197 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004198 * This may return before finding all the matches.
4199 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 */
4201 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004202ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204{
4205 static pos_T first_match_pos;
4206 static pos_T last_match_pos;
4207 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004208 static int found_all = FALSE; /* Found all matches of a
4209 certain type. */
4210 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211
Bram Moolenaar572cb562005-08-05 21:35:02 +00004212 pos_T *pos;
4213 char_u **matches;
4214 int save_p_scs;
4215 int save_p_ws;
4216 int save_p_ic;
4217 int i;
4218 int num_matches;
4219 int len;
4220 int found_new_match;
4221 int type = ctrl_x_mode;
4222 char_u *ptr;
4223 char_u *dict = NULL;
4224 int dict_f = 0;
4225 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004226 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004228 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 {
4230 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4231 ins_buf->b_scanned = 0;
4232 found_all = FALSE;
4233 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004234 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004235 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004236 last_match_pos = first_match_pos = *ini;
4237 }
4238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004239 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004240 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4242 for (;;)
4243 {
4244 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004245 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004247 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 * or if found_all says this entry is done. For ^X^L only use the
4249 * entries from 'complete' that look in loaded buffers. */
Bram Moolenaare4214502015-03-05 18:08:43 +01004250 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 {
4253 found_all = FALSE;
4254 while (*e_cpt == ',' || *e_cpt == ' ')
4255 e_cpt++;
4256 if (*e_cpt == '.' && !curbuf->b_scanned)
4257 {
4258 ins_buf = curbuf;
4259 first_match_pos = *ini;
4260 /* So that ^N can match word immediately after cursor */
4261 if (ctrl_x_mode == 0)
4262 dec(&first_match_pos);
4263 last_match_pos = first_match_pos;
4264 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004265
4266 /* Remember the first match so that the loop stops when we
4267 * wrap and come back there a second time. */
4268 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4271 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4272 {
4273 /* Scan a buffer, but not the current one. */
4274 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4275 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004276 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 first_match_pos.col = last_match_pos.col = 0;
4278 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4279 last_match_pos.lnum = 0;
4280 type = 0;
4281 }
4282 else /* unloaded buffer, scan like dictionary */
4283 {
4284 found_all = TRUE;
4285 if (ins_buf->b_fname == NULL)
4286 continue;
4287 type = CTRL_X_DICTIONARY;
4288 dict = ins_buf->b_fname;
4289 dict_f = DICT_EXACT;
4290 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004291 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 ins_buf->b_fname == NULL
4293 ? buf_spname(ins_buf)
4294 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004295 ? ins_buf->b_fname
4296 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004297 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 else if (*e_cpt == NUL)
4300 break;
4301 else
4302 {
Bram Moolenaare4214502015-03-05 18:08:43 +01004303 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 type = -1;
4305 else if (*e_cpt == 'k' || *e_cpt == 's')
4306 {
4307 if (*e_cpt == 'k')
4308 type = CTRL_X_DICTIONARY;
4309 else
4310 type = CTRL_X_THESAURUS;
4311 if (*++e_cpt != ',' && *e_cpt != NUL)
4312 {
4313 dict = e_cpt;
4314 dict_f = DICT_FIRST;
4315 }
4316 }
4317#ifdef FEAT_FIND_ID
4318 else if (*e_cpt == 'i')
4319 type = CTRL_X_PATH_PATTERNS;
4320 else if (*e_cpt == 'd')
4321 type = CTRL_X_PATH_DEFINES;
4322#endif
4323 else if (*e_cpt == ']' || *e_cpt == 't')
4324 {
4325 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004326 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004327 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
4329 else
4330 type = -1;
4331
4332 /* in any case e_cpt is advanced to the next entry */
4333 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4334
4335 found_all = TRUE;
4336 if (type == -1)
4337 continue;
4338 }
4339 }
4340
4341 switch (type)
4342 {
4343 case -1:
4344 break;
4345#ifdef FEAT_FIND_ID
4346 case CTRL_X_PATH_PATTERNS:
4347 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004348 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004349 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004351 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4353 (linenr_T)1, (linenr_T)MAXLNUM);
4354 break;
4355#endif
4356
4357 case CTRL_X_DICTIONARY:
4358 case CTRL_X_THESAURUS:
4359 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004360 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 : (type == CTRL_X_THESAURUS
4362 ? (*curbuf->b_p_tsr == NUL
4363 ? p_tsr
4364 : curbuf->b_p_tsr)
4365 : (*curbuf->b_p_dict == NUL
4366 ? p_dict
4367 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004368 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004369 dict != NULL ? dict_f
4370 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 dict = NULL;
4372 break;
4373
4374 case CTRL_X_TAGS:
4375 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4376 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004377 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004379 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380 * of matches is found when compl_pattern is empty */
4381 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4383 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4384 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4385 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004386 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 }
4388 p_ic = save_p_ic;
4389 break;
4390
4391 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4394 {
4395
4396 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004397 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004398 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 }
4400 break;
4401
4402 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 if (expand_cmdline(&compl_xp, compl_pattern,
4404 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004406 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 break;
4408
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004409#ifdef FEAT_COMPL_FUNC
4410 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004411 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004412 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004413 break;
4414#endif
4415
Bram Moolenaar488c6512005-08-11 20:09:58 +00004416 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004417#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004418 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004419 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004420 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004421 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004422#endif
4423 break;
4424
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 default: /* normal ^P/^N and ^X^L */
4426 /*
4427 * If 'infercase' is set, don't use 'smartcase' here
4428 */
4429 save_p_scs = p_scs;
4430 if (ins_buf->b_p_inf)
4431 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004432
Bram Moolenaar361aa502014-01-23 22:45:58 +01004433 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 * end but never from the middle, thus setting nowrapscan in this
4435 * buffers is a good idea, on the other hand, we always set
4436 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4437 save_p_ws = p_ws;
4438 if (ins_buf != curbuf)
4439 p_ws = FALSE;
4440 else if (*e_cpt == '.')
4441 p_ws = TRUE;
4442 for (;;)
4443 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004444 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004446 ++msg_silent; /* Don't want messages for wrapscan. */
4447
Bram Moolenaare4214502015-03-05 18:08:43 +01004448 /* CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
4449 * || word-wise search that
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004450 * has added a word that was at the beginning of the line */
Bram Moolenaare4214502015-03-05 18:08:43 +01004451 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004452 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004454 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004456 found_new_match = searchit(NULL, ins_buf, pos,
4457 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004458 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004459 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004460 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004461 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004463 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004464 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 first_match_pos = *pos;
4466 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004467 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 }
4469 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004470 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 found_new_match = FAIL;
4472 if (found_new_match == FAIL)
4473 {
4474 if (ins_buf == curbuf)
4475 found_all = TRUE;
4476 break;
4477 }
4478
4479 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 && ini->lnum == pos->lnum
4482 && ini->col == pos->col)
4483 continue;
4484 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
Bram Moolenaare4214502015-03-05 18:08:43 +01004485 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004487 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 {
4489 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4490 continue;
4491 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4492 if (!p_paste)
4493 ptr = skipwhite(ptr);
4494 }
4495 len = (int)STRLEN(ptr);
4496 }
4497 else
4498 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004499 char_u *tmp_ptr = ptr;
4500
4501 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004503 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 /* Skip if already inside a word. */
4505 if (vim_iswordp(tmp_ptr))
4506 continue;
4507 /* Find start of next word. */
4508 tmp_ptr = find_word_start(tmp_ptr);
4509 }
4510 /* Find end of this word. */
4511 tmp_ptr = find_word_end(tmp_ptr);
4512 len = (int)(tmp_ptr - ptr);
4513
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514 if ((compl_cont_status & CONT_ADDING)
4515 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 {
4517 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4518 {
4519 /* Try next line, if any. the new word will be
4520 * "join" as if the normal command "J" was used.
4521 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004522 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 * works -- Acevedo */
4524 STRNCPY(IObuff, ptr, len);
4525 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4526 tmp_ptr = ptr = skipwhite(ptr);
4527 /* Find start of next word. */
4528 tmp_ptr = find_word_start(tmp_ptr);
4529 /* Find end of next word. */
4530 tmp_ptr = find_word_end(tmp_ptr);
4531 if (tmp_ptr > ptr)
4532 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004533 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004535 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 IObuff[len++] = ' ';
4537 /* IObuf =~ "\k.* ", thus len >= 2 */
4538 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004539 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 || (vim_strchr(p_cpo, CPO_JOINSP)
4541 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004542 && (IObuff[len - 2] == '?'
4543 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 IObuff[len++] = ' ';
4545 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004546 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547 if (tmp_ptr - ptr >= IOSIZE - len)
4548 tmp_ptr = ptr + IOSIZE - len - 1;
4549 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4550 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004551 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004552 }
4553 IObuff[len] = NUL;
4554 ptr = IObuff;
4555 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004556 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 continue;
4558 }
4559 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004560 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004561 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004562 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 {
4564 found_new_match = OK;
4565 break;
4566 }
4567 }
4568 p_scs = save_p_scs;
4569 p_ws = save_p_ws;
4570 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004571
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004572 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004573 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004574 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 found_new_match = OK;
4576
4577 /* break the loop for specialized modes (use 'complete' just for the
4578 * generic ctrl_x_mode == 0) or when we've found a new match */
Bram Moolenaare4214502015-03-05 18:08:43 +01004579 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004580 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004581 {
4582 if (got_int)
4583 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004584 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004585 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004586 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004587
Bram Moolenaare4214502015-03-05 18:08:43 +01004588 if ((ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004589 || compl_interrupted)
4590 break;
4591 compl_started = TRUE;
4592 }
4593 else
4594 {
4595 /* Mark a buffer scanned when it has been scanned completely */
4596 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4597 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004598
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004599 compl_started = FALSE;
4600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004602 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603
Bram Moolenaare4214502015-03-05 18:08:43 +01004604 if ((ctrl_x_mode == 0 || CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 && *e_cpt == NUL) /* Got to end of 'complete' */
4606 found_new_match = FAIL;
4607
4608 i = -1; /* total of matches, unknown */
4609 if (found_new_match == FAIL
Bram Moolenaare4214502015-03-05 18:08:43 +01004610 || (ctrl_x_mode != 0 && !CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 i = ins_compl_make_cyclic();
4612
4613 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614 * just been made cyclic then we have to move compl_curr_match to the next
4615 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004616 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4617 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618 if (compl_curr_match == NULL)
4619 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 return i;
4621}
4622
4623/* Delete the old text being completed. */
4624 static void
4625ins_compl_delete()
4626{
4627 int i;
4628
4629 /*
4630 * In insert mode: Delete the typed part.
4631 * In replace mode: Put the old characters back, if any.
4632 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004633 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 backspace_until_column(i);
Bram Moolenaarf1924a92014-07-16 14:42:46 +02004635
Bram Moolenaar674fffe2014-07-23 13:50:46 +02004636 /* TODO: is this sufficient for redrawing? Redrawing everything causes
4637 * flicker, thus we can't do that. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 changed_cline_bef_curs();
Bram Moolenaar42a45122015-07-10 17:56:23 +02004639 /* clear v:completed_item */
4640 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641}
4642
4643/* Insert the new text being completed. */
4644 static void
4645ins_compl_insert()
4646{
Bram Moolenaar42a45122015-07-10 17:56:23 +02004647 dict_T *dict;
4648
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004649 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004650 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4651 compl_used_match = FALSE;
4652 else
4653 compl_used_match = TRUE;
Bram Moolenaar42a45122015-07-10 17:56:23 +02004654
4655 /* Set completed item. */
4656 /* { word, abbr, menu, kind, info } */
4657 dict = dict_alloc();
4658 if (dict != NULL)
4659 {
4660 dict_add_nr_str(dict, "word", 0L,
4661 EMPTY_IF_NULL(compl_shown_match->cp_str));
4662 dict_add_nr_str(dict, "abbr", 0L,
4663 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_ABBR]));
4664 dict_add_nr_str(dict, "menu", 0L,
4665 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_MENU]));
4666 dict_add_nr_str(dict, "kind", 0L,
4667 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_KIND]));
4668 dict_add_nr_str(dict, "info", 0L,
4669 EMPTY_IF_NULL(compl_shown_match->cp_text[CPT_INFO]));
4670 }
4671 set_vim_var_dict(VV_COMPLETED_ITEM, dict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672}
4673
4674/*
4675 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004676 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4677 * get more completions. If it is FALSE, then we just do nothing when there
4678 * are no more completions in a given direction. The latter case is used when
4679 * we are still in the middle of finding completions, to allow browsing
4680 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 * Return the total number of matches, or -1 if still unknown -- webb.
4682 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4684 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 *
4686 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004687 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4688 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 */
4690 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004691ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004693 int count; /* repeat completion this many times; should
4694 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004695 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004696{
4697 int num_matches = -1;
4698 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004699 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004700 compl_T *found_compl = NULL;
4701 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004702 int advance;
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004703 int started = compl_started;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004705 /* When user complete function return -1 for findstart which is next
4706 * time of 'always', compl_shown_match become NULL. */
4707 if (compl_shown_match == NULL)
4708 return -1;
4709
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004710 if (compl_leader != NULL
4711 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004713 /* Set "compl_shown_match" to the actually shown match, it may differ
4714 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004715 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004716 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004717 && compl_shown_match->cp_next != NULL
4718 && compl_shown_match->cp_next != compl_first_match)
4719 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004720
4721 /* If we didn't find it searching forward, and compl_shows_dir is
4722 * backward, find the last match. */
4723 if (compl_shows_dir == BACKWARD
4724 && !ins_compl_equal(compl_shown_match,
4725 compl_leader, (int)STRLEN(compl_leader))
4726 && (compl_shown_match->cp_next == NULL
4727 || compl_shown_match->cp_next == compl_first_match))
4728 {
4729 while (!ins_compl_equal(compl_shown_match,
4730 compl_leader, (int)STRLEN(compl_leader))
4731 && compl_shown_match->cp_prev != NULL
4732 && compl_shown_match->cp_prev != compl_first_match)
4733 compl_shown_match = compl_shown_match->cp_prev;
4734 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004735 }
4736
4737 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004738 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 /* Delete old text to be replaced */
4740 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004741
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004742 /* When finding the longest common text we stick at the original text,
4743 * don't let CTRL-N or CTRL-P move to the first match. */
4744 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4745
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004746 /* When restarting the search don't insert the first match either. */
4747 if (compl_restarting)
4748 {
4749 advance = FALSE;
4750 compl_restarting = FALSE;
4751 }
4752
Bram Moolenaare3226be2005-12-18 22:10:00 +00004753 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4754 * around. */
4755 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004757 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004759 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004760 found_end = (compl_first_match != NULL
4761 && (compl_shown_match->cp_next == compl_first_match
4762 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004763 }
4764 else if (compl_shows_dir == BACKWARD
4765 && compl_shown_match->cp_prev != NULL)
4766 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004767 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004768 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004769 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 }
4771 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004772 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004773 if (!allow_get_expansion)
4774 {
4775 if (advance)
4776 {
4777 if (compl_shows_dir == BACKWARD)
4778 compl_pending -= todo + 1;
4779 else
4780 compl_pending += todo + 1;
4781 }
4782 return -1;
4783 }
4784
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004785 if (!compl_no_select && advance)
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004786 {
4787 if (compl_shows_dir == BACKWARD)
4788 --compl_pending;
4789 else
4790 ++compl_pending;
4791 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004792
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004793 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004794 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004795
4796 /* handle any pending completions */
4797 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004798 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004799 {
4800 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4801 {
4802 compl_shown_match = compl_shown_match->cp_next;
4803 --compl_pending;
4804 }
4805 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4806 {
4807 compl_shown_match = compl_shown_match->cp_prev;
4808 ++compl_pending;
4809 }
4810 else
4811 break;
4812 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004813 found_end = FALSE;
4814 }
4815 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4816 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004817 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004818 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004819 ++todo;
4820 else
4821 /* Remember a matching item. */
4822 found_compl = compl_shown_match;
4823
4824 /* Stop at the end of the list when we found a usable match. */
4825 if (found_end)
4826 {
4827 if (found_compl != NULL)
4828 {
4829 compl_shown_match = found_compl;
4830 break;
4831 }
4832 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
4835
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004836 /* Insert the text of the new completion, or the compl_leader. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004837 if (compl_no_insert && !started)
4838 {
4839 ins_bytes(compl_orig_text + ins_compl_len());
4840 compl_used_match = FALSE;
4841 }
4842 else if (insert_match)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004843 {
4844 if (!compl_get_longest || compl_used_match)
4845 ins_compl_insert();
4846 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004847 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004848 }
4849 else
4850 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851
4852 if (!allow_get_expansion)
4853 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004854 /* may undisplay the popup menu first */
4855 ins_compl_upd_pum();
4856
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004857 /* redraw to show the user what was inserted */
4858 update_screen(0);
4859
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004860 /* display the updated popup menu */
4861 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004862#ifdef FEAT_GUI
4863 if (gui.in_use)
4864 {
4865 /* Show the cursor after the match, not after the redrawn text. */
4866 setcursor();
4867 out_flush();
4868 gui_update_cursor(FALSE, FALSE);
4869 }
4870#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004871
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 /* Delete old text to be replaced, since we're still searching and
4873 * don't want to match ourselves! */
4874 ins_compl_delete();
4875 }
4876
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004877 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004878 * menu is visible. */
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004879 if (compl_no_insert && !started)
4880 compl_enter_selects = TRUE;
4881 else
4882 compl_enter_selects = !insert_match && compl_match_array != NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004883
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 /*
4885 * Show the file name for the match (if any)
4886 * Truncate the file name to avoid a wait for return.
4887 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004888 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 {
4890 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004891 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892 if (i <= 0)
4893 i = 0;
4894 else
4895 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004896 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 msg(IObuff);
4898 redraw_cmdline = FALSE; /* don't overwrite! */
4899 }
4900
4901 return num_matches;
4902}
4903
4904/*
4905 * Call this while finding completions, to check whether the user has hit a key
4906 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004907 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004909 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 */
4911 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004912ins_compl_check_keys(frequency)
4913 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914{
4915 static int count = 0;
4916
4917 int c;
4918
4919 /* Don't check when reading keys from a script. That would break the test
4920 * scripts */
4921 if (using_script())
4922 return;
4923
4924 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004925 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return;
4927 count = 0;
4928
Bram Moolenaara260a972006-06-23 19:36:29 +00004929 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4930 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004931 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 if (c != NUL)
4933 {
4934 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4935 {
4936 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004937 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004938 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4939 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004941 else
4942 {
4943 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004944 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004945 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004946 if (c != K_IGNORE)
4947 {
4948 /* Don't interrupt completion when the character wasn't typed,
4949 * e.g., when doing @q to replay keys. */
4950 if (c != Ctrl_R && KeyTyped)
4951 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004952
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004953 vungetc(c);
4954 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004955 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
Bram Moolenaarb6be1e22015-07-10 18:18:40 +02004957 if (compl_pending != 0 && !got_int && !compl_no_insert)
Bram Moolenaara260a972006-06-23 19:36:29 +00004958 {
4959 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4960
4961 compl_pending = 0;
4962 (void)ins_compl_next(FALSE, todo, TRUE);
4963 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004964}
4965
4966/*
4967 * Decide the direction of Insert mode complete from the key typed.
4968 * Returns BACKWARD or FORWARD.
4969 */
4970 static int
4971ins_compl_key2dir(c)
4972 int c;
4973{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004974 if (c == Ctrl_P || c == Ctrl_L
4975 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4976 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004977 return BACKWARD;
4978 return FORWARD;
4979}
4980
4981/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004982 * Return TRUE for keys that are used for completion only when the popup menu
4983 * is visible.
4984 */
4985 static int
4986ins_compl_pum_key(c)
4987 int c;
4988{
4989 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004990 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4991 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004992}
4993
4994/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004995 * Decide the number of completions to move forward.
4996 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4997 */
4998 static int
4999ins_compl_key2count(c)
5000 int c;
5001{
5002 int h;
5003
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005004 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00005005 {
5006 h = pum_get_height();
5007 if (h > 3)
5008 h -= 2; /* keep some context */
5009 return h;
5010 }
5011 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005012}
5013
5014/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005015 * Return TRUE if completion with "c" should insert the match, FALSE if only
5016 * to change the currently selected completion.
5017 */
5018 static int
5019ins_compl_use_match(c)
5020 int c;
5021{
5022 switch (c)
5023 {
5024 case K_UP:
5025 case K_DOWN:
5026 case K_PAGEDOWN:
5027 case K_KPAGEDOWN:
5028 case K_S_DOWN:
5029 case K_PAGEUP:
5030 case K_KPAGEUP:
5031 case K_S_UP:
5032 return FALSE;
5033 }
5034 return TRUE;
5035}
5036
5037/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 * Do Insert mode completion.
5039 * Called when character "c" was typed, which has a meaning for completion.
5040 * Returns OK if completion was done, FAIL if something failed (out of mem).
5041 */
5042 static int
5043ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005044 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005046 char_u *line;
5047 int startcol = 0; /* column where searched text starts */
5048 colnr_T curs_col; /* cursor column */
5049 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005050 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051
Bram Moolenaare3226be2005-12-18 22:10:00 +00005052 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005053 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 {
5055 /* First time we hit ^N or ^P (in a row, I mean) */
5056
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 did_ai = FALSE;
5058#ifdef FEAT_SMARTINDENT
5059 did_si = FALSE;
5060 can_si = FALSE;
5061 can_si_back = FALSE;
5062#endif
5063 if (stop_arrow() == FAIL)
5064 return FAIL;
5065
5066 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005067 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00005068 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005070 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005071 * "compl_startpos" to the cursor as a pattern to add a new word
5072 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005073 * "compl_startpos" is not in the same line as the cursor then fix it
5074 * (the line has been split because it was longer than 'tw'). if SOL
5075 * is set then skip the previous pattern, a word at the beginning of
5076 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005077 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5078 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 /*
5081 * it is a continued search
5082 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005083 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5085 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5086 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005087 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005089 /* line (probably) wrapped, set compl_startpos to the
5090 * first non_blank in the line, if it is not a wordchar
5091 * include it to get a better pattern, but then we don't
5092 * want the "\\<" prefix, check it bellow */
5093 compl_col = (colnr_T)(skipwhite(line) - line);
5094 compl_startpos.col = compl_col;
5095 compl_startpos.lnum = curwin->w_cursor.lnum;
5096 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 }
5098 else
5099 {
5100 /* S_IPOS was set when we inserted a word that was at the
5101 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 * mode but first we need to redefine compl_startpos */
5103 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 compl_cont_status |= CONT_SOL;
5106 compl_startpos.col = (colnr_T)(skipwhite(
5107 line + compl_length
5108 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005110 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005113 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005114 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005118 compl_cont_status &= ~CONT_SOL;
5119 compl_length = (IOSIZE - MIN_SPACE);
5120 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005122 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5123 if (compl_length < 1)
5124 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005126 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005127 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005132 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005134 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005136 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005138 compl_cont_status = 0;
5139 compl_cont_status |= CONT_N_ADDS;
5140 compl_startpos = curwin->w_cursor;
5141 startcol = (int)curs_col;
5142 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 }
5144
5145 /* Work out completion pattern and original text -- webb */
5146 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5147 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005148 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5150 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005151 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005155 compl_col += ++startcol;
5156 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 }
5158 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 compl_pattern = str_foldcase(line + compl_col,
5160 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005162 compl_pattern = vim_strnsave(line + compl_col,
5163 compl_length);
5164 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 return FAIL;
5166 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
5169 char_u *prefix = (char_u *)"\\<";
5170
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005171 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005172 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005173 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005174 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 if (!vim_iswordp(line + compl_col)
5177 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 && (
5179#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005182 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183#endif
5184 )))
5185 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 STRCPY((char *)compl_pattern, prefix);
5187 (void)quote_meta(compl_pattern + STRLEN(prefix),
5188 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005192 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005194 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195#endif
5196 )
5197 {
5198 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005199 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5200 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005202 compl_col += curs_col;
5203 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 }
5205 else
5206 {
5207#ifdef FEAT_MBYTE
5208 /* Search the point of change class of multibyte character
5209 * or not a word single byte character backward. */
5210 if (has_mbyte)
5211 {
5212 int base_class;
5213 int head_off;
5214
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005215 startcol -= (*mb_head_off)(line, line + startcol);
5216 base_class = mb_get_class(line + startcol);
5217 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005219 head_off = (*mb_head_off)(line, line + startcol);
5220 if (base_class != mb_get_class(line + startcol
5221 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005223 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 }
5225 }
5226 else
5227#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005229 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005230 compl_col += ++startcol;
5231 compl_length = (int)curs_col - startcol;
5232 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 {
5234 /* Only match word with at least two chars -- webb
5235 * there's no need to call quote_meta,
5236 * alloc(7) is enough -- Acevedo
5237 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005238 compl_pattern = alloc(7);
5239 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005241 STRCPY((char *)compl_pattern, "\\<");
5242 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5243 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 }
5245 else
5246 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005247 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005248 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005249 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005250 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005251 STRCPY((char *)compl_pattern, "\\<");
5252 (void)quote_meta(compl_pattern + 2, line + compl_col,
5253 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254 }
5255 }
5256 }
Bram Moolenaare4214502015-03-05 18:08:43 +01005257 else if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005259 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005260 compl_length = (int)curs_col - (int)compl_col;
5261 if (compl_length < 0) /* cursor in indent: empty pattern */
5262 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264 compl_pattern = str_foldcase(line + compl_col, compl_length,
5265 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005267 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5268 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 return FAIL;
5270 }
5271 else if (ctrl_x_mode == CTRL_X_FILES)
5272 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005273 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005274 if (startcol > 0)
5275 {
5276 char_u *p = line + startcol;
5277
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005278 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005279 while (p > line && vim_isfilec(PTR2CHAR(p)))
5280 mb_ptr_back(line, p);
5281 if (p == line && vim_isfilec(PTR2CHAR(p)))
5282 startcol = 0;
5283 else
5284 startcol = (int)(p - line) + 1;
5285 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005286
Bram Moolenaar0300e462013-09-08 16:03:45 +02005287 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 compl_length = (int)curs_col - startcol;
5289 compl_pattern = addstar(line + compl_col, compl_length,
5290 EXPAND_FILES);
5291 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 return FAIL;
5293 }
5294 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5295 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 compl_pattern = vim_strnsave(line, curs_col);
5297 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005299 set_cmd_context(&compl_xp, compl_pattern,
5300 (int)STRLEN(compl_pattern), curs_col);
5301 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5302 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005303 /* No completion possible, use an empty pattern to get a
5304 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005305 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005306 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005307 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5308 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005310 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005311 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005312#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005313 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005314 * Call user defined function 'completefunc' with "a:findstart"
5315 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005316 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005317 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005318 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005319 char_u *funcname;
5320 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005321 win_T *curwin_save;
5322 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005323
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005324 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005325 * string */
5326 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5327 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5328 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005329 {
5330 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5331 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005332 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005333 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005334
5335 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005336 args[1] = NULL;
5337 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005338 curwin_save = curwin;
5339 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005340 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005341 if (curwin_save != curwin || curbuf_save != curbuf)
5342 {
5343 EMSG(_(e_complwin));
5344 return FAIL;
5345 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005346 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar834def32014-09-09 18:29:33 +02005347 validate_cursor();
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005348 if (!equalpos(curwin->w_cursor, pos))
5349 {
5350 EMSG(_(e_compldel));
5351 return FAIL;
5352 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005353
Bram Moolenaar6110a002012-01-26 18:58:38 +01005354 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005355 * cancel the complete without an error.
5356 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005357 if (col == -2)
5358 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005359 if (col == -3)
5360 {
5361 ctrl_x_mode = 0;
5362 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005363 if (!shortmess(SHM_COMPLETIONMENU))
5364 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005365 return FAIL;
5366 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005367
Bram Moolenaar82139082011-09-14 16:52:09 +02005368 /*
5369 * Reset extended parameters of completion, when start new
5370 * completion.
5371 */
5372 compl_opt_refresh_always = FALSE;
5373
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005374 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005375 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005376 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005377 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005378 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005379
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 /* Setup variables for completion. Need to obtain "line" again,
5381 * it may have become invalid. */
5382 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005383 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005384 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5385 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005386#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005387 return FAIL;
5388 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005389 else if (ctrl_x_mode == CTRL_X_SPELL)
5390 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005391#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005392 if (spell_bad_len > 0)
5393 compl_col = curs_col - spell_bad_len;
5394 else
5395 compl_col = spell_word_start(startcol);
5396 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005397 {
5398 compl_length = 0;
5399 compl_col = curs_col;
5400 }
5401 else
5402 {
5403 spell_expand_check_cap(compl_col);
5404 compl_length = (int)curs_col - compl_col;
5405 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005406 /* Need to obtain "line" again, it may have become invalid. */
5407 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005408 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5409 if (compl_pattern == NULL)
5410#endif
5411 return FAIL;
5412 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 else
5414 {
5415 EMSG2(_(e_intern2), "ins_complete()");
5416 return FAIL;
5417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005419 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005420 {
5421 edit_submode_pre = (char_u *)_(" Adding");
Bram Moolenaare4214502015-03-05 18:08:43 +01005422 if (CTRL_X_MODE_LINE_OR_EVAL(ctrl_x_mode))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 {
5424 /* Insert a new line, keep indentation but ignore 'comments' */
5425#ifdef FEAT_COMMENTS
5426 char_u *old = curbuf->b_p_com;
5427
5428 curbuf->b_p_com = (char_u *)"";
5429#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005430 compl_startpos.lnum = curwin->w_cursor.lnum;
5431 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 ins_eol('\r');
5433#ifdef FEAT_COMMENTS
5434 curbuf->b_p_com = old;
5435#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 compl_length = 0;
5437 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
5439 }
5440 else
5441 {
5442 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005443 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 }
5445
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005446 if (compl_cont_status & CONT_LOCAL)
5447 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 else
5449 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5450
Bram Moolenaar62951b12011-09-21 18:23:05 +02005451 /* If any of the original typed text has been changed we need to fix
5452 * the redo buffer. */
5453 ins_compl_fixRedoBufForLeader(NULL);
5454
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005455 /* Always add completion for the original text. */
5456 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005457 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5458 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005459 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005461 vim_free(compl_pattern);
5462 compl_pattern = NULL;
5463 vim_free(compl_orig_text);
5464 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 return FAIL;
5466 }
5467
5468 /* showmode might reset the internal line pointers, so it must
5469 * be called before line = ml_get(), or when this address is no
5470 * longer needed. -- Acevedo.
5471 */
5472 edit_submode_extra = (char_u *)_("-- Searching...");
5473 edit_submode_highl = HLF_COUNT;
5474 showmode();
5475 edit_submode_extra = NULL;
5476 out_flush();
5477 }
5478
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 compl_shown_match = compl_curr_match;
5480 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481
5482 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005483 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005485 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005486 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005488 /* may undisplay the popup menu */
5489 ins_compl_upd_pum();
5490
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005491 if (n > 1) /* all matches have been found */
5492 compl_matches = n;
5493 compl_curr_match = compl_shown_match;
5494 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495
Bram Moolenaard68071d2006-05-02 22:08:30 +00005496 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5497 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 if (got_int && !global_busy)
5499 {
5500 (void)vgetc();
5501 got_int = FALSE;
5502 }
5503
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005504 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005505 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005507 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5508 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5510 edit_submode_highl = HLF_E;
5511 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5512 * because we couldn't expand anything at first place, but if we used
5513 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5514 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005515 if ( compl_length > 1
5516 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 || (ctrl_x_mode != 0
5518 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5519 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005520 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 }
5522
Bram Moolenaar572cb562005-08-05 21:35:02 +00005523 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005524 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005526 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
5528 if (edit_submode_extra == NULL)
5529 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005530 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531 {
5532 edit_submode_extra = (char_u *)_("Back at original");
5533 edit_submode_highl = HLF_W;
5534 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005535 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005536 {
5537 edit_submode_extra = (char_u *)_("Word from other line");
5538 edit_submode_highl = HLF_COUNT;
5539 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005540 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 {
5542 edit_submode_extra = (char_u *)_("The only match");
5543 edit_submode_highl = HLF_COUNT;
5544 }
5545 else
5546 {
5547 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005548 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005550 int number = 0;
5551 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005552
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005553 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005554 {
5555 /* search backwards for the first valid (!= -1) number.
5556 * This should normally succeed already at the first loop
5557 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005558 for (match = compl_curr_match->cp_prev; match != NULL
5559 && match != compl_first_match;
5560 match = match->cp_prev)
5561 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005563 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 break;
5565 }
5566 if (match != NULL)
5567 /* go up and assign all numbers which are not assigned
5568 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005569 for (match = match->cp_next;
5570 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005571 match = match->cp_next)
5572 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 }
5574 else /* BACKWARD */
5575 {
5576 /* search forwards (upwards) for the first valid (!= -1)
5577 * number. This should normally succeed already at the
5578 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005579 for (match = compl_curr_match->cp_next; match != NULL
5580 && match != compl_first_match;
5581 match = match->cp_next)
5582 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005584 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 break;
5586 }
5587 if (match != NULL)
5588 /* go down and assign all numbers which are not
5589 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005590 for (match = match->cp_prev; match
5591 && match->cp_number == -1;
5592 match = match->cp_prev)
5593 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 }
5595 }
5596
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005597 /* The match should always have a sequence number now, this is
5598 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005599 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005601 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5602 * Translations may need more than twice that. */
5603 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005605 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005606 vim_snprintf((char *)match_ref, sizeof(match_ref),
5607 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005608 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005610 vim_snprintf((char *)match_ref, sizeof(match_ref),
5611 _("match %d"),
5612 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 edit_submode_extra = match_ref;
5614 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005615 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 curs_columns(FALSE);
5617 }
5618 }
5619 }
5620
5621 /* Show a message about what (completion) mode we're in. */
5622 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005623 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005625 if (edit_submode_extra != NULL)
5626 {
5627 if (!p_smd)
5628 msg_attr(edit_submode_extra,
5629 edit_submode_highl < HLF_COUNT
5630 ? hl_attr(edit_submode_highl) : 0);
5631 }
5632 else
5633 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635
Bram Moolenaard68071d2006-05-02 22:08:30 +00005636 /* Show the popup menu, unless we got interrupted. */
5637 if (!compl_interrupted)
5638 {
5639 /* RedrawingDisabled may be set when invoked through complete(). */
5640 n = RedrawingDisabled;
5641 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005642
5643 /* If the cursor moved we need to remove the pum first. */
5644 setcursor();
5645 if (save_w_wrow != curwin->w_wrow)
5646 ins_compl_del_pum();
5647
Bram Moolenaard68071d2006-05-02 22:08:30 +00005648 ins_compl_show_pum();
5649 setcursor();
5650 RedrawingDisabled = n;
5651 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005652 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005653 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005654
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 return OK;
5656}
5657
5658/*
5659 * Looks in the first "len" chars. of "src" for search-metachars.
5660 * If dest is not NULL the chars. are copied there quoting (with
5661 * a backslash) the metachars, and dest would be NUL terminated.
5662 * Returns the length (needed) of dest
5663 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005664 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665quote_meta(dest, src, len)
5666 char_u *dest;
5667 char_u *src;
5668 int len;
5669{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005670 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005672 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673 {
5674 switch (*src)
5675 {
5676 case '.':
5677 case '*':
5678 case '[':
5679 if (ctrl_x_mode == CTRL_X_DICTIONARY
5680 || ctrl_x_mode == CTRL_X_THESAURUS)
5681 break;
5682 case '~':
5683 if (!p_magic) /* quote these only if magic is set */
5684 break;
5685 case '\\':
5686 if (ctrl_x_mode == CTRL_X_DICTIONARY
5687 || ctrl_x_mode == CTRL_X_THESAURUS)
5688 break;
5689 case '^': /* currently it's not needed. */
5690 case '$':
5691 m++;
5692 if (dest != NULL)
5693 *dest++ = '\\';
5694 break;
5695 }
5696 if (dest != NULL)
5697 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005698# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 /* Copy remaining bytes of a multibyte character. */
5700 if (has_mbyte)
5701 {
5702 int i, mb_len;
5703
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005704 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 if (mb_len > 0 && len >= mb_len)
5706 for (i = 0; i < mb_len; ++i)
5707 {
5708 --len;
5709 ++src;
5710 if (dest != NULL)
5711 *dest++ = *src;
5712 }
5713 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 }
5716 if (dest != NULL)
5717 *dest = NUL;
5718
5719 return m;
5720}
5721#endif /* FEAT_INS_EXPAND */
5722
5723/*
5724 * Next character is interpreted literally.
5725 * A one, two or three digit decimal number is interpreted as its byte value.
5726 * If one or two digits are entered, the next character is given to vungetc().
5727 * For Unicode a character > 255 may be returned.
5728 */
5729 int
5730get_literal()
5731{
5732 int cc;
5733 int nc;
5734 int i;
5735 int hex = FALSE;
5736 int octal = FALSE;
5737#ifdef FEAT_MBYTE
5738 int unicode = 0;
5739#endif
5740
5741 if (got_int)
5742 return Ctrl_C;
5743
5744#ifdef FEAT_GUI
5745 /*
5746 * In GUI there is no point inserting the internal code for a special key.
5747 * It is more useful to insert the string "<KEY>" instead. This would
5748 * probably be useful in a text window too, but it would not be
5749 * vi-compatible (maybe there should be an option for it?) -- webb
5750 */
5751 if (gui.in_use)
5752 ++allow_keys;
5753#endif
5754#ifdef USE_ON_FLY_SCROLL
5755 dont_scroll = TRUE; /* disallow scrolling here */
5756#endif
5757 ++no_mapping; /* don't map the next key hits */
5758 cc = 0;
5759 i = 0;
5760 for (;;)
5761 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005762 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005763#ifdef FEAT_CMDL_INFO
5764 if (!(State & CMDLINE)
5765# ifdef FEAT_MBYTE
5766 && MB_BYTE2LEN_CHECK(nc) == 1
5767# endif
5768 )
5769 add_to_showcmd(nc);
5770#endif
5771 if (nc == 'x' || nc == 'X')
5772 hex = TRUE;
5773 else if (nc == 'o' || nc == 'O')
5774 octal = TRUE;
5775#ifdef FEAT_MBYTE
5776 else if (nc == 'u' || nc == 'U')
5777 unicode = nc;
5778#endif
5779 else
5780 {
5781 if (hex
5782#ifdef FEAT_MBYTE
5783 || unicode != 0
5784#endif
5785 )
5786 {
5787 if (!vim_isxdigit(nc))
5788 break;
5789 cc = cc * 16 + hex2nr(nc);
5790 }
5791 else if (octal)
5792 {
5793 if (nc < '0' || nc > '7')
5794 break;
5795 cc = cc * 8 + nc - '0';
5796 }
5797 else
5798 {
5799 if (!VIM_ISDIGIT(nc))
5800 break;
5801 cc = cc * 10 + nc - '0';
5802 }
5803
5804 ++i;
5805 }
5806
5807 if (cc > 255
5808#ifdef FEAT_MBYTE
5809 && unicode == 0
5810#endif
5811 )
5812 cc = 255; /* limit range to 0-255 */
5813 nc = 0;
5814
5815 if (hex) /* hex: up to two chars */
5816 {
5817 if (i >= 2)
5818 break;
5819 }
5820#ifdef FEAT_MBYTE
5821 else if (unicode) /* Unicode: up to four or eight chars */
5822 {
5823 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5824 break;
5825 }
5826#endif
5827 else if (i >= 3) /* decimal or octal: up to three chars */
5828 break;
5829 }
5830 if (i == 0) /* no number entered */
5831 {
5832 if (nc == K_ZERO) /* NUL is stored as NL */
5833 {
5834 cc = '\n';
5835 nc = 0;
5836 }
5837 else
5838 {
5839 cc = nc;
5840 nc = 0;
5841 }
5842 }
5843
5844 if (cc == 0) /* NUL is stored as NL */
5845 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005846#ifdef FEAT_MBYTE
5847 if (enc_dbcs && (cc & 0xff) == 0)
5848 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5849 second byte will cause trouble! */
5850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005851
5852 --no_mapping;
5853#ifdef FEAT_GUI
5854 if (gui.in_use)
5855 --allow_keys;
5856#endif
5857 if (nc)
5858 vungetc(nc);
5859 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5860 return cc;
5861}
5862
5863/*
5864 * Insert character, taking care of special keys and mod_mask
5865 */
5866 static void
5867insert_special(c, allow_modmask, ctrlv)
5868 int c;
5869 int allow_modmask;
5870 int ctrlv; /* c was typed after CTRL-V */
5871{
5872 char_u *p;
5873 int len;
5874
5875 /*
5876 * Special function key, translate into "<Key>". Up to the last '>' is
5877 * inserted with ins_str(), so as not to replace characters in replace
5878 * mode.
5879 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5880 * unless 'allow_modmask' is TRUE.
5881 */
5882#ifdef MACOS
5883 /* Command-key never produces a normal key */
5884 if (mod_mask & MOD_MASK_CMD)
5885 allow_modmask = TRUE;
5886#endif
5887 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5888 {
5889 p = get_special_key_name(c, mod_mask);
5890 len = (int)STRLEN(p);
5891 c = p[len - 1];
5892 if (len > 2)
5893 {
5894 if (stop_arrow() == FAIL)
5895 return;
5896 p[len - 1] = NUL;
5897 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005898 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005899 ctrlv = FALSE;
5900 }
5901 }
5902 if (stop_arrow() == OK)
5903 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5904}
5905
5906/*
5907 * Special characters in this context are those that need processing other
5908 * than the simple insertion that can be performed here. This includes ESC
5909 * which terminates the insert, and CR/NL which need special processing to
5910 * open up a new line. This routine tries to optimize insertions performed by
5911 * the "redo", "undo" or "put" commands, so it needs to know when it should
5912 * stop and defer processing to the "normal" mechanism.
5913 * '0' and '^' are special, because they can be followed by CTRL-D.
5914 */
5915#ifdef EBCDIC
5916# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5917#else
5918# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5919#endif
5920
5921#ifdef FEAT_MBYTE
5922# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5923#else
5924# define WHITECHAR(cc) vim_iswhite(cc)
5925#endif
5926
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005927/*
5928 * "flags": INSCHAR_FORMAT - force formatting
5929 * INSCHAR_CTRLV - char typed just after CTRL-V
5930 * INSCHAR_NO_FEX - don't use 'formatexpr'
5931 *
5932 * NOTE: passes the flags value straight through to internal_format() which,
5933 * beside INSCHAR_FORMAT (above), is also looking for these:
5934 * INSCHAR_DO_COM - format comments
5935 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5936 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 void
5938insertchar(c, flags, second_indent)
5939 int c; /* character to insert or NUL */
5940 int flags; /* INSCHAR_FORMAT, etc. */
5941 int second_indent; /* indent for second line if >= 0 */
5942{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005943 int textwidth;
5944#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005945 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 int fo_ins_blank;
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005948 int force_format = flags & INSCHAR_FORMAT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005949
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005950 textwidth = comp_textwidth(force_format);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005951 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952
5953 /*
5954 * Try to break the line in two or more pieces when:
5955 * - Always do this if we have been called to do formatting only.
5956 * - Always do this when 'formatoptions' has the 'a' flag and the line
5957 * ends in white space.
5958 * - Otherwise:
5959 * - Don't do this if inserting a blank
5960 * - Don't do this if an existing character is being replaced, unless
5961 * we're in VREPLACE mode.
5962 * - Do this if the cursor is not on the line where insert started
5963 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5964 * before the insert.
5965 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5966 * before 'textwidth'
5967 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005968 if (textwidth > 0
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005969 && (force_format
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 || (!vim_iswhite(c)
5971 && !((State & REPLACE_FLAG)
5972#ifdef FEAT_VREPLACE
5973 && !(State & VREPLACE_FLAG)
5974#endif
5975 && *ml_get_cursor() != NUL)
5976 && (curwin->w_cursor.lnum != Insstart.lnum
5977 || ((!has_format_option(FO_INS_LONG)
5978 || Insstart_textlen <= (colnr_T)textwidth)
5979 && (!fo_ins_blank
5980 || Insstart_blank_vcol <= (colnr_T)textwidth
5981 ))))))
5982 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005983 /* Format with 'formatexpr' when it's set. Use internal formatting
5984 * when 'formatexpr' isn't set or it returns non-zero. */
5985#if defined(FEAT_EVAL)
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005986 int do_internal = TRUE;
5987 colnr_T virtcol = get_nolist_virtcol()
5988 + char2cells(c != NUL ? c : gchar_cursor());
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005989
Bram Moolenaar0f8dd842015-03-08 14:48:49 +01005990 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0
5991 && (force_format || virtcol > (colnr_T)textwidth))
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005992 {
5993 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5994 /* It may be required to save for undo again, e.g. when setline()
5995 * was called. */
5996 ins_need_undo = TRUE;
5997 }
5998 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005999#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006000 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006002
Bram Moolenaar071d4272004-06-13 20:20:40 +00006003 if (c == NUL) /* only formatting was wanted */
6004 return;
6005
6006#ifdef FEAT_COMMENTS
6007 /* Check whether this character should end a comment. */
6008 if (did_ai && (int)c == end_comment_pending)
6009 {
6010 char_u *line;
6011 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
6012 int middle_len, end_len;
6013 int i;
6014
6015 /*
6016 * Need to remove existing (middle) comment leader and insert end
6017 * comment leader. First, check what comment leader we can find.
6018 */
Bram Moolenaar81340392012-06-06 16:12:59 +02006019 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
6021 {
6022 /* Skip middle-comment string */
6023 while (*p && p[-1] != ':') /* find end of middle flags */
6024 ++p;
6025 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6026 /* Don't count trailing white space for middle_len */
6027 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
6028 --middle_len;
6029
6030 /* Find the end-comment string */
6031 while (*p && p[-1] != ':') /* find end of end flags */
6032 ++p;
6033 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
6034
6035 /* Skip white space before the cursor */
6036 i = curwin->w_cursor.col;
6037 while (--i >= 0 && vim_iswhite(line[i]))
6038 ;
6039 i++;
6040
6041 /* Skip to before the middle leader */
6042 i -= middle_len;
6043
6044 /* Check some expected things before we go on */
6045 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
6046 {
6047 /* Backspace over all the stuff we want to replace */
6048 backspace_until_column(i);
6049
6050 /*
6051 * Insert the end-comment string, except for the last
6052 * character, which will get inserted as normal later.
6053 */
6054 ins_bytes_len(lead_end, end_len - 1);
6055 }
6056 }
6057 }
6058 end_comment_pending = NUL;
6059#endif
6060
6061 did_ai = FALSE;
6062#ifdef FEAT_SMARTINDENT
6063 did_si = FALSE;
6064 can_si = FALSE;
6065 can_si_back = FALSE;
6066#endif
6067
6068 /*
6069 * If there's any pending input, grab up to INPUT_BUFLEN at once.
6070 * This speeds up normal text input considerably.
6071 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
6072 * need to re-indent at a ':', or any other character (but not what
6073 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006074 * Don't do this when there an InsertCharPre autocommand is defined,
6075 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 */
6077#ifdef USE_ON_FLY_SCROLL
6078 dont_scroll = FALSE; /* allow scrolling here */
6079#endif
6080
6081 if ( !ISSPECIAL(c)
6082#ifdef FEAT_MBYTE
6083 && (!has_mbyte || (*mb_char2len)(c) == 1)
6084#endif
6085 && vpeekc() != NUL
6086 && !(State & REPLACE_FLAG)
6087#ifdef FEAT_CINDENT
6088 && !cindent_on()
6089#endif
6090#ifdef FEAT_RIGHTLEFT
6091 && !p_ri
6092#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006093#ifdef FEAT_AUTOCMD
6094 && !has_insertcharpre()
6095#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006096 )
6097 {
6098#define INPUT_BUFLEN 100
6099 char_u buf[INPUT_BUFLEN + 1];
6100 int i;
6101 colnr_T virtcol = 0;
6102
6103 buf[0] = c;
6104 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006105 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006106 virtcol = get_nolist_virtcol();
6107 /*
6108 * Stop the string when:
6109 * - no more chars available
6110 * - finding a special character (command key)
6111 * - buffer is full
6112 * - running into the 'textwidth' boundary
6113 * - need to check for abbreviation: A non-word char after a word-char
6114 */
6115 while ( (c = vpeekc()) != NUL
6116 && !ISSPECIAL(c)
6117#ifdef FEAT_MBYTE
6118 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6119#endif
6120 && i < INPUT_BUFLEN
6121 && (textwidth == 0
6122 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6123 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6124 {
6125#ifdef FEAT_RIGHTLEFT
6126 c = vgetc();
6127 if (p_hkmap && KeyTyped)
6128 c = hkmap(c); /* Hebrew mode mapping */
6129# ifdef FEAT_FKMAP
6130 if (p_fkmap && KeyTyped)
6131 c = fkmap(c); /* Farsi mode mapping */
6132# endif
6133 buf[i++] = c;
6134#else
6135 buf[i++] = vgetc();
6136#endif
6137 }
6138
6139#ifdef FEAT_DIGRAPHS
6140 do_digraph(-1); /* clear digraphs */
6141 do_digraph(buf[i-1]); /* may be the start of a digraph */
6142#endif
6143 buf[i] = NUL;
6144 ins_str(buf);
6145 if (flags & INSCHAR_CTRLV)
6146 {
6147 redo_literal(*buf);
6148 i = 1;
6149 }
6150 else
6151 i = 0;
6152 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006153 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006154 }
6155 else
6156 {
6157#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006158 int cc;
6159
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6161 {
6162 char_u buf[MB_MAXBYTES + 1];
6163
6164 (*mb_char2bytes)(c, buf);
6165 buf[cc] = NUL;
6166 ins_char_bytes(buf, cc);
6167 AppendCharToRedobuff(c);
6168 }
6169 else
6170#endif
6171 {
6172 ins_char(c);
6173 if (flags & INSCHAR_CTRLV)
6174 redo_literal(c);
6175 else
6176 AppendCharToRedobuff(c);
6177 }
6178 }
6179}
6180
6181/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006182 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006183 *
6184 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6185 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006186 */
6187 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006188internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006189 int textwidth;
6190 int second_indent;
6191 int flags;
6192 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006193 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006194{
6195 int cc;
6196 int save_char = NUL;
6197 int haveto_redraw = FALSE;
6198 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6199#ifdef FEAT_MBYTE
6200 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6201#endif
6202 int fo_white_par = has_format_option(FO_WHITE_PAR);
6203 int first_line = TRUE;
6204#ifdef FEAT_COMMENTS
6205 colnr_T leader_len;
6206 int no_leader = FALSE;
6207 int do_comments = (flags & INSCHAR_DO_COM);
6208#endif
Bram Moolenaar0026d472014-09-09 16:32:39 +02006209#ifdef FEAT_LINEBREAK
6210 int has_lbr = curwin->w_p_lbr;
6211
6212 /* make sure win_lbr_chartabsize() counts correctly */
6213 curwin->w_p_lbr = FALSE;
6214#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006215
6216 /*
6217 * When 'ai' is off we don't want a space under the cursor to be
6218 * deleted. Replace it with an 'x' temporarily.
6219 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006220 if (!curbuf->b_p_ai
6221#ifdef FEAT_VREPLACE
6222 && !(State & VREPLACE_FLAG)
6223#endif
6224 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006225 {
6226 cc = gchar_cursor();
6227 if (vim_iswhite(cc))
6228 {
6229 save_char = cc;
6230 pchar_cursor('x');
6231 }
6232 }
6233
6234 /*
6235 * Repeat breaking lines, until the current line is not too long.
6236 */
6237 while (!got_int)
6238 {
6239 int startcol; /* Cursor column at entry */
6240 int wantcol; /* column at textwidth border */
6241 int foundcol; /* column for start of spaces */
6242 int end_foundcol = 0; /* column for start of word */
6243 colnr_T len;
6244 colnr_T virtcol;
6245#ifdef FEAT_VREPLACE
6246 int orig_col = 0;
6247 char_u *saved_text = NULL;
6248#endif
6249 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006250 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006251
Bram Moolenaar97b98102009-11-17 16:41:01 +00006252 virtcol = get_nolist_virtcol()
6253 + char2cells(c != NUL ? c : gchar_cursor());
6254 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006255 break;
6256
6257#ifdef FEAT_COMMENTS
6258 if (no_leader)
6259 do_comments = FALSE;
6260 else if (!(flags & INSCHAR_FORMAT)
6261 && has_format_option(FO_WRAP_COMS))
6262 do_comments = TRUE;
6263
6264 /* Don't break until after the comment leader */
6265 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006266 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006267 else
6268 leader_len = 0;
6269
6270 /* If the line doesn't start with a comment leader, then don't
6271 * start one in a following broken line. Avoids that a %word
6272 * moved to the start of the next line causes all following lines
6273 * to start with %. */
6274 if (leader_len == 0)
6275 no_leader = TRUE;
6276#endif
6277 if (!(flags & INSCHAR_FORMAT)
6278#ifdef FEAT_COMMENTS
6279 && leader_len == 0
6280#endif
6281 && !has_format_option(FO_WRAP))
6282
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006283 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006284 if ((startcol = curwin->w_cursor.col) == 0)
6285 break;
6286
6287 /* find column of textwidth border */
6288 coladvance((colnr_T)textwidth);
6289 wantcol = curwin->w_cursor.col;
6290
Bram Moolenaar97b98102009-11-17 16:41:01 +00006291 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006292 foundcol = 0;
6293
6294 /*
6295 * Find position to break at.
6296 * Stop at first entered white when 'formatoptions' has 'v'
6297 */
6298 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006299 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006300 || curwin->w_cursor.lnum != Insstart.lnum
6301 || curwin->w_cursor.col >= Insstart.col)
6302 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006303 if (curwin->w_cursor.col == startcol && c != NUL)
6304 cc = c;
6305 else
6306 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006307 if (WHITECHAR(cc))
6308 {
6309 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006310 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006311
6312 /* find start of sequence of blanks */
6313 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6314 {
6315 dec_cursor();
6316 cc = gchar_cursor();
6317 }
6318 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6319 break; /* only spaces in front of text */
6320#ifdef FEAT_COMMENTS
6321 /* Don't break until after the comment leader */
6322 if (curwin->w_cursor.col < leader_len)
6323 break;
6324#endif
6325 if (has_format_option(FO_ONE_LETTER))
6326 {
6327 /* do not break after one-letter words */
6328 if (curwin->w_cursor.col == 0)
6329 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006330#ifdef FEAT_COMMENTS
6331 /* do not break "#a b" when 'tw' is 2 */
6332 if (curwin->w_cursor.col <= leader_len)
6333 break;
6334#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006335 col = curwin->w_cursor.col;
6336 dec_cursor();
6337 cc = gchar_cursor();
6338
6339 if (WHITECHAR(cc))
6340 continue; /* one-letter, continue */
6341 curwin->w_cursor.col = col;
6342 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006343
6344 inc_cursor();
6345
6346 end_foundcol = end_col + 1;
6347 foundcol = curwin->w_cursor.col;
6348 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006349 break;
6350 }
6351#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006352 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006353 {
6354 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006355 if (curwin->w_cursor.col != startcol)
6356 {
6357#ifdef FEAT_COMMENTS
6358 /* Don't break until after the comment leader */
6359 if (curwin->w_cursor.col < leader_len)
6360 break;
6361#endif
6362 col = curwin->w_cursor.col;
6363 inc_cursor();
6364 /* Don't change end_foundcol if already set. */
6365 if (foundcol != curwin->w_cursor.col)
6366 {
6367 foundcol = curwin->w_cursor.col;
6368 end_foundcol = foundcol;
6369 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6370 break;
6371 }
6372 curwin->w_cursor.col = col;
6373 }
6374
6375 if (curwin->w_cursor.col == 0)
6376 break;
6377
6378 col = curwin->w_cursor.col;
6379
6380 dec_cursor();
6381 cc = gchar_cursor();
6382
6383 if (WHITECHAR(cc))
6384 continue; /* break with space */
6385#ifdef FEAT_COMMENTS
6386 /* Don't break until after the comment leader */
6387 if (curwin->w_cursor.col < leader_len)
6388 break;
6389#endif
6390
6391 curwin->w_cursor.col = col;
6392
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006393 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006394 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006395 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6396 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006397 }
6398#endif
6399 if (curwin->w_cursor.col == 0)
6400 break;
6401 dec_cursor();
6402 }
6403
6404 if (foundcol == 0) /* no spaces, cannot break line */
6405 {
6406 curwin->w_cursor.col = startcol;
6407 break;
6408 }
6409
6410 /* Going to break the line, remove any "$" now. */
6411 undisplay_dollar();
6412
6413 /*
6414 * Offset between cursor position and line break is used by replace
6415 * stack functions. VREPLACE does not use this, and backspaces
6416 * over the text instead.
6417 */
6418#ifdef FEAT_VREPLACE
6419 if (State & VREPLACE_FLAG)
6420 orig_col = startcol; /* Will start backspacing from here */
6421 else
6422#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006423 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006424
6425 /*
6426 * adjust startcol for spaces that will be deleted and
6427 * characters that will remain on top line
6428 */
6429 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006430 while ((cc = gchar_cursor(), WHITECHAR(cc))
6431 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006432 inc_cursor();
6433 startcol -= curwin->w_cursor.col;
6434 if (startcol < 0)
6435 startcol = 0;
6436
6437#ifdef FEAT_VREPLACE
6438 if (State & VREPLACE_FLAG)
6439 {
6440 /*
6441 * In VREPLACE mode, we will backspace over the text to be
6442 * wrapped, so save a copy now to put on the next line.
6443 */
6444 saved_text = vim_strsave(ml_get_cursor());
6445 curwin->w_cursor.col = orig_col;
6446 if (saved_text == NULL)
6447 break; /* Can't do it, out of memory */
6448 saved_text[startcol] = NUL;
6449
6450 /* Backspace over characters that will move to the next line */
6451 if (!fo_white_par)
6452 backspace_until_column(foundcol);
6453 }
6454 else
6455#endif
6456 {
6457 /* put cursor after pos. to break line */
6458 if (!fo_white_par)
6459 curwin->w_cursor.col = foundcol;
6460 }
6461
6462 /*
6463 * Split the line just before the margin.
6464 * Only insert/delete lines, but don't really redraw the window.
6465 */
6466 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6467 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6468#ifdef FEAT_COMMENTS
6469 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006470 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006471#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006472 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6473 if (!(flags & INSCHAR_COM_LIST))
6474 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006475
6476 replace_offset = 0;
6477 if (first_line)
6478 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006479 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006480 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006481 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006482 * This section is for auto-wrap of numeric lists. When not
6483 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6484 * flag will be set and open_line() will handle it (as seen
6485 * above). The code here (and in get_number_indent()) will
6486 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006487 */
6488 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006489 second_indent =
6490 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006491 if (second_indent >= 0)
6492 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006493#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006494 if (State & VREPLACE_FLAG)
6495 change_indent(INDENT_SET, second_indent,
6496 FALSE, NUL, TRUE);
6497 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006498#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006499#ifdef FEAT_COMMENTS
6500 if (leader_len > 0 && second_indent - leader_len > 0)
6501 {
6502 int i;
6503 int padding = second_indent - leader_len;
6504
6505 /* We started at the first_line of a numbered list
6506 * that has a comment. the open_line() function has
6507 * inserted the proper comment leader and positioned
6508 * the cursor at the end of the split line. Now we
6509 * add the additional whitespace needed after the
6510 * comment leader for the numbered list. */
6511 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006512 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006513 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006514 }
6515 else
6516 {
6517#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006518 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006519#ifdef FEAT_COMMENTS
6520 }
6521#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006522 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006523 }
6524 first_line = FALSE;
6525 }
6526
6527#ifdef FEAT_VREPLACE
6528 if (State & VREPLACE_FLAG)
6529 {
6530 /*
6531 * In VREPLACE mode we have backspaced over the text to be
6532 * moved, now we re-insert it into the new line.
6533 */
6534 ins_bytes(saved_text);
6535 vim_free(saved_text);
6536 }
6537 else
6538#endif
6539 {
6540 /*
6541 * Check if cursor is not past the NUL off the line, cindent
6542 * may have added or removed indent.
6543 */
6544 curwin->w_cursor.col += startcol;
6545 len = (colnr_T)STRLEN(ml_get_curline());
6546 if (curwin->w_cursor.col > len)
6547 curwin->w_cursor.col = len;
6548 }
6549
6550 haveto_redraw = TRUE;
6551#ifdef FEAT_CINDENT
6552 can_cindent = TRUE;
6553#endif
6554 /* moved the cursor, don't autoindent or cindent now */
6555 did_ai = FALSE;
6556#ifdef FEAT_SMARTINDENT
6557 did_si = FALSE;
6558 can_si = FALSE;
6559 can_si_back = FALSE;
6560#endif
6561 line_breakcheck();
6562 }
6563
6564 if (save_char != NUL) /* put back space after cursor */
6565 pchar_cursor(save_char);
6566
Bram Moolenaar0026d472014-09-09 16:32:39 +02006567#ifdef FEAT_LINEBREAK
6568 curwin->w_p_lbr = has_lbr;
6569#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006570 if (!format_only && haveto_redraw)
6571 {
6572 update_topline();
6573 redraw_curbuf_later(VALID);
6574 }
6575}
6576
6577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 * Called after inserting or deleting text: When 'formatoptions' includes the
6579 * 'a' flag format from the current line until the end of the paragraph.
6580 * Keep the cursor at the same position relative to the text.
6581 * The caller must have saved the cursor line for undo, following ones will be
6582 * saved here.
6583 */
6584 void
6585auto_format(trailblank, prev_line)
6586 int trailblank; /* when TRUE also format with trailing blank */
6587 int prev_line; /* may start in previous line */
6588{
6589 pos_T pos;
6590 colnr_T len;
6591 char_u *old;
6592 char_u *new, *pnew;
6593 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006594 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006595
6596 if (!has_format_option(FO_AUTO))
6597 return;
6598
6599 pos = curwin->w_cursor;
6600 old = ml_get_curline();
6601
6602 /* may remove added space */
6603 check_auto_format(FALSE);
6604
6605 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6606 * user might insert normal text next. Also skip formatting when "1" is
6607 * in 'formatoptions' and there is a single character before the cursor.
6608 * Otherwise the line would be broken and when typing another non-white
6609 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006610 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 if (*old != NUL && !trailblank && wasatend)
6612 {
6613 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006614 cc = gchar_cursor();
6615 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6616 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006617 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006618 cc = gchar_cursor();
6619 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 {
6621 curwin->w_cursor = pos;
6622 return;
6623 }
6624 curwin->w_cursor = pos;
6625 }
6626
6627#ifdef FEAT_COMMENTS
6628 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6629 * comments. */
6630 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006631 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006632 return;
6633#endif
6634
6635 /*
6636 * May start formatting in a previous line, so that after "x" a word is
6637 * moved to the previous line if it fits there now. Only when this is not
6638 * the start of a paragraph.
6639 */
6640 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6641 {
6642 --curwin->w_cursor.lnum;
6643 if (u_save_cursor() == FAIL)
6644 return;
6645 }
6646
6647 /*
6648 * Do the formatting and restore the cursor position. "saved_cursor" will
6649 * be adjusted for the text formatting.
6650 */
6651 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006652 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 curwin->w_cursor = saved_cursor;
6654 saved_cursor.lnum = 0;
6655
6656 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6657 {
6658 /* "cannot happen" */
6659 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6660 coladvance((colnr_T)MAXCOL);
6661 }
6662 else
6663 check_cursor_col();
6664
6665 /* Insert mode: If the cursor is now after the end of the line while it
6666 * previously wasn't, the line was broken. Because of the rule above we
6667 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6668 * formatted. */
6669 if (!wasatend && has_format_option(FO_WHITE_PAR))
6670 {
6671 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006672 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 if (curwin->w_cursor.col == len)
6674 {
6675 pnew = vim_strnsave(new, len + 2);
6676 pnew[len] = ' ';
6677 pnew[len + 1] = NUL;
6678 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6679 /* remove the space later */
6680 did_add_space = TRUE;
6681 }
6682 else
6683 /* may remove added space */
6684 check_auto_format(FALSE);
6685 }
6686
6687 check_cursor();
6688}
6689
6690/*
6691 * When an extra space was added to continue a paragraph for auto-formatting,
6692 * delete it now. The space must be under the cursor, just after the insert
6693 * position.
6694 */
6695 static void
6696check_auto_format(end_insert)
6697 int end_insert; /* TRUE when ending Insert mode */
6698{
6699 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006700 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701
6702 if (did_add_space)
6703 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006704 cc = gchar_cursor();
6705 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 /* Somehow the space was removed already. */
6707 did_add_space = FALSE;
6708 else
6709 {
6710 if (!end_insert)
6711 {
6712 inc_cursor();
6713 c = gchar_cursor();
6714 dec_cursor();
6715 }
6716 if (c != NUL)
6717 {
6718 /* The space is no longer at the end of the line, delete it. */
6719 del_char(FALSE);
6720 did_add_space = FALSE;
6721 }
6722 }
6723 }
6724}
6725
6726/*
6727 * Find out textwidth to be used for formatting:
6728 * if 'textwidth' option is set, use it
6729 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6730 * if invalid value, use 0.
6731 * Set default to window width (maximum 79) for "gq" operator.
6732 */
6733 int
6734comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006735 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736{
6737 int textwidth;
6738
6739 textwidth = curbuf->b_p_tw;
6740 if (textwidth == 0 && curbuf->b_p_wm)
6741 {
6742 /* The width is the window width minus 'wrapmargin' minus all the
6743 * things that add to the margin. */
6744 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6745#ifdef FEAT_CMDWIN
6746 if (cmdwin_type != 0)
6747 textwidth -= 1;
6748#endif
6749#ifdef FEAT_FOLDING
6750 textwidth -= curwin->w_p_fdc;
6751#endif
6752#ifdef FEAT_SIGNS
6753 if (curwin->w_buffer->b_signlist != NULL
6754# ifdef FEAT_NETBEANS_INTG
Bram Moolenaar3b7b8362015-03-20 18:11:48 +01006755 || curwin->w_buffer->b_has_sign_column
Bram Moolenaar071d4272004-06-13 20:20:40 +00006756# endif
6757 )
6758 textwidth -= 1;
6759#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006760 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006761 textwidth -= 8;
6762 }
6763 if (textwidth < 0)
6764 textwidth = 0;
6765 if (ff && textwidth == 0)
6766 {
6767 textwidth = W_WIDTH(curwin) - 1;
6768 if (textwidth > 79)
6769 textwidth = 79;
6770 }
6771 return textwidth;
6772}
6773
6774/*
6775 * Put a character in the redo buffer, for when just after a CTRL-V.
6776 */
6777 static void
6778redo_literal(c)
6779 int c;
6780{
6781 char_u buf[10];
6782
6783 /* Only digits need special treatment. Translate them into a string of
6784 * three digits. */
6785 if (VIM_ISDIGIT(c))
6786 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006787 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 AppendToRedobuff(buf);
6789 }
6790 else
6791 AppendCharToRedobuff(c);
6792}
6793
6794/*
6795 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006796 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 */
6798 static void
6799start_arrow(end_insert_pos)
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006800 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801{
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02006802 start_arrow_common(end_insert_pos, TRUE);
6803}
6804
6805/*
6806 * Like start_arrow() but with end_change argument.
6807 * Will prepare for redo of CTRL-G U if "end_change" is FALSE.
6808 */
6809 static void
6810start_arrow_with_change(end_insert_pos, end_change)
6811 pos_T *end_insert_pos; /* can be NULL */
6812 int end_change; /* end undoable change */
6813{
6814 start_arrow_common(end_insert_pos, end_change);
6815 if (!end_change)
6816 {
6817 AppendCharToRedobuff(Ctrl_G);
6818 AppendCharToRedobuff('U');
6819 }
6820}
6821
6822 static void
6823start_arrow_common(end_insert_pos, end_change)
6824 pos_T *end_insert_pos; /* can be NULL */
6825 int end_change; /* end undoable change */
6826{
6827 if (!arrow_used && end_change) /* something has been inserted */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006828 {
6829 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006830 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831 arrow_used = TRUE; /* this means we stopped the current insert */
6832 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006833#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006834 check_spell_redraw();
6835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836}
6837
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006838#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006839/*
6840 * If we skipped highlighting word at cursor, do it now.
6841 * It may be skipped again, thus reset spell_redraw_lnum first.
6842 */
6843 static void
6844check_spell_redraw()
6845{
6846 if (spell_redraw_lnum != 0)
6847 {
6848 linenr_T lnum = spell_redraw_lnum;
6849
6850 spell_redraw_lnum = 0;
6851 redrawWinline(lnum, FALSE);
6852 }
6853}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006854
6855/*
6856 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6857 * spelled word, if there is one.
6858 */
6859 static void
6860spell_back_to_badword()
6861{
6862 pos_T tpos = curwin->w_cursor;
6863
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006864 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006865 if (curwin->w_cursor.col != tpos.col)
6866 start_arrow(&tpos);
6867}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006868#endif
6869
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870/*
6871 * stop_arrow() is called before a change is made in insert mode.
6872 * If an arrow key has been used, start a new insertion.
6873 * Returns FAIL if undo is impossible, shouldn't insert then.
6874 */
6875 int
6876stop_arrow()
6877{
6878 if (arrow_used)
6879 {
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006880 Insstart = curwin->w_cursor; /* new insertion starts here */
6881 if (Insstart.col > Insstart_orig.col && !ins_need_undo)
6882 /* Don't update the original insert position when moved to the
6883 * right, except when nothing was inserted yet. */
6884 update_Insstart_orig = FALSE;
6885 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
6886
Bram Moolenaar071d4272004-06-13 20:20:40 +00006887 if (u_save_cursor() == OK)
6888 {
6889 arrow_used = FALSE;
6890 ins_need_undo = FALSE;
6891 }
Bram Moolenaar1fc7e972014-08-16 18:13:03 +02006892
Bram Moolenaar071d4272004-06-13 20:20:40 +00006893 ai_col = 0;
6894#ifdef FEAT_VREPLACE
6895 if (State & VREPLACE_FLAG)
6896 {
6897 orig_line_count = curbuf->b_ml.ml_line_count;
6898 vr_lines_changed = 1;
6899 }
6900#endif
6901 ResetRedobuff();
6902 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006903 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 }
6905 else if (ins_need_undo)
6906 {
6907 if (u_save_cursor() == OK)
6908 ins_need_undo = FALSE;
6909 }
6910
6911#ifdef FEAT_FOLDING
6912 /* Always open fold at the cursor line when inserting something. */
6913 foldOpenCursor();
6914#endif
6915
6916 return (arrow_used || ins_need_undo ? FAIL : OK);
6917}
6918
6919/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006920 * Do a few things to stop inserting.
6921 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6922 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006923 */
6924 static void
Bram Moolenaarf332a652013-11-04 04:20:33 +01006925stop_insert(end_insert_pos, esc, nomove)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006926 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006927 int esc; /* called by ins_esc() */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006928 int nomove; /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006930 int cc;
6931 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932
6933 stop_redo_ins();
6934 replace_flush(); /* abandon replace stack */
6935
6936 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006937 * Save the inserted text for later redo with ^@ and CTRL-A.
6938 * Don't do it when "restart_edit" was set and nothing was inserted,
6939 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006940 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006941 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006942 if (did_restart_edit == 0 || (ptr != NULL
6943 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006944 {
6945 vim_free(last_insert);
6946 last_insert = ptr;
6947 last_insert_skip = new_insert_skip;
6948 }
6949 else
6950 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006952 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 {
6954 /* Auto-format now. It may seem strange to do this when stopping an
6955 * insertion (or moving the cursor), but it's required when appending
6956 * a line and having it end in a space. But only do it when something
6957 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006958 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006960 pos_T tpos = curwin->w_cursor;
6961
Bram Moolenaar071d4272004-06-13 20:20:40 +00006962 /* When the cursor is at the end of the line after a space the
6963 * formatting will move it to the following word. Avoid that by
6964 * moving the cursor onto the space. */
6965 cc = 'x';
6966 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6967 {
6968 dec_cursor();
6969 cc = gchar_cursor();
6970 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006971 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006972 }
6973
6974 auto_format(TRUE, FALSE);
6975
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006976 if (vim_iswhite(cc))
6977 {
6978 if (gchar_cursor() != NUL)
6979 inc_cursor();
6980#ifdef FEAT_VIRTUALEDIT
6981 /* If the cursor is still at the same character, also keep
6982 * the "coladd". */
6983 if (gchar_cursor() == NUL
6984 && curwin->w_cursor.lnum == tpos.lnum
6985 && curwin->w_cursor.col == tpos.col)
6986 curwin->w_cursor.coladd = tpos.coladd;
6987#endif
6988 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 }
6990
6991 /* If a space was inserted for auto-formatting, remove it now. */
6992 check_auto_format(TRUE);
6993
6994 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006995 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006996 * Do this when ESC was used or moving the cursor up/down.
6997 * Check for the old position still being valid, just in case the text
6998 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006999 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00007000 && curwin->w_cursor.lnum != end_insert_pos->lnum))
7001 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007003 pos_T tpos = curwin->w_cursor;
7004
7005 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007006 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007007 for (;;)
7008 {
7009 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
7010 --curwin->w_cursor.col;
7011 cc = gchar_cursor();
7012 if (!vim_iswhite(cc))
7013 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00007014 if (del_char(TRUE) == FAIL)
7015 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00007016 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00007017 if (curwin->w_cursor.lnum != tpos.lnum)
7018 curwin->w_cursor = tpos;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007019 else
7020 {
Bram Moolenaaref6875b2014-11-12 18:59:25 +01007021 /* reset tpos, could have been invalidated in the loop above */
7022 tpos = curwin->w_cursor;
Bram Moolenaar2f31e392014-10-31 19:20:36 +01007023 tpos.col++;
7024 if (cc != NUL && gchar_pos(&tpos) == NUL)
7025 ++curwin->w_cursor.col; /* put cursor back on the NUL */
7026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027
Bram Moolenaar071d4272004-06-13 20:20:40 +00007028 /* <C-S-Right> may have started Visual mode, adjust the position for
7029 * deleted characters. */
7030 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
7031 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007032 int len = (int)STRLEN(ml_get_curline());
7033
7034 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00007036 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007037#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00007038 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01007039#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007040 }
7041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 }
7043 }
7044 did_ai = FALSE;
7045#ifdef FEAT_SMARTINDENT
7046 did_si = FALSE;
7047 can_si = FALSE;
7048 can_si_back = FALSE;
7049#endif
7050
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007051 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
7052 * now in a different buffer. */
7053 if (end_insert_pos != NULL)
7054 {
7055 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01007056 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007057 curbuf->b_op_end = *end_insert_pos;
7058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059}
7060
7061/*
7062 * Set the last inserted text to a single character.
7063 * Used for the replace command.
7064 */
7065 void
7066set_last_insert(c)
7067 int c;
7068{
7069 char_u *s;
7070
7071 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007072 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 if (last_insert != NULL)
7074 {
7075 s = last_insert;
7076 /* Use the CTRL-V only when entering a special char */
7077 if (c < ' ' || c == DEL)
7078 *s++ = Ctrl_V;
7079 s = add_char2buf(c, s);
7080 *s++ = ESC;
7081 *s++ = NUL;
7082 last_insert_skip = 0;
7083 }
7084}
7085
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007086#if defined(EXITFREE) || defined(PROTO)
7087 void
7088free_last_insert()
7089{
7090 vim_free(last_insert);
7091 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007092# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00007093 vim_free(compl_orig_text);
7094 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00007095# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00007096}
7097#endif
7098
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099/*
7100 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
7101 * and CSI. Handle multi-byte characters.
7102 * Returns a pointer to after the added bytes.
7103 */
7104 char_u *
7105add_char2buf(c, s)
7106 int c;
7107 char_u *s;
7108{
7109#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007110 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007111 int i;
7112 int len;
7113
7114 len = (*mb_char2bytes)(c, temp);
7115 for (i = 0; i < len; ++i)
7116 {
7117 c = temp[i];
7118#endif
7119 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
7120 if (c == K_SPECIAL)
7121 {
7122 *s++ = K_SPECIAL;
7123 *s++ = KS_SPECIAL;
7124 *s++ = KE_FILLER;
7125 }
7126#ifdef FEAT_GUI
7127 else if (c == CSI)
7128 {
7129 *s++ = CSI;
7130 *s++ = KS_EXTRA;
7131 *s++ = (int)KE_CSI;
7132 }
7133#endif
7134 else
7135 *s++ = c;
7136#ifdef FEAT_MBYTE
7137 }
7138#endif
7139 return s;
7140}
7141
7142/*
7143 * move cursor to start of line
7144 * if flags & BL_WHITE move to first non-white
7145 * if flags & BL_SOL move to first non-white if startofline is set,
7146 * otherwise keep "curswant" column
7147 * if flags & BL_FIX don't leave the cursor on a NUL.
7148 */
7149 void
7150beginline(flags)
7151 int flags;
7152{
7153 if ((flags & BL_SOL) && !p_sol)
7154 coladvance(curwin->w_curswant);
7155 else
7156 {
7157 curwin->w_cursor.col = 0;
7158#ifdef FEAT_VIRTUALEDIT
7159 curwin->w_cursor.coladd = 0;
7160#endif
7161
7162 if (flags & (BL_WHITE | BL_SOL))
7163 {
7164 char_u *ptr;
7165
7166 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7167 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7168 ++curwin->w_cursor.col;
7169 }
7170 curwin->w_set_curswant = TRUE;
7171 }
7172}
7173
7174/*
7175 * oneright oneleft cursor_down cursor_up
7176 *
7177 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007178 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007179 * Return OK when successful, FAIL when we hit a line of file boundary.
7180 */
7181
7182 int
7183oneright()
7184{
7185 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007187
7188#ifdef FEAT_VIRTUALEDIT
7189 if (virtual_active())
7190 {
7191 pos_T prevpos = curwin->w_cursor;
7192
7193 /* Adjust for multi-wide char (excluding TAB) */
7194 ptr = ml_get_cursor();
7195 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007196# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007197 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007198# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007199 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007200# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007201 ))
7202 ? ptr2cells(ptr) : 1));
7203 curwin->w_set_curswant = TRUE;
7204 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7205 return (prevpos.col != curwin->w_cursor.col
7206 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7207 }
7208#endif
7209
7210 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007211 if (*ptr == NUL)
7212 return FAIL; /* already at the very end */
7213
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007215 if (has_mbyte)
7216 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217 else
7218#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007219 l = 1;
7220
7221 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7222 * contains "onemore". */
7223 if (ptr[l] == NUL
7224#ifdef FEAT_VIRTUALEDIT
7225 && (ve_flags & VE_ONEMORE) == 0
7226#endif
7227 )
7228 return FAIL;
7229 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007230
7231 curwin->w_set_curswant = TRUE;
7232 return OK;
7233}
7234
7235 int
7236oneleft()
7237{
7238#ifdef FEAT_VIRTUALEDIT
7239 if (virtual_active())
7240 {
7241 int width;
7242 int v = getviscol();
7243
7244 if (v == 0)
7245 return FAIL;
7246
7247# ifdef FEAT_LINEBREAK
7248 /* We might get stuck on 'showbreak', skip over it. */
7249 width = 1;
7250 for (;;)
7251 {
7252 coladvance(v - width);
Bram Moolenaar597a4222014-06-25 14:39:50 +02007253 /* getviscol() is slow, skip it when 'showbreak' is empty,
7254 * 'breakindent' is not set and there are no multi-byte
7255 * characters */
7256 if ((*p_sbr == NUL && !curwin->w_p_bri
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257# ifdef FEAT_MBYTE
7258 && !has_mbyte
7259# endif
7260 ) || getviscol() < v)
7261 break;
7262 ++width;
7263 }
7264# else
7265 coladvance(v - 1);
7266# endif
7267
7268 if (curwin->w_cursor.coladd == 1)
7269 {
7270 char_u *ptr;
7271
7272 /* Adjust for multi-wide char (not a TAB) */
7273 ptr = ml_get_cursor();
7274 if (*ptr != TAB && vim_isprintc(
7275# ifdef FEAT_MBYTE
7276 (*mb_ptr2char)(ptr)
7277# else
7278 *ptr
7279# endif
7280 ) && ptr2cells(ptr) > 1)
7281 curwin->w_cursor.coladd = 0;
7282 }
7283
7284 curwin->w_set_curswant = TRUE;
7285 return OK;
7286 }
7287#endif
7288
7289 if (curwin->w_cursor.col == 0)
7290 return FAIL;
7291
7292 curwin->w_set_curswant = TRUE;
7293 --curwin->w_cursor.col;
7294
7295#ifdef FEAT_MBYTE
7296 /* if the character on the left of the current cursor is a multi-byte
7297 * character, move to its first byte */
7298 if (has_mbyte)
7299 mb_adjust_cursor();
7300#endif
7301 return OK;
7302}
7303
7304 int
7305cursor_up(n, upd_topline)
7306 long n;
7307 int upd_topline; /* When TRUE: update topline */
7308{
7309 linenr_T lnum;
7310
7311 if (n > 0)
7312 {
7313 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007314 /* This fails if the cursor is already in the first line or the count
7315 * is larger than the line number and '-' is in 'cpoptions' */
7316 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007317 return FAIL;
7318 if (n >= lnum)
7319 lnum = 1;
7320 else
7321#ifdef FEAT_FOLDING
7322 if (hasAnyFolding(curwin))
7323 {
7324 /*
7325 * Count each sequence of folded lines as one logical line.
7326 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007327 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007328 (void)hasFolding(lnum, &lnum, NULL);
7329
7330 while (n--)
7331 {
7332 /* move up one line */
7333 --lnum;
7334 if (lnum <= 1)
7335 break;
7336 /* If we entered a fold, move to the beginning, unless in
7337 * Insert mode or when 'foldopen' contains "all": it will open
7338 * in a moment. */
7339 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7340 (void)hasFolding(lnum, &lnum, NULL);
7341 }
7342 if (lnum < 1)
7343 lnum = 1;
7344 }
7345 else
7346#endif
7347 lnum -= n;
7348 curwin->w_cursor.lnum = lnum;
7349 }
7350
7351 /* try to advance to the column we want to be at */
7352 coladvance(curwin->w_curswant);
7353
7354 if (upd_topline)
7355 update_topline(); /* make sure curwin->w_topline is valid */
7356
7357 return OK;
7358}
7359
7360/*
7361 * Cursor down a number of logical lines.
7362 */
7363 int
7364cursor_down(n, upd_topline)
7365 long n;
7366 int upd_topline; /* When TRUE: update topline */
7367{
7368 linenr_T lnum;
7369
7370 if (n > 0)
7371 {
7372 lnum = curwin->w_cursor.lnum;
7373#ifdef FEAT_FOLDING
7374 /* Move to last line of fold, will fail if it's the end-of-file. */
7375 (void)hasFolding(lnum, NULL, &lnum);
7376#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007377 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007378 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007379 if (lnum >= curbuf->b_ml.ml_line_count
7380 || (lnum + n > curbuf->b_ml.ml_line_count
7381 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007382 return FAIL;
7383 if (lnum + n >= curbuf->b_ml.ml_line_count)
7384 lnum = curbuf->b_ml.ml_line_count;
7385 else
7386#ifdef FEAT_FOLDING
7387 if (hasAnyFolding(curwin))
7388 {
7389 linenr_T last;
7390
7391 /* count each sequence of folded lines as one logical line */
7392 while (n--)
7393 {
7394 if (hasFolding(lnum, NULL, &last))
7395 lnum = last + 1;
7396 else
7397 ++lnum;
7398 if (lnum >= curbuf->b_ml.ml_line_count)
7399 break;
7400 }
7401 if (lnum > curbuf->b_ml.ml_line_count)
7402 lnum = curbuf->b_ml.ml_line_count;
7403 }
7404 else
7405#endif
7406 lnum += n;
7407 curwin->w_cursor.lnum = lnum;
7408 }
7409
7410 /* try to advance to the column we want to be at */
7411 coladvance(curwin->w_curswant);
7412
7413 if (upd_topline)
7414 update_topline(); /* make sure curwin->w_topline is valid */
7415
7416 return OK;
7417}
7418
7419/*
7420 * Stuff the last inserted text in the read buffer.
7421 * Last_insert actually is a copy of the redo buffer, so we
7422 * first have to remove the command.
7423 */
7424 int
7425stuff_inserted(c, count, no_esc)
7426 int c; /* Command character to be inserted */
7427 long count; /* Repeat this many times */
7428 int no_esc; /* Don't add an ESC at the end */
7429{
7430 char_u *esc_ptr;
7431 char_u *ptr;
7432 char_u *last_ptr;
7433 char_u last = NUL;
7434
7435 ptr = get_last_insert();
7436 if (ptr == NULL)
7437 {
7438 EMSG(_(e_noinstext));
7439 return FAIL;
7440 }
7441
7442 /* may want to stuff the command character, to start Insert mode */
7443 if (c != NUL)
7444 stuffcharReadbuff(c);
7445 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7446 *esc_ptr = NUL; /* remove the ESC */
7447
7448 /* when the last char is either "0" or "^" it will be quoted if no ESC
7449 * comes after it OR if it will inserted more than once and "ptr"
7450 * starts with ^D. -- Acevedo
7451 */
7452 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7453 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7454 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7455 {
7456 last = *last_ptr;
7457 *last_ptr = NUL;
7458 }
7459
7460 do
7461 {
7462 stuffReadbuff(ptr);
7463 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7464 if (last)
7465 stuffReadbuff((char_u *)(last == '0'
7466 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7467 : IF_EB("\026^", CTRL_V_STR "^")));
7468 }
7469 while (--count > 0);
7470
7471 if (last)
7472 *last_ptr = last;
7473
7474 if (esc_ptr != NULL)
7475 *esc_ptr = ESC; /* put the ESC back */
7476
7477 /* may want to stuff a trailing ESC, to get out of Insert mode */
7478 if (!no_esc)
7479 stuffcharReadbuff(ESC);
7480
7481 return OK;
7482}
7483
7484 char_u *
7485get_last_insert()
7486{
7487 if (last_insert == NULL)
7488 return NULL;
7489 return last_insert + last_insert_skip;
7490}
7491
7492/*
7493 * Get last inserted string, and remove trailing <Esc>.
7494 * Returns pointer to allocated memory (must be freed) or NULL.
7495 */
7496 char_u *
7497get_last_insert_save()
7498{
7499 char_u *s;
7500 int len;
7501
7502 if (last_insert == NULL)
7503 return NULL;
7504 s = vim_strsave(last_insert + last_insert_skip);
7505 if (s != NULL)
7506 {
7507 len = (int)STRLEN(s);
7508 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7509 s[len - 1] = NUL;
7510 }
7511 return s;
7512}
7513
7514/*
7515 * Check the word in front of the cursor for an abbreviation.
7516 * Called when the non-id character "c" has been entered.
7517 * When an abbreviation is recognized it is removed from the text and
7518 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7519 */
7520 static int
7521echeck_abbr(c)
7522 int c;
7523{
7524 /* Don't check for abbreviation in paste mode, when disabled and just
7525 * after moving around with cursor keys. */
7526 if (p_paste || no_abbr || arrow_used)
7527 return FALSE;
7528
7529 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7530 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7531}
7532
7533/*
7534 * replace-stack functions
7535 *
7536 * When replacing characters, the replaced characters are remembered for each
7537 * new character. This is used to re-insert the old text when backspacing.
7538 *
7539 * There is a NUL headed list of characters for each character that is
7540 * currently in the file after the insertion point. When BS is used, one NUL
7541 * headed list is put back for the deleted character.
7542 *
7543 * For a newline, there are two NUL headed lists. One contains the characters
7544 * that the NL replaced. The extra one stores the characters after the cursor
7545 * that were deleted (always white space).
7546 *
7547 * Replace_offset is normally 0, in which case replace_push will add a new
7548 * character at the end of the stack. If replace_offset is not 0, that many
7549 * characters will be left on the stack above the newly inserted character.
7550 */
7551
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007552static char_u *replace_stack = NULL;
7553static long replace_stack_nr = 0; /* next entry in replace stack */
7554static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555
7556 void
7557replace_push(c)
7558 int c; /* character that is replaced (NUL is none) */
7559{
7560 char_u *p;
7561
7562 if (replace_stack_nr < replace_offset) /* nothing to do */
7563 return;
7564 if (replace_stack_len <= replace_stack_nr)
7565 {
7566 replace_stack_len += 50;
7567 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7568 if (p == NULL) /* out of memory */
7569 {
7570 replace_stack_len -= 50;
7571 return;
7572 }
7573 if (replace_stack != NULL)
7574 {
7575 mch_memmove(p, replace_stack,
7576 (size_t)(replace_stack_nr * sizeof(char_u)));
7577 vim_free(replace_stack);
7578 }
7579 replace_stack = p;
7580 }
7581 p = replace_stack + replace_stack_nr - replace_offset;
7582 if (replace_offset)
7583 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7584 *p = c;
7585 ++replace_stack_nr;
7586}
7587
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007588#if defined(FEAT_MBYTE) || defined(PROTO)
7589/*
7590 * Push a character onto the replace stack. Handles a multi-byte character in
7591 * reverse byte order, so that the first byte is popped off first.
7592 * Return the number of bytes done (includes composing characters).
7593 */
7594 int
7595replace_push_mb(p)
7596 char_u *p;
7597{
7598 int l = (*mb_ptr2len)(p);
7599 int j;
7600
7601 for (j = l - 1; j >= 0; --j)
7602 replace_push(p[j]);
7603 return l;
7604}
7605#endif
7606
Bram Moolenaar071d4272004-06-13 20:20:40 +00007607/*
7608 * Pop one item from the replace stack.
7609 * return -1 if stack empty
7610 * return replaced character or NUL otherwise
7611 */
7612 static int
7613replace_pop()
7614{
7615 if (replace_stack_nr == 0)
7616 return -1;
7617 return (int)replace_stack[--replace_stack_nr];
7618}
7619
7620/*
7621 * Join the top two items on the replace stack. This removes to "off"'th NUL
7622 * encountered.
7623 */
7624 static void
7625replace_join(off)
7626 int off; /* offset for which NUL to remove */
7627{
7628 int i;
7629
7630 for (i = replace_stack_nr; --i >= 0; )
7631 if (replace_stack[i] == NUL && off-- <= 0)
7632 {
7633 --replace_stack_nr;
7634 mch_memmove(replace_stack + i, replace_stack + i + 1,
7635 (size_t)(replace_stack_nr - i));
7636 return;
7637 }
7638}
7639
7640/*
7641 * Pop bytes from the replace stack until a NUL is found, and insert them
7642 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7643 */
7644 static void
7645replace_pop_ins()
7646{
7647 int cc;
7648 int oldState = State;
7649
7650 State = NORMAL; /* don't want REPLACE here */
7651 while ((cc = replace_pop()) > 0)
7652 {
7653#ifdef FEAT_MBYTE
7654 mb_replace_pop_ins(cc);
7655#else
7656 ins_char(cc);
7657#endif
7658 dec_cursor();
7659 }
7660 State = oldState;
7661}
7662
7663#ifdef FEAT_MBYTE
7664/*
7665 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7666 * indicates a multi-byte char, pop the other bytes too.
7667 */
7668 static void
7669mb_replace_pop_ins(cc)
7670 int cc;
7671{
7672 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007673 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007674 int i;
7675 int c;
7676
7677 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7678 {
7679 buf[0] = cc;
7680 for (i = 1; i < n; ++i)
7681 buf[i] = replace_pop();
7682 ins_bytes_len(buf, n);
7683 }
7684 else
7685 ins_char(cc);
7686
7687 if (enc_utf8)
7688 /* Handle composing chars. */
7689 for (;;)
7690 {
7691 c = replace_pop();
7692 if (c == -1) /* stack empty */
7693 break;
7694 if ((n = MB_BYTE2LEN(c)) == 1)
7695 {
7696 /* Not a multi-byte char, put it back. */
7697 replace_push(c);
7698 break;
7699 }
7700 else
7701 {
7702 buf[0] = c;
7703 for (i = 1; i < n; ++i)
7704 buf[i] = replace_pop();
7705 if (utf_iscomposing(utf_ptr2char(buf)))
7706 ins_bytes_len(buf, n);
7707 else
7708 {
7709 /* Not a composing char, put it back. */
7710 for (i = n - 1; i >= 0; --i)
7711 replace_push(buf[i]);
7712 break;
7713 }
7714 }
7715 }
7716}
7717#endif
7718
7719/*
7720 * make the replace stack empty
7721 * (called when exiting replace mode)
7722 */
7723 static void
7724replace_flush()
7725{
7726 vim_free(replace_stack);
7727 replace_stack = NULL;
7728 replace_stack_len = 0;
7729 replace_stack_nr = 0;
7730}
7731
7732/*
7733 * Handle doing a BS for one character.
7734 * cc < 0: replace stack empty, just move cursor
7735 * cc == 0: character was inserted, delete it
7736 * cc > 0: character was replaced, put cc (first byte of original char) back
7737 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007738 * When "limit_col" is >= 0, don't delete before this column. Matters when
7739 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 */
7741 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007742replace_do_bs(limit_col)
7743 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007744{
7745 int cc;
7746#ifdef FEAT_VREPLACE
7747 int orig_len = 0;
7748 int ins_len;
7749 int orig_vcols = 0;
7750 colnr_T start_vcol;
7751 char_u *p;
7752 int i;
7753 int vcol;
7754#endif
7755
7756 cc = replace_pop();
7757 if (cc > 0)
7758 {
7759#ifdef FEAT_VREPLACE
7760 if (State & VREPLACE_FLAG)
7761 {
7762 /* Get the number of screen cells used by the character we are
7763 * going to delete. */
7764 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7765 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7766 }
7767#endif
7768#ifdef FEAT_MBYTE
7769 if (has_mbyte)
7770 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007771 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007772# ifdef FEAT_VREPLACE
7773 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007774 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775# endif
7776 replace_push(cc);
7777 }
7778 else
7779#endif
7780 {
7781 pchar_cursor(cc);
7782#ifdef FEAT_VREPLACE
7783 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007784 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785#endif
7786 }
7787 replace_pop_ins();
7788
7789#ifdef FEAT_VREPLACE
7790 if (State & VREPLACE_FLAG)
7791 {
7792 /* Get the number of screen cells used by the inserted characters */
7793 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007794 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007795 vcol = start_vcol;
7796 for (i = 0; i < ins_len; ++i)
7797 {
7798 vcol += chartabsize(p + i, vcol);
7799#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007800 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801#endif
7802 }
7803 vcol -= start_vcol;
7804
7805 /* Delete spaces that were inserted after the cursor to keep the
7806 * text aligned. */
7807 curwin->w_cursor.col += ins_len;
7808 while (vcol > orig_vcols && gchar_cursor() == ' ')
7809 {
7810 del_char(FALSE);
7811 ++orig_vcols;
7812 }
7813 curwin->w_cursor.col -= ins_len;
7814 }
7815#endif
7816
7817 /* mark the buffer as changed and prepare for displaying */
7818 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7819 }
7820 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007821 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007822}
7823
7824#ifdef FEAT_CINDENT
7825/*
7826 * Return TRUE if C-indenting is on.
7827 */
7828 static int
7829cindent_on()
7830{
7831 return (!p_paste && (curbuf->b_p_cin
7832# ifdef FEAT_EVAL
7833 || *curbuf->b_p_inde != NUL
7834# endif
7835 ));
7836}
7837#endif
7838
7839#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7840/*
7841 * Re-indent the current line, based on the current contents of it and the
7842 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7843 * confused what all the part that handles Control-T is doing that I'm not.
7844 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7845 */
7846
7847 void
7848fixthisline(get_the_indent)
7849 int (*get_the_indent) __ARGS((void));
7850{
Bram Moolenaarf7bb86d2015-07-28 21:17:36 +02007851 int amount = get_the_indent();
7852
7853 if (amount >= 0)
7854 {
7855 change_indent(INDENT_SET, amount, FALSE, 0, TRUE);
7856 if (linewhite(curwin->w_cursor.lnum))
7857 did_ai = TRUE; /* delete the indent if the line stays empty */
7858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859}
7860
7861 void
7862fix_indent()
7863{
7864 if (p_paste)
7865 return;
7866# ifdef FEAT_LISP
7867 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7868 fixthisline(get_lisp_indent);
7869# endif
7870# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7871 else
7872# endif
7873# ifdef FEAT_CINDENT
7874 if (cindent_on())
7875 do_c_expr_indent();
7876# endif
7877}
7878
7879#endif
7880
7881#ifdef FEAT_CINDENT
7882/*
7883 * return TRUE if 'cinkeys' contains the key "keytyped",
7884 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007885 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007886 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7887 *
7888 * "keytyped" can have a few special values:
7889 * KEY_OPEN_FORW
7890 * KEY_OPEN_BACK
7891 * KEY_COMPLETE just finished completion.
7892 *
7893 * If line_is_empty is TRUE accept keys with '0' before them.
7894 */
7895 int
7896in_cinkeys(keytyped, when, line_is_empty)
7897 int keytyped;
7898 int when;
7899 int line_is_empty;
7900{
7901 char_u *look;
7902 int try_match;
7903 int try_match_word;
7904 char_u *p;
7905 char_u *line;
7906 int icase;
7907 int i;
7908
Bram Moolenaar30848942009-12-24 14:46:12 +00007909 if (keytyped == NUL)
7910 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7911 return FALSE;
7912
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913#ifdef FEAT_EVAL
7914 if (*curbuf->b_p_inde != NUL)
7915 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7916 else
7917#endif
7918 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7919 while (*look)
7920 {
7921 /*
7922 * Find out if we want to try a match with this key, depending on
7923 * 'when' and a '*' or '!' before the key.
7924 */
7925 switch (when)
7926 {
7927 case '*': try_match = (*look == '*'); break;
7928 case '!': try_match = (*look == '!'); break;
7929 default: try_match = (*look != '*'); break;
7930 }
7931 if (*look == '*' || *look == '!')
7932 ++look;
7933
7934 /*
7935 * If there is a '0', only accept a match if the line is empty.
7936 * But may still match when typing last char of a word.
7937 */
7938 if (*look == '0')
7939 {
7940 try_match_word = try_match;
7941 if (!line_is_empty)
7942 try_match = FALSE;
7943 ++look;
7944 }
7945 else
7946 try_match_word = FALSE;
7947
7948 /*
7949 * does it look like a control character?
7950 */
7951 if (*look == '^'
7952#ifdef EBCDIC
7953 && (Ctrl_chr(look[1]) != 0)
7954#else
7955 && look[1] >= '?' && look[1] <= '_'
7956#endif
7957 )
7958 {
7959 if (try_match && keytyped == Ctrl_chr(look[1]))
7960 return TRUE;
7961 look += 2;
7962 }
7963 /*
7964 * 'o' means "o" command, open forward.
7965 * 'O' means "O" command, open backward.
7966 */
7967 else if (*look == 'o')
7968 {
7969 if (try_match && keytyped == KEY_OPEN_FORW)
7970 return TRUE;
7971 ++look;
7972 }
7973 else if (*look == 'O')
7974 {
7975 if (try_match && keytyped == KEY_OPEN_BACK)
7976 return TRUE;
7977 ++look;
7978 }
7979
7980 /*
7981 * 'e' means to check for "else" at start of line and just before the
7982 * cursor.
7983 */
7984 else if (*look == 'e')
7985 {
7986 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7987 {
7988 p = ml_get_curline();
7989 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7990 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7991 return TRUE;
7992 }
7993 ++look;
7994 }
7995
7996 /*
7997 * ':' only causes an indent if it is at the end of a label or case
7998 * statement, or when it was before typing the ':' (to fix
7999 * class::method for C++).
8000 */
8001 else if (*look == ':')
8002 {
8003 if (try_match && keytyped == ':')
8004 {
8005 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008006 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008007 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00008008 /* Need to get the line again after cin_islabel(). */
8009 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008010 if (curwin->w_cursor.col > 2
8011 && p[curwin->w_cursor.col - 1] == ':'
8012 && p[curwin->w_cursor.col - 2] == ':')
8013 {
8014 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02008015 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01008016 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00008017 p = ml_get_curline();
8018 p[curwin->w_cursor.col - 1] = ':';
8019 if (i)
8020 return TRUE;
8021 }
8022 }
8023 ++look;
8024 }
8025
8026
8027 /*
8028 * Is it a key in <>, maybe?
8029 */
8030 else if (*look == '<')
8031 {
8032 if (try_match)
8033 {
8034 /*
8035 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
8036 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
8037 * >, *, : and ! keys if they really really want to.
8038 */
8039 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
8040 && keytyped == look[1])
8041 return TRUE;
8042
8043 if (keytyped == get_special_key_code(look + 1))
8044 return TRUE;
8045 }
8046 while (*look && *look != '>')
8047 look++;
8048 while (*look == '>')
8049 look++;
8050 }
8051
8052 /*
8053 * Is it a word: "=word"?
8054 */
8055 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
8056 {
8057 ++look;
8058 if (*look == '~')
8059 {
8060 icase = TRUE;
8061 ++look;
8062 }
8063 else
8064 icase = FALSE;
8065 p = vim_strchr(look, ',');
8066 if (p == NULL)
8067 p = look + STRLEN(look);
8068 if ((try_match || try_match_word)
8069 && curwin->w_cursor.col >= (colnr_T)(p - look))
8070 {
8071 int match = FALSE;
8072
8073#ifdef FEAT_INS_EXPAND
8074 if (keytyped == KEY_COMPLETE)
8075 {
8076 char_u *s;
8077
8078 /* Just completed a word, check if it starts with "look".
8079 * search back for the start of a word. */
8080 line = ml_get_curline();
8081# ifdef FEAT_MBYTE
8082 if (has_mbyte)
8083 {
8084 char_u *n;
8085
8086 for (s = line + curwin->w_cursor.col; s > line; s = n)
8087 {
8088 n = mb_prevptr(line, s);
8089 if (!vim_iswordp(n))
8090 break;
8091 }
8092 }
8093 else
8094# endif
8095 for (s = line + curwin->w_cursor.col; s > line; --s)
8096 if (!vim_iswordc(s[-1]))
8097 break;
8098 if (s + (p - look) <= line + curwin->w_cursor.col
8099 && (icase
8100 ? MB_STRNICMP(s, look, p - look)
8101 : STRNCMP(s, look, p - look)) == 0)
8102 match = TRUE;
8103 }
8104 else
8105#endif
8106 /* TODO: multi-byte */
8107 if (keytyped == (int)p[-1] || (icase && keytyped < 256
8108 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
8109 {
8110 line = ml_get_cursor();
8111 if ((curwin->w_cursor.col == (colnr_T)(p - look)
8112 || !vim_iswordc(line[-(p - look) - 1]))
8113 && (icase
8114 ? MB_STRNICMP(line - (p - look), look, p - look)
8115 : STRNCMP(line - (p - look), look, p - look))
8116 == 0)
8117 match = TRUE;
8118 }
8119 if (match && try_match_word && !try_match)
8120 {
8121 /* "0=word": Check if there are only blanks before the
8122 * word. */
8123 line = ml_get_curline();
8124 if ((int)(skipwhite(line) - line) !=
8125 (int)(curwin->w_cursor.col - (p - look)))
8126 match = FALSE;
8127 }
8128 if (match)
8129 return TRUE;
8130 }
8131 look = p;
8132 }
8133
8134 /*
8135 * ok, it's a boring generic character.
8136 */
8137 else
8138 {
8139 if (try_match && *look == keytyped)
8140 return TRUE;
8141 ++look;
8142 }
8143
8144 /*
8145 * Skip over ", ".
8146 */
8147 look = skip_to_option_part(look);
8148 }
8149 return FALSE;
8150}
8151#endif /* FEAT_CINDENT */
8152
8153#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8154/*
8155 * Map Hebrew keyboard when in hkmap mode.
8156 */
8157 int
8158hkmap(c)
8159 int c;
8160{
8161 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8162 {
8163 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8164 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8165 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8166 static char_u map[26] =
8167 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8168 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8169 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8170 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8171 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8172 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8173 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8174 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8175 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8176
8177 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8178 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8179 /* '-1'='sofit' */
8180 else if (c == 'x')
8181 return 'X';
8182 else if (c == 'q')
8183 return '\''; /* {geresh}={'} */
8184 else if (c == 246)
8185 return ' '; /* \"o --> ' ' for a german keyboard */
8186 else if (c == 228)
8187 return ' '; /* \"a --> ' ' -- / -- */
8188 else if (c == 252)
8189 return ' '; /* \"u --> ' ' -- / -- */
8190#ifdef EBCDIC
8191 else if (islower(c))
8192#else
8193 /* NOTE: islower() does not do the right thing for us on Linux so we
8194 * do this the same was as 5.7 and previous, so it works correctly on
8195 * all systems. Specifically, the e.g. Delete and Arrow keys are
8196 * munged and won't work if e.g. searching for Hebrew text.
8197 */
8198 else if (c >= 'a' && c <= 'z')
8199#endif
8200 return (int)(map[CharOrdLow(c)] + p_aleph);
8201 else
8202 return c;
8203 }
8204 else
8205 {
8206 switch (c)
8207 {
8208 case '`': return ';';
8209 case '/': return '.';
8210 case '\'': return ',';
8211 case 'q': return '/';
8212 case 'w': return '\'';
8213
8214 /* Hebrew letters - set offset from 'a' */
8215 case ',': c = '{'; break;
8216 case '.': c = 'v'; break;
8217 case ';': c = 't'; break;
8218 default: {
8219 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8220
8221#ifdef EBCDIC
8222 /* see note about islower() above */
8223 if (!islower(c))
8224#else
8225 if (c < 'a' || c > 'z')
8226#endif
8227 return c;
8228 c = str[CharOrdLow(c)];
8229 break;
8230 }
8231 }
8232
8233 return (int)(CharOrdLow(c) + p_aleph);
8234 }
8235}
8236#endif
8237
8238 static void
8239ins_reg()
8240{
8241 int need_redraw = FALSE;
8242 int regname;
8243 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008244 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245
8246 /*
8247 * If we are going to wait for a character, show a '"'.
8248 */
8249 pc_status = PC_STATUS_UNSET;
8250 if (redrawing() && !char_avail())
8251 {
8252 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008253 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008254
8255 edit_putchar('"', TRUE);
8256#ifdef FEAT_CMDL_INFO
8257 add_to_showcmd_c(Ctrl_R);
8258#endif
8259 }
8260
8261#ifdef USE_ON_FLY_SCROLL
8262 dont_scroll = TRUE; /* disallow scrolling here */
8263#endif
8264
8265 /*
8266 * Don't map the register name. This also prevents the mode message to be
8267 * deleted when ESC is hit.
8268 */
8269 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008270 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008272 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8273 {
8274 /* Get a third key for literal register insertion */
8275 literally = regname;
8276#ifdef FEAT_CMDL_INFO
8277 add_to_showcmd_c(literally);
8278#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008279 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 }
8282 --no_mapping;
8283
8284#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008285 /* Don't call u_sync() while typing the expression or giving an error
8286 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 ++no_u_sync;
8288 if (regname == '=')
8289 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008290# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008292# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008293 /* Sync undo when evaluating the expression calls setline() or
8294 * append(), so that it can be undone separately. */
8295 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008296
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008298# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 /* Restore the Input Method. */
8300 if (im_on)
8301 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008302# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008304 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8305 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008306 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 else
8310 {
8311#endif
8312 if (literally == Ctrl_O || literally == Ctrl_P)
8313 {
8314 /* Append the command to the redo buffer. */
8315 AppendCharToRedobuff(Ctrl_R);
8316 AppendCharToRedobuff(literally);
8317 AppendCharToRedobuff(regname);
8318
8319 do_put(regname, BACKWARD, 1L,
8320 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8321 }
8322 else if (insert_reg(regname, literally) == FAIL)
8323 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008324 vim_beep(BO_REG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325 need_redraw = TRUE; /* remove the '"' */
8326 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008327 else if (stop_insert_mode)
8328 /* When the '=' register was used and a function was invoked that
8329 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8330 * insert anything, need to remove the '"' */
8331 need_redraw = TRUE;
8332
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333#ifdef FEAT_EVAL
8334 }
8335 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008336 if (u_sync_once == 1)
8337 ins_need_undo = TRUE;
8338 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339#endif
8340#ifdef FEAT_CMDL_INFO
8341 clear_showcmd();
8342#endif
8343
8344 /* If the inserted register is empty, we need to remove the '"' */
8345 if (need_redraw || stuff_empty())
8346 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008347
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008348 /* Disallow starting Visual mode here, would get a weird mode. */
8349 if (!vis_active && VIsual_active)
8350 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008351}
8352
8353/*
8354 * CTRL-G commands in Insert mode.
8355 */
8356 static void
8357ins_ctrl_g()
8358{
8359 int c;
8360
8361#ifdef FEAT_INS_EXPAND
8362 /* Right after CTRL-X the cursor will be after the ruler. */
8363 setcursor();
8364#endif
8365
8366 /*
8367 * Don't map the second key. This also prevents the mode message to be
8368 * deleted when ESC is hit.
8369 */
8370 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008371 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 --no_mapping;
8373 switch (c)
8374 {
8375 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8376 case K_UP:
8377 case Ctrl_K:
8378 case 'k': ins_up(TRUE);
8379 break;
8380
8381 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8382 case K_DOWN:
8383 case Ctrl_J:
8384 case 'j': ins_down(TRUE);
8385 break;
8386
8387 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008388 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008390
8391 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008392 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008393 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008394 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 break;
8396
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02008397 /* CTRL-G U: do not break undo with the next char */
8398 case 'U':
8399 /* Allow one left/right cursor movement with the next char,
8400 * without breaking undo. */
8401 dont_sync_undo = MAYBE;
8402 break;
8403
Bram Moolenaar071d4272004-06-13 20:20:40 +00008404 /* Unknown CTRL-G command, reserved for future expansion. */
Bram Moolenaar165bc692015-07-21 17:53:25 +02008405 default: vim_beep(BO_CTRLG);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 }
8407}
8408
8409/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008410 * CTRL-^ in Insert mode.
8411 */
8412 static void
8413ins_ctrl_hat()
8414{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008415 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008416 {
8417 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8418 if (State & LANGMAP)
8419 {
8420 curbuf->b_p_iminsert = B_IMODE_NONE;
8421 State &= ~LANGMAP;
8422 }
8423 else
8424 {
8425 curbuf->b_p_iminsert = B_IMODE_LMAP;
8426 State |= LANGMAP;
8427#ifdef USE_IM_CONTROL
8428 im_set_active(FALSE);
8429#endif
8430 }
8431 }
8432#ifdef USE_IM_CONTROL
8433 else
8434 {
8435 /* There are no ":lmap" mappings, toggle IM */
8436 if (im_get_status())
8437 {
8438 curbuf->b_p_iminsert = B_IMODE_NONE;
8439 im_set_active(FALSE);
8440 }
8441 else
8442 {
8443 curbuf->b_p_iminsert = B_IMODE_IM;
8444 State &= ~LANGMAP;
8445 im_set_active(TRUE);
8446 }
8447 }
8448#endif
8449 set_iminsert_global();
8450 showmode();
8451#ifdef FEAT_GUI
8452 /* may show different cursor shape or color */
8453 if (gui.in_use)
8454 gui_update_cursor(TRUE, FALSE);
8455#endif
8456#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8457 /* Show/unshow value of 'keymap' in status lines. */
8458 status_redraw_curbuf();
8459#endif
8460}
8461
8462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008463 * Handle ESC in insert mode.
8464 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8465 * insert.
8466 */
8467 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008468ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008469 long *count;
8470 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008471 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008472{
8473 int temp;
8474 static int disabled_redraw = FALSE;
8475
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008476#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008477 check_spell_redraw();
8478#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479#if defined(FEAT_HANGULIN)
8480# if defined(ESC_CHG_TO_ENG_MODE)
8481 hangul_input_state_set(0);
8482# endif
8483 if (composing_hangul)
8484 {
8485 push_raw_key(composing_hangul_buffer, 2);
8486 composing_hangul = 0;
8487 }
8488#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489
8490 temp = curwin->w_cursor.col;
8491 if (disabled_redraw)
8492 {
8493 --RedrawingDisabled;
8494 disabled_redraw = FALSE;
8495 }
8496 if (!arrow_used)
8497 {
8498 /*
8499 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008500 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8501 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008502 */
8503 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008504 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505
8506 /*
8507 * Repeating insert may take a long time. Check for
8508 * interrupt now and then.
8509 */
8510 if (*count > 0)
8511 {
8512 line_breakcheck();
8513 if (got_int)
8514 *count = 0;
8515 }
8516
8517 if (--*count > 0) /* repeat what was typed */
8518 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008519 /* Vi repeats the insert without replacing characters. */
8520 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8521 State &= ~REPLACE_FLAG;
8522
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 (void)start_redo_ins();
8524 if (cmdchar == 'r' || cmdchar == 'v')
Bram Moolenaar4f5ce332014-07-30 16:00:58 +02008525 stuffRedoReadbuff(ESC_STR); /* no ESC in redo buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 ++RedrawingDisabled;
8527 disabled_redraw = TRUE;
8528 return FALSE; /* repeat the insert */
8529 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008530 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 undisplay_dollar();
8532 }
8533
8534 /* When an autoindent was removed, curswant stays after the
8535 * indent */
8536 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8537 curwin->w_set_curswant = TRUE;
8538
8539 /* Remember the last Insert position in the '^ mark. */
8540 if (!cmdmod.keepjumps)
8541 curbuf->b_last_insert = curwin->w_cursor;
8542
8543 /*
8544 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008545 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008547 if (!nomove
8548 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008549#ifdef FEAT_VIRTUALEDIT
8550 || curwin->w_cursor.coladd > 0
8551#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008552 )
8553 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008554 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555#ifdef FEAT_RIGHTLEFT
8556 && !revins_on
8557#endif
8558 )
8559 {
8560#ifdef FEAT_VIRTUALEDIT
8561 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8562 {
8563 oneleft();
8564 if (restart_edit != NUL)
8565 ++curwin->w_cursor.coladd;
8566 }
8567 else
8568#endif
8569 {
8570 --curwin->w_cursor.col;
8571#ifdef FEAT_MBYTE
8572 /* Correct cursor for multi-byte character. */
8573 if (has_mbyte)
8574 mb_adjust_cursor();
8575#endif
8576 }
8577 }
8578
8579#ifdef USE_IM_CONTROL
8580 /* Disable IM to allow typing English directly for Normal mode commands.
8581 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8582 * well). */
8583 if (!(State & LANGMAP))
8584 im_save_status(&curbuf->b_p_iminsert);
8585 im_set_active(FALSE);
8586#endif
8587
8588 State = NORMAL;
8589 /* need to position cursor again (e.g. when on a TAB ) */
8590 changed_cline_bef_curs();
8591
8592#ifdef FEAT_MOUSE
8593 setmouse();
8594#endif
8595#ifdef CURSOR_SHAPE
8596 ui_cursor_shape(); /* may show different cursor shape */
8597#endif
8598
8599 /*
8600 * When recording or for CTRL-O, need to display the new mode.
8601 * Otherwise remove the mode message.
8602 */
8603 if (Recording || restart_edit != NUL)
8604 showmode();
8605 else if (p_smd)
8606 MSG("");
8607
8608 return TRUE; /* exit Insert mode */
8609}
8610
8611#ifdef FEAT_RIGHTLEFT
8612/*
8613 * Toggle language: hkmap and revins_on.
8614 * Move to end of reverse inserted text.
8615 */
8616 static void
8617ins_ctrl_()
8618{
8619 if (revins_on && revins_chars && revins_scol >= 0)
8620 {
8621 while (gchar_cursor() != NUL && revins_chars--)
8622 ++curwin->w_cursor.col;
8623 }
8624 p_ri = !p_ri;
8625 revins_on = (State == INSERT && p_ri);
8626 if (revins_on)
8627 {
8628 revins_scol = curwin->w_cursor.col;
8629 revins_legal++;
8630 revins_chars = 0;
8631 undisplay_dollar();
8632 }
8633 else
8634 revins_scol = -1;
8635#ifdef FEAT_FKMAP
8636 if (p_altkeymap)
8637 {
8638 /*
8639 * to be consistent also for redo command, using '.'
8640 * set arrow_used to true and stop it - causing to redo
8641 * characters entered in one mode (normal/reverse insert).
8642 */
8643 arrow_used = TRUE;
8644 (void)stop_arrow();
8645 p_fkmap = curwin->w_p_rl ^ p_ri;
8646 if (p_fkmap && p_ri)
8647 State = INSERT;
8648 }
8649 else
8650#endif
8651 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8652 showmode();
8653}
8654#endif
8655
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656/*
8657 * If 'keymodel' contains "startsel", may start selection.
8658 * Returns TRUE when a CTRL-O and other keys stuffed.
8659 */
8660 static int
8661ins_start_select(c)
8662 int c;
8663{
8664 if (km_startsel)
8665 switch (c)
8666 {
8667 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669 case K_PAGEUP:
8670 case K_KPAGEUP:
8671 case K_PAGEDOWN:
8672 case K_KPAGEDOWN:
8673# ifdef MACOS
8674 case K_LEFT:
8675 case K_RIGHT:
8676 case K_UP:
8677 case K_DOWN:
8678 case K_END:
8679 case K_HOME:
8680# endif
8681 if (!(mod_mask & MOD_MASK_SHIFT))
8682 break;
8683 /* FALLTHROUGH */
8684 case K_S_LEFT:
8685 case K_S_RIGHT:
8686 case K_S_UP:
8687 case K_S_DOWN:
8688 case K_S_END:
8689 case K_S_HOME:
8690 /* Start selection right away, the cursor can move with
8691 * CTRL-O when beyond the end of the line. */
8692 start_selection();
8693
8694 /* Execute the key in (insert) Select mode. */
8695 stuffcharReadbuff(Ctrl_O);
8696 if (mod_mask)
8697 {
8698 char_u buf[4];
8699
8700 buf[0] = K_SPECIAL;
8701 buf[1] = KS_MODIFIER;
8702 buf[2] = mod_mask;
8703 buf[3] = NUL;
8704 stuffReadbuff(buf);
8705 }
8706 stuffcharReadbuff(c);
8707 return TRUE;
8708 }
8709 return FALSE;
8710}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711
8712/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008713 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008714 */
8715 static void
8716ins_insert(replaceState)
8717 int replaceState;
8718{
8719#ifdef FEAT_FKMAP
8720 if (p_fkmap && p_ri)
8721 {
8722 beep_flush();
8723 EMSG(farsi_text_3); /* encoded in Farsi */
8724 return;
8725 }
8726#endif
8727
8728#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008729# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008730 set_vim_var_string(VV_INSERTMODE,
8731 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008732# ifdef FEAT_VREPLACE
8733 replaceState == VREPLACE ? "v" :
8734# endif
8735 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008736# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008737 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8738#endif
8739 if (State & REPLACE_FLAG)
8740 State = INSERT | (State & LANGMAP);
8741 else
8742 State = replaceState | (State & LANGMAP);
8743 AppendCharToRedobuff(K_INS);
8744 showmode();
8745#ifdef CURSOR_SHAPE
8746 ui_cursor_shape(); /* may show different cursor shape */
8747#endif
8748}
8749
8750/*
8751 * Pressed CTRL-O in Insert mode.
8752 */
8753 static void
8754ins_ctrl_o()
8755{
8756#ifdef FEAT_VREPLACE
8757 if (State & VREPLACE_FLAG)
8758 restart_edit = 'V';
8759 else
8760#endif
8761 if (State & REPLACE_FLAG)
8762 restart_edit = 'R';
8763 else
8764 restart_edit = 'I';
8765#ifdef FEAT_VIRTUALEDIT
8766 if (virtual_active())
8767 ins_at_eol = FALSE; /* cursor always keeps its column */
8768 else
8769#endif
8770 ins_at_eol = (gchar_cursor() == NUL);
8771}
8772
8773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774 * If the cursor is on an indent, ^T/^D insert/delete one
8775 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008776 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008777 * with vi. But vi only supports ^T and ^D after an
8778 * autoindent, we support it everywhere.
8779 */
8780 static void
8781ins_shift(c, lastc)
8782 int c;
8783 int lastc;
8784{
8785 if (stop_arrow() == FAIL)
8786 return;
8787 AppendCharToRedobuff(c);
8788
8789 /*
8790 * 0^D and ^^D: remove all indent.
8791 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008792 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8793 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008794 {
8795 --curwin->w_cursor.col;
8796 (void)del_char(FALSE); /* delete the '^' or '0' */
8797 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8798 if (State & REPLACE_FLAG)
8799 replace_pop_ins();
8800 if (lastc == '^')
8801 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008802 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803 }
8804 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008805 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008806
8807 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8808 did_ai = FALSE;
8809#ifdef FEAT_SMARTINDENT
8810 did_si = FALSE;
8811 can_si = FALSE;
8812 can_si_back = FALSE;
8813#endif
8814#ifdef FEAT_CINDENT
8815 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8816#endif
8817}
8818
8819 static void
8820ins_del()
8821{
8822 int temp;
8823
8824 if (stop_arrow() == FAIL)
8825 return;
8826 if (gchar_cursor() == NUL) /* delete newline */
8827 {
8828 temp = curwin->w_cursor.col;
8829 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008830 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar165bc692015-07-21 17:53:25 +02008831 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 else
8833 curwin->w_cursor.col = temp;
8834 }
Bram Moolenaar165bc692015-07-21 17:53:25 +02008835 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8836 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 did_ai = FALSE;
8838#ifdef FEAT_SMARTINDENT
8839 did_si = FALSE;
8840 can_si = FALSE;
8841 can_si_back = FALSE;
8842#endif
8843 AppendCharToRedobuff(K_DEL);
8844}
8845
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008846static void ins_bs_one __ARGS((colnr_T *vcolp));
8847
8848/*
8849 * Delete one character for ins_bs().
8850 */
8851 static void
8852ins_bs_one(vcolp)
8853 colnr_T *vcolp;
8854{
8855 dec_cursor();
8856 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8857 if (State & REPLACE_FLAG)
8858 {
8859 /* Don't delete characters before the insert point when in
8860 * Replace mode */
8861 if (curwin->w_cursor.lnum != Insstart.lnum
8862 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008863 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008864 }
8865 else
8866 (void)del_char(FALSE);
8867}
8868
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869/*
8870 * Handle Backspace, delete-word and delete-line in Insert mode.
8871 * Return TRUE when backspace was actually used.
8872 */
8873 static int
8874ins_bs(c, mode, inserted_space_p)
8875 int c;
8876 int mode;
8877 int *inserted_space_p;
8878{
8879 linenr_T lnum;
8880 int cc;
8881 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008882 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883 colnr_T mincol;
8884 int did_backspace = FALSE;
8885 int in_indent;
8886 int oldState;
8887#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008888 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889#endif
8890
8891 /*
8892 * can't delete anything in an empty file
8893 * can't backup past first character in buffer
8894 * can't backup past starting point unless 'backspace' > 1
8895 * can backup to a previous line if 'backspace' == 0
8896 */
8897 if ( bufempty()
8898 || (
8899#ifdef FEAT_RIGHTLEFT
8900 !revins_on &&
8901#endif
8902 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8903 || (!can_bs(BS_START)
8904 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008905 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8906 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008907 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8908 && curwin->w_cursor.col <= ai_col)
8909 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8910 {
Bram Moolenaar165bc692015-07-21 17:53:25 +02008911 vim_beep(BO_BS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 return FALSE;
8913 }
8914
8915 if (stop_arrow() == FAIL)
8916 return FALSE;
8917 in_indent = inindent(0);
8918#ifdef FEAT_CINDENT
8919 if (in_indent)
8920 can_cindent = FALSE;
8921#endif
8922#ifdef FEAT_COMMENTS
8923 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8924#endif
8925#ifdef FEAT_RIGHTLEFT
8926 if (revins_on) /* put cursor after last inserted char */
8927 inc_cursor();
8928#endif
8929
8930#ifdef FEAT_VIRTUALEDIT
8931 /* Virtualedit:
8932 * BACKSPACE_CHAR eats a virtual space
8933 * BACKSPACE_WORD eats all coladd
8934 * BACKSPACE_LINE eats all coladd and keeps going
8935 */
8936 if (curwin->w_cursor.coladd > 0)
8937 {
8938 if (mode == BACKSPACE_CHAR)
8939 {
8940 --curwin->w_cursor.coladd;
8941 return TRUE;
8942 }
8943 if (mode == BACKSPACE_WORD)
8944 {
8945 curwin->w_cursor.coladd = 0;
8946 return TRUE;
8947 }
8948 curwin->w_cursor.coladd = 0;
8949 }
8950#endif
8951
8952 /*
8953 * delete newline!
8954 */
8955 if (curwin->w_cursor.col == 0)
8956 {
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008957 lnum = Insstart.lnum;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008958 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008959#ifdef FEAT_RIGHTLEFT
8960 || revins_on
8961#endif
8962 )
8963 {
8964 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8965 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8966 return FALSE;
Bram Moolenaarc3bbad02015-02-17 17:50:26 +01008967 --Insstart.lnum;
8968 Insstart.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 }
8970 /*
8971 * In replace mode:
8972 * cc < 0: NL was inserted, delete it
8973 * cc >= 0: NL was replaced, put original characters back
8974 */
8975 cc = -1;
8976 if (State & REPLACE_FLAG)
8977 cc = replace_pop(); /* returns -1 if NL was inserted */
8978 /*
8979 * In replace mode, in the line we started replacing, we only move the
8980 * cursor.
8981 */
8982 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8983 {
8984 dec_cursor();
8985 }
8986 else
8987 {
8988#ifdef FEAT_VREPLACE
8989 if (!(State & VREPLACE_FLAG)
8990 || curwin->w_cursor.lnum > orig_line_count)
8991#endif
8992 {
8993 temp = gchar_cursor(); /* remember current char */
8994 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008995
8996 /* When "aw" is in 'formatoptions' we must delete the space at
8997 * the end of the line, otherwise the line will be broken
8998 * again when auto-formatting. */
8999 if (has_format_option(FO_AUTO)
9000 && has_format_option(FO_WHITE_PAR))
9001 {
9002 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
9003 TRUE);
9004 int len;
9005
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009006 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00009007 if (len > 0 && ptr[len - 1] == ' ')
9008 ptr[len - 1] = NUL;
9009 }
9010
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02009011 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 if (temp == NUL && gchar_cursor() != NUL)
9013 inc_cursor();
9014 }
9015#ifdef FEAT_VREPLACE
9016 else
9017 dec_cursor();
9018#endif
9019
9020 /*
9021 * In REPLACE mode we have to put back the text that was replaced
9022 * by the NL. On the replace stack is first a NUL-terminated
9023 * sequence of characters that were deleted and then the
9024 * characters that NL replaced.
9025 */
9026 if (State & REPLACE_FLAG)
9027 {
9028 /*
9029 * Do the next ins_char() in NORMAL state, to
9030 * prevent ins_char() from replacing characters and
9031 * avoiding showmatch().
9032 */
9033 oldState = State;
9034 State = NORMAL;
9035 /*
9036 * restore characters (blanks) deleted after cursor
9037 */
9038 while (cc > 0)
9039 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009040 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041#ifdef FEAT_MBYTE
9042 mb_replace_pop_ins(cc);
9043#else
9044 ins_char(cc);
9045#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009046 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 cc = replace_pop();
9048 }
9049 /* restore the characters that NL replaced */
9050 replace_pop_ins();
9051 State = oldState;
9052 }
9053 }
9054 did_ai = FALSE;
9055 }
9056 else
9057 {
9058 /*
9059 * Delete character(s) before the cursor.
9060 */
9061#ifdef FEAT_RIGHTLEFT
9062 if (revins_on) /* put cursor on last inserted char */
9063 dec_cursor();
9064#endif
9065 mincol = 0;
9066 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009067 if (mode == BACKSPACE_LINE
9068 && (curbuf->b_p_ai
9069#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00009070 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00009071#endif
9072 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073#ifdef FEAT_RIGHTLEFT
9074 && !revins_on
9075#endif
9076 )
9077 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009078 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00009080 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00009082 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 }
9084
9085 /*
9086 * Handle deleting one 'shiftwidth' or 'softtabstop'.
9087 */
9088 if ( mode == BACKSPACE_CHAR
9089 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009090 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00009091 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00009092 && (*(ml_get_cursor() - 1) == TAB
9093 || (*(ml_get_cursor() - 1) == ' '
9094 && (!*inserted_space_p
9095 || arrow_used))))))
9096 {
9097 int ts;
9098 colnr_T vcol;
9099 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009100 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101
9102 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00009103 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009104 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009105 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009106 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009107 /* Compute the virtual column where we want to be. Since
9108 * 'showbreak' may get in the way, need to get the last column of
9109 * the previous character. */
9110 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009111 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 dec_cursor();
9113 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
9114 inc_cursor();
9115 want_vcol = (want_vcol / ts) * ts;
9116
9117 /* delete characters until we are at or before want_vcol */
9118 while (vcol > want_vcol
9119 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009120 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121
9122 /* insert extra spaces until we are at want_vcol */
9123 while (vcol < want_vcol)
9124 {
9125 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009126 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9127 && curwin->w_cursor.col < Insstart_orig.col)
9128 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129
9130#ifdef FEAT_VREPLACE
9131 if (State & VREPLACE_FLAG)
9132 ins_char(' ');
9133 else
9134#endif
9135 {
9136 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009137 if ((State & REPLACE_FLAG))
9138 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 }
9140 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9141 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009142
9143 /* If we are now back where we started delete one character. Can
9144 * happen when using 'sts' and 'linebreak'. */
9145 if (vcol >= start_vcol)
9146 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 }
9148
9149 /*
9150 * Delete upto starting point, start of line or previous word.
9151 */
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009152 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009154#ifdef FEAT_MBYTE
9155 int cclass = 0, prev_cclass = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009157 if (has_mbyte)
9158 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009160 do
9161 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009163 if (!revins_on) /* put cursor on char to be deleted */
9164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 dec_cursor();
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009166
9167 cc = gchar_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168#ifdef FEAT_MBYTE
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009169 /* look multi-byte character class */
9170 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009171 {
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009172 prev_cclass = cclass;
9173 cclass = mb_get_class(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009174 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009176
9177 /* start of word? */
9178 if (mode == BACKSPACE_WORD && !vim_isspace(cc))
9179 {
9180 mode = BACKSPACE_WORD_NOT_SPACE;
9181 temp = vim_iswordc(cc);
9182 }
9183 /* end of word? */
9184 else if (mode == BACKSPACE_WORD_NOT_SPACE
9185 && ((vim_isspace(cc) || vim_iswordc(cc) != temp)
9186#ifdef FEAT_MBYTE
9187 || prev_cclass != cclass
9188#endif
9189 ))
9190 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191#ifdef FEAT_RIGHTLEFT
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009192 if (!revins_on)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193#endif
Bram Moolenaar310f2d52015-03-24 17:49:51 +01009194 inc_cursor();
9195#ifdef FEAT_RIGHTLEFT
9196 else if (State & REPLACE_FLAG)
9197 dec_cursor();
9198#endif
9199 break;
9200 }
9201 if (State & REPLACE_FLAG)
9202 replace_do_bs(-1);
9203 else
9204 {
9205#ifdef FEAT_MBYTE
9206 if (enc_utf8 && p_deco)
9207 (void)utfc_ptr2char(ml_get_cursor(), cpc);
9208#endif
9209 (void)del_char(FALSE);
9210#ifdef FEAT_MBYTE
9211 /*
9212 * If there are combining characters and 'delcombine' is set
9213 * move the cursor back. Don't back up before the base
9214 * character.
9215 */
9216 if (enc_utf8 && p_deco && cpc[0] != NUL)
9217 inc_cursor();
9218#endif
9219#ifdef FEAT_RIGHTLEFT
9220 if (revins_chars)
9221 {
9222 revins_chars--;
9223 revins_legal++;
9224 }
9225 if (revins_on && gchar_cursor() == NUL)
9226 break;
9227#endif
9228 }
9229 /* Just a single backspace?: */
9230 if (mode == BACKSPACE_CHAR)
9231 break;
9232 } while (
9233#ifdef FEAT_RIGHTLEFT
9234 revins_on ||
9235#endif
9236 (curwin->w_cursor.col > mincol
9237 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9238 || curwin->w_cursor.col != Insstart_orig.col)));
9239 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 did_backspace = TRUE;
9241 }
9242#ifdef FEAT_SMARTINDENT
9243 did_si = FALSE;
9244 can_si = FALSE;
9245 can_si_back = FALSE;
9246#endif
9247 if (curwin->w_cursor.col <= 1)
9248 did_ai = FALSE;
9249 /*
9250 * It's a little strange to put backspaces into the redo
9251 * buffer, but it makes auto-indent a lot easier to deal
9252 * with.
9253 */
9254 AppendCharToRedobuff(c);
9255
9256 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009257 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9258 && curwin->w_cursor.col < Insstart_orig.col)
9259 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260
9261 /* vi behaviour: the cursor moves backward but the character that
9262 * was there remains visible
9263 * Vim behaviour: the cursor moves backward and the character that
9264 * was there is erased from the screen.
9265 * We can emulate the vi behaviour by pretending there is a dollar
9266 * displayed even when there isn't.
9267 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009268 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269 dollar_vcol = curwin->w_virtcol;
9270
Bram Moolenaarce3be472008-01-14 19:12:28 +00009271#ifdef FEAT_FOLDING
9272 /* When deleting a char the cursor line must never be in a closed fold.
9273 * E.g., when 'foldmethod' is indent and deleting the first non-white
9274 * char before a Tab. */
9275 if (did_backspace)
9276 foldOpenCursor();
9277#endif
9278
Bram Moolenaar071d4272004-06-13 20:20:40 +00009279 return did_backspace;
9280}
9281
9282#ifdef FEAT_MOUSE
9283 static void
9284ins_mouse(c)
9285 int c;
9286{
9287 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009288 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009289
9290# ifdef FEAT_GUI
9291 /* When GUI is active, also move/paste when 'mouse' is empty */
9292 if (!gui.in_use)
9293# endif
9294 if (!mouse_has(MOUSE_INSERT))
9295 return;
9296
9297 undisplay_dollar();
9298 tpos = curwin->w_cursor;
9299 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9300 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009301#ifdef FEAT_WINDOWS
9302 win_T *new_curwin = curwin;
9303
9304 if (curwin != old_curwin && win_valid(old_curwin))
9305 {
9306 /* Mouse took us to another window. We need to go back to the
9307 * previous one to stop insert there properly. */
9308 curwin = old_curwin;
9309 curbuf = curwin->w_buffer;
9310 }
9311#endif
9312 start_arrow(curwin == old_curwin ? &tpos : NULL);
9313#ifdef FEAT_WINDOWS
9314 if (curwin != new_curwin && win_valid(new_curwin))
9315 {
9316 curwin = new_curwin;
9317 curbuf = curwin->w_buffer;
9318 }
9319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009320# ifdef FEAT_CINDENT
9321 can_cindent = TRUE;
9322# endif
9323 }
9324
9325#ifdef FEAT_WINDOWS
9326 /* redraw status lines (in case another window became active) */
9327 redraw_statuslines();
9328#endif
9329}
9330
9331 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009332ins_mousescroll(dir)
9333 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009334{
9335 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009336# if defined(FEAT_WINDOWS)
9337 win_T *old_curwin = curwin;
9338# endif
9339# ifdef FEAT_INS_EXPAND
9340 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341# endif
9342
9343 tpos = curwin->w_cursor;
9344
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009345# ifdef FEAT_WINDOWS
9346 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009347 {
9348 int row, col;
9349
9350 row = mouse_row;
9351 col = mouse_col;
9352
9353 /* find the window at the pointer coordinates */
9354 curwin = mouse_find_win(&row, &col);
9355 curbuf = curwin->w_buffer;
9356 }
9357 if (curwin == old_curwin)
9358# endif
9359 undisplay_dollar();
9360
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009361# ifdef FEAT_INS_EXPAND
9362 /* Don't scroll the window in which completion is being done. */
9363 if (!pum_visible()
9364# if defined(FEAT_WINDOWS)
9365 || curwin != old_curwin
9366# endif
9367 )
9368# endif
9369 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009370 if (dir == MSCR_DOWN || dir == MSCR_UP)
9371 {
9372 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9373 scroll_redraw(dir,
9374 (long)(curwin->w_botline - curwin->w_topline));
9375 else
9376 scroll_redraw(dir, 3L);
9377 }
9378#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009379 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009380 {
9381 int val, step = 6;
9382
9383 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9384 step = W_WIDTH(curwin);
9385 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9386 if (val < 0)
9387 val = 0;
9388 gui_do_horiz_scroll(val, TRUE);
9389 }
9390#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009391# ifdef FEAT_INS_EXPAND
9392 did_scroll = TRUE;
9393# endif
9394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009395
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009396# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009397 curwin->w_redr_status = TRUE;
9398
9399 curwin = old_curwin;
9400 curbuf = curwin->w_buffer;
9401# endif
9402
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009403# ifdef FEAT_INS_EXPAND
9404 /* The popup menu may overlay the window, need to redraw it.
9405 * TODO: Would be more efficient to only redraw the windows that are
9406 * overlapped by the popup menu. */
9407 if (pum_visible() && did_scroll)
9408 {
9409 redraw_all_later(NOT_VALID);
9410 ins_compl_show_pum();
9411 }
9412# endif
9413
Bram Moolenaar071d4272004-06-13 20:20:40 +00009414 if (!equalpos(curwin->w_cursor, tpos))
9415 {
9416 start_arrow(&tpos);
9417# ifdef FEAT_CINDENT
9418 can_cindent = TRUE;
9419# endif
9420 }
9421}
9422#endif
9423
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009424#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009425 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009426ins_tabline(c)
9427 int c;
9428{
9429 /* We will be leaving the current window, unless closing another tab. */
9430 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9431 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9432 {
9433 undisplay_dollar();
9434 start_arrow(&curwin->w_cursor);
9435# ifdef FEAT_CINDENT
9436 can_cindent = TRUE;
9437# endif
9438 }
9439
9440 if (c == K_TABLINE)
9441 goto_tabpage(current_tab);
9442 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009443 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009444 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009445 redraw_statuslines(); /* will redraw the tabline when needed */
9446 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009447}
9448#endif
9449
9450#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451 void
9452ins_scroll()
9453{
9454 pos_T tpos;
9455
9456 undisplay_dollar();
9457 tpos = curwin->w_cursor;
9458 if (gui_do_scroll())
9459 {
9460 start_arrow(&tpos);
9461# ifdef FEAT_CINDENT
9462 can_cindent = TRUE;
9463# endif
9464 }
9465}
9466
9467 void
9468ins_horscroll()
9469{
9470 pos_T tpos;
9471
9472 undisplay_dollar();
9473 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009474 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 {
9476 start_arrow(&tpos);
9477# ifdef FEAT_CINDENT
9478 can_cindent = TRUE;
9479# endif
9480 }
9481}
9482#endif
9483
9484 static void
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009485ins_left(end_change)
9486 int end_change; /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487{
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 (oneleft() == OK)
9497 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009498#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9499 /* Only call start_arrow() when not busy with preediting, it will
9500 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9501 if (!im_is_preediting())
9502#endif
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009503 {
9504 start_arrow_with_change(&tpos, end_change);
9505 if (!end_change)
9506 AppendCharToRedobuff(K_LEFT);
9507 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009508#ifdef FEAT_RIGHTLEFT
9509 /* If exit reversed string, position is fixed */
9510 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9511 revins_legal++;
9512 revins_chars++;
9513#endif
9514 }
9515
9516 /*
9517 * if 'whichwrap' set for cursor in insert mode may go to
9518 * previous line
9519 */
9520 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9521 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009522 /* always break undo when moving upwards/downwards, else undo may break */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009523 start_arrow(&tpos);
9524 --(curwin->w_cursor.lnum);
9525 coladvance((colnr_T)MAXCOL);
9526 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9527 }
9528 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009529 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009530 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009531}
9532
9533 static void
9534ins_home(c)
9535 int c;
9536{
9537 pos_T tpos;
9538
9539#ifdef FEAT_FOLDING
9540 if ((fdo_flags & FDO_HOR) && KeyTyped)
9541 foldOpenCursor();
9542#endif
9543 undisplay_dollar();
9544 tpos = curwin->w_cursor;
9545 if (c == K_C_HOME)
9546 curwin->w_cursor.lnum = 1;
9547 curwin->w_cursor.col = 0;
9548#ifdef FEAT_VIRTUALEDIT
9549 curwin->w_cursor.coladd = 0;
9550#endif
9551 curwin->w_curswant = 0;
9552 start_arrow(&tpos);
9553}
9554
9555 static void
9556ins_end(c)
9557 int c;
9558{
9559 pos_T tpos;
9560
9561#ifdef FEAT_FOLDING
9562 if ((fdo_flags & FDO_HOR) && KeyTyped)
9563 foldOpenCursor();
9564#endif
9565 undisplay_dollar();
9566 tpos = curwin->w_cursor;
9567 if (c == K_C_END)
9568 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9569 coladvance((colnr_T)MAXCOL);
9570 curwin->w_curswant = MAXCOL;
9571
9572 start_arrow(&tpos);
9573}
9574
9575 static void
9576ins_s_left()
9577{
9578#ifdef FEAT_FOLDING
9579 if ((fdo_flags & FDO_HOR) && KeyTyped)
9580 foldOpenCursor();
9581#endif
9582 undisplay_dollar();
9583 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9584 {
9585 start_arrow(&curwin->w_cursor);
9586 (void)bck_word(1L, FALSE, FALSE);
9587 curwin->w_set_curswant = TRUE;
9588 }
9589 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009590 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591}
9592
9593 static void
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009594ins_right(end_change)
9595 int end_change; /* end undoable change */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596{
9597#ifdef FEAT_FOLDING
9598 if ((fdo_flags & FDO_HOR) && KeyTyped)
9599 foldOpenCursor();
9600#endif
9601 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009602 if (gchar_cursor() != NUL
9603#ifdef FEAT_VIRTUALEDIT
9604 || virtual_active()
9605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606 )
9607 {
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009608 start_arrow_with_change(&curwin->w_cursor, end_change);
9609 if (!end_change)
9610 AppendCharToRedobuff(K_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009611 curwin->w_set_curswant = TRUE;
9612#ifdef FEAT_VIRTUALEDIT
9613 if (virtual_active())
9614 oneright();
9615 else
9616#endif
9617 {
9618#ifdef FEAT_MBYTE
9619 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009620 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 else
9622#endif
9623 ++curwin->w_cursor.col;
9624 }
9625
9626#ifdef FEAT_RIGHTLEFT
9627 revins_legal++;
9628 if (revins_chars)
9629 revins_chars--;
9630#endif
9631 }
9632 /* if 'whichwrap' set for cursor in insert mode, may move the
9633 * cursor to the next line */
9634 else if (vim_strchr(p_ww, ']') != NULL
9635 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9636 {
9637 start_arrow(&curwin->w_cursor);
9638 curwin->w_set_curswant = TRUE;
9639 ++curwin->w_cursor.lnum;
9640 curwin->w_cursor.col = 0;
9641 }
9642 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009643 vim_beep(BO_CRSR);
Bram Moolenaar8b5f65a2015-09-01 19:26:12 +02009644 dont_sync_undo = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645}
9646
9647 static void
9648ins_s_right()
9649{
9650#ifdef FEAT_FOLDING
9651 if ((fdo_flags & FDO_HOR) && KeyTyped)
9652 foldOpenCursor();
9653#endif
9654 undisplay_dollar();
9655 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9656 || gchar_cursor() != NUL)
9657 {
9658 start_arrow(&curwin->w_cursor);
9659 (void)fwd_word(1L, FALSE, 0);
9660 curwin->w_set_curswant = TRUE;
9661 }
9662 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009663 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664}
9665
9666 static void
9667ins_up(startcol)
9668 int startcol; /* when TRUE move to Insstart.col */
9669{
9670 pos_T tpos;
9671 linenr_T old_topline = curwin->w_topline;
9672#ifdef FEAT_DIFF
9673 int old_topfill = curwin->w_topfill;
9674#endif
9675
9676 undisplay_dollar();
9677 tpos = curwin->w_cursor;
9678 if (cursor_up(1L, TRUE) == OK)
9679 {
9680 if (startcol)
9681 coladvance(getvcol_nolist(&Insstart));
9682 if (old_topline != curwin->w_topline
9683#ifdef FEAT_DIFF
9684 || old_topfill != curwin->w_topfill
9685#endif
9686 )
9687 redraw_later(VALID);
9688 start_arrow(&tpos);
9689#ifdef FEAT_CINDENT
9690 can_cindent = TRUE;
9691#endif
9692 }
9693 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009694 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009695}
9696
9697 static void
9698ins_pageup()
9699{
9700 pos_T tpos;
9701
9702 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009703
9704#ifdef FEAT_WINDOWS
9705 if (mod_mask & MOD_MASK_CTRL)
9706 {
9707 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009708 if (first_tabpage->tp_next != NULL)
9709 {
9710 start_arrow(&curwin->w_cursor);
9711 goto_tabpage(-1);
9712 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009713 return;
9714 }
9715#endif
9716
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 tpos = curwin->w_cursor;
9718 if (onepage(BACKWARD, 1L) == OK)
9719 {
9720 start_arrow(&tpos);
9721#ifdef FEAT_CINDENT
9722 can_cindent = TRUE;
9723#endif
9724 }
9725 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009726 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727}
9728
9729 static void
9730ins_down(startcol)
9731 int startcol; /* when TRUE move to Insstart.col */
9732{
9733 pos_T tpos;
9734 linenr_T old_topline = curwin->w_topline;
9735#ifdef FEAT_DIFF
9736 int old_topfill = curwin->w_topfill;
9737#endif
9738
9739 undisplay_dollar();
9740 tpos = curwin->w_cursor;
9741 if (cursor_down(1L, TRUE) == OK)
9742 {
9743 if (startcol)
9744 coladvance(getvcol_nolist(&Insstart));
9745 if (old_topline != curwin->w_topline
9746#ifdef FEAT_DIFF
9747 || old_topfill != curwin->w_topfill
9748#endif
9749 )
9750 redraw_later(VALID);
9751 start_arrow(&tpos);
9752#ifdef FEAT_CINDENT
9753 can_cindent = TRUE;
9754#endif
9755 }
9756 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009757 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758}
9759
9760 static void
9761ins_pagedown()
9762{
9763 pos_T tpos;
9764
9765 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009766
9767#ifdef FEAT_WINDOWS
9768 if (mod_mask & MOD_MASK_CTRL)
9769 {
9770 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009771 if (first_tabpage->tp_next != NULL)
9772 {
9773 start_arrow(&curwin->w_cursor);
9774 goto_tabpage(0);
9775 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009776 return;
9777 }
9778#endif
9779
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 tpos = curwin->w_cursor;
9781 if (onepage(FORWARD, 1L) == OK)
9782 {
9783 start_arrow(&tpos);
9784#ifdef FEAT_CINDENT
9785 can_cindent = TRUE;
9786#endif
9787 }
9788 else
Bram Moolenaar165bc692015-07-21 17:53:25 +02009789 vim_beep(BO_CRSR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790}
9791
9792#ifdef FEAT_DND
9793 static void
9794ins_drop()
9795{
9796 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9797}
9798#endif
9799
9800/*
9801 * Handle TAB in Insert or Replace mode.
9802 * Return TRUE when the TAB needs to be inserted like a normal character.
9803 */
9804 static int
9805ins_tab()
9806{
9807 int ind;
9808 int i;
9809 int temp;
9810
9811 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9812 Insstart_blank_vcol = get_nolist_virtcol();
9813 if (echeck_abbr(TAB + ABBR_OFF))
9814 return FALSE;
9815
9816 ind = inindent(0);
9817#ifdef FEAT_CINDENT
9818 if (ind)
9819 can_cindent = FALSE;
9820#endif
9821
9822 /*
9823 * When nothing special, insert TAB like a normal character
9824 */
9825 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009826 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009827 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 return TRUE;
9829
9830 if (stop_arrow() == FAIL)
9831 return TRUE;
9832
9833 did_ai = FALSE;
9834#ifdef FEAT_SMARTINDENT
9835 did_si = FALSE;
9836 can_si = FALSE;
9837 can_si_back = FALSE;
9838#endif
9839 AppendToRedobuff((char_u *)"\t");
9840
9841 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009842 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009843 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9844 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 else /* otherwise use 'tabstop' */
9846 temp = (int)curbuf->b_p_ts;
9847 temp -= get_nolist_virtcol() % temp;
9848
9849 /*
9850 * Insert the first space with ins_char(). It will delete one char in
9851 * replace mode. Insert the rest with ins_str(); it will not delete any
9852 * chars. For VREPLACE mode, we use ins_char() for all characters.
9853 */
9854 ins_char(' ');
9855 while (--temp > 0)
9856 {
9857#ifdef FEAT_VREPLACE
9858 if (State & VREPLACE_FLAG)
9859 ins_char(' ');
9860 else
9861#endif
9862 {
9863 ins_str((char_u *)" ");
9864 if (State & REPLACE_FLAG) /* no char replaced */
9865 replace_push(NUL);
9866 }
9867 }
9868
9869 /*
9870 * When 'expandtab' not set: Replace spaces by TABs where possible.
9871 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009872 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873 {
9874 char_u *ptr;
9875#ifdef FEAT_VREPLACE
9876 char_u *saved_line = NULL; /* init for GCC */
9877 pos_T pos;
9878#endif
9879 pos_T fpos;
9880 pos_T *cursor;
9881 colnr_T want_vcol, vcol;
9882 int change_col = -1;
9883 int save_list = curwin->w_p_list;
9884
9885 /*
9886 * Get the current line. For VREPLACE mode, don't make real changes
9887 * yet, just work on a copy of the line.
9888 */
9889#ifdef FEAT_VREPLACE
9890 if (State & VREPLACE_FLAG)
9891 {
9892 pos = curwin->w_cursor;
9893 cursor = &pos;
9894 saved_line = vim_strsave(ml_get_curline());
9895 if (saved_line == NULL)
9896 return FALSE;
9897 ptr = saved_line + pos.col;
9898 }
9899 else
9900#endif
9901 {
9902 ptr = ml_get_cursor();
9903 cursor = &curwin->w_cursor;
9904 }
9905
9906 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9907 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9908 curwin->w_p_list = FALSE;
9909
9910 /* Find first white before the cursor */
9911 fpos = curwin->w_cursor;
9912 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9913 {
9914 --fpos.col;
9915 --ptr;
9916 }
9917
9918 /* In Replace mode, don't change characters before the insert point. */
9919 if ((State & REPLACE_FLAG)
9920 && fpos.lnum == Insstart.lnum
9921 && fpos.col < Insstart.col)
9922 {
9923 ptr += Insstart.col - fpos.col;
9924 fpos.col = Insstart.col;
9925 }
9926
9927 /* compute virtual column numbers of first white and cursor */
9928 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9929 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9930
Bram Moolenaar597a4222014-06-25 14:39:50 +02009931 /* Use as many TABs as possible. Beware of 'breakindent', 'showbreak'
9932 * and 'linebreak' adding extra virtual columns. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009933 while (vim_iswhite(*ptr))
9934 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009935 i = lbr_chartabsize(NULL, (char_u *)"\t", vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 if (vcol + i > want_vcol)
9937 break;
9938 if (*ptr != TAB)
9939 {
9940 *ptr = TAB;
9941 if (change_col < 0)
9942 {
9943 change_col = fpos.col; /* Column of first change */
9944 /* May have to adjust Insstart */
9945 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9946 Insstart.col = fpos.col;
9947 }
9948 }
9949 ++fpos.col;
9950 ++ptr;
9951 vcol += i;
9952 }
9953
9954 if (change_col >= 0)
9955 {
9956 int repl_off = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +02009957 char_u *line = ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958
9959 /* Skip over the spaces we need. */
9960 while (vcol < want_vcol && *ptr == ' ')
9961 {
Bram Moolenaar597a4222014-06-25 14:39:50 +02009962 vcol += lbr_chartabsize(line, ptr, vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 ++ptr;
9964 ++repl_off;
9965 }
9966 if (vcol > want_vcol)
9967 {
9968 /* Must have a char with 'showbreak' just before it. */
9969 --ptr;
9970 --repl_off;
9971 }
9972 fpos.col += repl_off;
9973
9974 /* Delete following spaces. */
9975 i = cursor->col - fpos.col;
9976 if (i > 0)
9977 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009978 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009979 /* correct replace stack. */
9980 if ((State & REPLACE_FLAG)
9981#ifdef FEAT_VREPLACE
9982 && !(State & VREPLACE_FLAG)
9983#endif
9984 )
9985 for (temp = i; --temp >= 0; )
9986 replace_join(repl_off);
9987 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009988#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009989 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009990 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009991 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009992 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9993 (char_u *)"\t", 1);
9994 }
9995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009996 cursor->col -= i;
9997
9998#ifdef FEAT_VREPLACE
9999 /*
10000 * In VREPLACE mode, we haven't changed anything yet. Do it now by
10001 * backspacing over the changed spacing and then inserting the new
10002 * spacing.
10003 */
10004 if (State & VREPLACE_FLAG)
10005 {
10006 /* Backspace from real cursor to change_col */
10007 backspace_until_column(change_col);
10008
10009 /* Insert each char in saved_line from changed_col to
10010 * ptr-cursor */
10011 ins_bytes_len(saved_line + change_col,
10012 cursor->col - change_col);
10013 }
10014#endif
10015 }
10016
10017#ifdef FEAT_VREPLACE
10018 if (State & VREPLACE_FLAG)
10019 vim_free(saved_line);
10020#endif
10021 curwin->w_p_list = save_list;
10022 }
10023
10024 return FALSE;
10025}
10026
10027/*
10028 * Handle CR or NL in insert mode.
10029 * Return TRUE when out of memory or can't undo.
10030 */
10031 static int
10032ins_eol(c)
10033 int c;
10034{
10035 int i;
10036
10037 if (echeck_abbr(c + ABBR_OFF))
10038 return FALSE;
10039 if (stop_arrow() == FAIL)
10040 return TRUE;
10041 undisplay_dollar();
10042
10043 /*
10044 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
10045 * character under the cursor. Only push a NUL on the replace stack,
10046 * nothing to put back when the NL is deleted.
10047 */
10048 if ((State & REPLACE_FLAG)
10049#ifdef FEAT_VREPLACE
10050 && !(State & VREPLACE_FLAG)
10051#endif
10052 )
10053 replace_push(NUL);
10054
10055 /*
10056 * In VREPLACE mode, a NL replaces the rest of the line, and starts
10057 * replacing the next line, so we push all of the characters left on the
10058 * line onto the replace stack. This is not done here though, it is done
10059 * in open_line().
10060 */
10061
Bram Moolenaarf193fff2006-04-27 00:02:13 +000010062#ifdef FEAT_VIRTUALEDIT
10063 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
10064 * CTRL-O). */
10065 if (virtual_active() && curwin->w_cursor.coladd > 0)
10066 coladvance(getviscol());
10067#endif
10068
Bram Moolenaar071d4272004-06-13 20:20:40 +000010069#ifdef FEAT_RIGHTLEFT
10070# ifdef FEAT_FKMAP
10071 if (p_altkeymap && p_fkmap)
10072 fkmap(NL);
10073# endif
10074 /* NL in reverse insert will always start in the end of
10075 * current line. */
10076 if (revins_on)
10077 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
10078#endif
10079
10080 AppendToRedobuff(NL_STR);
10081 i = open_line(FORWARD,
10082#ifdef FEAT_COMMENTS
10083 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
10084#endif
10085 0, old_indent);
10086 old_indent = 0;
10087#ifdef FEAT_CINDENT
10088 can_cindent = TRUE;
10089#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +000010090#ifdef FEAT_FOLDING
10091 /* When inserting a line the cursor line must never be in a closed fold. */
10092 foldOpenCursor();
10093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000010094
10095 return (!i);
10096}
10097
10098#ifdef FEAT_DIGRAPHS
10099/*
10100 * Handle digraph in insert mode.
10101 * Returns character still to be inserted, or NUL when nothing remaining to be
10102 * done.
10103 */
10104 static int
10105ins_digraph()
10106{
10107 int c;
10108 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010109 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010110
10111 pc_status = PC_STATUS_UNSET;
10112 if (redrawing() && !char_avail())
10113 {
10114 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010115 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010116
10117 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010118 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010119#ifdef FEAT_CMDL_INFO
10120 add_to_showcmd_c(Ctrl_K);
10121#endif
10122 }
10123
10124#ifdef USE_ON_FLY_SCROLL
10125 dont_scroll = TRUE; /* disallow scrolling here */
10126#endif
10127
10128 /* don't map the digraph chars. This also prevents the
10129 * mode message to be deleted when ESC is hit */
10130 ++no_mapping;
10131 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010132 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010133 --no_mapping;
10134 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010135 if (did_putchar)
10136 /* when the line fits in 'columns' the '?' is at the start of the next
10137 * line and will not be removed by the redraw */
10138 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +020010139
Bram Moolenaar071d4272004-06-13 20:20:40 +000010140 if (IS_SPECIAL(c) || mod_mask) /* special key */
10141 {
10142#ifdef FEAT_CMDL_INFO
10143 clear_showcmd();
10144#endif
10145 insert_special(c, TRUE, FALSE);
10146 return NUL;
10147 }
10148 if (c != ESC)
10149 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010150 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 if (redrawing() && !char_avail())
10152 {
10153 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +000010154 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155
10156 if (char2cells(c) == 1)
10157 {
Bram Moolenaar754b5602006-02-09 23:53:20 +000010158 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010159 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010160 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010161 }
10162#ifdef FEAT_CMDL_INFO
10163 add_to_showcmd_c(c);
10164#endif
10165 }
10166 ++no_mapping;
10167 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010168 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 --no_mapping;
10170 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010171 if (did_putchar)
10172 /* when the line fits in 'columns' the '?' is at the start of the
10173 * next line and will not be removed by a redraw */
10174 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010175 if (cc != ESC)
10176 {
10177 AppendToRedobuff((char_u *)CTRL_V_STR);
10178 c = getdigraph(c, cc, TRUE);
10179#ifdef FEAT_CMDL_INFO
10180 clear_showcmd();
10181#endif
10182 return c;
10183 }
10184 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010185#ifdef FEAT_CMDL_INFO
10186 clear_showcmd();
10187#endif
10188 return NUL;
10189}
10190#endif
10191
10192/*
10193 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10194 * Returns the char to be inserted, or NUL if none found.
10195 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010196 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000010197ins_copychar(lnum)
10198 linenr_T lnum;
10199{
10200 int c;
10201 int temp;
10202 char_u *ptr, *prev_ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010203 char_u *line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010204
10205 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10206 {
Bram Moolenaar165bc692015-07-21 17:53:25 +020010207 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010208 return NUL;
10209 }
10210
10211 /* try to advance to the cursor column */
10212 temp = 0;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010213 line = ptr = ml_get(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010214 prev_ptr = ptr;
10215 validate_virtcol();
10216 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10217 {
10218 prev_ptr = ptr;
Bram Moolenaar597a4222014-06-25 14:39:50 +020010219 temp += lbr_chartabsize_adv(line, &ptr, (colnr_T)temp);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 }
10221 if ((colnr_T)temp > curwin->w_virtcol)
10222 ptr = prev_ptr;
10223
10224#ifdef FEAT_MBYTE
10225 c = (*mb_ptr2char)(ptr);
10226#else
10227 c = *ptr;
10228#endif
10229 if (c == NUL)
Bram Moolenaar165bc692015-07-21 17:53:25 +020010230 vim_beep(BO_COPY);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010231 return c;
10232}
10233
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010234/*
10235 * CTRL-Y or CTRL-E typed in Insert mode.
10236 */
10237 static int
10238ins_ctrl_ey(tc)
10239 int tc;
10240{
10241 int c = tc;
10242
10243#ifdef FEAT_INS_EXPAND
10244 if (ctrl_x_mode == CTRL_X_SCROLL)
10245 {
10246 if (c == Ctrl_Y)
10247 scrolldown_clamp();
10248 else
10249 scrollup_clamp();
10250 redraw_later(VALID);
10251 }
10252 else
10253#endif
10254 {
10255 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10256 if (c != NUL)
10257 {
10258 long tw_save;
10259
10260 /* The character must be taken literally, insert like it
10261 * was typed after a CTRL-V, and pretend 'textwidth'
10262 * wasn't set. Digits, 'o' and 'x' are special after a
10263 * CTRL-V, don't use it for these. */
10264 if (c < 256 && !isalnum(c))
10265 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10266 tw_save = curbuf->b_p_tw;
10267 curbuf->b_p_tw = -1;
10268 insert_special(c, TRUE, FALSE);
10269 curbuf->b_p_tw = tw_save;
10270#ifdef FEAT_RIGHTLEFT
10271 revins_chars++;
10272 revins_legal++;
10273#endif
10274 c = Ctrl_V; /* pretend CTRL-V is last character */
10275 auto_format(FALSE, TRUE);
10276 }
10277 }
10278 return c;
10279}
10280
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281#ifdef FEAT_SMARTINDENT
10282/*
10283 * Try to do some very smart auto-indenting.
10284 * Used when inserting a "normal" character.
10285 */
10286 static void
10287ins_try_si(c)
10288 int c;
10289{
10290 pos_T *pos, old_pos;
10291 char_u *ptr;
10292 int i;
10293 int temp;
10294
10295 /*
10296 * do some very smart indenting when entering '{' or '}'
10297 */
10298 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10299 {
10300 /*
10301 * for '}' set indent equal to indent of line containing matching '{'
10302 */
10303 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10304 {
10305 old_pos = curwin->w_cursor;
10306 /*
10307 * If the matching '{' has a ')' immediately before it (ignoring
10308 * white-space), then line up with the start of the line
10309 * containing the matching '(' if there is one. This handles the
10310 * case where an "if (..\n..) {" statement continues over multiple
10311 * lines -- webb
10312 */
10313 ptr = ml_get(pos->lnum);
10314 i = pos->col;
10315 if (i > 0) /* skip blanks before '{' */
10316 while (--i > 0 && vim_iswhite(ptr[i]))
10317 ;
10318 curwin->w_cursor.lnum = pos->lnum;
10319 curwin->w_cursor.col = i;
10320 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10321 curwin->w_cursor = *pos;
10322 i = get_indent();
10323 curwin->w_cursor = old_pos;
10324#ifdef FEAT_VREPLACE
10325 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010326 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 else
10328#endif
10329 (void)set_indent(i, SIN_CHANGED);
10330 }
10331 else if (curwin->w_cursor.col > 0)
10332 {
10333 /*
10334 * when inserting '{' after "O" reduce indent, but not
10335 * more than indent of previous line
10336 */
10337 temp = TRUE;
10338 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10339 {
10340 old_pos = curwin->w_cursor;
10341 i = get_indent();
10342 while (curwin->w_cursor.lnum > 1)
10343 {
10344 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10345
10346 /* ignore empty lines and lines starting with '#'. */
10347 if (*ptr != '#' && *ptr != NUL)
10348 break;
10349 }
10350 if (get_indent() >= i)
10351 temp = FALSE;
10352 curwin->w_cursor = old_pos;
10353 }
10354 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010355 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010356 }
10357 }
10358
10359 /*
10360 * set indent of '#' always to 0
10361 */
10362 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10363 {
10364 /* remember current indent for next line */
10365 old_indent = get_indent();
10366 (void)set_indent(0, SIN_CHANGED);
10367 }
10368
10369 /* Adjust ai_col, the char at this position can be deleted. */
10370 if (ai_col > curwin->w_cursor.col)
10371 ai_col = curwin->w_cursor.col;
10372}
10373#endif
10374
10375/*
10376 * Get the value that w_virtcol would have when 'list' is off.
10377 * Unless 'cpo' contains the 'L' flag.
10378 */
10379 static colnr_T
10380get_nolist_virtcol()
10381{
10382 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10383 return getvcol_nolist(&curwin->w_cursor);
10384 validate_virtcol();
10385 return curwin->w_virtcol;
10386}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010387
10388#ifdef FEAT_AUTOCMD
10389/*
10390 * Handle the InsertCharPre autocommand.
10391 * "c" is the character that was typed.
10392 * Return a pointer to allocated memory with the replacement string.
10393 * Return NULL to continue inserting "c".
10394 */
10395 static char_u *
10396do_insert_char_pre(c)
10397 int c;
10398{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010399 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010400 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010401
10402 /* Return quickly when there is nothing to do. */
10403 if (!has_insertcharpre())
10404 return NULL;
10405
Bram Moolenaar704984a2012-06-01 14:57:51 +020010406#ifdef FEAT_MBYTE
10407 if (has_mbyte)
10408 buf[(*mb_char2bytes)(c, buf)] = NUL;
10409 else
10410#endif
10411 {
10412 buf[0] = c;
10413 buf[1] = NUL;
10414 }
10415
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010416 /* Lock the text to avoid weird things from happening. */
10417 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010418 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010419
Bram Moolenaar704984a2012-06-01 14:57:51 +020010420 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010421 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010422 {
10423 /* Get the value of v:char. It may be empty or more than one
10424 * character. Only use it when changed, otherwise continue with the
10425 * original character to avoid breaking autoindent. */
10426 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10427 res = vim_strsave(get_vim_var_str(VV_CHAR));
10428 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010429
10430 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10431 --textlock;
10432
10433 return res;
10434}
10435#endif