blob: 29d8d3e65d0dcb0a00aae90a02df7c9242aeac27 [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 */
267
268static char_u *last_insert = NULL; /* the text of the previous insert,
269 K_SPECIAL and CSI are escaped */
270static int last_insert_skip; /* nr of chars in front of previous insert */
271static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000272static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274#ifdef FEAT_CINDENT
275static int can_cindent; /* may do cindenting on this line */
276#endif
277
278static int old_indent = 0; /* for ^^D command in insert mode */
279
280#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000281static int revins_on; /* reverse insert mode on */
282static int revins_chars; /* how much to skip after edit */
283static int revins_legal; /* was the last char 'legal'? */
284static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285#endif
286
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287static int ins_need_undo; /* call u_save() before inserting a
288 char. Set when edit() is called.
289 after that arrow_used is used. */
290
291static int did_add_space = FALSE; /* auto_format() added an extra space
292 under the cursor */
293
294/*
295 * edit(): Start inserting text.
296 *
297 * "cmdchar" can be:
298 * 'i' normal insert command
299 * 'a' normal append command
300 * 'R' replace command
301 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
302 * but still only one <CR> is inserted. The <Esc> is not used for redo.
303 * 'g' "gI" command.
304 * 'V' "gR" command for Virtual Replace mode.
305 * 'v' "gr" command for single character Virtual Replace mode.
306 *
307 * This function is not called recursively. For CTRL-O commands, it returns
308 * and lets the caller handle the Normal-mode command.
309 *
310 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
311 */
312 int
313edit(cmdchar, startln, count)
314 int cmdchar;
315 int startln; /* if set, insert at start of line */
316 long count;
317{
318 int c = 0;
319 char_u *ptr;
320 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000321 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 int i;
324 int did_backspace = TRUE; /* previous char was backspace */
325#ifdef FEAT_CINDENT
326 int line_is_white = FALSE; /* line is empty before insert */
327#endif
328 linenr_T old_topline = 0; /* topline before insertion */
329#ifdef FEAT_DIFF
330 int old_topfill = -1;
331#endif
332 int inserted_space = FALSE; /* just inserted a space */
333 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000334 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000336 /* Remember whether editing was restarted after CTRL-O. */
337 did_restart_edit = restart_edit;
338
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 /* sleep before redrawing, needed for "CTRL-O :" that results in an
340 * error message */
341 check_for_delay(TRUE);
342
343#ifdef HAVE_SANDBOX
344 /* Don't allow inserting in the sandbox. */
345 if (sandbox != 0)
346 {
347 EMSG(_(e_sandbox));
348 return FALSE;
349 }
350#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000351 /* Don't allow changes in the buffer while editing the cmdline. The
352 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000353 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000354 {
355 EMSG(_(e_secure));
356 return FALSE;
357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000360 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000361 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000362 {
363 EMSG(_(e_secure));
364 return FALSE;
365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 ins_compl_clear(); /* clear stuff for CTRL-X mode */
367#endif
368
Bram Moolenaar843ee412004-06-30 16:16:41 +0000369#ifdef FEAT_AUTOCMD
370 /*
371 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
372 */
373 if (cmdchar != 'r' && cmdchar != 'v')
374 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100375 pos_T save_cursor = curwin->w_cursor;
376
Bram Moolenaar1e015462005-09-25 22:16:38 +0000377# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000378 if (cmdchar == 'R')
379 ptr = (char_u *)"r";
380 else if (cmdchar == 'V')
381 ptr = (char_u *)"v";
382 else
383 ptr = (char_u *)"i";
384 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200385 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000386# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000387 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100388
Bram Moolenaar097c9922013-05-19 21:15:15 +0200389 /* Make sure the cursor didn't move. Do call check_cursor_col() in
390 * case the text was modified. Since Insert mode was not started yet
391 * a call to check_cursor_col() may move the cursor, especially with
392 * the "A" command, thus set State to avoid that. Also check that the
393 * line number is still valid (lines may have been deleted).
394 * Do not restore if v:char was set to a non-empty string. */
395 if (!equalpos(curwin->w_cursor, save_cursor)
396# ifdef FEAT_EVAL
397 && *get_vim_var_str(VV_CHAR) == NUL
398# endif
399 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100400 {
401 int save_state = State;
402
403 curwin->w_cursor = save_cursor;
404 State = INSERT;
405 check_cursor_col();
406 State = save_state;
407 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000408 }
409#endif
410
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200411#ifdef FEAT_CONCEAL
412 /* Check if the cursor line needs redrawing before changing State. If
413 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200414 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200415#endif
416
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#ifdef FEAT_MOUSE
418 /*
419 * When doing a paste with the middle mouse button, Insstart is set to
420 * where the paste started.
421 */
422 if (where_paste_started.lnum != 0)
423 Insstart = where_paste_started;
424 else
425#endif
426 {
427 Insstart = curwin->w_cursor;
428 if (startln)
429 Insstart.col = 0;
430 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000431 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 Insstart_blank_vcol = MAXCOL;
433 if (!did_ai)
434 ai_col = 0;
435
436 if (cmdchar != NUL && restart_edit == 0)
437 {
438 ResetRedobuff();
439 AppendNumberToRedobuff(count);
440#ifdef FEAT_VREPLACE
441 if (cmdchar == 'V' || cmdchar == 'v')
442 {
443 /* "gR" or "gr" command */
444 AppendCharToRedobuff('g');
445 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
446 }
447 else
448#endif
449 {
450 AppendCharToRedobuff(cmdchar);
451 if (cmdchar == 'g') /* "gI" command */
452 AppendCharToRedobuff('I');
453 else if (cmdchar == 'r') /* "r<CR>" command */
454 count = 1; /* insert only one <CR> */
455 }
456 }
457
458 if (cmdchar == 'R')
459 {
460#ifdef FEAT_FKMAP
461 if (p_fkmap && p_ri)
462 {
463 beep_flush();
464 EMSG(farsi_text_3); /* encoded in Farsi */
465 State = INSERT;
466 }
467 else
468#endif
469 State = REPLACE;
470 }
471#ifdef FEAT_VREPLACE
472 else if (cmdchar == 'V' || cmdchar == 'v')
473 {
474 State = VREPLACE;
475 replaceState = VREPLACE;
476 orig_line_count = curbuf->b_ml.ml_line_count;
477 vr_lines_changed = 1;
478 }
479#endif
480 else
481 State = INSERT;
482
483 stop_insert_mode = FALSE;
484
485 /*
486 * Need to recompute the cursor position, it might move when the cursor is
487 * on a TAB or special character.
488 */
489 curs_columns(TRUE);
490
491 /*
492 * Enable langmap or IME, indicated by 'iminsert'.
493 * Note that IME may enabled/disabled without us noticing here, thus the
494 * 'iminsert' value may not reflect what is actually used. It is updated
495 * when hitting <Esc>.
496 */
497 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
498 State |= LANGMAP;
499#ifdef USE_IM_CONTROL
500 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
501#endif
502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503#ifdef FEAT_MOUSE
504 setmouse();
505#endif
506#ifdef FEAT_CMDL_INFO
507 clear_showcmd();
508#endif
509#ifdef FEAT_RIGHTLEFT
510 /* there is no reverse replace mode */
511 revins_on = (State == INSERT && p_ri);
512 if (revins_on)
513 undisplay_dollar();
514 revins_chars = 0;
515 revins_legal = 0;
516 revins_scol = -1;
517#endif
518
519 /*
520 * Handle restarting Insert mode.
521 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
522 * restart_edit non-zero, and something in the stuff buffer.
523 */
524 if (restart_edit != 0 && stuff_empty())
525 {
526#ifdef FEAT_MOUSE
527 /*
528 * After a paste we consider text typed to be part of the insert for
529 * the pasted text. You can backspace over the pasted text too.
530 */
531 if (where_paste_started.lnum)
532 arrow_used = FALSE;
533 else
534#endif
535 arrow_used = TRUE;
536 restart_edit = 0;
537
538 /*
539 * If the cursor was after the end-of-line before the CTRL-O and it is
540 * now at the end-of-line, put it after the end-of-line (this is not
541 * correct in very rare cases).
542 * Also do this if curswant is greater than the current virtual
543 * column. Eg after "^O$" or "^O80|".
544 */
545 validate_virtcol();
546 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000547 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 || curwin->w_curswant > curwin->w_virtcol)
549 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
550 {
551 if (ptr[1] == NUL)
552 ++curwin->w_cursor.col;
553#ifdef FEAT_MBYTE
554 else if (has_mbyte)
555 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000556 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (ptr[i] == NUL)
558 curwin->w_cursor.col += i;
559 }
560#endif
561 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000562 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
564 else
565 arrow_used = FALSE;
566
567 /* we are in insert mode now, don't need to start it anymore */
568 need_start_insertmode = FALSE;
569
570 /* Need to save the line for undo before inserting the first char. */
571 ins_need_undo = TRUE;
572
573#ifdef FEAT_MOUSE
574 where_paste_started.lnum = 0;
575#endif
576#ifdef FEAT_CINDENT
577 can_cindent = TRUE;
578#endif
579#ifdef FEAT_FOLDING
580 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
581 * restarting. */
582 if (!p_im && did_restart_edit == 0)
583 foldOpenCursor();
584#endif
585
586 /*
587 * If 'showmode' is set, show the current (insert/replace/..) mode.
588 * A warning message for changing a readonly file is given here, before
589 * actually changing anything. It's put after the mode, if any.
590 */
591 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000592 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 i = showmode();
594
595 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000596 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
598#ifdef CURSOR_SHAPE
599 ui_cursor_shape(); /* may show different cursor shape */
600#endif
601#ifdef FEAT_DIGRAPHS
602 do_digraph(-1); /* clear digraphs */
603#endif
604
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000605 /*
606 * Get the current length of the redo buffer, those characters have to be
607 * skipped if we want to get to the inserted characters.
608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 ptr = get_inserted();
610 if (ptr == NULL)
611 new_insert_skip = 0;
612 else
613 {
614 new_insert_skip = (int)STRLEN(ptr);
615 vim_free(ptr);
616 }
617
618 old_indent = 0;
619
620 /*
621 * Main loop in Insert mode: repeat until Insert mode is left.
622 */
623 for (;;)
624 {
625#ifdef FEAT_RIGHTLEFT
626 if (!revins_legal)
627 revins_scol = -1; /* reset on illegal motions */
628 else
629 revins_legal = 0;
630#endif
631 if (arrow_used) /* don't repeat insert when arrow key used */
632 count = 0;
633
634 if (stop_insert_mode)
635 {
636 /* ":stopinsert" used or 'insertmode' reset */
637 count = 0;
638 goto doESCkey;
639 }
640
641 /* set curwin->w_curswant for next K_DOWN or K_UP */
642 if (!arrow_used)
643 curwin->w_set_curswant = TRUE;
644
645 /* If there is no typeahead may check for timestamps (e.g., for when a
646 * menu invoked a shell command). */
647 if (stuff_empty())
648 {
649 did_check_timestamps = FALSE;
650 if (need_check_timestamps)
651 check_timestamps(FALSE);
652 }
653
654 /*
655 * When emsg() was called msg_scroll will have been set.
656 */
657 msg_scroll = FALSE;
658
659#ifdef FEAT_GUI
660 /* When 'mousefocus' is set a mouse movement may have taken us to
661 * another window. "need_mouse_correct" may then be set because of an
662 * autocommand. */
663 if (need_mouse_correct)
664 gui_mouse_correct();
665#endif
666
667#ifdef FEAT_FOLDING
668 /* Open fold at the cursor line, according to 'foldopen'. */
669 if (fdo_flags & FDO_INSERT)
670 foldOpenCursor();
671 /* Close folds where the cursor isn't, according to 'foldclose' */
672 if (!char_avail())
673 foldCheckClose();
674#endif
675
676 /*
677 * If we inserted a character at the last position of the last line in
678 * the window, scroll the window one line up. This avoids an extra
679 * redraw.
680 * This is detected when the cursor column is smaller after inserting
681 * something.
682 * Don't do this when the topline changed already, it has
683 * already been adjusted (by insertchar() calling open_line())).
684 */
685 if (curbuf->b_mod_set
686 && curwin->w_p_wrap
687 && !did_backspace
688 && curwin->w_topline == old_topline
689#ifdef FEAT_DIFF
690 && curwin->w_topfill == old_topfill
691#endif
692 )
693 {
694 mincol = curwin->w_wcol;
695 validate_cursor_col();
696
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000697 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 && curwin->w_wrow == W_WINROW(curwin)
699 + curwin->w_height - 1 - p_so
700 && (curwin->w_cursor.lnum != curwin->w_topline
701#ifdef FEAT_DIFF
702 || curwin->w_topfill > 0
703#endif
704 ))
705 {
706#ifdef FEAT_DIFF
707 if (curwin->w_topfill > 0)
708 --curwin->w_topfill;
709 else
710#endif
711#ifdef FEAT_FOLDING
712 if (hasFolding(curwin->w_topline, NULL, &old_topline))
713 set_topline(curwin, old_topline + 1);
714 else
715#endif
716 set_topline(curwin, curwin->w_topline + 1);
717 }
718 }
719
720 /* May need to adjust w_topline to show the cursor. */
721 update_topline();
722
723 did_backspace = FALSE;
724
725 validate_cursor(); /* may set must_redraw */
726
727 /*
728 * Redraw the display when no characters are waiting.
729 * Also shows mode, ruler and positions cursor.
730 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000731 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733#ifdef FEAT_SCROLLBIND
734 if (curwin->w_p_scb)
735 do_check_scrollbind(TRUE);
736#endif
737
Bram Moolenaar860cae12010-06-05 23:22:07 +0200738#ifdef FEAT_CURSORBIND
739 if (curwin->w_p_crb)
740 do_check_cursorbind();
741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 update_curswant();
743 old_topline = curwin->w_topline;
744#ifdef FEAT_DIFF
745 old_topfill = curwin->w_topfill;
746#endif
747
748#ifdef USE_ON_FLY_SCROLL
749 dont_scroll = FALSE; /* allow scrolling here */
750#endif
751
752 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000753 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 */
755 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000756 do
757 {
758 c = safe_vgetc();
759 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000761#ifdef FEAT_AUTOCMD
762 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
763 did_cursorhold = TRUE;
764#endif
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766#ifdef FEAT_RIGHTLEFT
767 if (p_hkmap && KeyTyped)
768 c = hkmap(c); /* Hebrew mode mapping */
769#endif
770#ifdef FEAT_FKMAP
771 if (p_fkmap && KeyTyped)
772 c = fkmap(c); /* Farsi mode mapping */
773#endif
774
775#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000776 /*
777 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000778 * and the cursor is still in the completed word. Only when there is
779 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000780 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000781 if (compl_started
782 && pum_wanted()
783 && curwin->w_cursor.col >= compl_col
784 && (compl_shown_match == NULL
785 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000786 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000787 /* BS: Delete one character from "compl_leader". */
788 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000789 && curwin->w_cursor.col > compl_col
790 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000791 continue;
792
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000793 /* When no match was selected or it was edited. */
794 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000795 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000796 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000797 * "compl_leader". Except when at the original match and
798 * there is nothing to add, CTRL-L works like CTRL-P then. */
799 if (c == Ctrl_L
800 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000801 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000802 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000803 {
804 ins_compl_addfrommatch();
805 continue;
806 }
807
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000808 /* A non-white character that fits in with the current
809 * completion: Add to "compl_leader". */
810 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000811 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100812#ifdef FEAT_AUTOCMD
813 /* Trigger InsertCharPre. */
814 char_u *str = do_insert_char_pre(c);
815 char_u *p;
816
817 if (str != NULL)
818 {
819 for (p = str; *p != NUL; mb_ptr_adv(p))
820 ins_compl_addleader(PTR2CHAR(p));
821 vim_free(str);
822 }
823 else
824#endif
825 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000826 continue;
827 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000828
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000829 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000830 * compl_enter_selects is set the Enter key does the same. */
831 if (c == Ctrl_Y || (compl_enter_selects
832 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000833 {
834 ins_compl_delete();
835 ins_compl_insert();
836 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000837 }
838 }
839
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
841 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000842 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000843 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000844 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#endif
846
Bram Moolenaar488c6512005-08-11 20:09:58 +0000847 /* CTRL-\ CTRL-N goes to Normal mode,
848 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
849 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (c == Ctrl_BSL)
851 {
852 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000853 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 ++no_mapping;
855 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000856 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 --no_mapping;
858 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000859 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000861 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 vungetc(c);
863 c = Ctrl_BSL;
864 }
865 else if (c == Ctrl_G && p_im)
866 continue;
867 else
868 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000869 if (c == Ctrl_O)
870 {
871 ins_ctrl_o();
872 ins_at_eol = FALSE; /* cursor keeps its column */
873 nomove = TRUE;
874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 count = 0;
876 goto doESCkey;
877 }
878 }
879
880#ifdef FEAT_DIGRAPHS
881 c = do_digraph(c);
882#endif
883
884#ifdef FEAT_INS_EXPAND
885 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
886 goto docomplete;
887#endif
888 if (c == Ctrl_V || c == Ctrl_Q)
889 {
890 ins_ctrl_v();
891 c = Ctrl_V; /* pretend CTRL-V is last typed character */
892 continue;
893 }
894
895#ifdef FEAT_CINDENT
896 if (cindent_on()
897# ifdef FEAT_INS_EXPAND
898 && ctrl_x_mode == 0
899# endif
900 )
901 {
902 /* A key name preceded by a bang means this key is not to be
903 * inserted. Skip ahead to the re-indenting below.
904 * A key name preceded by a star means that indenting has to be
905 * done before inserting the key. */
906 line_is_white = inindent(0);
907 if (in_cinkeys(c, '!', line_is_white))
908 goto force_cindent;
909 if (can_cindent && in_cinkeys(c, '*', line_is_white)
910 && stop_arrow() == OK)
911 do_c_expr_indent();
912 }
913#endif
914
915#ifdef FEAT_RIGHTLEFT
916 if (curwin->w_p_rl)
917 switch (c)
918 {
919 case K_LEFT: c = K_RIGHT; break;
920 case K_S_LEFT: c = K_S_RIGHT; break;
921 case K_C_LEFT: c = K_C_RIGHT; break;
922 case K_RIGHT: c = K_LEFT; break;
923 case K_S_RIGHT: c = K_S_LEFT; break;
924 case K_C_RIGHT: c = K_C_LEFT; break;
925 }
926#endif
927
928#ifdef FEAT_VISUAL
929 /*
930 * If 'keymodel' contains "startsel", may start selection. If it
931 * does, a CTRL-O and c will be stuffed, we need to get these
932 * characters.
933 */
934 if (ins_start_select(c))
935 continue;
936#endif
937
938 /*
939 * The big switch to handle a character in insert mode.
940 */
941 switch (c)
942 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (echeck_abbr(ESC + ABBR_OFF))
945 break;
946 /*FALLTHROUGH*/
947
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000948 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#ifdef FEAT_CMDWIN
950 if (c == Ctrl_C && cmdwin_type != 0)
951 {
952 /* Close the cmdline window. */
953 cmdwin_result = K_IGNORE;
954 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000955 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 goto doESCkey;
957 }
958#endif
959
960#ifdef UNIX
961do_intr:
962#endif
963 /* when 'insertmode' set, and not halfway a mapping, don't leave
964 * Insert mode */
965 if (goto_im())
966 {
967 if (got_int)
968 {
969 (void)vgetc(); /* flush all buffers */
970 got_int = FALSE;
971 }
972 else
973 vim_beep();
974 break;
975 }
976doESCkey:
977 /*
978 * This is the ONLY return from edit()!
979 */
980 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
981 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000982 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 o_lnum = curwin->w_cursor.lnum;
984
Bram Moolenaar488c6512005-08-11 20:09:58 +0000985 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000986 {
987#ifdef FEAT_AUTOCMD
988 if (cmdchar != 'r' && cmdchar != 'v')
989 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
990 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000991 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 continue;
996
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000997 case Ctrl_Z: /* suspend when 'insertmode' set */
998 if (!p_im)
999 goto normalchar; /* insert CTRL-Z as normal char */
1000 stuffReadbuff((char_u *)":st\r");
1001 c = Ctrl_O;
1002 /*FALLTHROUGH*/
1003
1004 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001005#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001006 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001007 goto docomplete;
1008#endif
1009 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1010 break;
1011 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001012
1013#ifdef FEAT_VIRTUALEDIT
1014 /* don't move the cursor left when 'virtualedit' has "onemore". */
1015 if (ve_flags & VE_ONEMORE)
1016 {
1017 ins_at_eol = FALSE;
1018 nomove = TRUE;
1019 }
1020#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001021 count = 0;
1022 goto doESCkey;
1023
Bram Moolenaar572cb562005-08-05 21:35:02 +00001024 case K_INS: /* toggle insert/replace mode */
1025 case K_KINS:
1026 ins_insert(replaceState);
1027 break;
1028
1029 case K_SELECT: /* end of Select mode mapping - ignore */
1030 break;
1031
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001032#ifdef FEAT_SNIFF
1033 case K_SNIFF: /* Sniff command received */
1034 stuffcharReadbuff(K_SNIFF);
1035 goto doESCkey;
1036#endif
1037
1038 case K_HELP: /* Help key works like <ESC> <Help> */
1039 case K_F1:
1040 case K_XF1:
1041 stuffcharReadbuff(K_HELP);
1042 if (p_im)
1043 need_start_insertmode = TRUE;
1044 goto doESCkey;
1045
1046#ifdef FEAT_NETBEANS_INTG
1047 case K_F21: /* NetBeans command */
1048 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001049 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001050 --no_mapping;
1051 netbeans_keycommand(i);
1052 break;
1053#endif
1054
1055 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 case NUL:
1057 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 /* For ^@ the trailing ESC will end the insert, unless there is an
1059 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1061 && c != Ctrl_A && !p_im)
1062 goto doESCkey; /* quit insert mode */
1063 inserted_space = FALSE;
1064 break;
1065
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 ins_reg();
1068 auto_format(FALSE, TRUE);
1069 inserted_space = FALSE;
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 ins_ctrl_g();
1074 break;
1075
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001076 case Ctrl_HAT: /* switch input mode and/or langmap */
1077 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 break;
1079
1080#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001081 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (!p_ari)
1083 goto normalchar;
1084 ins_ctrl_();
1085 break;
1086#endif
1087
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1090 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1091 goto docomplete;
1092#endif
1093 /* FALLTHROUGH */
1094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096# ifdef FEAT_INS_EXPAND
1097 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1098 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001099 if (has_compl_option(FALSE))
1100 goto docomplete;
1101 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 }
1103# endif
1104 ins_shift(c, lastc);
1105 auto_format(FALSE, TRUE);
1106 inserted_space = FALSE;
1107 break;
1108
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 case K_KDEL:
1111 ins_del();
1112 auto_format(FALSE, TRUE);
1113 break;
1114
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 case Ctrl_H:
1117 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1118 auto_format(FALSE, TRUE);
1119 break;
1120
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001121 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1123 auto_format(FALSE, TRUE);
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001127# ifdef FEAT_COMPL_FUNC
1128 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001130 goto docomplete;
1131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1133 auto_format(FALSE, TRUE);
1134 inserted_space = FALSE;
1135 break;
1136
1137#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 case K_LEFTMOUSE_NM:
1140 case K_LEFTDRAG:
1141 case K_LEFTRELEASE:
1142 case K_LEFTRELEASE_NM:
1143 case K_MIDDLEMOUSE:
1144 case K_MIDDLEDRAG:
1145 case K_MIDDLERELEASE:
1146 case K_RIGHTMOUSE:
1147 case K_RIGHTDRAG:
1148 case K_RIGHTRELEASE:
1149 case K_X1MOUSE:
1150 case K_X1DRAG:
1151 case K_X1RELEASE:
1152 case K_X2MOUSE:
1153 case K_X2DRAG:
1154 case K_X2RELEASE:
1155 ins_mouse(c);
1156 break;
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001159 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 break;
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001163 ins_mousescroll(MSCR_UP);
1164 break;
1165
1166 case K_MOUSELEFT: /* Scroll wheel left */
1167 ins_mousescroll(MSCR_LEFT);
1168 break;
1169
1170 case K_MOUSERIGHT: /* Scroll wheel right */
1171 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 break;
1173#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001174#ifdef FEAT_GUI_TABLINE
1175 case K_TABLINE:
1176 case K_TABMENU:
1177 ins_tabline(c);
1178 break;
1179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 break;
1183
Bram Moolenaar754b5602006-02-09 23:53:20 +00001184#ifdef FEAT_AUTOCMD
1185 case K_CURSORHOLD: /* Didn't type something for a while. */
1186 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1187 did_cursorhold = TRUE;
1188 break;
1189#endif
1190
Bram Moolenaar4770d092006-01-12 23:22:24 +00001191#ifdef FEAT_GUI_W32
1192 /* On Win32 ignore <M-F4>, we get it when closing the window was
1193 * cancelled. */
1194 case K_F4:
1195 if (mod_mask != MOD_MASK_ALT)
1196 goto normalchar;
1197 break;
1198#endif
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200#ifdef FEAT_GUI
1201 case K_VER_SCROLLBAR:
1202 ins_scroll();
1203 break;
1204
1205 case K_HOR_SCROLLBAR:
1206 ins_horscroll();
1207 break;
1208#endif
1209
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001210 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 case K_S_HOME:
1213 case K_C_HOME:
1214 ins_home(c);
1215 break;
1216
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001217 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 case K_S_END:
1220 case K_C_END:
1221 ins_end(c);
1222 break;
1223
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001225 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1226 ins_s_left();
1227 else
1228 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 case K_C_LEFT:
1233 ins_s_left();
1234 break;
1235
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001236 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001237 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1238 ins_s_right();
1239 else
1240 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 break;
1242
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001243 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 case K_C_RIGHT:
1245 ins_s_right();
1246 break;
1247
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001249#ifdef FEAT_INS_EXPAND
1250 if (pum_visible())
1251 goto docomplete;
1252#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001253 if (mod_mask & MOD_MASK_SHIFT)
1254 ins_pageup();
1255 else
1256 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 break;
1258
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001259 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 case K_PAGEUP:
1261 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001262#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001263 if (pum_visible())
1264 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 ins_pageup();
1267 break;
1268
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001269 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001270#ifdef FEAT_INS_EXPAND
1271 if (pum_visible())
1272 goto docomplete;
1273#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001274 if (mod_mask & MOD_MASK_SHIFT)
1275 ins_pagedown();
1276 else
1277 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 break;
1279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001280 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 case K_PAGEDOWN:
1282 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001283#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001284 if (pum_visible())
1285 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 ins_pagedown();
1288 break;
1289
1290#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ins_drop();
1293 break;
1294#endif
1295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 c = TAB;
1298 /* FALLTHROUGH */
1299
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001300 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1302 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1303 goto docomplete;
1304#endif
1305 inserted_space = FALSE;
1306 if (ins_tab())
1307 goto normalchar; /* insert TAB as a normal char */
1308 auto_format(FALSE, TRUE);
1309 break;
1310
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001311 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 c = CAR;
1313 /* FALLTHROUGH */
1314 case CAR:
1315 case NL:
1316#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1317 /* In a quickfix window a <CR> jumps to the error under the
1318 * cursor. */
1319 if (bt_quickfix(curbuf) && c == CAR)
1320 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001321 if (curwin->w_llist_ref == NULL) /* quickfix window */
1322 do_cmdline_cmd((char_u *)".cc");
1323 else /* location list window */
1324 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 break;
1326 }
1327#endif
1328#ifdef FEAT_CMDWIN
1329 if (cmdwin_type != 0)
1330 {
1331 /* Execute the command in the cmdline window. */
1332 cmdwin_result = CAR;
1333 goto doESCkey;
1334 }
1335#endif
1336 if (ins_eol(c) && !p_im)
1337 goto doESCkey; /* out of memory */
1338 auto_format(FALSE, FALSE);
1339 inserted_space = FALSE;
1340 break;
1341
Bram Moolenaar860cae12010-06-05 23:22:07 +02001342#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344# ifdef FEAT_INS_EXPAND
1345 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1346 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001347 if (has_compl_option(TRUE))
1348 goto docomplete;
1349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351# endif
1352# ifdef FEAT_DIGRAPHS
1353 c = ins_digraph();
1354 if (c == NUL)
1355 break;
1356# endif
1357 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001361 case Ctrl_X: /* Enter CTRL-X mode */
1362 ins_ctrl_x();
1363 break;
1364
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001365 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (ctrl_x_mode != CTRL_X_TAGS)
1367 goto normalchar;
1368 goto docomplete;
1369
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001370 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (ctrl_x_mode != CTRL_X_FILES)
1372 goto normalchar;
1373 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001374
1375 case 's': /* Spelling completion after ^X */
1376 case Ctrl_S:
1377 if (ctrl_x_mode != CTRL_X_SPELL)
1378 goto normalchar;
1379 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380#endif
1381
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383#ifdef FEAT_INS_EXPAND
1384 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1385#endif
1386 {
1387 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1388 if (p_im)
1389 {
1390 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1391 break;
1392 goto doESCkey;
1393 }
1394 goto normalchar;
1395 }
1396#ifdef FEAT_INS_EXPAND
1397 /* FALLTHROUGH */
1398
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001399 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 case Ctrl_N:
1401 /* if 'complete' is empty then plain ^P is no longer special,
1402 * but it is under other ^X modes */
1403 if (*curbuf->b_p_cpt == NUL
1404 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001405 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 goto normalchar;
1407
1408docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001409 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001412 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 break;
1414#endif /* FEAT_INS_EXPAND */
1415
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001416 case Ctrl_Y: /* copy from previous line or scroll down */
1417 case Ctrl_E: /* copy from next line or scroll up */
1418 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 break;
1420
1421 default:
1422#ifdef UNIX
1423 if (c == intr_char) /* special interrupt char */
1424 goto do_intr;
1425#endif
1426
Bram Moolenaare659c952011-05-19 17:25:41 +02001427normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001429 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001431#ifdef FEAT_AUTOCMD
1432 if (!p_paste)
1433 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001434 /* Trigger InsertCharPre. */
1435 char_u *str = do_insert_char_pre(c);
1436 char_u *p;
1437
1438 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001439 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001440 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001441 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001442 /* Insert the new value of v:char literally. */
1443 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001444 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001445 c = PTR2CHAR(p);
1446 if (c == CAR || c == K_KENTER || c == NL)
1447 ins_eol(c);
1448 else
1449 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001450 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001451 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001452 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001453 vim_free(str);
1454 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001455 }
1456
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001457 /* If the new value is already inserted or an empty string
1458 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001459 if (c == NUL)
1460 break;
1461 }
1462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463#ifdef FEAT_SMARTINDENT
1464 /* Try to perform smart-indenting. */
1465 ins_try_si(c);
1466#endif
1467
1468 if (c == ' ')
1469 {
1470 inserted_space = TRUE;
1471#ifdef FEAT_CINDENT
1472 if (inindent(0))
1473 can_cindent = FALSE;
1474#endif
1475 if (Insstart_blank_vcol == MAXCOL
1476 && curwin->w_cursor.lnum == Insstart.lnum)
1477 Insstart_blank_vcol = get_nolist_virtcol();
1478 }
1479
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001480 /* Insert a normal character and check for abbreviations on a
1481 * special character. Let CTRL-] expand abbreviations without
1482 * inserting it. */
1483 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484#ifdef FEAT_MBYTE
1485 /* Add ABBR_OFF for characters above 0x100, this is
1486 * what check_abbr() expects. */
1487 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1488#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001489 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 {
1491 insert_special(c, FALSE, FALSE);
1492#ifdef FEAT_RIGHTLEFT
1493 revins_legal++;
1494 revins_chars++;
1495#endif
1496 }
1497
1498 auto_format(FALSE, TRUE);
1499
1500#ifdef FEAT_FOLDING
1501 /* When inserting a character the cursor line must never be in a
1502 * closed fold. */
1503 foldOpenCursor();
1504#endif
1505 break;
1506 } /* end of switch (c) */
1507
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001508#ifdef FEAT_AUTOCMD
1509 /* If typed something may trigger CursorHoldI again. */
1510 if (c != K_CURSORHOLD)
1511 did_cursorhold = FALSE;
1512#endif
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 /* If the cursor was moved we didn't just insert a space */
1515 if (arrow_used)
1516 inserted_space = FALSE;
1517
1518#ifdef FEAT_CINDENT
1519 if (can_cindent && cindent_on()
1520# ifdef FEAT_INS_EXPAND
1521 && ctrl_x_mode == 0
1522# endif
1523 )
1524 {
1525force_cindent:
1526 /*
1527 * Indent now if a key was typed that is in 'cinkeys'.
1528 */
1529 if (in_cinkeys(c, ' ', line_is_white))
1530 {
1531 if (stop_arrow() == OK)
1532 /* re-indent the current line */
1533 do_c_expr_indent();
1534 }
1535 }
1536#endif /* FEAT_CINDENT */
1537
1538 } /* for (;;) */
1539 /* NOTREACHED */
1540}
1541
1542/*
1543 * Redraw for Insert mode.
1544 * This is postponed until getting the next character to make '$' in the 'cpo'
1545 * option work correctly.
1546 * Only redraw when there are no characters available. This speeds up
1547 * inserting sequences of characters (e.g., for CTRL-R).
1548 */
1549 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001550ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001551 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001553#ifdef FEAT_CONCEAL
1554 linenr_T conceal_old_cursor_line = 0;
1555 linenr_T conceal_new_cursor_line = 0;
1556 int conceal_update_lines = FALSE;
1557#endif
1558
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001559 if (char_avail())
1560 return;
1561
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001562#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001563 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1564 * visible, the command might delete it. */
1565 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001566# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001567 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001568# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001569# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001570 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001571# endif
1572# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001573 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001574# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001575 )
1576 && !equalpos(last_cursormoved, curwin->w_cursor)
1577# ifdef FEAT_INS_EXPAND
1578 && !pum_visible()
1579# endif
1580 )
1581 {
1582# ifdef FEAT_SYN_HL
1583 /* Need to update the screen first, to make sure syntax
1584 * highlighting is correct after making a change (e.g., inserting
1585 * a "(". The autocommand may also require a redraw, so it's done
1586 * again below, unfortunately. */
1587 if (syntax_present(curwin) && must_redraw)
1588 update_screen(0);
1589# endif
1590# ifdef FEAT_AUTOCMD
1591 if (has_cursormovedI())
1592 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1593# endif
1594# ifdef FEAT_CONCEAL
1595 if (curwin->w_p_cole > 0)
1596 {
1597 conceal_old_cursor_line = last_cursormoved.lnum;
1598 conceal_new_cursor_line = curwin->w_cursor.lnum;
1599 conceal_update_lines = TRUE;
1600 }
1601# endif
1602 last_cursormoved = curwin->w_cursor;
1603 }
1604#endif
1605
1606#ifdef FEAT_AUTOCMD
1607 /* Trigger TextChangedI if b_changedtick differs. */
1608 if (ready && has_textchangedI()
1609 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001610# ifdef FEAT_INS_EXPAND
1611 && !pum_visible()
1612# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001613 )
1614 {
1615 if (last_changedtick_buf == curbuf)
1616 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1617 last_changedtick_buf = curbuf;
1618 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001620#endif
1621
1622 if (must_redraw)
1623 update_screen(0);
1624 else if (clear_cmdline || redraw_cmdline)
1625 showmode(); /* clear cmdline and show mode */
1626# if defined(FEAT_CONCEAL)
1627 if ((conceal_update_lines
1628 && (conceal_old_cursor_line != conceal_new_cursor_line
1629 || conceal_cursor_line(curwin)))
1630 || need_cursor_line_redraw)
1631 {
1632 if (conceal_old_cursor_line != conceal_new_cursor_line)
1633 update_single_line(curwin, conceal_old_cursor_line);
1634 update_single_line(curwin, conceal_new_cursor_line == 0
1635 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1636 curwin->w_valid &= ~VALID_CROW;
1637 }
1638# endif
1639 showruler(FALSE);
1640 setcursor();
1641 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642}
1643
1644/*
1645 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1646 */
1647 static void
1648ins_ctrl_v()
1649{
1650 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001651 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
1653 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001654 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
1656 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001657 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001659 did_putchar = TRUE;
1660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1662
1663#ifdef FEAT_CMDL_INFO
1664 add_to_showcmd_c(Ctrl_V);
1665#endif
1666
1667 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001668 if (did_putchar)
1669 /* when the line fits in 'columns' the '^' is at the start of the next
1670 * line and will not removed by the redraw */
1671 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001672#ifdef FEAT_CMDL_INFO
1673 clear_showcmd();
1674#endif
1675 insert_special(c, FALSE, TRUE);
1676#ifdef FEAT_RIGHTLEFT
1677 revins_chars++;
1678 revins_legal++;
1679#endif
1680}
1681
1682/*
1683 * Put a character directly onto the screen. It's not stored in a buffer.
1684 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1685 */
1686static int pc_status;
1687#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1688#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1689#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1690#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001692static int pc_attr;
1693static int pc_row;
1694static int pc_col;
1695
1696 void
1697edit_putchar(c, highlight)
1698 int c;
1699 int highlight;
1700{
1701 int attr;
1702
1703 if (ScreenLines != NULL)
1704 {
1705 update_topline(); /* just in case w_topline isn't valid */
1706 validate_cursor();
1707 if (highlight)
1708 attr = hl_attr(HLF_8);
1709 else
1710 attr = 0;
1711 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1712 pc_col = W_WINCOL(curwin);
1713#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1714 pc_status = PC_STATUS_UNSET;
1715#endif
1716#ifdef FEAT_RIGHTLEFT
1717 if (curwin->w_p_rl)
1718 {
1719 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1720# ifdef FEAT_MBYTE
1721 if (has_mbyte)
1722 {
1723 int fix_col = mb_fix_col(pc_col, pc_row);
1724
1725 if (fix_col != pc_col)
1726 {
1727 screen_putchar(' ', pc_row, fix_col, attr);
1728 --curwin->w_wcol;
1729 pc_status = PC_STATUS_RIGHT;
1730 }
1731 }
1732# endif
1733 }
1734 else
1735#endif
1736 {
1737 pc_col += curwin->w_wcol;
1738#ifdef FEAT_MBYTE
1739 if (mb_lefthalve(pc_row, pc_col))
1740 pc_status = PC_STATUS_LEFT;
1741#endif
1742 }
1743
1744 /* save the character to be able to put it back */
1745#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1746 if (pc_status == PC_STATUS_UNSET)
1747#endif
1748 {
1749 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1750 pc_status = PC_STATUS_SET;
1751 }
1752 screen_putchar(c, pc_row, pc_col, attr);
1753 }
1754}
1755
1756/*
1757 * Undo the previous edit_putchar().
1758 */
1759 void
1760edit_unputchar()
1761{
1762 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1763 {
1764#if defined(FEAT_MBYTE)
1765 if (pc_status == PC_STATUS_RIGHT)
1766 ++curwin->w_wcol;
1767 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1768 redrawWinline(curwin->w_cursor.lnum, FALSE);
1769 else
1770#endif
1771 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1772 }
1773}
1774
1775/*
1776 * Called when p_dollar is set: display a '$' at the end of the changed text
1777 * Only works when cursor is in the line that changes.
1778 */
1779 void
1780display_dollar(col)
1781 colnr_T col;
1782{
1783 colnr_T save_col;
1784
1785 if (!redrawing())
1786 return;
1787
1788 cursor_off();
1789 save_col = curwin->w_cursor.col;
1790 curwin->w_cursor.col = col;
1791#ifdef FEAT_MBYTE
1792 if (has_mbyte)
1793 {
1794 char_u *p;
1795
1796 /* If on the last byte of a multi-byte move to the first byte. */
1797 p = ml_get_curline();
1798 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1799 }
1800#endif
1801 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1802 if (curwin->w_wcol < W_WIDTH(curwin))
1803 {
1804 edit_putchar('$', FALSE);
1805 dollar_vcol = curwin->w_virtcol;
1806 }
1807 curwin->w_cursor.col = save_col;
1808}
1809
1810/*
1811 * Call this function before moving the cursor from the normal insert position
1812 * in insert mode.
1813 */
1814 static void
1815undisplay_dollar()
1816{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001817 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001819 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001820 redrawWinline(curwin->w_cursor.lnum, FALSE);
1821 }
1822}
1823
1824/*
1825 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1826 * Keep the cursor on the same character.
1827 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1828 * type == INDENT_DEC decrease indent (for CTRL-D)
1829 * type == INDENT_SET set indent to "amount"
1830 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1831 */
1832 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001833change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834 int type;
1835 int amount;
1836 int round;
1837 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001838 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839{
1840 int vcol;
1841 int last_vcol;
1842 int insstart_less; /* reduction for Insstart.col */
1843 int new_cursor_col;
1844 int i;
1845 char_u *ptr;
1846 int save_p_list;
1847 int start_col;
1848 colnr_T vc;
1849#ifdef FEAT_VREPLACE
1850 colnr_T orig_col = 0; /* init for GCC */
1851 char_u *new_line, *orig_line = NULL; /* init for GCC */
1852
1853 /* VREPLACE mode needs to know what the line was like before changing */
1854 if (State & VREPLACE_FLAG)
1855 {
1856 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1857 orig_col = curwin->w_cursor.col;
1858 }
1859#endif
1860
1861 /* for the following tricks we don't want list mode */
1862 save_p_list = curwin->w_p_list;
1863 curwin->w_p_list = FALSE;
1864 vc = getvcol_nolist(&curwin->w_cursor);
1865 vcol = vc;
1866
1867 /*
1868 * For Replace mode we need to fix the replace stack later, which is only
1869 * possible when the cursor is in the indent. Remember the number of
1870 * characters before the cursor if it's possible.
1871 */
1872 start_col = curwin->w_cursor.col;
1873
1874 /* determine offset from first non-blank */
1875 new_cursor_col = curwin->w_cursor.col;
1876 beginline(BL_WHITE);
1877 new_cursor_col -= curwin->w_cursor.col;
1878
1879 insstart_less = curwin->w_cursor.col;
1880
1881 /*
1882 * If the cursor is in the indent, compute how many screen columns the
1883 * cursor is to the left of the first non-blank.
1884 */
1885 if (new_cursor_col < 0)
1886 vcol = get_indent() - vcol;
1887
1888 if (new_cursor_col > 0) /* can't fix replace stack */
1889 start_col = -1;
1890
1891 /*
1892 * Set the new indent. The cursor will be put on the first non-blank.
1893 */
1894 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001895 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 else
1897 {
1898#ifdef FEAT_VREPLACE
1899 int save_State = State;
1900
1901 /* Avoid being called recursively. */
1902 if (State & VREPLACE_FLAG)
1903 State = INSERT;
1904#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001905 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906#ifdef FEAT_VREPLACE
1907 State = save_State;
1908#endif
1909 }
1910 insstart_less -= curwin->w_cursor.col;
1911
1912 /*
1913 * Try to put cursor on same character.
1914 * If the cursor is at or after the first non-blank in the line,
1915 * compute the cursor column relative to the column of the first
1916 * non-blank character.
1917 * If we are not in insert mode, leave the cursor on the first non-blank.
1918 * If the cursor is before the first non-blank, position it relative
1919 * to the first non-blank, counted in screen columns.
1920 */
1921 if (new_cursor_col >= 0)
1922 {
1923 /*
1924 * When changing the indent while the cursor is touching it, reset
1925 * Insstart_col to 0.
1926 */
1927 if (new_cursor_col == 0)
1928 insstart_less = MAXCOL;
1929 new_cursor_col += curwin->w_cursor.col;
1930 }
1931 else if (!(State & INSERT))
1932 new_cursor_col = curwin->w_cursor.col;
1933 else
1934 {
1935 /*
1936 * Compute the screen column where the cursor should be.
1937 */
1938 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001939 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940
1941 /*
1942 * Advance the cursor until we reach the right screen column.
1943 */
1944 vcol = last_vcol = 0;
1945 new_cursor_col = -1;
1946 ptr = ml_get_curline();
1947 while (vcol <= (int)curwin->w_virtcol)
1948 {
1949 last_vcol = vcol;
1950#ifdef FEAT_MBYTE
1951 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001952 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 else
1954#endif
1955 ++new_cursor_col;
1956 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1957 }
1958 vcol = last_vcol;
1959
1960 /*
1961 * May need to insert spaces to be able to position the cursor on
1962 * the right screen column.
1963 */
1964 if (vcol != (int)curwin->w_virtcol)
1965 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001966 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001968 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 if (ptr != NULL)
1970 {
1971 new_cursor_col += i;
1972 ptr[i] = NUL;
1973 while (--i >= 0)
1974 ptr[i] = ' ';
1975 ins_str(ptr);
1976 vim_free(ptr);
1977 }
1978 }
1979
1980 /*
1981 * When changing the indent while the cursor is in it, reset
1982 * Insstart_col to 0.
1983 */
1984 insstart_less = MAXCOL;
1985 }
1986
1987 curwin->w_p_list = save_p_list;
1988
1989 if (new_cursor_col <= 0)
1990 curwin->w_cursor.col = 0;
1991 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001992 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993 curwin->w_set_curswant = TRUE;
1994 changed_cline_bef_curs();
1995
1996 /*
1997 * May have to adjust the start of the insert.
1998 */
1999 if (State & INSERT)
2000 {
2001 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2002 {
2003 if ((int)Insstart.col <= insstart_less)
2004 Insstart.col = 0;
2005 else
2006 Insstart.col -= insstart_less;
2007 }
2008 if ((int)ai_col <= insstart_less)
2009 ai_col = 0;
2010 else
2011 ai_col -= insstart_less;
2012 }
2013
2014 /*
2015 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2016 * If the number of characters before the cursor decreased, need to pop a
2017 * few characters from the replace stack.
2018 * If the number of characters before the cursor increased, need to push a
2019 * few NULs onto the replace stack.
2020 */
2021 if (REPLACE_NORMAL(State) && start_col >= 0)
2022 {
2023 while (start_col > (int)curwin->w_cursor.col)
2024 {
2025 replace_join(0); /* remove a NUL from the replace stack */
2026 --start_col;
2027 }
2028 while (start_col < (int)curwin->w_cursor.col || replaced)
2029 {
2030 replace_push(NUL);
2031 if (replaced)
2032 {
2033 replace_push(replaced);
2034 replaced = NUL;
2035 }
2036 ++start_col;
2037 }
2038 }
2039
2040#ifdef FEAT_VREPLACE
2041 /*
2042 * For VREPLACE mode, we also have to fix the replace stack. In this case
2043 * it is always possible because we backspace over the whole line and then
2044 * put it back again the way we wanted it.
2045 */
2046 if (State & VREPLACE_FLAG)
2047 {
2048 /* If orig_line didn't allocate, just return. At least we did the job,
2049 * even if you can't backspace. */
2050 if (orig_line == NULL)
2051 return;
2052
2053 /* Save new line */
2054 new_line = vim_strsave(ml_get_curline());
2055 if (new_line == NULL)
2056 return;
2057
2058 /* We only put back the new line up to the cursor */
2059 new_line[curwin->w_cursor.col] = NUL;
2060
2061 /* Put back original line */
2062 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2063 curwin->w_cursor.col = orig_col;
2064
2065 /* Backspace from cursor to start of line */
2066 backspace_until_column(0);
2067
2068 /* Insert new stuff into line again */
2069 ins_bytes(new_line);
2070
2071 vim_free(new_line);
2072 }
2073#endif
2074}
2075
2076/*
2077 * Truncate the space at the end of a line. This is to be used only in an
2078 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2079 * modes.
2080 */
2081 void
2082truncate_spaces(line)
2083 char_u *line;
2084{
2085 int i;
2086
2087 /* find start of trailing white space */
2088 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2089 {
2090 if (State & REPLACE_FLAG)
2091 replace_join(0); /* remove a NUL from the replace stack */
2092 }
2093 line[i + 1] = NUL;
2094}
2095
2096#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2097 || defined(FEAT_COMMENTS) || defined(PROTO)
2098/*
2099 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2100 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002101 * Will attempt not to go before "col" even when there is a composing
2102 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002103 */
2104 void
2105backspace_until_column(col)
2106 int col;
2107{
2108 while ((int)curwin->w_cursor.col > col)
2109 {
2110 curwin->w_cursor.col--;
2111 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002112 replace_do_bs(col);
2113 else if (!del_char_after_col(col))
2114 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 }
2116}
2117#endif
2118
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002119/*
2120 * Like del_char(), but make sure not to go before column "limit_col".
2121 * Only matters when there are composing characters.
2122 * Return TRUE when something was deleted.
2123 */
2124 static int
2125del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002126 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002127{
2128#ifdef FEAT_MBYTE
2129 if (enc_utf8 && limit_col >= 0)
2130 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002131 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002132
2133 /* Make sure the cursor is at the start of a character, but
2134 * skip forward again when going too far back because of a
2135 * composing character. */
2136 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002137 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002138 {
2139 int l = utf_ptr2len(ml_get_cursor());
2140
2141 if (l == 0) /* end of line */
2142 break;
2143 curwin->w_cursor.col += l;
2144 }
2145 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2146 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002147 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002148 }
2149 else
2150#endif
2151 (void)del_char(FALSE);
2152 return TRUE;
2153}
2154
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2156/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002157 * CTRL-X pressed in Insert mode.
2158 */
2159 static void
2160ins_ctrl_x()
2161{
2162 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2163 * CTRL-V works like CTRL-N */
2164 if (ctrl_x_mode != CTRL_X_CMDLINE)
2165 {
2166 /* if the next ^X<> won't ADD nothing, then reset
2167 * compl_cont_status */
2168 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002169 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002170 else
2171 compl_cont_status = 0;
2172 /* We're not sure which CTRL-X mode it will be yet */
2173 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2174 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2175 edit_submode_pre = NULL;
2176 showmode();
2177 }
2178}
2179
2180/*
2181 * Return TRUE if the 'dict' or 'tsr' option can be used.
2182 */
2183 static int
2184has_compl_option(dict_opt)
2185 int dict_opt;
2186{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002187 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002188# ifdef FEAT_SPELL
2189 && !curwin->w_p_spell
2190# endif
2191 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002192 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2193 {
2194 ctrl_x_mode = 0;
2195 edit_submode = NULL;
2196 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2197 : (char_u *)_("'thesaurus' option is empty"),
2198 hl_attr(HLF_E));
2199 if (emsg_silent == 0)
2200 {
2201 vim_beep();
2202 setcursor();
2203 out_flush();
2204 ui_delay(2000L, FALSE);
2205 }
2206 return FALSE;
2207 }
2208 return TRUE;
2209}
2210
2211/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2213 * This depends on the current mode.
2214 */
2215 int
2216vim_is_ctrl_x_key(c)
2217 int c;
2218{
2219 /* Always allow ^R - let it's results then be checked */
2220 if (c == Ctrl_R)
2221 return TRUE;
2222
Bram Moolenaare3226be2005-12-18 22:10:00 +00002223 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002224 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002225 return TRUE;
2226
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 switch (ctrl_x_mode)
2228 {
2229 case 0: /* Not in any CTRL-X mode */
2230 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2231 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002232 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2234 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2235 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002236 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002237 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002238 case CTRL_X_SCROLL:
2239 return (c == Ctrl_Y || c == Ctrl_E);
2240 case CTRL_X_WHOLE_LINE:
2241 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2242 case CTRL_X_FILES:
2243 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2244 case CTRL_X_DICTIONARY:
2245 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2246 case CTRL_X_THESAURUS:
2247 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2248 case CTRL_X_TAGS:
2249 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2250#ifdef FEAT_FIND_ID
2251 case CTRL_X_PATH_PATTERNS:
2252 return (c == Ctrl_P || c == Ctrl_N);
2253 case CTRL_X_PATH_DEFINES:
2254 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2255#endif
2256 case CTRL_X_CMDLINE:
2257 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2258 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002259#ifdef FEAT_COMPL_FUNC
2260 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002261 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002262 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002263 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002264#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002265 case CTRL_X_SPELL:
2266 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267 }
2268 EMSG(_(e_internal));
2269 return FALSE;
2270}
2271
2272/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002273 * Return TRUE when character "c" is part of the item currently being
2274 * completed. Used to decide whether to abandon complete mode when the menu
2275 * is visible.
2276 */
2277 static int
2278ins_compl_accept_char(c)
2279 int c;
2280{
2281 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2282 /* When expanding an identifier only accept identifier chars. */
2283 return vim_isIDc(c);
2284
2285 switch (ctrl_x_mode)
2286 {
2287 case CTRL_X_FILES:
2288 /* When expanding file name only accept file name chars. But not
2289 * path separators, so that "proto/<Tab>" expands files in
2290 * "proto", not "proto/" as a whole */
2291 return vim_isfilec(c) && !vim_ispathsep(c);
2292
2293 case CTRL_X_CMDLINE:
2294 case CTRL_X_OMNI:
2295 /* Command line and Omni completion can work with just about any
2296 * printable character, but do stop at white space. */
2297 return vim_isprintc(c) && !vim_iswhite(c);
2298
2299 case CTRL_X_WHOLE_LINE:
2300 /* For while line completion a space can be part of the line. */
2301 return vim_isprintc(c);
2302 }
2303 return vim_iswordc(c);
2304}
2305
2306/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002307 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002309 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310 * the rest of the word to be in -- webb
2311 */
2312 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002313ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 char_u *str;
2315 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002316 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 char_u *fname;
2318 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002319 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002321 char_u *p;
2322 int i, c;
2323 int actual_len; /* Take multi-byte characters */
2324 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002325 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002326 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 int has_lower = FALSE;
2328 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002330 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002332 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333
Bram Moolenaara2993e12007-08-12 14:38:46 +00002334 /* Find actual length of completion. */
2335#ifdef FEAT_MBYTE
2336 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002338 p = str;
2339 actual_len = 0;
2340 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002341 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002342 mb_ptr_adv(p);
2343 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 }
2345 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002346 else
2347#endif
2348 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349
Bram Moolenaara2993e12007-08-12 14:38:46 +00002350 /* Find actual length of original text. */
2351#ifdef FEAT_MBYTE
2352 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002354 p = compl_orig_text;
2355 actual_compl_length = 0;
2356 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002358 mb_ptr_adv(p);
2359 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 }
2361 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002362 else
2363#endif
2364 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002366 /* "actual_len" may be smaller than "actual_compl_length" when using
2367 * thesaurus, only use the minimum when comparing. */
2368 min_len = actual_len < actual_compl_length
2369 ? actual_len : actual_compl_length;
2370
Bram Moolenaara2993e12007-08-12 14:38:46 +00002371 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002372 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002373 if (wca != NULL)
2374 {
2375 p = str;
2376 for (i = 0; i < actual_len; ++i)
2377#ifdef FEAT_MBYTE
2378 if (has_mbyte)
2379 wca[i] = mb_ptr2char_adv(&p);
2380 else
2381#endif
2382 wca[i] = *(p++);
2383
2384 /* Rule 1: Were any chars converted to lower? */
2385 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002386 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002387 {
2388#ifdef FEAT_MBYTE
2389 if (has_mbyte)
2390 c = mb_ptr2char_adv(&p);
2391 else
2392#endif
2393 c = *(p++);
2394 if (MB_ISLOWER(c))
2395 {
2396 has_lower = TRUE;
2397 if (MB_ISUPPER(wca[i]))
2398 {
2399 /* Rule 1 is satisfied. */
2400 for (i = actual_compl_length; i < actual_len; ++i)
2401 wca[i] = MB_TOLOWER(wca[i]);
2402 break;
2403 }
2404 }
2405 }
2406
2407 /*
2408 * Rule 2: No lower case, 2nd consecutive letter converted to
2409 * upper case.
2410 */
2411 if (!has_lower)
2412 {
2413 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002414 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002415 {
2416#ifdef FEAT_MBYTE
2417 if (has_mbyte)
2418 c = mb_ptr2char_adv(&p);
2419 else
2420#endif
2421 c = *(p++);
2422 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2423 {
2424 /* Rule 2 is satisfied. */
2425 for (i = actual_compl_length; i < actual_len; ++i)
2426 wca[i] = MB_TOUPPER(wca[i]);
2427 break;
2428 }
2429 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2430 }
2431 }
2432
2433 /* Copy the original case of the part we typed. */
2434 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002435 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002436 {
2437#ifdef FEAT_MBYTE
2438 if (has_mbyte)
2439 c = mb_ptr2char_adv(&p);
2440 else
2441#endif
2442 c = *(p++);
2443 if (MB_ISLOWER(c))
2444 wca[i] = MB_TOLOWER(wca[i]);
2445 else if (MB_ISUPPER(c))
2446 wca[i] = MB_TOUPPER(wca[i]);
2447 }
2448
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002449 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002450 * Generate encoding specific output from wide character array.
2451 * Multi-byte characters can occupy up to five bytes more than
2452 * ASCII characters, and we also need one byte for NUL, so stay
2453 * six bytes away from the edge of IObuff.
2454 */
2455 p = IObuff;
2456 i = 0;
2457 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2458#ifdef FEAT_MBYTE
2459 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002460 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002461 else
2462#endif
2463 *(p++) = wca[i++];
2464 *p = NUL;
2465
2466 vim_free(wca);
2467 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002469 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2470 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002472 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473}
2474
2475/*
2476 * Add a match to the list of matches.
2477 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002478 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002479 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002481 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002482ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 char_u *str;
2484 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002485 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002487 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002488 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002489 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002490 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002492 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002493 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494
2495 ui_breakcheck();
2496 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002497 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 if (len < 0)
2499 len = (int)STRLEN(str);
2500
2501 /*
2502 * If the same match is already present, don't add it.
2503 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002504 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002506 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002507 do
2508 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002509 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002510 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002511 && match->cp_str[len] == NUL)
2512 return NOTDONE;
2513 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002514 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 }
2516
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002517 /* Remove any popup menu before changing the list of matches. */
2518 ins_compl_del_pum();
2519
Bram Moolenaar071d4272004-06-13 20:20:40 +00002520 /*
2521 * Allocate a new match structure.
2522 * Copy the values to the new match structure.
2523 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002524 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002525 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002526 return FAIL;
2527 match->cp_number = -1;
2528 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002529 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002530 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002531 {
2532 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002533 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002535 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002536
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002538 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2540 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002541 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002542 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002543 && compl_curr_match->cp_fname != NULL
2544 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002545 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002546 else if (fname != NULL)
2547 {
2548 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002549 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002550 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002552 match->cp_fname = NULL;
2553 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002554
2555 if (cptext != NULL)
2556 {
2557 int i;
2558
2559 for (i = 0; i < CPT_COUNT; ++i)
2560 if (cptext[i] != NULL && *cptext[i] != NUL)
2561 match->cp_text[i] = vim_strsave(cptext[i]);
2562 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563
2564 /*
2565 * Link the new match structure in the list of matches.
2566 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002567 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002568 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 else if (dir == FORWARD)
2570 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002571 match->cp_next = compl_curr_match->cp_next;
2572 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 }
2574 else /* BACKWARD */
2575 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002576 match->cp_next = compl_curr_match;
2577 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002579 if (match->cp_next)
2580 match->cp_next->cp_prev = match;
2581 if (match->cp_prev)
2582 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002584 compl_first_match = match;
2585 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002587 /*
2588 * Find the longest common string if still doing that.
2589 */
2590 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2591 ins_compl_longest_match(match);
2592
Bram Moolenaar071d4272004-06-13 20:20:40 +00002593 return OK;
2594}
2595
2596/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002597 * Return TRUE if "str[len]" matches with match->cp_str, considering
2598 * match->cp_icase.
2599 */
2600 static int
2601ins_compl_equal(match, str, len)
2602 compl_T *match;
2603 char_u *str;
2604 int len;
2605{
2606 if (match->cp_icase)
2607 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2608 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2609}
2610
2611/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002612 * Reduce the longest common string for match "match".
2613 */
2614 static void
2615ins_compl_longest_match(match)
2616 compl_T *match;
2617{
2618 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002619 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002620 int had_match;
2621
2622 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002623 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002624 /* First match, use it as a whole. */
2625 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002626 if (compl_leader != NULL)
2627 {
2628 had_match = (curwin->w_cursor.col > compl_col);
2629 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002630 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002631 ins_redraw(FALSE);
2632
2633 /* When the match isn't there (to avoid matching itself) remove it
2634 * again after redrawing. */
2635 if (!had_match)
2636 ins_compl_delete();
2637 compl_used_match = FALSE;
2638 }
2639 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002640 else
2641 {
2642 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002643 p = compl_leader;
2644 s = match->cp_str;
2645 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002646 {
2647#ifdef FEAT_MBYTE
2648 if (has_mbyte)
2649 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002650 c1 = mb_ptr2char(p);
2651 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002652 }
2653 else
2654#endif
2655 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002656 c1 = *p;
2657 c2 = *s;
2658 }
2659 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2660 : (c1 != c2))
2661 break;
2662#ifdef FEAT_MBYTE
2663 if (has_mbyte)
2664 {
2665 mb_ptr_adv(p);
2666 mb_ptr_adv(s);
2667 }
2668 else
2669#endif
2670 {
2671 ++p;
2672 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002673 }
2674 }
2675
2676 if (*p != NUL)
2677 {
2678 /* Leader was shortened, need to change the inserted text. */
2679 *p = NUL;
2680 had_match = (curwin->w_cursor.col > compl_col);
2681 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002682 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002683 ins_redraw(FALSE);
2684
2685 /* When the match isn't there (to avoid matching itself) remove it
2686 * again after redrawing. */
2687 if (!had_match)
2688 ins_compl_delete();
2689 }
2690
2691 compl_used_match = FALSE;
2692 }
2693}
2694
2695/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 * Add an array of matches to the list of matches.
2697 * Frees matches[].
2698 */
2699 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002700ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 int num_matches;
2702 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002703 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704{
2705 int i;
2706 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002707 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002708
Bram Moolenaar572cb562005-08-05 21:35:02 +00002709 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002710 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002711 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002713 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 FreeWild(num_matches, matches);
2715}
2716
2717/* Make the completion list cyclic.
2718 * Return the number of matches (excluding the original).
2719 */
2720 static int
2721ins_compl_make_cyclic()
2722{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002723 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 int count = 0;
2725
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002726 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 {
2728 /*
2729 * Find the end of the list.
2730 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002731 match = compl_first_match;
2732 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002733 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002735 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002736 ++count;
2737 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002738 match->cp_next = compl_first_match;
2739 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002740 }
2741 return count;
2742}
2743
Bram Moolenaara94bc432006-03-10 21:42:59 +00002744/*
2745 * Start completion for the complete() function.
2746 * "startcol" is where the matched text starts (1 is first column).
2747 * "list" is the list of matches.
2748 */
2749 void
2750set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002751 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002752 list_T *list;
2753{
2754 /* If already doing completions stop it. */
2755 if (ctrl_x_mode != 0)
2756 ins_compl_prep(' ');
2757 ins_compl_clear();
2758
2759 if (stop_arrow() == FAIL)
2760 return;
2761
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002762 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002763 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002764 startcol = curwin->w_cursor.col;
2765 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002766 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002767 /* compl_pattern doesn't need to be set */
2768 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2769 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002770 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002771 return;
2772
2773 /* Handle like dictionary completion. */
2774 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2775
2776 ins_compl_add_list(list);
2777 compl_matches = ins_compl_make_cyclic();
2778 compl_started = TRUE;
2779 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002780 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002781
2782 compl_curr_match = compl_first_match;
2783 ins_complete(Ctrl_N);
2784 out_flush();
2785}
2786
2787
Bram Moolenaar9372a112005-12-06 19:59:18 +00002788/* "compl_match_array" points the currently displayed list of entries in the
2789 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002790static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002791static int compl_match_arraysize;
2792
2793/*
2794 * Update the screen and when there is any scrolling remove the popup menu.
2795 */
2796 static void
2797ins_compl_upd_pum()
2798{
2799 int h;
2800
2801 if (compl_match_array != NULL)
2802 {
2803 h = curwin->w_cline_height;
2804 update_screen(0);
2805 if (h != curwin->w_cline_height)
2806 ins_compl_del_pum();
2807 }
2808}
2809
2810/*
2811 * Remove any popup menu.
2812 */
2813 static void
2814ins_compl_del_pum()
2815{
2816 if (compl_match_array != NULL)
2817 {
2818 pum_undisplay();
2819 vim_free(compl_match_array);
2820 compl_match_array = NULL;
2821 }
2822}
2823
2824/*
2825 * Return TRUE if the popup menu should be displayed.
2826 */
2827 static int
2828pum_wanted()
2829{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002830 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002831 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002832 return FALSE;
2833
2834 /* The display looks bad on a B&W display. */
2835 if (t_colors < 8
2836#ifdef FEAT_GUI
2837 && !gui.in_use
2838#endif
2839 )
2840 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002841 return TRUE;
2842}
2843
2844/*
2845 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002846 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002847 */
2848 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002849pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002850{
2851 compl_T *compl;
2852 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002853
2854 /* Don't display the popup menu if there are no matches or there is only
2855 * one (ignoring the original text). */
2856 compl = compl_first_match;
2857 i = 0;
2858 do
2859 {
2860 if (compl == NULL
2861 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2862 break;
2863 compl = compl->cp_next;
2864 } while (compl != compl_first_match);
2865
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002866 if (strstr((char *)p_cot, "menuone") != NULL)
2867 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002868 return (i >= 2);
2869}
2870
2871/*
2872 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002873 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002875 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002876ins_compl_show_pum()
2877{
2878 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002879 compl_T *shown_compl = NULL;
2880 int did_find_shown_match = FALSE;
2881 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002882 int i;
2883 int cur = -1;
2884 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002885 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002886
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002887 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002888 return;
2889
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002890#if defined(FEAT_EVAL)
2891 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2892 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2893#endif
2894
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002895 /* Update the screen before drawing the popup menu over it. */
2896 update_screen(0);
2897
2898 if (compl_match_array == NULL)
2899 {
2900 /* Need to build the popup menu list. */
2901 compl_match_arraysize = 0;
2902 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002903 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002904 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002905 do
2906 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002907 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2908 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002909 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002910 ++compl_match_arraysize;
2911 compl = compl->cp_next;
2912 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002913 if (compl_match_arraysize == 0)
2914 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002915 compl_match_array = (pumitem_T *)alloc_clear(
2916 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002917 * compl_match_arraysize));
2918 if (compl_match_array != NULL)
2919 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002920 /* If the current match is the original text don't find the first
2921 * match after it, don't highlight anything. */
2922 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2923 shown_match_ok = TRUE;
2924
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002925 i = 0;
2926 compl = compl_first_match;
2927 do
2928 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002929 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2930 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002931 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002932 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002933 if (!shown_match_ok)
2934 {
2935 if (compl == compl_shown_match || did_find_shown_match)
2936 {
2937 /* This item is the shown match or this is the
2938 * first displayed item after the shown match. */
2939 compl_shown_match = compl;
2940 did_find_shown_match = TRUE;
2941 shown_match_ok = TRUE;
2942 }
2943 else
2944 /* Remember this displayed match for when the
2945 * shown match is just below it. */
2946 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002947 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002948 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002949
2950 if (compl->cp_text[CPT_ABBR] != NULL)
2951 compl_match_array[i].pum_text =
2952 compl->cp_text[CPT_ABBR];
2953 else
2954 compl_match_array[i].pum_text = compl->cp_str;
2955 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2956 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2957 if (compl->cp_text[CPT_MENU] != NULL)
2958 compl_match_array[i++].pum_extra =
2959 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002960 else
2961 compl_match_array[i++].pum_extra = compl->cp_fname;
2962 }
2963
2964 if (compl == compl_shown_match)
2965 {
2966 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002967
2968 /* When the original text is the shown match don't set
2969 * compl_shown_match. */
2970 if (compl->cp_flags & ORIGINAL_TEXT)
2971 shown_match_ok = TRUE;
2972
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002973 if (!shown_match_ok && shown_compl != NULL)
2974 {
2975 /* The shown match isn't displayed, set it to the
2976 * previously displayed match. */
2977 compl_shown_match = shown_compl;
2978 shown_match_ok = TRUE;
2979 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002980 }
2981 compl = compl->cp_next;
2982 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002983
2984 if (!shown_match_ok) /* no displayed match at all */
2985 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002986 }
2987 }
2988 else
2989 {
2990 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002991 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002992 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2993 || compl_match_array[i].pum_text
2994 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002995 {
2996 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002997 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002998 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002999 }
3000
3001 if (compl_match_array != NULL)
3002 {
3003 /* Compute the screen column of the start of the completed text.
3004 * Use the cursor to get all wrapping and other settings right. */
3005 col = curwin->w_cursor.col;
3006 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003007 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003008 curwin->w_cursor.col = col;
3009 }
3010}
3011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012#define DICT_FIRST (1) /* use just first element in "dict" */
3013#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003014
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003016 * Add any identifiers that match the given pattern in the list of dictionary
3017 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 */
3019 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00003020ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
3021 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003023 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003024 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003026 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 char_u *ptr;
3028 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003029 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 char_u **files;
3031 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003033 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003034
Bram Moolenaar0b238792006-03-02 22:49:12 +00003035 if (*dict == NUL)
3036 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003037#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003038 /* When 'dictionary' is empty and spell checking is enabled use
3039 * "spell". */
3040 if (!thesaurus && curwin->w_p_spell)
3041 dict = (char_u *)"spell";
3042 else
3043#endif
3044 return;
3045 }
3046
Bram Moolenaar071d4272004-06-13 20:20:40 +00003047 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003048 if (buf == NULL)
3049 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003050 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003051
Bram Moolenaar071d4272004-06-13 20:20:40 +00003052 /* If 'infercase' is set, don't use 'smartcase' here */
3053 save_p_scs = p_scs;
3054 if (curbuf->b_p_inf)
3055 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003056
3057 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3058 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003059 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003060 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3061 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003062 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003063 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003064
3065 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003066 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003067 len = STRLEN(pat_esc) + 10;
3068 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003069 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003070 {
3071 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003072 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003073 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003074 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003075 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3076 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003077 vim_free(ptr);
3078 }
3079 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003080 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003081 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003082 if (regmatch.regprog == NULL)
3083 goto theend;
3084 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003085
Bram Moolenaar071d4272004-06-13 20:20:40 +00003086 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3087 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003088 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 {
3090 /* copy one dictionary file name into buf */
3091 if (flags == DICT_EXACT)
3092 {
3093 count = 1;
3094 files = &dict;
3095 }
3096 else
3097 {
3098 /* Expand wildcards in the dictionary name, but do not allow
3099 * backticks (for security, the 'dict' option may have been set in
3100 * a modeline). */
3101 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003102# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003103 if (!thesaurus && STRCMP(buf, "spell") == 0)
3104 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003105 else
3106# endif
3107 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003108 || expand_wildcards(1, &buf, &count, &files,
3109 EW_FILE|EW_SILENT) != OK)
3110 count = 0;
3111 }
3112
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003113# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003114 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003116 /* Complete from active spelling. Skip "\<" in the pattern, we
3117 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003118 if (pat[0] == '\\' && pat[1] == '<')
3119 ptr = pat + 2;
3120 else
3121 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003122 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003123 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003124 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003125# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003126 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003127 {
3128 ins_compl_files(count, files, thesaurus, flags,
3129 &regmatch, buf, &dir);
3130 if (flags != DICT_EXACT)
3131 FreeWild(count, files);
3132 }
3133 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003134 break;
3135 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003136
3137theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003139 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003140 vim_free(buf);
3141}
3142
Bram Moolenaar0b238792006-03-02 22:49:12 +00003143 static void
3144ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3145 int count;
3146 char_u **files;
3147 int thesaurus;
3148 int flags;
3149 regmatch_T *regmatch;
3150 char_u *buf;
3151 int *dir;
3152{
3153 char_u *ptr;
3154 int i;
3155 FILE *fp;
3156 int add_r;
3157
3158 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3159 {
3160 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3161 if (flags != DICT_EXACT)
3162 {
3163 vim_snprintf((char *)IObuff, IOSIZE,
3164 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003165 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003166 }
3167
3168 if (fp != NULL)
3169 {
3170 /*
3171 * Read dictionary file line by line.
3172 * Check each line for a match.
3173 */
3174 while (!got_int && !compl_interrupted
3175 && !vim_fgets(buf, LSIZE, fp))
3176 {
3177 ptr = buf;
3178 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3179 {
3180 ptr = regmatch->startp[0];
3181 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3182 ptr = find_line_end(ptr);
3183 else
3184 ptr = find_word_end(ptr);
3185 add_r = ins_compl_add_infercase(regmatch->startp[0],
3186 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003187 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003188 if (thesaurus)
3189 {
3190 char_u *wstart;
3191
3192 /*
3193 * Add the other matches on the line
3194 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003195 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003196 while (!got_int)
3197 {
3198 /* Find start of the next word. Skip white
3199 * space and punctuation. */
3200 ptr = find_word_start(ptr);
3201 if (*ptr == NUL || *ptr == NL)
3202 break;
3203 wstart = ptr;
3204
Bram Moolenaara2993e12007-08-12 14:38:46 +00003205 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003206#ifdef FEAT_MBYTE
3207 if (has_mbyte)
3208 /* Japanese words may have characters in
3209 * different classes, only separate words
3210 * with single-byte non-word characters. */
3211 while (*ptr != NUL)
3212 {
3213 int l = (*mb_ptr2len)(ptr);
3214
3215 if (l < 2 && !vim_iswordc(*ptr))
3216 break;
3217 ptr += l;
3218 }
3219 else
3220#endif
3221 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003222
3223 /* Add the word. Skip the regexp match. */
3224 if (wstart != regmatch->startp[0])
3225 add_r = ins_compl_add_infercase(wstart,
3226 (int)(ptr - wstart),
3227 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003228 }
3229 }
3230 if (add_r == OK)
3231 /* if dir was BACKWARD then honor it just once */
3232 *dir = FORWARD;
3233 else if (add_r == FAIL)
3234 break;
3235 /* avoid expensive call to vim_regexec() when at end
3236 * of line */
3237 if (*ptr == '\n' || got_int)
3238 break;
3239 }
3240 line_breakcheck();
3241 ins_compl_check_keys(50);
3242 }
3243 fclose(fp);
3244 }
3245 }
3246}
3247
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248/*
3249 * Find the start of the next word.
3250 * Returns a pointer to the first char of the word. Also stops at a NUL.
3251 */
3252 char_u *
3253find_word_start(ptr)
3254 char_u *ptr;
3255{
3256#ifdef FEAT_MBYTE
3257 if (has_mbyte)
3258 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003259 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 else
3261#endif
3262 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3263 ++ptr;
3264 return ptr;
3265}
3266
3267/*
3268 * Find the end of the word. Assumes it starts inside a word.
3269 * Returns a pointer to just after the word.
3270 */
3271 char_u *
3272find_word_end(ptr)
3273 char_u *ptr;
3274{
3275#ifdef FEAT_MBYTE
3276 int start_class;
3277
3278 if (has_mbyte)
3279 {
3280 start_class = mb_get_class(ptr);
3281 if (start_class > 1)
3282 while (*ptr != NUL)
3283 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003284 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285 if (mb_get_class(ptr) != start_class)
3286 break;
3287 }
3288 }
3289 else
3290#endif
3291 while (vim_iswordc(*ptr))
3292 ++ptr;
3293 return ptr;
3294}
3295
3296/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003297 * Find the end of the line, omitting CR and NL at the end.
3298 * Returns a pointer to just after the line.
3299 */
3300 static char_u *
3301find_line_end(ptr)
3302 char_u *ptr;
3303{
3304 char_u *s;
3305
3306 s = ptr + STRLEN(ptr);
3307 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3308 --s;
3309 return s;
3310}
3311
3312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 * Free the list of completions
3314 */
3315 static void
3316ins_compl_free()
3317{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003318 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003319 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003320
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003321 vim_free(compl_pattern);
3322 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003323 vim_free(compl_leader);
3324 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003326 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003328
3329 ins_compl_del_pum();
3330 pum_clear();
3331
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003332 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 do
3334 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003335 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003336 compl_curr_match = compl_curr_match->cp_next;
3337 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003339 if (match->cp_flags & FREE_FNAME)
3340 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003341 for (i = 0; i < CPT_COUNT; ++i)
3342 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003344 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3345 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003346 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347}
3348
3349 static void
3350ins_compl_clear()
3351{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003352 compl_cont_status = 0;
3353 compl_started = FALSE;
3354 compl_matches = 0;
3355 vim_free(compl_pattern);
3356 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003357 vim_free(compl_leader);
3358 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003359 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003360 vim_free(compl_orig_text);
3361 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003362 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363}
3364
3365/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003366 * Return TRUE when Insert completion is active.
3367 */
3368 int
3369ins_compl_active()
3370{
3371 return compl_started;
3372}
3373
3374/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003375 * Delete one character before the cursor and show the subset of the matches
3376 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003377 * Returns the character to be used, NUL if the work is done and another char
3378 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003379 */
3380 static int
3381ins_compl_bs()
3382{
3383 char_u *line;
3384 char_u *p;
3385
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003386 line = ml_get_curline();
3387 p = line + curwin->w_cursor.col;
3388 mb_ptr_back(line, p);
3389
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003390 /* Stop completion when the whole word was deleted. For Omni completion
3391 * allow the word to be deleted, we won't match everything. */
3392 if ((int)(p - line) - (int)compl_col < 0
3393 || ((int)(p - line) - (int)compl_col == 0
3394 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003395 return K_BS;
3396
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003397 /* Deleted more than what was used to find matches or didn't finish
3398 * finding all matches: need to look for matches all over again. */
3399 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003400 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003401 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003402
Bram Moolenaara6557602006-02-04 22:43:20 +00003403 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003404 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003405 if (compl_leader != NULL)
3406 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003407 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003408 if (compl_shown_match != NULL)
3409 /* Make sure current match is not a hidden item. */
3410 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003411 return NUL;
3412 }
3413 return K_BS;
3414}
Bram Moolenaara6557602006-02-04 22:43:20 +00003415
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003416/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003417 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3418 * be called.
3419 */
3420 static int
3421ins_compl_need_restart()
3422{
3423 /* Return TRUE if we didn't complete finding matches or when the
3424 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3425 return compl_was_interrupted
3426 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3427 && compl_opt_refresh_always);
3428}
3429
3430/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003431 * Called after changing "compl_leader".
3432 * Show the popup menu with a different set of matches.
3433 * May also search for matches again if the previous search was interrupted.
3434 */
3435 static void
3436ins_compl_new_leader()
3437{
3438 ins_compl_del_pum();
3439 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003440 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003441 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003442
3443 if (compl_started)
3444 ins_compl_set_original_text(compl_leader);
3445 else
3446 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003447#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003448 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003449#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003450 /*
3451 * Matches were cleared, need to search for them now. First display
3452 * the changed text before the cursor. Set "compl_restarting" to
3453 * avoid that the first match is inserted.
3454 */
3455 update_screen(0);
3456#ifdef FEAT_GUI
3457 if (gui.in_use)
3458 {
3459 /* Show the cursor after the match, not after the redrawn text. */
3460 setcursor();
3461 out_flush();
3462 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003463 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003464#endif
3465 compl_restarting = TRUE;
3466 if (ins_complete(Ctrl_N) == FAIL)
3467 compl_cont_status = 0;
3468 compl_restarting = FALSE;
3469 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003470
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003471 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003472
3473 /* Show the popup menu with a different set of matches. */
3474 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003475
3476 /* Don't let Enter select the original text when there is no popup menu. */
3477 if (compl_match_array == NULL)
3478 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003479}
3480
3481/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003482 * Return the length of the completion, from the completion start column to
3483 * the cursor column. Making sure it never goes below zero.
3484 */
3485 static int
3486ins_compl_len()
3487{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003488 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003489
3490 if (off < 0)
3491 return 0;
3492 return off;
3493}
3494
3495/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003496 * Append one character to the match leader. May reduce the number of
3497 * matches.
3498 */
3499 static void
3500ins_compl_addleader(c)
3501 int c;
3502{
3503#ifdef FEAT_MBYTE
3504 int cc;
3505
3506 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3507 {
3508 char_u buf[MB_MAXBYTES + 1];
3509
3510 (*mb_char2bytes)(c, buf);
3511 buf[cc] = NUL;
3512 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003513 if (compl_opt_refresh_always)
3514 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003515 }
3516 else
3517#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003518 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003519 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003520 if (compl_opt_refresh_always)
3521 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003522 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003523
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003524 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003525 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003526 ins_compl_restart();
3527
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003528 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003529 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003530 * break redo. */
3531 if (!compl_opt_refresh_always)
3532 {
3533 vim_free(compl_leader);
3534 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003535 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003536 if (compl_leader != NULL)
3537 ins_compl_new_leader();
3538 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003539}
3540
3541/*
3542 * Setup for finding completions again without leaving CTRL-X mode. Used when
3543 * BS or a key was typed while still searching for matches.
3544 */
3545 static void
3546ins_compl_restart()
3547{
3548 ins_compl_free();
3549 compl_started = FALSE;
3550 compl_matches = 0;
3551 compl_cont_status = 0;
3552 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003553}
3554
3555/*
3556 * Set the first match, the original text.
3557 */
3558 static void
3559ins_compl_set_original_text(str)
3560 char_u *str;
3561{
3562 char_u *p;
3563
3564 /* Replace the original text entry. */
3565 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3566 {
3567 p = vim_strsave(str);
3568 if (p != NULL)
3569 {
3570 vim_free(compl_first_match->cp_str);
3571 compl_first_match->cp_str = p;
3572 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003573 }
3574}
3575
3576/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003577 * Append one character to the match leader. May reduce the number of
3578 * matches.
3579 */
3580 static void
3581ins_compl_addfrommatch()
3582{
3583 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003584 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003585 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003586 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003587
3588 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003589 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003590 {
3591 /* When still at the original match use the first entry that matches
3592 * the leader. */
3593 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3594 {
3595 p = NULL;
3596 for (cp = compl_shown_match->cp_next; cp != NULL
3597 && cp != compl_first_match; cp = cp->cp_next)
3598 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003599 if (compl_leader == NULL
3600 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003601 (int)STRLEN(compl_leader)))
3602 {
3603 p = cp->cp_str;
3604 break;
3605 }
3606 }
3607 if (p == NULL || (int)STRLEN(p) <= len)
3608 return;
3609 }
3610 else
3611 return;
3612 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003613 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003614 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003615 ins_compl_addleader(c);
3616}
3617
3618/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003620 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003621 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003623 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624ins_compl_prep(c)
3625 int c;
3626{
3627 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003629 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630
3631 /* Forget any previous 'special' messages if this is actually
3632 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3633 */
3634 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3635 edit_submode_extra = NULL;
3636
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003637 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003638 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3639 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003640 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003642 /* Set "compl_get_longest" when finding the first matches. */
3643 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3644 || (ctrl_x_mode == 0 && !compl_started))
3645 {
3646 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3647 compl_used_match = TRUE;
3648 }
3649
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3651 {
3652 /*
3653 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3654 * it will be yet. Now we decide.
3655 */
3656 switch (c)
3657 {
3658 case Ctrl_E:
3659 case Ctrl_Y:
3660 ctrl_x_mode = CTRL_X_SCROLL;
3661 if (!(State & REPLACE_FLAG))
3662 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3663 else
3664 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3665 edit_submode_pre = NULL;
3666 showmode();
3667 break;
3668 case Ctrl_L:
3669 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3670 break;
3671 case Ctrl_F:
3672 ctrl_x_mode = CTRL_X_FILES;
3673 break;
3674 case Ctrl_K:
3675 ctrl_x_mode = CTRL_X_DICTIONARY;
3676 break;
3677 case Ctrl_R:
3678 /* Simply allow ^R to happen without affecting ^X mode */
3679 break;
3680 case Ctrl_T:
3681 ctrl_x_mode = CTRL_X_THESAURUS;
3682 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003683#ifdef FEAT_COMPL_FUNC
3684 case Ctrl_U:
3685 ctrl_x_mode = CTRL_X_FUNCTION;
3686 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003687 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003688 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003689 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003690#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003691 case 's':
3692 case Ctrl_S:
3693 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003694#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003695 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003696 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003697 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003698#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003699 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 case Ctrl_RSB:
3701 ctrl_x_mode = CTRL_X_TAGS;
3702 break;
3703#ifdef FEAT_FIND_ID
3704 case Ctrl_I:
3705 case K_S_TAB:
3706 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3707 break;
3708 case Ctrl_D:
3709 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3710 break;
3711#endif
3712 case Ctrl_V:
3713 case Ctrl_Q:
3714 ctrl_x_mode = CTRL_X_CMDLINE;
3715 break;
3716 case Ctrl_P:
3717 case Ctrl_N:
3718 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3719 * just started ^X mode, or there were enough ^X's to cancel
3720 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3721 * do normal expansion when interrupting a different mode (say
3722 * ^X^F^X^P or ^P^X^X^P, see below)
3723 * nothing changes if interrupting mode 0, (eg, the flag
3724 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003725 if (!(compl_cont_status & CONT_INTRPT))
3726 compl_cont_status |= CONT_LOCAL;
3727 else if (compl_cont_mode != 0)
3728 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 /* FALLTHROUGH */
3730 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003731 /* If we have typed at least 2 ^X's... for modes != 0, we set
3732 * compl_cont_status = 0 (eg, as if we had just started ^X
3733 * mode).
3734 * For mode 0, we set "compl_cont_mode" to an impossible
3735 * value, in both cases ^X^X can be used to restart the same
3736 * mode (avoiding ADDING mode).
3737 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3738 * 'complete' and local ^P expansions respectively.
3739 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3740 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 if (c == Ctrl_X)
3742 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003743 if (compl_cont_mode != 0)
3744 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003746 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748 ctrl_x_mode = 0;
3749 edit_submode = NULL;
3750 showmode();
3751 break;
3752 }
3753 }
3754 else if (ctrl_x_mode != 0)
3755 {
3756 /* We're already in CTRL-X mode, do we stay in it? */
3757 if (!vim_is_ctrl_x_key(c))
3758 {
3759 if (ctrl_x_mode == CTRL_X_SCROLL)
3760 ctrl_x_mode = 0;
3761 else
3762 ctrl_x_mode = CTRL_X_FINISHED;
3763 edit_submode = NULL;
3764 }
3765 showmode();
3766 }
3767
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003768 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769 {
3770 /* Show error message from attempted keyword completion (probably
3771 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003772 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003773 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003774 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3775 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 || ctrl_x_mode == CTRL_X_FINISHED)
3777 {
3778 /* Get here when we have finished typing a sequence of ^N and
3779 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003780 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003781 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 {
3783 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003784 * If any of the original typed text has been changed, eg when
3785 * ignorecase is set, we must add back-spaces to the redo
3786 * buffer. We add as few as necessary to delete just the part
3787 * of the original text that has changed.
3788 * When using the longest match, edited the match or used
3789 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003791 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3792 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003793 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003794 ptr = NULL;
3795 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 }
3797
3798#ifdef FEAT_CINDENT
3799 want_cindent = (can_cindent && cindent_on());
3800#endif
3801 /*
3802 * When completing whole lines: fix indent for 'cindent'.
3803 * Otherwise, break line if it's too long.
3804 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003805 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 {
3807#ifdef FEAT_CINDENT
3808 /* re-indent the current line */
3809 if (want_cindent)
3810 {
3811 do_c_expr_indent();
3812 want_cindent = FALSE; /* don't do it again */
3813 }
3814#endif
3815 }
3816 else
3817 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003818 int prev_col = curwin->w_cursor.col;
3819
Bram Moolenaar071d4272004-06-13 20:20:40 +00003820 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003821 if (prev_col > 0)
3822 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 if (stop_arrow() == OK)
3824 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003825 if (prev_col > 0
3826 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3827 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003828 }
3829
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003830 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003831 * the selection without inserting anything. When
3832 * compl_enter_selects is set the Enter key does the same. */
3833 if ((c == Ctrl_Y || (compl_enter_selects
3834 && (c == CAR || c == K_KENTER || c == NL)))
3835 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003836 retval = TRUE;
3837
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003838 /* CTRL-E means completion is Ended, go back to the typed text. */
3839 if (c == Ctrl_E)
3840 {
3841 ins_compl_delete();
3842 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003843 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003844 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003845 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003846 retval = TRUE;
3847 }
3848
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003849 auto_format(FALSE, TRUE);
3850
Bram Moolenaar071d4272004-06-13 20:20:40 +00003851 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003852 compl_started = FALSE;
3853 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 msg_clr_cmdline(); /* necessary for "noshowmode" */
3855 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003856 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003857 if (edit_submode != NULL)
3858 {
3859 edit_submode = NULL;
3860 showmode();
3861 }
3862
3863#ifdef FEAT_CINDENT
3864 /*
3865 * Indent now if a key was typed that is in 'cinkeys'.
3866 */
3867 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3868 do_c_expr_indent();
3869#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003870#ifdef FEAT_AUTOCMD
3871 /* Trigger the CompleteDone event to give scripts a chance to act
3872 * upon the completion. */
3873 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 }
3876 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003877#ifdef FEAT_AUTOCMD
3878 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3879 /* Trigger the CompleteDone event to give scripts a chance to act
3880 * upon the (possibly failed) completion. */
3881 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3882#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
3884 /* reset continue_* if we left expansion-mode, if we stay they'll be
3885 * (re)set properly in ins_complete() */
3886 if (!vim_is_ctrl_x_key(c))
3887 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003888 compl_cont_status = 0;
3889 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003890 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003891
3892 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003893}
3894
3895/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003896 * Fix the redo buffer for the completion leader replacing some of the typed
3897 * text. This inserts backspaces and appends the changed text.
3898 * "ptr" is the known leader text or NUL.
3899 */
3900 static void
3901ins_compl_fixRedoBufForLeader(ptr_arg)
3902 char_u *ptr_arg;
3903{
3904 int len;
3905 char_u *p;
3906 char_u *ptr = ptr_arg;
3907
3908 if (ptr == NULL)
3909 {
3910 if (compl_leader != NULL)
3911 ptr = compl_leader;
3912 else
3913 return; /* nothing to do */
3914 }
3915 if (compl_orig_text != NULL)
3916 {
3917 p = compl_orig_text;
3918 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3919 ;
3920#ifdef FEAT_MBYTE
3921 if (len > 0)
3922 len -= (*mb_head_off)(p, p + len);
3923#endif
3924 for (p += len; *p != NUL; mb_ptr_adv(p))
3925 AppendCharToRedobuff(K_BS);
3926 }
3927 else
3928 len = 0;
3929 if (ptr != NULL)
3930 AppendToRedobuffLit(ptr + len, -1);
3931}
3932
3933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3935 * (depending on flag) starting from buf and looking for a non-scanned
3936 * buffer (other than curbuf). curbuf is special, if it is called with
3937 * buf=curbuf then it has to be the first call for a given flag/expansion.
3938 *
3939 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3940 */
3941 static buf_T *
3942ins_compl_next_buf(buf, flag)
3943 buf_T *buf;
3944 int flag;
3945{
3946#ifdef FEAT_WINDOWS
3947 static win_T *wp;
3948#endif
3949
3950 if (flag == 'w') /* just windows */
3951 {
3952#ifdef FEAT_WINDOWS
3953 if (buf == curbuf) /* first call for this flag/expansion */
3954 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003955 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 && wp->w_buffer->b_scanned)
3957 ;
3958 buf = wp->w_buffer;
3959#else
3960 buf = curbuf;
3961#endif
3962 }
3963 else
3964 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3965 * (unlisted buffers)
3966 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003967 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968 && ((flag == 'U'
3969 ? buf->b_p_bl
3970 : (!buf->b_p_bl
3971 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003972 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 ;
3974 return buf;
3975}
3976
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003977#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003978static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003979
3980/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003981 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003982 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003983 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003984 static void
3985expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003986 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003987 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003988{
Bram Moolenaar82139082011-09-14 16:52:09 +02003989 list_T *matchlist = NULL;
3990 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003991 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003992 char_u *funcname;
3993 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003994 win_T *curwin_save;
3995 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02003996 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003997
Bram Moolenaare344bea2005-09-01 20:46:49 +00003998 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3999 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004000 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004001
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004002 /* Call 'completefunc' to obtain the list of matches. */
4003 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004004 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004005
Bram Moolenaare344bea2005-09-01 20:46:49 +00004006 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004007 curwin_save = curwin;
4008 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004009
4010 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004011 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004012 {
4013 switch (rettv.v_type)
4014 {
4015 case VAR_LIST:
4016 matchlist = rettv.vval.v_list;
4017 break;
4018 case VAR_DICT:
4019 matchdict = rettv.vval.v_dict;
4020 break;
4021 default:
4022 /* TODO: Give error message? */
4023 clear_tv(&rettv);
4024 break;
4025 }
4026 }
4027
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004028 if (curwin_save != curwin || curbuf_save != curbuf)
4029 {
4030 EMSG(_(e_complwin));
4031 goto theend;
4032 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004033 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004034 check_cursor();
4035 if (!equalpos(curwin->w_cursor, pos))
4036 {
4037 EMSG(_(e_compldel));
4038 goto theend;
4039 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004040
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004041 if (matchlist != NULL)
4042 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004043 else if (matchdict != NULL)
4044 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004045
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004046theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004047 if (matchdict != NULL)
4048 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004049 if (matchlist != NULL)
4050 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004051}
4052#endif /* FEAT_COMPL_FUNC */
4053
Bram Moolenaar39f05632006-03-19 22:15:26 +00004054#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004055/*
4056 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004057 */
4058 static void
4059ins_compl_add_list(list)
4060 list_T *list;
4061{
4062 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004063 int dir = compl_direction;
4064
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004065 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004066 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004067 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004068 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4069 /* if dir was BACKWARD then honor it just once */
4070 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004071 else if (did_emsg)
4072 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004073 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004074}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004075
4076/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004077 * Add completions from a dict.
4078 */
4079 static void
4080ins_compl_add_dict(dict)
4081 dict_T *dict;
4082{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004083 dictitem_T *di_refresh;
4084 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004085
4086 /* Check for optional "refresh" item. */
4087 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004088 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4089 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004090 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004091 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004092
4093 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4094 compl_opt_refresh_always = TRUE;
4095 }
4096
4097 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004098 di_words = dict_find(dict, (char_u *)"words", 5);
4099 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4100 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004101}
4102
4103/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004104 * Add a match to the list of matches from a typeval_T.
4105 * If the given string is already in the list of completions, then return
4106 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4107 * maybe because alloc() returns NULL, then FAIL is returned.
4108 */
4109 int
4110ins_compl_add_tv(tv, dir)
4111 typval_T *tv;
4112 int dir;
4113{
4114 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004115 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004116 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004117 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004118 char_u *(cptext[CPT_COUNT]);
4119
4120 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4121 {
4122 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4123 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4124 (char_u *)"abbr", FALSE);
4125 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4126 (char_u *)"menu", FALSE);
4127 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4128 (char_u *)"kind", FALSE);
4129 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4130 (char_u *)"info", FALSE);
4131 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4132 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004133 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004134 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004135 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4136 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004137 }
4138 else
4139 {
4140 word = get_tv_string_chk(tv);
4141 vim_memset(cptext, 0, sizeof(cptext));
4142 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004143 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004144 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004145 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004146}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004147#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004149/*
4150 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004151 * The search starts at position "ini" in curbuf and in the direction
4152 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004153 * When "compl_started" is FALSE start at that position, otherwise continue
4154 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004155 * This may return before finding all the matches.
4156 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 */
4158 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004159ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161{
4162 static pos_T first_match_pos;
4163 static pos_T last_match_pos;
4164 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004165 static int found_all = FALSE; /* Found all matches of a
4166 certain type. */
4167 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168
Bram Moolenaar572cb562005-08-05 21:35:02 +00004169 pos_T *pos;
4170 char_u **matches;
4171 int save_p_scs;
4172 int save_p_ws;
4173 int save_p_ic;
4174 int i;
4175 int num_matches;
4176 int len;
4177 int found_new_match;
4178 int type = ctrl_x_mode;
4179 char_u *ptr;
4180 char_u *dict = NULL;
4181 int dict_f = 0;
4182 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004183 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
4187 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4188 ins_buf->b_scanned = 0;
4189 found_all = FALSE;
4190 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004191 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004192 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 last_match_pos = first_match_pos = *ini;
4194 }
4195
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004197 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4199 for (;;)
4200 {
4201 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004202 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004204 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * or if found_all says this entry is done. For ^X^L only use the
4206 * entries from 'complete' that look in loaded buffers. */
4207 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004208 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 {
4210 found_all = FALSE;
4211 while (*e_cpt == ',' || *e_cpt == ' ')
4212 e_cpt++;
4213 if (*e_cpt == '.' && !curbuf->b_scanned)
4214 {
4215 ins_buf = curbuf;
4216 first_match_pos = *ini;
4217 /* So that ^N can match word immediately after cursor */
4218 if (ctrl_x_mode == 0)
4219 dec(&first_match_pos);
4220 last_match_pos = first_match_pos;
4221 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004222
4223 /* Remember the first match so that the loop stops when we
4224 * wrap and come back there a second time. */
4225 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 }
4227 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4228 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4229 {
4230 /* Scan a buffer, but not the current one. */
4231 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4232 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004233 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 first_match_pos.col = last_match_pos.col = 0;
4235 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4236 last_match_pos.lnum = 0;
4237 type = 0;
4238 }
4239 else /* unloaded buffer, scan like dictionary */
4240 {
4241 found_all = TRUE;
4242 if (ins_buf->b_fname == NULL)
4243 continue;
4244 type = CTRL_X_DICTIONARY;
4245 dict = ins_buf->b_fname;
4246 dict_f = DICT_EXACT;
4247 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004248 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 ins_buf->b_fname == NULL
4250 ? buf_spname(ins_buf)
4251 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004252 ? ins_buf->b_fname
4253 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004254 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 }
4256 else if (*e_cpt == NUL)
4257 break;
4258 else
4259 {
4260 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4261 type = -1;
4262 else if (*e_cpt == 'k' || *e_cpt == 's')
4263 {
4264 if (*e_cpt == 'k')
4265 type = CTRL_X_DICTIONARY;
4266 else
4267 type = CTRL_X_THESAURUS;
4268 if (*++e_cpt != ',' && *e_cpt != NUL)
4269 {
4270 dict = e_cpt;
4271 dict_f = DICT_FIRST;
4272 }
4273 }
4274#ifdef FEAT_FIND_ID
4275 else if (*e_cpt == 'i')
4276 type = CTRL_X_PATH_PATTERNS;
4277 else if (*e_cpt == 'd')
4278 type = CTRL_X_PATH_DEFINES;
4279#endif
4280 else if (*e_cpt == ']' || *e_cpt == 't')
4281 {
4282 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004283 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004284 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 else
4287 type = -1;
4288
4289 /* in any case e_cpt is advanced to the next entry */
4290 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4291
4292 found_all = TRUE;
4293 if (type == -1)
4294 continue;
4295 }
4296 }
4297
4298 switch (type)
4299 {
4300 case -1:
4301 break;
4302#ifdef FEAT_FIND_ID
4303 case CTRL_X_PATH_PATTERNS:
4304 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004305 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004306 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004308 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4310 (linenr_T)1, (linenr_T)MAXLNUM);
4311 break;
4312#endif
4313
4314 case CTRL_X_DICTIONARY:
4315 case CTRL_X_THESAURUS:
4316 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004317 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 : (type == CTRL_X_THESAURUS
4319 ? (*curbuf->b_p_tsr == NUL
4320 ? p_tsr
4321 : curbuf->b_p_tsr)
4322 : (*curbuf->b_p_dict == NUL
4323 ? p_dict
4324 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004325 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004326 dict != NULL ? dict_f
4327 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 dict = NULL;
4329 break;
4330
4331 case CTRL_X_TAGS:
4332 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4333 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004334 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004336 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004337 * of matches is found when compl_pattern is empty */
4338 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4340 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4341 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4342 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004343 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
4345 p_ic = save_p_ic;
4346 break;
4347
4348 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004349 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4351 {
4352
4353 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004355 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 }
4357 break;
4358
4359 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004360 if (expand_cmdline(&compl_xp, compl_pattern,
4361 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004363 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 break;
4365
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004366#ifdef FEAT_COMPL_FUNC
4367 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004368 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004369 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004370 break;
4371#endif
4372
Bram Moolenaar488c6512005-08-11 20:09:58 +00004373 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004374#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004375 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004376 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004377 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004378 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004379#endif
4380 break;
4381
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 default: /* normal ^P/^N and ^X^L */
4383 /*
4384 * If 'infercase' is set, don't use 'smartcase' here
4385 */
4386 save_p_scs = p_scs;
4387 if (ins_buf->b_p_inf)
4388 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389
Bram Moolenaar361aa502014-01-23 22:45:58 +01004390 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 * end but never from the middle, thus setting nowrapscan in this
4392 * buffers is a good idea, on the other hand, we always set
4393 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4394 save_p_ws = p_ws;
4395 if (ins_buf != curbuf)
4396 p_ws = FALSE;
4397 else if (*e_cpt == '.')
4398 p_ws = TRUE;
4399 for (;;)
4400 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004401 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004403 ++msg_silent; /* Don't want messages for wrapscan. */
4404
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004405 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4406 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004408 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004410 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004412 found_new_match = searchit(NULL, ins_buf, pos,
4413 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004414 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004415 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004416 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004417 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004419 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004420 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 first_match_pos = *pos;
4422 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004423 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 }
4425 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004426 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004427 found_new_match = FAIL;
4428 if (found_new_match == FAIL)
4429 {
4430 if (ins_buf == curbuf)
4431 found_all = TRUE;
4432 break;
4433 }
4434
4435 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004436 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004437 && ini->lnum == pos->lnum
4438 && ini->col == pos->col)
4439 continue;
4440 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4441 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4442 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004443 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 {
4445 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4446 continue;
4447 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4448 if (!p_paste)
4449 ptr = skipwhite(ptr);
4450 }
4451 len = (int)STRLEN(ptr);
4452 }
4453 else
4454 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455 char_u *tmp_ptr = ptr;
4456
4457 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 /* Skip if already inside a word. */
4461 if (vim_iswordp(tmp_ptr))
4462 continue;
4463 /* Find start of next word. */
4464 tmp_ptr = find_word_start(tmp_ptr);
4465 }
4466 /* Find end of this word. */
4467 tmp_ptr = find_word_end(tmp_ptr);
4468 len = (int)(tmp_ptr - ptr);
4469
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470 if ((compl_cont_status & CONT_ADDING)
4471 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004472 {
4473 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4474 {
4475 /* Try next line, if any. the new word will be
4476 * "join" as if the normal command "J" was used.
4477 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004478 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 * works -- Acevedo */
4480 STRNCPY(IObuff, ptr, len);
4481 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4482 tmp_ptr = ptr = skipwhite(ptr);
4483 /* Find start of next word. */
4484 tmp_ptr = find_word_start(tmp_ptr);
4485 /* Find end of next word. */
4486 tmp_ptr = find_word_end(tmp_ptr);
4487 if (tmp_ptr > ptr)
4488 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004489 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004491 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 IObuff[len++] = ' ';
4493 /* IObuf =~ "\k.* ", thus len >= 2 */
4494 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004495 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 || (vim_strchr(p_cpo, CPO_JOINSP)
4497 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004498 && (IObuff[len - 2] == '?'
4499 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 IObuff[len++] = ' ';
4501 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004502 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503 if (tmp_ptr - ptr >= IOSIZE - len)
4504 tmp_ptr = ptr + IOSIZE - len - 1;
4505 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4506 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004507 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 }
4509 IObuff[len] = NUL;
4510 ptr = IObuff;
4511 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004512 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 continue;
4514 }
4515 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004516 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004517 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004518 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 {
4520 found_new_match = OK;
4521 break;
4522 }
4523 }
4524 p_scs = save_p_scs;
4525 p_ws = save_p_ws;
4526 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004527
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004528 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004529 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004530 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 found_new_match = OK;
4532
4533 /* break the loop for specialized modes (use 'complete' just for the
4534 * generic ctrl_x_mode == 0) or when we've found a new match */
4535 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004536 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004537 {
4538 if (got_int)
4539 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004540 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004541 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004542 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004543
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004544 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4545 || compl_interrupted)
4546 break;
4547 compl_started = TRUE;
4548 }
4549 else
4550 {
4551 /* Mark a buffer scanned when it has been scanned completely */
4552 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4553 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004555 compl_started = FALSE;
4556 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004558 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559
4560 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4561 && *e_cpt == NUL) /* Got to end of 'complete' */
4562 found_new_match = FAIL;
4563
4564 i = -1; /* total of matches, unknown */
4565 if (found_new_match == FAIL
4566 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4567 i = ins_compl_make_cyclic();
4568
4569 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004570 * just been made cyclic then we have to move compl_curr_match to the next
4571 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004572 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4573 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004574 if (compl_curr_match == NULL)
4575 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004576 return i;
4577}
4578
4579/* Delete the old text being completed. */
4580 static void
4581ins_compl_delete()
4582{
4583 int i;
4584
4585 /*
4586 * In insert mode: Delete the typed part.
4587 * In replace mode: Put the old characters back, if any.
4588 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004589 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 backspace_until_column(i);
4591 changed_cline_bef_curs();
4592}
4593
4594/* Insert the new text being completed. */
4595 static void
4596ins_compl_insert()
4597{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004598 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004599 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4600 compl_used_match = FALSE;
4601 else
4602 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603}
4604
4605/*
4606 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004607 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4608 * get more completions. If it is FALSE, then we just do nothing when there
4609 * are no more completions in a given direction. The latter case is used when
4610 * we are still in the middle of finding completions, to allow browsing
4611 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 * Return the total number of matches, or -1 if still unknown -- webb.
4613 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004614 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4615 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 *
4617 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004618 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4619 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 */
4621 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004622ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004624 int count; /* repeat completion this many times; should
4625 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004626 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627{
4628 int num_matches = -1;
4629 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004630 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004631 compl_T *found_compl = NULL;
4632 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004633 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004635 /* When user complete function return -1 for findstart which is next
4636 * time of 'always', compl_shown_match become NULL. */
4637 if (compl_shown_match == NULL)
4638 return -1;
4639
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004640 if (compl_leader != NULL
4641 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004643 /* Set "compl_shown_match" to the actually shown match, it may differ
4644 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004645 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004646 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004647 && compl_shown_match->cp_next != NULL
4648 && compl_shown_match->cp_next != compl_first_match)
4649 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004650
4651 /* If we didn't find it searching forward, and compl_shows_dir is
4652 * backward, find the last match. */
4653 if (compl_shows_dir == BACKWARD
4654 && !ins_compl_equal(compl_shown_match,
4655 compl_leader, (int)STRLEN(compl_leader))
4656 && (compl_shown_match->cp_next == NULL
4657 || compl_shown_match->cp_next == compl_first_match))
4658 {
4659 while (!ins_compl_equal(compl_shown_match,
4660 compl_leader, (int)STRLEN(compl_leader))
4661 && compl_shown_match->cp_prev != NULL
4662 && compl_shown_match->cp_prev != compl_first_match)
4663 compl_shown_match = compl_shown_match->cp_prev;
4664 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004665 }
4666
4667 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004668 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 /* Delete old text to be replaced */
4670 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004671
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004672 /* When finding the longest common text we stick at the original text,
4673 * don't let CTRL-N or CTRL-P move to the first match. */
4674 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4675
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004676 /* When restarting the search don't insert the first match either. */
4677 if (compl_restarting)
4678 {
4679 advance = FALSE;
4680 compl_restarting = FALSE;
4681 }
4682
Bram Moolenaare3226be2005-12-18 22:10:00 +00004683 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4684 * around. */
4685 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004686 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004687 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004689 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004690 found_end = (compl_first_match != NULL
4691 && (compl_shown_match->cp_next == compl_first_match
4692 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004693 }
4694 else if (compl_shows_dir == BACKWARD
4695 && compl_shown_match->cp_prev != NULL)
4696 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004697 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004698 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004699 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 }
4701 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004702 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004703 if (!allow_get_expansion)
4704 {
4705 if (advance)
4706 {
4707 if (compl_shows_dir == BACKWARD)
4708 compl_pending -= todo + 1;
4709 else
4710 compl_pending += todo + 1;
4711 }
4712 return -1;
4713 }
4714
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004715 if (advance)
4716 {
4717 if (compl_shows_dir == BACKWARD)
4718 --compl_pending;
4719 else
4720 ++compl_pending;
4721 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004722
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004723 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004724 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004725
4726 /* handle any pending completions */
4727 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004728 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004729 {
4730 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4731 {
4732 compl_shown_match = compl_shown_match->cp_next;
4733 --compl_pending;
4734 }
4735 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4736 {
4737 compl_shown_match = compl_shown_match->cp_prev;
4738 ++compl_pending;
4739 }
4740 else
4741 break;
4742 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004743 found_end = FALSE;
4744 }
4745 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4746 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004747 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004748 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004749 ++todo;
4750 else
4751 /* Remember a matching item. */
4752 found_compl = compl_shown_match;
4753
4754 /* Stop at the end of the list when we found a usable match. */
4755 if (found_end)
4756 {
4757 if (found_compl != NULL)
4758 {
4759 compl_shown_match = found_compl;
4760 break;
4761 }
4762 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004763 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
4765
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004766 /* Insert the text of the new completion, or the compl_leader. */
4767 if (insert_match)
4768 {
4769 if (!compl_get_longest || compl_used_match)
4770 ins_compl_insert();
4771 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004772 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004773 }
4774 else
4775 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
4777 if (!allow_get_expansion)
4778 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004779 /* may undisplay the popup menu first */
4780 ins_compl_upd_pum();
4781
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004782 /* redraw to show the user what was inserted */
4783 update_screen(0);
4784
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004785 /* display the updated popup menu */
4786 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004787#ifdef FEAT_GUI
4788 if (gui.in_use)
4789 {
4790 /* Show the cursor after the match, not after the redrawn text. */
4791 setcursor();
4792 out_flush();
4793 gui_update_cursor(FALSE, FALSE);
4794 }
4795#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004796
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 /* Delete old text to be replaced, since we're still searching and
4798 * don't want to match ourselves! */
4799 ins_compl_delete();
4800 }
4801
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004802 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004803 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004804 compl_enter_selects = !insert_match && compl_match_array != NULL;
4805
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 /*
4807 * Show the file name for the match (if any)
4808 * Truncate the file name to avoid a wait for return.
4809 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004810 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
4812 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004813 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814 if (i <= 0)
4815 i = 0;
4816 else
4817 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004818 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 msg(IObuff);
4820 redraw_cmdline = FALSE; /* don't overwrite! */
4821 }
4822
4823 return num_matches;
4824}
4825
4826/*
4827 * Call this while finding completions, to check whether the user has hit a key
4828 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004829 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004831 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 */
4833 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004834ins_compl_check_keys(frequency)
4835 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836{
4837 static int count = 0;
4838
4839 int c;
4840
4841 /* Don't check when reading keys from a script. That would break the test
4842 * scripts */
4843 if (using_script())
4844 return;
4845
4846 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004847 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 return;
4849 count = 0;
4850
Bram Moolenaara260a972006-06-23 19:36:29 +00004851 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4852 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 if (c != NUL)
4855 {
4856 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4857 {
4858 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004859 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004860 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4861 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004863 else
4864 {
4865 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004866 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004867 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004868 if (c != K_IGNORE)
4869 {
4870 /* Don't interrupt completion when the character wasn't typed,
4871 * e.g., when doing @q to replay keys. */
4872 if (c != Ctrl_R && KeyTyped)
4873 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004874
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004875 vungetc(c);
4876 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004879 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004880 {
4881 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4882
4883 compl_pending = 0;
4884 (void)ins_compl_next(FALSE, todo, TRUE);
4885 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004886}
4887
4888/*
4889 * Decide the direction of Insert mode complete from the key typed.
4890 * Returns BACKWARD or FORWARD.
4891 */
4892 static int
4893ins_compl_key2dir(c)
4894 int c;
4895{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004896 if (c == Ctrl_P || c == Ctrl_L
4897 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4898 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004899 return BACKWARD;
4900 return FORWARD;
4901}
4902
4903/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004904 * Return TRUE for keys that are used for completion only when the popup menu
4905 * is visible.
4906 */
4907 static int
4908ins_compl_pum_key(c)
4909 int c;
4910{
4911 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004912 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4913 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004914}
4915
4916/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004917 * Decide the number of completions to move forward.
4918 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4919 */
4920 static int
4921ins_compl_key2count(c)
4922 int c;
4923{
4924 int h;
4925
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004926 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004927 {
4928 h = pum_get_height();
4929 if (h > 3)
4930 h -= 2; /* keep some context */
4931 return h;
4932 }
4933 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934}
4935
4936/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004937 * Return TRUE if completion with "c" should insert the match, FALSE if only
4938 * to change the currently selected completion.
4939 */
4940 static int
4941ins_compl_use_match(c)
4942 int c;
4943{
4944 switch (c)
4945 {
4946 case K_UP:
4947 case K_DOWN:
4948 case K_PAGEDOWN:
4949 case K_KPAGEDOWN:
4950 case K_S_DOWN:
4951 case K_PAGEUP:
4952 case K_KPAGEUP:
4953 case K_S_UP:
4954 return FALSE;
4955 }
4956 return TRUE;
4957}
4958
4959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 * Do Insert mode completion.
4961 * Called when character "c" was typed, which has a meaning for completion.
4962 * Returns OK if completion was done, FAIL if something failed (out of mem).
4963 */
4964 static int
4965ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004966 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004968 char_u *line;
4969 int startcol = 0; /* column where searched text starts */
4970 colnr_T curs_col; /* cursor column */
4971 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004972 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973
Bram Moolenaare3226be2005-12-18 22:10:00 +00004974 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004975 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 {
4977 /* First time we hit ^N or ^P (in a row, I mean) */
4978
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 did_ai = FALSE;
4980#ifdef FEAT_SMARTINDENT
4981 did_si = FALSE;
4982 can_si = FALSE;
4983 can_si_back = FALSE;
4984#endif
4985 if (stop_arrow() == FAIL)
4986 return FAIL;
4987
4988 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004989 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004990 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004992 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004993 * "compl_startpos" to the cursor as a pattern to add a new word
4994 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004995 * "compl_startpos" is not in the same line as the cursor then fix it
4996 * (the line has been split because it was longer than 'tw'). if SOL
4997 * is set then skip the previous pattern, a word at the beginning of
4998 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004999 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5000 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
5002 /*
5003 * it is a continued search
5004 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005005 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5007 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5008 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005009 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005011 /* line (probably) wrapped, set compl_startpos to the
5012 * first non_blank in the line, if it is not a wordchar
5013 * include it to get a better pattern, but then we don't
5014 * want the "\\<" prefix, check it bellow */
5015 compl_col = (colnr_T)(skipwhite(line) - line);
5016 compl_startpos.col = compl_col;
5017 compl_startpos.lnum = curwin->w_cursor.lnum;
5018 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 }
5020 else
5021 {
5022 /* S_IPOS was set when we inserted a word that was at the
5023 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005024 * mode but first we need to redefine compl_startpos */
5025 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005027 compl_cont_status |= CONT_SOL;
5028 compl_startpos.col = (colnr_T)(skipwhite(
5029 line + compl_length
5030 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005032 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005034 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005035 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005036 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005038 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005040 compl_cont_status &= ~CONT_SOL;
5041 compl_length = (IOSIZE - MIN_SPACE);
5042 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005044 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5045 if (compl_length < 1)
5046 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
5048 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005049 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005051 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 }
5053 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005054 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005056 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005058 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005060 compl_cont_status = 0;
5061 compl_cont_status |= CONT_N_ADDS;
5062 compl_startpos = curwin->w_cursor;
5063 startcol = (int)curs_col;
5064 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066
5067 /* Work out completion pattern and original text -- webb */
5068 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5069 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005070 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5072 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005073 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005075 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005077 compl_col += ++startcol;
5078 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 }
5080 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005081 compl_pattern = str_foldcase(line + compl_col,
5082 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 compl_pattern = vim_strnsave(line + compl_col,
5085 compl_length);
5086 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 return FAIL;
5088 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005089 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 {
5091 char_u *prefix = (char_u *)"\\<";
5092
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005093 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005095 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005096 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005098 if (!vim_iswordp(line + compl_col)
5099 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 && (
5101#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005104 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105#endif
5106 )))
5107 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005108 STRCPY((char *)compl_pattern, prefix);
5109 (void)quote_meta(compl_pattern + STRLEN(prefix),
5110 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005114 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#endif
5118 )
5119 {
5120 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5122 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005124 compl_col += curs_col;
5125 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 }
5127 else
5128 {
5129#ifdef FEAT_MBYTE
5130 /* Search the point of change class of multibyte character
5131 * or not a word single byte character backward. */
5132 if (has_mbyte)
5133 {
5134 int base_class;
5135 int head_off;
5136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005137 startcol -= (*mb_head_off)(line, line + startcol);
5138 base_class = mb_get_class(line + startcol);
5139 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 head_off = (*mb_head_off)(line, line + startcol);
5142 if (base_class != mb_get_class(line + startcol
5143 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005145 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 }
5147 }
5148 else
5149#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005150 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 compl_col += ++startcol;
5153 compl_length = (int)curs_col - startcol;
5154 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 {
5156 /* Only match word with at least two chars -- webb
5157 * there's no need to call quote_meta,
5158 * alloc(7) is enough -- Acevedo
5159 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 compl_pattern = alloc(7);
5161 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 STRCPY((char *)compl_pattern, "\\<");
5164 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5165 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167 else
5168 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005170 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 STRCPY((char *)compl_pattern, "\\<");
5174 (void)quote_meta(compl_pattern + 2, line + compl_col,
5175 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 }
5177 }
5178 }
5179 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5180 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005181 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005182 compl_length = (int)curs_col - (int)compl_col;
5183 if (compl_length < 0) /* cursor in indent: empty pattern */
5184 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 compl_pattern = str_foldcase(line + compl_col, compl_length,
5187 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5190 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 return FAIL;
5192 }
5193 else if (ctrl_x_mode == CTRL_X_FILES)
5194 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005195 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005196 if (startcol > 0)
5197 {
5198 char_u *p = line + startcol;
5199
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005200 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005201 while (p > line && vim_isfilec(PTR2CHAR(p)))
5202 mb_ptr_back(line, p);
5203 if (p == line && vim_isfilec(PTR2CHAR(p)))
5204 startcol = 0;
5205 else
5206 startcol = (int)(p - line) + 1;
5207 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005208
Bram Moolenaar0300e462013-09-08 16:03:45 +02005209 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005210 compl_length = (int)curs_col - startcol;
5211 compl_pattern = addstar(line + compl_col, compl_length,
5212 EXPAND_FILES);
5213 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 return FAIL;
5215 }
5216 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5217 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005218 compl_pattern = vim_strnsave(line, curs_col);
5219 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005221 set_cmd_context(&compl_xp, compl_pattern,
5222 (int)STRLEN(compl_pattern), curs_col);
5223 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5224 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005225 /* No completion possible, use an empty pattern to get a
5226 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005227 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005228 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005229 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5230 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005232 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005233 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005234#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005235 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005236 * Call user defined function 'completefunc' with "a:findstart"
5237 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005238 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005239 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005240 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005241 char_u *funcname;
5242 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005243 win_T *curwin_save;
5244 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005245
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005246 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005247 * string */
5248 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5249 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5250 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005251 {
5252 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5253 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005254 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005255 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005256
5257 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005258 args[1] = NULL;
5259 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005260 curwin_save = curwin;
5261 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005262 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005263 if (curwin_save != curwin || curbuf_save != curbuf)
5264 {
5265 EMSG(_(e_complwin));
5266 return FAIL;
5267 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005268 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005269 check_cursor();
5270 if (!equalpos(curwin->w_cursor, pos))
5271 {
5272 EMSG(_(e_compldel));
5273 return FAIL;
5274 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005275
Bram Moolenaar6110a002012-01-26 18:58:38 +01005276 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005277 * cancel the complete without an error.
5278 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005279 if (col == -2)
5280 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005281 if (col == -3)
5282 {
5283 ctrl_x_mode = 0;
5284 edit_submode = NULL;
5285 msg_clr_cmdline();
5286 return FAIL;
5287 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005288
Bram Moolenaar82139082011-09-14 16:52:09 +02005289 /*
5290 * Reset extended parameters of completion, when start new
5291 * completion.
5292 */
5293 compl_opt_refresh_always = FALSE;
5294
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005295 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005296 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005297 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005298 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005299 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 /* Setup variables for completion. Need to obtain "line" again,
5302 * it may have become invalid. */
5303 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005304 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5306 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005307#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005308 return FAIL;
5309 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005310 else if (ctrl_x_mode == CTRL_X_SPELL)
5311 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005312#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005313 if (spell_bad_len > 0)
5314 compl_col = curs_col - spell_bad_len;
5315 else
5316 compl_col = spell_word_start(startcol);
5317 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005318 {
5319 compl_length = 0;
5320 compl_col = curs_col;
5321 }
5322 else
5323 {
5324 spell_expand_check_cap(compl_col);
5325 compl_length = (int)curs_col - compl_col;
5326 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005327 /* Need to obtain "line" again, it may have become invalid. */
5328 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005329 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5330 if (compl_pattern == NULL)
5331#endif
5332 return FAIL;
5333 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005334 else
5335 {
5336 EMSG2(_(e_intern2), "ins_complete()");
5337 return FAIL;
5338 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 {
5342 edit_submode_pre = (char_u *)_(" Adding");
5343 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5344 {
5345 /* Insert a new line, keep indentation but ignore 'comments' */
5346#ifdef FEAT_COMMENTS
5347 char_u *old = curbuf->b_p_com;
5348
5349 curbuf->b_p_com = (char_u *)"";
5350#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 compl_startpos.lnum = curwin->w_cursor.lnum;
5352 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 ins_eol('\r');
5354#ifdef FEAT_COMMENTS
5355 curbuf->b_p_com = old;
5356#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005357 compl_length = 0;
5358 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
5360 }
5361 else
5362 {
5363 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 }
5366
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005367 if (compl_cont_status & CONT_LOCAL)
5368 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 else
5370 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5371
Bram Moolenaar62951b12011-09-21 18:23:05 +02005372 /* If any of the original typed text has been changed we need to fix
5373 * the redo buffer. */
5374 ins_compl_fixRedoBufForLeader(NULL);
5375
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005376 /* Always add completion for the original text. */
5377 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005378 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5379 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005380 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005382 vim_free(compl_pattern);
5383 compl_pattern = NULL;
5384 vim_free(compl_orig_text);
5385 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 return FAIL;
5387 }
5388
5389 /* showmode might reset the internal line pointers, so it must
5390 * be called before line = ml_get(), or when this address is no
5391 * longer needed. -- Acevedo.
5392 */
5393 edit_submode_extra = (char_u *)_("-- Searching...");
5394 edit_submode_highl = HLF_COUNT;
5395 showmode();
5396 edit_submode_extra = NULL;
5397 out_flush();
5398 }
5399
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005400 compl_shown_match = compl_curr_match;
5401 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402
5403 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005404 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005405 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005406 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005407 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005409 /* may undisplay the popup menu */
5410 ins_compl_upd_pum();
5411
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005412 if (n > 1) /* all matches have been found */
5413 compl_matches = n;
5414 compl_curr_match = compl_shown_match;
5415 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005416
Bram Moolenaard68071d2006-05-02 22:08:30 +00005417 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5418 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 if (got_int && !global_busy)
5420 {
5421 (void)vgetc();
5422 got_int = FALSE;
5423 }
5424
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005425 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005426 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005428 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5429 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5431 edit_submode_highl = HLF_E;
5432 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5433 * because we couldn't expand anything at first place, but if we used
5434 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5435 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 if ( compl_length > 1
5437 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 || (ctrl_x_mode != 0
5439 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5440 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 }
5443
Bram Moolenaar572cb562005-08-05 21:35:02 +00005444 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005445 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005446 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005447 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
5449 if (edit_submode_extra == NULL)
5450 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005451 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 {
5453 edit_submode_extra = (char_u *)_("Back at original");
5454 edit_submode_highl = HLF_W;
5455 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005456 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 edit_submode_extra = (char_u *)_("Word from other line");
5459 edit_submode_highl = HLF_COUNT;
5460 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005461 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 edit_submode_extra = (char_u *)_("The only match");
5464 edit_submode_highl = HLF_COUNT;
5465 }
5466 else
5467 {
5468 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005469 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005471 int number = 0;
5472 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005473
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005474 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
5476 /* search backwards for the first valid (!= -1) number.
5477 * This should normally succeed already at the first loop
5478 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005479 for (match = compl_curr_match->cp_prev; match != NULL
5480 && match != compl_first_match;
5481 match = match->cp_prev)
5482 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005484 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005485 break;
5486 }
5487 if (match != NULL)
5488 /* go up and assign all numbers which are not assigned
5489 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005490 for (match = match->cp_next;
5491 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005492 match = match->cp_next)
5493 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005494 }
5495 else /* BACKWARD */
5496 {
5497 /* search forwards (upwards) for the first valid (!= -1)
5498 * number. This should normally succeed already at the
5499 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005500 for (match = compl_curr_match->cp_next; match != NULL
5501 && match != compl_first_match;
5502 match = match->cp_next)
5503 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005505 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 break;
5507 }
5508 if (match != NULL)
5509 /* go down and assign all numbers which are not
5510 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005511 for (match = match->cp_prev; match
5512 && match->cp_number == -1;
5513 match = match->cp_prev)
5514 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 }
5516 }
5517
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005518 /* The match should always have a sequence number now, this is
5519 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005520 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005521 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005522 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5523 * Translations may need more than twice that. */
5524 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005526 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005527 vim_snprintf((char *)match_ref, sizeof(match_ref),
5528 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005529 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005531 vim_snprintf((char *)match_ref, sizeof(match_ref),
5532 _("match %d"),
5533 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534 edit_submode_extra = match_ref;
5535 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005536 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005537 curs_columns(FALSE);
5538 }
5539 }
5540 }
5541
5542 /* Show a message about what (completion) mode we're in. */
5543 showmode();
5544 if (edit_submode_extra != NULL)
5545 {
5546 if (!p_smd)
5547 msg_attr(edit_submode_extra,
5548 edit_submode_highl < HLF_COUNT
5549 ? hl_attr(edit_submode_highl) : 0);
5550 }
5551 else
5552 msg_clr_cmdline(); /* necessary for "noshowmode" */
5553
Bram Moolenaard68071d2006-05-02 22:08:30 +00005554 /* Show the popup menu, unless we got interrupted. */
5555 if (!compl_interrupted)
5556 {
5557 /* RedrawingDisabled may be set when invoked through complete(). */
5558 n = RedrawingDisabled;
5559 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005560
5561 /* If the cursor moved we need to remove the pum first. */
5562 setcursor();
5563 if (save_w_wrow != curwin->w_wrow)
5564 ins_compl_del_pum();
5565
Bram Moolenaard68071d2006-05-02 22:08:30 +00005566 ins_compl_show_pum();
5567 setcursor();
5568 RedrawingDisabled = n;
5569 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005570 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005571 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005572
Bram Moolenaar071d4272004-06-13 20:20:40 +00005573 return OK;
5574}
5575
5576/*
5577 * Looks in the first "len" chars. of "src" for search-metachars.
5578 * If dest is not NULL the chars. are copied there quoting (with
5579 * a backslash) the metachars, and dest would be NUL terminated.
5580 * Returns the length (needed) of dest
5581 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005582 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583quote_meta(dest, src, len)
5584 char_u *dest;
5585 char_u *src;
5586 int len;
5587{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005588 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005590 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591 {
5592 switch (*src)
5593 {
5594 case '.':
5595 case '*':
5596 case '[':
5597 if (ctrl_x_mode == CTRL_X_DICTIONARY
5598 || ctrl_x_mode == CTRL_X_THESAURUS)
5599 break;
5600 case '~':
5601 if (!p_magic) /* quote these only if magic is set */
5602 break;
5603 case '\\':
5604 if (ctrl_x_mode == CTRL_X_DICTIONARY
5605 || ctrl_x_mode == CTRL_X_THESAURUS)
5606 break;
5607 case '^': /* currently it's not needed. */
5608 case '$':
5609 m++;
5610 if (dest != NULL)
5611 *dest++ = '\\';
5612 break;
5613 }
5614 if (dest != NULL)
5615 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005616# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005617 /* Copy remaining bytes of a multibyte character. */
5618 if (has_mbyte)
5619 {
5620 int i, mb_len;
5621
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005622 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005623 if (mb_len > 0 && len >= mb_len)
5624 for (i = 0; i < mb_len; ++i)
5625 {
5626 --len;
5627 ++src;
5628 if (dest != NULL)
5629 *dest++ = *src;
5630 }
5631 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005633 }
5634 if (dest != NULL)
5635 *dest = NUL;
5636
5637 return m;
5638}
5639#endif /* FEAT_INS_EXPAND */
5640
5641/*
5642 * Next character is interpreted literally.
5643 * A one, two or three digit decimal number is interpreted as its byte value.
5644 * If one or two digits are entered, the next character is given to vungetc().
5645 * For Unicode a character > 255 may be returned.
5646 */
5647 int
5648get_literal()
5649{
5650 int cc;
5651 int nc;
5652 int i;
5653 int hex = FALSE;
5654 int octal = FALSE;
5655#ifdef FEAT_MBYTE
5656 int unicode = 0;
5657#endif
5658
5659 if (got_int)
5660 return Ctrl_C;
5661
5662#ifdef FEAT_GUI
5663 /*
5664 * In GUI there is no point inserting the internal code for a special key.
5665 * It is more useful to insert the string "<KEY>" instead. This would
5666 * probably be useful in a text window too, but it would not be
5667 * vi-compatible (maybe there should be an option for it?) -- webb
5668 */
5669 if (gui.in_use)
5670 ++allow_keys;
5671#endif
5672#ifdef USE_ON_FLY_SCROLL
5673 dont_scroll = TRUE; /* disallow scrolling here */
5674#endif
5675 ++no_mapping; /* don't map the next key hits */
5676 cc = 0;
5677 i = 0;
5678 for (;;)
5679 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005680 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681#ifdef FEAT_CMDL_INFO
5682 if (!(State & CMDLINE)
5683# ifdef FEAT_MBYTE
5684 && MB_BYTE2LEN_CHECK(nc) == 1
5685# endif
5686 )
5687 add_to_showcmd(nc);
5688#endif
5689 if (nc == 'x' || nc == 'X')
5690 hex = TRUE;
5691 else if (nc == 'o' || nc == 'O')
5692 octal = TRUE;
5693#ifdef FEAT_MBYTE
5694 else if (nc == 'u' || nc == 'U')
5695 unicode = nc;
5696#endif
5697 else
5698 {
5699 if (hex
5700#ifdef FEAT_MBYTE
5701 || unicode != 0
5702#endif
5703 )
5704 {
5705 if (!vim_isxdigit(nc))
5706 break;
5707 cc = cc * 16 + hex2nr(nc);
5708 }
5709 else if (octal)
5710 {
5711 if (nc < '0' || nc > '7')
5712 break;
5713 cc = cc * 8 + nc - '0';
5714 }
5715 else
5716 {
5717 if (!VIM_ISDIGIT(nc))
5718 break;
5719 cc = cc * 10 + nc - '0';
5720 }
5721
5722 ++i;
5723 }
5724
5725 if (cc > 255
5726#ifdef FEAT_MBYTE
5727 && unicode == 0
5728#endif
5729 )
5730 cc = 255; /* limit range to 0-255 */
5731 nc = 0;
5732
5733 if (hex) /* hex: up to two chars */
5734 {
5735 if (i >= 2)
5736 break;
5737 }
5738#ifdef FEAT_MBYTE
5739 else if (unicode) /* Unicode: up to four or eight chars */
5740 {
5741 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5742 break;
5743 }
5744#endif
5745 else if (i >= 3) /* decimal or octal: up to three chars */
5746 break;
5747 }
5748 if (i == 0) /* no number entered */
5749 {
5750 if (nc == K_ZERO) /* NUL is stored as NL */
5751 {
5752 cc = '\n';
5753 nc = 0;
5754 }
5755 else
5756 {
5757 cc = nc;
5758 nc = 0;
5759 }
5760 }
5761
5762 if (cc == 0) /* NUL is stored as NL */
5763 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005764#ifdef FEAT_MBYTE
5765 if (enc_dbcs && (cc & 0xff) == 0)
5766 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5767 second byte will cause trouble! */
5768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005769
5770 --no_mapping;
5771#ifdef FEAT_GUI
5772 if (gui.in_use)
5773 --allow_keys;
5774#endif
5775 if (nc)
5776 vungetc(nc);
5777 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5778 return cc;
5779}
5780
5781/*
5782 * Insert character, taking care of special keys and mod_mask
5783 */
5784 static void
5785insert_special(c, allow_modmask, ctrlv)
5786 int c;
5787 int allow_modmask;
5788 int ctrlv; /* c was typed after CTRL-V */
5789{
5790 char_u *p;
5791 int len;
5792
5793 /*
5794 * Special function key, translate into "<Key>". Up to the last '>' is
5795 * inserted with ins_str(), so as not to replace characters in replace
5796 * mode.
5797 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5798 * unless 'allow_modmask' is TRUE.
5799 */
5800#ifdef MACOS
5801 /* Command-key never produces a normal key */
5802 if (mod_mask & MOD_MASK_CMD)
5803 allow_modmask = TRUE;
5804#endif
5805 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5806 {
5807 p = get_special_key_name(c, mod_mask);
5808 len = (int)STRLEN(p);
5809 c = p[len - 1];
5810 if (len > 2)
5811 {
5812 if (stop_arrow() == FAIL)
5813 return;
5814 p[len - 1] = NUL;
5815 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005816 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 ctrlv = FALSE;
5818 }
5819 }
5820 if (stop_arrow() == OK)
5821 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5822}
5823
5824/*
5825 * Special characters in this context are those that need processing other
5826 * than the simple insertion that can be performed here. This includes ESC
5827 * which terminates the insert, and CR/NL which need special processing to
5828 * open up a new line. This routine tries to optimize insertions performed by
5829 * the "redo", "undo" or "put" commands, so it needs to know when it should
5830 * stop and defer processing to the "normal" mechanism.
5831 * '0' and '^' are special, because they can be followed by CTRL-D.
5832 */
5833#ifdef EBCDIC
5834# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5835#else
5836# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5837#endif
5838
5839#ifdef FEAT_MBYTE
5840# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5841#else
5842# define WHITECHAR(cc) vim_iswhite(cc)
5843#endif
5844
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005845/*
5846 * "flags": INSCHAR_FORMAT - force formatting
5847 * INSCHAR_CTRLV - char typed just after CTRL-V
5848 * INSCHAR_NO_FEX - don't use 'formatexpr'
5849 *
5850 * NOTE: passes the flags value straight through to internal_format() which,
5851 * beside INSCHAR_FORMAT (above), is also looking for these:
5852 * INSCHAR_DO_COM - format comments
5853 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5854 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 void
5856insertchar(c, flags, second_indent)
5857 int c; /* character to insert or NUL */
5858 int flags; /* INSCHAR_FORMAT, etc. */
5859 int second_indent; /* indent for second line if >= 0 */
5860{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 int textwidth;
5862#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866
5867 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5868 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869
5870 /*
5871 * Try to break the line in two or more pieces when:
5872 * - Always do this if we have been called to do formatting only.
5873 * - Always do this when 'formatoptions' has the 'a' flag and the line
5874 * ends in white space.
5875 * - Otherwise:
5876 * - Don't do this if inserting a blank
5877 * - Don't do this if an existing character is being replaced, unless
5878 * we're in VREPLACE mode.
5879 * - Do this if the cursor is not on the line where insert started
5880 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5881 * before the insert.
5882 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5883 * before 'textwidth'
5884 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005885 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005886 && ((flags & INSCHAR_FORMAT)
5887 || (!vim_iswhite(c)
5888 && !((State & REPLACE_FLAG)
5889#ifdef FEAT_VREPLACE
5890 && !(State & VREPLACE_FLAG)
5891#endif
5892 && *ml_get_cursor() != NUL)
5893 && (curwin->w_cursor.lnum != Insstart.lnum
5894 || ((!has_format_option(FO_INS_LONG)
5895 || Insstart_textlen <= (colnr_T)textwidth)
5896 && (!fo_ins_blank
5897 || Insstart_blank_vcol <= (colnr_T)textwidth
5898 ))))))
5899 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005900 /* Format with 'formatexpr' when it's set. Use internal formatting
5901 * when 'formatexpr' isn't set or it returns non-zero. */
5902#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005903 int do_internal = TRUE;
5904
Bram Moolenaar81a82092008-03-12 16:27:00 +00005905 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005906 {
5907 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5908 /* It may be required to save for undo again, e.g. when setline()
5909 * was called. */
5910 ins_need_undo = TRUE;
5911 }
5912 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005913#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005914 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005915 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005916
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 if (c == NUL) /* only formatting was wanted */
5918 return;
5919
5920#ifdef FEAT_COMMENTS
5921 /* Check whether this character should end a comment. */
5922 if (did_ai && (int)c == end_comment_pending)
5923 {
5924 char_u *line;
5925 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5926 int middle_len, end_len;
5927 int i;
5928
5929 /*
5930 * Need to remove existing (middle) comment leader and insert end
5931 * comment leader. First, check what comment leader we can find.
5932 */
Bram Moolenaar81340392012-06-06 16:12:59 +02005933 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5935 {
5936 /* Skip middle-comment string */
5937 while (*p && p[-1] != ':') /* find end of middle flags */
5938 ++p;
5939 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5940 /* Don't count trailing white space for middle_len */
5941 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5942 --middle_len;
5943
5944 /* Find the end-comment string */
5945 while (*p && p[-1] != ':') /* find end of end flags */
5946 ++p;
5947 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5948
5949 /* Skip white space before the cursor */
5950 i = curwin->w_cursor.col;
5951 while (--i >= 0 && vim_iswhite(line[i]))
5952 ;
5953 i++;
5954
5955 /* Skip to before the middle leader */
5956 i -= middle_len;
5957
5958 /* Check some expected things before we go on */
5959 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5960 {
5961 /* Backspace over all the stuff we want to replace */
5962 backspace_until_column(i);
5963
5964 /*
5965 * Insert the end-comment string, except for the last
5966 * character, which will get inserted as normal later.
5967 */
5968 ins_bytes_len(lead_end, end_len - 1);
5969 }
5970 }
5971 }
5972 end_comment_pending = NUL;
5973#endif
5974
5975 did_ai = FALSE;
5976#ifdef FEAT_SMARTINDENT
5977 did_si = FALSE;
5978 can_si = FALSE;
5979 can_si_back = FALSE;
5980#endif
5981
5982 /*
5983 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5984 * This speeds up normal text input considerably.
5985 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5986 * need to re-indent at a ':', or any other character (but not what
5987 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005988 * Don't do this when there an InsertCharPre autocommand is defined,
5989 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 */
5991#ifdef USE_ON_FLY_SCROLL
5992 dont_scroll = FALSE; /* allow scrolling here */
5993#endif
5994
5995 if ( !ISSPECIAL(c)
5996#ifdef FEAT_MBYTE
5997 && (!has_mbyte || (*mb_char2len)(c) == 1)
5998#endif
5999 && vpeekc() != NUL
6000 && !(State & REPLACE_FLAG)
6001#ifdef FEAT_CINDENT
6002 && !cindent_on()
6003#endif
6004#ifdef FEAT_RIGHTLEFT
6005 && !p_ri
6006#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006007#ifdef FEAT_AUTOCMD
6008 && !has_insertcharpre()
6009#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006010 )
6011 {
6012#define INPUT_BUFLEN 100
6013 char_u buf[INPUT_BUFLEN + 1];
6014 int i;
6015 colnr_T virtcol = 0;
6016
6017 buf[0] = c;
6018 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006019 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006020 virtcol = get_nolist_virtcol();
6021 /*
6022 * Stop the string when:
6023 * - no more chars available
6024 * - finding a special character (command key)
6025 * - buffer is full
6026 * - running into the 'textwidth' boundary
6027 * - need to check for abbreviation: A non-word char after a word-char
6028 */
6029 while ( (c = vpeekc()) != NUL
6030 && !ISSPECIAL(c)
6031#ifdef FEAT_MBYTE
6032 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6033#endif
6034 && i < INPUT_BUFLEN
6035 && (textwidth == 0
6036 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6037 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6038 {
6039#ifdef FEAT_RIGHTLEFT
6040 c = vgetc();
6041 if (p_hkmap && KeyTyped)
6042 c = hkmap(c); /* Hebrew mode mapping */
6043# ifdef FEAT_FKMAP
6044 if (p_fkmap && KeyTyped)
6045 c = fkmap(c); /* Farsi mode mapping */
6046# endif
6047 buf[i++] = c;
6048#else
6049 buf[i++] = vgetc();
6050#endif
6051 }
6052
6053#ifdef FEAT_DIGRAPHS
6054 do_digraph(-1); /* clear digraphs */
6055 do_digraph(buf[i-1]); /* may be the start of a digraph */
6056#endif
6057 buf[i] = NUL;
6058 ins_str(buf);
6059 if (flags & INSCHAR_CTRLV)
6060 {
6061 redo_literal(*buf);
6062 i = 1;
6063 }
6064 else
6065 i = 0;
6066 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006067 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006068 }
6069 else
6070 {
6071#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006072 int cc;
6073
Bram Moolenaar071d4272004-06-13 20:20:40 +00006074 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6075 {
6076 char_u buf[MB_MAXBYTES + 1];
6077
6078 (*mb_char2bytes)(c, buf);
6079 buf[cc] = NUL;
6080 ins_char_bytes(buf, cc);
6081 AppendCharToRedobuff(c);
6082 }
6083 else
6084#endif
6085 {
6086 ins_char(c);
6087 if (flags & INSCHAR_CTRLV)
6088 redo_literal(c);
6089 else
6090 AppendCharToRedobuff(c);
6091 }
6092 }
6093}
6094
6095/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006096 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006097 *
6098 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6099 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006100 */
6101 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006102internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006103 int textwidth;
6104 int second_indent;
6105 int flags;
6106 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006107 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006108{
6109 int cc;
6110 int save_char = NUL;
6111 int haveto_redraw = FALSE;
6112 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6113#ifdef FEAT_MBYTE
6114 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6115#endif
6116 int fo_white_par = has_format_option(FO_WHITE_PAR);
6117 int first_line = TRUE;
6118#ifdef FEAT_COMMENTS
6119 colnr_T leader_len;
6120 int no_leader = FALSE;
6121 int do_comments = (flags & INSCHAR_DO_COM);
6122#endif
6123
6124 /*
6125 * When 'ai' is off we don't want a space under the cursor to be
6126 * deleted. Replace it with an 'x' temporarily.
6127 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006128 if (!curbuf->b_p_ai
6129#ifdef FEAT_VREPLACE
6130 && !(State & VREPLACE_FLAG)
6131#endif
6132 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006133 {
6134 cc = gchar_cursor();
6135 if (vim_iswhite(cc))
6136 {
6137 save_char = cc;
6138 pchar_cursor('x');
6139 }
6140 }
6141
6142 /*
6143 * Repeat breaking lines, until the current line is not too long.
6144 */
6145 while (!got_int)
6146 {
6147 int startcol; /* Cursor column at entry */
6148 int wantcol; /* column at textwidth border */
6149 int foundcol; /* column for start of spaces */
6150 int end_foundcol = 0; /* column for start of word */
6151 colnr_T len;
6152 colnr_T virtcol;
6153#ifdef FEAT_VREPLACE
6154 int orig_col = 0;
6155 char_u *saved_text = NULL;
6156#endif
6157 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006158 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006159
Bram Moolenaar97b98102009-11-17 16:41:01 +00006160 virtcol = get_nolist_virtcol()
6161 + char2cells(c != NUL ? c : gchar_cursor());
6162 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006163 break;
6164
6165#ifdef FEAT_COMMENTS
6166 if (no_leader)
6167 do_comments = FALSE;
6168 else if (!(flags & INSCHAR_FORMAT)
6169 && has_format_option(FO_WRAP_COMS))
6170 do_comments = TRUE;
6171
6172 /* Don't break until after the comment leader */
6173 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006174 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006175 else
6176 leader_len = 0;
6177
6178 /* If the line doesn't start with a comment leader, then don't
6179 * start one in a following broken line. Avoids that a %word
6180 * moved to the start of the next line causes all following lines
6181 * to start with %. */
6182 if (leader_len == 0)
6183 no_leader = TRUE;
6184#endif
6185 if (!(flags & INSCHAR_FORMAT)
6186#ifdef FEAT_COMMENTS
6187 && leader_len == 0
6188#endif
6189 && !has_format_option(FO_WRAP))
6190
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006191 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006192 if ((startcol = curwin->w_cursor.col) == 0)
6193 break;
6194
6195 /* find column of textwidth border */
6196 coladvance((colnr_T)textwidth);
6197 wantcol = curwin->w_cursor.col;
6198
Bram Moolenaar97b98102009-11-17 16:41:01 +00006199 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006200 foundcol = 0;
6201
6202 /*
6203 * Find position to break at.
6204 * Stop at first entered white when 'formatoptions' has 'v'
6205 */
6206 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006207 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006208 || curwin->w_cursor.lnum != Insstart.lnum
6209 || curwin->w_cursor.col >= Insstart.col)
6210 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006211 if (curwin->w_cursor.col == startcol && c != NUL)
6212 cc = c;
6213 else
6214 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006215 if (WHITECHAR(cc))
6216 {
6217 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006218 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006219
6220 /* find start of sequence of blanks */
6221 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6222 {
6223 dec_cursor();
6224 cc = gchar_cursor();
6225 }
6226 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6227 break; /* only spaces in front of text */
6228#ifdef FEAT_COMMENTS
6229 /* Don't break until after the comment leader */
6230 if (curwin->w_cursor.col < leader_len)
6231 break;
6232#endif
6233 if (has_format_option(FO_ONE_LETTER))
6234 {
6235 /* do not break after one-letter words */
6236 if (curwin->w_cursor.col == 0)
6237 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006238#ifdef FEAT_COMMENTS
6239 /* do not break "#a b" when 'tw' is 2 */
6240 if (curwin->w_cursor.col <= leader_len)
6241 break;
6242#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006243 col = curwin->w_cursor.col;
6244 dec_cursor();
6245 cc = gchar_cursor();
6246
6247 if (WHITECHAR(cc))
6248 continue; /* one-letter, continue */
6249 curwin->w_cursor.col = col;
6250 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006251
6252 inc_cursor();
6253
6254 end_foundcol = end_col + 1;
6255 foundcol = curwin->w_cursor.col;
6256 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006257 break;
6258 }
6259#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006260 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006261 {
6262 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006263 if (curwin->w_cursor.col != startcol)
6264 {
6265#ifdef FEAT_COMMENTS
6266 /* Don't break until after the comment leader */
6267 if (curwin->w_cursor.col < leader_len)
6268 break;
6269#endif
6270 col = curwin->w_cursor.col;
6271 inc_cursor();
6272 /* Don't change end_foundcol if already set. */
6273 if (foundcol != curwin->w_cursor.col)
6274 {
6275 foundcol = curwin->w_cursor.col;
6276 end_foundcol = foundcol;
6277 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6278 break;
6279 }
6280 curwin->w_cursor.col = col;
6281 }
6282
6283 if (curwin->w_cursor.col == 0)
6284 break;
6285
6286 col = curwin->w_cursor.col;
6287
6288 dec_cursor();
6289 cc = gchar_cursor();
6290
6291 if (WHITECHAR(cc))
6292 continue; /* break with space */
6293#ifdef FEAT_COMMENTS
6294 /* Don't break until after the comment leader */
6295 if (curwin->w_cursor.col < leader_len)
6296 break;
6297#endif
6298
6299 curwin->w_cursor.col = col;
6300
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006301 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006302 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006303 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6304 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006305 }
6306#endif
6307 if (curwin->w_cursor.col == 0)
6308 break;
6309 dec_cursor();
6310 }
6311
6312 if (foundcol == 0) /* no spaces, cannot break line */
6313 {
6314 curwin->w_cursor.col = startcol;
6315 break;
6316 }
6317
6318 /* Going to break the line, remove any "$" now. */
6319 undisplay_dollar();
6320
6321 /*
6322 * Offset between cursor position and line break is used by replace
6323 * stack functions. VREPLACE does not use this, and backspaces
6324 * over the text instead.
6325 */
6326#ifdef FEAT_VREPLACE
6327 if (State & VREPLACE_FLAG)
6328 orig_col = startcol; /* Will start backspacing from here */
6329 else
6330#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006331 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006332
6333 /*
6334 * adjust startcol for spaces that will be deleted and
6335 * characters that will remain on top line
6336 */
6337 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006338 while ((cc = gchar_cursor(), WHITECHAR(cc))
6339 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006340 inc_cursor();
6341 startcol -= curwin->w_cursor.col;
6342 if (startcol < 0)
6343 startcol = 0;
6344
6345#ifdef FEAT_VREPLACE
6346 if (State & VREPLACE_FLAG)
6347 {
6348 /*
6349 * In VREPLACE mode, we will backspace over the text to be
6350 * wrapped, so save a copy now to put on the next line.
6351 */
6352 saved_text = vim_strsave(ml_get_cursor());
6353 curwin->w_cursor.col = orig_col;
6354 if (saved_text == NULL)
6355 break; /* Can't do it, out of memory */
6356 saved_text[startcol] = NUL;
6357
6358 /* Backspace over characters that will move to the next line */
6359 if (!fo_white_par)
6360 backspace_until_column(foundcol);
6361 }
6362 else
6363#endif
6364 {
6365 /* put cursor after pos. to break line */
6366 if (!fo_white_par)
6367 curwin->w_cursor.col = foundcol;
6368 }
6369
6370 /*
6371 * Split the line just before the margin.
6372 * Only insert/delete lines, but don't really redraw the window.
6373 */
6374 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6375 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6376#ifdef FEAT_COMMENTS
6377 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006378 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006379#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006380 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6381 if (!(flags & INSCHAR_COM_LIST))
6382 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006383
6384 replace_offset = 0;
6385 if (first_line)
6386 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006387 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006388 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006389 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006390 * This section is for auto-wrap of numeric lists. When not
6391 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6392 * flag will be set and open_line() will handle it (as seen
6393 * above). The code here (and in get_number_indent()) will
6394 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006395 */
6396 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006397 second_indent =
6398 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006399 if (second_indent >= 0)
6400 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006401#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006402 if (State & VREPLACE_FLAG)
6403 change_indent(INDENT_SET, second_indent,
6404 FALSE, NUL, TRUE);
6405 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006406#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006407#ifdef FEAT_COMMENTS
6408 if (leader_len > 0 && second_indent - leader_len > 0)
6409 {
6410 int i;
6411 int padding = second_indent - leader_len;
6412
6413 /* We started at the first_line of a numbered list
6414 * that has a comment. the open_line() function has
6415 * inserted the proper comment leader and positioned
6416 * the cursor at the end of the split line. Now we
6417 * add the additional whitespace needed after the
6418 * comment leader for the numbered list. */
6419 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006420 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006421 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006422 }
6423 else
6424 {
6425#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006426 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006427#ifdef FEAT_COMMENTS
6428 }
6429#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006430 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006431 }
6432 first_line = FALSE;
6433 }
6434
6435#ifdef FEAT_VREPLACE
6436 if (State & VREPLACE_FLAG)
6437 {
6438 /*
6439 * In VREPLACE mode we have backspaced over the text to be
6440 * moved, now we re-insert it into the new line.
6441 */
6442 ins_bytes(saved_text);
6443 vim_free(saved_text);
6444 }
6445 else
6446#endif
6447 {
6448 /*
6449 * Check if cursor is not past the NUL off the line, cindent
6450 * may have added or removed indent.
6451 */
6452 curwin->w_cursor.col += startcol;
6453 len = (colnr_T)STRLEN(ml_get_curline());
6454 if (curwin->w_cursor.col > len)
6455 curwin->w_cursor.col = len;
6456 }
6457
6458 haveto_redraw = TRUE;
6459#ifdef FEAT_CINDENT
6460 can_cindent = TRUE;
6461#endif
6462 /* moved the cursor, don't autoindent or cindent now */
6463 did_ai = FALSE;
6464#ifdef FEAT_SMARTINDENT
6465 did_si = FALSE;
6466 can_si = FALSE;
6467 can_si_back = FALSE;
6468#endif
6469 line_breakcheck();
6470 }
6471
6472 if (save_char != NUL) /* put back space after cursor */
6473 pchar_cursor(save_char);
6474
6475 if (!format_only && haveto_redraw)
6476 {
6477 update_topline();
6478 redraw_curbuf_later(VALID);
6479 }
6480}
6481
6482/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006483 * Called after inserting or deleting text: When 'formatoptions' includes the
6484 * 'a' flag format from the current line until the end of the paragraph.
6485 * Keep the cursor at the same position relative to the text.
6486 * The caller must have saved the cursor line for undo, following ones will be
6487 * saved here.
6488 */
6489 void
6490auto_format(trailblank, prev_line)
6491 int trailblank; /* when TRUE also format with trailing blank */
6492 int prev_line; /* may start in previous line */
6493{
6494 pos_T pos;
6495 colnr_T len;
6496 char_u *old;
6497 char_u *new, *pnew;
6498 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006499 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006500
6501 if (!has_format_option(FO_AUTO))
6502 return;
6503
6504 pos = curwin->w_cursor;
6505 old = ml_get_curline();
6506
6507 /* may remove added space */
6508 check_auto_format(FALSE);
6509
6510 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6511 * user might insert normal text next. Also skip formatting when "1" is
6512 * in 'formatoptions' and there is a single character before the cursor.
6513 * Otherwise the line would be broken and when typing another non-white
6514 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006515 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516 if (*old != NUL && !trailblank && wasatend)
6517 {
6518 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006519 cc = gchar_cursor();
6520 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6521 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006523 cc = gchar_cursor();
6524 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525 {
6526 curwin->w_cursor = pos;
6527 return;
6528 }
6529 curwin->w_cursor = pos;
6530 }
6531
6532#ifdef FEAT_COMMENTS
6533 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6534 * comments. */
6535 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006536 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006537 return;
6538#endif
6539
6540 /*
6541 * May start formatting in a previous line, so that after "x" a word is
6542 * moved to the previous line if it fits there now. Only when this is not
6543 * the start of a paragraph.
6544 */
6545 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6546 {
6547 --curwin->w_cursor.lnum;
6548 if (u_save_cursor() == FAIL)
6549 return;
6550 }
6551
6552 /*
6553 * Do the formatting and restore the cursor position. "saved_cursor" will
6554 * be adjusted for the text formatting.
6555 */
6556 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006557 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 curwin->w_cursor = saved_cursor;
6559 saved_cursor.lnum = 0;
6560
6561 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6562 {
6563 /* "cannot happen" */
6564 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6565 coladvance((colnr_T)MAXCOL);
6566 }
6567 else
6568 check_cursor_col();
6569
6570 /* Insert mode: If the cursor is now after the end of the line while it
6571 * previously wasn't, the line was broken. Because of the rule above we
6572 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6573 * formatted. */
6574 if (!wasatend && has_format_option(FO_WHITE_PAR))
6575 {
6576 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006577 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006578 if (curwin->w_cursor.col == len)
6579 {
6580 pnew = vim_strnsave(new, len + 2);
6581 pnew[len] = ' ';
6582 pnew[len + 1] = NUL;
6583 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6584 /* remove the space later */
6585 did_add_space = TRUE;
6586 }
6587 else
6588 /* may remove added space */
6589 check_auto_format(FALSE);
6590 }
6591
6592 check_cursor();
6593}
6594
6595/*
6596 * When an extra space was added to continue a paragraph for auto-formatting,
6597 * delete it now. The space must be under the cursor, just after the insert
6598 * position.
6599 */
6600 static void
6601check_auto_format(end_insert)
6602 int end_insert; /* TRUE when ending Insert mode */
6603{
6604 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006605 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606
6607 if (did_add_space)
6608 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006609 cc = gchar_cursor();
6610 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006611 /* Somehow the space was removed already. */
6612 did_add_space = FALSE;
6613 else
6614 {
6615 if (!end_insert)
6616 {
6617 inc_cursor();
6618 c = gchar_cursor();
6619 dec_cursor();
6620 }
6621 if (c != NUL)
6622 {
6623 /* The space is no longer at the end of the line, delete it. */
6624 del_char(FALSE);
6625 did_add_space = FALSE;
6626 }
6627 }
6628 }
6629}
6630
6631/*
6632 * Find out textwidth to be used for formatting:
6633 * if 'textwidth' option is set, use it
6634 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6635 * if invalid value, use 0.
6636 * Set default to window width (maximum 79) for "gq" operator.
6637 */
6638 int
6639comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006640 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641{
6642 int textwidth;
6643
6644 textwidth = curbuf->b_p_tw;
6645 if (textwidth == 0 && curbuf->b_p_wm)
6646 {
6647 /* The width is the window width minus 'wrapmargin' minus all the
6648 * things that add to the margin. */
6649 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6650#ifdef FEAT_CMDWIN
6651 if (cmdwin_type != 0)
6652 textwidth -= 1;
6653#endif
6654#ifdef FEAT_FOLDING
6655 textwidth -= curwin->w_p_fdc;
6656#endif
6657#ifdef FEAT_SIGNS
6658 if (curwin->w_buffer->b_signlist != NULL
6659# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006660 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006661# endif
6662 )
6663 textwidth -= 1;
6664#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006665 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006666 textwidth -= 8;
6667 }
6668 if (textwidth < 0)
6669 textwidth = 0;
6670 if (ff && textwidth == 0)
6671 {
6672 textwidth = W_WIDTH(curwin) - 1;
6673 if (textwidth > 79)
6674 textwidth = 79;
6675 }
6676 return textwidth;
6677}
6678
6679/*
6680 * Put a character in the redo buffer, for when just after a CTRL-V.
6681 */
6682 static void
6683redo_literal(c)
6684 int c;
6685{
6686 char_u buf[10];
6687
6688 /* Only digits need special treatment. Translate them into a string of
6689 * three digits. */
6690 if (VIM_ISDIGIT(c))
6691 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006692 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 AppendToRedobuff(buf);
6694 }
6695 else
6696 AppendCharToRedobuff(c);
6697}
6698
6699/*
6700 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006701 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702 */
6703 static void
6704start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006705 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
6707 if (!arrow_used) /* something has been inserted */
6708 {
6709 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006710 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006711 arrow_used = TRUE; /* this means we stopped the current insert */
6712 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006713#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006714 check_spell_redraw();
6715#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716}
6717
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006718#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006719/*
6720 * If we skipped highlighting word at cursor, do it now.
6721 * It may be skipped again, thus reset spell_redraw_lnum first.
6722 */
6723 static void
6724check_spell_redraw()
6725{
6726 if (spell_redraw_lnum != 0)
6727 {
6728 linenr_T lnum = spell_redraw_lnum;
6729
6730 spell_redraw_lnum = 0;
6731 redrawWinline(lnum, FALSE);
6732 }
6733}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006734
6735/*
6736 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6737 * spelled word, if there is one.
6738 */
6739 static void
6740spell_back_to_badword()
6741{
6742 pos_T tpos = curwin->w_cursor;
6743
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006744 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006745 if (curwin->w_cursor.col != tpos.col)
6746 start_arrow(&tpos);
6747}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006748#endif
6749
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750/*
6751 * stop_arrow() is called before a change is made in insert mode.
6752 * If an arrow key has been used, start a new insertion.
6753 * Returns FAIL if undo is impossible, shouldn't insert then.
6754 */
6755 int
6756stop_arrow()
6757{
6758 if (arrow_used)
6759 {
6760 if (u_save_cursor() == OK)
6761 {
6762 arrow_used = FALSE;
6763 ins_need_undo = FALSE;
6764 }
6765 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006766 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 ai_col = 0;
6768#ifdef FEAT_VREPLACE
6769 if (State & VREPLACE_FLAG)
6770 {
6771 orig_line_count = curbuf->b_ml.ml_line_count;
6772 vr_lines_changed = 1;
6773 }
6774#endif
6775 ResetRedobuff();
6776 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006777 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 }
6779 else if (ins_need_undo)
6780 {
6781 if (u_save_cursor() == OK)
6782 ins_need_undo = FALSE;
6783 }
6784
6785#ifdef FEAT_FOLDING
6786 /* Always open fold at the cursor line when inserting something. */
6787 foldOpenCursor();
6788#endif
6789
6790 return (arrow_used || ins_need_undo ? FAIL : OK);
6791}
6792
6793/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006794 * Do a few things to stop inserting.
6795 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6796 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 */
6798 static void
Bram Moolenaarf332a652013-11-04 04:20:33 +01006799stop_insert(end_insert_pos, esc, nomove)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006800 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006801 int esc; /* called by ins_esc() */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006802 int nomove; /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006804 int cc;
6805 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806
6807 stop_redo_ins();
6808 replace_flush(); /* abandon replace stack */
6809
6810 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006811 * Save the inserted text for later redo with ^@ and CTRL-A.
6812 * Don't do it when "restart_edit" was set and nothing was inserted,
6813 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006815 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006816 if (did_restart_edit == 0 || (ptr != NULL
6817 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006818 {
6819 vim_free(last_insert);
6820 last_insert = ptr;
6821 last_insert_skip = new_insert_skip;
6822 }
6823 else
6824 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006826 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006827 {
6828 /* Auto-format now. It may seem strange to do this when stopping an
6829 * insertion (or moving the cursor), but it's required when appending
6830 * a line and having it end in a space. But only do it when something
6831 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006832 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006834 pos_T tpos = curwin->w_cursor;
6835
Bram Moolenaar071d4272004-06-13 20:20:40 +00006836 /* When the cursor is at the end of the line after a space the
6837 * formatting will move it to the following word. Avoid that by
6838 * moving the cursor onto the space. */
6839 cc = 'x';
6840 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6841 {
6842 dec_cursor();
6843 cc = gchar_cursor();
6844 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006845 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 }
6847
6848 auto_format(TRUE, FALSE);
6849
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006850 if (vim_iswhite(cc))
6851 {
6852 if (gchar_cursor() != NUL)
6853 inc_cursor();
6854#ifdef FEAT_VIRTUALEDIT
6855 /* If the cursor is still at the same character, also keep
6856 * the "coladd". */
6857 if (gchar_cursor() == NUL
6858 && curwin->w_cursor.lnum == tpos.lnum
6859 && curwin->w_cursor.col == tpos.col)
6860 curwin->w_cursor.coladd = tpos.coladd;
6861#endif
6862 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006863 }
6864
6865 /* If a space was inserted for auto-formatting, remove it now. */
6866 check_auto_format(TRUE);
6867
6868 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006869 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006870 * Do this when ESC was used or moving the cursor up/down.
6871 * Check for the old position still being valid, just in case the text
6872 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006873 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006874 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6875 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006876 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006877 pos_T tpos = curwin->w_cursor;
6878
6879 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006880 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006881 for (;;)
6882 {
6883 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6884 --curwin->w_cursor.col;
6885 cc = gchar_cursor();
6886 if (!vim_iswhite(cc))
6887 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006888 if (del_char(TRUE) == FAIL)
6889 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006890 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006891 if (curwin->w_cursor.lnum != tpos.lnum)
6892 curwin->w_cursor = tpos;
6893 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006894 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6895
6896#ifdef FEAT_VISUAL
6897 /* <C-S-Right> may have started Visual mode, adjust the position for
6898 * deleted characters. */
6899 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6900 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006901 int len = (int)STRLEN(ml_get_curline());
6902
6903 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006905 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006906# ifdef FEAT_VIRTUALEDIT
6907 VIsual.coladd = 0;
6908# endif
6909 }
6910 }
6911#endif
6912 }
6913 }
6914 did_ai = FALSE;
6915#ifdef FEAT_SMARTINDENT
6916 did_si = FALSE;
6917 can_si = FALSE;
6918 can_si_back = FALSE;
6919#endif
6920
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006921 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6922 * now in a different buffer. */
6923 if (end_insert_pos != NULL)
6924 {
6925 curbuf->b_op_start = Insstart;
6926 curbuf->b_op_end = *end_insert_pos;
6927 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006928}
6929
6930/*
6931 * Set the last inserted text to a single character.
6932 * Used for the replace command.
6933 */
6934 void
6935set_last_insert(c)
6936 int c;
6937{
6938 char_u *s;
6939
6940 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006942 if (last_insert != NULL)
6943 {
6944 s = last_insert;
6945 /* Use the CTRL-V only when entering a special char */
6946 if (c < ' ' || c == DEL)
6947 *s++ = Ctrl_V;
6948 s = add_char2buf(c, s);
6949 *s++ = ESC;
6950 *s++ = NUL;
6951 last_insert_skip = 0;
6952 }
6953}
6954
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006955#if defined(EXITFREE) || defined(PROTO)
6956 void
6957free_last_insert()
6958{
6959 vim_free(last_insert);
6960 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006961# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006962 vim_free(compl_orig_text);
6963 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006964# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006965}
6966#endif
6967
Bram Moolenaar071d4272004-06-13 20:20:40 +00006968/*
6969 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6970 * and CSI. Handle multi-byte characters.
6971 * Returns a pointer to after the added bytes.
6972 */
6973 char_u *
6974add_char2buf(c, s)
6975 int c;
6976 char_u *s;
6977{
6978#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006979 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006980 int i;
6981 int len;
6982
6983 len = (*mb_char2bytes)(c, temp);
6984 for (i = 0; i < len; ++i)
6985 {
6986 c = temp[i];
6987#endif
6988 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6989 if (c == K_SPECIAL)
6990 {
6991 *s++ = K_SPECIAL;
6992 *s++ = KS_SPECIAL;
6993 *s++ = KE_FILLER;
6994 }
6995#ifdef FEAT_GUI
6996 else if (c == CSI)
6997 {
6998 *s++ = CSI;
6999 *s++ = KS_EXTRA;
7000 *s++ = (int)KE_CSI;
7001 }
7002#endif
7003 else
7004 *s++ = c;
7005#ifdef FEAT_MBYTE
7006 }
7007#endif
7008 return s;
7009}
7010
7011/*
7012 * move cursor to start of line
7013 * if flags & BL_WHITE move to first non-white
7014 * if flags & BL_SOL move to first non-white if startofline is set,
7015 * otherwise keep "curswant" column
7016 * if flags & BL_FIX don't leave the cursor on a NUL.
7017 */
7018 void
7019beginline(flags)
7020 int flags;
7021{
7022 if ((flags & BL_SOL) && !p_sol)
7023 coladvance(curwin->w_curswant);
7024 else
7025 {
7026 curwin->w_cursor.col = 0;
7027#ifdef FEAT_VIRTUALEDIT
7028 curwin->w_cursor.coladd = 0;
7029#endif
7030
7031 if (flags & (BL_WHITE | BL_SOL))
7032 {
7033 char_u *ptr;
7034
7035 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7036 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7037 ++curwin->w_cursor.col;
7038 }
7039 curwin->w_set_curswant = TRUE;
7040 }
7041}
7042
7043/*
7044 * oneright oneleft cursor_down cursor_up
7045 *
7046 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007047 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048 * Return OK when successful, FAIL when we hit a line of file boundary.
7049 */
7050
7051 int
7052oneright()
7053{
7054 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007056
7057#ifdef FEAT_VIRTUALEDIT
7058 if (virtual_active())
7059 {
7060 pos_T prevpos = curwin->w_cursor;
7061
7062 /* Adjust for multi-wide char (excluding TAB) */
7063 ptr = ml_get_cursor();
7064 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007065# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007066 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007067# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007068 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007070 ))
7071 ? ptr2cells(ptr) : 1));
7072 curwin->w_set_curswant = TRUE;
7073 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7074 return (prevpos.col != curwin->w_cursor.col
7075 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7076 }
7077#endif
7078
7079 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007080 if (*ptr == NUL)
7081 return FAIL; /* already at the very end */
7082
Bram Moolenaar071d4272004-06-13 20:20:40 +00007083#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007084 if (has_mbyte)
7085 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007086 else
7087#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007088 l = 1;
7089
7090 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7091 * contains "onemore". */
7092 if (ptr[l] == NUL
7093#ifdef FEAT_VIRTUALEDIT
7094 && (ve_flags & VE_ONEMORE) == 0
7095#endif
7096 )
7097 return FAIL;
7098 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099
7100 curwin->w_set_curswant = TRUE;
7101 return OK;
7102}
7103
7104 int
7105oneleft()
7106{
7107#ifdef FEAT_VIRTUALEDIT
7108 if (virtual_active())
7109 {
7110 int width;
7111 int v = getviscol();
7112
7113 if (v == 0)
7114 return FAIL;
7115
7116# ifdef FEAT_LINEBREAK
7117 /* We might get stuck on 'showbreak', skip over it. */
7118 width = 1;
7119 for (;;)
7120 {
7121 coladvance(v - width);
7122 /* getviscol() is slow, skip it when 'showbreak' is empty and
7123 * there are no multi-byte characters */
7124 if ((*p_sbr == NUL
7125# ifdef FEAT_MBYTE
7126 && !has_mbyte
7127# endif
7128 ) || getviscol() < v)
7129 break;
7130 ++width;
7131 }
7132# else
7133 coladvance(v - 1);
7134# endif
7135
7136 if (curwin->w_cursor.coladd == 1)
7137 {
7138 char_u *ptr;
7139
7140 /* Adjust for multi-wide char (not a TAB) */
7141 ptr = ml_get_cursor();
7142 if (*ptr != TAB && vim_isprintc(
7143# ifdef FEAT_MBYTE
7144 (*mb_ptr2char)(ptr)
7145# else
7146 *ptr
7147# endif
7148 ) && ptr2cells(ptr) > 1)
7149 curwin->w_cursor.coladd = 0;
7150 }
7151
7152 curwin->w_set_curswant = TRUE;
7153 return OK;
7154 }
7155#endif
7156
7157 if (curwin->w_cursor.col == 0)
7158 return FAIL;
7159
7160 curwin->w_set_curswant = TRUE;
7161 --curwin->w_cursor.col;
7162
7163#ifdef FEAT_MBYTE
7164 /* if the character on the left of the current cursor is a multi-byte
7165 * character, move to its first byte */
7166 if (has_mbyte)
7167 mb_adjust_cursor();
7168#endif
7169 return OK;
7170}
7171
7172 int
7173cursor_up(n, upd_topline)
7174 long n;
7175 int upd_topline; /* When TRUE: update topline */
7176{
7177 linenr_T lnum;
7178
7179 if (n > 0)
7180 {
7181 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007182 /* This fails if the cursor is already in the first line or the count
7183 * is larger than the line number and '-' is in 'cpoptions' */
7184 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185 return FAIL;
7186 if (n >= lnum)
7187 lnum = 1;
7188 else
7189#ifdef FEAT_FOLDING
7190 if (hasAnyFolding(curwin))
7191 {
7192 /*
7193 * Count each sequence of folded lines as one logical line.
7194 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007195 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007196 (void)hasFolding(lnum, &lnum, NULL);
7197
7198 while (n--)
7199 {
7200 /* move up one line */
7201 --lnum;
7202 if (lnum <= 1)
7203 break;
7204 /* If we entered a fold, move to the beginning, unless in
7205 * Insert mode or when 'foldopen' contains "all": it will open
7206 * in a moment. */
7207 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7208 (void)hasFolding(lnum, &lnum, NULL);
7209 }
7210 if (lnum < 1)
7211 lnum = 1;
7212 }
7213 else
7214#endif
7215 lnum -= n;
7216 curwin->w_cursor.lnum = lnum;
7217 }
7218
7219 /* try to advance to the column we want to be at */
7220 coladvance(curwin->w_curswant);
7221
7222 if (upd_topline)
7223 update_topline(); /* make sure curwin->w_topline is valid */
7224
7225 return OK;
7226}
7227
7228/*
7229 * Cursor down a number of logical lines.
7230 */
7231 int
7232cursor_down(n, upd_topline)
7233 long n;
7234 int upd_topline; /* When TRUE: update topline */
7235{
7236 linenr_T lnum;
7237
7238 if (n > 0)
7239 {
7240 lnum = curwin->w_cursor.lnum;
7241#ifdef FEAT_FOLDING
7242 /* Move to last line of fold, will fail if it's the end-of-file. */
7243 (void)hasFolding(lnum, NULL, &lnum);
7244#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007245 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007246 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007247 if (lnum >= curbuf->b_ml.ml_line_count
7248 || (lnum + n > curbuf->b_ml.ml_line_count
7249 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007250 return FAIL;
7251 if (lnum + n >= curbuf->b_ml.ml_line_count)
7252 lnum = curbuf->b_ml.ml_line_count;
7253 else
7254#ifdef FEAT_FOLDING
7255 if (hasAnyFolding(curwin))
7256 {
7257 linenr_T last;
7258
7259 /* count each sequence of folded lines as one logical line */
7260 while (n--)
7261 {
7262 if (hasFolding(lnum, NULL, &last))
7263 lnum = last + 1;
7264 else
7265 ++lnum;
7266 if (lnum >= curbuf->b_ml.ml_line_count)
7267 break;
7268 }
7269 if (lnum > curbuf->b_ml.ml_line_count)
7270 lnum = curbuf->b_ml.ml_line_count;
7271 }
7272 else
7273#endif
7274 lnum += n;
7275 curwin->w_cursor.lnum = lnum;
7276 }
7277
7278 /* try to advance to the column we want to be at */
7279 coladvance(curwin->w_curswant);
7280
7281 if (upd_topline)
7282 update_topline(); /* make sure curwin->w_topline is valid */
7283
7284 return OK;
7285}
7286
7287/*
7288 * Stuff the last inserted text in the read buffer.
7289 * Last_insert actually is a copy of the redo buffer, so we
7290 * first have to remove the command.
7291 */
7292 int
7293stuff_inserted(c, count, no_esc)
7294 int c; /* Command character to be inserted */
7295 long count; /* Repeat this many times */
7296 int no_esc; /* Don't add an ESC at the end */
7297{
7298 char_u *esc_ptr;
7299 char_u *ptr;
7300 char_u *last_ptr;
7301 char_u last = NUL;
7302
7303 ptr = get_last_insert();
7304 if (ptr == NULL)
7305 {
7306 EMSG(_(e_noinstext));
7307 return FAIL;
7308 }
7309
7310 /* may want to stuff the command character, to start Insert mode */
7311 if (c != NUL)
7312 stuffcharReadbuff(c);
7313 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7314 *esc_ptr = NUL; /* remove the ESC */
7315
7316 /* when the last char is either "0" or "^" it will be quoted if no ESC
7317 * comes after it OR if it will inserted more than once and "ptr"
7318 * starts with ^D. -- Acevedo
7319 */
7320 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7321 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7322 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7323 {
7324 last = *last_ptr;
7325 *last_ptr = NUL;
7326 }
7327
7328 do
7329 {
7330 stuffReadbuff(ptr);
7331 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7332 if (last)
7333 stuffReadbuff((char_u *)(last == '0'
7334 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7335 : IF_EB("\026^", CTRL_V_STR "^")));
7336 }
7337 while (--count > 0);
7338
7339 if (last)
7340 *last_ptr = last;
7341
7342 if (esc_ptr != NULL)
7343 *esc_ptr = ESC; /* put the ESC back */
7344
7345 /* may want to stuff a trailing ESC, to get out of Insert mode */
7346 if (!no_esc)
7347 stuffcharReadbuff(ESC);
7348
7349 return OK;
7350}
7351
7352 char_u *
7353get_last_insert()
7354{
7355 if (last_insert == NULL)
7356 return NULL;
7357 return last_insert + last_insert_skip;
7358}
7359
7360/*
7361 * Get last inserted string, and remove trailing <Esc>.
7362 * Returns pointer to allocated memory (must be freed) or NULL.
7363 */
7364 char_u *
7365get_last_insert_save()
7366{
7367 char_u *s;
7368 int len;
7369
7370 if (last_insert == NULL)
7371 return NULL;
7372 s = vim_strsave(last_insert + last_insert_skip);
7373 if (s != NULL)
7374 {
7375 len = (int)STRLEN(s);
7376 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7377 s[len - 1] = NUL;
7378 }
7379 return s;
7380}
7381
7382/*
7383 * Check the word in front of the cursor for an abbreviation.
7384 * Called when the non-id character "c" has been entered.
7385 * When an abbreviation is recognized it is removed from the text and
7386 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7387 */
7388 static int
7389echeck_abbr(c)
7390 int c;
7391{
7392 /* Don't check for abbreviation in paste mode, when disabled and just
7393 * after moving around with cursor keys. */
7394 if (p_paste || no_abbr || arrow_used)
7395 return FALSE;
7396
7397 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7398 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7399}
7400
7401/*
7402 * replace-stack functions
7403 *
7404 * When replacing characters, the replaced characters are remembered for each
7405 * new character. This is used to re-insert the old text when backspacing.
7406 *
7407 * There is a NUL headed list of characters for each character that is
7408 * currently in the file after the insertion point. When BS is used, one NUL
7409 * headed list is put back for the deleted character.
7410 *
7411 * For a newline, there are two NUL headed lists. One contains the characters
7412 * that the NL replaced. The extra one stores the characters after the cursor
7413 * that were deleted (always white space).
7414 *
7415 * Replace_offset is normally 0, in which case replace_push will add a new
7416 * character at the end of the stack. If replace_offset is not 0, that many
7417 * characters will be left on the stack above the newly inserted character.
7418 */
7419
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007420static char_u *replace_stack = NULL;
7421static long replace_stack_nr = 0; /* next entry in replace stack */
7422static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423
7424 void
7425replace_push(c)
7426 int c; /* character that is replaced (NUL is none) */
7427{
7428 char_u *p;
7429
7430 if (replace_stack_nr < replace_offset) /* nothing to do */
7431 return;
7432 if (replace_stack_len <= replace_stack_nr)
7433 {
7434 replace_stack_len += 50;
7435 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7436 if (p == NULL) /* out of memory */
7437 {
7438 replace_stack_len -= 50;
7439 return;
7440 }
7441 if (replace_stack != NULL)
7442 {
7443 mch_memmove(p, replace_stack,
7444 (size_t)(replace_stack_nr * sizeof(char_u)));
7445 vim_free(replace_stack);
7446 }
7447 replace_stack = p;
7448 }
7449 p = replace_stack + replace_stack_nr - replace_offset;
7450 if (replace_offset)
7451 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7452 *p = c;
7453 ++replace_stack_nr;
7454}
7455
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007456#if defined(FEAT_MBYTE) || defined(PROTO)
7457/*
7458 * Push a character onto the replace stack. Handles a multi-byte character in
7459 * reverse byte order, so that the first byte is popped off first.
7460 * Return the number of bytes done (includes composing characters).
7461 */
7462 int
7463replace_push_mb(p)
7464 char_u *p;
7465{
7466 int l = (*mb_ptr2len)(p);
7467 int j;
7468
7469 for (j = l - 1; j >= 0; --j)
7470 replace_push(p[j]);
7471 return l;
7472}
7473#endif
7474
Bram Moolenaar071d4272004-06-13 20:20:40 +00007475/*
7476 * Pop one item from the replace stack.
7477 * return -1 if stack empty
7478 * return replaced character or NUL otherwise
7479 */
7480 static int
7481replace_pop()
7482{
7483 if (replace_stack_nr == 0)
7484 return -1;
7485 return (int)replace_stack[--replace_stack_nr];
7486}
7487
7488/*
7489 * Join the top two items on the replace stack. This removes to "off"'th NUL
7490 * encountered.
7491 */
7492 static void
7493replace_join(off)
7494 int off; /* offset for which NUL to remove */
7495{
7496 int i;
7497
7498 for (i = replace_stack_nr; --i >= 0; )
7499 if (replace_stack[i] == NUL && off-- <= 0)
7500 {
7501 --replace_stack_nr;
7502 mch_memmove(replace_stack + i, replace_stack + i + 1,
7503 (size_t)(replace_stack_nr - i));
7504 return;
7505 }
7506}
7507
7508/*
7509 * Pop bytes from the replace stack until a NUL is found, and insert them
7510 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7511 */
7512 static void
7513replace_pop_ins()
7514{
7515 int cc;
7516 int oldState = State;
7517
7518 State = NORMAL; /* don't want REPLACE here */
7519 while ((cc = replace_pop()) > 0)
7520 {
7521#ifdef FEAT_MBYTE
7522 mb_replace_pop_ins(cc);
7523#else
7524 ins_char(cc);
7525#endif
7526 dec_cursor();
7527 }
7528 State = oldState;
7529}
7530
7531#ifdef FEAT_MBYTE
7532/*
7533 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7534 * indicates a multi-byte char, pop the other bytes too.
7535 */
7536 static void
7537mb_replace_pop_ins(cc)
7538 int cc;
7539{
7540 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007541 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007542 int i;
7543 int c;
7544
7545 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7546 {
7547 buf[0] = cc;
7548 for (i = 1; i < n; ++i)
7549 buf[i] = replace_pop();
7550 ins_bytes_len(buf, n);
7551 }
7552 else
7553 ins_char(cc);
7554
7555 if (enc_utf8)
7556 /* Handle composing chars. */
7557 for (;;)
7558 {
7559 c = replace_pop();
7560 if (c == -1) /* stack empty */
7561 break;
7562 if ((n = MB_BYTE2LEN(c)) == 1)
7563 {
7564 /* Not a multi-byte char, put it back. */
7565 replace_push(c);
7566 break;
7567 }
7568 else
7569 {
7570 buf[0] = c;
7571 for (i = 1; i < n; ++i)
7572 buf[i] = replace_pop();
7573 if (utf_iscomposing(utf_ptr2char(buf)))
7574 ins_bytes_len(buf, n);
7575 else
7576 {
7577 /* Not a composing char, put it back. */
7578 for (i = n - 1; i >= 0; --i)
7579 replace_push(buf[i]);
7580 break;
7581 }
7582 }
7583 }
7584}
7585#endif
7586
7587/*
7588 * make the replace stack empty
7589 * (called when exiting replace mode)
7590 */
7591 static void
7592replace_flush()
7593{
7594 vim_free(replace_stack);
7595 replace_stack = NULL;
7596 replace_stack_len = 0;
7597 replace_stack_nr = 0;
7598}
7599
7600/*
7601 * Handle doing a BS for one character.
7602 * cc < 0: replace stack empty, just move cursor
7603 * cc == 0: character was inserted, delete it
7604 * cc > 0: character was replaced, put cc (first byte of original char) back
7605 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007606 * When "limit_col" is >= 0, don't delete before this column. Matters when
7607 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007608 */
7609 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007610replace_do_bs(limit_col)
7611 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007612{
7613 int cc;
7614#ifdef FEAT_VREPLACE
7615 int orig_len = 0;
7616 int ins_len;
7617 int orig_vcols = 0;
7618 colnr_T start_vcol;
7619 char_u *p;
7620 int i;
7621 int vcol;
7622#endif
7623
7624 cc = replace_pop();
7625 if (cc > 0)
7626 {
7627#ifdef FEAT_VREPLACE
7628 if (State & VREPLACE_FLAG)
7629 {
7630 /* Get the number of screen cells used by the character we are
7631 * going to delete. */
7632 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7633 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7634 }
7635#endif
7636#ifdef FEAT_MBYTE
7637 if (has_mbyte)
7638 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007639 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007640# ifdef FEAT_VREPLACE
7641 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007642 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007643# endif
7644 replace_push(cc);
7645 }
7646 else
7647#endif
7648 {
7649 pchar_cursor(cc);
7650#ifdef FEAT_VREPLACE
7651 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007652 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653#endif
7654 }
7655 replace_pop_ins();
7656
7657#ifdef FEAT_VREPLACE
7658 if (State & VREPLACE_FLAG)
7659 {
7660 /* Get the number of screen cells used by the inserted characters */
7661 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007662 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007663 vcol = start_vcol;
7664 for (i = 0; i < ins_len; ++i)
7665 {
7666 vcol += chartabsize(p + i, vcol);
7667#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007668 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669#endif
7670 }
7671 vcol -= start_vcol;
7672
7673 /* Delete spaces that were inserted after the cursor to keep the
7674 * text aligned. */
7675 curwin->w_cursor.col += ins_len;
7676 while (vcol > orig_vcols && gchar_cursor() == ' ')
7677 {
7678 del_char(FALSE);
7679 ++orig_vcols;
7680 }
7681 curwin->w_cursor.col -= ins_len;
7682 }
7683#endif
7684
7685 /* mark the buffer as changed and prepare for displaying */
7686 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7687 }
7688 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007689 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007690}
7691
7692#ifdef FEAT_CINDENT
7693/*
7694 * Return TRUE if C-indenting is on.
7695 */
7696 static int
7697cindent_on()
7698{
7699 return (!p_paste && (curbuf->b_p_cin
7700# ifdef FEAT_EVAL
7701 || *curbuf->b_p_inde != NUL
7702# endif
7703 ));
7704}
7705#endif
7706
7707#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7708/*
7709 * Re-indent the current line, based on the current contents of it and the
7710 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7711 * confused what all the part that handles Control-T is doing that I'm not.
7712 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7713 */
7714
7715 void
7716fixthisline(get_the_indent)
7717 int (*get_the_indent) __ARGS((void));
7718{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007719 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007720 if (linewhite(curwin->w_cursor.lnum))
7721 did_ai = TRUE; /* delete the indent if the line stays empty */
7722}
7723
7724 void
7725fix_indent()
7726{
7727 if (p_paste)
7728 return;
7729# ifdef FEAT_LISP
7730 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7731 fixthisline(get_lisp_indent);
7732# endif
7733# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7734 else
7735# endif
7736# ifdef FEAT_CINDENT
7737 if (cindent_on())
7738 do_c_expr_indent();
7739# endif
7740}
7741
7742#endif
7743
7744#ifdef FEAT_CINDENT
7745/*
7746 * return TRUE if 'cinkeys' contains the key "keytyped",
7747 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007748 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7750 *
7751 * "keytyped" can have a few special values:
7752 * KEY_OPEN_FORW
7753 * KEY_OPEN_BACK
7754 * KEY_COMPLETE just finished completion.
7755 *
7756 * If line_is_empty is TRUE accept keys with '0' before them.
7757 */
7758 int
7759in_cinkeys(keytyped, when, line_is_empty)
7760 int keytyped;
7761 int when;
7762 int line_is_empty;
7763{
7764 char_u *look;
7765 int try_match;
7766 int try_match_word;
7767 char_u *p;
7768 char_u *line;
7769 int icase;
7770 int i;
7771
Bram Moolenaar30848942009-12-24 14:46:12 +00007772 if (keytyped == NUL)
7773 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7774 return FALSE;
7775
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776#ifdef FEAT_EVAL
7777 if (*curbuf->b_p_inde != NUL)
7778 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7779 else
7780#endif
7781 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7782 while (*look)
7783 {
7784 /*
7785 * Find out if we want to try a match with this key, depending on
7786 * 'when' and a '*' or '!' before the key.
7787 */
7788 switch (when)
7789 {
7790 case '*': try_match = (*look == '*'); break;
7791 case '!': try_match = (*look == '!'); break;
7792 default: try_match = (*look != '*'); break;
7793 }
7794 if (*look == '*' || *look == '!')
7795 ++look;
7796
7797 /*
7798 * If there is a '0', only accept a match if the line is empty.
7799 * But may still match when typing last char of a word.
7800 */
7801 if (*look == '0')
7802 {
7803 try_match_word = try_match;
7804 if (!line_is_empty)
7805 try_match = FALSE;
7806 ++look;
7807 }
7808 else
7809 try_match_word = FALSE;
7810
7811 /*
7812 * does it look like a control character?
7813 */
7814 if (*look == '^'
7815#ifdef EBCDIC
7816 && (Ctrl_chr(look[1]) != 0)
7817#else
7818 && look[1] >= '?' && look[1] <= '_'
7819#endif
7820 )
7821 {
7822 if (try_match && keytyped == Ctrl_chr(look[1]))
7823 return TRUE;
7824 look += 2;
7825 }
7826 /*
7827 * 'o' means "o" command, open forward.
7828 * 'O' means "O" command, open backward.
7829 */
7830 else if (*look == 'o')
7831 {
7832 if (try_match && keytyped == KEY_OPEN_FORW)
7833 return TRUE;
7834 ++look;
7835 }
7836 else if (*look == 'O')
7837 {
7838 if (try_match && keytyped == KEY_OPEN_BACK)
7839 return TRUE;
7840 ++look;
7841 }
7842
7843 /*
7844 * 'e' means to check for "else" at start of line and just before the
7845 * cursor.
7846 */
7847 else if (*look == 'e')
7848 {
7849 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7850 {
7851 p = ml_get_curline();
7852 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7853 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7854 return TRUE;
7855 }
7856 ++look;
7857 }
7858
7859 /*
7860 * ':' only causes an indent if it is at the end of a label or case
7861 * statement, or when it was before typing the ':' (to fix
7862 * class::method for C++).
7863 */
7864 else if (*look == ':')
7865 {
7866 if (try_match && keytyped == ':')
7867 {
7868 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007869 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007871 /* Need to get the line again after cin_islabel(). */
7872 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 if (curwin->w_cursor.col > 2
7874 && p[curwin->w_cursor.col - 1] == ':'
7875 && p[curwin->w_cursor.col - 2] == ':')
7876 {
7877 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007878 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007879 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 p = ml_get_curline();
7881 p[curwin->w_cursor.col - 1] = ':';
7882 if (i)
7883 return TRUE;
7884 }
7885 }
7886 ++look;
7887 }
7888
7889
7890 /*
7891 * Is it a key in <>, maybe?
7892 */
7893 else if (*look == '<')
7894 {
7895 if (try_match)
7896 {
7897 /*
7898 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7899 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7900 * >, *, : and ! keys if they really really want to.
7901 */
7902 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7903 && keytyped == look[1])
7904 return TRUE;
7905
7906 if (keytyped == get_special_key_code(look + 1))
7907 return TRUE;
7908 }
7909 while (*look && *look != '>')
7910 look++;
7911 while (*look == '>')
7912 look++;
7913 }
7914
7915 /*
7916 * Is it a word: "=word"?
7917 */
7918 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7919 {
7920 ++look;
7921 if (*look == '~')
7922 {
7923 icase = TRUE;
7924 ++look;
7925 }
7926 else
7927 icase = FALSE;
7928 p = vim_strchr(look, ',');
7929 if (p == NULL)
7930 p = look + STRLEN(look);
7931 if ((try_match || try_match_word)
7932 && curwin->w_cursor.col >= (colnr_T)(p - look))
7933 {
7934 int match = FALSE;
7935
7936#ifdef FEAT_INS_EXPAND
7937 if (keytyped == KEY_COMPLETE)
7938 {
7939 char_u *s;
7940
7941 /* Just completed a word, check if it starts with "look".
7942 * search back for the start of a word. */
7943 line = ml_get_curline();
7944# ifdef FEAT_MBYTE
7945 if (has_mbyte)
7946 {
7947 char_u *n;
7948
7949 for (s = line + curwin->w_cursor.col; s > line; s = n)
7950 {
7951 n = mb_prevptr(line, s);
7952 if (!vim_iswordp(n))
7953 break;
7954 }
7955 }
7956 else
7957# endif
7958 for (s = line + curwin->w_cursor.col; s > line; --s)
7959 if (!vim_iswordc(s[-1]))
7960 break;
7961 if (s + (p - look) <= line + curwin->w_cursor.col
7962 && (icase
7963 ? MB_STRNICMP(s, look, p - look)
7964 : STRNCMP(s, look, p - look)) == 0)
7965 match = TRUE;
7966 }
7967 else
7968#endif
7969 /* TODO: multi-byte */
7970 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7971 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7972 {
7973 line = ml_get_cursor();
7974 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7975 || !vim_iswordc(line[-(p - look) - 1]))
7976 && (icase
7977 ? MB_STRNICMP(line - (p - look), look, p - look)
7978 : STRNCMP(line - (p - look), look, p - look))
7979 == 0)
7980 match = TRUE;
7981 }
7982 if (match && try_match_word && !try_match)
7983 {
7984 /* "0=word": Check if there are only blanks before the
7985 * word. */
7986 line = ml_get_curline();
7987 if ((int)(skipwhite(line) - line) !=
7988 (int)(curwin->w_cursor.col - (p - look)))
7989 match = FALSE;
7990 }
7991 if (match)
7992 return TRUE;
7993 }
7994 look = p;
7995 }
7996
7997 /*
7998 * ok, it's a boring generic character.
7999 */
8000 else
8001 {
8002 if (try_match && *look == keytyped)
8003 return TRUE;
8004 ++look;
8005 }
8006
8007 /*
8008 * Skip over ", ".
8009 */
8010 look = skip_to_option_part(look);
8011 }
8012 return FALSE;
8013}
8014#endif /* FEAT_CINDENT */
8015
8016#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8017/*
8018 * Map Hebrew keyboard when in hkmap mode.
8019 */
8020 int
8021hkmap(c)
8022 int c;
8023{
8024 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8025 {
8026 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8027 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8028 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8029 static char_u map[26] =
8030 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8031 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8032 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8033 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8034 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8035 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8036 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8037 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8038 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8039
8040 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8041 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8042 /* '-1'='sofit' */
8043 else if (c == 'x')
8044 return 'X';
8045 else if (c == 'q')
8046 return '\''; /* {geresh}={'} */
8047 else if (c == 246)
8048 return ' '; /* \"o --> ' ' for a german keyboard */
8049 else if (c == 228)
8050 return ' '; /* \"a --> ' ' -- / -- */
8051 else if (c == 252)
8052 return ' '; /* \"u --> ' ' -- / -- */
8053#ifdef EBCDIC
8054 else if (islower(c))
8055#else
8056 /* NOTE: islower() does not do the right thing for us on Linux so we
8057 * do this the same was as 5.7 and previous, so it works correctly on
8058 * all systems. Specifically, the e.g. Delete and Arrow keys are
8059 * munged and won't work if e.g. searching for Hebrew text.
8060 */
8061 else if (c >= 'a' && c <= 'z')
8062#endif
8063 return (int)(map[CharOrdLow(c)] + p_aleph);
8064 else
8065 return c;
8066 }
8067 else
8068 {
8069 switch (c)
8070 {
8071 case '`': return ';';
8072 case '/': return '.';
8073 case '\'': return ',';
8074 case 'q': return '/';
8075 case 'w': return '\'';
8076
8077 /* Hebrew letters - set offset from 'a' */
8078 case ',': c = '{'; break;
8079 case '.': c = 'v'; break;
8080 case ';': c = 't'; break;
8081 default: {
8082 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8083
8084#ifdef EBCDIC
8085 /* see note about islower() above */
8086 if (!islower(c))
8087#else
8088 if (c < 'a' || c > 'z')
8089#endif
8090 return c;
8091 c = str[CharOrdLow(c)];
8092 break;
8093 }
8094 }
8095
8096 return (int)(CharOrdLow(c) + p_aleph);
8097 }
8098}
8099#endif
8100
8101 static void
8102ins_reg()
8103{
8104 int need_redraw = FALSE;
8105 int regname;
8106 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008107#ifdef FEAT_VISUAL
8108 int vis_active = VIsual_active;
8109#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110
8111 /*
8112 * If we are going to wait for a character, show a '"'.
8113 */
8114 pc_status = PC_STATUS_UNSET;
8115 if (redrawing() && !char_avail())
8116 {
8117 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008118 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008119
8120 edit_putchar('"', TRUE);
8121#ifdef FEAT_CMDL_INFO
8122 add_to_showcmd_c(Ctrl_R);
8123#endif
8124 }
8125
8126#ifdef USE_ON_FLY_SCROLL
8127 dont_scroll = TRUE; /* disallow scrolling here */
8128#endif
8129
8130 /*
8131 * Don't map the register name. This also prevents the mode message to be
8132 * deleted when ESC is hit.
8133 */
8134 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008135 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8138 {
8139 /* Get a third key for literal register insertion */
8140 literally = regname;
8141#ifdef FEAT_CMDL_INFO
8142 add_to_showcmd_c(literally);
8143#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008144 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008145 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 }
8147 --no_mapping;
8148
8149#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008150 /* Don't call u_sync() while typing the expression or giving an error
8151 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 ++no_u_sync;
8153 if (regname == '=')
8154 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008155# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008156 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008157# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008158 /* Sync undo when evaluating the expression calls setline() or
8159 * append(), so that it can be undone separately. */
8160 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008161
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008163# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 /* Restore the Input Method. */
8165 if (im_on)
8166 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008167# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008168 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008169 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8170 {
8171 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008173 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008174 else
8175 {
8176#endif
8177 if (literally == Ctrl_O || literally == Ctrl_P)
8178 {
8179 /* Append the command to the redo buffer. */
8180 AppendCharToRedobuff(Ctrl_R);
8181 AppendCharToRedobuff(literally);
8182 AppendCharToRedobuff(regname);
8183
8184 do_put(regname, BACKWARD, 1L,
8185 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8186 }
8187 else if (insert_reg(regname, literally) == FAIL)
8188 {
8189 vim_beep();
8190 need_redraw = TRUE; /* remove the '"' */
8191 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008192 else if (stop_insert_mode)
8193 /* When the '=' register was used and a function was invoked that
8194 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8195 * insert anything, need to remove the '"' */
8196 need_redraw = TRUE;
8197
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198#ifdef FEAT_EVAL
8199 }
8200 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008201 if (u_sync_once == 1)
8202 ins_need_undo = TRUE;
8203 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204#endif
8205#ifdef FEAT_CMDL_INFO
8206 clear_showcmd();
8207#endif
8208
8209 /* If the inserted register is empty, we need to remove the '"' */
8210 if (need_redraw || stuff_empty())
8211 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008212
8213#ifdef FEAT_VISUAL
8214 /* Disallow starting Visual mode here, would get a weird mode. */
8215 if (!vis_active && VIsual_active)
8216 end_visual_mode();
8217#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218}
8219
8220/*
8221 * CTRL-G commands in Insert mode.
8222 */
8223 static void
8224ins_ctrl_g()
8225{
8226 int c;
8227
8228#ifdef FEAT_INS_EXPAND
8229 /* Right after CTRL-X the cursor will be after the ruler. */
8230 setcursor();
8231#endif
8232
8233 /*
8234 * Don't map the second key. This also prevents the mode message to be
8235 * deleted when ESC is hit.
8236 */
8237 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008238 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239 --no_mapping;
8240 switch (c)
8241 {
8242 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8243 case K_UP:
8244 case Ctrl_K:
8245 case 'k': ins_up(TRUE);
8246 break;
8247
8248 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8249 case K_DOWN:
8250 case Ctrl_J:
8251 case 'j': ins_down(TRUE);
8252 break;
8253
8254 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008255 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008257
8258 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008259 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008260 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008261 break;
8262
8263 /* Unknown CTRL-G command, reserved for future expansion. */
8264 default: vim_beep();
8265 }
8266}
8267
8268/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008269 * CTRL-^ in Insert mode.
8270 */
8271 static void
8272ins_ctrl_hat()
8273{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008274 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008275 {
8276 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8277 if (State & LANGMAP)
8278 {
8279 curbuf->b_p_iminsert = B_IMODE_NONE;
8280 State &= ~LANGMAP;
8281 }
8282 else
8283 {
8284 curbuf->b_p_iminsert = B_IMODE_LMAP;
8285 State |= LANGMAP;
8286#ifdef USE_IM_CONTROL
8287 im_set_active(FALSE);
8288#endif
8289 }
8290 }
8291#ifdef USE_IM_CONTROL
8292 else
8293 {
8294 /* There are no ":lmap" mappings, toggle IM */
8295 if (im_get_status())
8296 {
8297 curbuf->b_p_iminsert = B_IMODE_NONE;
8298 im_set_active(FALSE);
8299 }
8300 else
8301 {
8302 curbuf->b_p_iminsert = B_IMODE_IM;
8303 State &= ~LANGMAP;
8304 im_set_active(TRUE);
8305 }
8306 }
8307#endif
8308 set_iminsert_global();
8309 showmode();
8310#ifdef FEAT_GUI
8311 /* may show different cursor shape or color */
8312 if (gui.in_use)
8313 gui_update_cursor(TRUE, FALSE);
8314#endif
8315#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8316 /* Show/unshow value of 'keymap' in status lines. */
8317 status_redraw_curbuf();
8318#endif
8319}
8320
8321/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008322 * Handle ESC in insert mode.
8323 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8324 * insert.
8325 */
8326 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008327ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 long *count;
8329 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008330 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008331{
8332 int temp;
8333 static int disabled_redraw = FALSE;
8334
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008335#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008336 check_spell_redraw();
8337#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338#if defined(FEAT_HANGULIN)
8339# if defined(ESC_CHG_TO_ENG_MODE)
8340 hangul_input_state_set(0);
8341# endif
8342 if (composing_hangul)
8343 {
8344 push_raw_key(composing_hangul_buffer, 2);
8345 composing_hangul = 0;
8346 }
8347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
8349 temp = curwin->w_cursor.col;
8350 if (disabled_redraw)
8351 {
8352 --RedrawingDisabled;
8353 disabled_redraw = FALSE;
8354 }
8355 if (!arrow_used)
8356 {
8357 /*
8358 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008359 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8360 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 */
8362 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008363 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364
8365 /*
8366 * Repeating insert may take a long time. Check for
8367 * interrupt now and then.
8368 */
8369 if (*count > 0)
8370 {
8371 line_breakcheck();
8372 if (got_int)
8373 *count = 0;
8374 }
8375
8376 if (--*count > 0) /* repeat what was typed */
8377 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008378 /* Vi repeats the insert without replacing characters. */
8379 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8380 State &= ~REPLACE_FLAG;
8381
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 (void)start_redo_ins();
8383 if (cmdchar == 'r' || cmdchar == 'v')
8384 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8385 ++RedrawingDisabled;
8386 disabled_redraw = TRUE;
8387 return FALSE; /* repeat the insert */
8388 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008389 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 undisplay_dollar();
8391 }
8392
8393 /* When an autoindent was removed, curswant stays after the
8394 * indent */
8395 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8396 curwin->w_set_curswant = TRUE;
8397
8398 /* Remember the last Insert position in the '^ mark. */
8399 if (!cmdmod.keepjumps)
8400 curbuf->b_last_insert = curwin->w_cursor;
8401
8402 /*
8403 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008404 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008406 if (!nomove
8407 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008408#ifdef FEAT_VIRTUALEDIT
8409 || curwin->w_cursor.coladd > 0
8410#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008411 )
8412 && (restart_edit == NUL
8413 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008415 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008417 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418#ifdef FEAT_RIGHTLEFT
8419 && !revins_on
8420#endif
8421 )
8422 {
8423#ifdef FEAT_VIRTUALEDIT
8424 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8425 {
8426 oneleft();
8427 if (restart_edit != NUL)
8428 ++curwin->w_cursor.coladd;
8429 }
8430 else
8431#endif
8432 {
8433 --curwin->w_cursor.col;
8434#ifdef FEAT_MBYTE
8435 /* Correct cursor for multi-byte character. */
8436 if (has_mbyte)
8437 mb_adjust_cursor();
8438#endif
8439 }
8440 }
8441
8442#ifdef USE_IM_CONTROL
8443 /* Disable IM to allow typing English directly for Normal mode commands.
8444 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8445 * well). */
8446 if (!(State & LANGMAP))
8447 im_save_status(&curbuf->b_p_iminsert);
8448 im_set_active(FALSE);
8449#endif
8450
8451 State = NORMAL;
8452 /* need to position cursor again (e.g. when on a TAB ) */
8453 changed_cline_bef_curs();
8454
8455#ifdef FEAT_MOUSE
8456 setmouse();
8457#endif
8458#ifdef CURSOR_SHAPE
8459 ui_cursor_shape(); /* may show different cursor shape */
8460#endif
8461
8462 /*
8463 * When recording or for CTRL-O, need to display the new mode.
8464 * Otherwise remove the mode message.
8465 */
8466 if (Recording || restart_edit != NUL)
8467 showmode();
8468 else if (p_smd)
8469 MSG("");
8470
8471 return TRUE; /* exit Insert mode */
8472}
8473
8474#ifdef FEAT_RIGHTLEFT
8475/*
8476 * Toggle language: hkmap and revins_on.
8477 * Move to end of reverse inserted text.
8478 */
8479 static void
8480ins_ctrl_()
8481{
8482 if (revins_on && revins_chars && revins_scol >= 0)
8483 {
8484 while (gchar_cursor() != NUL && revins_chars--)
8485 ++curwin->w_cursor.col;
8486 }
8487 p_ri = !p_ri;
8488 revins_on = (State == INSERT && p_ri);
8489 if (revins_on)
8490 {
8491 revins_scol = curwin->w_cursor.col;
8492 revins_legal++;
8493 revins_chars = 0;
8494 undisplay_dollar();
8495 }
8496 else
8497 revins_scol = -1;
8498#ifdef FEAT_FKMAP
8499 if (p_altkeymap)
8500 {
8501 /*
8502 * to be consistent also for redo command, using '.'
8503 * set arrow_used to true and stop it - causing to redo
8504 * characters entered in one mode (normal/reverse insert).
8505 */
8506 arrow_used = TRUE;
8507 (void)stop_arrow();
8508 p_fkmap = curwin->w_p_rl ^ p_ri;
8509 if (p_fkmap && p_ri)
8510 State = INSERT;
8511 }
8512 else
8513#endif
8514 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8515 showmode();
8516}
8517#endif
8518
8519#ifdef FEAT_VISUAL
8520/*
8521 * If 'keymodel' contains "startsel", may start selection.
8522 * Returns TRUE when a CTRL-O and other keys stuffed.
8523 */
8524 static int
8525ins_start_select(c)
8526 int c;
8527{
8528 if (km_startsel)
8529 switch (c)
8530 {
8531 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 case K_PAGEUP:
8534 case K_KPAGEUP:
8535 case K_PAGEDOWN:
8536 case K_KPAGEDOWN:
8537# ifdef MACOS
8538 case K_LEFT:
8539 case K_RIGHT:
8540 case K_UP:
8541 case K_DOWN:
8542 case K_END:
8543 case K_HOME:
8544# endif
8545 if (!(mod_mask & MOD_MASK_SHIFT))
8546 break;
8547 /* FALLTHROUGH */
8548 case K_S_LEFT:
8549 case K_S_RIGHT:
8550 case K_S_UP:
8551 case K_S_DOWN:
8552 case K_S_END:
8553 case K_S_HOME:
8554 /* Start selection right away, the cursor can move with
8555 * CTRL-O when beyond the end of the line. */
8556 start_selection();
8557
8558 /* Execute the key in (insert) Select mode. */
8559 stuffcharReadbuff(Ctrl_O);
8560 if (mod_mask)
8561 {
8562 char_u buf[4];
8563
8564 buf[0] = K_SPECIAL;
8565 buf[1] = KS_MODIFIER;
8566 buf[2] = mod_mask;
8567 buf[3] = NUL;
8568 stuffReadbuff(buf);
8569 }
8570 stuffcharReadbuff(c);
8571 return TRUE;
8572 }
8573 return FALSE;
8574}
8575#endif
8576
8577/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008578 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008579 */
8580 static void
8581ins_insert(replaceState)
8582 int replaceState;
8583{
8584#ifdef FEAT_FKMAP
8585 if (p_fkmap && p_ri)
8586 {
8587 beep_flush();
8588 EMSG(farsi_text_3); /* encoded in Farsi */
8589 return;
8590 }
8591#endif
8592
8593#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008594# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008595 set_vim_var_string(VV_INSERTMODE,
8596 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008597# ifdef FEAT_VREPLACE
8598 replaceState == VREPLACE ? "v" :
8599# endif
8600 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008601# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008602 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8603#endif
8604 if (State & REPLACE_FLAG)
8605 State = INSERT | (State & LANGMAP);
8606 else
8607 State = replaceState | (State & LANGMAP);
8608 AppendCharToRedobuff(K_INS);
8609 showmode();
8610#ifdef CURSOR_SHAPE
8611 ui_cursor_shape(); /* may show different cursor shape */
8612#endif
8613}
8614
8615/*
8616 * Pressed CTRL-O in Insert mode.
8617 */
8618 static void
8619ins_ctrl_o()
8620{
8621#ifdef FEAT_VREPLACE
8622 if (State & VREPLACE_FLAG)
8623 restart_edit = 'V';
8624 else
8625#endif
8626 if (State & REPLACE_FLAG)
8627 restart_edit = 'R';
8628 else
8629 restart_edit = 'I';
8630#ifdef FEAT_VIRTUALEDIT
8631 if (virtual_active())
8632 ins_at_eol = FALSE; /* cursor always keeps its column */
8633 else
8634#endif
8635 ins_at_eol = (gchar_cursor() == NUL);
8636}
8637
8638/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639 * If the cursor is on an indent, ^T/^D insert/delete one
8640 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008641 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 * with vi. But vi only supports ^T and ^D after an
8643 * autoindent, we support it everywhere.
8644 */
8645 static void
8646ins_shift(c, lastc)
8647 int c;
8648 int lastc;
8649{
8650 if (stop_arrow() == FAIL)
8651 return;
8652 AppendCharToRedobuff(c);
8653
8654 /*
8655 * 0^D and ^^D: remove all indent.
8656 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008657 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8658 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008659 {
8660 --curwin->w_cursor.col;
8661 (void)del_char(FALSE); /* delete the '^' or '0' */
8662 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8663 if (State & REPLACE_FLAG)
8664 replace_pop_ins();
8665 if (lastc == '^')
8666 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008667 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 }
8669 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008670 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671
8672 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8673 did_ai = FALSE;
8674#ifdef FEAT_SMARTINDENT
8675 did_si = FALSE;
8676 can_si = FALSE;
8677 can_si_back = FALSE;
8678#endif
8679#ifdef FEAT_CINDENT
8680 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8681#endif
8682}
8683
8684 static void
8685ins_del()
8686{
8687 int temp;
8688
8689 if (stop_arrow() == FAIL)
8690 return;
8691 if (gchar_cursor() == NUL) /* delete newline */
8692 {
8693 temp = curwin->w_cursor.col;
8694 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar81340392012-06-06 16:12:59 +02008695 || do_join(2, FALSE, TRUE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 vim_beep();
8697 else
8698 curwin->w_cursor.col = temp;
8699 }
8700 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8701 vim_beep();
8702 did_ai = FALSE;
8703#ifdef FEAT_SMARTINDENT
8704 did_si = FALSE;
8705 can_si = FALSE;
8706 can_si_back = FALSE;
8707#endif
8708 AppendCharToRedobuff(K_DEL);
8709}
8710
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008711static void ins_bs_one __ARGS((colnr_T *vcolp));
8712
8713/*
8714 * Delete one character for ins_bs().
8715 */
8716 static void
8717ins_bs_one(vcolp)
8718 colnr_T *vcolp;
8719{
8720 dec_cursor();
8721 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8722 if (State & REPLACE_FLAG)
8723 {
8724 /* Don't delete characters before the insert point when in
8725 * Replace mode */
8726 if (curwin->w_cursor.lnum != Insstart.lnum
8727 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008728 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008729 }
8730 else
8731 (void)del_char(FALSE);
8732}
8733
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734/*
8735 * Handle Backspace, delete-word and delete-line in Insert mode.
8736 * Return TRUE when backspace was actually used.
8737 */
8738 static int
8739ins_bs(c, mode, inserted_space_p)
8740 int c;
8741 int mode;
8742 int *inserted_space_p;
8743{
8744 linenr_T lnum;
8745 int cc;
8746 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008747 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748 colnr_T mincol;
8749 int did_backspace = FALSE;
8750 int in_indent;
8751 int oldState;
8752#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008753 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754#endif
8755
8756 /*
8757 * can't delete anything in an empty file
8758 * can't backup past first character in buffer
8759 * can't backup past starting point unless 'backspace' > 1
8760 * can backup to a previous line if 'backspace' == 0
8761 */
8762 if ( bufempty()
8763 || (
8764#ifdef FEAT_RIGHTLEFT
8765 !revins_on &&
8766#endif
8767 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8768 || (!can_bs(BS_START)
8769 && (arrow_used
8770 || (curwin->w_cursor.lnum == Insstart.lnum
8771 && curwin->w_cursor.col <= Insstart.col)))
8772 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8773 && curwin->w_cursor.col <= ai_col)
8774 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8775 {
8776 vim_beep();
8777 return FALSE;
8778 }
8779
8780 if (stop_arrow() == FAIL)
8781 return FALSE;
8782 in_indent = inindent(0);
8783#ifdef FEAT_CINDENT
8784 if (in_indent)
8785 can_cindent = FALSE;
8786#endif
8787#ifdef FEAT_COMMENTS
8788 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8789#endif
8790#ifdef FEAT_RIGHTLEFT
8791 if (revins_on) /* put cursor after last inserted char */
8792 inc_cursor();
8793#endif
8794
8795#ifdef FEAT_VIRTUALEDIT
8796 /* Virtualedit:
8797 * BACKSPACE_CHAR eats a virtual space
8798 * BACKSPACE_WORD eats all coladd
8799 * BACKSPACE_LINE eats all coladd and keeps going
8800 */
8801 if (curwin->w_cursor.coladd > 0)
8802 {
8803 if (mode == BACKSPACE_CHAR)
8804 {
8805 --curwin->w_cursor.coladd;
8806 return TRUE;
8807 }
8808 if (mode == BACKSPACE_WORD)
8809 {
8810 curwin->w_cursor.coladd = 0;
8811 return TRUE;
8812 }
8813 curwin->w_cursor.coladd = 0;
8814 }
8815#endif
8816
8817 /*
8818 * delete newline!
8819 */
8820 if (curwin->w_cursor.col == 0)
8821 {
8822 lnum = Insstart.lnum;
8823 if (curwin->w_cursor.lnum == Insstart.lnum
8824#ifdef FEAT_RIGHTLEFT
8825 || revins_on
8826#endif
8827 )
8828 {
8829 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8830 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8831 return FALSE;
8832 --Insstart.lnum;
8833 Insstart.col = MAXCOL;
8834 }
8835 /*
8836 * In replace mode:
8837 * cc < 0: NL was inserted, delete it
8838 * cc >= 0: NL was replaced, put original characters back
8839 */
8840 cc = -1;
8841 if (State & REPLACE_FLAG)
8842 cc = replace_pop(); /* returns -1 if NL was inserted */
8843 /*
8844 * In replace mode, in the line we started replacing, we only move the
8845 * cursor.
8846 */
8847 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8848 {
8849 dec_cursor();
8850 }
8851 else
8852 {
8853#ifdef FEAT_VREPLACE
8854 if (!(State & VREPLACE_FLAG)
8855 || curwin->w_cursor.lnum > orig_line_count)
8856#endif
8857 {
8858 temp = gchar_cursor(); /* remember current char */
8859 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008860
8861 /* When "aw" is in 'formatoptions' we must delete the space at
8862 * the end of the line, otherwise the line will be broken
8863 * again when auto-formatting. */
8864 if (has_format_option(FO_AUTO)
8865 && has_format_option(FO_WHITE_PAR))
8866 {
8867 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8868 TRUE);
8869 int len;
8870
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008871 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008872 if (len > 0 && ptr[len - 1] == ' ')
8873 ptr[len - 1] = NUL;
8874 }
8875
Bram Moolenaar81340392012-06-06 16:12:59 +02008876 (void)do_join(2, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 if (temp == NUL && gchar_cursor() != NUL)
8878 inc_cursor();
8879 }
8880#ifdef FEAT_VREPLACE
8881 else
8882 dec_cursor();
8883#endif
8884
8885 /*
8886 * In REPLACE mode we have to put back the text that was replaced
8887 * by the NL. On the replace stack is first a NUL-terminated
8888 * sequence of characters that were deleted and then the
8889 * characters that NL replaced.
8890 */
8891 if (State & REPLACE_FLAG)
8892 {
8893 /*
8894 * Do the next ins_char() in NORMAL state, to
8895 * prevent ins_char() from replacing characters and
8896 * avoiding showmatch().
8897 */
8898 oldState = State;
8899 State = NORMAL;
8900 /*
8901 * restore characters (blanks) deleted after cursor
8902 */
8903 while (cc > 0)
8904 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008905 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906#ifdef FEAT_MBYTE
8907 mb_replace_pop_ins(cc);
8908#else
8909 ins_char(cc);
8910#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008911 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 cc = replace_pop();
8913 }
8914 /* restore the characters that NL replaced */
8915 replace_pop_ins();
8916 State = oldState;
8917 }
8918 }
8919 did_ai = FALSE;
8920 }
8921 else
8922 {
8923 /*
8924 * Delete character(s) before the cursor.
8925 */
8926#ifdef FEAT_RIGHTLEFT
8927 if (revins_on) /* put cursor on last inserted char */
8928 dec_cursor();
8929#endif
8930 mincol = 0;
8931 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008932 if (mode == BACKSPACE_LINE
8933 && (curbuf->b_p_ai
8934#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008935 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008936#endif
8937 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938#ifdef FEAT_RIGHTLEFT
8939 && !revins_on
8940#endif
8941 )
8942 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008943 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008945 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008947 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008948 }
8949
8950 /*
8951 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8952 */
8953 if ( mode == BACKSPACE_CHAR
8954 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008955 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008956 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 && (*(ml_get_cursor() - 1) == TAB
8958 || (*(ml_get_cursor() - 1) == ' '
8959 && (!*inserted_space_p
8960 || arrow_used))))))
8961 {
8962 int ts;
8963 colnr_T vcol;
8964 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008965 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966
8967 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008968 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008969 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008971 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 /* Compute the virtual column where we want to be. Since
8973 * 'showbreak' may get in the way, need to get the last column of
8974 * the previous character. */
8975 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008976 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 dec_cursor();
8978 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8979 inc_cursor();
8980 want_vcol = (want_vcol / ts) * ts;
8981
8982 /* delete characters until we are at or before want_vcol */
8983 while (vcol > want_vcol
8984 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008985 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986
8987 /* insert extra spaces until we are at want_vcol */
8988 while (vcol < want_vcol)
8989 {
8990 /* Remember the first char we inserted */
8991 if (curwin->w_cursor.lnum == Insstart.lnum
8992 && curwin->w_cursor.col < Insstart.col)
8993 Insstart.col = curwin->w_cursor.col;
8994
8995#ifdef FEAT_VREPLACE
8996 if (State & VREPLACE_FLAG)
8997 ins_char(' ');
8998 else
8999#endif
9000 {
9001 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009002 if ((State & REPLACE_FLAG))
9003 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 }
9005 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9006 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009007
9008 /* If we are now back where we started delete one character. Can
9009 * happen when using 'sts' and 'linebreak'. */
9010 if (vcol >= start_vcol)
9011 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009012 }
9013
9014 /*
9015 * Delete upto starting point, start of line or previous word.
9016 */
9017 else do
9018 {
9019#ifdef FEAT_RIGHTLEFT
9020 if (!revins_on) /* put cursor on char to be deleted */
9021#endif
9022 dec_cursor();
9023
9024 /* start of word? */
9025 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
9026 {
9027 mode = BACKSPACE_WORD_NOT_SPACE;
9028 temp = vim_iswordc(gchar_cursor());
9029 }
9030 /* end of word? */
9031 else if (mode == BACKSPACE_WORD_NOT_SPACE
9032 && (vim_isspace(cc = gchar_cursor())
9033 || vim_iswordc(cc) != temp))
9034 {
9035#ifdef FEAT_RIGHTLEFT
9036 if (!revins_on)
9037#endif
9038 inc_cursor();
9039#ifdef FEAT_RIGHTLEFT
9040 else if (State & REPLACE_FLAG)
9041 dec_cursor();
9042#endif
9043 break;
9044 }
9045 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009046 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009047 else
9048 {
9049#ifdef FEAT_MBYTE
9050 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009051 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009052#endif
9053 (void)del_char(FALSE);
9054#ifdef FEAT_MBYTE
9055 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009056 * If there are combining characters and 'delcombine' is set
9057 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058 * character.
9059 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009060 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 inc_cursor();
9062#endif
9063#ifdef FEAT_RIGHTLEFT
9064 if (revins_chars)
9065 {
9066 revins_chars--;
9067 revins_legal++;
9068 }
9069 if (revins_on && gchar_cursor() == NUL)
9070 break;
9071#endif
9072 }
9073 /* Just a single backspace?: */
9074 if (mode == BACKSPACE_CHAR)
9075 break;
9076 } while (
9077#ifdef FEAT_RIGHTLEFT
9078 revins_on ||
9079#endif
9080 (curwin->w_cursor.col > mincol
9081 && (curwin->w_cursor.lnum != Insstart.lnum
9082 || curwin->w_cursor.col != Insstart.col)));
9083 did_backspace = TRUE;
9084 }
9085#ifdef FEAT_SMARTINDENT
9086 did_si = FALSE;
9087 can_si = FALSE;
9088 can_si_back = FALSE;
9089#endif
9090 if (curwin->w_cursor.col <= 1)
9091 did_ai = FALSE;
9092 /*
9093 * It's a little strange to put backspaces into the redo
9094 * buffer, but it makes auto-indent a lot easier to deal
9095 * with.
9096 */
9097 AppendCharToRedobuff(c);
9098
9099 /* If deleted before the insertion point, adjust it */
9100 if (curwin->w_cursor.lnum == Insstart.lnum
9101 && curwin->w_cursor.col < Insstart.col)
9102 Insstart.col = curwin->w_cursor.col;
9103
9104 /* vi behaviour: the cursor moves backward but the character that
9105 * was there remains visible
9106 * Vim behaviour: the cursor moves backward and the character that
9107 * was there is erased from the screen.
9108 * We can emulate the vi behaviour by pretending there is a dollar
9109 * displayed even when there isn't.
9110 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009111 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112 dollar_vcol = curwin->w_virtcol;
9113
Bram Moolenaarce3be472008-01-14 19:12:28 +00009114#ifdef FEAT_FOLDING
9115 /* When deleting a char the cursor line must never be in a closed fold.
9116 * E.g., when 'foldmethod' is indent and deleting the first non-white
9117 * char before a Tab. */
9118 if (did_backspace)
9119 foldOpenCursor();
9120#endif
9121
Bram Moolenaar071d4272004-06-13 20:20:40 +00009122 return did_backspace;
9123}
9124
9125#ifdef FEAT_MOUSE
9126 static void
9127ins_mouse(c)
9128 int c;
9129{
9130 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009131 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132
9133# ifdef FEAT_GUI
9134 /* When GUI is active, also move/paste when 'mouse' is empty */
9135 if (!gui.in_use)
9136# endif
9137 if (!mouse_has(MOUSE_INSERT))
9138 return;
9139
9140 undisplay_dollar();
9141 tpos = curwin->w_cursor;
9142 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9143 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009144#ifdef FEAT_WINDOWS
9145 win_T *new_curwin = curwin;
9146
9147 if (curwin != old_curwin && win_valid(old_curwin))
9148 {
9149 /* Mouse took us to another window. We need to go back to the
9150 * previous one to stop insert there properly. */
9151 curwin = old_curwin;
9152 curbuf = curwin->w_buffer;
9153 }
9154#endif
9155 start_arrow(curwin == old_curwin ? &tpos : NULL);
9156#ifdef FEAT_WINDOWS
9157 if (curwin != new_curwin && win_valid(new_curwin))
9158 {
9159 curwin = new_curwin;
9160 curbuf = curwin->w_buffer;
9161 }
9162#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009163# ifdef FEAT_CINDENT
9164 can_cindent = TRUE;
9165# endif
9166 }
9167
9168#ifdef FEAT_WINDOWS
9169 /* redraw status lines (in case another window became active) */
9170 redraw_statuslines();
9171#endif
9172}
9173
9174 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009175ins_mousescroll(dir)
9176 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009177{
9178 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009179# if defined(FEAT_WINDOWS)
9180 win_T *old_curwin = curwin;
9181# endif
9182# ifdef FEAT_INS_EXPAND
9183 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184# endif
9185
9186 tpos = curwin->w_cursor;
9187
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009188# ifdef FEAT_WINDOWS
9189 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 {
9191 int row, col;
9192
9193 row = mouse_row;
9194 col = mouse_col;
9195
9196 /* find the window at the pointer coordinates */
9197 curwin = mouse_find_win(&row, &col);
9198 curbuf = curwin->w_buffer;
9199 }
9200 if (curwin == old_curwin)
9201# endif
9202 undisplay_dollar();
9203
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009204# ifdef FEAT_INS_EXPAND
9205 /* Don't scroll the window in which completion is being done. */
9206 if (!pum_visible()
9207# if defined(FEAT_WINDOWS)
9208 || curwin != old_curwin
9209# endif
9210 )
9211# endif
9212 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009213 if (dir == MSCR_DOWN || dir == MSCR_UP)
9214 {
9215 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9216 scroll_redraw(dir,
9217 (long)(curwin->w_botline - curwin->w_topline));
9218 else
9219 scroll_redraw(dir, 3L);
9220 }
9221#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009222 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009223 {
9224 int val, step = 6;
9225
9226 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9227 step = W_WIDTH(curwin);
9228 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9229 if (val < 0)
9230 val = 0;
9231 gui_do_horiz_scroll(val, TRUE);
9232 }
9233#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009234# ifdef FEAT_INS_EXPAND
9235 did_scroll = TRUE;
9236# endif
9237 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009239# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 curwin->w_redr_status = TRUE;
9241
9242 curwin = old_curwin;
9243 curbuf = curwin->w_buffer;
9244# endif
9245
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009246# ifdef FEAT_INS_EXPAND
9247 /* The popup menu may overlay the window, need to redraw it.
9248 * TODO: Would be more efficient to only redraw the windows that are
9249 * overlapped by the popup menu. */
9250 if (pum_visible() && did_scroll)
9251 {
9252 redraw_all_later(NOT_VALID);
9253 ins_compl_show_pum();
9254 }
9255# endif
9256
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 if (!equalpos(curwin->w_cursor, tpos))
9258 {
9259 start_arrow(&tpos);
9260# ifdef FEAT_CINDENT
9261 can_cindent = TRUE;
9262# endif
9263 }
9264}
9265#endif
9266
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009267#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009268 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009269ins_tabline(c)
9270 int c;
9271{
9272 /* We will be leaving the current window, unless closing another tab. */
9273 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9274 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9275 {
9276 undisplay_dollar();
9277 start_arrow(&curwin->w_cursor);
9278# ifdef FEAT_CINDENT
9279 can_cindent = TRUE;
9280# endif
9281 }
9282
9283 if (c == K_TABLINE)
9284 goto_tabpage(current_tab);
9285 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009286 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009287 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009288 redraw_statuslines(); /* will redraw the tabline when needed */
9289 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009290}
9291#endif
9292
9293#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009294 void
9295ins_scroll()
9296{
9297 pos_T tpos;
9298
9299 undisplay_dollar();
9300 tpos = curwin->w_cursor;
9301 if (gui_do_scroll())
9302 {
9303 start_arrow(&tpos);
9304# ifdef FEAT_CINDENT
9305 can_cindent = TRUE;
9306# endif
9307 }
9308}
9309
9310 void
9311ins_horscroll()
9312{
9313 pos_T tpos;
9314
9315 undisplay_dollar();
9316 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009317 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318 {
9319 start_arrow(&tpos);
9320# ifdef FEAT_CINDENT
9321 can_cindent = TRUE;
9322# endif
9323 }
9324}
9325#endif
9326
9327 static void
9328ins_left()
9329{
9330 pos_T tpos;
9331
9332#ifdef FEAT_FOLDING
9333 if ((fdo_flags & FDO_HOR) && KeyTyped)
9334 foldOpenCursor();
9335#endif
9336 undisplay_dollar();
9337 tpos = curwin->w_cursor;
9338 if (oneleft() == OK)
9339 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009340#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9341 /* Only call start_arrow() when not busy with preediting, it will
9342 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9343 if (!im_is_preediting())
9344#endif
9345 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009346#ifdef FEAT_RIGHTLEFT
9347 /* If exit reversed string, position is fixed */
9348 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9349 revins_legal++;
9350 revins_chars++;
9351#endif
9352 }
9353
9354 /*
9355 * if 'whichwrap' set for cursor in insert mode may go to
9356 * previous line
9357 */
9358 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9359 {
9360 start_arrow(&tpos);
9361 --(curwin->w_cursor.lnum);
9362 coladvance((colnr_T)MAXCOL);
9363 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9364 }
9365 else
9366 vim_beep();
9367}
9368
9369 static void
9370ins_home(c)
9371 int c;
9372{
9373 pos_T tpos;
9374
9375#ifdef FEAT_FOLDING
9376 if ((fdo_flags & FDO_HOR) && KeyTyped)
9377 foldOpenCursor();
9378#endif
9379 undisplay_dollar();
9380 tpos = curwin->w_cursor;
9381 if (c == K_C_HOME)
9382 curwin->w_cursor.lnum = 1;
9383 curwin->w_cursor.col = 0;
9384#ifdef FEAT_VIRTUALEDIT
9385 curwin->w_cursor.coladd = 0;
9386#endif
9387 curwin->w_curswant = 0;
9388 start_arrow(&tpos);
9389}
9390
9391 static void
9392ins_end(c)
9393 int c;
9394{
9395 pos_T tpos;
9396
9397#ifdef FEAT_FOLDING
9398 if ((fdo_flags & FDO_HOR) && KeyTyped)
9399 foldOpenCursor();
9400#endif
9401 undisplay_dollar();
9402 tpos = curwin->w_cursor;
9403 if (c == K_C_END)
9404 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9405 coladvance((colnr_T)MAXCOL);
9406 curwin->w_curswant = MAXCOL;
9407
9408 start_arrow(&tpos);
9409}
9410
9411 static void
9412ins_s_left()
9413{
9414#ifdef FEAT_FOLDING
9415 if ((fdo_flags & FDO_HOR) && KeyTyped)
9416 foldOpenCursor();
9417#endif
9418 undisplay_dollar();
9419 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9420 {
9421 start_arrow(&curwin->w_cursor);
9422 (void)bck_word(1L, FALSE, FALSE);
9423 curwin->w_set_curswant = TRUE;
9424 }
9425 else
9426 vim_beep();
9427}
9428
9429 static void
9430ins_right()
9431{
9432#ifdef FEAT_FOLDING
9433 if ((fdo_flags & FDO_HOR) && KeyTyped)
9434 foldOpenCursor();
9435#endif
9436 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009437 if (gchar_cursor() != NUL
9438#ifdef FEAT_VIRTUALEDIT
9439 || virtual_active()
9440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009441 )
9442 {
9443 start_arrow(&curwin->w_cursor);
9444 curwin->w_set_curswant = TRUE;
9445#ifdef FEAT_VIRTUALEDIT
9446 if (virtual_active())
9447 oneright();
9448 else
9449#endif
9450 {
9451#ifdef FEAT_MBYTE
9452 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009453 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009454 else
9455#endif
9456 ++curwin->w_cursor.col;
9457 }
9458
9459#ifdef FEAT_RIGHTLEFT
9460 revins_legal++;
9461 if (revins_chars)
9462 revins_chars--;
9463#endif
9464 }
9465 /* if 'whichwrap' set for cursor in insert mode, may move the
9466 * cursor to the next line */
9467 else if (vim_strchr(p_ww, ']') != NULL
9468 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9469 {
9470 start_arrow(&curwin->w_cursor);
9471 curwin->w_set_curswant = TRUE;
9472 ++curwin->w_cursor.lnum;
9473 curwin->w_cursor.col = 0;
9474 }
9475 else
9476 vim_beep();
9477}
9478
9479 static void
9480ins_s_right()
9481{
9482#ifdef FEAT_FOLDING
9483 if ((fdo_flags & FDO_HOR) && KeyTyped)
9484 foldOpenCursor();
9485#endif
9486 undisplay_dollar();
9487 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9488 || gchar_cursor() != NUL)
9489 {
9490 start_arrow(&curwin->w_cursor);
9491 (void)fwd_word(1L, FALSE, 0);
9492 curwin->w_set_curswant = TRUE;
9493 }
9494 else
9495 vim_beep();
9496}
9497
9498 static void
9499ins_up(startcol)
9500 int startcol; /* when TRUE move to Insstart.col */
9501{
9502 pos_T tpos;
9503 linenr_T old_topline = curwin->w_topline;
9504#ifdef FEAT_DIFF
9505 int old_topfill = curwin->w_topfill;
9506#endif
9507
9508 undisplay_dollar();
9509 tpos = curwin->w_cursor;
9510 if (cursor_up(1L, TRUE) == OK)
9511 {
9512 if (startcol)
9513 coladvance(getvcol_nolist(&Insstart));
9514 if (old_topline != curwin->w_topline
9515#ifdef FEAT_DIFF
9516 || old_topfill != curwin->w_topfill
9517#endif
9518 )
9519 redraw_later(VALID);
9520 start_arrow(&tpos);
9521#ifdef FEAT_CINDENT
9522 can_cindent = TRUE;
9523#endif
9524 }
9525 else
9526 vim_beep();
9527}
9528
9529 static void
9530ins_pageup()
9531{
9532 pos_T tpos;
9533
9534 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009535
9536#ifdef FEAT_WINDOWS
9537 if (mod_mask & MOD_MASK_CTRL)
9538 {
9539 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009540 if (first_tabpage->tp_next != NULL)
9541 {
9542 start_arrow(&curwin->w_cursor);
9543 goto_tabpage(-1);
9544 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009545 return;
9546 }
9547#endif
9548
Bram Moolenaar071d4272004-06-13 20:20:40 +00009549 tpos = curwin->w_cursor;
9550 if (onepage(BACKWARD, 1L) == OK)
9551 {
9552 start_arrow(&tpos);
9553#ifdef FEAT_CINDENT
9554 can_cindent = TRUE;
9555#endif
9556 }
9557 else
9558 vim_beep();
9559}
9560
9561 static void
9562ins_down(startcol)
9563 int startcol; /* when TRUE move to Insstart.col */
9564{
9565 pos_T tpos;
9566 linenr_T old_topline = curwin->w_topline;
9567#ifdef FEAT_DIFF
9568 int old_topfill = curwin->w_topfill;
9569#endif
9570
9571 undisplay_dollar();
9572 tpos = curwin->w_cursor;
9573 if (cursor_down(1L, TRUE) == OK)
9574 {
9575 if (startcol)
9576 coladvance(getvcol_nolist(&Insstart));
9577 if (old_topline != curwin->w_topline
9578#ifdef FEAT_DIFF
9579 || old_topfill != curwin->w_topfill
9580#endif
9581 )
9582 redraw_later(VALID);
9583 start_arrow(&tpos);
9584#ifdef FEAT_CINDENT
9585 can_cindent = TRUE;
9586#endif
9587 }
9588 else
9589 vim_beep();
9590}
9591
9592 static void
9593ins_pagedown()
9594{
9595 pos_T tpos;
9596
9597 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009598
9599#ifdef FEAT_WINDOWS
9600 if (mod_mask & MOD_MASK_CTRL)
9601 {
9602 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009603 if (first_tabpage->tp_next != NULL)
9604 {
9605 start_arrow(&curwin->w_cursor);
9606 goto_tabpage(0);
9607 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009608 return;
9609 }
9610#endif
9611
Bram Moolenaar071d4272004-06-13 20:20:40 +00009612 tpos = curwin->w_cursor;
9613 if (onepage(FORWARD, 1L) == OK)
9614 {
9615 start_arrow(&tpos);
9616#ifdef FEAT_CINDENT
9617 can_cindent = TRUE;
9618#endif
9619 }
9620 else
9621 vim_beep();
9622}
9623
9624#ifdef FEAT_DND
9625 static void
9626ins_drop()
9627{
9628 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9629}
9630#endif
9631
9632/*
9633 * Handle TAB in Insert or Replace mode.
9634 * Return TRUE when the TAB needs to be inserted like a normal character.
9635 */
9636 static int
9637ins_tab()
9638{
9639 int ind;
9640 int i;
9641 int temp;
9642
9643 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9644 Insstart_blank_vcol = get_nolist_virtcol();
9645 if (echeck_abbr(TAB + ABBR_OFF))
9646 return FALSE;
9647
9648 ind = inindent(0);
9649#ifdef FEAT_CINDENT
9650 if (ind)
9651 can_cindent = FALSE;
9652#endif
9653
9654 /*
9655 * When nothing special, insert TAB like a normal character
9656 */
9657 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009658 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009659 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660 return TRUE;
9661
9662 if (stop_arrow() == FAIL)
9663 return TRUE;
9664
9665 did_ai = FALSE;
9666#ifdef FEAT_SMARTINDENT
9667 did_si = FALSE;
9668 can_si = FALSE;
9669 can_si_back = FALSE;
9670#endif
9671 AppendToRedobuff((char_u *)"\t");
9672
9673 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009674 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009675 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9676 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 else /* otherwise use 'tabstop' */
9678 temp = (int)curbuf->b_p_ts;
9679 temp -= get_nolist_virtcol() % temp;
9680
9681 /*
9682 * Insert the first space with ins_char(). It will delete one char in
9683 * replace mode. Insert the rest with ins_str(); it will not delete any
9684 * chars. For VREPLACE mode, we use ins_char() for all characters.
9685 */
9686 ins_char(' ');
9687 while (--temp > 0)
9688 {
9689#ifdef FEAT_VREPLACE
9690 if (State & VREPLACE_FLAG)
9691 ins_char(' ');
9692 else
9693#endif
9694 {
9695 ins_str((char_u *)" ");
9696 if (State & REPLACE_FLAG) /* no char replaced */
9697 replace_push(NUL);
9698 }
9699 }
9700
9701 /*
9702 * When 'expandtab' not set: Replace spaces by TABs where possible.
9703 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009704 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705 {
9706 char_u *ptr;
9707#ifdef FEAT_VREPLACE
9708 char_u *saved_line = NULL; /* init for GCC */
9709 pos_T pos;
9710#endif
9711 pos_T fpos;
9712 pos_T *cursor;
9713 colnr_T want_vcol, vcol;
9714 int change_col = -1;
9715 int save_list = curwin->w_p_list;
9716
9717 /*
9718 * Get the current line. For VREPLACE mode, don't make real changes
9719 * yet, just work on a copy of the line.
9720 */
9721#ifdef FEAT_VREPLACE
9722 if (State & VREPLACE_FLAG)
9723 {
9724 pos = curwin->w_cursor;
9725 cursor = &pos;
9726 saved_line = vim_strsave(ml_get_curline());
9727 if (saved_line == NULL)
9728 return FALSE;
9729 ptr = saved_line + pos.col;
9730 }
9731 else
9732#endif
9733 {
9734 ptr = ml_get_cursor();
9735 cursor = &curwin->w_cursor;
9736 }
9737
9738 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9739 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9740 curwin->w_p_list = FALSE;
9741
9742 /* Find first white before the cursor */
9743 fpos = curwin->w_cursor;
9744 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9745 {
9746 --fpos.col;
9747 --ptr;
9748 }
9749
9750 /* In Replace mode, don't change characters before the insert point. */
9751 if ((State & REPLACE_FLAG)
9752 && fpos.lnum == Insstart.lnum
9753 && fpos.col < Insstart.col)
9754 {
9755 ptr += Insstart.col - fpos.col;
9756 fpos.col = Insstart.col;
9757 }
9758
9759 /* compute virtual column numbers of first white and cursor */
9760 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9761 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9762
9763 /* Use as many TABs as possible. Beware of 'showbreak' and
9764 * 'linebreak' adding extra virtual columns. */
9765 while (vim_iswhite(*ptr))
9766 {
9767 i = lbr_chartabsize((char_u *)"\t", vcol);
9768 if (vcol + i > want_vcol)
9769 break;
9770 if (*ptr != TAB)
9771 {
9772 *ptr = TAB;
9773 if (change_col < 0)
9774 {
9775 change_col = fpos.col; /* Column of first change */
9776 /* May have to adjust Insstart */
9777 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9778 Insstart.col = fpos.col;
9779 }
9780 }
9781 ++fpos.col;
9782 ++ptr;
9783 vcol += i;
9784 }
9785
9786 if (change_col >= 0)
9787 {
9788 int repl_off = 0;
9789
9790 /* Skip over the spaces we need. */
9791 while (vcol < want_vcol && *ptr == ' ')
9792 {
9793 vcol += lbr_chartabsize(ptr, vcol);
9794 ++ptr;
9795 ++repl_off;
9796 }
9797 if (vcol > want_vcol)
9798 {
9799 /* Must have a char with 'showbreak' just before it. */
9800 --ptr;
9801 --repl_off;
9802 }
9803 fpos.col += repl_off;
9804
9805 /* Delete following spaces. */
9806 i = cursor->col - fpos.col;
9807 if (i > 0)
9808 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009809 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810 /* correct replace stack. */
9811 if ((State & REPLACE_FLAG)
9812#ifdef FEAT_VREPLACE
9813 && !(State & VREPLACE_FLAG)
9814#endif
9815 )
9816 for (temp = i; --temp >= 0; )
9817 replace_join(repl_off);
9818 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009819#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009820 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009821 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009822 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009823 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9824 (char_u *)"\t", 1);
9825 }
9826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009827 cursor->col -= i;
9828
9829#ifdef FEAT_VREPLACE
9830 /*
9831 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9832 * backspacing over the changed spacing and then inserting the new
9833 * spacing.
9834 */
9835 if (State & VREPLACE_FLAG)
9836 {
9837 /* Backspace from real cursor to change_col */
9838 backspace_until_column(change_col);
9839
9840 /* Insert each char in saved_line from changed_col to
9841 * ptr-cursor */
9842 ins_bytes_len(saved_line + change_col,
9843 cursor->col - change_col);
9844 }
9845#endif
9846 }
9847
9848#ifdef FEAT_VREPLACE
9849 if (State & VREPLACE_FLAG)
9850 vim_free(saved_line);
9851#endif
9852 curwin->w_p_list = save_list;
9853 }
9854
9855 return FALSE;
9856}
9857
9858/*
9859 * Handle CR or NL in insert mode.
9860 * Return TRUE when out of memory or can't undo.
9861 */
9862 static int
9863ins_eol(c)
9864 int c;
9865{
9866 int i;
9867
9868 if (echeck_abbr(c + ABBR_OFF))
9869 return FALSE;
9870 if (stop_arrow() == FAIL)
9871 return TRUE;
9872 undisplay_dollar();
9873
9874 /*
9875 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9876 * character under the cursor. Only push a NUL on the replace stack,
9877 * nothing to put back when the NL is deleted.
9878 */
9879 if ((State & REPLACE_FLAG)
9880#ifdef FEAT_VREPLACE
9881 && !(State & VREPLACE_FLAG)
9882#endif
9883 )
9884 replace_push(NUL);
9885
9886 /*
9887 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9888 * replacing the next line, so we push all of the characters left on the
9889 * line onto the replace stack. This is not done here though, it is done
9890 * in open_line().
9891 */
9892
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009893#ifdef FEAT_VIRTUALEDIT
9894 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9895 * CTRL-O). */
9896 if (virtual_active() && curwin->w_cursor.coladd > 0)
9897 coladvance(getviscol());
9898#endif
9899
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900#ifdef FEAT_RIGHTLEFT
9901# ifdef FEAT_FKMAP
9902 if (p_altkeymap && p_fkmap)
9903 fkmap(NL);
9904# endif
9905 /* NL in reverse insert will always start in the end of
9906 * current line. */
9907 if (revins_on)
9908 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9909#endif
9910
9911 AppendToRedobuff(NL_STR);
9912 i = open_line(FORWARD,
9913#ifdef FEAT_COMMENTS
9914 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9915#endif
9916 0, old_indent);
9917 old_indent = 0;
9918#ifdef FEAT_CINDENT
9919 can_cindent = TRUE;
9920#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009921#ifdef FEAT_FOLDING
9922 /* When inserting a line the cursor line must never be in a closed fold. */
9923 foldOpenCursor();
9924#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009925
9926 return (!i);
9927}
9928
9929#ifdef FEAT_DIGRAPHS
9930/*
9931 * Handle digraph in insert mode.
9932 * Returns character still to be inserted, or NUL when nothing remaining to be
9933 * done.
9934 */
9935 static int
9936ins_digraph()
9937{
9938 int c;
9939 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009940 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941
9942 pc_status = PC_STATUS_UNSET;
9943 if (redrawing() && !char_avail())
9944 {
9945 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009946 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947
9948 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009949 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009950#ifdef FEAT_CMDL_INFO
9951 add_to_showcmd_c(Ctrl_K);
9952#endif
9953 }
9954
9955#ifdef USE_ON_FLY_SCROLL
9956 dont_scroll = TRUE; /* disallow scrolling here */
9957#endif
9958
9959 /* don't map the digraph chars. This also prevents the
9960 * mode message to be deleted when ESC is hit */
9961 ++no_mapping;
9962 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009963 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 --no_mapping;
9965 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009966 if (did_putchar)
9967 /* when the line fits in 'columns' the '?' is at the start of the next
9968 * line and will not be removed by the redraw */
9969 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009970
Bram Moolenaar071d4272004-06-13 20:20:40 +00009971 if (IS_SPECIAL(c) || mod_mask) /* special key */
9972 {
9973#ifdef FEAT_CMDL_INFO
9974 clear_showcmd();
9975#endif
9976 insert_special(c, TRUE, FALSE);
9977 return NUL;
9978 }
9979 if (c != ESC)
9980 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009981 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009982 if (redrawing() && !char_avail())
9983 {
9984 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009985 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986
9987 if (char2cells(c) == 1)
9988 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009989 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009991 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009992 }
9993#ifdef FEAT_CMDL_INFO
9994 add_to_showcmd_c(c);
9995#endif
9996 }
9997 ++no_mapping;
9998 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009999 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000 --no_mapping;
10001 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010002 if (did_putchar)
10003 /* when the line fits in 'columns' the '?' is at the start of the
10004 * next line and will not be removed by a redraw */
10005 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010006 if (cc != ESC)
10007 {
10008 AppendToRedobuff((char_u *)CTRL_V_STR);
10009 c = getdigraph(c, cc, TRUE);
10010#ifdef FEAT_CMDL_INFO
10011 clear_showcmd();
10012#endif
10013 return c;
10014 }
10015 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010016#ifdef FEAT_CMDL_INFO
10017 clear_showcmd();
10018#endif
10019 return NUL;
10020}
10021#endif
10022
10023/*
10024 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10025 * Returns the char to be inserted, or NUL if none found.
10026 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010027 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000010028ins_copychar(lnum)
10029 linenr_T lnum;
10030{
10031 int c;
10032 int temp;
10033 char_u *ptr, *prev_ptr;
10034
10035 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10036 {
10037 vim_beep();
10038 return NUL;
10039 }
10040
10041 /* try to advance to the cursor column */
10042 temp = 0;
10043 ptr = ml_get(lnum);
10044 prev_ptr = ptr;
10045 validate_virtcol();
10046 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10047 {
10048 prev_ptr = ptr;
10049 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
10050 }
10051 if ((colnr_T)temp > curwin->w_virtcol)
10052 ptr = prev_ptr;
10053
10054#ifdef FEAT_MBYTE
10055 c = (*mb_ptr2char)(ptr);
10056#else
10057 c = *ptr;
10058#endif
10059 if (c == NUL)
10060 vim_beep();
10061 return c;
10062}
10063
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010064/*
10065 * CTRL-Y or CTRL-E typed in Insert mode.
10066 */
10067 static int
10068ins_ctrl_ey(tc)
10069 int tc;
10070{
10071 int c = tc;
10072
10073#ifdef FEAT_INS_EXPAND
10074 if (ctrl_x_mode == CTRL_X_SCROLL)
10075 {
10076 if (c == Ctrl_Y)
10077 scrolldown_clamp();
10078 else
10079 scrollup_clamp();
10080 redraw_later(VALID);
10081 }
10082 else
10083#endif
10084 {
10085 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10086 if (c != NUL)
10087 {
10088 long tw_save;
10089
10090 /* The character must be taken literally, insert like it
10091 * was typed after a CTRL-V, and pretend 'textwidth'
10092 * wasn't set. Digits, 'o' and 'x' are special after a
10093 * CTRL-V, don't use it for these. */
10094 if (c < 256 && !isalnum(c))
10095 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10096 tw_save = curbuf->b_p_tw;
10097 curbuf->b_p_tw = -1;
10098 insert_special(c, TRUE, FALSE);
10099 curbuf->b_p_tw = tw_save;
10100#ifdef FEAT_RIGHTLEFT
10101 revins_chars++;
10102 revins_legal++;
10103#endif
10104 c = Ctrl_V; /* pretend CTRL-V is last character */
10105 auto_format(FALSE, TRUE);
10106 }
10107 }
10108 return c;
10109}
10110
Bram Moolenaar071d4272004-06-13 20:20:40 +000010111#ifdef FEAT_SMARTINDENT
10112/*
10113 * Try to do some very smart auto-indenting.
10114 * Used when inserting a "normal" character.
10115 */
10116 static void
10117ins_try_si(c)
10118 int c;
10119{
10120 pos_T *pos, old_pos;
10121 char_u *ptr;
10122 int i;
10123 int temp;
10124
10125 /*
10126 * do some very smart indenting when entering '{' or '}'
10127 */
10128 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10129 {
10130 /*
10131 * for '}' set indent equal to indent of line containing matching '{'
10132 */
10133 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10134 {
10135 old_pos = curwin->w_cursor;
10136 /*
10137 * If the matching '{' has a ')' immediately before it (ignoring
10138 * white-space), then line up with the start of the line
10139 * containing the matching '(' if there is one. This handles the
10140 * case where an "if (..\n..) {" statement continues over multiple
10141 * lines -- webb
10142 */
10143 ptr = ml_get(pos->lnum);
10144 i = pos->col;
10145 if (i > 0) /* skip blanks before '{' */
10146 while (--i > 0 && vim_iswhite(ptr[i]))
10147 ;
10148 curwin->w_cursor.lnum = pos->lnum;
10149 curwin->w_cursor.col = i;
10150 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10151 curwin->w_cursor = *pos;
10152 i = get_indent();
10153 curwin->w_cursor = old_pos;
10154#ifdef FEAT_VREPLACE
10155 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010156 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010157 else
10158#endif
10159 (void)set_indent(i, SIN_CHANGED);
10160 }
10161 else if (curwin->w_cursor.col > 0)
10162 {
10163 /*
10164 * when inserting '{' after "O" reduce indent, but not
10165 * more than indent of previous line
10166 */
10167 temp = TRUE;
10168 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10169 {
10170 old_pos = curwin->w_cursor;
10171 i = get_indent();
10172 while (curwin->w_cursor.lnum > 1)
10173 {
10174 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10175
10176 /* ignore empty lines and lines starting with '#'. */
10177 if (*ptr != '#' && *ptr != NUL)
10178 break;
10179 }
10180 if (get_indent() >= i)
10181 temp = FALSE;
10182 curwin->w_cursor = old_pos;
10183 }
10184 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010185 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 }
10187 }
10188
10189 /*
10190 * set indent of '#' always to 0
10191 */
10192 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10193 {
10194 /* remember current indent for next line */
10195 old_indent = get_indent();
10196 (void)set_indent(0, SIN_CHANGED);
10197 }
10198
10199 /* Adjust ai_col, the char at this position can be deleted. */
10200 if (ai_col > curwin->w_cursor.col)
10201 ai_col = curwin->w_cursor.col;
10202}
10203#endif
10204
10205/*
10206 * Get the value that w_virtcol would have when 'list' is off.
10207 * Unless 'cpo' contains the 'L' flag.
10208 */
10209 static colnr_T
10210get_nolist_virtcol()
10211{
10212 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10213 return getvcol_nolist(&curwin->w_cursor);
10214 validate_virtcol();
10215 return curwin->w_virtcol;
10216}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010217
10218#ifdef FEAT_AUTOCMD
10219/*
10220 * Handle the InsertCharPre autocommand.
10221 * "c" is the character that was typed.
10222 * Return a pointer to allocated memory with the replacement string.
10223 * Return NULL to continue inserting "c".
10224 */
10225 static char_u *
10226do_insert_char_pre(c)
10227 int c;
10228{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010229 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010230 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010231
10232 /* Return quickly when there is nothing to do. */
10233 if (!has_insertcharpre())
10234 return NULL;
10235
Bram Moolenaar704984a2012-06-01 14:57:51 +020010236#ifdef FEAT_MBYTE
10237 if (has_mbyte)
10238 buf[(*mb_char2bytes)(c, buf)] = NUL;
10239 else
10240#endif
10241 {
10242 buf[0] = c;
10243 buf[1] = NUL;
10244 }
10245
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010246 /* Lock the text to avoid weird things from happening. */
10247 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010248 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010249
Bram Moolenaar704984a2012-06-01 14:57:51 +020010250 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010251 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010252 {
10253 /* Get the value of v:char. It may be empty or more than one
10254 * character. Only use it when changed, otherwise continue with the
10255 * original character to avoid breaking autoindent. */
10256 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10257 res = vim_strsave(get_vim_var_str(VV_CHAR));
10258 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010259
10260 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10261 --textlock;
10262
10263 return res;
10264}
10265#endif