blob: 88fd18af1d1cbc2b4639e123d879f5e676f0e5c2 [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 Moolenaar071d4272004-06-13 20:20:40 +0000202static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
203static 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 Moolenaar1e015462005-09-25 22:16:38 +0000385# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000386 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100387
388 /* Since Insert mode was not started yet a call to check_cursor_col()
389 * may have moved the cursor, especially with the "A" command. */
390 if (curwin->w_cursor.col != save_cursor.col
391 && curwin->w_cursor.lnum == save_cursor.lnum)
392 {
393 int save_state = State;
394
395 curwin->w_cursor = save_cursor;
396 State = INSERT;
397 check_cursor_col();
398 State = save_state;
399 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000400 }
401#endif
402
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200403#ifdef FEAT_CONCEAL
404 /* Check if the cursor line needs redrawing before changing State. If
405 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200406 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200407#endif
408
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409#ifdef FEAT_MOUSE
410 /*
411 * When doing a paste with the middle mouse button, Insstart is set to
412 * where the paste started.
413 */
414 if (where_paste_started.lnum != 0)
415 Insstart = where_paste_started;
416 else
417#endif
418 {
419 Insstart = curwin->w_cursor;
420 if (startln)
421 Insstart.col = 0;
422 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000423 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000424 Insstart_blank_vcol = MAXCOL;
425 if (!did_ai)
426 ai_col = 0;
427
428 if (cmdchar != NUL && restart_edit == 0)
429 {
430 ResetRedobuff();
431 AppendNumberToRedobuff(count);
432#ifdef FEAT_VREPLACE
433 if (cmdchar == 'V' || cmdchar == 'v')
434 {
435 /* "gR" or "gr" command */
436 AppendCharToRedobuff('g');
437 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
438 }
439 else
440#endif
441 {
442 AppendCharToRedobuff(cmdchar);
443 if (cmdchar == 'g') /* "gI" command */
444 AppendCharToRedobuff('I');
445 else if (cmdchar == 'r') /* "r<CR>" command */
446 count = 1; /* insert only one <CR> */
447 }
448 }
449
450 if (cmdchar == 'R')
451 {
452#ifdef FEAT_FKMAP
453 if (p_fkmap && p_ri)
454 {
455 beep_flush();
456 EMSG(farsi_text_3); /* encoded in Farsi */
457 State = INSERT;
458 }
459 else
460#endif
461 State = REPLACE;
462 }
463#ifdef FEAT_VREPLACE
464 else if (cmdchar == 'V' || cmdchar == 'v')
465 {
466 State = VREPLACE;
467 replaceState = VREPLACE;
468 orig_line_count = curbuf->b_ml.ml_line_count;
469 vr_lines_changed = 1;
470 }
471#endif
472 else
473 State = INSERT;
474
475 stop_insert_mode = FALSE;
476
477 /*
478 * Need to recompute the cursor position, it might move when the cursor is
479 * on a TAB or special character.
480 */
481 curs_columns(TRUE);
482
483 /*
484 * Enable langmap or IME, indicated by 'iminsert'.
485 * Note that IME may enabled/disabled without us noticing here, thus the
486 * 'iminsert' value may not reflect what is actually used. It is updated
487 * when hitting <Esc>.
488 */
489 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
490 State |= LANGMAP;
491#ifdef USE_IM_CONTROL
492 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
493#endif
494
Bram Moolenaar071d4272004-06-13 20:20:40 +0000495#ifdef FEAT_MOUSE
496 setmouse();
497#endif
498#ifdef FEAT_CMDL_INFO
499 clear_showcmd();
500#endif
501#ifdef FEAT_RIGHTLEFT
502 /* there is no reverse replace mode */
503 revins_on = (State == INSERT && p_ri);
504 if (revins_on)
505 undisplay_dollar();
506 revins_chars = 0;
507 revins_legal = 0;
508 revins_scol = -1;
509#endif
510
511 /*
512 * Handle restarting Insert mode.
513 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
514 * restart_edit non-zero, and something in the stuff buffer.
515 */
516 if (restart_edit != 0 && stuff_empty())
517 {
518#ifdef FEAT_MOUSE
519 /*
520 * After a paste we consider text typed to be part of the insert for
521 * the pasted text. You can backspace over the pasted text too.
522 */
523 if (where_paste_started.lnum)
524 arrow_used = FALSE;
525 else
526#endif
527 arrow_used = TRUE;
528 restart_edit = 0;
529
530 /*
531 * If the cursor was after the end-of-line before the CTRL-O and it is
532 * now at the end-of-line, put it after the end-of-line (this is not
533 * correct in very rare cases).
534 * Also do this if curswant is greater than the current virtual
535 * column. Eg after "^O$" or "^O80|".
536 */
537 validate_virtcol();
538 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000539 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 || curwin->w_curswant > curwin->w_virtcol)
541 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
542 {
543 if (ptr[1] == NUL)
544 ++curwin->w_cursor.col;
545#ifdef FEAT_MBYTE
546 else if (has_mbyte)
547 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000548 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000549 if (ptr[i] == NUL)
550 curwin->w_cursor.col += i;
551 }
552#endif
553 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000554 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000555 }
556 else
557 arrow_used = FALSE;
558
559 /* we are in insert mode now, don't need to start it anymore */
560 need_start_insertmode = FALSE;
561
562 /* Need to save the line for undo before inserting the first char. */
563 ins_need_undo = TRUE;
564
565#ifdef FEAT_MOUSE
566 where_paste_started.lnum = 0;
567#endif
568#ifdef FEAT_CINDENT
569 can_cindent = TRUE;
570#endif
571#ifdef FEAT_FOLDING
572 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
573 * restarting. */
574 if (!p_im && did_restart_edit == 0)
575 foldOpenCursor();
576#endif
577
578 /*
579 * If 'showmode' is set, show the current (insert/replace/..) mode.
580 * A warning message for changing a readonly file is given here, before
581 * actually changing anything. It's put after the mode, if any.
582 */
583 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000584 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000585 i = showmode();
586
587 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000588 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589
590#ifdef CURSOR_SHAPE
591 ui_cursor_shape(); /* may show different cursor shape */
592#endif
593#ifdef FEAT_DIGRAPHS
594 do_digraph(-1); /* clear digraphs */
595#endif
596
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000597 /*
598 * Get the current length of the redo buffer, those characters have to be
599 * skipped if we want to get to the inserted characters.
600 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000601 ptr = get_inserted();
602 if (ptr == NULL)
603 new_insert_skip = 0;
604 else
605 {
606 new_insert_skip = (int)STRLEN(ptr);
607 vim_free(ptr);
608 }
609
610 old_indent = 0;
611
612 /*
613 * Main loop in Insert mode: repeat until Insert mode is left.
614 */
615 for (;;)
616 {
617#ifdef FEAT_RIGHTLEFT
618 if (!revins_legal)
619 revins_scol = -1; /* reset on illegal motions */
620 else
621 revins_legal = 0;
622#endif
623 if (arrow_used) /* don't repeat insert when arrow key used */
624 count = 0;
625
626 if (stop_insert_mode)
627 {
628 /* ":stopinsert" used or 'insertmode' reset */
629 count = 0;
630 goto doESCkey;
631 }
632
633 /* set curwin->w_curswant for next K_DOWN or K_UP */
634 if (!arrow_used)
635 curwin->w_set_curswant = TRUE;
636
637 /* If there is no typeahead may check for timestamps (e.g., for when a
638 * menu invoked a shell command). */
639 if (stuff_empty())
640 {
641 did_check_timestamps = FALSE;
642 if (need_check_timestamps)
643 check_timestamps(FALSE);
644 }
645
646 /*
647 * When emsg() was called msg_scroll will have been set.
648 */
649 msg_scroll = FALSE;
650
651#ifdef FEAT_GUI
652 /* When 'mousefocus' is set a mouse movement may have taken us to
653 * another window. "need_mouse_correct" may then be set because of an
654 * autocommand. */
655 if (need_mouse_correct)
656 gui_mouse_correct();
657#endif
658
659#ifdef FEAT_FOLDING
660 /* Open fold at the cursor line, according to 'foldopen'. */
661 if (fdo_flags & FDO_INSERT)
662 foldOpenCursor();
663 /* Close folds where the cursor isn't, according to 'foldclose' */
664 if (!char_avail())
665 foldCheckClose();
666#endif
667
668 /*
669 * If we inserted a character at the last position of the last line in
670 * the window, scroll the window one line up. This avoids an extra
671 * redraw.
672 * This is detected when the cursor column is smaller after inserting
673 * something.
674 * Don't do this when the topline changed already, it has
675 * already been adjusted (by insertchar() calling open_line())).
676 */
677 if (curbuf->b_mod_set
678 && curwin->w_p_wrap
679 && !did_backspace
680 && curwin->w_topline == old_topline
681#ifdef FEAT_DIFF
682 && curwin->w_topfill == old_topfill
683#endif
684 )
685 {
686 mincol = curwin->w_wcol;
687 validate_cursor_col();
688
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000689 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000690 && curwin->w_wrow == W_WINROW(curwin)
691 + curwin->w_height - 1 - p_so
692 && (curwin->w_cursor.lnum != curwin->w_topline
693#ifdef FEAT_DIFF
694 || curwin->w_topfill > 0
695#endif
696 ))
697 {
698#ifdef FEAT_DIFF
699 if (curwin->w_topfill > 0)
700 --curwin->w_topfill;
701 else
702#endif
703#ifdef FEAT_FOLDING
704 if (hasFolding(curwin->w_topline, NULL, &old_topline))
705 set_topline(curwin, old_topline + 1);
706 else
707#endif
708 set_topline(curwin, curwin->w_topline + 1);
709 }
710 }
711
712 /* May need to adjust w_topline to show the cursor. */
713 update_topline();
714
715 did_backspace = FALSE;
716
717 validate_cursor(); /* may set must_redraw */
718
719 /*
720 * Redraw the display when no characters are waiting.
721 * Also shows mode, ruler and positions cursor.
722 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000723 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724
725#ifdef FEAT_SCROLLBIND
726 if (curwin->w_p_scb)
727 do_check_scrollbind(TRUE);
728#endif
729
Bram Moolenaar860cae12010-06-05 23:22:07 +0200730#ifdef FEAT_CURSORBIND
731 if (curwin->w_p_crb)
732 do_check_cursorbind();
733#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000734 update_curswant();
735 old_topline = curwin->w_topline;
736#ifdef FEAT_DIFF
737 old_topfill = curwin->w_topfill;
738#endif
739
740#ifdef USE_ON_FLY_SCROLL
741 dont_scroll = FALSE; /* allow scrolling here */
742#endif
743
744 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000745 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 */
747 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000748 do
749 {
750 c = safe_vgetc();
751 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000752
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000753#ifdef FEAT_AUTOCMD
754 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
755 did_cursorhold = TRUE;
756#endif
757
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758#ifdef FEAT_RIGHTLEFT
759 if (p_hkmap && KeyTyped)
760 c = hkmap(c); /* Hebrew mode mapping */
761#endif
762#ifdef FEAT_FKMAP
763 if (p_fkmap && KeyTyped)
764 c = fkmap(c); /* Farsi mode mapping */
765#endif
766
767#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000768 /*
769 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000770 * and the cursor is still in the completed word. Only when there is
771 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000772 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000773 if (compl_started
774 && pum_wanted()
775 && curwin->w_cursor.col >= compl_col
776 && (compl_shown_match == NULL
777 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000778 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000779 /* BS: Delete one character from "compl_leader". */
780 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000781 && curwin->w_cursor.col > compl_col
782 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000783 continue;
784
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000785 /* When no match was selected or it was edited. */
786 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000787 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000788 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000789 * "compl_leader". Except when at the original match and
790 * there is nothing to add, CTRL-L works like CTRL-P then. */
791 if (c == Ctrl_L
792 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000793 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000794 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000795 {
796 ins_compl_addfrommatch();
797 continue;
798 }
799
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000800 /* A non-white character that fits in with the current
801 * completion: Add to "compl_leader". */
802 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000803 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100804#ifdef FEAT_AUTOCMD
805 /* Trigger InsertCharPre. */
806 char_u *str = do_insert_char_pre(c);
807 char_u *p;
808
809 if (str != NULL)
810 {
811 for (p = str; *p != NUL; mb_ptr_adv(p))
812 ins_compl_addleader(PTR2CHAR(p));
813 vim_free(str);
814 }
815 else
816#endif
817 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000818 continue;
819 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000820
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000821 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000822 * compl_enter_selects is set the Enter key does the same. */
823 if (c == Ctrl_Y || (compl_enter_selects
824 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000825 {
826 ins_compl_delete();
827 ins_compl_insert();
828 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000829 }
830 }
831
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
833 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000834 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000835 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000836 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837#endif
838
Bram Moolenaar488c6512005-08-11 20:09:58 +0000839 /* CTRL-\ CTRL-N goes to Normal mode,
840 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
841 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000842 if (c == Ctrl_BSL)
843 {
844 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000845 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 ++no_mapping;
847 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000848 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000849 --no_mapping;
850 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000851 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000853 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 vungetc(c);
855 c = Ctrl_BSL;
856 }
857 else if (c == Ctrl_G && p_im)
858 continue;
859 else
860 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000861 if (c == Ctrl_O)
862 {
863 ins_ctrl_o();
864 ins_at_eol = FALSE; /* cursor keeps its column */
865 nomove = TRUE;
866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 count = 0;
868 goto doESCkey;
869 }
870 }
871
872#ifdef FEAT_DIGRAPHS
873 c = do_digraph(c);
874#endif
875
876#ifdef FEAT_INS_EXPAND
877 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
878 goto docomplete;
879#endif
880 if (c == Ctrl_V || c == Ctrl_Q)
881 {
882 ins_ctrl_v();
883 c = Ctrl_V; /* pretend CTRL-V is last typed character */
884 continue;
885 }
886
887#ifdef FEAT_CINDENT
888 if (cindent_on()
889# ifdef FEAT_INS_EXPAND
890 && ctrl_x_mode == 0
891# endif
892 )
893 {
894 /* A key name preceded by a bang means this key is not to be
895 * inserted. Skip ahead to the re-indenting below.
896 * A key name preceded by a star means that indenting has to be
897 * done before inserting the key. */
898 line_is_white = inindent(0);
899 if (in_cinkeys(c, '!', line_is_white))
900 goto force_cindent;
901 if (can_cindent && in_cinkeys(c, '*', line_is_white)
902 && stop_arrow() == OK)
903 do_c_expr_indent();
904 }
905#endif
906
907#ifdef FEAT_RIGHTLEFT
908 if (curwin->w_p_rl)
909 switch (c)
910 {
911 case K_LEFT: c = K_RIGHT; break;
912 case K_S_LEFT: c = K_S_RIGHT; break;
913 case K_C_LEFT: c = K_C_RIGHT; break;
914 case K_RIGHT: c = K_LEFT; break;
915 case K_S_RIGHT: c = K_S_LEFT; break;
916 case K_C_RIGHT: c = K_C_LEFT; break;
917 }
918#endif
919
920#ifdef FEAT_VISUAL
921 /*
922 * If 'keymodel' contains "startsel", may start selection. If it
923 * does, a CTRL-O and c will be stuffed, we need to get these
924 * characters.
925 */
926 if (ins_start_select(c))
927 continue;
928#endif
929
930 /*
931 * The big switch to handle a character in insert mode.
932 */
933 switch (c)
934 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000935 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000936 if (echeck_abbr(ESC + ABBR_OFF))
937 break;
938 /*FALLTHROUGH*/
939
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000940 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941#ifdef FEAT_CMDWIN
942 if (c == Ctrl_C && cmdwin_type != 0)
943 {
944 /* Close the cmdline window. */
945 cmdwin_result = K_IGNORE;
946 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000947 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000948 goto doESCkey;
949 }
950#endif
951
952#ifdef UNIX
953do_intr:
954#endif
955 /* when 'insertmode' set, and not halfway a mapping, don't leave
956 * Insert mode */
957 if (goto_im())
958 {
959 if (got_int)
960 {
961 (void)vgetc(); /* flush all buffers */
962 got_int = FALSE;
963 }
964 else
965 vim_beep();
966 break;
967 }
968doESCkey:
969 /*
970 * This is the ONLY return from edit()!
971 */
972 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
973 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000974 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000975 o_lnum = curwin->w_cursor.lnum;
976
Bram Moolenaar488c6512005-08-11 20:09:58 +0000977 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000978 {
979#ifdef FEAT_AUTOCMD
980 if (cmdchar != 'r' && cmdchar != 'v')
981 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
982 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000983 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000985 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000987 continue;
988
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989 case Ctrl_Z: /* suspend when 'insertmode' set */
990 if (!p_im)
991 goto normalchar; /* insert CTRL-Z as normal char */
992 stuffReadbuff((char_u *)":st\r");
993 c = Ctrl_O;
994 /*FALLTHROUGH*/
995
996 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000997#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000998 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000999 goto docomplete;
1000#endif
1001 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1002 break;
1003 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001004
1005#ifdef FEAT_VIRTUALEDIT
1006 /* don't move the cursor left when 'virtualedit' has "onemore". */
1007 if (ve_flags & VE_ONEMORE)
1008 {
1009 ins_at_eol = FALSE;
1010 nomove = TRUE;
1011 }
1012#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001013 count = 0;
1014 goto doESCkey;
1015
Bram Moolenaar572cb562005-08-05 21:35:02 +00001016 case K_INS: /* toggle insert/replace mode */
1017 case K_KINS:
1018 ins_insert(replaceState);
1019 break;
1020
1021 case K_SELECT: /* end of Select mode mapping - ignore */
1022 break;
1023
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001024#ifdef FEAT_SNIFF
1025 case K_SNIFF: /* Sniff command received */
1026 stuffcharReadbuff(K_SNIFF);
1027 goto doESCkey;
1028#endif
1029
1030 case K_HELP: /* Help key works like <ESC> <Help> */
1031 case K_F1:
1032 case K_XF1:
1033 stuffcharReadbuff(K_HELP);
1034 if (p_im)
1035 need_start_insertmode = TRUE;
1036 goto doESCkey;
1037
1038#ifdef FEAT_NETBEANS_INTG
1039 case K_F21: /* NetBeans command */
1040 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001041 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001042 --no_mapping;
1043 netbeans_keycommand(i);
1044 break;
1045#endif
1046
1047 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 case NUL:
1049 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001050 /* For ^@ the trailing ESC will end the insert, unless there is an
1051 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1053 && c != Ctrl_A && !p_im)
1054 goto doESCkey; /* quit insert mode */
1055 inserted_space = FALSE;
1056 break;
1057
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 ins_reg();
1060 auto_format(FALSE, TRUE);
1061 inserted_space = FALSE;
1062 break;
1063
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001064 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065 ins_ctrl_g();
1066 break;
1067
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001068 case Ctrl_HAT: /* switch input mode and/or langmap */
1069 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 break;
1071
1072#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 if (!p_ari)
1075 goto normalchar;
1076 ins_ctrl_();
1077 break;
1078#endif
1079
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1082 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1083 goto docomplete;
1084#endif
1085 /* FALLTHROUGH */
1086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088# ifdef FEAT_INS_EXPAND
1089 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1090 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001091 if (has_compl_option(FALSE))
1092 goto docomplete;
1093 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 }
1095# endif
1096 ins_shift(c, lastc);
1097 auto_format(FALSE, TRUE);
1098 inserted_space = FALSE;
1099 break;
1100
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001101 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 case K_KDEL:
1103 ins_del();
1104 auto_format(FALSE, TRUE);
1105 break;
1106
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001107 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001108 case Ctrl_H:
1109 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1110 auto_format(FALSE, TRUE);
1111 break;
1112
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001113 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1115 auto_format(FALSE, TRUE);
1116 break;
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001119# ifdef FEAT_COMPL_FUNC
1120 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001121 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001122 goto docomplete;
1123# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1125 auto_format(FALSE, TRUE);
1126 inserted_space = FALSE;
1127 break;
1128
1129#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001130 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001131 case K_LEFTMOUSE_NM:
1132 case K_LEFTDRAG:
1133 case K_LEFTRELEASE:
1134 case K_LEFTRELEASE_NM:
1135 case K_MIDDLEMOUSE:
1136 case K_MIDDLEDRAG:
1137 case K_MIDDLERELEASE:
1138 case K_RIGHTMOUSE:
1139 case K_RIGHTDRAG:
1140 case K_RIGHTRELEASE:
1141 case K_X1MOUSE:
1142 case K_X1DRAG:
1143 case K_X1RELEASE:
1144 case K_X2MOUSE:
1145 case K_X2DRAG:
1146 case K_X2RELEASE:
1147 ins_mouse(c);
1148 break;
1149
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001150 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001151 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 break;
1153
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001154 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001155 ins_mousescroll(MSCR_UP);
1156 break;
1157
1158 case K_MOUSELEFT: /* Scroll wheel left */
1159 ins_mousescroll(MSCR_LEFT);
1160 break;
1161
1162 case K_MOUSERIGHT: /* Scroll wheel right */
1163 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001164 break;
1165#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001166#ifdef FEAT_GUI_TABLINE
1167 case K_TABLINE:
1168 case K_TABMENU:
1169 ins_tabline(c);
1170 break;
1171#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001173 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 break;
1175
Bram Moolenaar754b5602006-02-09 23:53:20 +00001176#ifdef FEAT_AUTOCMD
1177 case K_CURSORHOLD: /* Didn't type something for a while. */
1178 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1179 did_cursorhold = TRUE;
1180 break;
1181#endif
1182
Bram Moolenaar4770d092006-01-12 23:22:24 +00001183#ifdef FEAT_GUI_W32
1184 /* On Win32 ignore <M-F4>, we get it when closing the window was
1185 * cancelled. */
1186 case K_F4:
1187 if (mod_mask != MOD_MASK_ALT)
1188 goto normalchar;
1189 break;
1190#endif
1191
Bram Moolenaar071d4272004-06-13 20:20:40 +00001192#ifdef FEAT_GUI
1193 case K_VER_SCROLLBAR:
1194 ins_scroll();
1195 break;
1196
1197 case K_HOR_SCROLLBAR:
1198 ins_horscroll();
1199 break;
1200#endif
1201
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001202 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 case K_S_HOME:
1205 case K_C_HOME:
1206 ins_home(c);
1207 break;
1208
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 case K_S_END:
1212 case K_C_END:
1213 ins_end(c);
1214 break;
1215
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001216 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001217 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1218 ins_s_left();
1219 else
1220 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 break;
1222
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001223 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 case K_C_LEFT:
1225 ins_s_left();
1226 break;
1227
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001228 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001229 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1230 ins_s_right();
1231 else
1232 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001233 break;
1234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001235 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001236 case K_C_RIGHT:
1237 ins_s_right();
1238 break;
1239
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001240 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001241#ifdef FEAT_INS_EXPAND
1242 if (pum_visible())
1243 goto docomplete;
1244#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001245 if (mod_mask & MOD_MASK_SHIFT)
1246 ins_pageup();
1247 else
1248 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 break;
1250
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001251 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001252 case K_PAGEUP:
1253 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001254#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001255 if (pum_visible())
1256 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001257#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 ins_pageup();
1259 break;
1260
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001261 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001262#ifdef FEAT_INS_EXPAND
1263 if (pum_visible())
1264 goto docomplete;
1265#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001266 if (mod_mask & MOD_MASK_SHIFT)
1267 ins_pagedown();
1268 else
1269 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 break;
1271
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001272 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001273 case K_PAGEDOWN:
1274 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001275#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001276 if (pum_visible())
1277 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001278#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279 ins_pagedown();
1280 break;
1281
1282#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001283 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 ins_drop();
1285 break;
1286#endif
1287
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001288 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 c = TAB;
1290 /* FALLTHROUGH */
1291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1294 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1295 goto docomplete;
1296#endif
1297 inserted_space = FALSE;
1298 if (ins_tab())
1299 goto normalchar; /* insert TAB as a normal char */
1300 auto_format(FALSE, TRUE);
1301 break;
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 c = CAR;
1305 /* FALLTHROUGH */
1306 case CAR:
1307 case NL:
1308#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1309 /* In a quickfix window a <CR> jumps to the error under the
1310 * cursor. */
1311 if (bt_quickfix(curbuf) && c == CAR)
1312 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001313 if (curwin->w_llist_ref == NULL) /* quickfix window */
1314 do_cmdline_cmd((char_u *)".cc");
1315 else /* location list window */
1316 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317 break;
1318 }
1319#endif
1320#ifdef FEAT_CMDWIN
1321 if (cmdwin_type != 0)
1322 {
1323 /* Execute the command in the cmdline window. */
1324 cmdwin_result = CAR;
1325 goto doESCkey;
1326 }
1327#endif
1328 if (ins_eol(c) && !p_im)
1329 goto doESCkey; /* out of memory */
1330 auto_format(FALSE, FALSE);
1331 inserted_space = FALSE;
1332 break;
1333
Bram Moolenaar860cae12010-06-05 23:22:07 +02001334#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001335 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336# ifdef FEAT_INS_EXPAND
1337 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1338 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 if (has_compl_option(TRUE))
1340 goto docomplete;
1341 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 }
1343# endif
1344# ifdef FEAT_DIGRAPHS
1345 c = ins_digraph();
1346 if (c == NUL)
1347 break;
1348# endif
1349 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001350#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351
1352#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001353 case Ctrl_X: /* Enter CTRL-X mode */
1354 ins_ctrl_x();
1355 break;
1356
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001357 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 if (ctrl_x_mode != CTRL_X_TAGS)
1359 goto normalchar;
1360 goto docomplete;
1361
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001362 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 if (ctrl_x_mode != CTRL_X_FILES)
1364 goto normalchar;
1365 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001366
1367 case 's': /* Spelling completion after ^X */
1368 case Ctrl_S:
1369 if (ctrl_x_mode != CTRL_X_SPELL)
1370 goto normalchar;
1371 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372#endif
1373
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001374 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375#ifdef FEAT_INS_EXPAND
1376 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1377#endif
1378 {
1379 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1380 if (p_im)
1381 {
1382 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1383 break;
1384 goto doESCkey;
1385 }
1386 goto normalchar;
1387 }
1388#ifdef FEAT_INS_EXPAND
1389 /* FALLTHROUGH */
1390
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001391 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 case Ctrl_N:
1393 /* if 'complete' is empty then plain ^P is no longer special,
1394 * but it is under other ^X modes */
1395 if (*curbuf->b_p_cpt == NUL
1396 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001397 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001398 goto normalchar;
1399
1400docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001401 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001402 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001403 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001404 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 break;
1406#endif /* FEAT_INS_EXPAND */
1407
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001408 case Ctrl_Y: /* copy from previous line or scroll down */
1409 case Ctrl_E: /* copy from next line or scroll up */
1410 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 break;
1412
1413 default:
1414#ifdef UNIX
1415 if (c == intr_char) /* special interrupt char */
1416 goto do_intr;
1417#endif
1418
Bram Moolenaare659c952011-05-19 17:25:41 +02001419normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420 /*
1421 * Insert a nomal character.
1422 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001423#ifdef FEAT_AUTOCMD
1424 if (!p_paste)
1425 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001426 /* Trigger InsertCharPre. */
1427 char_u *str = do_insert_char_pre(c);
1428 char_u *p;
1429
1430 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001431 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001432 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001433 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001434 /* Insert the new value of v:char literally. */
1435 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001436 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001437 c = PTR2CHAR(p);
1438 if (c == CAR || c == K_KENTER || c == NL)
1439 ins_eol(c);
1440 else
1441 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001442 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001443 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001444 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001445 vim_free(str);
1446 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001447 }
1448
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001449 /* If the new value is already inserted or an empty string
1450 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001451 if (c == NUL)
1452 break;
1453 }
1454#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455#ifdef FEAT_SMARTINDENT
1456 /* Try to perform smart-indenting. */
1457 ins_try_si(c);
1458#endif
1459
1460 if (c == ' ')
1461 {
1462 inserted_space = TRUE;
1463#ifdef FEAT_CINDENT
1464 if (inindent(0))
1465 can_cindent = FALSE;
1466#endif
1467 if (Insstart_blank_vcol == MAXCOL
1468 && curwin->w_cursor.lnum == Insstart.lnum)
1469 Insstart_blank_vcol = get_nolist_virtcol();
1470 }
1471
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001472 /* Insert a normal character and check for abbreviations on a
1473 * special character. Let CTRL-] expand abbreviations without
1474 * inserting it. */
1475 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476#ifdef FEAT_MBYTE
1477 /* Add ABBR_OFF for characters above 0x100, this is
1478 * what check_abbr() expects. */
1479 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1480#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001481 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001482 {
1483 insert_special(c, FALSE, FALSE);
1484#ifdef FEAT_RIGHTLEFT
1485 revins_legal++;
1486 revins_chars++;
1487#endif
1488 }
1489
1490 auto_format(FALSE, TRUE);
1491
1492#ifdef FEAT_FOLDING
1493 /* When inserting a character the cursor line must never be in a
1494 * closed fold. */
1495 foldOpenCursor();
1496#endif
1497 break;
1498 } /* end of switch (c) */
1499
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001500#ifdef FEAT_AUTOCMD
1501 /* If typed something may trigger CursorHoldI again. */
1502 if (c != K_CURSORHOLD)
1503 did_cursorhold = FALSE;
1504#endif
1505
Bram Moolenaar071d4272004-06-13 20:20:40 +00001506 /* If the cursor was moved we didn't just insert a space */
1507 if (arrow_used)
1508 inserted_space = FALSE;
1509
1510#ifdef FEAT_CINDENT
1511 if (can_cindent && cindent_on()
1512# ifdef FEAT_INS_EXPAND
1513 && ctrl_x_mode == 0
1514# endif
1515 )
1516 {
1517force_cindent:
1518 /*
1519 * Indent now if a key was typed that is in 'cinkeys'.
1520 */
1521 if (in_cinkeys(c, ' ', line_is_white))
1522 {
1523 if (stop_arrow() == OK)
1524 /* re-indent the current line */
1525 do_c_expr_indent();
1526 }
1527 }
1528#endif /* FEAT_CINDENT */
1529
1530 } /* for (;;) */
1531 /* NOTREACHED */
1532}
1533
1534/*
1535 * Redraw for Insert mode.
1536 * This is postponed until getting the next character to make '$' in the 'cpo'
1537 * option work correctly.
1538 * Only redraw when there are no characters available. This speeds up
1539 * inserting sequences of characters (e.g., for CTRL-R).
1540 */
1541 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001542ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001543 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001544{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001545#ifdef FEAT_CONCEAL
1546 linenr_T conceal_old_cursor_line = 0;
1547 linenr_T conceal_new_cursor_line = 0;
1548 int conceal_update_lines = FALSE;
1549#endif
1550
Bram Moolenaar071d4272004-06-13 20:20:40 +00001551 if (!char_avail())
1552 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001553#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001554 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1555 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001556 if (ready && (
1557# ifdef FEAT_AUTOCMD
1558 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001559# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001560# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1561 ||
1562# endif
1563# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001564 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001565# endif
1566 )
1567 && !equalpos(last_cursormoved, curwin->w_cursor)
1568# ifdef FEAT_INS_EXPAND
1569 && !pum_visible()
1570# endif
1571 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001572 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001573# ifdef FEAT_SYN_HL
1574 /* Need to update the screen first, to make sure syntax
1575 * highlighting is correct after making a change (e.g., inserting
1576 * a "(". The autocommand may also require a redraw, so it's done
1577 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001578 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001579 update_screen(0);
1580# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001581# ifdef FEAT_AUTOCMD
1582 if (has_cursormovedI())
1583 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1584# endif
1585# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001586 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001587 {
1588 conceal_old_cursor_line = last_cursormoved.lnum;
1589 conceal_new_cursor_line = curwin->w_cursor.lnum;
1590 conceal_update_lines = TRUE;
1591 }
1592# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001593 last_cursormoved = curwin->w_cursor;
1594 }
1595#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001596 if (must_redraw)
1597 update_screen(0);
1598 else if (clear_cmdline || redraw_cmdline)
1599 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001600# if defined(FEAT_CONCEAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001601 if ((conceal_update_lines
1602 && (conceal_old_cursor_line != conceal_new_cursor_line
1603 || conceal_cursor_line(curwin)))
1604 || need_cursor_line_redraw)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001605 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001606 if (conceal_old_cursor_line != conceal_new_cursor_line)
1607 update_single_line(curwin, conceal_old_cursor_line);
1608 update_single_line(curwin, conceal_new_cursor_line == 0
1609 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001610 curwin->w_valid &= ~VALID_CROW;
1611 }
1612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613 showruler(FALSE);
1614 setcursor();
1615 emsg_on_display = FALSE; /* may remove error message now */
1616 }
1617}
1618
1619/*
1620 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1621 */
1622 static void
1623ins_ctrl_v()
1624{
1625 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001626 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627
1628 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001629 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630
1631 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001632 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001634 did_putchar = TRUE;
1635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1637
1638#ifdef FEAT_CMDL_INFO
1639 add_to_showcmd_c(Ctrl_V);
1640#endif
1641
1642 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001643 if (did_putchar)
1644 /* when the line fits in 'columns' the '^' is at the start of the next
1645 * line and will not removed by the redraw */
1646 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647#ifdef FEAT_CMDL_INFO
1648 clear_showcmd();
1649#endif
1650 insert_special(c, FALSE, TRUE);
1651#ifdef FEAT_RIGHTLEFT
1652 revins_chars++;
1653 revins_legal++;
1654#endif
1655}
1656
1657/*
1658 * Put a character directly onto the screen. It's not stored in a buffer.
1659 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1660 */
1661static int pc_status;
1662#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1663#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1664#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1665#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667static int pc_attr;
1668static int pc_row;
1669static int pc_col;
1670
1671 void
1672edit_putchar(c, highlight)
1673 int c;
1674 int highlight;
1675{
1676 int attr;
1677
1678 if (ScreenLines != NULL)
1679 {
1680 update_topline(); /* just in case w_topline isn't valid */
1681 validate_cursor();
1682 if (highlight)
1683 attr = hl_attr(HLF_8);
1684 else
1685 attr = 0;
1686 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1687 pc_col = W_WINCOL(curwin);
1688#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1689 pc_status = PC_STATUS_UNSET;
1690#endif
1691#ifdef FEAT_RIGHTLEFT
1692 if (curwin->w_p_rl)
1693 {
1694 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1695# ifdef FEAT_MBYTE
1696 if (has_mbyte)
1697 {
1698 int fix_col = mb_fix_col(pc_col, pc_row);
1699
1700 if (fix_col != pc_col)
1701 {
1702 screen_putchar(' ', pc_row, fix_col, attr);
1703 --curwin->w_wcol;
1704 pc_status = PC_STATUS_RIGHT;
1705 }
1706 }
1707# endif
1708 }
1709 else
1710#endif
1711 {
1712 pc_col += curwin->w_wcol;
1713#ifdef FEAT_MBYTE
1714 if (mb_lefthalve(pc_row, pc_col))
1715 pc_status = PC_STATUS_LEFT;
1716#endif
1717 }
1718
1719 /* save the character to be able to put it back */
1720#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1721 if (pc_status == PC_STATUS_UNSET)
1722#endif
1723 {
1724 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1725 pc_status = PC_STATUS_SET;
1726 }
1727 screen_putchar(c, pc_row, pc_col, attr);
1728 }
1729}
1730
1731/*
1732 * Undo the previous edit_putchar().
1733 */
1734 void
1735edit_unputchar()
1736{
1737 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1738 {
1739#if defined(FEAT_MBYTE)
1740 if (pc_status == PC_STATUS_RIGHT)
1741 ++curwin->w_wcol;
1742 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1743 redrawWinline(curwin->w_cursor.lnum, FALSE);
1744 else
1745#endif
1746 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1747 }
1748}
1749
1750/*
1751 * Called when p_dollar is set: display a '$' at the end of the changed text
1752 * Only works when cursor is in the line that changes.
1753 */
1754 void
1755display_dollar(col)
1756 colnr_T col;
1757{
1758 colnr_T save_col;
1759
1760 if (!redrawing())
1761 return;
1762
1763 cursor_off();
1764 save_col = curwin->w_cursor.col;
1765 curwin->w_cursor.col = col;
1766#ifdef FEAT_MBYTE
1767 if (has_mbyte)
1768 {
1769 char_u *p;
1770
1771 /* If on the last byte of a multi-byte move to the first byte. */
1772 p = ml_get_curline();
1773 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1774 }
1775#endif
1776 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1777 if (curwin->w_wcol < W_WIDTH(curwin))
1778 {
1779 edit_putchar('$', FALSE);
1780 dollar_vcol = curwin->w_virtcol;
1781 }
1782 curwin->w_cursor.col = save_col;
1783}
1784
1785/*
1786 * Call this function before moving the cursor from the normal insert position
1787 * in insert mode.
1788 */
1789 static void
1790undisplay_dollar()
1791{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001792 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001794 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 redrawWinline(curwin->w_cursor.lnum, FALSE);
1796 }
1797}
1798
1799/*
1800 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1801 * Keep the cursor on the same character.
1802 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1803 * type == INDENT_DEC decrease indent (for CTRL-D)
1804 * type == INDENT_SET set indent to "amount"
1805 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1806 */
1807 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001808change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 int type;
1810 int amount;
1811 int round;
1812 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001813 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001814{
1815 int vcol;
1816 int last_vcol;
1817 int insstart_less; /* reduction for Insstart.col */
1818 int new_cursor_col;
1819 int i;
1820 char_u *ptr;
1821 int save_p_list;
1822 int start_col;
1823 colnr_T vc;
1824#ifdef FEAT_VREPLACE
1825 colnr_T orig_col = 0; /* init for GCC */
1826 char_u *new_line, *orig_line = NULL; /* init for GCC */
1827
1828 /* VREPLACE mode needs to know what the line was like before changing */
1829 if (State & VREPLACE_FLAG)
1830 {
1831 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1832 orig_col = curwin->w_cursor.col;
1833 }
1834#endif
1835
1836 /* for the following tricks we don't want list mode */
1837 save_p_list = curwin->w_p_list;
1838 curwin->w_p_list = FALSE;
1839 vc = getvcol_nolist(&curwin->w_cursor);
1840 vcol = vc;
1841
1842 /*
1843 * For Replace mode we need to fix the replace stack later, which is only
1844 * possible when the cursor is in the indent. Remember the number of
1845 * characters before the cursor if it's possible.
1846 */
1847 start_col = curwin->w_cursor.col;
1848
1849 /* determine offset from first non-blank */
1850 new_cursor_col = curwin->w_cursor.col;
1851 beginline(BL_WHITE);
1852 new_cursor_col -= curwin->w_cursor.col;
1853
1854 insstart_less = curwin->w_cursor.col;
1855
1856 /*
1857 * If the cursor is in the indent, compute how many screen columns the
1858 * cursor is to the left of the first non-blank.
1859 */
1860 if (new_cursor_col < 0)
1861 vcol = get_indent() - vcol;
1862
1863 if (new_cursor_col > 0) /* can't fix replace stack */
1864 start_col = -1;
1865
1866 /*
1867 * Set the new indent. The cursor will be put on the first non-blank.
1868 */
1869 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001870 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 else
1872 {
1873#ifdef FEAT_VREPLACE
1874 int save_State = State;
1875
1876 /* Avoid being called recursively. */
1877 if (State & VREPLACE_FLAG)
1878 State = INSERT;
1879#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001880 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001881#ifdef FEAT_VREPLACE
1882 State = save_State;
1883#endif
1884 }
1885 insstart_less -= curwin->w_cursor.col;
1886
1887 /*
1888 * Try to put cursor on same character.
1889 * If the cursor is at or after the first non-blank in the line,
1890 * compute the cursor column relative to the column of the first
1891 * non-blank character.
1892 * If we are not in insert mode, leave the cursor on the first non-blank.
1893 * If the cursor is before the first non-blank, position it relative
1894 * to the first non-blank, counted in screen columns.
1895 */
1896 if (new_cursor_col >= 0)
1897 {
1898 /*
1899 * When changing the indent while the cursor is touching it, reset
1900 * Insstart_col to 0.
1901 */
1902 if (new_cursor_col == 0)
1903 insstart_less = MAXCOL;
1904 new_cursor_col += curwin->w_cursor.col;
1905 }
1906 else if (!(State & INSERT))
1907 new_cursor_col = curwin->w_cursor.col;
1908 else
1909 {
1910 /*
1911 * Compute the screen column where the cursor should be.
1912 */
1913 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001914 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915
1916 /*
1917 * Advance the cursor until we reach the right screen column.
1918 */
1919 vcol = last_vcol = 0;
1920 new_cursor_col = -1;
1921 ptr = ml_get_curline();
1922 while (vcol <= (int)curwin->w_virtcol)
1923 {
1924 last_vcol = vcol;
1925#ifdef FEAT_MBYTE
1926 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001927 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 else
1929#endif
1930 ++new_cursor_col;
1931 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1932 }
1933 vcol = last_vcol;
1934
1935 /*
1936 * May need to insert spaces to be able to position the cursor on
1937 * the right screen column.
1938 */
1939 if (vcol != (int)curwin->w_virtcol)
1940 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001941 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001943 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 if (ptr != NULL)
1945 {
1946 new_cursor_col += i;
1947 ptr[i] = NUL;
1948 while (--i >= 0)
1949 ptr[i] = ' ';
1950 ins_str(ptr);
1951 vim_free(ptr);
1952 }
1953 }
1954
1955 /*
1956 * When changing the indent while the cursor is in it, reset
1957 * Insstart_col to 0.
1958 */
1959 insstart_less = MAXCOL;
1960 }
1961
1962 curwin->w_p_list = save_p_list;
1963
1964 if (new_cursor_col <= 0)
1965 curwin->w_cursor.col = 0;
1966 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001967 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001968 curwin->w_set_curswant = TRUE;
1969 changed_cline_bef_curs();
1970
1971 /*
1972 * May have to adjust the start of the insert.
1973 */
1974 if (State & INSERT)
1975 {
1976 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1977 {
1978 if ((int)Insstart.col <= insstart_less)
1979 Insstart.col = 0;
1980 else
1981 Insstart.col -= insstart_less;
1982 }
1983 if ((int)ai_col <= insstart_less)
1984 ai_col = 0;
1985 else
1986 ai_col -= insstart_less;
1987 }
1988
1989 /*
1990 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1991 * If the number of characters before the cursor decreased, need to pop a
1992 * few characters from the replace stack.
1993 * If the number of characters before the cursor increased, need to push a
1994 * few NULs onto the replace stack.
1995 */
1996 if (REPLACE_NORMAL(State) && start_col >= 0)
1997 {
1998 while (start_col > (int)curwin->w_cursor.col)
1999 {
2000 replace_join(0); /* remove a NUL from the replace stack */
2001 --start_col;
2002 }
2003 while (start_col < (int)curwin->w_cursor.col || replaced)
2004 {
2005 replace_push(NUL);
2006 if (replaced)
2007 {
2008 replace_push(replaced);
2009 replaced = NUL;
2010 }
2011 ++start_col;
2012 }
2013 }
2014
2015#ifdef FEAT_VREPLACE
2016 /*
2017 * For VREPLACE mode, we also have to fix the replace stack. In this case
2018 * it is always possible because we backspace over the whole line and then
2019 * put it back again the way we wanted it.
2020 */
2021 if (State & VREPLACE_FLAG)
2022 {
2023 /* If orig_line didn't allocate, just return. At least we did the job,
2024 * even if you can't backspace. */
2025 if (orig_line == NULL)
2026 return;
2027
2028 /* Save new line */
2029 new_line = vim_strsave(ml_get_curline());
2030 if (new_line == NULL)
2031 return;
2032
2033 /* We only put back the new line up to the cursor */
2034 new_line[curwin->w_cursor.col] = NUL;
2035
2036 /* Put back original line */
2037 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2038 curwin->w_cursor.col = orig_col;
2039
2040 /* Backspace from cursor to start of line */
2041 backspace_until_column(0);
2042
2043 /* Insert new stuff into line again */
2044 ins_bytes(new_line);
2045
2046 vim_free(new_line);
2047 }
2048#endif
2049}
2050
2051/*
2052 * Truncate the space at the end of a line. This is to be used only in an
2053 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2054 * modes.
2055 */
2056 void
2057truncate_spaces(line)
2058 char_u *line;
2059{
2060 int i;
2061
2062 /* find start of trailing white space */
2063 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2064 {
2065 if (State & REPLACE_FLAG)
2066 replace_join(0); /* remove a NUL from the replace stack */
2067 }
2068 line[i + 1] = NUL;
2069}
2070
2071#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2072 || defined(FEAT_COMMENTS) || defined(PROTO)
2073/*
2074 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2075 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002076 * Will attempt not to go before "col" even when there is a composing
2077 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002078 */
2079 void
2080backspace_until_column(col)
2081 int col;
2082{
2083 while ((int)curwin->w_cursor.col > col)
2084 {
2085 curwin->w_cursor.col--;
2086 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002087 replace_do_bs(col);
2088 else if (!del_char_after_col(col))
2089 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002090 }
2091}
2092#endif
2093
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002094/*
2095 * Like del_char(), but make sure not to go before column "limit_col".
2096 * Only matters when there are composing characters.
2097 * Return TRUE when something was deleted.
2098 */
2099 static int
2100del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002101 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002102{
2103#ifdef FEAT_MBYTE
2104 if (enc_utf8 && limit_col >= 0)
2105 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002106 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002107
2108 /* Make sure the cursor is at the start of a character, but
2109 * skip forward again when going too far back because of a
2110 * composing character. */
2111 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002112 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002113 {
2114 int l = utf_ptr2len(ml_get_cursor());
2115
2116 if (l == 0) /* end of line */
2117 break;
2118 curwin->w_cursor.col += l;
2119 }
2120 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2121 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002122 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002123 }
2124 else
2125#endif
2126 (void)del_char(FALSE);
2127 return TRUE;
2128}
2129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2131/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002132 * CTRL-X pressed in Insert mode.
2133 */
2134 static void
2135ins_ctrl_x()
2136{
2137 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2138 * CTRL-V works like CTRL-N */
2139 if (ctrl_x_mode != CTRL_X_CMDLINE)
2140 {
2141 /* if the next ^X<> won't ADD nothing, then reset
2142 * compl_cont_status */
2143 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002144 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002145 else
2146 compl_cont_status = 0;
2147 /* We're not sure which CTRL-X mode it will be yet */
2148 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2149 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2150 edit_submode_pre = NULL;
2151 showmode();
2152 }
2153}
2154
2155/*
2156 * Return TRUE if the 'dict' or 'tsr' option can be used.
2157 */
2158 static int
2159has_compl_option(dict_opt)
2160 int dict_opt;
2161{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002162 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002163# ifdef FEAT_SPELL
2164 && !curwin->w_p_spell
2165# endif
2166 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002167 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2168 {
2169 ctrl_x_mode = 0;
2170 edit_submode = NULL;
2171 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2172 : (char_u *)_("'thesaurus' option is empty"),
2173 hl_attr(HLF_E));
2174 if (emsg_silent == 0)
2175 {
2176 vim_beep();
2177 setcursor();
2178 out_flush();
2179 ui_delay(2000L, FALSE);
2180 }
2181 return FALSE;
2182 }
2183 return TRUE;
2184}
2185
2186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2188 * This depends on the current mode.
2189 */
2190 int
2191vim_is_ctrl_x_key(c)
2192 int c;
2193{
2194 /* Always allow ^R - let it's results then be checked */
2195 if (c == Ctrl_R)
2196 return TRUE;
2197
Bram Moolenaare3226be2005-12-18 22:10:00 +00002198 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002199 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002200 return TRUE;
2201
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 switch (ctrl_x_mode)
2203 {
2204 case 0: /* Not in any CTRL-X mode */
2205 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2206 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002207 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002208 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2209 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2210 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002211 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002212 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 case CTRL_X_SCROLL:
2214 return (c == Ctrl_Y || c == Ctrl_E);
2215 case CTRL_X_WHOLE_LINE:
2216 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2217 case CTRL_X_FILES:
2218 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2219 case CTRL_X_DICTIONARY:
2220 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2221 case CTRL_X_THESAURUS:
2222 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2223 case CTRL_X_TAGS:
2224 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2225#ifdef FEAT_FIND_ID
2226 case CTRL_X_PATH_PATTERNS:
2227 return (c == Ctrl_P || c == Ctrl_N);
2228 case CTRL_X_PATH_DEFINES:
2229 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2230#endif
2231 case CTRL_X_CMDLINE:
2232 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2233 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002234#ifdef FEAT_COMPL_FUNC
2235 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002236 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002237 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002238 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002239#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002240 case CTRL_X_SPELL:
2241 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243 EMSG(_(e_internal));
2244 return FALSE;
2245}
2246
2247/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002248 * Return TRUE when character "c" is part of the item currently being
2249 * completed. Used to decide whether to abandon complete mode when the menu
2250 * is visible.
2251 */
2252 static int
2253ins_compl_accept_char(c)
2254 int c;
2255{
2256 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2257 /* When expanding an identifier only accept identifier chars. */
2258 return vim_isIDc(c);
2259
2260 switch (ctrl_x_mode)
2261 {
2262 case CTRL_X_FILES:
2263 /* When expanding file name only accept file name chars. But not
2264 * path separators, so that "proto/<Tab>" expands files in
2265 * "proto", not "proto/" as a whole */
2266 return vim_isfilec(c) && !vim_ispathsep(c);
2267
2268 case CTRL_X_CMDLINE:
2269 case CTRL_X_OMNI:
2270 /* Command line and Omni completion can work with just about any
2271 * printable character, but do stop at white space. */
2272 return vim_isprintc(c) && !vim_iswhite(c);
2273
2274 case CTRL_X_WHOLE_LINE:
2275 /* For while line completion a space can be part of the line. */
2276 return vim_isprintc(c);
2277 }
2278 return vim_iswordc(c);
2279}
2280
2281/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002282 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002283 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002284 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285 * the rest of the word to be in -- webb
2286 */
2287 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002288ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289 char_u *str;
2290 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002291 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 char_u *fname;
2293 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002294 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002296 char_u *p;
2297 int i, c;
2298 int actual_len; /* Take multi-byte characters */
2299 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002300 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002301 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 int has_lower = FALSE;
2303 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002305 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002307 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308
Bram Moolenaara2993e12007-08-12 14:38:46 +00002309 /* Find actual length of completion. */
2310#ifdef FEAT_MBYTE
2311 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002313 p = str;
2314 actual_len = 0;
2315 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002316 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002317 mb_ptr_adv(p);
2318 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319 }
2320 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002321 else
2322#endif
2323 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324
Bram Moolenaara2993e12007-08-12 14:38:46 +00002325 /* Find actual length of original text. */
2326#ifdef FEAT_MBYTE
2327 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002329 p = compl_orig_text;
2330 actual_compl_length = 0;
2331 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002333 mb_ptr_adv(p);
2334 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 }
2336 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002337 else
2338#endif
2339 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002341 /* "actual_len" may be smaller than "actual_compl_length" when using
2342 * thesaurus, only use the minimum when comparing. */
2343 min_len = actual_len < actual_compl_length
2344 ? actual_len : actual_compl_length;
2345
Bram Moolenaara2993e12007-08-12 14:38:46 +00002346 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002347 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002348 if (wca != NULL)
2349 {
2350 p = str;
2351 for (i = 0; i < actual_len; ++i)
2352#ifdef FEAT_MBYTE
2353 if (has_mbyte)
2354 wca[i] = mb_ptr2char_adv(&p);
2355 else
2356#endif
2357 wca[i] = *(p++);
2358
2359 /* Rule 1: Were any chars converted to lower? */
2360 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002361 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002362 {
2363#ifdef FEAT_MBYTE
2364 if (has_mbyte)
2365 c = mb_ptr2char_adv(&p);
2366 else
2367#endif
2368 c = *(p++);
2369 if (MB_ISLOWER(c))
2370 {
2371 has_lower = TRUE;
2372 if (MB_ISUPPER(wca[i]))
2373 {
2374 /* Rule 1 is satisfied. */
2375 for (i = actual_compl_length; i < actual_len; ++i)
2376 wca[i] = MB_TOLOWER(wca[i]);
2377 break;
2378 }
2379 }
2380 }
2381
2382 /*
2383 * Rule 2: No lower case, 2nd consecutive letter converted to
2384 * upper case.
2385 */
2386 if (!has_lower)
2387 {
2388 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002389 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002390 {
2391#ifdef FEAT_MBYTE
2392 if (has_mbyte)
2393 c = mb_ptr2char_adv(&p);
2394 else
2395#endif
2396 c = *(p++);
2397 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2398 {
2399 /* Rule 2 is satisfied. */
2400 for (i = actual_compl_length; i < actual_len; ++i)
2401 wca[i] = MB_TOUPPER(wca[i]);
2402 break;
2403 }
2404 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2405 }
2406 }
2407
2408 /* Copy the original case of the part we typed. */
2409 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002410 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002411 {
2412#ifdef FEAT_MBYTE
2413 if (has_mbyte)
2414 c = mb_ptr2char_adv(&p);
2415 else
2416#endif
2417 c = *(p++);
2418 if (MB_ISLOWER(c))
2419 wca[i] = MB_TOLOWER(wca[i]);
2420 else if (MB_ISUPPER(c))
2421 wca[i] = MB_TOUPPER(wca[i]);
2422 }
2423
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002424 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002425 * Generate encoding specific output from wide character array.
2426 * Multi-byte characters can occupy up to five bytes more than
2427 * ASCII characters, and we also need one byte for NUL, so stay
2428 * six bytes away from the edge of IObuff.
2429 */
2430 p = IObuff;
2431 i = 0;
2432 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2433#ifdef FEAT_MBYTE
2434 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002435 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002436 else
2437#endif
2438 *(p++) = wca[i++];
2439 *p = NUL;
2440
2441 vim_free(wca);
2442 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002444 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2445 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002447 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448}
2449
2450/*
2451 * Add a match to the list of matches.
2452 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002453 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002454 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002456 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002457ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 char_u *str;
2459 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002460 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002462 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002463 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002464 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002465 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002467 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002468 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469
2470 ui_breakcheck();
2471 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002472 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 if (len < 0)
2474 len = (int)STRLEN(str);
2475
2476 /*
2477 * If the same match is already present, don't add it.
2478 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002479 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002481 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002482 do
2483 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002484 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002485 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002486 && match->cp_str[len] == NUL)
2487 return NOTDONE;
2488 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002489 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 }
2491
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002492 /* Remove any popup menu before changing the list of matches. */
2493 ins_compl_del_pum();
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 /*
2496 * Allocate a new match structure.
2497 * Copy the values to the new match structure.
2498 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002499 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002500 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002501 return FAIL;
2502 match->cp_number = -1;
2503 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002504 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002505 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002506 {
2507 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002508 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002510 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002511
Bram Moolenaar071d4272004-06-13 20:20:40 +00002512 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002513 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2515 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002516 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002517 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002518 && compl_curr_match->cp_fname != NULL
2519 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002520 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002521 else if (fname != NULL)
2522 {
2523 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002524 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002526 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002527 match->cp_fname = NULL;
2528 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002529
2530 if (cptext != NULL)
2531 {
2532 int i;
2533
2534 for (i = 0; i < CPT_COUNT; ++i)
2535 if (cptext[i] != NULL && *cptext[i] != NUL)
2536 match->cp_text[i] = vim_strsave(cptext[i]);
2537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538
2539 /*
2540 * Link the new match structure in the list of matches.
2541 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002542 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002543 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 else if (dir == FORWARD)
2545 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002546 match->cp_next = compl_curr_match->cp_next;
2547 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 }
2549 else /* BACKWARD */
2550 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002551 match->cp_next = compl_curr_match;
2552 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002554 if (match->cp_next)
2555 match->cp_next->cp_prev = match;
2556 if (match->cp_prev)
2557 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002559 compl_first_match = match;
2560 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002562 /*
2563 * Find the longest common string if still doing that.
2564 */
2565 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2566 ins_compl_longest_match(match);
2567
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 return OK;
2569}
2570
2571/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002572 * Return TRUE if "str[len]" matches with match->cp_str, considering
2573 * match->cp_icase.
2574 */
2575 static int
2576ins_compl_equal(match, str, len)
2577 compl_T *match;
2578 char_u *str;
2579 int len;
2580{
2581 if (match->cp_icase)
2582 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2583 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2584}
2585
2586/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002587 * Reduce the longest common string for match "match".
2588 */
2589 static void
2590ins_compl_longest_match(match)
2591 compl_T *match;
2592{
2593 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002594 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002595 int had_match;
2596
2597 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002598 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002599 /* First match, use it as a whole. */
2600 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002601 if (compl_leader != NULL)
2602 {
2603 had_match = (curwin->w_cursor.col > compl_col);
2604 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002605 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002606 ins_redraw(FALSE);
2607
2608 /* When the match isn't there (to avoid matching itself) remove it
2609 * again after redrawing. */
2610 if (!had_match)
2611 ins_compl_delete();
2612 compl_used_match = FALSE;
2613 }
2614 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002615 else
2616 {
2617 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002618 p = compl_leader;
2619 s = match->cp_str;
2620 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002621 {
2622#ifdef FEAT_MBYTE
2623 if (has_mbyte)
2624 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002625 c1 = mb_ptr2char(p);
2626 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002627 }
2628 else
2629#endif
2630 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002631 c1 = *p;
2632 c2 = *s;
2633 }
2634 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2635 : (c1 != c2))
2636 break;
2637#ifdef FEAT_MBYTE
2638 if (has_mbyte)
2639 {
2640 mb_ptr_adv(p);
2641 mb_ptr_adv(s);
2642 }
2643 else
2644#endif
2645 {
2646 ++p;
2647 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002648 }
2649 }
2650
2651 if (*p != NUL)
2652 {
2653 /* Leader was shortened, need to change the inserted text. */
2654 *p = NUL;
2655 had_match = (curwin->w_cursor.col > compl_col);
2656 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002657 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002658 ins_redraw(FALSE);
2659
2660 /* When the match isn't there (to avoid matching itself) remove it
2661 * again after redrawing. */
2662 if (!had_match)
2663 ins_compl_delete();
2664 }
2665
2666 compl_used_match = FALSE;
2667 }
2668}
2669
2670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002671 * Add an array of matches to the list of matches.
2672 * Frees matches[].
2673 */
2674 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002675ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 int num_matches;
2677 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002678 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679{
2680 int i;
2681 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002682 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002683
Bram Moolenaar572cb562005-08-05 21:35:02 +00002684 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002685 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002686 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002688 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 FreeWild(num_matches, matches);
2690}
2691
2692/* Make the completion list cyclic.
2693 * Return the number of matches (excluding the original).
2694 */
2695 static int
2696ins_compl_make_cyclic()
2697{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002698 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 int count = 0;
2700
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002701 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 {
2703 /*
2704 * Find the end of the list.
2705 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002706 match = compl_first_match;
2707 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002708 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002709 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002710 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711 ++count;
2712 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002713 match->cp_next = compl_first_match;
2714 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 }
2716 return count;
2717}
2718
Bram Moolenaara94bc432006-03-10 21:42:59 +00002719/*
2720 * Start completion for the complete() function.
2721 * "startcol" is where the matched text starts (1 is first column).
2722 * "list" is the list of matches.
2723 */
2724 void
2725set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002726 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002727 list_T *list;
2728{
2729 /* If already doing completions stop it. */
2730 if (ctrl_x_mode != 0)
2731 ins_compl_prep(' ');
2732 ins_compl_clear();
2733
2734 if (stop_arrow() == FAIL)
2735 return;
2736
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002737 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002738 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002739 startcol = curwin->w_cursor.col;
2740 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002741 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002742 /* compl_pattern doesn't need to be set */
2743 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2744 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002745 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002746 return;
2747
2748 /* Handle like dictionary completion. */
2749 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2750
2751 ins_compl_add_list(list);
2752 compl_matches = ins_compl_make_cyclic();
2753 compl_started = TRUE;
2754 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002755 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002756
2757 compl_curr_match = compl_first_match;
2758 ins_complete(Ctrl_N);
2759 out_flush();
2760}
2761
2762
Bram Moolenaar9372a112005-12-06 19:59:18 +00002763/* "compl_match_array" points the currently displayed list of entries in the
2764 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002765static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002766static int compl_match_arraysize;
2767
2768/*
2769 * Update the screen and when there is any scrolling remove the popup menu.
2770 */
2771 static void
2772ins_compl_upd_pum()
2773{
2774 int h;
2775
2776 if (compl_match_array != NULL)
2777 {
2778 h = curwin->w_cline_height;
2779 update_screen(0);
2780 if (h != curwin->w_cline_height)
2781 ins_compl_del_pum();
2782 }
2783}
2784
2785/*
2786 * Remove any popup menu.
2787 */
2788 static void
2789ins_compl_del_pum()
2790{
2791 if (compl_match_array != NULL)
2792 {
2793 pum_undisplay();
2794 vim_free(compl_match_array);
2795 compl_match_array = NULL;
2796 }
2797}
2798
2799/*
2800 * Return TRUE if the popup menu should be displayed.
2801 */
2802 static int
2803pum_wanted()
2804{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002805 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002806 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002807 return FALSE;
2808
2809 /* The display looks bad on a B&W display. */
2810 if (t_colors < 8
2811#ifdef FEAT_GUI
2812 && !gui.in_use
2813#endif
2814 )
2815 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002816 return TRUE;
2817}
2818
2819/*
2820 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002821 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002822 */
2823 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002824pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002825{
2826 compl_T *compl;
2827 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002828
2829 /* Don't display the popup menu if there are no matches or there is only
2830 * one (ignoring the original text). */
2831 compl = compl_first_match;
2832 i = 0;
2833 do
2834 {
2835 if (compl == NULL
2836 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2837 break;
2838 compl = compl->cp_next;
2839 } while (compl != compl_first_match);
2840
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002841 if (strstr((char *)p_cot, "menuone") != NULL)
2842 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002843 return (i >= 2);
2844}
2845
2846/*
2847 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002848 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002849 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002850 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002851ins_compl_show_pum()
2852{
2853 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002854 compl_T *shown_compl = NULL;
2855 int did_find_shown_match = FALSE;
2856 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002857 int i;
2858 int cur = -1;
2859 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002860 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002861
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002862 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002863 return;
2864
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002865#if defined(FEAT_EVAL)
2866 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2867 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2868#endif
2869
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002870 /* Update the screen before drawing the popup menu over it. */
2871 update_screen(0);
2872
2873 if (compl_match_array == NULL)
2874 {
2875 /* Need to build the popup menu list. */
2876 compl_match_arraysize = 0;
2877 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002878 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002879 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002880 do
2881 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002882 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2883 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002884 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002885 ++compl_match_arraysize;
2886 compl = compl->cp_next;
2887 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002888 if (compl_match_arraysize == 0)
2889 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002890 compl_match_array = (pumitem_T *)alloc_clear(
2891 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002892 * compl_match_arraysize));
2893 if (compl_match_array != NULL)
2894 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002895 /* If the current match is the original text don't find the first
2896 * match after it, don't highlight anything. */
2897 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2898 shown_match_ok = TRUE;
2899
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002900 i = 0;
2901 compl = compl_first_match;
2902 do
2903 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002904 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2905 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002906 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002907 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002908 if (!shown_match_ok)
2909 {
2910 if (compl == compl_shown_match || did_find_shown_match)
2911 {
2912 /* This item is the shown match or this is the
2913 * first displayed item after the shown match. */
2914 compl_shown_match = compl;
2915 did_find_shown_match = TRUE;
2916 shown_match_ok = TRUE;
2917 }
2918 else
2919 /* Remember this displayed match for when the
2920 * shown match is just below it. */
2921 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002922 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002923 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002924
2925 if (compl->cp_text[CPT_ABBR] != NULL)
2926 compl_match_array[i].pum_text =
2927 compl->cp_text[CPT_ABBR];
2928 else
2929 compl_match_array[i].pum_text = compl->cp_str;
2930 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2931 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2932 if (compl->cp_text[CPT_MENU] != NULL)
2933 compl_match_array[i++].pum_extra =
2934 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002935 else
2936 compl_match_array[i++].pum_extra = compl->cp_fname;
2937 }
2938
2939 if (compl == compl_shown_match)
2940 {
2941 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002942
2943 /* When the original text is the shown match don't set
2944 * compl_shown_match. */
2945 if (compl->cp_flags & ORIGINAL_TEXT)
2946 shown_match_ok = TRUE;
2947
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002948 if (!shown_match_ok && shown_compl != NULL)
2949 {
2950 /* The shown match isn't displayed, set it to the
2951 * previously displayed match. */
2952 compl_shown_match = shown_compl;
2953 shown_match_ok = TRUE;
2954 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002955 }
2956 compl = compl->cp_next;
2957 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002958
2959 if (!shown_match_ok) /* no displayed match at all */
2960 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002961 }
2962 }
2963 else
2964 {
2965 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002966 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002967 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2968 || compl_match_array[i].pum_text
2969 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002970 {
2971 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002972 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002973 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002974 }
2975
2976 if (compl_match_array != NULL)
2977 {
2978 /* Compute the screen column of the start of the completed text.
2979 * Use the cursor to get all wrapping and other settings right. */
2980 col = curwin->w_cursor.col;
2981 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002982 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002983 curwin->w_cursor.col = col;
2984 }
2985}
2986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987#define DICT_FIRST (1) /* use just first element in "dict" */
2988#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002989
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002991 * Add any identifiers that match the given pattern in the list of dictionary
2992 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 */
2994 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002995ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2996 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002998 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002999 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003000{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003001 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002 char_u *ptr;
3003 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005 char_u **files;
3006 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003008 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009
Bram Moolenaar0b238792006-03-02 22:49:12 +00003010 if (*dict == NUL)
3011 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003012#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003013 /* When 'dictionary' is empty and spell checking is enabled use
3014 * "spell". */
3015 if (!thesaurus && curwin->w_p_spell)
3016 dict = (char_u *)"spell";
3017 else
3018#endif
3019 return;
3020 }
3021
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003023 if (buf == NULL)
3024 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003025 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003026
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 /* If 'infercase' is set, don't use 'smartcase' here */
3028 save_p_scs = p_scs;
3029 if (curbuf->b_p_inf)
3030 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003031
3032 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3033 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003034 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003035 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3036 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003037 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003038 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003039
3040 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003041 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003042 len = STRLEN(pat_esc) + 10;
3043 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003044 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003045 {
3046 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003047 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003048 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003049 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003050 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3051 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003052 vim_free(ptr);
3053 }
3054 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003055 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003056 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003057 if (regmatch.regprog == NULL)
3058 goto theend;
3059 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003060
Bram Moolenaar071d4272004-06-13 20:20:40 +00003061 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3062 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003063 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003064 {
3065 /* copy one dictionary file name into buf */
3066 if (flags == DICT_EXACT)
3067 {
3068 count = 1;
3069 files = &dict;
3070 }
3071 else
3072 {
3073 /* Expand wildcards in the dictionary name, but do not allow
3074 * backticks (for security, the 'dict' option may have been set in
3075 * a modeline). */
3076 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003077# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003078 if (!thesaurus && STRCMP(buf, "spell") == 0)
3079 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003080 else
3081# endif
3082 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 || expand_wildcards(1, &buf, &count, &files,
3084 EW_FILE|EW_SILENT) != OK)
3085 count = 0;
3086 }
3087
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003088# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003089 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003091 /* Complete from active spelling. Skip "\<" in the pattern, we
3092 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003093 if (pat[0] == '\\' && pat[1] == '<')
3094 ptr = pat + 2;
3095 else
3096 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003097 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003099 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003100# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003101 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003102 {
3103 ins_compl_files(count, files, thesaurus, flags,
3104 &regmatch, buf, &dir);
3105 if (flags != DICT_EXACT)
3106 FreeWild(count, files);
3107 }
3108 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003109 break;
3110 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003111
3112theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 p_scs = save_p_scs;
3114 vim_free(regmatch.regprog);
3115 vim_free(buf);
3116}
3117
Bram Moolenaar0b238792006-03-02 22:49:12 +00003118 static void
3119ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3120 int count;
3121 char_u **files;
3122 int thesaurus;
3123 int flags;
3124 regmatch_T *regmatch;
3125 char_u *buf;
3126 int *dir;
3127{
3128 char_u *ptr;
3129 int i;
3130 FILE *fp;
3131 int add_r;
3132
3133 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3134 {
3135 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3136 if (flags != DICT_EXACT)
3137 {
3138 vim_snprintf((char *)IObuff, IOSIZE,
3139 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003140 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003141 }
3142
3143 if (fp != NULL)
3144 {
3145 /*
3146 * Read dictionary file line by line.
3147 * Check each line for a match.
3148 */
3149 while (!got_int && !compl_interrupted
3150 && !vim_fgets(buf, LSIZE, fp))
3151 {
3152 ptr = buf;
3153 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3154 {
3155 ptr = regmatch->startp[0];
3156 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3157 ptr = find_line_end(ptr);
3158 else
3159 ptr = find_word_end(ptr);
3160 add_r = ins_compl_add_infercase(regmatch->startp[0],
3161 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003162 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003163 if (thesaurus)
3164 {
3165 char_u *wstart;
3166
3167 /*
3168 * Add the other matches on the line
3169 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003170 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003171 while (!got_int)
3172 {
3173 /* Find start of the next word. Skip white
3174 * space and punctuation. */
3175 ptr = find_word_start(ptr);
3176 if (*ptr == NUL || *ptr == NL)
3177 break;
3178 wstart = ptr;
3179
Bram Moolenaara2993e12007-08-12 14:38:46 +00003180 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003181#ifdef FEAT_MBYTE
3182 if (has_mbyte)
3183 /* Japanese words may have characters in
3184 * different classes, only separate words
3185 * with single-byte non-word characters. */
3186 while (*ptr != NUL)
3187 {
3188 int l = (*mb_ptr2len)(ptr);
3189
3190 if (l < 2 && !vim_iswordc(*ptr))
3191 break;
3192 ptr += l;
3193 }
3194 else
3195#endif
3196 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003197
3198 /* Add the word. Skip the regexp match. */
3199 if (wstart != regmatch->startp[0])
3200 add_r = ins_compl_add_infercase(wstart,
3201 (int)(ptr - wstart),
3202 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003203 }
3204 }
3205 if (add_r == OK)
3206 /* if dir was BACKWARD then honor it just once */
3207 *dir = FORWARD;
3208 else if (add_r == FAIL)
3209 break;
3210 /* avoid expensive call to vim_regexec() when at end
3211 * of line */
3212 if (*ptr == '\n' || got_int)
3213 break;
3214 }
3215 line_breakcheck();
3216 ins_compl_check_keys(50);
3217 }
3218 fclose(fp);
3219 }
3220 }
3221}
3222
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223/*
3224 * Find the start of the next word.
3225 * Returns a pointer to the first char of the word. Also stops at a NUL.
3226 */
3227 char_u *
3228find_word_start(ptr)
3229 char_u *ptr;
3230{
3231#ifdef FEAT_MBYTE
3232 if (has_mbyte)
3233 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003234 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003235 else
3236#endif
3237 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3238 ++ptr;
3239 return ptr;
3240}
3241
3242/*
3243 * Find the end of the word. Assumes it starts inside a word.
3244 * Returns a pointer to just after the word.
3245 */
3246 char_u *
3247find_word_end(ptr)
3248 char_u *ptr;
3249{
3250#ifdef FEAT_MBYTE
3251 int start_class;
3252
3253 if (has_mbyte)
3254 {
3255 start_class = mb_get_class(ptr);
3256 if (start_class > 1)
3257 while (*ptr != NUL)
3258 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003259 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 if (mb_get_class(ptr) != start_class)
3261 break;
3262 }
3263 }
3264 else
3265#endif
3266 while (vim_iswordc(*ptr))
3267 ++ptr;
3268 return ptr;
3269}
3270
3271/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003272 * Find the end of the line, omitting CR and NL at the end.
3273 * Returns a pointer to just after the line.
3274 */
3275 static char_u *
3276find_line_end(ptr)
3277 char_u *ptr;
3278{
3279 char_u *s;
3280
3281 s = ptr + STRLEN(ptr);
3282 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3283 --s;
3284 return s;
3285}
3286
3287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 * Free the list of completions
3289 */
3290 static void
3291ins_compl_free()
3292{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003293 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003294 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003296 vim_free(compl_pattern);
3297 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003298 vim_free(compl_leader);
3299 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003300
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003301 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003303
3304 ins_compl_del_pum();
3305 pum_clear();
3306
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003307 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 do
3309 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003310 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003311 compl_curr_match = compl_curr_match->cp_next;
3312 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003313 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003314 if (match->cp_flags & FREE_FNAME)
3315 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003316 for (i = 0; i < CPT_COUNT; ++i)
3317 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003319 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3320 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003321 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003322}
3323
3324 static void
3325ins_compl_clear()
3326{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003327 compl_cont_status = 0;
3328 compl_started = FALSE;
3329 compl_matches = 0;
3330 vim_free(compl_pattern);
3331 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003332 vim_free(compl_leader);
3333 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003334 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003335 vim_free(compl_orig_text);
3336 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003337 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338}
3339
3340/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003341 * Return TRUE when Insert completion is active.
3342 */
3343 int
3344ins_compl_active()
3345{
3346 return compl_started;
3347}
3348
3349/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003350 * Delete one character before the cursor and show the subset of the matches
3351 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003352 * Returns the character to be used, NUL if the work is done and another char
3353 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003354 */
3355 static int
3356ins_compl_bs()
3357{
3358 char_u *line;
3359 char_u *p;
3360
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003361 line = ml_get_curline();
3362 p = line + curwin->w_cursor.col;
3363 mb_ptr_back(line, p);
3364
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003365 /* Stop completion when the whole word was deleted. For Omni completion
3366 * allow the word to be deleted, we won't match everything. */
3367 if ((int)(p - line) - (int)compl_col < 0
3368 || ((int)(p - line) - (int)compl_col == 0
3369 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003370 return K_BS;
3371
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003372 /* Deleted more than what was used to find matches or didn't finish
3373 * finding all matches: need to look for matches all over again. */
3374 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003375 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003376 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003377
Bram Moolenaara6557602006-02-04 22:43:20 +00003378 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003379 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003380 if (compl_leader != NULL)
3381 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003382 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003383 if (compl_shown_match != NULL)
3384 /* Make sure current match is not a hidden item. */
3385 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003386 return NUL;
3387 }
3388 return K_BS;
3389}
Bram Moolenaara6557602006-02-04 22:43:20 +00003390
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003391/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003392 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3393 * be called.
3394 */
3395 static int
3396ins_compl_need_restart()
3397{
3398 /* Return TRUE if we didn't complete finding matches or when the
3399 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3400 return compl_was_interrupted
3401 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3402 && compl_opt_refresh_always);
3403}
3404
3405/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003406 * Called after changing "compl_leader".
3407 * Show the popup menu with a different set of matches.
3408 * May also search for matches again if the previous search was interrupted.
3409 */
3410 static void
3411ins_compl_new_leader()
3412{
3413 ins_compl_del_pum();
3414 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003415 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003416 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003417
3418 if (compl_started)
3419 ins_compl_set_original_text(compl_leader);
3420 else
3421 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003422#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003423 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003424#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003425 /*
3426 * Matches were cleared, need to search for them now. First display
3427 * the changed text before the cursor. Set "compl_restarting" to
3428 * avoid that the first match is inserted.
3429 */
3430 update_screen(0);
3431#ifdef FEAT_GUI
3432 if (gui.in_use)
3433 {
3434 /* Show the cursor after the match, not after the redrawn text. */
3435 setcursor();
3436 out_flush();
3437 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003438 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003439#endif
3440 compl_restarting = TRUE;
3441 if (ins_complete(Ctrl_N) == FAIL)
3442 compl_cont_status = 0;
3443 compl_restarting = FALSE;
3444 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003445
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003446 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003447
3448 /* Show the popup menu with a different set of matches. */
3449 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003450
3451 /* Don't let Enter select the original text when there is no popup menu. */
3452 if (compl_match_array == NULL)
3453 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003454}
3455
3456/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003457 * Return the length of the completion, from the completion start column to
3458 * the cursor column. Making sure it never goes below zero.
3459 */
3460 static int
3461ins_compl_len()
3462{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003463 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003464
3465 if (off < 0)
3466 return 0;
3467 return off;
3468}
3469
3470/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003471 * Append one character to the match leader. May reduce the number of
3472 * matches.
3473 */
3474 static void
3475ins_compl_addleader(c)
3476 int c;
3477{
3478#ifdef FEAT_MBYTE
3479 int cc;
3480
3481 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3482 {
3483 char_u buf[MB_MAXBYTES + 1];
3484
3485 (*mb_char2bytes)(c, buf);
3486 buf[cc] = NUL;
3487 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003488 if (compl_opt_refresh_always)
3489 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003490 }
3491 else
3492#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003493 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003494 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003495 if (compl_opt_refresh_always)
3496 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003497 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003498
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003499 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003500 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003501 ins_compl_restart();
3502
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003503 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003504 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003505 * break redo. */
3506 if (!compl_opt_refresh_always)
3507 {
3508 vim_free(compl_leader);
3509 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003510 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003511 if (compl_leader != NULL)
3512 ins_compl_new_leader();
3513 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003514}
3515
3516/*
3517 * Setup for finding completions again without leaving CTRL-X mode. Used when
3518 * BS or a key was typed while still searching for matches.
3519 */
3520 static void
3521ins_compl_restart()
3522{
3523 ins_compl_free();
3524 compl_started = FALSE;
3525 compl_matches = 0;
3526 compl_cont_status = 0;
3527 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003528}
3529
3530/*
3531 * Set the first match, the original text.
3532 */
3533 static void
3534ins_compl_set_original_text(str)
3535 char_u *str;
3536{
3537 char_u *p;
3538
3539 /* Replace the original text entry. */
3540 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3541 {
3542 p = vim_strsave(str);
3543 if (p != NULL)
3544 {
3545 vim_free(compl_first_match->cp_str);
3546 compl_first_match->cp_str = p;
3547 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003548 }
3549}
3550
3551/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003552 * Append one character to the match leader. May reduce the number of
3553 * matches.
3554 */
3555 static void
3556ins_compl_addfrommatch()
3557{
3558 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003559 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003560 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003561 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003562
3563 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003564 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003565 {
3566 /* When still at the original match use the first entry that matches
3567 * the leader. */
3568 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3569 {
3570 p = NULL;
3571 for (cp = compl_shown_match->cp_next; cp != NULL
3572 && cp != compl_first_match; cp = cp->cp_next)
3573 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003574 if (compl_leader == NULL
3575 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003576 (int)STRLEN(compl_leader)))
3577 {
3578 p = cp->cp_str;
3579 break;
3580 }
3581 }
3582 if (p == NULL || (int)STRLEN(p) <= len)
3583 return;
3584 }
3585 else
3586 return;
3587 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003588 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003589 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003590 ins_compl_addleader(c);
3591}
3592
3593/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003595 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003596 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003598 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599ins_compl_prep(c)
3600 int c;
3601{
3602 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003604 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605
3606 /* Forget any previous 'special' messages if this is actually
3607 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3608 */
3609 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3610 edit_submode_extra = NULL;
3611
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003612 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003613 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3614 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003615 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003616
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003617 /* Set "compl_get_longest" when finding the first matches. */
3618 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3619 || (ctrl_x_mode == 0 && !compl_started))
3620 {
3621 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3622 compl_used_match = TRUE;
3623 }
3624
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3626 {
3627 /*
3628 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3629 * it will be yet. Now we decide.
3630 */
3631 switch (c)
3632 {
3633 case Ctrl_E:
3634 case Ctrl_Y:
3635 ctrl_x_mode = CTRL_X_SCROLL;
3636 if (!(State & REPLACE_FLAG))
3637 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3638 else
3639 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3640 edit_submode_pre = NULL;
3641 showmode();
3642 break;
3643 case Ctrl_L:
3644 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3645 break;
3646 case Ctrl_F:
3647 ctrl_x_mode = CTRL_X_FILES;
3648 break;
3649 case Ctrl_K:
3650 ctrl_x_mode = CTRL_X_DICTIONARY;
3651 break;
3652 case Ctrl_R:
3653 /* Simply allow ^R to happen without affecting ^X mode */
3654 break;
3655 case Ctrl_T:
3656 ctrl_x_mode = CTRL_X_THESAURUS;
3657 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003658#ifdef FEAT_COMPL_FUNC
3659 case Ctrl_U:
3660 ctrl_x_mode = CTRL_X_FUNCTION;
3661 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003662 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003663 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003664 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003665#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003666 case 's':
3667 case Ctrl_S:
3668 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003669#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003670 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003671 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003672 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003673#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003674 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 case Ctrl_RSB:
3676 ctrl_x_mode = CTRL_X_TAGS;
3677 break;
3678#ifdef FEAT_FIND_ID
3679 case Ctrl_I:
3680 case K_S_TAB:
3681 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3682 break;
3683 case Ctrl_D:
3684 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3685 break;
3686#endif
3687 case Ctrl_V:
3688 case Ctrl_Q:
3689 ctrl_x_mode = CTRL_X_CMDLINE;
3690 break;
3691 case Ctrl_P:
3692 case Ctrl_N:
3693 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3694 * just started ^X mode, or there were enough ^X's to cancel
3695 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3696 * do normal expansion when interrupting a different mode (say
3697 * ^X^F^X^P or ^P^X^X^P, see below)
3698 * nothing changes if interrupting mode 0, (eg, the flag
3699 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003700 if (!(compl_cont_status & CONT_INTRPT))
3701 compl_cont_status |= CONT_LOCAL;
3702 else if (compl_cont_mode != 0)
3703 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 /* FALLTHROUGH */
3705 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003706 /* If we have typed at least 2 ^X's... for modes != 0, we set
3707 * compl_cont_status = 0 (eg, as if we had just started ^X
3708 * mode).
3709 * For mode 0, we set "compl_cont_mode" to an impossible
3710 * value, in both cases ^X^X can be used to restart the same
3711 * mode (avoiding ADDING mode).
3712 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3713 * 'complete' and local ^P expansions respectively.
3714 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3715 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003716 if (c == Ctrl_X)
3717 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003718 if (compl_cont_mode != 0)
3719 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003721 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003722 }
3723 ctrl_x_mode = 0;
3724 edit_submode = NULL;
3725 showmode();
3726 break;
3727 }
3728 }
3729 else if (ctrl_x_mode != 0)
3730 {
3731 /* We're already in CTRL-X mode, do we stay in it? */
3732 if (!vim_is_ctrl_x_key(c))
3733 {
3734 if (ctrl_x_mode == CTRL_X_SCROLL)
3735 ctrl_x_mode = 0;
3736 else
3737 ctrl_x_mode = CTRL_X_FINISHED;
3738 edit_submode = NULL;
3739 }
3740 showmode();
3741 }
3742
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003743 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
3745 /* Show error message from attempted keyword completion (probably
3746 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003747 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003749 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3750 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 || ctrl_x_mode == CTRL_X_FINISHED)
3752 {
3753 /* Get here when we have finished typing a sequence of ^N and
3754 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003755 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003756 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
3758 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003759 * If any of the original typed text has been changed, eg when
3760 * ignorecase is set, we must add back-spaces to the redo
3761 * buffer. We add as few as necessary to delete just the part
3762 * of the original text that has changed.
3763 * When using the longest match, edited the match or used
3764 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003766 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3767 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003768 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003769 ptr = NULL;
3770 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 }
3772
3773#ifdef FEAT_CINDENT
3774 want_cindent = (can_cindent && cindent_on());
3775#endif
3776 /*
3777 * When completing whole lines: fix indent for 'cindent'.
3778 * Otherwise, break line if it's too long.
3779 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003780 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 {
3782#ifdef FEAT_CINDENT
3783 /* re-indent the current line */
3784 if (want_cindent)
3785 {
3786 do_c_expr_indent();
3787 want_cindent = FALSE; /* don't do it again */
3788 }
3789#endif
3790 }
3791 else
3792 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003793 int prev_col = curwin->w_cursor.col;
3794
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003796 if (prev_col > 0)
3797 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003798 if (stop_arrow() == OK)
3799 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003800 if (prev_col > 0
3801 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3802 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003803 }
3804
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003805 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003806 * the selection without inserting anything. When
3807 * compl_enter_selects is set the Enter key does the same. */
3808 if ((c == Ctrl_Y || (compl_enter_selects
3809 && (c == CAR || c == K_KENTER || c == NL)))
3810 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003811 retval = TRUE;
3812
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003813 /* CTRL-E means completion is Ended, go back to the typed text. */
3814 if (c == Ctrl_E)
3815 {
3816 ins_compl_delete();
3817 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003818 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003819 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003820 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003821 retval = TRUE;
3822 }
3823
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003824 auto_format(FALSE, TRUE);
3825
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003827 compl_started = FALSE;
3828 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003829 msg_clr_cmdline(); /* necessary for "noshowmode" */
3830 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003831 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 if (edit_submode != NULL)
3833 {
3834 edit_submode = NULL;
3835 showmode();
3836 }
3837
3838#ifdef FEAT_CINDENT
3839 /*
3840 * Indent now if a key was typed that is in 'cinkeys'.
3841 */
3842 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3843 do_c_expr_indent();
3844#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003845#ifdef FEAT_AUTOCMD
3846 /* Trigger the CompleteDone event to give scripts a chance to act
3847 * upon the completion. */
3848 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3849#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 }
3851 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003852#ifdef FEAT_AUTOCMD
3853 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3854 /* Trigger the CompleteDone event to give scripts a chance to act
3855 * upon the (possibly failed) completion. */
3856 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3857#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003858
3859 /* reset continue_* if we left expansion-mode, if we stay they'll be
3860 * (re)set properly in ins_complete() */
3861 if (!vim_is_ctrl_x_key(c))
3862 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003863 compl_cont_status = 0;
3864 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003866
3867 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003868}
3869
3870/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003871 * Fix the redo buffer for the completion leader replacing some of the typed
3872 * text. This inserts backspaces and appends the changed text.
3873 * "ptr" is the known leader text or NUL.
3874 */
3875 static void
3876ins_compl_fixRedoBufForLeader(ptr_arg)
3877 char_u *ptr_arg;
3878{
3879 int len;
3880 char_u *p;
3881 char_u *ptr = ptr_arg;
3882
3883 if (ptr == NULL)
3884 {
3885 if (compl_leader != NULL)
3886 ptr = compl_leader;
3887 else
3888 return; /* nothing to do */
3889 }
3890 if (compl_orig_text != NULL)
3891 {
3892 p = compl_orig_text;
3893 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3894 ;
3895#ifdef FEAT_MBYTE
3896 if (len > 0)
3897 len -= (*mb_head_off)(p, p + len);
3898#endif
3899 for (p += len; *p != NUL; mb_ptr_adv(p))
3900 AppendCharToRedobuff(K_BS);
3901 }
3902 else
3903 len = 0;
3904 if (ptr != NULL)
3905 AppendToRedobuffLit(ptr + len, -1);
3906}
3907
3908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003909 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3910 * (depending on flag) starting from buf and looking for a non-scanned
3911 * buffer (other than curbuf). curbuf is special, if it is called with
3912 * buf=curbuf then it has to be the first call for a given flag/expansion.
3913 *
3914 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3915 */
3916 static buf_T *
3917ins_compl_next_buf(buf, flag)
3918 buf_T *buf;
3919 int flag;
3920{
3921#ifdef FEAT_WINDOWS
3922 static win_T *wp;
3923#endif
3924
3925 if (flag == 'w') /* just windows */
3926 {
3927#ifdef FEAT_WINDOWS
3928 if (buf == curbuf) /* first call for this flag/expansion */
3929 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003930 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003931 && wp->w_buffer->b_scanned)
3932 ;
3933 buf = wp->w_buffer;
3934#else
3935 buf = curbuf;
3936#endif
3937 }
3938 else
3939 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3940 * (unlisted buffers)
3941 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003942 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 && ((flag == 'U'
3944 ? buf->b_p_bl
3945 : (!buf->b_p_bl
3946 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003947 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 ;
3949 return buf;
3950}
3951
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003952#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003953static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003954
3955/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003956 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003957 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003958 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003959 static void
3960expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003961 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003962 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003963{
Bram Moolenaar82139082011-09-14 16:52:09 +02003964 list_T *matchlist = NULL;
3965 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003966 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003967 char_u *funcname;
3968 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003969 win_T *curwin_save;
3970 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02003971 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003972
Bram Moolenaare344bea2005-09-01 20:46:49 +00003973 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3974 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003975 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003976
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003977 /* Call 'completefunc' to obtain the list of matches. */
3978 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003979 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003980
Bram Moolenaare344bea2005-09-01 20:46:49 +00003981 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003982 curwin_save = curwin;
3983 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02003984
3985 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02003986 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02003987 {
3988 switch (rettv.v_type)
3989 {
3990 case VAR_LIST:
3991 matchlist = rettv.vval.v_list;
3992 break;
3993 case VAR_DICT:
3994 matchdict = rettv.vval.v_dict;
3995 break;
3996 default:
3997 /* TODO: Give error message? */
3998 clear_tv(&rettv);
3999 break;
4000 }
4001 }
4002
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004003 if (curwin_save != curwin || curbuf_save != curbuf)
4004 {
4005 EMSG(_(e_complwin));
4006 goto theend;
4007 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004008 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004009 check_cursor();
4010 if (!equalpos(curwin->w_cursor, pos))
4011 {
4012 EMSG(_(e_compldel));
4013 goto theend;
4014 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004015
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004016 if (matchlist != NULL)
4017 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004018 else if (matchdict != NULL)
4019 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004020
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004021theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004022 if (matchdict != NULL)
4023 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004024 if (matchlist != NULL)
4025 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004026}
4027#endif /* FEAT_COMPL_FUNC */
4028
Bram Moolenaar39f05632006-03-19 22:15:26 +00004029#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004030/*
4031 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004032 */
4033 static void
4034ins_compl_add_list(list)
4035 list_T *list;
4036{
4037 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004038 int dir = compl_direction;
4039
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004040 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004041 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004042 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004043 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4044 /* if dir was BACKWARD then honor it just once */
4045 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004046 else if (did_emsg)
4047 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004048 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004049}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004050
4051/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004052 * Add completions from a dict.
4053 */
4054 static void
4055ins_compl_add_dict(dict)
4056 dict_T *dict;
4057{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004058 dictitem_T *di_refresh;
4059 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004060
4061 /* Check for optional "refresh" item. */
4062 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004063 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4064 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004065 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004066 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004067
4068 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4069 compl_opt_refresh_always = TRUE;
4070 }
4071
4072 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004073 di_words = dict_find(dict, (char_u *)"words", 5);
4074 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4075 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004076}
4077
4078/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004079 * Add a match to the list of matches from a typeval_T.
4080 * If the given string is already in the list of completions, then return
4081 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4082 * maybe because alloc() returns NULL, then FAIL is returned.
4083 */
4084 int
4085ins_compl_add_tv(tv, dir)
4086 typval_T *tv;
4087 int dir;
4088{
4089 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004090 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004091 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004092 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004093 char_u *(cptext[CPT_COUNT]);
4094
4095 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4096 {
4097 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4098 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4099 (char_u *)"abbr", FALSE);
4100 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4101 (char_u *)"menu", FALSE);
4102 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4103 (char_u *)"kind", FALSE);
4104 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4105 (char_u *)"info", FALSE);
4106 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4107 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004108 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004109 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004110 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4111 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004112 }
4113 else
4114 {
4115 word = get_tv_string_chk(tv);
4116 vim_memset(cptext, 0, sizeof(cptext));
4117 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004118 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004119 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004120 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004121}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004122#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124/*
4125 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004126 * The search starts at position "ini" in curbuf and in the direction
4127 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004128 * When "compl_started" is FALSE start at that position, otherwise continue
4129 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 * This may return before finding all the matches.
4131 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 */
4133 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004134ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136{
4137 static pos_T first_match_pos;
4138 static pos_T last_match_pos;
4139 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004140 static int found_all = FALSE; /* Found all matches of a
4141 certain type. */
4142 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143
Bram Moolenaar572cb562005-08-05 21:35:02 +00004144 pos_T *pos;
4145 char_u **matches;
4146 int save_p_scs;
4147 int save_p_ws;
4148 int save_p_ic;
4149 int i;
4150 int num_matches;
4151 int len;
4152 int found_new_match;
4153 int type = ctrl_x_mode;
4154 char_u *ptr;
4155 char_u *dict = NULL;
4156 int dict_f = 0;
4157 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004159 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 {
4161 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4162 ins_buf->b_scanned = 0;
4163 found_all = FALSE;
4164 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004165 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004166 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 last_match_pos = first_match_pos = *ini;
4168 }
4169
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004170 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004171 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4173 for (;;)
4174 {
4175 found_new_match = FAIL;
4176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004177 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 * or if found_all says this entry is done. For ^X^L only use the
4179 * entries from 'complete' that look in loaded buffers. */
4180 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004181 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 {
4183 found_all = FALSE;
4184 while (*e_cpt == ',' || *e_cpt == ' ')
4185 e_cpt++;
4186 if (*e_cpt == '.' && !curbuf->b_scanned)
4187 {
4188 ins_buf = curbuf;
4189 first_match_pos = *ini;
4190 /* So that ^N can match word immediately after cursor */
4191 if (ctrl_x_mode == 0)
4192 dec(&first_match_pos);
4193 last_match_pos = first_match_pos;
4194 type = 0;
4195 }
4196 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4197 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4198 {
4199 /* Scan a buffer, but not the current one. */
4200 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4201 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004202 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 first_match_pos.col = last_match_pos.col = 0;
4204 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4205 last_match_pos.lnum = 0;
4206 type = 0;
4207 }
4208 else /* unloaded buffer, scan like dictionary */
4209 {
4210 found_all = TRUE;
4211 if (ins_buf->b_fname == NULL)
4212 continue;
4213 type = CTRL_X_DICTIONARY;
4214 dict = ins_buf->b_fname;
4215 dict_f = DICT_EXACT;
4216 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004217 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 ins_buf->b_fname == NULL
4219 ? buf_spname(ins_buf)
4220 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004221 ? ins_buf->b_fname
4222 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004223 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225 else if (*e_cpt == NUL)
4226 break;
4227 else
4228 {
4229 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4230 type = -1;
4231 else if (*e_cpt == 'k' || *e_cpt == 's')
4232 {
4233 if (*e_cpt == 'k')
4234 type = CTRL_X_DICTIONARY;
4235 else
4236 type = CTRL_X_THESAURUS;
4237 if (*++e_cpt != ',' && *e_cpt != NUL)
4238 {
4239 dict = e_cpt;
4240 dict_f = DICT_FIRST;
4241 }
4242 }
4243#ifdef FEAT_FIND_ID
4244 else if (*e_cpt == 'i')
4245 type = CTRL_X_PATH_PATTERNS;
4246 else if (*e_cpt == 'd')
4247 type = CTRL_X_PATH_DEFINES;
4248#endif
4249 else if (*e_cpt == ']' || *e_cpt == 't')
4250 {
4251 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004252 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004253 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254 }
4255 else
4256 type = -1;
4257
4258 /* in any case e_cpt is advanced to the next entry */
4259 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4260
4261 found_all = TRUE;
4262 if (type == -1)
4263 continue;
4264 }
4265 }
4266
4267 switch (type)
4268 {
4269 case -1:
4270 break;
4271#ifdef FEAT_FIND_ID
4272 case CTRL_X_PATH_PATTERNS:
4273 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004274 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004277 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4279 (linenr_T)1, (linenr_T)MAXLNUM);
4280 break;
4281#endif
4282
4283 case CTRL_X_DICTIONARY:
4284 case CTRL_X_THESAURUS:
4285 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004286 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 : (type == CTRL_X_THESAURUS
4288 ? (*curbuf->b_p_tsr == NUL
4289 ? p_tsr
4290 : curbuf->b_p_tsr)
4291 : (*curbuf->b_p_dict == NUL
4292 ? p_dict
4293 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004294 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004295 dict != NULL ? dict_f
4296 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 dict = NULL;
4298 break;
4299
4300 case CTRL_X_TAGS:
4301 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4302 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004303 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004305 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004306 * of matches is found when compl_pattern is empty */
4307 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4309 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4310 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4311 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004312 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 }
4314 p_ic = save_p_ic;
4315 break;
4316
4317 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4320 {
4321
4322 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004324 ins_compl_add_matches(num_matches, matches,
4325#ifdef CASE_INSENSITIVE_FILENAME
4326 TRUE
4327#else
4328 FALSE
4329#endif
4330 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 }
4332 break;
4333
4334 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004335 if (expand_cmdline(&compl_xp, compl_pattern,
4336 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004338 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 break;
4340
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004341#ifdef FEAT_COMPL_FUNC
4342 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004343 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004344 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004345 break;
4346#endif
4347
Bram Moolenaar488c6512005-08-11 20:09:58 +00004348 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004349#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004350 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004351 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004352 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004353 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004354#endif
4355 break;
4356
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 default: /* normal ^P/^N and ^X^L */
4358 /*
4359 * If 'infercase' is set, don't use 'smartcase' here
4360 */
4361 save_p_scs = p_scs;
4362 if (ins_buf->b_p_inf)
4363 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004364
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 /* buffers other than curbuf are scanned from the beginning or the
4366 * end but never from the middle, thus setting nowrapscan in this
4367 * buffers is a good idea, on the other hand, we always set
4368 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4369 save_p_ws = p_ws;
4370 if (ins_buf != curbuf)
4371 p_ws = FALSE;
4372 else if (*e_cpt == '.')
4373 p_ws = TRUE;
4374 for (;;)
4375 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004376 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004378 ++msg_silent; /* Don't want messages for wrapscan. */
4379
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004380 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4381 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004383 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004385 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004387 found_new_match = searchit(NULL, ins_buf, pos,
4388 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004389 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004390 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004391 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004392 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004394 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004395 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 first_match_pos = *pos;
4397 last_match_pos = *pos;
4398 }
4399 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004400 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 found_new_match = FAIL;
4402 if (found_new_match == FAIL)
4403 {
4404 if (ins_buf == curbuf)
4405 found_all = TRUE;
4406 break;
4407 }
4408
4409 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004410 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 && ini->lnum == pos->lnum
4412 && ini->col == pos->col)
4413 continue;
4414 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4415 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4416 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 {
4419 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4420 continue;
4421 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4422 if (!p_paste)
4423 ptr = skipwhite(ptr);
4424 }
4425 len = (int)STRLEN(ptr);
4426 }
4427 else
4428 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004429 char_u *tmp_ptr = ptr;
4430
4431 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004433 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 /* Skip if already inside a word. */
4435 if (vim_iswordp(tmp_ptr))
4436 continue;
4437 /* Find start of next word. */
4438 tmp_ptr = find_word_start(tmp_ptr);
4439 }
4440 /* Find end of this word. */
4441 tmp_ptr = find_word_end(tmp_ptr);
4442 len = (int)(tmp_ptr - ptr);
4443
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 if ((compl_cont_status & CONT_ADDING)
4445 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 {
4447 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4448 {
4449 /* Try next line, if any. the new word will be
4450 * "join" as if the normal command "J" was used.
4451 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004452 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 * works -- Acevedo */
4454 STRNCPY(IObuff, ptr, len);
4455 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4456 tmp_ptr = ptr = skipwhite(ptr);
4457 /* Find start of next word. */
4458 tmp_ptr = find_word_start(tmp_ptr);
4459 /* Find end of next word. */
4460 tmp_ptr = find_word_end(tmp_ptr);
4461 if (tmp_ptr > ptr)
4462 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004463 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004465 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004466 IObuff[len++] = ' ';
4467 /* IObuf =~ "\k.* ", thus len >= 2 */
4468 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004469 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 || (vim_strchr(p_cpo, CPO_JOINSP)
4471 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004472 && (IObuff[len - 2] == '?'
4473 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 IObuff[len++] = ' ';
4475 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004476 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 if (tmp_ptr - ptr >= IOSIZE - len)
4478 tmp_ptr = ptr + IOSIZE - len - 1;
4479 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4480 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004481 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 IObuff[len] = NUL;
4484 ptr = IObuff;
4485 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004486 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 continue;
4488 }
4489 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004490 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004491 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004492 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 {
4494 found_new_match = OK;
4495 break;
4496 }
4497 }
4498 p_scs = save_p_scs;
4499 p_ws = save_p_ws;
4500 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004501
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004502 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004503 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004504 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 found_new_match = OK;
4506
4507 /* break the loop for specialized modes (use 'complete' just for the
4508 * generic ctrl_x_mode == 0) or when we've found a new match */
4509 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004510 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004511 {
4512 if (got_int)
4513 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004514 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004515 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004516 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004517
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004518 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4519 || compl_interrupted)
4520 break;
4521 compl_started = TRUE;
4522 }
4523 else
4524 {
4525 /* Mark a buffer scanned when it has been scanned completely */
4526 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4527 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004529 compl_started = FALSE;
4530 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533
4534 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4535 && *e_cpt == NUL) /* Got to end of 'complete' */
4536 found_new_match = FAIL;
4537
4538 i = -1; /* total of matches, unknown */
4539 if (found_new_match == FAIL
4540 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4541 i = ins_compl_make_cyclic();
4542
4543 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004544 * just been made cyclic then we have to move compl_curr_match to the next
4545 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004546 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4547 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004548 if (compl_curr_match == NULL)
4549 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004550 return i;
4551}
4552
4553/* Delete the old text being completed. */
4554 static void
4555ins_compl_delete()
4556{
4557 int i;
4558
4559 /*
4560 * In insert mode: Delete the typed part.
4561 * In replace mode: Put the old characters back, if any.
4562 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 backspace_until_column(i);
4565 changed_cline_bef_curs();
4566}
4567
4568/* Insert the new text being completed. */
4569 static void
4570ins_compl_insert()
4571{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004572 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004573 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4574 compl_used_match = FALSE;
4575 else
4576 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577}
4578
4579/*
4580 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004581 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4582 * get more completions. If it is FALSE, then we just do nothing when there
4583 * are no more completions in a given direction. The latter case is used when
4584 * we are still in the middle of finding completions, to allow browsing
4585 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 * Return the total number of matches, or -1 if still unknown -- webb.
4587 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004588 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4589 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004590 *
4591 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004592 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4593 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 */
4595 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004596ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004598 int count; /* repeat completion this many times; should
4599 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004600 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601{
4602 int num_matches = -1;
4603 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004604 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004605 compl_T *found_compl = NULL;
4606 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004607 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004609 /* When user complete function return -1 for findstart which is next
4610 * time of 'always', compl_shown_match become NULL. */
4611 if (compl_shown_match == NULL)
4612 return -1;
4613
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004614 if (compl_leader != NULL
4615 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004617 /* Set "compl_shown_match" to the actually shown match, it may differ
4618 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004619 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004620 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004621 && compl_shown_match->cp_next != NULL
4622 && compl_shown_match->cp_next != compl_first_match)
4623 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004624
4625 /* If we didn't find it searching forward, and compl_shows_dir is
4626 * backward, find the last match. */
4627 if (compl_shows_dir == BACKWARD
4628 && !ins_compl_equal(compl_shown_match,
4629 compl_leader, (int)STRLEN(compl_leader))
4630 && (compl_shown_match->cp_next == NULL
4631 || compl_shown_match->cp_next == compl_first_match))
4632 {
4633 while (!ins_compl_equal(compl_shown_match,
4634 compl_leader, (int)STRLEN(compl_leader))
4635 && compl_shown_match->cp_prev != NULL
4636 && compl_shown_match->cp_prev != compl_first_match)
4637 compl_shown_match = compl_shown_match->cp_prev;
4638 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004639 }
4640
4641 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004642 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 /* Delete old text to be replaced */
4644 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004645
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004646 /* When finding the longest common text we stick at the original text,
4647 * don't let CTRL-N or CTRL-P move to the first match. */
4648 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4649
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004650 /* When restarting the search don't insert the first match either. */
4651 if (compl_restarting)
4652 {
4653 advance = FALSE;
4654 compl_restarting = FALSE;
4655 }
4656
Bram Moolenaare3226be2005-12-18 22:10:00 +00004657 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4658 * around. */
4659 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004661 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004662 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004663 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004664 found_end = (compl_first_match != NULL
4665 && (compl_shown_match->cp_next == compl_first_match
4666 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004667 }
4668 else if (compl_shows_dir == BACKWARD
4669 && compl_shown_match->cp_prev != NULL)
4670 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004671 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004672 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004673 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 }
4675 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004676 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004677 if (!allow_get_expansion)
4678 {
4679 if (advance)
4680 {
4681 if (compl_shows_dir == BACKWARD)
4682 compl_pending -= todo + 1;
4683 else
4684 compl_pending += todo + 1;
4685 }
4686 return -1;
4687 }
4688
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004689 if (advance)
4690 {
4691 if (compl_shows_dir == BACKWARD)
4692 --compl_pending;
4693 else
4694 ++compl_pending;
4695 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004696
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004697 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004698 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004699
4700 /* handle any pending completions */
4701 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004702 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004703 {
4704 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4705 {
4706 compl_shown_match = compl_shown_match->cp_next;
4707 --compl_pending;
4708 }
4709 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4710 {
4711 compl_shown_match = compl_shown_match->cp_prev;
4712 ++compl_pending;
4713 }
4714 else
4715 break;
4716 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004717 found_end = FALSE;
4718 }
4719 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4720 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004721 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004722 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004723 ++todo;
4724 else
4725 /* Remember a matching item. */
4726 found_compl = compl_shown_match;
4727
4728 /* Stop at the end of the list when we found a usable match. */
4729 if (found_end)
4730 {
4731 if (found_compl != NULL)
4732 {
4733 compl_shown_match = found_compl;
4734 break;
4735 }
4736 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 }
4739
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004740 /* Insert the text of the new completion, or the compl_leader. */
4741 if (insert_match)
4742 {
4743 if (!compl_get_longest || compl_used_match)
4744 ins_compl_insert();
4745 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004746 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004747 }
4748 else
4749 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750
4751 if (!allow_get_expansion)
4752 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004753 /* may undisplay the popup menu first */
4754 ins_compl_upd_pum();
4755
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004756 /* redraw to show the user what was inserted */
4757 update_screen(0);
4758
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004759 /* display the updated popup menu */
4760 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004761#ifdef FEAT_GUI
4762 if (gui.in_use)
4763 {
4764 /* Show the cursor after the match, not after the redrawn text. */
4765 setcursor();
4766 out_flush();
4767 gui_update_cursor(FALSE, FALSE);
4768 }
4769#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004770
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 /* Delete old text to be replaced, since we're still searching and
4772 * don't want to match ourselves! */
4773 ins_compl_delete();
4774 }
4775
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004776 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004777 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004778 compl_enter_selects = !insert_match && compl_match_array != NULL;
4779
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 /*
4781 * Show the file name for the match (if any)
4782 * Truncate the file name to avoid a wait for return.
4783 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004784 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 {
4786 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004787 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (i <= 0)
4789 i = 0;
4790 else
4791 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004792 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 msg(IObuff);
4794 redraw_cmdline = FALSE; /* don't overwrite! */
4795 }
4796
4797 return num_matches;
4798}
4799
4800/*
4801 * Call this while finding completions, to check whether the user has hit a key
4802 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004803 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004805 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 */
4807 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004808ins_compl_check_keys(frequency)
4809 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810{
4811 static int count = 0;
4812
4813 int c;
4814
4815 /* Don't check when reading keys from a script. That would break the test
4816 * scripts */
4817 if (using_script())
4818 return;
4819
4820 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004821 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return;
4823 count = 0;
4824
Bram Moolenaara260a972006-06-23 19:36:29 +00004825 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4826 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 if (c != NUL)
4829 {
4830 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4831 {
4832 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004833 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004834 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4835 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004837 else
4838 {
4839 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004840 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004841 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004842 if (c != K_IGNORE)
4843 {
4844 /* Don't interrupt completion when the character wasn't typed,
4845 * e.g., when doing @q to replay keys. */
4846 if (c != Ctrl_R && KeyTyped)
4847 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004848
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004849 vungetc(c);
4850 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004853 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004854 {
4855 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4856
4857 compl_pending = 0;
4858 (void)ins_compl_next(FALSE, todo, TRUE);
4859 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004860}
4861
4862/*
4863 * Decide the direction of Insert mode complete from the key typed.
4864 * Returns BACKWARD or FORWARD.
4865 */
4866 static int
4867ins_compl_key2dir(c)
4868 int c;
4869{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004870 if (c == Ctrl_P || c == Ctrl_L
4871 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4872 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004873 return BACKWARD;
4874 return FORWARD;
4875}
4876
4877/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004878 * Return TRUE for keys that are used for completion only when the popup menu
4879 * is visible.
4880 */
4881 static int
4882ins_compl_pum_key(c)
4883 int c;
4884{
4885 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004886 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4887 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004888}
4889
4890/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004891 * Decide the number of completions to move forward.
4892 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4893 */
4894 static int
4895ins_compl_key2count(c)
4896 int c;
4897{
4898 int h;
4899
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004900 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004901 {
4902 h = pum_get_height();
4903 if (h > 3)
4904 h -= 2; /* keep some context */
4905 return h;
4906 }
4907 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908}
4909
4910/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004911 * Return TRUE if completion with "c" should insert the match, FALSE if only
4912 * to change the currently selected completion.
4913 */
4914 static int
4915ins_compl_use_match(c)
4916 int c;
4917{
4918 switch (c)
4919 {
4920 case K_UP:
4921 case K_DOWN:
4922 case K_PAGEDOWN:
4923 case K_KPAGEDOWN:
4924 case K_S_DOWN:
4925 case K_PAGEUP:
4926 case K_KPAGEUP:
4927 case K_S_UP:
4928 return FALSE;
4929 }
4930 return TRUE;
4931}
4932
4933/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 * Do Insert mode completion.
4935 * Called when character "c" was typed, which has a meaning for completion.
4936 * Returns OK if completion was done, FAIL if something failed (out of mem).
4937 */
4938 static int
4939ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004940 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004942 char_u *line;
4943 int startcol = 0; /* column where searched text starts */
4944 colnr_T curs_col; /* cursor column */
4945 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004946 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004947
Bram Moolenaare3226be2005-12-18 22:10:00 +00004948 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004949 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 {
4951 /* First time we hit ^N or ^P (in a row, I mean) */
4952
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 did_ai = FALSE;
4954#ifdef FEAT_SMARTINDENT
4955 did_si = FALSE;
4956 can_si = FALSE;
4957 can_si_back = FALSE;
4958#endif
4959 if (stop_arrow() == FAIL)
4960 return FAIL;
4961
4962 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004963 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004964 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004966 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004967 * "compl_startpos" to the cursor as a pattern to add a new word
4968 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004969 * "compl_startpos" is not in the same line as the cursor then fix it
4970 * (the line has been split because it was longer than 'tw'). if SOL
4971 * is set then skip the previous pattern, a word at the beginning of
4972 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004973 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4974 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 {
4976 /*
4977 * it is a continued search
4978 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004979 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4981 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4982 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004983 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004985 /* line (probably) wrapped, set compl_startpos to the
4986 * first non_blank in the line, if it is not a wordchar
4987 * include it to get a better pattern, but then we don't
4988 * want the "\\<" prefix, check it bellow */
4989 compl_col = (colnr_T)(skipwhite(line) - line);
4990 compl_startpos.col = compl_col;
4991 compl_startpos.lnum = curwin->w_cursor.lnum;
4992 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 }
4994 else
4995 {
4996 /* S_IPOS was set when we inserted a word that was at the
4997 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004998 * mode but first we need to redefine compl_startpos */
4999 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005001 compl_cont_status |= CONT_SOL;
5002 compl_startpos.col = (colnr_T)(skipwhite(
5003 line + compl_length
5004 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005006 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005007 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005008 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005009 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005010 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005012 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005014 compl_cont_status &= ~CONT_SOL;
5015 compl_length = (IOSIZE - MIN_SPACE);
5016 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005018 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5019 if (compl_length < 1)
5020 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005023 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005025 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
5027 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005028 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005030 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005032 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005034 compl_cont_status = 0;
5035 compl_cont_status |= CONT_N_ADDS;
5036 compl_startpos = curwin->w_cursor;
5037 startcol = (int)curs_col;
5038 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
5040
5041 /* Work out completion pattern and original text -- webb */
5042 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5043 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005044 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5046 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005047 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005049 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005051 compl_col += ++startcol;
5052 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 }
5054 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005055 compl_pattern = str_foldcase(line + compl_col,
5056 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005058 compl_pattern = vim_strnsave(line + compl_col,
5059 compl_length);
5060 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 return FAIL;
5062 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005063 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 {
5065 char_u *prefix = (char_u *)"\\<";
5066
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005067 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005068 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005069 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005070 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005072 if (!vim_iswordp(line + compl_col)
5073 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 && (
5075#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005076 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005078 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079#endif
5080 )))
5081 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005082 STRCPY((char *)compl_pattern, prefix);
5083 (void)quote_meta(compl_pattern + STRLEN(prefix),
5084 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005086 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005088 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005090 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091#endif
5092 )
5093 {
5094 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5096 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005098 compl_col += curs_col;
5099 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101 else
5102 {
5103#ifdef FEAT_MBYTE
5104 /* Search the point of change class of multibyte character
5105 * or not a word single byte character backward. */
5106 if (has_mbyte)
5107 {
5108 int base_class;
5109 int head_off;
5110
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 startcol -= (*mb_head_off)(line, line + startcol);
5112 base_class = mb_get_class(line + startcol);
5113 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005114 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005115 head_off = (*mb_head_off)(line, line + startcol);
5116 if (base_class != mb_get_class(line + startcol
5117 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005119 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 }
5122 else
5123#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005124 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005125 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005126 compl_col += ++startcol;
5127 compl_length = (int)curs_col - startcol;
5128 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
5130 /* Only match word with at least two chars -- webb
5131 * there's no need to call quote_meta,
5132 * alloc(7) is enough -- Acevedo
5133 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005134 compl_pattern = alloc(7);
5135 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005137 STRCPY((char *)compl_pattern, "\\<");
5138 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5139 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 }
5141 else
5142 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005143 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005144 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005145 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005147 STRCPY((char *)compl_pattern, "\\<");
5148 (void)quote_meta(compl_pattern + 2, line + compl_col,
5149 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 }
5152 }
5153 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5154 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005155 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005156 compl_length = (int)curs_col - (int)compl_col;
5157 if (compl_length < 0) /* cursor in indent: empty pattern */
5158 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 compl_pattern = str_foldcase(line + compl_col, compl_length,
5161 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5164 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 return FAIL;
5166 }
5167 else if (ctrl_x_mode == CTRL_X_FILES)
5168 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 compl_col += ++startcol;
5172 compl_length = (int)curs_col - startcol;
5173 compl_pattern = addstar(line + compl_col, compl_length,
5174 EXPAND_FILES);
5175 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 return FAIL;
5177 }
5178 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5179 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 compl_pattern = vim_strnsave(line, curs_col);
5181 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005183 set_cmd_context(&compl_xp, compl_pattern,
5184 (int)STRLEN(compl_pattern), curs_col);
5185 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5186 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005187 /* No completion possible, use an empty pattern to get a
5188 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005189 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005190 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005191 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5192 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005194 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005195 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005196#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005197 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005198 * Call user defined function 'completefunc' with "a:findstart"
5199 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005200 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005201 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005202 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005203 char_u *funcname;
5204 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005205 win_T *curwin_save;
5206 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005207
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005208 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005209 * string */
5210 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5211 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5212 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005213 {
5214 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5215 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005216 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005217 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005218
5219 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005220 args[1] = NULL;
5221 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005222 curwin_save = curwin;
5223 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005224 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005225 if (curwin_save != curwin || curbuf_save != curbuf)
5226 {
5227 EMSG(_(e_complwin));
5228 return FAIL;
5229 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005230 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005231 check_cursor();
5232 if (!equalpos(curwin->w_cursor, pos))
5233 {
5234 EMSG(_(e_compldel));
5235 return FAIL;
5236 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005237
Bram Moolenaar6110a002012-01-26 18:58:38 +01005238 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005239 * cancel the complete without an error.
5240 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005241 if (col == -2)
5242 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005243 if (col == -3)
5244 {
5245 ctrl_x_mode = 0;
5246 edit_submode = NULL;
5247 msg_clr_cmdline();
5248 return FAIL;
5249 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005250
Bram Moolenaar82139082011-09-14 16:52:09 +02005251 /*
5252 * Reset extended parameters of completion, when start new
5253 * completion.
5254 */
5255 compl_opt_refresh_always = FALSE;
5256
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005257 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005258 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005259 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005260 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005261 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005263 /* Setup variables for completion. Need to obtain "line" again,
5264 * it may have become invalid. */
5265 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005266 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005267 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5268 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005269#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005270 return FAIL;
5271 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005272 else if (ctrl_x_mode == CTRL_X_SPELL)
5273 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005274#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005275 if (spell_bad_len > 0)
5276 compl_col = curs_col - spell_bad_len;
5277 else
5278 compl_col = spell_word_start(startcol);
5279 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005280 {
5281 compl_length = 0;
5282 compl_col = curs_col;
5283 }
5284 else
5285 {
5286 spell_expand_check_cap(compl_col);
5287 compl_length = (int)curs_col - compl_col;
5288 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005289 /* Need to obtain "line" again, it may have become invalid. */
5290 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005291 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5292 if (compl_pattern == NULL)
5293#endif
5294 return FAIL;
5295 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005296 else
5297 {
5298 EMSG2(_(e_intern2), "ins_complete()");
5299 return FAIL;
5300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005302 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303 {
5304 edit_submode_pre = (char_u *)_(" Adding");
5305 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5306 {
5307 /* Insert a new line, keep indentation but ignore 'comments' */
5308#ifdef FEAT_COMMENTS
5309 char_u *old = curbuf->b_p_com;
5310
5311 curbuf->b_p_com = (char_u *)"";
5312#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 compl_startpos.lnum = curwin->w_cursor.lnum;
5314 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315 ins_eol('\r');
5316#ifdef FEAT_COMMENTS
5317 curbuf->b_p_com = old;
5318#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005319 compl_length = 0;
5320 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 }
5322 }
5323 else
5324 {
5325 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005326 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005327 }
5328
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005329 if (compl_cont_status & CONT_LOCAL)
5330 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331 else
5332 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5333
Bram Moolenaar62951b12011-09-21 18:23:05 +02005334 /* If any of the original typed text has been changed we need to fix
5335 * the redo buffer. */
5336 ins_compl_fixRedoBufForLeader(NULL);
5337
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005338 /* Always add completion for the original text. */
5339 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005340 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5341 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005342 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 vim_free(compl_pattern);
5345 compl_pattern = NULL;
5346 vim_free(compl_orig_text);
5347 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005348 return FAIL;
5349 }
5350
5351 /* showmode might reset the internal line pointers, so it must
5352 * be called before line = ml_get(), or when this address is no
5353 * longer needed. -- Acevedo.
5354 */
5355 edit_submode_extra = (char_u *)_("-- Searching...");
5356 edit_submode_highl = HLF_COUNT;
5357 showmode();
5358 edit_submode_extra = NULL;
5359 out_flush();
5360 }
5361
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 compl_shown_match = compl_curr_match;
5363 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364
5365 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005366 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005367 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005368 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005369 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005371 /* may undisplay the popup menu */
5372 ins_compl_upd_pum();
5373
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005374 if (n > 1) /* all matches have been found */
5375 compl_matches = n;
5376 compl_curr_match = compl_shown_match;
5377 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378
Bram Moolenaard68071d2006-05-02 22:08:30 +00005379 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5380 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 if (got_int && !global_busy)
5382 {
5383 (void)vgetc();
5384 got_int = FALSE;
5385 }
5386
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005387 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005388 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005390 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5391 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5393 edit_submode_highl = HLF_E;
5394 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5395 * because we couldn't expand anything at first place, but if we used
5396 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5397 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005398 if ( compl_length > 1
5399 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400 || (ctrl_x_mode != 0
5401 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5402 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005403 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 }
5405
Bram Moolenaar572cb562005-08-05 21:35:02 +00005406 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005407 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005409 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410
5411 if (edit_submode_extra == NULL)
5412 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005413 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005414 {
5415 edit_submode_extra = (char_u *)_("Back at original");
5416 edit_submode_highl = HLF_W;
5417 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 {
5420 edit_submode_extra = (char_u *)_("Word from other line");
5421 edit_submode_highl = HLF_COUNT;
5422 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005423 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 {
5425 edit_submode_extra = (char_u *)_("The only match");
5426 edit_submode_highl = HLF_COUNT;
5427 }
5428 else
5429 {
5430 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005431 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005433 int number = 0;
5434 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {
5438 /* search backwards for the first valid (!= -1) number.
5439 * This should normally succeed already at the first loop
5440 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005441 for (match = compl_curr_match->cp_prev; match != NULL
5442 && match != compl_first_match;
5443 match = match->cp_prev)
5444 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005446 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 break;
5448 }
5449 if (match != NULL)
5450 /* go up and assign all numbers which are not assigned
5451 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005452 for (match = match->cp_next;
5453 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005454 match = match->cp_next)
5455 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005456 }
5457 else /* BACKWARD */
5458 {
5459 /* search forwards (upwards) for the first valid (!= -1)
5460 * number. This should normally succeed already at the
5461 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005462 for (match = compl_curr_match->cp_next; match != NULL
5463 && match != compl_first_match;
5464 match = match->cp_next)
5465 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005466 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005467 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005468 break;
5469 }
5470 if (match != NULL)
5471 /* go down and assign all numbers which are not
5472 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005473 for (match = match->cp_prev; match
5474 && match->cp_number == -1;
5475 match = match->cp_prev)
5476 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005477 }
5478 }
5479
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005480 /* The match should always have a sequence number now, this is
5481 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005482 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005484 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5485 * Translations may need more than twice that. */
5486 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005487
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005488 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005489 vim_snprintf((char *)match_ref, sizeof(match_ref),
5490 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005491 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005493 vim_snprintf((char *)match_ref, sizeof(match_ref),
5494 _("match %d"),
5495 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 edit_submode_extra = match_ref;
5497 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005498 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 curs_columns(FALSE);
5500 }
5501 }
5502 }
5503
5504 /* Show a message about what (completion) mode we're in. */
5505 showmode();
5506 if (edit_submode_extra != NULL)
5507 {
5508 if (!p_smd)
5509 msg_attr(edit_submode_extra,
5510 edit_submode_highl < HLF_COUNT
5511 ? hl_attr(edit_submode_highl) : 0);
5512 }
5513 else
5514 msg_clr_cmdline(); /* necessary for "noshowmode" */
5515
Bram Moolenaard68071d2006-05-02 22:08:30 +00005516 /* Show the popup menu, unless we got interrupted. */
5517 if (!compl_interrupted)
5518 {
5519 /* RedrawingDisabled may be set when invoked through complete(). */
5520 n = RedrawingDisabled;
5521 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005522
5523 /* If the cursor moved we need to remove the pum first. */
5524 setcursor();
5525 if (save_w_wrow != curwin->w_wrow)
5526 ins_compl_del_pum();
5527
Bram Moolenaard68071d2006-05-02 22:08:30 +00005528 ins_compl_show_pum();
5529 setcursor();
5530 RedrawingDisabled = n;
5531 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005532 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005533 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005534
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 return OK;
5536}
5537
5538/*
5539 * Looks in the first "len" chars. of "src" for search-metachars.
5540 * If dest is not NULL the chars. are copied there quoting (with
5541 * a backslash) the metachars, and dest would be NUL terminated.
5542 * Returns the length (needed) of dest
5543 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005544 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005545quote_meta(dest, src, len)
5546 char_u *dest;
5547 char_u *src;
5548 int len;
5549{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005550 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005552 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 {
5554 switch (*src)
5555 {
5556 case '.':
5557 case '*':
5558 case '[':
5559 if (ctrl_x_mode == CTRL_X_DICTIONARY
5560 || ctrl_x_mode == CTRL_X_THESAURUS)
5561 break;
5562 case '~':
5563 if (!p_magic) /* quote these only if magic is set */
5564 break;
5565 case '\\':
5566 if (ctrl_x_mode == CTRL_X_DICTIONARY
5567 || ctrl_x_mode == CTRL_X_THESAURUS)
5568 break;
5569 case '^': /* currently it's not needed. */
5570 case '$':
5571 m++;
5572 if (dest != NULL)
5573 *dest++ = '\\';
5574 break;
5575 }
5576 if (dest != NULL)
5577 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005578# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005579 /* Copy remaining bytes of a multibyte character. */
5580 if (has_mbyte)
5581 {
5582 int i, mb_len;
5583
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005584 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 if (mb_len > 0 && len >= mb_len)
5586 for (i = 0; i < mb_len; ++i)
5587 {
5588 --len;
5589 ++src;
5590 if (dest != NULL)
5591 *dest++ = *src;
5592 }
5593 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005594# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005595 }
5596 if (dest != NULL)
5597 *dest = NUL;
5598
5599 return m;
5600}
5601#endif /* FEAT_INS_EXPAND */
5602
5603/*
5604 * Next character is interpreted literally.
5605 * A one, two or three digit decimal number is interpreted as its byte value.
5606 * If one or two digits are entered, the next character is given to vungetc().
5607 * For Unicode a character > 255 may be returned.
5608 */
5609 int
5610get_literal()
5611{
5612 int cc;
5613 int nc;
5614 int i;
5615 int hex = FALSE;
5616 int octal = FALSE;
5617#ifdef FEAT_MBYTE
5618 int unicode = 0;
5619#endif
5620
5621 if (got_int)
5622 return Ctrl_C;
5623
5624#ifdef FEAT_GUI
5625 /*
5626 * In GUI there is no point inserting the internal code for a special key.
5627 * It is more useful to insert the string "<KEY>" instead. This would
5628 * probably be useful in a text window too, but it would not be
5629 * vi-compatible (maybe there should be an option for it?) -- webb
5630 */
5631 if (gui.in_use)
5632 ++allow_keys;
5633#endif
5634#ifdef USE_ON_FLY_SCROLL
5635 dont_scroll = TRUE; /* disallow scrolling here */
5636#endif
5637 ++no_mapping; /* don't map the next key hits */
5638 cc = 0;
5639 i = 0;
5640 for (;;)
5641 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005642 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005643#ifdef FEAT_CMDL_INFO
5644 if (!(State & CMDLINE)
5645# ifdef FEAT_MBYTE
5646 && MB_BYTE2LEN_CHECK(nc) == 1
5647# endif
5648 )
5649 add_to_showcmd(nc);
5650#endif
5651 if (nc == 'x' || nc == 'X')
5652 hex = TRUE;
5653 else if (nc == 'o' || nc == 'O')
5654 octal = TRUE;
5655#ifdef FEAT_MBYTE
5656 else if (nc == 'u' || nc == 'U')
5657 unicode = nc;
5658#endif
5659 else
5660 {
5661 if (hex
5662#ifdef FEAT_MBYTE
5663 || unicode != 0
5664#endif
5665 )
5666 {
5667 if (!vim_isxdigit(nc))
5668 break;
5669 cc = cc * 16 + hex2nr(nc);
5670 }
5671 else if (octal)
5672 {
5673 if (nc < '0' || nc > '7')
5674 break;
5675 cc = cc * 8 + nc - '0';
5676 }
5677 else
5678 {
5679 if (!VIM_ISDIGIT(nc))
5680 break;
5681 cc = cc * 10 + nc - '0';
5682 }
5683
5684 ++i;
5685 }
5686
5687 if (cc > 255
5688#ifdef FEAT_MBYTE
5689 && unicode == 0
5690#endif
5691 )
5692 cc = 255; /* limit range to 0-255 */
5693 nc = 0;
5694
5695 if (hex) /* hex: up to two chars */
5696 {
5697 if (i >= 2)
5698 break;
5699 }
5700#ifdef FEAT_MBYTE
5701 else if (unicode) /* Unicode: up to four or eight chars */
5702 {
5703 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5704 break;
5705 }
5706#endif
5707 else if (i >= 3) /* decimal or octal: up to three chars */
5708 break;
5709 }
5710 if (i == 0) /* no number entered */
5711 {
5712 if (nc == K_ZERO) /* NUL is stored as NL */
5713 {
5714 cc = '\n';
5715 nc = 0;
5716 }
5717 else
5718 {
5719 cc = nc;
5720 nc = 0;
5721 }
5722 }
5723
5724 if (cc == 0) /* NUL is stored as NL */
5725 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005726#ifdef FEAT_MBYTE
5727 if (enc_dbcs && (cc & 0xff) == 0)
5728 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5729 second byte will cause trouble! */
5730#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005731
5732 --no_mapping;
5733#ifdef FEAT_GUI
5734 if (gui.in_use)
5735 --allow_keys;
5736#endif
5737 if (nc)
5738 vungetc(nc);
5739 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5740 return cc;
5741}
5742
5743/*
5744 * Insert character, taking care of special keys and mod_mask
5745 */
5746 static void
5747insert_special(c, allow_modmask, ctrlv)
5748 int c;
5749 int allow_modmask;
5750 int ctrlv; /* c was typed after CTRL-V */
5751{
5752 char_u *p;
5753 int len;
5754
5755 /*
5756 * Special function key, translate into "<Key>". Up to the last '>' is
5757 * inserted with ins_str(), so as not to replace characters in replace
5758 * mode.
5759 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5760 * unless 'allow_modmask' is TRUE.
5761 */
5762#ifdef MACOS
5763 /* Command-key never produces a normal key */
5764 if (mod_mask & MOD_MASK_CMD)
5765 allow_modmask = TRUE;
5766#endif
5767 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5768 {
5769 p = get_special_key_name(c, mod_mask);
5770 len = (int)STRLEN(p);
5771 c = p[len - 1];
5772 if (len > 2)
5773 {
5774 if (stop_arrow() == FAIL)
5775 return;
5776 p[len - 1] = NUL;
5777 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005778 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 ctrlv = FALSE;
5780 }
5781 }
5782 if (stop_arrow() == OK)
5783 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5784}
5785
5786/*
5787 * Special characters in this context are those that need processing other
5788 * than the simple insertion that can be performed here. This includes ESC
5789 * which terminates the insert, and CR/NL which need special processing to
5790 * open up a new line. This routine tries to optimize insertions performed by
5791 * the "redo", "undo" or "put" commands, so it needs to know when it should
5792 * stop and defer processing to the "normal" mechanism.
5793 * '0' and '^' are special, because they can be followed by CTRL-D.
5794 */
5795#ifdef EBCDIC
5796# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5797#else
5798# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5799#endif
5800
5801#ifdef FEAT_MBYTE
5802# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5803#else
5804# define WHITECHAR(cc) vim_iswhite(cc)
5805#endif
5806
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005807/*
5808 * "flags": INSCHAR_FORMAT - force formatting
5809 * INSCHAR_CTRLV - char typed just after CTRL-V
5810 * INSCHAR_NO_FEX - don't use 'formatexpr'
5811 *
5812 * NOTE: passes the flags value straight through to internal_format() which,
5813 * beside INSCHAR_FORMAT (above), is also looking for these:
5814 * INSCHAR_DO_COM - format comments
5815 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5816 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 void
5818insertchar(c, flags, second_indent)
5819 int c; /* character to insert or NUL */
5820 int flags; /* INSCHAR_FORMAT, etc. */
5821 int second_indent; /* indent for second line if >= 0 */
5822{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 int textwidth;
5824#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828
5829 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5830 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831
5832 /*
5833 * Try to break the line in two or more pieces when:
5834 * - Always do this if we have been called to do formatting only.
5835 * - Always do this when 'formatoptions' has the 'a' flag and the line
5836 * ends in white space.
5837 * - Otherwise:
5838 * - Don't do this if inserting a blank
5839 * - Don't do this if an existing character is being replaced, unless
5840 * we're in VREPLACE mode.
5841 * - Do this if the cursor is not on the line where insert started
5842 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5843 * before the insert.
5844 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5845 * before 'textwidth'
5846 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005847 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 && ((flags & INSCHAR_FORMAT)
5849 || (!vim_iswhite(c)
5850 && !((State & REPLACE_FLAG)
5851#ifdef FEAT_VREPLACE
5852 && !(State & VREPLACE_FLAG)
5853#endif
5854 && *ml_get_cursor() != NUL)
5855 && (curwin->w_cursor.lnum != Insstart.lnum
5856 || ((!has_format_option(FO_INS_LONG)
5857 || Insstart_textlen <= (colnr_T)textwidth)
5858 && (!fo_ins_blank
5859 || Insstart_blank_vcol <= (colnr_T)textwidth
5860 ))))))
5861 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005862 /* Format with 'formatexpr' when it's set. Use internal formatting
5863 * when 'formatexpr' isn't set or it returns non-zero. */
5864#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005865 int do_internal = TRUE;
5866
Bram Moolenaar81a82092008-03-12 16:27:00 +00005867 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005868 {
5869 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5870 /* It may be required to save for undo again, e.g. when setline()
5871 * was called. */
5872 ins_need_undo = TRUE;
5873 }
5874 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005875#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005876 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005878
Bram Moolenaar071d4272004-06-13 20:20:40 +00005879 if (c == NUL) /* only formatting was wanted */
5880 return;
5881
5882#ifdef FEAT_COMMENTS
5883 /* Check whether this character should end a comment. */
5884 if (did_ai && (int)c == end_comment_pending)
5885 {
5886 char_u *line;
5887 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5888 int middle_len, end_len;
5889 int i;
5890
5891 /*
5892 * Need to remove existing (middle) comment leader and insert end
5893 * comment leader. First, check what comment leader we can find.
5894 */
Bram Moolenaar81340392012-06-06 16:12:59 +02005895 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005896 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5897 {
5898 /* Skip middle-comment string */
5899 while (*p && p[-1] != ':') /* find end of middle flags */
5900 ++p;
5901 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5902 /* Don't count trailing white space for middle_len */
5903 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5904 --middle_len;
5905
5906 /* Find the end-comment string */
5907 while (*p && p[-1] != ':') /* find end of end flags */
5908 ++p;
5909 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5910
5911 /* Skip white space before the cursor */
5912 i = curwin->w_cursor.col;
5913 while (--i >= 0 && vim_iswhite(line[i]))
5914 ;
5915 i++;
5916
5917 /* Skip to before the middle leader */
5918 i -= middle_len;
5919
5920 /* Check some expected things before we go on */
5921 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5922 {
5923 /* Backspace over all the stuff we want to replace */
5924 backspace_until_column(i);
5925
5926 /*
5927 * Insert the end-comment string, except for the last
5928 * character, which will get inserted as normal later.
5929 */
5930 ins_bytes_len(lead_end, end_len - 1);
5931 }
5932 }
5933 }
5934 end_comment_pending = NUL;
5935#endif
5936
5937 did_ai = FALSE;
5938#ifdef FEAT_SMARTINDENT
5939 did_si = FALSE;
5940 can_si = FALSE;
5941 can_si_back = FALSE;
5942#endif
5943
5944 /*
5945 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5946 * This speeds up normal text input considerably.
5947 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5948 * need to re-indent at a ':', or any other character (but not what
5949 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005950 * Don't do this when there an InsertCharPre autocommand is defined,
5951 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005952 */
5953#ifdef USE_ON_FLY_SCROLL
5954 dont_scroll = FALSE; /* allow scrolling here */
5955#endif
5956
5957 if ( !ISSPECIAL(c)
5958#ifdef FEAT_MBYTE
5959 && (!has_mbyte || (*mb_char2len)(c) == 1)
5960#endif
5961 && vpeekc() != NUL
5962 && !(State & REPLACE_FLAG)
5963#ifdef FEAT_CINDENT
5964 && !cindent_on()
5965#endif
5966#ifdef FEAT_RIGHTLEFT
5967 && !p_ri
5968#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005969#ifdef FEAT_AUTOCMD
5970 && !has_insertcharpre()
5971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005972 )
5973 {
5974#define INPUT_BUFLEN 100
5975 char_u buf[INPUT_BUFLEN + 1];
5976 int i;
5977 colnr_T virtcol = 0;
5978
5979 buf[0] = c;
5980 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005981 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 virtcol = get_nolist_virtcol();
5983 /*
5984 * Stop the string when:
5985 * - no more chars available
5986 * - finding a special character (command key)
5987 * - buffer is full
5988 * - running into the 'textwidth' boundary
5989 * - need to check for abbreviation: A non-word char after a word-char
5990 */
5991 while ( (c = vpeekc()) != NUL
5992 && !ISSPECIAL(c)
5993#ifdef FEAT_MBYTE
5994 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5995#endif
5996 && i < INPUT_BUFLEN
5997 && (textwidth == 0
5998 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5999 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6000 {
6001#ifdef FEAT_RIGHTLEFT
6002 c = vgetc();
6003 if (p_hkmap && KeyTyped)
6004 c = hkmap(c); /* Hebrew mode mapping */
6005# ifdef FEAT_FKMAP
6006 if (p_fkmap && KeyTyped)
6007 c = fkmap(c); /* Farsi mode mapping */
6008# endif
6009 buf[i++] = c;
6010#else
6011 buf[i++] = vgetc();
6012#endif
6013 }
6014
6015#ifdef FEAT_DIGRAPHS
6016 do_digraph(-1); /* clear digraphs */
6017 do_digraph(buf[i-1]); /* may be the start of a digraph */
6018#endif
6019 buf[i] = NUL;
6020 ins_str(buf);
6021 if (flags & INSCHAR_CTRLV)
6022 {
6023 redo_literal(*buf);
6024 i = 1;
6025 }
6026 else
6027 i = 0;
6028 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006029 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006030 }
6031 else
6032 {
6033#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006034 int cc;
6035
Bram Moolenaar071d4272004-06-13 20:20:40 +00006036 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6037 {
6038 char_u buf[MB_MAXBYTES + 1];
6039
6040 (*mb_char2bytes)(c, buf);
6041 buf[cc] = NUL;
6042 ins_char_bytes(buf, cc);
6043 AppendCharToRedobuff(c);
6044 }
6045 else
6046#endif
6047 {
6048 ins_char(c);
6049 if (flags & INSCHAR_CTRLV)
6050 redo_literal(c);
6051 else
6052 AppendCharToRedobuff(c);
6053 }
6054 }
6055}
6056
6057/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006058 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006059 *
6060 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6061 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006062 */
6063 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006064internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006065 int textwidth;
6066 int second_indent;
6067 int flags;
6068 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006069 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006070{
6071 int cc;
6072 int save_char = NUL;
6073 int haveto_redraw = FALSE;
6074 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6075#ifdef FEAT_MBYTE
6076 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6077#endif
6078 int fo_white_par = has_format_option(FO_WHITE_PAR);
6079 int first_line = TRUE;
6080#ifdef FEAT_COMMENTS
6081 colnr_T leader_len;
6082 int no_leader = FALSE;
6083 int do_comments = (flags & INSCHAR_DO_COM);
6084#endif
6085
6086 /*
6087 * When 'ai' is off we don't want a space under the cursor to be
6088 * deleted. Replace it with an 'x' temporarily.
6089 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006090 if (!curbuf->b_p_ai
6091#ifdef FEAT_VREPLACE
6092 && !(State & VREPLACE_FLAG)
6093#endif
6094 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006095 {
6096 cc = gchar_cursor();
6097 if (vim_iswhite(cc))
6098 {
6099 save_char = cc;
6100 pchar_cursor('x');
6101 }
6102 }
6103
6104 /*
6105 * Repeat breaking lines, until the current line is not too long.
6106 */
6107 while (!got_int)
6108 {
6109 int startcol; /* Cursor column at entry */
6110 int wantcol; /* column at textwidth border */
6111 int foundcol; /* column for start of spaces */
6112 int end_foundcol = 0; /* column for start of word */
6113 colnr_T len;
6114 colnr_T virtcol;
6115#ifdef FEAT_VREPLACE
6116 int orig_col = 0;
6117 char_u *saved_text = NULL;
6118#endif
6119 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006120 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006121
Bram Moolenaar97b98102009-11-17 16:41:01 +00006122 virtcol = get_nolist_virtcol()
6123 + char2cells(c != NUL ? c : gchar_cursor());
6124 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006125 break;
6126
6127#ifdef FEAT_COMMENTS
6128 if (no_leader)
6129 do_comments = FALSE;
6130 else if (!(flags & INSCHAR_FORMAT)
6131 && has_format_option(FO_WRAP_COMS))
6132 do_comments = TRUE;
6133
6134 /* Don't break until after the comment leader */
6135 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006136 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006137 else
6138 leader_len = 0;
6139
6140 /* If the line doesn't start with a comment leader, then don't
6141 * start one in a following broken line. Avoids that a %word
6142 * moved to the start of the next line causes all following lines
6143 * to start with %. */
6144 if (leader_len == 0)
6145 no_leader = TRUE;
6146#endif
6147 if (!(flags & INSCHAR_FORMAT)
6148#ifdef FEAT_COMMENTS
6149 && leader_len == 0
6150#endif
6151 && !has_format_option(FO_WRAP))
6152
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006153 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006154 if ((startcol = curwin->w_cursor.col) == 0)
6155 break;
6156
6157 /* find column of textwidth border */
6158 coladvance((colnr_T)textwidth);
6159 wantcol = curwin->w_cursor.col;
6160
Bram Moolenaar97b98102009-11-17 16:41:01 +00006161 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006162 foundcol = 0;
6163
6164 /*
6165 * Find position to break at.
6166 * Stop at first entered white when 'formatoptions' has 'v'
6167 */
6168 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006169 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006170 || curwin->w_cursor.lnum != Insstart.lnum
6171 || curwin->w_cursor.col >= Insstart.col)
6172 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006173 if (curwin->w_cursor.col == startcol && c != NUL)
6174 cc = c;
6175 else
6176 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006177 if (WHITECHAR(cc))
6178 {
6179 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006180 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006181
6182 /* find start of sequence of blanks */
6183 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6184 {
6185 dec_cursor();
6186 cc = gchar_cursor();
6187 }
6188 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6189 break; /* only spaces in front of text */
6190#ifdef FEAT_COMMENTS
6191 /* Don't break until after the comment leader */
6192 if (curwin->w_cursor.col < leader_len)
6193 break;
6194#endif
6195 if (has_format_option(FO_ONE_LETTER))
6196 {
6197 /* do not break after one-letter words */
6198 if (curwin->w_cursor.col == 0)
6199 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006200#ifdef FEAT_COMMENTS
6201 /* do not break "#a b" when 'tw' is 2 */
6202 if (curwin->w_cursor.col <= leader_len)
6203 break;
6204#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006205 col = curwin->w_cursor.col;
6206 dec_cursor();
6207 cc = gchar_cursor();
6208
6209 if (WHITECHAR(cc))
6210 continue; /* one-letter, continue */
6211 curwin->w_cursor.col = col;
6212 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006213
6214 inc_cursor();
6215
6216 end_foundcol = end_col + 1;
6217 foundcol = curwin->w_cursor.col;
6218 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006219 break;
6220 }
6221#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006222 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006223 {
6224 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006225 if (curwin->w_cursor.col != startcol)
6226 {
6227#ifdef FEAT_COMMENTS
6228 /* Don't break until after the comment leader */
6229 if (curwin->w_cursor.col < leader_len)
6230 break;
6231#endif
6232 col = curwin->w_cursor.col;
6233 inc_cursor();
6234 /* Don't change end_foundcol if already set. */
6235 if (foundcol != curwin->w_cursor.col)
6236 {
6237 foundcol = curwin->w_cursor.col;
6238 end_foundcol = foundcol;
6239 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6240 break;
6241 }
6242 curwin->w_cursor.col = col;
6243 }
6244
6245 if (curwin->w_cursor.col == 0)
6246 break;
6247
6248 col = curwin->w_cursor.col;
6249
6250 dec_cursor();
6251 cc = gchar_cursor();
6252
6253 if (WHITECHAR(cc))
6254 continue; /* break with space */
6255#ifdef FEAT_COMMENTS
6256 /* Don't break until after the comment leader */
6257 if (curwin->w_cursor.col < leader_len)
6258 break;
6259#endif
6260
6261 curwin->w_cursor.col = col;
6262
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006263 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006264 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006265 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6266 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006267 }
6268#endif
6269 if (curwin->w_cursor.col == 0)
6270 break;
6271 dec_cursor();
6272 }
6273
6274 if (foundcol == 0) /* no spaces, cannot break line */
6275 {
6276 curwin->w_cursor.col = startcol;
6277 break;
6278 }
6279
6280 /* Going to break the line, remove any "$" now. */
6281 undisplay_dollar();
6282
6283 /*
6284 * Offset between cursor position and line break is used by replace
6285 * stack functions. VREPLACE does not use this, and backspaces
6286 * over the text instead.
6287 */
6288#ifdef FEAT_VREPLACE
6289 if (State & VREPLACE_FLAG)
6290 orig_col = startcol; /* Will start backspacing from here */
6291 else
6292#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006293 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006294
6295 /*
6296 * adjust startcol for spaces that will be deleted and
6297 * characters that will remain on top line
6298 */
6299 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006300 while ((cc = gchar_cursor(), WHITECHAR(cc))
6301 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006302 inc_cursor();
6303 startcol -= curwin->w_cursor.col;
6304 if (startcol < 0)
6305 startcol = 0;
6306
6307#ifdef FEAT_VREPLACE
6308 if (State & VREPLACE_FLAG)
6309 {
6310 /*
6311 * In VREPLACE mode, we will backspace over the text to be
6312 * wrapped, so save a copy now to put on the next line.
6313 */
6314 saved_text = vim_strsave(ml_get_cursor());
6315 curwin->w_cursor.col = orig_col;
6316 if (saved_text == NULL)
6317 break; /* Can't do it, out of memory */
6318 saved_text[startcol] = NUL;
6319
6320 /* Backspace over characters that will move to the next line */
6321 if (!fo_white_par)
6322 backspace_until_column(foundcol);
6323 }
6324 else
6325#endif
6326 {
6327 /* put cursor after pos. to break line */
6328 if (!fo_white_par)
6329 curwin->w_cursor.col = foundcol;
6330 }
6331
6332 /*
6333 * Split the line just before the margin.
6334 * Only insert/delete lines, but don't really redraw the window.
6335 */
6336 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6337 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6338#ifdef FEAT_COMMENTS
6339 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006340 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006341#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006342 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6343 if (!(flags & INSCHAR_COM_LIST))
6344 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006345
6346 replace_offset = 0;
6347 if (first_line)
6348 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006349 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006350 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006351 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006352 * This section is for auto-wrap of numeric lists. When not
6353 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6354 * flag will be set and open_line() will handle it (as seen
6355 * above). The code here (and in get_number_indent()) will
6356 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006357 */
6358 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006359 second_indent =
6360 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006361 if (second_indent >= 0)
6362 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006363#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006364 if (State & VREPLACE_FLAG)
6365 change_indent(INDENT_SET, second_indent,
6366 FALSE, NUL, TRUE);
6367 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006368#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006369#ifdef FEAT_COMMENTS
6370 if (leader_len > 0 && second_indent - leader_len > 0)
6371 {
6372 int i;
6373 int padding = second_indent - leader_len;
6374
6375 /* We started at the first_line of a numbered list
6376 * that has a comment. the open_line() function has
6377 * inserted the proper comment leader and positioned
6378 * the cursor at the end of the split line. Now we
6379 * add the additional whitespace needed after the
6380 * comment leader for the numbered list. */
6381 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006382 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006383 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006384 }
6385 else
6386 {
6387#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006388 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006389#ifdef FEAT_COMMENTS
6390 }
6391#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006392 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006393 }
6394 first_line = FALSE;
6395 }
6396
6397#ifdef FEAT_VREPLACE
6398 if (State & VREPLACE_FLAG)
6399 {
6400 /*
6401 * In VREPLACE mode we have backspaced over the text to be
6402 * moved, now we re-insert it into the new line.
6403 */
6404 ins_bytes(saved_text);
6405 vim_free(saved_text);
6406 }
6407 else
6408#endif
6409 {
6410 /*
6411 * Check if cursor is not past the NUL off the line, cindent
6412 * may have added or removed indent.
6413 */
6414 curwin->w_cursor.col += startcol;
6415 len = (colnr_T)STRLEN(ml_get_curline());
6416 if (curwin->w_cursor.col > len)
6417 curwin->w_cursor.col = len;
6418 }
6419
6420 haveto_redraw = TRUE;
6421#ifdef FEAT_CINDENT
6422 can_cindent = TRUE;
6423#endif
6424 /* moved the cursor, don't autoindent or cindent now */
6425 did_ai = FALSE;
6426#ifdef FEAT_SMARTINDENT
6427 did_si = FALSE;
6428 can_si = FALSE;
6429 can_si_back = FALSE;
6430#endif
6431 line_breakcheck();
6432 }
6433
6434 if (save_char != NUL) /* put back space after cursor */
6435 pchar_cursor(save_char);
6436
6437 if (!format_only && haveto_redraw)
6438 {
6439 update_topline();
6440 redraw_curbuf_later(VALID);
6441 }
6442}
6443
6444/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 * Called after inserting or deleting text: When 'formatoptions' includes the
6446 * 'a' flag format from the current line until the end of the paragraph.
6447 * Keep the cursor at the same position relative to the text.
6448 * The caller must have saved the cursor line for undo, following ones will be
6449 * saved here.
6450 */
6451 void
6452auto_format(trailblank, prev_line)
6453 int trailblank; /* when TRUE also format with trailing blank */
6454 int prev_line; /* may start in previous line */
6455{
6456 pos_T pos;
6457 colnr_T len;
6458 char_u *old;
6459 char_u *new, *pnew;
6460 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006461 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462
6463 if (!has_format_option(FO_AUTO))
6464 return;
6465
6466 pos = curwin->w_cursor;
6467 old = ml_get_curline();
6468
6469 /* may remove added space */
6470 check_auto_format(FALSE);
6471
6472 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6473 * user might insert normal text next. Also skip formatting when "1" is
6474 * in 'formatoptions' and there is a single character before the cursor.
6475 * Otherwise the line would be broken and when typing another non-white
6476 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006477 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 if (*old != NUL && !trailblank && wasatend)
6479 {
6480 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006481 cc = gchar_cursor();
6482 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6483 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006485 cc = gchar_cursor();
6486 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006487 {
6488 curwin->w_cursor = pos;
6489 return;
6490 }
6491 curwin->w_cursor = pos;
6492 }
6493
6494#ifdef FEAT_COMMENTS
6495 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6496 * comments. */
6497 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006498 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 return;
6500#endif
6501
6502 /*
6503 * May start formatting in a previous line, so that after "x" a word is
6504 * moved to the previous line if it fits there now. Only when this is not
6505 * the start of a paragraph.
6506 */
6507 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6508 {
6509 --curwin->w_cursor.lnum;
6510 if (u_save_cursor() == FAIL)
6511 return;
6512 }
6513
6514 /*
6515 * Do the formatting and restore the cursor position. "saved_cursor" will
6516 * be adjusted for the text formatting.
6517 */
6518 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006519 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 curwin->w_cursor = saved_cursor;
6521 saved_cursor.lnum = 0;
6522
6523 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6524 {
6525 /* "cannot happen" */
6526 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6527 coladvance((colnr_T)MAXCOL);
6528 }
6529 else
6530 check_cursor_col();
6531
6532 /* Insert mode: If the cursor is now after the end of the line while it
6533 * previously wasn't, the line was broken. Because of the rule above we
6534 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6535 * formatted. */
6536 if (!wasatend && has_format_option(FO_WHITE_PAR))
6537 {
6538 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006539 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 if (curwin->w_cursor.col == len)
6541 {
6542 pnew = vim_strnsave(new, len + 2);
6543 pnew[len] = ' ';
6544 pnew[len + 1] = NUL;
6545 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6546 /* remove the space later */
6547 did_add_space = TRUE;
6548 }
6549 else
6550 /* may remove added space */
6551 check_auto_format(FALSE);
6552 }
6553
6554 check_cursor();
6555}
6556
6557/*
6558 * When an extra space was added to continue a paragraph for auto-formatting,
6559 * delete it now. The space must be under the cursor, just after the insert
6560 * position.
6561 */
6562 static void
6563check_auto_format(end_insert)
6564 int end_insert; /* TRUE when ending Insert mode */
6565{
6566 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006567 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568
6569 if (did_add_space)
6570 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006571 cc = gchar_cursor();
6572 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006573 /* Somehow the space was removed already. */
6574 did_add_space = FALSE;
6575 else
6576 {
6577 if (!end_insert)
6578 {
6579 inc_cursor();
6580 c = gchar_cursor();
6581 dec_cursor();
6582 }
6583 if (c != NUL)
6584 {
6585 /* The space is no longer at the end of the line, delete it. */
6586 del_char(FALSE);
6587 did_add_space = FALSE;
6588 }
6589 }
6590 }
6591}
6592
6593/*
6594 * Find out textwidth to be used for formatting:
6595 * if 'textwidth' option is set, use it
6596 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6597 * if invalid value, use 0.
6598 * Set default to window width (maximum 79) for "gq" operator.
6599 */
6600 int
6601comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006602 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603{
6604 int textwidth;
6605
6606 textwidth = curbuf->b_p_tw;
6607 if (textwidth == 0 && curbuf->b_p_wm)
6608 {
6609 /* The width is the window width minus 'wrapmargin' minus all the
6610 * things that add to the margin. */
6611 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6612#ifdef FEAT_CMDWIN
6613 if (cmdwin_type != 0)
6614 textwidth -= 1;
6615#endif
6616#ifdef FEAT_FOLDING
6617 textwidth -= curwin->w_p_fdc;
6618#endif
6619#ifdef FEAT_SIGNS
6620 if (curwin->w_buffer->b_signlist != NULL
6621# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006622 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623# endif
6624 )
6625 textwidth -= 1;
6626#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006627 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006628 textwidth -= 8;
6629 }
6630 if (textwidth < 0)
6631 textwidth = 0;
6632 if (ff && textwidth == 0)
6633 {
6634 textwidth = W_WIDTH(curwin) - 1;
6635 if (textwidth > 79)
6636 textwidth = 79;
6637 }
6638 return textwidth;
6639}
6640
6641/*
6642 * Put a character in the redo buffer, for when just after a CTRL-V.
6643 */
6644 static void
6645redo_literal(c)
6646 int c;
6647{
6648 char_u buf[10];
6649
6650 /* Only digits need special treatment. Translate them into a string of
6651 * three digits. */
6652 if (VIM_ISDIGIT(c))
6653 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006654 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006655 AppendToRedobuff(buf);
6656 }
6657 else
6658 AppendCharToRedobuff(c);
6659}
6660
6661/*
6662 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006663 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006664 */
6665 static void
6666start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006667 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006668{
6669 if (!arrow_used) /* something has been inserted */
6670 {
6671 AppendToRedobuff(ESC_STR);
6672 stop_insert(end_insert_pos, FALSE);
6673 arrow_used = TRUE; /* this means we stopped the current insert */
6674 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006675#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006676 check_spell_redraw();
6677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678}
6679
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006680#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006681/*
6682 * If we skipped highlighting word at cursor, do it now.
6683 * It may be skipped again, thus reset spell_redraw_lnum first.
6684 */
6685 static void
6686check_spell_redraw()
6687{
6688 if (spell_redraw_lnum != 0)
6689 {
6690 linenr_T lnum = spell_redraw_lnum;
6691
6692 spell_redraw_lnum = 0;
6693 redrawWinline(lnum, FALSE);
6694 }
6695}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006696
6697/*
6698 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6699 * spelled word, if there is one.
6700 */
6701 static void
6702spell_back_to_badword()
6703{
6704 pos_T tpos = curwin->w_cursor;
6705
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006706 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006707 if (curwin->w_cursor.col != tpos.col)
6708 start_arrow(&tpos);
6709}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006710#endif
6711
Bram Moolenaar071d4272004-06-13 20:20:40 +00006712/*
6713 * stop_arrow() is called before a change is made in insert mode.
6714 * If an arrow key has been used, start a new insertion.
6715 * Returns FAIL if undo is impossible, shouldn't insert then.
6716 */
6717 int
6718stop_arrow()
6719{
6720 if (arrow_used)
6721 {
6722 if (u_save_cursor() == OK)
6723 {
6724 arrow_used = FALSE;
6725 ins_need_undo = FALSE;
6726 }
6727 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006728 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 ai_col = 0;
6730#ifdef FEAT_VREPLACE
6731 if (State & VREPLACE_FLAG)
6732 {
6733 orig_line_count = curbuf->b_ml.ml_line_count;
6734 vr_lines_changed = 1;
6735 }
6736#endif
6737 ResetRedobuff();
6738 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006739 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006740 }
6741 else if (ins_need_undo)
6742 {
6743 if (u_save_cursor() == OK)
6744 ins_need_undo = FALSE;
6745 }
6746
6747#ifdef FEAT_FOLDING
6748 /* Always open fold at the cursor line when inserting something. */
6749 foldOpenCursor();
6750#endif
6751
6752 return (arrow_used || ins_need_undo ? FAIL : OK);
6753}
6754
6755/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006756 * Do a few things to stop inserting.
6757 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6758 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759 */
6760 static void
6761stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006762 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006763 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006764{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006765 int cc;
6766 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767
6768 stop_redo_ins();
6769 replace_flush(); /* abandon replace stack */
6770
6771 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006772 * Save the inserted text for later redo with ^@ and CTRL-A.
6773 * Don't do it when "restart_edit" was set and nothing was inserted,
6774 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006776 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006777 if (did_restart_edit == 0 || (ptr != NULL
6778 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006779 {
6780 vim_free(last_insert);
6781 last_insert = ptr;
6782 last_insert_skip = new_insert_skip;
6783 }
6784 else
6785 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006787 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006788 {
6789 /* Auto-format now. It may seem strange to do this when stopping an
6790 * insertion (or moving the cursor), but it's required when appending
6791 * a line and having it end in a space. But only do it when something
6792 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006793 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006794 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006795 pos_T tpos = curwin->w_cursor;
6796
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 /* When the cursor is at the end of the line after a space the
6798 * formatting will move it to the following word. Avoid that by
6799 * moving the cursor onto the space. */
6800 cc = 'x';
6801 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6802 {
6803 dec_cursor();
6804 cc = gchar_cursor();
6805 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006806 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 }
6808
6809 auto_format(TRUE, FALSE);
6810
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006811 if (vim_iswhite(cc))
6812 {
6813 if (gchar_cursor() != NUL)
6814 inc_cursor();
6815#ifdef FEAT_VIRTUALEDIT
6816 /* If the cursor is still at the same character, also keep
6817 * the "coladd". */
6818 if (gchar_cursor() == NUL
6819 && curwin->w_cursor.lnum == tpos.lnum
6820 && curwin->w_cursor.col == tpos.col)
6821 curwin->w_cursor.coladd = tpos.coladd;
6822#endif
6823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006824 }
6825
6826 /* If a space was inserted for auto-formatting, remove it now. */
6827 check_auto_format(TRUE);
6828
6829 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006830 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006831 * Do this when ESC was used or moving the cursor up/down.
6832 * Check for the old position still being valid, just in case the text
6833 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006834 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006835 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6836 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006837 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006838 pos_T tpos = curwin->w_cursor;
6839
6840 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006841 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006842 for (;;)
6843 {
6844 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6845 --curwin->w_cursor.col;
6846 cc = gchar_cursor();
6847 if (!vim_iswhite(cc))
6848 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006849 if (del_char(TRUE) == FAIL)
6850 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006851 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006852 if (curwin->w_cursor.lnum != tpos.lnum)
6853 curwin->w_cursor = tpos;
6854 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6856
6857#ifdef FEAT_VISUAL
6858 /* <C-S-Right> may have started Visual mode, adjust the position for
6859 * deleted characters. */
6860 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6861 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006862 int len = (int)STRLEN(ml_get_curline());
6863
6864 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006866 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006867# ifdef FEAT_VIRTUALEDIT
6868 VIsual.coladd = 0;
6869# endif
6870 }
6871 }
6872#endif
6873 }
6874 }
6875 did_ai = FALSE;
6876#ifdef FEAT_SMARTINDENT
6877 did_si = FALSE;
6878 can_si = FALSE;
6879 can_si_back = FALSE;
6880#endif
6881
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006882 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6883 * now in a different buffer. */
6884 if (end_insert_pos != NULL)
6885 {
6886 curbuf->b_op_start = Insstart;
6887 curbuf->b_op_end = *end_insert_pos;
6888 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006889}
6890
6891/*
6892 * Set the last inserted text to a single character.
6893 * Used for the replace command.
6894 */
6895 void
6896set_last_insert(c)
6897 int c;
6898{
6899 char_u *s;
6900
6901 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006903 if (last_insert != NULL)
6904 {
6905 s = last_insert;
6906 /* Use the CTRL-V only when entering a special char */
6907 if (c < ' ' || c == DEL)
6908 *s++ = Ctrl_V;
6909 s = add_char2buf(c, s);
6910 *s++ = ESC;
6911 *s++ = NUL;
6912 last_insert_skip = 0;
6913 }
6914}
6915
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006916#if defined(EXITFREE) || defined(PROTO)
6917 void
6918free_last_insert()
6919{
6920 vim_free(last_insert);
6921 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006922# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006923 vim_free(compl_orig_text);
6924 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006925# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006926}
6927#endif
6928
Bram Moolenaar071d4272004-06-13 20:20:40 +00006929/*
6930 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6931 * and CSI. Handle multi-byte characters.
6932 * Returns a pointer to after the added bytes.
6933 */
6934 char_u *
6935add_char2buf(c, s)
6936 int c;
6937 char_u *s;
6938{
6939#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006940 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006941 int i;
6942 int len;
6943
6944 len = (*mb_char2bytes)(c, temp);
6945 for (i = 0; i < len; ++i)
6946 {
6947 c = temp[i];
6948#endif
6949 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6950 if (c == K_SPECIAL)
6951 {
6952 *s++ = K_SPECIAL;
6953 *s++ = KS_SPECIAL;
6954 *s++ = KE_FILLER;
6955 }
6956#ifdef FEAT_GUI
6957 else if (c == CSI)
6958 {
6959 *s++ = CSI;
6960 *s++ = KS_EXTRA;
6961 *s++ = (int)KE_CSI;
6962 }
6963#endif
6964 else
6965 *s++ = c;
6966#ifdef FEAT_MBYTE
6967 }
6968#endif
6969 return s;
6970}
6971
6972/*
6973 * move cursor to start of line
6974 * if flags & BL_WHITE move to first non-white
6975 * if flags & BL_SOL move to first non-white if startofline is set,
6976 * otherwise keep "curswant" column
6977 * if flags & BL_FIX don't leave the cursor on a NUL.
6978 */
6979 void
6980beginline(flags)
6981 int flags;
6982{
6983 if ((flags & BL_SOL) && !p_sol)
6984 coladvance(curwin->w_curswant);
6985 else
6986 {
6987 curwin->w_cursor.col = 0;
6988#ifdef FEAT_VIRTUALEDIT
6989 curwin->w_cursor.coladd = 0;
6990#endif
6991
6992 if (flags & (BL_WHITE | BL_SOL))
6993 {
6994 char_u *ptr;
6995
6996 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6997 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6998 ++curwin->w_cursor.col;
6999 }
7000 curwin->w_set_curswant = TRUE;
7001 }
7002}
7003
7004/*
7005 * oneright oneleft cursor_down cursor_up
7006 *
7007 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007008 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007009 * Return OK when successful, FAIL when we hit a line of file boundary.
7010 */
7011
7012 int
7013oneright()
7014{
7015 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007016 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007017
7018#ifdef FEAT_VIRTUALEDIT
7019 if (virtual_active())
7020 {
7021 pos_T prevpos = curwin->w_cursor;
7022
7023 /* Adjust for multi-wide char (excluding TAB) */
7024 ptr = ml_get_cursor();
7025 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007026# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007028# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007030# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 ))
7032 ? ptr2cells(ptr) : 1));
7033 curwin->w_set_curswant = TRUE;
7034 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7035 return (prevpos.col != curwin->w_cursor.col
7036 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7037 }
7038#endif
7039
7040 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007041 if (*ptr == NUL)
7042 return FAIL; /* already at the very end */
7043
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007045 if (has_mbyte)
7046 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 else
7048#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007049 l = 1;
7050
7051 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7052 * contains "onemore". */
7053 if (ptr[l] == NUL
7054#ifdef FEAT_VIRTUALEDIT
7055 && (ve_flags & VE_ONEMORE) == 0
7056#endif
7057 )
7058 return FAIL;
7059 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007060
7061 curwin->w_set_curswant = TRUE;
7062 return OK;
7063}
7064
7065 int
7066oneleft()
7067{
7068#ifdef FEAT_VIRTUALEDIT
7069 if (virtual_active())
7070 {
7071 int width;
7072 int v = getviscol();
7073
7074 if (v == 0)
7075 return FAIL;
7076
7077# ifdef FEAT_LINEBREAK
7078 /* We might get stuck on 'showbreak', skip over it. */
7079 width = 1;
7080 for (;;)
7081 {
7082 coladvance(v - width);
7083 /* getviscol() is slow, skip it when 'showbreak' is empty and
7084 * there are no multi-byte characters */
7085 if ((*p_sbr == NUL
7086# ifdef FEAT_MBYTE
7087 && !has_mbyte
7088# endif
7089 ) || getviscol() < v)
7090 break;
7091 ++width;
7092 }
7093# else
7094 coladvance(v - 1);
7095# endif
7096
7097 if (curwin->w_cursor.coladd == 1)
7098 {
7099 char_u *ptr;
7100
7101 /* Adjust for multi-wide char (not a TAB) */
7102 ptr = ml_get_cursor();
7103 if (*ptr != TAB && vim_isprintc(
7104# ifdef FEAT_MBYTE
7105 (*mb_ptr2char)(ptr)
7106# else
7107 *ptr
7108# endif
7109 ) && ptr2cells(ptr) > 1)
7110 curwin->w_cursor.coladd = 0;
7111 }
7112
7113 curwin->w_set_curswant = TRUE;
7114 return OK;
7115 }
7116#endif
7117
7118 if (curwin->w_cursor.col == 0)
7119 return FAIL;
7120
7121 curwin->w_set_curswant = TRUE;
7122 --curwin->w_cursor.col;
7123
7124#ifdef FEAT_MBYTE
7125 /* if the character on the left of the current cursor is a multi-byte
7126 * character, move to its first byte */
7127 if (has_mbyte)
7128 mb_adjust_cursor();
7129#endif
7130 return OK;
7131}
7132
7133 int
7134cursor_up(n, upd_topline)
7135 long n;
7136 int upd_topline; /* When TRUE: update topline */
7137{
7138 linenr_T lnum;
7139
7140 if (n > 0)
7141 {
7142 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007143 /* This fails if the cursor is already in the first line or the count
7144 * is larger than the line number and '-' is in 'cpoptions' */
7145 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007146 return FAIL;
7147 if (n >= lnum)
7148 lnum = 1;
7149 else
7150#ifdef FEAT_FOLDING
7151 if (hasAnyFolding(curwin))
7152 {
7153 /*
7154 * Count each sequence of folded lines as one logical line.
7155 */
7156 /* go to the the start of the current fold */
7157 (void)hasFolding(lnum, &lnum, NULL);
7158
7159 while (n--)
7160 {
7161 /* move up one line */
7162 --lnum;
7163 if (lnum <= 1)
7164 break;
7165 /* If we entered a fold, move to the beginning, unless in
7166 * Insert mode or when 'foldopen' contains "all": it will open
7167 * in a moment. */
7168 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7169 (void)hasFolding(lnum, &lnum, NULL);
7170 }
7171 if (lnum < 1)
7172 lnum = 1;
7173 }
7174 else
7175#endif
7176 lnum -= n;
7177 curwin->w_cursor.lnum = lnum;
7178 }
7179
7180 /* try to advance to the column we want to be at */
7181 coladvance(curwin->w_curswant);
7182
7183 if (upd_topline)
7184 update_topline(); /* make sure curwin->w_topline is valid */
7185
7186 return OK;
7187}
7188
7189/*
7190 * Cursor down a number of logical lines.
7191 */
7192 int
7193cursor_down(n, upd_topline)
7194 long n;
7195 int upd_topline; /* When TRUE: update topline */
7196{
7197 linenr_T lnum;
7198
7199 if (n > 0)
7200 {
7201 lnum = curwin->w_cursor.lnum;
7202#ifdef FEAT_FOLDING
7203 /* Move to last line of fold, will fail if it's the end-of-file. */
7204 (void)hasFolding(lnum, NULL, &lnum);
7205#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007206 /* This fails if the cursor is already in the last line or would move
7207 * beyound the last line and '-' is in 'cpoptions' */
7208 if (lnum >= curbuf->b_ml.ml_line_count
7209 || (lnum + n > curbuf->b_ml.ml_line_count
7210 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007211 return FAIL;
7212 if (lnum + n >= curbuf->b_ml.ml_line_count)
7213 lnum = curbuf->b_ml.ml_line_count;
7214 else
7215#ifdef FEAT_FOLDING
7216 if (hasAnyFolding(curwin))
7217 {
7218 linenr_T last;
7219
7220 /* count each sequence of folded lines as one logical line */
7221 while (n--)
7222 {
7223 if (hasFolding(lnum, NULL, &last))
7224 lnum = last + 1;
7225 else
7226 ++lnum;
7227 if (lnum >= curbuf->b_ml.ml_line_count)
7228 break;
7229 }
7230 if (lnum > curbuf->b_ml.ml_line_count)
7231 lnum = curbuf->b_ml.ml_line_count;
7232 }
7233 else
7234#endif
7235 lnum += n;
7236 curwin->w_cursor.lnum = lnum;
7237 }
7238
7239 /* try to advance to the column we want to be at */
7240 coladvance(curwin->w_curswant);
7241
7242 if (upd_topline)
7243 update_topline(); /* make sure curwin->w_topline is valid */
7244
7245 return OK;
7246}
7247
7248/*
7249 * Stuff the last inserted text in the read buffer.
7250 * Last_insert actually is a copy of the redo buffer, so we
7251 * first have to remove the command.
7252 */
7253 int
7254stuff_inserted(c, count, no_esc)
7255 int c; /* Command character to be inserted */
7256 long count; /* Repeat this many times */
7257 int no_esc; /* Don't add an ESC at the end */
7258{
7259 char_u *esc_ptr;
7260 char_u *ptr;
7261 char_u *last_ptr;
7262 char_u last = NUL;
7263
7264 ptr = get_last_insert();
7265 if (ptr == NULL)
7266 {
7267 EMSG(_(e_noinstext));
7268 return FAIL;
7269 }
7270
7271 /* may want to stuff the command character, to start Insert mode */
7272 if (c != NUL)
7273 stuffcharReadbuff(c);
7274 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7275 *esc_ptr = NUL; /* remove the ESC */
7276
7277 /* when the last char is either "0" or "^" it will be quoted if no ESC
7278 * comes after it OR if it will inserted more than once and "ptr"
7279 * starts with ^D. -- Acevedo
7280 */
7281 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7282 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7283 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7284 {
7285 last = *last_ptr;
7286 *last_ptr = NUL;
7287 }
7288
7289 do
7290 {
7291 stuffReadbuff(ptr);
7292 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7293 if (last)
7294 stuffReadbuff((char_u *)(last == '0'
7295 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7296 : IF_EB("\026^", CTRL_V_STR "^")));
7297 }
7298 while (--count > 0);
7299
7300 if (last)
7301 *last_ptr = last;
7302
7303 if (esc_ptr != NULL)
7304 *esc_ptr = ESC; /* put the ESC back */
7305
7306 /* may want to stuff a trailing ESC, to get out of Insert mode */
7307 if (!no_esc)
7308 stuffcharReadbuff(ESC);
7309
7310 return OK;
7311}
7312
7313 char_u *
7314get_last_insert()
7315{
7316 if (last_insert == NULL)
7317 return NULL;
7318 return last_insert + last_insert_skip;
7319}
7320
7321/*
7322 * Get last inserted string, and remove trailing <Esc>.
7323 * Returns pointer to allocated memory (must be freed) or NULL.
7324 */
7325 char_u *
7326get_last_insert_save()
7327{
7328 char_u *s;
7329 int len;
7330
7331 if (last_insert == NULL)
7332 return NULL;
7333 s = vim_strsave(last_insert + last_insert_skip);
7334 if (s != NULL)
7335 {
7336 len = (int)STRLEN(s);
7337 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7338 s[len - 1] = NUL;
7339 }
7340 return s;
7341}
7342
7343/*
7344 * Check the word in front of the cursor for an abbreviation.
7345 * Called when the non-id character "c" has been entered.
7346 * When an abbreviation is recognized it is removed from the text and
7347 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7348 */
7349 static int
7350echeck_abbr(c)
7351 int c;
7352{
7353 /* Don't check for abbreviation in paste mode, when disabled and just
7354 * after moving around with cursor keys. */
7355 if (p_paste || no_abbr || arrow_used)
7356 return FALSE;
7357
7358 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7359 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7360}
7361
7362/*
7363 * replace-stack functions
7364 *
7365 * When replacing characters, the replaced characters are remembered for each
7366 * new character. This is used to re-insert the old text when backspacing.
7367 *
7368 * There is a NUL headed list of characters for each character that is
7369 * currently in the file after the insertion point. When BS is used, one NUL
7370 * headed list is put back for the deleted character.
7371 *
7372 * For a newline, there are two NUL headed lists. One contains the characters
7373 * that the NL replaced. The extra one stores the characters after the cursor
7374 * that were deleted (always white space).
7375 *
7376 * Replace_offset is normally 0, in which case replace_push will add a new
7377 * character at the end of the stack. If replace_offset is not 0, that many
7378 * characters will be left on the stack above the newly inserted character.
7379 */
7380
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007381static char_u *replace_stack = NULL;
7382static long replace_stack_nr = 0; /* next entry in replace stack */
7383static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007384
7385 void
7386replace_push(c)
7387 int c; /* character that is replaced (NUL is none) */
7388{
7389 char_u *p;
7390
7391 if (replace_stack_nr < replace_offset) /* nothing to do */
7392 return;
7393 if (replace_stack_len <= replace_stack_nr)
7394 {
7395 replace_stack_len += 50;
7396 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7397 if (p == NULL) /* out of memory */
7398 {
7399 replace_stack_len -= 50;
7400 return;
7401 }
7402 if (replace_stack != NULL)
7403 {
7404 mch_memmove(p, replace_stack,
7405 (size_t)(replace_stack_nr * sizeof(char_u)));
7406 vim_free(replace_stack);
7407 }
7408 replace_stack = p;
7409 }
7410 p = replace_stack + replace_stack_nr - replace_offset;
7411 if (replace_offset)
7412 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7413 *p = c;
7414 ++replace_stack_nr;
7415}
7416
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007417#if defined(FEAT_MBYTE) || defined(PROTO)
7418/*
7419 * Push a character onto the replace stack. Handles a multi-byte character in
7420 * reverse byte order, so that the first byte is popped off first.
7421 * Return the number of bytes done (includes composing characters).
7422 */
7423 int
7424replace_push_mb(p)
7425 char_u *p;
7426{
7427 int l = (*mb_ptr2len)(p);
7428 int j;
7429
7430 for (j = l - 1; j >= 0; --j)
7431 replace_push(p[j]);
7432 return l;
7433}
7434#endif
7435
Bram Moolenaar071d4272004-06-13 20:20:40 +00007436/*
7437 * Pop one item from the replace stack.
7438 * return -1 if stack empty
7439 * return replaced character or NUL otherwise
7440 */
7441 static int
7442replace_pop()
7443{
7444 if (replace_stack_nr == 0)
7445 return -1;
7446 return (int)replace_stack[--replace_stack_nr];
7447}
7448
7449/*
7450 * Join the top two items on the replace stack. This removes to "off"'th NUL
7451 * encountered.
7452 */
7453 static void
7454replace_join(off)
7455 int off; /* offset for which NUL to remove */
7456{
7457 int i;
7458
7459 for (i = replace_stack_nr; --i >= 0; )
7460 if (replace_stack[i] == NUL && off-- <= 0)
7461 {
7462 --replace_stack_nr;
7463 mch_memmove(replace_stack + i, replace_stack + i + 1,
7464 (size_t)(replace_stack_nr - i));
7465 return;
7466 }
7467}
7468
7469/*
7470 * Pop bytes from the replace stack until a NUL is found, and insert them
7471 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7472 */
7473 static void
7474replace_pop_ins()
7475{
7476 int cc;
7477 int oldState = State;
7478
7479 State = NORMAL; /* don't want REPLACE here */
7480 while ((cc = replace_pop()) > 0)
7481 {
7482#ifdef FEAT_MBYTE
7483 mb_replace_pop_ins(cc);
7484#else
7485 ins_char(cc);
7486#endif
7487 dec_cursor();
7488 }
7489 State = oldState;
7490}
7491
7492#ifdef FEAT_MBYTE
7493/*
7494 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7495 * indicates a multi-byte char, pop the other bytes too.
7496 */
7497 static void
7498mb_replace_pop_ins(cc)
7499 int cc;
7500{
7501 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007502 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007503 int i;
7504 int c;
7505
7506 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7507 {
7508 buf[0] = cc;
7509 for (i = 1; i < n; ++i)
7510 buf[i] = replace_pop();
7511 ins_bytes_len(buf, n);
7512 }
7513 else
7514 ins_char(cc);
7515
7516 if (enc_utf8)
7517 /* Handle composing chars. */
7518 for (;;)
7519 {
7520 c = replace_pop();
7521 if (c == -1) /* stack empty */
7522 break;
7523 if ((n = MB_BYTE2LEN(c)) == 1)
7524 {
7525 /* Not a multi-byte char, put it back. */
7526 replace_push(c);
7527 break;
7528 }
7529 else
7530 {
7531 buf[0] = c;
7532 for (i = 1; i < n; ++i)
7533 buf[i] = replace_pop();
7534 if (utf_iscomposing(utf_ptr2char(buf)))
7535 ins_bytes_len(buf, n);
7536 else
7537 {
7538 /* Not a composing char, put it back. */
7539 for (i = n - 1; i >= 0; --i)
7540 replace_push(buf[i]);
7541 break;
7542 }
7543 }
7544 }
7545}
7546#endif
7547
7548/*
7549 * make the replace stack empty
7550 * (called when exiting replace mode)
7551 */
7552 static void
7553replace_flush()
7554{
7555 vim_free(replace_stack);
7556 replace_stack = NULL;
7557 replace_stack_len = 0;
7558 replace_stack_nr = 0;
7559}
7560
7561/*
7562 * Handle doing a BS for one character.
7563 * cc < 0: replace stack empty, just move cursor
7564 * cc == 0: character was inserted, delete it
7565 * cc > 0: character was replaced, put cc (first byte of original char) back
7566 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007567 * When "limit_col" is >= 0, don't delete before this column. Matters when
7568 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007569 */
7570 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007571replace_do_bs(limit_col)
7572 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007573{
7574 int cc;
7575#ifdef FEAT_VREPLACE
7576 int orig_len = 0;
7577 int ins_len;
7578 int orig_vcols = 0;
7579 colnr_T start_vcol;
7580 char_u *p;
7581 int i;
7582 int vcol;
7583#endif
7584
7585 cc = replace_pop();
7586 if (cc > 0)
7587 {
7588#ifdef FEAT_VREPLACE
7589 if (State & VREPLACE_FLAG)
7590 {
7591 /* Get the number of screen cells used by the character we are
7592 * going to delete. */
7593 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7594 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7595 }
7596#endif
7597#ifdef FEAT_MBYTE
7598 if (has_mbyte)
7599 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007600 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007601# ifdef FEAT_VREPLACE
7602 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007603 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007604# endif
7605 replace_push(cc);
7606 }
7607 else
7608#endif
7609 {
7610 pchar_cursor(cc);
7611#ifdef FEAT_VREPLACE
7612 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007613 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007614#endif
7615 }
7616 replace_pop_ins();
7617
7618#ifdef FEAT_VREPLACE
7619 if (State & VREPLACE_FLAG)
7620 {
7621 /* Get the number of screen cells used by the inserted characters */
7622 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007623 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007624 vcol = start_vcol;
7625 for (i = 0; i < ins_len; ++i)
7626 {
7627 vcol += chartabsize(p + i, vcol);
7628#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007629 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007630#endif
7631 }
7632 vcol -= start_vcol;
7633
7634 /* Delete spaces that were inserted after the cursor to keep the
7635 * text aligned. */
7636 curwin->w_cursor.col += ins_len;
7637 while (vcol > orig_vcols && gchar_cursor() == ' ')
7638 {
7639 del_char(FALSE);
7640 ++orig_vcols;
7641 }
7642 curwin->w_cursor.col -= ins_len;
7643 }
7644#endif
7645
7646 /* mark the buffer as changed and prepare for displaying */
7647 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7648 }
7649 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007650 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007651}
7652
7653#ifdef FEAT_CINDENT
7654/*
7655 * Return TRUE if C-indenting is on.
7656 */
7657 static int
7658cindent_on()
7659{
7660 return (!p_paste && (curbuf->b_p_cin
7661# ifdef FEAT_EVAL
7662 || *curbuf->b_p_inde != NUL
7663# endif
7664 ));
7665}
7666#endif
7667
7668#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7669/*
7670 * Re-indent the current line, based on the current contents of it and the
7671 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7672 * confused what all the part that handles Control-T is doing that I'm not.
7673 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7674 */
7675
7676 void
7677fixthisline(get_the_indent)
7678 int (*get_the_indent) __ARGS((void));
7679{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007680 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007681 if (linewhite(curwin->w_cursor.lnum))
7682 did_ai = TRUE; /* delete the indent if the line stays empty */
7683}
7684
7685 void
7686fix_indent()
7687{
7688 if (p_paste)
7689 return;
7690# ifdef FEAT_LISP
7691 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7692 fixthisline(get_lisp_indent);
7693# endif
7694# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7695 else
7696# endif
7697# ifdef FEAT_CINDENT
7698 if (cindent_on())
7699 do_c_expr_indent();
7700# endif
7701}
7702
7703#endif
7704
7705#ifdef FEAT_CINDENT
7706/*
7707 * return TRUE if 'cinkeys' contains the key "keytyped",
7708 * when == '*': Only if key is preceded with '*' (indent before insert)
7709 * when == '!': Only if key is prededed with '!' (don't insert)
7710 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7711 *
7712 * "keytyped" can have a few special values:
7713 * KEY_OPEN_FORW
7714 * KEY_OPEN_BACK
7715 * KEY_COMPLETE just finished completion.
7716 *
7717 * If line_is_empty is TRUE accept keys with '0' before them.
7718 */
7719 int
7720in_cinkeys(keytyped, when, line_is_empty)
7721 int keytyped;
7722 int when;
7723 int line_is_empty;
7724{
7725 char_u *look;
7726 int try_match;
7727 int try_match_word;
7728 char_u *p;
7729 char_u *line;
7730 int icase;
7731 int i;
7732
Bram Moolenaar30848942009-12-24 14:46:12 +00007733 if (keytyped == NUL)
7734 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7735 return FALSE;
7736
Bram Moolenaar071d4272004-06-13 20:20:40 +00007737#ifdef FEAT_EVAL
7738 if (*curbuf->b_p_inde != NUL)
7739 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7740 else
7741#endif
7742 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7743 while (*look)
7744 {
7745 /*
7746 * Find out if we want to try a match with this key, depending on
7747 * 'when' and a '*' or '!' before the key.
7748 */
7749 switch (when)
7750 {
7751 case '*': try_match = (*look == '*'); break;
7752 case '!': try_match = (*look == '!'); break;
7753 default: try_match = (*look != '*'); break;
7754 }
7755 if (*look == '*' || *look == '!')
7756 ++look;
7757
7758 /*
7759 * If there is a '0', only accept a match if the line is empty.
7760 * But may still match when typing last char of a word.
7761 */
7762 if (*look == '0')
7763 {
7764 try_match_word = try_match;
7765 if (!line_is_empty)
7766 try_match = FALSE;
7767 ++look;
7768 }
7769 else
7770 try_match_word = FALSE;
7771
7772 /*
7773 * does it look like a control character?
7774 */
7775 if (*look == '^'
7776#ifdef EBCDIC
7777 && (Ctrl_chr(look[1]) != 0)
7778#else
7779 && look[1] >= '?' && look[1] <= '_'
7780#endif
7781 )
7782 {
7783 if (try_match && keytyped == Ctrl_chr(look[1]))
7784 return TRUE;
7785 look += 2;
7786 }
7787 /*
7788 * 'o' means "o" command, open forward.
7789 * 'O' means "O" command, open backward.
7790 */
7791 else if (*look == 'o')
7792 {
7793 if (try_match && keytyped == KEY_OPEN_FORW)
7794 return TRUE;
7795 ++look;
7796 }
7797 else if (*look == 'O')
7798 {
7799 if (try_match && keytyped == KEY_OPEN_BACK)
7800 return TRUE;
7801 ++look;
7802 }
7803
7804 /*
7805 * 'e' means to check for "else" at start of line and just before the
7806 * cursor.
7807 */
7808 else if (*look == 'e')
7809 {
7810 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7811 {
7812 p = ml_get_curline();
7813 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7814 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7815 return TRUE;
7816 }
7817 ++look;
7818 }
7819
7820 /*
7821 * ':' only causes an indent if it is at the end of a label or case
7822 * statement, or when it was before typing the ':' (to fix
7823 * class::method for C++).
7824 */
7825 else if (*look == ':')
7826 {
7827 if (try_match && keytyped == ':')
7828 {
7829 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007830 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7831 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007832 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007833 /* Need to get the line again after cin_islabel(). */
7834 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835 if (curwin->w_cursor.col > 2
7836 && p[curwin->w_cursor.col - 1] == ':'
7837 && p[curwin->w_cursor.col - 2] == ':')
7838 {
7839 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007840 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 || cin_islabel(30));
7842 p = ml_get_curline();
7843 p[curwin->w_cursor.col - 1] = ':';
7844 if (i)
7845 return TRUE;
7846 }
7847 }
7848 ++look;
7849 }
7850
7851
7852 /*
7853 * Is it a key in <>, maybe?
7854 */
7855 else if (*look == '<')
7856 {
7857 if (try_match)
7858 {
7859 /*
7860 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7861 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7862 * >, *, : and ! keys if they really really want to.
7863 */
7864 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7865 && keytyped == look[1])
7866 return TRUE;
7867
7868 if (keytyped == get_special_key_code(look + 1))
7869 return TRUE;
7870 }
7871 while (*look && *look != '>')
7872 look++;
7873 while (*look == '>')
7874 look++;
7875 }
7876
7877 /*
7878 * Is it a word: "=word"?
7879 */
7880 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7881 {
7882 ++look;
7883 if (*look == '~')
7884 {
7885 icase = TRUE;
7886 ++look;
7887 }
7888 else
7889 icase = FALSE;
7890 p = vim_strchr(look, ',');
7891 if (p == NULL)
7892 p = look + STRLEN(look);
7893 if ((try_match || try_match_word)
7894 && curwin->w_cursor.col >= (colnr_T)(p - look))
7895 {
7896 int match = FALSE;
7897
7898#ifdef FEAT_INS_EXPAND
7899 if (keytyped == KEY_COMPLETE)
7900 {
7901 char_u *s;
7902
7903 /* Just completed a word, check if it starts with "look".
7904 * search back for the start of a word. */
7905 line = ml_get_curline();
7906# ifdef FEAT_MBYTE
7907 if (has_mbyte)
7908 {
7909 char_u *n;
7910
7911 for (s = line + curwin->w_cursor.col; s > line; s = n)
7912 {
7913 n = mb_prevptr(line, s);
7914 if (!vim_iswordp(n))
7915 break;
7916 }
7917 }
7918 else
7919# endif
7920 for (s = line + curwin->w_cursor.col; s > line; --s)
7921 if (!vim_iswordc(s[-1]))
7922 break;
7923 if (s + (p - look) <= line + curwin->w_cursor.col
7924 && (icase
7925 ? MB_STRNICMP(s, look, p - look)
7926 : STRNCMP(s, look, p - look)) == 0)
7927 match = TRUE;
7928 }
7929 else
7930#endif
7931 /* TODO: multi-byte */
7932 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7933 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7934 {
7935 line = ml_get_cursor();
7936 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7937 || !vim_iswordc(line[-(p - look) - 1]))
7938 && (icase
7939 ? MB_STRNICMP(line - (p - look), look, p - look)
7940 : STRNCMP(line - (p - look), look, p - look))
7941 == 0)
7942 match = TRUE;
7943 }
7944 if (match && try_match_word && !try_match)
7945 {
7946 /* "0=word": Check if there are only blanks before the
7947 * word. */
7948 line = ml_get_curline();
7949 if ((int)(skipwhite(line) - line) !=
7950 (int)(curwin->w_cursor.col - (p - look)))
7951 match = FALSE;
7952 }
7953 if (match)
7954 return TRUE;
7955 }
7956 look = p;
7957 }
7958
7959 /*
7960 * ok, it's a boring generic character.
7961 */
7962 else
7963 {
7964 if (try_match && *look == keytyped)
7965 return TRUE;
7966 ++look;
7967 }
7968
7969 /*
7970 * Skip over ", ".
7971 */
7972 look = skip_to_option_part(look);
7973 }
7974 return FALSE;
7975}
7976#endif /* FEAT_CINDENT */
7977
7978#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7979/*
7980 * Map Hebrew keyboard when in hkmap mode.
7981 */
7982 int
7983hkmap(c)
7984 int c;
7985{
7986 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7987 {
7988 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7989 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7990 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7991 static char_u map[26] =
7992 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7993 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7994 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7995 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7996 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7997 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7998 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7999 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8000 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8001
8002 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8003 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8004 /* '-1'='sofit' */
8005 else if (c == 'x')
8006 return 'X';
8007 else if (c == 'q')
8008 return '\''; /* {geresh}={'} */
8009 else if (c == 246)
8010 return ' '; /* \"o --> ' ' for a german keyboard */
8011 else if (c == 228)
8012 return ' '; /* \"a --> ' ' -- / -- */
8013 else if (c == 252)
8014 return ' '; /* \"u --> ' ' -- / -- */
8015#ifdef EBCDIC
8016 else if (islower(c))
8017#else
8018 /* NOTE: islower() does not do the right thing for us on Linux so we
8019 * do this the same was as 5.7 and previous, so it works correctly on
8020 * all systems. Specifically, the e.g. Delete and Arrow keys are
8021 * munged and won't work if e.g. searching for Hebrew text.
8022 */
8023 else if (c >= 'a' && c <= 'z')
8024#endif
8025 return (int)(map[CharOrdLow(c)] + p_aleph);
8026 else
8027 return c;
8028 }
8029 else
8030 {
8031 switch (c)
8032 {
8033 case '`': return ';';
8034 case '/': return '.';
8035 case '\'': return ',';
8036 case 'q': return '/';
8037 case 'w': return '\'';
8038
8039 /* Hebrew letters - set offset from 'a' */
8040 case ',': c = '{'; break;
8041 case '.': c = 'v'; break;
8042 case ';': c = 't'; break;
8043 default: {
8044 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8045
8046#ifdef EBCDIC
8047 /* see note about islower() above */
8048 if (!islower(c))
8049#else
8050 if (c < 'a' || c > 'z')
8051#endif
8052 return c;
8053 c = str[CharOrdLow(c)];
8054 break;
8055 }
8056 }
8057
8058 return (int)(CharOrdLow(c) + p_aleph);
8059 }
8060}
8061#endif
8062
8063 static void
8064ins_reg()
8065{
8066 int need_redraw = FALSE;
8067 int regname;
8068 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008069#ifdef FEAT_VISUAL
8070 int vis_active = VIsual_active;
8071#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008072
8073 /*
8074 * If we are going to wait for a character, show a '"'.
8075 */
8076 pc_status = PC_STATUS_UNSET;
8077 if (redrawing() && !char_avail())
8078 {
8079 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008080 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081
8082 edit_putchar('"', TRUE);
8083#ifdef FEAT_CMDL_INFO
8084 add_to_showcmd_c(Ctrl_R);
8085#endif
8086 }
8087
8088#ifdef USE_ON_FLY_SCROLL
8089 dont_scroll = TRUE; /* disallow scrolling here */
8090#endif
8091
8092 /*
8093 * Don't map the register name. This also prevents the mode message to be
8094 * deleted when ESC is hit.
8095 */
8096 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008097 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8100 {
8101 /* Get a third key for literal register insertion */
8102 literally = regname;
8103#ifdef FEAT_CMDL_INFO
8104 add_to_showcmd_c(literally);
8105#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008106 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008108 }
8109 --no_mapping;
8110
8111#ifdef FEAT_EVAL
8112 /*
8113 * Don't call u_sync() while getting the expression,
8114 * evaluating it or giving an error message for it!
8115 */
8116 ++no_u_sync;
8117 if (regname == '=')
8118 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008119# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008120 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008121# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008123# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 /* Restore the Input Method. */
8125 if (im_on)
8126 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008127# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008129 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8130 {
8131 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008133 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 else
8135 {
8136#endif
8137 if (literally == Ctrl_O || literally == Ctrl_P)
8138 {
8139 /* Append the command to the redo buffer. */
8140 AppendCharToRedobuff(Ctrl_R);
8141 AppendCharToRedobuff(literally);
8142 AppendCharToRedobuff(regname);
8143
8144 do_put(regname, BACKWARD, 1L,
8145 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8146 }
8147 else if (insert_reg(regname, literally) == FAIL)
8148 {
8149 vim_beep();
8150 need_redraw = TRUE; /* remove the '"' */
8151 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008152 else if (stop_insert_mode)
8153 /* When the '=' register was used and a function was invoked that
8154 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8155 * insert anything, need to remove the '"' */
8156 need_redraw = TRUE;
8157
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158#ifdef FEAT_EVAL
8159 }
8160 --no_u_sync;
8161#endif
8162#ifdef FEAT_CMDL_INFO
8163 clear_showcmd();
8164#endif
8165
8166 /* If the inserted register is empty, we need to remove the '"' */
8167 if (need_redraw || stuff_empty())
8168 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008169
8170#ifdef FEAT_VISUAL
8171 /* Disallow starting Visual mode here, would get a weird mode. */
8172 if (!vis_active && VIsual_active)
8173 end_visual_mode();
8174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175}
8176
8177/*
8178 * CTRL-G commands in Insert mode.
8179 */
8180 static void
8181ins_ctrl_g()
8182{
8183 int c;
8184
8185#ifdef FEAT_INS_EXPAND
8186 /* Right after CTRL-X the cursor will be after the ruler. */
8187 setcursor();
8188#endif
8189
8190 /*
8191 * Don't map the second key. This also prevents the mode message to be
8192 * deleted when ESC is hit.
8193 */
8194 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008195 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 --no_mapping;
8197 switch (c)
8198 {
8199 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8200 case K_UP:
8201 case Ctrl_K:
8202 case 'k': ins_up(TRUE);
8203 break;
8204
8205 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8206 case K_DOWN:
8207 case Ctrl_J:
8208 case 'j': ins_down(TRUE);
8209 break;
8210
8211 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008212 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008213 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008214
8215 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008216 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008217 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008218 break;
8219
8220 /* Unknown CTRL-G command, reserved for future expansion. */
8221 default: vim_beep();
8222 }
8223}
8224
8225/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008226 * CTRL-^ in Insert mode.
8227 */
8228 static void
8229ins_ctrl_hat()
8230{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008231 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008232 {
8233 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8234 if (State & LANGMAP)
8235 {
8236 curbuf->b_p_iminsert = B_IMODE_NONE;
8237 State &= ~LANGMAP;
8238 }
8239 else
8240 {
8241 curbuf->b_p_iminsert = B_IMODE_LMAP;
8242 State |= LANGMAP;
8243#ifdef USE_IM_CONTROL
8244 im_set_active(FALSE);
8245#endif
8246 }
8247 }
8248#ifdef USE_IM_CONTROL
8249 else
8250 {
8251 /* There are no ":lmap" mappings, toggle IM */
8252 if (im_get_status())
8253 {
8254 curbuf->b_p_iminsert = B_IMODE_NONE;
8255 im_set_active(FALSE);
8256 }
8257 else
8258 {
8259 curbuf->b_p_iminsert = B_IMODE_IM;
8260 State &= ~LANGMAP;
8261 im_set_active(TRUE);
8262 }
8263 }
8264#endif
8265 set_iminsert_global();
8266 showmode();
8267#ifdef FEAT_GUI
8268 /* may show different cursor shape or color */
8269 if (gui.in_use)
8270 gui_update_cursor(TRUE, FALSE);
8271#endif
8272#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8273 /* Show/unshow value of 'keymap' in status lines. */
8274 status_redraw_curbuf();
8275#endif
8276}
8277
8278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 * Handle ESC in insert mode.
8280 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8281 * insert.
8282 */
8283 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008284ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008285 long *count;
8286 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008287 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288{
8289 int temp;
8290 static int disabled_redraw = FALSE;
8291
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008292#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008293 check_spell_redraw();
8294#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008295#if defined(FEAT_HANGULIN)
8296# if defined(ESC_CHG_TO_ENG_MODE)
8297 hangul_input_state_set(0);
8298# endif
8299 if (composing_hangul)
8300 {
8301 push_raw_key(composing_hangul_buffer, 2);
8302 composing_hangul = 0;
8303 }
8304#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305
8306 temp = curwin->w_cursor.col;
8307 if (disabled_redraw)
8308 {
8309 --RedrawingDisabled;
8310 disabled_redraw = FALSE;
8311 }
8312 if (!arrow_used)
8313 {
8314 /*
8315 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008316 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8317 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008318 */
8319 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008320 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321
8322 /*
8323 * Repeating insert may take a long time. Check for
8324 * interrupt now and then.
8325 */
8326 if (*count > 0)
8327 {
8328 line_breakcheck();
8329 if (got_int)
8330 *count = 0;
8331 }
8332
8333 if (--*count > 0) /* repeat what was typed */
8334 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008335 /* Vi repeats the insert without replacing characters. */
8336 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8337 State &= ~REPLACE_FLAG;
8338
Bram Moolenaar071d4272004-06-13 20:20:40 +00008339 (void)start_redo_ins();
8340 if (cmdchar == 'r' || cmdchar == 'v')
8341 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8342 ++RedrawingDisabled;
8343 disabled_redraw = TRUE;
8344 return FALSE; /* repeat the insert */
8345 }
8346 stop_insert(&curwin->w_cursor, TRUE);
8347 undisplay_dollar();
8348 }
8349
8350 /* When an autoindent was removed, curswant stays after the
8351 * indent */
8352 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8353 curwin->w_set_curswant = TRUE;
8354
8355 /* Remember the last Insert position in the '^ mark. */
8356 if (!cmdmod.keepjumps)
8357 curbuf->b_last_insert = curwin->w_cursor;
8358
8359 /*
8360 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008361 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008362 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008363 if (!nomove
8364 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365#ifdef FEAT_VIRTUALEDIT
8366 || curwin->w_cursor.coladd > 0
8367#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008368 )
8369 && (restart_edit == NUL
8370 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008372 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008374 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008375#ifdef FEAT_RIGHTLEFT
8376 && !revins_on
8377#endif
8378 )
8379 {
8380#ifdef FEAT_VIRTUALEDIT
8381 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8382 {
8383 oneleft();
8384 if (restart_edit != NUL)
8385 ++curwin->w_cursor.coladd;
8386 }
8387 else
8388#endif
8389 {
8390 --curwin->w_cursor.col;
8391#ifdef FEAT_MBYTE
8392 /* Correct cursor for multi-byte character. */
8393 if (has_mbyte)
8394 mb_adjust_cursor();
8395#endif
8396 }
8397 }
8398
8399#ifdef USE_IM_CONTROL
8400 /* Disable IM to allow typing English directly for Normal mode commands.
8401 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8402 * well). */
8403 if (!(State & LANGMAP))
8404 im_save_status(&curbuf->b_p_iminsert);
8405 im_set_active(FALSE);
8406#endif
8407
8408 State = NORMAL;
8409 /* need to position cursor again (e.g. when on a TAB ) */
8410 changed_cline_bef_curs();
8411
8412#ifdef FEAT_MOUSE
8413 setmouse();
8414#endif
8415#ifdef CURSOR_SHAPE
8416 ui_cursor_shape(); /* may show different cursor shape */
8417#endif
8418
8419 /*
8420 * When recording or for CTRL-O, need to display the new mode.
8421 * Otherwise remove the mode message.
8422 */
8423 if (Recording || restart_edit != NUL)
8424 showmode();
8425 else if (p_smd)
8426 MSG("");
8427
8428 return TRUE; /* exit Insert mode */
8429}
8430
8431#ifdef FEAT_RIGHTLEFT
8432/*
8433 * Toggle language: hkmap and revins_on.
8434 * Move to end of reverse inserted text.
8435 */
8436 static void
8437ins_ctrl_()
8438{
8439 if (revins_on && revins_chars && revins_scol >= 0)
8440 {
8441 while (gchar_cursor() != NUL && revins_chars--)
8442 ++curwin->w_cursor.col;
8443 }
8444 p_ri = !p_ri;
8445 revins_on = (State == INSERT && p_ri);
8446 if (revins_on)
8447 {
8448 revins_scol = curwin->w_cursor.col;
8449 revins_legal++;
8450 revins_chars = 0;
8451 undisplay_dollar();
8452 }
8453 else
8454 revins_scol = -1;
8455#ifdef FEAT_FKMAP
8456 if (p_altkeymap)
8457 {
8458 /*
8459 * to be consistent also for redo command, using '.'
8460 * set arrow_used to true and stop it - causing to redo
8461 * characters entered in one mode (normal/reverse insert).
8462 */
8463 arrow_used = TRUE;
8464 (void)stop_arrow();
8465 p_fkmap = curwin->w_p_rl ^ p_ri;
8466 if (p_fkmap && p_ri)
8467 State = INSERT;
8468 }
8469 else
8470#endif
8471 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8472 showmode();
8473}
8474#endif
8475
8476#ifdef FEAT_VISUAL
8477/*
8478 * If 'keymodel' contains "startsel", may start selection.
8479 * Returns TRUE when a CTRL-O and other keys stuffed.
8480 */
8481 static int
8482ins_start_select(c)
8483 int c;
8484{
8485 if (km_startsel)
8486 switch (c)
8487 {
8488 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 case K_PAGEUP:
8491 case K_KPAGEUP:
8492 case K_PAGEDOWN:
8493 case K_KPAGEDOWN:
8494# ifdef MACOS
8495 case K_LEFT:
8496 case K_RIGHT:
8497 case K_UP:
8498 case K_DOWN:
8499 case K_END:
8500 case K_HOME:
8501# endif
8502 if (!(mod_mask & MOD_MASK_SHIFT))
8503 break;
8504 /* FALLTHROUGH */
8505 case K_S_LEFT:
8506 case K_S_RIGHT:
8507 case K_S_UP:
8508 case K_S_DOWN:
8509 case K_S_END:
8510 case K_S_HOME:
8511 /* Start selection right away, the cursor can move with
8512 * CTRL-O when beyond the end of the line. */
8513 start_selection();
8514
8515 /* Execute the key in (insert) Select mode. */
8516 stuffcharReadbuff(Ctrl_O);
8517 if (mod_mask)
8518 {
8519 char_u buf[4];
8520
8521 buf[0] = K_SPECIAL;
8522 buf[1] = KS_MODIFIER;
8523 buf[2] = mod_mask;
8524 buf[3] = NUL;
8525 stuffReadbuff(buf);
8526 }
8527 stuffcharReadbuff(c);
8528 return TRUE;
8529 }
8530 return FALSE;
8531}
8532#endif
8533
8534/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008535 * <Insert> key in Insert mode: toggle insert/remplace mode.
8536 */
8537 static void
8538ins_insert(replaceState)
8539 int replaceState;
8540{
8541#ifdef FEAT_FKMAP
8542 if (p_fkmap && p_ri)
8543 {
8544 beep_flush();
8545 EMSG(farsi_text_3); /* encoded in Farsi */
8546 return;
8547 }
8548#endif
8549
8550#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008551# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008552 set_vim_var_string(VV_INSERTMODE,
8553 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008554# ifdef FEAT_VREPLACE
8555 replaceState == VREPLACE ? "v" :
8556# endif
8557 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008558# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008559 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8560#endif
8561 if (State & REPLACE_FLAG)
8562 State = INSERT | (State & LANGMAP);
8563 else
8564 State = replaceState | (State & LANGMAP);
8565 AppendCharToRedobuff(K_INS);
8566 showmode();
8567#ifdef CURSOR_SHAPE
8568 ui_cursor_shape(); /* may show different cursor shape */
8569#endif
8570}
8571
8572/*
8573 * Pressed CTRL-O in Insert mode.
8574 */
8575 static void
8576ins_ctrl_o()
8577{
8578#ifdef FEAT_VREPLACE
8579 if (State & VREPLACE_FLAG)
8580 restart_edit = 'V';
8581 else
8582#endif
8583 if (State & REPLACE_FLAG)
8584 restart_edit = 'R';
8585 else
8586 restart_edit = 'I';
8587#ifdef FEAT_VIRTUALEDIT
8588 if (virtual_active())
8589 ins_at_eol = FALSE; /* cursor always keeps its column */
8590 else
8591#endif
8592 ins_at_eol = (gchar_cursor() == NUL);
8593}
8594
8595/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008596 * If the cursor is on an indent, ^T/^D insert/delete one
8597 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008598 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008599 * with vi. But vi only supports ^T and ^D after an
8600 * autoindent, we support it everywhere.
8601 */
8602 static void
8603ins_shift(c, lastc)
8604 int c;
8605 int lastc;
8606{
8607 if (stop_arrow() == FAIL)
8608 return;
8609 AppendCharToRedobuff(c);
8610
8611 /*
8612 * 0^D and ^^D: remove all indent.
8613 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008614 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8615 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 {
8617 --curwin->w_cursor.col;
8618 (void)del_char(FALSE); /* delete the '^' or '0' */
8619 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8620 if (State & REPLACE_FLAG)
8621 replace_pop_ins();
8622 if (lastc == '^')
8623 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008624 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 }
8626 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008627 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628
8629 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8630 did_ai = FALSE;
8631#ifdef FEAT_SMARTINDENT
8632 did_si = FALSE;
8633 can_si = FALSE;
8634 can_si_back = FALSE;
8635#endif
8636#ifdef FEAT_CINDENT
8637 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8638#endif
8639}
8640
8641 static void
8642ins_del()
8643{
8644 int temp;
8645
8646 if (stop_arrow() == FAIL)
8647 return;
8648 if (gchar_cursor() == NUL) /* delete newline */
8649 {
8650 temp = curwin->w_cursor.col;
8651 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar81340392012-06-06 16:12:59 +02008652 || do_join(2, FALSE, TRUE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 vim_beep();
8654 else
8655 curwin->w_cursor.col = temp;
8656 }
8657 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8658 vim_beep();
8659 did_ai = FALSE;
8660#ifdef FEAT_SMARTINDENT
8661 did_si = FALSE;
8662 can_si = FALSE;
8663 can_si_back = FALSE;
8664#endif
8665 AppendCharToRedobuff(K_DEL);
8666}
8667
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008668static void ins_bs_one __ARGS((colnr_T *vcolp));
8669
8670/*
8671 * Delete one character for ins_bs().
8672 */
8673 static void
8674ins_bs_one(vcolp)
8675 colnr_T *vcolp;
8676{
8677 dec_cursor();
8678 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8679 if (State & REPLACE_FLAG)
8680 {
8681 /* Don't delete characters before the insert point when in
8682 * Replace mode */
8683 if (curwin->w_cursor.lnum != Insstart.lnum
8684 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008685 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008686 }
8687 else
8688 (void)del_char(FALSE);
8689}
8690
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691/*
8692 * Handle Backspace, delete-word and delete-line in Insert mode.
8693 * Return TRUE when backspace was actually used.
8694 */
8695 static int
8696ins_bs(c, mode, inserted_space_p)
8697 int c;
8698 int mode;
8699 int *inserted_space_p;
8700{
8701 linenr_T lnum;
8702 int cc;
8703 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008704 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008705 colnr_T mincol;
8706 int did_backspace = FALSE;
8707 int in_indent;
8708 int oldState;
8709#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008710 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711#endif
8712
8713 /*
8714 * can't delete anything in an empty file
8715 * can't backup past first character in buffer
8716 * can't backup past starting point unless 'backspace' > 1
8717 * can backup to a previous line if 'backspace' == 0
8718 */
8719 if ( bufempty()
8720 || (
8721#ifdef FEAT_RIGHTLEFT
8722 !revins_on &&
8723#endif
8724 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8725 || (!can_bs(BS_START)
8726 && (arrow_used
8727 || (curwin->w_cursor.lnum == Insstart.lnum
8728 && curwin->w_cursor.col <= Insstart.col)))
8729 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8730 && curwin->w_cursor.col <= ai_col)
8731 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8732 {
8733 vim_beep();
8734 return FALSE;
8735 }
8736
8737 if (stop_arrow() == FAIL)
8738 return FALSE;
8739 in_indent = inindent(0);
8740#ifdef FEAT_CINDENT
8741 if (in_indent)
8742 can_cindent = FALSE;
8743#endif
8744#ifdef FEAT_COMMENTS
8745 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8746#endif
8747#ifdef FEAT_RIGHTLEFT
8748 if (revins_on) /* put cursor after last inserted char */
8749 inc_cursor();
8750#endif
8751
8752#ifdef FEAT_VIRTUALEDIT
8753 /* Virtualedit:
8754 * BACKSPACE_CHAR eats a virtual space
8755 * BACKSPACE_WORD eats all coladd
8756 * BACKSPACE_LINE eats all coladd and keeps going
8757 */
8758 if (curwin->w_cursor.coladd > 0)
8759 {
8760 if (mode == BACKSPACE_CHAR)
8761 {
8762 --curwin->w_cursor.coladd;
8763 return TRUE;
8764 }
8765 if (mode == BACKSPACE_WORD)
8766 {
8767 curwin->w_cursor.coladd = 0;
8768 return TRUE;
8769 }
8770 curwin->w_cursor.coladd = 0;
8771 }
8772#endif
8773
8774 /*
8775 * delete newline!
8776 */
8777 if (curwin->w_cursor.col == 0)
8778 {
8779 lnum = Insstart.lnum;
8780 if (curwin->w_cursor.lnum == Insstart.lnum
8781#ifdef FEAT_RIGHTLEFT
8782 || revins_on
8783#endif
8784 )
8785 {
8786 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8787 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8788 return FALSE;
8789 --Insstart.lnum;
8790 Insstart.col = MAXCOL;
8791 }
8792 /*
8793 * In replace mode:
8794 * cc < 0: NL was inserted, delete it
8795 * cc >= 0: NL was replaced, put original characters back
8796 */
8797 cc = -1;
8798 if (State & REPLACE_FLAG)
8799 cc = replace_pop(); /* returns -1 if NL was inserted */
8800 /*
8801 * In replace mode, in the line we started replacing, we only move the
8802 * cursor.
8803 */
8804 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8805 {
8806 dec_cursor();
8807 }
8808 else
8809 {
8810#ifdef FEAT_VREPLACE
8811 if (!(State & VREPLACE_FLAG)
8812 || curwin->w_cursor.lnum > orig_line_count)
8813#endif
8814 {
8815 temp = gchar_cursor(); /* remember current char */
8816 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008817
8818 /* When "aw" is in 'formatoptions' we must delete the space at
8819 * the end of the line, otherwise the line will be broken
8820 * again when auto-formatting. */
8821 if (has_format_option(FO_AUTO)
8822 && has_format_option(FO_WHITE_PAR))
8823 {
8824 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8825 TRUE);
8826 int len;
8827
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008828 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008829 if (len > 0 && ptr[len - 1] == ' ')
8830 ptr[len - 1] = NUL;
8831 }
8832
Bram Moolenaar81340392012-06-06 16:12:59 +02008833 (void)do_join(2, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 if (temp == NUL && gchar_cursor() != NUL)
8835 inc_cursor();
8836 }
8837#ifdef FEAT_VREPLACE
8838 else
8839 dec_cursor();
8840#endif
8841
8842 /*
8843 * In REPLACE mode we have to put back the text that was replaced
8844 * by the NL. On the replace stack is first a NUL-terminated
8845 * sequence of characters that were deleted and then the
8846 * characters that NL replaced.
8847 */
8848 if (State & REPLACE_FLAG)
8849 {
8850 /*
8851 * Do the next ins_char() in NORMAL state, to
8852 * prevent ins_char() from replacing characters and
8853 * avoiding showmatch().
8854 */
8855 oldState = State;
8856 State = NORMAL;
8857 /*
8858 * restore characters (blanks) deleted after cursor
8859 */
8860 while (cc > 0)
8861 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008862 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863#ifdef FEAT_MBYTE
8864 mb_replace_pop_ins(cc);
8865#else
8866 ins_char(cc);
8867#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008868 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 cc = replace_pop();
8870 }
8871 /* restore the characters that NL replaced */
8872 replace_pop_ins();
8873 State = oldState;
8874 }
8875 }
8876 did_ai = FALSE;
8877 }
8878 else
8879 {
8880 /*
8881 * Delete character(s) before the cursor.
8882 */
8883#ifdef FEAT_RIGHTLEFT
8884 if (revins_on) /* put cursor on last inserted char */
8885 dec_cursor();
8886#endif
8887 mincol = 0;
8888 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008889 if (mode == BACKSPACE_LINE
8890 && (curbuf->b_p_ai
8891#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008892 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008893#endif
8894 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895#ifdef FEAT_RIGHTLEFT
8896 && !revins_on
8897#endif
8898 )
8899 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008900 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008902 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008904 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 }
8906
8907 /*
8908 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8909 */
8910 if ( mode == BACKSPACE_CHAR
8911 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008912 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008913 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 && (*(ml_get_cursor() - 1) == TAB
8915 || (*(ml_get_cursor() - 1) == ' '
8916 && (!*inserted_space_p
8917 || arrow_used))))))
8918 {
8919 int ts;
8920 colnr_T vcol;
8921 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008922 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923
8924 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008925 if (p_sta && in_indent)
Bram Moolenaar14f24742012-08-08 18:01:05 +02008926 ts = (int)get_sw_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008928 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 /* Compute the virtual column where we want to be. Since
8930 * 'showbreak' may get in the way, need to get the last column of
8931 * the previous character. */
8932 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008933 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 dec_cursor();
8935 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8936 inc_cursor();
8937 want_vcol = (want_vcol / ts) * ts;
8938
8939 /* delete characters until we are at or before want_vcol */
8940 while (vcol > want_vcol
8941 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008942 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
8944 /* insert extra spaces until we are at want_vcol */
8945 while (vcol < want_vcol)
8946 {
8947 /* Remember the first char we inserted */
8948 if (curwin->w_cursor.lnum == Insstart.lnum
8949 && curwin->w_cursor.col < Insstart.col)
8950 Insstart.col = curwin->w_cursor.col;
8951
8952#ifdef FEAT_VREPLACE
8953 if (State & VREPLACE_FLAG)
8954 ins_char(' ');
8955 else
8956#endif
8957 {
8958 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008959 if ((State & REPLACE_FLAG))
8960 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008961 }
8962 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8963 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008964
8965 /* If we are now back where we started delete one character. Can
8966 * happen when using 'sts' and 'linebreak'. */
8967 if (vcol >= start_vcol)
8968 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 }
8970
8971 /*
8972 * Delete upto starting point, start of line or previous word.
8973 */
8974 else do
8975 {
8976#ifdef FEAT_RIGHTLEFT
8977 if (!revins_on) /* put cursor on char to be deleted */
8978#endif
8979 dec_cursor();
8980
8981 /* start of word? */
8982 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8983 {
8984 mode = BACKSPACE_WORD_NOT_SPACE;
8985 temp = vim_iswordc(gchar_cursor());
8986 }
8987 /* end of word? */
8988 else if (mode == BACKSPACE_WORD_NOT_SPACE
8989 && (vim_isspace(cc = gchar_cursor())
8990 || vim_iswordc(cc) != temp))
8991 {
8992#ifdef FEAT_RIGHTLEFT
8993 if (!revins_on)
8994#endif
8995 inc_cursor();
8996#ifdef FEAT_RIGHTLEFT
8997 else if (State & REPLACE_FLAG)
8998 dec_cursor();
8999#endif
9000 break;
9001 }
9002 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009003 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 else
9005 {
9006#ifdef FEAT_MBYTE
9007 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009008 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009009#endif
9010 (void)del_char(FALSE);
9011#ifdef FEAT_MBYTE
9012 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009013 * If there are combining characters and 'delcombine' is set
9014 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 * character.
9016 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009017 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 inc_cursor();
9019#endif
9020#ifdef FEAT_RIGHTLEFT
9021 if (revins_chars)
9022 {
9023 revins_chars--;
9024 revins_legal++;
9025 }
9026 if (revins_on && gchar_cursor() == NUL)
9027 break;
9028#endif
9029 }
9030 /* Just a single backspace?: */
9031 if (mode == BACKSPACE_CHAR)
9032 break;
9033 } while (
9034#ifdef FEAT_RIGHTLEFT
9035 revins_on ||
9036#endif
9037 (curwin->w_cursor.col > mincol
9038 && (curwin->w_cursor.lnum != Insstart.lnum
9039 || curwin->w_cursor.col != Insstart.col)));
9040 did_backspace = TRUE;
9041 }
9042#ifdef FEAT_SMARTINDENT
9043 did_si = FALSE;
9044 can_si = FALSE;
9045 can_si_back = FALSE;
9046#endif
9047 if (curwin->w_cursor.col <= 1)
9048 did_ai = FALSE;
9049 /*
9050 * It's a little strange to put backspaces into the redo
9051 * buffer, but it makes auto-indent a lot easier to deal
9052 * with.
9053 */
9054 AppendCharToRedobuff(c);
9055
9056 /* If deleted before the insertion point, adjust it */
9057 if (curwin->w_cursor.lnum == Insstart.lnum
9058 && curwin->w_cursor.col < Insstart.col)
9059 Insstart.col = curwin->w_cursor.col;
9060
9061 /* vi behaviour: the cursor moves backward but the character that
9062 * was there remains visible
9063 * Vim behaviour: the cursor moves backward and the character that
9064 * was there is erased from the screen.
9065 * We can emulate the vi behaviour by pretending there is a dollar
9066 * displayed even when there isn't.
9067 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009068 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069 dollar_vcol = curwin->w_virtcol;
9070
Bram Moolenaarce3be472008-01-14 19:12:28 +00009071#ifdef FEAT_FOLDING
9072 /* When deleting a char the cursor line must never be in a closed fold.
9073 * E.g., when 'foldmethod' is indent and deleting the first non-white
9074 * char before a Tab. */
9075 if (did_backspace)
9076 foldOpenCursor();
9077#endif
9078
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 return did_backspace;
9080}
9081
9082#ifdef FEAT_MOUSE
9083 static void
9084ins_mouse(c)
9085 int c;
9086{
9087 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009088 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089
9090# ifdef FEAT_GUI
9091 /* When GUI is active, also move/paste when 'mouse' is empty */
9092 if (!gui.in_use)
9093# endif
9094 if (!mouse_has(MOUSE_INSERT))
9095 return;
9096
9097 undisplay_dollar();
9098 tpos = curwin->w_cursor;
9099 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9100 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009101#ifdef FEAT_WINDOWS
9102 win_T *new_curwin = curwin;
9103
9104 if (curwin != old_curwin && win_valid(old_curwin))
9105 {
9106 /* Mouse took us to another window. We need to go back to the
9107 * previous one to stop insert there properly. */
9108 curwin = old_curwin;
9109 curbuf = curwin->w_buffer;
9110 }
9111#endif
9112 start_arrow(curwin == old_curwin ? &tpos : NULL);
9113#ifdef FEAT_WINDOWS
9114 if (curwin != new_curwin && win_valid(new_curwin))
9115 {
9116 curwin = new_curwin;
9117 curbuf = curwin->w_buffer;
9118 }
9119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120# ifdef FEAT_CINDENT
9121 can_cindent = TRUE;
9122# endif
9123 }
9124
9125#ifdef FEAT_WINDOWS
9126 /* redraw status lines (in case another window became active) */
9127 redraw_statuslines();
9128#endif
9129}
9130
9131 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009132ins_mousescroll(dir)
9133 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
9135 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009136# if defined(FEAT_WINDOWS)
9137 win_T *old_curwin = curwin;
9138# endif
9139# ifdef FEAT_INS_EXPAND
9140 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141# endif
9142
9143 tpos = curwin->w_cursor;
9144
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009145# ifdef FEAT_WINDOWS
9146 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009147 {
9148 int row, col;
9149
9150 row = mouse_row;
9151 col = mouse_col;
9152
9153 /* find the window at the pointer coordinates */
9154 curwin = mouse_find_win(&row, &col);
9155 curbuf = curwin->w_buffer;
9156 }
9157 if (curwin == old_curwin)
9158# endif
9159 undisplay_dollar();
9160
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009161# ifdef FEAT_INS_EXPAND
9162 /* Don't scroll the window in which completion is being done. */
9163 if (!pum_visible()
9164# if defined(FEAT_WINDOWS)
9165 || curwin != old_curwin
9166# endif
9167 )
9168# endif
9169 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009170 if (dir == MSCR_DOWN || dir == MSCR_UP)
9171 {
9172 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9173 scroll_redraw(dir,
9174 (long)(curwin->w_botline - curwin->w_topline));
9175 else
9176 scroll_redraw(dir, 3L);
9177 }
9178#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009179 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009180 {
9181 int val, step = 6;
9182
9183 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9184 step = W_WIDTH(curwin);
9185 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9186 if (val < 0)
9187 val = 0;
9188 gui_do_horiz_scroll(val, TRUE);
9189 }
9190#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009191# ifdef FEAT_INS_EXPAND
9192 did_scroll = TRUE;
9193# endif
9194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009195
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009196# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009197 curwin->w_redr_status = TRUE;
9198
9199 curwin = old_curwin;
9200 curbuf = curwin->w_buffer;
9201# endif
9202
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009203# ifdef FEAT_INS_EXPAND
9204 /* The popup menu may overlay the window, need to redraw it.
9205 * TODO: Would be more efficient to only redraw the windows that are
9206 * overlapped by the popup menu. */
9207 if (pum_visible() && did_scroll)
9208 {
9209 redraw_all_later(NOT_VALID);
9210 ins_compl_show_pum();
9211 }
9212# endif
9213
Bram Moolenaar071d4272004-06-13 20:20:40 +00009214 if (!equalpos(curwin->w_cursor, tpos))
9215 {
9216 start_arrow(&tpos);
9217# ifdef FEAT_CINDENT
9218 can_cindent = TRUE;
9219# endif
9220 }
9221}
9222#endif
9223
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009224#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009225 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009226ins_tabline(c)
9227 int c;
9228{
9229 /* We will be leaving the current window, unless closing another tab. */
9230 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9231 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9232 {
9233 undisplay_dollar();
9234 start_arrow(&curwin->w_cursor);
9235# ifdef FEAT_CINDENT
9236 can_cindent = TRUE;
9237# endif
9238 }
9239
9240 if (c == K_TABLINE)
9241 goto_tabpage(current_tab);
9242 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009243 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009244 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009245 redraw_statuslines(); /* will redraw the tabline when needed */
9246 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009247}
9248#endif
9249
9250#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009251 void
9252ins_scroll()
9253{
9254 pos_T tpos;
9255
9256 undisplay_dollar();
9257 tpos = curwin->w_cursor;
9258 if (gui_do_scroll())
9259 {
9260 start_arrow(&tpos);
9261# ifdef FEAT_CINDENT
9262 can_cindent = TRUE;
9263# endif
9264 }
9265}
9266
9267 void
9268ins_horscroll()
9269{
9270 pos_T tpos;
9271
9272 undisplay_dollar();
9273 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009274 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009275 {
9276 start_arrow(&tpos);
9277# ifdef FEAT_CINDENT
9278 can_cindent = TRUE;
9279# endif
9280 }
9281}
9282#endif
9283
9284 static void
9285ins_left()
9286{
9287 pos_T tpos;
9288
9289#ifdef FEAT_FOLDING
9290 if ((fdo_flags & FDO_HOR) && KeyTyped)
9291 foldOpenCursor();
9292#endif
9293 undisplay_dollar();
9294 tpos = curwin->w_cursor;
9295 if (oneleft() == OK)
9296 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009297#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9298 /* Only call start_arrow() when not busy with preediting, it will
9299 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9300 if (!im_is_preediting())
9301#endif
9302 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009303#ifdef FEAT_RIGHTLEFT
9304 /* If exit reversed string, position is fixed */
9305 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9306 revins_legal++;
9307 revins_chars++;
9308#endif
9309 }
9310
9311 /*
9312 * if 'whichwrap' set for cursor in insert mode may go to
9313 * previous line
9314 */
9315 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9316 {
9317 start_arrow(&tpos);
9318 --(curwin->w_cursor.lnum);
9319 coladvance((colnr_T)MAXCOL);
9320 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9321 }
9322 else
9323 vim_beep();
9324}
9325
9326 static void
9327ins_home(c)
9328 int c;
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 (c == K_C_HOME)
9339 curwin->w_cursor.lnum = 1;
9340 curwin->w_cursor.col = 0;
9341#ifdef FEAT_VIRTUALEDIT
9342 curwin->w_cursor.coladd = 0;
9343#endif
9344 curwin->w_curswant = 0;
9345 start_arrow(&tpos);
9346}
9347
9348 static void
9349ins_end(c)
9350 int c;
9351{
9352 pos_T tpos;
9353
9354#ifdef FEAT_FOLDING
9355 if ((fdo_flags & FDO_HOR) && KeyTyped)
9356 foldOpenCursor();
9357#endif
9358 undisplay_dollar();
9359 tpos = curwin->w_cursor;
9360 if (c == K_C_END)
9361 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9362 coladvance((colnr_T)MAXCOL);
9363 curwin->w_curswant = MAXCOL;
9364
9365 start_arrow(&tpos);
9366}
9367
9368 static void
9369ins_s_left()
9370{
9371#ifdef FEAT_FOLDING
9372 if ((fdo_flags & FDO_HOR) && KeyTyped)
9373 foldOpenCursor();
9374#endif
9375 undisplay_dollar();
9376 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9377 {
9378 start_arrow(&curwin->w_cursor);
9379 (void)bck_word(1L, FALSE, FALSE);
9380 curwin->w_set_curswant = TRUE;
9381 }
9382 else
9383 vim_beep();
9384}
9385
9386 static void
9387ins_right()
9388{
9389#ifdef FEAT_FOLDING
9390 if ((fdo_flags & FDO_HOR) && KeyTyped)
9391 foldOpenCursor();
9392#endif
9393 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009394 if (gchar_cursor() != NUL
9395#ifdef FEAT_VIRTUALEDIT
9396 || virtual_active()
9397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009398 )
9399 {
9400 start_arrow(&curwin->w_cursor);
9401 curwin->w_set_curswant = TRUE;
9402#ifdef FEAT_VIRTUALEDIT
9403 if (virtual_active())
9404 oneright();
9405 else
9406#endif
9407 {
9408#ifdef FEAT_MBYTE
9409 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009410 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009411 else
9412#endif
9413 ++curwin->w_cursor.col;
9414 }
9415
9416#ifdef FEAT_RIGHTLEFT
9417 revins_legal++;
9418 if (revins_chars)
9419 revins_chars--;
9420#endif
9421 }
9422 /* if 'whichwrap' set for cursor in insert mode, may move the
9423 * cursor to the next line */
9424 else if (vim_strchr(p_ww, ']') != NULL
9425 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9426 {
9427 start_arrow(&curwin->w_cursor);
9428 curwin->w_set_curswant = TRUE;
9429 ++curwin->w_cursor.lnum;
9430 curwin->w_cursor.col = 0;
9431 }
9432 else
9433 vim_beep();
9434}
9435
9436 static void
9437ins_s_right()
9438{
9439#ifdef FEAT_FOLDING
9440 if ((fdo_flags & FDO_HOR) && KeyTyped)
9441 foldOpenCursor();
9442#endif
9443 undisplay_dollar();
9444 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9445 || gchar_cursor() != NUL)
9446 {
9447 start_arrow(&curwin->w_cursor);
9448 (void)fwd_word(1L, FALSE, 0);
9449 curwin->w_set_curswant = TRUE;
9450 }
9451 else
9452 vim_beep();
9453}
9454
9455 static void
9456ins_up(startcol)
9457 int startcol; /* when TRUE move to Insstart.col */
9458{
9459 pos_T tpos;
9460 linenr_T old_topline = curwin->w_topline;
9461#ifdef FEAT_DIFF
9462 int old_topfill = curwin->w_topfill;
9463#endif
9464
9465 undisplay_dollar();
9466 tpos = curwin->w_cursor;
9467 if (cursor_up(1L, TRUE) == OK)
9468 {
9469 if (startcol)
9470 coladvance(getvcol_nolist(&Insstart));
9471 if (old_topline != curwin->w_topline
9472#ifdef FEAT_DIFF
9473 || old_topfill != curwin->w_topfill
9474#endif
9475 )
9476 redraw_later(VALID);
9477 start_arrow(&tpos);
9478#ifdef FEAT_CINDENT
9479 can_cindent = TRUE;
9480#endif
9481 }
9482 else
9483 vim_beep();
9484}
9485
9486 static void
9487ins_pageup()
9488{
9489 pos_T tpos;
9490
9491 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009492
9493#ifdef FEAT_WINDOWS
9494 if (mod_mask & MOD_MASK_CTRL)
9495 {
9496 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009497 if (first_tabpage->tp_next != NULL)
9498 {
9499 start_arrow(&curwin->w_cursor);
9500 goto_tabpage(-1);
9501 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009502 return;
9503 }
9504#endif
9505
Bram Moolenaar071d4272004-06-13 20:20:40 +00009506 tpos = curwin->w_cursor;
9507 if (onepage(BACKWARD, 1L) == OK)
9508 {
9509 start_arrow(&tpos);
9510#ifdef FEAT_CINDENT
9511 can_cindent = TRUE;
9512#endif
9513 }
9514 else
9515 vim_beep();
9516}
9517
9518 static void
9519ins_down(startcol)
9520 int startcol; /* when TRUE move to Insstart.col */
9521{
9522 pos_T tpos;
9523 linenr_T old_topline = curwin->w_topline;
9524#ifdef FEAT_DIFF
9525 int old_topfill = curwin->w_topfill;
9526#endif
9527
9528 undisplay_dollar();
9529 tpos = curwin->w_cursor;
9530 if (cursor_down(1L, TRUE) == OK)
9531 {
9532 if (startcol)
9533 coladvance(getvcol_nolist(&Insstart));
9534 if (old_topline != curwin->w_topline
9535#ifdef FEAT_DIFF
9536 || old_topfill != curwin->w_topfill
9537#endif
9538 )
9539 redraw_later(VALID);
9540 start_arrow(&tpos);
9541#ifdef FEAT_CINDENT
9542 can_cindent = TRUE;
9543#endif
9544 }
9545 else
9546 vim_beep();
9547}
9548
9549 static void
9550ins_pagedown()
9551{
9552 pos_T tpos;
9553
9554 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009555
9556#ifdef FEAT_WINDOWS
9557 if (mod_mask & MOD_MASK_CTRL)
9558 {
9559 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009560 if (first_tabpage->tp_next != NULL)
9561 {
9562 start_arrow(&curwin->w_cursor);
9563 goto_tabpage(0);
9564 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009565 return;
9566 }
9567#endif
9568
Bram Moolenaar071d4272004-06-13 20:20:40 +00009569 tpos = curwin->w_cursor;
9570 if (onepage(FORWARD, 1L) == OK)
9571 {
9572 start_arrow(&tpos);
9573#ifdef FEAT_CINDENT
9574 can_cindent = TRUE;
9575#endif
9576 }
9577 else
9578 vim_beep();
9579}
9580
9581#ifdef FEAT_DND
9582 static void
9583ins_drop()
9584{
9585 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9586}
9587#endif
9588
9589/*
9590 * Handle TAB in Insert or Replace mode.
9591 * Return TRUE when the TAB needs to be inserted like a normal character.
9592 */
9593 static int
9594ins_tab()
9595{
9596 int ind;
9597 int i;
9598 int temp;
9599
9600 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9601 Insstart_blank_vcol = get_nolist_virtcol();
9602 if (echeck_abbr(TAB + ABBR_OFF))
9603 return FALSE;
9604
9605 ind = inindent(0);
9606#ifdef FEAT_CINDENT
9607 if (ind)
9608 can_cindent = FALSE;
9609#endif
9610
9611 /*
9612 * When nothing special, insert TAB like a normal character
9613 */
9614 if (!curbuf->b_p_et
Bram Moolenaar14f24742012-08-08 18:01:05 +02009615 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value())
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009616 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009617 return TRUE;
9618
9619 if (stop_arrow() == FAIL)
9620 return TRUE;
9621
9622 did_ai = FALSE;
9623#ifdef FEAT_SMARTINDENT
9624 did_si = FALSE;
9625 can_si = FALSE;
9626 can_si_back = FALSE;
9627#endif
9628 AppendToRedobuff((char_u *)"\t");
9629
9630 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar14f24742012-08-08 18:01:05 +02009631 temp = (int)get_sw_value();
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009632 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9633 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634 else /* otherwise use 'tabstop' */
9635 temp = (int)curbuf->b_p_ts;
9636 temp -= get_nolist_virtcol() % temp;
9637
9638 /*
9639 * Insert the first space with ins_char(). It will delete one char in
9640 * replace mode. Insert the rest with ins_str(); it will not delete any
9641 * chars. For VREPLACE mode, we use ins_char() for all characters.
9642 */
9643 ins_char(' ');
9644 while (--temp > 0)
9645 {
9646#ifdef FEAT_VREPLACE
9647 if (State & VREPLACE_FLAG)
9648 ins_char(' ');
9649 else
9650#endif
9651 {
9652 ins_str((char_u *)" ");
9653 if (State & REPLACE_FLAG) /* no char replaced */
9654 replace_push(NUL);
9655 }
9656 }
9657
9658 /*
9659 * When 'expandtab' not set: Replace spaces by TABs where possible.
9660 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009661 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 {
9663 char_u *ptr;
9664#ifdef FEAT_VREPLACE
9665 char_u *saved_line = NULL; /* init for GCC */
9666 pos_T pos;
9667#endif
9668 pos_T fpos;
9669 pos_T *cursor;
9670 colnr_T want_vcol, vcol;
9671 int change_col = -1;
9672 int save_list = curwin->w_p_list;
9673
9674 /*
9675 * Get the current line. For VREPLACE mode, don't make real changes
9676 * yet, just work on a copy of the line.
9677 */
9678#ifdef FEAT_VREPLACE
9679 if (State & VREPLACE_FLAG)
9680 {
9681 pos = curwin->w_cursor;
9682 cursor = &pos;
9683 saved_line = vim_strsave(ml_get_curline());
9684 if (saved_line == NULL)
9685 return FALSE;
9686 ptr = saved_line + pos.col;
9687 }
9688 else
9689#endif
9690 {
9691 ptr = ml_get_cursor();
9692 cursor = &curwin->w_cursor;
9693 }
9694
9695 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9696 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9697 curwin->w_p_list = FALSE;
9698
9699 /* Find first white before the cursor */
9700 fpos = curwin->w_cursor;
9701 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9702 {
9703 --fpos.col;
9704 --ptr;
9705 }
9706
9707 /* In Replace mode, don't change characters before the insert point. */
9708 if ((State & REPLACE_FLAG)
9709 && fpos.lnum == Insstart.lnum
9710 && fpos.col < Insstart.col)
9711 {
9712 ptr += Insstart.col - fpos.col;
9713 fpos.col = Insstart.col;
9714 }
9715
9716 /* compute virtual column numbers of first white and cursor */
9717 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9718 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9719
9720 /* Use as many TABs as possible. Beware of 'showbreak' and
9721 * 'linebreak' adding extra virtual columns. */
9722 while (vim_iswhite(*ptr))
9723 {
9724 i = lbr_chartabsize((char_u *)"\t", vcol);
9725 if (vcol + i > want_vcol)
9726 break;
9727 if (*ptr != TAB)
9728 {
9729 *ptr = TAB;
9730 if (change_col < 0)
9731 {
9732 change_col = fpos.col; /* Column of first change */
9733 /* May have to adjust Insstart */
9734 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9735 Insstart.col = fpos.col;
9736 }
9737 }
9738 ++fpos.col;
9739 ++ptr;
9740 vcol += i;
9741 }
9742
9743 if (change_col >= 0)
9744 {
9745 int repl_off = 0;
9746
9747 /* Skip over the spaces we need. */
9748 while (vcol < want_vcol && *ptr == ' ')
9749 {
9750 vcol += lbr_chartabsize(ptr, vcol);
9751 ++ptr;
9752 ++repl_off;
9753 }
9754 if (vcol > want_vcol)
9755 {
9756 /* Must have a char with 'showbreak' just before it. */
9757 --ptr;
9758 --repl_off;
9759 }
9760 fpos.col += repl_off;
9761
9762 /* Delete following spaces. */
9763 i = cursor->col - fpos.col;
9764 if (i > 0)
9765 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009766 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 /* correct replace stack. */
9768 if ((State & REPLACE_FLAG)
9769#ifdef FEAT_VREPLACE
9770 && !(State & VREPLACE_FLAG)
9771#endif
9772 )
9773 for (temp = i; --temp >= 0; )
9774 replace_join(repl_off);
9775 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009776#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009777 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009778 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009779 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009780 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9781 (char_u *)"\t", 1);
9782 }
9783#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 cursor->col -= i;
9785
9786#ifdef FEAT_VREPLACE
9787 /*
9788 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9789 * backspacing over the changed spacing and then inserting the new
9790 * spacing.
9791 */
9792 if (State & VREPLACE_FLAG)
9793 {
9794 /* Backspace from real cursor to change_col */
9795 backspace_until_column(change_col);
9796
9797 /* Insert each char in saved_line from changed_col to
9798 * ptr-cursor */
9799 ins_bytes_len(saved_line + change_col,
9800 cursor->col - change_col);
9801 }
9802#endif
9803 }
9804
9805#ifdef FEAT_VREPLACE
9806 if (State & VREPLACE_FLAG)
9807 vim_free(saved_line);
9808#endif
9809 curwin->w_p_list = save_list;
9810 }
9811
9812 return FALSE;
9813}
9814
9815/*
9816 * Handle CR or NL in insert mode.
9817 * Return TRUE when out of memory or can't undo.
9818 */
9819 static int
9820ins_eol(c)
9821 int c;
9822{
9823 int i;
9824
9825 if (echeck_abbr(c + ABBR_OFF))
9826 return FALSE;
9827 if (stop_arrow() == FAIL)
9828 return TRUE;
9829 undisplay_dollar();
9830
9831 /*
9832 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9833 * character under the cursor. Only push a NUL on the replace stack,
9834 * nothing to put back when the NL is deleted.
9835 */
9836 if ((State & REPLACE_FLAG)
9837#ifdef FEAT_VREPLACE
9838 && !(State & VREPLACE_FLAG)
9839#endif
9840 )
9841 replace_push(NUL);
9842
9843 /*
9844 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9845 * replacing the next line, so we push all of the characters left on the
9846 * line onto the replace stack. This is not done here though, it is done
9847 * in open_line().
9848 */
9849
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009850#ifdef FEAT_VIRTUALEDIT
9851 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9852 * CTRL-O). */
9853 if (virtual_active() && curwin->w_cursor.coladd > 0)
9854 coladvance(getviscol());
9855#endif
9856
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857#ifdef FEAT_RIGHTLEFT
9858# ifdef FEAT_FKMAP
9859 if (p_altkeymap && p_fkmap)
9860 fkmap(NL);
9861# endif
9862 /* NL in reverse insert will always start in the end of
9863 * current line. */
9864 if (revins_on)
9865 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9866#endif
9867
9868 AppendToRedobuff(NL_STR);
9869 i = open_line(FORWARD,
9870#ifdef FEAT_COMMENTS
9871 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9872#endif
9873 0, old_indent);
9874 old_indent = 0;
9875#ifdef FEAT_CINDENT
9876 can_cindent = TRUE;
9877#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009878#ifdef FEAT_FOLDING
9879 /* When inserting a line the cursor line must never be in a closed fold. */
9880 foldOpenCursor();
9881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882
9883 return (!i);
9884}
9885
9886#ifdef FEAT_DIGRAPHS
9887/*
9888 * Handle digraph in insert mode.
9889 * Returns character still to be inserted, or NUL when nothing remaining to be
9890 * done.
9891 */
9892 static int
9893ins_digraph()
9894{
9895 int c;
9896 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009897 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898
9899 pc_status = PC_STATUS_UNSET;
9900 if (redrawing() && !char_avail())
9901 {
9902 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009903 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009904
9905 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009906 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907#ifdef FEAT_CMDL_INFO
9908 add_to_showcmd_c(Ctrl_K);
9909#endif
9910 }
9911
9912#ifdef USE_ON_FLY_SCROLL
9913 dont_scroll = TRUE; /* disallow scrolling here */
9914#endif
9915
9916 /* don't map the digraph chars. This also prevents the
9917 * mode message to be deleted when ESC is hit */
9918 ++no_mapping;
9919 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009920 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009921 --no_mapping;
9922 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009923 if (did_putchar)
9924 /* when the line fits in 'columns' the '?' is at the start of the next
9925 * line and will not be removed by the redraw */
9926 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009927
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928 if (IS_SPECIAL(c) || mod_mask) /* special key */
9929 {
9930#ifdef FEAT_CMDL_INFO
9931 clear_showcmd();
9932#endif
9933 insert_special(c, TRUE, FALSE);
9934 return NUL;
9935 }
9936 if (c != ESC)
9937 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009938 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939 if (redrawing() && !char_avail())
9940 {
9941 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009942 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943
9944 if (char2cells(c) == 1)
9945 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009946 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009947 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009948 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009949 }
9950#ifdef FEAT_CMDL_INFO
9951 add_to_showcmd_c(c);
9952#endif
9953 }
9954 ++no_mapping;
9955 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009956 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009957 --no_mapping;
9958 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009959 if (did_putchar)
9960 /* when the line fits in 'columns' the '?' is at the start of the
9961 * next line and will not be removed by a redraw */
9962 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963 if (cc != ESC)
9964 {
9965 AppendToRedobuff((char_u *)CTRL_V_STR);
9966 c = getdigraph(c, cc, TRUE);
9967#ifdef FEAT_CMDL_INFO
9968 clear_showcmd();
9969#endif
9970 return c;
9971 }
9972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009973#ifdef FEAT_CMDL_INFO
9974 clear_showcmd();
9975#endif
9976 return NUL;
9977}
9978#endif
9979
9980/*
9981 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9982 * Returns the char to be inserted, or NUL if none found.
9983 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02009984 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985ins_copychar(lnum)
9986 linenr_T lnum;
9987{
9988 int c;
9989 int temp;
9990 char_u *ptr, *prev_ptr;
9991
9992 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9993 {
9994 vim_beep();
9995 return NUL;
9996 }
9997
9998 /* try to advance to the cursor column */
9999 temp = 0;
10000 ptr = ml_get(lnum);
10001 prev_ptr = ptr;
10002 validate_virtcol();
10003 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10004 {
10005 prev_ptr = ptr;
10006 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
10007 }
10008 if ((colnr_T)temp > curwin->w_virtcol)
10009 ptr = prev_ptr;
10010
10011#ifdef FEAT_MBYTE
10012 c = (*mb_ptr2char)(ptr);
10013#else
10014 c = *ptr;
10015#endif
10016 if (c == NUL)
10017 vim_beep();
10018 return c;
10019}
10020
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010021/*
10022 * CTRL-Y or CTRL-E typed in Insert mode.
10023 */
10024 static int
10025ins_ctrl_ey(tc)
10026 int tc;
10027{
10028 int c = tc;
10029
10030#ifdef FEAT_INS_EXPAND
10031 if (ctrl_x_mode == CTRL_X_SCROLL)
10032 {
10033 if (c == Ctrl_Y)
10034 scrolldown_clamp();
10035 else
10036 scrollup_clamp();
10037 redraw_later(VALID);
10038 }
10039 else
10040#endif
10041 {
10042 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10043 if (c != NUL)
10044 {
10045 long tw_save;
10046
10047 /* The character must be taken literally, insert like it
10048 * was typed after a CTRL-V, and pretend 'textwidth'
10049 * wasn't set. Digits, 'o' and 'x' are special after a
10050 * CTRL-V, don't use it for these. */
10051 if (c < 256 && !isalnum(c))
10052 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10053 tw_save = curbuf->b_p_tw;
10054 curbuf->b_p_tw = -1;
10055 insert_special(c, TRUE, FALSE);
10056 curbuf->b_p_tw = tw_save;
10057#ifdef FEAT_RIGHTLEFT
10058 revins_chars++;
10059 revins_legal++;
10060#endif
10061 c = Ctrl_V; /* pretend CTRL-V is last character */
10062 auto_format(FALSE, TRUE);
10063 }
10064 }
10065 return c;
10066}
10067
Bram Moolenaar071d4272004-06-13 20:20:40 +000010068#ifdef FEAT_SMARTINDENT
10069/*
10070 * Try to do some very smart auto-indenting.
10071 * Used when inserting a "normal" character.
10072 */
10073 static void
10074ins_try_si(c)
10075 int c;
10076{
10077 pos_T *pos, old_pos;
10078 char_u *ptr;
10079 int i;
10080 int temp;
10081
10082 /*
10083 * do some very smart indenting when entering '{' or '}'
10084 */
10085 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10086 {
10087 /*
10088 * for '}' set indent equal to indent of line containing matching '{'
10089 */
10090 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10091 {
10092 old_pos = curwin->w_cursor;
10093 /*
10094 * If the matching '{' has a ')' immediately before it (ignoring
10095 * white-space), then line up with the start of the line
10096 * containing the matching '(' if there is one. This handles the
10097 * case where an "if (..\n..) {" statement continues over multiple
10098 * lines -- webb
10099 */
10100 ptr = ml_get(pos->lnum);
10101 i = pos->col;
10102 if (i > 0) /* skip blanks before '{' */
10103 while (--i > 0 && vim_iswhite(ptr[i]))
10104 ;
10105 curwin->w_cursor.lnum = pos->lnum;
10106 curwin->w_cursor.col = i;
10107 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10108 curwin->w_cursor = *pos;
10109 i = get_indent();
10110 curwin->w_cursor = old_pos;
10111#ifdef FEAT_VREPLACE
10112 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010113 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010114 else
10115#endif
10116 (void)set_indent(i, SIN_CHANGED);
10117 }
10118 else if (curwin->w_cursor.col > 0)
10119 {
10120 /*
10121 * when inserting '{' after "O" reduce indent, but not
10122 * more than indent of previous line
10123 */
10124 temp = TRUE;
10125 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10126 {
10127 old_pos = curwin->w_cursor;
10128 i = get_indent();
10129 while (curwin->w_cursor.lnum > 1)
10130 {
10131 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10132
10133 /* ignore empty lines and lines starting with '#'. */
10134 if (*ptr != '#' && *ptr != NUL)
10135 break;
10136 }
10137 if (get_indent() >= i)
10138 temp = FALSE;
10139 curwin->w_cursor = old_pos;
10140 }
10141 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010142 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010143 }
10144 }
10145
10146 /*
10147 * set indent of '#' always to 0
10148 */
10149 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10150 {
10151 /* remember current indent for next line */
10152 old_indent = get_indent();
10153 (void)set_indent(0, SIN_CHANGED);
10154 }
10155
10156 /* Adjust ai_col, the char at this position can be deleted. */
10157 if (ai_col > curwin->w_cursor.col)
10158 ai_col = curwin->w_cursor.col;
10159}
10160#endif
10161
10162/*
10163 * Get the value that w_virtcol would have when 'list' is off.
10164 * Unless 'cpo' contains the 'L' flag.
10165 */
10166 static colnr_T
10167get_nolist_virtcol()
10168{
10169 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10170 return getvcol_nolist(&curwin->w_cursor);
10171 validate_virtcol();
10172 return curwin->w_virtcol;
10173}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010174
10175#ifdef FEAT_AUTOCMD
10176/*
10177 * Handle the InsertCharPre autocommand.
10178 * "c" is the character that was typed.
10179 * Return a pointer to allocated memory with the replacement string.
10180 * Return NULL to continue inserting "c".
10181 */
10182 static char_u *
10183do_insert_char_pre(c)
10184 int c;
10185{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010186 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010187 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010188
10189 /* Return quickly when there is nothing to do. */
10190 if (!has_insertcharpre())
10191 return NULL;
10192
Bram Moolenaar704984a2012-06-01 14:57:51 +020010193#ifdef FEAT_MBYTE
10194 if (has_mbyte)
10195 buf[(*mb_char2bytes)(c, buf)] = NUL;
10196 else
10197#endif
10198 {
10199 buf[0] = c;
10200 buf[1] = NUL;
10201 }
10202
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010203 /* Lock the text to avoid weird things from happening. */
10204 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010205 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010206
Bram Moolenaar704984a2012-06-01 14:57:51 +020010207 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010208 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010209 {
10210 /* Get the value of v:char. It may be empty or more than one
10211 * character. Only use it when changed, otherwise continue with the
10212 * original character to avoid breaking autoindent. */
10213 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10214 res = vim_strsave(get_vim_var_str(VV_CHAR));
10215 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010216
10217 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10218 --textlock;
10219
10220 return res;
10221}
10222#endif