blob: 5d609519ac508b447cfa66c07791fddeceed4c61 [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 Moolenaar186628f2013-03-19 13:33:23 +01001596#ifdef FEAT_AUTOCMD
1597 /* Trigger TextChangedI if b_changedtick differs. */
1598 if (!ready && has_textchangedI()
1599 && last_changedtick != curbuf->b_changedtick
1600# ifdef FEAT_INS_EXPAND
1601 && !pum_visible()
1602# endif
1603 )
1604 {
1605 if (last_changedtick_buf == curbuf)
1606 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1607 last_changedtick_buf = curbuf;
1608 last_changedtick = curbuf->b_changedtick;
1609 }
1610#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001611 if (must_redraw)
1612 update_screen(0);
1613 else if (clear_cmdline || redraw_cmdline)
1614 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001615# if defined(FEAT_CONCEAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001616 if ((conceal_update_lines
1617 && (conceal_old_cursor_line != conceal_new_cursor_line
1618 || conceal_cursor_line(curwin)))
1619 || need_cursor_line_redraw)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001620 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001621 if (conceal_old_cursor_line != conceal_new_cursor_line)
1622 update_single_line(curwin, conceal_old_cursor_line);
1623 update_single_line(curwin, conceal_new_cursor_line == 0
1624 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001625 curwin->w_valid &= ~VALID_CROW;
1626 }
1627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 showruler(FALSE);
1629 setcursor();
1630 emsg_on_display = FALSE; /* may remove error message now */
1631 }
1632}
1633
1634/*
1635 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1636 */
1637 static void
1638ins_ctrl_v()
1639{
1640 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001641 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
1643 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001644 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001647 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001649 did_putchar = TRUE;
1650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1652
1653#ifdef FEAT_CMDL_INFO
1654 add_to_showcmd_c(Ctrl_V);
1655#endif
1656
1657 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001658 if (did_putchar)
1659 /* when the line fits in 'columns' the '^' is at the start of the next
1660 * line and will not removed by the redraw */
1661 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662#ifdef FEAT_CMDL_INFO
1663 clear_showcmd();
1664#endif
1665 insert_special(c, FALSE, TRUE);
1666#ifdef FEAT_RIGHTLEFT
1667 revins_chars++;
1668 revins_legal++;
1669#endif
1670}
1671
1672/*
1673 * Put a character directly onto the screen. It's not stored in a buffer.
1674 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1675 */
1676static int pc_status;
1677#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1678#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1679#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1680#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001682static int pc_attr;
1683static int pc_row;
1684static int pc_col;
1685
1686 void
1687edit_putchar(c, highlight)
1688 int c;
1689 int highlight;
1690{
1691 int attr;
1692
1693 if (ScreenLines != NULL)
1694 {
1695 update_topline(); /* just in case w_topline isn't valid */
1696 validate_cursor();
1697 if (highlight)
1698 attr = hl_attr(HLF_8);
1699 else
1700 attr = 0;
1701 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1702 pc_col = W_WINCOL(curwin);
1703#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1704 pc_status = PC_STATUS_UNSET;
1705#endif
1706#ifdef FEAT_RIGHTLEFT
1707 if (curwin->w_p_rl)
1708 {
1709 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1710# ifdef FEAT_MBYTE
1711 if (has_mbyte)
1712 {
1713 int fix_col = mb_fix_col(pc_col, pc_row);
1714
1715 if (fix_col != pc_col)
1716 {
1717 screen_putchar(' ', pc_row, fix_col, attr);
1718 --curwin->w_wcol;
1719 pc_status = PC_STATUS_RIGHT;
1720 }
1721 }
1722# endif
1723 }
1724 else
1725#endif
1726 {
1727 pc_col += curwin->w_wcol;
1728#ifdef FEAT_MBYTE
1729 if (mb_lefthalve(pc_row, pc_col))
1730 pc_status = PC_STATUS_LEFT;
1731#endif
1732 }
1733
1734 /* save the character to be able to put it back */
1735#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1736 if (pc_status == PC_STATUS_UNSET)
1737#endif
1738 {
1739 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1740 pc_status = PC_STATUS_SET;
1741 }
1742 screen_putchar(c, pc_row, pc_col, attr);
1743 }
1744}
1745
1746/*
1747 * Undo the previous edit_putchar().
1748 */
1749 void
1750edit_unputchar()
1751{
1752 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1753 {
1754#if defined(FEAT_MBYTE)
1755 if (pc_status == PC_STATUS_RIGHT)
1756 ++curwin->w_wcol;
1757 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1758 redrawWinline(curwin->w_cursor.lnum, FALSE);
1759 else
1760#endif
1761 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1762 }
1763}
1764
1765/*
1766 * Called when p_dollar is set: display a '$' at the end of the changed text
1767 * Only works when cursor is in the line that changes.
1768 */
1769 void
1770display_dollar(col)
1771 colnr_T col;
1772{
1773 colnr_T save_col;
1774
1775 if (!redrawing())
1776 return;
1777
1778 cursor_off();
1779 save_col = curwin->w_cursor.col;
1780 curwin->w_cursor.col = col;
1781#ifdef FEAT_MBYTE
1782 if (has_mbyte)
1783 {
1784 char_u *p;
1785
1786 /* If on the last byte of a multi-byte move to the first byte. */
1787 p = ml_get_curline();
1788 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1789 }
1790#endif
1791 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1792 if (curwin->w_wcol < W_WIDTH(curwin))
1793 {
1794 edit_putchar('$', FALSE);
1795 dollar_vcol = curwin->w_virtcol;
1796 }
1797 curwin->w_cursor.col = save_col;
1798}
1799
1800/*
1801 * Call this function before moving the cursor from the normal insert position
1802 * in insert mode.
1803 */
1804 static void
1805undisplay_dollar()
1806{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001807 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001809 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 redrawWinline(curwin->w_cursor.lnum, FALSE);
1811 }
1812}
1813
1814/*
1815 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1816 * Keep the cursor on the same character.
1817 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1818 * type == INDENT_DEC decrease indent (for CTRL-D)
1819 * type == INDENT_SET set indent to "amount"
1820 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1821 */
1822 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001823change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824 int type;
1825 int amount;
1826 int round;
1827 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001828 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829{
1830 int vcol;
1831 int last_vcol;
1832 int insstart_less; /* reduction for Insstart.col */
1833 int new_cursor_col;
1834 int i;
1835 char_u *ptr;
1836 int save_p_list;
1837 int start_col;
1838 colnr_T vc;
1839#ifdef FEAT_VREPLACE
1840 colnr_T orig_col = 0; /* init for GCC */
1841 char_u *new_line, *orig_line = NULL; /* init for GCC */
1842
1843 /* VREPLACE mode needs to know what the line was like before changing */
1844 if (State & VREPLACE_FLAG)
1845 {
1846 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1847 orig_col = curwin->w_cursor.col;
1848 }
1849#endif
1850
1851 /* for the following tricks we don't want list mode */
1852 save_p_list = curwin->w_p_list;
1853 curwin->w_p_list = FALSE;
1854 vc = getvcol_nolist(&curwin->w_cursor);
1855 vcol = vc;
1856
1857 /*
1858 * For Replace mode we need to fix the replace stack later, which is only
1859 * possible when the cursor is in the indent. Remember the number of
1860 * characters before the cursor if it's possible.
1861 */
1862 start_col = curwin->w_cursor.col;
1863
1864 /* determine offset from first non-blank */
1865 new_cursor_col = curwin->w_cursor.col;
1866 beginline(BL_WHITE);
1867 new_cursor_col -= curwin->w_cursor.col;
1868
1869 insstart_less = curwin->w_cursor.col;
1870
1871 /*
1872 * If the cursor is in the indent, compute how many screen columns the
1873 * cursor is to the left of the first non-blank.
1874 */
1875 if (new_cursor_col < 0)
1876 vcol = get_indent() - vcol;
1877
1878 if (new_cursor_col > 0) /* can't fix replace stack */
1879 start_col = -1;
1880
1881 /*
1882 * Set the new indent. The cursor will be put on the first non-blank.
1883 */
1884 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001885 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001886 else
1887 {
1888#ifdef FEAT_VREPLACE
1889 int save_State = State;
1890
1891 /* Avoid being called recursively. */
1892 if (State & VREPLACE_FLAG)
1893 State = INSERT;
1894#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001895 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896#ifdef FEAT_VREPLACE
1897 State = save_State;
1898#endif
1899 }
1900 insstart_less -= curwin->w_cursor.col;
1901
1902 /*
1903 * Try to put cursor on same character.
1904 * If the cursor is at or after the first non-blank in the line,
1905 * compute the cursor column relative to the column of the first
1906 * non-blank character.
1907 * If we are not in insert mode, leave the cursor on the first non-blank.
1908 * If the cursor is before the first non-blank, position it relative
1909 * to the first non-blank, counted in screen columns.
1910 */
1911 if (new_cursor_col >= 0)
1912 {
1913 /*
1914 * When changing the indent while the cursor is touching it, reset
1915 * Insstart_col to 0.
1916 */
1917 if (new_cursor_col == 0)
1918 insstart_less = MAXCOL;
1919 new_cursor_col += curwin->w_cursor.col;
1920 }
1921 else if (!(State & INSERT))
1922 new_cursor_col = curwin->w_cursor.col;
1923 else
1924 {
1925 /*
1926 * Compute the screen column where the cursor should be.
1927 */
1928 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001929 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930
1931 /*
1932 * Advance the cursor until we reach the right screen column.
1933 */
1934 vcol = last_vcol = 0;
1935 new_cursor_col = -1;
1936 ptr = ml_get_curline();
1937 while (vcol <= (int)curwin->w_virtcol)
1938 {
1939 last_vcol = vcol;
1940#ifdef FEAT_MBYTE
1941 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001942 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 else
1944#endif
1945 ++new_cursor_col;
1946 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1947 }
1948 vcol = last_vcol;
1949
1950 /*
1951 * May need to insert spaces to be able to position the cursor on
1952 * the right screen column.
1953 */
1954 if (vcol != (int)curwin->w_virtcol)
1955 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001956 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001958 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 if (ptr != NULL)
1960 {
1961 new_cursor_col += i;
1962 ptr[i] = NUL;
1963 while (--i >= 0)
1964 ptr[i] = ' ';
1965 ins_str(ptr);
1966 vim_free(ptr);
1967 }
1968 }
1969
1970 /*
1971 * When changing the indent while the cursor is in it, reset
1972 * Insstart_col to 0.
1973 */
1974 insstart_less = MAXCOL;
1975 }
1976
1977 curwin->w_p_list = save_p_list;
1978
1979 if (new_cursor_col <= 0)
1980 curwin->w_cursor.col = 0;
1981 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001982 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001983 curwin->w_set_curswant = TRUE;
1984 changed_cline_bef_curs();
1985
1986 /*
1987 * May have to adjust the start of the insert.
1988 */
1989 if (State & INSERT)
1990 {
1991 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1992 {
1993 if ((int)Insstart.col <= insstart_less)
1994 Insstart.col = 0;
1995 else
1996 Insstart.col -= insstart_less;
1997 }
1998 if ((int)ai_col <= insstart_less)
1999 ai_col = 0;
2000 else
2001 ai_col -= insstart_less;
2002 }
2003
2004 /*
2005 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2006 * If the number of characters before the cursor decreased, need to pop a
2007 * few characters from the replace stack.
2008 * If the number of characters before the cursor increased, need to push a
2009 * few NULs onto the replace stack.
2010 */
2011 if (REPLACE_NORMAL(State) && start_col >= 0)
2012 {
2013 while (start_col > (int)curwin->w_cursor.col)
2014 {
2015 replace_join(0); /* remove a NUL from the replace stack */
2016 --start_col;
2017 }
2018 while (start_col < (int)curwin->w_cursor.col || replaced)
2019 {
2020 replace_push(NUL);
2021 if (replaced)
2022 {
2023 replace_push(replaced);
2024 replaced = NUL;
2025 }
2026 ++start_col;
2027 }
2028 }
2029
2030#ifdef FEAT_VREPLACE
2031 /*
2032 * For VREPLACE mode, we also have to fix the replace stack. In this case
2033 * it is always possible because we backspace over the whole line and then
2034 * put it back again the way we wanted it.
2035 */
2036 if (State & VREPLACE_FLAG)
2037 {
2038 /* If orig_line didn't allocate, just return. At least we did the job,
2039 * even if you can't backspace. */
2040 if (orig_line == NULL)
2041 return;
2042
2043 /* Save new line */
2044 new_line = vim_strsave(ml_get_curline());
2045 if (new_line == NULL)
2046 return;
2047
2048 /* We only put back the new line up to the cursor */
2049 new_line[curwin->w_cursor.col] = NUL;
2050
2051 /* Put back original line */
2052 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2053 curwin->w_cursor.col = orig_col;
2054
2055 /* Backspace from cursor to start of line */
2056 backspace_until_column(0);
2057
2058 /* Insert new stuff into line again */
2059 ins_bytes(new_line);
2060
2061 vim_free(new_line);
2062 }
2063#endif
2064}
2065
2066/*
2067 * Truncate the space at the end of a line. This is to be used only in an
2068 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2069 * modes.
2070 */
2071 void
2072truncate_spaces(line)
2073 char_u *line;
2074{
2075 int i;
2076
2077 /* find start of trailing white space */
2078 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2079 {
2080 if (State & REPLACE_FLAG)
2081 replace_join(0); /* remove a NUL from the replace stack */
2082 }
2083 line[i + 1] = NUL;
2084}
2085
2086#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2087 || defined(FEAT_COMMENTS) || defined(PROTO)
2088/*
2089 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2090 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002091 * Will attempt not to go before "col" even when there is a composing
2092 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002093 */
2094 void
2095backspace_until_column(col)
2096 int col;
2097{
2098 while ((int)curwin->w_cursor.col > col)
2099 {
2100 curwin->w_cursor.col--;
2101 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002102 replace_do_bs(col);
2103 else if (!del_char_after_col(col))
2104 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106}
2107#endif
2108
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002109/*
2110 * Like del_char(), but make sure not to go before column "limit_col".
2111 * Only matters when there are composing characters.
2112 * Return TRUE when something was deleted.
2113 */
2114 static int
2115del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002116 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002117{
2118#ifdef FEAT_MBYTE
2119 if (enc_utf8 && limit_col >= 0)
2120 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002121 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002122
2123 /* Make sure the cursor is at the start of a character, but
2124 * skip forward again when going too far back because of a
2125 * composing character. */
2126 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002127 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002128 {
2129 int l = utf_ptr2len(ml_get_cursor());
2130
2131 if (l == 0) /* end of line */
2132 break;
2133 curwin->w_cursor.col += l;
2134 }
2135 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2136 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002137 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002138 }
2139 else
2140#endif
2141 (void)del_char(FALSE);
2142 return TRUE;
2143}
2144
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2146/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002147 * CTRL-X pressed in Insert mode.
2148 */
2149 static void
2150ins_ctrl_x()
2151{
2152 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2153 * CTRL-V works like CTRL-N */
2154 if (ctrl_x_mode != CTRL_X_CMDLINE)
2155 {
2156 /* if the next ^X<> won't ADD nothing, then reset
2157 * compl_cont_status */
2158 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002159 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002160 else
2161 compl_cont_status = 0;
2162 /* We're not sure which CTRL-X mode it will be yet */
2163 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2164 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2165 edit_submode_pre = NULL;
2166 showmode();
2167 }
2168}
2169
2170/*
2171 * Return TRUE if the 'dict' or 'tsr' option can be used.
2172 */
2173 static int
2174has_compl_option(dict_opt)
2175 int dict_opt;
2176{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002177 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002178# ifdef FEAT_SPELL
2179 && !curwin->w_p_spell
2180# endif
2181 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002182 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2183 {
2184 ctrl_x_mode = 0;
2185 edit_submode = NULL;
2186 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2187 : (char_u *)_("'thesaurus' option is empty"),
2188 hl_attr(HLF_E));
2189 if (emsg_silent == 0)
2190 {
2191 vim_beep();
2192 setcursor();
2193 out_flush();
2194 ui_delay(2000L, FALSE);
2195 }
2196 return FALSE;
2197 }
2198 return TRUE;
2199}
2200
2201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2203 * This depends on the current mode.
2204 */
2205 int
2206vim_is_ctrl_x_key(c)
2207 int c;
2208{
2209 /* Always allow ^R - let it's results then be checked */
2210 if (c == Ctrl_R)
2211 return TRUE;
2212
Bram Moolenaare3226be2005-12-18 22:10:00 +00002213 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002214 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002215 return TRUE;
2216
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 switch (ctrl_x_mode)
2218 {
2219 case 0: /* Not in any CTRL-X mode */
2220 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2221 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002222 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2224 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2225 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002226 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002227 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 case CTRL_X_SCROLL:
2229 return (c == Ctrl_Y || c == Ctrl_E);
2230 case CTRL_X_WHOLE_LINE:
2231 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2232 case CTRL_X_FILES:
2233 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2234 case CTRL_X_DICTIONARY:
2235 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2236 case CTRL_X_THESAURUS:
2237 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2238 case CTRL_X_TAGS:
2239 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2240#ifdef FEAT_FIND_ID
2241 case CTRL_X_PATH_PATTERNS:
2242 return (c == Ctrl_P || c == Ctrl_N);
2243 case CTRL_X_PATH_DEFINES:
2244 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2245#endif
2246 case CTRL_X_CMDLINE:
2247 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2248 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002249#ifdef FEAT_COMPL_FUNC
2250 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002251 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002252 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002253 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002254#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002255 case CTRL_X_SPELL:
2256 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002257 }
2258 EMSG(_(e_internal));
2259 return FALSE;
2260}
2261
2262/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002263 * Return TRUE when character "c" is part of the item currently being
2264 * completed. Used to decide whether to abandon complete mode when the menu
2265 * is visible.
2266 */
2267 static int
2268ins_compl_accept_char(c)
2269 int c;
2270{
2271 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2272 /* When expanding an identifier only accept identifier chars. */
2273 return vim_isIDc(c);
2274
2275 switch (ctrl_x_mode)
2276 {
2277 case CTRL_X_FILES:
2278 /* When expanding file name only accept file name chars. But not
2279 * path separators, so that "proto/<Tab>" expands files in
2280 * "proto", not "proto/" as a whole */
2281 return vim_isfilec(c) && !vim_ispathsep(c);
2282
2283 case CTRL_X_CMDLINE:
2284 case CTRL_X_OMNI:
2285 /* Command line and Omni completion can work with just about any
2286 * printable character, but do stop at white space. */
2287 return vim_isprintc(c) && !vim_iswhite(c);
2288
2289 case CTRL_X_WHOLE_LINE:
2290 /* For while line completion a space can be part of the line. */
2291 return vim_isprintc(c);
2292 }
2293 return vim_iswordc(c);
2294}
2295
2296/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002297 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002299 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300 * the rest of the word to be in -- webb
2301 */
2302 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002303ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 char_u *str;
2305 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002306 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002307 char_u *fname;
2308 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002309 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002311 char_u *p;
2312 int i, c;
2313 int actual_len; /* Take multi-byte characters */
2314 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002315 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002316 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 int has_lower = FALSE;
2318 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002320 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002322 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323
Bram Moolenaara2993e12007-08-12 14:38:46 +00002324 /* Find actual length of completion. */
2325#ifdef FEAT_MBYTE
2326 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002328 p = str;
2329 actual_len = 0;
2330 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002332 mb_ptr_adv(p);
2333 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 }
2335 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002336 else
2337#endif
2338 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339
Bram Moolenaara2993e12007-08-12 14:38:46 +00002340 /* Find actual length of original text. */
2341#ifdef FEAT_MBYTE
2342 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002343 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002344 p = compl_orig_text;
2345 actual_compl_length = 0;
2346 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002348 mb_ptr_adv(p);
2349 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350 }
2351 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002352 else
2353#endif
2354 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002356 /* "actual_len" may be smaller than "actual_compl_length" when using
2357 * thesaurus, only use the minimum when comparing. */
2358 min_len = actual_len < actual_compl_length
2359 ? actual_len : actual_compl_length;
2360
Bram Moolenaara2993e12007-08-12 14:38:46 +00002361 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002362 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002363 if (wca != NULL)
2364 {
2365 p = str;
2366 for (i = 0; i < actual_len; ++i)
2367#ifdef FEAT_MBYTE
2368 if (has_mbyte)
2369 wca[i] = mb_ptr2char_adv(&p);
2370 else
2371#endif
2372 wca[i] = *(p++);
2373
2374 /* Rule 1: Were any chars converted to lower? */
2375 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002376 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002377 {
2378#ifdef FEAT_MBYTE
2379 if (has_mbyte)
2380 c = mb_ptr2char_adv(&p);
2381 else
2382#endif
2383 c = *(p++);
2384 if (MB_ISLOWER(c))
2385 {
2386 has_lower = TRUE;
2387 if (MB_ISUPPER(wca[i]))
2388 {
2389 /* Rule 1 is satisfied. */
2390 for (i = actual_compl_length; i < actual_len; ++i)
2391 wca[i] = MB_TOLOWER(wca[i]);
2392 break;
2393 }
2394 }
2395 }
2396
2397 /*
2398 * Rule 2: No lower case, 2nd consecutive letter converted to
2399 * upper case.
2400 */
2401 if (!has_lower)
2402 {
2403 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002404 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002405 {
2406#ifdef FEAT_MBYTE
2407 if (has_mbyte)
2408 c = mb_ptr2char_adv(&p);
2409 else
2410#endif
2411 c = *(p++);
2412 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2413 {
2414 /* Rule 2 is satisfied. */
2415 for (i = actual_compl_length; i < actual_len; ++i)
2416 wca[i] = MB_TOUPPER(wca[i]);
2417 break;
2418 }
2419 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2420 }
2421 }
2422
2423 /* Copy the original case of the part we typed. */
2424 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002425 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002426 {
2427#ifdef FEAT_MBYTE
2428 if (has_mbyte)
2429 c = mb_ptr2char_adv(&p);
2430 else
2431#endif
2432 c = *(p++);
2433 if (MB_ISLOWER(c))
2434 wca[i] = MB_TOLOWER(wca[i]);
2435 else if (MB_ISUPPER(c))
2436 wca[i] = MB_TOUPPER(wca[i]);
2437 }
2438
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002439 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002440 * Generate encoding specific output from wide character array.
2441 * Multi-byte characters can occupy up to five bytes more than
2442 * ASCII characters, and we also need one byte for NUL, so stay
2443 * six bytes away from the edge of IObuff.
2444 */
2445 p = IObuff;
2446 i = 0;
2447 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2448#ifdef FEAT_MBYTE
2449 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002450 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002451 else
2452#endif
2453 *(p++) = wca[i++];
2454 *p = NUL;
2455
2456 vim_free(wca);
2457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002459 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2460 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002461 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002462 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463}
2464
2465/*
2466 * Add a match to the list of matches.
2467 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002468 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002469 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002471 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002472ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002473 char_u *str;
2474 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002475 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002477 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002478 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002479 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002480 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002482 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002483 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 ui_breakcheck();
2486 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002487 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488 if (len < 0)
2489 len = (int)STRLEN(str);
2490
2491 /*
2492 * If the same match is already present, don't add it.
2493 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002494 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002496 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 do
2498 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002499 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002500 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002501 && match->cp_str[len] == NUL)
2502 return NOTDONE;
2503 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002504 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 }
2506
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002507 /* Remove any popup menu before changing the list of matches. */
2508 ins_compl_del_pum();
2509
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 /*
2511 * Allocate a new match structure.
2512 * Copy the values to the new match structure.
2513 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002514 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002516 return FAIL;
2517 match->cp_number = -1;
2518 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002519 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002520 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 {
2522 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002523 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002525 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002526
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002528 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2530 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002531 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002532 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002533 && compl_curr_match->cp_fname != NULL
2534 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002535 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002536 else if (fname != NULL)
2537 {
2538 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002539 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002540 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002541 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002542 match->cp_fname = NULL;
2543 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002544
2545 if (cptext != NULL)
2546 {
2547 int i;
2548
2549 for (i = 0; i < CPT_COUNT; ++i)
2550 if (cptext[i] != NULL && *cptext[i] != NUL)
2551 match->cp_text[i] = vim_strsave(cptext[i]);
2552 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553
2554 /*
2555 * Link the new match structure in the list of matches.
2556 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002557 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002558 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 else if (dir == FORWARD)
2560 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002561 match->cp_next = compl_curr_match->cp_next;
2562 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002563 }
2564 else /* BACKWARD */
2565 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 match->cp_next = compl_curr_match;
2567 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002569 if (match->cp_next)
2570 match->cp_next->cp_prev = match;
2571 if (match->cp_prev)
2572 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002574 compl_first_match = match;
2575 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002577 /*
2578 * Find the longest common string if still doing that.
2579 */
2580 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2581 ins_compl_longest_match(match);
2582
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 return OK;
2584}
2585
2586/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002587 * Return TRUE if "str[len]" matches with match->cp_str, considering
2588 * match->cp_icase.
2589 */
2590 static int
2591ins_compl_equal(match, str, len)
2592 compl_T *match;
2593 char_u *str;
2594 int len;
2595{
2596 if (match->cp_icase)
2597 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2598 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2599}
2600
2601/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002602 * Reduce the longest common string for match "match".
2603 */
2604 static void
2605ins_compl_longest_match(match)
2606 compl_T *match;
2607{
2608 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002609 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002610 int had_match;
2611
2612 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002613 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002614 /* First match, use it as a whole. */
2615 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002616 if (compl_leader != NULL)
2617 {
2618 had_match = (curwin->w_cursor.col > compl_col);
2619 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002620 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002621 ins_redraw(FALSE);
2622
2623 /* When the match isn't there (to avoid matching itself) remove it
2624 * again after redrawing. */
2625 if (!had_match)
2626 ins_compl_delete();
2627 compl_used_match = FALSE;
2628 }
2629 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002630 else
2631 {
2632 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002633 p = compl_leader;
2634 s = match->cp_str;
2635 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002636 {
2637#ifdef FEAT_MBYTE
2638 if (has_mbyte)
2639 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002640 c1 = mb_ptr2char(p);
2641 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002642 }
2643 else
2644#endif
2645 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002646 c1 = *p;
2647 c2 = *s;
2648 }
2649 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2650 : (c1 != c2))
2651 break;
2652#ifdef FEAT_MBYTE
2653 if (has_mbyte)
2654 {
2655 mb_ptr_adv(p);
2656 mb_ptr_adv(s);
2657 }
2658 else
2659#endif
2660 {
2661 ++p;
2662 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002663 }
2664 }
2665
2666 if (*p != NUL)
2667 {
2668 /* Leader was shortened, need to change the inserted text. */
2669 *p = NUL;
2670 had_match = (curwin->w_cursor.col > compl_col);
2671 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002672 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002673 ins_redraw(FALSE);
2674
2675 /* When the match isn't there (to avoid matching itself) remove it
2676 * again after redrawing. */
2677 if (!had_match)
2678 ins_compl_delete();
2679 }
2680
2681 compl_used_match = FALSE;
2682 }
2683}
2684
2685/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002686 * Add an array of matches to the list of matches.
2687 * Frees matches[].
2688 */
2689 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002690ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 int num_matches;
2692 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002693 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694{
2695 int i;
2696 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002697 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698
Bram Moolenaar572cb562005-08-05 21:35:02 +00002699 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002700 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002701 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002703 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 FreeWild(num_matches, matches);
2705}
2706
2707/* Make the completion list cyclic.
2708 * Return the number of matches (excluding the original).
2709 */
2710 static int
2711ins_compl_make_cyclic()
2712{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002713 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002714 int count = 0;
2715
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002716 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 {
2718 /*
2719 * Find the end of the list.
2720 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002721 match = compl_first_match;
2722 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002723 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002724 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002725 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002726 ++count;
2727 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002728 match->cp_next = compl_first_match;
2729 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 }
2731 return count;
2732}
2733
Bram Moolenaara94bc432006-03-10 21:42:59 +00002734/*
2735 * Start completion for the complete() function.
2736 * "startcol" is where the matched text starts (1 is first column).
2737 * "list" is the list of matches.
2738 */
2739 void
2740set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002741 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002742 list_T *list;
2743{
2744 /* If already doing completions stop it. */
2745 if (ctrl_x_mode != 0)
2746 ins_compl_prep(' ');
2747 ins_compl_clear();
2748
2749 if (stop_arrow() == FAIL)
2750 return;
2751
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002752 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002753 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002754 startcol = curwin->w_cursor.col;
2755 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002756 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002757 /* compl_pattern doesn't need to be set */
2758 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2759 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002760 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002761 return;
2762
2763 /* Handle like dictionary completion. */
2764 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2765
2766 ins_compl_add_list(list);
2767 compl_matches = ins_compl_make_cyclic();
2768 compl_started = TRUE;
2769 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002770 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002771
2772 compl_curr_match = compl_first_match;
2773 ins_complete(Ctrl_N);
2774 out_flush();
2775}
2776
2777
Bram Moolenaar9372a112005-12-06 19:59:18 +00002778/* "compl_match_array" points the currently displayed list of entries in the
2779 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002780static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002781static int compl_match_arraysize;
2782
2783/*
2784 * Update the screen and when there is any scrolling remove the popup menu.
2785 */
2786 static void
2787ins_compl_upd_pum()
2788{
2789 int h;
2790
2791 if (compl_match_array != NULL)
2792 {
2793 h = curwin->w_cline_height;
2794 update_screen(0);
2795 if (h != curwin->w_cline_height)
2796 ins_compl_del_pum();
2797 }
2798}
2799
2800/*
2801 * Remove any popup menu.
2802 */
2803 static void
2804ins_compl_del_pum()
2805{
2806 if (compl_match_array != NULL)
2807 {
2808 pum_undisplay();
2809 vim_free(compl_match_array);
2810 compl_match_array = NULL;
2811 }
2812}
2813
2814/*
2815 * Return TRUE if the popup menu should be displayed.
2816 */
2817 static int
2818pum_wanted()
2819{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002820 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002821 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002822 return FALSE;
2823
2824 /* The display looks bad on a B&W display. */
2825 if (t_colors < 8
2826#ifdef FEAT_GUI
2827 && !gui.in_use
2828#endif
2829 )
2830 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002831 return TRUE;
2832}
2833
2834/*
2835 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002836 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002837 */
2838 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002839pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002840{
2841 compl_T *compl;
2842 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002843
2844 /* Don't display the popup menu if there are no matches or there is only
2845 * one (ignoring the original text). */
2846 compl = compl_first_match;
2847 i = 0;
2848 do
2849 {
2850 if (compl == NULL
2851 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2852 break;
2853 compl = compl->cp_next;
2854 } while (compl != compl_first_match);
2855
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002856 if (strstr((char *)p_cot, "menuone") != NULL)
2857 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002858 return (i >= 2);
2859}
2860
2861/*
2862 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002863 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002864 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002865 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002866ins_compl_show_pum()
2867{
2868 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002869 compl_T *shown_compl = NULL;
2870 int did_find_shown_match = FALSE;
2871 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002872 int i;
2873 int cur = -1;
2874 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002875 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002876
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002877 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002878 return;
2879
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002880#if defined(FEAT_EVAL)
2881 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2882 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2883#endif
2884
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002885 /* Update the screen before drawing the popup menu over it. */
2886 update_screen(0);
2887
2888 if (compl_match_array == NULL)
2889 {
2890 /* Need to build the popup menu list. */
2891 compl_match_arraysize = 0;
2892 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002893 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002894 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002895 do
2896 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002897 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2898 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002899 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002900 ++compl_match_arraysize;
2901 compl = compl->cp_next;
2902 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002903 if (compl_match_arraysize == 0)
2904 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002905 compl_match_array = (pumitem_T *)alloc_clear(
2906 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002907 * compl_match_arraysize));
2908 if (compl_match_array != NULL)
2909 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002910 /* If the current match is the original text don't find the first
2911 * match after it, don't highlight anything. */
2912 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2913 shown_match_ok = TRUE;
2914
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002915 i = 0;
2916 compl = compl_first_match;
2917 do
2918 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002919 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2920 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002921 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002922 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002923 if (!shown_match_ok)
2924 {
2925 if (compl == compl_shown_match || did_find_shown_match)
2926 {
2927 /* This item is the shown match or this is the
2928 * first displayed item after the shown match. */
2929 compl_shown_match = compl;
2930 did_find_shown_match = TRUE;
2931 shown_match_ok = TRUE;
2932 }
2933 else
2934 /* Remember this displayed match for when the
2935 * shown match is just below it. */
2936 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002937 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002938 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002939
2940 if (compl->cp_text[CPT_ABBR] != NULL)
2941 compl_match_array[i].pum_text =
2942 compl->cp_text[CPT_ABBR];
2943 else
2944 compl_match_array[i].pum_text = compl->cp_str;
2945 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2946 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2947 if (compl->cp_text[CPT_MENU] != NULL)
2948 compl_match_array[i++].pum_extra =
2949 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002950 else
2951 compl_match_array[i++].pum_extra = compl->cp_fname;
2952 }
2953
2954 if (compl == compl_shown_match)
2955 {
2956 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002957
2958 /* When the original text is the shown match don't set
2959 * compl_shown_match. */
2960 if (compl->cp_flags & ORIGINAL_TEXT)
2961 shown_match_ok = TRUE;
2962
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002963 if (!shown_match_ok && shown_compl != NULL)
2964 {
2965 /* The shown match isn't displayed, set it to the
2966 * previously displayed match. */
2967 compl_shown_match = shown_compl;
2968 shown_match_ok = TRUE;
2969 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002970 }
2971 compl = compl->cp_next;
2972 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002973
2974 if (!shown_match_ok) /* no displayed match at all */
2975 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002976 }
2977 }
2978 else
2979 {
2980 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002981 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002982 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2983 || compl_match_array[i].pum_text
2984 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002985 {
2986 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002987 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002988 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002989 }
2990
2991 if (compl_match_array != NULL)
2992 {
2993 /* Compute the screen column of the start of the completed text.
2994 * Use the cursor to get all wrapping and other settings right. */
2995 col = curwin->w_cursor.col;
2996 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002997 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002998 curwin->w_cursor.col = col;
2999 }
3000}
3001
Bram Moolenaar071d4272004-06-13 20:20:40 +00003002#define DICT_FIRST (1) /* use just first element in "dict" */
3003#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003004
Bram Moolenaar071d4272004-06-13 20:20:40 +00003005/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003006 * Add any identifiers that match the given pattern in the list of dictionary
3007 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003008 */
3009 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00003010ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
3011 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003013 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003014 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003016 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 char_u *ptr;
3018 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 char_u **files;
3021 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003022 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003023 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024
Bram Moolenaar0b238792006-03-02 22:49:12 +00003025 if (*dict == NUL)
3026 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003027#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003028 /* When 'dictionary' is empty and spell checking is enabled use
3029 * "spell". */
3030 if (!thesaurus && curwin->w_p_spell)
3031 dict = (char_u *)"spell";
3032 else
3033#endif
3034 return;
3035 }
3036
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003038 if (buf == NULL)
3039 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003040 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003041
Bram Moolenaar071d4272004-06-13 20:20:40 +00003042 /* If 'infercase' is set, don't use 'smartcase' here */
3043 save_p_scs = p_scs;
3044 if (curbuf->b_p_inf)
3045 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003046
3047 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3048 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003049 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003050 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3051 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003052 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003053 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003054
3055 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003056 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003057 len = STRLEN(pat_esc) + 10;
3058 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003059 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003060 {
3061 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003062 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003063 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003064 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003065 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3066 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003067 vim_free(ptr);
3068 }
3069 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003070 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003071 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003072 if (regmatch.regprog == NULL)
3073 goto theend;
3074 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003075
Bram Moolenaar071d4272004-06-13 20:20:40 +00003076 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3077 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003078 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
3080 /* copy one dictionary file name into buf */
3081 if (flags == DICT_EXACT)
3082 {
3083 count = 1;
3084 files = &dict;
3085 }
3086 else
3087 {
3088 /* Expand wildcards in the dictionary name, but do not allow
3089 * backticks (for security, the 'dict' option may have been set in
3090 * a modeline). */
3091 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003092# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003093 if (!thesaurus && STRCMP(buf, "spell") == 0)
3094 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003095 else
3096# endif
3097 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 || expand_wildcards(1, &buf, &count, &files,
3099 EW_FILE|EW_SILENT) != OK)
3100 count = 0;
3101 }
3102
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003103# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003104 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003106 /* Complete from active spelling. Skip "\<" in the pattern, we
3107 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003108 if (pat[0] == '\\' && pat[1] == '<')
3109 ptr = pat + 2;
3110 else
3111 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003112 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003114 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003115# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003116 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003117 {
3118 ins_compl_files(count, files, thesaurus, flags,
3119 &regmatch, buf, &dir);
3120 if (flags != DICT_EXACT)
3121 FreeWild(count, files);
3122 }
3123 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003124 break;
3125 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003126
3127theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128 p_scs = save_p_scs;
3129 vim_free(regmatch.regprog);
3130 vim_free(buf);
3131}
3132
Bram Moolenaar0b238792006-03-02 22:49:12 +00003133 static void
3134ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3135 int count;
3136 char_u **files;
3137 int thesaurus;
3138 int flags;
3139 regmatch_T *regmatch;
3140 char_u *buf;
3141 int *dir;
3142{
3143 char_u *ptr;
3144 int i;
3145 FILE *fp;
3146 int add_r;
3147
3148 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3149 {
3150 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3151 if (flags != DICT_EXACT)
3152 {
3153 vim_snprintf((char *)IObuff, IOSIZE,
3154 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003155 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003156 }
3157
3158 if (fp != NULL)
3159 {
3160 /*
3161 * Read dictionary file line by line.
3162 * Check each line for a match.
3163 */
3164 while (!got_int && !compl_interrupted
3165 && !vim_fgets(buf, LSIZE, fp))
3166 {
3167 ptr = buf;
3168 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3169 {
3170 ptr = regmatch->startp[0];
3171 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3172 ptr = find_line_end(ptr);
3173 else
3174 ptr = find_word_end(ptr);
3175 add_r = ins_compl_add_infercase(regmatch->startp[0],
3176 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003177 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003178 if (thesaurus)
3179 {
3180 char_u *wstart;
3181
3182 /*
3183 * Add the other matches on the line
3184 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003185 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003186 while (!got_int)
3187 {
3188 /* Find start of the next word. Skip white
3189 * space and punctuation. */
3190 ptr = find_word_start(ptr);
3191 if (*ptr == NUL || *ptr == NL)
3192 break;
3193 wstart = ptr;
3194
Bram Moolenaara2993e12007-08-12 14:38:46 +00003195 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003196#ifdef FEAT_MBYTE
3197 if (has_mbyte)
3198 /* Japanese words may have characters in
3199 * different classes, only separate words
3200 * with single-byte non-word characters. */
3201 while (*ptr != NUL)
3202 {
3203 int l = (*mb_ptr2len)(ptr);
3204
3205 if (l < 2 && !vim_iswordc(*ptr))
3206 break;
3207 ptr += l;
3208 }
3209 else
3210#endif
3211 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003212
3213 /* Add the word. Skip the regexp match. */
3214 if (wstart != regmatch->startp[0])
3215 add_r = ins_compl_add_infercase(wstart,
3216 (int)(ptr - wstart),
3217 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003218 }
3219 }
3220 if (add_r == OK)
3221 /* if dir was BACKWARD then honor it just once */
3222 *dir = FORWARD;
3223 else if (add_r == FAIL)
3224 break;
3225 /* avoid expensive call to vim_regexec() when at end
3226 * of line */
3227 if (*ptr == '\n' || got_int)
3228 break;
3229 }
3230 line_breakcheck();
3231 ins_compl_check_keys(50);
3232 }
3233 fclose(fp);
3234 }
3235 }
3236}
3237
Bram Moolenaar071d4272004-06-13 20:20:40 +00003238/*
3239 * Find the start of the next word.
3240 * Returns a pointer to the first char of the word. Also stops at a NUL.
3241 */
3242 char_u *
3243find_word_start(ptr)
3244 char_u *ptr;
3245{
3246#ifdef FEAT_MBYTE
3247 if (has_mbyte)
3248 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003249 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 else
3251#endif
3252 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3253 ++ptr;
3254 return ptr;
3255}
3256
3257/*
3258 * Find the end of the word. Assumes it starts inside a word.
3259 * Returns a pointer to just after the word.
3260 */
3261 char_u *
3262find_word_end(ptr)
3263 char_u *ptr;
3264{
3265#ifdef FEAT_MBYTE
3266 int start_class;
3267
3268 if (has_mbyte)
3269 {
3270 start_class = mb_get_class(ptr);
3271 if (start_class > 1)
3272 while (*ptr != NUL)
3273 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003274 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003275 if (mb_get_class(ptr) != start_class)
3276 break;
3277 }
3278 }
3279 else
3280#endif
3281 while (vim_iswordc(*ptr))
3282 ++ptr;
3283 return ptr;
3284}
3285
3286/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003287 * Find the end of the line, omitting CR and NL at the end.
3288 * Returns a pointer to just after the line.
3289 */
3290 static char_u *
3291find_line_end(ptr)
3292 char_u *ptr;
3293{
3294 char_u *s;
3295
3296 s = ptr + STRLEN(ptr);
3297 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3298 --s;
3299 return s;
3300}
3301
3302/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 * Free the list of completions
3304 */
3305 static void
3306ins_compl_free()
3307{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003308 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003309 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003310
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003311 vim_free(compl_pattern);
3312 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003313 vim_free(compl_leader);
3314 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003315
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003316 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003317 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003318
3319 ins_compl_del_pum();
3320 pum_clear();
3321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003322 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 do
3324 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003325 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003326 compl_curr_match = compl_curr_match->cp_next;
3327 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003329 if (match->cp_flags & FREE_FNAME)
3330 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003331 for (i = 0; i < CPT_COUNT; ++i)
3332 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003333 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003334 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3335 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003336 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003337}
3338
3339 static void
3340ins_compl_clear()
3341{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003342 compl_cont_status = 0;
3343 compl_started = FALSE;
3344 compl_matches = 0;
3345 vim_free(compl_pattern);
3346 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003347 vim_free(compl_leader);
3348 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003350 vim_free(compl_orig_text);
3351 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003352 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353}
3354
3355/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003356 * Return TRUE when Insert completion is active.
3357 */
3358 int
3359ins_compl_active()
3360{
3361 return compl_started;
3362}
3363
3364/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003365 * Delete one character before the cursor and show the subset of the matches
3366 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003367 * Returns the character to be used, NUL if the work is done and another char
3368 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003369 */
3370 static int
3371ins_compl_bs()
3372{
3373 char_u *line;
3374 char_u *p;
3375
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003376 line = ml_get_curline();
3377 p = line + curwin->w_cursor.col;
3378 mb_ptr_back(line, p);
3379
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003380 /* Stop completion when the whole word was deleted. For Omni completion
3381 * allow the word to be deleted, we won't match everything. */
3382 if ((int)(p - line) - (int)compl_col < 0
3383 || ((int)(p - line) - (int)compl_col == 0
3384 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003385 return K_BS;
3386
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003387 /* Deleted more than what was used to find matches or didn't finish
3388 * finding all matches: need to look for matches all over again. */
3389 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003390 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003391 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003392
Bram Moolenaara6557602006-02-04 22:43:20 +00003393 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003394 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003395 if (compl_leader != NULL)
3396 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003397 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003398 if (compl_shown_match != NULL)
3399 /* Make sure current match is not a hidden item. */
3400 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003401 return NUL;
3402 }
3403 return K_BS;
3404}
Bram Moolenaara6557602006-02-04 22:43:20 +00003405
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003406/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003407 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3408 * be called.
3409 */
3410 static int
3411ins_compl_need_restart()
3412{
3413 /* Return TRUE if we didn't complete finding matches or when the
3414 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3415 return compl_was_interrupted
3416 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3417 && compl_opt_refresh_always);
3418}
3419
3420/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003421 * Called after changing "compl_leader".
3422 * Show the popup menu with a different set of matches.
3423 * May also search for matches again if the previous search was interrupted.
3424 */
3425 static void
3426ins_compl_new_leader()
3427{
3428 ins_compl_del_pum();
3429 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003430 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003431 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003432
3433 if (compl_started)
3434 ins_compl_set_original_text(compl_leader);
3435 else
3436 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003437#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003438 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003439#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003440 /*
3441 * Matches were cleared, need to search for them now. First display
3442 * the changed text before the cursor. Set "compl_restarting" to
3443 * avoid that the first match is inserted.
3444 */
3445 update_screen(0);
3446#ifdef FEAT_GUI
3447 if (gui.in_use)
3448 {
3449 /* Show the cursor after the match, not after the redrawn text. */
3450 setcursor();
3451 out_flush();
3452 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003453 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003454#endif
3455 compl_restarting = TRUE;
3456 if (ins_complete(Ctrl_N) == FAIL)
3457 compl_cont_status = 0;
3458 compl_restarting = FALSE;
3459 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003460
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003461 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003462
3463 /* Show the popup menu with a different set of matches. */
3464 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003465
3466 /* Don't let Enter select the original text when there is no popup menu. */
3467 if (compl_match_array == NULL)
3468 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003469}
3470
3471/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003472 * Return the length of the completion, from the completion start column to
3473 * the cursor column. Making sure it never goes below zero.
3474 */
3475 static int
3476ins_compl_len()
3477{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003478 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003479
3480 if (off < 0)
3481 return 0;
3482 return off;
3483}
3484
3485/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003486 * Append one character to the match leader. May reduce the number of
3487 * matches.
3488 */
3489 static void
3490ins_compl_addleader(c)
3491 int c;
3492{
3493#ifdef FEAT_MBYTE
3494 int cc;
3495
3496 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3497 {
3498 char_u buf[MB_MAXBYTES + 1];
3499
3500 (*mb_char2bytes)(c, buf);
3501 buf[cc] = NUL;
3502 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003503 if (compl_opt_refresh_always)
3504 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003505 }
3506 else
3507#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003508 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003509 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003510 if (compl_opt_refresh_always)
3511 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003512 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003513
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003514 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003515 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003516 ins_compl_restart();
3517
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003518 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003519 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003520 * break redo. */
3521 if (!compl_opt_refresh_always)
3522 {
3523 vim_free(compl_leader);
3524 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003525 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003526 if (compl_leader != NULL)
3527 ins_compl_new_leader();
3528 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003529}
3530
3531/*
3532 * Setup for finding completions again without leaving CTRL-X mode. Used when
3533 * BS or a key was typed while still searching for matches.
3534 */
3535 static void
3536ins_compl_restart()
3537{
3538 ins_compl_free();
3539 compl_started = FALSE;
3540 compl_matches = 0;
3541 compl_cont_status = 0;
3542 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003543}
3544
3545/*
3546 * Set the first match, the original text.
3547 */
3548 static void
3549ins_compl_set_original_text(str)
3550 char_u *str;
3551{
3552 char_u *p;
3553
3554 /* Replace the original text entry. */
3555 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3556 {
3557 p = vim_strsave(str);
3558 if (p != NULL)
3559 {
3560 vim_free(compl_first_match->cp_str);
3561 compl_first_match->cp_str = p;
3562 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003563 }
3564}
3565
3566/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003567 * Append one character to the match leader. May reduce the number of
3568 * matches.
3569 */
3570 static void
3571ins_compl_addfrommatch()
3572{
3573 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003574 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003575 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003576 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003577
3578 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003579 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003580 {
3581 /* When still at the original match use the first entry that matches
3582 * the leader. */
3583 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3584 {
3585 p = NULL;
3586 for (cp = compl_shown_match->cp_next; cp != NULL
3587 && cp != compl_first_match; cp = cp->cp_next)
3588 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003589 if (compl_leader == NULL
3590 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003591 (int)STRLEN(compl_leader)))
3592 {
3593 p = cp->cp_str;
3594 break;
3595 }
3596 }
3597 if (p == NULL || (int)STRLEN(p) <= len)
3598 return;
3599 }
3600 else
3601 return;
3602 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003603 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003604 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003605 ins_compl_addleader(c);
3606}
3607
3608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003610 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003611 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003613 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614ins_compl_prep(c)
3615 int c;
3616{
3617 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003619 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620
3621 /* Forget any previous 'special' messages if this is actually
3622 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3623 */
3624 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3625 edit_submode_extra = NULL;
3626
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003627 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003628 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3629 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003630 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003632 /* Set "compl_get_longest" when finding the first matches. */
3633 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3634 || (ctrl_x_mode == 0 && !compl_started))
3635 {
3636 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3637 compl_used_match = TRUE;
3638 }
3639
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3641 {
3642 /*
3643 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3644 * it will be yet. Now we decide.
3645 */
3646 switch (c)
3647 {
3648 case Ctrl_E:
3649 case Ctrl_Y:
3650 ctrl_x_mode = CTRL_X_SCROLL;
3651 if (!(State & REPLACE_FLAG))
3652 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3653 else
3654 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3655 edit_submode_pre = NULL;
3656 showmode();
3657 break;
3658 case Ctrl_L:
3659 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3660 break;
3661 case Ctrl_F:
3662 ctrl_x_mode = CTRL_X_FILES;
3663 break;
3664 case Ctrl_K:
3665 ctrl_x_mode = CTRL_X_DICTIONARY;
3666 break;
3667 case Ctrl_R:
3668 /* Simply allow ^R to happen without affecting ^X mode */
3669 break;
3670 case Ctrl_T:
3671 ctrl_x_mode = CTRL_X_THESAURUS;
3672 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003673#ifdef FEAT_COMPL_FUNC
3674 case Ctrl_U:
3675 ctrl_x_mode = CTRL_X_FUNCTION;
3676 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003677 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003678 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003679 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003680#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003681 case 's':
3682 case Ctrl_S:
3683 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003684#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003685 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003686 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003687 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003688#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003689 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 case Ctrl_RSB:
3691 ctrl_x_mode = CTRL_X_TAGS;
3692 break;
3693#ifdef FEAT_FIND_ID
3694 case Ctrl_I:
3695 case K_S_TAB:
3696 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3697 break;
3698 case Ctrl_D:
3699 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3700 break;
3701#endif
3702 case Ctrl_V:
3703 case Ctrl_Q:
3704 ctrl_x_mode = CTRL_X_CMDLINE;
3705 break;
3706 case Ctrl_P:
3707 case Ctrl_N:
3708 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3709 * just started ^X mode, or there were enough ^X's to cancel
3710 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3711 * do normal expansion when interrupting a different mode (say
3712 * ^X^F^X^P or ^P^X^X^P, see below)
3713 * nothing changes if interrupting mode 0, (eg, the flag
3714 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003715 if (!(compl_cont_status & CONT_INTRPT))
3716 compl_cont_status |= CONT_LOCAL;
3717 else if (compl_cont_mode != 0)
3718 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003719 /* FALLTHROUGH */
3720 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003721 /* If we have typed at least 2 ^X's... for modes != 0, we set
3722 * compl_cont_status = 0 (eg, as if we had just started ^X
3723 * mode).
3724 * For mode 0, we set "compl_cont_mode" to an impossible
3725 * value, in both cases ^X^X can be used to restart the same
3726 * mode (avoiding ADDING mode).
3727 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3728 * 'complete' and local ^P expansions respectively.
3729 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3730 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 if (c == Ctrl_X)
3732 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003733 if (compl_cont_mode != 0)
3734 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003736 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 }
3738 ctrl_x_mode = 0;
3739 edit_submode = NULL;
3740 showmode();
3741 break;
3742 }
3743 }
3744 else if (ctrl_x_mode != 0)
3745 {
3746 /* We're already in CTRL-X mode, do we stay in it? */
3747 if (!vim_is_ctrl_x_key(c))
3748 {
3749 if (ctrl_x_mode == CTRL_X_SCROLL)
3750 ctrl_x_mode = 0;
3751 else
3752 ctrl_x_mode = CTRL_X_FINISHED;
3753 edit_submode = NULL;
3754 }
3755 showmode();
3756 }
3757
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003758 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 {
3760 /* Show error message from attempted keyword completion (probably
3761 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003762 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003763 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003764 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3765 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 || ctrl_x_mode == CTRL_X_FINISHED)
3767 {
3768 /* Get here when we have finished typing a sequence of ^N and
3769 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003770 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003771 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
3773 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003774 * If any of the original typed text has been changed, eg when
3775 * ignorecase is set, we must add back-spaces to the redo
3776 * buffer. We add as few as necessary to delete just the part
3777 * of the original text that has changed.
3778 * When using the longest match, edited the match or used
3779 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003781 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3782 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003783 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003784 ptr = NULL;
3785 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
3787
3788#ifdef FEAT_CINDENT
3789 want_cindent = (can_cindent && cindent_on());
3790#endif
3791 /*
3792 * When completing whole lines: fix indent for 'cindent'.
3793 * Otherwise, break line if it's too long.
3794 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003795 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 {
3797#ifdef FEAT_CINDENT
3798 /* re-indent the current line */
3799 if (want_cindent)
3800 {
3801 do_c_expr_indent();
3802 want_cindent = FALSE; /* don't do it again */
3803 }
3804#endif
3805 }
3806 else
3807 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003808 int prev_col = curwin->w_cursor.col;
3809
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003811 if (prev_col > 0)
3812 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (stop_arrow() == OK)
3814 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003815 if (prev_col > 0
3816 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3817 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003818 }
3819
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003820 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003821 * the selection without inserting anything. When
3822 * compl_enter_selects is set the Enter key does the same. */
3823 if ((c == Ctrl_Y || (compl_enter_selects
3824 && (c == CAR || c == K_KENTER || c == NL)))
3825 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003826 retval = TRUE;
3827
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003828 /* CTRL-E means completion is Ended, go back to the typed text. */
3829 if (c == Ctrl_E)
3830 {
3831 ins_compl_delete();
3832 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003833 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003834 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003835 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003836 retval = TRUE;
3837 }
3838
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003839 auto_format(FALSE, TRUE);
3840
Bram Moolenaar071d4272004-06-13 20:20:40 +00003841 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003842 compl_started = FALSE;
3843 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003844 msg_clr_cmdline(); /* necessary for "noshowmode" */
3845 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003846 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003847 if (edit_submode != NULL)
3848 {
3849 edit_submode = NULL;
3850 showmode();
3851 }
3852
3853#ifdef FEAT_CINDENT
3854 /*
3855 * Indent now if a key was typed that is in 'cinkeys'.
3856 */
3857 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3858 do_c_expr_indent();
3859#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003860#ifdef FEAT_AUTOCMD
3861 /* Trigger the CompleteDone event to give scripts a chance to act
3862 * upon the completion. */
3863 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3864#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003865 }
3866 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003867#ifdef FEAT_AUTOCMD
3868 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3869 /* Trigger the CompleteDone event to give scripts a chance to act
3870 * upon the (possibly failed) completion. */
3871 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873
3874 /* reset continue_* if we left expansion-mode, if we stay they'll be
3875 * (re)set properly in ins_complete() */
3876 if (!vim_is_ctrl_x_key(c))
3877 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003878 compl_cont_status = 0;
3879 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003880 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003881
3882 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883}
3884
3885/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003886 * Fix the redo buffer for the completion leader replacing some of the typed
3887 * text. This inserts backspaces and appends the changed text.
3888 * "ptr" is the known leader text or NUL.
3889 */
3890 static void
3891ins_compl_fixRedoBufForLeader(ptr_arg)
3892 char_u *ptr_arg;
3893{
3894 int len;
3895 char_u *p;
3896 char_u *ptr = ptr_arg;
3897
3898 if (ptr == NULL)
3899 {
3900 if (compl_leader != NULL)
3901 ptr = compl_leader;
3902 else
3903 return; /* nothing to do */
3904 }
3905 if (compl_orig_text != NULL)
3906 {
3907 p = compl_orig_text;
3908 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3909 ;
3910#ifdef FEAT_MBYTE
3911 if (len > 0)
3912 len -= (*mb_head_off)(p, p + len);
3913#endif
3914 for (p += len; *p != NUL; mb_ptr_adv(p))
3915 AppendCharToRedobuff(K_BS);
3916 }
3917 else
3918 len = 0;
3919 if (ptr != NULL)
3920 AppendToRedobuffLit(ptr + len, -1);
3921}
3922
3923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3925 * (depending on flag) starting from buf and looking for a non-scanned
3926 * buffer (other than curbuf). curbuf is special, if it is called with
3927 * buf=curbuf then it has to be the first call for a given flag/expansion.
3928 *
3929 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3930 */
3931 static buf_T *
3932ins_compl_next_buf(buf, flag)
3933 buf_T *buf;
3934 int flag;
3935{
3936#ifdef FEAT_WINDOWS
3937 static win_T *wp;
3938#endif
3939
3940 if (flag == 'w') /* just windows */
3941 {
3942#ifdef FEAT_WINDOWS
3943 if (buf == curbuf) /* first call for this flag/expansion */
3944 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003945 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946 && wp->w_buffer->b_scanned)
3947 ;
3948 buf = wp->w_buffer;
3949#else
3950 buf = curbuf;
3951#endif
3952 }
3953 else
3954 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3955 * (unlisted buffers)
3956 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003957 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 && ((flag == 'U'
3959 ? buf->b_p_bl
3960 : (!buf->b_p_bl
3961 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003962 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003963 ;
3964 return buf;
3965}
3966
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003967#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003968static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003969
3970/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003971 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003972 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003973 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003974 static void
3975expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003976 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003977 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003978{
Bram Moolenaar82139082011-09-14 16:52:09 +02003979 list_T *matchlist = NULL;
3980 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003981 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003982 char_u *funcname;
3983 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003984 win_T *curwin_save;
3985 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02003986 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003987
Bram Moolenaare344bea2005-09-01 20:46:49 +00003988 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3989 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003990 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003991
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003992 /* Call 'completefunc' to obtain the list of matches. */
3993 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003994 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003995
Bram Moolenaare344bea2005-09-01 20:46:49 +00003996 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003997 curwin_save = curwin;
3998 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02003999
4000 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004001 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004002 {
4003 switch (rettv.v_type)
4004 {
4005 case VAR_LIST:
4006 matchlist = rettv.vval.v_list;
4007 break;
4008 case VAR_DICT:
4009 matchdict = rettv.vval.v_dict;
4010 break;
4011 default:
4012 /* TODO: Give error message? */
4013 clear_tv(&rettv);
4014 break;
4015 }
4016 }
4017
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004018 if (curwin_save != curwin || curbuf_save != curbuf)
4019 {
4020 EMSG(_(e_complwin));
4021 goto theend;
4022 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004023 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004024 check_cursor();
4025 if (!equalpos(curwin->w_cursor, pos))
4026 {
4027 EMSG(_(e_compldel));
4028 goto theend;
4029 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004030
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004031 if (matchlist != NULL)
4032 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004033 else if (matchdict != NULL)
4034 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004035
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004036theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004037 if (matchdict != NULL)
4038 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004039 if (matchlist != NULL)
4040 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004041}
4042#endif /* FEAT_COMPL_FUNC */
4043
Bram Moolenaar39f05632006-03-19 22:15:26 +00004044#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004045/*
4046 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004047 */
4048 static void
4049ins_compl_add_list(list)
4050 list_T *list;
4051{
4052 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004053 int dir = compl_direction;
4054
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004055 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004056 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004057 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004058 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4059 /* if dir was BACKWARD then honor it just once */
4060 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004061 else if (did_emsg)
4062 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004063 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004064}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004065
4066/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004067 * Add completions from a dict.
4068 */
4069 static void
4070ins_compl_add_dict(dict)
4071 dict_T *dict;
4072{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004073 dictitem_T *di_refresh;
4074 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004075
4076 /* Check for optional "refresh" item. */
4077 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004078 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4079 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004080 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004081 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004082
4083 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4084 compl_opt_refresh_always = TRUE;
4085 }
4086
4087 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004088 di_words = dict_find(dict, (char_u *)"words", 5);
4089 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4090 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004091}
4092
4093/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004094 * Add a match to the list of matches from a typeval_T.
4095 * If the given string is already in the list of completions, then return
4096 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4097 * maybe because alloc() returns NULL, then FAIL is returned.
4098 */
4099 int
4100ins_compl_add_tv(tv, dir)
4101 typval_T *tv;
4102 int dir;
4103{
4104 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004105 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004106 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004107 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004108 char_u *(cptext[CPT_COUNT]);
4109
4110 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4111 {
4112 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4113 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4114 (char_u *)"abbr", FALSE);
4115 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4116 (char_u *)"menu", FALSE);
4117 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4118 (char_u *)"kind", FALSE);
4119 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4120 (char_u *)"info", FALSE);
4121 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4122 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004123 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004124 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004125 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4126 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004127 }
4128 else
4129 {
4130 word = get_tv_string_chk(tv);
4131 vim_memset(cptext, 0, sizeof(cptext));
4132 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004133 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004134 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004135 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004136}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004137#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139/*
4140 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004141 * The search starts at position "ini" in curbuf and in the direction
4142 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004143 * When "compl_started" is FALSE start at that position, otherwise continue
4144 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004145 * This may return before finding all the matches.
4146 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 */
4148 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004149ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151{
4152 static pos_T first_match_pos;
4153 static pos_T last_match_pos;
4154 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004155 static int found_all = FALSE; /* Found all matches of a
4156 certain type. */
4157 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158
Bram Moolenaar572cb562005-08-05 21:35:02 +00004159 pos_T *pos;
4160 char_u **matches;
4161 int save_p_scs;
4162 int save_p_ws;
4163 int save_p_ic;
4164 int i;
4165 int num_matches;
4166 int len;
4167 int found_new_match;
4168 int type = ctrl_x_mode;
4169 char_u *ptr;
4170 char_u *dict = NULL;
4171 int dict_f = 0;
4172 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004174 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 {
4176 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4177 ins_buf->b_scanned = 0;
4178 found_all = FALSE;
4179 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004180 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004181 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182 last_match_pos = first_match_pos = *ini;
4183 }
4184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004186 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4188 for (;;)
4189 {
4190 found_new_match = FAIL;
4191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004192 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 * or if found_all says this entry is done. For ^X^L only use the
4194 * entries from 'complete' that look in loaded buffers. */
4195 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 {
4198 found_all = FALSE;
4199 while (*e_cpt == ',' || *e_cpt == ' ')
4200 e_cpt++;
4201 if (*e_cpt == '.' && !curbuf->b_scanned)
4202 {
4203 ins_buf = curbuf;
4204 first_match_pos = *ini;
4205 /* So that ^N can match word immediately after cursor */
4206 if (ctrl_x_mode == 0)
4207 dec(&first_match_pos);
4208 last_match_pos = first_match_pos;
4209 type = 0;
4210 }
4211 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4212 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4213 {
4214 /* Scan a buffer, but not the current one. */
4215 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4216 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004217 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 first_match_pos.col = last_match_pos.col = 0;
4219 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4220 last_match_pos.lnum = 0;
4221 type = 0;
4222 }
4223 else /* unloaded buffer, scan like dictionary */
4224 {
4225 found_all = TRUE;
4226 if (ins_buf->b_fname == NULL)
4227 continue;
4228 type = CTRL_X_DICTIONARY;
4229 dict = ins_buf->b_fname;
4230 dict_f = DICT_EXACT;
4231 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004232 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 ins_buf->b_fname == NULL
4234 ? buf_spname(ins_buf)
4235 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004236 ? ins_buf->b_fname
4237 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004238 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 }
4240 else if (*e_cpt == NUL)
4241 break;
4242 else
4243 {
4244 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4245 type = -1;
4246 else if (*e_cpt == 'k' || *e_cpt == 's')
4247 {
4248 if (*e_cpt == 'k')
4249 type = CTRL_X_DICTIONARY;
4250 else
4251 type = CTRL_X_THESAURUS;
4252 if (*++e_cpt != ',' && *e_cpt != NUL)
4253 {
4254 dict = e_cpt;
4255 dict_f = DICT_FIRST;
4256 }
4257 }
4258#ifdef FEAT_FIND_ID
4259 else if (*e_cpt == 'i')
4260 type = CTRL_X_PATH_PATTERNS;
4261 else if (*e_cpt == 'd')
4262 type = CTRL_X_PATH_DEFINES;
4263#endif
4264 else if (*e_cpt == ']' || *e_cpt == 't')
4265 {
4266 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004267 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004268 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 }
4270 else
4271 type = -1;
4272
4273 /* in any case e_cpt is advanced to the next entry */
4274 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4275
4276 found_all = TRUE;
4277 if (type == -1)
4278 continue;
4279 }
4280 }
4281
4282 switch (type)
4283 {
4284 case -1:
4285 break;
4286#ifdef FEAT_FIND_ID
4287 case CTRL_X_PATH_PATTERNS:
4288 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004289 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004290 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004292 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4294 (linenr_T)1, (linenr_T)MAXLNUM);
4295 break;
4296#endif
4297
4298 case CTRL_X_DICTIONARY:
4299 case CTRL_X_THESAURUS:
4300 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004301 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 : (type == CTRL_X_THESAURUS
4303 ? (*curbuf->b_p_tsr == NUL
4304 ? p_tsr
4305 : curbuf->b_p_tsr)
4306 : (*curbuf->b_p_dict == NUL
4307 ? p_dict
4308 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004309 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004310 dict != NULL ? dict_f
4311 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312 dict = NULL;
4313 break;
4314
4315 case CTRL_X_TAGS:
4316 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4317 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004318 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004320 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004321 * of matches is found when compl_pattern is empty */
4322 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4324 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4325 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4326 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004327 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328 }
4329 p_ic = save_p_ic;
4330 break;
4331
4332 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004333 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4335 {
4336
4337 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004339 ins_compl_add_matches(num_matches, matches,
4340#ifdef CASE_INSENSITIVE_FILENAME
4341 TRUE
4342#else
4343 FALSE
4344#endif
4345 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 }
4347 break;
4348
4349 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004350 if (expand_cmdline(&compl_xp, compl_pattern,
4351 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004353 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 break;
4355
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004356#ifdef FEAT_COMPL_FUNC
4357 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004358 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004359 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004360 break;
4361#endif
4362
Bram Moolenaar488c6512005-08-11 20:09:58 +00004363 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004364#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004365 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004366 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004367 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004368 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004369#endif
4370 break;
4371
Bram Moolenaar071d4272004-06-13 20:20:40 +00004372 default: /* normal ^P/^N and ^X^L */
4373 /*
4374 * If 'infercase' is set, don't use 'smartcase' here
4375 */
4376 save_p_scs = p_scs;
4377 if (ins_buf->b_p_inf)
4378 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 /* buffers other than curbuf are scanned from the beginning or the
4381 * end but never from the middle, thus setting nowrapscan in this
4382 * buffers is a good idea, on the other hand, we always set
4383 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4384 save_p_ws = p_ws;
4385 if (ins_buf != curbuf)
4386 p_ws = FALSE;
4387 else if (*e_cpt == '.')
4388 p_ws = TRUE;
4389 for (;;)
4390 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004391 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004393 ++msg_silent; /* Don't want messages for wrapscan. */
4394
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004395 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4396 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004400 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004402 found_new_match = searchit(NULL, ins_buf, pos,
4403 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004404 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004405 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004406 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004407 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004409 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004410 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 first_match_pos = *pos;
4412 last_match_pos = *pos;
4413 }
4414 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004415 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 found_new_match = FAIL;
4417 if (found_new_match == FAIL)
4418 {
4419 if (ins_buf == curbuf)
4420 found_all = TRUE;
4421 break;
4422 }
4423
4424 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004425 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 && ini->lnum == pos->lnum
4427 && ini->col == pos->col)
4428 continue;
4429 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4430 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4431 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004432 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 {
4434 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4435 continue;
4436 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4437 if (!p_paste)
4438 ptr = skipwhite(ptr);
4439 }
4440 len = (int)STRLEN(ptr);
4441 }
4442 else
4443 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004444 char_u *tmp_ptr = ptr;
4445
4446 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004448 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 /* Skip if already inside a word. */
4450 if (vim_iswordp(tmp_ptr))
4451 continue;
4452 /* Find start of next word. */
4453 tmp_ptr = find_word_start(tmp_ptr);
4454 }
4455 /* Find end of this word. */
4456 tmp_ptr = find_word_end(tmp_ptr);
4457 len = (int)(tmp_ptr - ptr);
4458
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459 if ((compl_cont_status & CONT_ADDING)
4460 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 {
4462 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4463 {
4464 /* Try next line, if any. the new word will be
4465 * "join" as if the normal command "J" was used.
4466 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004467 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 * works -- Acevedo */
4469 STRNCPY(IObuff, ptr, len);
4470 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4471 tmp_ptr = ptr = skipwhite(ptr);
4472 /* Find start of next word. */
4473 tmp_ptr = find_word_start(tmp_ptr);
4474 /* Find end of next word. */
4475 tmp_ptr = find_word_end(tmp_ptr);
4476 if (tmp_ptr > ptr)
4477 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004478 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004479 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004480 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 IObuff[len++] = ' ';
4482 /* IObuf =~ "\k.* ", thus len >= 2 */
4483 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004484 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 || (vim_strchr(p_cpo, CPO_JOINSP)
4486 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004487 && (IObuff[len - 2] == '?'
4488 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 IObuff[len++] = ' ';
4490 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004491 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 if (tmp_ptr - ptr >= IOSIZE - len)
4493 tmp_ptr = ptr + IOSIZE - len - 1;
4494 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4495 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004496 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 }
4498 IObuff[len] = NUL;
4499 ptr = IObuff;
4500 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004501 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502 continue;
4503 }
4504 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004505 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004506 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004507 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004508 {
4509 found_new_match = OK;
4510 break;
4511 }
4512 }
4513 p_scs = save_p_scs;
4514 p_ws = save_p_ws;
4515 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004516
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004517 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004518 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004519 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 found_new_match = OK;
4521
4522 /* break the loop for specialized modes (use 'complete' just for the
4523 * generic ctrl_x_mode == 0) or when we've found a new match */
4524 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004525 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004526 {
4527 if (got_int)
4528 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004529 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004530 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004531 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004532
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004533 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4534 || compl_interrupted)
4535 break;
4536 compl_started = TRUE;
4537 }
4538 else
4539 {
4540 /* Mark a buffer scanned when it has been scanned completely */
4541 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4542 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004543
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004544 compl_started = FALSE;
4545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004547 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004548
4549 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4550 && *e_cpt == NUL) /* Got to end of 'complete' */
4551 found_new_match = FAIL;
4552
4553 i = -1; /* total of matches, unknown */
4554 if (found_new_match == FAIL
4555 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4556 i = ins_compl_make_cyclic();
4557
4558 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004559 * just been made cyclic then we have to move compl_curr_match to the next
4560 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004561 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4562 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004563 if (compl_curr_match == NULL)
4564 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004565 return i;
4566}
4567
4568/* Delete the old text being completed. */
4569 static void
4570ins_compl_delete()
4571{
4572 int i;
4573
4574 /*
4575 * In insert mode: Delete the typed part.
4576 * In replace mode: Put the old characters back, if any.
4577 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004578 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 backspace_until_column(i);
4580 changed_cline_bef_curs();
4581}
4582
4583/* Insert the new text being completed. */
4584 static void
4585ins_compl_insert()
4586{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004587 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004588 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4589 compl_used_match = FALSE;
4590 else
4591 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004592}
4593
4594/*
4595 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004596 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4597 * get more completions. If it is FALSE, then we just do nothing when there
4598 * are no more completions in a given direction. The latter case is used when
4599 * we are still in the middle of finding completions, to allow browsing
4600 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 * Return the total number of matches, or -1 if still unknown -- webb.
4602 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004603 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4604 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004605 *
4606 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004607 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4608 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 */
4610 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004611ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004613 int count; /* repeat completion this many times; should
4614 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004615 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616{
4617 int num_matches = -1;
4618 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004619 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004620 compl_T *found_compl = NULL;
4621 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004622 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004624 /* When user complete function return -1 for findstart which is next
4625 * time of 'always', compl_shown_match become NULL. */
4626 if (compl_shown_match == NULL)
4627 return -1;
4628
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004629 if (compl_leader != NULL
4630 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004632 /* Set "compl_shown_match" to the actually shown match, it may differ
4633 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004634 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004635 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004636 && compl_shown_match->cp_next != NULL
4637 && compl_shown_match->cp_next != compl_first_match)
4638 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004639
4640 /* If we didn't find it searching forward, and compl_shows_dir is
4641 * backward, find the last match. */
4642 if (compl_shows_dir == BACKWARD
4643 && !ins_compl_equal(compl_shown_match,
4644 compl_leader, (int)STRLEN(compl_leader))
4645 && (compl_shown_match->cp_next == NULL
4646 || compl_shown_match->cp_next == compl_first_match))
4647 {
4648 while (!ins_compl_equal(compl_shown_match,
4649 compl_leader, (int)STRLEN(compl_leader))
4650 && compl_shown_match->cp_prev != NULL
4651 && compl_shown_match->cp_prev != compl_first_match)
4652 compl_shown_match = compl_shown_match->cp_prev;
4653 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004654 }
4655
4656 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004657 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004658 /* Delete old text to be replaced */
4659 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004660
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004661 /* When finding the longest common text we stick at the original text,
4662 * don't let CTRL-N or CTRL-P move to the first match. */
4663 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4664
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004665 /* When restarting the search don't insert the first match either. */
4666 if (compl_restarting)
4667 {
4668 advance = FALSE;
4669 compl_restarting = FALSE;
4670 }
4671
Bram Moolenaare3226be2005-12-18 22:10:00 +00004672 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4673 * around. */
4674 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004675 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004676 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004678 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004679 found_end = (compl_first_match != NULL
4680 && (compl_shown_match->cp_next == compl_first_match
4681 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004682 }
4683 else if (compl_shows_dir == BACKWARD
4684 && compl_shown_match->cp_prev != NULL)
4685 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004686 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004687 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004688 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004689 }
4690 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004691 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004692 if (!allow_get_expansion)
4693 {
4694 if (advance)
4695 {
4696 if (compl_shows_dir == BACKWARD)
4697 compl_pending -= todo + 1;
4698 else
4699 compl_pending += todo + 1;
4700 }
4701 return -1;
4702 }
4703
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004704 if (advance)
4705 {
4706 if (compl_shows_dir == BACKWARD)
4707 --compl_pending;
4708 else
4709 ++compl_pending;
4710 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004711
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004712 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004713 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004714
4715 /* handle any pending completions */
4716 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004717 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004718 {
4719 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4720 {
4721 compl_shown_match = compl_shown_match->cp_next;
4722 --compl_pending;
4723 }
4724 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4725 {
4726 compl_shown_match = compl_shown_match->cp_prev;
4727 ++compl_pending;
4728 }
4729 else
4730 break;
4731 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004732 found_end = FALSE;
4733 }
4734 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4735 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004736 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004737 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004738 ++todo;
4739 else
4740 /* Remember a matching item. */
4741 found_compl = compl_shown_match;
4742
4743 /* Stop at the end of the list when we found a usable match. */
4744 if (found_end)
4745 {
4746 if (found_compl != NULL)
4747 {
4748 compl_shown_match = found_compl;
4749 break;
4750 }
4751 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 }
4754
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004755 /* Insert the text of the new completion, or the compl_leader. */
4756 if (insert_match)
4757 {
4758 if (!compl_get_longest || compl_used_match)
4759 ins_compl_insert();
4760 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004761 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004762 }
4763 else
4764 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765
4766 if (!allow_get_expansion)
4767 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004768 /* may undisplay the popup menu first */
4769 ins_compl_upd_pum();
4770
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004771 /* redraw to show the user what was inserted */
4772 update_screen(0);
4773
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004774 /* display the updated popup menu */
4775 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004776#ifdef FEAT_GUI
4777 if (gui.in_use)
4778 {
4779 /* Show the cursor after the match, not after the redrawn text. */
4780 setcursor();
4781 out_flush();
4782 gui_update_cursor(FALSE, FALSE);
4783 }
4784#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004785
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 /* Delete old text to be replaced, since we're still searching and
4787 * don't want to match ourselves! */
4788 ins_compl_delete();
4789 }
4790
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004791 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004792 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004793 compl_enter_selects = !insert_match && compl_match_array != NULL;
4794
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 /*
4796 * Show the file name for the match (if any)
4797 * Truncate the file name to avoid a wait for return.
4798 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004799 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
4801 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004802 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 if (i <= 0)
4804 i = 0;
4805 else
4806 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004807 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 msg(IObuff);
4809 redraw_cmdline = FALSE; /* don't overwrite! */
4810 }
4811
4812 return num_matches;
4813}
4814
4815/*
4816 * Call this while finding completions, to check whether the user has hit a key
4817 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004818 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004820 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 */
4822 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004823ins_compl_check_keys(frequency)
4824 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825{
4826 static int count = 0;
4827
4828 int c;
4829
4830 /* Don't check when reading keys from a script. That would break the test
4831 * scripts */
4832 if (using_script())
4833 return;
4834
4835 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004836 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 return;
4838 count = 0;
4839
Bram Moolenaara260a972006-06-23 19:36:29 +00004840 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4841 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 if (c != NUL)
4844 {
4845 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4846 {
4847 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004848 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004849 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4850 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004852 else
4853 {
4854 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004855 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004856 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004857 if (c != K_IGNORE)
4858 {
4859 /* Don't interrupt completion when the character wasn't typed,
4860 * e.g., when doing @q to replay keys. */
4861 if (c != Ctrl_R && KeyTyped)
4862 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004863
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004864 vungetc(c);
4865 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004868 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004869 {
4870 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4871
4872 compl_pending = 0;
4873 (void)ins_compl_next(FALSE, todo, TRUE);
4874 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004875}
4876
4877/*
4878 * Decide the direction of Insert mode complete from the key typed.
4879 * Returns BACKWARD or FORWARD.
4880 */
4881 static int
4882ins_compl_key2dir(c)
4883 int c;
4884{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004885 if (c == Ctrl_P || c == Ctrl_L
4886 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4887 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004888 return BACKWARD;
4889 return FORWARD;
4890}
4891
4892/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004893 * Return TRUE for keys that are used for completion only when the popup menu
4894 * is visible.
4895 */
4896 static int
4897ins_compl_pum_key(c)
4898 int c;
4899{
4900 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004901 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4902 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004903}
4904
4905/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004906 * Decide the number of completions to move forward.
4907 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4908 */
4909 static int
4910ins_compl_key2count(c)
4911 int c;
4912{
4913 int h;
4914
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004915 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004916 {
4917 h = pum_get_height();
4918 if (h > 3)
4919 h -= 2; /* keep some context */
4920 return h;
4921 }
4922 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923}
4924
4925/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004926 * Return TRUE if completion with "c" should insert the match, FALSE if only
4927 * to change the currently selected completion.
4928 */
4929 static int
4930ins_compl_use_match(c)
4931 int c;
4932{
4933 switch (c)
4934 {
4935 case K_UP:
4936 case K_DOWN:
4937 case K_PAGEDOWN:
4938 case K_KPAGEDOWN:
4939 case K_S_DOWN:
4940 case K_PAGEUP:
4941 case K_KPAGEUP:
4942 case K_S_UP:
4943 return FALSE;
4944 }
4945 return TRUE;
4946}
4947
4948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 * Do Insert mode completion.
4950 * Called when character "c" was typed, which has a meaning for completion.
4951 * Returns OK if completion was done, FAIL if something failed (out of mem).
4952 */
4953 static int
4954ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004955 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004957 char_u *line;
4958 int startcol = 0; /* column where searched text starts */
4959 colnr_T curs_col; /* cursor column */
4960 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004961 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962
Bram Moolenaare3226be2005-12-18 22:10:00 +00004963 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004964 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
4966 /* First time we hit ^N or ^P (in a row, I mean) */
4967
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 did_ai = FALSE;
4969#ifdef FEAT_SMARTINDENT
4970 did_si = FALSE;
4971 can_si = FALSE;
4972 can_si_back = FALSE;
4973#endif
4974 if (stop_arrow() == FAIL)
4975 return FAIL;
4976
4977 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004978 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004979 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004981 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004982 * "compl_startpos" to the cursor as a pattern to add a new word
4983 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004984 * "compl_startpos" is not in the same line as the cursor then fix it
4985 * (the line has been split because it was longer than 'tw'). if SOL
4986 * is set then skip the previous pattern, a word at the beginning of
4987 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004988 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4989 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 {
4991 /*
4992 * it is a continued search
4993 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004994 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4996 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4997 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004998 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005000 /* line (probably) wrapped, set compl_startpos to the
5001 * first non_blank in the line, if it is not a wordchar
5002 * include it to get a better pattern, but then we don't
5003 * want the "\\<" prefix, check it bellow */
5004 compl_col = (colnr_T)(skipwhite(line) - line);
5005 compl_startpos.col = compl_col;
5006 compl_startpos.lnum = curwin->w_cursor.lnum;
5007 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009 else
5010 {
5011 /* S_IPOS was set when we inserted a word that was at the
5012 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005013 * mode but first we need to redefine compl_startpos */
5014 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005016 compl_cont_status |= CONT_SOL;
5017 compl_startpos.col = (colnr_T)(skipwhite(
5018 line + compl_length
5019 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005021 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005023 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005024 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005025 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005027 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005029 compl_cont_status &= ~CONT_SOL;
5030 compl_length = (IOSIZE - MIN_SPACE);
5031 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005033 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5034 if (compl_length < 1)
5035 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 }
5037 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005038 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005040 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 }
5042 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005043 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005045 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005047 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005049 compl_cont_status = 0;
5050 compl_cont_status |= CONT_N_ADDS;
5051 compl_startpos = curwin->w_cursor;
5052 startcol = (int)curs_col;
5053 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
5055
5056 /* Work out completion pattern and original text -- webb */
5057 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5058 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005059 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5061 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005062 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005064 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005066 compl_col += ++startcol;
5067 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 }
5069 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005070 compl_pattern = str_foldcase(line + compl_col,
5071 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005073 compl_pattern = vim_strnsave(line + compl_col,
5074 compl_length);
5075 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 return FAIL;
5077 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005078 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
5080 char_u *prefix = (char_u *)"\\<";
5081
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005082 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005083 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005084 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005085 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005087 if (!vim_iswordp(line + compl_col)
5088 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 && (
5090#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005091 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005093 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094#endif
5095 )))
5096 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005097 STRCPY((char *)compl_pattern, prefix);
5098 (void)quote_meta(compl_pattern + STRLEN(prefix),
5099 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005101 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106#endif
5107 )
5108 {
5109 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005110 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5111 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005113 compl_col += curs_col;
5114 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116 else
5117 {
5118#ifdef FEAT_MBYTE
5119 /* Search the point of change class of multibyte character
5120 * or not a word single byte character backward. */
5121 if (has_mbyte)
5122 {
5123 int base_class;
5124 int head_off;
5125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005126 startcol -= (*mb_head_off)(line, line + startcol);
5127 base_class = mb_get_class(line + startcol);
5128 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005130 head_off = (*mb_head_off)(line, line + startcol);
5131 if (base_class != mb_get_class(line + startcol
5132 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005134 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 }
5136 }
5137 else
5138#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 compl_col += ++startcol;
5142 compl_length = (int)curs_col - startcol;
5143 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
5145 /* Only match word with at least two chars -- webb
5146 * there's no need to call quote_meta,
5147 * alloc(7) is enough -- Acevedo
5148 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005149 compl_pattern = alloc(7);
5150 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 STRCPY((char *)compl_pattern, "\\<");
5153 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5154 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156 else
5157 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005158 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005159 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005162 STRCPY((char *)compl_pattern, "\\<");
5163 (void)quote_meta(compl_pattern + 2, line + compl_col,
5164 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166 }
5167 }
5168 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5169 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005170 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005171 compl_length = (int)curs_col - (int)compl_col;
5172 if (compl_length < 0) /* cursor in indent: empty pattern */
5173 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 compl_pattern = str_foldcase(line + compl_col, compl_length,
5176 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005178 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5179 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 return FAIL;
5181 }
5182 else if (ctrl_x_mode == CTRL_X_FILES)
5183 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005184 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 compl_col += ++startcol;
5187 compl_length = (int)curs_col - startcol;
5188 compl_pattern = addstar(line + compl_col, compl_length,
5189 EXPAND_FILES);
5190 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005191 return FAIL;
5192 }
5193 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5194 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005195 compl_pattern = vim_strnsave(line, curs_col);
5196 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005198 set_cmd_context(&compl_xp, compl_pattern,
5199 (int)STRLEN(compl_pattern), curs_col);
5200 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5201 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005202 /* No completion possible, use an empty pattern to get a
5203 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005204 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005205 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005206 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5207 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005208 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005209 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005210 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005211#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005212 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005213 * Call user defined function 'completefunc' with "a:findstart"
5214 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005215 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005216 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005217 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005218 char_u *funcname;
5219 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005220 win_T *curwin_save;
5221 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005222
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005223 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005224 * string */
5225 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5226 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5227 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005228 {
5229 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5230 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005231 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005232 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005233
5234 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005235 args[1] = NULL;
5236 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005237 curwin_save = curwin;
5238 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005239 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005240 if (curwin_save != curwin || curbuf_save != curbuf)
5241 {
5242 EMSG(_(e_complwin));
5243 return FAIL;
5244 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005245 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005246 check_cursor();
5247 if (!equalpos(curwin->w_cursor, pos))
5248 {
5249 EMSG(_(e_compldel));
5250 return FAIL;
5251 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005252
Bram Moolenaar6110a002012-01-26 18:58:38 +01005253 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005254 * cancel the complete without an error.
5255 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005256 if (col == -2)
5257 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005258 if (col == -3)
5259 {
5260 ctrl_x_mode = 0;
5261 edit_submode = NULL;
5262 msg_clr_cmdline();
5263 return FAIL;
5264 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005265
Bram Moolenaar82139082011-09-14 16:52:09 +02005266 /*
5267 * Reset extended parameters of completion, when start new
5268 * completion.
5269 */
5270 compl_opt_refresh_always = FALSE;
5271
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005272 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005273 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005274 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005275 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005276 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005277
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005278 /* Setup variables for completion. Need to obtain "line" again,
5279 * it may have become invalid. */
5280 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005281 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005282 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5283 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005284#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005285 return FAIL;
5286 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005287 else if (ctrl_x_mode == CTRL_X_SPELL)
5288 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005289#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005290 if (spell_bad_len > 0)
5291 compl_col = curs_col - spell_bad_len;
5292 else
5293 compl_col = spell_word_start(startcol);
5294 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005295 {
5296 compl_length = 0;
5297 compl_col = curs_col;
5298 }
5299 else
5300 {
5301 spell_expand_check_cap(compl_col);
5302 compl_length = (int)curs_col - compl_col;
5303 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005304 /* Need to obtain "line" again, it may have become invalid. */
5305 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005306 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5307 if (compl_pattern == NULL)
5308#endif
5309 return FAIL;
5310 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005311 else
5312 {
5313 EMSG2(_(e_intern2), "ins_complete()");
5314 return FAIL;
5315 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005316
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005317 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005318 {
5319 edit_submode_pre = (char_u *)_(" Adding");
5320 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5321 {
5322 /* Insert a new line, keep indentation but ignore 'comments' */
5323#ifdef FEAT_COMMENTS
5324 char_u *old = curbuf->b_p_com;
5325
5326 curbuf->b_p_com = (char_u *)"";
5327#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005328 compl_startpos.lnum = curwin->w_cursor.lnum;
5329 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 ins_eol('\r');
5331#ifdef FEAT_COMMENTS
5332 curbuf->b_p_com = old;
5333#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005334 compl_length = 0;
5335 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 }
5337 }
5338 else
5339 {
5340 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005341 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 }
5343
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 if (compl_cont_status & CONT_LOCAL)
5345 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 else
5347 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5348
Bram Moolenaar62951b12011-09-21 18:23:05 +02005349 /* If any of the original typed text has been changed we need to fix
5350 * the redo buffer. */
5351 ins_compl_fixRedoBufForLeader(NULL);
5352
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005353 /* Always add completion for the original text. */
5354 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5356 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005357 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005359 vim_free(compl_pattern);
5360 compl_pattern = NULL;
5361 vim_free(compl_orig_text);
5362 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005363 return FAIL;
5364 }
5365
5366 /* showmode might reset the internal line pointers, so it must
5367 * be called before line = ml_get(), or when this address is no
5368 * longer needed. -- Acevedo.
5369 */
5370 edit_submode_extra = (char_u *)_("-- Searching...");
5371 edit_submode_highl = HLF_COUNT;
5372 showmode();
5373 edit_submode_extra = NULL;
5374 out_flush();
5375 }
5376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005377 compl_shown_match = compl_curr_match;
5378 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379
5380 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005381 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005383 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005384 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005386 /* may undisplay the popup menu */
5387 ins_compl_upd_pum();
5388
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005389 if (n > 1) /* all matches have been found */
5390 compl_matches = n;
5391 compl_curr_match = compl_shown_match;
5392 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393
Bram Moolenaard68071d2006-05-02 22:08:30 +00005394 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5395 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396 if (got_int && !global_busy)
5397 {
5398 (void)vgetc();
5399 got_int = FALSE;
5400 }
5401
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005402 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005403 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005404 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005405 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5406 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5408 edit_submode_highl = HLF_E;
5409 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5410 * because we couldn't expand anything at first place, but if we used
5411 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5412 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005413 if ( compl_length > 1
5414 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005415 || (ctrl_x_mode != 0
5416 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5417 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005418 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005419 }
5420
Bram Moolenaar572cb562005-08-05 21:35:02 +00005421 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005422 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005423 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005424 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005425
5426 if (edit_submode_extra == NULL)
5427 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005428 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 {
5430 edit_submode_extra = (char_u *)_("Back at original");
5431 edit_submode_highl = HLF_W;
5432 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005433 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005434 {
5435 edit_submode_extra = (char_u *)_("Word from other line");
5436 edit_submode_highl = HLF_COUNT;
5437 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005438 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 {
5440 edit_submode_extra = (char_u *)_("The only match");
5441 edit_submode_highl = HLF_COUNT;
5442 }
5443 else
5444 {
5445 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005446 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005448 int number = 0;
5449 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005451 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452 {
5453 /* search backwards for the first valid (!= -1) number.
5454 * This should normally succeed already at the first loop
5455 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005456 for (match = compl_curr_match->cp_prev; match != NULL
5457 && match != compl_first_match;
5458 match = match->cp_prev)
5459 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005461 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 break;
5463 }
5464 if (match != NULL)
5465 /* go up and assign all numbers which are not assigned
5466 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005467 for (match = match->cp_next;
5468 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005469 match = match->cp_next)
5470 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005471 }
5472 else /* BACKWARD */
5473 {
5474 /* search forwards (upwards) for the first valid (!= -1)
5475 * number. This should normally succeed already at the
5476 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005477 for (match = compl_curr_match->cp_next; match != NULL
5478 && match != compl_first_match;
5479 match = match->cp_next)
5480 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005481 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005482 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005483 break;
5484 }
5485 if (match != NULL)
5486 /* go down and assign all numbers which are not
5487 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005488 for (match = match->cp_prev; match
5489 && match->cp_number == -1;
5490 match = match->cp_prev)
5491 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 }
5493 }
5494
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005495 /* The match should always have a sequence number now, this is
5496 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005497 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005498 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005499 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5500 * Translations may need more than twice that. */
5501 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005502
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005503 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005504 vim_snprintf((char *)match_ref, sizeof(match_ref),
5505 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005506 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005508 vim_snprintf((char *)match_ref, sizeof(match_ref),
5509 _("match %d"),
5510 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 edit_submode_extra = match_ref;
5512 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005513 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 curs_columns(FALSE);
5515 }
5516 }
5517 }
5518
5519 /* Show a message about what (completion) mode we're in. */
5520 showmode();
5521 if (edit_submode_extra != NULL)
5522 {
5523 if (!p_smd)
5524 msg_attr(edit_submode_extra,
5525 edit_submode_highl < HLF_COUNT
5526 ? hl_attr(edit_submode_highl) : 0);
5527 }
5528 else
5529 msg_clr_cmdline(); /* necessary for "noshowmode" */
5530
Bram Moolenaard68071d2006-05-02 22:08:30 +00005531 /* Show the popup menu, unless we got interrupted. */
5532 if (!compl_interrupted)
5533 {
5534 /* RedrawingDisabled may be set when invoked through complete(). */
5535 n = RedrawingDisabled;
5536 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005537
5538 /* If the cursor moved we need to remove the pum first. */
5539 setcursor();
5540 if (save_w_wrow != curwin->w_wrow)
5541 ins_compl_del_pum();
5542
Bram Moolenaard68071d2006-05-02 22:08:30 +00005543 ins_compl_show_pum();
5544 setcursor();
5545 RedrawingDisabled = n;
5546 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005547 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005548 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005549
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 return OK;
5551}
5552
5553/*
5554 * Looks in the first "len" chars. of "src" for search-metachars.
5555 * If dest is not NULL the chars. are copied there quoting (with
5556 * a backslash) the metachars, and dest would be NUL terminated.
5557 * Returns the length (needed) of dest
5558 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005559 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560quote_meta(dest, src, len)
5561 char_u *dest;
5562 char_u *src;
5563 int len;
5564{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005565 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005567 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 {
5569 switch (*src)
5570 {
5571 case '.':
5572 case '*':
5573 case '[':
5574 if (ctrl_x_mode == CTRL_X_DICTIONARY
5575 || ctrl_x_mode == CTRL_X_THESAURUS)
5576 break;
5577 case '~':
5578 if (!p_magic) /* quote these only if magic is set */
5579 break;
5580 case '\\':
5581 if (ctrl_x_mode == CTRL_X_DICTIONARY
5582 || ctrl_x_mode == CTRL_X_THESAURUS)
5583 break;
5584 case '^': /* currently it's not needed. */
5585 case '$':
5586 m++;
5587 if (dest != NULL)
5588 *dest++ = '\\';
5589 break;
5590 }
5591 if (dest != NULL)
5592 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005593# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005594 /* Copy remaining bytes of a multibyte character. */
5595 if (has_mbyte)
5596 {
5597 int i, mb_len;
5598
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005599 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005600 if (mb_len > 0 && len >= mb_len)
5601 for (i = 0; i < mb_len; ++i)
5602 {
5603 --len;
5604 ++src;
5605 if (dest != NULL)
5606 *dest++ = *src;
5607 }
5608 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005609# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 }
5611 if (dest != NULL)
5612 *dest = NUL;
5613
5614 return m;
5615}
5616#endif /* FEAT_INS_EXPAND */
5617
5618/*
5619 * Next character is interpreted literally.
5620 * A one, two or three digit decimal number is interpreted as its byte value.
5621 * If one or two digits are entered, the next character is given to vungetc().
5622 * For Unicode a character > 255 may be returned.
5623 */
5624 int
5625get_literal()
5626{
5627 int cc;
5628 int nc;
5629 int i;
5630 int hex = FALSE;
5631 int octal = FALSE;
5632#ifdef FEAT_MBYTE
5633 int unicode = 0;
5634#endif
5635
5636 if (got_int)
5637 return Ctrl_C;
5638
5639#ifdef FEAT_GUI
5640 /*
5641 * In GUI there is no point inserting the internal code for a special key.
5642 * It is more useful to insert the string "<KEY>" instead. This would
5643 * probably be useful in a text window too, but it would not be
5644 * vi-compatible (maybe there should be an option for it?) -- webb
5645 */
5646 if (gui.in_use)
5647 ++allow_keys;
5648#endif
5649#ifdef USE_ON_FLY_SCROLL
5650 dont_scroll = TRUE; /* disallow scrolling here */
5651#endif
5652 ++no_mapping; /* don't map the next key hits */
5653 cc = 0;
5654 i = 0;
5655 for (;;)
5656 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005657 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658#ifdef FEAT_CMDL_INFO
5659 if (!(State & CMDLINE)
5660# ifdef FEAT_MBYTE
5661 && MB_BYTE2LEN_CHECK(nc) == 1
5662# endif
5663 )
5664 add_to_showcmd(nc);
5665#endif
5666 if (nc == 'x' || nc == 'X')
5667 hex = TRUE;
5668 else if (nc == 'o' || nc == 'O')
5669 octal = TRUE;
5670#ifdef FEAT_MBYTE
5671 else if (nc == 'u' || nc == 'U')
5672 unicode = nc;
5673#endif
5674 else
5675 {
5676 if (hex
5677#ifdef FEAT_MBYTE
5678 || unicode != 0
5679#endif
5680 )
5681 {
5682 if (!vim_isxdigit(nc))
5683 break;
5684 cc = cc * 16 + hex2nr(nc);
5685 }
5686 else if (octal)
5687 {
5688 if (nc < '0' || nc > '7')
5689 break;
5690 cc = cc * 8 + nc - '0';
5691 }
5692 else
5693 {
5694 if (!VIM_ISDIGIT(nc))
5695 break;
5696 cc = cc * 10 + nc - '0';
5697 }
5698
5699 ++i;
5700 }
5701
5702 if (cc > 255
5703#ifdef FEAT_MBYTE
5704 && unicode == 0
5705#endif
5706 )
5707 cc = 255; /* limit range to 0-255 */
5708 nc = 0;
5709
5710 if (hex) /* hex: up to two chars */
5711 {
5712 if (i >= 2)
5713 break;
5714 }
5715#ifdef FEAT_MBYTE
5716 else if (unicode) /* Unicode: up to four or eight chars */
5717 {
5718 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5719 break;
5720 }
5721#endif
5722 else if (i >= 3) /* decimal or octal: up to three chars */
5723 break;
5724 }
5725 if (i == 0) /* no number entered */
5726 {
5727 if (nc == K_ZERO) /* NUL is stored as NL */
5728 {
5729 cc = '\n';
5730 nc = 0;
5731 }
5732 else
5733 {
5734 cc = nc;
5735 nc = 0;
5736 }
5737 }
5738
5739 if (cc == 0) /* NUL is stored as NL */
5740 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005741#ifdef FEAT_MBYTE
5742 if (enc_dbcs && (cc & 0xff) == 0)
5743 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5744 second byte will cause trouble! */
5745#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005746
5747 --no_mapping;
5748#ifdef FEAT_GUI
5749 if (gui.in_use)
5750 --allow_keys;
5751#endif
5752 if (nc)
5753 vungetc(nc);
5754 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5755 return cc;
5756}
5757
5758/*
5759 * Insert character, taking care of special keys and mod_mask
5760 */
5761 static void
5762insert_special(c, allow_modmask, ctrlv)
5763 int c;
5764 int allow_modmask;
5765 int ctrlv; /* c was typed after CTRL-V */
5766{
5767 char_u *p;
5768 int len;
5769
5770 /*
5771 * Special function key, translate into "<Key>". Up to the last '>' is
5772 * inserted with ins_str(), so as not to replace characters in replace
5773 * mode.
5774 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5775 * unless 'allow_modmask' is TRUE.
5776 */
5777#ifdef MACOS
5778 /* Command-key never produces a normal key */
5779 if (mod_mask & MOD_MASK_CMD)
5780 allow_modmask = TRUE;
5781#endif
5782 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5783 {
5784 p = get_special_key_name(c, mod_mask);
5785 len = (int)STRLEN(p);
5786 c = p[len - 1];
5787 if (len > 2)
5788 {
5789 if (stop_arrow() == FAIL)
5790 return;
5791 p[len - 1] = NUL;
5792 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005793 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794 ctrlv = FALSE;
5795 }
5796 }
5797 if (stop_arrow() == OK)
5798 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5799}
5800
5801/*
5802 * Special characters in this context are those that need processing other
5803 * than the simple insertion that can be performed here. This includes ESC
5804 * which terminates the insert, and CR/NL which need special processing to
5805 * open up a new line. This routine tries to optimize insertions performed by
5806 * the "redo", "undo" or "put" commands, so it needs to know when it should
5807 * stop and defer processing to the "normal" mechanism.
5808 * '0' and '^' are special, because they can be followed by CTRL-D.
5809 */
5810#ifdef EBCDIC
5811# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5812#else
5813# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5814#endif
5815
5816#ifdef FEAT_MBYTE
5817# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5818#else
5819# define WHITECHAR(cc) vim_iswhite(cc)
5820#endif
5821
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005822/*
5823 * "flags": INSCHAR_FORMAT - force formatting
5824 * INSCHAR_CTRLV - char typed just after CTRL-V
5825 * INSCHAR_NO_FEX - don't use 'formatexpr'
5826 *
5827 * NOTE: passes the flags value straight through to internal_format() which,
5828 * beside INSCHAR_FORMAT (above), is also looking for these:
5829 * INSCHAR_DO_COM - format comments
5830 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5831 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 void
5833insertchar(c, flags, second_indent)
5834 int c; /* character to insert or NUL */
5835 int flags; /* INSCHAR_FORMAT, etc. */
5836 int second_indent; /* indent for second line if >= 0 */
5837{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 int textwidth;
5839#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843
5844 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5845 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
5847 /*
5848 * Try to break the line in two or more pieces when:
5849 * - Always do this if we have been called to do formatting only.
5850 * - Always do this when 'formatoptions' has the 'a' flag and the line
5851 * ends in white space.
5852 * - Otherwise:
5853 * - Don't do this if inserting a blank
5854 * - Don't do this if an existing character is being replaced, unless
5855 * we're in VREPLACE mode.
5856 * - Do this if the cursor is not on the line where insert started
5857 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5858 * before the insert.
5859 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5860 * before 'textwidth'
5861 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005862 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 && ((flags & INSCHAR_FORMAT)
5864 || (!vim_iswhite(c)
5865 && !((State & REPLACE_FLAG)
5866#ifdef FEAT_VREPLACE
5867 && !(State & VREPLACE_FLAG)
5868#endif
5869 && *ml_get_cursor() != NUL)
5870 && (curwin->w_cursor.lnum != Insstart.lnum
5871 || ((!has_format_option(FO_INS_LONG)
5872 || Insstart_textlen <= (colnr_T)textwidth)
5873 && (!fo_ins_blank
5874 || Insstart_blank_vcol <= (colnr_T)textwidth
5875 ))))))
5876 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005877 /* Format with 'formatexpr' when it's set. Use internal formatting
5878 * when 'formatexpr' isn't set or it returns non-zero. */
5879#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005880 int do_internal = TRUE;
5881
Bram Moolenaar81a82092008-03-12 16:27:00 +00005882 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005883 {
5884 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5885 /* It may be required to save for undo again, e.g. when setline()
5886 * was called. */
5887 ins_need_undo = TRUE;
5888 }
5889 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005890#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005891 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005892 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005893
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 if (c == NUL) /* only formatting was wanted */
5895 return;
5896
5897#ifdef FEAT_COMMENTS
5898 /* Check whether this character should end a comment. */
5899 if (did_ai && (int)c == end_comment_pending)
5900 {
5901 char_u *line;
5902 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5903 int middle_len, end_len;
5904 int i;
5905
5906 /*
5907 * Need to remove existing (middle) comment leader and insert end
5908 * comment leader. First, check what comment leader we can find.
5909 */
Bram Moolenaar81340392012-06-06 16:12:59 +02005910 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005911 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5912 {
5913 /* Skip middle-comment string */
5914 while (*p && p[-1] != ':') /* find end of middle flags */
5915 ++p;
5916 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5917 /* Don't count trailing white space for middle_len */
5918 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5919 --middle_len;
5920
5921 /* Find the end-comment string */
5922 while (*p && p[-1] != ':') /* find end of end flags */
5923 ++p;
5924 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5925
5926 /* Skip white space before the cursor */
5927 i = curwin->w_cursor.col;
5928 while (--i >= 0 && vim_iswhite(line[i]))
5929 ;
5930 i++;
5931
5932 /* Skip to before the middle leader */
5933 i -= middle_len;
5934
5935 /* Check some expected things before we go on */
5936 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5937 {
5938 /* Backspace over all the stuff we want to replace */
5939 backspace_until_column(i);
5940
5941 /*
5942 * Insert the end-comment string, except for the last
5943 * character, which will get inserted as normal later.
5944 */
5945 ins_bytes_len(lead_end, end_len - 1);
5946 }
5947 }
5948 }
5949 end_comment_pending = NUL;
5950#endif
5951
5952 did_ai = FALSE;
5953#ifdef FEAT_SMARTINDENT
5954 did_si = FALSE;
5955 can_si = FALSE;
5956 can_si_back = FALSE;
5957#endif
5958
5959 /*
5960 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5961 * This speeds up normal text input considerably.
5962 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5963 * need to re-indent at a ':', or any other character (but not what
5964 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005965 * Don't do this when there an InsertCharPre autocommand is defined,
5966 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005967 */
5968#ifdef USE_ON_FLY_SCROLL
5969 dont_scroll = FALSE; /* allow scrolling here */
5970#endif
5971
5972 if ( !ISSPECIAL(c)
5973#ifdef FEAT_MBYTE
5974 && (!has_mbyte || (*mb_char2len)(c) == 1)
5975#endif
5976 && vpeekc() != NUL
5977 && !(State & REPLACE_FLAG)
5978#ifdef FEAT_CINDENT
5979 && !cindent_on()
5980#endif
5981#ifdef FEAT_RIGHTLEFT
5982 && !p_ri
5983#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005984#ifdef FEAT_AUTOCMD
5985 && !has_insertcharpre()
5986#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 )
5988 {
5989#define INPUT_BUFLEN 100
5990 char_u buf[INPUT_BUFLEN + 1];
5991 int i;
5992 colnr_T virtcol = 0;
5993
5994 buf[0] = c;
5995 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005996 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005997 virtcol = get_nolist_virtcol();
5998 /*
5999 * Stop the string when:
6000 * - no more chars available
6001 * - finding a special character (command key)
6002 * - buffer is full
6003 * - running into the 'textwidth' boundary
6004 * - need to check for abbreviation: A non-word char after a word-char
6005 */
6006 while ( (c = vpeekc()) != NUL
6007 && !ISSPECIAL(c)
6008#ifdef FEAT_MBYTE
6009 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6010#endif
6011 && i < INPUT_BUFLEN
6012 && (textwidth == 0
6013 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6014 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6015 {
6016#ifdef FEAT_RIGHTLEFT
6017 c = vgetc();
6018 if (p_hkmap && KeyTyped)
6019 c = hkmap(c); /* Hebrew mode mapping */
6020# ifdef FEAT_FKMAP
6021 if (p_fkmap && KeyTyped)
6022 c = fkmap(c); /* Farsi mode mapping */
6023# endif
6024 buf[i++] = c;
6025#else
6026 buf[i++] = vgetc();
6027#endif
6028 }
6029
6030#ifdef FEAT_DIGRAPHS
6031 do_digraph(-1); /* clear digraphs */
6032 do_digraph(buf[i-1]); /* may be the start of a digraph */
6033#endif
6034 buf[i] = NUL;
6035 ins_str(buf);
6036 if (flags & INSCHAR_CTRLV)
6037 {
6038 redo_literal(*buf);
6039 i = 1;
6040 }
6041 else
6042 i = 0;
6043 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006044 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006045 }
6046 else
6047 {
6048#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006049 int cc;
6050
Bram Moolenaar071d4272004-06-13 20:20:40 +00006051 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6052 {
6053 char_u buf[MB_MAXBYTES + 1];
6054
6055 (*mb_char2bytes)(c, buf);
6056 buf[cc] = NUL;
6057 ins_char_bytes(buf, cc);
6058 AppendCharToRedobuff(c);
6059 }
6060 else
6061#endif
6062 {
6063 ins_char(c);
6064 if (flags & INSCHAR_CTRLV)
6065 redo_literal(c);
6066 else
6067 AppendCharToRedobuff(c);
6068 }
6069 }
6070}
6071
6072/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006073 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006074 *
6075 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6076 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006077 */
6078 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006079internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006080 int textwidth;
6081 int second_indent;
6082 int flags;
6083 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006084 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006085{
6086 int cc;
6087 int save_char = NUL;
6088 int haveto_redraw = FALSE;
6089 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6090#ifdef FEAT_MBYTE
6091 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6092#endif
6093 int fo_white_par = has_format_option(FO_WHITE_PAR);
6094 int first_line = TRUE;
6095#ifdef FEAT_COMMENTS
6096 colnr_T leader_len;
6097 int no_leader = FALSE;
6098 int do_comments = (flags & INSCHAR_DO_COM);
6099#endif
6100
6101 /*
6102 * When 'ai' is off we don't want a space under the cursor to be
6103 * deleted. Replace it with an 'x' temporarily.
6104 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006105 if (!curbuf->b_p_ai
6106#ifdef FEAT_VREPLACE
6107 && !(State & VREPLACE_FLAG)
6108#endif
6109 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006110 {
6111 cc = gchar_cursor();
6112 if (vim_iswhite(cc))
6113 {
6114 save_char = cc;
6115 pchar_cursor('x');
6116 }
6117 }
6118
6119 /*
6120 * Repeat breaking lines, until the current line is not too long.
6121 */
6122 while (!got_int)
6123 {
6124 int startcol; /* Cursor column at entry */
6125 int wantcol; /* column at textwidth border */
6126 int foundcol; /* column for start of spaces */
6127 int end_foundcol = 0; /* column for start of word */
6128 colnr_T len;
6129 colnr_T virtcol;
6130#ifdef FEAT_VREPLACE
6131 int orig_col = 0;
6132 char_u *saved_text = NULL;
6133#endif
6134 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006135 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006136
Bram Moolenaar97b98102009-11-17 16:41:01 +00006137 virtcol = get_nolist_virtcol()
6138 + char2cells(c != NUL ? c : gchar_cursor());
6139 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006140 break;
6141
6142#ifdef FEAT_COMMENTS
6143 if (no_leader)
6144 do_comments = FALSE;
6145 else if (!(flags & INSCHAR_FORMAT)
6146 && has_format_option(FO_WRAP_COMS))
6147 do_comments = TRUE;
6148
6149 /* Don't break until after the comment leader */
6150 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006151 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006152 else
6153 leader_len = 0;
6154
6155 /* If the line doesn't start with a comment leader, then don't
6156 * start one in a following broken line. Avoids that a %word
6157 * moved to the start of the next line causes all following lines
6158 * to start with %. */
6159 if (leader_len == 0)
6160 no_leader = TRUE;
6161#endif
6162 if (!(flags & INSCHAR_FORMAT)
6163#ifdef FEAT_COMMENTS
6164 && leader_len == 0
6165#endif
6166 && !has_format_option(FO_WRAP))
6167
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006168 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006169 if ((startcol = curwin->w_cursor.col) == 0)
6170 break;
6171
6172 /* find column of textwidth border */
6173 coladvance((colnr_T)textwidth);
6174 wantcol = curwin->w_cursor.col;
6175
Bram Moolenaar97b98102009-11-17 16:41:01 +00006176 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006177 foundcol = 0;
6178
6179 /*
6180 * Find position to break at.
6181 * Stop at first entered white when 'formatoptions' has 'v'
6182 */
6183 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006184 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006185 || curwin->w_cursor.lnum != Insstart.lnum
6186 || curwin->w_cursor.col >= Insstart.col)
6187 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006188 if (curwin->w_cursor.col == startcol && c != NUL)
6189 cc = c;
6190 else
6191 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006192 if (WHITECHAR(cc))
6193 {
6194 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006195 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006196
6197 /* find start of sequence of blanks */
6198 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6199 {
6200 dec_cursor();
6201 cc = gchar_cursor();
6202 }
6203 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6204 break; /* only spaces in front of text */
6205#ifdef FEAT_COMMENTS
6206 /* Don't break until after the comment leader */
6207 if (curwin->w_cursor.col < leader_len)
6208 break;
6209#endif
6210 if (has_format_option(FO_ONE_LETTER))
6211 {
6212 /* do not break after one-letter words */
6213 if (curwin->w_cursor.col == 0)
6214 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006215#ifdef FEAT_COMMENTS
6216 /* do not break "#a b" when 'tw' is 2 */
6217 if (curwin->w_cursor.col <= leader_len)
6218 break;
6219#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006220 col = curwin->w_cursor.col;
6221 dec_cursor();
6222 cc = gchar_cursor();
6223
6224 if (WHITECHAR(cc))
6225 continue; /* one-letter, continue */
6226 curwin->w_cursor.col = col;
6227 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006228
6229 inc_cursor();
6230
6231 end_foundcol = end_col + 1;
6232 foundcol = curwin->w_cursor.col;
6233 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006234 break;
6235 }
6236#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006237 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006238 {
6239 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006240 if (curwin->w_cursor.col != startcol)
6241 {
6242#ifdef FEAT_COMMENTS
6243 /* Don't break until after the comment leader */
6244 if (curwin->w_cursor.col < leader_len)
6245 break;
6246#endif
6247 col = curwin->w_cursor.col;
6248 inc_cursor();
6249 /* Don't change end_foundcol if already set. */
6250 if (foundcol != curwin->w_cursor.col)
6251 {
6252 foundcol = curwin->w_cursor.col;
6253 end_foundcol = foundcol;
6254 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6255 break;
6256 }
6257 curwin->w_cursor.col = col;
6258 }
6259
6260 if (curwin->w_cursor.col == 0)
6261 break;
6262
6263 col = curwin->w_cursor.col;
6264
6265 dec_cursor();
6266 cc = gchar_cursor();
6267
6268 if (WHITECHAR(cc))
6269 continue; /* break with space */
6270#ifdef FEAT_COMMENTS
6271 /* Don't break until after the comment leader */
6272 if (curwin->w_cursor.col < leader_len)
6273 break;
6274#endif
6275
6276 curwin->w_cursor.col = col;
6277
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006278 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006279 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006280 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6281 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282 }
6283#endif
6284 if (curwin->w_cursor.col == 0)
6285 break;
6286 dec_cursor();
6287 }
6288
6289 if (foundcol == 0) /* no spaces, cannot break line */
6290 {
6291 curwin->w_cursor.col = startcol;
6292 break;
6293 }
6294
6295 /* Going to break the line, remove any "$" now. */
6296 undisplay_dollar();
6297
6298 /*
6299 * Offset between cursor position and line break is used by replace
6300 * stack functions. VREPLACE does not use this, and backspaces
6301 * over the text instead.
6302 */
6303#ifdef FEAT_VREPLACE
6304 if (State & VREPLACE_FLAG)
6305 orig_col = startcol; /* Will start backspacing from here */
6306 else
6307#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006308 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006309
6310 /*
6311 * adjust startcol for spaces that will be deleted and
6312 * characters that will remain on top line
6313 */
6314 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006315 while ((cc = gchar_cursor(), WHITECHAR(cc))
6316 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006317 inc_cursor();
6318 startcol -= curwin->w_cursor.col;
6319 if (startcol < 0)
6320 startcol = 0;
6321
6322#ifdef FEAT_VREPLACE
6323 if (State & VREPLACE_FLAG)
6324 {
6325 /*
6326 * In VREPLACE mode, we will backspace over the text to be
6327 * wrapped, so save a copy now to put on the next line.
6328 */
6329 saved_text = vim_strsave(ml_get_cursor());
6330 curwin->w_cursor.col = orig_col;
6331 if (saved_text == NULL)
6332 break; /* Can't do it, out of memory */
6333 saved_text[startcol] = NUL;
6334
6335 /* Backspace over characters that will move to the next line */
6336 if (!fo_white_par)
6337 backspace_until_column(foundcol);
6338 }
6339 else
6340#endif
6341 {
6342 /* put cursor after pos. to break line */
6343 if (!fo_white_par)
6344 curwin->w_cursor.col = foundcol;
6345 }
6346
6347 /*
6348 * Split the line just before the margin.
6349 * Only insert/delete lines, but don't really redraw the window.
6350 */
6351 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6352 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6353#ifdef FEAT_COMMENTS
6354 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006355 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006356#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006357 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6358 if (!(flags & INSCHAR_COM_LIST))
6359 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006360
6361 replace_offset = 0;
6362 if (first_line)
6363 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006364 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006365 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006366 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006367 * This section is for auto-wrap of numeric lists. When not
6368 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6369 * flag will be set and open_line() will handle it (as seen
6370 * above). The code here (and in get_number_indent()) will
6371 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006372 */
6373 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006374 second_indent =
6375 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006376 if (second_indent >= 0)
6377 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006378#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006379 if (State & VREPLACE_FLAG)
6380 change_indent(INDENT_SET, second_indent,
6381 FALSE, NUL, TRUE);
6382 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006383#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006384#ifdef FEAT_COMMENTS
6385 if (leader_len > 0 && second_indent - leader_len > 0)
6386 {
6387 int i;
6388 int padding = second_indent - leader_len;
6389
6390 /* We started at the first_line of a numbered list
6391 * that has a comment. the open_line() function has
6392 * inserted the proper comment leader and positioned
6393 * the cursor at the end of the split line. Now we
6394 * add the additional whitespace needed after the
6395 * comment leader for the numbered list. */
6396 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006397 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006398 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006399 }
6400 else
6401 {
6402#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006403 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006404#ifdef FEAT_COMMENTS
6405 }
6406#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006407 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006408 }
6409 first_line = FALSE;
6410 }
6411
6412#ifdef FEAT_VREPLACE
6413 if (State & VREPLACE_FLAG)
6414 {
6415 /*
6416 * In VREPLACE mode we have backspaced over the text to be
6417 * moved, now we re-insert it into the new line.
6418 */
6419 ins_bytes(saved_text);
6420 vim_free(saved_text);
6421 }
6422 else
6423#endif
6424 {
6425 /*
6426 * Check if cursor is not past the NUL off the line, cindent
6427 * may have added or removed indent.
6428 */
6429 curwin->w_cursor.col += startcol;
6430 len = (colnr_T)STRLEN(ml_get_curline());
6431 if (curwin->w_cursor.col > len)
6432 curwin->w_cursor.col = len;
6433 }
6434
6435 haveto_redraw = TRUE;
6436#ifdef FEAT_CINDENT
6437 can_cindent = TRUE;
6438#endif
6439 /* moved the cursor, don't autoindent or cindent now */
6440 did_ai = FALSE;
6441#ifdef FEAT_SMARTINDENT
6442 did_si = FALSE;
6443 can_si = FALSE;
6444 can_si_back = FALSE;
6445#endif
6446 line_breakcheck();
6447 }
6448
6449 if (save_char != NUL) /* put back space after cursor */
6450 pchar_cursor(save_char);
6451
6452 if (!format_only && haveto_redraw)
6453 {
6454 update_topline();
6455 redraw_curbuf_later(VALID);
6456 }
6457}
6458
6459/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460 * Called after inserting or deleting text: When 'formatoptions' includes the
6461 * 'a' flag format from the current line until the end of the paragraph.
6462 * Keep the cursor at the same position relative to the text.
6463 * The caller must have saved the cursor line for undo, following ones will be
6464 * saved here.
6465 */
6466 void
6467auto_format(trailblank, prev_line)
6468 int trailblank; /* when TRUE also format with trailing blank */
6469 int prev_line; /* may start in previous line */
6470{
6471 pos_T pos;
6472 colnr_T len;
6473 char_u *old;
6474 char_u *new, *pnew;
6475 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006476 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477
6478 if (!has_format_option(FO_AUTO))
6479 return;
6480
6481 pos = curwin->w_cursor;
6482 old = ml_get_curline();
6483
6484 /* may remove added space */
6485 check_auto_format(FALSE);
6486
6487 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6488 * user might insert normal text next. Also skip formatting when "1" is
6489 * in 'formatoptions' and there is a single character before the cursor.
6490 * Otherwise the line would be broken and when typing another non-white
6491 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006492 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 if (*old != NUL && !trailblank && wasatend)
6494 {
6495 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006496 cc = gchar_cursor();
6497 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6498 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006499 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006500 cc = gchar_cursor();
6501 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 {
6503 curwin->w_cursor = pos;
6504 return;
6505 }
6506 curwin->w_cursor = pos;
6507 }
6508
6509#ifdef FEAT_COMMENTS
6510 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6511 * comments. */
6512 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006513 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006514 return;
6515#endif
6516
6517 /*
6518 * May start formatting in a previous line, so that after "x" a word is
6519 * moved to the previous line if it fits there now. Only when this is not
6520 * the start of a paragraph.
6521 */
6522 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6523 {
6524 --curwin->w_cursor.lnum;
6525 if (u_save_cursor() == FAIL)
6526 return;
6527 }
6528
6529 /*
6530 * Do the formatting and restore the cursor position. "saved_cursor" will
6531 * be adjusted for the text formatting.
6532 */
6533 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006534 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 curwin->w_cursor = saved_cursor;
6536 saved_cursor.lnum = 0;
6537
6538 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6539 {
6540 /* "cannot happen" */
6541 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6542 coladvance((colnr_T)MAXCOL);
6543 }
6544 else
6545 check_cursor_col();
6546
6547 /* Insert mode: If the cursor is now after the end of the line while it
6548 * previously wasn't, the line was broken. Because of the rule above we
6549 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6550 * formatted. */
6551 if (!wasatend && has_format_option(FO_WHITE_PAR))
6552 {
6553 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006554 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006555 if (curwin->w_cursor.col == len)
6556 {
6557 pnew = vim_strnsave(new, len + 2);
6558 pnew[len] = ' ';
6559 pnew[len + 1] = NUL;
6560 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6561 /* remove the space later */
6562 did_add_space = TRUE;
6563 }
6564 else
6565 /* may remove added space */
6566 check_auto_format(FALSE);
6567 }
6568
6569 check_cursor();
6570}
6571
6572/*
6573 * When an extra space was added to continue a paragraph for auto-formatting,
6574 * delete it now. The space must be under the cursor, just after the insert
6575 * position.
6576 */
6577 static void
6578check_auto_format(end_insert)
6579 int end_insert; /* TRUE when ending Insert mode */
6580{
6581 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006582 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583
6584 if (did_add_space)
6585 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006586 cc = gchar_cursor();
6587 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006588 /* Somehow the space was removed already. */
6589 did_add_space = FALSE;
6590 else
6591 {
6592 if (!end_insert)
6593 {
6594 inc_cursor();
6595 c = gchar_cursor();
6596 dec_cursor();
6597 }
6598 if (c != NUL)
6599 {
6600 /* The space is no longer at the end of the line, delete it. */
6601 del_char(FALSE);
6602 did_add_space = FALSE;
6603 }
6604 }
6605 }
6606}
6607
6608/*
6609 * Find out textwidth to be used for formatting:
6610 * if 'textwidth' option is set, use it
6611 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6612 * if invalid value, use 0.
6613 * Set default to window width (maximum 79) for "gq" operator.
6614 */
6615 int
6616comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006617 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006618{
6619 int textwidth;
6620
6621 textwidth = curbuf->b_p_tw;
6622 if (textwidth == 0 && curbuf->b_p_wm)
6623 {
6624 /* The width is the window width minus 'wrapmargin' minus all the
6625 * things that add to the margin. */
6626 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6627#ifdef FEAT_CMDWIN
6628 if (cmdwin_type != 0)
6629 textwidth -= 1;
6630#endif
6631#ifdef FEAT_FOLDING
6632 textwidth -= curwin->w_p_fdc;
6633#endif
6634#ifdef FEAT_SIGNS
6635 if (curwin->w_buffer->b_signlist != NULL
6636# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006637 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006638# endif
6639 )
6640 textwidth -= 1;
6641#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006642 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006643 textwidth -= 8;
6644 }
6645 if (textwidth < 0)
6646 textwidth = 0;
6647 if (ff && textwidth == 0)
6648 {
6649 textwidth = W_WIDTH(curwin) - 1;
6650 if (textwidth > 79)
6651 textwidth = 79;
6652 }
6653 return textwidth;
6654}
6655
6656/*
6657 * Put a character in the redo buffer, for when just after a CTRL-V.
6658 */
6659 static void
6660redo_literal(c)
6661 int c;
6662{
6663 char_u buf[10];
6664
6665 /* Only digits need special treatment. Translate them into a string of
6666 * three digits. */
6667 if (VIM_ISDIGIT(c))
6668 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006669 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006670 AppendToRedobuff(buf);
6671 }
6672 else
6673 AppendCharToRedobuff(c);
6674}
6675
6676/*
6677 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006678 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679 */
6680 static void
6681start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006682 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006683{
6684 if (!arrow_used) /* something has been inserted */
6685 {
6686 AppendToRedobuff(ESC_STR);
6687 stop_insert(end_insert_pos, FALSE);
6688 arrow_used = TRUE; /* this means we stopped the current insert */
6689 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006690#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006691 check_spell_redraw();
6692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693}
6694
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006695#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006696/*
6697 * If we skipped highlighting word at cursor, do it now.
6698 * It may be skipped again, thus reset spell_redraw_lnum first.
6699 */
6700 static void
6701check_spell_redraw()
6702{
6703 if (spell_redraw_lnum != 0)
6704 {
6705 linenr_T lnum = spell_redraw_lnum;
6706
6707 spell_redraw_lnum = 0;
6708 redrawWinline(lnum, FALSE);
6709 }
6710}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006711
6712/*
6713 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6714 * spelled word, if there is one.
6715 */
6716 static void
6717spell_back_to_badword()
6718{
6719 pos_T tpos = curwin->w_cursor;
6720
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006721 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006722 if (curwin->w_cursor.col != tpos.col)
6723 start_arrow(&tpos);
6724}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006725#endif
6726
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727/*
6728 * stop_arrow() is called before a change is made in insert mode.
6729 * If an arrow key has been used, start a new insertion.
6730 * Returns FAIL if undo is impossible, shouldn't insert then.
6731 */
6732 int
6733stop_arrow()
6734{
6735 if (arrow_used)
6736 {
6737 if (u_save_cursor() == OK)
6738 {
6739 arrow_used = FALSE;
6740 ins_need_undo = FALSE;
6741 }
6742 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006743 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744 ai_col = 0;
6745#ifdef FEAT_VREPLACE
6746 if (State & VREPLACE_FLAG)
6747 {
6748 orig_line_count = curbuf->b_ml.ml_line_count;
6749 vr_lines_changed = 1;
6750 }
6751#endif
6752 ResetRedobuff();
6753 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006754 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006755 }
6756 else if (ins_need_undo)
6757 {
6758 if (u_save_cursor() == OK)
6759 ins_need_undo = FALSE;
6760 }
6761
6762#ifdef FEAT_FOLDING
6763 /* Always open fold at the cursor line when inserting something. */
6764 foldOpenCursor();
6765#endif
6766
6767 return (arrow_used || ins_need_undo ? FAIL : OK);
6768}
6769
6770/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006771 * Do a few things to stop inserting.
6772 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6773 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006774 */
6775 static void
6776stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006777 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006778 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006780 int cc;
6781 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782
6783 stop_redo_ins();
6784 replace_flush(); /* abandon replace stack */
6785
6786 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006787 * Save the inserted text for later redo with ^@ and CTRL-A.
6788 * Don't do it when "restart_edit" was set and nothing was inserted,
6789 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006790 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006791 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006792 if (did_restart_edit == 0 || (ptr != NULL
6793 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006794 {
6795 vim_free(last_insert);
6796 last_insert = ptr;
6797 last_insert_skip = new_insert_skip;
6798 }
6799 else
6800 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006802 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 {
6804 /* Auto-format now. It may seem strange to do this when stopping an
6805 * insertion (or moving the cursor), but it's required when appending
6806 * a line and having it end in a space. But only do it when something
6807 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006808 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006810 pos_T tpos = curwin->w_cursor;
6811
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 /* When the cursor is at the end of the line after a space the
6813 * formatting will move it to the following word. Avoid that by
6814 * moving the cursor onto the space. */
6815 cc = 'x';
6816 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6817 {
6818 dec_cursor();
6819 cc = gchar_cursor();
6820 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006821 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 }
6823
6824 auto_format(TRUE, FALSE);
6825
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006826 if (vim_iswhite(cc))
6827 {
6828 if (gchar_cursor() != NUL)
6829 inc_cursor();
6830#ifdef FEAT_VIRTUALEDIT
6831 /* If the cursor is still at the same character, also keep
6832 * the "coladd". */
6833 if (gchar_cursor() == NUL
6834 && curwin->w_cursor.lnum == tpos.lnum
6835 && curwin->w_cursor.col == tpos.col)
6836 curwin->w_cursor.coladd = tpos.coladd;
6837#endif
6838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006839 }
6840
6841 /* If a space was inserted for auto-formatting, remove it now. */
6842 check_auto_format(TRUE);
6843
6844 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006845 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006846 * Do this when ESC was used or moving the cursor up/down.
6847 * Check for the old position still being valid, just in case the text
6848 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006849 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006850 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6851 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006852 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006853 pos_T tpos = curwin->w_cursor;
6854
6855 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006856 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006857 for (;;)
6858 {
6859 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6860 --curwin->w_cursor.col;
6861 cc = gchar_cursor();
6862 if (!vim_iswhite(cc))
6863 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006864 if (del_char(TRUE) == FAIL)
6865 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006866 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006867 if (curwin->w_cursor.lnum != tpos.lnum)
6868 curwin->w_cursor = tpos;
6869 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006870 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6871
6872#ifdef FEAT_VISUAL
6873 /* <C-S-Right> may have started Visual mode, adjust the position for
6874 * deleted characters. */
6875 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6876 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006877 int len = (int)STRLEN(ml_get_curline());
6878
6879 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006880 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006881 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006882# ifdef FEAT_VIRTUALEDIT
6883 VIsual.coladd = 0;
6884# endif
6885 }
6886 }
6887#endif
6888 }
6889 }
6890 did_ai = FALSE;
6891#ifdef FEAT_SMARTINDENT
6892 did_si = FALSE;
6893 can_si = FALSE;
6894 can_si_back = FALSE;
6895#endif
6896
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006897 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6898 * now in a different buffer. */
6899 if (end_insert_pos != NULL)
6900 {
6901 curbuf->b_op_start = Insstart;
6902 curbuf->b_op_end = *end_insert_pos;
6903 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904}
6905
6906/*
6907 * Set the last inserted text to a single character.
6908 * Used for the replace command.
6909 */
6910 void
6911set_last_insert(c)
6912 int c;
6913{
6914 char_u *s;
6915
6916 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006917 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 if (last_insert != NULL)
6919 {
6920 s = last_insert;
6921 /* Use the CTRL-V only when entering a special char */
6922 if (c < ' ' || c == DEL)
6923 *s++ = Ctrl_V;
6924 s = add_char2buf(c, s);
6925 *s++ = ESC;
6926 *s++ = NUL;
6927 last_insert_skip = 0;
6928 }
6929}
6930
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006931#if defined(EXITFREE) || defined(PROTO)
6932 void
6933free_last_insert()
6934{
6935 vim_free(last_insert);
6936 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006937# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006938 vim_free(compl_orig_text);
6939 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006940# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006941}
6942#endif
6943
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944/*
6945 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6946 * and CSI. Handle multi-byte characters.
6947 * Returns a pointer to after the added bytes.
6948 */
6949 char_u *
6950add_char2buf(c, s)
6951 int c;
6952 char_u *s;
6953{
6954#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006955 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006956 int i;
6957 int len;
6958
6959 len = (*mb_char2bytes)(c, temp);
6960 for (i = 0; i < len; ++i)
6961 {
6962 c = temp[i];
6963#endif
6964 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6965 if (c == K_SPECIAL)
6966 {
6967 *s++ = K_SPECIAL;
6968 *s++ = KS_SPECIAL;
6969 *s++ = KE_FILLER;
6970 }
6971#ifdef FEAT_GUI
6972 else if (c == CSI)
6973 {
6974 *s++ = CSI;
6975 *s++ = KS_EXTRA;
6976 *s++ = (int)KE_CSI;
6977 }
6978#endif
6979 else
6980 *s++ = c;
6981#ifdef FEAT_MBYTE
6982 }
6983#endif
6984 return s;
6985}
6986
6987/*
6988 * move cursor to start of line
6989 * if flags & BL_WHITE move to first non-white
6990 * if flags & BL_SOL move to first non-white if startofline is set,
6991 * otherwise keep "curswant" column
6992 * if flags & BL_FIX don't leave the cursor on a NUL.
6993 */
6994 void
6995beginline(flags)
6996 int flags;
6997{
6998 if ((flags & BL_SOL) && !p_sol)
6999 coladvance(curwin->w_curswant);
7000 else
7001 {
7002 curwin->w_cursor.col = 0;
7003#ifdef FEAT_VIRTUALEDIT
7004 curwin->w_cursor.coladd = 0;
7005#endif
7006
7007 if (flags & (BL_WHITE | BL_SOL))
7008 {
7009 char_u *ptr;
7010
7011 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7012 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7013 ++curwin->w_cursor.col;
7014 }
7015 curwin->w_set_curswant = TRUE;
7016 }
7017}
7018
7019/*
7020 * oneright oneleft cursor_down cursor_up
7021 *
7022 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007023 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007024 * Return OK when successful, FAIL when we hit a line of file boundary.
7025 */
7026
7027 int
7028oneright()
7029{
7030 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007031 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007032
7033#ifdef FEAT_VIRTUALEDIT
7034 if (virtual_active())
7035 {
7036 pos_T prevpos = curwin->w_cursor;
7037
7038 /* Adjust for multi-wide char (excluding TAB) */
7039 ptr = ml_get_cursor();
7040 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007041# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007043# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007044 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007045# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046 ))
7047 ? ptr2cells(ptr) : 1));
7048 curwin->w_set_curswant = TRUE;
7049 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7050 return (prevpos.col != curwin->w_cursor.col
7051 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7052 }
7053#endif
7054
7055 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007056 if (*ptr == NUL)
7057 return FAIL; /* already at the very end */
7058
Bram Moolenaar071d4272004-06-13 20:20:40 +00007059#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007060 if (has_mbyte)
7061 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 else
7063#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007064 l = 1;
7065
7066 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7067 * contains "onemore". */
7068 if (ptr[l] == NUL
7069#ifdef FEAT_VIRTUALEDIT
7070 && (ve_flags & VE_ONEMORE) == 0
7071#endif
7072 )
7073 return FAIL;
7074 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075
7076 curwin->w_set_curswant = TRUE;
7077 return OK;
7078}
7079
7080 int
7081oneleft()
7082{
7083#ifdef FEAT_VIRTUALEDIT
7084 if (virtual_active())
7085 {
7086 int width;
7087 int v = getviscol();
7088
7089 if (v == 0)
7090 return FAIL;
7091
7092# ifdef FEAT_LINEBREAK
7093 /* We might get stuck on 'showbreak', skip over it. */
7094 width = 1;
7095 for (;;)
7096 {
7097 coladvance(v - width);
7098 /* getviscol() is slow, skip it when 'showbreak' is empty and
7099 * there are no multi-byte characters */
7100 if ((*p_sbr == NUL
7101# ifdef FEAT_MBYTE
7102 && !has_mbyte
7103# endif
7104 ) || getviscol() < v)
7105 break;
7106 ++width;
7107 }
7108# else
7109 coladvance(v - 1);
7110# endif
7111
7112 if (curwin->w_cursor.coladd == 1)
7113 {
7114 char_u *ptr;
7115
7116 /* Adjust for multi-wide char (not a TAB) */
7117 ptr = ml_get_cursor();
7118 if (*ptr != TAB && vim_isprintc(
7119# ifdef FEAT_MBYTE
7120 (*mb_ptr2char)(ptr)
7121# else
7122 *ptr
7123# endif
7124 ) && ptr2cells(ptr) > 1)
7125 curwin->w_cursor.coladd = 0;
7126 }
7127
7128 curwin->w_set_curswant = TRUE;
7129 return OK;
7130 }
7131#endif
7132
7133 if (curwin->w_cursor.col == 0)
7134 return FAIL;
7135
7136 curwin->w_set_curswant = TRUE;
7137 --curwin->w_cursor.col;
7138
7139#ifdef FEAT_MBYTE
7140 /* if the character on the left of the current cursor is a multi-byte
7141 * character, move to its first byte */
7142 if (has_mbyte)
7143 mb_adjust_cursor();
7144#endif
7145 return OK;
7146}
7147
7148 int
7149cursor_up(n, upd_topline)
7150 long n;
7151 int upd_topline; /* When TRUE: update topline */
7152{
7153 linenr_T lnum;
7154
7155 if (n > 0)
7156 {
7157 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007158 /* This fails if the cursor is already in the first line or the count
7159 * is larger than the line number and '-' is in 'cpoptions' */
7160 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007161 return FAIL;
7162 if (n >= lnum)
7163 lnum = 1;
7164 else
7165#ifdef FEAT_FOLDING
7166 if (hasAnyFolding(curwin))
7167 {
7168 /*
7169 * Count each sequence of folded lines as one logical line.
7170 */
7171 /* go to the the start of the current fold */
7172 (void)hasFolding(lnum, &lnum, NULL);
7173
7174 while (n--)
7175 {
7176 /* move up one line */
7177 --lnum;
7178 if (lnum <= 1)
7179 break;
7180 /* If we entered a fold, move to the beginning, unless in
7181 * Insert mode or when 'foldopen' contains "all": it will open
7182 * in a moment. */
7183 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7184 (void)hasFolding(lnum, &lnum, NULL);
7185 }
7186 if (lnum < 1)
7187 lnum = 1;
7188 }
7189 else
7190#endif
7191 lnum -= n;
7192 curwin->w_cursor.lnum = lnum;
7193 }
7194
7195 /* try to advance to the column we want to be at */
7196 coladvance(curwin->w_curswant);
7197
7198 if (upd_topline)
7199 update_topline(); /* make sure curwin->w_topline is valid */
7200
7201 return OK;
7202}
7203
7204/*
7205 * Cursor down a number of logical lines.
7206 */
7207 int
7208cursor_down(n, upd_topline)
7209 long n;
7210 int upd_topline; /* When TRUE: update topline */
7211{
7212 linenr_T lnum;
7213
7214 if (n > 0)
7215 {
7216 lnum = curwin->w_cursor.lnum;
7217#ifdef FEAT_FOLDING
7218 /* Move to last line of fold, will fail if it's the end-of-file. */
7219 (void)hasFolding(lnum, NULL, &lnum);
7220#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007221 /* This fails if the cursor is already in the last line or would move
7222 * beyound the last line and '-' is in 'cpoptions' */
7223 if (lnum >= curbuf->b_ml.ml_line_count
7224 || (lnum + n > curbuf->b_ml.ml_line_count
7225 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226 return FAIL;
7227 if (lnum + n >= curbuf->b_ml.ml_line_count)
7228 lnum = curbuf->b_ml.ml_line_count;
7229 else
7230#ifdef FEAT_FOLDING
7231 if (hasAnyFolding(curwin))
7232 {
7233 linenr_T last;
7234
7235 /* count each sequence of folded lines as one logical line */
7236 while (n--)
7237 {
7238 if (hasFolding(lnum, NULL, &last))
7239 lnum = last + 1;
7240 else
7241 ++lnum;
7242 if (lnum >= curbuf->b_ml.ml_line_count)
7243 break;
7244 }
7245 if (lnum > curbuf->b_ml.ml_line_count)
7246 lnum = curbuf->b_ml.ml_line_count;
7247 }
7248 else
7249#endif
7250 lnum += n;
7251 curwin->w_cursor.lnum = lnum;
7252 }
7253
7254 /* try to advance to the column we want to be at */
7255 coladvance(curwin->w_curswant);
7256
7257 if (upd_topline)
7258 update_topline(); /* make sure curwin->w_topline is valid */
7259
7260 return OK;
7261}
7262
7263/*
7264 * Stuff the last inserted text in the read buffer.
7265 * Last_insert actually is a copy of the redo buffer, so we
7266 * first have to remove the command.
7267 */
7268 int
7269stuff_inserted(c, count, no_esc)
7270 int c; /* Command character to be inserted */
7271 long count; /* Repeat this many times */
7272 int no_esc; /* Don't add an ESC at the end */
7273{
7274 char_u *esc_ptr;
7275 char_u *ptr;
7276 char_u *last_ptr;
7277 char_u last = NUL;
7278
7279 ptr = get_last_insert();
7280 if (ptr == NULL)
7281 {
7282 EMSG(_(e_noinstext));
7283 return FAIL;
7284 }
7285
7286 /* may want to stuff the command character, to start Insert mode */
7287 if (c != NUL)
7288 stuffcharReadbuff(c);
7289 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7290 *esc_ptr = NUL; /* remove the ESC */
7291
7292 /* when the last char is either "0" or "^" it will be quoted if no ESC
7293 * comes after it OR if it will inserted more than once and "ptr"
7294 * starts with ^D. -- Acevedo
7295 */
7296 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7297 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7298 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7299 {
7300 last = *last_ptr;
7301 *last_ptr = NUL;
7302 }
7303
7304 do
7305 {
7306 stuffReadbuff(ptr);
7307 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7308 if (last)
7309 stuffReadbuff((char_u *)(last == '0'
7310 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7311 : IF_EB("\026^", CTRL_V_STR "^")));
7312 }
7313 while (--count > 0);
7314
7315 if (last)
7316 *last_ptr = last;
7317
7318 if (esc_ptr != NULL)
7319 *esc_ptr = ESC; /* put the ESC back */
7320
7321 /* may want to stuff a trailing ESC, to get out of Insert mode */
7322 if (!no_esc)
7323 stuffcharReadbuff(ESC);
7324
7325 return OK;
7326}
7327
7328 char_u *
7329get_last_insert()
7330{
7331 if (last_insert == NULL)
7332 return NULL;
7333 return last_insert + last_insert_skip;
7334}
7335
7336/*
7337 * Get last inserted string, and remove trailing <Esc>.
7338 * Returns pointer to allocated memory (must be freed) or NULL.
7339 */
7340 char_u *
7341get_last_insert_save()
7342{
7343 char_u *s;
7344 int len;
7345
7346 if (last_insert == NULL)
7347 return NULL;
7348 s = vim_strsave(last_insert + last_insert_skip);
7349 if (s != NULL)
7350 {
7351 len = (int)STRLEN(s);
7352 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7353 s[len - 1] = NUL;
7354 }
7355 return s;
7356}
7357
7358/*
7359 * Check the word in front of the cursor for an abbreviation.
7360 * Called when the non-id character "c" has been entered.
7361 * When an abbreviation is recognized it is removed from the text and
7362 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7363 */
7364 static int
7365echeck_abbr(c)
7366 int c;
7367{
7368 /* Don't check for abbreviation in paste mode, when disabled and just
7369 * after moving around with cursor keys. */
7370 if (p_paste || no_abbr || arrow_used)
7371 return FALSE;
7372
7373 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7374 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7375}
7376
7377/*
7378 * replace-stack functions
7379 *
7380 * When replacing characters, the replaced characters are remembered for each
7381 * new character. This is used to re-insert the old text when backspacing.
7382 *
7383 * There is a NUL headed list of characters for each character that is
7384 * currently in the file after the insertion point. When BS is used, one NUL
7385 * headed list is put back for the deleted character.
7386 *
7387 * For a newline, there are two NUL headed lists. One contains the characters
7388 * that the NL replaced. The extra one stores the characters after the cursor
7389 * that were deleted (always white space).
7390 *
7391 * Replace_offset is normally 0, in which case replace_push will add a new
7392 * character at the end of the stack. If replace_offset is not 0, that many
7393 * characters will be left on the stack above the newly inserted character.
7394 */
7395
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007396static char_u *replace_stack = NULL;
7397static long replace_stack_nr = 0; /* next entry in replace stack */
7398static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399
7400 void
7401replace_push(c)
7402 int c; /* character that is replaced (NUL is none) */
7403{
7404 char_u *p;
7405
7406 if (replace_stack_nr < replace_offset) /* nothing to do */
7407 return;
7408 if (replace_stack_len <= replace_stack_nr)
7409 {
7410 replace_stack_len += 50;
7411 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7412 if (p == NULL) /* out of memory */
7413 {
7414 replace_stack_len -= 50;
7415 return;
7416 }
7417 if (replace_stack != NULL)
7418 {
7419 mch_memmove(p, replace_stack,
7420 (size_t)(replace_stack_nr * sizeof(char_u)));
7421 vim_free(replace_stack);
7422 }
7423 replace_stack = p;
7424 }
7425 p = replace_stack + replace_stack_nr - replace_offset;
7426 if (replace_offset)
7427 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7428 *p = c;
7429 ++replace_stack_nr;
7430}
7431
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007432#if defined(FEAT_MBYTE) || defined(PROTO)
7433/*
7434 * Push a character onto the replace stack. Handles a multi-byte character in
7435 * reverse byte order, so that the first byte is popped off first.
7436 * Return the number of bytes done (includes composing characters).
7437 */
7438 int
7439replace_push_mb(p)
7440 char_u *p;
7441{
7442 int l = (*mb_ptr2len)(p);
7443 int j;
7444
7445 for (j = l - 1; j >= 0; --j)
7446 replace_push(p[j]);
7447 return l;
7448}
7449#endif
7450
Bram Moolenaar071d4272004-06-13 20:20:40 +00007451/*
7452 * Pop one item from the replace stack.
7453 * return -1 if stack empty
7454 * return replaced character or NUL otherwise
7455 */
7456 static int
7457replace_pop()
7458{
7459 if (replace_stack_nr == 0)
7460 return -1;
7461 return (int)replace_stack[--replace_stack_nr];
7462}
7463
7464/*
7465 * Join the top two items on the replace stack. This removes to "off"'th NUL
7466 * encountered.
7467 */
7468 static void
7469replace_join(off)
7470 int off; /* offset for which NUL to remove */
7471{
7472 int i;
7473
7474 for (i = replace_stack_nr; --i >= 0; )
7475 if (replace_stack[i] == NUL && off-- <= 0)
7476 {
7477 --replace_stack_nr;
7478 mch_memmove(replace_stack + i, replace_stack + i + 1,
7479 (size_t)(replace_stack_nr - i));
7480 return;
7481 }
7482}
7483
7484/*
7485 * Pop bytes from the replace stack until a NUL is found, and insert them
7486 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7487 */
7488 static void
7489replace_pop_ins()
7490{
7491 int cc;
7492 int oldState = State;
7493
7494 State = NORMAL; /* don't want REPLACE here */
7495 while ((cc = replace_pop()) > 0)
7496 {
7497#ifdef FEAT_MBYTE
7498 mb_replace_pop_ins(cc);
7499#else
7500 ins_char(cc);
7501#endif
7502 dec_cursor();
7503 }
7504 State = oldState;
7505}
7506
7507#ifdef FEAT_MBYTE
7508/*
7509 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7510 * indicates a multi-byte char, pop the other bytes too.
7511 */
7512 static void
7513mb_replace_pop_ins(cc)
7514 int cc;
7515{
7516 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007517 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518 int i;
7519 int c;
7520
7521 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7522 {
7523 buf[0] = cc;
7524 for (i = 1; i < n; ++i)
7525 buf[i] = replace_pop();
7526 ins_bytes_len(buf, n);
7527 }
7528 else
7529 ins_char(cc);
7530
7531 if (enc_utf8)
7532 /* Handle composing chars. */
7533 for (;;)
7534 {
7535 c = replace_pop();
7536 if (c == -1) /* stack empty */
7537 break;
7538 if ((n = MB_BYTE2LEN(c)) == 1)
7539 {
7540 /* Not a multi-byte char, put it back. */
7541 replace_push(c);
7542 break;
7543 }
7544 else
7545 {
7546 buf[0] = c;
7547 for (i = 1; i < n; ++i)
7548 buf[i] = replace_pop();
7549 if (utf_iscomposing(utf_ptr2char(buf)))
7550 ins_bytes_len(buf, n);
7551 else
7552 {
7553 /* Not a composing char, put it back. */
7554 for (i = n - 1; i >= 0; --i)
7555 replace_push(buf[i]);
7556 break;
7557 }
7558 }
7559 }
7560}
7561#endif
7562
7563/*
7564 * make the replace stack empty
7565 * (called when exiting replace mode)
7566 */
7567 static void
7568replace_flush()
7569{
7570 vim_free(replace_stack);
7571 replace_stack = NULL;
7572 replace_stack_len = 0;
7573 replace_stack_nr = 0;
7574}
7575
7576/*
7577 * Handle doing a BS for one character.
7578 * cc < 0: replace stack empty, just move cursor
7579 * cc == 0: character was inserted, delete it
7580 * cc > 0: character was replaced, put cc (first byte of original char) back
7581 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007582 * When "limit_col" is >= 0, don't delete before this column. Matters when
7583 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007584 */
7585 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007586replace_do_bs(limit_col)
7587 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007588{
7589 int cc;
7590#ifdef FEAT_VREPLACE
7591 int orig_len = 0;
7592 int ins_len;
7593 int orig_vcols = 0;
7594 colnr_T start_vcol;
7595 char_u *p;
7596 int i;
7597 int vcol;
7598#endif
7599
7600 cc = replace_pop();
7601 if (cc > 0)
7602 {
7603#ifdef FEAT_VREPLACE
7604 if (State & VREPLACE_FLAG)
7605 {
7606 /* Get the number of screen cells used by the character we are
7607 * going to delete. */
7608 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7609 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7610 }
7611#endif
7612#ifdef FEAT_MBYTE
7613 if (has_mbyte)
7614 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007615 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007616# ifdef FEAT_VREPLACE
7617 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007618 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619# endif
7620 replace_push(cc);
7621 }
7622 else
7623#endif
7624 {
7625 pchar_cursor(cc);
7626#ifdef FEAT_VREPLACE
7627 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007628 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007629#endif
7630 }
7631 replace_pop_ins();
7632
7633#ifdef FEAT_VREPLACE
7634 if (State & VREPLACE_FLAG)
7635 {
7636 /* Get the number of screen cells used by the inserted characters */
7637 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007638 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007639 vcol = start_vcol;
7640 for (i = 0; i < ins_len; ++i)
7641 {
7642 vcol += chartabsize(p + i, vcol);
7643#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007644 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007645#endif
7646 }
7647 vcol -= start_vcol;
7648
7649 /* Delete spaces that were inserted after the cursor to keep the
7650 * text aligned. */
7651 curwin->w_cursor.col += ins_len;
7652 while (vcol > orig_vcols && gchar_cursor() == ' ')
7653 {
7654 del_char(FALSE);
7655 ++orig_vcols;
7656 }
7657 curwin->w_cursor.col -= ins_len;
7658 }
7659#endif
7660
7661 /* mark the buffer as changed and prepare for displaying */
7662 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7663 }
7664 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007665 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007666}
7667
7668#ifdef FEAT_CINDENT
7669/*
7670 * Return TRUE if C-indenting is on.
7671 */
7672 static int
7673cindent_on()
7674{
7675 return (!p_paste && (curbuf->b_p_cin
7676# ifdef FEAT_EVAL
7677 || *curbuf->b_p_inde != NUL
7678# endif
7679 ));
7680}
7681#endif
7682
7683#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7684/*
7685 * Re-indent the current line, based on the current contents of it and the
7686 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7687 * confused what all the part that handles Control-T is doing that I'm not.
7688 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7689 */
7690
7691 void
7692fixthisline(get_the_indent)
7693 int (*get_the_indent) __ARGS((void));
7694{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007695 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007696 if (linewhite(curwin->w_cursor.lnum))
7697 did_ai = TRUE; /* delete the indent if the line stays empty */
7698}
7699
7700 void
7701fix_indent()
7702{
7703 if (p_paste)
7704 return;
7705# ifdef FEAT_LISP
7706 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7707 fixthisline(get_lisp_indent);
7708# endif
7709# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7710 else
7711# endif
7712# ifdef FEAT_CINDENT
7713 if (cindent_on())
7714 do_c_expr_indent();
7715# endif
7716}
7717
7718#endif
7719
7720#ifdef FEAT_CINDENT
7721/*
7722 * return TRUE if 'cinkeys' contains the key "keytyped",
7723 * when == '*': Only if key is preceded with '*' (indent before insert)
7724 * when == '!': Only if key is prededed with '!' (don't insert)
7725 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7726 *
7727 * "keytyped" can have a few special values:
7728 * KEY_OPEN_FORW
7729 * KEY_OPEN_BACK
7730 * KEY_COMPLETE just finished completion.
7731 *
7732 * If line_is_empty is TRUE accept keys with '0' before them.
7733 */
7734 int
7735in_cinkeys(keytyped, when, line_is_empty)
7736 int keytyped;
7737 int when;
7738 int line_is_empty;
7739{
7740 char_u *look;
7741 int try_match;
7742 int try_match_word;
7743 char_u *p;
7744 char_u *line;
7745 int icase;
7746 int i;
7747
Bram Moolenaar30848942009-12-24 14:46:12 +00007748 if (keytyped == NUL)
7749 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7750 return FALSE;
7751
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752#ifdef FEAT_EVAL
7753 if (*curbuf->b_p_inde != NUL)
7754 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7755 else
7756#endif
7757 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7758 while (*look)
7759 {
7760 /*
7761 * Find out if we want to try a match with this key, depending on
7762 * 'when' and a '*' or '!' before the key.
7763 */
7764 switch (when)
7765 {
7766 case '*': try_match = (*look == '*'); break;
7767 case '!': try_match = (*look == '!'); break;
7768 default: try_match = (*look != '*'); break;
7769 }
7770 if (*look == '*' || *look == '!')
7771 ++look;
7772
7773 /*
7774 * If there is a '0', only accept a match if the line is empty.
7775 * But may still match when typing last char of a word.
7776 */
7777 if (*look == '0')
7778 {
7779 try_match_word = try_match;
7780 if (!line_is_empty)
7781 try_match = FALSE;
7782 ++look;
7783 }
7784 else
7785 try_match_word = FALSE;
7786
7787 /*
7788 * does it look like a control character?
7789 */
7790 if (*look == '^'
7791#ifdef EBCDIC
7792 && (Ctrl_chr(look[1]) != 0)
7793#else
7794 && look[1] >= '?' && look[1] <= '_'
7795#endif
7796 )
7797 {
7798 if (try_match && keytyped == Ctrl_chr(look[1]))
7799 return TRUE;
7800 look += 2;
7801 }
7802 /*
7803 * 'o' means "o" command, open forward.
7804 * 'O' means "O" command, open backward.
7805 */
7806 else if (*look == 'o')
7807 {
7808 if (try_match && keytyped == KEY_OPEN_FORW)
7809 return TRUE;
7810 ++look;
7811 }
7812 else if (*look == 'O')
7813 {
7814 if (try_match && keytyped == KEY_OPEN_BACK)
7815 return TRUE;
7816 ++look;
7817 }
7818
7819 /*
7820 * 'e' means to check for "else" at start of line and just before the
7821 * cursor.
7822 */
7823 else if (*look == 'e')
7824 {
7825 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7826 {
7827 p = ml_get_curline();
7828 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7829 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7830 return TRUE;
7831 }
7832 ++look;
7833 }
7834
7835 /*
7836 * ':' only causes an indent if it is at the end of a label or case
7837 * statement, or when it was before typing the ':' (to fix
7838 * class::method for C++).
7839 */
7840 else if (*look == ':')
7841 {
7842 if (try_match && keytyped == ':')
7843 {
7844 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007845 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7846 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007848 /* Need to get the line again after cin_islabel(). */
7849 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 if (curwin->w_cursor.col > 2
7851 && p[curwin->w_cursor.col - 1] == ':'
7852 && p[curwin->w_cursor.col - 2] == ':')
7853 {
7854 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007855 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856 || cin_islabel(30));
7857 p = ml_get_curline();
7858 p[curwin->w_cursor.col - 1] = ':';
7859 if (i)
7860 return TRUE;
7861 }
7862 }
7863 ++look;
7864 }
7865
7866
7867 /*
7868 * Is it a key in <>, maybe?
7869 */
7870 else if (*look == '<')
7871 {
7872 if (try_match)
7873 {
7874 /*
7875 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7876 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7877 * >, *, : and ! keys if they really really want to.
7878 */
7879 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7880 && keytyped == look[1])
7881 return TRUE;
7882
7883 if (keytyped == get_special_key_code(look + 1))
7884 return TRUE;
7885 }
7886 while (*look && *look != '>')
7887 look++;
7888 while (*look == '>')
7889 look++;
7890 }
7891
7892 /*
7893 * Is it a word: "=word"?
7894 */
7895 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7896 {
7897 ++look;
7898 if (*look == '~')
7899 {
7900 icase = TRUE;
7901 ++look;
7902 }
7903 else
7904 icase = FALSE;
7905 p = vim_strchr(look, ',');
7906 if (p == NULL)
7907 p = look + STRLEN(look);
7908 if ((try_match || try_match_word)
7909 && curwin->w_cursor.col >= (colnr_T)(p - look))
7910 {
7911 int match = FALSE;
7912
7913#ifdef FEAT_INS_EXPAND
7914 if (keytyped == KEY_COMPLETE)
7915 {
7916 char_u *s;
7917
7918 /* Just completed a word, check if it starts with "look".
7919 * search back for the start of a word. */
7920 line = ml_get_curline();
7921# ifdef FEAT_MBYTE
7922 if (has_mbyte)
7923 {
7924 char_u *n;
7925
7926 for (s = line + curwin->w_cursor.col; s > line; s = n)
7927 {
7928 n = mb_prevptr(line, s);
7929 if (!vim_iswordp(n))
7930 break;
7931 }
7932 }
7933 else
7934# endif
7935 for (s = line + curwin->w_cursor.col; s > line; --s)
7936 if (!vim_iswordc(s[-1]))
7937 break;
7938 if (s + (p - look) <= line + curwin->w_cursor.col
7939 && (icase
7940 ? MB_STRNICMP(s, look, p - look)
7941 : STRNCMP(s, look, p - look)) == 0)
7942 match = TRUE;
7943 }
7944 else
7945#endif
7946 /* TODO: multi-byte */
7947 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7948 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7949 {
7950 line = ml_get_cursor();
7951 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7952 || !vim_iswordc(line[-(p - look) - 1]))
7953 && (icase
7954 ? MB_STRNICMP(line - (p - look), look, p - look)
7955 : STRNCMP(line - (p - look), look, p - look))
7956 == 0)
7957 match = TRUE;
7958 }
7959 if (match && try_match_word && !try_match)
7960 {
7961 /* "0=word": Check if there are only blanks before the
7962 * word. */
7963 line = ml_get_curline();
7964 if ((int)(skipwhite(line) - line) !=
7965 (int)(curwin->w_cursor.col - (p - look)))
7966 match = FALSE;
7967 }
7968 if (match)
7969 return TRUE;
7970 }
7971 look = p;
7972 }
7973
7974 /*
7975 * ok, it's a boring generic character.
7976 */
7977 else
7978 {
7979 if (try_match && *look == keytyped)
7980 return TRUE;
7981 ++look;
7982 }
7983
7984 /*
7985 * Skip over ", ".
7986 */
7987 look = skip_to_option_part(look);
7988 }
7989 return FALSE;
7990}
7991#endif /* FEAT_CINDENT */
7992
7993#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7994/*
7995 * Map Hebrew keyboard when in hkmap mode.
7996 */
7997 int
7998hkmap(c)
7999 int c;
8000{
8001 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8002 {
8003 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8004 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8005 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8006 static char_u map[26] =
8007 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8008 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8009 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8010 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8011 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8012 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8013 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8014 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8015 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8016
8017 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8018 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8019 /* '-1'='sofit' */
8020 else if (c == 'x')
8021 return 'X';
8022 else if (c == 'q')
8023 return '\''; /* {geresh}={'} */
8024 else if (c == 246)
8025 return ' '; /* \"o --> ' ' for a german keyboard */
8026 else if (c == 228)
8027 return ' '; /* \"a --> ' ' -- / -- */
8028 else if (c == 252)
8029 return ' '; /* \"u --> ' ' -- / -- */
8030#ifdef EBCDIC
8031 else if (islower(c))
8032#else
8033 /* NOTE: islower() does not do the right thing for us on Linux so we
8034 * do this the same was as 5.7 and previous, so it works correctly on
8035 * all systems. Specifically, the e.g. Delete and Arrow keys are
8036 * munged and won't work if e.g. searching for Hebrew text.
8037 */
8038 else if (c >= 'a' && c <= 'z')
8039#endif
8040 return (int)(map[CharOrdLow(c)] + p_aleph);
8041 else
8042 return c;
8043 }
8044 else
8045 {
8046 switch (c)
8047 {
8048 case '`': return ';';
8049 case '/': return '.';
8050 case '\'': return ',';
8051 case 'q': return '/';
8052 case 'w': return '\'';
8053
8054 /* Hebrew letters - set offset from 'a' */
8055 case ',': c = '{'; break;
8056 case '.': c = 'v'; break;
8057 case ';': c = 't'; break;
8058 default: {
8059 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8060
8061#ifdef EBCDIC
8062 /* see note about islower() above */
8063 if (!islower(c))
8064#else
8065 if (c < 'a' || c > 'z')
8066#endif
8067 return c;
8068 c = str[CharOrdLow(c)];
8069 break;
8070 }
8071 }
8072
8073 return (int)(CharOrdLow(c) + p_aleph);
8074 }
8075}
8076#endif
8077
8078 static void
8079ins_reg()
8080{
8081 int need_redraw = FALSE;
8082 int regname;
8083 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008084#ifdef FEAT_VISUAL
8085 int vis_active = VIsual_active;
8086#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008087
8088 /*
8089 * If we are going to wait for a character, show a '"'.
8090 */
8091 pc_status = PC_STATUS_UNSET;
8092 if (redrawing() && !char_avail())
8093 {
8094 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008095 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096
8097 edit_putchar('"', TRUE);
8098#ifdef FEAT_CMDL_INFO
8099 add_to_showcmd_c(Ctrl_R);
8100#endif
8101 }
8102
8103#ifdef USE_ON_FLY_SCROLL
8104 dont_scroll = TRUE; /* disallow scrolling here */
8105#endif
8106
8107 /*
8108 * Don't map the register name. This also prevents the mode message to be
8109 * deleted when ESC is hit.
8110 */
8111 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008112 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8115 {
8116 /* Get a third key for literal register insertion */
8117 literally = regname;
8118#ifdef FEAT_CMDL_INFO
8119 add_to_showcmd_c(literally);
8120#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008121 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008122 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 }
8124 --no_mapping;
8125
8126#ifdef FEAT_EVAL
8127 /*
8128 * Don't call u_sync() while getting the expression,
8129 * evaluating it or giving an error message for it!
8130 */
8131 ++no_u_sync;
8132 if (regname == '=')
8133 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008134# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008136# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008138# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 /* Restore the Input Method. */
8140 if (im_on)
8141 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008142# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008144 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8145 {
8146 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008149 else
8150 {
8151#endif
8152 if (literally == Ctrl_O || literally == Ctrl_P)
8153 {
8154 /* Append the command to the redo buffer. */
8155 AppendCharToRedobuff(Ctrl_R);
8156 AppendCharToRedobuff(literally);
8157 AppendCharToRedobuff(regname);
8158
8159 do_put(regname, BACKWARD, 1L,
8160 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8161 }
8162 else if (insert_reg(regname, literally) == FAIL)
8163 {
8164 vim_beep();
8165 need_redraw = TRUE; /* remove the '"' */
8166 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008167 else if (stop_insert_mode)
8168 /* When the '=' register was used and a function was invoked that
8169 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8170 * insert anything, need to remove the '"' */
8171 need_redraw = TRUE;
8172
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173#ifdef FEAT_EVAL
8174 }
8175 --no_u_sync;
8176#endif
8177#ifdef FEAT_CMDL_INFO
8178 clear_showcmd();
8179#endif
8180
8181 /* If the inserted register is empty, we need to remove the '"' */
8182 if (need_redraw || stuff_empty())
8183 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008184
8185#ifdef FEAT_VISUAL
8186 /* Disallow starting Visual mode here, would get a weird mode. */
8187 if (!vis_active && VIsual_active)
8188 end_visual_mode();
8189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190}
8191
8192/*
8193 * CTRL-G commands in Insert mode.
8194 */
8195 static void
8196ins_ctrl_g()
8197{
8198 int c;
8199
8200#ifdef FEAT_INS_EXPAND
8201 /* Right after CTRL-X the cursor will be after the ruler. */
8202 setcursor();
8203#endif
8204
8205 /*
8206 * Don't map the second key. This also prevents the mode message to be
8207 * deleted when ESC is hit.
8208 */
8209 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008210 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211 --no_mapping;
8212 switch (c)
8213 {
8214 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8215 case K_UP:
8216 case Ctrl_K:
8217 case 'k': ins_up(TRUE);
8218 break;
8219
8220 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8221 case K_DOWN:
8222 case Ctrl_J:
8223 case 'j': ins_down(TRUE);
8224 break;
8225
8226 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008227 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008229
8230 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008231 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008232 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 break;
8234
8235 /* Unknown CTRL-G command, reserved for future expansion. */
8236 default: vim_beep();
8237 }
8238}
8239
8240/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008241 * CTRL-^ in Insert mode.
8242 */
8243 static void
8244ins_ctrl_hat()
8245{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008246 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008247 {
8248 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8249 if (State & LANGMAP)
8250 {
8251 curbuf->b_p_iminsert = B_IMODE_NONE;
8252 State &= ~LANGMAP;
8253 }
8254 else
8255 {
8256 curbuf->b_p_iminsert = B_IMODE_LMAP;
8257 State |= LANGMAP;
8258#ifdef USE_IM_CONTROL
8259 im_set_active(FALSE);
8260#endif
8261 }
8262 }
8263#ifdef USE_IM_CONTROL
8264 else
8265 {
8266 /* There are no ":lmap" mappings, toggle IM */
8267 if (im_get_status())
8268 {
8269 curbuf->b_p_iminsert = B_IMODE_NONE;
8270 im_set_active(FALSE);
8271 }
8272 else
8273 {
8274 curbuf->b_p_iminsert = B_IMODE_IM;
8275 State &= ~LANGMAP;
8276 im_set_active(TRUE);
8277 }
8278 }
8279#endif
8280 set_iminsert_global();
8281 showmode();
8282#ifdef FEAT_GUI
8283 /* may show different cursor shape or color */
8284 if (gui.in_use)
8285 gui_update_cursor(TRUE, FALSE);
8286#endif
8287#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8288 /* Show/unshow value of 'keymap' in status lines. */
8289 status_redraw_curbuf();
8290#endif
8291}
8292
8293/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 * Handle ESC in insert mode.
8295 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8296 * insert.
8297 */
8298 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008299ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 long *count;
8301 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008302 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303{
8304 int temp;
8305 static int disabled_redraw = FALSE;
8306
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008307#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008308 check_spell_redraw();
8309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310#if defined(FEAT_HANGULIN)
8311# if defined(ESC_CHG_TO_ENG_MODE)
8312 hangul_input_state_set(0);
8313# endif
8314 if (composing_hangul)
8315 {
8316 push_raw_key(composing_hangul_buffer, 2);
8317 composing_hangul = 0;
8318 }
8319#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320
8321 temp = curwin->w_cursor.col;
8322 if (disabled_redraw)
8323 {
8324 --RedrawingDisabled;
8325 disabled_redraw = FALSE;
8326 }
8327 if (!arrow_used)
8328 {
8329 /*
8330 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008331 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8332 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 */
8334 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008335 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336
8337 /*
8338 * Repeating insert may take a long time. Check for
8339 * interrupt now and then.
8340 */
8341 if (*count > 0)
8342 {
8343 line_breakcheck();
8344 if (got_int)
8345 *count = 0;
8346 }
8347
8348 if (--*count > 0) /* repeat what was typed */
8349 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008350 /* Vi repeats the insert without replacing characters. */
8351 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8352 State &= ~REPLACE_FLAG;
8353
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 (void)start_redo_ins();
8355 if (cmdchar == 'r' || cmdchar == 'v')
8356 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8357 ++RedrawingDisabled;
8358 disabled_redraw = TRUE;
8359 return FALSE; /* repeat the insert */
8360 }
8361 stop_insert(&curwin->w_cursor, TRUE);
8362 undisplay_dollar();
8363 }
8364
8365 /* When an autoindent was removed, curswant stays after the
8366 * indent */
8367 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8368 curwin->w_set_curswant = TRUE;
8369
8370 /* Remember the last Insert position in the '^ mark. */
8371 if (!cmdmod.keepjumps)
8372 curbuf->b_last_insert = curwin->w_cursor;
8373
8374 /*
8375 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008376 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008377 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008378 if (!nomove
8379 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008380#ifdef FEAT_VIRTUALEDIT
8381 || curwin->w_cursor.coladd > 0
8382#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008383 )
8384 && (restart_edit == NUL
8385 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008387 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008389 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390#ifdef FEAT_RIGHTLEFT
8391 && !revins_on
8392#endif
8393 )
8394 {
8395#ifdef FEAT_VIRTUALEDIT
8396 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8397 {
8398 oneleft();
8399 if (restart_edit != NUL)
8400 ++curwin->w_cursor.coladd;
8401 }
8402 else
8403#endif
8404 {
8405 --curwin->w_cursor.col;
8406#ifdef FEAT_MBYTE
8407 /* Correct cursor for multi-byte character. */
8408 if (has_mbyte)
8409 mb_adjust_cursor();
8410#endif
8411 }
8412 }
8413
8414#ifdef USE_IM_CONTROL
8415 /* Disable IM to allow typing English directly for Normal mode commands.
8416 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8417 * well). */
8418 if (!(State & LANGMAP))
8419 im_save_status(&curbuf->b_p_iminsert);
8420 im_set_active(FALSE);
8421#endif
8422
8423 State = NORMAL;
8424 /* need to position cursor again (e.g. when on a TAB ) */
8425 changed_cline_bef_curs();
8426
8427#ifdef FEAT_MOUSE
8428 setmouse();
8429#endif
8430#ifdef CURSOR_SHAPE
8431 ui_cursor_shape(); /* may show different cursor shape */
8432#endif
8433
8434 /*
8435 * When recording or for CTRL-O, need to display the new mode.
8436 * Otherwise remove the mode message.
8437 */
8438 if (Recording || restart_edit != NUL)
8439 showmode();
8440 else if (p_smd)
8441 MSG("");
8442
8443 return TRUE; /* exit Insert mode */
8444}
8445
8446#ifdef FEAT_RIGHTLEFT
8447/*
8448 * Toggle language: hkmap and revins_on.
8449 * Move to end of reverse inserted text.
8450 */
8451 static void
8452ins_ctrl_()
8453{
8454 if (revins_on && revins_chars && revins_scol >= 0)
8455 {
8456 while (gchar_cursor() != NUL && revins_chars--)
8457 ++curwin->w_cursor.col;
8458 }
8459 p_ri = !p_ri;
8460 revins_on = (State == INSERT && p_ri);
8461 if (revins_on)
8462 {
8463 revins_scol = curwin->w_cursor.col;
8464 revins_legal++;
8465 revins_chars = 0;
8466 undisplay_dollar();
8467 }
8468 else
8469 revins_scol = -1;
8470#ifdef FEAT_FKMAP
8471 if (p_altkeymap)
8472 {
8473 /*
8474 * to be consistent also for redo command, using '.'
8475 * set arrow_used to true and stop it - causing to redo
8476 * characters entered in one mode (normal/reverse insert).
8477 */
8478 arrow_used = TRUE;
8479 (void)stop_arrow();
8480 p_fkmap = curwin->w_p_rl ^ p_ri;
8481 if (p_fkmap && p_ri)
8482 State = INSERT;
8483 }
8484 else
8485#endif
8486 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8487 showmode();
8488}
8489#endif
8490
8491#ifdef FEAT_VISUAL
8492/*
8493 * If 'keymodel' contains "startsel", may start selection.
8494 * Returns TRUE when a CTRL-O and other keys stuffed.
8495 */
8496 static int
8497ins_start_select(c)
8498 int c;
8499{
8500 if (km_startsel)
8501 switch (c)
8502 {
8503 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 case K_PAGEUP:
8506 case K_KPAGEUP:
8507 case K_PAGEDOWN:
8508 case K_KPAGEDOWN:
8509# ifdef MACOS
8510 case K_LEFT:
8511 case K_RIGHT:
8512 case K_UP:
8513 case K_DOWN:
8514 case K_END:
8515 case K_HOME:
8516# endif
8517 if (!(mod_mask & MOD_MASK_SHIFT))
8518 break;
8519 /* FALLTHROUGH */
8520 case K_S_LEFT:
8521 case K_S_RIGHT:
8522 case K_S_UP:
8523 case K_S_DOWN:
8524 case K_S_END:
8525 case K_S_HOME:
8526 /* Start selection right away, the cursor can move with
8527 * CTRL-O when beyond the end of the line. */
8528 start_selection();
8529
8530 /* Execute the key in (insert) Select mode. */
8531 stuffcharReadbuff(Ctrl_O);
8532 if (mod_mask)
8533 {
8534 char_u buf[4];
8535
8536 buf[0] = K_SPECIAL;
8537 buf[1] = KS_MODIFIER;
8538 buf[2] = mod_mask;
8539 buf[3] = NUL;
8540 stuffReadbuff(buf);
8541 }
8542 stuffcharReadbuff(c);
8543 return TRUE;
8544 }
8545 return FALSE;
8546}
8547#endif
8548
8549/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008550 * <Insert> key in Insert mode: toggle insert/remplace mode.
8551 */
8552 static void
8553ins_insert(replaceState)
8554 int replaceState;
8555{
8556#ifdef FEAT_FKMAP
8557 if (p_fkmap && p_ri)
8558 {
8559 beep_flush();
8560 EMSG(farsi_text_3); /* encoded in Farsi */
8561 return;
8562 }
8563#endif
8564
8565#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008566# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008567 set_vim_var_string(VV_INSERTMODE,
8568 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008569# ifdef FEAT_VREPLACE
8570 replaceState == VREPLACE ? "v" :
8571# endif
8572 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008573# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008574 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8575#endif
8576 if (State & REPLACE_FLAG)
8577 State = INSERT | (State & LANGMAP);
8578 else
8579 State = replaceState | (State & LANGMAP);
8580 AppendCharToRedobuff(K_INS);
8581 showmode();
8582#ifdef CURSOR_SHAPE
8583 ui_cursor_shape(); /* may show different cursor shape */
8584#endif
8585}
8586
8587/*
8588 * Pressed CTRL-O in Insert mode.
8589 */
8590 static void
8591ins_ctrl_o()
8592{
8593#ifdef FEAT_VREPLACE
8594 if (State & VREPLACE_FLAG)
8595 restart_edit = 'V';
8596 else
8597#endif
8598 if (State & REPLACE_FLAG)
8599 restart_edit = 'R';
8600 else
8601 restart_edit = 'I';
8602#ifdef FEAT_VIRTUALEDIT
8603 if (virtual_active())
8604 ins_at_eol = FALSE; /* cursor always keeps its column */
8605 else
8606#endif
8607 ins_at_eol = (gchar_cursor() == NUL);
8608}
8609
8610/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 * If the cursor is on an indent, ^T/^D insert/delete one
8612 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008613 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614 * with vi. But vi only supports ^T and ^D after an
8615 * autoindent, we support it everywhere.
8616 */
8617 static void
8618ins_shift(c, lastc)
8619 int c;
8620 int lastc;
8621{
8622 if (stop_arrow() == FAIL)
8623 return;
8624 AppendCharToRedobuff(c);
8625
8626 /*
8627 * 0^D and ^^D: remove all indent.
8628 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008629 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8630 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008631 {
8632 --curwin->w_cursor.col;
8633 (void)del_char(FALSE); /* delete the '^' or '0' */
8634 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8635 if (State & REPLACE_FLAG)
8636 replace_pop_ins();
8637 if (lastc == '^')
8638 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008639 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 }
8641 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008642 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643
8644 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8645 did_ai = FALSE;
8646#ifdef FEAT_SMARTINDENT
8647 did_si = FALSE;
8648 can_si = FALSE;
8649 can_si_back = FALSE;
8650#endif
8651#ifdef FEAT_CINDENT
8652 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8653#endif
8654}
8655
8656 static void
8657ins_del()
8658{
8659 int temp;
8660
8661 if (stop_arrow() == FAIL)
8662 return;
8663 if (gchar_cursor() == NUL) /* delete newline */
8664 {
8665 temp = curwin->w_cursor.col;
8666 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar81340392012-06-06 16:12:59 +02008667 || do_join(2, FALSE, TRUE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 vim_beep();
8669 else
8670 curwin->w_cursor.col = temp;
8671 }
8672 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8673 vim_beep();
8674 did_ai = FALSE;
8675#ifdef FEAT_SMARTINDENT
8676 did_si = FALSE;
8677 can_si = FALSE;
8678 can_si_back = FALSE;
8679#endif
8680 AppendCharToRedobuff(K_DEL);
8681}
8682
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008683static void ins_bs_one __ARGS((colnr_T *vcolp));
8684
8685/*
8686 * Delete one character for ins_bs().
8687 */
8688 static void
8689ins_bs_one(vcolp)
8690 colnr_T *vcolp;
8691{
8692 dec_cursor();
8693 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8694 if (State & REPLACE_FLAG)
8695 {
8696 /* Don't delete characters before the insert point when in
8697 * Replace mode */
8698 if (curwin->w_cursor.lnum != Insstart.lnum
8699 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008700 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008701 }
8702 else
8703 (void)del_char(FALSE);
8704}
8705
Bram Moolenaar071d4272004-06-13 20:20:40 +00008706/*
8707 * Handle Backspace, delete-word and delete-line in Insert mode.
8708 * Return TRUE when backspace was actually used.
8709 */
8710 static int
8711ins_bs(c, mode, inserted_space_p)
8712 int c;
8713 int mode;
8714 int *inserted_space_p;
8715{
8716 linenr_T lnum;
8717 int cc;
8718 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008719 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008720 colnr_T mincol;
8721 int did_backspace = FALSE;
8722 int in_indent;
8723 int oldState;
8724#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008725 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726#endif
8727
8728 /*
8729 * can't delete anything in an empty file
8730 * can't backup past first character in buffer
8731 * can't backup past starting point unless 'backspace' > 1
8732 * can backup to a previous line if 'backspace' == 0
8733 */
8734 if ( bufempty()
8735 || (
8736#ifdef FEAT_RIGHTLEFT
8737 !revins_on &&
8738#endif
8739 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8740 || (!can_bs(BS_START)
8741 && (arrow_used
8742 || (curwin->w_cursor.lnum == Insstart.lnum
8743 && curwin->w_cursor.col <= Insstart.col)))
8744 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8745 && curwin->w_cursor.col <= ai_col)
8746 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8747 {
8748 vim_beep();
8749 return FALSE;
8750 }
8751
8752 if (stop_arrow() == FAIL)
8753 return FALSE;
8754 in_indent = inindent(0);
8755#ifdef FEAT_CINDENT
8756 if (in_indent)
8757 can_cindent = FALSE;
8758#endif
8759#ifdef FEAT_COMMENTS
8760 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8761#endif
8762#ifdef FEAT_RIGHTLEFT
8763 if (revins_on) /* put cursor after last inserted char */
8764 inc_cursor();
8765#endif
8766
8767#ifdef FEAT_VIRTUALEDIT
8768 /* Virtualedit:
8769 * BACKSPACE_CHAR eats a virtual space
8770 * BACKSPACE_WORD eats all coladd
8771 * BACKSPACE_LINE eats all coladd and keeps going
8772 */
8773 if (curwin->w_cursor.coladd > 0)
8774 {
8775 if (mode == BACKSPACE_CHAR)
8776 {
8777 --curwin->w_cursor.coladd;
8778 return TRUE;
8779 }
8780 if (mode == BACKSPACE_WORD)
8781 {
8782 curwin->w_cursor.coladd = 0;
8783 return TRUE;
8784 }
8785 curwin->w_cursor.coladd = 0;
8786 }
8787#endif
8788
8789 /*
8790 * delete newline!
8791 */
8792 if (curwin->w_cursor.col == 0)
8793 {
8794 lnum = Insstart.lnum;
8795 if (curwin->w_cursor.lnum == Insstart.lnum
8796#ifdef FEAT_RIGHTLEFT
8797 || revins_on
8798#endif
8799 )
8800 {
8801 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8802 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8803 return FALSE;
8804 --Insstart.lnum;
8805 Insstart.col = MAXCOL;
8806 }
8807 /*
8808 * In replace mode:
8809 * cc < 0: NL was inserted, delete it
8810 * cc >= 0: NL was replaced, put original characters back
8811 */
8812 cc = -1;
8813 if (State & REPLACE_FLAG)
8814 cc = replace_pop(); /* returns -1 if NL was inserted */
8815 /*
8816 * In replace mode, in the line we started replacing, we only move the
8817 * cursor.
8818 */
8819 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8820 {
8821 dec_cursor();
8822 }
8823 else
8824 {
8825#ifdef FEAT_VREPLACE
8826 if (!(State & VREPLACE_FLAG)
8827 || curwin->w_cursor.lnum > orig_line_count)
8828#endif
8829 {
8830 temp = gchar_cursor(); /* remember current char */
8831 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008832
8833 /* When "aw" is in 'formatoptions' we must delete the space at
8834 * the end of the line, otherwise the line will be broken
8835 * again when auto-formatting. */
8836 if (has_format_option(FO_AUTO)
8837 && has_format_option(FO_WHITE_PAR))
8838 {
8839 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8840 TRUE);
8841 int len;
8842
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008843 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008844 if (len > 0 && ptr[len - 1] == ' ')
8845 ptr[len - 1] = NUL;
8846 }
8847
Bram Moolenaar81340392012-06-06 16:12:59 +02008848 (void)do_join(2, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 if (temp == NUL && gchar_cursor() != NUL)
8850 inc_cursor();
8851 }
8852#ifdef FEAT_VREPLACE
8853 else
8854 dec_cursor();
8855#endif
8856
8857 /*
8858 * In REPLACE mode we have to put back the text that was replaced
8859 * by the NL. On the replace stack is first a NUL-terminated
8860 * sequence of characters that were deleted and then the
8861 * characters that NL replaced.
8862 */
8863 if (State & REPLACE_FLAG)
8864 {
8865 /*
8866 * Do the next ins_char() in NORMAL state, to
8867 * prevent ins_char() from replacing characters and
8868 * avoiding showmatch().
8869 */
8870 oldState = State;
8871 State = NORMAL;
8872 /*
8873 * restore characters (blanks) deleted after cursor
8874 */
8875 while (cc > 0)
8876 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008877 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878#ifdef FEAT_MBYTE
8879 mb_replace_pop_ins(cc);
8880#else
8881 ins_char(cc);
8882#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008883 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 cc = replace_pop();
8885 }
8886 /* restore the characters that NL replaced */
8887 replace_pop_ins();
8888 State = oldState;
8889 }
8890 }
8891 did_ai = FALSE;
8892 }
8893 else
8894 {
8895 /*
8896 * Delete character(s) before the cursor.
8897 */
8898#ifdef FEAT_RIGHTLEFT
8899 if (revins_on) /* put cursor on last inserted char */
8900 dec_cursor();
8901#endif
8902 mincol = 0;
8903 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008904 if (mode == BACKSPACE_LINE
8905 && (curbuf->b_p_ai
8906#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008907 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008908#endif
8909 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910#ifdef FEAT_RIGHTLEFT
8911 && !revins_on
8912#endif
8913 )
8914 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008915 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008917 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008919 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 }
8921
8922 /*
8923 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8924 */
8925 if ( mode == BACKSPACE_CHAR
8926 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008927 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008928 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008929 && (*(ml_get_cursor() - 1) == TAB
8930 || (*(ml_get_cursor() - 1) == ' '
8931 && (!*inserted_space_p
8932 || arrow_used))))))
8933 {
8934 int ts;
8935 colnr_T vcol;
8936 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008937 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938
8939 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008940 if (p_sta && in_indent)
Bram Moolenaar14f24742012-08-08 18:01:05 +02008941 ts = (int)get_sw_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008943 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 /* Compute the virtual column where we want to be. Since
8945 * 'showbreak' may get in the way, need to get the last column of
8946 * the previous character. */
8947 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008948 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 dec_cursor();
8950 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8951 inc_cursor();
8952 want_vcol = (want_vcol / ts) * ts;
8953
8954 /* delete characters until we are at or before want_vcol */
8955 while (vcol > want_vcol
8956 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008957 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008958
8959 /* insert extra spaces until we are at want_vcol */
8960 while (vcol < want_vcol)
8961 {
8962 /* Remember the first char we inserted */
8963 if (curwin->w_cursor.lnum == Insstart.lnum
8964 && curwin->w_cursor.col < Insstart.col)
8965 Insstart.col = curwin->w_cursor.col;
8966
8967#ifdef FEAT_VREPLACE
8968 if (State & VREPLACE_FLAG)
8969 ins_char(' ');
8970 else
8971#endif
8972 {
8973 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008974 if ((State & REPLACE_FLAG))
8975 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008976 }
8977 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8978 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008979
8980 /* If we are now back where we started delete one character. Can
8981 * happen when using 'sts' and 'linebreak'. */
8982 if (vcol >= start_vcol)
8983 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984 }
8985
8986 /*
8987 * Delete upto starting point, start of line or previous word.
8988 */
8989 else do
8990 {
8991#ifdef FEAT_RIGHTLEFT
8992 if (!revins_on) /* put cursor on char to be deleted */
8993#endif
8994 dec_cursor();
8995
8996 /* start of word? */
8997 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8998 {
8999 mode = BACKSPACE_WORD_NOT_SPACE;
9000 temp = vim_iswordc(gchar_cursor());
9001 }
9002 /* end of word? */
9003 else if (mode == BACKSPACE_WORD_NOT_SPACE
9004 && (vim_isspace(cc = gchar_cursor())
9005 || vim_iswordc(cc) != temp))
9006 {
9007#ifdef FEAT_RIGHTLEFT
9008 if (!revins_on)
9009#endif
9010 inc_cursor();
9011#ifdef FEAT_RIGHTLEFT
9012 else if (State & REPLACE_FLAG)
9013 dec_cursor();
9014#endif
9015 break;
9016 }
9017 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009018 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019 else
9020 {
9021#ifdef FEAT_MBYTE
9022 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009023 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024#endif
9025 (void)del_char(FALSE);
9026#ifdef FEAT_MBYTE
9027 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009028 * If there are combining characters and 'delcombine' is set
9029 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00009030 * character.
9031 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009032 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009033 inc_cursor();
9034#endif
9035#ifdef FEAT_RIGHTLEFT
9036 if (revins_chars)
9037 {
9038 revins_chars--;
9039 revins_legal++;
9040 }
9041 if (revins_on && gchar_cursor() == NUL)
9042 break;
9043#endif
9044 }
9045 /* Just a single backspace?: */
9046 if (mode == BACKSPACE_CHAR)
9047 break;
9048 } while (
9049#ifdef FEAT_RIGHTLEFT
9050 revins_on ||
9051#endif
9052 (curwin->w_cursor.col > mincol
9053 && (curwin->w_cursor.lnum != Insstart.lnum
9054 || curwin->w_cursor.col != Insstart.col)));
9055 did_backspace = TRUE;
9056 }
9057#ifdef FEAT_SMARTINDENT
9058 did_si = FALSE;
9059 can_si = FALSE;
9060 can_si_back = FALSE;
9061#endif
9062 if (curwin->w_cursor.col <= 1)
9063 did_ai = FALSE;
9064 /*
9065 * It's a little strange to put backspaces into the redo
9066 * buffer, but it makes auto-indent a lot easier to deal
9067 * with.
9068 */
9069 AppendCharToRedobuff(c);
9070
9071 /* If deleted before the insertion point, adjust it */
9072 if (curwin->w_cursor.lnum == Insstart.lnum
9073 && curwin->w_cursor.col < Insstart.col)
9074 Insstart.col = curwin->w_cursor.col;
9075
9076 /* vi behaviour: the cursor moves backward but the character that
9077 * was there remains visible
9078 * Vim behaviour: the cursor moves backward and the character that
9079 * was there is erased from the screen.
9080 * We can emulate the vi behaviour by pretending there is a dollar
9081 * displayed even when there isn't.
9082 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009083 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084 dollar_vcol = curwin->w_virtcol;
9085
Bram Moolenaarce3be472008-01-14 19:12:28 +00009086#ifdef FEAT_FOLDING
9087 /* When deleting a char the cursor line must never be in a closed fold.
9088 * E.g., when 'foldmethod' is indent and deleting the first non-white
9089 * char before a Tab. */
9090 if (did_backspace)
9091 foldOpenCursor();
9092#endif
9093
Bram Moolenaar071d4272004-06-13 20:20:40 +00009094 return did_backspace;
9095}
9096
9097#ifdef FEAT_MOUSE
9098 static void
9099ins_mouse(c)
9100 int c;
9101{
9102 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009103 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009104
9105# ifdef FEAT_GUI
9106 /* When GUI is active, also move/paste when 'mouse' is empty */
9107 if (!gui.in_use)
9108# endif
9109 if (!mouse_has(MOUSE_INSERT))
9110 return;
9111
9112 undisplay_dollar();
9113 tpos = curwin->w_cursor;
9114 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9115 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009116#ifdef FEAT_WINDOWS
9117 win_T *new_curwin = curwin;
9118
9119 if (curwin != old_curwin && win_valid(old_curwin))
9120 {
9121 /* Mouse took us to another window. We need to go back to the
9122 * previous one to stop insert there properly. */
9123 curwin = old_curwin;
9124 curbuf = curwin->w_buffer;
9125 }
9126#endif
9127 start_arrow(curwin == old_curwin ? &tpos : NULL);
9128#ifdef FEAT_WINDOWS
9129 if (curwin != new_curwin && win_valid(new_curwin))
9130 {
9131 curwin = new_curwin;
9132 curbuf = curwin->w_buffer;
9133 }
9134#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009135# ifdef FEAT_CINDENT
9136 can_cindent = TRUE;
9137# endif
9138 }
9139
9140#ifdef FEAT_WINDOWS
9141 /* redraw status lines (in case another window became active) */
9142 redraw_statuslines();
9143#endif
9144}
9145
9146 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009147ins_mousescroll(dir)
9148 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009149{
9150 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009151# if defined(FEAT_WINDOWS)
9152 win_T *old_curwin = curwin;
9153# endif
9154# ifdef FEAT_INS_EXPAND
9155 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156# endif
9157
9158 tpos = curwin->w_cursor;
9159
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009160# ifdef FEAT_WINDOWS
9161 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 {
9163 int row, col;
9164
9165 row = mouse_row;
9166 col = mouse_col;
9167
9168 /* find the window at the pointer coordinates */
9169 curwin = mouse_find_win(&row, &col);
9170 curbuf = curwin->w_buffer;
9171 }
9172 if (curwin == old_curwin)
9173# endif
9174 undisplay_dollar();
9175
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009176# ifdef FEAT_INS_EXPAND
9177 /* Don't scroll the window in which completion is being done. */
9178 if (!pum_visible()
9179# if defined(FEAT_WINDOWS)
9180 || curwin != old_curwin
9181# endif
9182 )
9183# endif
9184 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009185 if (dir == MSCR_DOWN || dir == MSCR_UP)
9186 {
9187 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9188 scroll_redraw(dir,
9189 (long)(curwin->w_botline - curwin->w_topline));
9190 else
9191 scroll_redraw(dir, 3L);
9192 }
9193#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009194 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009195 {
9196 int val, step = 6;
9197
9198 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9199 step = W_WIDTH(curwin);
9200 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9201 if (val < 0)
9202 val = 0;
9203 gui_do_horiz_scroll(val, TRUE);
9204 }
9205#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009206# ifdef FEAT_INS_EXPAND
9207 did_scroll = TRUE;
9208# endif
9209 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009211# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212 curwin->w_redr_status = TRUE;
9213
9214 curwin = old_curwin;
9215 curbuf = curwin->w_buffer;
9216# endif
9217
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009218# ifdef FEAT_INS_EXPAND
9219 /* The popup menu may overlay the window, need to redraw it.
9220 * TODO: Would be more efficient to only redraw the windows that are
9221 * overlapped by the popup menu. */
9222 if (pum_visible() && did_scroll)
9223 {
9224 redraw_all_later(NOT_VALID);
9225 ins_compl_show_pum();
9226 }
9227# endif
9228
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 if (!equalpos(curwin->w_cursor, tpos))
9230 {
9231 start_arrow(&tpos);
9232# ifdef FEAT_CINDENT
9233 can_cindent = TRUE;
9234# endif
9235 }
9236}
9237#endif
9238
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009239#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009240 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009241ins_tabline(c)
9242 int c;
9243{
9244 /* We will be leaving the current window, unless closing another tab. */
9245 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9246 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9247 {
9248 undisplay_dollar();
9249 start_arrow(&curwin->w_cursor);
9250# ifdef FEAT_CINDENT
9251 can_cindent = TRUE;
9252# endif
9253 }
9254
9255 if (c == K_TABLINE)
9256 goto_tabpage(current_tab);
9257 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009258 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009259 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009260 redraw_statuslines(); /* will redraw the tabline when needed */
9261 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009262}
9263#endif
9264
9265#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 void
9267ins_scroll()
9268{
9269 pos_T tpos;
9270
9271 undisplay_dollar();
9272 tpos = curwin->w_cursor;
9273 if (gui_do_scroll())
9274 {
9275 start_arrow(&tpos);
9276# ifdef FEAT_CINDENT
9277 can_cindent = TRUE;
9278# endif
9279 }
9280}
9281
9282 void
9283ins_horscroll()
9284{
9285 pos_T tpos;
9286
9287 undisplay_dollar();
9288 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009289 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009290 {
9291 start_arrow(&tpos);
9292# ifdef FEAT_CINDENT
9293 can_cindent = TRUE;
9294# endif
9295 }
9296}
9297#endif
9298
9299 static void
9300ins_left()
9301{
9302 pos_T tpos;
9303
9304#ifdef FEAT_FOLDING
9305 if ((fdo_flags & FDO_HOR) && KeyTyped)
9306 foldOpenCursor();
9307#endif
9308 undisplay_dollar();
9309 tpos = curwin->w_cursor;
9310 if (oneleft() == OK)
9311 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009312#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9313 /* Only call start_arrow() when not busy with preediting, it will
9314 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9315 if (!im_is_preediting())
9316#endif
9317 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009318#ifdef FEAT_RIGHTLEFT
9319 /* If exit reversed string, position is fixed */
9320 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9321 revins_legal++;
9322 revins_chars++;
9323#endif
9324 }
9325
9326 /*
9327 * if 'whichwrap' set for cursor in insert mode may go to
9328 * previous line
9329 */
9330 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9331 {
9332 start_arrow(&tpos);
9333 --(curwin->w_cursor.lnum);
9334 coladvance((colnr_T)MAXCOL);
9335 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9336 }
9337 else
9338 vim_beep();
9339}
9340
9341 static void
9342ins_home(c)
9343 int c;
9344{
9345 pos_T tpos;
9346
9347#ifdef FEAT_FOLDING
9348 if ((fdo_flags & FDO_HOR) && KeyTyped)
9349 foldOpenCursor();
9350#endif
9351 undisplay_dollar();
9352 tpos = curwin->w_cursor;
9353 if (c == K_C_HOME)
9354 curwin->w_cursor.lnum = 1;
9355 curwin->w_cursor.col = 0;
9356#ifdef FEAT_VIRTUALEDIT
9357 curwin->w_cursor.coladd = 0;
9358#endif
9359 curwin->w_curswant = 0;
9360 start_arrow(&tpos);
9361}
9362
9363 static void
9364ins_end(c)
9365 int c;
9366{
9367 pos_T tpos;
9368
9369#ifdef FEAT_FOLDING
9370 if ((fdo_flags & FDO_HOR) && KeyTyped)
9371 foldOpenCursor();
9372#endif
9373 undisplay_dollar();
9374 tpos = curwin->w_cursor;
9375 if (c == K_C_END)
9376 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9377 coladvance((colnr_T)MAXCOL);
9378 curwin->w_curswant = MAXCOL;
9379
9380 start_arrow(&tpos);
9381}
9382
9383 static void
9384ins_s_left()
9385{
9386#ifdef FEAT_FOLDING
9387 if ((fdo_flags & FDO_HOR) && KeyTyped)
9388 foldOpenCursor();
9389#endif
9390 undisplay_dollar();
9391 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9392 {
9393 start_arrow(&curwin->w_cursor);
9394 (void)bck_word(1L, FALSE, FALSE);
9395 curwin->w_set_curswant = TRUE;
9396 }
9397 else
9398 vim_beep();
9399}
9400
9401 static void
9402ins_right()
9403{
9404#ifdef FEAT_FOLDING
9405 if ((fdo_flags & FDO_HOR) && KeyTyped)
9406 foldOpenCursor();
9407#endif
9408 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009409 if (gchar_cursor() != NUL
9410#ifdef FEAT_VIRTUALEDIT
9411 || virtual_active()
9412#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009413 )
9414 {
9415 start_arrow(&curwin->w_cursor);
9416 curwin->w_set_curswant = TRUE;
9417#ifdef FEAT_VIRTUALEDIT
9418 if (virtual_active())
9419 oneright();
9420 else
9421#endif
9422 {
9423#ifdef FEAT_MBYTE
9424 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009425 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009426 else
9427#endif
9428 ++curwin->w_cursor.col;
9429 }
9430
9431#ifdef FEAT_RIGHTLEFT
9432 revins_legal++;
9433 if (revins_chars)
9434 revins_chars--;
9435#endif
9436 }
9437 /* if 'whichwrap' set for cursor in insert mode, may move the
9438 * cursor to the next line */
9439 else if (vim_strchr(p_ww, ']') != NULL
9440 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9441 {
9442 start_arrow(&curwin->w_cursor);
9443 curwin->w_set_curswant = TRUE;
9444 ++curwin->w_cursor.lnum;
9445 curwin->w_cursor.col = 0;
9446 }
9447 else
9448 vim_beep();
9449}
9450
9451 static void
9452ins_s_right()
9453{
9454#ifdef FEAT_FOLDING
9455 if ((fdo_flags & FDO_HOR) && KeyTyped)
9456 foldOpenCursor();
9457#endif
9458 undisplay_dollar();
9459 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9460 || gchar_cursor() != NUL)
9461 {
9462 start_arrow(&curwin->w_cursor);
9463 (void)fwd_word(1L, FALSE, 0);
9464 curwin->w_set_curswant = TRUE;
9465 }
9466 else
9467 vim_beep();
9468}
9469
9470 static void
9471ins_up(startcol)
9472 int startcol; /* when TRUE move to Insstart.col */
9473{
9474 pos_T tpos;
9475 linenr_T old_topline = curwin->w_topline;
9476#ifdef FEAT_DIFF
9477 int old_topfill = curwin->w_topfill;
9478#endif
9479
9480 undisplay_dollar();
9481 tpos = curwin->w_cursor;
9482 if (cursor_up(1L, TRUE) == OK)
9483 {
9484 if (startcol)
9485 coladvance(getvcol_nolist(&Insstart));
9486 if (old_topline != curwin->w_topline
9487#ifdef FEAT_DIFF
9488 || old_topfill != curwin->w_topfill
9489#endif
9490 )
9491 redraw_later(VALID);
9492 start_arrow(&tpos);
9493#ifdef FEAT_CINDENT
9494 can_cindent = TRUE;
9495#endif
9496 }
9497 else
9498 vim_beep();
9499}
9500
9501 static void
9502ins_pageup()
9503{
9504 pos_T tpos;
9505
9506 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009507
9508#ifdef FEAT_WINDOWS
9509 if (mod_mask & MOD_MASK_CTRL)
9510 {
9511 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009512 if (first_tabpage->tp_next != NULL)
9513 {
9514 start_arrow(&curwin->w_cursor);
9515 goto_tabpage(-1);
9516 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009517 return;
9518 }
9519#endif
9520
Bram Moolenaar071d4272004-06-13 20:20:40 +00009521 tpos = curwin->w_cursor;
9522 if (onepage(BACKWARD, 1L) == OK)
9523 {
9524 start_arrow(&tpos);
9525#ifdef FEAT_CINDENT
9526 can_cindent = TRUE;
9527#endif
9528 }
9529 else
9530 vim_beep();
9531}
9532
9533 static void
9534ins_down(startcol)
9535 int startcol; /* when TRUE move to Insstart.col */
9536{
9537 pos_T tpos;
9538 linenr_T old_topline = curwin->w_topline;
9539#ifdef FEAT_DIFF
9540 int old_topfill = curwin->w_topfill;
9541#endif
9542
9543 undisplay_dollar();
9544 tpos = curwin->w_cursor;
9545 if (cursor_down(1L, TRUE) == OK)
9546 {
9547 if (startcol)
9548 coladvance(getvcol_nolist(&Insstart));
9549 if (old_topline != curwin->w_topline
9550#ifdef FEAT_DIFF
9551 || old_topfill != curwin->w_topfill
9552#endif
9553 )
9554 redraw_later(VALID);
9555 start_arrow(&tpos);
9556#ifdef FEAT_CINDENT
9557 can_cindent = TRUE;
9558#endif
9559 }
9560 else
9561 vim_beep();
9562}
9563
9564 static void
9565ins_pagedown()
9566{
9567 pos_T tpos;
9568
9569 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009570
9571#ifdef FEAT_WINDOWS
9572 if (mod_mask & MOD_MASK_CTRL)
9573 {
9574 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009575 if (first_tabpage->tp_next != NULL)
9576 {
9577 start_arrow(&curwin->w_cursor);
9578 goto_tabpage(0);
9579 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009580 return;
9581 }
9582#endif
9583
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584 tpos = curwin->w_cursor;
9585 if (onepage(FORWARD, 1L) == OK)
9586 {
9587 start_arrow(&tpos);
9588#ifdef FEAT_CINDENT
9589 can_cindent = TRUE;
9590#endif
9591 }
9592 else
9593 vim_beep();
9594}
9595
9596#ifdef FEAT_DND
9597 static void
9598ins_drop()
9599{
9600 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9601}
9602#endif
9603
9604/*
9605 * Handle TAB in Insert or Replace mode.
9606 * Return TRUE when the TAB needs to be inserted like a normal character.
9607 */
9608 static int
9609ins_tab()
9610{
9611 int ind;
9612 int i;
9613 int temp;
9614
9615 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9616 Insstart_blank_vcol = get_nolist_virtcol();
9617 if (echeck_abbr(TAB + ABBR_OFF))
9618 return FALSE;
9619
9620 ind = inindent(0);
9621#ifdef FEAT_CINDENT
9622 if (ind)
9623 can_cindent = FALSE;
9624#endif
9625
9626 /*
9627 * When nothing special, insert TAB like a normal character
9628 */
9629 if (!curbuf->b_p_et
Bram Moolenaar14f24742012-08-08 18:01:05 +02009630 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value())
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009631 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632 return TRUE;
9633
9634 if (stop_arrow() == FAIL)
9635 return TRUE;
9636
9637 did_ai = FALSE;
9638#ifdef FEAT_SMARTINDENT
9639 did_si = FALSE;
9640 can_si = FALSE;
9641 can_si_back = FALSE;
9642#endif
9643 AppendToRedobuff((char_u *)"\t");
9644
9645 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar14f24742012-08-08 18:01:05 +02009646 temp = (int)get_sw_value();
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009647 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9648 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 else /* otherwise use 'tabstop' */
9650 temp = (int)curbuf->b_p_ts;
9651 temp -= get_nolist_virtcol() % temp;
9652
9653 /*
9654 * Insert the first space with ins_char(). It will delete one char in
9655 * replace mode. Insert the rest with ins_str(); it will not delete any
9656 * chars. For VREPLACE mode, we use ins_char() for all characters.
9657 */
9658 ins_char(' ');
9659 while (--temp > 0)
9660 {
9661#ifdef FEAT_VREPLACE
9662 if (State & VREPLACE_FLAG)
9663 ins_char(' ');
9664 else
9665#endif
9666 {
9667 ins_str((char_u *)" ");
9668 if (State & REPLACE_FLAG) /* no char replaced */
9669 replace_push(NUL);
9670 }
9671 }
9672
9673 /*
9674 * When 'expandtab' not set: Replace spaces by TABs where possible.
9675 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009676 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 {
9678 char_u *ptr;
9679#ifdef FEAT_VREPLACE
9680 char_u *saved_line = NULL; /* init for GCC */
9681 pos_T pos;
9682#endif
9683 pos_T fpos;
9684 pos_T *cursor;
9685 colnr_T want_vcol, vcol;
9686 int change_col = -1;
9687 int save_list = curwin->w_p_list;
9688
9689 /*
9690 * Get the current line. For VREPLACE mode, don't make real changes
9691 * yet, just work on a copy of the line.
9692 */
9693#ifdef FEAT_VREPLACE
9694 if (State & VREPLACE_FLAG)
9695 {
9696 pos = curwin->w_cursor;
9697 cursor = &pos;
9698 saved_line = vim_strsave(ml_get_curline());
9699 if (saved_line == NULL)
9700 return FALSE;
9701 ptr = saved_line + pos.col;
9702 }
9703 else
9704#endif
9705 {
9706 ptr = ml_get_cursor();
9707 cursor = &curwin->w_cursor;
9708 }
9709
9710 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9711 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9712 curwin->w_p_list = FALSE;
9713
9714 /* Find first white before the cursor */
9715 fpos = curwin->w_cursor;
9716 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9717 {
9718 --fpos.col;
9719 --ptr;
9720 }
9721
9722 /* In Replace mode, don't change characters before the insert point. */
9723 if ((State & REPLACE_FLAG)
9724 && fpos.lnum == Insstart.lnum
9725 && fpos.col < Insstart.col)
9726 {
9727 ptr += Insstart.col - fpos.col;
9728 fpos.col = Insstart.col;
9729 }
9730
9731 /* compute virtual column numbers of first white and cursor */
9732 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9733 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9734
9735 /* Use as many TABs as possible. Beware of 'showbreak' and
9736 * 'linebreak' adding extra virtual columns. */
9737 while (vim_iswhite(*ptr))
9738 {
9739 i = lbr_chartabsize((char_u *)"\t", vcol);
9740 if (vcol + i > want_vcol)
9741 break;
9742 if (*ptr != TAB)
9743 {
9744 *ptr = TAB;
9745 if (change_col < 0)
9746 {
9747 change_col = fpos.col; /* Column of first change */
9748 /* May have to adjust Insstart */
9749 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9750 Insstart.col = fpos.col;
9751 }
9752 }
9753 ++fpos.col;
9754 ++ptr;
9755 vcol += i;
9756 }
9757
9758 if (change_col >= 0)
9759 {
9760 int repl_off = 0;
9761
9762 /* Skip over the spaces we need. */
9763 while (vcol < want_vcol && *ptr == ' ')
9764 {
9765 vcol += lbr_chartabsize(ptr, vcol);
9766 ++ptr;
9767 ++repl_off;
9768 }
9769 if (vcol > want_vcol)
9770 {
9771 /* Must have a char with 'showbreak' just before it. */
9772 --ptr;
9773 --repl_off;
9774 }
9775 fpos.col += repl_off;
9776
9777 /* Delete following spaces. */
9778 i = cursor->col - fpos.col;
9779 if (i > 0)
9780 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009781 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 /* correct replace stack. */
9783 if ((State & REPLACE_FLAG)
9784#ifdef FEAT_VREPLACE
9785 && !(State & VREPLACE_FLAG)
9786#endif
9787 )
9788 for (temp = i; --temp >= 0; )
9789 replace_join(repl_off);
9790 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009791#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009792 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009793 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009794 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009795 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9796 (char_u *)"\t", 1);
9797 }
9798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 cursor->col -= i;
9800
9801#ifdef FEAT_VREPLACE
9802 /*
9803 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9804 * backspacing over the changed spacing and then inserting the new
9805 * spacing.
9806 */
9807 if (State & VREPLACE_FLAG)
9808 {
9809 /* Backspace from real cursor to change_col */
9810 backspace_until_column(change_col);
9811
9812 /* Insert each char in saved_line from changed_col to
9813 * ptr-cursor */
9814 ins_bytes_len(saved_line + change_col,
9815 cursor->col - change_col);
9816 }
9817#endif
9818 }
9819
9820#ifdef FEAT_VREPLACE
9821 if (State & VREPLACE_FLAG)
9822 vim_free(saved_line);
9823#endif
9824 curwin->w_p_list = save_list;
9825 }
9826
9827 return FALSE;
9828}
9829
9830/*
9831 * Handle CR or NL in insert mode.
9832 * Return TRUE when out of memory or can't undo.
9833 */
9834 static int
9835ins_eol(c)
9836 int c;
9837{
9838 int i;
9839
9840 if (echeck_abbr(c + ABBR_OFF))
9841 return FALSE;
9842 if (stop_arrow() == FAIL)
9843 return TRUE;
9844 undisplay_dollar();
9845
9846 /*
9847 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9848 * character under the cursor. Only push a NUL on the replace stack,
9849 * nothing to put back when the NL is deleted.
9850 */
9851 if ((State & REPLACE_FLAG)
9852#ifdef FEAT_VREPLACE
9853 && !(State & VREPLACE_FLAG)
9854#endif
9855 )
9856 replace_push(NUL);
9857
9858 /*
9859 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9860 * replacing the next line, so we push all of the characters left on the
9861 * line onto the replace stack. This is not done here though, it is done
9862 * in open_line().
9863 */
9864
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009865#ifdef FEAT_VIRTUALEDIT
9866 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9867 * CTRL-O). */
9868 if (virtual_active() && curwin->w_cursor.coladd > 0)
9869 coladvance(getviscol());
9870#endif
9871
Bram Moolenaar071d4272004-06-13 20:20:40 +00009872#ifdef FEAT_RIGHTLEFT
9873# ifdef FEAT_FKMAP
9874 if (p_altkeymap && p_fkmap)
9875 fkmap(NL);
9876# endif
9877 /* NL in reverse insert will always start in the end of
9878 * current line. */
9879 if (revins_on)
9880 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9881#endif
9882
9883 AppendToRedobuff(NL_STR);
9884 i = open_line(FORWARD,
9885#ifdef FEAT_COMMENTS
9886 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9887#endif
9888 0, old_indent);
9889 old_indent = 0;
9890#ifdef FEAT_CINDENT
9891 can_cindent = TRUE;
9892#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009893#ifdef FEAT_FOLDING
9894 /* When inserting a line the cursor line must never be in a closed fold. */
9895 foldOpenCursor();
9896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009897
9898 return (!i);
9899}
9900
9901#ifdef FEAT_DIGRAPHS
9902/*
9903 * Handle digraph in insert mode.
9904 * Returns character still to be inserted, or NUL when nothing remaining to be
9905 * done.
9906 */
9907 static int
9908ins_digraph()
9909{
9910 int c;
9911 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009912 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009913
9914 pc_status = PC_STATUS_UNSET;
9915 if (redrawing() && !char_avail())
9916 {
9917 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009918 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009919
9920 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009921 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009922#ifdef FEAT_CMDL_INFO
9923 add_to_showcmd_c(Ctrl_K);
9924#endif
9925 }
9926
9927#ifdef USE_ON_FLY_SCROLL
9928 dont_scroll = TRUE; /* disallow scrolling here */
9929#endif
9930
9931 /* don't map the digraph chars. This also prevents the
9932 * mode message to be deleted when ESC is hit */
9933 ++no_mapping;
9934 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009935 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009936 --no_mapping;
9937 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009938 if (did_putchar)
9939 /* when the line fits in 'columns' the '?' is at the start of the next
9940 * line and will not be removed by the redraw */
9941 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009942
Bram Moolenaar071d4272004-06-13 20:20:40 +00009943 if (IS_SPECIAL(c) || mod_mask) /* special key */
9944 {
9945#ifdef FEAT_CMDL_INFO
9946 clear_showcmd();
9947#endif
9948 insert_special(c, TRUE, FALSE);
9949 return NUL;
9950 }
9951 if (c != ESC)
9952 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009953 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009954 if (redrawing() && !char_avail())
9955 {
9956 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009957 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009958
9959 if (char2cells(c) == 1)
9960 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009961 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009963 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009964 }
9965#ifdef FEAT_CMDL_INFO
9966 add_to_showcmd_c(c);
9967#endif
9968 }
9969 ++no_mapping;
9970 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009971 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009972 --no_mapping;
9973 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009974 if (did_putchar)
9975 /* when the line fits in 'columns' the '?' is at the start of the
9976 * next line and will not be removed by a redraw */
9977 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009978 if (cc != ESC)
9979 {
9980 AppendToRedobuff((char_u *)CTRL_V_STR);
9981 c = getdigraph(c, cc, TRUE);
9982#ifdef FEAT_CMDL_INFO
9983 clear_showcmd();
9984#endif
9985 return c;
9986 }
9987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988#ifdef FEAT_CMDL_INFO
9989 clear_showcmd();
9990#endif
9991 return NUL;
9992}
9993#endif
9994
9995/*
9996 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9997 * Returns the char to be inserted, or NUL if none found.
9998 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02009999 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000010000ins_copychar(lnum)
10001 linenr_T lnum;
10002{
10003 int c;
10004 int temp;
10005 char_u *ptr, *prev_ptr;
10006
10007 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10008 {
10009 vim_beep();
10010 return NUL;
10011 }
10012
10013 /* try to advance to the cursor column */
10014 temp = 0;
10015 ptr = ml_get(lnum);
10016 prev_ptr = ptr;
10017 validate_virtcol();
10018 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10019 {
10020 prev_ptr = ptr;
10021 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
10022 }
10023 if ((colnr_T)temp > curwin->w_virtcol)
10024 ptr = prev_ptr;
10025
10026#ifdef FEAT_MBYTE
10027 c = (*mb_ptr2char)(ptr);
10028#else
10029 c = *ptr;
10030#endif
10031 if (c == NUL)
10032 vim_beep();
10033 return c;
10034}
10035
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010036/*
10037 * CTRL-Y or CTRL-E typed in Insert mode.
10038 */
10039 static int
10040ins_ctrl_ey(tc)
10041 int tc;
10042{
10043 int c = tc;
10044
10045#ifdef FEAT_INS_EXPAND
10046 if (ctrl_x_mode == CTRL_X_SCROLL)
10047 {
10048 if (c == Ctrl_Y)
10049 scrolldown_clamp();
10050 else
10051 scrollup_clamp();
10052 redraw_later(VALID);
10053 }
10054 else
10055#endif
10056 {
10057 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10058 if (c != NUL)
10059 {
10060 long tw_save;
10061
10062 /* The character must be taken literally, insert like it
10063 * was typed after a CTRL-V, and pretend 'textwidth'
10064 * wasn't set. Digits, 'o' and 'x' are special after a
10065 * CTRL-V, don't use it for these. */
10066 if (c < 256 && !isalnum(c))
10067 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10068 tw_save = curbuf->b_p_tw;
10069 curbuf->b_p_tw = -1;
10070 insert_special(c, TRUE, FALSE);
10071 curbuf->b_p_tw = tw_save;
10072#ifdef FEAT_RIGHTLEFT
10073 revins_chars++;
10074 revins_legal++;
10075#endif
10076 c = Ctrl_V; /* pretend CTRL-V is last character */
10077 auto_format(FALSE, TRUE);
10078 }
10079 }
10080 return c;
10081}
10082
Bram Moolenaar071d4272004-06-13 20:20:40 +000010083#ifdef FEAT_SMARTINDENT
10084/*
10085 * Try to do some very smart auto-indenting.
10086 * Used when inserting a "normal" character.
10087 */
10088 static void
10089ins_try_si(c)
10090 int c;
10091{
10092 pos_T *pos, old_pos;
10093 char_u *ptr;
10094 int i;
10095 int temp;
10096
10097 /*
10098 * do some very smart indenting when entering '{' or '}'
10099 */
10100 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10101 {
10102 /*
10103 * for '}' set indent equal to indent of line containing matching '{'
10104 */
10105 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10106 {
10107 old_pos = curwin->w_cursor;
10108 /*
10109 * If the matching '{' has a ')' immediately before it (ignoring
10110 * white-space), then line up with the start of the line
10111 * containing the matching '(' if there is one. This handles the
10112 * case where an "if (..\n..) {" statement continues over multiple
10113 * lines -- webb
10114 */
10115 ptr = ml_get(pos->lnum);
10116 i = pos->col;
10117 if (i > 0) /* skip blanks before '{' */
10118 while (--i > 0 && vim_iswhite(ptr[i]))
10119 ;
10120 curwin->w_cursor.lnum = pos->lnum;
10121 curwin->w_cursor.col = i;
10122 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10123 curwin->w_cursor = *pos;
10124 i = get_indent();
10125 curwin->w_cursor = old_pos;
10126#ifdef FEAT_VREPLACE
10127 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010128 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010129 else
10130#endif
10131 (void)set_indent(i, SIN_CHANGED);
10132 }
10133 else if (curwin->w_cursor.col > 0)
10134 {
10135 /*
10136 * when inserting '{' after "O" reduce indent, but not
10137 * more than indent of previous line
10138 */
10139 temp = TRUE;
10140 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10141 {
10142 old_pos = curwin->w_cursor;
10143 i = get_indent();
10144 while (curwin->w_cursor.lnum > 1)
10145 {
10146 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10147
10148 /* ignore empty lines and lines starting with '#'. */
10149 if (*ptr != '#' && *ptr != NUL)
10150 break;
10151 }
10152 if (get_indent() >= i)
10153 temp = FALSE;
10154 curwin->w_cursor = old_pos;
10155 }
10156 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010157 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010158 }
10159 }
10160
10161 /*
10162 * set indent of '#' always to 0
10163 */
10164 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10165 {
10166 /* remember current indent for next line */
10167 old_indent = get_indent();
10168 (void)set_indent(0, SIN_CHANGED);
10169 }
10170
10171 /* Adjust ai_col, the char at this position can be deleted. */
10172 if (ai_col > curwin->w_cursor.col)
10173 ai_col = curwin->w_cursor.col;
10174}
10175#endif
10176
10177/*
10178 * Get the value that w_virtcol would have when 'list' is off.
10179 * Unless 'cpo' contains the 'L' flag.
10180 */
10181 static colnr_T
10182get_nolist_virtcol()
10183{
10184 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10185 return getvcol_nolist(&curwin->w_cursor);
10186 validate_virtcol();
10187 return curwin->w_virtcol;
10188}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010189
10190#ifdef FEAT_AUTOCMD
10191/*
10192 * Handle the InsertCharPre autocommand.
10193 * "c" is the character that was typed.
10194 * Return a pointer to allocated memory with the replacement string.
10195 * Return NULL to continue inserting "c".
10196 */
10197 static char_u *
10198do_insert_char_pre(c)
10199 int c;
10200{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010201 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010202 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010203
10204 /* Return quickly when there is nothing to do. */
10205 if (!has_insertcharpre())
10206 return NULL;
10207
Bram Moolenaar704984a2012-06-01 14:57:51 +020010208#ifdef FEAT_MBYTE
10209 if (has_mbyte)
10210 buf[(*mb_char2bytes)(c, buf)] = NUL;
10211 else
10212#endif
10213 {
10214 buf[0] = c;
10215 buf[1] = NUL;
10216 }
10217
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010218 /* Lock the text to avoid weird things from happening. */
10219 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010220 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010221
Bram Moolenaar704984a2012-06-01 14:57:51 +020010222 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010223 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010224 {
10225 /* Get the value of v:char. It may be empty or more than one
10226 * character. Only use it when changed, otherwise continue with the
10227 * original character to avoid breaking autoindent. */
10228 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10229 res = vim_strsave(get_vim_var_str(VV_CHAR));
10230 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010231
10232 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10233 --textlock;
10234
10235 return res;
10236}
10237#endif