blob: 57bd7761e1b78364db7e9402c47d787aa55965d4 [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 Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000056 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000060static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010061#ifdef FEAT_COMPL_FUNC
62static char e_complwin[] = N_("E839: Completion function changed window");
63static char e_compldel[] = N_("E840: Completion function deleted text");
64#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66/*
67 * Structure used to store one match for insert completion.
68 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000069typedef struct compl_S compl_T;
70struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
Bram Moolenaar572cb562005-08-05 21:35:02 +000072 compl_T *cp_next;
73 compl_T *cp_prev;
74 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000075 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000076 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000077 char_u *cp_fname; /* file containing the match, allocated when
78 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000079 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
80 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081};
82
Bram Moolenaar572cb562005-08-05 21:35:02 +000083#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#define FREE_FNAME (2)
85
86/*
87 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000088 * "compl_first_match" points to the start of the list.
89 * "compl_curr_match" points to the currently selected entry.
90 * "compl_shown_match" is different from compl_curr_match during
91 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000093static compl_T *compl_first_match = NULL;
94static compl_T *compl_curr_match = NULL;
95static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar779b74b2006-04-10 14:55:34 +000097/* After using a cursor key <Enter> selects a match in the popup menu,
98 * otherwise it inserts a line break. */
99static int compl_enter_selects = FALSE;
100
Bram Moolenaara6557602006-02-04 22:43:20 +0000101/* When "compl_leader" is not NULL only matches that start with this string
102 * are used. */
103static char_u *compl_leader = NULL;
104
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000105static int compl_get_longest = FALSE; /* put longest common string
106 in compl_leader */
107
Bram Moolenaara6557602006-02-04 22:43:20 +0000108static int compl_used_match; /* Selected one of the matches. When
109 FALSE the match was edited or using
110 the longest common string. */
111
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000112static int compl_was_interrupted = FALSE; /* didn't finish finding
113 completions. */
114
115static int compl_restarting = FALSE; /* don't insert match */
116
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000117/* When the first completion is done "compl_started" is set. When it's
118 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000119static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000120
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000121/* Set when doing something for completion that may call edit() recursively,
122 * which is not allowed. */
123static int compl_busy = FALSE;
124
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static int compl_matches = 0;
126static char_u *compl_pattern = NULL;
127static int compl_direction = FORWARD;
128static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000129static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000130static pos_T compl_startpos;
131static colnr_T compl_col = 0; /* column where the text starts
132 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static char_u *compl_orig_text = NULL; /* text as it was before
134 * completion started */
135static int compl_cont_mode = 0;
136static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaar82139082011-09-14 16:52:09 +0200138static int compl_opt_refresh_always = FALSE;
139
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000140static void ins_ctrl_x __ARGS((void));
141static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000142static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000143static 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 +0000144static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000145static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000146static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000148static void ins_compl_upd_pum __ARGS((void));
149static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000150static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000151static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000152static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000153static 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 +0000154static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static void ins_compl_free __ARGS((void));
156static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000157static int ins_compl_bs __ARGS((void));
Bram Moolenaar82139082011-09-14 16:52:09 +0200158static int ins_compl_need_restart __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000159static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000160static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar82139082011-09-14 16:52:09 +0200161static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000162static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000163static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000164static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000165static int ins_compl_prep __ARGS((int c));
Bram Moolenaar62951b12011-09-21 18:23:05 +0200166static void ins_compl_fixRedoBufForLeader __ARGS((char_u *ptr_arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000168#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
169static void ins_compl_add_list __ARGS((list_T *list));
Bram Moolenaar82139082011-09-14 16:52:09 +0200170static void ins_compl_add_dict __ARGS((dict_T *dict));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000171#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000172static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173static void ins_compl_delete __ARGS((void));
174static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000175static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000176static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000177static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000178static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000179static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000181static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#endif /* FEAT_INS_EXPAND */
183
184#define BACKSPACE_CHAR 1
185#define BACKSPACE_WORD 2
186#define BACKSPACE_WORD_NOT_SPACE 3
187#define BACKSPACE_LINE 4
188
Bram Moolenaar754b5602006-02-09 23:53:20 +0000189static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void ins_ctrl_v __ARGS((void));
191static void undisplay_dollar __ARGS((void));
192static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000193static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194static void check_auto_format __ARGS((int));
195static void redo_literal __ARGS((int c));
196static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000197#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000198static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000199static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000200static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000201#endif
Bram Moolenaarf332a652013-11-04 04:20:33 +0100202static void stop_insert __ARGS((pos_T *end_insert_pos, int esc, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203static int echeck_abbr __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204static int replace_pop __ARGS((void));
205static void replace_join __ARGS((int off));
206static void replace_pop_ins __ARGS((void));
207#ifdef FEAT_MBYTE
208static void mb_replace_pop_ins __ARGS((int cc));
209#endif
210static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000211static void replace_do_bs __ARGS((int limit_col));
212static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifdef FEAT_CINDENT
214static int cindent_on __ARGS((void));
215#endif
216static void ins_reg __ARGS((void));
217static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000218static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000219static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#ifdef FEAT_RIGHTLEFT
221static void ins_ctrl_ __ARGS((void));
222#endif
223#ifdef FEAT_VISUAL
224static int ins_start_select __ARGS((int c));
225#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000226static void ins_insert __ARGS((int replaceState));
227static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static void ins_shift __ARGS((int c, int lastc));
229static void ins_del __ARGS((void));
230static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
231#ifdef FEAT_MOUSE
232static void ins_mouse __ARGS((int c));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200233static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000235#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
236static void ins_tabline __ARGS((int c));
237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238static void ins_left __ARGS((void));
239static void ins_home __ARGS((int c));
240static void ins_end __ARGS((int c));
241static void ins_s_left __ARGS((void));
242static void ins_right __ARGS((void));
243static void ins_s_right __ARGS((void));
244static void ins_up __ARGS((int startcol));
245static void ins_pageup __ARGS((void));
246static void ins_down __ARGS((int startcol));
247static void ins_pagedown __ARGS((void));
248#ifdef FEAT_DND
249static void ins_drop __ARGS((void));
250#endif
251static int ins_tab __ARGS((void));
252static int ins_eol __ARGS((int c));
253#ifdef FEAT_DIGRAPHS
254static int ins_digraph __ARGS((void));
255#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000256static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_SMARTINDENT
258static void ins_try_si __ARGS((int c));
259#endif
260static colnr_T get_nolist_virtcol __ARGS((void));
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100261#ifdef FEAT_AUTOCMD
262static char_u *do_insert_char_pre __ARGS((int c));
263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
265static colnr_T Insstart_textlen; /* length of line when insert started */
266static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100267static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000268
269static char_u *last_insert = NULL; /* the text of the previous insert,
270 K_SPECIAL and CSI are escaped */
271static int last_insert_skip; /* nr of chars in front of previous insert */
272static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000273static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275#ifdef FEAT_CINDENT
276static int can_cindent; /* may do cindenting on this line */
277#endif
278
279static int old_indent = 0; /* for ^^D command in insert mode */
280
281#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000282static int revins_on; /* reverse insert mode on */
283static int revins_chars; /* how much to skip after edit */
284static int revins_legal; /* was the last char 'legal'? */
285static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286#endif
287
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288static int ins_need_undo; /* call u_save() before inserting a
289 char. Set when edit() is called.
290 after that arrow_used is used. */
291
292static int did_add_space = FALSE; /* auto_format() added an extra space
293 under the cursor */
294
295/*
296 * edit(): Start inserting text.
297 *
298 * "cmdchar" can be:
299 * 'i' normal insert command
300 * 'a' normal append command
301 * 'R' replace command
302 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
303 * but still only one <CR> is inserted. The <Esc> is not used for redo.
304 * 'g' "gI" command.
305 * 'V' "gR" command for Virtual Replace mode.
306 * 'v' "gr" command for single character Virtual Replace mode.
307 *
308 * This function is not called recursively. For CTRL-O commands, it returns
309 * and lets the caller handle the Normal-mode command.
310 *
311 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
312 */
313 int
314edit(cmdchar, startln, count)
315 int cmdchar;
316 int startln; /* if set, insert at start of line */
317 long count;
318{
319 int c = 0;
320 char_u *ptr;
321 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000322 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 int i;
325 int did_backspace = TRUE; /* previous char was backspace */
326#ifdef FEAT_CINDENT
327 int line_is_white = FALSE; /* line is empty before insert */
328#endif
329 linenr_T old_topline = 0; /* topline before insertion */
330#ifdef FEAT_DIFF
331 int old_topfill = -1;
332#endif
333 int inserted_space = FALSE; /* just inserted a space */
334 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000335 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000337 /* Remember whether editing was restarted after CTRL-O. */
338 did_restart_edit = restart_edit;
339
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 /* sleep before redrawing, needed for "CTRL-O :" that results in an
341 * error message */
342 check_for_delay(TRUE);
343
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100344 /* set Insstart_orig to Insstart */
345 update_Insstart_orig = TRUE;
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347#ifdef HAVE_SANDBOX
348 /* Don't allow inserting in the sandbox. */
349 if (sandbox != 0)
350 {
351 EMSG(_(e_sandbox));
352 return FALSE;
353 }
354#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000355 /* Don't allow changes in the buffer while editing the cmdline. The
356 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000357 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000358 {
359 EMSG(_(e_secure));
360 return FALSE;
361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000362
363#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000364 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000365 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000366 {
367 EMSG(_(e_secure));
368 return FALSE;
369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000370 ins_compl_clear(); /* clear stuff for CTRL-X mode */
371#endif
372
Bram Moolenaar843ee412004-06-30 16:16:41 +0000373#ifdef FEAT_AUTOCMD
374 /*
375 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
376 */
377 if (cmdchar != 'r' && cmdchar != 'v')
378 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100379 pos_T save_cursor = curwin->w_cursor;
380
Bram Moolenaar1e015462005-09-25 22:16:38 +0000381# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000382 if (cmdchar == 'R')
383 ptr = (char_u *)"r";
384 else if (cmdchar == 'V')
385 ptr = (char_u *)"v";
386 else
387 ptr = (char_u *)"i";
388 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200389 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000390# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000391 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100392
Bram Moolenaar097c9922013-05-19 21:15:15 +0200393 /* Make sure the cursor didn't move. Do call check_cursor_col() in
394 * case the text was modified. Since Insert mode was not started yet
395 * a call to check_cursor_col() may move the cursor, especially with
396 * the "A" command, thus set State to avoid that. Also check that the
397 * line number is still valid (lines may have been deleted).
398 * Do not restore if v:char was set to a non-empty string. */
399 if (!equalpos(curwin->w_cursor, save_cursor)
400# ifdef FEAT_EVAL
401 && *get_vim_var_str(VV_CHAR) == NUL
402# endif
403 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100404 {
405 int save_state = State;
406
407 curwin->w_cursor = save_cursor;
408 State = INSERT;
409 check_cursor_col();
410 State = save_state;
411 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000412 }
413#endif
414
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200415#ifdef FEAT_CONCEAL
416 /* Check if the cursor line needs redrawing before changing State. If
417 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200418 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200419#endif
420
Bram Moolenaar071d4272004-06-13 20:20:40 +0000421#ifdef FEAT_MOUSE
422 /*
423 * When doing a paste with the middle mouse button, Insstart is set to
424 * where the paste started.
425 */
426 if (where_paste_started.lnum != 0)
427 Insstart = where_paste_started;
428 else
429#endif
430 {
431 Insstart = curwin->w_cursor;
432 if (startln)
433 Insstart.col = 0;
434 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000435 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000436 Insstart_blank_vcol = MAXCOL;
437 if (!did_ai)
438 ai_col = 0;
439
440 if (cmdchar != NUL && restart_edit == 0)
441 {
442 ResetRedobuff();
443 AppendNumberToRedobuff(count);
444#ifdef FEAT_VREPLACE
445 if (cmdchar == 'V' || cmdchar == 'v')
446 {
447 /* "gR" or "gr" command */
448 AppendCharToRedobuff('g');
449 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
450 }
451 else
452#endif
453 {
454 AppendCharToRedobuff(cmdchar);
455 if (cmdchar == 'g') /* "gI" command */
456 AppendCharToRedobuff('I');
457 else if (cmdchar == 'r') /* "r<CR>" command */
458 count = 1; /* insert only one <CR> */
459 }
460 }
461
462 if (cmdchar == 'R')
463 {
464#ifdef FEAT_FKMAP
465 if (p_fkmap && p_ri)
466 {
467 beep_flush();
468 EMSG(farsi_text_3); /* encoded in Farsi */
469 State = INSERT;
470 }
471 else
472#endif
473 State = REPLACE;
474 }
475#ifdef FEAT_VREPLACE
476 else if (cmdchar == 'V' || cmdchar == 'v')
477 {
478 State = VREPLACE;
479 replaceState = VREPLACE;
480 orig_line_count = curbuf->b_ml.ml_line_count;
481 vr_lines_changed = 1;
482 }
483#endif
484 else
485 State = INSERT;
486
487 stop_insert_mode = FALSE;
488
489 /*
490 * Need to recompute the cursor position, it might move when the cursor is
491 * on a TAB or special character.
492 */
493 curs_columns(TRUE);
494
495 /*
496 * Enable langmap or IME, indicated by 'iminsert'.
497 * Note that IME may enabled/disabled without us noticing here, thus the
498 * 'iminsert' value may not reflect what is actually used. It is updated
499 * when hitting <Esc>.
500 */
501 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
502 State |= LANGMAP;
503#ifdef USE_IM_CONTROL
504 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
505#endif
506
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507#ifdef FEAT_MOUSE
508 setmouse();
509#endif
510#ifdef FEAT_CMDL_INFO
511 clear_showcmd();
512#endif
513#ifdef FEAT_RIGHTLEFT
514 /* there is no reverse replace mode */
515 revins_on = (State == INSERT && p_ri);
516 if (revins_on)
517 undisplay_dollar();
518 revins_chars = 0;
519 revins_legal = 0;
520 revins_scol = -1;
521#endif
522
523 /*
524 * Handle restarting Insert mode.
525 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
526 * restart_edit non-zero, and something in the stuff buffer.
527 */
528 if (restart_edit != 0 && stuff_empty())
529 {
530#ifdef FEAT_MOUSE
531 /*
532 * After a paste we consider text typed to be part of the insert for
533 * the pasted text. You can backspace over the pasted text too.
534 */
535 if (where_paste_started.lnum)
536 arrow_used = FALSE;
537 else
538#endif
539 arrow_used = TRUE;
540 restart_edit = 0;
541
542 /*
543 * If the cursor was after the end-of-line before the CTRL-O and it is
544 * now at the end-of-line, put it after the end-of-line (this is not
545 * correct in very rare cases).
546 * Also do this if curswant is greater than the current virtual
547 * column. Eg after "^O$" or "^O80|".
548 */
549 validate_virtcol();
550 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000551 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 || curwin->w_curswant > curwin->w_virtcol)
553 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
554 {
555 if (ptr[1] == NUL)
556 ++curwin->w_cursor.col;
557#ifdef FEAT_MBYTE
558 else if (has_mbyte)
559 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000560 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 if (ptr[i] == NUL)
562 curwin->w_cursor.col += i;
563 }
564#endif
565 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000566 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567 }
568 else
569 arrow_used = FALSE;
570
571 /* we are in insert mode now, don't need to start it anymore */
572 need_start_insertmode = FALSE;
573
574 /* Need to save the line for undo before inserting the first char. */
575 ins_need_undo = TRUE;
576
577#ifdef FEAT_MOUSE
578 where_paste_started.lnum = 0;
579#endif
580#ifdef FEAT_CINDENT
581 can_cindent = TRUE;
582#endif
583#ifdef FEAT_FOLDING
584 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
585 * restarting. */
586 if (!p_im && did_restart_edit == 0)
587 foldOpenCursor();
588#endif
589
590 /*
591 * If 'showmode' is set, show the current (insert/replace/..) mode.
592 * A warning message for changing a readonly file is given here, before
593 * actually changing anything. It's put after the mode, if any.
594 */
595 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000596 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597 i = showmode();
598
599 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000600 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601
602#ifdef CURSOR_SHAPE
603 ui_cursor_shape(); /* may show different cursor shape */
604#endif
605#ifdef FEAT_DIGRAPHS
606 do_digraph(-1); /* clear digraphs */
607#endif
608
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000609 /*
610 * Get the current length of the redo buffer, those characters have to be
611 * skipped if we want to get to the inserted characters.
612 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000613 ptr = get_inserted();
614 if (ptr == NULL)
615 new_insert_skip = 0;
616 else
617 {
618 new_insert_skip = (int)STRLEN(ptr);
619 vim_free(ptr);
620 }
621
622 old_indent = 0;
623
624 /*
625 * Main loop in Insert mode: repeat until Insert mode is left.
626 */
627 for (;;)
628 {
629#ifdef FEAT_RIGHTLEFT
630 if (!revins_legal)
631 revins_scol = -1; /* reset on illegal motions */
632 else
633 revins_legal = 0;
634#endif
635 if (arrow_used) /* don't repeat insert when arrow key used */
636 count = 0;
637
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100638 if (update_Insstart_orig)
639 Insstart_orig = Insstart;
640
Bram Moolenaar071d4272004-06-13 20:20:40 +0000641 if (stop_insert_mode)
642 {
643 /* ":stopinsert" used or 'insertmode' reset */
644 count = 0;
645 goto doESCkey;
646 }
647
648 /* set curwin->w_curswant for next K_DOWN or K_UP */
649 if (!arrow_used)
650 curwin->w_set_curswant = TRUE;
651
652 /* If there is no typeahead may check for timestamps (e.g., for when a
653 * menu invoked a shell command). */
654 if (stuff_empty())
655 {
656 did_check_timestamps = FALSE;
657 if (need_check_timestamps)
658 check_timestamps(FALSE);
659 }
660
661 /*
662 * When emsg() was called msg_scroll will have been set.
663 */
664 msg_scroll = FALSE;
665
666#ifdef FEAT_GUI
667 /* When 'mousefocus' is set a mouse movement may have taken us to
668 * another window. "need_mouse_correct" may then be set because of an
669 * autocommand. */
670 if (need_mouse_correct)
671 gui_mouse_correct();
672#endif
673
674#ifdef FEAT_FOLDING
675 /* Open fold at the cursor line, according to 'foldopen'. */
676 if (fdo_flags & FDO_INSERT)
677 foldOpenCursor();
678 /* Close folds where the cursor isn't, according to 'foldclose' */
679 if (!char_avail())
680 foldCheckClose();
681#endif
682
683 /*
684 * If we inserted a character at the last position of the last line in
685 * the window, scroll the window one line up. This avoids an extra
686 * redraw.
687 * This is detected when the cursor column is smaller after inserting
688 * something.
689 * Don't do this when the topline changed already, it has
690 * already been adjusted (by insertchar() calling open_line())).
691 */
692 if (curbuf->b_mod_set
693 && curwin->w_p_wrap
694 && !did_backspace
695 && curwin->w_topline == old_topline
696#ifdef FEAT_DIFF
697 && curwin->w_topfill == old_topfill
698#endif
699 )
700 {
701 mincol = curwin->w_wcol;
702 validate_cursor_col();
703
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000704 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 && curwin->w_wrow == W_WINROW(curwin)
706 + curwin->w_height - 1 - p_so
707 && (curwin->w_cursor.lnum != curwin->w_topline
708#ifdef FEAT_DIFF
709 || curwin->w_topfill > 0
710#endif
711 ))
712 {
713#ifdef FEAT_DIFF
714 if (curwin->w_topfill > 0)
715 --curwin->w_topfill;
716 else
717#endif
718#ifdef FEAT_FOLDING
719 if (hasFolding(curwin->w_topline, NULL, &old_topline))
720 set_topline(curwin, old_topline + 1);
721 else
722#endif
723 set_topline(curwin, curwin->w_topline + 1);
724 }
725 }
726
727 /* May need to adjust w_topline to show the cursor. */
728 update_topline();
729
730 did_backspace = FALSE;
731
732 validate_cursor(); /* may set must_redraw */
733
734 /*
735 * Redraw the display when no characters are waiting.
736 * Also shows mode, ruler and positions cursor.
737 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000738 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000739
740#ifdef FEAT_SCROLLBIND
741 if (curwin->w_p_scb)
742 do_check_scrollbind(TRUE);
743#endif
744
Bram Moolenaar860cae12010-06-05 23:22:07 +0200745#ifdef FEAT_CURSORBIND
746 if (curwin->w_p_crb)
747 do_check_cursorbind();
748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 update_curswant();
750 old_topline = curwin->w_topline;
751#ifdef FEAT_DIFF
752 old_topfill = curwin->w_topfill;
753#endif
754
755#ifdef USE_ON_FLY_SCROLL
756 dont_scroll = FALSE; /* allow scrolling here */
757#endif
758
759 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000760 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 */
762 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000763 do
764 {
765 c = safe_vgetc();
766 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000767
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000768#ifdef FEAT_AUTOCMD
769 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
770 did_cursorhold = TRUE;
771#endif
772
Bram Moolenaar071d4272004-06-13 20:20:40 +0000773#ifdef FEAT_RIGHTLEFT
774 if (p_hkmap && KeyTyped)
775 c = hkmap(c); /* Hebrew mode mapping */
776#endif
777#ifdef FEAT_FKMAP
778 if (p_fkmap && KeyTyped)
779 c = fkmap(c); /* Farsi mode mapping */
780#endif
781
782#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000783 /*
784 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000785 * and the cursor is still in the completed word. Only when there is
786 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000787 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000788 if (compl_started
789 && pum_wanted()
790 && curwin->w_cursor.col >= compl_col
791 && (compl_shown_match == NULL
792 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000793 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000794 /* BS: Delete one character from "compl_leader". */
795 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000796 && curwin->w_cursor.col > compl_col
797 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000798 continue;
799
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000800 /* When no match was selected or it was edited. */
801 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000802 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000803 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000804 * "compl_leader". Except when at the original match and
805 * there is nothing to add, CTRL-L works like CTRL-P then. */
806 if (c == Ctrl_L
807 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000808 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000809 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000810 {
811 ins_compl_addfrommatch();
812 continue;
813 }
814
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000815 /* A non-white character that fits in with the current
816 * completion: Add to "compl_leader". */
817 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000818 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100819#ifdef FEAT_AUTOCMD
820 /* Trigger InsertCharPre. */
821 char_u *str = do_insert_char_pre(c);
822 char_u *p;
823
824 if (str != NULL)
825 {
826 for (p = str; *p != NUL; mb_ptr_adv(p))
827 ins_compl_addleader(PTR2CHAR(p));
828 vim_free(str);
829 }
830 else
831#endif
832 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000833 continue;
834 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000835
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000836 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000837 * compl_enter_selects is set the Enter key does the same. */
838 if (c == Ctrl_Y || (compl_enter_selects
839 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000840 {
841 ins_compl_delete();
842 ins_compl_insert();
843 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000844 }
845 }
846
Bram Moolenaar071d4272004-06-13 20:20:40 +0000847 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
848 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000849 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000850 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000851 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852#endif
853
Bram Moolenaar488c6512005-08-11 20:09:58 +0000854 /* CTRL-\ CTRL-N goes to Normal mode,
855 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
856 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 if (c == Ctrl_BSL)
858 {
859 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000860 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 ++no_mapping;
862 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000863 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 --no_mapping;
865 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000866 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000868 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000869 vungetc(c);
870 c = Ctrl_BSL;
871 }
872 else if (c == Ctrl_G && p_im)
873 continue;
874 else
875 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000876 if (c == Ctrl_O)
877 {
878 ins_ctrl_o();
879 ins_at_eol = FALSE; /* cursor keeps its column */
880 nomove = TRUE;
881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000882 count = 0;
883 goto doESCkey;
884 }
885 }
886
887#ifdef FEAT_DIGRAPHS
888 c = do_digraph(c);
889#endif
890
891#ifdef FEAT_INS_EXPAND
892 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
893 goto docomplete;
894#endif
895 if (c == Ctrl_V || c == Ctrl_Q)
896 {
897 ins_ctrl_v();
898 c = Ctrl_V; /* pretend CTRL-V is last typed character */
899 continue;
900 }
901
902#ifdef FEAT_CINDENT
903 if (cindent_on()
904# ifdef FEAT_INS_EXPAND
905 && ctrl_x_mode == 0
906# endif
907 )
908 {
909 /* A key name preceded by a bang means this key is not to be
910 * inserted. Skip ahead to the re-indenting below.
911 * A key name preceded by a star means that indenting has to be
912 * done before inserting the key. */
913 line_is_white = inindent(0);
914 if (in_cinkeys(c, '!', line_is_white))
915 goto force_cindent;
916 if (can_cindent && in_cinkeys(c, '*', line_is_white)
917 && stop_arrow() == OK)
918 do_c_expr_indent();
919 }
920#endif
921
922#ifdef FEAT_RIGHTLEFT
923 if (curwin->w_p_rl)
924 switch (c)
925 {
926 case K_LEFT: c = K_RIGHT; break;
927 case K_S_LEFT: c = K_S_RIGHT; break;
928 case K_C_LEFT: c = K_C_RIGHT; break;
929 case K_RIGHT: c = K_LEFT; break;
930 case K_S_RIGHT: c = K_S_LEFT; break;
931 case K_C_RIGHT: c = K_C_LEFT; break;
932 }
933#endif
934
935#ifdef FEAT_VISUAL
936 /*
937 * If 'keymodel' contains "startsel", may start selection. If it
938 * does, a CTRL-O and c will be stuffed, we need to get these
939 * characters.
940 */
941 if (ins_start_select(c))
942 continue;
943#endif
944
945 /*
946 * The big switch to handle a character in insert mode.
947 */
948 switch (c)
949 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000950 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 if (echeck_abbr(ESC + ABBR_OFF))
952 break;
953 /*FALLTHROUGH*/
954
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000955 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956#ifdef FEAT_CMDWIN
957 if (c == Ctrl_C && cmdwin_type != 0)
958 {
959 /* Close the cmdline window. */
960 cmdwin_result = K_IGNORE;
961 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000962 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000963 goto doESCkey;
964 }
965#endif
966
967#ifdef UNIX
968do_intr:
969#endif
970 /* when 'insertmode' set, and not halfway a mapping, don't leave
971 * Insert mode */
972 if (goto_im())
973 {
974 if (got_int)
975 {
976 (void)vgetc(); /* flush all buffers */
977 got_int = FALSE;
978 }
979 else
980 vim_beep();
981 break;
982 }
983doESCkey:
984 /*
985 * This is the ONLY return from edit()!
986 */
987 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
988 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000989 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000990 o_lnum = curwin->w_cursor.lnum;
991
Bram Moolenaar488c6512005-08-11 20:09:58 +0000992 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000993 {
994#ifdef FEAT_AUTOCMD
995 if (cmdchar != 'r' && cmdchar != 'v')
996 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
997 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000998 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001000 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +00001001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 continue;
1003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 case Ctrl_Z: /* suspend when 'insertmode' set */
1005 if (!p_im)
1006 goto normalchar; /* insert CTRL-Z as normal char */
1007 stuffReadbuff((char_u *)":st\r");
1008 c = Ctrl_O;
1009 /*FALLTHROUGH*/
1010
1011 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001012#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001013 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001014 goto docomplete;
1015#endif
1016 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1017 break;
1018 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001019
1020#ifdef FEAT_VIRTUALEDIT
1021 /* don't move the cursor left when 'virtualedit' has "onemore". */
1022 if (ve_flags & VE_ONEMORE)
1023 {
1024 ins_at_eol = FALSE;
1025 nomove = TRUE;
1026 }
1027#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001028 count = 0;
1029 goto doESCkey;
1030
Bram Moolenaar572cb562005-08-05 21:35:02 +00001031 case K_INS: /* toggle insert/replace mode */
1032 case K_KINS:
1033 ins_insert(replaceState);
1034 break;
1035
1036 case K_SELECT: /* end of Select mode mapping - ignore */
1037 break;
1038
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001039#ifdef FEAT_SNIFF
1040 case K_SNIFF: /* Sniff command received */
1041 stuffcharReadbuff(K_SNIFF);
1042 goto doESCkey;
1043#endif
1044
1045 case K_HELP: /* Help key works like <ESC> <Help> */
1046 case K_F1:
1047 case K_XF1:
1048 stuffcharReadbuff(K_HELP);
1049 if (p_im)
1050 need_start_insertmode = TRUE;
1051 goto doESCkey;
1052
1053#ifdef FEAT_NETBEANS_INTG
1054 case K_F21: /* NetBeans command */
1055 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001056 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 --no_mapping;
1058 netbeans_keycommand(i);
1059 break;
1060#endif
1061
1062 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 case NUL:
1064 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 /* For ^@ the trailing ESC will end the insert, unless there is an
1066 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1068 && c != Ctrl_A && !p_im)
1069 goto doESCkey; /* quit insert mode */
1070 inserted_space = FALSE;
1071 break;
1072
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 ins_reg();
1075 auto_format(FALSE, TRUE);
1076 inserted_space = FALSE;
1077 break;
1078
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 ins_ctrl_g();
1081 break;
1082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 case Ctrl_HAT: /* switch input mode and/or langmap */
1084 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 break;
1086
1087#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 if (!p_ari)
1090 goto normalchar;
1091 ins_ctrl_();
1092 break;
1093#endif
1094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1097 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1098 goto docomplete;
1099#endif
1100 /* FALLTHROUGH */
1101
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001103# ifdef FEAT_INS_EXPAND
1104 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1105 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001106 if (has_compl_option(FALSE))
1107 goto docomplete;
1108 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 }
1110# endif
1111 ins_shift(c, lastc);
1112 auto_format(FALSE, TRUE);
1113 inserted_space = FALSE;
1114 break;
1115
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 case K_KDEL:
1118 ins_del();
1119 auto_format(FALSE, TRUE);
1120 break;
1121
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001122 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001123 case Ctrl_H:
1124 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1125 auto_format(FALSE, TRUE);
1126 break;
1127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001128 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1130 auto_format(FALSE, TRUE);
1131 break;
1132
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001133 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001134# ifdef FEAT_COMPL_FUNC
1135 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001137 goto docomplete;
1138# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1140 auto_format(FALSE, TRUE);
1141 inserted_space = FALSE;
1142 break;
1143
1144#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001145 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case K_LEFTMOUSE_NM:
1147 case K_LEFTDRAG:
1148 case K_LEFTRELEASE:
1149 case K_LEFTRELEASE_NM:
1150 case K_MIDDLEMOUSE:
1151 case K_MIDDLEDRAG:
1152 case K_MIDDLERELEASE:
1153 case K_RIGHTMOUSE:
1154 case K_RIGHTDRAG:
1155 case K_RIGHTRELEASE:
1156 case K_X1MOUSE:
1157 case K_X1DRAG:
1158 case K_X1RELEASE:
1159 case K_X2MOUSE:
1160 case K_X2DRAG:
1161 case K_X2RELEASE:
1162 ins_mouse(c);
1163 break;
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001166 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001170 ins_mousescroll(MSCR_UP);
1171 break;
1172
1173 case K_MOUSELEFT: /* Scroll wheel left */
1174 ins_mousescroll(MSCR_LEFT);
1175 break;
1176
1177 case K_MOUSERIGHT: /* Scroll wheel right */
1178 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 break;
1180#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001181#ifdef FEAT_GUI_TABLINE
1182 case K_TABLINE:
1183 case K_TABMENU:
1184 ins_tabline(c);
1185 break;
1186#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 break;
1190
Bram Moolenaar754b5602006-02-09 23:53:20 +00001191#ifdef FEAT_AUTOCMD
1192 case K_CURSORHOLD: /* Didn't type something for a while. */
1193 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1194 did_cursorhold = TRUE;
1195 break;
1196#endif
1197
Bram Moolenaar4770d092006-01-12 23:22:24 +00001198#ifdef FEAT_GUI_W32
1199 /* On Win32 ignore <M-F4>, we get it when closing the window was
1200 * cancelled. */
1201 case K_F4:
1202 if (mod_mask != MOD_MASK_ALT)
1203 goto normalchar;
1204 break;
1205#endif
1206
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207#ifdef FEAT_GUI
1208 case K_VER_SCROLLBAR:
1209 ins_scroll();
1210 break;
1211
1212 case K_HOR_SCROLLBAR:
1213 ins_horscroll();
1214 break;
1215#endif
1216
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001217 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 case K_S_HOME:
1220 case K_C_HOME:
1221 ins_home(c);
1222 break;
1223
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 case K_S_END:
1227 case K_C_END:
1228 ins_end(c);
1229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001232 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1233 ins_s_left();
1234 else
1235 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 break;
1237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 case K_C_LEFT:
1240 ins_s_left();
1241 break;
1242
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001243 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001244 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1245 ins_s_right();
1246 else
1247 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 break;
1249
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001250 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251 case K_C_RIGHT:
1252 ins_s_right();
1253 break;
1254
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001255 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001256#ifdef FEAT_INS_EXPAND
1257 if (pum_visible())
1258 goto docomplete;
1259#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001260 if (mod_mask & MOD_MASK_SHIFT)
1261 ins_pageup();
1262 else
1263 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 break;
1265
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001266 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 case K_PAGEUP:
1268 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001269#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001270 if (pum_visible())
1271 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 ins_pageup();
1274 break;
1275
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001276 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001277#ifdef FEAT_INS_EXPAND
1278 if (pum_visible())
1279 goto docomplete;
1280#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001281 if (mod_mask & MOD_MASK_SHIFT)
1282 ins_pagedown();
1283 else
1284 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001285 break;
1286
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001287 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 case K_PAGEDOWN:
1289 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001290#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001291 if (pum_visible())
1292 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001293#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001294 ins_pagedown();
1295 break;
1296
1297#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001298 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299 ins_drop();
1300 break;
1301#endif
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 c = TAB;
1305 /* FALLTHROUGH */
1306
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001307 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1309 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1310 goto docomplete;
1311#endif
1312 inserted_space = FALSE;
1313 if (ins_tab())
1314 goto normalchar; /* insert TAB as a normal char */
1315 auto_format(FALSE, TRUE);
1316 break;
1317
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001318 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319 c = CAR;
1320 /* FALLTHROUGH */
1321 case CAR:
1322 case NL:
1323#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1324 /* In a quickfix window a <CR> jumps to the error under the
1325 * cursor. */
1326 if (bt_quickfix(curbuf) && c == CAR)
1327 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001328 if (curwin->w_llist_ref == NULL) /* quickfix window */
1329 do_cmdline_cmd((char_u *)".cc");
1330 else /* location list window */
1331 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001332 break;
1333 }
1334#endif
1335#ifdef FEAT_CMDWIN
1336 if (cmdwin_type != 0)
1337 {
1338 /* Execute the command in the cmdline window. */
1339 cmdwin_result = CAR;
1340 goto doESCkey;
1341 }
1342#endif
1343 if (ins_eol(c) && !p_im)
1344 goto doESCkey; /* out of memory */
1345 auto_format(FALSE, FALSE);
1346 inserted_space = FALSE;
1347 break;
1348
Bram Moolenaar860cae12010-06-05 23:22:07 +02001349#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001350 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351# ifdef FEAT_INS_EXPAND
1352 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1353 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001354 if (has_compl_option(TRUE))
1355 goto docomplete;
1356 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 }
1358# endif
1359# ifdef FEAT_DIGRAPHS
1360 c = ins_digraph();
1361 if (c == NUL)
1362 break;
1363# endif
1364 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366
1367#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001368 case Ctrl_X: /* Enter CTRL-X mode */
1369 ins_ctrl_x();
1370 break;
1371
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001372 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373 if (ctrl_x_mode != CTRL_X_TAGS)
1374 goto normalchar;
1375 goto docomplete;
1376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001377 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 if (ctrl_x_mode != CTRL_X_FILES)
1379 goto normalchar;
1380 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001381
1382 case 's': /* Spelling completion after ^X */
1383 case Ctrl_S:
1384 if (ctrl_x_mode != CTRL_X_SPELL)
1385 goto normalchar;
1386 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387#endif
1388
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001389 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390#ifdef FEAT_INS_EXPAND
1391 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1392#endif
1393 {
1394 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1395 if (p_im)
1396 {
1397 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1398 break;
1399 goto doESCkey;
1400 }
1401 goto normalchar;
1402 }
1403#ifdef FEAT_INS_EXPAND
1404 /* FALLTHROUGH */
1405
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001406 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001407 case Ctrl_N:
1408 /* if 'complete' is empty then plain ^P is no longer special,
1409 * but it is under other ^X modes */
1410 if (*curbuf->b_p_cpt == NUL
1411 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001412 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 goto normalchar;
1414
1415docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001416 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001418 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001419 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 break;
1421#endif /* FEAT_INS_EXPAND */
1422
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001423 case Ctrl_Y: /* copy from previous line or scroll down */
1424 case Ctrl_E: /* copy from next line or scroll up */
1425 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426 break;
1427
1428 default:
1429#ifdef UNIX
1430 if (c == intr_char) /* special interrupt char */
1431 goto do_intr;
1432#endif
1433
Bram Moolenaare659c952011-05-19 17:25:41 +02001434normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001436 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001437 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001438#ifdef FEAT_AUTOCMD
1439 if (!p_paste)
1440 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001441 /* Trigger InsertCharPre. */
1442 char_u *str = do_insert_char_pre(c);
1443 char_u *p;
1444
1445 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001446 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001447 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001448 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001449 /* Insert the new value of v:char literally. */
1450 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001451 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001452 c = PTR2CHAR(p);
1453 if (c == CAR || c == K_KENTER || c == NL)
1454 ins_eol(c);
1455 else
1456 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001457 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001458 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001459 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001460 vim_free(str);
1461 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001462 }
1463
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001464 /* If the new value is already inserted or an empty string
1465 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001466 if (c == NUL)
1467 break;
1468 }
1469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001470#ifdef FEAT_SMARTINDENT
1471 /* Try to perform smart-indenting. */
1472 ins_try_si(c);
1473#endif
1474
1475 if (c == ' ')
1476 {
1477 inserted_space = TRUE;
1478#ifdef FEAT_CINDENT
1479 if (inindent(0))
1480 can_cindent = FALSE;
1481#endif
1482 if (Insstart_blank_vcol == MAXCOL
1483 && curwin->w_cursor.lnum == Insstart.lnum)
1484 Insstart_blank_vcol = get_nolist_virtcol();
1485 }
1486
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001487 /* Insert a normal character and check for abbreviations on a
1488 * special character. Let CTRL-] expand abbreviations without
1489 * inserting it. */
1490 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491#ifdef FEAT_MBYTE
1492 /* Add ABBR_OFF for characters above 0x100, this is
1493 * what check_abbr() expects. */
1494 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1495#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001496 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 {
1498 insert_special(c, FALSE, FALSE);
1499#ifdef FEAT_RIGHTLEFT
1500 revins_legal++;
1501 revins_chars++;
1502#endif
1503 }
1504
1505 auto_format(FALSE, TRUE);
1506
1507#ifdef FEAT_FOLDING
1508 /* When inserting a character the cursor line must never be in a
1509 * closed fold. */
1510 foldOpenCursor();
1511#endif
1512 break;
1513 } /* end of switch (c) */
1514
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001515#ifdef FEAT_AUTOCMD
1516 /* If typed something may trigger CursorHoldI again. */
1517 if (c != K_CURSORHOLD)
1518 did_cursorhold = FALSE;
1519#endif
1520
Bram Moolenaar071d4272004-06-13 20:20:40 +00001521 /* If the cursor was moved we didn't just insert a space */
1522 if (arrow_used)
1523 inserted_space = FALSE;
1524
1525#ifdef FEAT_CINDENT
1526 if (can_cindent && cindent_on()
1527# ifdef FEAT_INS_EXPAND
1528 && ctrl_x_mode == 0
1529# endif
1530 )
1531 {
1532force_cindent:
1533 /*
1534 * Indent now if a key was typed that is in 'cinkeys'.
1535 */
1536 if (in_cinkeys(c, ' ', line_is_white))
1537 {
1538 if (stop_arrow() == OK)
1539 /* re-indent the current line */
1540 do_c_expr_indent();
1541 }
1542 }
1543#endif /* FEAT_CINDENT */
1544
1545 } /* for (;;) */
1546 /* NOTREACHED */
1547}
1548
1549/*
1550 * Redraw for Insert mode.
1551 * This is postponed until getting the next character to make '$' in the 'cpo'
1552 * option work correctly.
1553 * Only redraw when there are no characters available. This speeds up
1554 * inserting sequences of characters (e.g., for CTRL-R).
1555 */
1556 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001557ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001558 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001560#ifdef FEAT_CONCEAL
1561 linenr_T conceal_old_cursor_line = 0;
1562 linenr_T conceal_new_cursor_line = 0;
1563 int conceal_update_lines = FALSE;
1564#endif
1565
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001566 if (char_avail())
1567 return;
1568
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001569#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001570 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1571 * visible, the command might delete it. */
1572 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001573# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001574 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001575# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001576# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001577 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001578# endif
1579# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001580 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001581# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001582 )
1583 && !equalpos(last_cursormoved, curwin->w_cursor)
1584# ifdef FEAT_INS_EXPAND
1585 && !pum_visible()
1586# endif
1587 )
1588 {
1589# ifdef FEAT_SYN_HL
1590 /* Need to update the screen first, to make sure syntax
1591 * highlighting is correct after making a change (e.g., inserting
1592 * a "(". The autocommand may also require a redraw, so it's done
1593 * again below, unfortunately. */
1594 if (syntax_present(curwin) && must_redraw)
1595 update_screen(0);
1596# endif
1597# ifdef FEAT_AUTOCMD
1598 if (has_cursormovedI())
1599 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1600# endif
1601# ifdef FEAT_CONCEAL
1602 if (curwin->w_p_cole > 0)
1603 {
1604 conceal_old_cursor_line = last_cursormoved.lnum;
1605 conceal_new_cursor_line = curwin->w_cursor.lnum;
1606 conceal_update_lines = TRUE;
1607 }
1608# endif
1609 last_cursormoved = curwin->w_cursor;
1610 }
1611#endif
1612
1613#ifdef FEAT_AUTOCMD
1614 /* Trigger TextChangedI if b_changedtick differs. */
1615 if (ready && has_textchangedI()
1616 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001617# ifdef FEAT_INS_EXPAND
1618 && !pum_visible()
1619# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001620 )
1621 {
1622 if (last_changedtick_buf == curbuf)
1623 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1624 last_changedtick_buf = curbuf;
1625 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001626 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001627#endif
1628
1629 if (must_redraw)
1630 update_screen(0);
1631 else if (clear_cmdline || redraw_cmdline)
1632 showmode(); /* clear cmdline and show mode */
1633# if defined(FEAT_CONCEAL)
1634 if ((conceal_update_lines
1635 && (conceal_old_cursor_line != conceal_new_cursor_line
1636 || conceal_cursor_line(curwin)))
1637 || need_cursor_line_redraw)
1638 {
1639 if (conceal_old_cursor_line != conceal_new_cursor_line)
1640 update_single_line(curwin, conceal_old_cursor_line);
1641 update_single_line(curwin, conceal_new_cursor_line == 0
1642 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1643 curwin->w_valid &= ~VALID_CROW;
1644 }
1645# endif
1646 showruler(FALSE);
1647 setcursor();
1648 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649}
1650
1651/*
1652 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1653 */
1654 static void
1655ins_ctrl_v()
1656{
1657 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001658 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659
1660 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001661 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662
1663 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001664 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001666 did_putchar = TRUE;
1667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1669
1670#ifdef FEAT_CMDL_INFO
1671 add_to_showcmd_c(Ctrl_V);
1672#endif
1673
1674 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001675 if (did_putchar)
1676 /* when the line fits in 'columns' the '^' is at the start of the next
1677 * line and will not removed by the redraw */
1678 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001679#ifdef FEAT_CMDL_INFO
1680 clear_showcmd();
1681#endif
1682 insert_special(c, FALSE, TRUE);
1683#ifdef FEAT_RIGHTLEFT
1684 revins_chars++;
1685 revins_legal++;
1686#endif
1687}
1688
1689/*
1690 * Put a character directly onto the screen. It's not stored in a buffer.
1691 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1692 */
1693static int pc_status;
1694#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1695#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1696#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1697#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001698static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001699static int pc_attr;
1700static int pc_row;
1701static int pc_col;
1702
1703 void
1704edit_putchar(c, highlight)
1705 int c;
1706 int highlight;
1707{
1708 int attr;
1709
1710 if (ScreenLines != NULL)
1711 {
1712 update_topline(); /* just in case w_topline isn't valid */
1713 validate_cursor();
1714 if (highlight)
1715 attr = hl_attr(HLF_8);
1716 else
1717 attr = 0;
1718 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1719 pc_col = W_WINCOL(curwin);
1720#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1721 pc_status = PC_STATUS_UNSET;
1722#endif
1723#ifdef FEAT_RIGHTLEFT
1724 if (curwin->w_p_rl)
1725 {
1726 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1727# ifdef FEAT_MBYTE
1728 if (has_mbyte)
1729 {
1730 int fix_col = mb_fix_col(pc_col, pc_row);
1731
1732 if (fix_col != pc_col)
1733 {
1734 screen_putchar(' ', pc_row, fix_col, attr);
1735 --curwin->w_wcol;
1736 pc_status = PC_STATUS_RIGHT;
1737 }
1738 }
1739# endif
1740 }
1741 else
1742#endif
1743 {
1744 pc_col += curwin->w_wcol;
1745#ifdef FEAT_MBYTE
1746 if (mb_lefthalve(pc_row, pc_col))
1747 pc_status = PC_STATUS_LEFT;
1748#endif
1749 }
1750
1751 /* save the character to be able to put it back */
1752#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1753 if (pc_status == PC_STATUS_UNSET)
1754#endif
1755 {
1756 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1757 pc_status = PC_STATUS_SET;
1758 }
1759 screen_putchar(c, pc_row, pc_col, attr);
1760 }
1761}
1762
1763/*
1764 * Undo the previous edit_putchar().
1765 */
1766 void
1767edit_unputchar()
1768{
1769 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1770 {
1771#if defined(FEAT_MBYTE)
1772 if (pc_status == PC_STATUS_RIGHT)
1773 ++curwin->w_wcol;
1774 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1775 redrawWinline(curwin->w_cursor.lnum, FALSE);
1776 else
1777#endif
1778 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1779 }
1780}
1781
1782/*
1783 * Called when p_dollar is set: display a '$' at the end of the changed text
1784 * Only works when cursor is in the line that changes.
1785 */
1786 void
1787display_dollar(col)
1788 colnr_T col;
1789{
1790 colnr_T save_col;
1791
1792 if (!redrawing())
1793 return;
1794
1795 cursor_off();
1796 save_col = curwin->w_cursor.col;
1797 curwin->w_cursor.col = col;
1798#ifdef FEAT_MBYTE
1799 if (has_mbyte)
1800 {
1801 char_u *p;
1802
1803 /* If on the last byte of a multi-byte move to the first byte. */
1804 p = ml_get_curline();
1805 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1806 }
1807#endif
1808 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1809 if (curwin->w_wcol < W_WIDTH(curwin))
1810 {
1811 edit_putchar('$', FALSE);
1812 dollar_vcol = curwin->w_virtcol;
1813 }
1814 curwin->w_cursor.col = save_col;
1815}
1816
1817/*
1818 * Call this function before moving the cursor from the normal insert position
1819 * in insert mode.
1820 */
1821 static void
1822undisplay_dollar()
1823{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001824 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001825 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001826 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001827 redrawWinline(curwin->w_cursor.lnum, FALSE);
1828 }
1829}
1830
1831/*
1832 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1833 * Keep the cursor on the same character.
1834 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1835 * type == INDENT_DEC decrease indent (for CTRL-D)
1836 * type == INDENT_SET set indent to "amount"
1837 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1838 */
1839 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001840change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841 int type;
1842 int amount;
1843 int round;
1844 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001845 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846{
1847 int vcol;
1848 int last_vcol;
1849 int insstart_less; /* reduction for Insstart.col */
1850 int new_cursor_col;
1851 int i;
1852 char_u *ptr;
1853 int save_p_list;
1854 int start_col;
1855 colnr_T vc;
1856#ifdef FEAT_VREPLACE
1857 colnr_T orig_col = 0; /* init for GCC */
1858 char_u *new_line, *orig_line = NULL; /* init for GCC */
1859
1860 /* VREPLACE mode needs to know what the line was like before changing */
1861 if (State & VREPLACE_FLAG)
1862 {
1863 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1864 orig_col = curwin->w_cursor.col;
1865 }
1866#endif
1867
1868 /* for the following tricks we don't want list mode */
1869 save_p_list = curwin->w_p_list;
1870 curwin->w_p_list = FALSE;
1871 vc = getvcol_nolist(&curwin->w_cursor);
1872 vcol = vc;
1873
1874 /*
1875 * For Replace mode we need to fix the replace stack later, which is only
1876 * possible when the cursor is in the indent. Remember the number of
1877 * characters before the cursor if it's possible.
1878 */
1879 start_col = curwin->w_cursor.col;
1880
1881 /* determine offset from first non-blank */
1882 new_cursor_col = curwin->w_cursor.col;
1883 beginline(BL_WHITE);
1884 new_cursor_col -= curwin->w_cursor.col;
1885
1886 insstart_less = curwin->w_cursor.col;
1887
1888 /*
1889 * If the cursor is in the indent, compute how many screen columns the
1890 * cursor is to the left of the first non-blank.
1891 */
1892 if (new_cursor_col < 0)
1893 vcol = get_indent() - vcol;
1894
1895 if (new_cursor_col > 0) /* can't fix replace stack */
1896 start_col = -1;
1897
1898 /*
1899 * Set the new indent. The cursor will be put on the first non-blank.
1900 */
1901 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001902 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 else
1904 {
1905#ifdef FEAT_VREPLACE
1906 int save_State = State;
1907
1908 /* Avoid being called recursively. */
1909 if (State & VREPLACE_FLAG)
1910 State = INSERT;
1911#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001912 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913#ifdef FEAT_VREPLACE
1914 State = save_State;
1915#endif
1916 }
1917 insstart_less -= curwin->w_cursor.col;
1918
1919 /*
1920 * Try to put cursor on same character.
1921 * If the cursor is at or after the first non-blank in the line,
1922 * compute the cursor column relative to the column of the first
1923 * non-blank character.
1924 * If we are not in insert mode, leave the cursor on the first non-blank.
1925 * If the cursor is before the first non-blank, position it relative
1926 * to the first non-blank, counted in screen columns.
1927 */
1928 if (new_cursor_col >= 0)
1929 {
1930 /*
1931 * When changing the indent while the cursor is touching it, reset
1932 * Insstart_col to 0.
1933 */
1934 if (new_cursor_col == 0)
1935 insstart_less = MAXCOL;
1936 new_cursor_col += curwin->w_cursor.col;
1937 }
1938 else if (!(State & INSERT))
1939 new_cursor_col = curwin->w_cursor.col;
1940 else
1941 {
1942 /*
1943 * Compute the screen column where the cursor should be.
1944 */
1945 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001946 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947
1948 /*
1949 * Advance the cursor until we reach the right screen column.
1950 */
1951 vcol = last_vcol = 0;
1952 new_cursor_col = -1;
1953 ptr = ml_get_curline();
1954 while (vcol <= (int)curwin->w_virtcol)
1955 {
1956 last_vcol = vcol;
1957#ifdef FEAT_MBYTE
1958 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001959 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 else
1961#endif
1962 ++new_cursor_col;
1963 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1964 }
1965 vcol = last_vcol;
1966
1967 /*
1968 * May need to insert spaces to be able to position the cursor on
1969 * the right screen column.
1970 */
1971 if (vcol != (int)curwin->w_virtcol)
1972 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001973 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001974 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001975 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 if (ptr != NULL)
1977 {
1978 new_cursor_col += i;
1979 ptr[i] = NUL;
1980 while (--i >= 0)
1981 ptr[i] = ' ';
1982 ins_str(ptr);
1983 vim_free(ptr);
1984 }
1985 }
1986
1987 /*
1988 * When changing the indent while the cursor is in it, reset
1989 * Insstart_col to 0.
1990 */
1991 insstart_less = MAXCOL;
1992 }
1993
1994 curwin->w_p_list = save_p_list;
1995
1996 if (new_cursor_col <= 0)
1997 curwin->w_cursor.col = 0;
1998 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001999 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002000 curwin->w_set_curswant = TRUE;
2001 changed_cline_bef_curs();
2002
2003 /*
2004 * May have to adjust the start of the insert.
2005 */
2006 if (State & INSERT)
2007 {
2008 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2009 {
2010 if ((int)Insstart.col <= insstart_less)
2011 Insstart.col = 0;
2012 else
2013 Insstart.col -= insstart_less;
2014 }
2015 if ((int)ai_col <= insstart_less)
2016 ai_col = 0;
2017 else
2018 ai_col -= insstart_less;
2019 }
2020
2021 /*
2022 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2023 * If the number of characters before the cursor decreased, need to pop a
2024 * few characters from the replace stack.
2025 * If the number of characters before the cursor increased, need to push a
2026 * few NULs onto the replace stack.
2027 */
2028 if (REPLACE_NORMAL(State) && start_col >= 0)
2029 {
2030 while (start_col > (int)curwin->w_cursor.col)
2031 {
2032 replace_join(0); /* remove a NUL from the replace stack */
2033 --start_col;
2034 }
2035 while (start_col < (int)curwin->w_cursor.col || replaced)
2036 {
2037 replace_push(NUL);
2038 if (replaced)
2039 {
2040 replace_push(replaced);
2041 replaced = NUL;
2042 }
2043 ++start_col;
2044 }
2045 }
2046
2047#ifdef FEAT_VREPLACE
2048 /*
2049 * For VREPLACE mode, we also have to fix the replace stack. In this case
2050 * it is always possible because we backspace over the whole line and then
2051 * put it back again the way we wanted it.
2052 */
2053 if (State & VREPLACE_FLAG)
2054 {
2055 /* If orig_line didn't allocate, just return. At least we did the job,
2056 * even if you can't backspace. */
2057 if (orig_line == NULL)
2058 return;
2059
2060 /* Save new line */
2061 new_line = vim_strsave(ml_get_curline());
2062 if (new_line == NULL)
2063 return;
2064
2065 /* We only put back the new line up to the cursor */
2066 new_line[curwin->w_cursor.col] = NUL;
2067
2068 /* Put back original line */
2069 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2070 curwin->w_cursor.col = orig_col;
2071
2072 /* Backspace from cursor to start of line */
2073 backspace_until_column(0);
2074
2075 /* Insert new stuff into line again */
2076 ins_bytes(new_line);
2077
2078 vim_free(new_line);
2079 }
2080#endif
2081}
2082
2083/*
2084 * Truncate the space at the end of a line. This is to be used only in an
2085 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2086 * modes.
2087 */
2088 void
2089truncate_spaces(line)
2090 char_u *line;
2091{
2092 int i;
2093
2094 /* find start of trailing white space */
2095 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2096 {
2097 if (State & REPLACE_FLAG)
2098 replace_join(0); /* remove a NUL from the replace stack */
2099 }
2100 line[i + 1] = NUL;
2101}
2102
2103#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2104 || defined(FEAT_COMMENTS) || defined(PROTO)
2105/*
2106 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2107 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002108 * Will attempt not to go before "col" even when there is a composing
2109 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002110 */
2111 void
2112backspace_until_column(col)
2113 int col;
2114{
2115 while ((int)curwin->w_cursor.col > col)
2116 {
2117 curwin->w_cursor.col--;
2118 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002119 replace_do_bs(col);
2120 else if (!del_char_after_col(col))
2121 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 }
2123}
2124#endif
2125
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002126/*
2127 * Like del_char(), but make sure not to go before column "limit_col".
2128 * Only matters when there are composing characters.
2129 * Return TRUE when something was deleted.
2130 */
2131 static int
2132del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002133 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002134{
2135#ifdef FEAT_MBYTE
2136 if (enc_utf8 && limit_col >= 0)
2137 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002138 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002139
2140 /* Make sure the cursor is at the start of a character, but
2141 * skip forward again when going too far back because of a
2142 * composing character. */
2143 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002144 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002145 {
2146 int l = utf_ptr2len(ml_get_cursor());
2147
2148 if (l == 0) /* end of line */
2149 break;
2150 curwin->w_cursor.col += l;
2151 }
2152 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2153 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002154 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002155 }
2156 else
2157#endif
2158 (void)del_char(FALSE);
2159 return TRUE;
2160}
2161
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2163/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002164 * CTRL-X pressed in Insert mode.
2165 */
2166 static void
2167ins_ctrl_x()
2168{
2169 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2170 * CTRL-V works like CTRL-N */
2171 if (ctrl_x_mode != CTRL_X_CMDLINE)
2172 {
2173 /* if the next ^X<> won't ADD nothing, then reset
2174 * compl_cont_status */
2175 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002176 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002177 else
2178 compl_cont_status = 0;
2179 /* We're not sure which CTRL-X mode it will be yet */
2180 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2181 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2182 edit_submode_pre = NULL;
2183 showmode();
2184 }
2185}
2186
2187/*
2188 * Return TRUE if the 'dict' or 'tsr' option can be used.
2189 */
2190 static int
2191has_compl_option(dict_opt)
2192 int dict_opt;
2193{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002194 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002195# ifdef FEAT_SPELL
2196 && !curwin->w_p_spell
2197# endif
2198 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002199 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2200 {
2201 ctrl_x_mode = 0;
2202 edit_submode = NULL;
2203 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2204 : (char_u *)_("'thesaurus' option is empty"),
2205 hl_attr(HLF_E));
2206 if (emsg_silent == 0)
2207 {
2208 vim_beep();
2209 setcursor();
2210 out_flush();
2211 ui_delay(2000L, FALSE);
2212 }
2213 return FALSE;
2214 }
2215 return TRUE;
2216}
2217
2218/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2220 * This depends on the current mode.
2221 */
2222 int
2223vim_is_ctrl_x_key(c)
2224 int c;
2225{
2226 /* Always allow ^R - let it's results then be checked */
2227 if (c == Ctrl_R)
2228 return TRUE;
2229
Bram Moolenaare3226be2005-12-18 22:10:00 +00002230 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002231 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002232 return TRUE;
2233
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 switch (ctrl_x_mode)
2235 {
2236 case 0: /* Not in any CTRL-X mode */
2237 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2238 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002239 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2241 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2242 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002243 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002244 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002245 case CTRL_X_SCROLL:
2246 return (c == Ctrl_Y || c == Ctrl_E);
2247 case CTRL_X_WHOLE_LINE:
2248 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2249 case CTRL_X_FILES:
2250 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2251 case CTRL_X_DICTIONARY:
2252 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2253 case CTRL_X_THESAURUS:
2254 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2255 case CTRL_X_TAGS:
2256 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2257#ifdef FEAT_FIND_ID
2258 case CTRL_X_PATH_PATTERNS:
2259 return (c == Ctrl_P || c == Ctrl_N);
2260 case CTRL_X_PATH_DEFINES:
2261 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2262#endif
2263 case CTRL_X_CMDLINE:
2264 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2265 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002266#ifdef FEAT_COMPL_FUNC
2267 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002268 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002269 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002270 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002271#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002272 case CTRL_X_SPELL:
2273 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 }
2275 EMSG(_(e_internal));
2276 return FALSE;
2277}
2278
2279/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002280 * Return TRUE when character "c" is part of the item currently being
2281 * completed. Used to decide whether to abandon complete mode when the menu
2282 * is visible.
2283 */
2284 static int
2285ins_compl_accept_char(c)
2286 int c;
2287{
2288 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2289 /* When expanding an identifier only accept identifier chars. */
2290 return vim_isIDc(c);
2291
2292 switch (ctrl_x_mode)
2293 {
2294 case CTRL_X_FILES:
2295 /* When expanding file name only accept file name chars. But not
2296 * path separators, so that "proto/<Tab>" expands files in
2297 * "proto", not "proto/" as a whole */
2298 return vim_isfilec(c) && !vim_ispathsep(c);
2299
2300 case CTRL_X_CMDLINE:
2301 case CTRL_X_OMNI:
2302 /* Command line and Omni completion can work with just about any
2303 * printable character, but do stop at white space. */
2304 return vim_isprintc(c) && !vim_iswhite(c);
2305
2306 case CTRL_X_WHOLE_LINE:
2307 /* For while line completion a space can be part of the line. */
2308 return vim_isprintc(c);
2309 }
2310 return vim_iswordc(c);
2311}
2312
2313/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002314 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002316 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 * the rest of the word to be in -- webb
2318 */
2319 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002320ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 char_u *str;
2322 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002323 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 char_u *fname;
2325 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002326 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002328 char_u *p;
2329 int i, c;
2330 int actual_len; /* Take multi-byte characters */
2331 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002332 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002333 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 int has_lower = FALSE;
2335 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002337 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002339 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340
Bram Moolenaara2993e12007-08-12 14:38:46 +00002341 /* Find actual length of completion. */
2342#ifdef FEAT_MBYTE
2343 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002345 p = str;
2346 actual_len = 0;
2347 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002348 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002349 mb_ptr_adv(p);
2350 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 }
2352 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002353 else
2354#endif
2355 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356
Bram Moolenaara2993e12007-08-12 14:38:46 +00002357 /* Find actual length of original text. */
2358#ifdef FEAT_MBYTE
2359 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002361 p = compl_orig_text;
2362 actual_compl_length = 0;
2363 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002365 mb_ptr_adv(p);
2366 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 }
2368 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002369 else
2370#endif
2371 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002372
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002373 /* "actual_len" may be smaller than "actual_compl_length" when using
2374 * thesaurus, only use the minimum when comparing. */
2375 min_len = actual_len < actual_compl_length
2376 ? actual_len : actual_compl_length;
2377
Bram Moolenaara2993e12007-08-12 14:38:46 +00002378 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002379 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002380 if (wca != NULL)
2381 {
2382 p = str;
2383 for (i = 0; i < actual_len; ++i)
2384#ifdef FEAT_MBYTE
2385 if (has_mbyte)
2386 wca[i] = mb_ptr2char_adv(&p);
2387 else
2388#endif
2389 wca[i] = *(p++);
2390
2391 /* Rule 1: Were any chars converted to lower? */
2392 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002393 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002394 {
2395#ifdef FEAT_MBYTE
2396 if (has_mbyte)
2397 c = mb_ptr2char_adv(&p);
2398 else
2399#endif
2400 c = *(p++);
2401 if (MB_ISLOWER(c))
2402 {
2403 has_lower = TRUE;
2404 if (MB_ISUPPER(wca[i]))
2405 {
2406 /* Rule 1 is satisfied. */
2407 for (i = actual_compl_length; i < actual_len; ++i)
2408 wca[i] = MB_TOLOWER(wca[i]);
2409 break;
2410 }
2411 }
2412 }
2413
2414 /*
2415 * Rule 2: No lower case, 2nd consecutive letter converted to
2416 * upper case.
2417 */
2418 if (!has_lower)
2419 {
2420 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002421 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002422 {
2423#ifdef FEAT_MBYTE
2424 if (has_mbyte)
2425 c = mb_ptr2char_adv(&p);
2426 else
2427#endif
2428 c = *(p++);
2429 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2430 {
2431 /* Rule 2 is satisfied. */
2432 for (i = actual_compl_length; i < actual_len; ++i)
2433 wca[i] = MB_TOUPPER(wca[i]);
2434 break;
2435 }
2436 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2437 }
2438 }
2439
2440 /* Copy the original case of the part we typed. */
2441 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002442 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002443 {
2444#ifdef FEAT_MBYTE
2445 if (has_mbyte)
2446 c = mb_ptr2char_adv(&p);
2447 else
2448#endif
2449 c = *(p++);
2450 if (MB_ISLOWER(c))
2451 wca[i] = MB_TOLOWER(wca[i]);
2452 else if (MB_ISUPPER(c))
2453 wca[i] = MB_TOUPPER(wca[i]);
2454 }
2455
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002456 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002457 * Generate encoding specific output from wide character array.
2458 * Multi-byte characters can occupy up to five bytes more than
2459 * ASCII characters, and we also need one byte for NUL, so stay
2460 * six bytes away from the edge of IObuff.
2461 */
2462 p = IObuff;
2463 i = 0;
2464 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2465#ifdef FEAT_MBYTE
2466 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002467 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002468 else
2469#endif
2470 *(p++) = wca[i++];
2471 *p = NUL;
2472
2473 vim_free(wca);
2474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002476 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2477 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002479 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480}
2481
2482/*
2483 * Add a match to the list of matches.
2484 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002485 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002486 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002487 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002488 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002489ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 char_u *str;
2491 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002492 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002493 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002494 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002495 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002496 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002497 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002499 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002500 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501
2502 ui_breakcheck();
2503 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002504 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 if (len < 0)
2506 len = (int)STRLEN(str);
2507
2508 /*
2509 * If the same match is already present, don't add it.
2510 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002511 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002513 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 do
2515 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002516 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002517 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002518 && match->cp_str[len] == NUL)
2519 return NOTDONE;
2520 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002521 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 }
2523
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002524 /* Remove any popup menu before changing the list of matches. */
2525 ins_compl_del_pum();
2526
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 /*
2528 * Allocate a new match structure.
2529 * Copy the values to the new match structure.
2530 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002531 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002533 return FAIL;
2534 match->cp_number = -1;
2535 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002536 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002537 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 {
2539 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002540 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002542 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002543
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002545 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2547 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002548 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002549 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002550 && compl_curr_match->cp_fname != NULL
2551 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002552 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002553 else if (fname != NULL)
2554 {
2555 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002556 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002559 match->cp_fname = NULL;
2560 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002561
2562 if (cptext != NULL)
2563 {
2564 int i;
2565
2566 for (i = 0; i < CPT_COUNT; ++i)
2567 if (cptext[i] != NULL && *cptext[i] != NUL)
2568 match->cp_text[i] = vim_strsave(cptext[i]);
2569 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570
2571 /*
2572 * Link the new match structure in the list of matches.
2573 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002574 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002575 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 else if (dir == FORWARD)
2577 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002578 match->cp_next = compl_curr_match->cp_next;
2579 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 }
2581 else /* BACKWARD */
2582 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002583 match->cp_next = compl_curr_match;
2584 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002586 if (match->cp_next)
2587 match->cp_next->cp_prev = match;
2588 if (match->cp_prev)
2589 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002591 compl_first_match = match;
2592 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002594 /*
2595 * Find the longest common string if still doing that.
2596 */
2597 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2598 ins_compl_longest_match(match);
2599
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 return OK;
2601}
2602
2603/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002604 * Return TRUE if "str[len]" matches with match->cp_str, considering
2605 * match->cp_icase.
2606 */
2607 static int
2608ins_compl_equal(match, str, len)
2609 compl_T *match;
2610 char_u *str;
2611 int len;
2612{
2613 if (match->cp_icase)
2614 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2615 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2616}
2617
2618/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002619 * Reduce the longest common string for match "match".
2620 */
2621 static void
2622ins_compl_longest_match(match)
2623 compl_T *match;
2624{
2625 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002626 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002627 int had_match;
2628
2629 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002630 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002631 /* First match, use it as a whole. */
2632 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002633 if (compl_leader != NULL)
2634 {
2635 had_match = (curwin->w_cursor.col > compl_col);
2636 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002637 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002638 ins_redraw(FALSE);
2639
2640 /* When the match isn't there (to avoid matching itself) remove it
2641 * again after redrawing. */
2642 if (!had_match)
2643 ins_compl_delete();
2644 compl_used_match = FALSE;
2645 }
2646 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002647 else
2648 {
2649 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002650 p = compl_leader;
2651 s = match->cp_str;
2652 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002653 {
2654#ifdef FEAT_MBYTE
2655 if (has_mbyte)
2656 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002657 c1 = mb_ptr2char(p);
2658 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002659 }
2660 else
2661#endif
2662 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002663 c1 = *p;
2664 c2 = *s;
2665 }
2666 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2667 : (c1 != c2))
2668 break;
2669#ifdef FEAT_MBYTE
2670 if (has_mbyte)
2671 {
2672 mb_ptr_adv(p);
2673 mb_ptr_adv(s);
2674 }
2675 else
2676#endif
2677 {
2678 ++p;
2679 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002680 }
2681 }
2682
2683 if (*p != NUL)
2684 {
2685 /* Leader was shortened, need to change the inserted text. */
2686 *p = NUL;
2687 had_match = (curwin->w_cursor.col > compl_col);
2688 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002689 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002690 ins_redraw(FALSE);
2691
2692 /* When the match isn't there (to avoid matching itself) remove it
2693 * again after redrawing. */
2694 if (!had_match)
2695 ins_compl_delete();
2696 }
2697
2698 compl_used_match = FALSE;
2699 }
2700}
2701
2702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002703 * Add an array of matches to the list of matches.
2704 * Frees matches[].
2705 */
2706 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002707ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708 int num_matches;
2709 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002710 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711{
2712 int i;
2713 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002714 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715
Bram Moolenaar572cb562005-08-05 21:35:02 +00002716 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002717 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002718 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002719 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002720 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002721 FreeWild(num_matches, matches);
2722}
2723
2724/* Make the completion list cyclic.
2725 * Return the number of matches (excluding the original).
2726 */
2727 static int
2728ins_compl_make_cyclic()
2729{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002730 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002731 int count = 0;
2732
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002733 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
2735 /*
2736 * Find the end of the list.
2737 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002738 match = compl_first_match;
2739 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002740 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002741 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002742 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 ++count;
2744 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002745 match->cp_next = compl_first_match;
2746 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002747 }
2748 return count;
2749}
2750
Bram Moolenaara94bc432006-03-10 21:42:59 +00002751/*
2752 * Start completion for the complete() function.
2753 * "startcol" is where the matched text starts (1 is first column).
2754 * "list" is the list of matches.
2755 */
2756 void
2757set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002758 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002759 list_T *list;
2760{
2761 /* If already doing completions stop it. */
2762 if (ctrl_x_mode != 0)
2763 ins_compl_prep(' ');
2764 ins_compl_clear();
2765
2766 if (stop_arrow() == FAIL)
2767 return;
2768
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002769 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002770 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002771 startcol = curwin->w_cursor.col;
2772 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002773 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002774 /* compl_pattern doesn't need to be set */
2775 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2776 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002777 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002778 return;
2779
2780 /* Handle like dictionary completion. */
2781 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2782
2783 ins_compl_add_list(list);
2784 compl_matches = ins_compl_make_cyclic();
2785 compl_started = TRUE;
2786 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002787 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002788
2789 compl_curr_match = compl_first_match;
2790 ins_complete(Ctrl_N);
2791 out_flush();
2792}
2793
2794
Bram Moolenaar9372a112005-12-06 19:59:18 +00002795/* "compl_match_array" points the currently displayed list of entries in the
2796 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002797static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002798static int compl_match_arraysize;
2799
2800/*
2801 * Update the screen and when there is any scrolling remove the popup menu.
2802 */
2803 static void
2804ins_compl_upd_pum()
2805{
2806 int h;
2807
2808 if (compl_match_array != NULL)
2809 {
2810 h = curwin->w_cline_height;
2811 update_screen(0);
2812 if (h != curwin->w_cline_height)
2813 ins_compl_del_pum();
2814 }
2815}
2816
2817/*
2818 * Remove any popup menu.
2819 */
2820 static void
2821ins_compl_del_pum()
2822{
2823 if (compl_match_array != NULL)
2824 {
2825 pum_undisplay();
2826 vim_free(compl_match_array);
2827 compl_match_array = NULL;
2828 }
2829}
2830
2831/*
2832 * Return TRUE if the popup menu should be displayed.
2833 */
2834 static int
2835pum_wanted()
2836{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002837 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002838 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002839 return FALSE;
2840
2841 /* The display looks bad on a B&W display. */
2842 if (t_colors < 8
2843#ifdef FEAT_GUI
2844 && !gui.in_use
2845#endif
2846 )
2847 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002848 return TRUE;
2849}
2850
2851/*
2852 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002853 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002854 */
2855 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002856pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002857{
2858 compl_T *compl;
2859 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002860
2861 /* Don't display the popup menu if there are no matches or there is only
2862 * one (ignoring the original text). */
2863 compl = compl_first_match;
2864 i = 0;
2865 do
2866 {
2867 if (compl == NULL
2868 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2869 break;
2870 compl = compl->cp_next;
2871 } while (compl != compl_first_match);
2872
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002873 if (strstr((char *)p_cot, "menuone") != NULL)
2874 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002875 return (i >= 2);
2876}
2877
2878/*
2879 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002880 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002881 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002882 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002883ins_compl_show_pum()
2884{
2885 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002886 compl_T *shown_compl = NULL;
2887 int did_find_shown_match = FALSE;
2888 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002889 int i;
2890 int cur = -1;
2891 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002892 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002893
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002894 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002895 return;
2896
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002897#if defined(FEAT_EVAL)
2898 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2899 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2900#endif
2901
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002902 /* Update the screen before drawing the popup menu over it. */
2903 update_screen(0);
2904
2905 if (compl_match_array == NULL)
2906 {
2907 /* Need to build the popup menu list. */
2908 compl_match_arraysize = 0;
2909 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002910 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002911 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002912 do
2913 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002914 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2915 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002916 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002917 ++compl_match_arraysize;
2918 compl = compl->cp_next;
2919 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002920 if (compl_match_arraysize == 0)
2921 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002922 compl_match_array = (pumitem_T *)alloc_clear(
2923 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002924 * compl_match_arraysize));
2925 if (compl_match_array != NULL)
2926 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002927 /* If the current match is the original text don't find the first
2928 * match after it, don't highlight anything. */
2929 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2930 shown_match_ok = TRUE;
2931
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002932 i = 0;
2933 compl = compl_first_match;
2934 do
2935 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002936 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2937 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002938 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002939 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002940 if (!shown_match_ok)
2941 {
2942 if (compl == compl_shown_match || did_find_shown_match)
2943 {
2944 /* This item is the shown match or this is the
2945 * first displayed item after the shown match. */
2946 compl_shown_match = compl;
2947 did_find_shown_match = TRUE;
2948 shown_match_ok = TRUE;
2949 }
2950 else
2951 /* Remember this displayed match for when the
2952 * shown match is just below it. */
2953 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002954 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002955 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002956
2957 if (compl->cp_text[CPT_ABBR] != NULL)
2958 compl_match_array[i].pum_text =
2959 compl->cp_text[CPT_ABBR];
2960 else
2961 compl_match_array[i].pum_text = compl->cp_str;
2962 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2963 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2964 if (compl->cp_text[CPT_MENU] != NULL)
2965 compl_match_array[i++].pum_extra =
2966 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002967 else
2968 compl_match_array[i++].pum_extra = compl->cp_fname;
2969 }
2970
2971 if (compl == compl_shown_match)
2972 {
2973 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002974
2975 /* When the original text is the shown match don't set
2976 * compl_shown_match. */
2977 if (compl->cp_flags & ORIGINAL_TEXT)
2978 shown_match_ok = TRUE;
2979
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002980 if (!shown_match_ok && shown_compl != NULL)
2981 {
2982 /* The shown match isn't displayed, set it to the
2983 * previously displayed match. */
2984 compl_shown_match = shown_compl;
2985 shown_match_ok = TRUE;
2986 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002987 }
2988 compl = compl->cp_next;
2989 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002990
2991 if (!shown_match_ok) /* no displayed match at all */
2992 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002993 }
2994 }
2995 else
2996 {
2997 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002998 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002999 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
3000 || compl_match_array[i].pum_text
3001 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003002 {
3003 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003004 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003005 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003006 }
3007
3008 if (compl_match_array != NULL)
3009 {
3010 /* Compute the screen column of the start of the completed text.
3011 * Use the cursor to get all wrapping and other settings right. */
3012 col = curwin->w_cursor.col;
3013 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003014 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003015 curwin->w_cursor.col = col;
3016 }
3017}
3018
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019#define DICT_FIRST (1) /* use just first element in "dict" */
3020#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003023 * Add any identifiers that match the given pattern in the list of dictionary
3024 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 */
3026 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00003027ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
3028 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003030 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003031 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003033 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034 char_u *ptr;
3035 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003036 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 char_u **files;
3038 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003040 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041
Bram Moolenaar0b238792006-03-02 22:49:12 +00003042 if (*dict == NUL)
3043 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003044#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003045 /* When 'dictionary' is empty and spell checking is enabled use
3046 * "spell". */
3047 if (!thesaurus && curwin->w_p_spell)
3048 dict = (char_u *)"spell";
3049 else
3050#endif
3051 return;
3052 }
3053
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003055 if (buf == NULL)
3056 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003057 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003058
Bram Moolenaar071d4272004-06-13 20:20:40 +00003059 /* If 'infercase' is set, don't use 'smartcase' here */
3060 save_p_scs = p_scs;
3061 if (curbuf->b_p_inf)
3062 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003063
3064 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3065 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003066 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003067 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3068 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003069 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003070 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003071
3072 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003073 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003074 len = STRLEN(pat_esc) + 10;
3075 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003076 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003077 {
3078 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003079 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003080 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003081 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003082 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3083 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003084 vim_free(ptr);
3085 }
3086 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003087 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003088 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003089 if (regmatch.regprog == NULL)
3090 goto theend;
3091 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3094 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003095 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003096 {
3097 /* copy one dictionary file name into buf */
3098 if (flags == DICT_EXACT)
3099 {
3100 count = 1;
3101 files = &dict;
3102 }
3103 else
3104 {
3105 /* Expand wildcards in the dictionary name, but do not allow
3106 * backticks (for security, the 'dict' option may have been set in
3107 * a modeline). */
3108 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003109# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003110 if (!thesaurus && STRCMP(buf, "spell") == 0)
3111 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003112 else
3113# endif
3114 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 || expand_wildcards(1, &buf, &count, &files,
3116 EW_FILE|EW_SILENT) != OK)
3117 count = 0;
3118 }
3119
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003120# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003121 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003122 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003123 /* Complete from active spelling. Skip "\<" in the pattern, we
3124 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003125 if (pat[0] == '\\' && pat[1] == '<')
3126 ptr = pat + 2;
3127 else
3128 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003129 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003131 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003132# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003133 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003134 {
3135 ins_compl_files(count, files, thesaurus, flags,
3136 &regmatch, buf, &dir);
3137 if (flags != DICT_EXACT)
3138 FreeWild(count, files);
3139 }
3140 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 break;
3142 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003143
3144theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003145 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003146 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147 vim_free(buf);
3148}
3149
Bram Moolenaar0b238792006-03-02 22:49:12 +00003150 static void
3151ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3152 int count;
3153 char_u **files;
3154 int thesaurus;
3155 int flags;
3156 regmatch_T *regmatch;
3157 char_u *buf;
3158 int *dir;
3159{
3160 char_u *ptr;
3161 int i;
3162 FILE *fp;
3163 int add_r;
3164
3165 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3166 {
3167 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3168 if (flags != DICT_EXACT)
3169 {
3170 vim_snprintf((char *)IObuff, IOSIZE,
3171 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003172 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003173 }
3174
3175 if (fp != NULL)
3176 {
3177 /*
3178 * Read dictionary file line by line.
3179 * Check each line for a match.
3180 */
3181 while (!got_int && !compl_interrupted
3182 && !vim_fgets(buf, LSIZE, fp))
3183 {
3184 ptr = buf;
3185 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3186 {
3187 ptr = regmatch->startp[0];
3188 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3189 ptr = find_line_end(ptr);
3190 else
3191 ptr = find_word_end(ptr);
3192 add_r = ins_compl_add_infercase(regmatch->startp[0],
3193 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003194 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003195 if (thesaurus)
3196 {
3197 char_u *wstart;
3198
3199 /*
3200 * Add the other matches on the line
3201 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003202 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003203 while (!got_int)
3204 {
3205 /* Find start of the next word. Skip white
3206 * space and punctuation. */
3207 ptr = find_word_start(ptr);
3208 if (*ptr == NUL || *ptr == NL)
3209 break;
3210 wstart = ptr;
3211
Bram Moolenaara2993e12007-08-12 14:38:46 +00003212 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003213#ifdef FEAT_MBYTE
3214 if (has_mbyte)
3215 /* Japanese words may have characters in
3216 * different classes, only separate words
3217 * with single-byte non-word characters. */
3218 while (*ptr != NUL)
3219 {
3220 int l = (*mb_ptr2len)(ptr);
3221
3222 if (l < 2 && !vim_iswordc(*ptr))
3223 break;
3224 ptr += l;
3225 }
3226 else
3227#endif
3228 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003229
3230 /* Add the word. Skip the regexp match. */
3231 if (wstart != regmatch->startp[0])
3232 add_r = ins_compl_add_infercase(wstart,
3233 (int)(ptr - wstart),
3234 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003235 }
3236 }
3237 if (add_r == OK)
3238 /* if dir was BACKWARD then honor it just once */
3239 *dir = FORWARD;
3240 else if (add_r == FAIL)
3241 break;
3242 /* avoid expensive call to vim_regexec() when at end
3243 * of line */
3244 if (*ptr == '\n' || got_int)
3245 break;
3246 }
3247 line_breakcheck();
3248 ins_compl_check_keys(50);
3249 }
3250 fclose(fp);
3251 }
3252 }
3253}
3254
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255/*
3256 * Find the start of the next word.
3257 * Returns a pointer to the first char of the word. Also stops at a NUL.
3258 */
3259 char_u *
3260find_word_start(ptr)
3261 char_u *ptr;
3262{
3263#ifdef FEAT_MBYTE
3264 if (has_mbyte)
3265 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003266 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003267 else
3268#endif
3269 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3270 ++ptr;
3271 return ptr;
3272}
3273
3274/*
3275 * Find the end of the word. Assumes it starts inside a word.
3276 * Returns a pointer to just after the word.
3277 */
3278 char_u *
3279find_word_end(ptr)
3280 char_u *ptr;
3281{
3282#ifdef FEAT_MBYTE
3283 int start_class;
3284
3285 if (has_mbyte)
3286 {
3287 start_class = mb_get_class(ptr);
3288 if (start_class > 1)
3289 while (*ptr != NUL)
3290 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003291 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 if (mb_get_class(ptr) != start_class)
3293 break;
3294 }
3295 }
3296 else
3297#endif
3298 while (vim_iswordc(*ptr))
3299 ++ptr;
3300 return ptr;
3301}
3302
3303/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003304 * Find the end of the line, omitting CR and NL at the end.
3305 * Returns a pointer to just after the line.
3306 */
3307 static char_u *
3308find_line_end(ptr)
3309 char_u *ptr;
3310{
3311 char_u *s;
3312
3313 s = ptr + STRLEN(ptr);
3314 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3315 --s;
3316 return s;
3317}
3318
3319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320 * Free the list of completions
3321 */
3322 static void
3323ins_compl_free()
3324{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003325 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003326 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003328 vim_free(compl_pattern);
3329 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003330 vim_free(compl_leader);
3331 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003333 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003335
3336 ins_compl_del_pum();
3337 pum_clear();
3338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003339 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340 do
3341 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003342 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003343 compl_curr_match = compl_curr_match->cp_next;
3344 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003346 if (match->cp_flags & FREE_FNAME)
3347 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003348 for (i = 0; i < CPT_COUNT; ++i)
3349 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003351 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3352 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003353 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003354}
3355
3356 static void
3357ins_compl_clear()
3358{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003359 compl_cont_status = 0;
3360 compl_started = FALSE;
3361 compl_matches = 0;
3362 vim_free(compl_pattern);
3363 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003364 vim_free(compl_leader);
3365 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003367 vim_free(compl_orig_text);
3368 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003369 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370}
3371
3372/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003373 * Return TRUE when Insert completion is active.
3374 */
3375 int
3376ins_compl_active()
3377{
3378 return compl_started;
3379}
3380
3381/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003382 * Delete one character before the cursor and show the subset of the matches
3383 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003384 * Returns the character to be used, NUL if the work is done and another char
3385 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003386 */
3387 static int
3388ins_compl_bs()
3389{
3390 char_u *line;
3391 char_u *p;
3392
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003393 line = ml_get_curline();
3394 p = line + curwin->w_cursor.col;
3395 mb_ptr_back(line, p);
3396
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003397 /* Stop completion when the whole word was deleted. For Omni completion
3398 * allow the word to be deleted, we won't match everything. */
3399 if ((int)(p - line) - (int)compl_col < 0
3400 || ((int)(p - line) - (int)compl_col == 0
3401 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003402 return K_BS;
3403
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003404 /* Deleted more than what was used to find matches or didn't finish
3405 * finding all matches: need to look for matches all over again. */
3406 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003407 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003408 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003409
Bram Moolenaara6557602006-02-04 22:43:20 +00003410 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003411 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003412 if (compl_leader != NULL)
3413 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003414 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003415 if (compl_shown_match != NULL)
3416 /* Make sure current match is not a hidden item. */
3417 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003418 return NUL;
3419 }
3420 return K_BS;
3421}
Bram Moolenaara6557602006-02-04 22:43:20 +00003422
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003423/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003424 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3425 * be called.
3426 */
3427 static int
3428ins_compl_need_restart()
3429{
3430 /* Return TRUE if we didn't complete finding matches or when the
3431 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3432 return compl_was_interrupted
3433 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3434 && compl_opt_refresh_always);
3435}
3436
3437/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003438 * Called after changing "compl_leader".
3439 * Show the popup menu with a different set of matches.
3440 * May also search for matches again if the previous search was interrupted.
3441 */
3442 static void
3443ins_compl_new_leader()
3444{
3445 ins_compl_del_pum();
3446 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003447 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003448 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003449
3450 if (compl_started)
3451 ins_compl_set_original_text(compl_leader);
3452 else
3453 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003454#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003455 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003456#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003457 /*
3458 * Matches were cleared, need to search for them now. First display
3459 * the changed text before the cursor. Set "compl_restarting" to
3460 * avoid that the first match is inserted.
3461 */
3462 update_screen(0);
3463#ifdef FEAT_GUI
3464 if (gui.in_use)
3465 {
3466 /* Show the cursor after the match, not after the redrawn text. */
3467 setcursor();
3468 out_flush();
3469 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003470 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003471#endif
3472 compl_restarting = TRUE;
3473 if (ins_complete(Ctrl_N) == FAIL)
3474 compl_cont_status = 0;
3475 compl_restarting = FALSE;
3476 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003477
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003478 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003479
3480 /* Show the popup menu with a different set of matches. */
3481 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003482
3483 /* Don't let Enter select the original text when there is no popup menu. */
3484 if (compl_match_array == NULL)
3485 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003486}
3487
3488/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003489 * Return the length of the completion, from the completion start column to
3490 * the cursor column. Making sure it never goes below zero.
3491 */
3492 static int
3493ins_compl_len()
3494{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003495 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003496
3497 if (off < 0)
3498 return 0;
3499 return off;
3500}
3501
3502/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003503 * Append one character to the match leader. May reduce the number of
3504 * matches.
3505 */
3506 static void
3507ins_compl_addleader(c)
3508 int c;
3509{
3510#ifdef FEAT_MBYTE
3511 int cc;
3512
3513 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3514 {
3515 char_u buf[MB_MAXBYTES + 1];
3516
3517 (*mb_char2bytes)(c, buf);
3518 buf[cc] = NUL;
3519 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003520 if (compl_opt_refresh_always)
3521 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003522 }
3523 else
3524#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003525 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003526 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003527 if (compl_opt_refresh_always)
3528 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003529 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003530
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003531 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003532 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003533 ins_compl_restart();
3534
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003535 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003536 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003537 * break redo. */
3538 if (!compl_opt_refresh_always)
3539 {
3540 vim_free(compl_leader);
3541 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003542 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003543 if (compl_leader != NULL)
3544 ins_compl_new_leader();
3545 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003546}
3547
3548/*
3549 * Setup for finding completions again without leaving CTRL-X mode. Used when
3550 * BS or a key was typed while still searching for matches.
3551 */
3552 static void
3553ins_compl_restart()
3554{
3555 ins_compl_free();
3556 compl_started = FALSE;
3557 compl_matches = 0;
3558 compl_cont_status = 0;
3559 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003560}
3561
3562/*
3563 * Set the first match, the original text.
3564 */
3565 static void
3566ins_compl_set_original_text(str)
3567 char_u *str;
3568{
3569 char_u *p;
3570
3571 /* Replace the original text entry. */
3572 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3573 {
3574 p = vim_strsave(str);
3575 if (p != NULL)
3576 {
3577 vim_free(compl_first_match->cp_str);
3578 compl_first_match->cp_str = p;
3579 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003580 }
3581}
3582
3583/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003584 * Append one character to the match leader. May reduce the number of
3585 * matches.
3586 */
3587 static void
3588ins_compl_addfrommatch()
3589{
3590 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003591 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003592 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003593 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003594
3595 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003596 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003597 {
3598 /* When still at the original match use the first entry that matches
3599 * the leader. */
3600 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3601 {
3602 p = NULL;
3603 for (cp = compl_shown_match->cp_next; cp != NULL
3604 && cp != compl_first_match; cp = cp->cp_next)
3605 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003606 if (compl_leader == NULL
3607 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003608 (int)STRLEN(compl_leader)))
3609 {
3610 p = cp->cp_str;
3611 break;
3612 }
3613 }
3614 if (p == NULL || (int)STRLEN(p) <= len)
3615 return;
3616 }
3617 else
3618 return;
3619 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003620 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003621 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003622 ins_compl_addleader(c);
3623}
3624
3625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003627 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003628 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003630 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631ins_compl_prep(c)
3632 int c;
3633{
3634 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003635 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003636 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637
3638 /* Forget any previous 'special' messages if this is actually
3639 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3640 */
3641 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3642 edit_submode_extra = NULL;
3643
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003644 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003645 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3646 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003647 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003648
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003649 /* Set "compl_get_longest" when finding the first matches. */
3650 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3651 || (ctrl_x_mode == 0 && !compl_started))
3652 {
3653 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3654 compl_used_match = TRUE;
3655 }
3656
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3658 {
3659 /*
3660 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3661 * it will be yet. Now we decide.
3662 */
3663 switch (c)
3664 {
3665 case Ctrl_E:
3666 case Ctrl_Y:
3667 ctrl_x_mode = CTRL_X_SCROLL;
3668 if (!(State & REPLACE_FLAG))
3669 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3670 else
3671 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3672 edit_submode_pre = NULL;
3673 showmode();
3674 break;
3675 case Ctrl_L:
3676 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3677 break;
3678 case Ctrl_F:
3679 ctrl_x_mode = CTRL_X_FILES;
3680 break;
3681 case Ctrl_K:
3682 ctrl_x_mode = CTRL_X_DICTIONARY;
3683 break;
3684 case Ctrl_R:
3685 /* Simply allow ^R to happen without affecting ^X mode */
3686 break;
3687 case Ctrl_T:
3688 ctrl_x_mode = CTRL_X_THESAURUS;
3689 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003690#ifdef FEAT_COMPL_FUNC
3691 case Ctrl_U:
3692 ctrl_x_mode = CTRL_X_FUNCTION;
3693 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003694 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003695 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003696 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003697#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003698 case 's':
3699 case Ctrl_S:
3700 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003701#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003702 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003703 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003704 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003705#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 case Ctrl_RSB:
3708 ctrl_x_mode = CTRL_X_TAGS;
3709 break;
3710#ifdef FEAT_FIND_ID
3711 case Ctrl_I:
3712 case K_S_TAB:
3713 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3714 break;
3715 case Ctrl_D:
3716 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3717 break;
3718#endif
3719 case Ctrl_V:
3720 case Ctrl_Q:
3721 ctrl_x_mode = CTRL_X_CMDLINE;
3722 break;
3723 case Ctrl_P:
3724 case Ctrl_N:
3725 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3726 * just started ^X mode, or there were enough ^X's to cancel
3727 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3728 * do normal expansion when interrupting a different mode (say
3729 * ^X^F^X^P or ^P^X^X^P, see below)
3730 * nothing changes if interrupting mode 0, (eg, the flag
3731 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003732 if (!(compl_cont_status & CONT_INTRPT))
3733 compl_cont_status |= CONT_LOCAL;
3734 else if (compl_cont_mode != 0)
3735 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 /* FALLTHROUGH */
3737 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003738 /* If we have typed at least 2 ^X's... for modes != 0, we set
3739 * compl_cont_status = 0 (eg, as if we had just started ^X
3740 * mode).
3741 * For mode 0, we set "compl_cont_mode" to an impossible
3742 * value, in both cases ^X^X can be used to restart the same
3743 * mode (avoiding ADDING mode).
3744 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3745 * 'complete' and local ^P expansions respectively.
3746 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3747 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 if (c == Ctrl_X)
3749 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003750 if (compl_cont_mode != 0)
3751 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003753 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 }
3755 ctrl_x_mode = 0;
3756 edit_submode = NULL;
3757 showmode();
3758 break;
3759 }
3760 }
3761 else if (ctrl_x_mode != 0)
3762 {
3763 /* We're already in CTRL-X mode, do we stay in it? */
3764 if (!vim_is_ctrl_x_key(c))
3765 {
3766 if (ctrl_x_mode == CTRL_X_SCROLL)
3767 ctrl_x_mode = 0;
3768 else
3769 ctrl_x_mode = CTRL_X_FINISHED;
3770 edit_submode = NULL;
3771 }
3772 showmode();
3773 }
3774
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003775 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 {
3777 /* Show error message from attempted keyword completion (probably
3778 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003779 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003781 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3782 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 || ctrl_x_mode == CTRL_X_FINISHED)
3784 {
3785 /* Get here when we have finished typing a sequence of ^N and
3786 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003787 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003788 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 {
3790 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003791 * If any of the original typed text has been changed, eg when
3792 * ignorecase is set, we must add back-spaces to the redo
3793 * buffer. We add as few as necessary to delete just the part
3794 * of the original text that has changed.
3795 * When using the longest match, edited the match or used
3796 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003797 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003798 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3799 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003800 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003801 ptr = NULL;
3802 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804
3805#ifdef FEAT_CINDENT
3806 want_cindent = (can_cindent && cindent_on());
3807#endif
3808 /*
3809 * When completing whole lines: fix indent for 'cindent'.
3810 * Otherwise, break line if it's too long.
3811 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003812 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 {
3814#ifdef FEAT_CINDENT
3815 /* re-indent the current line */
3816 if (want_cindent)
3817 {
3818 do_c_expr_indent();
3819 want_cindent = FALSE; /* don't do it again */
3820 }
3821#endif
3822 }
3823 else
3824 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003825 int prev_col = curwin->w_cursor.col;
3826
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003828 if (prev_col > 0)
3829 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 if (stop_arrow() == OK)
3831 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003832 if (prev_col > 0
3833 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3834 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
3836
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003837 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003838 * the selection without inserting anything. When
3839 * compl_enter_selects is set the Enter key does the same. */
3840 if ((c == Ctrl_Y || (compl_enter_selects
3841 && (c == CAR || c == K_KENTER || c == NL)))
3842 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003843 retval = TRUE;
3844
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003845 /* CTRL-E means completion is Ended, go back to the typed text. */
3846 if (c == Ctrl_E)
3847 {
3848 ins_compl_delete();
3849 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003850 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003851 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003852 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003853 retval = TRUE;
3854 }
3855
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003856 auto_format(FALSE, TRUE);
3857
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003859 compl_started = FALSE;
3860 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 msg_clr_cmdline(); /* necessary for "noshowmode" */
3862 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003863 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003864 if (edit_submode != NULL)
3865 {
3866 edit_submode = NULL;
3867 showmode();
3868 }
3869
3870#ifdef FEAT_CINDENT
3871 /*
3872 * Indent now if a key was typed that is in 'cinkeys'.
3873 */
3874 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3875 do_c_expr_indent();
3876#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003877#ifdef FEAT_AUTOCMD
3878 /* Trigger the CompleteDone event to give scripts a chance to act
3879 * upon the completion. */
3880 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882 }
3883 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003884#ifdef FEAT_AUTOCMD
3885 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3886 /* Trigger the CompleteDone event to give scripts a chance to act
3887 * upon the (possibly failed) completion. */
3888 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3889#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890
3891 /* reset continue_* if we left expansion-mode, if we stay they'll be
3892 * (re)set properly in ins_complete() */
3893 if (!vim_is_ctrl_x_key(c))
3894 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003895 compl_cont_status = 0;
3896 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003898
3899 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900}
3901
3902/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003903 * Fix the redo buffer for the completion leader replacing some of the typed
3904 * text. This inserts backspaces and appends the changed text.
3905 * "ptr" is the known leader text or NUL.
3906 */
3907 static void
3908ins_compl_fixRedoBufForLeader(ptr_arg)
3909 char_u *ptr_arg;
3910{
3911 int len;
3912 char_u *p;
3913 char_u *ptr = ptr_arg;
3914
3915 if (ptr == NULL)
3916 {
3917 if (compl_leader != NULL)
3918 ptr = compl_leader;
3919 else
3920 return; /* nothing to do */
3921 }
3922 if (compl_orig_text != NULL)
3923 {
3924 p = compl_orig_text;
3925 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3926 ;
3927#ifdef FEAT_MBYTE
3928 if (len > 0)
3929 len -= (*mb_head_off)(p, p + len);
3930#endif
3931 for (p += len; *p != NUL; mb_ptr_adv(p))
3932 AppendCharToRedobuff(K_BS);
3933 }
3934 else
3935 len = 0;
3936 if (ptr != NULL)
3937 AppendToRedobuffLit(ptr + len, -1);
3938}
3939
3940/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003941 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3942 * (depending on flag) starting from buf and looking for a non-scanned
3943 * buffer (other than curbuf). curbuf is special, if it is called with
3944 * buf=curbuf then it has to be the first call for a given flag/expansion.
3945 *
3946 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3947 */
3948 static buf_T *
3949ins_compl_next_buf(buf, flag)
3950 buf_T *buf;
3951 int flag;
3952{
3953#ifdef FEAT_WINDOWS
3954 static win_T *wp;
3955#endif
3956
3957 if (flag == 'w') /* just windows */
3958 {
3959#ifdef FEAT_WINDOWS
3960 if (buf == curbuf) /* first call for this flag/expansion */
3961 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003962 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 && wp->w_buffer->b_scanned)
3964 ;
3965 buf = wp->w_buffer;
3966#else
3967 buf = curbuf;
3968#endif
3969 }
3970 else
3971 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3972 * (unlisted buffers)
3973 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003974 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975 && ((flag == 'U'
3976 ? buf->b_p_bl
3977 : (!buf->b_p_bl
3978 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003979 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 ;
3981 return buf;
3982}
3983
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003984#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003985static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003986
3987/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003988 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003989 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003990 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003991 static void
3992expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003993 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003994 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003995{
Bram Moolenaar82139082011-09-14 16:52:09 +02003996 list_T *matchlist = NULL;
3997 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003998 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003999 char_u *funcname;
4000 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004001 win_T *curwin_save;
4002 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004003 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004004
Bram Moolenaare344bea2005-09-01 20:46:49 +00004005 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4006 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004007 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004008
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004009 /* Call 'completefunc' to obtain the list of matches. */
4010 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004011 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004012
Bram Moolenaare344bea2005-09-01 20:46:49 +00004013 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004014 curwin_save = curwin;
4015 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004016
4017 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004018 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004019 {
4020 switch (rettv.v_type)
4021 {
4022 case VAR_LIST:
4023 matchlist = rettv.vval.v_list;
4024 break;
4025 case VAR_DICT:
4026 matchdict = rettv.vval.v_dict;
4027 break;
4028 default:
4029 /* TODO: Give error message? */
4030 clear_tv(&rettv);
4031 break;
4032 }
4033 }
4034
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004035 if (curwin_save != curwin || curbuf_save != curbuf)
4036 {
4037 EMSG(_(e_complwin));
4038 goto theend;
4039 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004040 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004041 check_cursor();
4042 if (!equalpos(curwin->w_cursor, pos))
4043 {
4044 EMSG(_(e_compldel));
4045 goto theend;
4046 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004047
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004048 if (matchlist != NULL)
4049 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004050 else if (matchdict != NULL)
4051 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004052
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004053theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004054 if (matchdict != NULL)
4055 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004056 if (matchlist != NULL)
4057 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004058}
4059#endif /* FEAT_COMPL_FUNC */
4060
Bram Moolenaar39f05632006-03-19 22:15:26 +00004061#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004062/*
4063 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004064 */
4065 static void
4066ins_compl_add_list(list)
4067 list_T *list;
4068{
4069 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004070 int dir = compl_direction;
4071
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004072 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004073 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004074 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004075 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4076 /* if dir was BACKWARD then honor it just once */
4077 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004078 else if (did_emsg)
4079 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004080 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004081}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004082
4083/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004084 * Add completions from a dict.
4085 */
4086 static void
4087ins_compl_add_dict(dict)
4088 dict_T *dict;
4089{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004090 dictitem_T *di_refresh;
4091 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004092
4093 /* Check for optional "refresh" item. */
4094 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004095 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4096 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004097 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004098 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004099
4100 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4101 compl_opt_refresh_always = TRUE;
4102 }
4103
4104 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004105 di_words = dict_find(dict, (char_u *)"words", 5);
4106 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4107 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004108}
4109
4110/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004111 * Add a match to the list of matches from a typeval_T.
4112 * If the given string is already in the list of completions, then return
4113 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4114 * maybe because alloc() returns NULL, then FAIL is returned.
4115 */
4116 int
4117ins_compl_add_tv(tv, dir)
4118 typval_T *tv;
4119 int dir;
4120{
4121 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004122 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004123 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004124 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004125 char_u *(cptext[CPT_COUNT]);
4126
4127 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4128 {
4129 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4130 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4131 (char_u *)"abbr", FALSE);
4132 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4133 (char_u *)"menu", FALSE);
4134 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4135 (char_u *)"kind", FALSE);
4136 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4137 (char_u *)"info", FALSE);
4138 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4139 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004140 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004141 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004142 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4143 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004144 }
4145 else
4146 {
4147 word = get_tv_string_chk(tv);
4148 vim_memset(cptext, 0, sizeof(cptext));
4149 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004150 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004151 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004152 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004153}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004154#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004155
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004156/*
4157 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004158 * The search starts at position "ini" in curbuf and in the direction
4159 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004160 * When "compl_started" is FALSE start at that position, otherwise continue
4161 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004162 * This may return before finding all the matches.
4163 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 */
4165 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004166ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168{
4169 static pos_T first_match_pos;
4170 static pos_T last_match_pos;
4171 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004172 static int found_all = FALSE; /* Found all matches of a
4173 certain type. */
4174 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175
Bram Moolenaar572cb562005-08-05 21:35:02 +00004176 pos_T *pos;
4177 char_u **matches;
4178 int save_p_scs;
4179 int save_p_ws;
4180 int save_p_ic;
4181 int i;
4182 int num_matches;
4183 int len;
4184 int found_new_match;
4185 int type = ctrl_x_mode;
4186 char_u *ptr;
4187 char_u *dict = NULL;
4188 int dict_f = 0;
4189 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004190 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004192 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 {
4194 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4195 ins_buf->b_scanned = 0;
4196 found_all = FALSE;
4197 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004198 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004199 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004200 last_match_pos = first_match_pos = *ini;
4201 }
4202
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004203 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004204 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4206 for (;;)
4207 {
4208 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004209 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004211 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 * or if found_all says this entry is done. For ^X^L only use the
4213 * entries from 'complete' that look in loaded buffers. */
4214 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004215 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
4217 found_all = FALSE;
4218 while (*e_cpt == ',' || *e_cpt == ' ')
4219 e_cpt++;
4220 if (*e_cpt == '.' && !curbuf->b_scanned)
4221 {
4222 ins_buf = curbuf;
4223 first_match_pos = *ini;
4224 /* So that ^N can match word immediately after cursor */
4225 if (ctrl_x_mode == 0)
4226 dec(&first_match_pos);
4227 last_match_pos = first_match_pos;
4228 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004229
4230 /* Remember the first match so that the loop stops when we
4231 * wrap and come back there a second time. */
4232 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 }
4234 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4235 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4236 {
4237 /* Scan a buffer, but not the current one. */
4238 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4239 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004240 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 first_match_pos.col = last_match_pos.col = 0;
4242 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4243 last_match_pos.lnum = 0;
4244 type = 0;
4245 }
4246 else /* unloaded buffer, scan like dictionary */
4247 {
4248 found_all = TRUE;
4249 if (ins_buf->b_fname == NULL)
4250 continue;
4251 type = CTRL_X_DICTIONARY;
4252 dict = ins_buf->b_fname;
4253 dict_f = DICT_EXACT;
4254 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004255 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 ins_buf->b_fname == NULL
4257 ? buf_spname(ins_buf)
4258 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004259 ? ins_buf->b_fname
4260 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004261 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 }
4263 else if (*e_cpt == NUL)
4264 break;
4265 else
4266 {
4267 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4268 type = -1;
4269 else if (*e_cpt == 'k' || *e_cpt == 's')
4270 {
4271 if (*e_cpt == 'k')
4272 type = CTRL_X_DICTIONARY;
4273 else
4274 type = CTRL_X_THESAURUS;
4275 if (*++e_cpt != ',' && *e_cpt != NUL)
4276 {
4277 dict = e_cpt;
4278 dict_f = DICT_FIRST;
4279 }
4280 }
4281#ifdef FEAT_FIND_ID
4282 else if (*e_cpt == 'i')
4283 type = CTRL_X_PATH_PATTERNS;
4284 else if (*e_cpt == 'd')
4285 type = CTRL_X_PATH_DEFINES;
4286#endif
4287 else if (*e_cpt == ']' || *e_cpt == 't')
4288 {
4289 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004290 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004291 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004292 }
4293 else
4294 type = -1;
4295
4296 /* in any case e_cpt is advanced to the next entry */
4297 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4298
4299 found_all = TRUE;
4300 if (type == -1)
4301 continue;
4302 }
4303 }
4304
4305 switch (type)
4306 {
4307 case -1:
4308 break;
4309#ifdef FEAT_FIND_ID
4310 case CTRL_X_PATH_PATTERNS:
4311 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004312 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004313 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004315 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4317 (linenr_T)1, (linenr_T)MAXLNUM);
4318 break;
4319#endif
4320
4321 case CTRL_X_DICTIONARY:
4322 case CTRL_X_THESAURUS:
4323 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004324 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 : (type == CTRL_X_THESAURUS
4326 ? (*curbuf->b_p_tsr == NUL
4327 ? p_tsr
4328 : curbuf->b_p_tsr)
4329 : (*curbuf->b_p_dict == NUL
4330 ? p_dict
4331 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004332 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004333 dict != NULL ? dict_f
4334 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 dict = NULL;
4336 break;
4337
4338 case CTRL_X_TAGS:
4339 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4340 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004341 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004343 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 * of matches is found when compl_pattern is empty */
4345 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4347 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4348 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4349 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004350 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 }
4352 p_ic = save_p_ic;
4353 break;
4354
4355 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004356 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4358 {
4359
4360 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004362 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 }
4364 break;
4365
4366 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004367 if (expand_cmdline(&compl_xp, compl_pattern,
4368 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004370 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 break;
4372
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004373#ifdef FEAT_COMPL_FUNC
4374 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004375 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004376 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004377 break;
4378#endif
4379
Bram Moolenaar488c6512005-08-11 20:09:58 +00004380 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004381#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004382 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004383 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004384 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004385 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004386#endif
4387 break;
4388
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 default: /* normal ^P/^N and ^X^L */
4390 /*
4391 * If 'infercase' is set, don't use 'smartcase' here
4392 */
4393 save_p_scs = p_scs;
4394 if (ins_buf->b_p_inf)
4395 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004396
Bram Moolenaar361aa502014-01-23 22:45:58 +01004397 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 * end but never from the middle, thus setting nowrapscan in this
4399 * buffers is a good idea, on the other hand, we always set
4400 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4401 save_p_ws = p_ws;
4402 if (ins_buf != curbuf)
4403 p_ws = FALSE;
4404 else if (*e_cpt == '.')
4405 p_ws = TRUE;
4406 for (;;)
4407 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004408 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004410 ++msg_silent; /* Don't want messages for wrapscan. */
4411
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004412 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4413 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004415 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004417 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004419 found_new_match = searchit(NULL, ins_buf, pos,
4420 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004422 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004423 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004424 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004426 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004427 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 first_match_pos = *pos;
4429 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004430 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 }
4432 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004433 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 found_new_match = FAIL;
4435 if (found_new_match == FAIL)
4436 {
4437 if (ins_buf == curbuf)
4438 found_all = TRUE;
4439 break;
4440 }
4441
4442 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004443 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 && ini->lnum == pos->lnum
4445 && ini->col == pos->col)
4446 continue;
4447 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4448 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4449 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004450 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
4452 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4453 continue;
4454 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4455 if (!p_paste)
4456 ptr = skipwhite(ptr);
4457 }
4458 len = (int)STRLEN(ptr);
4459 }
4460 else
4461 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462 char_u *tmp_ptr = ptr;
4463
4464 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004466 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004467 /* Skip if already inside a word. */
4468 if (vim_iswordp(tmp_ptr))
4469 continue;
4470 /* Find start of next word. */
4471 tmp_ptr = find_word_start(tmp_ptr);
4472 }
4473 /* Find end of this word. */
4474 tmp_ptr = find_word_end(tmp_ptr);
4475 len = (int)(tmp_ptr - ptr);
4476
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004477 if ((compl_cont_status & CONT_ADDING)
4478 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
4480 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4481 {
4482 /* Try next line, if any. the new word will be
4483 * "join" as if the normal command "J" was used.
4484 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004485 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486 * works -- Acevedo */
4487 STRNCPY(IObuff, ptr, len);
4488 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4489 tmp_ptr = ptr = skipwhite(ptr);
4490 /* Find start of next word. */
4491 tmp_ptr = find_word_start(tmp_ptr);
4492 /* Find end of next word. */
4493 tmp_ptr = find_word_end(tmp_ptr);
4494 if (tmp_ptr > ptr)
4495 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004496 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004498 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004499 IObuff[len++] = ' ';
4500 /* IObuf =~ "\k.* ", thus len >= 2 */
4501 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004502 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 || (vim_strchr(p_cpo, CPO_JOINSP)
4504 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004505 && (IObuff[len - 2] == '?'
4506 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 IObuff[len++] = ' ';
4508 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004509 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004510 if (tmp_ptr - ptr >= IOSIZE - len)
4511 tmp_ptr = ptr + IOSIZE - len - 1;
4512 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4513 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004514 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 }
4516 IObuff[len] = NUL;
4517 ptr = IObuff;
4518 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004519 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 continue;
4521 }
4522 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004523 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004524 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004525 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 {
4527 found_new_match = OK;
4528 break;
4529 }
4530 }
4531 p_scs = save_p_scs;
4532 p_ws = save_p_ws;
4533 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004534
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004535 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004536 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004537 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004538 found_new_match = OK;
4539
4540 /* break the loop for specialized modes (use 'complete' just for the
4541 * generic ctrl_x_mode == 0) or when we've found a new match */
4542 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004543 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004544 {
4545 if (got_int)
4546 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004547 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004548 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004549 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004550
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004551 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4552 || compl_interrupted)
4553 break;
4554 compl_started = TRUE;
4555 }
4556 else
4557 {
4558 /* Mark a buffer scanned when it has been scanned completely */
4559 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4560 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004562 compl_started = FALSE;
4563 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004565 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
4567 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4568 && *e_cpt == NUL) /* Got to end of 'complete' */
4569 found_new_match = FAIL;
4570
4571 i = -1; /* total of matches, unknown */
4572 if (found_new_match == FAIL
4573 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4574 i = ins_compl_make_cyclic();
4575
4576 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004577 * just been made cyclic then we have to move compl_curr_match to the next
4578 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004579 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4580 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 if (compl_curr_match == NULL)
4582 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 return i;
4584}
4585
4586/* Delete the old text being completed. */
4587 static void
4588ins_compl_delete()
4589{
4590 int i;
4591
4592 /*
4593 * In insert mode: Delete the typed part.
4594 * In replace mode: Put the old characters back, if any.
4595 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004596 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 backspace_until_column(i);
4598 changed_cline_bef_curs();
4599}
4600
4601/* Insert the new text being completed. */
4602 static void
4603ins_compl_insert()
4604{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004605 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004606 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4607 compl_used_match = FALSE;
4608 else
4609 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004610}
4611
4612/*
4613 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004614 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4615 * get more completions. If it is FALSE, then we just do nothing when there
4616 * are no more completions in a given direction. The latter case is used when
4617 * we are still in the middle of finding completions, to allow browsing
4618 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619 * Return the total number of matches, or -1 if still unknown -- webb.
4620 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004621 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4622 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 *
4624 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004625 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4626 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 */
4628 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004629ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004631 int count; /* repeat completion this many times; should
4632 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004633 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634{
4635 int num_matches = -1;
4636 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004637 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004638 compl_T *found_compl = NULL;
4639 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004640 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004642 /* When user complete function return -1 for findstart which is next
4643 * time of 'always', compl_shown_match become NULL. */
4644 if (compl_shown_match == NULL)
4645 return -1;
4646
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004647 if (compl_leader != NULL
4648 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004649 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004650 /* Set "compl_shown_match" to the actually shown match, it may differ
4651 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004652 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004653 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004654 && compl_shown_match->cp_next != NULL
4655 && compl_shown_match->cp_next != compl_first_match)
4656 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004657
4658 /* If we didn't find it searching forward, and compl_shows_dir is
4659 * backward, find the last match. */
4660 if (compl_shows_dir == BACKWARD
4661 && !ins_compl_equal(compl_shown_match,
4662 compl_leader, (int)STRLEN(compl_leader))
4663 && (compl_shown_match->cp_next == NULL
4664 || compl_shown_match->cp_next == compl_first_match))
4665 {
4666 while (!ins_compl_equal(compl_shown_match,
4667 compl_leader, (int)STRLEN(compl_leader))
4668 && compl_shown_match->cp_prev != NULL
4669 && compl_shown_match->cp_prev != compl_first_match)
4670 compl_shown_match = compl_shown_match->cp_prev;
4671 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004672 }
4673
4674 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004675 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676 /* Delete old text to be replaced */
4677 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004678
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004679 /* When finding the longest common text we stick at the original text,
4680 * don't let CTRL-N or CTRL-P move to the first match. */
4681 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4682
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004683 /* When restarting the search don't insert the first match either. */
4684 if (compl_restarting)
4685 {
4686 advance = FALSE;
4687 compl_restarting = FALSE;
4688 }
4689
Bram Moolenaare3226be2005-12-18 22:10:00 +00004690 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4691 * around. */
4692 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004693 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004694 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004696 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004697 found_end = (compl_first_match != NULL
4698 && (compl_shown_match->cp_next == compl_first_match
4699 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004700 }
4701 else if (compl_shows_dir == BACKWARD
4702 && compl_shown_match->cp_prev != NULL)
4703 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004704 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004705 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004706 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004709 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004710 if (!allow_get_expansion)
4711 {
4712 if (advance)
4713 {
4714 if (compl_shows_dir == BACKWARD)
4715 compl_pending -= todo + 1;
4716 else
4717 compl_pending += todo + 1;
4718 }
4719 return -1;
4720 }
4721
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004722 if (advance)
4723 {
4724 if (compl_shows_dir == BACKWARD)
4725 --compl_pending;
4726 else
4727 ++compl_pending;
4728 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004729
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004730 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004731 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004732
4733 /* handle any pending completions */
4734 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004735 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004736 {
4737 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4738 {
4739 compl_shown_match = compl_shown_match->cp_next;
4740 --compl_pending;
4741 }
4742 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4743 {
4744 compl_shown_match = compl_shown_match->cp_prev;
4745 ++compl_pending;
4746 }
4747 else
4748 break;
4749 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004750 found_end = FALSE;
4751 }
4752 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4753 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004754 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004755 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004756 ++todo;
4757 else
4758 /* Remember a matching item. */
4759 found_compl = compl_shown_match;
4760
4761 /* Stop at the end of the list when we found a usable match. */
4762 if (found_end)
4763 {
4764 if (found_compl != NULL)
4765 {
4766 compl_shown_match = found_compl;
4767 break;
4768 }
4769 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004770 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 }
4772
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004773 /* Insert the text of the new completion, or the compl_leader. */
4774 if (insert_match)
4775 {
4776 if (!compl_get_longest || compl_used_match)
4777 ins_compl_insert();
4778 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004779 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004780 }
4781 else
4782 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783
4784 if (!allow_get_expansion)
4785 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004786 /* may undisplay the popup menu first */
4787 ins_compl_upd_pum();
4788
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004789 /* redraw to show the user what was inserted */
4790 update_screen(0);
4791
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004792 /* display the updated popup menu */
4793 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004794#ifdef FEAT_GUI
4795 if (gui.in_use)
4796 {
4797 /* Show the cursor after the match, not after the redrawn text. */
4798 setcursor();
4799 out_flush();
4800 gui_update_cursor(FALSE, FALSE);
4801 }
4802#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004803
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 /* Delete old text to be replaced, since we're still searching and
4805 * don't want to match ourselves! */
4806 ins_compl_delete();
4807 }
4808
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004809 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004810 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004811 compl_enter_selects = !insert_match && compl_match_array != NULL;
4812
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 /*
4814 * Show the file name for the match (if any)
4815 * Truncate the file name to avoid a wait for return.
4816 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004817 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
4819 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004820 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 if (i <= 0)
4822 i = 0;
4823 else
4824 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004825 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 msg(IObuff);
4827 redraw_cmdline = FALSE; /* don't overwrite! */
4828 }
4829
4830 return num_matches;
4831}
4832
4833/*
4834 * Call this while finding completions, to check whether the user has hit a key
4835 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004836 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004838 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 */
4840 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004841ins_compl_check_keys(frequency)
4842 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843{
4844 static int count = 0;
4845
4846 int c;
4847
4848 /* Don't check when reading keys from a script. That would break the test
4849 * scripts */
4850 if (using_script())
4851 return;
4852
4853 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004854 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 return;
4856 count = 0;
4857
Bram Moolenaara260a972006-06-23 19:36:29 +00004858 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4859 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 if (c != NUL)
4862 {
4863 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4864 {
4865 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004866 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004867 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4868 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004870 else
4871 {
4872 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004873 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004874 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004875 if (c != K_IGNORE)
4876 {
4877 /* Don't interrupt completion when the character wasn't typed,
4878 * e.g., when doing @q to replay keys. */
4879 if (c != Ctrl_R && KeyTyped)
4880 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004881
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004882 vungetc(c);
4883 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004884 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004886 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004887 {
4888 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4889
4890 compl_pending = 0;
4891 (void)ins_compl_next(FALSE, todo, TRUE);
4892 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004893}
4894
4895/*
4896 * Decide the direction of Insert mode complete from the key typed.
4897 * Returns BACKWARD or FORWARD.
4898 */
4899 static int
4900ins_compl_key2dir(c)
4901 int c;
4902{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004903 if (c == Ctrl_P || c == Ctrl_L
4904 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4905 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004906 return BACKWARD;
4907 return FORWARD;
4908}
4909
4910/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004911 * Return TRUE for keys that are used for completion only when the popup menu
4912 * is visible.
4913 */
4914 static int
4915ins_compl_pum_key(c)
4916 int c;
4917{
4918 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004919 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4920 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004921}
4922
4923/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004924 * Decide the number of completions to move forward.
4925 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4926 */
4927 static int
4928ins_compl_key2count(c)
4929 int c;
4930{
4931 int h;
4932
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004933 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004934 {
4935 h = pum_get_height();
4936 if (h > 3)
4937 h -= 2; /* keep some context */
4938 return h;
4939 }
4940 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941}
4942
4943/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004944 * Return TRUE if completion with "c" should insert the match, FALSE if only
4945 * to change the currently selected completion.
4946 */
4947 static int
4948ins_compl_use_match(c)
4949 int c;
4950{
4951 switch (c)
4952 {
4953 case K_UP:
4954 case K_DOWN:
4955 case K_PAGEDOWN:
4956 case K_KPAGEDOWN:
4957 case K_S_DOWN:
4958 case K_PAGEUP:
4959 case K_KPAGEUP:
4960 case K_S_UP:
4961 return FALSE;
4962 }
4963 return TRUE;
4964}
4965
4966/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 * Do Insert mode completion.
4968 * Called when character "c" was typed, which has a meaning for completion.
4969 * Returns OK if completion was done, FAIL if something failed (out of mem).
4970 */
4971 static int
4972ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004973 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004975 char_u *line;
4976 int startcol = 0; /* column where searched text starts */
4977 colnr_T curs_col; /* cursor column */
4978 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004979 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
Bram Moolenaare3226be2005-12-18 22:10:00 +00004981 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004982 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
4984 /* First time we hit ^N or ^P (in a row, I mean) */
4985
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 did_ai = FALSE;
4987#ifdef FEAT_SMARTINDENT
4988 did_si = FALSE;
4989 can_si = FALSE;
4990 can_si_back = FALSE;
4991#endif
4992 if (stop_arrow() == FAIL)
4993 return FAIL;
4994
4995 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004996 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004997 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004999 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005000 * "compl_startpos" to the cursor as a pattern to add a new word
5001 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00005002 * "compl_startpos" is not in the same line as the cursor then fix it
5003 * (the line has been split because it was longer than 'tw'). if SOL
5004 * is set then skip the previous pattern, a word at the beginning of
5005 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005006 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5007 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 {
5009 /*
5010 * it is a continued search
5011 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005012 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5014 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5015 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005016 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005018 /* line (probably) wrapped, set compl_startpos to the
5019 * first non_blank in the line, if it is not a wordchar
5020 * include it to get a better pattern, but then we don't
5021 * want the "\\<" prefix, check it bellow */
5022 compl_col = (colnr_T)(skipwhite(line) - line);
5023 compl_startpos.col = compl_col;
5024 compl_startpos.lnum = curwin->w_cursor.lnum;
5025 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027 else
5028 {
5029 /* S_IPOS was set when we inserted a word that was at the
5030 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005031 * mode but first we need to redefine compl_startpos */
5032 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005034 compl_cont_status |= CONT_SOL;
5035 compl_startpos.col = (colnr_T)(skipwhite(
5036 line + compl_length
5037 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005039 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005041 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005042 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005043 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005045 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005047 compl_cont_status &= ~CONT_SOL;
5048 compl_length = (IOSIZE - MIN_SPACE);
5049 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005051 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5052 if (compl_length < 1)
5053 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005056 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005058 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 }
5060 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005061 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005063 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005065 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005067 compl_cont_status = 0;
5068 compl_cont_status |= CONT_N_ADDS;
5069 compl_startpos = curwin->w_cursor;
5070 startcol = (int)curs_col;
5071 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 }
5073
5074 /* Work out completion pattern and original text -- webb */
5075 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5076 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005077 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5079 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005080 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005082 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 compl_col += ++startcol;
5085 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 }
5087 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005088 compl_pattern = str_foldcase(line + compl_col,
5089 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005091 compl_pattern = vim_strnsave(line + compl_col,
5092 compl_length);
5093 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 return FAIL;
5095 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005096 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 {
5098 char_u *prefix = (char_u *)"\\<";
5099
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005100 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005101 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005102 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 if (!vim_iswordp(line + compl_col)
5106 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 && (
5108#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005109 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112#endif
5113 )))
5114 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005115 STRCPY((char *)compl_pattern, prefix);
5116 (void)quote_meta(compl_pattern + STRLEN(prefix),
5117 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005119 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005122#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005123 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124#endif
5125 )
5126 {
5127 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005128 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5129 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005131 compl_col += curs_col;
5132 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 }
5134 else
5135 {
5136#ifdef FEAT_MBYTE
5137 /* Search the point of change class of multibyte character
5138 * or not a word single byte character backward. */
5139 if (has_mbyte)
5140 {
5141 int base_class;
5142 int head_off;
5143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005144 startcol -= (*mb_head_off)(line, line + startcol);
5145 base_class = mb_get_class(line + startcol);
5146 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005148 head_off = (*mb_head_off)(line, line + startcol);
5149 if (base_class != mb_get_class(line + startcol
5150 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 }
5155 else
5156#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005159 compl_col += ++startcol;
5160 compl_length = (int)curs_col - startcol;
5161 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {
5163 /* Only match word with at least two chars -- webb
5164 * there's no need to call quote_meta,
5165 * alloc(7) is enough -- Acevedo
5166 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 compl_pattern = alloc(7);
5168 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005170 STRCPY((char *)compl_pattern, "\\<");
5171 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5172 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
5174 else
5175 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005176 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005177 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005178 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 STRCPY((char *)compl_pattern, "\\<");
5181 (void)quote_meta(compl_pattern + 2, line + compl_col,
5182 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 }
5184 }
5185 }
5186 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5187 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005188 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 compl_length = (int)curs_col - (int)compl_col;
5190 if (compl_length < 0) /* cursor in indent: empty pattern */
5191 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005193 compl_pattern = str_foldcase(line + compl_col, compl_length,
5194 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005196 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5197 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 return FAIL;
5199 }
5200 else if (ctrl_x_mode == CTRL_X_FILES)
5201 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005202 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005203 if (startcol > 0)
5204 {
5205 char_u *p = line + startcol;
5206
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005207 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005208 while (p > line && vim_isfilec(PTR2CHAR(p)))
5209 mb_ptr_back(line, p);
5210 if (p == line && vim_isfilec(PTR2CHAR(p)))
5211 startcol = 0;
5212 else
5213 startcol = (int)(p - line) + 1;
5214 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005215
Bram Moolenaar0300e462013-09-08 16:03:45 +02005216 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005217 compl_length = (int)curs_col - startcol;
5218 compl_pattern = addstar(line + compl_col, compl_length,
5219 EXPAND_FILES);
5220 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005221 return FAIL;
5222 }
5223 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5224 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 compl_pattern = vim_strnsave(line, curs_col);
5226 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005227 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 set_cmd_context(&compl_xp, compl_pattern,
5229 (int)STRLEN(compl_pattern), curs_col);
5230 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5231 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005232 /* No completion possible, use an empty pattern to get a
5233 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005234 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005235 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005236 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5237 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005239 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005240 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005241#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005242 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005243 * Call user defined function 'completefunc' with "a:findstart"
5244 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005245 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005246 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005247 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005248 char_u *funcname;
5249 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005250 win_T *curwin_save;
5251 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005252
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005253 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005254 * string */
5255 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5256 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5257 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005258 {
5259 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5260 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005261 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005262 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005263
5264 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005265 args[1] = NULL;
5266 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005267 curwin_save = curwin;
5268 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005269 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005270 if (curwin_save != curwin || curbuf_save != curbuf)
5271 {
5272 EMSG(_(e_complwin));
5273 return FAIL;
5274 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005275 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005276 check_cursor();
5277 if (!equalpos(curwin->w_cursor, pos))
5278 {
5279 EMSG(_(e_compldel));
5280 return FAIL;
5281 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005282
Bram Moolenaar6110a002012-01-26 18:58:38 +01005283 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005284 * cancel the complete without an error.
5285 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005286 if (col == -2)
5287 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005288 if (col == -3)
5289 {
5290 ctrl_x_mode = 0;
5291 edit_submode = NULL;
5292 msg_clr_cmdline();
5293 return FAIL;
5294 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005295
Bram Moolenaar82139082011-09-14 16:52:09 +02005296 /*
5297 * Reset extended parameters of completion, when start new
5298 * completion.
5299 */
5300 compl_opt_refresh_always = FALSE;
5301
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005302 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005303 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005304 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005305 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005306 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 /* Setup variables for completion. Need to obtain "line" again,
5309 * it may have become invalid. */
5310 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005311 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005312 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5313 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005314#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005315 return FAIL;
5316 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005317 else if (ctrl_x_mode == CTRL_X_SPELL)
5318 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005319#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005320 if (spell_bad_len > 0)
5321 compl_col = curs_col - spell_bad_len;
5322 else
5323 compl_col = spell_word_start(startcol);
5324 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005325 {
5326 compl_length = 0;
5327 compl_col = curs_col;
5328 }
5329 else
5330 {
5331 spell_expand_check_cap(compl_col);
5332 compl_length = (int)curs_col - compl_col;
5333 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005334 /* Need to obtain "line" again, it may have become invalid. */
5335 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005336 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5337 if (compl_pattern == NULL)
5338#endif
5339 return FAIL;
5340 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005341 else
5342 {
5343 EMSG2(_(e_intern2), "ins_complete()");
5344 return FAIL;
5345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005347 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 {
5349 edit_submode_pre = (char_u *)_(" Adding");
5350 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5351 {
5352 /* Insert a new line, keep indentation but ignore 'comments' */
5353#ifdef FEAT_COMMENTS
5354 char_u *old = curbuf->b_p_com;
5355
5356 curbuf->b_p_com = (char_u *)"";
5357#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005358 compl_startpos.lnum = curwin->w_cursor.lnum;
5359 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005360 ins_eol('\r');
5361#ifdef FEAT_COMMENTS
5362 curbuf->b_p_com = old;
5363#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 compl_length = 0;
5365 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367 }
5368 else
5369 {
5370 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005371 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 }
5373
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005374 if (compl_cont_status & CONT_LOCAL)
5375 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 else
5377 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5378
Bram Moolenaar62951b12011-09-21 18:23:05 +02005379 /* If any of the original typed text has been changed we need to fix
5380 * the redo buffer. */
5381 ins_compl_fixRedoBufForLeader(NULL);
5382
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005383 /* Always add completion for the original text. */
5384 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005385 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5386 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005387 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005389 vim_free(compl_pattern);
5390 compl_pattern = NULL;
5391 vim_free(compl_orig_text);
5392 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 return FAIL;
5394 }
5395
5396 /* showmode might reset the internal line pointers, so it must
5397 * be called before line = ml_get(), or when this address is no
5398 * longer needed. -- Acevedo.
5399 */
5400 edit_submode_extra = (char_u *)_("-- Searching...");
5401 edit_submode_highl = HLF_COUNT;
5402 showmode();
5403 edit_submode_extra = NULL;
5404 out_flush();
5405 }
5406
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005407 compl_shown_match = compl_curr_match;
5408 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409
5410 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005411 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005413 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005414 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005416 /* may undisplay the popup menu */
5417 ins_compl_upd_pum();
5418
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005419 if (n > 1) /* all matches have been found */
5420 compl_matches = n;
5421 compl_curr_match = compl_shown_match;
5422 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423
Bram Moolenaard68071d2006-05-02 22:08:30 +00005424 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5425 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 if (got_int && !global_busy)
5427 {
5428 (void)vgetc();
5429 got_int = FALSE;
5430 }
5431
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005432 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005433 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005435 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5436 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5438 edit_submode_highl = HLF_E;
5439 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5440 * because we couldn't expand anything at first place, but if we used
5441 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5442 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005443 if ( compl_length > 1
5444 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 || (ctrl_x_mode != 0
5446 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5447 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005448 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449 }
5450
Bram Moolenaar572cb562005-08-05 21:35:02 +00005451 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005452 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005454 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455
5456 if (edit_submode_extra == NULL)
5457 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005458 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005459 {
5460 edit_submode_extra = (char_u *)_("Back at original");
5461 edit_submode_highl = HLF_W;
5462 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005463 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 {
5465 edit_submode_extra = (char_u *)_("Word from other line");
5466 edit_submode_highl = HLF_COUNT;
5467 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005468 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 {
5470 edit_submode_extra = (char_u *)_("The only match");
5471 edit_submode_highl = HLF_COUNT;
5472 }
5473 else
5474 {
5475 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005476 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005478 int number = 0;
5479 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005481 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482 {
5483 /* search backwards for the first valid (!= -1) number.
5484 * This should normally succeed already at the first loop
5485 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005486 for (match = compl_curr_match->cp_prev; match != NULL
5487 && match != compl_first_match;
5488 match = match->cp_prev)
5489 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005491 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 break;
5493 }
5494 if (match != NULL)
5495 /* go up and assign all numbers which are not assigned
5496 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005497 for (match = match->cp_next;
5498 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005499 match = match->cp_next)
5500 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 }
5502 else /* BACKWARD */
5503 {
5504 /* search forwards (upwards) for the first valid (!= -1)
5505 * number. This should normally succeed already at the
5506 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005507 for (match = compl_curr_match->cp_next; match != NULL
5508 && match != compl_first_match;
5509 match = match->cp_next)
5510 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005512 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513 break;
5514 }
5515 if (match != NULL)
5516 /* go down and assign all numbers which are not
5517 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005518 for (match = match->cp_prev; match
5519 && match->cp_number == -1;
5520 match = match->cp_prev)
5521 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 }
5523 }
5524
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005525 /* The match should always have a sequence number now, this is
5526 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005527 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005529 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5530 * Translations may need more than twice that. */
5531 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005532
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005533 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005534 vim_snprintf((char *)match_ref, sizeof(match_ref),
5535 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005536 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005538 vim_snprintf((char *)match_ref, sizeof(match_ref),
5539 _("match %d"),
5540 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 edit_submode_extra = match_ref;
5542 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005543 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 curs_columns(FALSE);
5545 }
5546 }
5547 }
5548
5549 /* Show a message about what (completion) mode we're in. */
5550 showmode();
5551 if (edit_submode_extra != NULL)
5552 {
5553 if (!p_smd)
5554 msg_attr(edit_submode_extra,
5555 edit_submode_highl < HLF_COUNT
5556 ? hl_attr(edit_submode_highl) : 0);
5557 }
5558 else
5559 msg_clr_cmdline(); /* necessary for "noshowmode" */
5560
Bram Moolenaard68071d2006-05-02 22:08:30 +00005561 /* Show the popup menu, unless we got interrupted. */
5562 if (!compl_interrupted)
5563 {
5564 /* RedrawingDisabled may be set when invoked through complete(). */
5565 n = RedrawingDisabled;
5566 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005567
5568 /* If the cursor moved we need to remove the pum first. */
5569 setcursor();
5570 if (save_w_wrow != curwin->w_wrow)
5571 ins_compl_del_pum();
5572
Bram Moolenaard68071d2006-05-02 22:08:30 +00005573 ins_compl_show_pum();
5574 setcursor();
5575 RedrawingDisabled = n;
5576 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005577 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005578 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005579
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 return OK;
5581}
5582
5583/*
5584 * Looks in the first "len" chars. of "src" for search-metachars.
5585 * If dest is not NULL the chars. are copied there quoting (with
5586 * a backslash) the metachars, and dest would be NUL terminated.
5587 * Returns the length (needed) of dest
5588 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005589 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005590quote_meta(dest, src, len)
5591 char_u *dest;
5592 char_u *src;
5593 int len;
5594{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005595 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005596
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005597 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005598 {
5599 switch (*src)
5600 {
5601 case '.':
5602 case '*':
5603 case '[':
5604 if (ctrl_x_mode == CTRL_X_DICTIONARY
5605 || ctrl_x_mode == CTRL_X_THESAURUS)
5606 break;
5607 case '~':
5608 if (!p_magic) /* quote these only if magic is set */
5609 break;
5610 case '\\':
5611 if (ctrl_x_mode == CTRL_X_DICTIONARY
5612 || ctrl_x_mode == CTRL_X_THESAURUS)
5613 break;
5614 case '^': /* currently it's not needed. */
5615 case '$':
5616 m++;
5617 if (dest != NULL)
5618 *dest++ = '\\';
5619 break;
5620 }
5621 if (dest != NULL)
5622 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005623# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005624 /* Copy remaining bytes of a multibyte character. */
5625 if (has_mbyte)
5626 {
5627 int i, mb_len;
5628
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005629 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 if (mb_len > 0 && len >= mb_len)
5631 for (i = 0; i < mb_len; ++i)
5632 {
5633 --len;
5634 ++src;
5635 if (dest != NULL)
5636 *dest++ = *src;
5637 }
5638 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005639# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640 }
5641 if (dest != NULL)
5642 *dest = NUL;
5643
5644 return m;
5645}
5646#endif /* FEAT_INS_EXPAND */
5647
5648/*
5649 * Next character is interpreted literally.
5650 * A one, two or three digit decimal number is interpreted as its byte value.
5651 * If one or two digits are entered, the next character is given to vungetc().
5652 * For Unicode a character > 255 may be returned.
5653 */
5654 int
5655get_literal()
5656{
5657 int cc;
5658 int nc;
5659 int i;
5660 int hex = FALSE;
5661 int octal = FALSE;
5662#ifdef FEAT_MBYTE
5663 int unicode = 0;
5664#endif
5665
5666 if (got_int)
5667 return Ctrl_C;
5668
5669#ifdef FEAT_GUI
5670 /*
5671 * In GUI there is no point inserting the internal code for a special key.
5672 * It is more useful to insert the string "<KEY>" instead. This would
5673 * probably be useful in a text window too, but it would not be
5674 * vi-compatible (maybe there should be an option for it?) -- webb
5675 */
5676 if (gui.in_use)
5677 ++allow_keys;
5678#endif
5679#ifdef USE_ON_FLY_SCROLL
5680 dont_scroll = TRUE; /* disallow scrolling here */
5681#endif
5682 ++no_mapping; /* don't map the next key hits */
5683 cc = 0;
5684 i = 0;
5685 for (;;)
5686 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005687 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688#ifdef FEAT_CMDL_INFO
5689 if (!(State & CMDLINE)
5690# ifdef FEAT_MBYTE
5691 && MB_BYTE2LEN_CHECK(nc) == 1
5692# endif
5693 )
5694 add_to_showcmd(nc);
5695#endif
5696 if (nc == 'x' || nc == 'X')
5697 hex = TRUE;
5698 else if (nc == 'o' || nc == 'O')
5699 octal = TRUE;
5700#ifdef FEAT_MBYTE
5701 else if (nc == 'u' || nc == 'U')
5702 unicode = nc;
5703#endif
5704 else
5705 {
5706 if (hex
5707#ifdef FEAT_MBYTE
5708 || unicode != 0
5709#endif
5710 )
5711 {
5712 if (!vim_isxdigit(nc))
5713 break;
5714 cc = cc * 16 + hex2nr(nc);
5715 }
5716 else if (octal)
5717 {
5718 if (nc < '0' || nc > '7')
5719 break;
5720 cc = cc * 8 + nc - '0';
5721 }
5722 else
5723 {
5724 if (!VIM_ISDIGIT(nc))
5725 break;
5726 cc = cc * 10 + nc - '0';
5727 }
5728
5729 ++i;
5730 }
5731
5732 if (cc > 255
5733#ifdef FEAT_MBYTE
5734 && unicode == 0
5735#endif
5736 )
5737 cc = 255; /* limit range to 0-255 */
5738 nc = 0;
5739
5740 if (hex) /* hex: up to two chars */
5741 {
5742 if (i >= 2)
5743 break;
5744 }
5745#ifdef FEAT_MBYTE
5746 else if (unicode) /* Unicode: up to four or eight chars */
5747 {
5748 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5749 break;
5750 }
5751#endif
5752 else if (i >= 3) /* decimal or octal: up to three chars */
5753 break;
5754 }
5755 if (i == 0) /* no number entered */
5756 {
5757 if (nc == K_ZERO) /* NUL is stored as NL */
5758 {
5759 cc = '\n';
5760 nc = 0;
5761 }
5762 else
5763 {
5764 cc = nc;
5765 nc = 0;
5766 }
5767 }
5768
5769 if (cc == 0) /* NUL is stored as NL */
5770 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005771#ifdef FEAT_MBYTE
5772 if (enc_dbcs && (cc & 0xff) == 0)
5773 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5774 second byte will cause trouble! */
5775#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776
5777 --no_mapping;
5778#ifdef FEAT_GUI
5779 if (gui.in_use)
5780 --allow_keys;
5781#endif
5782 if (nc)
5783 vungetc(nc);
5784 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5785 return cc;
5786}
5787
5788/*
5789 * Insert character, taking care of special keys and mod_mask
5790 */
5791 static void
5792insert_special(c, allow_modmask, ctrlv)
5793 int c;
5794 int allow_modmask;
5795 int ctrlv; /* c was typed after CTRL-V */
5796{
5797 char_u *p;
5798 int len;
5799
5800 /*
5801 * Special function key, translate into "<Key>". Up to the last '>' is
5802 * inserted with ins_str(), so as not to replace characters in replace
5803 * mode.
5804 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5805 * unless 'allow_modmask' is TRUE.
5806 */
5807#ifdef MACOS
5808 /* Command-key never produces a normal key */
5809 if (mod_mask & MOD_MASK_CMD)
5810 allow_modmask = TRUE;
5811#endif
5812 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5813 {
5814 p = get_special_key_name(c, mod_mask);
5815 len = (int)STRLEN(p);
5816 c = p[len - 1];
5817 if (len > 2)
5818 {
5819 if (stop_arrow() == FAIL)
5820 return;
5821 p[len - 1] = NUL;
5822 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005823 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 ctrlv = FALSE;
5825 }
5826 }
5827 if (stop_arrow() == OK)
5828 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5829}
5830
5831/*
5832 * Special characters in this context are those that need processing other
5833 * than the simple insertion that can be performed here. This includes ESC
5834 * which terminates the insert, and CR/NL which need special processing to
5835 * open up a new line. This routine tries to optimize insertions performed by
5836 * the "redo", "undo" or "put" commands, so it needs to know when it should
5837 * stop and defer processing to the "normal" mechanism.
5838 * '0' and '^' are special, because they can be followed by CTRL-D.
5839 */
5840#ifdef EBCDIC
5841# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5842#else
5843# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5844#endif
5845
5846#ifdef FEAT_MBYTE
5847# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5848#else
5849# define WHITECHAR(cc) vim_iswhite(cc)
5850#endif
5851
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005852/*
5853 * "flags": INSCHAR_FORMAT - force formatting
5854 * INSCHAR_CTRLV - char typed just after CTRL-V
5855 * INSCHAR_NO_FEX - don't use 'formatexpr'
5856 *
5857 * NOTE: passes the flags value straight through to internal_format() which,
5858 * beside INSCHAR_FORMAT (above), is also looking for these:
5859 * INSCHAR_DO_COM - format comments
5860 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5861 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 void
5863insertchar(c, flags, second_indent)
5864 int c; /* character to insert or NUL */
5865 int flags; /* INSCHAR_FORMAT, etc. */
5866 int second_indent; /* indent for second line if >= 0 */
5867{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 int textwidth;
5869#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873
5874 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5875 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005876
5877 /*
5878 * Try to break the line in two or more pieces when:
5879 * - Always do this if we have been called to do formatting only.
5880 * - Always do this when 'formatoptions' has the 'a' flag and the line
5881 * ends in white space.
5882 * - Otherwise:
5883 * - Don't do this if inserting a blank
5884 * - Don't do this if an existing character is being replaced, unless
5885 * we're in VREPLACE mode.
5886 * - Do this if the cursor is not on the line where insert started
5887 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5888 * before the insert.
5889 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5890 * before 'textwidth'
5891 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005892 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893 && ((flags & INSCHAR_FORMAT)
5894 || (!vim_iswhite(c)
5895 && !((State & REPLACE_FLAG)
5896#ifdef FEAT_VREPLACE
5897 && !(State & VREPLACE_FLAG)
5898#endif
5899 && *ml_get_cursor() != NUL)
5900 && (curwin->w_cursor.lnum != Insstart.lnum
5901 || ((!has_format_option(FO_INS_LONG)
5902 || Insstart_textlen <= (colnr_T)textwidth)
5903 && (!fo_ins_blank
5904 || Insstart_blank_vcol <= (colnr_T)textwidth
5905 ))))))
5906 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005907 /* Format with 'formatexpr' when it's set. Use internal formatting
5908 * when 'formatexpr' isn't set or it returns non-zero. */
5909#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005910 int do_internal = TRUE;
5911
Bram Moolenaar81a82092008-03-12 16:27:00 +00005912 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005913 {
5914 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5915 /* It may be required to save for undo again, e.g. when setline()
5916 * was called. */
5917 ins_need_undo = TRUE;
5918 }
5919 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005920#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005921 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005922 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005923
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 if (c == NUL) /* only formatting was wanted */
5925 return;
5926
5927#ifdef FEAT_COMMENTS
5928 /* Check whether this character should end a comment. */
5929 if (did_ai && (int)c == end_comment_pending)
5930 {
5931 char_u *line;
5932 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5933 int middle_len, end_len;
5934 int i;
5935
5936 /*
5937 * Need to remove existing (middle) comment leader and insert end
5938 * comment leader. First, check what comment leader we can find.
5939 */
Bram Moolenaar81340392012-06-06 16:12:59 +02005940 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005941 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5942 {
5943 /* Skip middle-comment string */
5944 while (*p && p[-1] != ':') /* find end of middle flags */
5945 ++p;
5946 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5947 /* Don't count trailing white space for middle_len */
5948 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5949 --middle_len;
5950
5951 /* Find the end-comment string */
5952 while (*p && p[-1] != ':') /* find end of end flags */
5953 ++p;
5954 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5955
5956 /* Skip white space before the cursor */
5957 i = curwin->w_cursor.col;
5958 while (--i >= 0 && vim_iswhite(line[i]))
5959 ;
5960 i++;
5961
5962 /* Skip to before the middle leader */
5963 i -= middle_len;
5964
5965 /* Check some expected things before we go on */
5966 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5967 {
5968 /* Backspace over all the stuff we want to replace */
5969 backspace_until_column(i);
5970
5971 /*
5972 * Insert the end-comment string, except for the last
5973 * character, which will get inserted as normal later.
5974 */
5975 ins_bytes_len(lead_end, end_len - 1);
5976 }
5977 }
5978 }
5979 end_comment_pending = NUL;
5980#endif
5981
5982 did_ai = FALSE;
5983#ifdef FEAT_SMARTINDENT
5984 did_si = FALSE;
5985 can_si = FALSE;
5986 can_si_back = FALSE;
5987#endif
5988
5989 /*
5990 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5991 * This speeds up normal text input considerably.
5992 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5993 * need to re-indent at a ':', or any other character (but not what
5994 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005995 * Don't do this when there an InsertCharPre autocommand is defined,
5996 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 */
5998#ifdef USE_ON_FLY_SCROLL
5999 dont_scroll = FALSE; /* allow scrolling here */
6000#endif
6001
6002 if ( !ISSPECIAL(c)
6003#ifdef FEAT_MBYTE
6004 && (!has_mbyte || (*mb_char2len)(c) == 1)
6005#endif
6006 && vpeekc() != NUL
6007 && !(State & REPLACE_FLAG)
6008#ifdef FEAT_CINDENT
6009 && !cindent_on()
6010#endif
6011#ifdef FEAT_RIGHTLEFT
6012 && !p_ri
6013#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006014#ifdef FEAT_AUTOCMD
6015 && !has_insertcharpre()
6016#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006017 )
6018 {
6019#define INPUT_BUFLEN 100
6020 char_u buf[INPUT_BUFLEN + 1];
6021 int i;
6022 colnr_T virtcol = 0;
6023
6024 buf[0] = c;
6025 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006026 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006027 virtcol = get_nolist_virtcol();
6028 /*
6029 * Stop the string when:
6030 * - no more chars available
6031 * - finding a special character (command key)
6032 * - buffer is full
6033 * - running into the 'textwidth' boundary
6034 * - need to check for abbreviation: A non-word char after a word-char
6035 */
6036 while ( (c = vpeekc()) != NUL
6037 && !ISSPECIAL(c)
6038#ifdef FEAT_MBYTE
6039 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6040#endif
6041 && i < INPUT_BUFLEN
6042 && (textwidth == 0
6043 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6044 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6045 {
6046#ifdef FEAT_RIGHTLEFT
6047 c = vgetc();
6048 if (p_hkmap && KeyTyped)
6049 c = hkmap(c); /* Hebrew mode mapping */
6050# ifdef FEAT_FKMAP
6051 if (p_fkmap && KeyTyped)
6052 c = fkmap(c); /* Farsi mode mapping */
6053# endif
6054 buf[i++] = c;
6055#else
6056 buf[i++] = vgetc();
6057#endif
6058 }
6059
6060#ifdef FEAT_DIGRAPHS
6061 do_digraph(-1); /* clear digraphs */
6062 do_digraph(buf[i-1]); /* may be the start of a digraph */
6063#endif
6064 buf[i] = NUL;
6065 ins_str(buf);
6066 if (flags & INSCHAR_CTRLV)
6067 {
6068 redo_literal(*buf);
6069 i = 1;
6070 }
6071 else
6072 i = 0;
6073 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006074 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006075 }
6076 else
6077 {
6078#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006079 int cc;
6080
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6082 {
6083 char_u buf[MB_MAXBYTES + 1];
6084
6085 (*mb_char2bytes)(c, buf);
6086 buf[cc] = NUL;
6087 ins_char_bytes(buf, cc);
6088 AppendCharToRedobuff(c);
6089 }
6090 else
6091#endif
6092 {
6093 ins_char(c);
6094 if (flags & INSCHAR_CTRLV)
6095 redo_literal(c);
6096 else
6097 AppendCharToRedobuff(c);
6098 }
6099 }
6100}
6101
6102/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006103 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006104 *
6105 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6106 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006107 */
6108 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006109internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006110 int textwidth;
6111 int second_indent;
6112 int flags;
6113 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006114 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006115{
6116 int cc;
6117 int save_char = NUL;
6118 int haveto_redraw = FALSE;
6119 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6120#ifdef FEAT_MBYTE
6121 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6122#endif
6123 int fo_white_par = has_format_option(FO_WHITE_PAR);
6124 int first_line = TRUE;
6125#ifdef FEAT_COMMENTS
6126 colnr_T leader_len;
6127 int no_leader = FALSE;
6128 int do_comments = (flags & INSCHAR_DO_COM);
6129#endif
6130
6131 /*
6132 * When 'ai' is off we don't want a space under the cursor to be
6133 * deleted. Replace it with an 'x' temporarily.
6134 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006135 if (!curbuf->b_p_ai
6136#ifdef FEAT_VREPLACE
6137 && !(State & VREPLACE_FLAG)
6138#endif
6139 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006140 {
6141 cc = gchar_cursor();
6142 if (vim_iswhite(cc))
6143 {
6144 save_char = cc;
6145 pchar_cursor('x');
6146 }
6147 }
6148
6149 /*
6150 * Repeat breaking lines, until the current line is not too long.
6151 */
6152 while (!got_int)
6153 {
6154 int startcol; /* Cursor column at entry */
6155 int wantcol; /* column at textwidth border */
6156 int foundcol; /* column for start of spaces */
6157 int end_foundcol = 0; /* column for start of word */
6158 colnr_T len;
6159 colnr_T virtcol;
6160#ifdef FEAT_VREPLACE
6161 int orig_col = 0;
6162 char_u *saved_text = NULL;
6163#endif
6164 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006165 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006166
Bram Moolenaar97b98102009-11-17 16:41:01 +00006167 virtcol = get_nolist_virtcol()
6168 + char2cells(c != NUL ? c : gchar_cursor());
6169 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006170 break;
6171
6172#ifdef FEAT_COMMENTS
6173 if (no_leader)
6174 do_comments = FALSE;
6175 else if (!(flags & INSCHAR_FORMAT)
6176 && has_format_option(FO_WRAP_COMS))
6177 do_comments = TRUE;
6178
6179 /* Don't break until after the comment leader */
6180 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006181 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006182 else
6183 leader_len = 0;
6184
6185 /* If the line doesn't start with a comment leader, then don't
6186 * start one in a following broken line. Avoids that a %word
6187 * moved to the start of the next line causes all following lines
6188 * to start with %. */
6189 if (leader_len == 0)
6190 no_leader = TRUE;
6191#endif
6192 if (!(flags & INSCHAR_FORMAT)
6193#ifdef FEAT_COMMENTS
6194 && leader_len == 0
6195#endif
6196 && !has_format_option(FO_WRAP))
6197
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006198 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006199 if ((startcol = curwin->w_cursor.col) == 0)
6200 break;
6201
6202 /* find column of textwidth border */
6203 coladvance((colnr_T)textwidth);
6204 wantcol = curwin->w_cursor.col;
6205
Bram Moolenaar97b98102009-11-17 16:41:01 +00006206 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006207 foundcol = 0;
6208
6209 /*
6210 * Find position to break at.
6211 * Stop at first entered white when 'formatoptions' has 'v'
6212 */
6213 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006214 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006215 || curwin->w_cursor.lnum != Insstart.lnum
6216 || curwin->w_cursor.col >= Insstart.col)
6217 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006218 if (curwin->w_cursor.col == startcol && c != NUL)
6219 cc = c;
6220 else
6221 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006222 if (WHITECHAR(cc))
6223 {
6224 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006225 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006226
6227 /* find start of sequence of blanks */
6228 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6229 {
6230 dec_cursor();
6231 cc = gchar_cursor();
6232 }
6233 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6234 break; /* only spaces in front of text */
6235#ifdef FEAT_COMMENTS
6236 /* Don't break until after the comment leader */
6237 if (curwin->w_cursor.col < leader_len)
6238 break;
6239#endif
6240 if (has_format_option(FO_ONE_LETTER))
6241 {
6242 /* do not break after one-letter words */
6243 if (curwin->w_cursor.col == 0)
6244 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006245#ifdef FEAT_COMMENTS
6246 /* do not break "#a b" when 'tw' is 2 */
6247 if (curwin->w_cursor.col <= leader_len)
6248 break;
6249#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006250 col = curwin->w_cursor.col;
6251 dec_cursor();
6252 cc = gchar_cursor();
6253
6254 if (WHITECHAR(cc))
6255 continue; /* one-letter, continue */
6256 curwin->w_cursor.col = col;
6257 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006258
6259 inc_cursor();
6260
6261 end_foundcol = end_col + 1;
6262 foundcol = curwin->w_cursor.col;
6263 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006264 break;
6265 }
6266#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006267 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006268 {
6269 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006270 if (curwin->w_cursor.col != startcol)
6271 {
6272#ifdef FEAT_COMMENTS
6273 /* Don't break until after the comment leader */
6274 if (curwin->w_cursor.col < leader_len)
6275 break;
6276#endif
6277 col = curwin->w_cursor.col;
6278 inc_cursor();
6279 /* Don't change end_foundcol if already set. */
6280 if (foundcol != curwin->w_cursor.col)
6281 {
6282 foundcol = curwin->w_cursor.col;
6283 end_foundcol = foundcol;
6284 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6285 break;
6286 }
6287 curwin->w_cursor.col = col;
6288 }
6289
6290 if (curwin->w_cursor.col == 0)
6291 break;
6292
6293 col = curwin->w_cursor.col;
6294
6295 dec_cursor();
6296 cc = gchar_cursor();
6297
6298 if (WHITECHAR(cc))
6299 continue; /* break with space */
6300#ifdef FEAT_COMMENTS
6301 /* Don't break until after the comment leader */
6302 if (curwin->w_cursor.col < leader_len)
6303 break;
6304#endif
6305
6306 curwin->w_cursor.col = col;
6307
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006308 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006309 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006310 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6311 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006312 }
6313#endif
6314 if (curwin->w_cursor.col == 0)
6315 break;
6316 dec_cursor();
6317 }
6318
6319 if (foundcol == 0) /* no spaces, cannot break line */
6320 {
6321 curwin->w_cursor.col = startcol;
6322 break;
6323 }
6324
6325 /* Going to break the line, remove any "$" now. */
6326 undisplay_dollar();
6327
6328 /*
6329 * Offset between cursor position and line break is used by replace
6330 * stack functions. VREPLACE does not use this, and backspaces
6331 * over the text instead.
6332 */
6333#ifdef FEAT_VREPLACE
6334 if (State & VREPLACE_FLAG)
6335 orig_col = startcol; /* Will start backspacing from here */
6336 else
6337#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006338 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006339
6340 /*
6341 * adjust startcol for spaces that will be deleted and
6342 * characters that will remain on top line
6343 */
6344 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006345 while ((cc = gchar_cursor(), WHITECHAR(cc))
6346 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006347 inc_cursor();
6348 startcol -= curwin->w_cursor.col;
6349 if (startcol < 0)
6350 startcol = 0;
6351
6352#ifdef FEAT_VREPLACE
6353 if (State & VREPLACE_FLAG)
6354 {
6355 /*
6356 * In VREPLACE mode, we will backspace over the text to be
6357 * wrapped, so save a copy now to put on the next line.
6358 */
6359 saved_text = vim_strsave(ml_get_cursor());
6360 curwin->w_cursor.col = orig_col;
6361 if (saved_text == NULL)
6362 break; /* Can't do it, out of memory */
6363 saved_text[startcol] = NUL;
6364
6365 /* Backspace over characters that will move to the next line */
6366 if (!fo_white_par)
6367 backspace_until_column(foundcol);
6368 }
6369 else
6370#endif
6371 {
6372 /* put cursor after pos. to break line */
6373 if (!fo_white_par)
6374 curwin->w_cursor.col = foundcol;
6375 }
6376
6377 /*
6378 * Split the line just before the margin.
6379 * Only insert/delete lines, but don't really redraw the window.
6380 */
6381 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6382 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6383#ifdef FEAT_COMMENTS
6384 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006385 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006386#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006387 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6388 if (!(flags & INSCHAR_COM_LIST))
6389 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006390
6391 replace_offset = 0;
6392 if (first_line)
6393 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006394 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006395 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006396 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006397 * This section is for auto-wrap of numeric lists. When not
6398 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6399 * flag will be set and open_line() will handle it (as seen
6400 * above). The code here (and in get_number_indent()) will
6401 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006402 */
6403 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006404 second_indent =
6405 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006406 if (second_indent >= 0)
6407 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006408#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006409 if (State & VREPLACE_FLAG)
6410 change_indent(INDENT_SET, second_indent,
6411 FALSE, NUL, TRUE);
6412 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006413#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006414#ifdef FEAT_COMMENTS
6415 if (leader_len > 0 && second_indent - leader_len > 0)
6416 {
6417 int i;
6418 int padding = second_indent - leader_len;
6419
6420 /* We started at the first_line of a numbered list
6421 * that has a comment. the open_line() function has
6422 * inserted the proper comment leader and positioned
6423 * the cursor at the end of the split line. Now we
6424 * add the additional whitespace needed after the
6425 * comment leader for the numbered list. */
6426 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006427 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006428 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006429 }
6430 else
6431 {
6432#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006433 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006434#ifdef FEAT_COMMENTS
6435 }
6436#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006437 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006438 }
6439 first_line = FALSE;
6440 }
6441
6442#ifdef FEAT_VREPLACE
6443 if (State & VREPLACE_FLAG)
6444 {
6445 /*
6446 * In VREPLACE mode we have backspaced over the text to be
6447 * moved, now we re-insert it into the new line.
6448 */
6449 ins_bytes(saved_text);
6450 vim_free(saved_text);
6451 }
6452 else
6453#endif
6454 {
6455 /*
6456 * Check if cursor is not past the NUL off the line, cindent
6457 * may have added or removed indent.
6458 */
6459 curwin->w_cursor.col += startcol;
6460 len = (colnr_T)STRLEN(ml_get_curline());
6461 if (curwin->w_cursor.col > len)
6462 curwin->w_cursor.col = len;
6463 }
6464
6465 haveto_redraw = TRUE;
6466#ifdef FEAT_CINDENT
6467 can_cindent = TRUE;
6468#endif
6469 /* moved the cursor, don't autoindent or cindent now */
6470 did_ai = FALSE;
6471#ifdef FEAT_SMARTINDENT
6472 did_si = FALSE;
6473 can_si = FALSE;
6474 can_si_back = FALSE;
6475#endif
6476 line_breakcheck();
6477 }
6478
6479 if (save_char != NUL) /* put back space after cursor */
6480 pchar_cursor(save_char);
6481
6482 if (!format_only && haveto_redraw)
6483 {
6484 update_topline();
6485 redraw_curbuf_later(VALID);
6486 }
6487}
6488
6489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 * Called after inserting or deleting text: When 'formatoptions' includes the
6491 * 'a' flag format from the current line until the end of the paragraph.
6492 * Keep the cursor at the same position relative to the text.
6493 * The caller must have saved the cursor line for undo, following ones will be
6494 * saved here.
6495 */
6496 void
6497auto_format(trailblank, prev_line)
6498 int trailblank; /* when TRUE also format with trailing blank */
6499 int prev_line; /* may start in previous line */
6500{
6501 pos_T pos;
6502 colnr_T len;
6503 char_u *old;
6504 char_u *new, *pnew;
6505 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006506 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006507
6508 if (!has_format_option(FO_AUTO))
6509 return;
6510
6511 pos = curwin->w_cursor;
6512 old = ml_get_curline();
6513
6514 /* may remove added space */
6515 check_auto_format(FALSE);
6516
6517 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6518 * user might insert normal text next. Also skip formatting when "1" is
6519 * in 'formatoptions' and there is a single character before the cursor.
6520 * Otherwise the line would be broken and when typing another non-white
6521 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006522 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 if (*old != NUL && !trailblank && wasatend)
6524 {
6525 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006526 cc = gchar_cursor();
6527 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6528 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006529 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006530 cc = gchar_cursor();
6531 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
6533 curwin->w_cursor = pos;
6534 return;
6535 }
6536 curwin->w_cursor = pos;
6537 }
6538
6539#ifdef FEAT_COMMENTS
6540 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6541 * comments. */
6542 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006543 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006544 return;
6545#endif
6546
6547 /*
6548 * May start formatting in a previous line, so that after "x" a word is
6549 * moved to the previous line if it fits there now. Only when this is not
6550 * the start of a paragraph.
6551 */
6552 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6553 {
6554 --curwin->w_cursor.lnum;
6555 if (u_save_cursor() == FAIL)
6556 return;
6557 }
6558
6559 /*
6560 * Do the formatting and restore the cursor position. "saved_cursor" will
6561 * be adjusted for the text formatting.
6562 */
6563 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006564 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565 curwin->w_cursor = saved_cursor;
6566 saved_cursor.lnum = 0;
6567
6568 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6569 {
6570 /* "cannot happen" */
6571 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6572 coladvance((colnr_T)MAXCOL);
6573 }
6574 else
6575 check_cursor_col();
6576
6577 /* Insert mode: If the cursor is now after the end of the line while it
6578 * previously wasn't, the line was broken. Because of the rule above we
6579 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6580 * formatted. */
6581 if (!wasatend && has_format_option(FO_WHITE_PAR))
6582 {
6583 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006584 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585 if (curwin->w_cursor.col == len)
6586 {
6587 pnew = vim_strnsave(new, len + 2);
6588 pnew[len] = ' ';
6589 pnew[len + 1] = NUL;
6590 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6591 /* remove the space later */
6592 did_add_space = TRUE;
6593 }
6594 else
6595 /* may remove added space */
6596 check_auto_format(FALSE);
6597 }
6598
6599 check_cursor();
6600}
6601
6602/*
6603 * When an extra space was added to continue a paragraph for auto-formatting,
6604 * delete it now. The space must be under the cursor, just after the insert
6605 * position.
6606 */
6607 static void
6608check_auto_format(end_insert)
6609 int end_insert; /* TRUE when ending Insert mode */
6610{
6611 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006612 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006613
6614 if (did_add_space)
6615 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006616 cc = gchar_cursor();
6617 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618 /* Somehow the space was removed already. */
6619 did_add_space = FALSE;
6620 else
6621 {
6622 if (!end_insert)
6623 {
6624 inc_cursor();
6625 c = gchar_cursor();
6626 dec_cursor();
6627 }
6628 if (c != NUL)
6629 {
6630 /* The space is no longer at the end of the line, delete it. */
6631 del_char(FALSE);
6632 did_add_space = FALSE;
6633 }
6634 }
6635 }
6636}
6637
6638/*
6639 * Find out textwidth to be used for formatting:
6640 * if 'textwidth' option is set, use it
6641 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6642 * if invalid value, use 0.
6643 * Set default to window width (maximum 79) for "gq" operator.
6644 */
6645 int
6646comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006647 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648{
6649 int textwidth;
6650
6651 textwidth = curbuf->b_p_tw;
6652 if (textwidth == 0 && curbuf->b_p_wm)
6653 {
6654 /* The width is the window width minus 'wrapmargin' minus all the
6655 * things that add to the margin. */
6656 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6657#ifdef FEAT_CMDWIN
6658 if (cmdwin_type != 0)
6659 textwidth -= 1;
6660#endif
6661#ifdef FEAT_FOLDING
6662 textwidth -= curwin->w_p_fdc;
6663#endif
6664#ifdef FEAT_SIGNS
6665 if (curwin->w_buffer->b_signlist != NULL
6666# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006667 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668# endif
6669 )
6670 textwidth -= 1;
6671#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006672 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 textwidth -= 8;
6674 }
6675 if (textwidth < 0)
6676 textwidth = 0;
6677 if (ff && textwidth == 0)
6678 {
6679 textwidth = W_WIDTH(curwin) - 1;
6680 if (textwidth > 79)
6681 textwidth = 79;
6682 }
6683 return textwidth;
6684}
6685
6686/*
6687 * Put a character in the redo buffer, for when just after a CTRL-V.
6688 */
6689 static void
6690redo_literal(c)
6691 int c;
6692{
6693 char_u buf[10];
6694
6695 /* Only digits need special treatment. Translate them into a string of
6696 * three digits. */
6697 if (VIM_ISDIGIT(c))
6698 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006699 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 AppendToRedobuff(buf);
6701 }
6702 else
6703 AppendCharToRedobuff(c);
6704}
6705
6706/*
6707 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006708 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 */
6710 static void
6711start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006712 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006713{
6714 if (!arrow_used) /* something has been inserted */
6715 {
6716 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006717 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718 arrow_used = TRUE; /* this means we stopped the current insert */
6719 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006720#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006721 check_spell_redraw();
6722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006723}
6724
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006725#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006726/*
6727 * If we skipped highlighting word at cursor, do it now.
6728 * It may be skipped again, thus reset spell_redraw_lnum first.
6729 */
6730 static void
6731check_spell_redraw()
6732{
6733 if (spell_redraw_lnum != 0)
6734 {
6735 linenr_T lnum = spell_redraw_lnum;
6736
6737 spell_redraw_lnum = 0;
6738 redrawWinline(lnum, FALSE);
6739 }
6740}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006741
6742/*
6743 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6744 * spelled word, if there is one.
6745 */
6746 static void
6747spell_back_to_badword()
6748{
6749 pos_T tpos = curwin->w_cursor;
6750
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006751 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006752 if (curwin->w_cursor.col != tpos.col)
6753 start_arrow(&tpos);
6754}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006755#endif
6756
Bram Moolenaar071d4272004-06-13 20:20:40 +00006757/*
6758 * stop_arrow() is called before a change is made in insert mode.
6759 * If an arrow key has been used, start a new insertion.
6760 * Returns FAIL if undo is impossible, shouldn't insert then.
6761 */
6762 int
6763stop_arrow()
6764{
6765 if (arrow_used)
6766 {
6767 if (u_save_cursor() == OK)
6768 {
6769 arrow_used = FALSE;
6770 ins_need_undo = FALSE;
6771 }
6772 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006773 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 ai_col = 0;
6775#ifdef FEAT_VREPLACE
6776 if (State & VREPLACE_FLAG)
6777 {
6778 orig_line_count = curbuf->b_ml.ml_line_count;
6779 vr_lines_changed = 1;
6780 }
6781#endif
6782 ResetRedobuff();
6783 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006784 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785 }
6786 else if (ins_need_undo)
6787 {
6788 if (u_save_cursor() == OK)
6789 ins_need_undo = FALSE;
6790 }
6791
6792#ifdef FEAT_FOLDING
6793 /* Always open fold at the cursor line when inserting something. */
6794 foldOpenCursor();
6795#endif
6796
6797 return (arrow_used || ins_need_undo ? FAIL : OK);
6798}
6799
6800/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006801 * Do a few things to stop inserting.
6802 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6803 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 */
6805 static void
Bram Moolenaarf332a652013-11-04 04:20:33 +01006806stop_insert(end_insert_pos, esc, nomove)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006807 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006808 int esc; /* called by ins_esc() */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006809 int nomove; /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006810{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006811 int cc;
6812 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006813
6814 stop_redo_ins();
6815 replace_flush(); /* abandon replace stack */
6816
6817 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006818 * Save the inserted text for later redo with ^@ and CTRL-A.
6819 * Don't do it when "restart_edit" was set and nothing was inserted,
6820 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006821 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006822 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006823 if (did_restart_edit == 0 || (ptr != NULL
6824 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006825 {
6826 vim_free(last_insert);
6827 last_insert = ptr;
6828 last_insert_skip = new_insert_skip;
6829 }
6830 else
6831 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006832
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006833 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006834 {
6835 /* Auto-format now. It may seem strange to do this when stopping an
6836 * insertion (or moving the cursor), but it's required when appending
6837 * a line and having it end in a space. But only do it when something
6838 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006839 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006840 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006841 pos_T tpos = curwin->w_cursor;
6842
Bram Moolenaar071d4272004-06-13 20:20:40 +00006843 /* When the cursor is at the end of the line after a space the
6844 * formatting will move it to the following word. Avoid that by
6845 * moving the cursor onto the space. */
6846 cc = 'x';
6847 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6848 {
6849 dec_cursor();
6850 cc = gchar_cursor();
6851 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006852 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006853 }
6854
6855 auto_format(TRUE, FALSE);
6856
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006857 if (vim_iswhite(cc))
6858 {
6859 if (gchar_cursor() != NUL)
6860 inc_cursor();
6861#ifdef FEAT_VIRTUALEDIT
6862 /* If the cursor is still at the same character, also keep
6863 * the "coladd". */
6864 if (gchar_cursor() == NUL
6865 && curwin->w_cursor.lnum == tpos.lnum
6866 && curwin->w_cursor.col == tpos.col)
6867 curwin->w_cursor.coladd = tpos.coladd;
6868#endif
6869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 }
6871
6872 /* If a space was inserted for auto-formatting, remove it now. */
6873 check_auto_format(TRUE);
6874
6875 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006876 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006877 * Do this when ESC was used or moving the cursor up/down.
6878 * Check for the old position still being valid, just in case the text
6879 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006880 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006881 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6882 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006884 pos_T tpos = curwin->w_cursor;
6885
6886 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006887 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006888 for (;;)
6889 {
6890 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6891 --curwin->w_cursor.col;
6892 cc = gchar_cursor();
6893 if (!vim_iswhite(cc))
6894 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006895 if (del_char(TRUE) == FAIL)
6896 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006897 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006898 if (curwin->w_cursor.lnum != tpos.lnum)
6899 curwin->w_cursor = tpos;
6900 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006901 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6902
6903#ifdef FEAT_VISUAL
6904 /* <C-S-Right> may have started Visual mode, adjust the position for
6905 * deleted characters. */
6906 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6907 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006908 int len = (int)STRLEN(ml_get_curline());
6909
6910 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006912 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006913# ifdef FEAT_VIRTUALEDIT
6914 VIsual.coladd = 0;
6915# endif
6916 }
6917 }
6918#endif
6919 }
6920 }
6921 did_ai = FALSE;
6922#ifdef FEAT_SMARTINDENT
6923 did_si = FALSE;
6924 can_si = FALSE;
6925 can_si_back = FALSE;
6926#endif
6927
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006928 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6929 * now in a different buffer. */
6930 if (end_insert_pos != NULL)
6931 {
6932 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01006933 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006934 curbuf->b_op_end = *end_insert_pos;
6935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006936}
6937
6938/*
6939 * Set the last inserted text to a single character.
6940 * Used for the replace command.
6941 */
6942 void
6943set_last_insert(c)
6944 int c;
6945{
6946 char_u *s;
6947
6948 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006950 if (last_insert != NULL)
6951 {
6952 s = last_insert;
6953 /* Use the CTRL-V only when entering a special char */
6954 if (c < ' ' || c == DEL)
6955 *s++ = Ctrl_V;
6956 s = add_char2buf(c, s);
6957 *s++ = ESC;
6958 *s++ = NUL;
6959 last_insert_skip = 0;
6960 }
6961}
6962
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006963#if defined(EXITFREE) || defined(PROTO)
6964 void
6965free_last_insert()
6966{
6967 vim_free(last_insert);
6968 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006969# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006970 vim_free(compl_orig_text);
6971 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006972# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006973}
6974#endif
6975
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976/*
6977 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6978 * and CSI. Handle multi-byte characters.
6979 * Returns a pointer to after the added bytes.
6980 */
6981 char_u *
6982add_char2buf(c, s)
6983 int c;
6984 char_u *s;
6985{
6986#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006987 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006988 int i;
6989 int len;
6990
6991 len = (*mb_char2bytes)(c, temp);
6992 for (i = 0; i < len; ++i)
6993 {
6994 c = temp[i];
6995#endif
6996 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6997 if (c == K_SPECIAL)
6998 {
6999 *s++ = K_SPECIAL;
7000 *s++ = KS_SPECIAL;
7001 *s++ = KE_FILLER;
7002 }
7003#ifdef FEAT_GUI
7004 else if (c == CSI)
7005 {
7006 *s++ = CSI;
7007 *s++ = KS_EXTRA;
7008 *s++ = (int)KE_CSI;
7009 }
7010#endif
7011 else
7012 *s++ = c;
7013#ifdef FEAT_MBYTE
7014 }
7015#endif
7016 return s;
7017}
7018
7019/*
7020 * move cursor to start of line
7021 * if flags & BL_WHITE move to first non-white
7022 * if flags & BL_SOL move to first non-white if startofline is set,
7023 * otherwise keep "curswant" column
7024 * if flags & BL_FIX don't leave the cursor on a NUL.
7025 */
7026 void
7027beginline(flags)
7028 int flags;
7029{
7030 if ((flags & BL_SOL) && !p_sol)
7031 coladvance(curwin->w_curswant);
7032 else
7033 {
7034 curwin->w_cursor.col = 0;
7035#ifdef FEAT_VIRTUALEDIT
7036 curwin->w_cursor.coladd = 0;
7037#endif
7038
7039 if (flags & (BL_WHITE | BL_SOL))
7040 {
7041 char_u *ptr;
7042
7043 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7044 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7045 ++curwin->w_cursor.col;
7046 }
7047 curwin->w_set_curswant = TRUE;
7048 }
7049}
7050
7051/*
7052 * oneright oneleft cursor_down cursor_up
7053 *
7054 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007055 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056 * Return OK when successful, FAIL when we hit a line of file boundary.
7057 */
7058
7059 int
7060oneright()
7061{
7062 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007064
7065#ifdef FEAT_VIRTUALEDIT
7066 if (virtual_active())
7067 {
7068 pos_T prevpos = curwin->w_cursor;
7069
7070 /* Adjust for multi-wide char (excluding TAB) */
7071 ptr = ml_get_cursor();
7072 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007073# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007074 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007075# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007076 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078 ))
7079 ? ptr2cells(ptr) : 1));
7080 curwin->w_set_curswant = TRUE;
7081 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7082 return (prevpos.col != curwin->w_cursor.col
7083 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7084 }
7085#endif
7086
7087 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007088 if (*ptr == NUL)
7089 return FAIL; /* already at the very end */
7090
Bram Moolenaar071d4272004-06-13 20:20:40 +00007091#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007092 if (has_mbyte)
7093 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007094 else
7095#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007096 l = 1;
7097
7098 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7099 * contains "onemore". */
7100 if (ptr[l] == NUL
7101#ifdef FEAT_VIRTUALEDIT
7102 && (ve_flags & VE_ONEMORE) == 0
7103#endif
7104 )
7105 return FAIL;
7106 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007107
7108 curwin->w_set_curswant = TRUE;
7109 return OK;
7110}
7111
7112 int
7113oneleft()
7114{
7115#ifdef FEAT_VIRTUALEDIT
7116 if (virtual_active())
7117 {
7118 int width;
7119 int v = getviscol();
7120
7121 if (v == 0)
7122 return FAIL;
7123
7124# ifdef FEAT_LINEBREAK
7125 /* We might get stuck on 'showbreak', skip over it. */
7126 width = 1;
7127 for (;;)
7128 {
7129 coladvance(v - width);
7130 /* getviscol() is slow, skip it when 'showbreak' is empty and
7131 * there are no multi-byte characters */
7132 if ((*p_sbr == NUL
7133# ifdef FEAT_MBYTE
7134 && !has_mbyte
7135# endif
7136 ) || getviscol() < v)
7137 break;
7138 ++width;
7139 }
7140# else
7141 coladvance(v - 1);
7142# endif
7143
7144 if (curwin->w_cursor.coladd == 1)
7145 {
7146 char_u *ptr;
7147
7148 /* Adjust for multi-wide char (not a TAB) */
7149 ptr = ml_get_cursor();
7150 if (*ptr != TAB && vim_isprintc(
7151# ifdef FEAT_MBYTE
7152 (*mb_ptr2char)(ptr)
7153# else
7154 *ptr
7155# endif
7156 ) && ptr2cells(ptr) > 1)
7157 curwin->w_cursor.coladd = 0;
7158 }
7159
7160 curwin->w_set_curswant = TRUE;
7161 return OK;
7162 }
7163#endif
7164
7165 if (curwin->w_cursor.col == 0)
7166 return FAIL;
7167
7168 curwin->w_set_curswant = TRUE;
7169 --curwin->w_cursor.col;
7170
7171#ifdef FEAT_MBYTE
7172 /* if the character on the left of the current cursor is a multi-byte
7173 * character, move to its first byte */
7174 if (has_mbyte)
7175 mb_adjust_cursor();
7176#endif
7177 return OK;
7178}
7179
7180 int
7181cursor_up(n, upd_topline)
7182 long n;
7183 int upd_topline; /* When TRUE: update topline */
7184{
7185 linenr_T lnum;
7186
7187 if (n > 0)
7188 {
7189 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007190 /* This fails if the cursor is already in the first line or the count
7191 * is larger than the line number and '-' is in 'cpoptions' */
7192 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007193 return FAIL;
7194 if (n >= lnum)
7195 lnum = 1;
7196 else
7197#ifdef FEAT_FOLDING
7198 if (hasAnyFolding(curwin))
7199 {
7200 /*
7201 * Count each sequence of folded lines as one logical line.
7202 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007203 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007204 (void)hasFolding(lnum, &lnum, NULL);
7205
7206 while (n--)
7207 {
7208 /* move up one line */
7209 --lnum;
7210 if (lnum <= 1)
7211 break;
7212 /* If we entered a fold, move to the beginning, unless in
7213 * Insert mode or when 'foldopen' contains "all": it will open
7214 * in a moment. */
7215 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7216 (void)hasFolding(lnum, &lnum, NULL);
7217 }
7218 if (lnum < 1)
7219 lnum = 1;
7220 }
7221 else
7222#endif
7223 lnum -= n;
7224 curwin->w_cursor.lnum = lnum;
7225 }
7226
7227 /* try to advance to the column we want to be at */
7228 coladvance(curwin->w_curswant);
7229
7230 if (upd_topline)
7231 update_topline(); /* make sure curwin->w_topline is valid */
7232
7233 return OK;
7234}
7235
7236/*
7237 * Cursor down a number of logical lines.
7238 */
7239 int
7240cursor_down(n, upd_topline)
7241 long n;
7242 int upd_topline; /* When TRUE: update topline */
7243{
7244 linenr_T lnum;
7245
7246 if (n > 0)
7247 {
7248 lnum = curwin->w_cursor.lnum;
7249#ifdef FEAT_FOLDING
7250 /* Move to last line of fold, will fail if it's the end-of-file. */
7251 (void)hasFolding(lnum, NULL, &lnum);
7252#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007253 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007254 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007255 if (lnum >= curbuf->b_ml.ml_line_count
7256 || (lnum + n > curbuf->b_ml.ml_line_count
7257 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007258 return FAIL;
7259 if (lnum + n >= curbuf->b_ml.ml_line_count)
7260 lnum = curbuf->b_ml.ml_line_count;
7261 else
7262#ifdef FEAT_FOLDING
7263 if (hasAnyFolding(curwin))
7264 {
7265 linenr_T last;
7266
7267 /* count each sequence of folded lines as one logical line */
7268 while (n--)
7269 {
7270 if (hasFolding(lnum, NULL, &last))
7271 lnum = last + 1;
7272 else
7273 ++lnum;
7274 if (lnum >= curbuf->b_ml.ml_line_count)
7275 break;
7276 }
7277 if (lnum > curbuf->b_ml.ml_line_count)
7278 lnum = curbuf->b_ml.ml_line_count;
7279 }
7280 else
7281#endif
7282 lnum += n;
7283 curwin->w_cursor.lnum = lnum;
7284 }
7285
7286 /* try to advance to the column we want to be at */
7287 coladvance(curwin->w_curswant);
7288
7289 if (upd_topline)
7290 update_topline(); /* make sure curwin->w_topline is valid */
7291
7292 return OK;
7293}
7294
7295/*
7296 * Stuff the last inserted text in the read buffer.
7297 * Last_insert actually is a copy of the redo buffer, so we
7298 * first have to remove the command.
7299 */
7300 int
7301stuff_inserted(c, count, no_esc)
7302 int c; /* Command character to be inserted */
7303 long count; /* Repeat this many times */
7304 int no_esc; /* Don't add an ESC at the end */
7305{
7306 char_u *esc_ptr;
7307 char_u *ptr;
7308 char_u *last_ptr;
7309 char_u last = NUL;
7310
7311 ptr = get_last_insert();
7312 if (ptr == NULL)
7313 {
7314 EMSG(_(e_noinstext));
7315 return FAIL;
7316 }
7317
7318 /* may want to stuff the command character, to start Insert mode */
7319 if (c != NUL)
7320 stuffcharReadbuff(c);
7321 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7322 *esc_ptr = NUL; /* remove the ESC */
7323
7324 /* when the last char is either "0" or "^" it will be quoted if no ESC
7325 * comes after it OR if it will inserted more than once and "ptr"
7326 * starts with ^D. -- Acevedo
7327 */
7328 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7329 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7330 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7331 {
7332 last = *last_ptr;
7333 *last_ptr = NUL;
7334 }
7335
7336 do
7337 {
7338 stuffReadbuff(ptr);
7339 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7340 if (last)
7341 stuffReadbuff((char_u *)(last == '0'
7342 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7343 : IF_EB("\026^", CTRL_V_STR "^")));
7344 }
7345 while (--count > 0);
7346
7347 if (last)
7348 *last_ptr = last;
7349
7350 if (esc_ptr != NULL)
7351 *esc_ptr = ESC; /* put the ESC back */
7352
7353 /* may want to stuff a trailing ESC, to get out of Insert mode */
7354 if (!no_esc)
7355 stuffcharReadbuff(ESC);
7356
7357 return OK;
7358}
7359
7360 char_u *
7361get_last_insert()
7362{
7363 if (last_insert == NULL)
7364 return NULL;
7365 return last_insert + last_insert_skip;
7366}
7367
7368/*
7369 * Get last inserted string, and remove trailing <Esc>.
7370 * Returns pointer to allocated memory (must be freed) or NULL.
7371 */
7372 char_u *
7373get_last_insert_save()
7374{
7375 char_u *s;
7376 int len;
7377
7378 if (last_insert == NULL)
7379 return NULL;
7380 s = vim_strsave(last_insert + last_insert_skip);
7381 if (s != NULL)
7382 {
7383 len = (int)STRLEN(s);
7384 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7385 s[len - 1] = NUL;
7386 }
7387 return s;
7388}
7389
7390/*
7391 * Check the word in front of the cursor for an abbreviation.
7392 * Called when the non-id character "c" has been entered.
7393 * When an abbreviation is recognized it is removed from the text and
7394 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7395 */
7396 static int
7397echeck_abbr(c)
7398 int c;
7399{
7400 /* Don't check for abbreviation in paste mode, when disabled and just
7401 * after moving around with cursor keys. */
7402 if (p_paste || no_abbr || arrow_used)
7403 return FALSE;
7404
7405 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7406 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7407}
7408
7409/*
7410 * replace-stack functions
7411 *
7412 * When replacing characters, the replaced characters are remembered for each
7413 * new character. This is used to re-insert the old text when backspacing.
7414 *
7415 * There is a NUL headed list of characters for each character that is
7416 * currently in the file after the insertion point. When BS is used, one NUL
7417 * headed list is put back for the deleted character.
7418 *
7419 * For a newline, there are two NUL headed lists. One contains the characters
7420 * that the NL replaced. The extra one stores the characters after the cursor
7421 * that were deleted (always white space).
7422 *
7423 * Replace_offset is normally 0, in which case replace_push will add a new
7424 * character at the end of the stack. If replace_offset is not 0, that many
7425 * characters will be left on the stack above the newly inserted character.
7426 */
7427
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007428static char_u *replace_stack = NULL;
7429static long replace_stack_nr = 0; /* next entry in replace stack */
7430static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007431
7432 void
7433replace_push(c)
7434 int c; /* character that is replaced (NUL is none) */
7435{
7436 char_u *p;
7437
7438 if (replace_stack_nr < replace_offset) /* nothing to do */
7439 return;
7440 if (replace_stack_len <= replace_stack_nr)
7441 {
7442 replace_stack_len += 50;
7443 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7444 if (p == NULL) /* out of memory */
7445 {
7446 replace_stack_len -= 50;
7447 return;
7448 }
7449 if (replace_stack != NULL)
7450 {
7451 mch_memmove(p, replace_stack,
7452 (size_t)(replace_stack_nr * sizeof(char_u)));
7453 vim_free(replace_stack);
7454 }
7455 replace_stack = p;
7456 }
7457 p = replace_stack + replace_stack_nr - replace_offset;
7458 if (replace_offset)
7459 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7460 *p = c;
7461 ++replace_stack_nr;
7462}
7463
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007464#if defined(FEAT_MBYTE) || defined(PROTO)
7465/*
7466 * Push a character onto the replace stack. Handles a multi-byte character in
7467 * reverse byte order, so that the first byte is popped off first.
7468 * Return the number of bytes done (includes composing characters).
7469 */
7470 int
7471replace_push_mb(p)
7472 char_u *p;
7473{
7474 int l = (*mb_ptr2len)(p);
7475 int j;
7476
7477 for (j = l - 1; j >= 0; --j)
7478 replace_push(p[j]);
7479 return l;
7480}
7481#endif
7482
Bram Moolenaar071d4272004-06-13 20:20:40 +00007483/*
7484 * Pop one item from the replace stack.
7485 * return -1 if stack empty
7486 * return replaced character or NUL otherwise
7487 */
7488 static int
7489replace_pop()
7490{
7491 if (replace_stack_nr == 0)
7492 return -1;
7493 return (int)replace_stack[--replace_stack_nr];
7494}
7495
7496/*
7497 * Join the top two items on the replace stack. This removes to "off"'th NUL
7498 * encountered.
7499 */
7500 static void
7501replace_join(off)
7502 int off; /* offset for which NUL to remove */
7503{
7504 int i;
7505
7506 for (i = replace_stack_nr; --i >= 0; )
7507 if (replace_stack[i] == NUL && off-- <= 0)
7508 {
7509 --replace_stack_nr;
7510 mch_memmove(replace_stack + i, replace_stack + i + 1,
7511 (size_t)(replace_stack_nr - i));
7512 return;
7513 }
7514}
7515
7516/*
7517 * Pop bytes from the replace stack until a NUL is found, and insert them
7518 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7519 */
7520 static void
7521replace_pop_ins()
7522{
7523 int cc;
7524 int oldState = State;
7525
7526 State = NORMAL; /* don't want REPLACE here */
7527 while ((cc = replace_pop()) > 0)
7528 {
7529#ifdef FEAT_MBYTE
7530 mb_replace_pop_ins(cc);
7531#else
7532 ins_char(cc);
7533#endif
7534 dec_cursor();
7535 }
7536 State = oldState;
7537}
7538
7539#ifdef FEAT_MBYTE
7540/*
7541 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7542 * indicates a multi-byte char, pop the other bytes too.
7543 */
7544 static void
7545mb_replace_pop_ins(cc)
7546 int cc;
7547{
7548 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007549 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007550 int i;
7551 int c;
7552
7553 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7554 {
7555 buf[0] = cc;
7556 for (i = 1; i < n; ++i)
7557 buf[i] = replace_pop();
7558 ins_bytes_len(buf, n);
7559 }
7560 else
7561 ins_char(cc);
7562
7563 if (enc_utf8)
7564 /* Handle composing chars. */
7565 for (;;)
7566 {
7567 c = replace_pop();
7568 if (c == -1) /* stack empty */
7569 break;
7570 if ((n = MB_BYTE2LEN(c)) == 1)
7571 {
7572 /* Not a multi-byte char, put it back. */
7573 replace_push(c);
7574 break;
7575 }
7576 else
7577 {
7578 buf[0] = c;
7579 for (i = 1; i < n; ++i)
7580 buf[i] = replace_pop();
7581 if (utf_iscomposing(utf_ptr2char(buf)))
7582 ins_bytes_len(buf, n);
7583 else
7584 {
7585 /* Not a composing char, put it back. */
7586 for (i = n - 1; i >= 0; --i)
7587 replace_push(buf[i]);
7588 break;
7589 }
7590 }
7591 }
7592}
7593#endif
7594
7595/*
7596 * make the replace stack empty
7597 * (called when exiting replace mode)
7598 */
7599 static void
7600replace_flush()
7601{
7602 vim_free(replace_stack);
7603 replace_stack = NULL;
7604 replace_stack_len = 0;
7605 replace_stack_nr = 0;
7606}
7607
7608/*
7609 * Handle doing a BS for one character.
7610 * cc < 0: replace stack empty, just move cursor
7611 * cc == 0: character was inserted, delete it
7612 * cc > 0: character was replaced, put cc (first byte of original char) back
7613 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007614 * When "limit_col" is >= 0, don't delete before this column. Matters when
7615 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616 */
7617 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007618replace_do_bs(limit_col)
7619 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007620{
7621 int cc;
7622#ifdef FEAT_VREPLACE
7623 int orig_len = 0;
7624 int ins_len;
7625 int orig_vcols = 0;
7626 colnr_T start_vcol;
7627 char_u *p;
7628 int i;
7629 int vcol;
7630#endif
7631
7632 cc = replace_pop();
7633 if (cc > 0)
7634 {
7635#ifdef FEAT_VREPLACE
7636 if (State & VREPLACE_FLAG)
7637 {
7638 /* Get the number of screen cells used by the character we are
7639 * going to delete. */
7640 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7641 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7642 }
7643#endif
7644#ifdef FEAT_MBYTE
7645 if (has_mbyte)
7646 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007647 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648# ifdef FEAT_VREPLACE
7649 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007650 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651# endif
7652 replace_push(cc);
7653 }
7654 else
7655#endif
7656 {
7657 pchar_cursor(cc);
7658#ifdef FEAT_VREPLACE
7659 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007660 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007661#endif
7662 }
7663 replace_pop_ins();
7664
7665#ifdef FEAT_VREPLACE
7666 if (State & VREPLACE_FLAG)
7667 {
7668 /* Get the number of screen cells used by the inserted characters */
7669 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007670 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007671 vcol = start_vcol;
7672 for (i = 0; i < ins_len; ++i)
7673 {
7674 vcol += chartabsize(p + i, vcol);
7675#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007676 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007677#endif
7678 }
7679 vcol -= start_vcol;
7680
7681 /* Delete spaces that were inserted after the cursor to keep the
7682 * text aligned. */
7683 curwin->w_cursor.col += ins_len;
7684 while (vcol > orig_vcols && gchar_cursor() == ' ')
7685 {
7686 del_char(FALSE);
7687 ++orig_vcols;
7688 }
7689 curwin->w_cursor.col -= ins_len;
7690 }
7691#endif
7692
7693 /* mark the buffer as changed and prepare for displaying */
7694 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7695 }
7696 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007697 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007698}
7699
7700#ifdef FEAT_CINDENT
7701/*
7702 * Return TRUE if C-indenting is on.
7703 */
7704 static int
7705cindent_on()
7706{
7707 return (!p_paste && (curbuf->b_p_cin
7708# ifdef FEAT_EVAL
7709 || *curbuf->b_p_inde != NUL
7710# endif
7711 ));
7712}
7713#endif
7714
7715#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7716/*
7717 * Re-indent the current line, based on the current contents of it and the
7718 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7719 * confused what all the part that handles Control-T is doing that I'm not.
7720 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7721 */
7722
7723 void
7724fixthisline(get_the_indent)
7725 int (*get_the_indent) __ARGS((void));
7726{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007727 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 if (linewhite(curwin->w_cursor.lnum))
7729 did_ai = TRUE; /* delete the indent if the line stays empty */
7730}
7731
7732 void
7733fix_indent()
7734{
7735 if (p_paste)
7736 return;
7737# ifdef FEAT_LISP
7738 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7739 fixthisline(get_lisp_indent);
7740# endif
7741# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7742 else
7743# endif
7744# ifdef FEAT_CINDENT
7745 if (cindent_on())
7746 do_c_expr_indent();
7747# endif
7748}
7749
7750#endif
7751
7752#ifdef FEAT_CINDENT
7753/*
7754 * return TRUE if 'cinkeys' contains the key "keytyped",
7755 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007756 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7758 *
7759 * "keytyped" can have a few special values:
7760 * KEY_OPEN_FORW
7761 * KEY_OPEN_BACK
7762 * KEY_COMPLETE just finished completion.
7763 *
7764 * If line_is_empty is TRUE accept keys with '0' before them.
7765 */
7766 int
7767in_cinkeys(keytyped, when, line_is_empty)
7768 int keytyped;
7769 int when;
7770 int line_is_empty;
7771{
7772 char_u *look;
7773 int try_match;
7774 int try_match_word;
7775 char_u *p;
7776 char_u *line;
7777 int icase;
7778 int i;
7779
Bram Moolenaar30848942009-12-24 14:46:12 +00007780 if (keytyped == NUL)
7781 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7782 return FALSE;
7783
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784#ifdef FEAT_EVAL
7785 if (*curbuf->b_p_inde != NUL)
7786 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7787 else
7788#endif
7789 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7790 while (*look)
7791 {
7792 /*
7793 * Find out if we want to try a match with this key, depending on
7794 * 'when' and a '*' or '!' before the key.
7795 */
7796 switch (when)
7797 {
7798 case '*': try_match = (*look == '*'); break;
7799 case '!': try_match = (*look == '!'); break;
7800 default: try_match = (*look != '*'); break;
7801 }
7802 if (*look == '*' || *look == '!')
7803 ++look;
7804
7805 /*
7806 * If there is a '0', only accept a match if the line is empty.
7807 * But may still match when typing last char of a word.
7808 */
7809 if (*look == '0')
7810 {
7811 try_match_word = try_match;
7812 if (!line_is_empty)
7813 try_match = FALSE;
7814 ++look;
7815 }
7816 else
7817 try_match_word = FALSE;
7818
7819 /*
7820 * does it look like a control character?
7821 */
7822 if (*look == '^'
7823#ifdef EBCDIC
7824 && (Ctrl_chr(look[1]) != 0)
7825#else
7826 && look[1] >= '?' && look[1] <= '_'
7827#endif
7828 )
7829 {
7830 if (try_match && keytyped == Ctrl_chr(look[1]))
7831 return TRUE;
7832 look += 2;
7833 }
7834 /*
7835 * 'o' means "o" command, open forward.
7836 * 'O' means "O" command, open backward.
7837 */
7838 else if (*look == 'o')
7839 {
7840 if (try_match && keytyped == KEY_OPEN_FORW)
7841 return TRUE;
7842 ++look;
7843 }
7844 else if (*look == 'O')
7845 {
7846 if (try_match && keytyped == KEY_OPEN_BACK)
7847 return TRUE;
7848 ++look;
7849 }
7850
7851 /*
7852 * 'e' means to check for "else" at start of line and just before the
7853 * cursor.
7854 */
7855 else if (*look == 'e')
7856 {
7857 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7858 {
7859 p = ml_get_curline();
7860 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7861 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7862 return TRUE;
7863 }
7864 ++look;
7865 }
7866
7867 /*
7868 * ':' only causes an indent if it is at the end of a label or case
7869 * statement, or when it was before typing the ':' (to fix
7870 * class::method for C++).
7871 */
7872 else if (*look == ':')
7873 {
7874 if (try_match && keytyped == ':')
7875 {
7876 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007877 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007879 /* Need to get the line again after cin_islabel(). */
7880 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007881 if (curwin->w_cursor.col > 2
7882 && p[curwin->w_cursor.col - 1] == ':'
7883 && p[curwin->w_cursor.col - 2] == ':')
7884 {
7885 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007886 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007887 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007888 p = ml_get_curline();
7889 p[curwin->w_cursor.col - 1] = ':';
7890 if (i)
7891 return TRUE;
7892 }
7893 }
7894 ++look;
7895 }
7896
7897
7898 /*
7899 * Is it a key in <>, maybe?
7900 */
7901 else if (*look == '<')
7902 {
7903 if (try_match)
7904 {
7905 /*
7906 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7907 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7908 * >, *, : and ! keys if they really really want to.
7909 */
7910 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7911 && keytyped == look[1])
7912 return TRUE;
7913
7914 if (keytyped == get_special_key_code(look + 1))
7915 return TRUE;
7916 }
7917 while (*look && *look != '>')
7918 look++;
7919 while (*look == '>')
7920 look++;
7921 }
7922
7923 /*
7924 * Is it a word: "=word"?
7925 */
7926 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7927 {
7928 ++look;
7929 if (*look == '~')
7930 {
7931 icase = TRUE;
7932 ++look;
7933 }
7934 else
7935 icase = FALSE;
7936 p = vim_strchr(look, ',');
7937 if (p == NULL)
7938 p = look + STRLEN(look);
7939 if ((try_match || try_match_word)
7940 && curwin->w_cursor.col >= (colnr_T)(p - look))
7941 {
7942 int match = FALSE;
7943
7944#ifdef FEAT_INS_EXPAND
7945 if (keytyped == KEY_COMPLETE)
7946 {
7947 char_u *s;
7948
7949 /* Just completed a word, check if it starts with "look".
7950 * search back for the start of a word. */
7951 line = ml_get_curline();
7952# ifdef FEAT_MBYTE
7953 if (has_mbyte)
7954 {
7955 char_u *n;
7956
7957 for (s = line + curwin->w_cursor.col; s > line; s = n)
7958 {
7959 n = mb_prevptr(line, s);
7960 if (!vim_iswordp(n))
7961 break;
7962 }
7963 }
7964 else
7965# endif
7966 for (s = line + curwin->w_cursor.col; s > line; --s)
7967 if (!vim_iswordc(s[-1]))
7968 break;
7969 if (s + (p - look) <= line + curwin->w_cursor.col
7970 && (icase
7971 ? MB_STRNICMP(s, look, p - look)
7972 : STRNCMP(s, look, p - look)) == 0)
7973 match = TRUE;
7974 }
7975 else
7976#endif
7977 /* TODO: multi-byte */
7978 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7979 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7980 {
7981 line = ml_get_cursor();
7982 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7983 || !vim_iswordc(line[-(p - look) - 1]))
7984 && (icase
7985 ? MB_STRNICMP(line - (p - look), look, p - look)
7986 : STRNCMP(line - (p - look), look, p - look))
7987 == 0)
7988 match = TRUE;
7989 }
7990 if (match && try_match_word && !try_match)
7991 {
7992 /* "0=word": Check if there are only blanks before the
7993 * word. */
7994 line = ml_get_curline();
7995 if ((int)(skipwhite(line) - line) !=
7996 (int)(curwin->w_cursor.col - (p - look)))
7997 match = FALSE;
7998 }
7999 if (match)
8000 return TRUE;
8001 }
8002 look = p;
8003 }
8004
8005 /*
8006 * ok, it's a boring generic character.
8007 */
8008 else
8009 {
8010 if (try_match && *look == keytyped)
8011 return TRUE;
8012 ++look;
8013 }
8014
8015 /*
8016 * Skip over ", ".
8017 */
8018 look = skip_to_option_part(look);
8019 }
8020 return FALSE;
8021}
8022#endif /* FEAT_CINDENT */
8023
8024#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8025/*
8026 * Map Hebrew keyboard when in hkmap mode.
8027 */
8028 int
8029hkmap(c)
8030 int c;
8031{
8032 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8033 {
8034 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8035 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8036 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8037 static char_u map[26] =
8038 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8039 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8040 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8041 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8042 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8043 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8044 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8045 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8046 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8047
8048 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8049 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8050 /* '-1'='sofit' */
8051 else if (c == 'x')
8052 return 'X';
8053 else if (c == 'q')
8054 return '\''; /* {geresh}={'} */
8055 else if (c == 246)
8056 return ' '; /* \"o --> ' ' for a german keyboard */
8057 else if (c == 228)
8058 return ' '; /* \"a --> ' ' -- / -- */
8059 else if (c == 252)
8060 return ' '; /* \"u --> ' ' -- / -- */
8061#ifdef EBCDIC
8062 else if (islower(c))
8063#else
8064 /* NOTE: islower() does not do the right thing for us on Linux so we
8065 * do this the same was as 5.7 and previous, so it works correctly on
8066 * all systems. Specifically, the e.g. Delete and Arrow keys are
8067 * munged and won't work if e.g. searching for Hebrew text.
8068 */
8069 else if (c >= 'a' && c <= 'z')
8070#endif
8071 return (int)(map[CharOrdLow(c)] + p_aleph);
8072 else
8073 return c;
8074 }
8075 else
8076 {
8077 switch (c)
8078 {
8079 case '`': return ';';
8080 case '/': return '.';
8081 case '\'': return ',';
8082 case 'q': return '/';
8083 case 'w': return '\'';
8084
8085 /* Hebrew letters - set offset from 'a' */
8086 case ',': c = '{'; break;
8087 case '.': c = 'v'; break;
8088 case ';': c = 't'; break;
8089 default: {
8090 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8091
8092#ifdef EBCDIC
8093 /* see note about islower() above */
8094 if (!islower(c))
8095#else
8096 if (c < 'a' || c > 'z')
8097#endif
8098 return c;
8099 c = str[CharOrdLow(c)];
8100 break;
8101 }
8102 }
8103
8104 return (int)(CharOrdLow(c) + p_aleph);
8105 }
8106}
8107#endif
8108
8109 static void
8110ins_reg()
8111{
8112 int need_redraw = FALSE;
8113 int regname;
8114 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008115#ifdef FEAT_VISUAL
8116 int vis_active = VIsual_active;
8117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008118
8119 /*
8120 * If we are going to wait for a character, show a '"'.
8121 */
8122 pc_status = PC_STATUS_UNSET;
8123 if (redrawing() && !char_avail())
8124 {
8125 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008126 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008127
8128 edit_putchar('"', TRUE);
8129#ifdef FEAT_CMDL_INFO
8130 add_to_showcmd_c(Ctrl_R);
8131#endif
8132 }
8133
8134#ifdef USE_ON_FLY_SCROLL
8135 dont_scroll = TRUE; /* disallow scrolling here */
8136#endif
8137
8138 /*
8139 * Don't map the register name. This also prevents the mode message to be
8140 * deleted when ESC is hit.
8141 */
8142 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008143 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8146 {
8147 /* Get a third key for literal register insertion */
8148 literally = regname;
8149#ifdef FEAT_CMDL_INFO
8150 add_to_showcmd_c(literally);
8151#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008152 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008153 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 }
8155 --no_mapping;
8156
8157#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008158 /* Don't call u_sync() while typing the expression or giving an error
8159 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 ++no_u_sync;
8161 if (regname == '=')
8162 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008163# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008165# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008166 /* Sync undo when evaluating the expression calls setline() or
8167 * append(), so that it can be undone separately. */
8168 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008169
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008171# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 /* Restore the Input Method. */
8173 if (im_on)
8174 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008175# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008176 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008177 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8178 {
8179 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008180 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 else
8183 {
8184#endif
8185 if (literally == Ctrl_O || literally == Ctrl_P)
8186 {
8187 /* Append the command to the redo buffer. */
8188 AppendCharToRedobuff(Ctrl_R);
8189 AppendCharToRedobuff(literally);
8190 AppendCharToRedobuff(regname);
8191
8192 do_put(regname, BACKWARD, 1L,
8193 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8194 }
8195 else if (insert_reg(regname, literally) == FAIL)
8196 {
8197 vim_beep();
8198 need_redraw = TRUE; /* remove the '"' */
8199 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008200 else if (stop_insert_mode)
8201 /* When the '=' register was used and a function was invoked that
8202 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8203 * insert anything, need to remove the '"' */
8204 need_redraw = TRUE;
8205
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206#ifdef FEAT_EVAL
8207 }
8208 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008209 if (u_sync_once == 1)
8210 ins_need_undo = TRUE;
8211 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212#endif
8213#ifdef FEAT_CMDL_INFO
8214 clear_showcmd();
8215#endif
8216
8217 /* If the inserted register is empty, we need to remove the '"' */
8218 if (need_redraw || stuff_empty())
8219 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008220
8221#ifdef FEAT_VISUAL
8222 /* Disallow starting Visual mode here, would get a weird mode. */
8223 if (!vis_active && VIsual_active)
8224 end_visual_mode();
8225#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226}
8227
8228/*
8229 * CTRL-G commands in Insert mode.
8230 */
8231 static void
8232ins_ctrl_g()
8233{
8234 int c;
8235
8236#ifdef FEAT_INS_EXPAND
8237 /* Right after CTRL-X the cursor will be after the ruler. */
8238 setcursor();
8239#endif
8240
8241 /*
8242 * Don't map the second key. This also prevents the mode message to be
8243 * deleted when ESC is hit.
8244 */
8245 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008246 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 --no_mapping;
8248 switch (c)
8249 {
8250 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8251 case K_UP:
8252 case Ctrl_K:
8253 case 'k': ins_up(TRUE);
8254 break;
8255
8256 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8257 case K_DOWN:
8258 case Ctrl_J:
8259 case 'j': ins_down(TRUE);
8260 break;
8261
8262 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008263 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008264 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008265
8266 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008267 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008268 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008269 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008270 break;
8271
8272 /* Unknown CTRL-G command, reserved for future expansion. */
8273 default: vim_beep();
8274 }
8275}
8276
8277/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008278 * CTRL-^ in Insert mode.
8279 */
8280 static void
8281ins_ctrl_hat()
8282{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008283 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008284 {
8285 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8286 if (State & LANGMAP)
8287 {
8288 curbuf->b_p_iminsert = B_IMODE_NONE;
8289 State &= ~LANGMAP;
8290 }
8291 else
8292 {
8293 curbuf->b_p_iminsert = B_IMODE_LMAP;
8294 State |= LANGMAP;
8295#ifdef USE_IM_CONTROL
8296 im_set_active(FALSE);
8297#endif
8298 }
8299 }
8300#ifdef USE_IM_CONTROL
8301 else
8302 {
8303 /* There are no ":lmap" mappings, toggle IM */
8304 if (im_get_status())
8305 {
8306 curbuf->b_p_iminsert = B_IMODE_NONE;
8307 im_set_active(FALSE);
8308 }
8309 else
8310 {
8311 curbuf->b_p_iminsert = B_IMODE_IM;
8312 State &= ~LANGMAP;
8313 im_set_active(TRUE);
8314 }
8315 }
8316#endif
8317 set_iminsert_global();
8318 showmode();
8319#ifdef FEAT_GUI
8320 /* may show different cursor shape or color */
8321 if (gui.in_use)
8322 gui_update_cursor(TRUE, FALSE);
8323#endif
8324#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8325 /* Show/unshow value of 'keymap' in status lines. */
8326 status_redraw_curbuf();
8327#endif
8328}
8329
8330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331 * Handle ESC in insert mode.
8332 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8333 * insert.
8334 */
8335 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008336ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 long *count;
8338 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008339 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340{
8341 int temp;
8342 static int disabled_redraw = FALSE;
8343
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008344#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008345 check_spell_redraw();
8346#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008347#if defined(FEAT_HANGULIN)
8348# if defined(ESC_CHG_TO_ENG_MODE)
8349 hangul_input_state_set(0);
8350# endif
8351 if (composing_hangul)
8352 {
8353 push_raw_key(composing_hangul_buffer, 2);
8354 composing_hangul = 0;
8355 }
8356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357
8358 temp = curwin->w_cursor.col;
8359 if (disabled_redraw)
8360 {
8361 --RedrawingDisabled;
8362 disabled_redraw = FALSE;
8363 }
8364 if (!arrow_used)
8365 {
8366 /*
8367 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008368 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8369 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370 */
8371 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008372 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373
8374 /*
8375 * Repeating insert may take a long time. Check for
8376 * interrupt now and then.
8377 */
8378 if (*count > 0)
8379 {
8380 line_breakcheck();
8381 if (got_int)
8382 *count = 0;
8383 }
8384
8385 if (--*count > 0) /* repeat what was typed */
8386 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008387 /* Vi repeats the insert without replacing characters. */
8388 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8389 State &= ~REPLACE_FLAG;
8390
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391 (void)start_redo_ins();
8392 if (cmdchar == 'r' || cmdchar == 'v')
8393 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8394 ++RedrawingDisabled;
8395 disabled_redraw = TRUE;
8396 return FALSE; /* repeat the insert */
8397 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008398 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 undisplay_dollar();
8400 }
8401
8402 /* When an autoindent was removed, curswant stays after the
8403 * indent */
8404 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8405 curwin->w_set_curswant = TRUE;
8406
8407 /* Remember the last Insert position in the '^ mark. */
8408 if (!cmdmod.keepjumps)
8409 curbuf->b_last_insert = curwin->w_cursor;
8410
8411 /*
8412 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008413 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008415 if (!nomove
8416 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417#ifdef FEAT_VIRTUALEDIT
8418 || curwin->w_cursor.coladd > 0
8419#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008420 )
8421 && (restart_edit == NUL
8422 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008424 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008426 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008427#ifdef FEAT_RIGHTLEFT
8428 && !revins_on
8429#endif
8430 )
8431 {
8432#ifdef FEAT_VIRTUALEDIT
8433 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8434 {
8435 oneleft();
8436 if (restart_edit != NUL)
8437 ++curwin->w_cursor.coladd;
8438 }
8439 else
8440#endif
8441 {
8442 --curwin->w_cursor.col;
8443#ifdef FEAT_MBYTE
8444 /* Correct cursor for multi-byte character. */
8445 if (has_mbyte)
8446 mb_adjust_cursor();
8447#endif
8448 }
8449 }
8450
8451#ifdef USE_IM_CONTROL
8452 /* Disable IM to allow typing English directly for Normal mode commands.
8453 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8454 * well). */
8455 if (!(State & LANGMAP))
8456 im_save_status(&curbuf->b_p_iminsert);
8457 im_set_active(FALSE);
8458#endif
8459
8460 State = NORMAL;
8461 /* need to position cursor again (e.g. when on a TAB ) */
8462 changed_cline_bef_curs();
8463
8464#ifdef FEAT_MOUSE
8465 setmouse();
8466#endif
8467#ifdef CURSOR_SHAPE
8468 ui_cursor_shape(); /* may show different cursor shape */
8469#endif
8470
8471 /*
8472 * When recording or for CTRL-O, need to display the new mode.
8473 * Otherwise remove the mode message.
8474 */
8475 if (Recording || restart_edit != NUL)
8476 showmode();
8477 else if (p_smd)
8478 MSG("");
8479
8480 return TRUE; /* exit Insert mode */
8481}
8482
8483#ifdef FEAT_RIGHTLEFT
8484/*
8485 * Toggle language: hkmap and revins_on.
8486 * Move to end of reverse inserted text.
8487 */
8488 static void
8489ins_ctrl_()
8490{
8491 if (revins_on && revins_chars && revins_scol >= 0)
8492 {
8493 while (gchar_cursor() != NUL && revins_chars--)
8494 ++curwin->w_cursor.col;
8495 }
8496 p_ri = !p_ri;
8497 revins_on = (State == INSERT && p_ri);
8498 if (revins_on)
8499 {
8500 revins_scol = curwin->w_cursor.col;
8501 revins_legal++;
8502 revins_chars = 0;
8503 undisplay_dollar();
8504 }
8505 else
8506 revins_scol = -1;
8507#ifdef FEAT_FKMAP
8508 if (p_altkeymap)
8509 {
8510 /*
8511 * to be consistent also for redo command, using '.'
8512 * set arrow_used to true and stop it - causing to redo
8513 * characters entered in one mode (normal/reverse insert).
8514 */
8515 arrow_used = TRUE;
8516 (void)stop_arrow();
8517 p_fkmap = curwin->w_p_rl ^ p_ri;
8518 if (p_fkmap && p_ri)
8519 State = INSERT;
8520 }
8521 else
8522#endif
8523 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8524 showmode();
8525}
8526#endif
8527
8528#ifdef FEAT_VISUAL
8529/*
8530 * If 'keymodel' contains "startsel", may start selection.
8531 * Returns TRUE when a CTRL-O and other keys stuffed.
8532 */
8533 static int
8534ins_start_select(c)
8535 int c;
8536{
8537 if (km_startsel)
8538 switch (c)
8539 {
8540 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 case K_PAGEUP:
8543 case K_KPAGEUP:
8544 case K_PAGEDOWN:
8545 case K_KPAGEDOWN:
8546# ifdef MACOS
8547 case K_LEFT:
8548 case K_RIGHT:
8549 case K_UP:
8550 case K_DOWN:
8551 case K_END:
8552 case K_HOME:
8553# endif
8554 if (!(mod_mask & MOD_MASK_SHIFT))
8555 break;
8556 /* FALLTHROUGH */
8557 case K_S_LEFT:
8558 case K_S_RIGHT:
8559 case K_S_UP:
8560 case K_S_DOWN:
8561 case K_S_END:
8562 case K_S_HOME:
8563 /* Start selection right away, the cursor can move with
8564 * CTRL-O when beyond the end of the line. */
8565 start_selection();
8566
8567 /* Execute the key in (insert) Select mode. */
8568 stuffcharReadbuff(Ctrl_O);
8569 if (mod_mask)
8570 {
8571 char_u buf[4];
8572
8573 buf[0] = K_SPECIAL;
8574 buf[1] = KS_MODIFIER;
8575 buf[2] = mod_mask;
8576 buf[3] = NUL;
8577 stuffReadbuff(buf);
8578 }
8579 stuffcharReadbuff(c);
8580 return TRUE;
8581 }
8582 return FALSE;
8583}
8584#endif
8585
8586/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008587 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008588 */
8589 static void
8590ins_insert(replaceState)
8591 int replaceState;
8592{
8593#ifdef FEAT_FKMAP
8594 if (p_fkmap && p_ri)
8595 {
8596 beep_flush();
8597 EMSG(farsi_text_3); /* encoded in Farsi */
8598 return;
8599 }
8600#endif
8601
8602#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008603# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008604 set_vim_var_string(VV_INSERTMODE,
8605 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008606# ifdef FEAT_VREPLACE
8607 replaceState == VREPLACE ? "v" :
8608# endif
8609 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008610# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008611 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8612#endif
8613 if (State & REPLACE_FLAG)
8614 State = INSERT | (State & LANGMAP);
8615 else
8616 State = replaceState | (State & LANGMAP);
8617 AppendCharToRedobuff(K_INS);
8618 showmode();
8619#ifdef CURSOR_SHAPE
8620 ui_cursor_shape(); /* may show different cursor shape */
8621#endif
8622}
8623
8624/*
8625 * Pressed CTRL-O in Insert mode.
8626 */
8627 static void
8628ins_ctrl_o()
8629{
8630#ifdef FEAT_VREPLACE
8631 if (State & VREPLACE_FLAG)
8632 restart_edit = 'V';
8633 else
8634#endif
8635 if (State & REPLACE_FLAG)
8636 restart_edit = 'R';
8637 else
8638 restart_edit = 'I';
8639#ifdef FEAT_VIRTUALEDIT
8640 if (virtual_active())
8641 ins_at_eol = FALSE; /* cursor always keeps its column */
8642 else
8643#endif
8644 ins_at_eol = (gchar_cursor() == NUL);
8645}
8646
8647/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 * If the cursor is on an indent, ^T/^D insert/delete one
8649 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008650 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008651 * with vi. But vi only supports ^T and ^D after an
8652 * autoindent, we support it everywhere.
8653 */
8654 static void
8655ins_shift(c, lastc)
8656 int c;
8657 int lastc;
8658{
8659 if (stop_arrow() == FAIL)
8660 return;
8661 AppendCharToRedobuff(c);
8662
8663 /*
8664 * 0^D and ^^D: remove all indent.
8665 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008666 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8667 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 {
8669 --curwin->w_cursor.col;
8670 (void)del_char(FALSE); /* delete the '^' or '0' */
8671 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8672 if (State & REPLACE_FLAG)
8673 replace_pop_ins();
8674 if (lastc == '^')
8675 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008676 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008677 }
8678 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008679 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680
8681 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8682 did_ai = FALSE;
8683#ifdef FEAT_SMARTINDENT
8684 did_si = FALSE;
8685 can_si = FALSE;
8686 can_si_back = FALSE;
8687#endif
8688#ifdef FEAT_CINDENT
8689 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8690#endif
8691}
8692
8693 static void
8694ins_del()
8695{
8696 int temp;
8697
8698 if (stop_arrow() == FAIL)
8699 return;
8700 if (gchar_cursor() == NUL) /* delete newline */
8701 {
8702 temp = curwin->w_cursor.col;
8703 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar81340392012-06-06 16:12:59 +02008704 || do_join(2, FALSE, TRUE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 vim_beep();
8706 else
8707 curwin->w_cursor.col = temp;
8708 }
8709 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8710 vim_beep();
8711 did_ai = FALSE;
8712#ifdef FEAT_SMARTINDENT
8713 did_si = FALSE;
8714 can_si = FALSE;
8715 can_si_back = FALSE;
8716#endif
8717 AppendCharToRedobuff(K_DEL);
8718}
8719
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008720static void ins_bs_one __ARGS((colnr_T *vcolp));
8721
8722/*
8723 * Delete one character for ins_bs().
8724 */
8725 static void
8726ins_bs_one(vcolp)
8727 colnr_T *vcolp;
8728{
8729 dec_cursor();
8730 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8731 if (State & REPLACE_FLAG)
8732 {
8733 /* Don't delete characters before the insert point when in
8734 * Replace mode */
8735 if (curwin->w_cursor.lnum != Insstart.lnum
8736 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008737 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008738 }
8739 else
8740 (void)del_char(FALSE);
8741}
8742
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743/*
8744 * Handle Backspace, delete-word and delete-line in Insert mode.
8745 * Return TRUE when backspace was actually used.
8746 */
8747 static int
8748ins_bs(c, mode, inserted_space_p)
8749 int c;
8750 int mode;
8751 int *inserted_space_p;
8752{
8753 linenr_T lnum;
8754 int cc;
8755 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008756 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 colnr_T mincol;
8758 int did_backspace = FALSE;
8759 int in_indent;
8760 int oldState;
8761#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008762 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763#endif
8764
8765 /*
8766 * can't delete anything in an empty file
8767 * can't backup past first character in buffer
8768 * can't backup past starting point unless 'backspace' > 1
8769 * can backup to a previous line if 'backspace' == 0
8770 */
8771 if ( bufempty()
8772 || (
8773#ifdef FEAT_RIGHTLEFT
8774 !revins_on &&
8775#endif
8776 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8777 || (!can_bs(BS_START)
8778 && (arrow_used
8779 || (curwin->w_cursor.lnum == Insstart.lnum
8780 && curwin->w_cursor.col <= Insstart.col)))
8781 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8782 && curwin->w_cursor.col <= ai_col)
8783 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8784 {
8785 vim_beep();
8786 return FALSE;
8787 }
8788
8789 if (stop_arrow() == FAIL)
8790 return FALSE;
8791 in_indent = inindent(0);
8792#ifdef FEAT_CINDENT
8793 if (in_indent)
8794 can_cindent = FALSE;
8795#endif
8796#ifdef FEAT_COMMENTS
8797 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8798#endif
8799#ifdef FEAT_RIGHTLEFT
8800 if (revins_on) /* put cursor after last inserted char */
8801 inc_cursor();
8802#endif
8803
8804#ifdef FEAT_VIRTUALEDIT
8805 /* Virtualedit:
8806 * BACKSPACE_CHAR eats a virtual space
8807 * BACKSPACE_WORD eats all coladd
8808 * BACKSPACE_LINE eats all coladd and keeps going
8809 */
8810 if (curwin->w_cursor.coladd > 0)
8811 {
8812 if (mode == BACKSPACE_CHAR)
8813 {
8814 --curwin->w_cursor.coladd;
8815 return TRUE;
8816 }
8817 if (mode == BACKSPACE_WORD)
8818 {
8819 curwin->w_cursor.coladd = 0;
8820 return TRUE;
8821 }
8822 curwin->w_cursor.coladd = 0;
8823 }
8824#endif
8825
8826 /*
8827 * delete newline!
8828 */
8829 if (curwin->w_cursor.col == 0)
8830 {
8831 lnum = Insstart.lnum;
8832 if (curwin->w_cursor.lnum == Insstart.lnum
8833#ifdef FEAT_RIGHTLEFT
8834 || revins_on
8835#endif
8836 )
8837 {
8838 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8839 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8840 return FALSE;
8841 --Insstart.lnum;
8842 Insstart.col = MAXCOL;
8843 }
8844 /*
8845 * In replace mode:
8846 * cc < 0: NL was inserted, delete it
8847 * cc >= 0: NL was replaced, put original characters back
8848 */
8849 cc = -1;
8850 if (State & REPLACE_FLAG)
8851 cc = replace_pop(); /* returns -1 if NL was inserted */
8852 /*
8853 * In replace mode, in the line we started replacing, we only move the
8854 * cursor.
8855 */
8856 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8857 {
8858 dec_cursor();
8859 }
8860 else
8861 {
8862#ifdef FEAT_VREPLACE
8863 if (!(State & VREPLACE_FLAG)
8864 || curwin->w_cursor.lnum > orig_line_count)
8865#endif
8866 {
8867 temp = gchar_cursor(); /* remember current char */
8868 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008869
8870 /* When "aw" is in 'formatoptions' we must delete the space at
8871 * the end of the line, otherwise the line will be broken
8872 * again when auto-formatting. */
8873 if (has_format_option(FO_AUTO)
8874 && has_format_option(FO_WHITE_PAR))
8875 {
8876 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8877 TRUE);
8878 int len;
8879
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008880 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008881 if (len > 0 && ptr[len - 1] == ' ')
8882 ptr[len - 1] = NUL;
8883 }
8884
Bram Moolenaar81340392012-06-06 16:12:59 +02008885 (void)do_join(2, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 if (temp == NUL && gchar_cursor() != NUL)
8887 inc_cursor();
8888 }
8889#ifdef FEAT_VREPLACE
8890 else
8891 dec_cursor();
8892#endif
8893
8894 /*
8895 * In REPLACE mode we have to put back the text that was replaced
8896 * by the NL. On the replace stack is first a NUL-terminated
8897 * sequence of characters that were deleted and then the
8898 * characters that NL replaced.
8899 */
8900 if (State & REPLACE_FLAG)
8901 {
8902 /*
8903 * Do the next ins_char() in NORMAL state, to
8904 * prevent ins_char() from replacing characters and
8905 * avoiding showmatch().
8906 */
8907 oldState = State;
8908 State = NORMAL;
8909 /*
8910 * restore characters (blanks) deleted after cursor
8911 */
8912 while (cc > 0)
8913 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008914 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915#ifdef FEAT_MBYTE
8916 mb_replace_pop_ins(cc);
8917#else
8918 ins_char(cc);
8919#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008920 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 cc = replace_pop();
8922 }
8923 /* restore the characters that NL replaced */
8924 replace_pop_ins();
8925 State = oldState;
8926 }
8927 }
8928 did_ai = FALSE;
8929 }
8930 else
8931 {
8932 /*
8933 * Delete character(s) before the cursor.
8934 */
8935#ifdef FEAT_RIGHTLEFT
8936 if (revins_on) /* put cursor on last inserted char */
8937 dec_cursor();
8938#endif
8939 mincol = 0;
8940 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008941 if (mode == BACKSPACE_LINE
8942 && (curbuf->b_p_ai
8943#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008944 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008945#endif
8946 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947#ifdef FEAT_RIGHTLEFT
8948 && !revins_on
8949#endif
8950 )
8951 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008952 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008954 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008956 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 }
8958
8959 /*
8960 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8961 */
8962 if ( mode == BACKSPACE_CHAR
8963 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008964 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008965 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966 && (*(ml_get_cursor() - 1) == TAB
8967 || (*(ml_get_cursor() - 1) == ' '
8968 && (!*inserted_space_p
8969 || arrow_used))))))
8970 {
8971 int ts;
8972 colnr_T vcol;
8973 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008974 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975
8976 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008977 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008978 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008979 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008980 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 /* Compute the virtual column where we want to be. Since
8982 * 'showbreak' may get in the way, need to get the last column of
8983 * the previous character. */
8984 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008985 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 dec_cursor();
8987 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8988 inc_cursor();
8989 want_vcol = (want_vcol / ts) * ts;
8990
8991 /* delete characters until we are at or before want_vcol */
8992 while (vcol > want_vcol
8993 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008994 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995
8996 /* insert extra spaces until we are at want_vcol */
8997 while (vcol < want_vcol)
8998 {
8999 /* Remember the first char we inserted */
9000 if (curwin->w_cursor.lnum == Insstart.lnum
9001 && curwin->w_cursor.col < Insstart.col)
9002 Insstart.col = curwin->w_cursor.col;
9003
9004#ifdef FEAT_VREPLACE
9005 if (State & VREPLACE_FLAG)
9006 ins_char(' ');
9007 else
9008#endif
9009 {
9010 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009011 if ((State & REPLACE_FLAG))
9012 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009013 }
9014 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9015 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009016
9017 /* If we are now back where we started delete one character. Can
9018 * happen when using 'sts' and 'linebreak'. */
9019 if (vcol >= start_vcol)
9020 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 }
9022
9023 /*
9024 * Delete upto starting point, start of line or previous word.
9025 */
9026 else do
9027 {
9028#ifdef FEAT_RIGHTLEFT
9029 if (!revins_on) /* put cursor on char to be deleted */
9030#endif
9031 dec_cursor();
9032
9033 /* start of word? */
9034 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
9035 {
9036 mode = BACKSPACE_WORD_NOT_SPACE;
9037 temp = vim_iswordc(gchar_cursor());
9038 }
9039 /* end of word? */
9040 else if (mode == BACKSPACE_WORD_NOT_SPACE
9041 && (vim_isspace(cc = gchar_cursor())
9042 || vim_iswordc(cc) != temp))
9043 {
9044#ifdef FEAT_RIGHTLEFT
9045 if (!revins_on)
9046#endif
9047 inc_cursor();
9048#ifdef FEAT_RIGHTLEFT
9049 else if (State & REPLACE_FLAG)
9050 dec_cursor();
9051#endif
9052 break;
9053 }
9054 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009055 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 else
9057 {
9058#ifdef FEAT_MBYTE
9059 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009060 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061#endif
9062 (void)del_char(FALSE);
9063#ifdef FEAT_MBYTE
9064 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009065 * If there are combining characters and 'delcombine' is set
9066 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067 * character.
9068 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009069 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009070 inc_cursor();
9071#endif
9072#ifdef FEAT_RIGHTLEFT
9073 if (revins_chars)
9074 {
9075 revins_chars--;
9076 revins_legal++;
9077 }
9078 if (revins_on && gchar_cursor() == NUL)
9079 break;
9080#endif
9081 }
9082 /* Just a single backspace?: */
9083 if (mode == BACKSPACE_CHAR)
9084 break;
9085 } while (
9086#ifdef FEAT_RIGHTLEFT
9087 revins_on ||
9088#endif
9089 (curwin->w_cursor.col > mincol
9090 && (curwin->w_cursor.lnum != Insstart.lnum
9091 || curwin->w_cursor.col != Insstart.col)));
9092 did_backspace = TRUE;
9093 }
9094#ifdef FEAT_SMARTINDENT
9095 did_si = FALSE;
9096 can_si = FALSE;
9097 can_si_back = FALSE;
9098#endif
9099 if (curwin->w_cursor.col <= 1)
9100 did_ai = FALSE;
9101 /*
9102 * It's a little strange to put backspaces into the redo
9103 * buffer, but it makes auto-indent a lot easier to deal
9104 * with.
9105 */
9106 AppendCharToRedobuff(c);
9107
9108 /* If deleted before the insertion point, adjust it */
9109 if (curwin->w_cursor.lnum == Insstart.lnum
9110 && curwin->w_cursor.col < Insstart.col)
9111 Insstart.col = curwin->w_cursor.col;
9112
9113 /* vi behaviour: the cursor moves backward but the character that
9114 * was there remains visible
9115 * Vim behaviour: the cursor moves backward and the character that
9116 * was there is erased from the screen.
9117 * We can emulate the vi behaviour by pretending there is a dollar
9118 * displayed even when there isn't.
9119 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009120 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009121 dollar_vcol = curwin->w_virtcol;
9122
Bram Moolenaarce3be472008-01-14 19:12:28 +00009123#ifdef FEAT_FOLDING
9124 /* When deleting a char the cursor line must never be in a closed fold.
9125 * E.g., when 'foldmethod' is indent and deleting the first non-white
9126 * char before a Tab. */
9127 if (did_backspace)
9128 foldOpenCursor();
9129#endif
9130
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 return did_backspace;
9132}
9133
9134#ifdef FEAT_MOUSE
9135 static void
9136ins_mouse(c)
9137 int c;
9138{
9139 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009140 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141
9142# ifdef FEAT_GUI
9143 /* When GUI is active, also move/paste when 'mouse' is empty */
9144 if (!gui.in_use)
9145# endif
9146 if (!mouse_has(MOUSE_INSERT))
9147 return;
9148
9149 undisplay_dollar();
9150 tpos = curwin->w_cursor;
9151 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9152 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009153#ifdef FEAT_WINDOWS
9154 win_T *new_curwin = curwin;
9155
9156 if (curwin != old_curwin && win_valid(old_curwin))
9157 {
9158 /* Mouse took us to another window. We need to go back to the
9159 * previous one to stop insert there properly. */
9160 curwin = old_curwin;
9161 curbuf = curwin->w_buffer;
9162 }
9163#endif
9164 start_arrow(curwin == old_curwin ? &tpos : NULL);
9165#ifdef FEAT_WINDOWS
9166 if (curwin != new_curwin && win_valid(new_curwin))
9167 {
9168 curwin = new_curwin;
9169 curbuf = curwin->w_buffer;
9170 }
9171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009172# ifdef FEAT_CINDENT
9173 can_cindent = TRUE;
9174# endif
9175 }
9176
9177#ifdef FEAT_WINDOWS
9178 /* redraw status lines (in case another window became active) */
9179 redraw_statuslines();
9180#endif
9181}
9182
9183 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009184ins_mousescroll(dir)
9185 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009186{
9187 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009188# if defined(FEAT_WINDOWS)
9189 win_T *old_curwin = curwin;
9190# endif
9191# ifdef FEAT_INS_EXPAND
9192 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193# endif
9194
9195 tpos = curwin->w_cursor;
9196
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009197# ifdef FEAT_WINDOWS
9198 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 {
9200 int row, col;
9201
9202 row = mouse_row;
9203 col = mouse_col;
9204
9205 /* find the window at the pointer coordinates */
9206 curwin = mouse_find_win(&row, &col);
9207 curbuf = curwin->w_buffer;
9208 }
9209 if (curwin == old_curwin)
9210# endif
9211 undisplay_dollar();
9212
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009213# ifdef FEAT_INS_EXPAND
9214 /* Don't scroll the window in which completion is being done. */
9215 if (!pum_visible()
9216# if defined(FEAT_WINDOWS)
9217 || curwin != old_curwin
9218# endif
9219 )
9220# endif
9221 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009222 if (dir == MSCR_DOWN || dir == MSCR_UP)
9223 {
9224 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9225 scroll_redraw(dir,
9226 (long)(curwin->w_botline - curwin->w_topline));
9227 else
9228 scroll_redraw(dir, 3L);
9229 }
9230#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009231 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009232 {
9233 int val, step = 6;
9234
9235 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9236 step = W_WIDTH(curwin);
9237 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9238 if (val < 0)
9239 val = 0;
9240 gui_do_horiz_scroll(val, TRUE);
9241 }
9242#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009243# ifdef FEAT_INS_EXPAND
9244 did_scroll = TRUE;
9245# endif
9246 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009247
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009248# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 curwin->w_redr_status = TRUE;
9250
9251 curwin = old_curwin;
9252 curbuf = curwin->w_buffer;
9253# endif
9254
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009255# ifdef FEAT_INS_EXPAND
9256 /* The popup menu may overlay the window, need to redraw it.
9257 * TODO: Would be more efficient to only redraw the windows that are
9258 * overlapped by the popup menu. */
9259 if (pum_visible() && did_scroll)
9260 {
9261 redraw_all_later(NOT_VALID);
9262 ins_compl_show_pum();
9263 }
9264# endif
9265
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 if (!equalpos(curwin->w_cursor, tpos))
9267 {
9268 start_arrow(&tpos);
9269# ifdef FEAT_CINDENT
9270 can_cindent = TRUE;
9271# endif
9272 }
9273}
9274#endif
9275
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009276#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009277 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009278ins_tabline(c)
9279 int c;
9280{
9281 /* We will be leaving the current window, unless closing another tab. */
9282 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9283 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9284 {
9285 undisplay_dollar();
9286 start_arrow(&curwin->w_cursor);
9287# ifdef FEAT_CINDENT
9288 can_cindent = TRUE;
9289# endif
9290 }
9291
9292 if (c == K_TABLINE)
9293 goto_tabpage(current_tab);
9294 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009295 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009296 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009297 redraw_statuslines(); /* will redraw the tabline when needed */
9298 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009299}
9300#endif
9301
9302#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303 void
9304ins_scroll()
9305{
9306 pos_T tpos;
9307
9308 undisplay_dollar();
9309 tpos = curwin->w_cursor;
9310 if (gui_do_scroll())
9311 {
9312 start_arrow(&tpos);
9313# ifdef FEAT_CINDENT
9314 can_cindent = TRUE;
9315# endif
9316 }
9317}
9318
9319 void
9320ins_horscroll()
9321{
9322 pos_T tpos;
9323
9324 undisplay_dollar();
9325 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009326 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009327 {
9328 start_arrow(&tpos);
9329# ifdef FEAT_CINDENT
9330 can_cindent = TRUE;
9331# endif
9332 }
9333}
9334#endif
9335
9336 static void
9337ins_left()
9338{
9339 pos_T tpos;
9340
9341#ifdef FEAT_FOLDING
9342 if ((fdo_flags & FDO_HOR) && KeyTyped)
9343 foldOpenCursor();
9344#endif
9345 undisplay_dollar();
9346 tpos = curwin->w_cursor;
9347 if (oneleft() == OK)
9348 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009349#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9350 /* Only call start_arrow() when not busy with preediting, it will
9351 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9352 if (!im_is_preediting())
9353#endif
9354 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009355#ifdef FEAT_RIGHTLEFT
9356 /* If exit reversed string, position is fixed */
9357 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9358 revins_legal++;
9359 revins_chars++;
9360#endif
9361 }
9362
9363 /*
9364 * if 'whichwrap' set for cursor in insert mode may go to
9365 * previous line
9366 */
9367 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9368 {
9369 start_arrow(&tpos);
9370 --(curwin->w_cursor.lnum);
9371 coladvance((colnr_T)MAXCOL);
9372 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9373 }
9374 else
9375 vim_beep();
9376}
9377
9378 static void
9379ins_home(c)
9380 int c;
9381{
9382 pos_T tpos;
9383
9384#ifdef FEAT_FOLDING
9385 if ((fdo_flags & FDO_HOR) && KeyTyped)
9386 foldOpenCursor();
9387#endif
9388 undisplay_dollar();
9389 tpos = curwin->w_cursor;
9390 if (c == K_C_HOME)
9391 curwin->w_cursor.lnum = 1;
9392 curwin->w_cursor.col = 0;
9393#ifdef FEAT_VIRTUALEDIT
9394 curwin->w_cursor.coladd = 0;
9395#endif
9396 curwin->w_curswant = 0;
9397 start_arrow(&tpos);
9398}
9399
9400 static void
9401ins_end(c)
9402 int c;
9403{
9404 pos_T tpos;
9405
9406#ifdef FEAT_FOLDING
9407 if ((fdo_flags & FDO_HOR) && KeyTyped)
9408 foldOpenCursor();
9409#endif
9410 undisplay_dollar();
9411 tpos = curwin->w_cursor;
9412 if (c == K_C_END)
9413 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9414 coladvance((colnr_T)MAXCOL);
9415 curwin->w_curswant = MAXCOL;
9416
9417 start_arrow(&tpos);
9418}
9419
9420 static void
9421ins_s_left()
9422{
9423#ifdef FEAT_FOLDING
9424 if ((fdo_flags & FDO_HOR) && KeyTyped)
9425 foldOpenCursor();
9426#endif
9427 undisplay_dollar();
9428 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9429 {
9430 start_arrow(&curwin->w_cursor);
9431 (void)bck_word(1L, FALSE, FALSE);
9432 curwin->w_set_curswant = TRUE;
9433 }
9434 else
9435 vim_beep();
9436}
9437
9438 static void
9439ins_right()
9440{
9441#ifdef FEAT_FOLDING
9442 if ((fdo_flags & FDO_HOR) && KeyTyped)
9443 foldOpenCursor();
9444#endif
9445 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009446 if (gchar_cursor() != NUL
9447#ifdef FEAT_VIRTUALEDIT
9448 || virtual_active()
9449#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009450 )
9451 {
9452 start_arrow(&curwin->w_cursor);
9453 curwin->w_set_curswant = TRUE;
9454#ifdef FEAT_VIRTUALEDIT
9455 if (virtual_active())
9456 oneright();
9457 else
9458#endif
9459 {
9460#ifdef FEAT_MBYTE
9461 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009462 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009463 else
9464#endif
9465 ++curwin->w_cursor.col;
9466 }
9467
9468#ifdef FEAT_RIGHTLEFT
9469 revins_legal++;
9470 if (revins_chars)
9471 revins_chars--;
9472#endif
9473 }
9474 /* if 'whichwrap' set for cursor in insert mode, may move the
9475 * cursor to the next line */
9476 else if (vim_strchr(p_ww, ']') != NULL
9477 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9478 {
9479 start_arrow(&curwin->w_cursor);
9480 curwin->w_set_curswant = TRUE;
9481 ++curwin->w_cursor.lnum;
9482 curwin->w_cursor.col = 0;
9483 }
9484 else
9485 vim_beep();
9486}
9487
9488 static void
9489ins_s_right()
9490{
9491#ifdef FEAT_FOLDING
9492 if ((fdo_flags & FDO_HOR) && KeyTyped)
9493 foldOpenCursor();
9494#endif
9495 undisplay_dollar();
9496 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9497 || gchar_cursor() != NUL)
9498 {
9499 start_arrow(&curwin->w_cursor);
9500 (void)fwd_word(1L, FALSE, 0);
9501 curwin->w_set_curswant = TRUE;
9502 }
9503 else
9504 vim_beep();
9505}
9506
9507 static void
9508ins_up(startcol)
9509 int startcol; /* when TRUE move to Insstart.col */
9510{
9511 pos_T tpos;
9512 linenr_T old_topline = curwin->w_topline;
9513#ifdef FEAT_DIFF
9514 int old_topfill = curwin->w_topfill;
9515#endif
9516
9517 undisplay_dollar();
9518 tpos = curwin->w_cursor;
9519 if (cursor_up(1L, TRUE) == OK)
9520 {
9521 if (startcol)
9522 coladvance(getvcol_nolist(&Insstart));
9523 if (old_topline != curwin->w_topline
9524#ifdef FEAT_DIFF
9525 || old_topfill != curwin->w_topfill
9526#endif
9527 )
9528 redraw_later(VALID);
9529 start_arrow(&tpos);
9530#ifdef FEAT_CINDENT
9531 can_cindent = TRUE;
9532#endif
9533 }
9534 else
9535 vim_beep();
9536}
9537
9538 static void
9539ins_pageup()
9540{
9541 pos_T tpos;
9542
9543 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009544
9545#ifdef FEAT_WINDOWS
9546 if (mod_mask & MOD_MASK_CTRL)
9547 {
9548 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009549 if (first_tabpage->tp_next != NULL)
9550 {
9551 start_arrow(&curwin->w_cursor);
9552 goto_tabpage(-1);
9553 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009554 return;
9555 }
9556#endif
9557
Bram Moolenaar071d4272004-06-13 20:20:40 +00009558 tpos = curwin->w_cursor;
9559 if (onepage(BACKWARD, 1L) == OK)
9560 {
9561 start_arrow(&tpos);
9562#ifdef FEAT_CINDENT
9563 can_cindent = TRUE;
9564#endif
9565 }
9566 else
9567 vim_beep();
9568}
9569
9570 static void
9571ins_down(startcol)
9572 int startcol; /* when TRUE move to Insstart.col */
9573{
9574 pos_T tpos;
9575 linenr_T old_topline = curwin->w_topline;
9576#ifdef FEAT_DIFF
9577 int old_topfill = curwin->w_topfill;
9578#endif
9579
9580 undisplay_dollar();
9581 tpos = curwin->w_cursor;
9582 if (cursor_down(1L, TRUE) == OK)
9583 {
9584 if (startcol)
9585 coladvance(getvcol_nolist(&Insstart));
9586 if (old_topline != curwin->w_topline
9587#ifdef FEAT_DIFF
9588 || old_topfill != curwin->w_topfill
9589#endif
9590 )
9591 redraw_later(VALID);
9592 start_arrow(&tpos);
9593#ifdef FEAT_CINDENT
9594 can_cindent = TRUE;
9595#endif
9596 }
9597 else
9598 vim_beep();
9599}
9600
9601 static void
9602ins_pagedown()
9603{
9604 pos_T tpos;
9605
9606 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009607
9608#ifdef FEAT_WINDOWS
9609 if (mod_mask & MOD_MASK_CTRL)
9610 {
9611 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009612 if (first_tabpage->tp_next != NULL)
9613 {
9614 start_arrow(&curwin->w_cursor);
9615 goto_tabpage(0);
9616 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009617 return;
9618 }
9619#endif
9620
Bram Moolenaar071d4272004-06-13 20:20:40 +00009621 tpos = curwin->w_cursor;
9622 if (onepage(FORWARD, 1L) == OK)
9623 {
9624 start_arrow(&tpos);
9625#ifdef FEAT_CINDENT
9626 can_cindent = TRUE;
9627#endif
9628 }
9629 else
9630 vim_beep();
9631}
9632
9633#ifdef FEAT_DND
9634 static void
9635ins_drop()
9636{
9637 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9638}
9639#endif
9640
9641/*
9642 * Handle TAB in Insert or Replace mode.
9643 * Return TRUE when the TAB needs to be inserted like a normal character.
9644 */
9645 static int
9646ins_tab()
9647{
9648 int ind;
9649 int i;
9650 int temp;
9651
9652 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9653 Insstart_blank_vcol = get_nolist_virtcol();
9654 if (echeck_abbr(TAB + ABBR_OFF))
9655 return FALSE;
9656
9657 ind = inindent(0);
9658#ifdef FEAT_CINDENT
9659 if (ind)
9660 can_cindent = FALSE;
9661#endif
9662
9663 /*
9664 * When nothing special, insert TAB like a normal character
9665 */
9666 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009667 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009668 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 return TRUE;
9670
9671 if (stop_arrow() == FAIL)
9672 return TRUE;
9673
9674 did_ai = FALSE;
9675#ifdef FEAT_SMARTINDENT
9676 did_si = FALSE;
9677 can_si = FALSE;
9678 can_si_back = FALSE;
9679#endif
9680 AppendToRedobuff((char_u *)"\t");
9681
9682 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009683 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009684 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9685 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686 else /* otherwise use 'tabstop' */
9687 temp = (int)curbuf->b_p_ts;
9688 temp -= get_nolist_virtcol() % temp;
9689
9690 /*
9691 * Insert the first space with ins_char(). It will delete one char in
9692 * replace mode. Insert the rest with ins_str(); it will not delete any
9693 * chars. For VREPLACE mode, we use ins_char() for all characters.
9694 */
9695 ins_char(' ');
9696 while (--temp > 0)
9697 {
9698#ifdef FEAT_VREPLACE
9699 if (State & VREPLACE_FLAG)
9700 ins_char(' ');
9701 else
9702#endif
9703 {
9704 ins_str((char_u *)" ");
9705 if (State & REPLACE_FLAG) /* no char replaced */
9706 replace_push(NUL);
9707 }
9708 }
9709
9710 /*
9711 * When 'expandtab' not set: Replace spaces by TABs where possible.
9712 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009713 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 {
9715 char_u *ptr;
9716#ifdef FEAT_VREPLACE
9717 char_u *saved_line = NULL; /* init for GCC */
9718 pos_T pos;
9719#endif
9720 pos_T fpos;
9721 pos_T *cursor;
9722 colnr_T want_vcol, vcol;
9723 int change_col = -1;
9724 int save_list = curwin->w_p_list;
9725
9726 /*
9727 * Get the current line. For VREPLACE mode, don't make real changes
9728 * yet, just work on a copy of the line.
9729 */
9730#ifdef FEAT_VREPLACE
9731 if (State & VREPLACE_FLAG)
9732 {
9733 pos = curwin->w_cursor;
9734 cursor = &pos;
9735 saved_line = vim_strsave(ml_get_curline());
9736 if (saved_line == NULL)
9737 return FALSE;
9738 ptr = saved_line + pos.col;
9739 }
9740 else
9741#endif
9742 {
9743 ptr = ml_get_cursor();
9744 cursor = &curwin->w_cursor;
9745 }
9746
9747 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9748 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9749 curwin->w_p_list = FALSE;
9750
9751 /* Find first white before the cursor */
9752 fpos = curwin->w_cursor;
9753 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9754 {
9755 --fpos.col;
9756 --ptr;
9757 }
9758
9759 /* In Replace mode, don't change characters before the insert point. */
9760 if ((State & REPLACE_FLAG)
9761 && fpos.lnum == Insstart.lnum
9762 && fpos.col < Insstart.col)
9763 {
9764 ptr += Insstart.col - fpos.col;
9765 fpos.col = Insstart.col;
9766 }
9767
9768 /* compute virtual column numbers of first white and cursor */
9769 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9770 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9771
9772 /* Use as many TABs as possible. Beware of 'showbreak' and
9773 * 'linebreak' adding extra virtual columns. */
9774 while (vim_iswhite(*ptr))
9775 {
9776 i = lbr_chartabsize((char_u *)"\t", vcol);
9777 if (vcol + i > want_vcol)
9778 break;
9779 if (*ptr != TAB)
9780 {
9781 *ptr = TAB;
9782 if (change_col < 0)
9783 {
9784 change_col = fpos.col; /* Column of first change */
9785 /* May have to adjust Insstart */
9786 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9787 Insstart.col = fpos.col;
9788 }
9789 }
9790 ++fpos.col;
9791 ++ptr;
9792 vcol += i;
9793 }
9794
9795 if (change_col >= 0)
9796 {
9797 int repl_off = 0;
9798
9799 /* Skip over the spaces we need. */
9800 while (vcol < want_vcol && *ptr == ' ')
9801 {
9802 vcol += lbr_chartabsize(ptr, vcol);
9803 ++ptr;
9804 ++repl_off;
9805 }
9806 if (vcol > want_vcol)
9807 {
9808 /* Must have a char with 'showbreak' just before it. */
9809 --ptr;
9810 --repl_off;
9811 }
9812 fpos.col += repl_off;
9813
9814 /* Delete following spaces. */
9815 i = cursor->col - fpos.col;
9816 if (i > 0)
9817 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009818 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819 /* correct replace stack. */
9820 if ((State & REPLACE_FLAG)
9821#ifdef FEAT_VREPLACE
9822 && !(State & VREPLACE_FLAG)
9823#endif
9824 )
9825 for (temp = i; --temp >= 0; )
9826 replace_join(repl_off);
9827 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009828#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009829 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009830 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009831 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009832 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9833 (char_u *)"\t", 1);
9834 }
9835#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009836 cursor->col -= i;
9837
9838#ifdef FEAT_VREPLACE
9839 /*
9840 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9841 * backspacing over the changed spacing and then inserting the new
9842 * spacing.
9843 */
9844 if (State & VREPLACE_FLAG)
9845 {
9846 /* Backspace from real cursor to change_col */
9847 backspace_until_column(change_col);
9848
9849 /* Insert each char in saved_line from changed_col to
9850 * ptr-cursor */
9851 ins_bytes_len(saved_line + change_col,
9852 cursor->col - change_col);
9853 }
9854#endif
9855 }
9856
9857#ifdef FEAT_VREPLACE
9858 if (State & VREPLACE_FLAG)
9859 vim_free(saved_line);
9860#endif
9861 curwin->w_p_list = save_list;
9862 }
9863
9864 return FALSE;
9865}
9866
9867/*
9868 * Handle CR or NL in insert mode.
9869 * Return TRUE when out of memory or can't undo.
9870 */
9871 static int
9872ins_eol(c)
9873 int c;
9874{
9875 int i;
9876
9877 if (echeck_abbr(c + ABBR_OFF))
9878 return FALSE;
9879 if (stop_arrow() == FAIL)
9880 return TRUE;
9881 undisplay_dollar();
9882
9883 /*
9884 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9885 * character under the cursor. Only push a NUL on the replace stack,
9886 * nothing to put back when the NL is deleted.
9887 */
9888 if ((State & REPLACE_FLAG)
9889#ifdef FEAT_VREPLACE
9890 && !(State & VREPLACE_FLAG)
9891#endif
9892 )
9893 replace_push(NUL);
9894
9895 /*
9896 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9897 * replacing the next line, so we push all of the characters left on the
9898 * line onto the replace stack. This is not done here though, it is done
9899 * in open_line().
9900 */
9901
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009902#ifdef FEAT_VIRTUALEDIT
9903 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9904 * CTRL-O). */
9905 if (virtual_active() && curwin->w_cursor.coladd > 0)
9906 coladvance(getviscol());
9907#endif
9908
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909#ifdef FEAT_RIGHTLEFT
9910# ifdef FEAT_FKMAP
9911 if (p_altkeymap && p_fkmap)
9912 fkmap(NL);
9913# endif
9914 /* NL in reverse insert will always start in the end of
9915 * current line. */
9916 if (revins_on)
9917 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9918#endif
9919
9920 AppendToRedobuff(NL_STR);
9921 i = open_line(FORWARD,
9922#ifdef FEAT_COMMENTS
9923 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9924#endif
9925 0, old_indent);
9926 old_indent = 0;
9927#ifdef FEAT_CINDENT
9928 can_cindent = TRUE;
9929#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009930#ifdef FEAT_FOLDING
9931 /* When inserting a line the cursor line must never be in a closed fold. */
9932 foldOpenCursor();
9933#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009934
9935 return (!i);
9936}
9937
9938#ifdef FEAT_DIGRAPHS
9939/*
9940 * Handle digraph in insert mode.
9941 * Returns character still to be inserted, or NUL when nothing remaining to be
9942 * done.
9943 */
9944 static int
9945ins_digraph()
9946{
9947 int c;
9948 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009949 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950
9951 pc_status = PC_STATUS_UNSET;
9952 if (redrawing() && !char_avail())
9953 {
9954 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009955 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009956
9957 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009958 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959#ifdef FEAT_CMDL_INFO
9960 add_to_showcmd_c(Ctrl_K);
9961#endif
9962 }
9963
9964#ifdef USE_ON_FLY_SCROLL
9965 dont_scroll = TRUE; /* disallow scrolling here */
9966#endif
9967
9968 /* don't map the digraph chars. This also prevents the
9969 * mode message to be deleted when ESC is hit */
9970 ++no_mapping;
9971 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009972 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973 --no_mapping;
9974 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009975 if (did_putchar)
9976 /* when the line fits in 'columns' the '?' is at the start of the next
9977 * line and will not be removed by the redraw */
9978 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009979
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 if (IS_SPECIAL(c) || mod_mask) /* special key */
9981 {
9982#ifdef FEAT_CMDL_INFO
9983 clear_showcmd();
9984#endif
9985 insert_special(c, TRUE, FALSE);
9986 return NUL;
9987 }
9988 if (c != ESC)
9989 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009990 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009991 if (redrawing() && !char_avail())
9992 {
9993 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009994 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009995
9996 if (char2cells(c) == 1)
9997 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009998 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009999 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010000 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010001 }
10002#ifdef FEAT_CMDL_INFO
10003 add_to_showcmd_c(c);
10004#endif
10005 }
10006 ++no_mapping;
10007 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +000010008 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010009 --no_mapping;
10010 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010011 if (did_putchar)
10012 /* when the line fits in 'columns' the '?' is at the start of the
10013 * next line and will not be removed by a redraw */
10014 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010015 if (cc != ESC)
10016 {
10017 AppendToRedobuff((char_u *)CTRL_V_STR);
10018 c = getdigraph(c, cc, TRUE);
10019#ifdef FEAT_CMDL_INFO
10020 clear_showcmd();
10021#endif
10022 return c;
10023 }
10024 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010025#ifdef FEAT_CMDL_INFO
10026 clear_showcmd();
10027#endif
10028 return NUL;
10029}
10030#endif
10031
10032/*
10033 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10034 * Returns the char to be inserted, or NUL if none found.
10035 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010036 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000010037ins_copychar(lnum)
10038 linenr_T lnum;
10039{
10040 int c;
10041 int temp;
10042 char_u *ptr, *prev_ptr;
10043
10044 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10045 {
10046 vim_beep();
10047 return NUL;
10048 }
10049
10050 /* try to advance to the cursor column */
10051 temp = 0;
10052 ptr = ml_get(lnum);
10053 prev_ptr = ptr;
10054 validate_virtcol();
10055 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10056 {
10057 prev_ptr = ptr;
10058 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
10059 }
10060 if ((colnr_T)temp > curwin->w_virtcol)
10061 ptr = prev_ptr;
10062
10063#ifdef FEAT_MBYTE
10064 c = (*mb_ptr2char)(ptr);
10065#else
10066 c = *ptr;
10067#endif
10068 if (c == NUL)
10069 vim_beep();
10070 return c;
10071}
10072
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010073/*
10074 * CTRL-Y or CTRL-E typed in Insert mode.
10075 */
10076 static int
10077ins_ctrl_ey(tc)
10078 int tc;
10079{
10080 int c = tc;
10081
10082#ifdef FEAT_INS_EXPAND
10083 if (ctrl_x_mode == CTRL_X_SCROLL)
10084 {
10085 if (c == Ctrl_Y)
10086 scrolldown_clamp();
10087 else
10088 scrollup_clamp();
10089 redraw_later(VALID);
10090 }
10091 else
10092#endif
10093 {
10094 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10095 if (c != NUL)
10096 {
10097 long tw_save;
10098
10099 /* The character must be taken literally, insert like it
10100 * was typed after a CTRL-V, and pretend 'textwidth'
10101 * wasn't set. Digits, 'o' and 'x' are special after a
10102 * CTRL-V, don't use it for these. */
10103 if (c < 256 && !isalnum(c))
10104 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10105 tw_save = curbuf->b_p_tw;
10106 curbuf->b_p_tw = -1;
10107 insert_special(c, TRUE, FALSE);
10108 curbuf->b_p_tw = tw_save;
10109#ifdef FEAT_RIGHTLEFT
10110 revins_chars++;
10111 revins_legal++;
10112#endif
10113 c = Ctrl_V; /* pretend CTRL-V is last character */
10114 auto_format(FALSE, TRUE);
10115 }
10116 }
10117 return c;
10118}
10119
Bram Moolenaar071d4272004-06-13 20:20:40 +000010120#ifdef FEAT_SMARTINDENT
10121/*
10122 * Try to do some very smart auto-indenting.
10123 * Used when inserting a "normal" character.
10124 */
10125 static void
10126ins_try_si(c)
10127 int c;
10128{
10129 pos_T *pos, old_pos;
10130 char_u *ptr;
10131 int i;
10132 int temp;
10133
10134 /*
10135 * do some very smart indenting when entering '{' or '}'
10136 */
10137 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10138 {
10139 /*
10140 * for '}' set indent equal to indent of line containing matching '{'
10141 */
10142 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10143 {
10144 old_pos = curwin->w_cursor;
10145 /*
10146 * If the matching '{' has a ')' immediately before it (ignoring
10147 * white-space), then line up with the start of the line
10148 * containing the matching '(' if there is one. This handles the
10149 * case where an "if (..\n..) {" statement continues over multiple
10150 * lines -- webb
10151 */
10152 ptr = ml_get(pos->lnum);
10153 i = pos->col;
10154 if (i > 0) /* skip blanks before '{' */
10155 while (--i > 0 && vim_iswhite(ptr[i]))
10156 ;
10157 curwin->w_cursor.lnum = pos->lnum;
10158 curwin->w_cursor.col = i;
10159 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10160 curwin->w_cursor = *pos;
10161 i = get_indent();
10162 curwin->w_cursor = old_pos;
10163#ifdef FEAT_VREPLACE
10164 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010165 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010166 else
10167#endif
10168 (void)set_indent(i, SIN_CHANGED);
10169 }
10170 else if (curwin->w_cursor.col > 0)
10171 {
10172 /*
10173 * when inserting '{' after "O" reduce indent, but not
10174 * more than indent of previous line
10175 */
10176 temp = TRUE;
10177 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10178 {
10179 old_pos = curwin->w_cursor;
10180 i = get_indent();
10181 while (curwin->w_cursor.lnum > 1)
10182 {
10183 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10184
10185 /* ignore empty lines and lines starting with '#'. */
10186 if (*ptr != '#' && *ptr != NUL)
10187 break;
10188 }
10189 if (get_indent() >= i)
10190 temp = FALSE;
10191 curwin->w_cursor = old_pos;
10192 }
10193 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010194 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195 }
10196 }
10197
10198 /*
10199 * set indent of '#' always to 0
10200 */
10201 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10202 {
10203 /* remember current indent for next line */
10204 old_indent = get_indent();
10205 (void)set_indent(0, SIN_CHANGED);
10206 }
10207
10208 /* Adjust ai_col, the char at this position can be deleted. */
10209 if (ai_col > curwin->w_cursor.col)
10210 ai_col = curwin->w_cursor.col;
10211}
10212#endif
10213
10214/*
10215 * Get the value that w_virtcol would have when 'list' is off.
10216 * Unless 'cpo' contains the 'L' flag.
10217 */
10218 static colnr_T
10219get_nolist_virtcol()
10220{
10221 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10222 return getvcol_nolist(&curwin->w_cursor);
10223 validate_virtcol();
10224 return curwin->w_virtcol;
10225}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010226
10227#ifdef FEAT_AUTOCMD
10228/*
10229 * Handle the InsertCharPre autocommand.
10230 * "c" is the character that was typed.
10231 * Return a pointer to allocated memory with the replacement string.
10232 * Return NULL to continue inserting "c".
10233 */
10234 static char_u *
10235do_insert_char_pre(c)
10236 int c;
10237{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010238 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010239 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010240
10241 /* Return quickly when there is nothing to do. */
10242 if (!has_insertcharpre())
10243 return NULL;
10244
Bram Moolenaar704984a2012-06-01 14:57:51 +020010245#ifdef FEAT_MBYTE
10246 if (has_mbyte)
10247 buf[(*mb_char2bytes)(c, buf)] = NUL;
10248 else
10249#endif
10250 {
10251 buf[0] = c;
10252 buf[1] = NUL;
10253 }
10254
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010255 /* Lock the text to avoid weird things from happening. */
10256 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010257 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010258
Bram Moolenaar704984a2012-06-01 14:57:51 +020010259 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010260 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010261 {
10262 /* Get the value of v:char. It may be empty or more than one
10263 * character. Only use it when changed, otherwise continue with the
10264 * original character to avoid breaking autoindent. */
10265 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10266 res = vim_strsave(get_vim_var_str(VV_CHAR));
10267 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010268
10269 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10270 --textlock;
10271
10272 return res;
10273}
10274#endif