blob: ee308bd79966912f0ff5486e66052d0a663dcad2 [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 Moolenaar097c9922013-05-19 21:15:15 +0200385 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000386# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000387 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100388
Bram Moolenaar097c9922013-05-19 21:15:15 +0200389 /* Make sure the cursor didn't move. Do call check_cursor_col() in
390 * case the text was modified. Since Insert mode was not started yet
391 * a call to check_cursor_col() may move the cursor, especially with
392 * the "A" command, thus set State to avoid that. Also check that the
393 * line number is still valid (lines may have been deleted).
394 * Do not restore if v:char was set to a non-empty string. */
395 if (!equalpos(curwin->w_cursor, save_cursor)
396# ifdef FEAT_EVAL
397 && *get_vim_var_str(VV_CHAR) == NUL
398# endif
399 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100400 {
401 int save_state = State;
402
403 curwin->w_cursor = save_cursor;
404 State = INSERT;
405 check_cursor_col();
406 State = save_state;
407 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000408 }
409#endif
410
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200411#ifdef FEAT_CONCEAL
412 /* Check if the cursor line needs redrawing before changing State. If
413 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200414 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200415#endif
416
Bram Moolenaar071d4272004-06-13 20:20:40 +0000417#ifdef FEAT_MOUSE
418 /*
419 * When doing a paste with the middle mouse button, Insstart is set to
420 * where the paste started.
421 */
422 if (where_paste_started.lnum != 0)
423 Insstart = where_paste_started;
424 else
425#endif
426 {
427 Insstart = curwin->w_cursor;
428 if (startln)
429 Insstart.col = 0;
430 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000431 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000432 Insstart_blank_vcol = MAXCOL;
433 if (!did_ai)
434 ai_col = 0;
435
436 if (cmdchar != NUL && restart_edit == 0)
437 {
438 ResetRedobuff();
439 AppendNumberToRedobuff(count);
440#ifdef FEAT_VREPLACE
441 if (cmdchar == 'V' || cmdchar == 'v')
442 {
443 /* "gR" or "gr" command */
444 AppendCharToRedobuff('g');
445 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
446 }
447 else
448#endif
449 {
450 AppendCharToRedobuff(cmdchar);
451 if (cmdchar == 'g') /* "gI" command */
452 AppendCharToRedobuff('I');
453 else if (cmdchar == 'r') /* "r<CR>" command */
454 count = 1; /* insert only one <CR> */
455 }
456 }
457
458 if (cmdchar == 'R')
459 {
460#ifdef FEAT_FKMAP
461 if (p_fkmap && p_ri)
462 {
463 beep_flush();
464 EMSG(farsi_text_3); /* encoded in Farsi */
465 State = INSERT;
466 }
467 else
468#endif
469 State = REPLACE;
470 }
471#ifdef FEAT_VREPLACE
472 else if (cmdchar == 'V' || cmdchar == 'v')
473 {
474 State = VREPLACE;
475 replaceState = VREPLACE;
476 orig_line_count = curbuf->b_ml.ml_line_count;
477 vr_lines_changed = 1;
478 }
479#endif
480 else
481 State = INSERT;
482
483 stop_insert_mode = FALSE;
484
485 /*
486 * Need to recompute the cursor position, it might move when the cursor is
487 * on a TAB or special character.
488 */
489 curs_columns(TRUE);
490
491 /*
492 * Enable langmap or IME, indicated by 'iminsert'.
493 * Note that IME may enabled/disabled without us noticing here, thus the
494 * 'iminsert' value may not reflect what is actually used. It is updated
495 * when hitting <Esc>.
496 */
497 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
498 State |= LANGMAP;
499#ifdef USE_IM_CONTROL
500 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
501#endif
502
Bram Moolenaar071d4272004-06-13 20:20:40 +0000503#ifdef FEAT_MOUSE
504 setmouse();
505#endif
506#ifdef FEAT_CMDL_INFO
507 clear_showcmd();
508#endif
509#ifdef FEAT_RIGHTLEFT
510 /* there is no reverse replace mode */
511 revins_on = (State == INSERT && p_ri);
512 if (revins_on)
513 undisplay_dollar();
514 revins_chars = 0;
515 revins_legal = 0;
516 revins_scol = -1;
517#endif
518
519 /*
520 * Handle restarting Insert mode.
521 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
522 * restart_edit non-zero, and something in the stuff buffer.
523 */
524 if (restart_edit != 0 && stuff_empty())
525 {
526#ifdef FEAT_MOUSE
527 /*
528 * After a paste we consider text typed to be part of the insert for
529 * the pasted text. You can backspace over the pasted text too.
530 */
531 if (where_paste_started.lnum)
532 arrow_used = FALSE;
533 else
534#endif
535 arrow_used = TRUE;
536 restart_edit = 0;
537
538 /*
539 * If the cursor was after the end-of-line before the CTRL-O and it is
540 * now at the end-of-line, put it after the end-of-line (this is not
541 * correct in very rare cases).
542 * Also do this if curswant is greater than the current virtual
543 * column. Eg after "^O$" or "^O80|".
544 */
545 validate_virtcol();
546 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000547 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000548 || curwin->w_curswant > curwin->w_virtcol)
549 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
550 {
551 if (ptr[1] == NUL)
552 ++curwin->w_cursor.col;
553#ifdef FEAT_MBYTE
554 else if (has_mbyte)
555 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000556 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000557 if (ptr[i] == NUL)
558 curwin->w_cursor.col += i;
559 }
560#endif
561 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000562 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 }
564 else
565 arrow_used = FALSE;
566
567 /* we are in insert mode now, don't need to start it anymore */
568 need_start_insertmode = FALSE;
569
570 /* Need to save the line for undo before inserting the first char. */
571 ins_need_undo = TRUE;
572
573#ifdef FEAT_MOUSE
574 where_paste_started.lnum = 0;
575#endif
576#ifdef FEAT_CINDENT
577 can_cindent = TRUE;
578#endif
579#ifdef FEAT_FOLDING
580 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
581 * restarting. */
582 if (!p_im && did_restart_edit == 0)
583 foldOpenCursor();
584#endif
585
586 /*
587 * If 'showmode' is set, show the current (insert/replace/..) mode.
588 * A warning message for changing a readonly file is given here, before
589 * actually changing anything. It's put after the mode, if any.
590 */
591 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000592 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000593 i = showmode();
594
595 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000596 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000597
598#ifdef CURSOR_SHAPE
599 ui_cursor_shape(); /* may show different cursor shape */
600#endif
601#ifdef FEAT_DIGRAPHS
602 do_digraph(-1); /* clear digraphs */
603#endif
604
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000605 /*
606 * Get the current length of the redo buffer, those characters have to be
607 * skipped if we want to get to the inserted characters.
608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000609 ptr = get_inserted();
610 if (ptr == NULL)
611 new_insert_skip = 0;
612 else
613 {
614 new_insert_skip = (int)STRLEN(ptr);
615 vim_free(ptr);
616 }
617
618 old_indent = 0;
619
620 /*
621 * Main loop in Insert mode: repeat until Insert mode is left.
622 */
623 for (;;)
624 {
625#ifdef FEAT_RIGHTLEFT
626 if (!revins_legal)
627 revins_scol = -1; /* reset on illegal motions */
628 else
629 revins_legal = 0;
630#endif
631 if (arrow_used) /* don't repeat insert when arrow key used */
632 count = 0;
633
634 if (stop_insert_mode)
635 {
636 /* ":stopinsert" used or 'insertmode' reset */
637 count = 0;
638 goto doESCkey;
639 }
640
641 /* set curwin->w_curswant for next K_DOWN or K_UP */
642 if (!arrow_used)
643 curwin->w_set_curswant = TRUE;
644
645 /* If there is no typeahead may check for timestamps (e.g., for when a
646 * menu invoked a shell command). */
647 if (stuff_empty())
648 {
649 did_check_timestamps = FALSE;
650 if (need_check_timestamps)
651 check_timestamps(FALSE);
652 }
653
654 /*
655 * When emsg() was called msg_scroll will have been set.
656 */
657 msg_scroll = FALSE;
658
659#ifdef FEAT_GUI
660 /* When 'mousefocus' is set a mouse movement may have taken us to
661 * another window. "need_mouse_correct" may then be set because of an
662 * autocommand. */
663 if (need_mouse_correct)
664 gui_mouse_correct();
665#endif
666
667#ifdef FEAT_FOLDING
668 /* Open fold at the cursor line, according to 'foldopen'. */
669 if (fdo_flags & FDO_INSERT)
670 foldOpenCursor();
671 /* Close folds where the cursor isn't, according to 'foldclose' */
672 if (!char_avail())
673 foldCheckClose();
674#endif
675
676 /*
677 * If we inserted a character at the last position of the last line in
678 * the window, scroll the window one line up. This avoids an extra
679 * redraw.
680 * This is detected when the cursor column is smaller after inserting
681 * something.
682 * Don't do this when the topline changed already, it has
683 * already been adjusted (by insertchar() calling open_line())).
684 */
685 if (curbuf->b_mod_set
686 && curwin->w_p_wrap
687 && !did_backspace
688 && curwin->w_topline == old_topline
689#ifdef FEAT_DIFF
690 && curwin->w_topfill == old_topfill
691#endif
692 )
693 {
694 mincol = curwin->w_wcol;
695 validate_cursor_col();
696
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000697 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000698 && curwin->w_wrow == W_WINROW(curwin)
699 + curwin->w_height - 1 - p_so
700 && (curwin->w_cursor.lnum != curwin->w_topline
701#ifdef FEAT_DIFF
702 || curwin->w_topfill > 0
703#endif
704 ))
705 {
706#ifdef FEAT_DIFF
707 if (curwin->w_topfill > 0)
708 --curwin->w_topfill;
709 else
710#endif
711#ifdef FEAT_FOLDING
712 if (hasFolding(curwin->w_topline, NULL, &old_topline))
713 set_topline(curwin, old_topline + 1);
714 else
715#endif
716 set_topline(curwin, curwin->w_topline + 1);
717 }
718 }
719
720 /* May need to adjust w_topline to show the cursor. */
721 update_topline();
722
723 did_backspace = FALSE;
724
725 validate_cursor(); /* may set must_redraw */
726
727 /*
728 * Redraw the display when no characters are waiting.
729 * Also shows mode, ruler and positions cursor.
730 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000731 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732
733#ifdef FEAT_SCROLLBIND
734 if (curwin->w_p_scb)
735 do_check_scrollbind(TRUE);
736#endif
737
Bram Moolenaar860cae12010-06-05 23:22:07 +0200738#ifdef FEAT_CURSORBIND
739 if (curwin->w_p_crb)
740 do_check_cursorbind();
741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000742 update_curswant();
743 old_topline = curwin->w_topline;
744#ifdef FEAT_DIFF
745 old_topfill = curwin->w_topfill;
746#endif
747
748#ifdef USE_ON_FLY_SCROLL
749 dont_scroll = FALSE; /* allow scrolling here */
750#endif
751
752 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000753 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000754 */
755 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000756 do
757 {
758 c = safe_vgetc();
759 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000760
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000761#ifdef FEAT_AUTOCMD
762 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
763 did_cursorhold = TRUE;
764#endif
765
Bram Moolenaar071d4272004-06-13 20:20:40 +0000766#ifdef FEAT_RIGHTLEFT
767 if (p_hkmap && KeyTyped)
768 c = hkmap(c); /* Hebrew mode mapping */
769#endif
770#ifdef FEAT_FKMAP
771 if (p_fkmap && KeyTyped)
772 c = fkmap(c); /* Farsi mode mapping */
773#endif
774
775#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000776 /*
777 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000778 * and the cursor is still in the completed word. Only when there is
779 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000780 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000781 if (compl_started
782 && pum_wanted()
783 && curwin->w_cursor.col >= compl_col
784 && (compl_shown_match == NULL
785 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000786 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000787 /* BS: Delete one character from "compl_leader". */
788 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000789 && curwin->w_cursor.col > compl_col
790 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000791 continue;
792
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000793 /* When no match was selected or it was edited. */
794 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000795 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000796 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000797 * "compl_leader". Except when at the original match and
798 * there is nothing to add, CTRL-L works like CTRL-P then. */
799 if (c == Ctrl_L
800 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000801 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000802 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000803 {
804 ins_compl_addfrommatch();
805 continue;
806 }
807
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000808 /* A non-white character that fits in with the current
809 * completion: Add to "compl_leader". */
810 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000811 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100812#ifdef FEAT_AUTOCMD
813 /* Trigger InsertCharPre. */
814 char_u *str = do_insert_char_pre(c);
815 char_u *p;
816
817 if (str != NULL)
818 {
819 for (p = str; *p != NUL; mb_ptr_adv(p))
820 ins_compl_addleader(PTR2CHAR(p));
821 vim_free(str);
822 }
823 else
824#endif
825 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000826 continue;
827 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000828
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000829 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000830 * compl_enter_selects is set the Enter key does the same. */
831 if (c == Ctrl_Y || (compl_enter_selects
832 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000833 {
834 ins_compl_delete();
835 ins_compl_insert();
836 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000837 }
838 }
839
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
841 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000842 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000843 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000844 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845#endif
846
Bram Moolenaar488c6512005-08-11 20:09:58 +0000847 /* CTRL-\ CTRL-N goes to Normal mode,
848 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
849 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850 if (c == Ctrl_BSL)
851 {
852 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000853 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000854 ++no_mapping;
855 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000856 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000857 --no_mapping;
858 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000859 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000861 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 vungetc(c);
863 c = Ctrl_BSL;
864 }
865 else if (c == Ctrl_G && p_im)
866 continue;
867 else
868 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000869 if (c == Ctrl_O)
870 {
871 ins_ctrl_o();
872 ins_at_eol = FALSE; /* cursor keeps its column */
873 nomove = TRUE;
874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 count = 0;
876 goto doESCkey;
877 }
878 }
879
880#ifdef FEAT_DIGRAPHS
881 c = do_digraph(c);
882#endif
883
884#ifdef FEAT_INS_EXPAND
885 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
886 goto docomplete;
887#endif
888 if (c == Ctrl_V || c == Ctrl_Q)
889 {
890 ins_ctrl_v();
891 c = Ctrl_V; /* pretend CTRL-V is last typed character */
892 continue;
893 }
894
895#ifdef FEAT_CINDENT
896 if (cindent_on()
897# ifdef FEAT_INS_EXPAND
898 && ctrl_x_mode == 0
899# endif
900 )
901 {
902 /* A key name preceded by a bang means this key is not to be
903 * inserted. Skip ahead to the re-indenting below.
904 * A key name preceded by a star means that indenting has to be
905 * done before inserting the key. */
906 line_is_white = inindent(0);
907 if (in_cinkeys(c, '!', line_is_white))
908 goto force_cindent;
909 if (can_cindent && in_cinkeys(c, '*', line_is_white)
910 && stop_arrow() == OK)
911 do_c_expr_indent();
912 }
913#endif
914
915#ifdef FEAT_RIGHTLEFT
916 if (curwin->w_p_rl)
917 switch (c)
918 {
919 case K_LEFT: c = K_RIGHT; break;
920 case K_S_LEFT: c = K_S_RIGHT; break;
921 case K_C_LEFT: c = K_C_RIGHT; break;
922 case K_RIGHT: c = K_LEFT; break;
923 case K_S_RIGHT: c = K_S_LEFT; break;
924 case K_C_RIGHT: c = K_C_LEFT; break;
925 }
926#endif
927
928#ifdef FEAT_VISUAL
929 /*
930 * If 'keymodel' contains "startsel", may start selection. If it
931 * does, a CTRL-O and c will be stuffed, we need to get these
932 * characters.
933 */
934 if (ins_start_select(c))
935 continue;
936#endif
937
938 /*
939 * The big switch to handle a character in insert mode.
940 */
941 switch (c)
942 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000944 if (echeck_abbr(ESC + ABBR_OFF))
945 break;
946 /*FALLTHROUGH*/
947
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000948 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949#ifdef FEAT_CMDWIN
950 if (c == Ctrl_C && cmdwin_type != 0)
951 {
952 /* Close the cmdline window. */
953 cmdwin_result = K_IGNORE;
954 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000955 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000956 goto doESCkey;
957 }
958#endif
959
960#ifdef UNIX
961do_intr:
962#endif
963 /* when 'insertmode' set, and not halfway a mapping, don't leave
964 * Insert mode */
965 if (goto_im())
966 {
967 if (got_int)
968 {
969 (void)vgetc(); /* flush all buffers */
970 got_int = FALSE;
971 }
972 else
973 vim_beep();
974 break;
975 }
976doESCkey:
977 /*
978 * This is the ONLY return from edit()!
979 */
980 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
981 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000982 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000983 o_lnum = curwin->w_cursor.lnum;
984
Bram Moolenaar488c6512005-08-11 20:09:58 +0000985 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000986 {
987#ifdef FEAT_AUTOCMD
988 if (cmdchar != 'r' && cmdchar != 'v')
989 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
990 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000991 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000993 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000995 continue;
996
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000997 case Ctrl_Z: /* suspend when 'insertmode' set */
998 if (!p_im)
999 goto normalchar; /* insert CTRL-Z as normal char */
1000 stuffReadbuff((char_u *)":st\r");
1001 c = Ctrl_O;
1002 /*FALLTHROUGH*/
1003
1004 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001005#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001006 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001007 goto docomplete;
1008#endif
1009 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1010 break;
1011 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001012
1013#ifdef FEAT_VIRTUALEDIT
1014 /* don't move the cursor left when 'virtualedit' has "onemore". */
1015 if (ve_flags & VE_ONEMORE)
1016 {
1017 ins_at_eol = FALSE;
1018 nomove = TRUE;
1019 }
1020#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001021 count = 0;
1022 goto doESCkey;
1023
Bram Moolenaar572cb562005-08-05 21:35:02 +00001024 case K_INS: /* toggle insert/replace mode */
1025 case K_KINS:
1026 ins_insert(replaceState);
1027 break;
1028
1029 case K_SELECT: /* end of Select mode mapping - ignore */
1030 break;
1031
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001032#ifdef FEAT_SNIFF
1033 case K_SNIFF: /* Sniff command received */
1034 stuffcharReadbuff(K_SNIFF);
1035 goto doESCkey;
1036#endif
1037
1038 case K_HELP: /* Help key works like <ESC> <Help> */
1039 case K_F1:
1040 case K_XF1:
1041 stuffcharReadbuff(K_HELP);
1042 if (p_im)
1043 need_start_insertmode = TRUE;
1044 goto doESCkey;
1045
1046#ifdef FEAT_NETBEANS_INTG
1047 case K_F21: /* NetBeans command */
1048 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001049 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001050 --no_mapping;
1051 netbeans_keycommand(i);
1052 break;
1053#endif
1054
1055 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 case NUL:
1057 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 /* For ^@ the trailing ESC will end the insert, unless there is an
1059 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1061 && c != Ctrl_A && !p_im)
1062 goto doESCkey; /* quit insert mode */
1063 inserted_space = FALSE;
1064 break;
1065
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 ins_reg();
1068 auto_format(FALSE, TRUE);
1069 inserted_space = FALSE;
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 ins_ctrl_g();
1074 break;
1075
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001076 case Ctrl_HAT: /* switch input mode and/or langmap */
1077 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 break;
1079
1080#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001081 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 if (!p_ari)
1083 goto normalchar;
1084 ins_ctrl_();
1085 break;
1086#endif
1087
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1090 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1091 goto docomplete;
1092#endif
1093 /* FALLTHROUGH */
1094
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096# ifdef FEAT_INS_EXPAND
1097 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1098 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001099 if (has_compl_option(FALSE))
1100 goto docomplete;
1101 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001102 }
1103# endif
1104 ins_shift(c, lastc);
1105 auto_format(FALSE, TRUE);
1106 inserted_space = FALSE;
1107 break;
1108
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001109 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 case K_KDEL:
1111 ins_del();
1112 auto_format(FALSE, TRUE);
1113 break;
1114
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 case Ctrl_H:
1117 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1118 auto_format(FALSE, TRUE);
1119 break;
1120
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001121 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1123 auto_format(FALSE, TRUE);
1124 break;
1125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001126 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001127# ifdef FEAT_COMPL_FUNC
1128 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001130 goto docomplete;
1131# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001132 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1133 auto_format(FALSE, TRUE);
1134 inserted_space = FALSE;
1135 break;
1136
1137#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 case K_LEFTMOUSE_NM:
1140 case K_LEFTDRAG:
1141 case K_LEFTRELEASE:
1142 case K_LEFTRELEASE_NM:
1143 case K_MIDDLEMOUSE:
1144 case K_MIDDLEDRAG:
1145 case K_MIDDLERELEASE:
1146 case K_RIGHTMOUSE:
1147 case K_RIGHTDRAG:
1148 case K_RIGHTRELEASE:
1149 case K_X1MOUSE:
1150 case K_X1DRAG:
1151 case K_X1RELEASE:
1152 case K_X2MOUSE:
1153 case K_X2DRAG:
1154 case K_X2RELEASE:
1155 ins_mouse(c);
1156 break;
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001159 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 break;
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001163 ins_mousescroll(MSCR_UP);
1164 break;
1165
1166 case K_MOUSELEFT: /* Scroll wheel left */
1167 ins_mousescroll(MSCR_LEFT);
1168 break;
1169
1170 case K_MOUSERIGHT: /* Scroll wheel right */
1171 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001172 break;
1173#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001174#ifdef FEAT_GUI_TABLINE
1175 case K_TABLINE:
1176 case K_TABMENU:
1177 ins_tabline(c);
1178 break;
1179#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 break;
1183
Bram Moolenaar754b5602006-02-09 23:53:20 +00001184#ifdef FEAT_AUTOCMD
1185 case K_CURSORHOLD: /* Didn't type something for a while. */
1186 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1187 did_cursorhold = TRUE;
1188 break;
1189#endif
1190
Bram Moolenaar4770d092006-01-12 23:22:24 +00001191#ifdef FEAT_GUI_W32
1192 /* On Win32 ignore <M-F4>, we get it when closing the window was
1193 * cancelled. */
1194 case K_F4:
1195 if (mod_mask != MOD_MASK_ALT)
1196 goto normalchar;
1197 break;
1198#endif
1199
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200#ifdef FEAT_GUI
1201 case K_VER_SCROLLBAR:
1202 ins_scroll();
1203 break;
1204
1205 case K_HOR_SCROLLBAR:
1206 ins_horscroll();
1207 break;
1208#endif
1209
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001210 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001211 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 case K_S_HOME:
1213 case K_C_HOME:
1214 ins_home(c);
1215 break;
1216
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001217 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 case K_S_END:
1220 case K_C_END:
1221 ins_end(c);
1222 break;
1223
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001224 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001225 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1226 ins_s_left();
1227 else
1228 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 break;
1230
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001231 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 case K_C_LEFT:
1233 ins_s_left();
1234 break;
1235
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001236 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001237 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1238 ins_s_right();
1239 else
1240 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 break;
1242
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001243 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 case K_C_RIGHT:
1245 ins_s_right();
1246 break;
1247
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001249#ifdef FEAT_INS_EXPAND
1250 if (pum_visible())
1251 goto docomplete;
1252#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001253 if (mod_mask & MOD_MASK_SHIFT)
1254 ins_pageup();
1255 else
1256 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257 break;
1258
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001259 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 case K_PAGEUP:
1261 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001262#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001263 if (pum_visible())
1264 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001266 ins_pageup();
1267 break;
1268
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001269 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001270#ifdef FEAT_INS_EXPAND
1271 if (pum_visible())
1272 goto docomplete;
1273#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001274 if (mod_mask & MOD_MASK_SHIFT)
1275 ins_pagedown();
1276 else
1277 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278 break;
1279
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001280 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 case K_PAGEDOWN:
1282 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001283#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001284 if (pum_visible())
1285 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001287 ins_pagedown();
1288 break;
1289
1290#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001291 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 ins_drop();
1293 break;
1294#endif
1295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297 c = TAB;
1298 /* FALLTHROUGH */
1299
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001300 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1302 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1303 goto docomplete;
1304#endif
1305 inserted_space = FALSE;
1306 if (ins_tab())
1307 goto normalchar; /* insert TAB as a normal char */
1308 auto_format(FALSE, TRUE);
1309 break;
1310
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001311 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001312 c = CAR;
1313 /* FALLTHROUGH */
1314 case CAR:
1315 case NL:
1316#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1317 /* In a quickfix window a <CR> jumps to the error under the
1318 * cursor. */
1319 if (bt_quickfix(curbuf) && c == CAR)
1320 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001321 if (curwin->w_llist_ref == NULL) /* quickfix window */
1322 do_cmdline_cmd((char_u *)".cc");
1323 else /* location list window */
1324 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325 break;
1326 }
1327#endif
1328#ifdef FEAT_CMDWIN
1329 if (cmdwin_type != 0)
1330 {
1331 /* Execute the command in the cmdline window. */
1332 cmdwin_result = CAR;
1333 goto doESCkey;
1334 }
1335#endif
1336 if (ins_eol(c) && !p_im)
1337 goto doESCkey; /* out of memory */
1338 auto_format(FALSE, FALSE);
1339 inserted_space = FALSE;
1340 break;
1341
Bram Moolenaar860cae12010-06-05 23:22:07 +02001342#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344# ifdef FEAT_INS_EXPAND
1345 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1346 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001347 if (has_compl_option(TRUE))
1348 goto docomplete;
1349 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 }
1351# endif
1352# ifdef FEAT_DIGRAPHS
1353 c = ins_digraph();
1354 if (c == NUL)
1355 break;
1356# endif
1357 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001358#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359
1360#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001361 case Ctrl_X: /* Enter CTRL-X mode */
1362 ins_ctrl_x();
1363 break;
1364
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001365 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (ctrl_x_mode != CTRL_X_TAGS)
1367 goto normalchar;
1368 goto docomplete;
1369
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001370 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001371 if (ctrl_x_mode != CTRL_X_FILES)
1372 goto normalchar;
1373 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001374
1375 case 's': /* Spelling completion after ^X */
1376 case Ctrl_S:
1377 if (ctrl_x_mode != CTRL_X_SPELL)
1378 goto normalchar;
1379 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380#endif
1381
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383#ifdef FEAT_INS_EXPAND
1384 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1385#endif
1386 {
1387 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1388 if (p_im)
1389 {
1390 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1391 break;
1392 goto doESCkey;
1393 }
1394 goto normalchar;
1395 }
1396#ifdef FEAT_INS_EXPAND
1397 /* FALLTHROUGH */
1398
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001399 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001400 case Ctrl_N:
1401 /* if 'complete' is empty then plain ^P is no longer special,
1402 * but it is under other ^X modes */
1403 if (*curbuf->b_p_cpt == NUL
1404 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001405 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 goto normalchar;
1407
1408docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001409 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001412 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 break;
1414#endif /* FEAT_INS_EXPAND */
1415
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001416 case Ctrl_Y: /* copy from previous line or scroll down */
1417 case Ctrl_E: /* copy from next line or scroll up */
1418 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 break;
1420
1421 default:
1422#ifdef UNIX
1423 if (c == intr_char) /* special interrupt char */
1424 goto do_intr;
1425#endif
1426
Bram Moolenaare659c952011-05-19 17:25:41 +02001427normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001429 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001431#ifdef FEAT_AUTOCMD
1432 if (!p_paste)
1433 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001434 /* Trigger InsertCharPre. */
1435 char_u *str = do_insert_char_pre(c);
1436 char_u *p;
1437
1438 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001439 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001440 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001441 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001442 /* Insert the new value of v:char literally. */
1443 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001444 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001445 c = PTR2CHAR(p);
1446 if (c == CAR || c == K_KENTER || c == NL)
1447 ins_eol(c);
1448 else
1449 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001450 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001451 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001452 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001453 vim_free(str);
1454 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001455 }
1456
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001457 /* If the new value is already inserted or an empty string
1458 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001459 if (c == NUL)
1460 break;
1461 }
1462#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463#ifdef FEAT_SMARTINDENT
1464 /* Try to perform smart-indenting. */
1465 ins_try_si(c);
1466#endif
1467
1468 if (c == ' ')
1469 {
1470 inserted_space = TRUE;
1471#ifdef FEAT_CINDENT
1472 if (inindent(0))
1473 can_cindent = FALSE;
1474#endif
1475 if (Insstart_blank_vcol == MAXCOL
1476 && curwin->w_cursor.lnum == Insstart.lnum)
1477 Insstart_blank_vcol = get_nolist_virtcol();
1478 }
1479
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001480 /* Insert a normal character and check for abbreviations on a
1481 * special character. Let CTRL-] expand abbreviations without
1482 * inserting it. */
1483 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484#ifdef FEAT_MBYTE
1485 /* Add ABBR_OFF for characters above 0x100, this is
1486 * what check_abbr() expects. */
1487 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1488#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001489 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490 {
1491 insert_special(c, FALSE, FALSE);
1492#ifdef FEAT_RIGHTLEFT
1493 revins_legal++;
1494 revins_chars++;
1495#endif
1496 }
1497
1498 auto_format(FALSE, TRUE);
1499
1500#ifdef FEAT_FOLDING
1501 /* When inserting a character the cursor line must never be in a
1502 * closed fold. */
1503 foldOpenCursor();
1504#endif
1505 break;
1506 } /* end of switch (c) */
1507
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001508#ifdef FEAT_AUTOCMD
1509 /* If typed something may trigger CursorHoldI again. */
1510 if (c != K_CURSORHOLD)
1511 did_cursorhold = FALSE;
1512#endif
1513
Bram Moolenaar071d4272004-06-13 20:20:40 +00001514 /* If the cursor was moved we didn't just insert a space */
1515 if (arrow_used)
1516 inserted_space = FALSE;
1517
1518#ifdef FEAT_CINDENT
1519 if (can_cindent && cindent_on()
1520# ifdef FEAT_INS_EXPAND
1521 && ctrl_x_mode == 0
1522# endif
1523 )
1524 {
1525force_cindent:
1526 /*
1527 * Indent now if a key was typed that is in 'cinkeys'.
1528 */
1529 if (in_cinkeys(c, ' ', line_is_white))
1530 {
1531 if (stop_arrow() == OK)
1532 /* re-indent the current line */
1533 do_c_expr_indent();
1534 }
1535 }
1536#endif /* FEAT_CINDENT */
1537
1538 } /* for (;;) */
1539 /* NOTREACHED */
1540}
1541
1542/*
1543 * Redraw for Insert mode.
1544 * This is postponed until getting the next character to make '$' in the 'cpo'
1545 * option work correctly.
1546 * Only redraw when there are no characters available. This speeds up
1547 * inserting sequences of characters (e.g., for CTRL-R).
1548 */
1549 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001550ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001551 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001553#ifdef FEAT_CONCEAL
1554 linenr_T conceal_old_cursor_line = 0;
1555 linenr_T conceal_new_cursor_line = 0;
1556 int conceal_update_lines = FALSE;
1557#endif
1558
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559 if (!char_avail())
1560 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001561#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001562 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1563 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001564 if (ready && (
1565# ifdef FEAT_AUTOCMD
1566 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001567# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001568# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1569 ||
1570# endif
1571# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001572 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001573# endif
1574 )
1575 && !equalpos(last_cursormoved, curwin->w_cursor)
1576# ifdef FEAT_INS_EXPAND
1577 && !pum_visible()
1578# endif
1579 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001580 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001581# ifdef FEAT_SYN_HL
1582 /* Need to update the screen first, to make sure syntax
1583 * highlighting is correct after making a change (e.g., inserting
1584 * a "(". The autocommand may also require a redraw, so it's done
1585 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001586 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001587 update_screen(0);
1588# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001589# ifdef FEAT_AUTOCMD
1590 if (has_cursormovedI())
1591 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1592# endif
1593# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001594 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001595 {
1596 conceal_old_cursor_line = last_cursormoved.lnum;
1597 conceal_new_cursor_line = curwin->w_cursor.lnum;
1598 conceal_update_lines = TRUE;
1599 }
1600# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001601 last_cursormoved = curwin->w_cursor;
1602 }
1603#endif
Bram Moolenaar186628f2013-03-19 13:33:23 +01001604#ifdef FEAT_AUTOCMD
1605 /* Trigger TextChangedI if b_changedtick differs. */
1606 if (!ready && has_textchangedI()
1607 && last_changedtick != curbuf->b_changedtick
1608# ifdef FEAT_INS_EXPAND
1609 && !pum_visible()
1610# endif
1611 )
1612 {
1613 if (last_changedtick_buf == curbuf)
1614 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1615 last_changedtick_buf = curbuf;
1616 last_changedtick = curbuf->b_changedtick;
1617 }
1618#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 if (must_redraw)
1620 update_screen(0);
1621 else if (clear_cmdline || redraw_cmdline)
1622 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001623# if defined(FEAT_CONCEAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001624 if ((conceal_update_lines
1625 && (conceal_old_cursor_line != conceal_new_cursor_line
1626 || conceal_cursor_line(curwin)))
1627 || need_cursor_line_redraw)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001628 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001629 if (conceal_old_cursor_line != conceal_new_cursor_line)
1630 update_single_line(curwin, conceal_old_cursor_line);
1631 update_single_line(curwin, conceal_new_cursor_line == 0
1632 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001633 curwin->w_valid &= ~VALID_CROW;
1634 }
1635# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636 showruler(FALSE);
1637 setcursor();
1638 emsg_on_display = FALSE; /* may remove error message now */
1639 }
1640}
1641
1642/*
1643 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1644 */
1645 static void
1646ins_ctrl_v()
1647{
1648 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001649 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650
1651 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001652 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653
1654 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001655 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001656 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001657 did_putchar = TRUE;
1658 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001659 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1660
1661#ifdef FEAT_CMDL_INFO
1662 add_to_showcmd_c(Ctrl_V);
1663#endif
1664
1665 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001666 if (did_putchar)
1667 /* when the line fits in 'columns' the '^' is at the start of the next
1668 * line and will not removed by the redraw */
1669 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670#ifdef FEAT_CMDL_INFO
1671 clear_showcmd();
1672#endif
1673 insert_special(c, FALSE, TRUE);
1674#ifdef FEAT_RIGHTLEFT
1675 revins_chars++;
1676 revins_legal++;
1677#endif
1678}
1679
1680/*
1681 * Put a character directly onto the screen. It's not stored in a buffer.
1682 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1683 */
1684static int pc_status;
1685#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1686#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1687#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1688#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001689static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001690static int pc_attr;
1691static int pc_row;
1692static int pc_col;
1693
1694 void
1695edit_putchar(c, highlight)
1696 int c;
1697 int highlight;
1698{
1699 int attr;
1700
1701 if (ScreenLines != NULL)
1702 {
1703 update_topline(); /* just in case w_topline isn't valid */
1704 validate_cursor();
1705 if (highlight)
1706 attr = hl_attr(HLF_8);
1707 else
1708 attr = 0;
1709 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1710 pc_col = W_WINCOL(curwin);
1711#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1712 pc_status = PC_STATUS_UNSET;
1713#endif
1714#ifdef FEAT_RIGHTLEFT
1715 if (curwin->w_p_rl)
1716 {
1717 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1718# ifdef FEAT_MBYTE
1719 if (has_mbyte)
1720 {
1721 int fix_col = mb_fix_col(pc_col, pc_row);
1722
1723 if (fix_col != pc_col)
1724 {
1725 screen_putchar(' ', pc_row, fix_col, attr);
1726 --curwin->w_wcol;
1727 pc_status = PC_STATUS_RIGHT;
1728 }
1729 }
1730# endif
1731 }
1732 else
1733#endif
1734 {
1735 pc_col += curwin->w_wcol;
1736#ifdef FEAT_MBYTE
1737 if (mb_lefthalve(pc_row, pc_col))
1738 pc_status = PC_STATUS_LEFT;
1739#endif
1740 }
1741
1742 /* save the character to be able to put it back */
1743#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1744 if (pc_status == PC_STATUS_UNSET)
1745#endif
1746 {
1747 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1748 pc_status = PC_STATUS_SET;
1749 }
1750 screen_putchar(c, pc_row, pc_col, attr);
1751 }
1752}
1753
1754/*
1755 * Undo the previous edit_putchar().
1756 */
1757 void
1758edit_unputchar()
1759{
1760 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1761 {
1762#if defined(FEAT_MBYTE)
1763 if (pc_status == PC_STATUS_RIGHT)
1764 ++curwin->w_wcol;
1765 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1766 redrawWinline(curwin->w_cursor.lnum, FALSE);
1767 else
1768#endif
1769 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1770 }
1771}
1772
1773/*
1774 * Called when p_dollar is set: display a '$' at the end of the changed text
1775 * Only works when cursor is in the line that changes.
1776 */
1777 void
1778display_dollar(col)
1779 colnr_T col;
1780{
1781 colnr_T save_col;
1782
1783 if (!redrawing())
1784 return;
1785
1786 cursor_off();
1787 save_col = curwin->w_cursor.col;
1788 curwin->w_cursor.col = col;
1789#ifdef FEAT_MBYTE
1790 if (has_mbyte)
1791 {
1792 char_u *p;
1793
1794 /* If on the last byte of a multi-byte move to the first byte. */
1795 p = ml_get_curline();
1796 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1797 }
1798#endif
1799 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1800 if (curwin->w_wcol < W_WIDTH(curwin))
1801 {
1802 edit_putchar('$', FALSE);
1803 dollar_vcol = curwin->w_virtcol;
1804 }
1805 curwin->w_cursor.col = save_col;
1806}
1807
1808/*
1809 * Call this function before moving the cursor from the normal insert position
1810 * in insert mode.
1811 */
1812 static void
1813undisplay_dollar()
1814{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001815 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001816 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001817 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001818 redrawWinline(curwin->w_cursor.lnum, FALSE);
1819 }
1820}
1821
1822/*
1823 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1824 * Keep the cursor on the same character.
1825 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1826 * type == INDENT_DEC decrease indent (for CTRL-D)
1827 * type == INDENT_SET set indent to "amount"
1828 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1829 */
1830 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001831change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001832 int type;
1833 int amount;
1834 int round;
1835 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001836 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837{
1838 int vcol;
1839 int last_vcol;
1840 int insstart_less; /* reduction for Insstart.col */
1841 int new_cursor_col;
1842 int i;
1843 char_u *ptr;
1844 int save_p_list;
1845 int start_col;
1846 colnr_T vc;
1847#ifdef FEAT_VREPLACE
1848 colnr_T orig_col = 0; /* init for GCC */
1849 char_u *new_line, *orig_line = NULL; /* init for GCC */
1850
1851 /* VREPLACE mode needs to know what the line was like before changing */
1852 if (State & VREPLACE_FLAG)
1853 {
1854 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1855 orig_col = curwin->w_cursor.col;
1856 }
1857#endif
1858
1859 /* for the following tricks we don't want list mode */
1860 save_p_list = curwin->w_p_list;
1861 curwin->w_p_list = FALSE;
1862 vc = getvcol_nolist(&curwin->w_cursor);
1863 vcol = vc;
1864
1865 /*
1866 * For Replace mode we need to fix the replace stack later, which is only
1867 * possible when the cursor is in the indent. Remember the number of
1868 * characters before the cursor if it's possible.
1869 */
1870 start_col = curwin->w_cursor.col;
1871
1872 /* determine offset from first non-blank */
1873 new_cursor_col = curwin->w_cursor.col;
1874 beginline(BL_WHITE);
1875 new_cursor_col -= curwin->w_cursor.col;
1876
1877 insstart_less = curwin->w_cursor.col;
1878
1879 /*
1880 * If the cursor is in the indent, compute how many screen columns the
1881 * cursor is to the left of the first non-blank.
1882 */
1883 if (new_cursor_col < 0)
1884 vcol = get_indent() - vcol;
1885
1886 if (new_cursor_col > 0) /* can't fix replace stack */
1887 start_col = -1;
1888
1889 /*
1890 * Set the new indent. The cursor will be put on the first non-blank.
1891 */
1892 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001893 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001894 else
1895 {
1896#ifdef FEAT_VREPLACE
1897 int save_State = State;
1898
1899 /* Avoid being called recursively. */
1900 if (State & VREPLACE_FLAG)
1901 State = INSERT;
1902#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001903 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904#ifdef FEAT_VREPLACE
1905 State = save_State;
1906#endif
1907 }
1908 insstart_less -= curwin->w_cursor.col;
1909
1910 /*
1911 * Try to put cursor on same character.
1912 * If the cursor is at or after the first non-blank in the line,
1913 * compute the cursor column relative to the column of the first
1914 * non-blank character.
1915 * If we are not in insert mode, leave the cursor on the first non-blank.
1916 * If the cursor is before the first non-blank, position it relative
1917 * to the first non-blank, counted in screen columns.
1918 */
1919 if (new_cursor_col >= 0)
1920 {
1921 /*
1922 * When changing the indent while the cursor is touching it, reset
1923 * Insstart_col to 0.
1924 */
1925 if (new_cursor_col == 0)
1926 insstart_less = MAXCOL;
1927 new_cursor_col += curwin->w_cursor.col;
1928 }
1929 else if (!(State & INSERT))
1930 new_cursor_col = curwin->w_cursor.col;
1931 else
1932 {
1933 /*
1934 * Compute the screen column where the cursor should be.
1935 */
1936 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001937 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938
1939 /*
1940 * Advance the cursor until we reach the right screen column.
1941 */
1942 vcol = last_vcol = 0;
1943 new_cursor_col = -1;
1944 ptr = ml_get_curline();
1945 while (vcol <= (int)curwin->w_virtcol)
1946 {
1947 last_vcol = vcol;
1948#ifdef FEAT_MBYTE
1949 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001950 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001951 else
1952#endif
1953 ++new_cursor_col;
1954 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1955 }
1956 vcol = last_vcol;
1957
1958 /*
1959 * May need to insert spaces to be able to position the cursor on
1960 * the right screen column.
1961 */
1962 if (vcol != (int)curwin->w_virtcol)
1963 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001964 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001966 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 if (ptr != NULL)
1968 {
1969 new_cursor_col += i;
1970 ptr[i] = NUL;
1971 while (--i >= 0)
1972 ptr[i] = ' ';
1973 ins_str(ptr);
1974 vim_free(ptr);
1975 }
1976 }
1977
1978 /*
1979 * When changing the indent while the cursor is in it, reset
1980 * Insstart_col to 0.
1981 */
1982 insstart_less = MAXCOL;
1983 }
1984
1985 curwin->w_p_list = save_p_list;
1986
1987 if (new_cursor_col <= 0)
1988 curwin->w_cursor.col = 0;
1989 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001990 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001991 curwin->w_set_curswant = TRUE;
1992 changed_cline_bef_curs();
1993
1994 /*
1995 * May have to adjust the start of the insert.
1996 */
1997 if (State & INSERT)
1998 {
1999 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2000 {
2001 if ((int)Insstart.col <= insstart_less)
2002 Insstart.col = 0;
2003 else
2004 Insstart.col -= insstart_less;
2005 }
2006 if ((int)ai_col <= insstart_less)
2007 ai_col = 0;
2008 else
2009 ai_col -= insstart_less;
2010 }
2011
2012 /*
2013 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2014 * If the number of characters before the cursor decreased, need to pop a
2015 * few characters from the replace stack.
2016 * If the number of characters before the cursor increased, need to push a
2017 * few NULs onto the replace stack.
2018 */
2019 if (REPLACE_NORMAL(State) && start_col >= 0)
2020 {
2021 while (start_col > (int)curwin->w_cursor.col)
2022 {
2023 replace_join(0); /* remove a NUL from the replace stack */
2024 --start_col;
2025 }
2026 while (start_col < (int)curwin->w_cursor.col || replaced)
2027 {
2028 replace_push(NUL);
2029 if (replaced)
2030 {
2031 replace_push(replaced);
2032 replaced = NUL;
2033 }
2034 ++start_col;
2035 }
2036 }
2037
2038#ifdef FEAT_VREPLACE
2039 /*
2040 * For VREPLACE mode, we also have to fix the replace stack. In this case
2041 * it is always possible because we backspace over the whole line and then
2042 * put it back again the way we wanted it.
2043 */
2044 if (State & VREPLACE_FLAG)
2045 {
2046 /* If orig_line didn't allocate, just return. At least we did the job,
2047 * even if you can't backspace. */
2048 if (orig_line == NULL)
2049 return;
2050
2051 /* Save new line */
2052 new_line = vim_strsave(ml_get_curline());
2053 if (new_line == NULL)
2054 return;
2055
2056 /* We only put back the new line up to the cursor */
2057 new_line[curwin->w_cursor.col] = NUL;
2058
2059 /* Put back original line */
2060 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2061 curwin->w_cursor.col = orig_col;
2062
2063 /* Backspace from cursor to start of line */
2064 backspace_until_column(0);
2065
2066 /* Insert new stuff into line again */
2067 ins_bytes(new_line);
2068
2069 vim_free(new_line);
2070 }
2071#endif
2072}
2073
2074/*
2075 * Truncate the space at the end of a line. This is to be used only in an
2076 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2077 * modes.
2078 */
2079 void
2080truncate_spaces(line)
2081 char_u *line;
2082{
2083 int i;
2084
2085 /* find start of trailing white space */
2086 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2087 {
2088 if (State & REPLACE_FLAG)
2089 replace_join(0); /* remove a NUL from the replace stack */
2090 }
2091 line[i + 1] = NUL;
2092}
2093
2094#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2095 || defined(FEAT_COMMENTS) || defined(PROTO)
2096/*
2097 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2098 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002099 * Will attempt not to go before "col" even when there is a composing
2100 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002101 */
2102 void
2103backspace_until_column(col)
2104 int col;
2105{
2106 while ((int)curwin->w_cursor.col > col)
2107 {
2108 curwin->w_cursor.col--;
2109 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002110 replace_do_bs(col);
2111 else if (!del_char_after_col(col))
2112 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
2114}
2115#endif
2116
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002117/*
2118 * Like del_char(), but make sure not to go before column "limit_col".
2119 * Only matters when there are composing characters.
2120 * Return TRUE when something was deleted.
2121 */
2122 static int
2123del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002124 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002125{
2126#ifdef FEAT_MBYTE
2127 if (enc_utf8 && limit_col >= 0)
2128 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002129 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002130
2131 /* Make sure the cursor is at the start of a character, but
2132 * skip forward again when going too far back because of a
2133 * composing character. */
2134 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002135 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002136 {
2137 int l = utf_ptr2len(ml_get_cursor());
2138
2139 if (l == 0) /* end of line */
2140 break;
2141 curwin->w_cursor.col += l;
2142 }
2143 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2144 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002145 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002146 }
2147 else
2148#endif
2149 (void)del_char(FALSE);
2150 return TRUE;
2151}
2152
Bram Moolenaar071d4272004-06-13 20:20:40 +00002153#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2154/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002155 * CTRL-X pressed in Insert mode.
2156 */
2157 static void
2158ins_ctrl_x()
2159{
2160 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2161 * CTRL-V works like CTRL-N */
2162 if (ctrl_x_mode != CTRL_X_CMDLINE)
2163 {
2164 /* if the next ^X<> won't ADD nothing, then reset
2165 * compl_cont_status */
2166 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002167 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002168 else
2169 compl_cont_status = 0;
2170 /* We're not sure which CTRL-X mode it will be yet */
2171 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2172 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2173 edit_submode_pre = NULL;
2174 showmode();
2175 }
2176}
2177
2178/*
2179 * Return TRUE if the 'dict' or 'tsr' option can be used.
2180 */
2181 static int
2182has_compl_option(dict_opt)
2183 int dict_opt;
2184{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002185 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002186# ifdef FEAT_SPELL
2187 && !curwin->w_p_spell
2188# endif
2189 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002190 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2191 {
2192 ctrl_x_mode = 0;
2193 edit_submode = NULL;
2194 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2195 : (char_u *)_("'thesaurus' option is empty"),
2196 hl_attr(HLF_E));
2197 if (emsg_silent == 0)
2198 {
2199 vim_beep();
2200 setcursor();
2201 out_flush();
2202 ui_delay(2000L, FALSE);
2203 }
2204 return FALSE;
2205 }
2206 return TRUE;
2207}
2208
2209/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2211 * This depends on the current mode.
2212 */
2213 int
2214vim_is_ctrl_x_key(c)
2215 int c;
2216{
2217 /* Always allow ^R - let it's results then be checked */
2218 if (c == Ctrl_R)
2219 return TRUE;
2220
Bram Moolenaare3226be2005-12-18 22:10:00 +00002221 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002222 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002223 return TRUE;
2224
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 switch (ctrl_x_mode)
2226 {
2227 case 0: /* Not in any CTRL-X mode */
2228 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2229 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002230 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2232 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2233 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002234 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002235 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 case CTRL_X_SCROLL:
2237 return (c == Ctrl_Y || c == Ctrl_E);
2238 case CTRL_X_WHOLE_LINE:
2239 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2240 case CTRL_X_FILES:
2241 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2242 case CTRL_X_DICTIONARY:
2243 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2244 case CTRL_X_THESAURUS:
2245 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2246 case CTRL_X_TAGS:
2247 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2248#ifdef FEAT_FIND_ID
2249 case CTRL_X_PATH_PATTERNS:
2250 return (c == Ctrl_P || c == Ctrl_N);
2251 case CTRL_X_PATH_DEFINES:
2252 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2253#endif
2254 case CTRL_X_CMDLINE:
2255 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2256 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002257#ifdef FEAT_COMPL_FUNC
2258 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002259 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002260 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002261 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002262#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002263 case CTRL_X_SPELL:
2264 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002265 }
2266 EMSG(_(e_internal));
2267 return FALSE;
2268}
2269
2270/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002271 * Return TRUE when character "c" is part of the item currently being
2272 * completed. Used to decide whether to abandon complete mode when the menu
2273 * is visible.
2274 */
2275 static int
2276ins_compl_accept_char(c)
2277 int c;
2278{
2279 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2280 /* When expanding an identifier only accept identifier chars. */
2281 return vim_isIDc(c);
2282
2283 switch (ctrl_x_mode)
2284 {
2285 case CTRL_X_FILES:
2286 /* When expanding file name only accept file name chars. But not
2287 * path separators, so that "proto/<Tab>" expands files in
2288 * "proto", not "proto/" as a whole */
2289 return vim_isfilec(c) && !vim_ispathsep(c);
2290
2291 case CTRL_X_CMDLINE:
2292 case CTRL_X_OMNI:
2293 /* Command line and Omni completion can work with just about any
2294 * printable character, but do stop at white space. */
2295 return vim_isprintc(c) && !vim_iswhite(c);
2296
2297 case CTRL_X_WHOLE_LINE:
2298 /* For while line completion a space can be part of the line. */
2299 return vim_isprintc(c);
2300 }
2301 return vim_iswordc(c);
2302}
2303
2304/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002305 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002307 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 * the rest of the word to be in -- webb
2309 */
2310 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002311ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 char_u *str;
2313 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002314 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 char_u *fname;
2316 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002317 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002319 char_u *p;
2320 int i, c;
2321 int actual_len; /* Take multi-byte characters */
2322 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002323 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002324 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 int has_lower = FALSE;
2326 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002328 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002330 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002331
Bram Moolenaara2993e12007-08-12 14:38:46 +00002332 /* Find actual length of completion. */
2333#ifdef FEAT_MBYTE
2334 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002335 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002336 p = str;
2337 actual_len = 0;
2338 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002340 mb_ptr_adv(p);
2341 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002342 }
2343 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002344 else
2345#endif
2346 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347
Bram Moolenaara2993e12007-08-12 14:38:46 +00002348 /* Find actual length of original text. */
2349#ifdef FEAT_MBYTE
2350 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002352 p = compl_orig_text;
2353 actual_compl_length = 0;
2354 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002356 mb_ptr_adv(p);
2357 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002358 }
2359 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002360 else
2361#endif
2362 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002364 /* "actual_len" may be smaller than "actual_compl_length" when using
2365 * thesaurus, only use the minimum when comparing. */
2366 min_len = actual_len < actual_compl_length
2367 ? actual_len : actual_compl_length;
2368
Bram Moolenaara2993e12007-08-12 14:38:46 +00002369 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002370 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002371 if (wca != NULL)
2372 {
2373 p = str;
2374 for (i = 0; i < actual_len; ++i)
2375#ifdef FEAT_MBYTE
2376 if (has_mbyte)
2377 wca[i] = mb_ptr2char_adv(&p);
2378 else
2379#endif
2380 wca[i] = *(p++);
2381
2382 /* Rule 1: Were any chars converted to lower? */
2383 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002384 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002385 {
2386#ifdef FEAT_MBYTE
2387 if (has_mbyte)
2388 c = mb_ptr2char_adv(&p);
2389 else
2390#endif
2391 c = *(p++);
2392 if (MB_ISLOWER(c))
2393 {
2394 has_lower = TRUE;
2395 if (MB_ISUPPER(wca[i]))
2396 {
2397 /* Rule 1 is satisfied. */
2398 for (i = actual_compl_length; i < actual_len; ++i)
2399 wca[i] = MB_TOLOWER(wca[i]);
2400 break;
2401 }
2402 }
2403 }
2404
2405 /*
2406 * Rule 2: No lower case, 2nd consecutive letter converted to
2407 * upper case.
2408 */
2409 if (!has_lower)
2410 {
2411 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002412 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002413 {
2414#ifdef FEAT_MBYTE
2415 if (has_mbyte)
2416 c = mb_ptr2char_adv(&p);
2417 else
2418#endif
2419 c = *(p++);
2420 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2421 {
2422 /* Rule 2 is satisfied. */
2423 for (i = actual_compl_length; i < actual_len; ++i)
2424 wca[i] = MB_TOUPPER(wca[i]);
2425 break;
2426 }
2427 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2428 }
2429 }
2430
2431 /* Copy the original case of the part we typed. */
2432 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002433 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002434 {
2435#ifdef FEAT_MBYTE
2436 if (has_mbyte)
2437 c = mb_ptr2char_adv(&p);
2438 else
2439#endif
2440 c = *(p++);
2441 if (MB_ISLOWER(c))
2442 wca[i] = MB_TOLOWER(wca[i]);
2443 else if (MB_ISUPPER(c))
2444 wca[i] = MB_TOUPPER(wca[i]);
2445 }
2446
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002447 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002448 * Generate encoding specific output from wide character array.
2449 * Multi-byte characters can occupy up to five bytes more than
2450 * ASCII characters, and we also need one byte for NUL, so stay
2451 * six bytes away from the edge of IObuff.
2452 */
2453 p = IObuff;
2454 i = 0;
2455 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2456#ifdef FEAT_MBYTE
2457 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002458 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002459 else
2460#endif
2461 *(p++) = wca[i++];
2462 *p = NUL;
2463
2464 vim_free(wca);
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002467 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2468 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002470 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471}
2472
2473/*
2474 * Add a match to the list of matches.
2475 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002476 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002477 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002478 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002479 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002480ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 char_u *str;
2482 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002483 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002485 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002486 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002487 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002488 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002490 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002491 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002492
2493 ui_breakcheck();
2494 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002495 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 if (len < 0)
2497 len = (int)STRLEN(str);
2498
2499 /*
2500 * If the same match is already present, don't add it.
2501 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002502 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002504 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002505 do
2506 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002507 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002508 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002509 && match->cp_str[len] == NUL)
2510 return NOTDONE;
2511 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002512 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 }
2514
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002515 /* Remove any popup menu before changing the list of matches. */
2516 ins_compl_del_pum();
2517
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 /*
2519 * Allocate a new match structure.
2520 * Copy the values to the new match structure.
2521 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002522 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002524 return FAIL;
2525 match->cp_number = -1;
2526 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002527 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002528 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 {
2530 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002531 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002533 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002534
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002536 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2538 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002539 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002540 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002541 && compl_curr_match->cp_fname != NULL
2542 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002543 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002544 else if (fname != NULL)
2545 {
2546 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002547 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002549 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002550 match->cp_fname = NULL;
2551 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002552
2553 if (cptext != NULL)
2554 {
2555 int i;
2556
2557 for (i = 0; i < CPT_COUNT; ++i)
2558 if (cptext[i] != NULL && *cptext[i] != NUL)
2559 match->cp_text[i] = vim_strsave(cptext[i]);
2560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561
2562 /*
2563 * Link the new match structure in the list of matches.
2564 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002565 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 else if (dir == FORWARD)
2568 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002569 match->cp_next = compl_curr_match->cp_next;
2570 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
2572 else /* BACKWARD */
2573 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002574 match->cp_next = compl_curr_match;
2575 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002577 if (match->cp_next)
2578 match->cp_next->cp_prev = match;
2579 if (match->cp_prev)
2580 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002582 compl_first_match = match;
2583 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002584
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002585 /*
2586 * Find the longest common string if still doing that.
2587 */
2588 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2589 ins_compl_longest_match(match);
2590
Bram Moolenaar071d4272004-06-13 20:20:40 +00002591 return OK;
2592}
2593
2594/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002595 * Return TRUE if "str[len]" matches with match->cp_str, considering
2596 * match->cp_icase.
2597 */
2598 static int
2599ins_compl_equal(match, str, len)
2600 compl_T *match;
2601 char_u *str;
2602 int len;
2603{
2604 if (match->cp_icase)
2605 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2606 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2607}
2608
2609/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002610 * Reduce the longest common string for match "match".
2611 */
2612 static void
2613ins_compl_longest_match(match)
2614 compl_T *match;
2615{
2616 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002617 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002618 int had_match;
2619
2620 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002621 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002622 /* First match, use it as a whole. */
2623 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002624 if (compl_leader != NULL)
2625 {
2626 had_match = (curwin->w_cursor.col > compl_col);
2627 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002628 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002629 ins_redraw(FALSE);
2630
2631 /* When the match isn't there (to avoid matching itself) remove it
2632 * again after redrawing. */
2633 if (!had_match)
2634 ins_compl_delete();
2635 compl_used_match = FALSE;
2636 }
2637 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002638 else
2639 {
2640 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002641 p = compl_leader;
2642 s = match->cp_str;
2643 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002644 {
2645#ifdef FEAT_MBYTE
2646 if (has_mbyte)
2647 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002648 c1 = mb_ptr2char(p);
2649 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002650 }
2651 else
2652#endif
2653 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002654 c1 = *p;
2655 c2 = *s;
2656 }
2657 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2658 : (c1 != c2))
2659 break;
2660#ifdef FEAT_MBYTE
2661 if (has_mbyte)
2662 {
2663 mb_ptr_adv(p);
2664 mb_ptr_adv(s);
2665 }
2666 else
2667#endif
2668 {
2669 ++p;
2670 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002671 }
2672 }
2673
2674 if (*p != NUL)
2675 {
2676 /* Leader was shortened, need to change the inserted text. */
2677 *p = NUL;
2678 had_match = (curwin->w_cursor.col > compl_col);
2679 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002680 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002681 ins_redraw(FALSE);
2682
2683 /* When the match isn't there (to avoid matching itself) remove it
2684 * again after redrawing. */
2685 if (!had_match)
2686 ins_compl_delete();
2687 }
2688
2689 compl_used_match = FALSE;
2690 }
2691}
2692
2693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 * Add an array of matches to the list of matches.
2695 * Frees matches[].
2696 */
2697 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002698ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 int num_matches;
2700 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002701 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002702{
2703 int i;
2704 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002705 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002706
Bram Moolenaar572cb562005-08-05 21:35:02 +00002707 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002708 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002709 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002710 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002711 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002712 FreeWild(num_matches, matches);
2713}
2714
2715/* Make the completion list cyclic.
2716 * Return the number of matches (excluding the original).
2717 */
2718 static int
2719ins_compl_make_cyclic()
2720{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002721 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002722 int count = 0;
2723
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002724 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002725 {
2726 /*
2727 * Find the end of the list.
2728 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002729 match = compl_first_match;
2730 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002731 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002732 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002733 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002734 ++count;
2735 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002736 match->cp_next = compl_first_match;
2737 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002738 }
2739 return count;
2740}
2741
Bram Moolenaara94bc432006-03-10 21:42:59 +00002742/*
2743 * Start completion for the complete() function.
2744 * "startcol" is where the matched text starts (1 is first column).
2745 * "list" is the list of matches.
2746 */
2747 void
2748set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002749 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002750 list_T *list;
2751{
2752 /* If already doing completions stop it. */
2753 if (ctrl_x_mode != 0)
2754 ins_compl_prep(' ');
2755 ins_compl_clear();
2756
2757 if (stop_arrow() == FAIL)
2758 return;
2759
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002760 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002761 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002762 startcol = curwin->w_cursor.col;
2763 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002764 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002765 /* compl_pattern doesn't need to be set */
2766 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2767 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002768 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002769 return;
2770
2771 /* Handle like dictionary completion. */
2772 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2773
2774 ins_compl_add_list(list);
2775 compl_matches = ins_compl_make_cyclic();
2776 compl_started = TRUE;
2777 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002778 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002779
2780 compl_curr_match = compl_first_match;
2781 ins_complete(Ctrl_N);
2782 out_flush();
2783}
2784
2785
Bram Moolenaar9372a112005-12-06 19:59:18 +00002786/* "compl_match_array" points the currently displayed list of entries in the
2787 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002788static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002789static int compl_match_arraysize;
2790
2791/*
2792 * Update the screen and when there is any scrolling remove the popup menu.
2793 */
2794 static void
2795ins_compl_upd_pum()
2796{
2797 int h;
2798
2799 if (compl_match_array != NULL)
2800 {
2801 h = curwin->w_cline_height;
2802 update_screen(0);
2803 if (h != curwin->w_cline_height)
2804 ins_compl_del_pum();
2805 }
2806}
2807
2808/*
2809 * Remove any popup menu.
2810 */
2811 static void
2812ins_compl_del_pum()
2813{
2814 if (compl_match_array != NULL)
2815 {
2816 pum_undisplay();
2817 vim_free(compl_match_array);
2818 compl_match_array = NULL;
2819 }
2820}
2821
2822/*
2823 * Return TRUE if the popup menu should be displayed.
2824 */
2825 static int
2826pum_wanted()
2827{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002828 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002829 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002830 return FALSE;
2831
2832 /* The display looks bad on a B&W display. */
2833 if (t_colors < 8
2834#ifdef FEAT_GUI
2835 && !gui.in_use
2836#endif
2837 )
2838 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002839 return TRUE;
2840}
2841
2842/*
2843 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002844 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002845 */
2846 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002847pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002848{
2849 compl_T *compl;
2850 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002851
2852 /* Don't display the popup menu if there are no matches or there is only
2853 * one (ignoring the original text). */
2854 compl = compl_first_match;
2855 i = 0;
2856 do
2857 {
2858 if (compl == NULL
2859 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2860 break;
2861 compl = compl->cp_next;
2862 } while (compl != compl_first_match);
2863
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002864 if (strstr((char *)p_cot, "menuone") != NULL)
2865 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002866 return (i >= 2);
2867}
2868
2869/*
2870 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002871 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002872 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002873 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874ins_compl_show_pum()
2875{
2876 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002877 compl_T *shown_compl = NULL;
2878 int did_find_shown_match = FALSE;
2879 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002880 int i;
2881 int cur = -1;
2882 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002883 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002884
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002885 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002886 return;
2887
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002888#if defined(FEAT_EVAL)
2889 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2890 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2891#endif
2892
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002893 /* Update the screen before drawing the popup menu over it. */
2894 update_screen(0);
2895
2896 if (compl_match_array == NULL)
2897 {
2898 /* Need to build the popup menu list. */
2899 compl_match_arraysize = 0;
2900 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002901 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002902 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002903 do
2904 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002905 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2906 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002907 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002908 ++compl_match_arraysize;
2909 compl = compl->cp_next;
2910 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002911 if (compl_match_arraysize == 0)
2912 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002913 compl_match_array = (pumitem_T *)alloc_clear(
2914 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002915 * compl_match_arraysize));
2916 if (compl_match_array != NULL)
2917 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002918 /* If the current match is the original text don't find the first
2919 * match after it, don't highlight anything. */
2920 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2921 shown_match_ok = TRUE;
2922
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002923 i = 0;
2924 compl = compl_first_match;
2925 do
2926 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002927 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2928 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002929 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002930 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002931 if (!shown_match_ok)
2932 {
2933 if (compl == compl_shown_match || did_find_shown_match)
2934 {
2935 /* This item is the shown match or this is the
2936 * first displayed item after the shown match. */
2937 compl_shown_match = compl;
2938 did_find_shown_match = TRUE;
2939 shown_match_ok = TRUE;
2940 }
2941 else
2942 /* Remember this displayed match for when the
2943 * shown match is just below it. */
2944 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002945 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002946 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002947
2948 if (compl->cp_text[CPT_ABBR] != NULL)
2949 compl_match_array[i].pum_text =
2950 compl->cp_text[CPT_ABBR];
2951 else
2952 compl_match_array[i].pum_text = compl->cp_str;
2953 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2954 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2955 if (compl->cp_text[CPT_MENU] != NULL)
2956 compl_match_array[i++].pum_extra =
2957 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002958 else
2959 compl_match_array[i++].pum_extra = compl->cp_fname;
2960 }
2961
2962 if (compl == compl_shown_match)
2963 {
2964 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002965
2966 /* When the original text is the shown match don't set
2967 * compl_shown_match. */
2968 if (compl->cp_flags & ORIGINAL_TEXT)
2969 shown_match_ok = TRUE;
2970
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002971 if (!shown_match_ok && shown_compl != NULL)
2972 {
2973 /* The shown match isn't displayed, set it to the
2974 * previously displayed match. */
2975 compl_shown_match = shown_compl;
2976 shown_match_ok = TRUE;
2977 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002978 }
2979 compl = compl->cp_next;
2980 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002981
2982 if (!shown_match_ok) /* no displayed match at all */
2983 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002984 }
2985 }
2986 else
2987 {
2988 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002989 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002990 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2991 || compl_match_array[i].pum_text
2992 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002993 {
2994 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002995 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002996 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002997 }
2998
2999 if (compl_match_array != NULL)
3000 {
3001 /* Compute the screen column of the start of the completed text.
3002 * Use the cursor to get all wrapping and other settings right. */
3003 col = curwin->w_cursor.col;
3004 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003005 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003006 curwin->w_cursor.col = col;
3007 }
3008}
3009
Bram Moolenaar071d4272004-06-13 20:20:40 +00003010#define DICT_FIRST (1) /* use just first element in "dict" */
3011#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003012
Bram Moolenaar071d4272004-06-13 20:20:40 +00003013/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003014 * Add any identifiers that match the given pattern in the list of dictionary
3015 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 */
3017 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00003018ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
3019 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003020 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003021 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003022 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003023{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003024 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 char_u *ptr;
3026 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003027 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028 char_u **files;
3029 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003031 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032
Bram Moolenaar0b238792006-03-02 22:49:12 +00003033 if (*dict == NUL)
3034 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003035#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003036 /* When 'dictionary' is empty and spell checking is enabled use
3037 * "spell". */
3038 if (!thesaurus && curwin->w_p_spell)
3039 dict = (char_u *)"spell";
3040 else
3041#endif
3042 return;
3043 }
3044
Bram Moolenaar071d4272004-06-13 20:20:40 +00003045 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003046 if (buf == NULL)
3047 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003048 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 /* If 'infercase' is set, don't use 'smartcase' here */
3051 save_p_scs = p_scs;
3052 if (curbuf->b_p_inf)
3053 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003054
3055 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3056 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003057 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003058 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3059 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003060 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003061 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003062
3063 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003064 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003065 len = STRLEN(pat_esc) + 10;
3066 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003067 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003068 {
3069 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003070 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003071 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003072 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003073 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3074 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003075 vim_free(ptr);
3076 }
3077 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003078 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003079 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003080 if (regmatch.regprog == NULL)
3081 goto theend;
3082 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003083
Bram Moolenaar071d4272004-06-13 20:20:40 +00003084 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3085 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003086 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 {
3088 /* copy one dictionary file name into buf */
3089 if (flags == DICT_EXACT)
3090 {
3091 count = 1;
3092 files = &dict;
3093 }
3094 else
3095 {
3096 /* Expand wildcards in the dictionary name, but do not allow
3097 * backticks (for security, the 'dict' option may have been set in
3098 * a modeline). */
3099 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003100# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003101 if (!thesaurus && STRCMP(buf, "spell") == 0)
3102 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003103 else
3104# endif
3105 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003106 || expand_wildcards(1, &buf, &count, &files,
3107 EW_FILE|EW_SILENT) != OK)
3108 count = 0;
3109 }
3110
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003111# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003112 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003113 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003114 /* Complete from active spelling. Skip "\<" in the pattern, we
3115 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003116 if (pat[0] == '\\' && pat[1] == '<')
3117 ptr = pat + 2;
3118 else
3119 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003120 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003121 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003122 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003123# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003124 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003125 {
3126 ins_compl_files(count, files, thesaurus, flags,
3127 &regmatch, buf, &dir);
3128 if (flags != DICT_EXACT)
3129 FreeWild(count, files);
3130 }
3131 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003132 break;
3133 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003134
3135theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003136 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003137 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003138 vim_free(buf);
3139}
3140
Bram Moolenaar0b238792006-03-02 22:49:12 +00003141 static void
3142ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3143 int count;
3144 char_u **files;
3145 int thesaurus;
3146 int flags;
3147 regmatch_T *regmatch;
3148 char_u *buf;
3149 int *dir;
3150{
3151 char_u *ptr;
3152 int i;
3153 FILE *fp;
3154 int add_r;
3155
3156 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3157 {
3158 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3159 if (flags != DICT_EXACT)
3160 {
3161 vim_snprintf((char *)IObuff, IOSIZE,
3162 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003163 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003164 }
3165
3166 if (fp != NULL)
3167 {
3168 /*
3169 * Read dictionary file line by line.
3170 * Check each line for a match.
3171 */
3172 while (!got_int && !compl_interrupted
3173 && !vim_fgets(buf, LSIZE, fp))
3174 {
3175 ptr = buf;
3176 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3177 {
3178 ptr = regmatch->startp[0];
3179 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3180 ptr = find_line_end(ptr);
3181 else
3182 ptr = find_word_end(ptr);
3183 add_r = ins_compl_add_infercase(regmatch->startp[0],
3184 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003185 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003186 if (thesaurus)
3187 {
3188 char_u *wstart;
3189
3190 /*
3191 * Add the other matches on the line
3192 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003193 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003194 while (!got_int)
3195 {
3196 /* Find start of the next word. Skip white
3197 * space and punctuation. */
3198 ptr = find_word_start(ptr);
3199 if (*ptr == NUL || *ptr == NL)
3200 break;
3201 wstart = ptr;
3202
Bram Moolenaara2993e12007-08-12 14:38:46 +00003203 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003204#ifdef FEAT_MBYTE
3205 if (has_mbyte)
3206 /* Japanese words may have characters in
3207 * different classes, only separate words
3208 * with single-byte non-word characters. */
3209 while (*ptr != NUL)
3210 {
3211 int l = (*mb_ptr2len)(ptr);
3212
3213 if (l < 2 && !vim_iswordc(*ptr))
3214 break;
3215 ptr += l;
3216 }
3217 else
3218#endif
3219 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003220
3221 /* Add the word. Skip the regexp match. */
3222 if (wstart != regmatch->startp[0])
3223 add_r = ins_compl_add_infercase(wstart,
3224 (int)(ptr - wstart),
3225 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003226 }
3227 }
3228 if (add_r == OK)
3229 /* if dir was BACKWARD then honor it just once */
3230 *dir = FORWARD;
3231 else if (add_r == FAIL)
3232 break;
3233 /* avoid expensive call to vim_regexec() when at end
3234 * of line */
3235 if (*ptr == '\n' || got_int)
3236 break;
3237 }
3238 line_breakcheck();
3239 ins_compl_check_keys(50);
3240 }
3241 fclose(fp);
3242 }
3243 }
3244}
3245
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246/*
3247 * Find the start of the next word.
3248 * Returns a pointer to the first char of the word. Also stops at a NUL.
3249 */
3250 char_u *
3251find_word_start(ptr)
3252 char_u *ptr;
3253{
3254#ifdef FEAT_MBYTE
3255 if (has_mbyte)
3256 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003257 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003258 else
3259#endif
3260 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3261 ++ptr;
3262 return ptr;
3263}
3264
3265/*
3266 * Find the end of the word. Assumes it starts inside a word.
3267 * Returns a pointer to just after the word.
3268 */
3269 char_u *
3270find_word_end(ptr)
3271 char_u *ptr;
3272{
3273#ifdef FEAT_MBYTE
3274 int start_class;
3275
3276 if (has_mbyte)
3277 {
3278 start_class = mb_get_class(ptr);
3279 if (start_class > 1)
3280 while (*ptr != NUL)
3281 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003282 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003283 if (mb_get_class(ptr) != start_class)
3284 break;
3285 }
3286 }
3287 else
3288#endif
3289 while (vim_iswordc(*ptr))
3290 ++ptr;
3291 return ptr;
3292}
3293
3294/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003295 * Find the end of the line, omitting CR and NL at the end.
3296 * Returns a pointer to just after the line.
3297 */
3298 static char_u *
3299find_line_end(ptr)
3300 char_u *ptr;
3301{
3302 char_u *s;
3303
3304 s = ptr + STRLEN(ptr);
3305 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3306 --s;
3307 return s;
3308}
3309
3310/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311 * Free the list of completions
3312 */
3313 static void
3314ins_compl_free()
3315{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003316 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003317 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003318
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003319 vim_free(compl_pattern);
3320 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003321 vim_free(compl_leader);
3322 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003324 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003325 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003326
3327 ins_compl_del_pum();
3328 pum_clear();
3329
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003330 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003331 do
3332 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003333 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003334 compl_curr_match = compl_curr_match->cp_next;
3335 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003337 if (match->cp_flags & FREE_FNAME)
3338 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003339 for (i = 0; i < CPT_COUNT; ++i)
3340 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003342 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3343 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003344 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345}
3346
3347 static void
3348ins_compl_clear()
3349{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003350 compl_cont_status = 0;
3351 compl_started = FALSE;
3352 compl_matches = 0;
3353 vim_free(compl_pattern);
3354 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003355 vim_free(compl_leader);
3356 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003358 vim_free(compl_orig_text);
3359 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003360 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003361}
3362
3363/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003364 * Return TRUE when Insert completion is active.
3365 */
3366 int
3367ins_compl_active()
3368{
3369 return compl_started;
3370}
3371
3372/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003373 * Delete one character before the cursor and show the subset of the matches
3374 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003375 * Returns the character to be used, NUL if the work is done and another char
3376 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003377 */
3378 static int
3379ins_compl_bs()
3380{
3381 char_u *line;
3382 char_u *p;
3383
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003384 line = ml_get_curline();
3385 p = line + curwin->w_cursor.col;
3386 mb_ptr_back(line, p);
3387
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003388 /* Stop completion when the whole word was deleted. For Omni completion
3389 * allow the word to be deleted, we won't match everything. */
3390 if ((int)(p - line) - (int)compl_col < 0
3391 || ((int)(p - line) - (int)compl_col == 0
3392 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003393 return K_BS;
3394
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003395 /* Deleted more than what was used to find matches or didn't finish
3396 * finding all matches: need to look for matches all over again. */
3397 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003398 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003399 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003400
Bram Moolenaara6557602006-02-04 22:43:20 +00003401 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003402 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003403 if (compl_leader != NULL)
3404 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003405 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003406 if (compl_shown_match != NULL)
3407 /* Make sure current match is not a hidden item. */
3408 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003409 return NUL;
3410 }
3411 return K_BS;
3412}
Bram Moolenaara6557602006-02-04 22:43:20 +00003413
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003414/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003415 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3416 * be called.
3417 */
3418 static int
3419ins_compl_need_restart()
3420{
3421 /* Return TRUE if we didn't complete finding matches or when the
3422 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3423 return compl_was_interrupted
3424 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3425 && compl_opt_refresh_always);
3426}
3427
3428/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003429 * Called after changing "compl_leader".
3430 * Show the popup menu with a different set of matches.
3431 * May also search for matches again if the previous search was interrupted.
3432 */
3433 static void
3434ins_compl_new_leader()
3435{
3436 ins_compl_del_pum();
3437 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003438 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003439 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003440
3441 if (compl_started)
3442 ins_compl_set_original_text(compl_leader);
3443 else
3444 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003445#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003446 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003447#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003448 /*
3449 * Matches were cleared, need to search for them now. First display
3450 * the changed text before the cursor. Set "compl_restarting" to
3451 * avoid that the first match is inserted.
3452 */
3453 update_screen(0);
3454#ifdef FEAT_GUI
3455 if (gui.in_use)
3456 {
3457 /* Show the cursor after the match, not after the redrawn text. */
3458 setcursor();
3459 out_flush();
3460 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003461 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003462#endif
3463 compl_restarting = TRUE;
3464 if (ins_complete(Ctrl_N) == FAIL)
3465 compl_cont_status = 0;
3466 compl_restarting = FALSE;
3467 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003468
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003469 compl_enter_selects = !compl_used_match;
Bram Moolenaar6b487142013-06-29 16:21:58 +02003470 compl_shown_match = compl_curr_match = compl_first_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003471
3472 /* Show the popup menu with a different set of matches. */
3473 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003474
3475 /* Don't let Enter select the original text when there is no popup menu. */
3476 if (compl_match_array == NULL)
3477 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003478}
3479
3480/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003481 * Return the length of the completion, from the completion start column to
3482 * the cursor column. Making sure it never goes below zero.
3483 */
3484 static int
3485ins_compl_len()
3486{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003487 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003488
3489 if (off < 0)
3490 return 0;
3491 return off;
3492}
3493
3494/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003495 * Append one character to the match leader. May reduce the number of
3496 * matches.
3497 */
3498 static void
3499ins_compl_addleader(c)
3500 int c;
3501{
3502#ifdef FEAT_MBYTE
3503 int cc;
3504
3505 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3506 {
3507 char_u buf[MB_MAXBYTES + 1];
3508
3509 (*mb_char2bytes)(c, buf);
3510 buf[cc] = NUL;
3511 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003512 if (compl_opt_refresh_always)
3513 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003514 }
3515 else
3516#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003517 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003518 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003519 if (compl_opt_refresh_always)
3520 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003521 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003522
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003523 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003524 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003525 ins_compl_restart();
3526
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003527 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003528 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003529 * break redo. */
3530 if (!compl_opt_refresh_always)
3531 {
3532 vim_free(compl_leader);
3533 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003534 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003535 if (compl_leader != NULL)
3536 ins_compl_new_leader();
3537 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003538}
3539
3540/*
3541 * Setup for finding completions again without leaving CTRL-X mode. Used when
3542 * BS or a key was typed while still searching for matches.
3543 */
3544 static void
3545ins_compl_restart()
3546{
3547 ins_compl_free();
3548 compl_started = FALSE;
3549 compl_matches = 0;
3550 compl_cont_status = 0;
3551 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003552}
3553
3554/*
3555 * Set the first match, the original text.
3556 */
3557 static void
3558ins_compl_set_original_text(str)
3559 char_u *str;
3560{
3561 char_u *p;
3562
3563 /* Replace the original text entry. */
3564 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3565 {
3566 p = vim_strsave(str);
3567 if (p != NULL)
3568 {
3569 vim_free(compl_first_match->cp_str);
3570 compl_first_match->cp_str = p;
3571 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003572 }
3573}
3574
3575/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003576 * Append one character to the match leader. May reduce the number of
3577 * matches.
3578 */
3579 static void
3580ins_compl_addfrommatch()
3581{
3582 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003583 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003584 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003585 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003586
3587 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003588 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003589 {
3590 /* When still at the original match use the first entry that matches
3591 * the leader. */
3592 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3593 {
3594 p = NULL;
3595 for (cp = compl_shown_match->cp_next; cp != NULL
3596 && cp != compl_first_match; cp = cp->cp_next)
3597 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003598 if (compl_leader == NULL
3599 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003600 (int)STRLEN(compl_leader)))
3601 {
3602 p = cp->cp_str;
3603 break;
3604 }
3605 }
3606 if (p == NULL || (int)STRLEN(p) <= len)
3607 return;
3608 }
3609 else
3610 return;
3611 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003612 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003613 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003614 ins_compl_addleader(c);
3615}
3616
3617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003619 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003620 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003622 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623ins_compl_prep(c)
3624 int c;
3625{
3626 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003628 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630 /* Forget any previous 'special' messages if this is actually
3631 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3632 */
3633 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3634 edit_submode_extra = NULL;
3635
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003636 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003637 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3638 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003639 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003640
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003641 /* Set "compl_get_longest" when finding the first matches. */
3642 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3643 || (ctrl_x_mode == 0 && !compl_started))
3644 {
3645 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3646 compl_used_match = TRUE;
3647 }
3648
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3650 {
3651 /*
3652 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3653 * it will be yet. Now we decide.
3654 */
3655 switch (c)
3656 {
3657 case Ctrl_E:
3658 case Ctrl_Y:
3659 ctrl_x_mode = CTRL_X_SCROLL;
3660 if (!(State & REPLACE_FLAG))
3661 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3662 else
3663 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3664 edit_submode_pre = NULL;
3665 showmode();
3666 break;
3667 case Ctrl_L:
3668 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3669 break;
3670 case Ctrl_F:
3671 ctrl_x_mode = CTRL_X_FILES;
3672 break;
3673 case Ctrl_K:
3674 ctrl_x_mode = CTRL_X_DICTIONARY;
3675 break;
3676 case Ctrl_R:
3677 /* Simply allow ^R to happen without affecting ^X mode */
3678 break;
3679 case Ctrl_T:
3680 ctrl_x_mode = CTRL_X_THESAURUS;
3681 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003682#ifdef FEAT_COMPL_FUNC
3683 case Ctrl_U:
3684 ctrl_x_mode = CTRL_X_FUNCTION;
3685 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003686 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003687 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003688 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003689#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003690 case 's':
3691 case Ctrl_S:
3692 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003693#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003694 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003695 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003696 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003697#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003698 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003699 case Ctrl_RSB:
3700 ctrl_x_mode = CTRL_X_TAGS;
3701 break;
3702#ifdef FEAT_FIND_ID
3703 case Ctrl_I:
3704 case K_S_TAB:
3705 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3706 break;
3707 case Ctrl_D:
3708 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3709 break;
3710#endif
3711 case Ctrl_V:
3712 case Ctrl_Q:
3713 ctrl_x_mode = CTRL_X_CMDLINE;
3714 break;
3715 case Ctrl_P:
3716 case Ctrl_N:
3717 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3718 * just started ^X mode, or there were enough ^X's to cancel
3719 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3720 * do normal expansion when interrupting a different mode (say
3721 * ^X^F^X^P or ^P^X^X^P, see below)
3722 * nothing changes if interrupting mode 0, (eg, the flag
3723 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003724 if (!(compl_cont_status & CONT_INTRPT))
3725 compl_cont_status |= CONT_LOCAL;
3726 else if (compl_cont_mode != 0)
3727 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 /* FALLTHROUGH */
3729 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003730 /* If we have typed at least 2 ^X's... for modes != 0, we set
3731 * compl_cont_status = 0 (eg, as if we had just started ^X
3732 * mode).
3733 * For mode 0, we set "compl_cont_mode" to an impossible
3734 * value, in both cases ^X^X can be used to restart the same
3735 * mode (avoiding ADDING mode).
3736 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3737 * 'complete' and local ^P expansions respectively.
3738 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3739 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 if (c == Ctrl_X)
3741 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003742 if (compl_cont_mode != 0)
3743 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003745 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 }
3747 ctrl_x_mode = 0;
3748 edit_submode = NULL;
3749 showmode();
3750 break;
3751 }
3752 }
3753 else if (ctrl_x_mode != 0)
3754 {
3755 /* We're already in CTRL-X mode, do we stay in it? */
3756 if (!vim_is_ctrl_x_key(c))
3757 {
3758 if (ctrl_x_mode == CTRL_X_SCROLL)
3759 ctrl_x_mode = 0;
3760 else
3761 ctrl_x_mode = CTRL_X_FINISHED;
3762 edit_submode = NULL;
3763 }
3764 showmode();
3765 }
3766
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003767 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 {
3769 /* Show error message from attempted keyword completion (probably
3770 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003771 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003773 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3774 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 || ctrl_x_mode == CTRL_X_FINISHED)
3776 {
3777 /* Get here when we have finished typing a sequence of ^N and
3778 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003779 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003780 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003781 {
3782 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003783 * If any of the original typed text has been changed, eg when
3784 * ignorecase is set, we must add back-spaces to the redo
3785 * buffer. We add as few as necessary to delete just the part
3786 * of the original text that has changed.
3787 * When using the longest match, edited the match or used
3788 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003789 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003790 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3791 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003792 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003793 ptr = NULL;
3794 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003795 }
3796
3797#ifdef FEAT_CINDENT
3798 want_cindent = (can_cindent && cindent_on());
3799#endif
3800 /*
3801 * When completing whole lines: fix indent for 'cindent'.
3802 * Otherwise, break line if it's too long.
3803 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003804 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 {
3806#ifdef FEAT_CINDENT
3807 /* re-indent the current line */
3808 if (want_cindent)
3809 {
3810 do_c_expr_indent();
3811 want_cindent = FALSE; /* don't do it again */
3812 }
3813#endif
3814 }
3815 else
3816 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003817 int prev_col = curwin->w_cursor.col;
3818
Bram Moolenaar071d4272004-06-13 20:20:40 +00003819 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003820 if (prev_col > 0)
3821 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003822 if (stop_arrow() == OK)
3823 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003824 if (prev_col > 0
3825 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3826 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 }
3828
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003829 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003830 * the selection without inserting anything. When
3831 * compl_enter_selects is set the Enter key does the same. */
3832 if ((c == Ctrl_Y || (compl_enter_selects
3833 && (c == CAR || c == K_KENTER || c == NL)))
3834 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003835 retval = TRUE;
3836
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003837 /* CTRL-E means completion is Ended, go back to the typed text. */
3838 if (c == Ctrl_E)
3839 {
3840 ins_compl_delete();
3841 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003842 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003843 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003844 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003845 retval = TRUE;
3846 }
3847
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003848 auto_format(FALSE, TRUE);
3849
Bram Moolenaar071d4272004-06-13 20:20:40 +00003850 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003851 compl_started = FALSE;
3852 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003853 msg_clr_cmdline(); /* necessary for "noshowmode" */
3854 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003855 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003856 if (edit_submode != NULL)
3857 {
3858 edit_submode = NULL;
3859 showmode();
3860 }
3861
3862#ifdef FEAT_CINDENT
3863 /*
3864 * Indent now if a key was typed that is in 'cinkeys'.
3865 */
3866 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3867 do_c_expr_indent();
3868#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003869#ifdef FEAT_AUTOCMD
3870 /* Trigger the CompleteDone event to give scripts a chance to act
3871 * upon the completion. */
3872 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 }
3875 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003876#ifdef FEAT_AUTOCMD
3877 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3878 /* Trigger the CompleteDone event to give scripts a chance to act
3879 * upon the (possibly failed) completion. */
3880 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3881#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003882
3883 /* reset continue_* if we left expansion-mode, if we stay they'll be
3884 * (re)set properly in ins_complete() */
3885 if (!vim_is_ctrl_x_key(c))
3886 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003887 compl_cont_status = 0;
3888 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003889 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003890
3891 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892}
3893
3894/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003895 * Fix the redo buffer for the completion leader replacing some of the typed
3896 * text. This inserts backspaces and appends the changed text.
3897 * "ptr" is the known leader text or NUL.
3898 */
3899 static void
3900ins_compl_fixRedoBufForLeader(ptr_arg)
3901 char_u *ptr_arg;
3902{
3903 int len;
3904 char_u *p;
3905 char_u *ptr = ptr_arg;
3906
3907 if (ptr == NULL)
3908 {
3909 if (compl_leader != NULL)
3910 ptr = compl_leader;
3911 else
3912 return; /* nothing to do */
3913 }
3914 if (compl_orig_text != NULL)
3915 {
3916 p = compl_orig_text;
3917 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3918 ;
3919#ifdef FEAT_MBYTE
3920 if (len > 0)
3921 len -= (*mb_head_off)(p, p + len);
3922#endif
3923 for (p += len; *p != NUL; mb_ptr_adv(p))
3924 AppendCharToRedobuff(K_BS);
3925 }
3926 else
3927 len = 0;
3928 if (ptr != NULL)
3929 AppendToRedobuffLit(ptr + len, -1);
3930}
3931
3932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3934 * (depending on flag) starting from buf and looking for a non-scanned
3935 * buffer (other than curbuf). curbuf is special, if it is called with
3936 * buf=curbuf then it has to be the first call for a given flag/expansion.
3937 *
3938 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3939 */
3940 static buf_T *
3941ins_compl_next_buf(buf, flag)
3942 buf_T *buf;
3943 int flag;
3944{
3945#ifdef FEAT_WINDOWS
3946 static win_T *wp;
3947#endif
3948
3949 if (flag == 'w') /* just windows */
3950 {
3951#ifdef FEAT_WINDOWS
3952 if (buf == curbuf) /* first call for this flag/expansion */
3953 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003954 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 && wp->w_buffer->b_scanned)
3956 ;
3957 buf = wp->w_buffer;
3958#else
3959 buf = curbuf;
3960#endif
3961 }
3962 else
3963 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3964 * (unlisted buffers)
3965 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003966 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003967 && ((flag == 'U'
3968 ? buf->b_p_bl
3969 : (!buf->b_p_bl
3970 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003971 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 ;
3973 return buf;
3974}
3975
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003976#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003977static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003978
3979/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003980 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003981 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003982 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003983 static void
3984expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003985 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003986 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003987{
Bram Moolenaar82139082011-09-14 16:52:09 +02003988 list_T *matchlist = NULL;
3989 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003990 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003991 char_u *funcname;
3992 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003993 win_T *curwin_save;
3994 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02003995 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003996
Bram Moolenaare344bea2005-09-01 20:46:49 +00003997 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3998 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003999 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004000
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004001 /* Call 'completefunc' to obtain the list of matches. */
4002 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004003 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004004
Bram Moolenaare344bea2005-09-01 20:46:49 +00004005 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004006 curwin_save = curwin;
4007 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004008
4009 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004010 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004011 {
4012 switch (rettv.v_type)
4013 {
4014 case VAR_LIST:
4015 matchlist = rettv.vval.v_list;
4016 break;
4017 case VAR_DICT:
4018 matchdict = rettv.vval.v_dict;
4019 break;
4020 default:
4021 /* TODO: Give error message? */
4022 clear_tv(&rettv);
4023 break;
4024 }
4025 }
4026
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004027 if (curwin_save != curwin || curbuf_save != curbuf)
4028 {
4029 EMSG(_(e_complwin));
4030 goto theend;
4031 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004032 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004033 check_cursor();
4034 if (!equalpos(curwin->w_cursor, pos))
4035 {
4036 EMSG(_(e_compldel));
4037 goto theend;
4038 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004039
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004040 if (matchlist != NULL)
4041 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004042 else if (matchdict != NULL)
4043 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004044
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004045theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004046 if (matchdict != NULL)
4047 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004048 if (matchlist != NULL)
4049 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004050}
4051#endif /* FEAT_COMPL_FUNC */
4052
Bram Moolenaar39f05632006-03-19 22:15:26 +00004053#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004054/*
4055 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004056 */
4057 static void
4058ins_compl_add_list(list)
4059 list_T *list;
4060{
4061 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004062 int dir = compl_direction;
4063
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004064 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004065 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004066 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004067 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4068 /* if dir was BACKWARD then honor it just once */
4069 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004070 else if (did_emsg)
4071 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004072 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004073}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004074
4075/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004076 * Add completions from a dict.
4077 */
4078 static void
4079ins_compl_add_dict(dict)
4080 dict_T *dict;
4081{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004082 dictitem_T *di_refresh;
4083 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004084
4085 /* Check for optional "refresh" item. */
4086 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004087 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4088 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004089 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004090 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004091
4092 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4093 compl_opt_refresh_always = TRUE;
4094 }
4095
4096 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004097 di_words = dict_find(dict, (char_u *)"words", 5);
4098 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4099 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004100}
4101
4102/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004103 * Add a match to the list of matches from a typeval_T.
4104 * If the given string is already in the list of completions, then return
4105 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4106 * maybe because alloc() returns NULL, then FAIL is returned.
4107 */
4108 int
4109ins_compl_add_tv(tv, dir)
4110 typval_T *tv;
4111 int dir;
4112{
4113 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004114 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004115 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004116 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004117 char_u *(cptext[CPT_COUNT]);
4118
4119 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4120 {
4121 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4122 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4123 (char_u *)"abbr", FALSE);
4124 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4125 (char_u *)"menu", FALSE);
4126 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4127 (char_u *)"kind", FALSE);
4128 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4129 (char_u *)"info", FALSE);
4130 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4131 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004132 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004133 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004134 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4135 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004136 }
4137 else
4138 {
4139 word = get_tv_string_chk(tv);
4140 vim_memset(cptext, 0, sizeof(cptext));
4141 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004142 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004143 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004144 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004145}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004146#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004148/*
4149 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004150 * The search starts at position "ini" in curbuf and in the direction
4151 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004152 * When "compl_started" is FALSE start at that position, otherwise continue
4153 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004154 * This may return before finding all the matches.
4155 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 */
4157 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004158ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160{
4161 static pos_T first_match_pos;
4162 static pos_T last_match_pos;
4163 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004164 static int found_all = FALSE; /* Found all matches of a
4165 certain type. */
4166 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167
Bram Moolenaar572cb562005-08-05 21:35:02 +00004168 pos_T *pos;
4169 char_u **matches;
4170 int save_p_scs;
4171 int save_p_ws;
4172 int save_p_ic;
4173 int i;
4174 int num_matches;
4175 int len;
4176 int found_new_match;
4177 int type = ctrl_x_mode;
4178 char_u *ptr;
4179 char_u *dict = NULL;
4180 int dict_f = 0;
4181 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004182
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004183 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
4185 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4186 ins_buf->b_scanned = 0;
4187 found_all = FALSE;
4188 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004189 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004190 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 last_match_pos = first_match_pos = *ini;
4192 }
4193
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004194 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004195 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004196 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4197 for (;;)
4198 {
4199 found_new_match = FAIL;
4200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004201 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 * or if found_all says this entry is done. For ^X^L only use the
4203 * entries from 'complete' that look in loaded buffers. */
4204 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004205 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
4207 found_all = FALSE;
4208 while (*e_cpt == ',' || *e_cpt == ' ')
4209 e_cpt++;
4210 if (*e_cpt == '.' && !curbuf->b_scanned)
4211 {
4212 ins_buf = curbuf;
4213 first_match_pos = *ini;
4214 /* So that ^N can match word immediately after cursor */
4215 if (ctrl_x_mode == 0)
4216 dec(&first_match_pos);
4217 last_match_pos = first_match_pos;
4218 type = 0;
4219 }
4220 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4221 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4222 {
4223 /* Scan a buffer, but not the current one. */
4224 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4225 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004226 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 first_match_pos.col = last_match_pos.col = 0;
4228 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4229 last_match_pos.lnum = 0;
4230 type = 0;
4231 }
4232 else /* unloaded buffer, scan like dictionary */
4233 {
4234 found_all = TRUE;
4235 if (ins_buf->b_fname == NULL)
4236 continue;
4237 type = CTRL_X_DICTIONARY;
4238 dict = ins_buf->b_fname;
4239 dict_f = DICT_EXACT;
4240 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004241 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 ins_buf->b_fname == NULL
4243 ? buf_spname(ins_buf)
4244 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004245 ? ins_buf->b_fname
4246 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004247 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 }
4249 else if (*e_cpt == NUL)
4250 break;
4251 else
4252 {
4253 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4254 type = -1;
4255 else if (*e_cpt == 'k' || *e_cpt == 's')
4256 {
4257 if (*e_cpt == 'k')
4258 type = CTRL_X_DICTIONARY;
4259 else
4260 type = CTRL_X_THESAURUS;
4261 if (*++e_cpt != ',' && *e_cpt != NUL)
4262 {
4263 dict = e_cpt;
4264 dict_f = DICT_FIRST;
4265 }
4266 }
4267#ifdef FEAT_FIND_ID
4268 else if (*e_cpt == 'i')
4269 type = CTRL_X_PATH_PATTERNS;
4270 else if (*e_cpt == 'd')
4271 type = CTRL_X_PATH_DEFINES;
4272#endif
4273 else if (*e_cpt == ']' || *e_cpt == 't')
4274 {
4275 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004276 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004277 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 else
4280 type = -1;
4281
4282 /* in any case e_cpt is advanced to the next entry */
4283 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4284
4285 found_all = TRUE;
4286 if (type == -1)
4287 continue;
4288 }
4289 }
4290
4291 switch (type)
4292 {
4293 case -1:
4294 break;
4295#ifdef FEAT_FIND_ID
4296 case CTRL_X_PATH_PATTERNS:
4297 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004298 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004299 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004301 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4303 (linenr_T)1, (linenr_T)MAXLNUM);
4304 break;
4305#endif
4306
4307 case CTRL_X_DICTIONARY:
4308 case CTRL_X_THESAURUS:
4309 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004310 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 : (type == CTRL_X_THESAURUS
4312 ? (*curbuf->b_p_tsr == NUL
4313 ? p_tsr
4314 : curbuf->b_p_tsr)
4315 : (*curbuf->b_p_dict == NUL
4316 ? p_dict
4317 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004318 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004319 dict != NULL ? dict_f
4320 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 dict = NULL;
4322 break;
4323
4324 case CTRL_X_TAGS:
4325 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4326 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004327 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004328
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004329 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004330 * of matches is found when compl_pattern is empty */
4331 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4333 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4334 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4335 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004336 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 }
4338 p_ic = save_p_ic;
4339 break;
4340
4341 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004342 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4344 {
4345
4346 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004347 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004348 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 }
4350 break;
4351
4352 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353 if (expand_cmdline(&compl_xp, compl_pattern,
4354 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004356 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 break;
4358
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004359#ifdef FEAT_COMPL_FUNC
4360 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004361 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004362 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004363 break;
4364#endif
4365
Bram Moolenaar488c6512005-08-11 20:09:58 +00004366 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004367#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004368 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004369 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004370 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004371 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004372#endif
4373 break;
4374
Bram Moolenaar071d4272004-06-13 20:20:40 +00004375 default: /* normal ^P/^N and ^X^L */
4376 /*
4377 * If 'infercase' is set, don't use 'smartcase' here
4378 */
4379 save_p_scs = p_scs;
4380 if (ins_buf->b_p_inf)
4381 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 /* buffers other than curbuf are scanned from the beginning or the
4384 * end but never from the middle, thus setting nowrapscan in this
4385 * buffers is a good idea, on the other hand, we always set
4386 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4387 save_p_ws = p_ws;
4388 if (ins_buf != curbuf)
4389 p_ws = FALSE;
4390 else if (*e_cpt == '.')
4391 p_ws = TRUE;
4392 for (;;)
4393 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004394 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004396 ++msg_silent; /* Don't want messages for wrapscan. */
4397
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004398 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4399 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004403 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004405 found_new_match = searchit(NULL, ins_buf, pos,
4406 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004407 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004408 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004409 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004410 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004412 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 first_match_pos = *pos;
4415 last_match_pos = *pos;
4416 }
4417 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004418 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004419 found_new_match = FAIL;
4420 if (found_new_match == FAIL)
4421 {
4422 if (ins_buf == curbuf)
4423 found_all = TRUE;
4424 break;
4425 }
4426
4427 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004428 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 && ini->lnum == pos->lnum
4430 && ini->col == pos->col)
4431 continue;
4432 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4433 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4434 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004435 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 {
4437 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4438 continue;
4439 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4440 if (!p_paste)
4441 ptr = skipwhite(ptr);
4442 }
4443 len = (int)STRLEN(ptr);
4444 }
4445 else
4446 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447 char_u *tmp_ptr = ptr;
4448
4449 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004451 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 /* Skip if already inside a word. */
4453 if (vim_iswordp(tmp_ptr))
4454 continue;
4455 /* Find start of next word. */
4456 tmp_ptr = find_word_start(tmp_ptr);
4457 }
4458 /* Find end of this word. */
4459 tmp_ptr = find_word_end(tmp_ptr);
4460 len = (int)(tmp_ptr - ptr);
4461
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004462 if ((compl_cont_status & CONT_ADDING)
4463 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 {
4465 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4466 {
4467 /* Try next line, if any. the new word will be
4468 * "join" as if the normal command "J" was used.
4469 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004470 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 * works -- Acevedo */
4472 STRNCPY(IObuff, ptr, len);
4473 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4474 tmp_ptr = ptr = skipwhite(ptr);
4475 /* Find start of next word. */
4476 tmp_ptr = find_word_start(tmp_ptr);
4477 /* Find end of next word. */
4478 tmp_ptr = find_word_end(tmp_ptr);
4479 if (tmp_ptr > ptr)
4480 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004481 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004483 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484 IObuff[len++] = ' ';
4485 /* IObuf =~ "\k.* ", thus len >= 2 */
4486 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004487 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004488 || (vim_strchr(p_cpo, CPO_JOINSP)
4489 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004490 && (IObuff[len - 2] == '?'
4491 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004492 IObuff[len++] = ' ';
4493 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004494 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004495 if (tmp_ptr - ptr >= IOSIZE - len)
4496 tmp_ptr = ptr + IOSIZE - len - 1;
4497 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4498 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004499 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
4501 IObuff[len] = NUL;
4502 ptr = IObuff;
4503 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004504 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004505 continue;
4506 }
4507 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004508 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004509 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004510 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004511 {
4512 found_new_match = OK;
4513 break;
4514 }
4515 }
4516 p_scs = save_p_scs;
4517 p_ws = save_p_ws;
4518 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004519
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004520 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004521 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004522 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 found_new_match = OK;
4524
4525 /* break the loop for specialized modes (use 'complete' just for the
4526 * generic ctrl_x_mode == 0) or when we've found a new match */
4527 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004528 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004529 {
4530 if (got_int)
4531 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004532 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004533 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004534 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004535
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004536 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4537 || compl_interrupted)
4538 break;
4539 compl_started = TRUE;
4540 }
4541 else
4542 {
4543 /* Mark a buffer scanned when it has been scanned completely */
4544 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4545 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004547 compl_started = FALSE;
4548 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004549 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004550 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551
4552 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4553 && *e_cpt == NUL) /* Got to end of 'complete' */
4554 found_new_match = FAIL;
4555
4556 i = -1; /* total of matches, unknown */
4557 if (found_new_match == FAIL
4558 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4559 i = ins_compl_make_cyclic();
4560
4561 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 * just been made cyclic then we have to move compl_curr_match to the next
4563 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004564 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4565 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004566 if (compl_curr_match == NULL)
4567 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004568 return i;
4569}
4570
4571/* Delete the old text being completed. */
4572 static void
4573ins_compl_delete()
4574{
4575 int i;
4576
4577 /*
4578 * In insert mode: Delete the typed part.
4579 * In replace mode: Put the old characters back, if any.
4580 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004581 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 backspace_until_column(i);
4583 changed_cline_bef_curs();
4584}
4585
4586/* Insert the new text being completed. */
4587 static void
4588ins_compl_insert()
4589{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004590 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004591 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4592 compl_used_match = FALSE;
4593 else
4594 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595}
4596
4597/*
4598 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004599 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4600 * get more completions. If it is FALSE, then we just do nothing when there
4601 * are no more completions in a given direction. The latter case is used when
4602 * we are still in the middle of finding completions, to allow browsing
4603 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 * Return the total number of matches, or -1 if still unknown -- webb.
4605 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004606 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4607 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 *
4609 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004610 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4611 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 */
4613 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004614ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004616 int count; /* repeat completion this many times; should
4617 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004618 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004619{
4620 int num_matches = -1;
4621 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004622 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004623 compl_T *found_compl = NULL;
4624 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004625 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004627 /* When user complete function return -1 for findstart which is next
4628 * time of 'always', compl_shown_match become NULL. */
4629 if (compl_shown_match == NULL)
4630 return -1;
4631
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004632 if (compl_leader != NULL
4633 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004635 /* Set "compl_shown_match" to the actually shown match, it may differ
4636 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004637 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004638 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004639 && compl_shown_match->cp_next != NULL
4640 && compl_shown_match->cp_next != compl_first_match)
4641 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004642
4643 /* If we didn't find it searching forward, and compl_shows_dir is
4644 * backward, find the last match. */
4645 if (compl_shows_dir == BACKWARD
4646 && !ins_compl_equal(compl_shown_match,
4647 compl_leader, (int)STRLEN(compl_leader))
4648 && (compl_shown_match->cp_next == NULL
4649 || compl_shown_match->cp_next == compl_first_match))
4650 {
4651 while (!ins_compl_equal(compl_shown_match,
4652 compl_leader, (int)STRLEN(compl_leader))
4653 && compl_shown_match->cp_prev != NULL
4654 && compl_shown_match->cp_prev != compl_first_match)
4655 compl_shown_match = compl_shown_match->cp_prev;
4656 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004657 }
4658
4659 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004660 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004661 /* Delete old text to be replaced */
4662 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004663
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004664 /* When finding the longest common text we stick at the original text,
4665 * don't let CTRL-N or CTRL-P move to the first match. */
4666 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4667
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004668 /* When restarting the search don't insert the first match either. */
4669 if (compl_restarting)
4670 {
4671 advance = FALSE;
4672 compl_restarting = FALSE;
4673 }
4674
Bram Moolenaare3226be2005-12-18 22:10:00 +00004675 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4676 * around. */
4677 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004679 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004681 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004682 found_end = (compl_first_match != NULL
4683 && (compl_shown_match->cp_next == compl_first_match
4684 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004685 }
4686 else if (compl_shows_dir == BACKWARD
4687 && compl_shown_match->cp_prev != NULL)
4688 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004689 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004690 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004691 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 }
4693 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004694 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004695 if (!allow_get_expansion)
4696 {
4697 if (advance)
4698 {
4699 if (compl_shows_dir == BACKWARD)
4700 compl_pending -= todo + 1;
4701 else
4702 compl_pending += todo + 1;
4703 }
4704 return -1;
4705 }
4706
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004707 if (advance)
4708 {
4709 if (compl_shows_dir == BACKWARD)
4710 --compl_pending;
4711 else
4712 ++compl_pending;
4713 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004714
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004715 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004716 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004717
4718 /* handle any pending completions */
4719 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004720 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004721 {
4722 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4723 {
4724 compl_shown_match = compl_shown_match->cp_next;
4725 --compl_pending;
4726 }
4727 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4728 {
4729 compl_shown_match = compl_shown_match->cp_prev;
4730 ++compl_pending;
4731 }
4732 else
4733 break;
4734 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004735 found_end = FALSE;
4736 }
4737 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4738 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004739 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004740 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004741 ++todo;
4742 else
4743 /* Remember a matching item. */
4744 found_compl = compl_shown_match;
4745
4746 /* Stop at the end of the list when we found a usable match. */
4747 if (found_end)
4748 {
4749 if (found_compl != NULL)
4750 {
4751 compl_shown_match = found_compl;
4752 break;
4753 }
4754 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004758 /* Insert the text of the new completion, or the compl_leader. */
4759 if (insert_match)
4760 {
4761 if (!compl_get_longest || compl_used_match)
4762 ins_compl_insert();
4763 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004764 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004765 }
4766 else
4767 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768
4769 if (!allow_get_expansion)
4770 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004771 /* may undisplay the popup menu first */
4772 ins_compl_upd_pum();
4773
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004774 /* redraw to show the user what was inserted */
4775 update_screen(0);
4776
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004777 /* display the updated popup menu */
4778 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004779#ifdef FEAT_GUI
4780 if (gui.in_use)
4781 {
4782 /* Show the cursor after the match, not after the redrawn text. */
4783 setcursor();
4784 out_flush();
4785 gui_update_cursor(FALSE, FALSE);
4786 }
4787#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004788
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 /* Delete old text to be replaced, since we're still searching and
4790 * don't want to match ourselves! */
4791 ins_compl_delete();
4792 }
4793
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004794 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004795 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004796 compl_enter_selects = !insert_match && compl_match_array != NULL;
4797
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 /*
4799 * Show the file name for the match (if any)
4800 * Truncate the file name to avoid a wait for return.
4801 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004802 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004803 {
4804 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004805 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 if (i <= 0)
4807 i = 0;
4808 else
4809 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004810 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 msg(IObuff);
4812 redraw_cmdline = FALSE; /* don't overwrite! */
4813 }
4814
4815 return num_matches;
4816}
4817
4818/*
4819 * Call this while finding completions, to check whether the user has hit a key
4820 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004821 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004823 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824 */
4825 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004826ins_compl_check_keys(frequency)
4827 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828{
4829 static int count = 0;
4830
4831 int c;
4832
4833 /* Don't check when reading keys from a script. That would break the test
4834 * scripts */
4835 if (using_script())
4836 return;
4837
4838 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004839 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 return;
4841 count = 0;
4842
Bram Moolenaara260a972006-06-23 19:36:29 +00004843 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4844 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 if (c != NUL)
4847 {
4848 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4849 {
4850 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004851 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004852 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4853 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004855 else
4856 {
4857 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004858 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004859 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004860 if (c != K_IGNORE)
4861 {
4862 /* Don't interrupt completion when the character wasn't typed,
4863 * e.g., when doing @q to replay keys. */
4864 if (c != Ctrl_R && KeyTyped)
4865 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004866
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004867 vungetc(c);
4868 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004870 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004871 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004872 {
4873 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4874
4875 compl_pending = 0;
4876 (void)ins_compl_next(FALSE, todo, TRUE);
4877 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004878}
4879
4880/*
4881 * Decide the direction of Insert mode complete from the key typed.
4882 * Returns BACKWARD or FORWARD.
4883 */
4884 static int
4885ins_compl_key2dir(c)
4886 int c;
4887{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004888 if (c == Ctrl_P || c == Ctrl_L
4889 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4890 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004891 return BACKWARD;
4892 return FORWARD;
4893}
4894
4895/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004896 * Return TRUE for keys that are used for completion only when the popup menu
4897 * is visible.
4898 */
4899 static int
4900ins_compl_pum_key(c)
4901 int c;
4902{
4903 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004904 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4905 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004906}
4907
4908/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004909 * Decide the number of completions to move forward.
4910 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4911 */
4912 static int
4913ins_compl_key2count(c)
4914 int c;
4915{
4916 int h;
4917
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004918 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004919 {
4920 h = pum_get_height();
4921 if (h > 3)
4922 h -= 2; /* keep some context */
4923 return h;
4924 }
4925 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926}
4927
4928/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004929 * Return TRUE if completion with "c" should insert the match, FALSE if only
4930 * to change the currently selected completion.
4931 */
4932 static int
4933ins_compl_use_match(c)
4934 int c;
4935{
4936 switch (c)
4937 {
4938 case K_UP:
4939 case K_DOWN:
4940 case K_PAGEDOWN:
4941 case K_KPAGEDOWN:
4942 case K_S_DOWN:
4943 case K_PAGEUP:
4944 case K_KPAGEUP:
4945 case K_S_UP:
4946 return FALSE;
4947 }
4948 return TRUE;
4949}
4950
4951/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 * Do Insert mode completion.
4953 * Called when character "c" was typed, which has a meaning for completion.
4954 * Returns OK if completion was done, FAIL if something failed (out of mem).
4955 */
4956 static int
4957ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004958 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004960 char_u *line;
4961 int startcol = 0; /* column where searched text starts */
4962 colnr_T curs_col; /* cursor column */
4963 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004964 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965
Bram Moolenaare3226be2005-12-18 22:10:00 +00004966 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004967 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
4969 /* First time we hit ^N or ^P (in a row, I mean) */
4970
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 did_ai = FALSE;
4972#ifdef FEAT_SMARTINDENT
4973 did_si = FALSE;
4974 can_si = FALSE;
4975 can_si_back = FALSE;
4976#endif
4977 if (stop_arrow() == FAIL)
4978 return FAIL;
4979
4980 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004981 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004982 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004984 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004985 * "compl_startpos" to the cursor as a pattern to add a new word
4986 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004987 * "compl_startpos" is not in the same line as the cursor then fix it
4988 * (the line has been split because it was longer than 'tw'). if SOL
4989 * is set then skip the previous pattern, a word at the beginning of
4990 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004991 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4992 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 {
4994 /*
4995 * it is a continued search
4996 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004997 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4999 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5000 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005001 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005003 /* line (probably) wrapped, set compl_startpos to the
5004 * first non_blank in the line, if it is not a wordchar
5005 * include it to get a better pattern, but then we don't
5006 * want the "\\<" prefix, check it bellow */
5007 compl_col = (colnr_T)(skipwhite(line) - line);
5008 compl_startpos.col = compl_col;
5009 compl_startpos.lnum = curwin->w_cursor.lnum;
5010 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 }
5012 else
5013 {
5014 /* S_IPOS was set when we inserted a word that was at the
5015 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005016 * mode but first we need to redefine compl_startpos */
5017 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005019 compl_cont_status |= CONT_SOL;
5020 compl_startpos.col = (colnr_T)(skipwhite(
5021 line + compl_length
5022 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005024 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005025 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005026 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005027 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005028 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005030 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005032 compl_cont_status &= ~CONT_SOL;
5033 compl_length = (IOSIZE - MIN_SPACE);
5034 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005036 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5037 if (compl_length < 1)
5038 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
5040 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005041 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005043 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005046 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005048 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005050 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005052 compl_cont_status = 0;
5053 compl_cont_status |= CONT_N_ADDS;
5054 compl_startpos = curwin->w_cursor;
5055 startcol = (int)curs_col;
5056 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 }
5058
5059 /* Work out completion pattern and original text -- webb */
5060 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5061 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005062 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5064 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005065 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005067 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005069 compl_col += ++startcol;
5070 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 }
5072 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005073 compl_pattern = str_foldcase(line + compl_col,
5074 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005076 compl_pattern = vim_strnsave(line + compl_col,
5077 compl_length);
5078 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 return FAIL;
5080 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005081 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 {
5083 char_u *prefix = (char_u *)"\\<";
5084
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005085 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005086 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005087 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005088 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005090 if (!vim_iswordp(line + compl_col)
5091 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 && (
5093#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005096 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097#endif
5098 )))
5099 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005100 STRCPY((char *)compl_pattern, prefix);
5101 (void)quote_meta(compl_pattern + STRLEN(prefix),
5102 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005103 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005104 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005106 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005108 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109#endif
5110 )
5111 {
5112 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005113 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5114 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 compl_col += curs_col;
5117 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 else
5120 {
5121#ifdef FEAT_MBYTE
5122 /* Search the point of change class of multibyte character
5123 * or not a word single byte character backward. */
5124 if (has_mbyte)
5125 {
5126 int base_class;
5127 int head_off;
5128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 startcol -= (*mb_head_off)(line, line + startcol);
5130 base_class = mb_get_class(line + startcol);
5131 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 head_off = (*mb_head_off)(line, line + startcol);
5134 if (base_class != mb_get_class(line + startcol
5135 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005137 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 }
5139 }
5140 else
5141#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005142 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005144 compl_col += ++startcol;
5145 compl_length = (int)curs_col - startcol;
5146 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 {
5148 /* Only match word with at least two chars -- webb
5149 * there's no need to call quote_meta,
5150 * alloc(7) is enough -- Acevedo
5151 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 compl_pattern = alloc(7);
5153 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005155 STRCPY((char *)compl_pattern, "\\<");
5156 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5157 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
5159 else
5160 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005161 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005162 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005163 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005165 STRCPY((char *)compl_pattern, "\\<");
5166 (void)quote_meta(compl_pattern + 2, line + compl_col,
5167 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 }
5169 }
5170 }
5171 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5172 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005173 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005174 compl_length = (int)curs_col - (int)compl_col;
5175 if (compl_length < 0) /* cursor in indent: empty pattern */
5176 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005178 compl_pattern = str_foldcase(line + compl_col, compl_length,
5179 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005181 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5182 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 return FAIL;
5184 }
5185 else if (ctrl_x_mode == CTRL_X_FILES)
5186 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005189 compl_col += ++startcol;
5190 compl_length = (int)curs_col - startcol;
5191 compl_pattern = addstar(line + compl_col, compl_length,
5192 EXPAND_FILES);
5193 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 return FAIL;
5195 }
5196 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5197 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005198 compl_pattern = vim_strnsave(line, curs_col);
5199 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005201 set_cmd_context(&compl_xp, compl_pattern,
5202 (int)STRLEN(compl_pattern), curs_col);
5203 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5204 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005205 /* No completion possible, use an empty pattern to get a
5206 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005207 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005208 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005209 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5210 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005212 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005213 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005214#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005215 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005216 * Call user defined function 'completefunc' with "a:findstart"
5217 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005218 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005219 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005220 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005221 char_u *funcname;
5222 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005223 win_T *curwin_save;
5224 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005225
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005226 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005227 * string */
5228 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5229 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5230 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005231 {
5232 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5233 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005234 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005235 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005236
5237 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005238 args[1] = NULL;
5239 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005240 curwin_save = curwin;
5241 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005242 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005243 if (curwin_save != curwin || curbuf_save != curbuf)
5244 {
5245 EMSG(_(e_complwin));
5246 return FAIL;
5247 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005248 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005249 check_cursor();
5250 if (!equalpos(curwin->w_cursor, pos))
5251 {
5252 EMSG(_(e_compldel));
5253 return FAIL;
5254 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005255
Bram Moolenaar6110a002012-01-26 18:58:38 +01005256 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005257 * cancel the complete without an error.
5258 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005259 if (col == -2)
5260 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005261 if (col == -3)
5262 {
5263 ctrl_x_mode = 0;
5264 edit_submode = NULL;
5265 msg_clr_cmdline();
5266 return FAIL;
5267 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005268
Bram Moolenaar82139082011-09-14 16:52:09 +02005269 /*
5270 * Reset extended parameters of completion, when start new
5271 * completion.
5272 */
5273 compl_opt_refresh_always = FALSE;
5274
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005275 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005276 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005277 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005278 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005279 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005280
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 /* Setup variables for completion. Need to obtain "line" again,
5282 * it may have become invalid. */
5283 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005284 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005285 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5286 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005287#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 return FAIL;
5289 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005290 else if (ctrl_x_mode == CTRL_X_SPELL)
5291 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005292#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005293 if (spell_bad_len > 0)
5294 compl_col = curs_col - spell_bad_len;
5295 else
5296 compl_col = spell_word_start(startcol);
5297 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005298 {
5299 compl_length = 0;
5300 compl_col = curs_col;
5301 }
5302 else
5303 {
5304 spell_expand_check_cap(compl_col);
5305 compl_length = (int)curs_col - compl_col;
5306 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005307 /* Need to obtain "line" again, it may have become invalid. */
5308 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005309 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5310 if (compl_pattern == NULL)
5311#endif
5312 return FAIL;
5313 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005314 else
5315 {
5316 EMSG2(_(e_intern2), "ins_complete()");
5317 return FAIL;
5318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005320 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321 {
5322 edit_submode_pre = (char_u *)_(" Adding");
5323 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5324 {
5325 /* Insert a new line, keep indentation but ignore 'comments' */
5326#ifdef FEAT_COMMENTS
5327 char_u *old = curbuf->b_p_com;
5328
5329 curbuf->b_p_com = (char_u *)"";
5330#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005331 compl_startpos.lnum = curwin->w_cursor.lnum;
5332 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 ins_eol('\r');
5334#ifdef FEAT_COMMENTS
5335 curbuf->b_p_com = old;
5336#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005337 compl_length = 0;
5338 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339 }
5340 }
5341 else
5342 {
5343 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005344 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005345 }
5346
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005347 if (compl_cont_status & CONT_LOCAL)
5348 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 else
5350 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5351
Bram Moolenaar62951b12011-09-21 18:23:05 +02005352 /* If any of the original typed text has been changed we need to fix
5353 * the redo buffer. */
5354 ins_compl_fixRedoBufForLeader(NULL);
5355
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005356 /* Always add completion for the original text. */
5357 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005358 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5359 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005360 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 vim_free(compl_pattern);
5363 compl_pattern = NULL;
5364 vim_free(compl_orig_text);
5365 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 return FAIL;
5367 }
5368
5369 /* showmode might reset the internal line pointers, so it must
5370 * be called before line = ml_get(), or when this address is no
5371 * longer needed. -- Acevedo.
5372 */
5373 edit_submode_extra = (char_u *)_("-- Searching...");
5374 edit_submode_highl = HLF_COUNT;
5375 showmode();
5376 edit_submode_extra = NULL;
5377 out_flush();
5378 }
5379
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 compl_shown_match = compl_curr_match;
5381 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005382
5383 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005384 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005386 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005387 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005389 /* may undisplay the popup menu */
5390 ins_compl_upd_pum();
5391
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005392 if (n > 1) /* all matches have been found */
5393 compl_matches = n;
5394 compl_curr_match = compl_shown_match;
5395 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
Bram Moolenaard68071d2006-05-02 22:08:30 +00005397 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5398 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 if (got_int && !global_busy)
5400 {
5401 (void)vgetc();
5402 got_int = FALSE;
5403 }
5404
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005405 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005406 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005408 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5409 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5411 edit_submode_highl = HLF_E;
5412 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5413 * because we couldn't expand anything at first place, but if we used
5414 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5415 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005416 if ( compl_length > 1
5417 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 || (ctrl_x_mode != 0
5419 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5420 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005421 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422 }
5423
Bram Moolenaar572cb562005-08-05 21:35:02 +00005424 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005425 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005426 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005427 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428
5429 if (edit_submode_extra == NULL)
5430 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005431 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {
5433 edit_submode_extra = (char_u *)_("Back at original");
5434 edit_submode_highl = HLF_W;
5435 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005436 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005437 {
5438 edit_submode_extra = (char_u *)_("Word from other line");
5439 edit_submode_highl = HLF_COUNT;
5440 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005441 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 {
5443 edit_submode_extra = (char_u *)_("The only match");
5444 edit_submode_highl = HLF_COUNT;
5445 }
5446 else
5447 {
5448 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005449 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005450 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005451 int number = 0;
5452 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005454 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005455 {
5456 /* search backwards for the first valid (!= -1) number.
5457 * This should normally succeed already at the first loop
5458 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005459 for (match = compl_curr_match->cp_prev; match != NULL
5460 && match != compl_first_match;
5461 match = match->cp_prev)
5462 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005463 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005464 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005465 break;
5466 }
5467 if (match != NULL)
5468 /* go up and assign all numbers which are not assigned
5469 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005470 for (match = match->cp_next;
5471 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005472 match = match->cp_next)
5473 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005474 }
5475 else /* BACKWARD */
5476 {
5477 /* search forwards (upwards) for the first valid (!= -1)
5478 * number. This should normally succeed already at the
5479 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005480 for (match = compl_curr_match->cp_next; match != NULL
5481 && match != compl_first_match;
5482 match = match->cp_next)
5483 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005484 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005485 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005486 break;
5487 }
5488 if (match != NULL)
5489 /* go down and assign all numbers which are not
5490 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005491 for (match = match->cp_prev; match
5492 && match->cp_number == -1;
5493 match = match->cp_prev)
5494 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005495 }
5496 }
5497
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005498 /* The match should always have a sequence number now, this is
5499 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005500 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005502 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5503 * Translations may need more than twice that. */
5504 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005506 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005507 vim_snprintf((char *)match_ref, sizeof(match_ref),
5508 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005509 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005511 vim_snprintf((char *)match_ref, sizeof(match_ref),
5512 _("match %d"),
5513 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 edit_submode_extra = match_ref;
5515 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005516 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005517 curs_columns(FALSE);
5518 }
5519 }
5520 }
5521
5522 /* Show a message about what (completion) mode we're in. */
5523 showmode();
5524 if (edit_submode_extra != NULL)
5525 {
5526 if (!p_smd)
5527 msg_attr(edit_submode_extra,
5528 edit_submode_highl < HLF_COUNT
5529 ? hl_attr(edit_submode_highl) : 0);
5530 }
5531 else
5532 msg_clr_cmdline(); /* necessary for "noshowmode" */
5533
Bram Moolenaard68071d2006-05-02 22:08:30 +00005534 /* Show the popup menu, unless we got interrupted. */
5535 if (!compl_interrupted)
5536 {
5537 /* RedrawingDisabled may be set when invoked through complete(). */
5538 n = RedrawingDisabled;
5539 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005540
5541 /* If the cursor moved we need to remove the pum first. */
5542 setcursor();
5543 if (save_w_wrow != curwin->w_wrow)
5544 ins_compl_del_pum();
5545
Bram Moolenaard68071d2006-05-02 22:08:30 +00005546 ins_compl_show_pum();
5547 setcursor();
5548 RedrawingDisabled = n;
5549 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005550 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005551 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005552
Bram Moolenaar071d4272004-06-13 20:20:40 +00005553 return OK;
5554}
5555
5556/*
5557 * Looks in the first "len" chars. of "src" for search-metachars.
5558 * If dest is not NULL the chars. are copied there quoting (with
5559 * a backslash) the metachars, and dest would be NUL terminated.
5560 * Returns the length (needed) of dest
5561 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005562 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005563quote_meta(dest, src, len)
5564 char_u *dest;
5565 char_u *src;
5566 int len;
5567{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005568 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005570 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005571 {
5572 switch (*src)
5573 {
5574 case '.':
5575 case '*':
5576 case '[':
5577 if (ctrl_x_mode == CTRL_X_DICTIONARY
5578 || ctrl_x_mode == CTRL_X_THESAURUS)
5579 break;
5580 case '~':
5581 if (!p_magic) /* quote these only if magic is set */
5582 break;
5583 case '\\':
5584 if (ctrl_x_mode == CTRL_X_DICTIONARY
5585 || ctrl_x_mode == CTRL_X_THESAURUS)
5586 break;
5587 case '^': /* currently it's not needed. */
5588 case '$':
5589 m++;
5590 if (dest != NULL)
5591 *dest++ = '\\';
5592 break;
5593 }
5594 if (dest != NULL)
5595 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005596# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597 /* Copy remaining bytes of a multibyte character. */
5598 if (has_mbyte)
5599 {
5600 int i, mb_len;
5601
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005602 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005603 if (mb_len > 0 && len >= mb_len)
5604 for (i = 0; i < mb_len; ++i)
5605 {
5606 --len;
5607 ++src;
5608 if (dest != NULL)
5609 *dest++ = *src;
5610 }
5611 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005612# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005613 }
5614 if (dest != NULL)
5615 *dest = NUL;
5616
5617 return m;
5618}
5619#endif /* FEAT_INS_EXPAND */
5620
5621/*
5622 * Next character is interpreted literally.
5623 * A one, two or three digit decimal number is interpreted as its byte value.
5624 * If one or two digits are entered, the next character is given to vungetc().
5625 * For Unicode a character > 255 may be returned.
5626 */
5627 int
5628get_literal()
5629{
5630 int cc;
5631 int nc;
5632 int i;
5633 int hex = FALSE;
5634 int octal = FALSE;
5635#ifdef FEAT_MBYTE
5636 int unicode = 0;
5637#endif
5638
5639 if (got_int)
5640 return Ctrl_C;
5641
5642#ifdef FEAT_GUI
5643 /*
5644 * In GUI there is no point inserting the internal code for a special key.
5645 * It is more useful to insert the string "<KEY>" instead. This would
5646 * probably be useful in a text window too, but it would not be
5647 * vi-compatible (maybe there should be an option for it?) -- webb
5648 */
5649 if (gui.in_use)
5650 ++allow_keys;
5651#endif
5652#ifdef USE_ON_FLY_SCROLL
5653 dont_scroll = TRUE; /* disallow scrolling here */
5654#endif
5655 ++no_mapping; /* don't map the next key hits */
5656 cc = 0;
5657 i = 0;
5658 for (;;)
5659 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005660 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661#ifdef FEAT_CMDL_INFO
5662 if (!(State & CMDLINE)
5663# ifdef FEAT_MBYTE
5664 && MB_BYTE2LEN_CHECK(nc) == 1
5665# endif
5666 )
5667 add_to_showcmd(nc);
5668#endif
5669 if (nc == 'x' || nc == 'X')
5670 hex = TRUE;
5671 else if (nc == 'o' || nc == 'O')
5672 octal = TRUE;
5673#ifdef FEAT_MBYTE
5674 else if (nc == 'u' || nc == 'U')
5675 unicode = nc;
5676#endif
5677 else
5678 {
5679 if (hex
5680#ifdef FEAT_MBYTE
5681 || unicode != 0
5682#endif
5683 )
5684 {
5685 if (!vim_isxdigit(nc))
5686 break;
5687 cc = cc * 16 + hex2nr(nc);
5688 }
5689 else if (octal)
5690 {
5691 if (nc < '0' || nc > '7')
5692 break;
5693 cc = cc * 8 + nc - '0';
5694 }
5695 else
5696 {
5697 if (!VIM_ISDIGIT(nc))
5698 break;
5699 cc = cc * 10 + nc - '0';
5700 }
5701
5702 ++i;
5703 }
5704
5705 if (cc > 255
5706#ifdef FEAT_MBYTE
5707 && unicode == 0
5708#endif
5709 )
5710 cc = 255; /* limit range to 0-255 */
5711 nc = 0;
5712
5713 if (hex) /* hex: up to two chars */
5714 {
5715 if (i >= 2)
5716 break;
5717 }
5718#ifdef FEAT_MBYTE
5719 else if (unicode) /* Unicode: up to four or eight chars */
5720 {
5721 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5722 break;
5723 }
5724#endif
5725 else if (i >= 3) /* decimal or octal: up to three chars */
5726 break;
5727 }
5728 if (i == 0) /* no number entered */
5729 {
5730 if (nc == K_ZERO) /* NUL is stored as NL */
5731 {
5732 cc = '\n';
5733 nc = 0;
5734 }
5735 else
5736 {
5737 cc = nc;
5738 nc = 0;
5739 }
5740 }
5741
5742 if (cc == 0) /* NUL is stored as NL */
5743 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005744#ifdef FEAT_MBYTE
5745 if (enc_dbcs && (cc & 0xff) == 0)
5746 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5747 second byte will cause trouble! */
5748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005749
5750 --no_mapping;
5751#ifdef FEAT_GUI
5752 if (gui.in_use)
5753 --allow_keys;
5754#endif
5755 if (nc)
5756 vungetc(nc);
5757 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5758 return cc;
5759}
5760
5761/*
5762 * Insert character, taking care of special keys and mod_mask
5763 */
5764 static void
5765insert_special(c, allow_modmask, ctrlv)
5766 int c;
5767 int allow_modmask;
5768 int ctrlv; /* c was typed after CTRL-V */
5769{
5770 char_u *p;
5771 int len;
5772
5773 /*
5774 * Special function key, translate into "<Key>". Up to the last '>' is
5775 * inserted with ins_str(), so as not to replace characters in replace
5776 * mode.
5777 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5778 * unless 'allow_modmask' is TRUE.
5779 */
5780#ifdef MACOS
5781 /* Command-key never produces a normal key */
5782 if (mod_mask & MOD_MASK_CMD)
5783 allow_modmask = TRUE;
5784#endif
5785 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5786 {
5787 p = get_special_key_name(c, mod_mask);
5788 len = (int)STRLEN(p);
5789 c = p[len - 1];
5790 if (len > 2)
5791 {
5792 if (stop_arrow() == FAIL)
5793 return;
5794 p[len - 1] = NUL;
5795 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005796 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005797 ctrlv = FALSE;
5798 }
5799 }
5800 if (stop_arrow() == OK)
5801 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5802}
5803
5804/*
5805 * Special characters in this context are those that need processing other
5806 * than the simple insertion that can be performed here. This includes ESC
5807 * which terminates the insert, and CR/NL which need special processing to
5808 * open up a new line. This routine tries to optimize insertions performed by
5809 * the "redo", "undo" or "put" commands, so it needs to know when it should
5810 * stop and defer processing to the "normal" mechanism.
5811 * '0' and '^' are special, because they can be followed by CTRL-D.
5812 */
5813#ifdef EBCDIC
5814# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5815#else
5816# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5817#endif
5818
5819#ifdef FEAT_MBYTE
5820# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5821#else
5822# define WHITECHAR(cc) vim_iswhite(cc)
5823#endif
5824
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005825/*
5826 * "flags": INSCHAR_FORMAT - force formatting
5827 * INSCHAR_CTRLV - char typed just after CTRL-V
5828 * INSCHAR_NO_FEX - don't use 'formatexpr'
5829 *
5830 * NOTE: passes the flags value straight through to internal_format() which,
5831 * beside INSCHAR_FORMAT (above), is also looking for these:
5832 * INSCHAR_DO_COM - format comments
5833 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5834 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 void
5836insertchar(c, flags, second_indent)
5837 int c; /* character to insert or NUL */
5838 int flags; /* INSCHAR_FORMAT, etc. */
5839 int second_indent; /* indent for second line if >= 0 */
5840{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 int textwidth;
5842#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846
5847 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5848 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005849
5850 /*
5851 * Try to break the line in two or more pieces when:
5852 * - Always do this if we have been called to do formatting only.
5853 * - Always do this when 'formatoptions' has the 'a' flag and the line
5854 * ends in white space.
5855 * - Otherwise:
5856 * - Don't do this if inserting a blank
5857 * - Don't do this if an existing character is being replaced, unless
5858 * we're in VREPLACE mode.
5859 * - Do this if the cursor is not on the line where insert started
5860 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5861 * before the insert.
5862 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5863 * before 'textwidth'
5864 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005865 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 && ((flags & INSCHAR_FORMAT)
5867 || (!vim_iswhite(c)
5868 && !((State & REPLACE_FLAG)
5869#ifdef FEAT_VREPLACE
5870 && !(State & VREPLACE_FLAG)
5871#endif
5872 && *ml_get_cursor() != NUL)
5873 && (curwin->w_cursor.lnum != Insstart.lnum
5874 || ((!has_format_option(FO_INS_LONG)
5875 || Insstart_textlen <= (colnr_T)textwidth)
5876 && (!fo_ins_blank
5877 || Insstart_blank_vcol <= (colnr_T)textwidth
5878 ))))))
5879 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005880 /* Format with 'formatexpr' when it's set. Use internal formatting
5881 * when 'formatexpr' isn't set or it returns non-zero. */
5882#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005883 int do_internal = TRUE;
5884
Bram Moolenaar81a82092008-03-12 16:27:00 +00005885 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005886 {
5887 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5888 /* It may be required to save for undo again, e.g. when setline()
5889 * was called. */
5890 ins_need_undo = TRUE;
5891 }
5892 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005893#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005894 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005895 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005896
Bram Moolenaar071d4272004-06-13 20:20:40 +00005897 if (c == NUL) /* only formatting was wanted */
5898 return;
5899
5900#ifdef FEAT_COMMENTS
5901 /* Check whether this character should end a comment. */
5902 if (did_ai && (int)c == end_comment_pending)
5903 {
5904 char_u *line;
5905 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5906 int middle_len, end_len;
5907 int i;
5908
5909 /*
5910 * Need to remove existing (middle) comment leader and insert end
5911 * comment leader. First, check what comment leader we can find.
5912 */
Bram Moolenaar81340392012-06-06 16:12:59 +02005913 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005914 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5915 {
5916 /* Skip middle-comment string */
5917 while (*p && p[-1] != ':') /* find end of middle flags */
5918 ++p;
5919 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5920 /* Don't count trailing white space for middle_len */
5921 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5922 --middle_len;
5923
5924 /* Find the end-comment string */
5925 while (*p && p[-1] != ':') /* find end of end flags */
5926 ++p;
5927 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5928
5929 /* Skip white space before the cursor */
5930 i = curwin->w_cursor.col;
5931 while (--i >= 0 && vim_iswhite(line[i]))
5932 ;
5933 i++;
5934
5935 /* Skip to before the middle leader */
5936 i -= middle_len;
5937
5938 /* Check some expected things before we go on */
5939 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5940 {
5941 /* Backspace over all the stuff we want to replace */
5942 backspace_until_column(i);
5943
5944 /*
5945 * Insert the end-comment string, except for the last
5946 * character, which will get inserted as normal later.
5947 */
5948 ins_bytes_len(lead_end, end_len - 1);
5949 }
5950 }
5951 }
5952 end_comment_pending = NUL;
5953#endif
5954
5955 did_ai = FALSE;
5956#ifdef FEAT_SMARTINDENT
5957 did_si = FALSE;
5958 can_si = FALSE;
5959 can_si_back = FALSE;
5960#endif
5961
5962 /*
5963 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5964 * This speeds up normal text input considerably.
5965 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5966 * need to re-indent at a ':', or any other character (but not what
5967 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005968 * Don't do this when there an InsertCharPre autocommand is defined,
5969 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005970 */
5971#ifdef USE_ON_FLY_SCROLL
5972 dont_scroll = FALSE; /* allow scrolling here */
5973#endif
5974
5975 if ( !ISSPECIAL(c)
5976#ifdef FEAT_MBYTE
5977 && (!has_mbyte || (*mb_char2len)(c) == 1)
5978#endif
5979 && vpeekc() != NUL
5980 && !(State & REPLACE_FLAG)
5981#ifdef FEAT_CINDENT
5982 && !cindent_on()
5983#endif
5984#ifdef FEAT_RIGHTLEFT
5985 && !p_ri
5986#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005987#ifdef FEAT_AUTOCMD
5988 && !has_insertcharpre()
5989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005990 )
5991 {
5992#define INPUT_BUFLEN 100
5993 char_u buf[INPUT_BUFLEN + 1];
5994 int i;
5995 colnr_T virtcol = 0;
5996
5997 buf[0] = c;
5998 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006000 virtcol = get_nolist_virtcol();
6001 /*
6002 * Stop the string when:
6003 * - no more chars available
6004 * - finding a special character (command key)
6005 * - buffer is full
6006 * - running into the 'textwidth' boundary
6007 * - need to check for abbreviation: A non-word char after a word-char
6008 */
6009 while ( (c = vpeekc()) != NUL
6010 && !ISSPECIAL(c)
6011#ifdef FEAT_MBYTE
6012 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6013#endif
6014 && i < INPUT_BUFLEN
6015 && (textwidth == 0
6016 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6017 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6018 {
6019#ifdef FEAT_RIGHTLEFT
6020 c = vgetc();
6021 if (p_hkmap && KeyTyped)
6022 c = hkmap(c); /* Hebrew mode mapping */
6023# ifdef FEAT_FKMAP
6024 if (p_fkmap && KeyTyped)
6025 c = fkmap(c); /* Farsi mode mapping */
6026# endif
6027 buf[i++] = c;
6028#else
6029 buf[i++] = vgetc();
6030#endif
6031 }
6032
6033#ifdef FEAT_DIGRAPHS
6034 do_digraph(-1); /* clear digraphs */
6035 do_digraph(buf[i-1]); /* may be the start of a digraph */
6036#endif
6037 buf[i] = NUL;
6038 ins_str(buf);
6039 if (flags & INSCHAR_CTRLV)
6040 {
6041 redo_literal(*buf);
6042 i = 1;
6043 }
6044 else
6045 i = 0;
6046 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006047 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006048 }
6049 else
6050 {
6051#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006052 int cc;
6053
Bram Moolenaar071d4272004-06-13 20:20:40 +00006054 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6055 {
6056 char_u buf[MB_MAXBYTES + 1];
6057
6058 (*mb_char2bytes)(c, buf);
6059 buf[cc] = NUL;
6060 ins_char_bytes(buf, cc);
6061 AppendCharToRedobuff(c);
6062 }
6063 else
6064#endif
6065 {
6066 ins_char(c);
6067 if (flags & INSCHAR_CTRLV)
6068 redo_literal(c);
6069 else
6070 AppendCharToRedobuff(c);
6071 }
6072 }
6073}
6074
6075/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006076 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006077 *
6078 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6079 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006080 */
6081 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006082internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006083 int textwidth;
6084 int second_indent;
6085 int flags;
6086 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006087 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006088{
6089 int cc;
6090 int save_char = NUL;
6091 int haveto_redraw = FALSE;
6092 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6093#ifdef FEAT_MBYTE
6094 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6095#endif
6096 int fo_white_par = has_format_option(FO_WHITE_PAR);
6097 int first_line = TRUE;
6098#ifdef FEAT_COMMENTS
6099 colnr_T leader_len;
6100 int no_leader = FALSE;
6101 int do_comments = (flags & INSCHAR_DO_COM);
6102#endif
6103
6104 /*
6105 * When 'ai' is off we don't want a space under the cursor to be
6106 * deleted. Replace it with an 'x' temporarily.
6107 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006108 if (!curbuf->b_p_ai
6109#ifdef FEAT_VREPLACE
6110 && !(State & VREPLACE_FLAG)
6111#endif
6112 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006113 {
6114 cc = gchar_cursor();
6115 if (vim_iswhite(cc))
6116 {
6117 save_char = cc;
6118 pchar_cursor('x');
6119 }
6120 }
6121
6122 /*
6123 * Repeat breaking lines, until the current line is not too long.
6124 */
6125 while (!got_int)
6126 {
6127 int startcol; /* Cursor column at entry */
6128 int wantcol; /* column at textwidth border */
6129 int foundcol; /* column for start of spaces */
6130 int end_foundcol = 0; /* column for start of word */
6131 colnr_T len;
6132 colnr_T virtcol;
6133#ifdef FEAT_VREPLACE
6134 int orig_col = 0;
6135 char_u *saved_text = NULL;
6136#endif
6137 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006138 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006139
Bram Moolenaar97b98102009-11-17 16:41:01 +00006140 virtcol = get_nolist_virtcol()
6141 + char2cells(c != NUL ? c : gchar_cursor());
6142 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006143 break;
6144
6145#ifdef FEAT_COMMENTS
6146 if (no_leader)
6147 do_comments = FALSE;
6148 else if (!(flags & INSCHAR_FORMAT)
6149 && has_format_option(FO_WRAP_COMS))
6150 do_comments = TRUE;
6151
6152 /* Don't break until after the comment leader */
6153 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006154 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006155 else
6156 leader_len = 0;
6157
6158 /* If the line doesn't start with a comment leader, then don't
6159 * start one in a following broken line. Avoids that a %word
6160 * moved to the start of the next line causes all following lines
6161 * to start with %. */
6162 if (leader_len == 0)
6163 no_leader = TRUE;
6164#endif
6165 if (!(flags & INSCHAR_FORMAT)
6166#ifdef FEAT_COMMENTS
6167 && leader_len == 0
6168#endif
6169 && !has_format_option(FO_WRAP))
6170
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006171 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006172 if ((startcol = curwin->w_cursor.col) == 0)
6173 break;
6174
6175 /* find column of textwidth border */
6176 coladvance((colnr_T)textwidth);
6177 wantcol = curwin->w_cursor.col;
6178
Bram Moolenaar97b98102009-11-17 16:41:01 +00006179 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006180 foundcol = 0;
6181
6182 /*
6183 * Find position to break at.
6184 * Stop at first entered white when 'formatoptions' has 'v'
6185 */
6186 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006187 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006188 || curwin->w_cursor.lnum != Insstart.lnum
6189 || curwin->w_cursor.col >= Insstart.col)
6190 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006191 if (curwin->w_cursor.col == startcol && c != NUL)
6192 cc = c;
6193 else
6194 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006195 if (WHITECHAR(cc))
6196 {
6197 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006198 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006199
6200 /* find start of sequence of blanks */
6201 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6202 {
6203 dec_cursor();
6204 cc = gchar_cursor();
6205 }
6206 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6207 break; /* only spaces in front of text */
6208#ifdef FEAT_COMMENTS
6209 /* Don't break until after the comment leader */
6210 if (curwin->w_cursor.col < leader_len)
6211 break;
6212#endif
6213 if (has_format_option(FO_ONE_LETTER))
6214 {
6215 /* do not break after one-letter words */
6216 if (curwin->w_cursor.col == 0)
6217 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006218#ifdef FEAT_COMMENTS
6219 /* do not break "#a b" when 'tw' is 2 */
6220 if (curwin->w_cursor.col <= leader_len)
6221 break;
6222#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006223 col = curwin->w_cursor.col;
6224 dec_cursor();
6225 cc = gchar_cursor();
6226
6227 if (WHITECHAR(cc))
6228 continue; /* one-letter, continue */
6229 curwin->w_cursor.col = col;
6230 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006231
6232 inc_cursor();
6233
6234 end_foundcol = end_col + 1;
6235 foundcol = curwin->w_cursor.col;
6236 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006237 break;
6238 }
6239#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006240 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006241 {
6242 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006243 if (curwin->w_cursor.col != startcol)
6244 {
6245#ifdef FEAT_COMMENTS
6246 /* Don't break until after the comment leader */
6247 if (curwin->w_cursor.col < leader_len)
6248 break;
6249#endif
6250 col = curwin->w_cursor.col;
6251 inc_cursor();
6252 /* Don't change end_foundcol if already set. */
6253 if (foundcol != curwin->w_cursor.col)
6254 {
6255 foundcol = curwin->w_cursor.col;
6256 end_foundcol = foundcol;
6257 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6258 break;
6259 }
6260 curwin->w_cursor.col = col;
6261 }
6262
6263 if (curwin->w_cursor.col == 0)
6264 break;
6265
6266 col = curwin->w_cursor.col;
6267
6268 dec_cursor();
6269 cc = gchar_cursor();
6270
6271 if (WHITECHAR(cc))
6272 continue; /* break with space */
6273#ifdef FEAT_COMMENTS
6274 /* Don't break until after the comment leader */
6275 if (curwin->w_cursor.col < leader_len)
6276 break;
6277#endif
6278
6279 curwin->w_cursor.col = col;
6280
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006281 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006282 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006283 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6284 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006285 }
6286#endif
6287 if (curwin->w_cursor.col == 0)
6288 break;
6289 dec_cursor();
6290 }
6291
6292 if (foundcol == 0) /* no spaces, cannot break line */
6293 {
6294 curwin->w_cursor.col = startcol;
6295 break;
6296 }
6297
6298 /* Going to break the line, remove any "$" now. */
6299 undisplay_dollar();
6300
6301 /*
6302 * Offset between cursor position and line break is used by replace
6303 * stack functions. VREPLACE does not use this, and backspaces
6304 * over the text instead.
6305 */
6306#ifdef FEAT_VREPLACE
6307 if (State & VREPLACE_FLAG)
6308 orig_col = startcol; /* Will start backspacing from here */
6309 else
6310#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006311 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006312
6313 /*
6314 * adjust startcol for spaces that will be deleted and
6315 * characters that will remain on top line
6316 */
6317 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006318 while ((cc = gchar_cursor(), WHITECHAR(cc))
6319 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006320 inc_cursor();
6321 startcol -= curwin->w_cursor.col;
6322 if (startcol < 0)
6323 startcol = 0;
6324
6325#ifdef FEAT_VREPLACE
6326 if (State & VREPLACE_FLAG)
6327 {
6328 /*
6329 * In VREPLACE mode, we will backspace over the text to be
6330 * wrapped, so save a copy now to put on the next line.
6331 */
6332 saved_text = vim_strsave(ml_get_cursor());
6333 curwin->w_cursor.col = orig_col;
6334 if (saved_text == NULL)
6335 break; /* Can't do it, out of memory */
6336 saved_text[startcol] = NUL;
6337
6338 /* Backspace over characters that will move to the next line */
6339 if (!fo_white_par)
6340 backspace_until_column(foundcol);
6341 }
6342 else
6343#endif
6344 {
6345 /* put cursor after pos. to break line */
6346 if (!fo_white_par)
6347 curwin->w_cursor.col = foundcol;
6348 }
6349
6350 /*
6351 * Split the line just before the margin.
6352 * Only insert/delete lines, but don't really redraw the window.
6353 */
6354 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6355 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6356#ifdef FEAT_COMMENTS
6357 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006358 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006359#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006360 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6361 if (!(flags & INSCHAR_COM_LIST))
6362 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006363
6364 replace_offset = 0;
6365 if (first_line)
6366 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006367 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006368 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006369 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006370 * This section is for auto-wrap of numeric lists. When not
6371 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6372 * flag will be set and open_line() will handle it (as seen
6373 * above). The code here (and in get_number_indent()) will
6374 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006375 */
6376 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006377 second_indent =
6378 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006379 if (second_indent >= 0)
6380 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006381#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006382 if (State & VREPLACE_FLAG)
6383 change_indent(INDENT_SET, second_indent,
6384 FALSE, NUL, TRUE);
6385 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006386#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006387#ifdef FEAT_COMMENTS
6388 if (leader_len > 0 && second_indent - leader_len > 0)
6389 {
6390 int i;
6391 int padding = second_indent - leader_len;
6392
6393 /* We started at the first_line of a numbered list
6394 * that has a comment. the open_line() function has
6395 * inserted the proper comment leader and positioned
6396 * the cursor at the end of the split line. Now we
6397 * add the additional whitespace needed after the
6398 * comment leader for the numbered list. */
6399 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006400 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006401 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006402 }
6403 else
6404 {
6405#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006406 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006407#ifdef FEAT_COMMENTS
6408 }
6409#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006410 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006411 }
6412 first_line = FALSE;
6413 }
6414
6415#ifdef FEAT_VREPLACE
6416 if (State & VREPLACE_FLAG)
6417 {
6418 /*
6419 * In VREPLACE mode we have backspaced over the text to be
6420 * moved, now we re-insert it into the new line.
6421 */
6422 ins_bytes(saved_text);
6423 vim_free(saved_text);
6424 }
6425 else
6426#endif
6427 {
6428 /*
6429 * Check if cursor is not past the NUL off the line, cindent
6430 * may have added or removed indent.
6431 */
6432 curwin->w_cursor.col += startcol;
6433 len = (colnr_T)STRLEN(ml_get_curline());
6434 if (curwin->w_cursor.col > len)
6435 curwin->w_cursor.col = len;
6436 }
6437
6438 haveto_redraw = TRUE;
6439#ifdef FEAT_CINDENT
6440 can_cindent = TRUE;
6441#endif
6442 /* moved the cursor, don't autoindent or cindent now */
6443 did_ai = FALSE;
6444#ifdef FEAT_SMARTINDENT
6445 did_si = FALSE;
6446 can_si = FALSE;
6447 can_si_back = FALSE;
6448#endif
6449 line_breakcheck();
6450 }
6451
6452 if (save_char != NUL) /* put back space after cursor */
6453 pchar_cursor(save_char);
6454
6455 if (!format_only && haveto_redraw)
6456 {
6457 update_topline();
6458 redraw_curbuf_later(VALID);
6459 }
6460}
6461
6462/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463 * Called after inserting or deleting text: When 'formatoptions' includes the
6464 * 'a' flag format from the current line until the end of the paragraph.
6465 * Keep the cursor at the same position relative to the text.
6466 * The caller must have saved the cursor line for undo, following ones will be
6467 * saved here.
6468 */
6469 void
6470auto_format(trailblank, prev_line)
6471 int trailblank; /* when TRUE also format with trailing blank */
6472 int prev_line; /* may start in previous line */
6473{
6474 pos_T pos;
6475 colnr_T len;
6476 char_u *old;
6477 char_u *new, *pnew;
6478 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006479 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
6481 if (!has_format_option(FO_AUTO))
6482 return;
6483
6484 pos = curwin->w_cursor;
6485 old = ml_get_curline();
6486
6487 /* may remove added space */
6488 check_auto_format(FALSE);
6489
6490 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6491 * user might insert normal text next. Also skip formatting when "1" is
6492 * in 'formatoptions' and there is a single character before the cursor.
6493 * Otherwise the line would be broken and when typing another non-white
6494 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006495 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006496 if (*old != NUL && !trailblank && wasatend)
6497 {
6498 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006499 cc = gchar_cursor();
6500 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6501 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006503 cc = gchar_cursor();
6504 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505 {
6506 curwin->w_cursor = pos;
6507 return;
6508 }
6509 curwin->w_cursor = pos;
6510 }
6511
6512#ifdef FEAT_COMMENTS
6513 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6514 * comments. */
6515 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006516 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006517 return;
6518#endif
6519
6520 /*
6521 * May start formatting in a previous line, so that after "x" a word is
6522 * moved to the previous line if it fits there now. Only when this is not
6523 * the start of a paragraph.
6524 */
6525 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6526 {
6527 --curwin->w_cursor.lnum;
6528 if (u_save_cursor() == FAIL)
6529 return;
6530 }
6531
6532 /*
6533 * Do the formatting and restore the cursor position. "saved_cursor" will
6534 * be adjusted for the text formatting.
6535 */
6536 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006537 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006538 curwin->w_cursor = saved_cursor;
6539 saved_cursor.lnum = 0;
6540
6541 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6542 {
6543 /* "cannot happen" */
6544 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6545 coladvance((colnr_T)MAXCOL);
6546 }
6547 else
6548 check_cursor_col();
6549
6550 /* Insert mode: If the cursor is now after the end of the line while it
6551 * previously wasn't, the line was broken. Because of the rule above we
6552 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6553 * formatted. */
6554 if (!wasatend && has_format_option(FO_WHITE_PAR))
6555 {
6556 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006557 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006558 if (curwin->w_cursor.col == len)
6559 {
6560 pnew = vim_strnsave(new, len + 2);
6561 pnew[len] = ' ';
6562 pnew[len + 1] = NUL;
6563 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6564 /* remove the space later */
6565 did_add_space = TRUE;
6566 }
6567 else
6568 /* may remove added space */
6569 check_auto_format(FALSE);
6570 }
6571
6572 check_cursor();
6573}
6574
6575/*
6576 * When an extra space was added to continue a paragraph for auto-formatting,
6577 * delete it now. The space must be under the cursor, just after the insert
6578 * position.
6579 */
6580 static void
6581check_auto_format(end_insert)
6582 int end_insert; /* TRUE when ending Insert mode */
6583{
6584 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006585 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586
6587 if (did_add_space)
6588 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006589 cc = gchar_cursor();
6590 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591 /* Somehow the space was removed already. */
6592 did_add_space = FALSE;
6593 else
6594 {
6595 if (!end_insert)
6596 {
6597 inc_cursor();
6598 c = gchar_cursor();
6599 dec_cursor();
6600 }
6601 if (c != NUL)
6602 {
6603 /* The space is no longer at the end of the line, delete it. */
6604 del_char(FALSE);
6605 did_add_space = FALSE;
6606 }
6607 }
6608 }
6609}
6610
6611/*
6612 * Find out textwidth to be used for formatting:
6613 * if 'textwidth' option is set, use it
6614 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6615 * if invalid value, use 0.
6616 * Set default to window width (maximum 79) for "gq" operator.
6617 */
6618 int
6619comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006620 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621{
6622 int textwidth;
6623
6624 textwidth = curbuf->b_p_tw;
6625 if (textwidth == 0 && curbuf->b_p_wm)
6626 {
6627 /* The width is the window width minus 'wrapmargin' minus all the
6628 * things that add to the margin. */
6629 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6630#ifdef FEAT_CMDWIN
6631 if (cmdwin_type != 0)
6632 textwidth -= 1;
6633#endif
6634#ifdef FEAT_FOLDING
6635 textwidth -= curwin->w_p_fdc;
6636#endif
6637#ifdef FEAT_SIGNS
6638 if (curwin->w_buffer->b_signlist != NULL
6639# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006640 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641# endif
6642 )
6643 textwidth -= 1;
6644#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006645 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006646 textwidth -= 8;
6647 }
6648 if (textwidth < 0)
6649 textwidth = 0;
6650 if (ff && textwidth == 0)
6651 {
6652 textwidth = W_WIDTH(curwin) - 1;
6653 if (textwidth > 79)
6654 textwidth = 79;
6655 }
6656 return textwidth;
6657}
6658
6659/*
6660 * Put a character in the redo buffer, for when just after a CTRL-V.
6661 */
6662 static void
6663redo_literal(c)
6664 int c;
6665{
6666 char_u buf[10];
6667
6668 /* Only digits need special treatment. Translate them into a string of
6669 * three digits. */
6670 if (VIM_ISDIGIT(c))
6671 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006672 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006673 AppendToRedobuff(buf);
6674 }
6675 else
6676 AppendCharToRedobuff(c);
6677}
6678
6679/*
6680 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006681 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 */
6683 static void
6684start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006685 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006686{
6687 if (!arrow_used) /* something has been inserted */
6688 {
6689 AppendToRedobuff(ESC_STR);
6690 stop_insert(end_insert_pos, FALSE);
6691 arrow_used = TRUE; /* this means we stopped the current insert */
6692 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006693#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006694 check_spell_redraw();
6695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006696}
6697
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006698#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006699/*
6700 * If we skipped highlighting word at cursor, do it now.
6701 * It may be skipped again, thus reset spell_redraw_lnum first.
6702 */
6703 static void
6704check_spell_redraw()
6705{
6706 if (spell_redraw_lnum != 0)
6707 {
6708 linenr_T lnum = spell_redraw_lnum;
6709
6710 spell_redraw_lnum = 0;
6711 redrawWinline(lnum, FALSE);
6712 }
6713}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006714
6715/*
6716 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6717 * spelled word, if there is one.
6718 */
6719 static void
6720spell_back_to_badword()
6721{
6722 pos_T tpos = curwin->w_cursor;
6723
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006724 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006725 if (curwin->w_cursor.col != tpos.col)
6726 start_arrow(&tpos);
6727}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006728#endif
6729
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730/*
6731 * stop_arrow() is called before a change is made in insert mode.
6732 * If an arrow key has been used, start a new insertion.
6733 * Returns FAIL if undo is impossible, shouldn't insert then.
6734 */
6735 int
6736stop_arrow()
6737{
6738 if (arrow_used)
6739 {
6740 if (u_save_cursor() == OK)
6741 {
6742 arrow_used = FALSE;
6743 ins_need_undo = FALSE;
6744 }
6745 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006746 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 ai_col = 0;
6748#ifdef FEAT_VREPLACE
6749 if (State & VREPLACE_FLAG)
6750 {
6751 orig_line_count = curbuf->b_ml.ml_line_count;
6752 vr_lines_changed = 1;
6753 }
6754#endif
6755 ResetRedobuff();
6756 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006757 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 }
6759 else if (ins_need_undo)
6760 {
6761 if (u_save_cursor() == OK)
6762 ins_need_undo = FALSE;
6763 }
6764
6765#ifdef FEAT_FOLDING
6766 /* Always open fold at the cursor line when inserting something. */
6767 foldOpenCursor();
6768#endif
6769
6770 return (arrow_used || ins_need_undo ? FAIL : OK);
6771}
6772
6773/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006774 * Do a few things to stop inserting.
6775 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6776 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 */
6778 static void
6779stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006780 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006781 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006782{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006783 int cc;
6784 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006785
6786 stop_redo_ins();
6787 replace_flush(); /* abandon replace stack */
6788
6789 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006790 * Save the inserted text for later redo with ^@ and CTRL-A.
6791 * Don't do it when "restart_edit" was set and nothing was inserted,
6792 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006793 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006794 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006795 if (did_restart_edit == 0 || (ptr != NULL
6796 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006797 {
6798 vim_free(last_insert);
6799 last_insert = ptr;
6800 last_insert_skip = new_insert_skip;
6801 }
6802 else
6803 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006805 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006806 {
6807 /* Auto-format now. It may seem strange to do this when stopping an
6808 * insertion (or moving the cursor), but it's required when appending
6809 * a line and having it end in a space. But only do it when something
6810 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006811 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006812 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006813 pos_T tpos = curwin->w_cursor;
6814
Bram Moolenaar071d4272004-06-13 20:20:40 +00006815 /* When the cursor is at the end of the line after a space the
6816 * formatting will move it to the following word. Avoid that by
6817 * moving the cursor onto the space. */
6818 cc = 'x';
6819 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6820 {
6821 dec_cursor();
6822 cc = gchar_cursor();
6823 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006824 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006825 }
6826
6827 auto_format(TRUE, FALSE);
6828
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006829 if (vim_iswhite(cc))
6830 {
6831 if (gchar_cursor() != NUL)
6832 inc_cursor();
6833#ifdef FEAT_VIRTUALEDIT
6834 /* If the cursor is still at the same character, also keep
6835 * the "coladd". */
6836 if (gchar_cursor() == NUL
6837 && curwin->w_cursor.lnum == tpos.lnum
6838 && curwin->w_cursor.col == tpos.col)
6839 curwin->w_cursor.coladd = tpos.coladd;
6840#endif
6841 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006842 }
6843
6844 /* If a space was inserted for auto-formatting, remove it now. */
6845 check_auto_format(TRUE);
6846
6847 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006848 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006849 * Do this when ESC was used or moving the cursor up/down.
6850 * Check for the old position still being valid, just in case the text
6851 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006852 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006853 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6854 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006855 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006856 pos_T tpos = curwin->w_cursor;
6857
6858 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006859 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006860 for (;;)
6861 {
6862 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6863 --curwin->w_cursor.col;
6864 cc = gchar_cursor();
6865 if (!vim_iswhite(cc))
6866 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006867 if (del_char(TRUE) == FAIL)
6868 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006869 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006870 if (curwin->w_cursor.lnum != tpos.lnum)
6871 curwin->w_cursor = tpos;
6872 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6874
6875#ifdef FEAT_VISUAL
6876 /* <C-S-Right> may have started Visual mode, adjust the position for
6877 * deleted characters. */
6878 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6879 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006880 int len = (int)STRLEN(ml_get_curline());
6881
6882 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006884 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006885# ifdef FEAT_VIRTUALEDIT
6886 VIsual.coladd = 0;
6887# endif
6888 }
6889 }
6890#endif
6891 }
6892 }
6893 did_ai = FALSE;
6894#ifdef FEAT_SMARTINDENT
6895 did_si = FALSE;
6896 can_si = FALSE;
6897 can_si_back = FALSE;
6898#endif
6899
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006900 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6901 * now in a different buffer. */
6902 if (end_insert_pos != NULL)
6903 {
6904 curbuf->b_op_start = Insstart;
6905 curbuf->b_op_end = *end_insert_pos;
6906 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006907}
6908
6909/*
6910 * Set the last inserted text to a single character.
6911 * Used for the replace command.
6912 */
6913 void
6914set_last_insert(c)
6915 int c;
6916{
6917 char_u *s;
6918
6919 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006920 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006921 if (last_insert != NULL)
6922 {
6923 s = last_insert;
6924 /* Use the CTRL-V only when entering a special char */
6925 if (c < ' ' || c == DEL)
6926 *s++ = Ctrl_V;
6927 s = add_char2buf(c, s);
6928 *s++ = ESC;
6929 *s++ = NUL;
6930 last_insert_skip = 0;
6931 }
6932}
6933
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006934#if defined(EXITFREE) || defined(PROTO)
6935 void
6936free_last_insert()
6937{
6938 vim_free(last_insert);
6939 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006940# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006941 vim_free(compl_orig_text);
6942 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006943# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006944}
6945#endif
6946
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947/*
6948 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6949 * and CSI. Handle multi-byte characters.
6950 * Returns a pointer to after the added bytes.
6951 */
6952 char_u *
6953add_char2buf(c, s)
6954 int c;
6955 char_u *s;
6956{
6957#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006958 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959 int i;
6960 int len;
6961
6962 len = (*mb_char2bytes)(c, temp);
6963 for (i = 0; i < len; ++i)
6964 {
6965 c = temp[i];
6966#endif
6967 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6968 if (c == K_SPECIAL)
6969 {
6970 *s++ = K_SPECIAL;
6971 *s++ = KS_SPECIAL;
6972 *s++ = KE_FILLER;
6973 }
6974#ifdef FEAT_GUI
6975 else if (c == CSI)
6976 {
6977 *s++ = CSI;
6978 *s++ = KS_EXTRA;
6979 *s++ = (int)KE_CSI;
6980 }
6981#endif
6982 else
6983 *s++ = c;
6984#ifdef FEAT_MBYTE
6985 }
6986#endif
6987 return s;
6988}
6989
6990/*
6991 * move cursor to start of line
6992 * if flags & BL_WHITE move to first non-white
6993 * if flags & BL_SOL move to first non-white if startofline is set,
6994 * otherwise keep "curswant" column
6995 * if flags & BL_FIX don't leave the cursor on a NUL.
6996 */
6997 void
6998beginline(flags)
6999 int flags;
7000{
7001 if ((flags & BL_SOL) && !p_sol)
7002 coladvance(curwin->w_curswant);
7003 else
7004 {
7005 curwin->w_cursor.col = 0;
7006#ifdef FEAT_VIRTUALEDIT
7007 curwin->w_cursor.coladd = 0;
7008#endif
7009
7010 if (flags & (BL_WHITE | BL_SOL))
7011 {
7012 char_u *ptr;
7013
7014 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7015 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7016 ++curwin->w_cursor.col;
7017 }
7018 curwin->w_set_curswant = TRUE;
7019 }
7020}
7021
7022/*
7023 * oneright oneleft cursor_down cursor_up
7024 *
7025 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007026 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007027 * Return OK when successful, FAIL when we hit a line of file boundary.
7028 */
7029
7030 int
7031oneright()
7032{
7033 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007034 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007035
7036#ifdef FEAT_VIRTUALEDIT
7037 if (virtual_active())
7038 {
7039 pos_T prevpos = curwin->w_cursor;
7040
7041 /* Adjust for multi-wide char (excluding TAB) */
7042 ptr = ml_get_cursor();
7043 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007044# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007045 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007046# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007048# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007049 ))
7050 ? ptr2cells(ptr) : 1));
7051 curwin->w_set_curswant = TRUE;
7052 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7053 return (prevpos.col != curwin->w_cursor.col
7054 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7055 }
7056#endif
7057
7058 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007059 if (*ptr == NUL)
7060 return FAIL; /* already at the very end */
7061
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007063 if (has_mbyte)
7064 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007065 else
7066#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007067 l = 1;
7068
7069 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7070 * contains "onemore". */
7071 if (ptr[l] == NUL
7072#ifdef FEAT_VIRTUALEDIT
7073 && (ve_flags & VE_ONEMORE) == 0
7074#endif
7075 )
7076 return FAIL;
7077 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007078
7079 curwin->w_set_curswant = TRUE;
7080 return OK;
7081}
7082
7083 int
7084oneleft()
7085{
7086#ifdef FEAT_VIRTUALEDIT
7087 if (virtual_active())
7088 {
7089 int width;
7090 int v = getviscol();
7091
7092 if (v == 0)
7093 return FAIL;
7094
7095# ifdef FEAT_LINEBREAK
7096 /* We might get stuck on 'showbreak', skip over it. */
7097 width = 1;
7098 for (;;)
7099 {
7100 coladvance(v - width);
7101 /* getviscol() is slow, skip it when 'showbreak' is empty and
7102 * there are no multi-byte characters */
7103 if ((*p_sbr == NUL
7104# ifdef FEAT_MBYTE
7105 && !has_mbyte
7106# endif
7107 ) || getviscol() < v)
7108 break;
7109 ++width;
7110 }
7111# else
7112 coladvance(v - 1);
7113# endif
7114
7115 if (curwin->w_cursor.coladd == 1)
7116 {
7117 char_u *ptr;
7118
7119 /* Adjust for multi-wide char (not a TAB) */
7120 ptr = ml_get_cursor();
7121 if (*ptr != TAB && vim_isprintc(
7122# ifdef FEAT_MBYTE
7123 (*mb_ptr2char)(ptr)
7124# else
7125 *ptr
7126# endif
7127 ) && ptr2cells(ptr) > 1)
7128 curwin->w_cursor.coladd = 0;
7129 }
7130
7131 curwin->w_set_curswant = TRUE;
7132 return OK;
7133 }
7134#endif
7135
7136 if (curwin->w_cursor.col == 0)
7137 return FAIL;
7138
7139 curwin->w_set_curswant = TRUE;
7140 --curwin->w_cursor.col;
7141
7142#ifdef FEAT_MBYTE
7143 /* if the character on the left of the current cursor is a multi-byte
7144 * character, move to its first byte */
7145 if (has_mbyte)
7146 mb_adjust_cursor();
7147#endif
7148 return OK;
7149}
7150
7151 int
7152cursor_up(n, upd_topline)
7153 long n;
7154 int upd_topline; /* When TRUE: update topline */
7155{
7156 linenr_T lnum;
7157
7158 if (n > 0)
7159 {
7160 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007161 /* This fails if the cursor is already in the first line or the count
7162 * is larger than the line number and '-' is in 'cpoptions' */
7163 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007164 return FAIL;
7165 if (n >= lnum)
7166 lnum = 1;
7167 else
7168#ifdef FEAT_FOLDING
7169 if (hasAnyFolding(curwin))
7170 {
7171 /*
7172 * Count each sequence of folded lines as one logical line.
7173 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007174 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007175 (void)hasFolding(lnum, &lnum, NULL);
7176
7177 while (n--)
7178 {
7179 /* move up one line */
7180 --lnum;
7181 if (lnum <= 1)
7182 break;
7183 /* If we entered a fold, move to the beginning, unless in
7184 * Insert mode or when 'foldopen' contains "all": it will open
7185 * in a moment. */
7186 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7187 (void)hasFolding(lnum, &lnum, NULL);
7188 }
7189 if (lnum < 1)
7190 lnum = 1;
7191 }
7192 else
7193#endif
7194 lnum -= n;
7195 curwin->w_cursor.lnum = lnum;
7196 }
7197
7198 /* try to advance to the column we want to be at */
7199 coladvance(curwin->w_curswant);
7200
7201 if (upd_topline)
7202 update_topline(); /* make sure curwin->w_topline is valid */
7203
7204 return OK;
7205}
7206
7207/*
7208 * Cursor down a number of logical lines.
7209 */
7210 int
7211cursor_down(n, upd_topline)
7212 long n;
7213 int upd_topline; /* When TRUE: update topline */
7214{
7215 linenr_T lnum;
7216
7217 if (n > 0)
7218 {
7219 lnum = curwin->w_cursor.lnum;
7220#ifdef FEAT_FOLDING
7221 /* Move to last line of fold, will fail if it's the end-of-file. */
7222 (void)hasFolding(lnum, NULL, &lnum);
7223#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007224 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007225 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007226 if (lnum >= curbuf->b_ml.ml_line_count
7227 || (lnum + n > curbuf->b_ml.ml_line_count
7228 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007229 return FAIL;
7230 if (lnum + n >= curbuf->b_ml.ml_line_count)
7231 lnum = curbuf->b_ml.ml_line_count;
7232 else
7233#ifdef FEAT_FOLDING
7234 if (hasAnyFolding(curwin))
7235 {
7236 linenr_T last;
7237
7238 /* count each sequence of folded lines as one logical line */
7239 while (n--)
7240 {
7241 if (hasFolding(lnum, NULL, &last))
7242 lnum = last + 1;
7243 else
7244 ++lnum;
7245 if (lnum >= curbuf->b_ml.ml_line_count)
7246 break;
7247 }
7248 if (lnum > curbuf->b_ml.ml_line_count)
7249 lnum = curbuf->b_ml.ml_line_count;
7250 }
7251 else
7252#endif
7253 lnum += n;
7254 curwin->w_cursor.lnum = lnum;
7255 }
7256
7257 /* try to advance to the column we want to be at */
7258 coladvance(curwin->w_curswant);
7259
7260 if (upd_topline)
7261 update_topline(); /* make sure curwin->w_topline is valid */
7262
7263 return OK;
7264}
7265
7266/*
7267 * Stuff the last inserted text in the read buffer.
7268 * Last_insert actually is a copy of the redo buffer, so we
7269 * first have to remove the command.
7270 */
7271 int
7272stuff_inserted(c, count, no_esc)
7273 int c; /* Command character to be inserted */
7274 long count; /* Repeat this many times */
7275 int no_esc; /* Don't add an ESC at the end */
7276{
7277 char_u *esc_ptr;
7278 char_u *ptr;
7279 char_u *last_ptr;
7280 char_u last = NUL;
7281
7282 ptr = get_last_insert();
7283 if (ptr == NULL)
7284 {
7285 EMSG(_(e_noinstext));
7286 return FAIL;
7287 }
7288
7289 /* may want to stuff the command character, to start Insert mode */
7290 if (c != NUL)
7291 stuffcharReadbuff(c);
7292 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7293 *esc_ptr = NUL; /* remove the ESC */
7294
7295 /* when the last char is either "0" or "^" it will be quoted if no ESC
7296 * comes after it OR if it will inserted more than once and "ptr"
7297 * starts with ^D. -- Acevedo
7298 */
7299 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7300 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7301 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7302 {
7303 last = *last_ptr;
7304 *last_ptr = NUL;
7305 }
7306
7307 do
7308 {
7309 stuffReadbuff(ptr);
7310 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7311 if (last)
7312 stuffReadbuff((char_u *)(last == '0'
7313 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7314 : IF_EB("\026^", CTRL_V_STR "^")));
7315 }
7316 while (--count > 0);
7317
7318 if (last)
7319 *last_ptr = last;
7320
7321 if (esc_ptr != NULL)
7322 *esc_ptr = ESC; /* put the ESC back */
7323
7324 /* may want to stuff a trailing ESC, to get out of Insert mode */
7325 if (!no_esc)
7326 stuffcharReadbuff(ESC);
7327
7328 return OK;
7329}
7330
7331 char_u *
7332get_last_insert()
7333{
7334 if (last_insert == NULL)
7335 return NULL;
7336 return last_insert + last_insert_skip;
7337}
7338
7339/*
7340 * Get last inserted string, and remove trailing <Esc>.
7341 * Returns pointer to allocated memory (must be freed) or NULL.
7342 */
7343 char_u *
7344get_last_insert_save()
7345{
7346 char_u *s;
7347 int len;
7348
7349 if (last_insert == NULL)
7350 return NULL;
7351 s = vim_strsave(last_insert + last_insert_skip);
7352 if (s != NULL)
7353 {
7354 len = (int)STRLEN(s);
7355 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7356 s[len - 1] = NUL;
7357 }
7358 return s;
7359}
7360
7361/*
7362 * Check the word in front of the cursor for an abbreviation.
7363 * Called when the non-id character "c" has been entered.
7364 * When an abbreviation is recognized it is removed from the text and
7365 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7366 */
7367 static int
7368echeck_abbr(c)
7369 int c;
7370{
7371 /* Don't check for abbreviation in paste mode, when disabled and just
7372 * after moving around with cursor keys. */
7373 if (p_paste || no_abbr || arrow_used)
7374 return FALSE;
7375
7376 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7377 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7378}
7379
7380/*
7381 * replace-stack functions
7382 *
7383 * When replacing characters, the replaced characters are remembered for each
7384 * new character. This is used to re-insert the old text when backspacing.
7385 *
7386 * There is a NUL headed list of characters for each character that is
7387 * currently in the file after the insertion point. When BS is used, one NUL
7388 * headed list is put back for the deleted character.
7389 *
7390 * For a newline, there are two NUL headed lists. One contains the characters
7391 * that the NL replaced. The extra one stores the characters after the cursor
7392 * that were deleted (always white space).
7393 *
7394 * Replace_offset is normally 0, in which case replace_push will add a new
7395 * character at the end of the stack. If replace_offset is not 0, that many
7396 * characters will be left on the stack above the newly inserted character.
7397 */
7398
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007399static char_u *replace_stack = NULL;
7400static long replace_stack_nr = 0; /* next entry in replace stack */
7401static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007402
7403 void
7404replace_push(c)
7405 int c; /* character that is replaced (NUL is none) */
7406{
7407 char_u *p;
7408
7409 if (replace_stack_nr < replace_offset) /* nothing to do */
7410 return;
7411 if (replace_stack_len <= replace_stack_nr)
7412 {
7413 replace_stack_len += 50;
7414 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7415 if (p == NULL) /* out of memory */
7416 {
7417 replace_stack_len -= 50;
7418 return;
7419 }
7420 if (replace_stack != NULL)
7421 {
7422 mch_memmove(p, replace_stack,
7423 (size_t)(replace_stack_nr * sizeof(char_u)));
7424 vim_free(replace_stack);
7425 }
7426 replace_stack = p;
7427 }
7428 p = replace_stack + replace_stack_nr - replace_offset;
7429 if (replace_offset)
7430 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7431 *p = c;
7432 ++replace_stack_nr;
7433}
7434
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007435#if defined(FEAT_MBYTE) || defined(PROTO)
7436/*
7437 * Push a character onto the replace stack. Handles a multi-byte character in
7438 * reverse byte order, so that the first byte is popped off first.
7439 * Return the number of bytes done (includes composing characters).
7440 */
7441 int
7442replace_push_mb(p)
7443 char_u *p;
7444{
7445 int l = (*mb_ptr2len)(p);
7446 int j;
7447
7448 for (j = l - 1; j >= 0; --j)
7449 replace_push(p[j]);
7450 return l;
7451}
7452#endif
7453
Bram Moolenaar071d4272004-06-13 20:20:40 +00007454/*
7455 * Pop one item from the replace stack.
7456 * return -1 if stack empty
7457 * return replaced character or NUL otherwise
7458 */
7459 static int
7460replace_pop()
7461{
7462 if (replace_stack_nr == 0)
7463 return -1;
7464 return (int)replace_stack[--replace_stack_nr];
7465}
7466
7467/*
7468 * Join the top two items on the replace stack. This removes to "off"'th NUL
7469 * encountered.
7470 */
7471 static void
7472replace_join(off)
7473 int off; /* offset for which NUL to remove */
7474{
7475 int i;
7476
7477 for (i = replace_stack_nr; --i >= 0; )
7478 if (replace_stack[i] == NUL && off-- <= 0)
7479 {
7480 --replace_stack_nr;
7481 mch_memmove(replace_stack + i, replace_stack + i + 1,
7482 (size_t)(replace_stack_nr - i));
7483 return;
7484 }
7485}
7486
7487/*
7488 * Pop bytes from the replace stack until a NUL is found, and insert them
7489 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7490 */
7491 static void
7492replace_pop_ins()
7493{
7494 int cc;
7495 int oldState = State;
7496
7497 State = NORMAL; /* don't want REPLACE here */
7498 while ((cc = replace_pop()) > 0)
7499 {
7500#ifdef FEAT_MBYTE
7501 mb_replace_pop_ins(cc);
7502#else
7503 ins_char(cc);
7504#endif
7505 dec_cursor();
7506 }
7507 State = oldState;
7508}
7509
7510#ifdef FEAT_MBYTE
7511/*
7512 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7513 * indicates a multi-byte char, pop the other bytes too.
7514 */
7515 static void
7516mb_replace_pop_ins(cc)
7517 int cc;
7518{
7519 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007520 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521 int i;
7522 int c;
7523
7524 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7525 {
7526 buf[0] = cc;
7527 for (i = 1; i < n; ++i)
7528 buf[i] = replace_pop();
7529 ins_bytes_len(buf, n);
7530 }
7531 else
7532 ins_char(cc);
7533
7534 if (enc_utf8)
7535 /* Handle composing chars. */
7536 for (;;)
7537 {
7538 c = replace_pop();
7539 if (c == -1) /* stack empty */
7540 break;
7541 if ((n = MB_BYTE2LEN(c)) == 1)
7542 {
7543 /* Not a multi-byte char, put it back. */
7544 replace_push(c);
7545 break;
7546 }
7547 else
7548 {
7549 buf[0] = c;
7550 for (i = 1; i < n; ++i)
7551 buf[i] = replace_pop();
7552 if (utf_iscomposing(utf_ptr2char(buf)))
7553 ins_bytes_len(buf, n);
7554 else
7555 {
7556 /* Not a composing char, put it back. */
7557 for (i = n - 1; i >= 0; --i)
7558 replace_push(buf[i]);
7559 break;
7560 }
7561 }
7562 }
7563}
7564#endif
7565
7566/*
7567 * make the replace stack empty
7568 * (called when exiting replace mode)
7569 */
7570 static void
7571replace_flush()
7572{
7573 vim_free(replace_stack);
7574 replace_stack = NULL;
7575 replace_stack_len = 0;
7576 replace_stack_nr = 0;
7577}
7578
7579/*
7580 * Handle doing a BS for one character.
7581 * cc < 0: replace stack empty, just move cursor
7582 * cc == 0: character was inserted, delete it
7583 * cc > 0: character was replaced, put cc (first byte of original char) back
7584 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007585 * When "limit_col" is >= 0, don't delete before this column. Matters when
7586 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007587 */
7588 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007589replace_do_bs(limit_col)
7590 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007591{
7592 int cc;
7593#ifdef FEAT_VREPLACE
7594 int orig_len = 0;
7595 int ins_len;
7596 int orig_vcols = 0;
7597 colnr_T start_vcol;
7598 char_u *p;
7599 int i;
7600 int vcol;
7601#endif
7602
7603 cc = replace_pop();
7604 if (cc > 0)
7605 {
7606#ifdef FEAT_VREPLACE
7607 if (State & VREPLACE_FLAG)
7608 {
7609 /* Get the number of screen cells used by the character we are
7610 * going to delete. */
7611 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7612 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7613 }
7614#endif
7615#ifdef FEAT_MBYTE
7616 if (has_mbyte)
7617 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007618 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619# ifdef FEAT_VREPLACE
7620 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007621 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007622# endif
7623 replace_push(cc);
7624 }
7625 else
7626#endif
7627 {
7628 pchar_cursor(cc);
7629#ifdef FEAT_VREPLACE
7630 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007631 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007632#endif
7633 }
7634 replace_pop_ins();
7635
7636#ifdef FEAT_VREPLACE
7637 if (State & VREPLACE_FLAG)
7638 {
7639 /* Get the number of screen cells used by the inserted characters */
7640 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007641 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007642 vcol = start_vcol;
7643 for (i = 0; i < ins_len; ++i)
7644 {
7645 vcol += chartabsize(p + i, vcol);
7646#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007647 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007648#endif
7649 }
7650 vcol -= start_vcol;
7651
7652 /* Delete spaces that were inserted after the cursor to keep the
7653 * text aligned. */
7654 curwin->w_cursor.col += ins_len;
7655 while (vcol > orig_vcols && gchar_cursor() == ' ')
7656 {
7657 del_char(FALSE);
7658 ++orig_vcols;
7659 }
7660 curwin->w_cursor.col -= ins_len;
7661 }
7662#endif
7663
7664 /* mark the buffer as changed and prepare for displaying */
7665 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7666 }
7667 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007668 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007669}
7670
7671#ifdef FEAT_CINDENT
7672/*
7673 * Return TRUE if C-indenting is on.
7674 */
7675 static int
7676cindent_on()
7677{
7678 return (!p_paste && (curbuf->b_p_cin
7679# ifdef FEAT_EVAL
7680 || *curbuf->b_p_inde != NUL
7681# endif
7682 ));
7683}
7684#endif
7685
7686#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7687/*
7688 * Re-indent the current line, based on the current contents of it and the
7689 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7690 * confused what all the part that handles Control-T is doing that I'm not.
7691 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7692 */
7693
7694 void
7695fixthisline(get_the_indent)
7696 int (*get_the_indent) __ARGS((void));
7697{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007698 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007699 if (linewhite(curwin->w_cursor.lnum))
7700 did_ai = TRUE; /* delete the indent if the line stays empty */
7701}
7702
7703 void
7704fix_indent()
7705{
7706 if (p_paste)
7707 return;
7708# ifdef FEAT_LISP
7709 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7710 fixthisline(get_lisp_indent);
7711# endif
7712# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7713 else
7714# endif
7715# ifdef FEAT_CINDENT
7716 if (cindent_on())
7717 do_c_expr_indent();
7718# endif
7719}
7720
7721#endif
7722
7723#ifdef FEAT_CINDENT
7724/*
7725 * return TRUE if 'cinkeys' contains the key "keytyped",
7726 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007727 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7729 *
7730 * "keytyped" can have a few special values:
7731 * KEY_OPEN_FORW
7732 * KEY_OPEN_BACK
7733 * KEY_COMPLETE just finished completion.
7734 *
7735 * If line_is_empty is TRUE accept keys with '0' before them.
7736 */
7737 int
7738in_cinkeys(keytyped, when, line_is_empty)
7739 int keytyped;
7740 int when;
7741 int line_is_empty;
7742{
7743 char_u *look;
7744 int try_match;
7745 int try_match_word;
7746 char_u *p;
7747 char_u *line;
7748 int icase;
7749 int i;
7750
Bram Moolenaar30848942009-12-24 14:46:12 +00007751 if (keytyped == NUL)
7752 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7753 return FALSE;
7754
Bram Moolenaar071d4272004-06-13 20:20:40 +00007755#ifdef FEAT_EVAL
7756 if (*curbuf->b_p_inde != NUL)
7757 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7758 else
7759#endif
7760 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7761 while (*look)
7762 {
7763 /*
7764 * Find out if we want to try a match with this key, depending on
7765 * 'when' and a '*' or '!' before the key.
7766 */
7767 switch (when)
7768 {
7769 case '*': try_match = (*look == '*'); break;
7770 case '!': try_match = (*look == '!'); break;
7771 default: try_match = (*look != '*'); break;
7772 }
7773 if (*look == '*' || *look == '!')
7774 ++look;
7775
7776 /*
7777 * If there is a '0', only accept a match if the line is empty.
7778 * But may still match when typing last char of a word.
7779 */
7780 if (*look == '0')
7781 {
7782 try_match_word = try_match;
7783 if (!line_is_empty)
7784 try_match = FALSE;
7785 ++look;
7786 }
7787 else
7788 try_match_word = FALSE;
7789
7790 /*
7791 * does it look like a control character?
7792 */
7793 if (*look == '^'
7794#ifdef EBCDIC
7795 && (Ctrl_chr(look[1]) != 0)
7796#else
7797 && look[1] >= '?' && look[1] <= '_'
7798#endif
7799 )
7800 {
7801 if (try_match && keytyped == Ctrl_chr(look[1]))
7802 return TRUE;
7803 look += 2;
7804 }
7805 /*
7806 * 'o' means "o" command, open forward.
7807 * 'O' means "O" command, open backward.
7808 */
7809 else if (*look == 'o')
7810 {
7811 if (try_match && keytyped == KEY_OPEN_FORW)
7812 return TRUE;
7813 ++look;
7814 }
7815 else if (*look == 'O')
7816 {
7817 if (try_match && keytyped == KEY_OPEN_BACK)
7818 return TRUE;
7819 ++look;
7820 }
7821
7822 /*
7823 * 'e' means to check for "else" at start of line and just before the
7824 * cursor.
7825 */
7826 else if (*look == 'e')
7827 {
7828 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7829 {
7830 p = ml_get_curline();
7831 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7832 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7833 return TRUE;
7834 }
7835 ++look;
7836 }
7837
7838 /*
7839 * ':' only causes an indent if it is at the end of a label or case
7840 * statement, or when it was before typing the ':' (to fix
7841 * class::method for C++).
7842 */
7843 else if (*look == ':')
7844 {
7845 if (try_match && keytyped == ':')
7846 {
7847 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007848 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7849 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007851 /* Need to get the line again after cin_islabel(). */
7852 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007853 if (curwin->w_cursor.col > 2
7854 && p[curwin->w_cursor.col - 1] == ':'
7855 && p[curwin->w_cursor.col - 2] == ':')
7856 {
7857 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007858 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 || cin_islabel(30));
7860 p = ml_get_curline();
7861 p[curwin->w_cursor.col - 1] = ':';
7862 if (i)
7863 return TRUE;
7864 }
7865 }
7866 ++look;
7867 }
7868
7869
7870 /*
7871 * Is it a key in <>, maybe?
7872 */
7873 else if (*look == '<')
7874 {
7875 if (try_match)
7876 {
7877 /*
7878 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7879 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7880 * >, *, : and ! keys if they really really want to.
7881 */
7882 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7883 && keytyped == look[1])
7884 return TRUE;
7885
7886 if (keytyped == get_special_key_code(look + 1))
7887 return TRUE;
7888 }
7889 while (*look && *look != '>')
7890 look++;
7891 while (*look == '>')
7892 look++;
7893 }
7894
7895 /*
7896 * Is it a word: "=word"?
7897 */
7898 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7899 {
7900 ++look;
7901 if (*look == '~')
7902 {
7903 icase = TRUE;
7904 ++look;
7905 }
7906 else
7907 icase = FALSE;
7908 p = vim_strchr(look, ',');
7909 if (p == NULL)
7910 p = look + STRLEN(look);
7911 if ((try_match || try_match_word)
7912 && curwin->w_cursor.col >= (colnr_T)(p - look))
7913 {
7914 int match = FALSE;
7915
7916#ifdef FEAT_INS_EXPAND
7917 if (keytyped == KEY_COMPLETE)
7918 {
7919 char_u *s;
7920
7921 /* Just completed a word, check if it starts with "look".
7922 * search back for the start of a word. */
7923 line = ml_get_curline();
7924# ifdef FEAT_MBYTE
7925 if (has_mbyte)
7926 {
7927 char_u *n;
7928
7929 for (s = line + curwin->w_cursor.col; s > line; s = n)
7930 {
7931 n = mb_prevptr(line, s);
7932 if (!vim_iswordp(n))
7933 break;
7934 }
7935 }
7936 else
7937# endif
7938 for (s = line + curwin->w_cursor.col; s > line; --s)
7939 if (!vim_iswordc(s[-1]))
7940 break;
7941 if (s + (p - look) <= line + curwin->w_cursor.col
7942 && (icase
7943 ? MB_STRNICMP(s, look, p - look)
7944 : STRNCMP(s, look, p - look)) == 0)
7945 match = TRUE;
7946 }
7947 else
7948#endif
7949 /* TODO: multi-byte */
7950 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7951 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7952 {
7953 line = ml_get_cursor();
7954 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7955 || !vim_iswordc(line[-(p - look) - 1]))
7956 && (icase
7957 ? MB_STRNICMP(line - (p - look), look, p - look)
7958 : STRNCMP(line - (p - look), look, p - look))
7959 == 0)
7960 match = TRUE;
7961 }
7962 if (match && try_match_word && !try_match)
7963 {
7964 /* "0=word": Check if there are only blanks before the
7965 * word. */
7966 line = ml_get_curline();
7967 if ((int)(skipwhite(line) - line) !=
7968 (int)(curwin->w_cursor.col - (p - look)))
7969 match = FALSE;
7970 }
7971 if (match)
7972 return TRUE;
7973 }
7974 look = p;
7975 }
7976
7977 /*
7978 * ok, it's a boring generic character.
7979 */
7980 else
7981 {
7982 if (try_match && *look == keytyped)
7983 return TRUE;
7984 ++look;
7985 }
7986
7987 /*
7988 * Skip over ", ".
7989 */
7990 look = skip_to_option_part(look);
7991 }
7992 return FALSE;
7993}
7994#endif /* FEAT_CINDENT */
7995
7996#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7997/*
7998 * Map Hebrew keyboard when in hkmap mode.
7999 */
8000 int
8001hkmap(c)
8002 int c;
8003{
8004 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8005 {
8006 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8007 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8008 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8009 static char_u map[26] =
8010 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8011 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8012 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8013 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8014 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8015 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8016 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8017 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8018 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8019
8020 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8021 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8022 /* '-1'='sofit' */
8023 else if (c == 'x')
8024 return 'X';
8025 else if (c == 'q')
8026 return '\''; /* {geresh}={'} */
8027 else if (c == 246)
8028 return ' '; /* \"o --> ' ' for a german keyboard */
8029 else if (c == 228)
8030 return ' '; /* \"a --> ' ' -- / -- */
8031 else if (c == 252)
8032 return ' '; /* \"u --> ' ' -- / -- */
8033#ifdef EBCDIC
8034 else if (islower(c))
8035#else
8036 /* NOTE: islower() does not do the right thing for us on Linux so we
8037 * do this the same was as 5.7 and previous, so it works correctly on
8038 * all systems. Specifically, the e.g. Delete and Arrow keys are
8039 * munged and won't work if e.g. searching for Hebrew text.
8040 */
8041 else if (c >= 'a' && c <= 'z')
8042#endif
8043 return (int)(map[CharOrdLow(c)] + p_aleph);
8044 else
8045 return c;
8046 }
8047 else
8048 {
8049 switch (c)
8050 {
8051 case '`': return ';';
8052 case '/': return '.';
8053 case '\'': return ',';
8054 case 'q': return '/';
8055 case 'w': return '\'';
8056
8057 /* Hebrew letters - set offset from 'a' */
8058 case ',': c = '{'; break;
8059 case '.': c = 'v'; break;
8060 case ';': c = 't'; break;
8061 default: {
8062 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8063
8064#ifdef EBCDIC
8065 /* see note about islower() above */
8066 if (!islower(c))
8067#else
8068 if (c < 'a' || c > 'z')
8069#endif
8070 return c;
8071 c = str[CharOrdLow(c)];
8072 break;
8073 }
8074 }
8075
8076 return (int)(CharOrdLow(c) + p_aleph);
8077 }
8078}
8079#endif
8080
8081 static void
8082ins_reg()
8083{
8084 int need_redraw = FALSE;
8085 int regname;
8086 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008087#ifdef FEAT_VISUAL
8088 int vis_active = VIsual_active;
8089#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090
8091 /*
8092 * If we are going to wait for a character, show a '"'.
8093 */
8094 pc_status = PC_STATUS_UNSET;
8095 if (redrawing() && !char_avail())
8096 {
8097 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008098 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099
8100 edit_putchar('"', TRUE);
8101#ifdef FEAT_CMDL_INFO
8102 add_to_showcmd_c(Ctrl_R);
8103#endif
8104 }
8105
8106#ifdef USE_ON_FLY_SCROLL
8107 dont_scroll = TRUE; /* disallow scrolling here */
8108#endif
8109
8110 /*
8111 * Don't map the register name. This also prevents the mode message to be
8112 * deleted when ESC is hit.
8113 */
8114 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008115 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008116 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8118 {
8119 /* Get a third key for literal register insertion */
8120 literally = regname;
8121#ifdef FEAT_CMDL_INFO
8122 add_to_showcmd_c(literally);
8123#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008124 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008125 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 }
8127 --no_mapping;
8128
8129#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008130 /* Don't call u_sync() while typing the expression or giving an error
8131 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008132 ++no_u_sync;
8133 if (regname == '=')
8134 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008135# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008136 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008137# endif
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008138 /* Sync undo, so the effect of e.g., setline() can be undone. */
8139 u_sync(TRUE);
8140 ins_need_undo = TRUE;
8141
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008143# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 /* Restore the Input Method. */
8145 if (im_on)
8146 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008147# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008149 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8150 {
8151 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008152 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008153 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 else
8155 {
8156#endif
8157 if (literally == Ctrl_O || literally == Ctrl_P)
8158 {
8159 /* Append the command to the redo buffer. */
8160 AppendCharToRedobuff(Ctrl_R);
8161 AppendCharToRedobuff(literally);
8162 AppendCharToRedobuff(regname);
8163
8164 do_put(regname, BACKWARD, 1L,
8165 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8166 }
8167 else if (insert_reg(regname, literally) == FAIL)
8168 {
8169 vim_beep();
8170 need_redraw = TRUE; /* remove the '"' */
8171 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008172 else if (stop_insert_mode)
8173 /* When the '=' register was used and a function was invoked that
8174 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8175 * insert anything, need to remove the '"' */
8176 need_redraw = TRUE;
8177
Bram Moolenaar071d4272004-06-13 20:20:40 +00008178#ifdef FEAT_EVAL
8179 }
8180 --no_u_sync;
8181#endif
8182#ifdef FEAT_CMDL_INFO
8183 clear_showcmd();
8184#endif
8185
8186 /* If the inserted register is empty, we need to remove the '"' */
8187 if (need_redraw || stuff_empty())
8188 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008189
8190#ifdef FEAT_VISUAL
8191 /* Disallow starting Visual mode here, would get a weird mode. */
8192 if (!vis_active && VIsual_active)
8193 end_visual_mode();
8194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195}
8196
8197/*
8198 * CTRL-G commands in Insert mode.
8199 */
8200 static void
8201ins_ctrl_g()
8202{
8203 int c;
8204
8205#ifdef FEAT_INS_EXPAND
8206 /* Right after CTRL-X the cursor will be after the ruler. */
8207 setcursor();
8208#endif
8209
8210 /*
8211 * Don't map the second key. This also prevents the mode message to be
8212 * deleted when ESC is hit.
8213 */
8214 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008215 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 --no_mapping;
8217 switch (c)
8218 {
8219 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8220 case K_UP:
8221 case Ctrl_K:
8222 case 'k': ins_up(TRUE);
8223 break;
8224
8225 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8226 case K_DOWN:
8227 case Ctrl_J:
8228 case 'j': ins_down(TRUE);
8229 break;
8230
8231 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008232 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008234
8235 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008236 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008237 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 break;
8239
8240 /* Unknown CTRL-G command, reserved for future expansion. */
8241 default: vim_beep();
8242 }
8243}
8244
8245/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008246 * CTRL-^ in Insert mode.
8247 */
8248 static void
8249ins_ctrl_hat()
8250{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008251 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008252 {
8253 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8254 if (State & LANGMAP)
8255 {
8256 curbuf->b_p_iminsert = B_IMODE_NONE;
8257 State &= ~LANGMAP;
8258 }
8259 else
8260 {
8261 curbuf->b_p_iminsert = B_IMODE_LMAP;
8262 State |= LANGMAP;
8263#ifdef USE_IM_CONTROL
8264 im_set_active(FALSE);
8265#endif
8266 }
8267 }
8268#ifdef USE_IM_CONTROL
8269 else
8270 {
8271 /* There are no ":lmap" mappings, toggle IM */
8272 if (im_get_status())
8273 {
8274 curbuf->b_p_iminsert = B_IMODE_NONE;
8275 im_set_active(FALSE);
8276 }
8277 else
8278 {
8279 curbuf->b_p_iminsert = B_IMODE_IM;
8280 State &= ~LANGMAP;
8281 im_set_active(TRUE);
8282 }
8283 }
8284#endif
8285 set_iminsert_global();
8286 showmode();
8287#ifdef FEAT_GUI
8288 /* may show different cursor shape or color */
8289 if (gui.in_use)
8290 gui_update_cursor(TRUE, FALSE);
8291#endif
8292#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8293 /* Show/unshow value of 'keymap' in status lines. */
8294 status_redraw_curbuf();
8295#endif
8296}
8297
8298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008299 * Handle ESC in insert mode.
8300 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8301 * insert.
8302 */
8303 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008304ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 long *count;
8306 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008307 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308{
8309 int temp;
8310 static int disabled_redraw = FALSE;
8311
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008312#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008313 check_spell_redraw();
8314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315#if defined(FEAT_HANGULIN)
8316# if defined(ESC_CHG_TO_ENG_MODE)
8317 hangul_input_state_set(0);
8318# endif
8319 if (composing_hangul)
8320 {
8321 push_raw_key(composing_hangul_buffer, 2);
8322 composing_hangul = 0;
8323 }
8324#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008325
8326 temp = curwin->w_cursor.col;
8327 if (disabled_redraw)
8328 {
8329 --RedrawingDisabled;
8330 disabled_redraw = FALSE;
8331 }
8332 if (!arrow_used)
8333 {
8334 /*
8335 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008336 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8337 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 */
8339 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008340 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341
8342 /*
8343 * Repeating insert may take a long time. Check for
8344 * interrupt now and then.
8345 */
8346 if (*count > 0)
8347 {
8348 line_breakcheck();
8349 if (got_int)
8350 *count = 0;
8351 }
8352
8353 if (--*count > 0) /* repeat what was typed */
8354 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008355 /* Vi repeats the insert without replacing characters. */
8356 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8357 State &= ~REPLACE_FLAG;
8358
Bram Moolenaar071d4272004-06-13 20:20:40 +00008359 (void)start_redo_ins();
8360 if (cmdchar == 'r' || cmdchar == 'v')
8361 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8362 ++RedrawingDisabled;
8363 disabled_redraw = TRUE;
8364 return FALSE; /* repeat the insert */
8365 }
8366 stop_insert(&curwin->w_cursor, TRUE);
8367 undisplay_dollar();
8368 }
8369
8370 /* When an autoindent was removed, curswant stays after the
8371 * indent */
8372 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8373 curwin->w_set_curswant = TRUE;
8374
8375 /* Remember the last Insert position in the '^ mark. */
8376 if (!cmdmod.keepjumps)
8377 curbuf->b_last_insert = curwin->w_cursor;
8378
8379 /*
8380 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008381 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008382 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008383 if (!nomove
8384 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008385#ifdef FEAT_VIRTUALEDIT
8386 || curwin->w_cursor.coladd > 0
8387#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008388 )
8389 && (restart_edit == NUL
8390 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008391#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008392 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008394 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395#ifdef FEAT_RIGHTLEFT
8396 && !revins_on
8397#endif
8398 )
8399 {
8400#ifdef FEAT_VIRTUALEDIT
8401 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8402 {
8403 oneleft();
8404 if (restart_edit != NUL)
8405 ++curwin->w_cursor.coladd;
8406 }
8407 else
8408#endif
8409 {
8410 --curwin->w_cursor.col;
8411#ifdef FEAT_MBYTE
8412 /* Correct cursor for multi-byte character. */
8413 if (has_mbyte)
8414 mb_adjust_cursor();
8415#endif
8416 }
8417 }
8418
8419#ifdef USE_IM_CONTROL
8420 /* Disable IM to allow typing English directly for Normal mode commands.
8421 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8422 * well). */
8423 if (!(State & LANGMAP))
8424 im_save_status(&curbuf->b_p_iminsert);
8425 im_set_active(FALSE);
8426#endif
8427
8428 State = NORMAL;
8429 /* need to position cursor again (e.g. when on a TAB ) */
8430 changed_cline_bef_curs();
8431
8432#ifdef FEAT_MOUSE
8433 setmouse();
8434#endif
8435#ifdef CURSOR_SHAPE
8436 ui_cursor_shape(); /* may show different cursor shape */
8437#endif
8438
8439 /*
8440 * When recording or for CTRL-O, need to display the new mode.
8441 * Otherwise remove the mode message.
8442 */
8443 if (Recording || restart_edit != NUL)
8444 showmode();
8445 else if (p_smd)
8446 MSG("");
8447
8448 return TRUE; /* exit Insert mode */
8449}
8450
8451#ifdef FEAT_RIGHTLEFT
8452/*
8453 * Toggle language: hkmap and revins_on.
8454 * Move to end of reverse inserted text.
8455 */
8456 static void
8457ins_ctrl_()
8458{
8459 if (revins_on && revins_chars && revins_scol >= 0)
8460 {
8461 while (gchar_cursor() != NUL && revins_chars--)
8462 ++curwin->w_cursor.col;
8463 }
8464 p_ri = !p_ri;
8465 revins_on = (State == INSERT && p_ri);
8466 if (revins_on)
8467 {
8468 revins_scol = curwin->w_cursor.col;
8469 revins_legal++;
8470 revins_chars = 0;
8471 undisplay_dollar();
8472 }
8473 else
8474 revins_scol = -1;
8475#ifdef FEAT_FKMAP
8476 if (p_altkeymap)
8477 {
8478 /*
8479 * to be consistent also for redo command, using '.'
8480 * set arrow_used to true and stop it - causing to redo
8481 * characters entered in one mode (normal/reverse insert).
8482 */
8483 arrow_used = TRUE;
8484 (void)stop_arrow();
8485 p_fkmap = curwin->w_p_rl ^ p_ri;
8486 if (p_fkmap && p_ri)
8487 State = INSERT;
8488 }
8489 else
8490#endif
8491 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8492 showmode();
8493}
8494#endif
8495
8496#ifdef FEAT_VISUAL
8497/*
8498 * If 'keymodel' contains "startsel", may start selection.
8499 * Returns TRUE when a CTRL-O and other keys stuffed.
8500 */
8501 static int
8502ins_start_select(c)
8503 int c;
8504{
8505 if (km_startsel)
8506 switch (c)
8507 {
8508 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 case K_PAGEUP:
8511 case K_KPAGEUP:
8512 case K_PAGEDOWN:
8513 case K_KPAGEDOWN:
8514# ifdef MACOS
8515 case K_LEFT:
8516 case K_RIGHT:
8517 case K_UP:
8518 case K_DOWN:
8519 case K_END:
8520 case K_HOME:
8521# endif
8522 if (!(mod_mask & MOD_MASK_SHIFT))
8523 break;
8524 /* FALLTHROUGH */
8525 case K_S_LEFT:
8526 case K_S_RIGHT:
8527 case K_S_UP:
8528 case K_S_DOWN:
8529 case K_S_END:
8530 case K_S_HOME:
8531 /* Start selection right away, the cursor can move with
8532 * CTRL-O when beyond the end of the line. */
8533 start_selection();
8534
8535 /* Execute the key in (insert) Select mode. */
8536 stuffcharReadbuff(Ctrl_O);
8537 if (mod_mask)
8538 {
8539 char_u buf[4];
8540
8541 buf[0] = K_SPECIAL;
8542 buf[1] = KS_MODIFIER;
8543 buf[2] = mod_mask;
8544 buf[3] = NUL;
8545 stuffReadbuff(buf);
8546 }
8547 stuffcharReadbuff(c);
8548 return TRUE;
8549 }
8550 return FALSE;
8551}
8552#endif
8553
8554/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008555 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008556 */
8557 static void
8558ins_insert(replaceState)
8559 int replaceState;
8560{
8561#ifdef FEAT_FKMAP
8562 if (p_fkmap && p_ri)
8563 {
8564 beep_flush();
8565 EMSG(farsi_text_3); /* encoded in Farsi */
8566 return;
8567 }
8568#endif
8569
8570#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008571# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008572 set_vim_var_string(VV_INSERTMODE,
8573 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008574# ifdef FEAT_VREPLACE
8575 replaceState == VREPLACE ? "v" :
8576# endif
8577 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008578# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008579 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8580#endif
8581 if (State & REPLACE_FLAG)
8582 State = INSERT | (State & LANGMAP);
8583 else
8584 State = replaceState | (State & LANGMAP);
8585 AppendCharToRedobuff(K_INS);
8586 showmode();
8587#ifdef CURSOR_SHAPE
8588 ui_cursor_shape(); /* may show different cursor shape */
8589#endif
8590}
8591
8592/*
8593 * Pressed CTRL-O in Insert mode.
8594 */
8595 static void
8596ins_ctrl_o()
8597{
8598#ifdef FEAT_VREPLACE
8599 if (State & VREPLACE_FLAG)
8600 restart_edit = 'V';
8601 else
8602#endif
8603 if (State & REPLACE_FLAG)
8604 restart_edit = 'R';
8605 else
8606 restart_edit = 'I';
8607#ifdef FEAT_VIRTUALEDIT
8608 if (virtual_active())
8609 ins_at_eol = FALSE; /* cursor always keeps its column */
8610 else
8611#endif
8612 ins_at_eol = (gchar_cursor() == NUL);
8613}
8614
8615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 * If the cursor is on an indent, ^T/^D insert/delete one
8617 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008618 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619 * with vi. But vi only supports ^T and ^D after an
8620 * autoindent, we support it everywhere.
8621 */
8622 static void
8623ins_shift(c, lastc)
8624 int c;
8625 int lastc;
8626{
8627 if (stop_arrow() == FAIL)
8628 return;
8629 AppendCharToRedobuff(c);
8630
8631 /*
8632 * 0^D and ^^D: remove all indent.
8633 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008634 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8635 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008636 {
8637 --curwin->w_cursor.col;
8638 (void)del_char(FALSE); /* delete the '^' or '0' */
8639 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8640 if (State & REPLACE_FLAG)
8641 replace_pop_ins();
8642 if (lastc == '^')
8643 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008644 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 }
8646 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008647 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648
8649 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8650 did_ai = FALSE;
8651#ifdef FEAT_SMARTINDENT
8652 did_si = FALSE;
8653 can_si = FALSE;
8654 can_si_back = FALSE;
8655#endif
8656#ifdef FEAT_CINDENT
8657 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8658#endif
8659}
8660
8661 static void
8662ins_del()
8663{
8664 int temp;
8665
8666 if (stop_arrow() == FAIL)
8667 return;
8668 if (gchar_cursor() == NUL) /* delete newline */
8669 {
8670 temp = curwin->w_cursor.col;
8671 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar81340392012-06-06 16:12:59 +02008672 || do_join(2, FALSE, TRUE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 vim_beep();
8674 else
8675 curwin->w_cursor.col = temp;
8676 }
8677 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8678 vim_beep();
8679 did_ai = FALSE;
8680#ifdef FEAT_SMARTINDENT
8681 did_si = FALSE;
8682 can_si = FALSE;
8683 can_si_back = FALSE;
8684#endif
8685 AppendCharToRedobuff(K_DEL);
8686}
8687
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008688static void ins_bs_one __ARGS((colnr_T *vcolp));
8689
8690/*
8691 * Delete one character for ins_bs().
8692 */
8693 static void
8694ins_bs_one(vcolp)
8695 colnr_T *vcolp;
8696{
8697 dec_cursor();
8698 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8699 if (State & REPLACE_FLAG)
8700 {
8701 /* Don't delete characters before the insert point when in
8702 * Replace mode */
8703 if (curwin->w_cursor.lnum != Insstart.lnum
8704 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008705 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008706 }
8707 else
8708 (void)del_char(FALSE);
8709}
8710
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711/*
8712 * Handle Backspace, delete-word and delete-line in Insert mode.
8713 * Return TRUE when backspace was actually used.
8714 */
8715 static int
8716ins_bs(c, mode, inserted_space_p)
8717 int c;
8718 int mode;
8719 int *inserted_space_p;
8720{
8721 linenr_T lnum;
8722 int cc;
8723 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008724 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008725 colnr_T mincol;
8726 int did_backspace = FALSE;
8727 int in_indent;
8728 int oldState;
8729#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008730 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731#endif
8732
8733 /*
8734 * can't delete anything in an empty file
8735 * can't backup past first character in buffer
8736 * can't backup past starting point unless 'backspace' > 1
8737 * can backup to a previous line if 'backspace' == 0
8738 */
8739 if ( bufempty()
8740 || (
8741#ifdef FEAT_RIGHTLEFT
8742 !revins_on &&
8743#endif
8744 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8745 || (!can_bs(BS_START)
8746 && (arrow_used
8747 || (curwin->w_cursor.lnum == Insstart.lnum
8748 && curwin->w_cursor.col <= Insstart.col)))
8749 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8750 && curwin->w_cursor.col <= ai_col)
8751 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8752 {
8753 vim_beep();
8754 return FALSE;
8755 }
8756
8757 if (stop_arrow() == FAIL)
8758 return FALSE;
8759 in_indent = inindent(0);
8760#ifdef FEAT_CINDENT
8761 if (in_indent)
8762 can_cindent = FALSE;
8763#endif
8764#ifdef FEAT_COMMENTS
8765 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8766#endif
8767#ifdef FEAT_RIGHTLEFT
8768 if (revins_on) /* put cursor after last inserted char */
8769 inc_cursor();
8770#endif
8771
8772#ifdef FEAT_VIRTUALEDIT
8773 /* Virtualedit:
8774 * BACKSPACE_CHAR eats a virtual space
8775 * BACKSPACE_WORD eats all coladd
8776 * BACKSPACE_LINE eats all coladd and keeps going
8777 */
8778 if (curwin->w_cursor.coladd > 0)
8779 {
8780 if (mode == BACKSPACE_CHAR)
8781 {
8782 --curwin->w_cursor.coladd;
8783 return TRUE;
8784 }
8785 if (mode == BACKSPACE_WORD)
8786 {
8787 curwin->w_cursor.coladd = 0;
8788 return TRUE;
8789 }
8790 curwin->w_cursor.coladd = 0;
8791 }
8792#endif
8793
8794 /*
8795 * delete newline!
8796 */
8797 if (curwin->w_cursor.col == 0)
8798 {
8799 lnum = Insstart.lnum;
8800 if (curwin->w_cursor.lnum == Insstart.lnum
8801#ifdef FEAT_RIGHTLEFT
8802 || revins_on
8803#endif
8804 )
8805 {
8806 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8807 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8808 return FALSE;
8809 --Insstart.lnum;
8810 Insstart.col = MAXCOL;
8811 }
8812 /*
8813 * In replace mode:
8814 * cc < 0: NL was inserted, delete it
8815 * cc >= 0: NL was replaced, put original characters back
8816 */
8817 cc = -1;
8818 if (State & REPLACE_FLAG)
8819 cc = replace_pop(); /* returns -1 if NL was inserted */
8820 /*
8821 * In replace mode, in the line we started replacing, we only move the
8822 * cursor.
8823 */
8824 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8825 {
8826 dec_cursor();
8827 }
8828 else
8829 {
8830#ifdef FEAT_VREPLACE
8831 if (!(State & VREPLACE_FLAG)
8832 || curwin->w_cursor.lnum > orig_line_count)
8833#endif
8834 {
8835 temp = gchar_cursor(); /* remember current char */
8836 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008837
8838 /* When "aw" is in 'formatoptions' we must delete the space at
8839 * the end of the line, otherwise the line will be broken
8840 * again when auto-formatting. */
8841 if (has_format_option(FO_AUTO)
8842 && has_format_option(FO_WHITE_PAR))
8843 {
8844 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8845 TRUE);
8846 int len;
8847
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008848 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008849 if (len > 0 && ptr[len - 1] == ' ')
8850 ptr[len - 1] = NUL;
8851 }
8852
Bram Moolenaar81340392012-06-06 16:12:59 +02008853 (void)do_join(2, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 if (temp == NUL && gchar_cursor() != NUL)
8855 inc_cursor();
8856 }
8857#ifdef FEAT_VREPLACE
8858 else
8859 dec_cursor();
8860#endif
8861
8862 /*
8863 * In REPLACE mode we have to put back the text that was replaced
8864 * by the NL. On the replace stack is first a NUL-terminated
8865 * sequence of characters that were deleted and then the
8866 * characters that NL replaced.
8867 */
8868 if (State & REPLACE_FLAG)
8869 {
8870 /*
8871 * Do the next ins_char() in NORMAL state, to
8872 * prevent ins_char() from replacing characters and
8873 * avoiding showmatch().
8874 */
8875 oldState = State;
8876 State = NORMAL;
8877 /*
8878 * restore characters (blanks) deleted after cursor
8879 */
8880 while (cc > 0)
8881 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008882 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883#ifdef FEAT_MBYTE
8884 mb_replace_pop_ins(cc);
8885#else
8886 ins_char(cc);
8887#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008888 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 cc = replace_pop();
8890 }
8891 /* restore the characters that NL replaced */
8892 replace_pop_ins();
8893 State = oldState;
8894 }
8895 }
8896 did_ai = FALSE;
8897 }
8898 else
8899 {
8900 /*
8901 * Delete character(s) before the cursor.
8902 */
8903#ifdef FEAT_RIGHTLEFT
8904 if (revins_on) /* put cursor on last inserted char */
8905 dec_cursor();
8906#endif
8907 mincol = 0;
8908 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008909 if (mode == BACKSPACE_LINE
8910 && (curbuf->b_p_ai
8911#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008912 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008913#endif
8914 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915#ifdef FEAT_RIGHTLEFT
8916 && !revins_on
8917#endif
8918 )
8919 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008920 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008922 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008924 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925 }
8926
8927 /*
8928 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8929 */
8930 if ( mode == BACKSPACE_CHAR
8931 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008932 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008933 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 && (*(ml_get_cursor() - 1) == TAB
8935 || (*(ml_get_cursor() - 1) == ' '
8936 && (!*inserted_space_p
8937 || arrow_used))))))
8938 {
8939 int ts;
8940 colnr_T vcol;
8941 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008942 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008943
8944 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008945 if (p_sta && in_indent)
Bram Moolenaar14f24742012-08-08 18:01:05 +02008946 ts = (int)get_sw_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008947 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008948 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 /* Compute the virtual column where we want to be. Since
8950 * 'showbreak' may get in the way, need to get the last column of
8951 * the previous character. */
8952 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008953 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 dec_cursor();
8955 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8956 inc_cursor();
8957 want_vcol = (want_vcol / ts) * ts;
8958
8959 /* delete characters until we are at or before want_vcol */
8960 while (vcol > want_vcol
8961 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008962 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008963
8964 /* insert extra spaces until we are at want_vcol */
8965 while (vcol < want_vcol)
8966 {
8967 /* Remember the first char we inserted */
8968 if (curwin->w_cursor.lnum == Insstart.lnum
8969 && curwin->w_cursor.col < Insstart.col)
8970 Insstart.col = curwin->w_cursor.col;
8971
8972#ifdef FEAT_VREPLACE
8973 if (State & VREPLACE_FLAG)
8974 ins_char(' ');
8975 else
8976#endif
8977 {
8978 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008979 if ((State & REPLACE_FLAG))
8980 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008981 }
8982 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8983 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008984
8985 /* If we are now back where we started delete one character. Can
8986 * happen when using 'sts' and 'linebreak'. */
8987 if (vcol >= start_vcol)
8988 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 }
8990
8991 /*
8992 * Delete upto starting point, start of line or previous word.
8993 */
8994 else do
8995 {
8996#ifdef FEAT_RIGHTLEFT
8997 if (!revins_on) /* put cursor on char to be deleted */
8998#endif
8999 dec_cursor();
9000
9001 /* start of word? */
9002 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
9003 {
9004 mode = BACKSPACE_WORD_NOT_SPACE;
9005 temp = vim_iswordc(gchar_cursor());
9006 }
9007 /* end of word? */
9008 else if (mode == BACKSPACE_WORD_NOT_SPACE
9009 && (vim_isspace(cc = gchar_cursor())
9010 || vim_iswordc(cc) != temp))
9011 {
9012#ifdef FEAT_RIGHTLEFT
9013 if (!revins_on)
9014#endif
9015 inc_cursor();
9016#ifdef FEAT_RIGHTLEFT
9017 else if (State & REPLACE_FLAG)
9018 dec_cursor();
9019#endif
9020 break;
9021 }
9022 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009023 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024 else
9025 {
9026#ifdef FEAT_MBYTE
9027 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009028 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009029#endif
9030 (void)del_char(FALSE);
9031#ifdef FEAT_MBYTE
9032 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009033 * If there are combining characters and 'delcombine' is set
9034 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00009035 * character.
9036 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009037 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038 inc_cursor();
9039#endif
9040#ifdef FEAT_RIGHTLEFT
9041 if (revins_chars)
9042 {
9043 revins_chars--;
9044 revins_legal++;
9045 }
9046 if (revins_on && gchar_cursor() == NUL)
9047 break;
9048#endif
9049 }
9050 /* Just a single backspace?: */
9051 if (mode == BACKSPACE_CHAR)
9052 break;
9053 } while (
9054#ifdef FEAT_RIGHTLEFT
9055 revins_on ||
9056#endif
9057 (curwin->w_cursor.col > mincol
9058 && (curwin->w_cursor.lnum != Insstart.lnum
9059 || curwin->w_cursor.col != Insstart.col)));
9060 did_backspace = TRUE;
9061 }
9062#ifdef FEAT_SMARTINDENT
9063 did_si = FALSE;
9064 can_si = FALSE;
9065 can_si_back = FALSE;
9066#endif
9067 if (curwin->w_cursor.col <= 1)
9068 did_ai = FALSE;
9069 /*
9070 * It's a little strange to put backspaces into the redo
9071 * buffer, but it makes auto-indent a lot easier to deal
9072 * with.
9073 */
9074 AppendCharToRedobuff(c);
9075
9076 /* If deleted before the insertion point, adjust it */
9077 if (curwin->w_cursor.lnum == Insstart.lnum
9078 && curwin->w_cursor.col < Insstart.col)
9079 Insstart.col = curwin->w_cursor.col;
9080
9081 /* vi behaviour: the cursor moves backward but the character that
9082 * was there remains visible
9083 * Vim behaviour: the cursor moves backward and the character that
9084 * was there is erased from the screen.
9085 * We can emulate the vi behaviour by pretending there is a dollar
9086 * displayed even when there isn't.
9087 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009088 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009089 dollar_vcol = curwin->w_virtcol;
9090
Bram Moolenaarce3be472008-01-14 19:12:28 +00009091#ifdef FEAT_FOLDING
9092 /* When deleting a char the cursor line must never be in a closed fold.
9093 * E.g., when 'foldmethod' is indent and deleting the first non-white
9094 * char before a Tab. */
9095 if (did_backspace)
9096 foldOpenCursor();
9097#endif
9098
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 return did_backspace;
9100}
9101
9102#ifdef FEAT_MOUSE
9103 static void
9104ins_mouse(c)
9105 int c;
9106{
9107 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009108 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009109
9110# ifdef FEAT_GUI
9111 /* When GUI is active, also move/paste when 'mouse' is empty */
9112 if (!gui.in_use)
9113# endif
9114 if (!mouse_has(MOUSE_INSERT))
9115 return;
9116
9117 undisplay_dollar();
9118 tpos = curwin->w_cursor;
9119 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9120 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009121#ifdef FEAT_WINDOWS
9122 win_T *new_curwin = curwin;
9123
9124 if (curwin != old_curwin && win_valid(old_curwin))
9125 {
9126 /* Mouse took us to another window. We need to go back to the
9127 * previous one to stop insert there properly. */
9128 curwin = old_curwin;
9129 curbuf = curwin->w_buffer;
9130 }
9131#endif
9132 start_arrow(curwin == old_curwin ? &tpos : NULL);
9133#ifdef FEAT_WINDOWS
9134 if (curwin != new_curwin && win_valid(new_curwin))
9135 {
9136 curwin = new_curwin;
9137 curbuf = curwin->w_buffer;
9138 }
9139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009140# ifdef FEAT_CINDENT
9141 can_cindent = TRUE;
9142# endif
9143 }
9144
9145#ifdef FEAT_WINDOWS
9146 /* redraw status lines (in case another window became active) */
9147 redraw_statuslines();
9148#endif
9149}
9150
9151 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009152ins_mousescroll(dir)
9153 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154{
9155 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009156# if defined(FEAT_WINDOWS)
9157 win_T *old_curwin = curwin;
9158# endif
9159# ifdef FEAT_INS_EXPAND
9160 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161# endif
9162
9163 tpos = curwin->w_cursor;
9164
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009165# ifdef FEAT_WINDOWS
9166 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009167 {
9168 int row, col;
9169
9170 row = mouse_row;
9171 col = mouse_col;
9172
9173 /* find the window at the pointer coordinates */
9174 curwin = mouse_find_win(&row, &col);
9175 curbuf = curwin->w_buffer;
9176 }
9177 if (curwin == old_curwin)
9178# endif
9179 undisplay_dollar();
9180
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009181# ifdef FEAT_INS_EXPAND
9182 /* Don't scroll the window in which completion is being done. */
9183 if (!pum_visible()
9184# if defined(FEAT_WINDOWS)
9185 || curwin != old_curwin
9186# endif
9187 )
9188# endif
9189 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009190 if (dir == MSCR_DOWN || dir == MSCR_UP)
9191 {
9192 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9193 scroll_redraw(dir,
9194 (long)(curwin->w_botline - curwin->w_topline));
9195 else
9196 scroll_redraw(dir, 3L);
9197 }
9198#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009199 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009200 {
9201 int val, step = 6;
9202
9203 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9204 step = W_WIDTH(curwin);
9205 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9206 if (val < 0)
9207 val = 0;
9208 gui_do_horiz_scroll(val, TRUE);
9209 }
9210#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009211# ifdef FEAT_INS_EXPAND
9212 did_scroll = TRUE;
9213# endif
9214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009216# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009217 curwin->w_redr_status = TRUE;
9218
9219 curwin = old_curwin;
9220 curbuf = curwin->w_buffer;
9221# endif
9222
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009223# ifdef FEAT_INS_EXPAND
9224 /* The popup menu may overlay the window, need to redraw it.
9225 * TODO: Would be more efficient to only redraw the windows that are
9226 * overlapped by the popup menu. */
9227 if (pum_visible() && did_scroll)
9228 {
9229 redraw_all_later(NOT_VALID);
9230 ins_compl_show_pum();
9231 }
9232# endif
9233
Bram Moolenaar071d4272004-06-13 20:20:40 +00009234 if (!equalpos(curwin->w_cursor, tpos))
9235 {
9236 start_arrow(&tpos);
9237# ifdef FEAT_CINDENT
9238 can_cindent = TRUE;
9239# endif
9240 }
9241}
9242#endif
9243
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009244#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009245 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009246ins_tabline(c)
9247 int c;
9248{
9249 /* We will be leaving the current window, unless closing another tab. */
9250 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9251 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9252 {
9253 undisplay_dollar();
9254 start_arrow(&curwin->w_cursor);
9255# ifdef FEAT_CINDENT
9256 can_cindent = TRUE;
9257# endif
9258 }
9259
9260 if (c == K_TABLINE)
9261 goto_tabpage(current_tab);
9262 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009263 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009264 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009265 redraw_statuslines(); /* will redraw the tabline when needed */
9266 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009267}
9268#endif
9269
9270#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271 void
9272ins_scroll()
9273{
9274 pos_T tpos;
9275
9276 undisplay_dollar();
9277 tpos = curwin->w_cursor;
9278 if (gui_do_scroll())
9279 {
9280 start_arrow(&tpos);
9281# ifdef FEAT_CINDENT
9282 can_cindent = TRUE;
9283# endif
9284 }
9285}
9286
9287 void
9288ins_horscroll()
9289{
9290 pos_T tpos;
9291
9292 undisplay_dollar();
9293 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009294 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 {
9296 start_arrow(&tpos);
9297# ifdef FEAT_CINDENT
9298 can_cindent = TRUE;
9299# endif
9300 }
9301}
9302#endif
9303
9304 static void
9305ins_left()
9306{
9307 pos_T tpos;
9308
9309#ifdef FEAT_FOLDING
9310 if ((fdo_flags & FDO_HOR) && KeyTyped)
9311 foldOpenCursor();
9312#endif
9313 undisplay_dollar();
9314 tpos = curwin->w_cursor;
9315 if (oneleft() == OK)
9316 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009317#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9318 /* Only call start_arrow() when not busy with preediting, it will
9319 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9320 if (!im_is_preediting())
9321#endif
9322 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009323#ifdef FEAT_RIGHTLEFT
9324 /* If exit reversed string, position is fixed */
9325 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9326 revins_legal++;
9327 revins_chars++;
9328#endif
9329 }
9330
9331 /*
9332 * if 'whichwrap' set for cursor in insert mode may go to
9333 * previous line
9334 */
9335 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9336 {
9337 start_arrow(&tpos);
9338 --(curwin->w_cursor.lnum);
9339 coladvance((colnr_T)MAXCOL);
9340 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9341 }
9342 else
9343 vim_beep();
9344}
9345
9346 static void
9347ins_home(c)
9348 int c;
9349{
9350 pos_T tpos;
9351
9352#ifdef FEAT_FOLDING
9353 if ((fdo_flags & FDO_HOR) && KeyTyped)
9354 foldOpenCursor();
9355#endif
9356 undisplay_dollar();
9357 tpos = curwin->w_cursor;
9358 if (c == K_C_HOME)
9359 curwin->w_cursor.lnum = 1;
9360 curwin->w_cursor.col = 0;
9361#ifdef FEAT_VIRTUALEDIT
9362 curwin->w_cursor.coladd = 0;
9363#endif
9364 curwin->w_curswant = 0;
9365 start_arrow(&tpos);
9366}
9367
9368 static void
9369ins_end(c)
9370 int c;
9371{
9372 pos_T tpos;
9373
9374#ifdef FEAT_FOLDING
9375 if ((fdo_flags & FDO_HOR) && KeyTyped)
9376 foldOpenCursor();
9377#endif
9378 undisplay_dollar();
9379 tpos = curwin->w_cursor;
9380 if (c == K_C_END)
9381 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9382 coladvance((colnr_T)MAXCOL);
9383 curwin->w_curswant = MAXCOL;
9384
9385 start_arrow(&tpos);
9386}
9387
9388 static void
9389ins_s_left()
9390{
9391#ifdef FEAT_FOLDING
9392 if ((fdo_flags & FDO_HOR) && KeyTyped)
9393 foldOpenCursor();
9394#endif
9395 undisplay_dollar();
9396 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9397 {
9398 start_arrow(&curwin->w_cursor);
9399 (void)bck_word(1L, FALSE, FALSE);
9400 curwin->w_set_curswant = TRUE;
9401 }
9402 else
9403 vim_beep();
9404}
9405
9406 static void
9407ins_right()
9408{
9409#ifdef FEAT_FOLDING
9410 if ((fdo_flags & FDO_HOR) && KeyTyped)
9411 foldOpenCursor();
9412#endif
9413 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009414 if (gchar_cursor() != NUL
9415#ifdef FEAT_VIRTUALEDIT
9416 || virtual_active()
9417#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009418 )
9419 {
9420 start_arrow(&curwin->w_cursor);
9421 curwin->w_set_curswant = TRUE;
9422#ifdef FEAT_VIRTUALEDIT
9423 if (virtual_active())
9424 oneright();
9425 else
9426#endif
9427 {
9428#ifdef FEAT_MBYTE
9429 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009430 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009431 else
9432#endif
9433 ++curwin->w_cursor.col;
9434 }
9435
9436#ifdef FEAT_RIGHTLEFT
9437 revins_legal++;
9438 if (revins_chars)
9439 revins_chars--;
9440#endif
9441 }
9442 /* if 'whichwrap' set for cursor in insert mode, may move the
9443 * cursor to the next line */
9444 else if (vim_strchr(p_ww, ']') != NULL
9445 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9446 {
9447 start_arrow(&curwin->w_cursor);
9448 curwin->w_set_curswant = TRUE;
9449 ++curwin->w_cursor.lnum;
9450 curwin->w_cursor.col = 0;
9451 }
9452 else
9453 vim_beep();
9454}
9455
9456 static void
9457ins_s_right()
9458{
9459#ifdef FEAT_FOLDING
9460 if ((fdo_flags & FDO_HOR) && KeyTyped)
9461 foldOpenCursor();
9462#endif
9463 undisplay_dollar();
9464 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9465 || gchar_cursor() != NUL)
9466 {
9467 start_arrow(&curwin->w_cursor);
9468 (void)fwd_word(1L, FALSE, 0);
9469 curwin->w_set_curswant = TRUE;
9470 }
9471 else
9472 vim_beep();
9473}
9474
9475 static void
9476ins_up(startcol)
9477 int startcol; /* when TRUE move to Insstart.col */
9478{
9479 pos_T tpos;
9480 linenr_T old_topline = curwin->w_topline;
9481#ifdef FEAT_DIFF
9482 int old_topfill = curwin->w_topfill;
9483#endif
9484
9485 undisplay_dollar();
9486 tpos = curwin->w_cursor;
9487 if (cursor_up(1L, TRUE) == OK)
9488 {
9489 if (startcol)
9490 coladvance(getvcol_nolist(&Insstart));
9491 if (old_topline != curwin->w_topline
9492#ifdef FEAT_DIFF
9493 || old_topfill != curwin->w_topfill
9494#endif
9495 )
9496 redraw_later(VALID);
9497 start_arrow(&tpos);
9498#ifdef FEAT_CINDENT
9499 can_cindent = TRUE;
9500#endif
9501 }
9502 else
9503 vim_beep();
9504}
9505
9506 static void
9507ins_pageup()
9508{
9509 pos_T tpos;
9510
9511 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009512
9513#ifdef FEAT_WINDOWS
9514 if (mod_mask & MOD_MASK_CTRL)
9515 {
9516 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009517 if (first_tabpage->tp_next != NULL)
9518 {
9519 start_arrow(&curwin->w_cursor);
9520 goto_tabpage(-1);
9521 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009522 return;
9523 }
9524#endif
9525
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526 tpos = curwin->w_cursor;
9527 if (onepage(BACKWARD, 1L) == OK)
9528 {
9529 start_arrow(&tpos);
9530#ifdef FEAT_CINDENT
9531 can_cindent = TRUE;
9532#endif
9533 }
9534 else
9535 vim_beep();
9536}
9537
9538 static void
9539ins_down(startcol)
9540 int startcol; /* when TRUE move to Insstart.col */
9541{
9542 pos_T tpos;
9543 linenr_T old_topline = curwin->w_topline;
9544#ifdef FEAT_DIFF
9545 int old_topfill = curwin->w_topfill;
9546#endif
9547
9548 undisplay_dollar();
9549 tpos = curwin->w_cursor;
9550 if (cursor_down(1L, TRUE) == OK)
9551 {
9552 if (startcol)
9553 coladvance(getvcol_nolist(&Insstart));
9554 if (old_topline != curwin->w_topline
9555#ifdef FEAT_DIFF
9556 || old_topfill != curwin->w_topfill
9557#endif
9558 )
9559 redraw_later(VALID);
9560 start_arrow(&tpos);
9561#ifdef FEAT_CINDENT
9562 can_cindent = TRUE;
9563#endif
9564 }
9565 else
9566 vim_beep();
9567}
9568
9569 static void
9570ins_pagedown()
9571{
9572 pos_T tpos;
9573
9574 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009575
9576#ifdef FEAT_WINDOWS
9577 if (mod_mask & MOD_MASK_CTRL)
9578 {
9579 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009580 if (first_tabpage->tp_next != NULL)
9581 {
9582 start_arrow(&curwin->w_cursor);
9583 goto_tabpage(0);
9584 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009585 return;
9586 }
9587#endif
9588
Bram Moolenaar071d4272004-06-13 20:20:40 +00009589 tpos = curwin->w_cursor;
9590 if (onepage(FORWARD, 1L) == OK)
9591 {
9592 start_arrow(&tpos);
9593#ifdef FEAT_CINDENT
9594 can_cindent = TRUE;
9595#endif
9596 }
9597 else
9598 vim_beep();
9599}
9600
9601#ifdef FEAT_DND
9602 static void
9603ins_drop()
9604{
9605 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9606}
9607#endif
9608
9609/*
9610 * Handle TAB in Insert or Replace mode.
9611 * Return TRUE when the TAB needs to be inserted like a normal character.
9612 */
9613 static int
9614ins_tab()
9615{
9616 int ind;
9617 int i;
9618 int temp;
9619
9620 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9621 Insstart_blank_vcol = get_nolist_virtcol();
9622 if (echeck_abbr(TAB + ABBR_OFF))
9623 return FALSE;
9624
9625 ind = inindent(0);
9626#ifdef FEAT_CINDENT
9627 if (ind)
9628 can_cindent = FALSE;
9629#endif
9630
9631 /*
9632 * When nothing special, insert TAB like a normal character
9633 */
9634 if (!curbuf->b_p_et
Bram Moolenaar14f24742012-08-08 18:01:05 +02009635 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value())
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009636 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637 return TRUE;
9638
9639 if (stop_arrow() == FAIL)
9640 return TRUE;
9641
9642 did_ai = FALSE;
9643#ifdef FEAT_SMARTINDENT
9644 did_si = FALSE;
9645 can_si = FALSE;
9646 can_si_back = FALSE;
9647#endif
9648 AppendToRedobuff((char_u *)"\t");
9649
9650 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar14f24742012-08-08 18:01:05 +02009651 temp = (int)get_sw_value();
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009652 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9653 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 else /* otherwise use 'tabstop' */
9655 temp = (int)curbuf->b_p_ts;
9656 temp -= get_nolist_virtcol() % temp;
9657
9658 /*
9659 * Insert the first space with ins_char(). It will delete one char in
9660 * replace mode. Insert the rest with ins_str(); it will not delete any
9661 * chars. For VREPLACE mode, we use ins_char() for all characters.
9662 */
9663 ins_char(' ');
9664 while (--temp > 0)
9665 {
9666#ifdef FEAT_VREPLACE
9667 if (State & VREPLACE_FLAG)
9668 ins_char(' ');
9669 else
9670#endif
9671 {
9672 ins_str((char_u *)" ");
9673 if (State & REPLACE_FLAG) /* no char replaced */
9674 replace_push(NUL);
9675 }
9676 }
9677
9678 /*
9679 * When 'expandtab' not set: Replace spaces by TABs where possible.
9680 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009681 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 {
9683 char_u *ptr;
9684#ifdef FEAT_VREPLACE
9685 char_u *saved_line = NULL; /* init for GCC */
9686 pos_T pos;
9687#endif
9688 pos_T fpos;
9689 pos_T *cursor;
9690 colnr_T want_vcol, vcol;
9691 int change_col = -1;
9692 int save_list = curwin->w_p_list;
9693
9694 /*
9695 * Get the current line. For VREPLACE mode, don't make real changes
9696 * yet, just work on a copy of the line.
9697 */
9698#ifdef FEAT_VREPLACE
9699 if (State & VREPLACE_FLAG)
9700 {
9701 pos = curwin->w_cursor;
9702 cursor = &pos;
9703 saved_line = vim_strsave(ml_get_curline());
9704 if (saved_line == NULL)
9705 return FALSE;
9706 ptr = saved_line + pos.col;
9707 }
9708 else
9709#endif
9710 {
9711 ptr = ml_get_cursor();
9712 cursor = &curwin->w_cursor;
9713 }
9714
9715 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9716 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9717 curwin->w_p_list = FALSE;
9718
9719 /* Find first white before the cursor */
9720 fpos = curwin->w_cursor;
9721 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9722 {
9723 --fpos.col;
9724 --ptr;
9725 }
9726
9727 /* In Replace mode, don't change characters before the insert point. */
9728 if ((State & REPLACE_FLAG)
9729 && fpos.lnum == Insstart.lnum
9730 && fpos.col < Insstart.col)
9731 {
9732 ptr += Insstart.col - fpos.col;
9733 fpos.col = Insstart.col;
9734 }
9735
9736 /* compute virtual column numbers of first white and cursor */
9737 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9738 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9739
9740 /* Use as many TABs as possible. Beware of 'showbreak' and
9741 * 'linebreak' adding extra virtual columns. */
9742 while (vim_iswhite(*ptr))
9743 {
9744 i = lbr_chartabsize((char_u *)"\t", vcol);
9745 if (vcol + i > want_vcol)
9746 break;
9747 if (*ptr != TAB)
9748 {
9749 *ptr = TAB;
9750 if (change_col < 0)
9751 {
9752 change_col = fpos.col; /* Column of first change */
9753 /* May have to adjust Insstart */
9754 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9755 Insstart.col = fpos.col;
9756 }
9757 }
9758 ++fpos.col;
9759 ++ptr;
9760 vcol += i;
9761 }
9762
9763 if (change_col >= 0)
9764 {
9765 int repl_off = 0;
9766
9767 /* Skip over the spaces we need. */
9768 while (vcol < want_vcol && *ptr == ' ')
9769 {
9770 vcol += lbr_chartabsize(ptr, vcol);
9771 ++ptr;
9772 ++repl_off;
9773 }
9774 if (vcol > want_vcol)
9775 {
9776 /* Must have a char with 'showbreak' just before it. */
9777 --ptr;
9778 --repl_off;
9779 }
9780 fpos.col += repl_off;
9781
9782 /* Delete following spaces. */
9783 i = cursor->col - fpos.col;
9784 if (i > 0)
9785 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009786 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787 /* correct replace stack. */
9788 if ((State & REPLACE_FLAG)
9789#ifdef FEAT_VREPLACE
9790 && !(State & VREPLACE_FLAG)
9791#endif
9792 )
9793 for (temp = i; --temp >= 0; )
9794 replace_join(repl_off);
9795 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009796#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009797 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009798 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009799 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009800 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9801 (char_u *)"\t", 1);
9802 }
9803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009804 cursor->col -= i;
9805
9806#ifdef FEAT_VREPLACE
9807 /*
9808 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9809 * backspacing over the changed spacing and then inserting the new
9810 * spacing.
9811 */
9812 if (State & VREPLACE_FLAG)
9813 {
9814 /* Backspace from real cursor to change_col */
9815 backspace_until_column(change_col);
9816
9817 /* Insert each char in saved_line from changed_col to
9818 * ptr-cursor */
9819 ins_bytes_len(saved_line + change_col,
9820 cursor->col - change_col);
9821 }
9822#endif
9823 }
9824
9825#ifdef FEAT_VREPLACE
9826 if (State & VREPLACE_FLAG)
9827 vim_free(saved_line);
9828#endif
9829 curwin->w_p_list = save_list;
9830 }
9831
9832 return FALSE;
9833}
9834
9835/*
9836 * Handle CR or NL in insert mode.
9837 * Return TRUE when out of memory or can't undo.
9838 */
9839 static int
9840ins_eol(c)
9841 int c;
9842{
9843 int i;
9844
9845 if (echeck_abbr(c + ABBR_OFF))
9846 return FALSE;
9847 if (stop_arrow() == FAIL)
9848 return TRUE;
9849 undisplay_dollar();
9850
9851 /*
9852 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9853 * character under the cursor. Only push a NUL on the replace stack,
9854 * nothing to put back when the NL is deleted.
9855 */
9856 if ((State & REPLACE_FLAG)
9857#ifdef FEAT_VREPLACE
9858 && !(State & VREPLACE_FLAG)
9859#endif
9860 )
9861 replace_push(NUL);
9862
9863 /*
9864 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9865 * replacing the next line, so we push all of the characters left on the
9866 * line onto the replace stack. This is not done here though, it is done
9867 * in open_line().
9868 */
9869
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009870#ifdef FEAT_VIRTUALEDIT
9871 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9872 * CTRL-O). */
9873 if (virtual_active() && curwin->w_cursor.coladd > 0)
9874 coladvance(getviscol());
9875#endif
9876
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877#ifdef FEAT_RIGHTLEFT
9878# ifdef FEAT_FKMAP
9879 if (p_altkeymap && p_fkmap)
9880 fkmap(NL);
9881# endif
9882 /* NL in reverse insert will always start in the end of
9883 * current line. */
9884 if (revins_on)
9885 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9886#endif
9887
9888 AppendToRedobuff(NL_STR);
9889 i = open_line(FORWARD,
9890#ifdef FEAT_COMMENTS
9891 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9892#endif
9893 0, old_indent);
9894 old_indent = 0;
9895#ifdef FEAT_CINDENT
9896 can_cindent = TRUE;
9897#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009898#ifdef FEAT_FOLDING
9899 /* When inserting a line the cursor line must never be in a closed fold. */
9900 foldOpenCursor();
9901#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902
9903 return (!i);
9904}
9905
9906#ifdef FEAT_DIGRAPHS
9907/*
9908 * Handle digraph in insert mode.
9909 * Returns character still to be inserted, or NUL when nothing remaining to be
9910 * done.
9911 */
9912 static int
9913ins_digraph()
9914{
9915 int c;
9916 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009917 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918
9919 pc_status = PC_STATUS_UNSET;
9920 if (redrawing() && !char_avail())
9921 {
9922 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009923 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009924
9925 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009926 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009927#ifdef FEAT_CMDL_INFO
9928 add_to_showcmd_c(Ctrl_K);
9929#endif
9930 }
9931
9932#ifdef USE_ON_FLY_SCROLL
9933 dont_scroll = TRUE; /* disallow scrolling here */
9934#endif
9935
9936 /* don't map the digraph chars. This also prevents the
9937 * mode message to be deleted when ESC is hit */
9938 ++no_mapping;
9939 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009940 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009941 --no_mapping;
9942 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009943 if (did_putchar)
9944 /* when the line fits in 'columns' the '?' is at the start of the next
9945 * line and will not be removed by the redraw */
9946 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009947
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948 if (IS_SPECIAL(c) || mod_mask) /* special key */
9949 {
9950#ifdef FEAT_CMDL_INFO
9951 clear_showcmd();
9952#endif
9953 insert_special(c, TRUE, FALSE);
9954 return NUL;
9955 }
9956 if (c != ESC)
9957 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009958 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009959 if (redrawing() && !char_avail())
9960 {
9961 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009962 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009963
9964 if (char2cells(c) == 1)
9965 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009966 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009967 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009968 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 }
9970#ifdef FEAT_CMDL_INFO
9971 add_to_showcmd_c(c);
9972#endif
9973 }
9974 ++no_mapping;
9975 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009976 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009977 --no_mapping;
9978 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009979 if (did_putchar)
9980 /* when the line fits in 'columns' the '?' is at the start of the
9981 * next line and will not be removed by a redraw */
9982 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009983 if (cc != ESC)
9984 {
9985 AppendToRedobuff((char_u *)CTRL_V_STR);
9986 c = getdigraph(c, cc, TRUE);
9987#ifdef FEAT_CMDL_INFO
9988 clear_showcmd();
9989#endif
9990 return c;
9991 }
9992 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009993#ifdef FEAT_CMDL_INFO
9994 clear_showcmd();
9995#endif
9996 return NUL;
9997}
9998#endif
9999
10000/*
10001 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10002 * Returns the char to be inserted, or NUL if none found.
10003 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010004 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000010005ins_copychar(lnum)
10006 linenr_T lnum;
10007{
10008 int c;
10009 int temp;
10010 char_u *ptr, *prev_ptr;
10011
10012 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10013 {
10014 vim_beep();
10015 return NUL;
10016 }
10017
10018 /* try to advance to the cursor column */
10019 temp = 0;
10020 ptr = ml_get(lnum);
10021 prev_ptr = ptr;
10022 validate_virtcol();
10023 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10024 {
10025 prev_ptr = ptr;
10026 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
10027 }
10028 if ((colnr_T)temp > curwin->w_virtcol)
10029 ptr = prev_ptr;
10030
10031#ifdef FEAT_MBYTE
10032 c = (*mb_ptr2char)(ptr);
10033#else
10034 c = *ptr;
10035#endif
10036 if (c == NUL)
10037 vim_beep();
10038 return c;
10039}
10040
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010041/*
10042 * CTRL-Y or CTRL-E typed in Insert mode.
10043 */
10044 static int
10045ins_ctrl_ey(tc)
10046 int tc;
10047{
10048 int c = tc;
10049
10050#ifdef FEAT_INS_EXPAND
10051 if (ctrl_x_mode == CTRL_X_SCROLL)
10052 {
10053 if (c == Ctrl_Y)
10054 scrolldown_clamp();
10055 else
10056 scrollup_clamp();
10057 redraw_later(VALID);
10058 }
10059 else
10060#endif
10061 {
10062 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10063 if (c != NUL)
10064 {
10065 long tw_save;
10066
10067 /* The character must be taken literally, insert like it
10068 * was typed after a CTRL-V, and pretend 'textwidth'
10069 * wasn't set. Digits, 'o' and 'x' are special after a
10070 * CTRL-V, don't use it for these. */
10071 if (c < 256 && !isalnum(c))
10072 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10073 tw_save = curbuf->b_p_tw;
10074 curbuf->b_p_tw = -1;
10075 insert_special(c, TRUE, FALSE);
10076 curbuf->b_p_tw = tw_save;
10077#ifdef FEAT_RIGHTLEFT
10078 revins_chars++;
10079 revins_legal++;
10080#endif
10081 c = Ctrl_V; /* pretend CTRL-V is last character */
10082 auto_format(FALSE, TRUE);
10083 }
10084 }
10085 return c;
10086}
10087
Bram Moolenaar071d4272004-06-13 20:20:40 +000010088#ifdef FEAT_SMARTINDENT
10089/*
10090 * Try to do some very smart auto-indenting.
10091 * Used when inserting a "normal" character.
10092 */
10093 static void
10094ins_try_si(c)
10095 int c;
10096{
10097 pos_T *pos, old_pos;
10098 char_u *ptr;
10099 int i;
10100 int temp;
10101
10102 /*
10103 * do some very smart indenting when entering '{' or '}'
10104 */
10105 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10106 {
10107 /*
10108 * for '}' set indent equal to indent of line containing matching '{'
10109 */
10110 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10111 {
10112 old_pos = curwin->w_cursor;
10113 /*
10114 * If the matching '{' has a ')' immediately before it (ignoring
10115 * white-space), then line up with the start of the line
10116 * containing the matching '(' if there is one. This handles the
10117 * case where an "if (..\n..) {" statement continues over multiple
10118 * lines -- webb
10119 */
10120 ptr = ml_get(pos->lnum);
10121 i = pos->col;
10122 if (i > 0) /* skip blanks before '{' */
10123 while (--i > 0 && vim_iswhite(ptr[i]))
10124 ;
10125 curwin->w_cursor.lnum = pos->lnum;
10126 curwin->w_cursor.col = i;
10127 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10128 curwin->w_cursor = *pos;
10129 i = get_indent();
10130 curwin->w_cursor = old_pos;
10131#ifdef FEAT_VREPLACE
10132 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010133 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010134 else
10135#endif
10136 (void)set_indent(i, SIN_CHANGED);
10137 }
10138 else if (curwin->w_cursor.col > 0)
10139 {
10140 /*
10141 * when inserting '{' after "O" reduce indent, but not
10142 * more than indent of previous line
10143 */
10144 temp = TRUE;
10145 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10146 {
10147 old_pos = curwin->w_cursor;
10148 i = get_indent();
10149 while (curwin->w_cursor.lnum > 1)
10150 {
10151 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10152
10153 /* ignore empty lines and lines starting with '#'. */
10154 if (*ptr != '#' && *ptr != NUL)
10155 break;
10156 }
10157 if (get_indent() >= i)
10158 temp = FALSE;
10159 curwin->w_cursor = old_pos;
10160 }
10161 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010162 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010163 }
10164 }
10165
10166 /*
10167 * set indent of '#' always to 0
10168 */
10169 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10170 {
10171 /* remember current indent for next line */
10172 old_indent = get_indent();
10173 (void)set_indent(0, SIN_CHANGED);
10174 }
10175
10176 /* Adjust ai_col, the char at this position can be deleted. */
10177 if (ai_col > curwin->w_cursor.col)
10178 ai_col = curwin->w_cursor.col;
10179}
10180#endif
10181
10182/*
10183 * Get the value that w_virtcol would have when 'list' is off.
10184 * Unless 'cpo' contains the 'L' flag.
10185 */
10186 static colnr_T
10187get_nolist_virtcol()
10188{
10189 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10190 return getvcol_nolist(&curwin->w_cursor);
10191 validate_virtcol();
10192 return curwin->w_virtcol;
10193}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010194
10195#ifdef FEAT_AUTOCMD
10196/*
10197 * Handle the InsertCharPre autocommand.
10198 * "c" is the character that was typed.
10199 * Return a pointer to allocated memory with the replacement string.
10200 * Return NULL to continue inserting "c".
10201 */
10202 static char_u *
10203do_insert_char_pre(c)
10204 int c;
10205{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010206 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010207 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010208
10209 /* Return quickly when there is nothing to do. */
10210 if (!has_insertcharpre())
10211 return NULL;
10212
Bram Moolenaar704984a2012-06-01 14:57:51 +020010213#ifdef FEAT_MBYTE
10214 if (has_mbyte)
10215 buf[(*mb_char2bytes)(c, buf)] = NUL;
10216 else
10217#endif
10218 {
10219 buf[0] = c;
10220 buf[1] = NUL;
10221 }
10222
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010223 /* Lock the text to avoid weird things from happening. */
10224 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010225 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010226
Bram Moolenaar704984a2012-06-01 14:57:51 +020010227 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010228 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010229 {
10230 /* Get the value of v:char. It may be empty or more than one
10231 * character. Only use it when changed, otherwise continue with the
10232 * original character to avoid breaking autoindent. */
10233 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10234 res = vim_strsave(get_vim_var_str(VV_CHAR));
10235 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010236
10237 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10238 --textlock;
10239
10240 return res;
10241}
10242#endif