blob: 94ec789769b0cb6a31ffa7f5795e780f243eb1aa [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000056 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000060static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010061#ifdef FEAT_COMPL_FUNC
62static char e_complwin[] = N_("E839: Completion function changed window");
63static char e_compldel[] = N_("E840: Completion function deleted text");
64#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66/*
67 * Structure used to store one match for insert completion.
68 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000069typedef struct compl_S compl_T;
70struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
Bram Moolenaar572cb562005-08-05 21:35:02 +000072 compl_T *cp_next;
73 compl_T *cp_prev;
74 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000075 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000076 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000077 char_u *cp_fname; /* file containing the match, allocated when
78 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000079 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
80 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081};
82
Bram Moolenaar572cb562005-08-05 21:35:02 +000083#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#define FREE_FNAME (2)
85
86/*
87 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000088 * "compl_first_match" points to the start of the list.
89 * "compl_curr_match" points to the currently selected entry.
90 * "compl_shown_match" is different from compl_curr_match during
91 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000093static compl_T *compl_first_match = NULL;
94static compl_T *compl_curr_match = NULL;
95static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar779b74b2006-04-10 14:55:34 +000097/* After using a cursor key <Enter> selects a match in the popup menu,
98 * otherwise it inserts a line break. */
99static int compl_enter_selects = FALSE;
100
Bram Moolenaara6557602006-02-04 22:43:20 +0000101/* When "compl_leader" is not NULL only matches that start with this string
102 * are used. */
103static char_u *compl_leader = NULL;
104
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000105static int compl_get_longest = FALSE; /* put longest common string
106 in compl_leader */
107
Bram Moolenaara6557602006-02-04 22:43:20 +0000108static int compl_used_match; /* Selected one of the matches. When
109 FALSE the match was edited or using
110 the longest common string. */
111
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000112static int compl_was_interrupted = FALSE; /* didn't finish finding
113 completions. */
114
115static int compl_restarting = FALSE; /* don't insert match */
116
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000117/* When the first completion is done "compl_started" is set. When it's
118 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000119static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000120
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000121/* Set when doing something for completion that may call edit() recursively,
122 * which is not allowed. */
123static int compl_busy = FALSE;
124
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static int compl_matches = 0;
126static char_u *compl_pattern = NULL;
127static int compl_direction = FORWARD;
128static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000129static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000130static pos_T compl_startpos;
131static colnr_T compl_col = 0; /* column where the text starts
132 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static char_u *compl_orig_text = NULL; /* text as it was before
134 * completion started */
135static int compl_cont_mode = 0;
136static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaar82139082011-09-14 16:52:09 +0200138static int compl_opt_refresh_always = FALSE;
139
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000140static void ins_ctrl_x __ARGS((void));
141static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000142static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000143static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000144static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000145static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000146static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000148static void ins_compl_upd_pum __ARGS((void));
149static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000150static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000151static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000152static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000153static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000154static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static void ins_compl_free __ARGS((void));
156static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000157static int ins_compl_bs __ARGS((void));
Bram Moolenaar82139082011-09-14 16:52:09 +0200158static int ins_compl_need_restart __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000159static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000160static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar82139082011-09-14 16:52:09 +0200161static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000162static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000163static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000164static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000165static int ins_compl_prep __ARGS((int c));
Bram Moolenaar62951b12011-09-21 18:23:05 +0200166static void ins_compl_fixRedoBufForLeader __ARGS((char_u *ptr_arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000168#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
169static void ins_compl_add_list __ARGS((list_T *list));
Bram Moolenaar82139082011-09-14 16:52:09 +0200170static void ins_compl_add_dict __ARGS((dict_T *dict));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000171#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000172static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173static void ins_compl_delete __ARGS((void));
174static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000175static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000176static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000177static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000178static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000179static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000181static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#endif /* FEAT_INS_EXPAND */
183
184#define BACKSPACE_CHAR 1
185#define BACKSPACE_WORD 2
186#define BACKSPACE_WORD_NOT_SPACE 3
187#define BACKSPACE_LINE 4
188
Bram Moolenaar754b5602006-02-09 23:53:20 +0000189static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void ins_ctrl_v __ARGS((void));
191static void undisplay_dollar __ARGS((void));
192static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000193static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194static void check_auto_format __ARGS((int));
195static void redo_literal __ARGS((int c));
196static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000197#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000198static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000199static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000200static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000201#endif
Bram Moolenaarf332a652013-11-04 04:20:33 +0100202static void stop_insert __ARGS((pos_T *end_insert_pos, int esc, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203static int echeck_abbr __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204static int replace_pop __ARGS((void));
205static void replace_join __ARGS((int off));
206static void replace_pop_ins __ARGS((void));
207#ifdef FEAT_MBYTE
208static void mb_replace_pop_ins __ARGS((int cc));
209#endif
210static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000211static void replace_do_bs __ARGS((int limit_col));
212static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifdef FEAT_CINDENT
214static int cindent_on __ARGS((void));
215#endif
216static void ins_reg __ARGS((void));
217static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000218static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000219static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#ifdef FEAT_RIGHTLEFT
221static void ins_ctrl_ __ARGS((void));
222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223static int ins_start_select __ARGS((int c));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000224static void ins_insert __ARGS((int replaceState));
225static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226static void ins_shift __ARGS((int c, int lastc));
227static void ins_del __ARGS((void));
228static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
229#ifdef FEAT_MOUSE
230static void ins_mouse __ARGS((int c));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200231static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000233#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
234static void ins_tabline __ARGS((int c));
235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236static void ins_left __ARGS((void));
237static void ins_home __ARGS((int c));
238static void ins_end __ARGS((int c));
239static void ins_s_left __ARGS((void));
240static void ins_right __ARGS((void));
241static void ins_s_right __ARGS((void));
242static void ins_up __ARGS((int startcol));
243static void ins_pageup __ARGS((void));
244static void ins_down __ARGS((int startcol));
245static void ins_pagedown __ARGS((void));
246#ifdef FEAT_DND
247static void ins_drop __ARGS((void));
248#endif
249static int ins_tab __ARGS((void));
250static int ins_eol __ARGS((int c));
251#ifdef FEAT_DIGRAPHS
252static int ins_digraph __ARGS((void));
253#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000254static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000255#ifdef FEAT_SMARTINDENT
256static void ins_try_si __ARGS((int c));
257#endif
258static colnr_T get_nolist_virtcol __ARGS((void));
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100259#ifdef FEAT_AUTOCMD
260static char_u *do_insert_char_pre __ARGS((int c));
261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000262
263static colnr_T Insstart_textlen; /* length of line when insert started */
264static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100265static int update_Insstart_orig = TRUE; /* set Insstart_orig to Insstart */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
267static char_u *last_insert = NULL; /* the text of the previous insert,
268 K_SPECIAL and CSI are escaped */
269static int last_insert_skip; /* nr of chars in front of previous insert */
270static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000271static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000272
273#ifdef FEAT_CINDENT
274static int can_cindent; /* may do cindenting on this line */
275#endif
276
277static int old_indent = 0; /* for ^^D command in insert mode */
278
279#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000280static int revins_on; /* reverse insert mode on */
281static int revins_chars; /* how much to skip after edit */
282static int revins_legal; /* was the last char 'legal'? */
283static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000284#endif
285
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286static int ins_need_undo; /* call u_save() before inserting a
287 char. Set when edit() is called.
288 after that arrow_used is used. */
289
290static int did_add_space = FALSE; /* auto_format() added an extra space
291 under the cursor */
292
293/*
294 * edit(): Start inserting text.
295 *
296 * "cmdchar" can be:
297 * 'i' normal insert command
298 * 'a' normal append command
299 * 'R' replace command
300 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
301 * but still only one <CR> is inserted. The <Esc> is not used for redo.
302 * 'g' "gI" command.
303 * 'V' "gR" command for Virtual Replace mode.
304 * 'v' "gr" command for single character Virtual Replace mode.
305 *
306 * This function is not called recursively. For CTRL-O commands, it returns
307 * and lets the caller handle the Normal-mode command.
308 *
309 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
310 */
311 int
312edit(cmdchar, startln, count)
313 int cmdchar;
314 int startln; /* if set, insert at start of line */
315 long count;
316{
317 int c = 0;
318 char_u *ptr;
319 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000320 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000321 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 int i;
323 int did_backspace = TRUE; /* previous char was backspace */
324#ifdef FEAT_CINDENT
325 int line_is_white = FALSE; /* line is empty before insert */
326#endif
327 linenr_T old_topline = 0; /* topline before insertion */
328#ifdef FEAT_DIFF
329 int old_topfill = -1;
330#endif
331 int inserted_space = FALSE; /* just inserted a space */
332 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000333 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000334
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000335 /* Remember whether editing was restarted after CTRL-O. */
336 did_restart_edit = restart_edit;
337
Bram Moolenaar071d4272004-06-13 20:20:40 +0000338 /* sleep before redrawing, needed for "CTRL-O :" that results in an
339 * error message */
340 check_for_delay(TRUE);
341
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100342 /* set Insstart_orig to Insstart */
343 update_Insstart_orig = TRUE;
344
Bram Moolenaar071d4272004-06-13 20:20:40 +0000345#ifdef HAVE_SANDBOX
346 /* Don't allow inserting in the sandbox. */
347 if (sandbox != 0)
348 {
349 EMSG(_(e_sandbox));
350 return FALSE;
351 }
352#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000353 /* Don't allow changes in the buffer while editing the cmdline. The
354 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000355 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000356 {
357 EMSG(_(e_secure));
358 return FALSE;
359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000360
361#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000362 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000363 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000364 {
365 EMSG(_(e_secure));
366 return FALSE;
367 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000368 ins_compl_clear(); /* clear stuff for CTRL-X mode */
369#endif
370
Bram Moolenaar843ee412004-06-30 16:16:41 +0000371#ifdef FEAT_AUTOCMD
372 /*
373 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
374 */
375 if (cmdchar != 'r' && cmdchar != 'v')
376 {
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100377 pos_T save_cursor = curwin->w_cursor;
378
Bram Moolenaar1e015462005-09-25 22:16:38 +0000379# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000380 if (cmdchar == 'R')
381 ptr = (char_u *)"r";
382 else if (cmdchar == 'V')
383 ptr = (char_u *)"v";
384 else
385 ptr = (char_u *)"i";
386 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar097c9922013-05-19 21:15:15 +0200387 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
Bram Moolenaar1e015462005-09-25 22:16:38 +0000388# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000389 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100390
Bram Moolenaar097c9922013-05-19 21:15:15 +0200391 /* Make sure the cursor didn't move. Do call check_cursor_col() in
392 * case the text was modified. Since Insert mode was not started yet
393 * a call to check_cursor_col() may move the cursor, especially with
394 * the "A" command, thus set State to avoid that. Also check that the
395 * line number is still valid (lines may have been deleted).
396 * Do not restore if v:char was set to a non-empty string. */
397 if (!equalpos(curwin->w_cursor, save_cursor)
398# ifdef FEAT_EVAL
399 && *get_vim_var_str(VV_CHAR) == NUL
400# endif
401 && save_cursor.lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar3e37fd02013-01-17 15:37:01 +0100402 {
403 int save_state = State;
404
405 curwin->w_cursor = save_cursor;
406 State = INSERT;
407 check_cursor_col();
408 State = save_state;
409 }
Bram Moolenaar843ee412004-06-30 16:16:41 +0000410 }
411#endif
412
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200413#ifdef FEAT_CONCEAL
414 /* Check if the cursor line needs redrawing before changing State. If
415 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200416 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200417#endif
418
Bram Moolenaar071d4272004-06-13 20:20:40 +0000419#ifdef FEAT_MOUSE
420 /*
421 * When doing a paste with the middle mouse button, Insstart is set to
422 * where the paste started.
423 */
424 if (where_paste_started.lnum != 0)
425 Insstart = where_paste_started;
426 else
427#endif
428 {
429 Insstart = curwin->w_cursor;
430 if (startln)
431 Insstart.col = 0;
432 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000433 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000434 Insstart_blank_vcol = MAXCOL;
435 if (!did_ai)
436 ai_col = 0;
437
438 if (cmdchar != NUL && restart_edit == 0)
439 {
440 ResetRedobuff();
441 AppendNumberToRedobuff(count);
442#ifdef FEAT_VREPLACE
443 if (cmdchar == 'V' || cmdchar == 'v')
444 {
445 /* "gR" or "gr" command */
446 AppendCharToRedobuff('g');
447 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
448 }
449 else
450#endif
451 {
452 AppendCharToRedobuff(cmdchar);
453 if (cmdchar == 'g') /* "gI" command */
454 AppendCharToRedobuff('I');
455 else if (cmdchar == 'r') /* "r<CR>" command */
456 count = 1; /* insert only one <CR> */
457 }
458 }
459
460 if (cmdchar == 'R')
461 {
462#ifdef FEAT_FKMAP
463 if (p_fkmap && p_ri)
464 {
465 beep_flush();
466 EMSG(farsi_text_3); /* encoded in Farsi */
467 State = INSERT;
468 }
469 else
470#endif
471 State = REPLACE;
472 }
473#ifdef FEAT_VREPLACE
474 else if (cmdchar == 'V' || cmdchar == 'v')
475 {
476 State = VREPLACE;
477 replaceState = VREPLACE;
478 orig_line_count = curbuf->b_ml.ml_line_count;
479 vr_lines_changed = 1;
480 }
481#endif
482 else
483 State = INSERT;
484
485 stop_insert_mode = FALSE;
486
487 /*
488 * Need to recompute the cursor position, it might move when the cursor is
489 * on a TAB or special character.
490 */
491 curs_columns(TRUE);
492
493 /*
494 * Enable langmap or IME, indicated by 'iminsert'.
495 * Note that IME may enabled/disabled without us noticing here, thus the
496 * 'iminsert' value may not reflect what is actually used. It is updated
497 * when hitting <Esc>.
498 */
499 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
500 State |= LANGMAP;
501#ifdef USE_IM_CONTROL
502 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
503#endif
504
Bram Moolenaar071d4272004-06-13 20:20:40 +0000505#ifdef FEAT_MOUSE
506 setmouse();
507#endif
508#ifdef FEAT_CMDL_INFO
509 clear_showcmd();
510#endif
511#ifdef FEAT_RIGHTLEFT
512 /* there is no reverse replace mode */
513 revins_on = (State == INSERT && p_ri);
514 if (revins_on)
515 undisplay_dollar();
516 revins_chars = 0;
517 revins_legal = 0;
518 revins_scol = -1;
519#endif
520
521 /*
522 * Handle restarting Insert mode.
523 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
524 * restart_edit non-zero, and something in the stuff buffer.
525 */
526 if (restart_edit != 0 && stuff_empty())
527 {
528#ifdef FEAT_MOUSE
529 /*
530 * After a paste we consider text typed to be part of the insert for
531 * the pasted text. You can backspace over the pasted text too.
532 */
533 if (where_paste_started.lnum)
534 arrow_used = FALSE;
535 else
536#endif
537 arrow_used = TRUE;
538 restart_edit = 0;
539
540 /*
541 * If the cursor was after the end-of-line before the CTRL-O and it is
542 * now at the end-of-line, put it after the end-of-line (this is not
543 * correct in very rare cases).
544 * Also do this if curswant is greater than the current virtual
545 * column. Eg after "^O$" or "^O80|".
546 */
547 validate_virtcol();
548 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000549 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000550 || curwin->w_curswant > curwin->w_virtcol)
551 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
552 {
553 if (ptr[1] == NUL)
554 ++curwin->w_cursor.col;
555#ifdef FEAT_MBYTE
556 else if (has_mbyte)
557 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000558 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000559 if (ptr[i] == NUL)
560 curwin->w_cursor.col += i;
561 }
562#endif
563 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000564 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000565 }
566 else
567 arrow_used = FALSE;
568
569 /* we are in insert mode now, don't need to start it anymore */
570 need_start_insertmode = FALSE;
571
572 /* Need to save the line for undo before inserting the first char. */
573 ins_need_undo = TRUE;
574
575#ifdef FEAT_MOUSE
576 where_paste_started.lnum = 0;
577#endif
578#ifdef FEAT_CINDENT
579 can_cindent = TRUE;
580#endif
581#ifdef FEAT_FOLDING
582 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
583 * restarting. */
584 if (!p_im && did_restart_edit == 0)
585 foldOpenCursor();
586#endif
587
588 /*
589 * If 'showmode' is set, show the current (insert/replace/..) mode.
590 * A warning message for changing a readonly file is given here, before
591 * actually changing anything. It's put after the mode, if any.
592 */
593 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000594 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000595 i = showmode();
596
597 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000598 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000599
600#ifdef CURSOR_SHAPE
601 ui_cursor_shape(); /* may show different cursor shape */
602#endif
603#ifdef FEAT_DIGRAPHS
604 do_digraph(-1); /* clear digraphs */
605#endif
606
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000607 /*
608 * Get the current length of the redo buffer, those characters have to be
609 * skipped if we want to get to the inserted characters.
610 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000611 ptr = get_inserted();
612 if (ptr == NULL)
613 new_insert_skip = 0;
614 else
615 {
616 new_insert_skip = (int)STRLEN(ptr);
617 vim_free(ptr);
618 }
619
620 old_indent = 0;
621
622 /*
623 * Main loop in Insert mode: repeat until Insert mode is left.
624 */
625 for (;;)
626 {
627#ifdef FEAT_RIGHTLEFT
628 if (!revins_legal)
629 revins_scol = -1; /* reset on illegal motions */
630 else
631 revins_legal = 0;
632#endif
633 if (arrow_used) /* don't repeat insert when arrow key used */
634 count = 0;
635
Bram Moolenaarb1d90a32014-02-22 23:03:55 +0100636 if (update_Insstart_orig)
637 Insstart_orig = Insstart;
638
Bram Moolenaar071d4272004-06-13 20:20:40 +0000639 if (stop_insert_mode)
640 {
641 /* ":stopinsert" used or 'insertmode' reset */
642 count = 0;
643 goto doESCkey;
644 }
645
646 /* set curwin->w_curswant for next K_DOWN or K_UP */
647 if (!arrow_used)
648 curwin->w_set_curswant = TRUE;
649
650 /* If there is no typeahead may check for timestamps (e.g., for when a
651 * menu invoked a shell command). */
652 if (stuff_empty())
653 {
654 did_check_timestamps = FALSE;
655 if (need_check_timestamps)
656 check_timestamps(FALSE);
657 }
658
659 /*
660 * When emsg() was called msg_scroll will have been set.
661 */
662 msg_scroll = FALSE;
663
664#ifdef FEAT_GUI
665 /* When 'mousefocus' is set a mouse movement may have taken us to
666 * another window. "need_mouse_correct" may then be set because of an
667 * autocommand. */
668 if (need_mouse_correct)
669 gui_mouse_correct();
670#endif
671
672#ifdef FEAT_FOLDING
673 /* Open fold at the cursor line, according to 'foldopen'. */
674 if (fdo_flags & FDO_INSERT)
675 foldOpenCursor();
676 /* Close folds where the cursor isn't, according to 'foldclose' */
677 if (!char_avail())
678 foldCheckClose();
679#endif
680
681 /*
682 * If we inserted a character at the last position of the last line in
683 * the window, scroll the window one line up. This avoids an extra
684 * redraw.
685 * This is detected when the cursor column is smaller after inserting
686 * something.
687 * Don't do this when the topline changed already, it has
688 * already been adjusted (by insertchar() calling open_line())).
689 */
690 if (curbuf->b_mod_set
691 && curwin->w_p_wrap
692 && !did_backspace
693 && curwin->w_topline == old_topline
694#ifdef FEAT_DIFF
695 && curwin->w_topfill == old_topfill
696#endif
697 )
698 {
699 mincol = curwin->w_wcol;
700 validate_cursor_col();
701
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000702 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000703 && curwin->w_wrow == W_WINROW(curwin)
704 + curwin->w_height - 1 - p_so
705 && (curwin->w_cursor.lnum != curwin->w_topline
706#ifdef FEAT_DIFF
707 || curwin->w_topfill > 0
708#endif
709 ))
710 {
711#ifdef FEAT_DIFF
712 if (curwin->w_topfill > 0)
713 --curwin->w_topfill;
714 else
715#endif
716#ifdef FEAT_FOLDING
717 if (hasFolding(curwin->w_topline, NULL, &old_topline))
718 set_topline(curwin, old_topline + 1);
719 else
720#endif
721 set_topline(curwin, curwin->w_topline + 1);
722 }
723 }
724
725 /* May need to adjust w_topline to show the cursor. */
726 update_topline();
727
728 did_backspace = FALSE;
729
730 validate_cursor(); /* may set must_redraw */
731
732 /*
733 * Redraw the display when no characters are waiting.
734 * Also shows mode, ruler and positions cursor.
735 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000736 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
738#ifdef FEAT_SCROLLBIND
739 if (curwin->w_p_scb)
740 do_check_scrollbind(TRUE);
741#endif
742
Bram Moolenaar860cae12010-06-05 23:22:07 +0200743#ifdef FEAT_CURSORBIND
744 if (curwin->w_p_crb)
745 do_check_cursorbind();
746#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 update_curswant();
748 old_topline = curwin->w_topline;
749#ifdef FEAT_DIFF
750 old_topfill = curwin->w_topfill;
751#endif
752
753#ifdef USE_ON_FLY_SCROLL
754 dont_scroll = FALSE; /* allow scrolling here */
755#endif
756
757 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000758 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 */
760 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000761 do
762 {
763 c = safe_vgetc();
764 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000766#ifdef FEAT_AUTOCMD
767 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
768 did_cursorhold = TRUE;
769#endif
770
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771#ifdef FEAT_RIGHTLEFT
772 if (p_hkmap && KeyTyped)
773 c = hkmap(c); /* Hebrew mode mapping */
774#endif
775#ifdef FEAT_FKMAP
776 if (p_fkmap && KeyTyped)
777 c = fkmap(c); /* Farsi mode mapping */
778#endif
779
780#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000781 /*
782 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000783 * and the cursor is still in the completed word. Only when there is
784 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000785 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000786 if (compl_started
787 && pum_wanted()
788 && curwin->w_cursor.col >= compl_col
789 && (compl_shown_match == NULL
790 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000791 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000792 /* BS: Delete one character from "compl_leader". */
793 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000794 && curwin->w_cursor.col > compl_col
795 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000796 continue;
797
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000798 /* When no match was selected or it was edited. */
799 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000800 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000801 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000802 * "compl_leader". Except when at the original match and
803 * there is nothing to add, CTRL-L works like CTRL-P then. */
804 if (c == Ctrl_L
805 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000806 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000807 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000808 {
809 ins_compl_addfrommatch();
810 continue;
811 }
812
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000813 /* A non-white character that fits in with the current
814 * completion: Add to "compl_leader". */
815 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000816 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100817#ifdef FEAT_AUTOCMD
818 /* Trigger InsertCharPre. */
819 char_u *str = do_insert_char_pre(c);
820 char_u *p;
821
822 if (str != NULL)
823 {
824 for (p = str; *p != NUL; mb_ptr_adv(p))
825 ins_compl_addleader(PTR2CHAR(p));
826 vim_free(str);
827 }
828 else
829#endif
830 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000831 continue;
832 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000833
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000834 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000835 * compl_enter_selects is set the Enter key does the same. */
836 if (c == Ctrl_Y || (compl_enter_selects
837 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000838 {
839 ins_compl_delete();
840 ins_compl_insert();
841 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000842 }
843 }
844
Bram Moolenaar071d4272004-06-13 20:20:40 +0000845 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
846 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000847 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000848 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000849 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000850#endif
851
Bram Moolenaar488c6512005-08-11 20:09:58 +0000852 /* CTRL-\ CTRL-N goes to Normal mode,
853 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
854 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000855 if (c == Ctrl_BSL)
856 {
857 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000858 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 ++no_mapping;
860 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000861 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000862 --no_mapping;
863 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000864 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000866 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000867 vungetc(c);
868 c = Ctrl_BSL;
869 }
870 else if (c == Ctrl_G && p_im)
871 continue;
872 else
873 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000874 if (c == Ctrl_O)
875 {
876 ins_ctrl_o();
877 ins_at_eol = FALSE; /* cursor keeps its column */
878 nomove = TRUE;
879 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000880 count = 0;
881 goto doESCkey;
882 }
883 }
884
885#ifdef FEAT_DIGRAPHS
886 c = do_digraph(c);
887#endif
888
889#ifdef FEAT_INS_EXPAND
890 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
891 goto docomplete;
892#endif
893 if (c == Ctrl_V || c == Ctrl_Q)
894 {
895 ins_ctrl_v();
896 c = Ctrl_V; /* pretend CTRL-V is last typed character */
897 continue;
898 }
899
900#ifdef FEAT_CINDENT
901 if (cindent_on()
902# ifdef FEAT_INS_EXPAND
903 && ctrl_x_mode == 0
904# endif
905 )
906 {
907 /* A key name preceded by a bang means this key is not to be
908 * inserted. Skip ahead to the re-indenting below.
909 * A key name preceded by a star means that indenting has to be
910 * done before inserting the key. */
911 line_is_white = inindent(0);
912 if (in_cinkeys(c, '!', line_is_white))
913 goto force_cindent;
914 if (can_cindent && in_cinkeys(c, '*', line_is_white)
915 && stop_arrow() == OK)
916 do_c_expr_indent();
917 }
918#endif
919
920#ifdef FEAT_RIGHTLEFT
921 if (curwin->w_p_rl)
922 switch (c)
923 {
924 case K_LEFT: c = K_RIGHT; break;
925 case K_S_LEFT: c = K_S_RIGHT; break;
926 case K_C_LEFT: c = K_C_RIGHT; break;
927 case K_RIGHT: c = K_LEFT; break;
928 case K_S_RIGHT: c = K_S_LEFT; break;
929 case K_C_RIGHT: c = K_C_LEFT; break;
930 }
931#endif
932
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 /*
934 * If 'keymodel' contains "startsel", may start selection. If it
935 * does, a CTRL-O and c will be stuffed, we need to get these
936 * characters.
937 */
938 if (ins_start_select(c))
939 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940
941 /*
942 * The big switch to handle a character in insert mode.
943 */
944 switch (c)
945 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000946 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000947 if (echeck_abbr(ESC + ABBR_OFF))
948 break;
949 /*FALLTHROUGH*/
950
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000951 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952#ifdef FEAT_CMDWIN
953 if (c == Ctrl_C && cmdwin_type != 0)
954 {
955 /* Close the cmdline window. */
956 cmdwin_result = K_IGNORE;
957 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000958 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000959 goto doESCkey;
960 }
961#endif
962
963#ifdef UNIX
964do_intr:
965#endif
966 /* when 'insertmode' set, and not halfway a mapping, don't leave
967 * Insert mode */
968 if (goto_im())
969 {
970 if (got_int)
971 {
972 (void)vgetc(); /* flush all buffers */
973 got_int = FALSE;
974 }
975 else
976 vim_beep();
977 break;
978 }
979doESCkey:
980 /*
981 * This is the ONLY return from edit()!
982 */
983 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
984 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000985 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000986 o_lnum = curwin->w_cursor.lnum;
987
Bram Moolenaar488c6512005-08-11 20:09:58 +0000988 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000989 {
990#ifdef FEAT_AUTOCMD
991 if (cmdchar != 'r' && cmdchar != 'v')
992 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
993 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000994 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000995#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000996 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000997 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 continue;
999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 case Ctrl_Z: /* suspend when 'insertmode' set */
1001 if (!p_im)
1002 goto normalchar; /* insert CTRL-Z as normal char */
1003 stuffReadbuff((char_u *)":st\r");
1004 c = Ctrl_O;
1005 /*FALLTHROUGH*/
1006
1007 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +00001008#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001009 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001010 goto docomplete;
1011#endif
1012 if (echeck_abbr(Ctrl_O + ABBR_OFF))
1013 break;
1014 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +00001015
1016#ifdef FEAT_VIRTUALEDIT
1017 /* don't move the cursor left when 'virtualedit' has "onemore". */
1018 if (ve_flags & VE_ONEMORE)
1019 {
1020 ins_at_eol = FALSE;
1021 nomove = TRUE;
1022 }
1023#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001024 count = 0;
1025 goto doESCkey;
1026
Bram Moolenaar572cb562005-08-05 21:35:02 +00001027 case K_INS: /* toggle insert/replace mode */
1028 case K_KINS:
1029 ins_insert(replaceState);
1030 break;
1031
1032 case K_SELECT: /* end of Select mode mapping - ignore */
1033 break;
1034
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001035#ifdef FEAT_SNIFF
1036 case K_SNIFF: /* Sniff command received */
1037 stuffcharReadbuff(K_SNIFF);
1038 goto doESCkey;
1039#endif
1040
1041 case K_HELP: /* Help key works like <ESC> <Help> */
1042 case K_F1:
1043 case K_XF1:
1044 stuffcharReadbuff(K_HELP);
1045 if (p_im)
1046 need_start_insertmode = TRUE;
1047 goto doESCkey;
1048
1049#ifdef FEAT_NETBEANS_INTG
1050 case K_F21: /* NetBeans command */
1051 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001052 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001053 --no_mapping;
1054 netbeans_keycommand(i);
1055 break;
1056#endif
1057
1058 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 case NUL:
1060 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 /* For ^@ the trailing ESC will end the insert, unless there is an
1062 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1064 && c != Ctrl_A && !p_im)
1065 goto doESCkey; /* quit insert mode */
1066 inserted_space = FALSE;
1067 break;
1068
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001069 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070 ins_reg();
1071 auto_format(FALSE, TRUE);
1072 inserted_space = FALSE;
1073 break;
1074
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 ins_ctrl_g();
1077 break;
1078
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079 case Ctrl_HAT: /* switch input mode and/or langmap */
1080 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 break;
1082
1083#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001084 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 if (!p_ari)
1086 goto normalchar;
1087 ins_ctrl_();
1088 break;
1089#endif
1090
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001091 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1093 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1094 goto docomplete;
1095#endif
1096 /* FALLTHROUGH */
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099# ifdef FEAT_INS_EXPAND
1100 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1101 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001102 if (has_compl_option(FALSE))
1103 goto docomplete;
1104 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 }
1106# endif
1107 ins_shift(c, lastc);
1108 auto_format(FALSE, TRUE);
1109 inserted_space = FALSE;
1110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 case K_KDEL:
1114 ins_del();
1115 auto_format(FALSE, TRUE);
1116 break;
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001119 case Ctrl_H:
1120 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1121 auto_format(FALSE, TRUE);
1122 break;
1123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001124 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001125 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1126 auto_format(FALSE, TRUE);
1127 break;
1128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001129 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001130# ifdef FEAT_COMPL_FUNC
1131 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001132 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001133 goto docomplete;
1134# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001135 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1136 auto_format(FALSE, TRUE);
1137 inserted_space = FALSE;
1138 break;
1139
1140#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001141 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142 case K_LEFTMOUSE_NM:
1143 case K_LEFTDRAG:
1144 case K_LEFTRELEASE:
1145 case K_LEFTRELEASE_NM:
1146 case K_MIDDLEMOUSE:
1147 case K_MIDDLEDRAG:
1148 case K_MIDDLERELEASE:
1149 case K_RIGHTMOUSE:
1150 case K_RIGHTDRAG:
1151 case K_RIGHTRELEASE:
1152 case K_X1MOUSE:
1153 case K_X1DRAG:
1154 case K_X1RELEASE:
1155 case K_X2MOUSE:
1156 case K_X2DRAG:
1157 case K_X2RELEASE:
1158 ins_mouse(c);
1159 break;
1160
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001161 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001162 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 break;
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001166 ins_mousescroll(MSCR_UP);
1167 break;
1168
1169 case K_MOUSELEFT: /* Scroll wheel left */
1170 ins_mousescroll(MSCR_LEFT);
1171 break;
1172
1173 case K_MOUSERIGHT: /* Scroll wheel right */
1174 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 break;
1176#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001177#ifdef FEAT_GUI_TABLINE
1178 case K_TABLINE:
1179 case K_TABMENU:
1180 ins_tabline(c);
1181 break;
1182#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001184 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 break;
1186
Bram Moolenaar754b5602006-02-09 23:53:20 +00001187#ifdef FEAT_AUTOCMD
1188 case K_CURSORHOLD: /* Didn't type something for a while. */
1189 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1190 did_cursorhold = TRUE;
1191 break;
1192#endif
1193
Bram Moolenaar4770d092006-01-12 23:22:24 +00001194#ifdef FEAT_GUI_W32
1195 /* On Win32 ignore <M-F4>, we get it when closing the window was
1196 * cancelled. */
1197 case K_F4:
1198 if (mod_mask != MOD_MASK_ALT)
1199 goto normalchar;
1200 break;
1201#endif
1202
Bram Moolenaar071d4272004-06-13 20:20:40 +00001203#ifdef FEAT_GUI
1204 case K_VER_SCROLLBAR:
1205 ins_scroll();
1206 break;
1207
1208 case K_HOR_SCROLLBAR:
1209 ins_horscroll();
1210 break;
1211#endif
1212
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001213 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 case K_S_HOME:
1216 case K_C_HOME:
1217 ins_home(c);
1218 break;
1219
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 case K_S_END:
1223 case K_C_END:
1224 ins_end(c);
1225 break;
1226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001227 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001228 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1229 ins_s_left();
1230 else
1231 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001232 break;
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 case K_C_LEFT:
1236 ins_s_left();
1237 break;
1238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001239 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001240 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1241 ins_s_right();
1242 else
1243 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 break;
1245
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001246 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247 case K_C_RIGHT:
1248 ins_s_right();
1249 break;
1250
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001251 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001252#ifdef FEAT_INS_EXPAND
1253 if (pum_visible())
1254 goto docomplete;
1255#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001256 if (mod_mask & MOD_MASK_SHIFT)
1257 ins_pageup();
1258 else
1259 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260 break;
1261
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001262 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 case K_PAGEUP:
1264 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001265#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001266 if (pum_visible())
1267 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001268#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 ins_pageup();
1270 break;
1271
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001272 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001273#ifdef FEAT_INS_EXPAND
1274 if (pum_visible())
1275 goto docomplete;
1276#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001277 if (mod_mask & MOD_MASK_SHIFT)
1278 ins_pagedown();
1279 else
1280 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 break;
1282
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001283 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 case K_PAGEDOWN:
1285 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001286#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001287 if (pum_visible())
1288 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001289#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 ins_pagedown();
1291 break;
1292
1293#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001294 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001295 ins_drop();
1296 break;
1297#endif
1298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 c = TAB;
1301 /* FALLTHROUGH */
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1305 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1306 goto docomplete;
1307#endif
1308 inserted_space = FALSE;
1309 if (ins_tab())
1310 goto normalchar; /* insert TAB as a normal char */
1311 auto_format(FALSE, TRUE);
1312 break;
1313
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001314 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315 c = CAR;
1316 /* FALLTHROUGH */
1317 case CAR:
1318 case NL:
1319#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1320 /* In a quickfix window a <CR> jumps to the error under the
1321 * cursor. */
1322 if (bt_quickfix(curbuf) && c == CAR)
1323 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001324 if (curwin->w_llist_ref == NULL) /* quickfix window */
1325 do_cmdline_cmd((char_u *)".cc");
1326 else /* location list window */
1327 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 break;
1329 }
1330#endif
1331#ifdef FEAT_CMDWIN
1332 if (cmdwin_type != 0)
1333 {
1334 /* Execute the command in the cmdline window. */
1335 cmdwin_result = CAR;
1336 goto doESCkey;
1337 }
1338#endif
1339 if (ins_eol(c) && !p_im)
1340 goto doESCkey; /* out of memory */
1341 auto_format(FALSE, FALSE);
1342 inserted_space = FALSE;
1343 break;
1344
Bram Moolenaar860cae12010-06-05 23:22:07 +02001345#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001346 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347# ifdef FEAT_INS_EXPAND
1348 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1349 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001350 if (has_compl_option(TRUE))
1351 goto docomplete;
1352 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 }
1354# endif
1355# ifdef FEAT_DIGRAPHS
1356 c = ins_digraph();
1357 if (c == NUL)
1358 break;
1359# endif
1360 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362
1363#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001364 case Ctrl_X: /* Enter CTRL-X mode */
1365 ins_ctrl_x();
1366 break;
1367
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001368 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 if (ctrl_x_mode != CTRL_X_TAGS)
1370 goto normalchar;
1371 goto docomplete;
1372
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001373 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001374 if (ctrl_x_mode != CTRL_X_FILES)
1375 goto normalchar;
1376 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001377
1378 case 's': /* Spelling completion after ^X */
1379 case Ctrl_S:
1380 if (ctrl_x_mode != CTRL_X_SPELL)
1381 goto normalchar;
1382 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383#endif
1384
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001385 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001386#ifdef FEAT_INS_EXPAND
1387 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1388#endif
1389 {
1390 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1391 if (p_im)
1392 {
1393 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1394 break;
1395 goto doESCkey;
1396 }
1397 goto normalchar;
1398 }
1399#ifdef FEAT_INS_EXPAND
1400 /* FALLTHROUGH */
1401
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001402 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 case Ctrl_N:
1404 /* if 'complete' is empty then plain ^P is no longer special,
1405 * but it is under other ^X modes */
1406 if (*curbuf->b_p_cpt == NUL
1407 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001408 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 goto normalchar;
1410
1411docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001412 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001414 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001415 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 break;
1417#endif /* FEAT_INS_EXPAND */
1418
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001419 case Ctrl_Y: /* copy from previous line or scroll down */
1420 case Ctrl_E: /* copy from next line or scroll up */
1421 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 break;
1423
1424 default:
1425#ifdef UNIX
1426 if (c == intr_char) /* special interrupt char */
1427 goto do_intr;
1428#endif
1429
Bram Moolenaare659c952011-05-19 17:25:41 +02001430normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001431 /*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02001432 * Insert a normal character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001434#ifdef FEAT_AUTOCMD
1435 if (!p_paste)
1436 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001437 /* Trigger InsertCharPre. */
1438 char_u *str = do_insert_char_pre(c);
1439 char_u *p;
1440
1441 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001442 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001443 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001444 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001445 /* Insert the new value of v:char literally. */
1446 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001447 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001448 c = PTR2CHAR(p);
1449 if (c == CAR || c == K_KENTER || c == NL)
1450 ins_eol(c);
1451 else
1452 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001453 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001454 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001455 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001456 vim_free(str);
1457 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001458 }
1459
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001460 /* If the new value is already inserted or an empty string
1461 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001462 if (c == NUL)
1463 break;
1464 }
1465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466#ifdef FEAT_SMARTINDENT
1467 /* Try to perform smart-indenting. */
1468 ins_try_si(c);
1469#endif
1470
1471 if (c == ' ')
1472 {
1473 inserted_space = TRUE;
1474#ifdef FEAT_CINDENT
1475 if (inindent(0))
1476 can_cindent = FALSE;
1477#endif
1478 if (Insstart_blank_vcol == MAXCOL
1479 && curwin->w_cursor.lnum == Insstart.lnum)
1480 Insstart_blank_vcol = get_nolist_virtcol();
1481 }
1482
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001483 /* Insert a normal character and check for abbreviations on a
1484 * special character. Let CTRL-] expand abbreviations without
1485 * inserting it. */
1486 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487#ifdef FEAT_MBYTE
1488 /* Add ABBR_OFF for characters above 0x100, this is
1489 * what check_abbr() expects. */
1490 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1491#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001492 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 {
1494 insert_special(c, FALSE, FALSE);
1495#ifdef FEAT_RIGHTLEFT
1496 revins_legal++;
1497 revins_chars++;
1498#endif
1499 }
1500
1501 auto_format(FALSE, TRUE);
1502
1503#ifdef FEAT_FOLDING
1504 /* When inserting a character the cursor line must never be in a
1505 * closed fold. */
1506 foldOpenCursor();
1507#endif
1508 break;
1509 } /* end of switch (c) */
1510
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001511#ifdef FEAT_AUTOCMD
1512 /* If typed something may trigger CursorHoldI again. */
1513 if (c != K_CURSORHOLD)
1514 did_cursorhold = FALSE;
1515#endif
1516
Bram Moolenaar071d4272004-06-13 20:20:40 +00001517 /* If the cursor was moved we didn't just insert a space */
1518 if (arrow_used)
1519 inserted_space = FALSE;
1520
1521#ifdef FEAT_CINDENT
1522 if (can_cindent && cindent_on()
1523# ifdef FEAT_INS_EXPAND
1524 && ctrl_x_mode == 0
1525# endif
1526 )
1527 {
1528force_cindent:
1529 /*
1530 * Indent now if a key was typed that is in 'cinkeys'.
1531 */
1532 if (in_cinkeys(c, ' ', line_is_white))
1533 {
1534 if (stop_arrow() == OK)
1535 /* re-indent the current line */
1536 do_c_expr_indent();
1537 }
1538 }
1539#endif /* FEAT_CINDENT */
1540
1541 } /* for (;;) */
1542 /* NOTREACHED */
1543}
1544
1545/*
1546 * Redraw for Insert mode.
1547 * This is postponed until getting the next character to make '$' in the 'cpo'
1548 * option work correctly.
1549 * Only redraw when there are no characters available. This speeds up
1550 * inserting sequences of characters (e.g., for CTRL-R).
1551 */
1552 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001553ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001554 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001555{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001556#ifdef FEAT_CONCEAL
1557 linenr_T conceal_old_cursor_line = 0;
1558 linenr_T conceal_new_cursor_line = 0;
1559 int conceal_update_lines = FALSE;
1560#endif
1561
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001562 if (char_avail())
1563 return;
1564
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001565#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001566 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1567 * visible, the command might delete it. */
1568 if (ready && (
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001569# ifdef FEAT_AUTOCMD
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001570 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001571# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001572# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001573 ||
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001574# endif
1575# ifdef FEAT_CONCEAL
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001576 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001577# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001578 )
1579 && !equalpos(last_cursormoved, curwin->w_cursor)
1580# ifdef FEAT_INS_EXPAND
1581 && !pum_visible()
1582# endif
1583 )
1584 {
1585# ifdef FEAT_SYN_HL
1586 /* Need to update the screen first, to make sure syntax
1587 * highlighting is correct after making a change (e.g., inserting
1588 * a "(". The autocommand may also require a redraw, so it's done
1589 * again below, unfortunately. */
1590 if (syntax_present(curwin) && must_redraw)
1591 update_screen(0);
1592# endif
1593# ifdef FEAT_AUTOCMD
1594 if (has_cursormovedI())
1595 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1596# endif
1597# ifdef FEAT_CONCEAL
1598 if (curwin->w_p_cole > 0)
1599 {
1600 conceal_old_cursor_line = last_cursormoved.lnum;
1601 conceal_new_cursor_line = curwin->w_cursor.lnum;
1602 conceal_update_lines = TRUE;
1603 }
1604# endif
1605 last_cursormoved = curwin->w_cursor;
1606 }
1607#endif
1608
1609#ifdef FEAT_AUTOCMD
1610 /* Trigger TextChangedI if b_changedtick differs. */
1611 if (ready && has_textchangedI()
1612 && last_changedtick != curbuf->b_changedtick
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001613# ifdef FEAT_INS_EXPAND
1614 && !pum_visible()
1615# endif
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001616 )
1617 {
1618 if (last_changedtick_buf == curbuf)
1619 apply_autocmds(EVENT_TEXTCHANGEDI, NULL, NULL, FALSE, curbuf);
1620 last_changedtick_buf = curbuf;
1621 last_changedtick = curbuf->b_changedtick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 }
Bram Moolenaare21b6b22014-01-14 12:17:02 +01001623#endif
1624
1625 if (must_redraw)
1626 update_screen(0);
1627 else if (clear_cmdline || redraw_cmdline)
1628 showmode(); /* clear cmdline and show mode */
1629# if defined(FEAT_CONCEAL)
1630 if ((conceal_update_lines
1631 && (conceal_old_cursor_line != conceal_new_cursor_line
1632 || conceal_cursor_line(curwin)))
1633 || need_cursor_line_redraw)
1634 {
1635 if (conceal_old_cursor_line != conceal_new_cursor_line)
1636 update_single_line(curwin, conceal_old_cursor_line);
1637 update_single_line(curwin, conceal_new_cursor_line == 0
1638 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
1639 curwin->w_valid &= ~VALID_CROW;
1640 }
1641# endif
1642 showruler(FALSE);
1643 setcursor();
1644 emsg_on_display = FALSE; /* may remove error message now */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645}
1646
1647/*
1648 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1649 */
1650 static void
1651ins_ctrl_v()
1652{
1653 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001654 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
1656 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001657 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658
1659 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001660 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001662 did_putchar = TRUE;
1663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1665
1666#ifdef FEAT_CMDL_INFO
1667 add_to_showcmd_c(Ctrl_V);
1668#endif
1669
1670 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001671 if (did_putchar)
1672 /* when the line fits in 'columns' the '^' is at the start of the next
1673 * line and will not removed by the redraw */
1674 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675#ifdef FEAT_CMDL_INFO
1676 clear_showcmd();
1677#endif
1678 insert_special(c, FALSE, TRUE);
1679#ifdef FEAT_RIGHTLEFT
1680 revins_chars++;
1681 revins_legal++;
1682#endif
1683}
1684
1685/*
1686 * Put a character directly onto the screen. It's not stored in a buffer.
1687 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1688 */
1689static int pc_status;
1690#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1691#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1692#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1693#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001694static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001695static int pc_attr;
1696static int pc_row;
1697static int pc_col;
1698
1699 void
1700edit_putchar(c, highlight)
1701 int c;
1702 int highlight;
1703{
1704 int attr;
1705
1706 if (ScreenLines != NULL)
1707 {
1708 update_topline(); /* just in case w_topline isn't valid */
1709 validate_cursor();
1710 if (highlight)
1711 attr = hl_attr(HLF_8);
1712 else
1713 attr = 0;
1714 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1715 pc_col = W_WINCOL(curwin);
1716#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1717 pc_status = PC_STATUS_UNSET;
1718#endif
1719#ifdef FEAT_RIGHTLEFT
1720 if (curwin->w_p_rl)
1721 {
1722 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1723# ifdef FEAT_MBYTE
1724 if (has_mbyte)
1725 {
1726 int fix_col = mb_fix_col(pc_col, pc_row);
1727
1728 if (fix_col != pc_col)
1729 {
1730 screen_putchar(' ', pc_row, fix_col, attr);
1731 --curwin->w_wcol;
1732 pc_status = PC_STATUS_RIGHT;
1733 }
1734 }
1735# endif
1736 }
1737 else
1738#endif
1739 {
1740 pc_col += curwin->w_wcol;
1741#ifdef FEAT_MBYTE
1742 if (mb_lefthalve(pc_row, pc_col))
1743 pc_status = PC_STATUS_LEFT;
1744#endif
1745 }
1746
1747 /* save the character to be able to put it back */
1748#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1749 if (pc_status == PC_STATUS_UNSET)
1750#endif
1751 {
1752 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1753 pc_status = PC_STATUS_SET;
1754 }
1755 screen_putchar(c, pc_row, pc_col, attr);
1756 }
1757}
1758
1759/*
1760 * Undo the previous edit_putchar().
1761 */
1762 void
1763edit_unputchar()
1764{
1765 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1766 {
1767#if defined(FEAT_MBYTE)
1768 if (pc_status == PC_STATUS_RIGHT)
1769 ++curwin->w_wcol;
1770 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1771 redrawWinline(curwin->w_cursor.lnum, FALSE);
1772 else
1773#endif
1774 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1775 }
1776}
1777
1778/*
1779 * Called when p_dollar is set: display a '$' at the end of the changed text
1780 * Only works when cursor is in the line that changes.
1781 */
1782 void
1783display_dollar(col)
1784 colnr_T col;
1785{
1786 colnr_T save_col;
1787
1788 if (!redrawing())
1789 return;
1790
1791 cursor_off();
1792 save_col = curwin->w_cursor.col;
1793 curwin->w_cursor.col = col;
1794#ifdef FEAT_MBYTE
1795 if (has_mbyte)
1796 {
1797 char_u *p;
1798
1799 /* If on the last byte of a multi-byte move to the first byte. */
1800 p = ml_get_curline();
1801 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1802 }
1803#endif
1804 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1805 if (curwin->w_wcol < W_WIDTH(curwin))
1806 {
1807 edit_putchar('$', FALSE);
1808 dollar_vcol = curwin->w_virtcol;
1809 }
1810 curwin->w_cursor.col = save_col;
1811}
1812
1813/*
1814 * Call this function before moving the cursor from the normal insert position
1815 * in insert mode.
1816 */
1817 static void
1818undisplay_dollar()
1819{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001820 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001821 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001822 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001823 redrawWinline(curwin->w_cursor.lnum, FALSE);
1824 }
1825}
1826
1827/*
1828 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1829 * Keep the cursor on the same character.
1830 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1831 * type == INDENT_DEC decrease indent (for CTRL-D)
1832 * type == INDENT_SET set indent to "amount"
1833 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1834 */
1835 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001836change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 int type;
1838 int amount;
1839 int round;
1840 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001841 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842{
1843 int vcol;
1844 int last_vcol;
1845 int insstart_less; /* reduction for Insstart.col */
1846 int new_cursor_col;
1847 int i;
1848 char_u *ptr;
1849 int save_p_list;
1850 int start_col;
1851 colnr_T vc;
1852#ifdef FEAT_VREPLACE
1853 colnr_T orig_col = 0; /* init for GCC */
1854 char_u *new_line, *orig_line = NULL; /* init for GCC */
1855
1856 /* VREPLACE mode needs to know what the line was like before changing */
1857 if (State & VREPLACE_FLAG)
1858 {
1859 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1860 orig_col = curwin->w_cursor.col;
1861 }
1862#endif
1863
1864 /* for the following tricks we don't want list mode */
1865 save_p_list = curwin->w_p_list;
1866 curwin->w_p_list = FALSE;
1867 vc = getvcol_nolist(&curwin->w_cursor);
1868 vcol = vc;
1869
1870 /*
1871 * For Replace mode we need to fix the replace stack later, which is only
1872 * possible when the cursor is in the indent. Remember the number of
1873 * characters before the cursor if it's possible.
1874 */
1875 start_col = curwin->w_cursor.col;
1876
1877 /* determine offset from first non-blank */
1878 new_cursor_col = curwin->w_cursor.col;
1879 beginline(BL_WHITE);
1880 new_cursor_col -= curwin->w_cursor.col;
1881
1882 insstart_less = curwin->w_cursor.col;
1883
1884 /*
1885 * If the cursor is in the indent, compute how many screen columns the
1886 * cursor is to the left of the first non-blank.
1887 */
1888 if (new_cursor_col < 0)
1889 vcol = get_indent() - vcol;
1890
1891 if (new_cursor_col > 0) /* can't fix replace stack */
1892 start_col = -1;
1893
1894 /*
1895 * Set the new indent. The cursor will be put on the first non-blank.
1896 */
1897 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001898 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 else
1900 {
1901#ifdef FEAT_VREPLACE
1902 int save_State = State;
1903
1904 /* Avoid being called recursively. */
1905 if (State & VREPLACE_FLAG)
1906 State = INSERT;
1907#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001908 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909#ifdef FEAT_VREPLACE
1910 State = save_State;
1911#endif
1912 }
1913 insstart_less -= curwin->w_cursor.col;
1914
1915 /*
1916 * Try to put cursor on same character.
1917 * If the cursor is at or after the first non-blank in the line,
1918 * compute the cursor column relative to the column of the first
1919 * non-blank character.
1920 * If we are not in insert mode, leave the cursor on the first non-blank.
1921 * If the cursor is before the first non-blank, position it relative
1922 * to the first non-blank, counted in screen columns.
1923 */
1924 if (new_cursor_col >= 0)
1925 {
1926 /*
1927 * When changing the indent while the cursor is touching it, reset
1928 * Insstart_col to 0.
1929 */
1930 if (new_cursor_col == 0)
1931 insstart_less = MAXCOL;
1932 new_cursor_col += curwin->w_cursor.col;
1933 }
1934 else if (!(State & INSERT))
1935 new_cursor_col = curwin->w_cursor.col;
1936 else
1937 {
1938 /*
1939 * Compute the screen column where the cursor should be.
1940 */
1941 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001942 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943
1944 /*
1945 * Advance the cursor until we reach the right screen column.
1946 */
1947 vcol = last_vcol = 0;
1948 new_cursor_col = -1;
1949 ptr = ml_get_curline();
1950 while (vcol <= (int)curwin->w_virtcol)
1951 {
1952 last_vcol = vcol;
1953#ifdef FEAT_MBYTE
1954 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001955 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 else
1957#endif
1958 ++new_cursor_col;
1959 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1960 }
1961 vcol = last_vcol;
1962
1963 /*
1964 * May need to insert spaces to be able to position the cursor on
1965 * the right screen column.
1966 */
1967 if (vcol != (int)curwin->w_virtcol)
1968 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001969 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001971 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 if (ptr != NULL)
1973 {
1974 new_cursor_col += i;
1975 ptr[i] = NUL;
1976 while (--i >= 0)
1977 ptr[i] = ' ';
1978 ins_str(ptr);
1979 vim_free(ptr);
1980 }
1981 }
1982
1983 /*
1984 * When changing the indent while the cursor is in it, reset
1985 * Insstart_col to 0.
1986 */
1987 insstart_less = MAXCOL;
1988 }
1989
1990 curwin->w_p_list = save_p_list;
1991
1992 if (new_cursor_col <= 0)
1993 curwin->w_cursor.col = 0;
1994 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001995 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001996 curwin->w_set_curswant = TRUE;
1997 changed_cline_bef_curs();
1998
1999 /*
2000 * May have to adjust the start of the insert.
2001 */
2002 if (State & INSERT)
2003 {
2004 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
2005 {
2006 if ((int)Insstart.col <= insstart_less)
2007 Insstart.col = 0;
2008 else
2009 Insstart.col -= insstart_less;
2010 }
2011 if ((int)ai_col <= insstart_less)
2012 ai_col = 0;
2013 else
2014 ai_col -= insstart_less;
2015 }
2016
2017 /*
2018 * For REPLACE mode, may have to fix the replace stack, if it's possible.
2019 * If the number of characters before the cursor decreased, need to pop a
2020 * few characters from the replace stack.
2021 * If the number of characters before the cursor increased, need to push a
2022 * few NULs onto the replace stack.
2023 */
2024 if (REPLACE_NORMAL(State) && start_col >= 0)
2025 {
2026 while (start_col > (int)curwin->w_cursor.col)
2027 {
2028 replace_join(0); /* remove a NUL from the replace stack */
2029 --start_col;
2030 }
2031 while (start_col < (int)curwin->w_cursor.col || replaced)
2032 {
2033 replace_push(NUL);
2034 if (replaced)
2035 {
2036 replace_push(replaced);
2037 replaced = NUL;
2038 }
2039 ++start_col;
2040 }
2041 }
2042
2043#ifdef FEAT_VREPLACE
2044 /*
2045 * For VREPLACE mode, we also have to fix the replace stack. In this case
2046 * it is always possible because we backspace over the whole line and then
2047 * put it back again the way we wanted it.
2048 */
2049 if (State & VREPLACE_FLAG)
2050 {
2051 /* If orig_line didn't allocate, just return. At least we did the job,
2052 * even if you can't backspace. */
2053 if (orig_line == NULL)
2054 return;
2055
2056 /* Save new line */
2057 new_line = vim_strsave(ml_get_curline());
2058 if (new_line == NULL)
2059 return;
2060
2061 /* We only put back the new line up to the cursor */
2062 new_line[curwin->w_cursor.col] = NUL;
2063
2064 /* Put back original line */
2065 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2066 curwin->w_cursor.col = orig_col;
2067
2068 /* Backspace from cursor to start of line */
2069 backspace_until_column(0);
2070
2071 /* Insert new stuff into line again */
2072 ins_bytes(new_line);
2073
2074 vim_free(new_line);
2075 }
2076#endif
2077}
2078
2079/*
2080 * Truncate the space at the end of a line. This is to be used only in an
2081 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2082 * modes.
2083 */
2084 void
2085truncate_spaces(line)
2086 char_u *line;
2087{
2088 int i;
2089
2090 /* find start of trailing white space */
2091 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2092 {
2093 if (State & REPLACE_FLAG)
2094 replace_join(0); /* remove a NUL from the replace stack */
2095 }
2096 line[i + 1] = NUL;
2097}
2098
2099#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2100 || defined(FEAT_COMMENTS) || defined(PROTO)
2101/*
2102 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2103 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002104 * Will attempt not to go before "col" even when there is a composing
2105 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002106 */
2107 void
2108backspace_until_column(col)
2109 int col;
2110{
2111 while ((int)curwin->w_cursor.col > col)
2112 {
2113 curwin->w_cursor.col--;
2114 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002115 replace_do_bs(col);
2116 else if (!del_char_after_col(col))
2117 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002118 }
2119}
2120#endif
2121
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002122/*
2123 * Like del_char(), but make sure not to go before column "limit_col".
2124 * Only matters when there are composing characters.
2125 * Return TRUE when something was deleted.
2126 */
2127 static int
2128del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002129 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002130{
2131#ifdef FEAT_MBYTE
2132 if (enc_utf8 && limit_col >= 0)
2133 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002134 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002135
2136 /* Make sure the cursor is at the start of a character, but
2137 * skip forward again when going too far back because of a
2138 * composing character. */
2139 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002140 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002141 {
2142 int l = utf_ptr2len(ml_get_cursor());
2143
2144 if (l == 0) /* end of line */
2145 break;
2146 curwin->w_cursor.col += l;
2147 }
2148 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2149 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002150 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002151 }
2152 else
2153#endif
2154 (void)del_char(FALSE);
2155 return TRUE;
2156}
2157
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2159/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002160 * CTRL-X pressed in Insert mode.
2161 */
2162 static void
2163ins_ctrl_x()
2164{
2165 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2166 * CTRL-V works like CTRL-N */
2167 if (ctrl_x_mode != CTRL_X_CMDLINE)
2168 {
2169 /* if the next ^X<> won't ADD nothing, then reset
2170 * compl_cont_status */
2171 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002172 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002173 else
2174 compl_cont_status = 0;
2175 /* We're not sure which CTRL-X mode it will be yet */
2176 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2177 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2178 edit_submode_pre = NULL;
2179 showmode();
2180 }
2181}
2182
2183/*
2184 * Return TRUE if the 'dict' or 'tsr' option can be used.
2185 */
2186 static int
2187has_compl_option(dict_opt)
2188 int dict_opt;
2189{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002190 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002191# ifdef FEAT_SPELL
2192 && !curwin->w_p_spell
2193# endif
2194 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002195 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2196 {
2197 ctrl_x_mode = 0;
2198 edit_submode = NULL;
2199 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2200 : (char_u *)_("'thesaurus' option is empty"),
2201 hl_attr(HLF_E));
2202 if (emsg_silent == 0)
2203 {
2204 vim_beep();
2205 setcursor();
2206 out_flush();
2207 ui_delay(2000L, FALSE);
2208 }
2209 return FALSE;
2210 }
2211 return TRUE;
2212}
2213
2214/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2216 * This depends on the current mode.
2217 */
2218 int
2219vim_is_ctrl_x_key(c)
2220 int c;
2221{
2222 /* Always allow ^R - let it's results then be checked */
2223 if (c == Ctrl_R)
2224 return TRUE;
2225
Bram Moolenaare3226be2005-12-18 22:10:00 +00002226 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002227 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002228 return TRUE;
2229
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 switch (ctrl_x_mode)
2231 {
2232 case 0: /* Not in any CTRL-X mode */
2233 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2234 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002235 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2237 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2238 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002239 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002240 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 case CTRL_X_SCROLL:
2242 return (c == Ctrl_Y || c == Ctrl_E);
2243 case CTRL_X_WHOLE_LINE:
2244 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2245 case CTRL_X_FILES:
2246 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2247 case CTRL_X_DICTIONARY:
2248 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2249 case CTRL_X_THESAURUS:
2250 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2251 case CTRL_X_TAGS:
2252 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2253#ifdef FEAT_FIND_ID
2254 case CTRL_X_PATH_PATTERNS:
2255 return (c == Ctrl_P || c == Ctrl_N);
2256 case CTRL_X_PATH_DEFINES:
2257 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2258#endif
2259 case CTRL_X_CMDLINE:
2260 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2261 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002262#ifdef FEAT_COMPL_FUNC
2263 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002264 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002265 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002266 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002267#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002268 case CTRL_X_SPELL:
2269 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 }
2271 EMSG(_(e_internal));
2272 return FALSE;
2273}
2274
2275/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002276 * Return TRUE when character "c" is part of the item currently being
2277 * completed. Used to decide whether to abandon complete mode when the menu
2278 * is visible.
2279 */
2280 static int
2281ins_compl_accept_char(c)
2282 int c;
2283{
2284 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2285 /* When expanding an identifier only accept identifier chars. */
2286 return vim_isIDc(c);
2287
2288 switch (ctrl_x_mode)
2289 {
2290 case CTRL_X_FILES:
2291 /* When expanding file name only accept file name chars. But not
2292 * path separators, so that "proto/<Tab>" expands files in
2293 * "proto", not "proto/" as a whole */
2294 return vim_isfilec(c) && !vim_ispathsep(c);
2295
2296 case CTRL_X_CMDLINE:
2297 case CTRL_X_OMNI:
2298 /* Command line and Omni completion can work with just about any
2299 * printable character, but do stop at white space. */
2300 return vim_isprintc(c) && !vim_iswhite(c);
2301
2302 case CTRL_X_WHOLE_LINE:
2303 /* For while line completion a space can be part of the line. */
2304 return vim_isprintc(c);
2305 }
2306 return vim_iswordc(c);
2307}
2308
2309/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002310 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002312 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 * the rest of the word to be in -- webb
2314 */
2315 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002316ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 char_u *str;
2318 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002319 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 char_u *fname;
2321 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002322 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002324 char_u *p;
2325 int i, c;
2326 int actual_len; /* Take multi-byte characters */
2327 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002328 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002329 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 int has_lower = FALSE;
2331 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002333 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002334 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002335 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
Bram Moolenaara2993e12007-08-12 14:38:46 +00002337 /* Find actual length of completion. */
2338#ifdef FEAT_MBYTE
2339 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002341 p = str;
2342 actual_len = 0;
2343 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002345 mb_ptr_adv(p);
2346 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
2348 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002349 else
2350#endif
2351 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
Bram Moolenaara2993e12007-08-12 14:38:46 +00002353 /* Find actual length of original text. */
2354#ifdef FEAT_MBYTE
2355 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002357 p = compl_orig_text;
2358 actual_compl_length = 0;
2359 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002360 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002361 mb_ptr_adv(p);
2362 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 }
2364 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002365 else
2366#endif
2367 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002369 /* "actual_len" may be smaller than "actual_compl_length" when using
2370 * thesaurus, only use the minimum when comparing. */
2371 min_len = actual_len < actual_compl_length
2372 ? actual_len : actual_compl_length;
2373
Bram Moolenaara2993e12007-08-12 14:38:46 +00002374 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002375 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002376 if (wca != NULL)
2377 {
2378 p = str;
2379 for (i = 0; i < actual_len; ++i)
2380#ifdef FEAT_MBYTE
2381 if (has_mbyte)
2382 wca[i] = mb_ptr2char_adv(&p);
2383 else
2384#endif
2385 wca[i] = *(p++);
2386
2387 /* Rule 1: Were any chars converted to lower? */
2388 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002389 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002390 {
2391#ifdef FEAT_MBYTE
2392 if (has_mbyte)
2393 c = mb_ptr2char_adv(&p);
2394 else
2395#endif
2396 c = *(p++);
2397 if (MB_ISLOWER(c))
2398 {
2399 has_lower = TRUE;
2400 if (MB_ISUPPER(wca[i]))
2401 {
2402 /* Rule 1 is satisfied. */
2403 for (i = actual_compl_length; i < actual_len; ++i)
2404 wca[i] = MB_TOLOWER(wca[i]);
2405 break;
2406 }
2407 }
2408 }
2409
2410 /*
2411 * Rule 2: No lower case, 2nd consecutive letter converted to
2412 * upper case.
2413 */
2414 if (!has_lower)
2415 {
2416 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002417 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002418 {
2419#ifdef FEAT_MBYTE
2420 if (has_mbyte)
2421 c = mb_ptr2char_adv(&p);
2422 else
2423#endif
2424 c = *(p++);
2425 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2426 {
2427 /* Rule 2 is satisfied. */
2428 for (i = actual_compl_length; i < actual_len; ++i)
2429 wca[i] = MB_TOUPPER(wca[i]);
2430 break;
2431 }
2432 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2433 }
2434 }
2435
2436 /* Copy the original case of the part we typed. */
2437 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002438 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002439 {
2440#ifdef FEAT_MBYTE
2441 if (has_mbyte)
2442 c = mb_ptr2char_adv(&p);
2443 else
2444#endif
2445 c = *(p++);
2446 if (MB_ISLOWER(c))
2447 wca[i] = MB_TOLOWER(wca[i]);
2448 else if (MB_ISUPPER(c))
2449 wca[i] = MB_TOUPPER(wca[i]);
2450 }
2451
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002452 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002453 * Generate encoding specific output from wide character array.
2454 * Multi-byte characters can occupy up to five bytes more than
2455 * ASCII characters, and we also need one byte for NUL, so stay
2456 * six bytes away from the edge of IObuff.
2457 */
2458 p = IObuff;
2459 i = 0;
2460 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2461#ifdef FEAT_MBYTE
2462 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002463 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002464 else
2465#endif
2466 *(p++) = wca[i++];
2467 *p = NUL;
2468
2469 vim_free(wca);
2470 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002472 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2473 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002474 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002475 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476}
2477
2478/*
2479 * Add a match to the list of matches.
2480 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002481 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002482 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002483 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002484 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002485ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 char_u *str;
2487 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002488 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002490 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002491 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002492 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002493 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002495 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002496 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497
2498 ui_breakcheck();
2499 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002500 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 if (len < 0)
2502 len = (int)STRLEN(str);
2503
2504 /*
2505 * If the same match is already present, don't add it.
2506 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002508 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002509 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002510 do
2511 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002512 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002513 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002514 && match->cp_str[len] == NUL)
2515 return NOTDONE;
2516 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002517 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002518 }
2519
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002520 /* Remove any popup menu before changing the list of matches. */
2521 ins_compl_del_pum();
2522
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523 /*
2524 * Allocate a new match structure.
2525 * Copy the values to the new match structure.
2526 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002527 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002529 return FAIL;
2530 match->cp_number = -1;
2531 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002532 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002533 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 {
2535 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002536 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002538 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002539
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002541 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2543 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002544 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002545 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002546 && compl_curr_match->cp_fname != NULL
2547 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002548 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002549 else if (fname != NULL)
2550 {
2551 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002552 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002555 match->cp_fname = NULL;
2556 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002557
2558 if (cptext != NULL)
2559 {
2560 int i;
2561
2562 for (i = 0; i < CPT_COUNT; ++i)
2563 if (cptext[i] != NULL && *cptext[i] != NUL)
2564 match->cp_text[i] = vim_strsave(cptext[i]);
2565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566
2567 /*
2568 * Link the new match structure in the list of matches.
2569 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002570 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002571 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 else if (dir == FORWARD)
2573 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002574 match->cp_next = compl_curr_match->cp_next;
2575 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 }
2577 else /* BACKWARD */
2578 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002579 match->cp_next = compl_curr_match;
2580 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002582 if (match->cp_next)
2583 match->cp_next->cp_prev = match;
2584 if (match->cp_prev)
2585 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002587 compl_first_match = match;
2588 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002590 /*
2591 * Find the longest common string if still doing that.
2592 */
2593 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2594 ins_compl_longest_match(match);
2595
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 return OK;
2597}
2598
2599/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002600 * Return TRUE if "str[len]" matches with match->cp_str, considering
2601 * match->cp_icase.
2602 */
2603 static int
2604ins_compl_equal(match, str, len)
2605 compl_T *match;
2606 char_u *str;
2607 int len;
2608{
2609 if (match->cp_icase)
2610 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2611 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2612}
2613
2614/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002615 * Reduce the longest common string for match "match".
2616 */
2617 static void
2618ins_compl_longest_match(match)
2619 compl_T *match;
2620{
2621 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002622 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002623 int had_match;
2624
2625 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002626 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002627 /* First match, use it as a whole. */
2628 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002629 if (compl_leader != NULL)
2630 {
2631 had_match = (curwin->w_cursor.col > compl_col);
2632 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002633 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002634 ins_redraw(FALSE);
2635
2636 /* When the match isn't there (to avoid matching itself) remove it
2637 * again after redrawing. */
2638 if (!had_match)
2639 ins_compl_delete();
2640 compl_used_match = FALSE;
2641 }
2642 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002643 else
2644 {
2645 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002646 p = compl_leader;
2647 s = match->cp_str;
2648 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002649 {
2650#ifdef FEAT_MBYTE
2651 if (has_mbyte)
2652 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002653 c1 = mb_ptr2char(p);
2654 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002655 }
2656 else
2657#endif
2658 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002659 c1 = *p;
2660 c2 = *s;
2661 }
2662 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2663 : (c1 != c2))
2664 break;
2665#ifdef FEAT_MBYTE
2666 if (has_mbyte)
2667 {
2668 mb_ptr_adv(p);
2669 mb_ptr_adv(s);
2670 }
2671 else
2672#endif
2673 {
2674 ++p;
2675 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002676 }
2677 }
2678
2679 if (*p != NUL)
2680 {
2681 /* Leader was shortened, need to change the inserted text. */
2682 *p = NUL;
2683 had_match = (curwin->w_cursor.col > compl_col);
2684 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002685 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002686 ins_redraw(FALSE);
2687
2688 /* When the match isn't there (to avoid matching itself) remove it
2689 * again after redrawing. */
2690 if (!had_match)
2691 ins_compl_delete();
2692 }
2693
2694 compl_used_match = FALSE;
2695 }
2696}
2697
2698/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 * Add an array of matches to the list of matches.
2700 * Frees matches[].
2701 */
2702 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002703ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 int num_matches;
2705 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002706 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002707{
2708 int i;
2709 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002710 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002711
Bram Moolenaar572cb562005-08-05 21:35:02 +00002712 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002713 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002714 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002715 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002716 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002717 FreeWild(num_matches, matches);
2718}
2719
2720/* Make the completion list cyclic.
2721 * Return the number of matches (excluding the original).
2722 */
2723 static int
2724ins_compl_make_cyclic()
2725{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002726 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002727 int count = 0;
2728
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002729 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002730 {
2731 /*
2732 * Find the end of the list.
2733 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002734 match = compl_first_match;
2735 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002736 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002737 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002738 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002739 ++count;
2740 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002741 match->cp_next = compl_first_match;
2742 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002743 }
2744 return count;
2745}
2746
Bram Moolenaara94bc432006-03-10 21:42:59 +00002747/*
2748 * Start completion for the complete() function.
2749 * "startcol" is where the matched text starts (1 is first column).
2750 * "list" is the list of matches.
2751 */
2752 void
2753set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002754 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002755 list_T *list;
2756{
2757 /* If already doing completions stop it. */
2758 if (ctrl_x_mode != 0)
2759 ins_compl_prep(' ');
2760 ins_compl_clear();
2761
2762 if (stop_arrow() == FAIL)
2763 return;
2764
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002765 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002766 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002767 startcol = curwin->w_cursor.col;
2768 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002769 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002770 /* compl_pattern doesn't need to be set */
2771 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2772 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002773 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002774 return;
2775
2776 /* Handle like dictionary completion. */
2777 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2778
2779 ins_compl_add_list(list);
2780 compl_matches = ins_compl_make_cyclic();
2781 compl_started = TRUE;
2782 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002783 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002784
2785 compl_curr_match = compl_first_match;
2786 ins_complete(Ctrl_N);
2787 out_flush();
2788}
2789
2790
Bram Moolenaar9372a112005-12-06 19:59:18 +00002791/* "compl_match_array" points the currently displayed list of entries in the
2792 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002793static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002794static int compl_match_arraysize;
2795
2796/*
2797 * Update the screen and when there is any scrolling remove the popup menu.
2798 */
2799 static void
2800ins_compl_upd_pum()
2801{
2802 int h;
2803
2804 if (compl_match_array != NULL)
2805 {
2806 h = curwin->w_cline_height;
2807 update_screen(0);
2808 if (h != curwin->w_cline_height)
2809 ins_compl_del_pum();
2810 }
2811}
2812
2813/*
2814 * Remove any popup menu.
2815 */
2816 static void
2817ins_compl_del_pum()
2818{
2819 if (compl_match_array != NULL)
2820 {
2821 pum_undisplay();
2822 vim_free(compl_match_array);
2823 compl_match_array = NULL;
2824 }
2825}
2826
2827/*
2828 * Return TRUE if the popup menu should be displayed.
2829 */
2830 static int
2831pum_wanted()
2832{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002833 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002834 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002835 return FALSE;
2836
2837 /* The display looks bad on a B&W display. */
2838 if (t_colors < 8
2839#ifdef FEAT_GUI
2840 && !gui.in_use
2841#endif
2842 )
2843 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002844 return TRUE;
2845}
2846
2847/*
2848 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002849 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002850 */
2851 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002852pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002853{
2854 compl_T *compl;
2855 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002856
2857 /* Don't display the popup menu if there are no matches or there is only
2858 * one (ignoring the original text). */
2859 compl = compl_first_match;
2860 i = 0;
2861 do
2862 {
2863 if (compl == NULL
2864 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2865 break;
2866 compl = compl->cp_next;
2867 } while (compl != compl_first_match);
2868
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002869 if (strstr((char *)p_cot, "menuone") != NULL)
2870 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002871 return (i >= 2);
2872}
2873
2874/*
2875 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002876 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002877 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002878 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002879ins_compl_show_pum()
2880{
2881 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002882 compl_T *shown_compl = NULL;
2883 int did_find_shown_match = FALSE;
2884 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002885 int i;
2886 int cur = -1;
2887 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002888 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002889
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002890 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002891 return;
2892
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002893#if defined(FEAT_EVAL)
2894 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2895 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2896#endif
2897
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002898 /* Update the screen before drawing the popup menu over it. */
2899 update_screen(0);
2900
2901 if (compl_match_array == NULL)
2902 {
2903 /* Need to build the popup menu list. */
2904 compl_match_arraysize = 0;
2905 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002906 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002907 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002908 do
2909 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002910 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2911 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002912 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002913 ++compl_match_arraysize;
2914 compl = compl->cp_next;
2915 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002916 if (compl_match_arraysize == 0)
2917 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002918 compl_match_array = (pumitem_T *)alloc_clear(
2919 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002920 * compl_match_arraysize));
2921 if (compl_match_array != NULL)
2922 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002923 /* If the current match is the original text don't find the first
2924 * match after it, don't highlight anything. */
2925 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2926 shown_match_ok = TRUE;
2927
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002928 i = 0;
2929 compl = compl_first_match;
2930 do
2931 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002932 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2933 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002934 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002935 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002936 if (!shown_match_ok)
2937 {
2938 if (compl == compl_shown_match || did_find_shown_match)
2939 {
2940 /* This item is the shown match or this is the
2941 * first displayed item after the shown match. */
2942 compl_shown_match = compl;
2943 did_find_shown_match = TRUE;
2944 shown_match_ok = TRUE;
2945 }
2946 else
2947 /* Remember this displayed match for when the
2948 * shown match is just below it. */
2949 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002950 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002951 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002952
2953 if (compl->cp_text[CPT_ABBR] != NULL)
2954 compl_match_array[i].pum_text =
2955 compl->cp_text[CPT_ABBR];
2956 else
2957 compl_match_array[i].pum_text = compl->cp_str;
2958 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2959 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2960 if (compl->cp_text[CPT_MENU] != NULL)
2961 compl_match_array[i++].pum_extra =
2962 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002963 else
2964 compl_match_array[i++].pum_extra = compl->cp_fname;
2965 }
2966
2967 if (compl == compl_shown_match)
2968 {
2969 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002970
2971 /* When the original text is the shown match don't set
2972 * compl_shown_match. */
2973 if (compl->cp_flags & ORIGINAL_TEXT)
2974 shown_match_ok = TRUE;
2975
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002976 if (!shown_match_ok && shown_compl != NULL)
2977 {
2978 /* The shown match isn't displayed, set it to the
2979 * previously displayed match. */
2980 compl_shown_match = shown_compl;
2981 shown_match_ok = TRUE;
2982 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002983 }
2984 compl = compl->cp_next;
2985 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002986
2987 if (!shown_match_ok) /* no displayed match at all */
2988 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002989 }
2990 }
2991 else
2992 {
2993 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002994 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002995 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2996 || compl_match_array[i].pum_text
2997 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002998 {
2999 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00003000 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003001 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003002 }
3003
3004 if (compl_match_array != NULL)
3005 {
3006 /* Compute the screen column of the start of the completed text.
3007 * Use the cursor to get all wrapping and other settings right. */
3008 col = curwin->w_cursor.col;
3009 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00003010 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003011 curwin->w_cursor.col = col;
3012 }
3013}
3014
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015#define DICT_FIRST (1) /* use just first element in "dict" */
3016#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00003017
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00003019 * Add any identifiers that match the given pattern in the list of dictionary
3020 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 */
3022 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00003023ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
3024 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003025 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003026 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003027 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003028{
Bram Moolenaar0b238792006-03-02 22:49:12 +00003029 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003030 char_u *ptr;
3031 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003032 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003033 char_u **files;
3034 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003036 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037
Bram Moolenaar0b238792006-03-02 22:49:12 +00003038 if (*dict == NUL)
3039 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003040#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003041 /* When 'dictionary' is empty and spell checking is enabled use
3042 * "spell". */
3043 if (!thesaurus && curwin->w_p_spell)
3044 dict = (char_u *)"spell";
3045 else
3046#endif
3047 return;
3048 }
3049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003051 if (buf == NULL)
3052 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003053 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003054
Bram Moolenaar071d4272004-06-13 20:20:40 +00003055 /* If 'infercase' is set, don't use 'smartcase' here */
3056 save_p_scs = p_scs;
3057 if (curbuf->b_p_inf)
3058 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003059
3060 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3061 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003062 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003063 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3064 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003065 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003066 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003067
3068 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003069 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003070 len = STRLEN(pat_esc) + 10;
3071 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003072 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003073 {
3074 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003075 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003076 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003077 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003078 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3079 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003080 vim_free(ptr);
3081 }
3082 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003083 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003084 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003085 if (regmatch.regprog == NULL)
3086 goto theend;
3087 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003088
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3090 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003091 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003092 {
3093 /* copy one dictionary file name into buf */
3094 if (flags == DICT_EXACT)
3095 {
3096 count = 1;
3097 files = &dict;
3098 }
3099 else
3100 {
3101 /* Expand wildcards in the dictionary name, but do not allow
3102 * backticks (for security, the 'dict' option may have been set in
3103 * a modeline). */
3104 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003105# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003106 if (!thesaurus && STRCMP(buf, "spell") == 0)
3107 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003108 else
3109# endif
3110 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003111 || expand_wildcards(1, &buf, &count, &files,
3112 EW_FILE|EW_SILENT) != OK)
3113 count = 0;
3114 }
3115
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003116# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003117 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003118 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003119 /* Complete from active spelling. Skip "\<" in the pattern, we
3120 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003121 if (pat[0] == '\\' && pat[1] == '<')
3122 ptr = pat + 2;
3123 else
3124 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003125 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003127 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003128# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003129 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003130 {
3131 ins_compl_files(count, files, thesaurus, flags,
3132 &regmatch, buf, &dir);
3133 if (flags != DICT_EXACT)
3134 FreeWild(count, files);
3135 }
3136 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003137 break;
3138 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003139
3140theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 p_scs = save_p_scs;
Bram Moolenaar473de612013-06-08 18:19:48 +02003142 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 vim_free(buf);
3144}
3145
Bram Moolenaar0b238792006-03-02 22:49:12 +00003146 static void
3147ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3148 int count;
3149 char_u **files;
3150 int thesaurus;
3151 int flags;
3152 regmatch_T *regmatch;
3153 char_u *buf;
3154 int *dir;
3155{
3156 char_u *ptr;
3157 int i;
3158 FILE *fp;
3159 int add_r;
3160
3161 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3162 {
3163 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3164 if (flags != DICT_EXACT)
3165 {
3166 vim_snprintf((char *)IObuff, IOSIZE,
3167 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003168 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003169 }
3170
3171 if (fp != NULL)
3172 {
3173 /*
3174 * Read dictionary file line by line.
3175 * Check each line for a match.
3176 */
3177 while (!got_int && !compl_interrupted
3178 && !vim_fgets(buf, LSIZE, fp))
3179 {
3180 ptr = buf;
3181 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3182 {
3183 ptr = regmatch->startp[0];
3184 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3185 ptr = find_line_end(ptr);
3186 else
3187 ptr = find_word_end(ptr);
3188 add_r = ins_compl_add_infercase(regmatch->startp[0],
3189 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003190 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003191 if (thesaurus)
3192 {
3193 char_u *wstart;
3194
3195 /*
3196 * Add the other matches on the line
3197 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003198 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003199 while (!got_int)
3200 {
3201 /* Find start of the next word. Skip white
3202 * space and punctuation. */
3203 ptr = find_word_start(ptr);
3204 if (*ptr == NUL || *ptr == NL)
3205 break;
3206 wstart = ptr;
3207
Bram Moolenaara2993e12007-08-12 14:38:46 +00003208 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003209#ifdef FEAT_MBYTE
3210 if (has_mbyte)
3211 /* Japanese words may have characters in
3212 * different classes, only separate words
3213 * with single-byte non-word characters. */
3214 while (*ptr != NUL)
3215 {
3216 int l = (*mb_ptr2len)(ptr);
3217
3218 if (l < 2 && !vim_iswordc(*ptr))
3219 break;
3220 ptr += l;
3221 }
3222 else
3223#endif
3224 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003225
3226 /* Add the word. Skip the regexp match. */
3227 if (wstart != regmatch->startp[0])
3228 add_r = ins_compl_add_infercase(wstart,
3229 (int)(ptr - wstart),
3230 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003231 }
3232 }
3233 if (add_r == OK)
3234 /* if dir was BACKWARD then honor it just once */
3235 *dir = FORWARD;
3236 else if (add_r == FAIL)
3237 break;
3238 /* avoid expensive call to vim_regexec() when at end
3239 * of line */
3240 if (*ptr == '\n' || got_int)
3241 break;
3242 }
3243 line_breakcheck();
3244 ins_compl_check_keys(50);
3245 }
3246 fclose(fp);
3247 }
3248 }
3249}
3250
Bram Moolenaar071d4272004-06-13 20:20:40 +00003251/*
3252 * Find the start of the next word.
3253 * Returns a pointer to the first char of the word. Also stops at a NUL.
3254 */
3255 char_u *
3256find_word_start(ptr)
3257 char_u *ptr;
3258{
3259#ifdef FEAT_MBYTE
3260 if (has_mbyte)
3261 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003262 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003263 else
3264#endif
3265 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3266 ++ptr;
3267 return ptr;
3268}
3269
3270/*
3271 * Find the end of the word. Assumes it starts inside a word.
3272 * Returns a pointer to just after the word.
3273 */
3274 char_u *
3275find_word_end(ptr)
3276 char_u *ptr;
3277{
3278#ifdef FEAT_MBYTE
3279 int start_class;
3280
3281 if (has_mbyte)
3282 {
3283 start_class = mb_get_class(ptr);
3284 if (start_class > 1)
3285 while (*ptr != NUL)
3286 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003287 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003288 if (mb_get_class(ptr) != start_class)
3289 break;
3290 }
3291 }
3292 else
3293#endif
3294 while (vim_iswordc(*ptr))
3295 ++ptr;
3296 return ptr;
3297}
3298
3299/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003300 * Find the end of the line, omitting CR and NL at the end.
3301 * Returns a pointer to just after the line.
3302 */
3303 static char_u *
3304find_line_end(ptr)
3305 char_u *ptr;
3306{
3307 char_u *s;
3308
3309 s = ptr + STRLEN(ptr);
3310 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3311 --s;
3312 return s;
3313}
3314
3315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003316 * Free the list of completions
3317 */
3318 static void
3319ins_compl_free()
3320{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003321 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003322 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003324 vim_free(compl_pattern);
3325 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003326 vim_free(compl_leader);
3327 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003329 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003330 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003331
3332 ins_compl_del_pum();
3333 pum_clear();
3334
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003335 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336 do
3337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003338 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003339 compl_curr_match = compl_curr_match->cp_next;
3340 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003341 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003342 if (match->cp_flags & FREE_FNAME)
3343 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003344 for (i = 0; i < CPT_COUNT; ++i)
3345 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003347 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3348 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003349 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350}
3351
3352 static void
3353ins_compl_clear()
3354{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003355 compl_cont_status = 0;
3356 compl_started = FALSE;
3357 compl_matches = 0;
3358 vim_free(compl_pattern);
3359 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003360 vim_free(compl_leader);
3361 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003363 vim_free(compl_orig_text);
3364 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003365 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003366}
3367
3368/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003369 * Return TRUE when Insert completion is active.
3370 */
3371 int
3372ins_compl_active()
3373{
3374 return compl_started;
3375}
3376
3377/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003378 * Delete one character before the cursor and show the subset of the matches
3379 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003380 * Returns the character to be used, NUL if the work is done and another char
3381 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003382 */
3383 static int
3384ins_compl_bs()
3385{
3386 char_u *line;
3387 char_u *p;
3388
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003389 line = ml_get_curline();
3390 p = line + curwin->w_cursor.col;
3391 mb_ptr_back(line, p);
3392
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003393 /* Stop completion when the whole word was deleted. For Omni completion
3394 * allow the word to be deleted, we won't match everything. */
3395 if ((int)(p - line) - (int)compl_col < 0
3396 || ((int)(p - line) - (int)compl_col == 0
3397 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003398 return K_BS;
3399
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003400 /* Deleted more than what was used to find matches or didn't finish
3401 * finding all matches: need to look for matches all over again. */
3402 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003403 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003404 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003405
Bram Moolenaara6557602006-02-04 22:43:20 +00003406 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003407 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003408 if (compl_leader != NULL)
3409 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003410 ins_compl_new_leader();
Bram Moolenaar3978e082013-03-07 19:38:54 +01003411 if (compl_shown_match != NULL)
3412 /* Make sure current match is not a hidden item. */
3413 compl_curr_match = compl_shown_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003414 return NUL;
3415 }
3416 return K_BS;
3417}
Bram Moolenaara6557602006-02-04 22:43:20 +00003418
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003419/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003420 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3421 * be called.
3422 */
3423 static int
3424ins_compl_need_restart()
3425{
3426 /* Return TRUE if we didn't complete finding matches or when the
3427 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3428 return compl_was_interrupted
3429 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3430 && compl_opt_refresh_always);
3431}
3432
3433/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003434 * Called after changing "compl_leader".
3435 * Show the popup menu with a different set of matches.
3436 * May also search for matches again if the previous search was interrupted.
3437 */
3438 static void
3439ins_compl_new_leader()
3440{
3441 ins_compl_del_pum();
3442 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003443 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003444 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003445
3446 if (compl_started)
3447 ins_compl_set_original_text(compl_leader);
3448 else
3449 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003450#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003451 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003452#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003453 /*
3454 * Matches were cleared, need to search for them now. First display
3455 * the changed text before the cursor. Set "compl_restarting" to
3456 * avoid that the first match is inserted.
3457 */
3458 update_screen(0);
3459#ifdef FEAT_GUI
3460 if (gui.in_use)
3461 {
3462 /* Show the cursor after the match, not after the redrawn text. */
3463 setcursor();
3464 out_flush();
3465 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003466 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003467#endif
3468 compl_restarting = TRUE;
3469 if (ins_complete(Ctrl_N) == FAIL)
3470 compl_cont_status = 0;
3471 compl_restarting = FALSE;
3472 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003473
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003474 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003475
3476 /* Show the popup menu with a different set of matches. */
3477 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003478
3479 /* Don't let Enter select the original text when there is no popup menu. */
3480 if (compl_match_array == NULL)
3481 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003482}
3483
3484/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003485 * Return the length of the completion, from the completion start column to
3486 * the cursor column. Making sure it never goes below zero.
3487 */
3488 static int
3489ins_compl_len()
3490{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003491 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003492
3493 if (off < 0)
3494 return 0;
3495 return off;
3496}
3497
3498/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003499 * Append one character to the match leader. May reduce the number of
3500 * matches.
3501 */
3502 static void
3503ins_compl_addleader(c)
3504 int c;
3505{
3506#ifdef FEAT_MBYTE
3507 int cc;
3508
3509 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3510 {
3511 char_u buf[MB_MAXBYTES + 1];
3512
3513 (*mb_char2bytes)(c, buf);
3514 buf[cc] = NUL;
3515 ins_char_bytes(buf, cc);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003516 if (compl_opt_refresh_always)
3517 AppendToRedobuff(buf);
Bram Moolenaara6557602006-02-04 22:43:20 +00003518 }
3519 else
3520#endif
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003521 {
Bram Moolenaara6557602006-02-04 22:43:20 +00003522 ins_char(c);
Bram Moolenaar22189a42012-06-20 22:56:02 +02003523 if (compl_opt_refresh_always)
3524 AppendCharToRedobuff(c);
Bram Moolenaar5e1a0a92012-06-20 14:26:35 +02003525 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003526
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003527 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003528 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003529 ins_compl_restart();
3530
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003531 /* When 'always' is set, don't reset compl_leader. While completing,
Bram Moolenaar22189a42012-06-20 22:56:02 +02003532 * cursor doesn't point original position, changing compl_leader would
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003533 * break redo. */
3534 if (!compl_opt_refresh_always)
3535 {
3536 vim_free(compl_leader);
3537 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003538 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003539 if (compl_leader != NULL)
3540 ins_compl_new_leader();
3541 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003542}
3543
3544/*
3545 * Setup for finding completions again without leaving CTRL-X mode. Used when
3546 * BS or a key was typed while still searching for matches.
3547 */
3548 static void
3549ins_compl_restart()
3550{
3551 ins_compl_free();
3552 compl_started = FALSE;
3553 compl_matches = 0;
3554 compl_cont_status = 0;
3555 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003556}
3557
3558/*
3559 * Set the first match, the original text.
3560 */
3561 static void
3562ins_compl_set_original_text(str)
3563 char_u *str;
3564{
3565 char_u *p;
3566
3567 /* Replace the original text entry. */
3568 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3569 {
3570 p = vim_strsave(str);
3571 if (p != NULL)
3572 {
3573 vim_free(compl_first_match->cp_str);
3574 compl_first_match->cp_str = p;
3575 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003576 }
3577}
3578
3579/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003580 * Append one character to the match leader. May reduce the number of
3581 * matches.
3582 */
3583 static void
3584ins_compl_addfrommatch()
3585{
3586 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003587 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003588 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003589 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003590
3591 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003592 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003593 {
3594 /* When still at the original match use the first entry that matches
3595 * the leader. */
3596 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3597 {
3598 p = NULL;
3599 for (cp = compl_shown_match->cp_next; cp != NULL
3600 && cp != compl_first_match; cp = cp->cp_next)
3601 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003602 if (compl_leader == NULL
3603 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003604 (int)STRLEN(compl_leader)))
3605 {
3606 p = cp->cp_str;
3607 break;
3608 }
3609 }
3610 if (p == NULL || (int)STRLEN(p) <= len)
3611 return;
3612 }
3613 else
3614 return;
3615 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003616 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003617 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003618 ins_compl_addleader(c);
3619}
3620
3621/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003622 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003623 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003624 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003626 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627ins_compl_prep(c)
3628 int c;
3629{
3630 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003632 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 /* Forget any previous 'special' messages if this is actually
3635 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3636 */
3637 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3638 edit_submode_extra = NULL;
3639
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003640 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003641 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3642 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003643 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003645 /* Set "compl_get_longest" when finding the first matches. */
3646 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3647 || (ctrl_x_mode == 0 && !compl_started))
3648 {
3649 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3650 compl_used_match = TRUE;
3651 }
3652
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3654 {
3655 /*
3656 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3657 * it will be yet. Now we decide.
3658 */
3659 switch (c)
3660 {
3661 case Ctrl_E:
3662 case Ctrl_Y:
3663 ctrl_x_mode = CTRL_X_SCROLL;
3664 if (!(State & REPLACE_FLAG))
3665 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3666 else
3667 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3668 edit_submode_pre = NULL;
3669 showmode();
3670 break;
3671 case Ctrl_L:
3672 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3673 break;
3674 case Ctrl_F:
3675 ctrl_x_mode = CTRL_X_FILES;
3676 break;
3677 case Ctrl_K:
3678 ctrl_x_mode = CTRL_X_DICTIONARY;
3679 break;
3680 case Ctrl_R:
3681 /* Simply allow ^R to happen without affecting ^X mode */
3682 break;
3683 case Ctrl_T:
3684 ctrl_x_mode = CTRL_X_THESAURUS;
3685 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003686#ifdef FEAT_COMPL_FUNC
3687 case Ctrl_U:
3688 ctrl_x_mode = CTRL_X_FUNCTION;
3689 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003690 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003691 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003692 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003693#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003694 case 's':
3695 case Ctrl_S:
3696 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003697#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003698 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003699 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003700 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003701#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003702 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 case Ctrl_RSB:
3704 ctrl_x_mode = CTRL_X_TAGS;
3705 break;
3706#ifdef FEAT_FIND_ID
3707 case Ctrl_I:
3708 case K_S_TAB:
3709 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3710 break;
3711 case Ctrl_D:
3712 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3713 break;
3714#endif
3715 case Ctrl_V:
3716 case Ctrl_Q:
3717 ctrl_x_mode = CTRL_X_CMDLINE;
3718 break;
3719 case Ctrl_P:
3720 case Ctrl_N:
3721 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3722 * just started ^X mode, or there were enough ^X's to cancel
3723 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3724 * do normal expansion when interrupting a different mode (say
3725 * ^X^F^X^P or ^P^X^X^P, see below)
3726 * nothing changes if interrupting mode 0, (eg, the flag
3727 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003728 if (!(compl_cont_status & CONT_INTRPT))
3729 compl_cont_status |= CONT_LOCAL;
3730 else if (compl_cont_mode != 0)
3731 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 /* FALLTHROUGH */
3733 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003734 /* If we have typed at least 2 ^X's... for modes != 0, we set
3735 * compl_cont_status = 0 (eg, as if we had just started ^X
3736 * mode).
3737 * For mode 0, we set "compl_cont_mode" to an impossible
3738 * value, in both cases ^X^X can be used to restart the same
3739 * mode (avoiding ADDING mode).
3740 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3741 * 'complete' and local ^P expansions respectively.
3742 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3743 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (c == Ctrl_X)
3745 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003746 if (compl_cont_mode != 0)
3747 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003749 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 }
3751 ctrl_x_mode = 0;
3752 edit_submode = NULL;
3753 showmode();
3754 break;
3755 }
3756 }
3757 else if (ctrl_x_mode != 0)
3758 {
3759 /* We're already in CTRL-X mode, do we stay in it? */
3760 if (!vim_is_ctrl_x_key(c))
3761 {
3762 if (ctrl_x_mode == CTRL_X_SCROLL)
3763 ctrl_x_mode = 0;
3764 else
3765 ctrl_x_mode = CTRL_X_FINISHED;
3766 edit_submode = NULL;
3767 }
3768 showmode();
3769 }
3770
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003771 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 {
3773 /* Show error message from attempted keyword completion (probably
3774 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003775 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003777 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3778 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 || ctrl_x_mode == CTRL_X_FINISHED)
3780 {
3781 /* Get here when we have finished typing a sequence of ^N and
3782 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003783 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003784 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003785 {
3786 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003787 * If any of the original typed text has been changed, eg when
3788 * ignorecase is set, we must add back-spaces to the redo
3789 * buffer. We add as few as necessary to delete just the part
3790 * of the original text that has changed.
3791 * When using the longest match, edited the match or used
3792 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003793 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003794 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3795 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003796 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003797 ptr = NULL;
3798 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003799 }
3800
3801#ifdef FEAT_CINDENT
3802 want_cindent = (can_cindent && cindent_on());
3803#endif
3804 /*
3805 * When completing whole lines: fix indent for 'cindent'.
3806 * Otherwise, break line if it's too long.
3807 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003808 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 {
3810#ifdef FEAT_CINDENT
3811 /* re-indent the current line */
3812 if (want_cindent)
3813 {
3814 do_c_expr_indent();
3815 want_cindent = FALSE; /* don't do it again */
3816 }
3817#endif
3818 }
3819 else
3820 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003821 int prev_col = curwin->w_cursor.col;
3822
Bram Moolenaar071d4272004-06-13 20:20:40 +00003823 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003824 if (prev_col > 0)
3825 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003826 if (stop_arrow() == OK)
3827 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003828 if (prev_col > 0
3829 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3830 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003831 }
3832
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003833 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003834 * the selection without inserting anything. When
3835 * compl_enter_selects is set the Enter key does the same. */
3836 if ((c == Ctrl_Y || (compl_enter_selects
3837 && (c == CAR || c == K_KENTER || c == NL)))
3838 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003839 retval = TRUE;
3840
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003841 /* CTRL-E means completion is Ended, go back to the typed text. */
3842 if (c == Ctrl_E)
3843 {
3844 ins_compl_delete();
3845 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003846 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003847 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003848 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003849 retval = TRUE;
3850 }
3851
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003852 auto_format(FALSE, TRUE);
3853
Bram Moolenaar071d4272004-06-13 20:20:40 +00003854 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003855 compl_started = FALSE;
3856 compl_matches = 0;
Bram Moolenaarea389e92014-05-28 21:40:52 +02003857 if (!shortmess(SHM_COMPLETIONMENU))
3858 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003859 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003860 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003861 if (edit_submode != NULL)
3862 {
3863 edit_submode = NULL;
3864 showmode();
3865 }
3866
3867#ifdef FEAT_CINDENT
3868 /*
3869 * Indent now if a key was typed that is in 'cinkeys'.
3870 */
3871 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3872 do_c_expr_indent();
3873#endif
Bram Moolenaarcfa3cae2012-07-10 17:14:56 +02003874#ifdef FEAT_AUTOCMD
3875 /* Trigger the CompleteDone event to give scripts a chance to act
3876 * upon the completion. */
3877 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 }
3880 }
Bram Moolenaara3914322013-02-13 16:30:21 +01003881#ifdef FEAT_AUTOCMD
3882 else if (ctrl_x_mode == CTRL_X_LOCAL_MSG)
3883 /* Trigger the CompleteDone event to give scripts a chance to act
3884 * upon the (possibly failed) completion. */
3885 apply_autocmds(EVENT_COMPLETEDONE, NULL, NULL, FALSE, curbuf);
3886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887
3888 /* reset continue_* if we left expansion-mode, if we stay they'll be
3889 * (re)set properly in ins_complete() */
3890 if (!vim_is_ctrl_x_key(c))
3891 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003892 compl_cont_status = 0;
3893 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003894 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003895
3896 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003897}
3898
3899/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003900 * Fix the redo buffer for the completion leader replacing some of the typed
3901 * text. This inserts backspaces and appends the changed text.
3902 * "ptr" is the known leader text or NUL.
3903 */
3904 static void
3905ins_compl_fixRedoBufForLeader(ptr_arg)
3906 char_u *ptr_arg;
3907{
3908 int len;
3909 char_u *p;
3910 char_u *ptr = ptr_arg;
3911
3912 if (ptr == NULL)
3913 {
3914 if (compl_leader != NULL)
3915 ptr = compl_leader;
3916 else
3917 return; /* nothing to do */
3918 }
3919 if (compl_orig_text != NULL)
3920 {
3921 p = compl_orig_text;
3922 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3923 ;
3924#ifdef FEAT_MBYTE
3925 if (len > 0)
3926 len -= (*mb_head_off)(p, p + len);
3927#endif
3928 for (p += len; *p != NUL; mb_ptr_adv(p))
3929 AppendCharToRedobuff(K_BS);
3930 }
3931 else
3932 len = 0;
3933 if (ptr != NULL)
3934 AppendToRedobuffLit(ptr + len, -1);
3935}
3936
3937/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3939 * (depending on flag) starting from buf and looking for a non-scanned
3940 * buffer (other than curbuf). curbuf is special, if it is called with
3941 * buf=curbuf then it has to be the first call for a given flag/expansion.
3942 *
3943 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3944 */
3945 static buf_T *
3946ins_compl_next_buf(buf, flag)
3947 buf_T *buf;
3948 int flag;
3949{
3950#ifdef FEAT_WINDOWS
3951 static win_T *wp;
3952#endif
3953
3954 if (flag == 'w') /* just windows */
3955 {
3956#ifdef FEAT_WINDOWS
3957 if (buf == curbuf) /* first call for this flag/expansion */
3958 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003959 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003960 && wp->w_buffer->b_scanned)
3961 ;
3962 buf = wp->w_buffer;
3963#else
3964 buf = curbuf;
3965#endif
3966 }
3967 else
3968 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3969 * (unlisted buffers)
3970 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003971 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 && ((flag == 'U'
3973 ? buf->b_p_bl
3974 : (!buf->b_p_bl
3975 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003976 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977 ;
3978 return buf;
3979}
3980
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003981#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003982static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003983
3984/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003985 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003986 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003987 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003988 static void
3989expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003990 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003991 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003992{
Bram Moolenaar82139082011-09-14 16:52:09 +02003993 list_T *matchlist = NULL;
3994 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003995 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003996 char_u *funcname;
3997 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003998 win_T *curwin_save;
3999 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02004000 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004001
Bram Moolenaare344bea2005-09-01 20:46:49 +00004002 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4003 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004004 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004005
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004006 /* Call 'completefunc' to obtain the list of matches. */
4007 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004008 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004009
Bram Moolenaare344bea2005-09-01 20:46:49 +00004010 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004011 curwin_save = curwin;
4012 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02004013
4014 /* Call a function, which returns a list or dict. */
Bram Moolenaar0cbba942012-07-25 16:47:03 +02004015 if (call_vim_function(funcname, 2, args, FALSE, FALSE, &rettv) == OK)
Bram Moolenaar82139082011-09-14 16:52:09 +02004016 {
4017 switch (rettv.v_type)
4018 {
4019 case VAR_LIST:
4020 matchlist = rettv.vval.v_list;
4021 break;
4022 case VAR_DICT:
4023 matchdict = rettv.vval.v_dict;
4024 break;
4025 default:
4026 /* TODO: Give error message? */
4027 clear_tv(&rettv);
4028 break;
4029 }
4030 }
4031
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004032 if (curwin_save != curwin || curbuf_save != curbuf)
4033 {
4034 EMSG(_(e_complwin));
4035 goto theend;
4036 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00004037 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004038 check_cursor();
4039 if (!equalpos(curwin->w_cursor, pos))
4040 {
4041 EMSG(_(e_compldel));
4042 goto theend;
4043 }
Bram Moolenaar82139082011-09-14 16:52:09 +02004044
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004045 if (matchlist != NULL)
4046 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02004047 else if (matchdict != NULL)
4048 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004049
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004050theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02004051 if (matchdict != NULL)
4052 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01004053 if (matchlist != NULL)
4054 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00004055}
4056#endif /* FEAT_COMPL_FUNC */
4057
Bram Moolenaar39f05632006-03-19 22:15:26 +00004058#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004059/*
4060 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004061 */
4062 static void
4063ins_compl_add_list(list)
4064 list_T *list;
4065{
4066 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004067 int dir = compl_direction;
4068
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004069 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004070 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004071 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004072 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4073 /* if dir was BACKWARD then honor it just once */
4074 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004075 else if (did_emsg)
4076 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004077 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004078}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004079
4080/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004081 * Add completions from a dict.
4082 */
4083 static void
4084ins_compl_add_dict(dict)
4085 dict_T *dict;
4086{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004087 dictitem_T *di_refresh;
4088 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004089
4090 /* Check for optional "refresh" item. */
4091 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004092 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4093 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004094 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004095 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004096
4097 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4098 compl_opt_refresh_always = TRUE;
4099 }
4100
4101 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004102 di_words = dict_find(dict, (char_u *)"words", 5);
4103 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4104 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004105}
4106
4107/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004108 * Add a match to the list of matches from a typeval_T.
4109 * If the given string is already in the list of completions, then return
4110 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4111 * maybe because alloc() returns NULL, then FAIL is returned.
4112 */
4113 int
4114ins_compl_add_tv(tv, dir)
4115 typval_T *tv;
4116 int dir;
4117{
4118 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004119 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004120 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004121 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004122 char_u *(cptext[CPT_COUNT]);
4123
4124 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4125 {
4126 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4127 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4128 (char_u *)"abbr", FALSE);
4129 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4130 (char_u *)"menu", FALSE);
4131 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4132 (char_u *)"kind", FALSE);
4133 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4134 (char_u *)"info", FALSE);
4135 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4136 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004137 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004138 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004139 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4140 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004141 }
4142 else
4143 {
4144 word = get_tv_string_chk(tv);
4145 vim_memset(cptext, 0, sizeof(cptext));
4146 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004147 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004148 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004149 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004150}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004151#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004152
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004153/*
4154 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004155 * The search starts at position "ini" in curbuf and in the direction
4156 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004157 * When "compl_started" is FALSE start at that position, otherwise continue
4158 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004159 * This may return before finding all the matches.
4160 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004161 */
4162 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004163ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004164 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165{
4166 static pos_T first_match_pos;
4167 static pos_T last_match_pos;
4168 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004169 static int found_all = FALSE; /* Found all matches of a
4170 certain type. */
4171 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
Bram Moolenaar572cb562005-08-05 21:35:02 +00004173 pos_T *pos;
4174 char_u **matches;
4175 int save_p_scs;
4176 int save_p_ws;
4177 int save_p_ic;
4178 int i;
4179 int num_matches;
4180 int len;
4181 int found_new_match;
4182 int type = ctrl_x_mode;
4183 char_u *ptr;
4184 char_u *dict = NULL;
4185 int dict_f = 0;
4186 compl_T *old_match;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004187 int set_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004189 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 {
4191 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4192 ins_buf->b_scanned = 0;
4193 found_all = FALSE;
4194 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004195 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004196 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 last_match_pos = first_match_pos = *ini;
4198 }
4199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004200 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004201 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4203 for (;;)
4204 {
4205 found_new_match = FAIL;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004206 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004208 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * or if found_all says this entry is done. For ^X^L only use the
4210 * entries from 'complete' that look in loaded buffers. */
4211 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004212 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 {
4214 found_all = FALSE;
4215 while (*e_cpt == ',' || *e_cpt == ' ')
4216 e_cpt++;
4217 if (*e_cpt == '.' && !curbuf->b_scanned)
4218 {
4219 ins_buf = curbuf;
4220 first_match_pos = *ini;
4221 /* So that ^N can match word immediately after cursor */
4222 if (ctrl_x_mode == 0)
4223 dec(&first_match_pos);
4224 last_match_pos = first_match_pos;
4225 type = 0;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004226
4227 /* Remember the first match so that the loop stops when we
4228 * wrap and come back there a second time. */
4229 set_match_pos = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 }
4231 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4232 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4233 {
4234 /* Scan a buffer, but not the current one. */
4235 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4236 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004237 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 first_match_pos.col = last_match_pos.col = 0;
4239 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4240 last_match_pos.lnum = 0;
4241 type = 0;
4242 }
4243 else /* unloaded buffer, scan like dictionary */
4244 {
4245 found_all = TRUE;
4246 if (ins_buf->b_fname == NULL)
4247 continue;
4248 type = CTRL_X_DICTIONARY;
4249 dict = ins_buf->b_fname;
4250 dict_f = DICT_EXACT;
4251 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004252 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 ins_buf->b_fname == NULL
4254 ? buf_spname(ins_buf)
4255 : ins_buf->b_sfname == NULL
Bram Moolenaar4ccb2652012-10-04 22:38:37 +02004256 ? ins_buf->b_fname
4257 : ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004258 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 }
4260 else if (*e_cpt == NUL)
4261 break;
4262 else
4263 {
4264 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4265 type = -1;
4266 else if (*e_cpt == 'k' || *e_cpt == 's')
4267 {
4268 if (*e_cpt == 'k')
4269 type = CTRL_X_DICTIONARY;
4270 else
4271 type = CTRL_X_THESAURUS;
4272 if (*++e_cpt != ',' && *e_cpt != NUL)
4273 {
4274 dict = e_cpt;
4275 dict_f = DICT_FIRST;
4276 }
4277 }
4278#ifdef FEAT_FIND_ID
4279 else if (*e_cpt == 'i')
4280 type = CTRL_X_PATH_PATTERNS;
4281 else if (*e_cpt == 'd')
4282 type = CTRL_X_PATH_DEFINES;
4283#endif
4284 else if (*e_cpt == ']' || *e_cpt == 't')
4285 {
4286 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004287 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004288 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 }
4290 else
4291 type = -1;
4292
4293 /* in any case e_cpt is advanced to the next entry */
4294 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4295
4296 found_all = TRUE;
4297 if (type == -1)
4298 continue;
4299 }
4300 }
4301
4302 switch (type)
4303 {
4304 case -1:
4305 break;
4306#ifdef FEAT_FIND_ID
4307 case CTRL_X_PATH_PATTERNS:
4308 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004309 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004310 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004312 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4314 (linenr_T)1, (linenr_T)MAXLNUM);
4315 break;
4316#endif
4317
4318 case CTRL_X_DICTIONARY:
4319 case CTRL_X_THESAURUS:
4320 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004321 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 : (type == CTRL_X_THESAURUS
4323 ? (*curbuf->b_p_tsr == NUL
4324 ? p_tsr
4325 : curbuf->b_p_tsr)
4326 : (*curbuf->b_p_dict == NUL
4327 ? p_dict
4328 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004329 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004330 dict != NULL ? dict_f
4331 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 dict = NULL;
4333 break;
4334
4335 case CTRL_X_TAGS:
4336 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4337 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004340 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004341 * of matches is found when compl_pattern is empty */
4342 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004343 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4344 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4345 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4346 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004347 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 }
4349 p_ic = save_p_ic;
4350 break;
4351
4352 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4355 {
4356
4357 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004358 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaar71afbfe2013-03-19 16:49:16 +01004359 ins_compl_add_matches(num_matches, matches, p_fic || p_wic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360 }
4361 break;
4362
4363 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004364 if (expand_cmdline(&compl_xp, compl_pattern,
4365 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004367 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 break;
4369
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004370#ifdef FEAT_COMPL_FUNC
4371 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004372 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004373 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004374 break;
4375#endif
4376
Bram Moolenaar488c6512005-08-11 20:09:58 +00004377 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004378#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004379 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004380 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004381 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004382 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004383#endif
4384 break;
4385
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 default: /* normal ^P/^N and ^X^L */
4387 /*
4388 * If 'infercase' is set, don't use 'smartcase' here
4389 */
4390 save_p_scs = p_scs;
4391 if (ins_buf->b_p_inf)
4392 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393
Bram Moolenaar361aa502014-01-23 22:45:58 +01004394 /* Buffers other than curbuf are scanned from the beginning or the
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 * end but never from the middle, thus setting nowrapscan in this
4396 * buffers is a good idea, on the other hand, we always set
4397 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4398 save_p_ws = p_ws;
4399 if (ins_buf != curbuf)
4400 p_ws = FALSE;
4401 else if (*e_cpt == '.')
4402 p_ws = TRUE;
4403 for (;;)
4404 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004405 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004406
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004407 ++msg_silent; /* Don't want messages for wrapscan. */
4408
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004409 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4410 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004412 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004414 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004416 found_new_match = searchit(NULL, ins_buf, pos,
4417 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004418 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004419 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004420 --msg_silent;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004421 if (!compl_started || set_match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004423 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004424 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 first_match_pos = *pos;
4426 last_match_pos = *pos;
Bram Moolenaar361aa502014-01-23 22:45:58 +01004427 set_match_pos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428 }
4429 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004430 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 found_new_match = FAIL;
4432 if (found_new_match == FAIL)
4433 {
4434 if (ins_buf == curbuf)
4435 found_all = TRUE;
4436 break;
4437 }
4438
4439 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004440 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004441 && ini->lnum == pos->lnum
4442 && ini->col == pos->col)
4443 continue;
4444 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4445 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4446 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004447 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448 {
4449 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4450 continue;
4451 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4452 if (!p_paste)
4453 ptr = skipwhite(ptr);
4454 }
4455 len = (int)STRLEN(ptr);
4456 }
4457 else
4458 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004459 char_u *tmp_ptr = ptr;
4460
4461 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004463 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 /* Skip if already inside a word. */
4465 if (vim_iswordp(tmp_ptr))
4466 continue;
4467 /* Find start of next word. */
4468 tmp_ptr = find_word_start(tmp_ptr);
4469 }
4470 /* Find end of this word. */
4471 tmp_ptr = find_word_end(tmp_ptr);
4472 len = (int)(tmp_ptr - ptr);
4473
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004474 if ((compl_cont_status & CONT_ADDING)
4475 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
4477 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4478 {
4479 /* Try next line, if any. the new word will be
4480 * "join" as if the normal command "J" was used.
4481 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004482 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 * works -- Acevedo */
4484 STRNCPY(IObuff, ptr, len);
4485 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4486 tmp_ptr = ptr = skipwhite(ptr);
4487 /* Find start of next word. */
4488 tmp_ptr = find_word_start(tmp_ptr);
4489 /* Find end of next word. */
4490 tmp_ptr = find_word_end(tmp_ptr);
4491 if (tmp_ptr > ptr)
4492 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004493 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004494 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004495 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 IObuff[len++] = ' ';
4497 /* IObuf =~ "\k.* ", thus len >= 2 */
4498 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004499 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 || (vim_strchr(p_cpo, CPO_JOINSP)
4501 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004502 && (IObuff[len - 2] == '?'
4503 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004504 IObuff[len++] = ' ';
4505 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004506 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 if (tmp_ptr - ptr >= IOSIZE - len)
4508 tmp_ptr = ptr + IOSIZE - len - 1;
4509 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4510 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004511 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004512 }
4513 IObuff[len] = NUL;
4514 ptr = IObuff;
4515 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004516 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 continue;
4518 }
4519 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004520 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004521 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004522 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 {
4524 found_new_match = OK;
4525 break;
4526 }
4527 }
4528 p_scs = save_p_scs;
4529 p_ws = save_p_ws;
4530 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004531
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004533 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004534 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 found_new_match = OK;
4536
4537 /* break the loop for specialized modes (use 'complete' just for the
4538 * generic ctrl_x_mode == 0) or when we've found a new match */
4539 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004540 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004541 {
4542 if (got_int)
4543 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004544 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004545 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004546 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004547
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004548 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4549 || compl_interrupted)
4550 break;
4551 compl_started = TRUE;
4552 }
4553 else
4554 {
4555 /* Mark a buffer scanned when it has been scanned completely */
4556 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4557 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004559 compl_started = FALSE;
4560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004562 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563
4564 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4565 && *e_cpt == NUL) /* Got to end of 'complete' */
4566 found_new_match = FAIL;
4567
4568 i = -1; /* total of matches, unknown */
4569 if (found_new_match == FAIL
4570 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4571 i = ins_compl_make_cyclic();
4572
4573 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004574 * just been made cyclic then we have to move compl_curr_match to the next
4575 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004576 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4577 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004578 if (compl_curr_match == NULL)
4579 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 return i;
4581}
4582
4583/* Delete the old text being completed. */
4584 static void
4585ins_compl_delete()
4586{
4587 int i;
4588
4589 /*
4590 * In insert mode: Delete the typed part.
4591 * In replace mode: Put the old characters back, if any.
4592 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004593 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004594 backspace_until_column(i);
4595 changed_cline_bef_curs();
4596}
4597
4598/* Insert the new text being completed. */
4599 static void
4600ins_compl_insert()
4601{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004602 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004603 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4604 compl_used_match = FALSE;
4605 else
4606 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607}
4608
4609/*
4610 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004611 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4612 * get more completions. If it is FALSE, then we just do nothing when there
4613 * are no more completions in a given direction. The latter case is used when
4614 * we are still in the middle of finding completions, to allow browsing
4615 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 * Return the total number of matches, or -1 if still unknown -- webb.
4617 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004618 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4619 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 *
4621 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004622 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4623 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 */
4625 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004626ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004628 int count; /* repeat completion this many times; should
4629 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004630 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631{
4632 int num_matches = -1;
4633 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004634 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004635 compl_T *found_compl = NULL;
4636 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004637 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004639 /* When user complete function return -1 for findstart which is next
4640 * time of 'always', compl_shown_match become NULL. */
4641 if (compl_shown_match == NULL)
4642 return -1;
4643
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004644 if (compl_leader != NULL
4645 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004647 /* Set "compl_shown_match" to the actually shown match, it may differ
4648 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004649 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004650 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004651 && compl_shown_match->cp_next != NULL
4652 && compl_shown_match->cp_next != compl_first_match)
4653 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004654
4655 /* If we didn't find it searching forward, and compl_shows_dir is
4656 * backward, find the last match. */
4657 if (compl_shows_dir == BACKWARD
4658 && !ins_compl_equal(compl_shown_match,
4659 compl_leader, (int)STRLEN(compl_leader))
4660 && (compl_shown_match->cp_next == NULL
4661 || compl_shown_match->cp_next == compl_first_match))
4662 {
4663 while (!ins_compl_equal(compl_shown_match,
4664 compl_leader, (int)STRLEN(compl_leader))
4665 && compl_shown_match->cp_prev != NULL
4666 && compl_shown_match->cp_prev != compl_first_match)
4667 compl_shown_match = compl_shown_match->cp_prev;
4668 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004669 }
4670
4671 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004672 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004673 /* Delete old text to be replaced */
4674 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004675
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004676 /* When finding the longest common text we stick at the original text,
4677 * don't let CTRL-N or CTRL-P move to the first match. */
4678 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4679
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004680 /* When restarting the search don't insert the first match either. */
4681 if (compl_restarting)
4682 {
4683 advance = FALSE;
4684 compl_restarting = FALSE;
4685 }
4686
Bram Moolenaare3226be2005-12-18 22:10:00 +00004687 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4688 * around. */
4689 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004690 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004691 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004693 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004694 found_end = (compl_first_match != NULL
4695 && (compl_shown_match->cp_next == compl_first_match
4696 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004697 }
4698 else if (compl_shows_dir == BACKWARD
4699 && compl_shown_match->cp_prev != NULL)
4700 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004701 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004702 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004703 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 }
4705 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004706 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004707 if (!allow_get_expansion)
4708 {
4709 if (advance)
4710 {
4711 if (compl_shows_dir == BACKWARD)
4712 compl_pending -= todo + 1;
4713 else
4714 compl_pending += todo + 1;
4715 }
4716 return -1;
4717 }
4718
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004719 if (advance)
4720 {
4721 if (compl_shows_dir == BACKWARD)
4722 --compl_pending;
4723 else
4724 ++compl_pending;
4725 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004726
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004727 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004728 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004729
4730 /* handle any pending completions */
4731 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004732 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004733 {
4734 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4735 {
4736 compl_shown_match = compl_shown_match->cp_next;
4737 --compl_pending;
4738 }
4739 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4740 {
4741 compl_shown_match = compl_shown_match->cp_prev;
4742 ++compl_pending;
4743 }
4744 else
4745 break;
4746 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004747 found_end = FALSE;
4748 }
4749 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4750 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004751 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004752 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004753 ++todo;
4754 else
4755 /* Remember a matching item. */
4756 found_compl = compl_shown_match;
4757
4758 /* Stop at the end of the list when we found a usable match. */
4759 if (found_end)
4760 {
4761 if (found_compl != NULL)
4762 {
4763 compl_shown_match = found_compl;
4764 break;
4765 }
4766 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004770 /* Insert the text of the new completion, or the compl_leader. */
4771 if (insert_match)
4772 {
4773 if (!compl_get_longest || compl_used_match)
4774 ins_compl_insert();
4775 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004776 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004777 }
4778 else
4779 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
4781 if (!allow_get_expansion)
4782 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004783 /* may undisplay the popup menu first */
4784 ins_compl_upd_pum();
4785
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004786 /* redraw to show the user what was inserted */
4787 update_screen(0);
4788
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004789 /* display the updated popup menu */
4790 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004791#ifdef FEAT_GUI
4792 if (gui.in_use)
4793 {
4794 /* Show the cursor after the match, not after the redrawn text. */
4795 setcursor();
4796 out_flush();
4797 gui_update_cursor(FALSE, FALSE);
4798 }
4799#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004800
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 /* Delete old text to be replaced, since we're still searching and
4802 * don't want to match ourselves! */
4803 ins_compl_delete();
4804 }
4805
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004806 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004807 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004808 compl_enter_selects = !insert_match && compl_match_array != NULL;
4809
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 /*
4811 * Show the file name for the match (if any)
4812 * Truncate the file name to avoid a wait for return.
4813 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004814 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
4816 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004817 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 if (i <= 0)
4819 i = 0;
4820 else
4821 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004822 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 msg(IObuff);
4824 redraw_cmdline = FALSE; /* don't overwrite! */
4825 }
4826
4827 return num_matches;
4828}
4829
4830/*
4831 * Call this while finding completions, to check whether the user has hit a key
4832 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004833 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004835 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 */
4837 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004838ins_compl_check_keys(frequency)
4839 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840{
4841 static int count = 0;
4842
4843 int c;
4844
4845 /* Don't check when reading keys from a script. That would break the test
4846 * scripts */
4847 if (using_script())
4848 return;
4849
4850 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004851 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 return;
4853 count = 0;
4854
Bram Moolenaara260a972006-06-23 19:36:29 +00004855 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4856 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004857 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 if (c != NUL)
4859 {
4860 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4861 {
4862 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004863 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004864 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4865 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004867 else
4868 {
4869 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004870 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004871 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004872 if (c != K_IGNORE)
4873 {
4874 /* Don't interrupt completion when the character wasn't typed,
4875 * e.g., when doing @q to replay keys. */
4876 if (c != Ctrl_R && KeyTyped)
4877 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004878
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004879 vungetc(c);
4880 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004883 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004884 {
4885 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4886
4887 compl_pending = 0;
4888 (void)ins_compl_next(FALSE, todo, TRUE);
4889 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004890}
4891
4892/*
4893 * Decide the direction of Insert mode complete from the key typed.
4894 * Returns BACKWARD or FORWARD.
4895 */
4896 static int
4897ins_compl_key2dir(c)
4898 int c;
4899{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004900 if (c == Ctrl_P || c == Ctrl_L
4901 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4902 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004903 return BACKWARD;
4904 return FORWARD;
4905}
4906
4907/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004908 * Return TRUE for keys that are used for completion only when the popup menu
4909 * is visible.
4910 */
4911 static int
4912ins_compl_pum_key(c)
4913 int c;
4914{
4915 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004916 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4917 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004918}
4919
4920/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004921 * Decide the number of completions to move forward.
4922 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4923 */
4924 static int
4925ins_compl_key2count(c)
4926 int c;
4927{
4928 int h;
4929
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004930 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004931 {
4932 h = pum_get_height();
4933 if (h > 3)
4934 h -= 2; /* keep some context */
4935 return h;
4936 }
4937 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004938}
4939
4940/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004941 * Return TRUE if completion with "c" should insert the match, FALSE if only
4942 * to change the currently selected completion.
4943 */
4944 static int
4945ins_compl_use_match(c)
4946 int c;
4947{
4948 switch (c)
4949 {
4950 case K_UP:
4951 case K_DOWN:
4952 case K_PAGEDOWN:
4953 case K_KPAGEDOWN:
4954 case K_S_DOWN:
4955 case K_PAGEUP:
4956 case K_KPAGEUP:
4957 case K_S_UP:
4958 return FALSE;
4959 }
4960 return TRUE;
4961}
4962
4963/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004964 * Do Insert mode completion.
4965 * Called when character "c" was typed, which has a meaning for completion.
4966 * Returns OK if completion was done, FAIL if something failed (out of mem).
4967 */
4968 static int
4969ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004970 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004972 char_u *line;
4973 int startcol = 0; /* column where searched text starts */
4974 colnr_T curs_col; /* cursor column */
4975 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004976 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977
Bram Moolenaare3226be2005-12-18 22:10:00 +00004978 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004979 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 {
4981 /* First time we hit ^N or ^P (in a row, I mean) */
4982
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 did_ai = FALSE;
4984#ifdef FEAT_SMARTINDENT
4985 did_si = FALSE;
4986 can_si = FALSE;
4987 can_si_back = FALSE;
4988#endif
4989 if (stop_arrow() == FAIL)
4990 return FAIL;
4991
4992 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004993 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004994 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004996 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004997 * "compl_startpos" to the cursor as a pattern to add a new word
4998 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004999 * "compl_startpos" is not in the same line as the cursor then fix it
5000 * (the line has been split because it was longer than 'tw'). if SOL
5001 * is set then skip the previous pattern, a word at the beginning of
5002 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005003 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
5004 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 /*
5007 * it is a continued search
5008 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005009 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
5011 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5012 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005013 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005015 /* line (probably) wrapped, set compl_startpos to the
5016 * first non_blank in the line, if it is not a wordchar
5017 * include it to get a better pattern, but then we don't
5018 * want the "\\<" prefix, check it bellow */
5019 compl_col = (colnr_T)(skipwhite(line) - line);
5020 compl_startpos.col = compl_col;
5021 compl_startpos.lnum = curwin->w_cursor.lnum;
5022 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 else
5025 {
5026 /* S_IPOS was set when we inserted a word that was at the
5027 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005028 * mode but first we need to redefine compl_startpos */
5029 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005031 compl_cont_status |= CONT_SOL;
5032 compl_startpos.col = (colnr_T)(skipwhite(
5033 line + compl_length
5034 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005035 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005036 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005037 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005038 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005039 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00005040 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005042 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005044 compl_cont_status &= ~CONT_SOL;
5045 compl_length = (IOSIZE - MIN_SPACE);
5046 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005048 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
5049 if (compl_length < 1)
5050 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 }
5052 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005053 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005055 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 }
5057 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005058 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005060 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005062 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005064 compl_cont_status = 0;
5065 compl_cont_status |= CONT_N_ADDS;
5066 compl_startpos = curwin->w_cursor;
5067 startcol = (int)curs_col;
5068 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
5070
5071 /* Work out completion pattern and original text -- webb */
5072 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5073 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005074 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5076 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005077 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005079 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005081 compl_col += ++startcol;
5082 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 }
5084 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005085 compl_pattern = str_foldcase(line + compl_col,
5086 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005088 compl_pattern = vim_strnsave(line + compl_col,
5089 compl_length);
5090 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091 return FAIL;
5092 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005093 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 char_u *prefix = (char_u *)"\\<";
5096
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005097 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005098 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005099 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005100 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 if (!vim_iswordp(line + compl_col)
5103 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 && (
5105#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005106 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005108 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109#endif
5110 )))
5111 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 STRCPY((char *)compl_pattern, prefix);
5113 (void)quote_meta(compl_pattern + STRLEN(prefix),
5114 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005118 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005120 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121#endif
5122 )
5123 {
5124 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5126 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005128 compl_col += curs_col;
5129 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 }
5131 else
5132 {
5133#ifdef FEAT_MBYTE
5134 /* Search the point of change class of multibyte character
5135 * or not a word single byte character backward. */
5136 if (has_mbyte)
5137 {
5138 int base_class;
5139 int head_off;
5140
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 startcol -= (*mb_head_off)(line, line + startcol);
5142 base_class = mb_get_class(line + startcol);
5143 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005145 head_off = (*mb_head_off)(line, line + startcol);
5146 if (base_class != mb_get_class(line + startcol
5147 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005149 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 }
5152 else
5153#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005156 compl_col += ++startcol;
5157 compl_length = (int)curs_col - startcol;
5158 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
5160 /* Only match word with at least two chars -- webb
5161 * there's no need to call quote_meta,
5162 * alloc(7) is enough -- Acevedo
5163 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005164 compl_pattern = alloc(7);
5165 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 STRCPY((char *)compl_pattern, "\\<");
5168 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5169 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 }
5171 else
5172 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005174 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005175 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 STRCPY((char *)compl_pattern, "\\<");
5178 (void)quote_meta(compl_pattern + 2, line + compl_col,
5179 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 }
5181 }
5182 }
5183 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5184 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005185 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005186 compl_length = (int)curs_col - (int)compl_col;
5187 if (compl_length < 0) /* cursor in indent: empty pattern */
5188 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005190 compl_pattern = str_foldcase(line + compl_col, compl_length,
5191 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005193 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5194 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 return FAIL;
5196 }
5197 else if (ctrl_x_mode == CTRL_X_FILES)
5198 {
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005199 /* Go back to just before the first filename character. */
Bram Moolenaardd407342013-09-08 20:00:48 +02005200 if (startcol > 0)
5201 {
5202 char_u *p = line + startcol;
5203
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005204 mb_ptr_back(line, p);
Bram Moolenaardd407342013-09-08 20:00:48 +02005205 while (p > line && vim_isfilec(PTR2CHAR(p)))
5206 mb_ptr_back(line, p);
5207 if (p == line && vim_isfilec(PTR2CHAR(p)))
5208 startcol = 0;
5209 else
5210 startcol = (int)(p - line) + 1;
5211 }
Bram Moolenaar00b764a2013-09-05 13:50:53 +02005212
Bram Moolenaar0300e462013-09-08 16:03:45 +02005213 compl_col += startcol;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005214 compl_length = (int)curs_col - startcol;
5215 compl_pattern = addstar(line + compl_col, compl_length,
5216 EXPAND_FILES);
5217 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 return FAIL;
5219 }
5220 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5221 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005222 compl_pattern = vim_strnsave(line, curs_col);
5223 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005224 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 set_cmd_context(&compl_xp, compl_pattern,
5226 (int)STRLEN(compl_pattern), curs_col);
5227 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5228 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005229 /* No completion possible, use an empty pattern to get a
5230 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005231 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005232 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005233 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5234 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005235 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005236 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005237 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005238#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005239 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005240 * Call user defined function 'completefunc' with "a:findstart"
5241 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005242 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005243 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005244 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005245 char_u *funcname;
5246 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005247 win_T *curwin_save;
5248 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005249
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005250 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005251 * string */
5252 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5253 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5254 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005255 {
5256 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5257 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005258 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005259 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005260
5261 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005262 args[1] = NULL;
5263 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005264 curwin_save = curwin;
5265 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005266 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005267 if (curwin_save != curwin || curbuf_save != curbuf)
5268 {
5269 EMSG(_(e_complwin));
5270 return FAIL;
5271 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005272 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005273 check_cursor();
5274 if (!equalpos(curwin->w_cursor, pos))
5275 {
5276 EMSG(_(e_compldel));
5277 return FAIL;
5278 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005279
Bram Moolenaar6110a002012-01-26 18:58:38 +01005280 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005281 * cancel the complete without an error.
5282 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005283 if (col == -2)
5284 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005285 if (col == -3)
5286 {
5287 ctrl_x_mode = 0;
5288 edit_submode = NULL;
Bram Moolenaarea389e92014-05-28 21:40:52 +02005289 if (!shortmess(SHM_COMPLETIONMENU))
5290 msg_clr_cmdline();
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005291 return FAIL;
5292 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005293
Bram Moolenaar82139082011-09-14 16:52:09 +02005294 /*
5295 * Reset extended parameters of completion, when start new
5296 * completion.
5297 */
5298 compl_opt_refresh_always = FALSE;
5299
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005300 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005301 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005302 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005303 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005304 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005305
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005306 /* Setup variables for completion. Need to obtain "line" again,
5307 * it may have become invalid. */
5308 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005309 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005310 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5311 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005312#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005313 return FAIL;
5314 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005315 else if (ctrl_x_mode == CTRL_X_SPELL)
5316 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005317#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005318 if (spell_bad_len > 0)
5319 compl_col = curs_col - spell_bad_len;
5320 else
5321 compl_col = spell_word_start(startcol);
5322 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005323 {
5324 compl_length = 0;
5325 compl_col = curs_col;
5326 }
5327 else
5328 {
5329 spell_expand_check_cap(compl_col);
5330 compl_length = (int)curs_col - compl_col;
5331 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005332 /* Need to obtain "line" again, it may have become invalid. */
5333 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005334 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5335 if (compl_pattern == NULL)
5336#endif
5337 return FAIL;
5338 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005339 else
5340 {
5341 EMSG2(_(e_intern2), "ins_complete()");
5342 return FAIL;
5343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005345 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
5347 edit_submode_pre = (char_u *)_(" Adding");
5348 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5349 {
5350 /* Insert a new line, keep indentation but ignore 'comments' */
5351#ifdef FEAT_COMMENTS
5352 char_u *old = curbuf->b_p_com;
5353
5354 curbuf->b_p_com = (char_u *)"";
5355#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005356 compl_startpos.lnum = curwin->w_cursor.lnum;
5357 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005358 ins_eol('\r');
5359#ifdef FEAT_COMMENTS
5360 curbuf->b_p_com = old;
5361#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005362 compl_length = 0;
5363 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005364 }
5365 }
5366 else
5367 {
5368 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005369 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 }
5371
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 if (compl_cont_status & CONT_LOCAL)
5373 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005374 else
5375 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5376
Bram Moolenaar62951b12011-09-21 18:23:05 +02005377 /* If any of the original typed text has been changed we need to fix
5378 * the redo buffer. */
5379 ins_compl_fixRedoBufForLeader(NULL);
5380
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005381 /* Always add completion for the original text. */
5382 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005383 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5384 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005385 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005387 vim_free(compl_pattern);
5388 compl_pattern = NULL;
5389 vim_free(compl_orig_text);
5390 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005391 return FAIL;
5392 }
5393
5394 /* showmode might reset the internal line pointers, so it must
5395 * be called before line = ml_get(), or when this address is no
5396 * longer needed. -- Acevedo.
5397 */
5398 edit_submode_extra = (char_u *)_("-- Searching...");
5399 edit_submode_highl = HLF_COUNT;
5400 showmode();
5401 edit_submode_extra = NULL;
5402 out_flush();
5403 }
5404
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005405 compl_shown_match = compl_curr_match;
5406 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407
5408 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005409 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005411 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005412 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005414 /* may undisplay the popup menu */
5415 ins_compl_upd_pum();
5416
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005417 if (n > 1) /* all matches have been found */
5418 compl_matches = n;
5419 compl_curr_match = compl_shown_match;
5420 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421
Bram Moolenaard68071d2006-05-02 22:08:30 +00005422 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5423 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005424 if (got_int && !global_busy)
5425 {
5426 (void)vgetc();
5427 got_int = FALSE;
5428 }
5429
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005430 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005431 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005432 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005433 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5434 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005435 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5436 edit_submode_highl = HLF_E;
5437 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5438 * because we couldn't expand anything at first place, but if we used
5439 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5440 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005441 if ( compl_length > 1
5442 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005443 || (ctrl_x_mode != 0
5444 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5445 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005446 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005447 }
5448
Bram Moolenaar572cb562005-08-05 21:35:02 +00005449 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005451 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005452 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453
5454 if (edit_submode_extra == NULL)
5455 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005456 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 {
5458 edit_submode_extra = (char_u *)_("Back at original");
5459 edit_submode_highl = HLF_W;
5460 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005461 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005462 {
5463 edit_submode_extra = (char_u *)_("Word from other line");
5464 edit_submode_highl = HLF_COUNT;
5465 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005466 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005467 {
5468 edit_submode_extra = (char_u *)_("The only match");
5469 edit_submode_highl = HLF_COUNT;
5470 }
5471 else
5472 {
5473 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005474 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005475 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005476 int number = 0;
5477 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005478
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005479 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480 {
5481 /* search backwards for the first valid (!= -1) number.
5482 * This should normally succeed already at the first loop
5483 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005484 for (match = compl_curr_match->cp_prev; match != NULL
5485 && match != compl_first_match;
5486 match = match->cp_prev)
5487 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005489 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005490 break;
5491 }
5492 if (match != NULL)
5493 /* go up and assign all numbers which are not assigned
5494 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005495 for (match = match->cp_next;
5496 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005497 match = match->cp_next)
5498 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005499 }
5500 else /* BACKWARD */
5501 {
5502 /* search forwards (upwards) for the first valid (!= -1)
5503 * number. This should normally succeed already at the
5504 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005505 for (match = compl_curr_match->cp_next; match != NULL
5506 && match != compl_first_match;
5507 match = match->cp_next)
5508 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005510 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511 break;
5512 }
5513 if (match != NULL)
5514 /* go down and assign all numbers which are not
5515 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005516 for (match = match->cp_prev; match
5517 && match->cp_number == -1;
5518 match = match->cp_prev)
5519 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005520 }
5521 }
5522
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005523 /* The match should always have a sequence number now, this is
5524 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005525 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005527 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5528 * Translations may need more than twice that. */
5529 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005531 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005532 vim_snprintf((char *)match_ref, sizeof(match_ref),
5533 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005534 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005535 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005536 vim_snprintf((char *)match_ref, sizeof(match_ref),
5537 _("match %d"),
5538 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005539 edit_submode_extra = match_ref;
5540 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005541 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005542 curs_columns(FALSE);
5543 }
5544 }
5545 }
5546
5547 /* Show a message about what (completion) mode we're in. */
5548 showmode();
Bram Moolenaarea389e92014-05-28 21:40:52 +02005549 if (!shortmess(SHM_COMPLETIONMENU))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 {
Bram Moolenaarea389e92014-05-28 21:40:52 +02005551 if (edit_submode_extra != NULL)
5552 {
5553 if (!p_smd)
5554 msg_attr(edit_submode_extra,
5555 edit_submode_highl < HLF_COUNT
5556 ? hl_attr(edit_submode_highl) : 0);
5557 }
5558 else
5559 msg_clr_cmdline(); /* necessary for "noshowmode" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005561
Bram Moolenaard68071d2006-05-02 22:08:30 +00005562 /* Show the popup menu, unless we got interrupted. */
5563 if (!compl_interrupted)
5564 {
5565 /* RedrawingDisabled may be set when invoked through complete(). */
5566 n = RedrawingDisabled;
5567 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005568
5569 /* If the cursor moved we need to remove the pum first. */
5570 setcursor();
5571 if (save_w_wrow != curwin->w_wrow)
5572 ins_compl_del_pum();
5573
Bram Moolenaard68071d2006-05-02 22:08:30 +00005574 ins_compl_show_pum();
5575 setcursor();
5576 RedrawingDisabled = n;
5577 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005578 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005579 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005580
Bram Moolenaar071d4272004-06-13 20:20:40 +00005581 return OK;
5582}
5583
5584/*
5585 * Looks in the first "len" chars. of "src" for search-metachars.
5586 * If dest is not NULL the chars. are copied there quoting (with
5587 * a backslash) the metachars, and dest would be NUL terminated.
5588 * Returns the length (needed) of dest
5589 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005590 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591quote_meta(dest, src, len)
5592 char_u *dest;
5593 char_u *src;
5594 int len;
5595{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005596 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005597
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005598 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005599 {
5600 switch (*src)
5601 {
5602 case '.':
5603 case '*':
5604 case '[':
5605 if (ctrl_x_mode == CTRL_X_DICTIONARY
5606 || ctrl_x_mode == CTRL_X_THESAURUS)
5607 break;
5608 case '~':
5609 if (!p_magic) /* quote these only if magic is set */
5610 break;
5611 case '\\':
5612 if (ctrl_x_mode == CTRL_X_DICTIONARY
5613 || ctrl_x_mode == CTRL_X_THESAURUS)
5614 break;
5615 case '^': /* currently it's not needed. */
5616 case '$':
5617 m++;
5618 if (dest != NULL)
5619 *dest++ = '\\';
5620 break;
5621 }
5622 if (dest != NULL)
5623 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005624# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005625 /* Copy remaining bytes of a multibyte character. */
5626 if (has_mbyte)
5627 {
5628 int i, mb_len;
5629
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005630 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 if (mb_len > 0 && len >= mb_len)
5632 for (i = 0; i < mb_len; ++i)
5633 {
5634 --len;
5635 ++src;
5636 if (dest != NULL)
5637 *dest++ = *src;
5638 }
5639 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005640# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005641 }
5642 if (dest != NULL)
5643 *dest = NUL;
5644
5645 return m;
5646}
5647#endif /* FEAT_INS_EXPAND */
5648
5649/*
5650 * Next character is interpreted literally.
5651 * A one, two or three digit decimal number is interpreted as its byte value.
5652 * If one or two digits are entered, the next character is given to vungetc().
5653 * For Unicode a character > 255 may be returned.
5654 */
5655 int
5656get_literal()
5657{
5658 int cc;
5659 int nc;
5660 int i;
5661 int hex = FALSE;
5662 int octal = FALSE;
5663#ifdef FEAT_MBYTE
5664 int unicode = 0;
5665#endif
5666
5667 if (got_int)
5668 return Ctrl_C;
5669
5670#ifdef FEAT_GUI
5671 /*
5672 * In GUI there is no point inserting the internal code for a special key.
5673 * It is more useful to insert the string "<KEY>" instead. This would
5674 * probably be useful in a text window too, but it would not be
5675 * vi-compatible (maybe there should be an option for it?) -- webb
5676 */
5677 if (gui.in_use)
5678 ++allow_keys;
5679#endif
5680#ifdef USE_ON_FLY_SCROLL
5681 dont_scroll = TRUE; /* disallow scrolling here */
5682#endif
5683 ++no_mapping; /* don't map the next key hits */
5684 cc = 0;
5685 i = 0;
5686 for (;;)
5687 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005688 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689#ifdef FEAT_CMDL_INFO
5690 if (!(State & CMDLINE)
5691# ifdef FEAT_MBYTE
5692 && MB_BYTE2LEN_CHECK(nc) == 1
5693# endif
5694 )
5695 add_to_showcmd(nc);
5696#endif
5697 if (nc == 'x' || nc == 'X')
5698 hex = TRUE;
5699 else if (nc == 'o' || nc == 'O')
5700 octal = TRUE;
5701#ifdef FEAT_MBYTE
5702 else if (nc == 'u' || nc == 'U')
5703 unicode = nc;
5704#endif
5705 else
5706 {
5707 if (hex
5708#ifdef FEAT_MBYTE
5709 || unicode != 0
5710#endif
5711 )
5712 {
5713 if (!vim_isxdigit(nc))
5714 break;
5715 cc = cc * 16 + hex2nr(nc);
5716 }
5717 else if (octal)
5718 {
5719 if (nc < '0' || nc > '7')
5720 break;
5721 cc = cc * 8 + nc - '0';
5722 }
5723 else
5724 {
5725 if (!VIM_ISDIGIT(nc))
5726 break;
5727 cc = cc * 10 + nc - '0';
5728 }
5729
5730 ++i;
5731 }
5732
5733 if (cc > 255
5734#ifdef FEAT_MBYTE
5735 && unicode == 0
5736#endif
5737 )
5738 cc = 255; /* limit range to 0-255 */
5739 nc = 0;
5740
5741 if (hex) /* hex: up to two chars */
5742 {
5743 if (i >= 2)
5744 break;
5745 }
5746#ifdef FEAT_MBYTE
5747 else if (unicode) /* Unicode: up to four or eight chars */
5748 {
5749 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5750 break;
5751 }
5752#endif
5753 else if (i >= 3) /* decimal or octal: up to three chars */
5754 break;
5755 }
5756 if (i == 0) /* no number entered */
5757 {
5758 if (nc == K_ZERO) /* NUL is stored as NL */
5759 {
5760 cc = '\n';
5761 nc = 0;
5762 }
5763 else
5764 {
5765 cc = nc;
5766 nc = 0;
5767 }
5768 }
5769
5770 if (cc == 0) /* NUL is stored as NL */
5771 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005772#ifdef FEAT_MBYTE
5773 if (enc_dbcs && (cc & 0xff) == 0)
5774 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5775 second byte will cause trouble! */
5776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
5778 --no_mapping;
5779#ifdef FEAT_GUI
5780 if (gui.in_use)
5781 --allow_keys;
5782#endif
5783 if (nc)
5784 vungetc(nc);
5785 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5786 return cc;
5787}
5788
5789/*
5790 * Insert character, taking care of special keys and mod_mask
5791 */
5792 static void
5793insert_special(c, allow_modmask, ctrlv)
5794 int c;
5795 int allow_modmask;
5796 int ctrlv; /* c was typed after CTRL-V */
5797{
5798 char_u *p;
5799 int len;
5800
5801 /*
5802 * Special function key, translate into "<Key>". Up to the last '>' is
5803 * inserted with ins_str(), so as not to replace characters in replace
5804 * mode.
5805 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5806 * unless 'allow_modmask' is TRUE.
5807 */
5808#ifdef MACOS
5809 /* Command-key never produces a normal key */
5810 if (mod_mask & MOD_MASK_CMD)
5811 allow_modmask = TRUE;
5812#endif
5813 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5814 {
5815 p = get_special_key_name(c, mod_mask);
5816 len = (int)STRLEN(p);
5817 c = p[len - 1];
5818 if (len > 2)
5819 {
5820 if (stop_arrow() == FAIL)
5821 return;
5822 p[len - 1] = NUL;
5823 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005824 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 ctrlv = FALSE;
5826 }
5827 }
5828 if (stop_arrow() == OK)
5829 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5830}
5831
5832/*
5833 * Special characters in this context are those that need processing other
5834 * than the simple insertion that can be performed here. This includes ESC
5835 * which terminates the insert, and CR/NL which need special processing to
5836 * open up a new line. This routine tries to optimize insertions performed by
5837 * the "redo", "undo" or "put" commands, so it needs to know when it should
5838 * stop and defer processing to the "normal" mechanism.
5839 * '0' and '^' are special, because they can be followed by CTRL-D.
5840 */
5841#ifdef EBCDIC
5842# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5843#else
5844# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5845#endif
5846
5847#ifdef FEAT_MBYTE
5848# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5849#else
5850# define WHITECHAR(cc) vim_iswhite(cc)
5851#endif
5852
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005853/*
5854 * "flags": INSCHAR_FORMAT - force formatting
5855 * INSCHAR_CTRLV - char typed just after CTRL-V
5856 * INSCHAR_NO_FEX - don't use 'formatexpr'
5857 *
5858 * NOTE: passes the flags value straight through to internal_format() which,
5859 * beside INSCHAR_FORMAT (above), is also looking for these:
5860 * INSCHAR_DO_COM - format comments
5861 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5862 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 void
5864insertchar(c, flags, second_indent)
5865 int c; /* character to insert or NUL */
5866 int flags; /* INSCHAR_FORMAT, etc. */
5867 int second_indent; /* indent for second line if >= 0 */
5868{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005869 int textwidth;
5870#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005872#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874
5875 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5876 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877
5878 /*
5879 * Try to break the line in two or more pieces when:
5880 * - Always do this if we have been called to do formatting only.
5881 * - Always do this when 'formatoptions' has the 'a' flag and the line
5882 * ends in white space.
5883 * - Otherwise:
5884 * - Don't do this if inserting a blank
5885 * - Don't do this if an existing character is being replaced, unless
5886 * we're in VREPLACE mode.
5887 * - Do this if the cursor is not on the line where insert started
5888 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5889 * before the insert.
5890 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5891 * before 'textwidth'
5892 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005893 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005894 && ((flags & INSCHAR_FORMAT)
5895 || (!vim_iswhite(c)
5896 && !((State & REPLACE_FLAG)
5897#ifdef FEAT_VREPLACE
5898 && !(State & VREPLACE_FLAG)
5899#endif
5900 && *ml_get_cursor() != NUL)
5901 && (curwin->w_cursor.lnum != Insstart.lnum
5902 || ((!has_format_option(FO_INS_LONG)
5903 || Insstart_textlen <= (colnr_T)textwidth)
5904 && (!fo_ins_blank
5905 || Insstart_blank_vcol <= (colnr_T)textwidth
5906 ))))))
5907 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005908 /* Format with 'formatexpr' when it's set. Use internal formatting
5909 * when 'formatexpr' isn't set or it returns non-zero. */
5910#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005911 int do_internal = TRUE;
5912
Bram Moolenaar81a82092008-03-12 16:27:00 +00005913 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005914 {
5915 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5916 /* It may be required to save for undo again, e.g. when setline()
5917 * was called. */
5918 ins_need_undo = TRUE;
5919 }
5920 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005921#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005922 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005924
Bram Moolenaar071d4272004-06-13 20:20:40 +00005925 if (c == NUL) /* only formatting was wanted */
5926 return;
5927
5928#ifdef FEAT_COMMENTS
5929 /* Check whether this character should end a comment. */
5930 if (did_ai && (int)c == end_comment_pending)
5931 {
5932 char_u *line;
5933 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5934 int middle_len, end_len;
5935 int i;
5936
5937 /*
5938 * Need to remove existing (middle) comment leader and insert end
5939 * comment leader. First, check what comment leader we can find.
5940 */
Bram Moolenaar81340392012-06-06 16:12:59 +02005941 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005942 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5943 {
5944 /* Skip middle-comment string */
5945 while (*p && p[-1] != ':') /* find end of middle flags */
5946 ++p;
5947 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5948 /* Don't count trailing white space for middle_len */
5949 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5950 --middle_len;
5951
5952 /* Find the end-comment string */
5953 while (*p && p[-1] != ':') /* find end of end flags */
5954 ++p;
5955 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5956
5957 /* Skip white space before the cursor */
5958 i = curwin->w_cursor.col;
5959 while (--i >= 0 && vim_iswhite(line[i]))
5960 ;
5961 i++;
5962
5963 /* Skip to before the middle leader */
5964 i -= middle_len;
5965
5966 /* Check some expected things before we go on */
5967 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5968 {
5969 /* Backspace over all the stuff we want to replace */
5970 backspace_until_column(i);
5971
5972 /*
5973 * Insert the end-comment string, except for the last
5974 * character, which will get inserted as normal later.
5975 */
5976 ins_bytes_len(lead_end, end_len - 1);
5977 }
5978 }
5979 }
5980 end_comment_pending = NUL;
5981#endif
5982
5983 did_ai = FALSE;
5984#ifdef FEAT_SMARTINDENT
5985 did_si = FALSE;
5986 can_si = FALSE;
5987 can_si_back = FALSE;
5988#endif
5989
5990 /*
5991 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5992 * This speeds up normal text input considerably.
5993 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5994 * need to re-indent at a ':', or any other character (but not what
5995 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005996 * Don't do this when there an InsertCharPre autocommand is defined,
5997 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005998 */
5999#ifdef USE_ON_FLY_SCROLL
6000 dont_scroll = FALSE; /* allow scrolling here */
6001#endif
6002
6003 if ( !ISSPECIAL(c)
6004#ifdef FEAT_MBYTE
6005 && (!has_mbyte || (*mb_char2len)(c) == 1)
6006#endif
6007 && vpeekc() != NUL
6008 && !(State & REPLACE_FLAG)
6009#ifdef FEAT_CINDENT
6010 && !cindent_on()
6011#endif
6012#ifdef FEAT_RIGHTLEFT
6013 && !p_ri
6014#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01006015#ifdef FEAT_AUTOCMD
6016 && !has_insertcharpre()
6017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006018 )
6019 {
6020#define INPUT_BUFLEN 100
6021 char_u buf[INPUT_BUFLEN + 1];
6022 int i;
6023 colnr_T virtcol = 0;
6024
6025 buf[0] = c;
6026 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006027 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006028 virtcol = get_nolist_virtcol();
6029 /*
6030 * Stop the string when:
6031 * - no more chars available
6032 * - finding a special character (command key)
6033 * - buffer is full
6034 * - running into the 'textwidth' boundary
6035 * - need to check for abbreviation: A non-word char after a word-char
6036 */
6037 while ( (c = vpeekc()) != NUL
6038 && !ISSPECIAL(c)
6039#ifdef FEAT_MBYTE
6040 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
6041#endif
6042 && i < INPUT_BUFLEN
6043 && (textwidth == 0
6044 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
6045 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
6046 {
6047#ifdef FEAT_RIGHTLEFT
6048 c = vgetc();
6049 if (p_hkmap && KeyTyped)
6050 c = hkmap(c); /* Hebrew mode mapping */
6051# ifdef FEAT_FKMAP
6052 if (p_fkmap && KeyTyped)
6053 c = fkmap(c); /* Farsi mode mapping */
6054# endif
6055 buf[i++] = c;
6056#else
6057 buf[i++] = vgetc();
6058#endif
6059 }
6060
6061#ifdef FEAT_DIGRAPHS
6062 do_digraph(-1); /* clear digraphs */
6063 do_digraph(buf[i-1]); /* may be the start of a digraph */
6064#endif
6065 buf[i] = NUL;
6066 ins_str(buf);
6067 if (flags & INSCHAR_CTRLV)
6068 {
6069 redo_literal(*buf);
6070 i = 1;
6071 }
6072 else
6073 i = 0;
6074 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00006075 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006076 }
6077 else
6078 {
6079#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006080 int cc;
6081
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6083 {
6084 char_u buf[MB_MAXBYTES + 1];
6085
6086 (*mb_char2bytes)(c, buf);
6087 buf[cc] = NUL;
6088 ins_char_bytes(buf, cc);
6089 AppendCharToRedobuff(c);
6090 }
6091 else
6092#endif
6093 {
6094 ins_char(c);
6095 if (flags & INSCHAR_CTRLV)
6096 redo_literal(c);
6097 else
6098 AppendCharToRedobuff(c);
6099 }
6100 }
6101}
6102
6103/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006104 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006105 *
6106 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6107 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006108 */
6109 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006110internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006111 int textwidth;
6112 int second_indent;
6113 int flags;
6114 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006115 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006116{
6117 int cc;
6118 int save_char = NUL;
6119 int haveto_redraw = FALSE;
6120 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6121#ifdef FEAT_MBYTE
6122 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6123#endif
6124 int fo_white_par = has_format_option(FO_WHITE_PAR);
6125 int first_line = TRUE;
6126#ifdef FEAT_COMMENTS
6127 colnr_T leader_len;
6128 int no_leader = FALSE;
6129 int do_comments = (flags & INSCHAR_DO_COM);
6130#endif
6131
6132 /*
6133 * When 'ai' is off we don't want a space under the cursor to be
6134 * deleted. Replace it with an 'x' temporarily.
6135 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006136 if (!curbuf->b_p_ai
6137#ifdef FEAT_VREPLACE
6138 && !(State & VREPLACE_FLAG)
6139#endif
6140 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006141 {
6142 cc = gchar_cursor();
6143 if (vim_iswhite(cc))
6144 {
6145 save_char = cc;
6146 pchar_cursor('x');
6147 }
6148 }
6149
6150 /*
6151 * Repeat breaking lines, until the current line is not too long.
6152 */
6153 while (!got_int)
6154 {
6155 int startcol; /* Cursor column at entry */
6156 int wantcol; /* column at textwidth border */
6157 int foundcol; /* column for start of spaces */
6158 int end_foundcol = 0; /* column for start of word */
6159 colnr_T len;
6160 colnr_T virtcol;
6161#ifdef FEAT_VREPLACE
6162 int orig_col = 0;
6163 char_u *saved_text = NULL;
6164#endif
6165 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006166 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006167
Bram Moolenaar97b98102009-11-17 16:41:01 +00006168 virtcol = get_nolist_virtcol()
6169 + char2cells(c != NUL ? c : gchar_cursor());
6170 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006171 break;
6172
6173#ifdef FEAT_COMMENTS
6174 if (no_leader)
6175 do_comments = FALSE;
6176 else if (!(flags & INSCHAR_FORMAT)
6177 && has_format_option(FO_WRAP_COMS))
6178 do_comments = TRUE;
6179
6180 /* Don't break until after the comment leader */
6181 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006182 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006183 else
6184 leader_len = 0;
6185
6186 /* If the line doesn't start with a comment leader, then don't
6187 * start one in a following broken line. Avoids that a %word
6188 * moved to the start of the next line causes all following lines
6189 * to start with %. */
6190 if (leader_len == 0)
6191 no_leader = TRUE;
6192#endif
6193 if (!(flags & INSCHAR_FORMAT)
6194#ifdef FEAT_COMMENTS
6195 && leader_len == 0
6196#endif
6197 && !has_format_option(FO_WRAP))
6198
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006199 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006200 if ((startcol = curwin->w_cursor.col) == 0)
6201 break;
6202
6203 /* find column of textwidth border */
6204 coladvance((colnr_T)textwidth);
6205 wantcol = curwin->w_cursor.col;
6206
Bram Moolenaar97b98102009-11-17 16:41:01 +00006207 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006208 foundcol = 0;
6209
6210 /*
6211 * Find position to break at.
6212 * Stop at first entered white when 'formatoptions' has 'v'
6213 */
6214 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006215 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006216 || curwin->w_cursor.lnum != Insstart.lnum
6217 || curwin->w_cursor.col >= Insstart.col)
6218 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006219 if (curwin->w_cursor.col == startcol && c != NUL)
6220 cc = c;
6221 else
6222 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006223 if (WHITECHAR(cc))
6224 {
6225 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006226 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006227
6228 /* find start of sequence of blanks */
6229 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6230 {
6231 dec_cursor();
6232 cc = gchar_cursor();
6233 }
6234 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6235 break; /* only spaces in front of text */
6236#ifdef FEAT_COMMENTS
6237 /* Don't break until after the comment leader */
6238 if (curwin->w_cursor.col < leader_len)
6239 break;
6240#endif
6241 if (has_format_option(FO_ONE_LETTER))
6242 {
6243 /* do not break after one-letter words */
6244 if (curwin->w_cursor.col == 0)
6245 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006246#ifdef FEAT_COMMENTS
6247 /* do not break "#a b" when 'tw' is 2 */
6248 if (curwin->w_cursor.col <= leader_len)
6249 break;
6250#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006251 col = curwin->w_cursor.col;
6252 dec_cursor();
6253 cc = gchar_cursor();
6254
6255 if (WHITECHAR(cc))
6256 continue; /* one-letter, continue */
6257 curwin->w_cursor.col = col;
6258 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006259
6260 inc_cursor();
6261
6262 end_foundcol = end_col + 1;
6263 foundcol = curwin->w_cursor.col;
6264 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006265 break;
6266 }
6267#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006268 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006269 {
6270 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006271 if (curwin->w_cursor.col != startcol)
6272 {
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 col = curwin->w_cursor.col;
6279 inc_cursor();
6280 /* Don't change end_foundcol if already set. */
6281 if (foundcol != curwin->w_cursor.col)
6282 {
6283 foundcol = curwin->w_cursor.col;
6284 end_foundcol = foundcol;
6285 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6286 break;
6287 }
6288 curwin->w_cursor.col = col;
6289 }
6290
6291 if (curwin->w_cursor.col == 0)
6292 break;
6293
6294 col = curwin->w_cursor.col;
6295
6296 dec_cursor();
6297 cc = gchar_cursor();
6298
6299 if (WHITECHAR(cc))
6300 continue; /* break with space */
6301#ifdef FEAT_COMMENTS
6302 /* Don't break until after the comment leader */
6303 if (curwin->w_cursor.col < leader_len)
6304 break;
6305#endif
6306
6307 curwin->w_cursor.col = col;
6308
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006309 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006310 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006311 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6312 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006313 }
6314#endif
6315 if (curwin->w_cursor.col == 0)
6316 break;
6317 dec_cursor();
6318 }
6319
6320 if (foundcol == 0) /* no spaces, cannot break line */
6321 {
6322 curwin->w_cursor.col = startcol;
6323 break;
6324 }
6325
6326 /* Going to break the line, remove any "$" now. */
6327 undisplay_dollar();
6328
6329 /*
6330 * Offset between cursor position and line break is used by replace
6331 * stack functions. VREPLACE does not use this, and backspaces
6332 * over the text instead.
6333 */
6334#ifdef FEAT_VREPLACE
6335 if (State & VREPLACE_FLAG)
6336 orig_col = startcol; /* Will start backspacing from here */
6337 else
6338#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006339 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006340
6341 /*
6342 * adjust startcol for spaces that will be deleted and
6343 * characters that will remain on top line
6344 */
6345 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006346 while ((cc = gchar_cursor(), WHITECHAR(cc))
6347 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006348 inc_cursor();
6349 startcol -= curwin->w_cursor.col;
6350 if (startcol < 0)
6351 startcol = 0;
6352
6353#ifdef FEAT_VREPLACE
6354 if (State & VREPLACE_FLAG)
6355 {
6356 /*
6357 * In VREPLACE mode, we will backspace over the text to be
6358 * wrapped, so save a copy now to put on the next line.
6359 */
6360 saved_text = vim_strsave(ml_get_cursor());
6361 curwin->w_cursor.col = orig_col;
6362 if (saved_text == NULL)
6363 break; /* Can't do it, out of memory */
6364 saved_text[startcol] = NUL;
6365
6366 /* Backspace over characters that will move to the next line */
6367 if (!fo_white_par)
6368 backspace_until_column(foundcol);
6369 }
6370 else
6371#endif
6372 {
6373 /* put cursor after pos. to break line */
6374 if (!fo_white_par)
6375 curwin->w_cursor.col = foundcol;
6376 }
6377
6378 /*
6379 * Split the line just before the margin.
6380 * Only insert/delete lines, but don't really redraw the window.
6381 */
6382 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6383 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6384#ifdef FEAT_COMMENTS
6385 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006386 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006387#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006388 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6389 if (!(flags & INSCHAR_COM_LIST))
6390 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006391
6392 replace_offset = 0;
6393 if (first_line)
6394 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006395 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006396 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006397 /*
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006398 * This section is for auto-wrap of numeric lists. When not
6399 * in insert mode (i.e. format_lines()), the INSCHAR_COM_LIST
6400 * flag will be set and open_line() will handle it (as seen
6401 * above). The code here (and in get_number_indent()) will
6402 * recognize comments if needed...
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006403 */
6404 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006405 second_indent =
6406 get_number_indent(curwin->w_cursor.lnum - 1);
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006407 if (second_indent >= 0)
6408 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006409#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006410 if (State & VREPLACE_FLAG)
6411 change_indent(INDENT_SET, second_indent,
6412 FALSE, NUL, TRUE);
6413 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006414#endif
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006415#ifdef FEAT_COMMENTS
6416 if (leader_len > 0 && second_indent - leader_len > 0)
6417 {
6418 int i;
6419 int padding = second_indent - leader_len;
6420
6421 /* We started at the first_line of a numbered list
6422 * that has a comment. the open_line() function has
6423 * inserted the proper comment leader and positioned
6424 * the cursor at the end of the split line. Now we
6425 * add the additional whitespace needed after the
6426 * comment leader for the numbered list. */
6427 for (i = 0; i < padding; i++)
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006428 ins_str((char_u *)" ");
Bram Moolenaar5967abb2012-07-06 13:36:48 +02006429 changed_bytes(curwin->w_cursor.lnum, leader_len);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006430 }
6431 else
6432 {
6433#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006434 (void)set_indent(second_indent, SIN_CHANGED);
Bram Moolenaar96b7ca52012-06-29 15:04:49 +02006435#ifdef FEAT_COMMENTS
6436 }
6437#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006438 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006439 }
6440 first_line = FALSE;
6441 }
6442
6443#ifdef FEAT_VREPLACE
6444 if (State & VREPLACE_FLAG)
6445 {
6446 /*
6447 * In VREPLACE mode we have backspaced over the text to be
6448 * moved, now we re-insert it into the new line.
6449 */
6450 ins_bytes(saved_text);
6451 vim_free(saved_text);
6452 }
6453 else
6454#endif
6455 {
6456 /*
6457 * Check if cursor is not past the NUL off the line, cindent
6458 * may have added or removed indent.
6459 */
6460 curwin->w_cursor.col += startcol;
6461 len = (colnr_T)STRLEN(ml_get_curline());
6462 if (curwin->w_cursor.col > len)
6463 curwin->w_cursor.col = len;
6464 }
6465
6466 haveto_redraw = TRUE;
6467#ifdef FEAT_CINDENT
6468 can_cindent = TRUE;
6469#endif
6470 /* moved the cursor, don't autoindent or cindent now */
6471 did_ai = FALSE;
6472#ifdef FEAT_SMARTINDENT
6473 did_si = FALSE;
6474 can_si = FALSE;
6475 can_si_back = FALSE;
6476#endif
6477 line_breakcheck();
6478 }
6479
6480 if (save_char != NUL) /* put back space after cursor */
6481 pchar_cursor(save_char);
6482
6483 if (!format_only && haveto_redraw)
6484 {
6485 update_topline();
6486 redraw_curbuf_later(VALID);
6487 }
6488}
6489
6490/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 * Called after inserting or deleting text: When 'formatoptions' includes the
6492 * 'a' flag format from the current line until the end of the paragraph.
6493 * Keep the cursor at the same position relative to the text.
6494 * The caller must have saved the cursor line for undo, following ones will be
6495 * saved here.
6496 */
6497 void
6498auto_format(trailblank, prev_line)
6499 int trailblank; /* when TRUE also format with trailing blank */
6500 int prev_line; /* may start in previous line */
6501{
6502 pos_T pos;
6503 colnr_T len;
6504 char_u *old;
6505 char_u *new, *pnew;
6506 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006507 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006508
6509 if (!has_format_option(FO_AUTO))
6510 return;
6511
6512 pos = curwin->w_cursor;
6513 old = ml_get_curline();
6514
6515 /* may remove added space */
6516 check_auto_format(FALSE);
6517
6518 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6519 * user might insert normal text next. Also skip formatting when "1" is
6520 * in 'formatoptions' and there is a single character before the cursor.
6521 * Otherwise the line would be broken and when typing another non-white
6522 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006523 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524 if (*old != NUL && !trailblank && wasatend)
6525 {
6526 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006527 cc = gchar_cursor();
6528 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6529 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006530 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006531 cc = gchar_cursor();
6532 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
6534 curwin->w_cursor = pos;
6535 return;
6536 }
6537 curwin->w_cursor = pos;
6538 }
6539
6540#ifdef FEAT_COMMENTS
6541 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6542 * comments. */
6543 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006544 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 return;
6546#endif
6547
6548 /*
6549 * May start formatting in a previous line, so that after "x" a word is
6550 * moved to the previous line if it fits there now. Only when this is not
6551 * the start of a paragraph.
6552 */
6553 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6554 {
6555 --curwin->w_cursor.lnum;
6556 if (u_save_cursor() == FAIL)
6557 return;
6558 }
6559
6560 /*
6561 * Do the formatting and restore the cursor position. "saved_cursor" will
6562 * be adjusted for the text formatting.
6563 */
6564 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006565 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006566 curwin->w_cursor = saved_cursor;
6567 saved_cursor.lnum = 0;
6568
6569 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6570 {
6571 /* "cannot happen" */
6572 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6573 coladvance((colnr_T)MAXCOL);
6574 }
6575 else
6576 check_cursor_col();
6577
6578 /* Insert mode: If the cursor is now after the end of the line while it
6579 * previously wasn't, the line was broken. Because of the rule above we
6580 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6581 * formatted. */
6582 if (!wasatend && has_format_option(FO_WHITE_PAR))
6583 {
6584 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006585 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006586 if (curwin->w_cursor.col == len)
6587 {
6588 pnew = vim_strnsave(new, len + 2);
6589 pnew[len] = ' ';
6590 pnew[len + 1] = NUL;
6591 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6592 /* remove the space later */
6593 did_add_space = TRUE;
6594 }
6595 else
6596 /* may remove added space */
6597 check_auto_format(FALSE);
6598 }
6599
6600 check_cursor();
6601}
6602
6603/*
6604 * When an extra space was added to continue a paragraph for auto-formatting,
6605 * delete it now. The space must be under the cursor, just after the insert
6606 * position.
6607 */
6608 static void
6609check_auto_format(end_insert)
6610 int end_insert; /* TRUE when ending Insert mode */
6611{
6612 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006613 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006614
6615 if (did_add_space)
6616 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006617 cc = gchar_cursor();
6618 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 /* Somehow the space was removed already. */
6620 did_add_space = FALSE;
6621 else
6622 {
6623 if (!end_insert)
6624 {
6625 inc_cursor();
6626 c = gchar_cursor();
6627 dec_cursor();
6628 }
6629 if (c != NUL)
6630 {
6631 /* The space is no longer at the end of the line, delete it. */
6632 del_char(FALSE);
6633 did_add_space = FALSE;
6634 }
6635 }
6636 }
6637}
6638
6639/*
6640 * Find out textwidth to be used for formatting:
6641 * if 'textwidth' option is set, use it
6642 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6643 * if invalid value, use 0.
6644 * Set default to window width (maximum 79) for "gq" operator.
6645 */
6646 int
6647comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006648 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006649{
6650 int textwidth;
6651
6652 textwidth = curbuf->b_p_tw;
6653 if (textwidth == 0 && curbuf->b_p_wm)
6654 {
6655 /* The width is the window width minus 'wrapmargin' minus all the
6656 * things that add to the margin. */
6657 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6658#ifdef FEAT_CMDWIN
6659 if (cmdwin_type != 0)
6660 textwidth -= 1;
6661#endif
6662#ifdef FEAT_FOLDING
6663 textwidth -= curwin->w_p_fdc;
6664#endif
6665#ifdef FEAT_SIGNS
6666 if (curwin->w_buffer->b_signlist != NULL
6667# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006668 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006669# endif
6670 )
6671 textwidth -= 1;
6672#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006673 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 textwidth -= 8;
6675 }
6676 if (textwidth < 0)
6677 textwidth = 0;
6678 if (ff && textwidth == 0)
6679 {
6680 textwidth = W_WIDTH(curwin) - 1;
6681 if (textwidth > 79)
6682 textwidth = 79;
6683 }
6684 return textwidth;
6685}
6686
6687/*
6688 * Put a character in the redo buffer, for when just after a CTRL-V.
6689 */
6690 static void
6691redo_literal(c)
6692 int c;
6693{
6694 char_u buf[10];
6695
6696 /* Only digits need special treatment. Translate them into a string of
6697 * three digits. */
6698 if (VIM_ISDIGIT(c))
6699 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006700 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 AppendToRedobuff(buf);
6702 }
6703 else
6704 AppendCharToRedobuff(c);
6705}
6706
6707/*
6708 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006709 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 */
6711 static void
6712start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006713 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714{
6715 if (!arrow_used) /* something has been inserted */
6716 {
6717 AppendToRedobuff(ESC_STR);
Bram Moolenaarf332a652013-11-04 04:20:33 +01006718 stop_insert(end_insert_pos, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 arrow_used = TRUE; /* this means we stopped the current insert */
6720 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006721#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006722 check_spell_redraw();
6723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006724}
6725
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006726#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006727/*
6728 * If we skipped highlighting word at cursor, do it now.
6729 * It may be skipped again, thus reset spell_redraw_lnum first.
6730 */
6731 static void
6732check_spell_redraw()
6733{
6734 if (spell_redraw_lnum != 0)
6735 {
6736 linenr_T lnum = spell_redraw_lnum;
6737
6738 spell_redraw_lnum = 0;
6739 redrawWinline(lnum, FALSE);
6740 }
6741}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006742
6743/*
6744 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6745 * spelled word, if there is one.
6746 */
6747 static void
6748spell_back_to_badword()
6749{
6750 pos_T tpos = curwin->w_cursor;
6751
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006752 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006753 if (curwin->w_cursor.col != tpos.col)
6754 start_arrow(&tpos);
6755}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006756#endif
6757
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758/*
6759 * stop_arrow() is called before a change is made in insert mode.
6760 * If an arrow key has been used, start a new insertion.
6761 * Returns FAIL if undo is impossible, shouldn't insert then.
6762 */
6763 int
6764stop_arrow()
6765{
6766 if (arrow_used)
6767 {
6768 if (u_save_cursor() == OK)
6769 {
6770 arrow_used = FALSE;
6771 ins_need_undo = FALSE;
6772 }
6773 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006774 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006775 ai_col = 0;
6776#ifdef FEAT_VREPLACE
6777 if (State & VREPLACE_FLAG)
6778 {
6779 orig_line_count = curbuf->b_ml.ml_line_count;
6780 vr_lines_changed = 1;
6781 }
6782#endif
6783 ResetRedobuff();
6784 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006785 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786 }
6787 else if (ins_need_undo)
6788 {
6789 if (u_save_cursor() == OK)
6790 ins_need_undo = FALSE;
6791 }
6792
6793#ifdef FEAT_FOLDING
6794 /* Always open fold at the cursor line when inserting something. */
6795 foldOpenCursor();
6796#endif
6797
6798 return (arrow_used || ins_need_undo ? FAIL : OK);
6799}
6800
6801/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006802 * Do a few things to stop inserting.
6803 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6804 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006805 */
6806 static void
Bram Moolenaarf332a652013-11-04 04:20:33 +01006807stop_insert(end_insert_pos, esc, nomove)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006808 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006809 int esc; /* called by ins_esc() */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006810 int nomove; /* <c-\><c-o>, don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006811{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006812 int cc;
6813 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006814
6815 stop_redo_ins();
6816 replace_flush(); /* abandon replace stack */
6817
6818 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006819 * Save the inserted text for later redo with ^@ and CTRL-A.
6820 * Don't do it when "restart_edit" was set and nothing was inserted,
6821 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006822 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006823 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006824 if (did_restart_edit == 0 || (ptr != NULL
6825 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006826 {
6827 vim_free(last_insert);
6828 last_insert = ptr;
6829 last_insert_skip = new_insert_skip;
6830 }
6831 else
6832 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006833
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006834 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006835 {
6836 /* Auto-format now. It may seem strange to do this when stopping an
6837 * insertion (or moving the cursor), but it's required when appending
6838 * a line and having it end in a space. But only do it when something
6839 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006840 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006841 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006842 pos_T tpos = curwin->w_cursor;
6843
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 /* When the cursor is at the end of the line after a space the
6845 * formatting will move it to the following word. Avoid that by
6846 * moving the cursor onto the space. */
6847 cc = 'x';
6848 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6849 {
6850 dec_cursor();
6851 cc = gchar_cursor();
6852 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006853 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006854 }
6855
6856 auto_format(TRUE, FALSE);
6857
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006858 if (vim_iswhite(cc))
6859 {
6860 if (gchar_cursor() != NUL)
6861 inc_cursor();
6862#ifdef FEAT_VIRTUALEDIT
6863 /* If the cursor is still at the same character, also keep
6864 * the "coladd". */
6865 if (gchar_cursor() == NUL
6866 && curwin->w_cursor.lnum == tpos.lnum
6867 && curwin->w_cursor.col == tpos.col)
6868 curwin->w_cursor.coladd = tpos.coladd;
6869#endif
6870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871 }
6872
6873 /* If a space was inserted for auto-formatting, remove it now. */
6874 check_auto_format(TRUE);
6875
6876 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006877 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006878 * Do this when ESC was used or moving the cursor up/down.
6879 * Check for the old position still being valid, just in case the text
6880 * got changed unexpectedly. */
Bram Moolenaarf332a652013-11-04 04:20:33 +01006881 if (!nomove && did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006882 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6883 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006884 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006885 pos_T tpos = curwin->w_cursor;
6886
6887 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006888 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006889 for (;;)
6890 {
6891 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6892 --curwin->w_cursor.col;
6893 cc = gchar_cursor();
6894 if (!vim_iswhite(cc))
6895 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006896 if (del_char(TRUE) == FAIL)
6897 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006898 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006899 if (curwin->w_cursor.lnum != tpos.lnum)
6900 curwin->w_cursor = tpos;
6901 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006902 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6903
Bram Moolenaar071d4272004-06-13 20:20:40 +00006904 /* <C-S-Right> may have started Visual mode, adjust the position for
6905 * deleted characters. */
6906 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6907 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006908 int len = (int)STRLEN(ml_get_curline());
6909
6910 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006912 VIsual.col = len;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006913#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar071d4272004-06-13 20:20:40 +00006914 VIsual.coladd = 0;
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01006915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006916 }
6917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006918 }
6919 }
6920 did_ai = FALSE;
6921#ifdef FEAT_SMARTINDENT
6922 did_si = FALSE;
6923 can_si = FALSE;
6924 can_si_back = FALSE;
6925#endif
6926
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006927 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6928 * now in a different buffer. */
6929 if (end_insert_pos != NULL)
6930 {
6931 curbuf->b_op_start = Insstart;
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01006932 curbuf->b_op_start_orig = Insstart_orig;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006933 curbuf->b_op_end = *end_insert_pos;
6934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006935}
6936
6937/*
6938 * Set the last inserted text to a single character.
6939 * Used for the replace command.
6940 */
6941 void
6942set_last_insert(c)
6943 int c;
6944{
6945 char_u *s;
6946
6947 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006949 if (last_insert != NULL)
6950 {
6951 s = last_insert;
6952 /* Use the CTRL-V only when entering a special char */
6953 if (c < ' ' || c == DEL)
6954 *s++ = Ctrl_V;
6955 s = add_char2buf(c, s);
6956 *s++ = ESC;
6957 *s++ = NUL;
6958 last_insert_skip = 0;
6959 }
6960}
6961
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006962#if defined(EXITFREE) || defined(PROTO)
6963 void
6964free_last_insert()
6965{
6966 vim_free(last_insert);
6967 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006968# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006969 vim_free(compl_orig_text);
6970 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006971# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006972}
6973#endif
6974
Bram Moolenaar071d4272004-06-13 20:20:40 +00006975/*
6976 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6977 * and CSI. Handle multi-byte characters.
6978 * Returns a pointer to after the added bytes.
6979 */
6980 char_u *
6981add_char2buf(c, s)
6982 int c;
6983 char_u *s;
6984{
6985#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006986 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006987 int i;
6988 int len;
6989
6990 len = (*mb_char2bytes)(c, temp);
6991 for (i = 0; i < len; ++i)
6992 {
6993 c = temp[i];
6994#endif
6995 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6996 if (c == K_SPECIAL)
6997 {
6998 *s++ = K_SPECIAL;
6999 *s++ = KS_SPECIAL;
7000 *s++ = KE_FILLER;
7001 }
7002#ifdef FEAT_GUI
7003 else if (c == CSI)
7004 {
7005 *s++ = CSI;
7006 *s++ = KS_EXTRA;
7007 *s++ = (int)KE_CSI;
7008 }
7009#endif
7010 else
7011 *s++ = c;
7012#ifdef FEAT_MBYTE
7013 }
7014#endif
7015 return s;
7016}
7017
7018/*
7019 * move cursor to start of line
7020 * if flags & BL_WHITE move to first non-white
7021 * if flags & BL_SOL move to first non-white if startofline is set,
7022 * otherwise keep "curswant" column
7023 * if flags & BL_FIX don't leave the cursor on a NUL.
7024 */
7025 void
7026beginline(flags)
7027 int flags;
7028{
7029 if ((flags & BL_SOL) && !p_sol)
7030 coladvance(curwin->w_curswant);
7031 else
7032 {
7033 curwin->w_cursor.col = 0;
7034#ifdef FEAT_VIRTUALEDIT
7035 curwin->w_cursor.coladd = 0;
7036#endif
7037
7038 if (flags & (BL_WHITE | BL_SOL))
7039 {
7040 char_u *ptr;
7041
7042 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
7043 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
7044 ++curwin->w_cursor.col;
7045 }
7046 curwin->w_set_curswant = TRUE;
7047 }
7048}
7049
7050/*
7051 * oneright oneleft cursor_down cursor_up
7052 *
7053 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007054 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007055 * Return OK when successful, FAIL when we hit a line of file boundary.
7056 */
7057
7058 int
7059oneright()
7060{
7061 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063
7064#ifdef FEAT_VIRTUALEDIT
7065 if (virtual_active())
7066 {
7067 pos_T prevpos = curwin->w_cursor;
7068
7069 /* Adjust for multi-wide char (excluding TAB) */
7070 ptr = ml_get_cursor();
7071 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007072# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00007073 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007074# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00007075 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007076# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007077 ))
7078 ? ptr2cells(ptr) : 1));
7079 curwin->w_set_curswant = TRUE;
7080 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
7081 return (prevpos.col != curwin->w_cursor.col
7082 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
7083 }
7084#endif
7085
7086 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007087 if (*ptr == NUL)
7088 return FAIL; /* already at the very end */
7089
Bram Moolenaar071d4272004-06-13 20:20:40 +00007090#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007091 if (has_mbyte)
7092 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007093 else
7094#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00007095 l = 1;
7096
7097 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
7098 * contains "onemore". */
7099 if (ptr[l] == NUL
7100#ifdef FEAT_VIRTUALEDIT
7101 && (ve_flags & VE_ONEMORE) == 0
7102#endif
7103 )
7104 return FAIL;
7105 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007106
7107 curwin->w_set_curswant = TRUE;
7108 return OK;
7109}
7110
7111 int
7112oneleft()
7113{
7114#ifdef FEAT_VIRTUALEDIT
7115 if (virtual_active())
7116 {
7117 int width;
7118 int v = getviscol();
7119
7120 if (v == 0)
7121 return FAIL;
7122
7123# ifdef FEAT_LINEBREAK
7124 /* We might get stuck on 'showbreak', skip over it. */
7125 width = 1;
7126 for (;;)
7127 {
7128 coladvance(v - width);
7129 /* getviscol() is slow, skip it when 'showbreak' is empty and
7130 * there are no multi-byte characters */
7131 if ((*p_sbr == NUL
7132# ifdef FEAT_MBYTE
7133 && !has_mbyte
7134# endif
7135 ) || getviscol() < v)
7136 break;
7137 ++width;
7138 }
7139# else
7140 coladvance(v - 1);
7141# endif
7142
7143 if (curwin->w_cursor.coladd == 1)
7144 {
7145 char_u *ptr;
7146
7147 /* Adjust for multi-wide char (not a TAB) */
7148 ptr = ml_get_cursor();
7149 if (*ptr != TAB && vim_isprintc(
7150# ifdef FEAT_MBYTE
7151 (*mb_ptr2char)(ptr)
7152# else
7153 *ptr
7154# endif
7155 ) && ptr2cells(ptr) > 1)
7156 curwin->w_cursor.coladd = 0;
7157 }
7158
7159 curwin->w_set_curswant = TRUE;
7160 return OK;
7161 }
7162#endif
7163
7164 if (curwin->w_cursor.col == 0)
7165 return FAIL;
7166
7167 curwin->w_set_curswant = TRUE;
7168 --curwin->w_cursor.col;
7169
7170#ifdef FEAT_MBYTE
7171 /* if the character on the left of the current cursor is a multi-byte
7172 * character, move to its first byte */
7173 if (has_mbyte)
7174 mb_adjust_cursor();
7175#endif
7176 return OK;
7177}
7178
7179 int
7180cursor_up(n, upd_topline)
7181 long n;
7182 int upd_topline; /* When TRUE: update topline */
7183{
7184 linenr_T lnum;
7185
7186 if (n > 0)
7187 {
7188 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007189 /* This fails if the cursor is already in the first line or the count
7190 * is larger than the line number and '-' is in 'cpoptions' */
7191 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007192 return FAIL;
7193 if (n >= lnum)
7194 lnum = 1;
7195 else
7196#ifdef FEAT_FOLDING
7197 if (hasAnyFolding(curwin))
7198 {
7199 /*
7200 * Count each sequence of folded lines as one logical line.
7201 */
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007202 /* go to the start of the current fold */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007203 (void)hasFolding(lnum, &lnum, NULL);
7204
7205 while (n--)
7206 {
7207 /* move up one line */
7208 --lnum;
7209 if (lnum <= 1)
7210 break;
7211 /* If we entered a fold, move to the beginning, unless in
7212 * Insert mode or when 'foldopen' contains "all": it will open
7213 * in a moment. */
7214 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7215 (void)hasFolding(lnum, &lnum, NULL);
7216 }
7217 if (lnum < 1)
7218 lnum = 1;
7219 }
7220 else
7221#endif
7222 lnum -= n;
7223 curwin->w_cursor.lnum = lnum;
7224 }
7225
7226 /* try to advance to the column we want to be at */
7227 coladvance(curwin->w_curswant);
7228
7229 if (upd_topline)
7230 update_topline(); /* make sure curwin->w_topline is valid */
7231
7232 return OK;
7233}
7234
7235/*
7236 * Cursor down a number of logical lines.
7237 */
7238 int
7239cursor_down(n, upd_topline)
7240 long n;
7241 int upd_topline; /* When TRUE: update topline */
7242{
7243 linenr_T lnum;
7244
7245 if (n > 0)
7246 {
7247 lnum = curwin->w_cursor.lnum;
7248#ifdef FEAT_FOLDING
7249 /* Move to last line of fold, will fail if it's the end-of-file. */
7250 (void)hasFolding(lnum, NULL, &lnum);
7251#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007252 /* This fails if the cursor is already in the last line or would move
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007253 * beyond the last line and '-' is in 'cpoptions' */
Bram Moolenaar7c626922005-02-07 22:01:03 +00007254 if (lnum >= curbuf->b_ml.ml_line_count
7255 || (lnum + n > curbuf->b_ml.ml_line_count
7256 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007257 return FAIL;
7258 if (lnum + n >= curbuf->b_ml.ml_line_count)
7259 lnum = curbuf->b_ml.ml_line_count;
7260 else
7261#ifdef FEAT_FOLDING
7262 if (hasAnyFolding(curwin))
7263 {
7264 linenr_T last;
7265
7266 /* count each sequence of folded lines as one logical line */
7267 while (n--)
7268 {
7269 if (hasFolding(lnum, NULL, &last))
7270 lnum = last + 1;
7271 else
7272 ++lnum;
7273 if (lnum >= curbuf->b_ml.ml_line_count)
7274 break;
7275 }
7276 if (lnum > curbuf->b_ml.ml_line_count)
7277 lnum = curbuf->b_ml.ml_line_count;
7278 }
7279 else
7280#endif
7281 lnum += n;
7282 curwin->w_cursor.lnum = lnum;
7283 }
7284
7285 /* try to advance to the column we want to be at */
7286 coladvance(curwin->w_curswant);
7287
7288 if (upd_topline)
7289 update_topline(); /* make sure curwin->w_topline is valid */
7290
7291 return OK;
7292}
7293
7294/*
7295 * Stuff the last inserted text in the read buffer.
7296 * Last_insert actually is a copy of the redo buffer, so we
7297 * first have to remove the command.
7298 */
7299 int
7300stuff_inserted(c, count, no_esc)
7301 int c; /* Command character to be inserted */
7302 long count; /* Repeat this many times */
7303 int no_esc; /* Don't add an ESC at the end */
7304{
7305 char_u *esc_ptr;
7306 char_u *ptr;
7307 char_u *last_ptr;
7308 char_u last = NUL;
7309
7310 ptr = get_last_insert();
7311 if (ptr == NULL)
7312 {
7313 EMSG(_(e_noinstext));
7314 return FAIL;
7315 }
7316
7317 /* may want to stuff the command character, to start Insert mode */
7318 if (c != NUL)
7319 stuffcharReadbuff(c);
7320 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7321 *esc_ptr = NUL; /* remove the ESC */
7322
7323 /* when the last char is either "0" or "^" it will be quoted if no ESC
7324 * comes after it OR if it will inserted more than once and "ptr"
7325 * starts with ^D. -- Acevedo
7326 */
7327 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7328 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7329 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7330 {
7331 last = *last_ptr;
7332 *last_ptr = NUL;
7333 }
7334
7335 do
7336 {
7337 stuffReadbuff(ptr);
7338 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7339 if (last)
7340 stuffReadbuff((char_u *)(last == '0'
7341 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7342 : IF_EB("\026^", CTRL_V_STR "^")));
7343 }
7344 while (--count > 0);
7345
7346 if (last)
7347 *last_ptr = last;
7348
7349 if (esc_ptr != NULL)
7350 *esc_ptr = ESC; /* put the ESC back */
7351
7352 /* may want to stuff a trailing ESC, to get out of Insert mode */
7353 if (!no_esc)
7354 stuffcharReadbuff(ESC);
7355
7356 return OK;
7357}
7358
7359 char_u *
7360get_last_insert()
7361{
7362 if (last_insert == NULL)
7363 return NULL;
7364 return last_insert + last_insert_skip;
7365}
7366
7367/*
7368 * Get last inserted string, and remove trailing <Esc>.
7369 * Returns pointer to allocated memory (must be freed) or NULL.
7370 */
7371 char_u *
7372get_last_insert_save()
7373{
7374 char_u *s;
7375 int len;
7376
7377 if (last_insert == NULL)
7378 return NULL;
7379 s = vim_strsave(last_insert + last_insert_skip);
7380 if (s != NULL)
7381 {
7382 len = (int)STRLEN(s);
7383 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7384 s[len - 1] = NUL;
7385 }
7386 return s;
7387}
7388
7389/*
7390 * Check the word in front of the cursor for an abbreviation.
7391 * Called when the non-id character "c" has been entered.
7392 * When an abbreviation is recognized it is removed from the text and
7393 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7394 */
7395 static int
7396echeck_abbr(c)
7397 int c;
7398{
7399 /* Don't check for abbreviation in paste mode, when disabled and just
7400 * after moving around with cursor keys. */
7401 if (p_paste || no_abbr || arrow_used)
7402 return FALSE;
7403
7404 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7405 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7406}
7407
7408/*
7409 * replace-stack functions
7410 *
7411 * When replacing characters, the replaced characters are remembered for each
7412 * new character. This is used to re-insert the old text when backspacing.
7413 *
7414 * There is a NUL headed list of characters for each character that is
7415 * currently in the file after the insertion point. When BS is used, one NUL
7416 * headed list is put back for the deleted character.
7417 *
7418 * For a newline, there are two NUL headed lists. One contains the characters
7419 * that the NL replaced. The extra one stores the characters after the cursor
7420 * that were deleted (always white space).
7421 *
7422 * Replace_offset is normally 0, in which case replace_push will add a new
7423 * character at the end of the stack. If replace_offset is not 0, that many
7424 * characters will be left on the stack above the newly inserted character.
7425 */
7426
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007427static char_u *replace_stack = NULL;
7428static long replace_stack_nr = 0; /* next entry in replace stack */
7429static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007430
7431 void
7432replace_push(c)
7433 int c; /* character that is replaced (NUL is none) */
7434{
7435 char_u *p;
7436
7437 if (replace_stack_nr < replace_offset) /* nothing to do */
7438 return;
7439 if (replace_stack_len <= replace_stack_nr)
7440 {
7441 replace_stack_len += 50;
7442 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7443 if (p == NULL) /* out of memory */
7444 {
7445 replace_stack_len -= 50;
7446 return;
7447 }
7448 if (replace_stack != NULL)
7449 {
7450 mch_memmove(p, replace_stack,
7451 (size_t)(replace_stack_nr * sizeof(char_u)));
7452 vim_free(replace_stack);
7453 }
7454 replace_stack = p;
7455 }
7456 p = replace_stack + replace_stack_nr - replace_offset;
7457 if (replace_offset)
7458 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7459 *p = c;
7460 ++replace_stack_nr;
7461}
7462
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007463#if defined(FEAT_MBYTE) || defined(PROTO)
7464/*
7465 * Push a character onto the replace stack. Handles a multi-byte character in
7466 * reverse byte order, so that the first byte is popped off first.
7467 * Return the number of bytes done (includes composing characters).
7468 */
7469 int
7470replace_push_mb(p)
7471 char_u *p;
7472{
7473 int l = (*mb_ptr2len)(p);
7474 int j;
7475
7476 for (j = l - 1; j >= 0; --j)
7477 replace_push(p[j]);
7478 return l;
7479}
7480#endif
7481
Bram Moolenaar071d4272004-06-13 20:20:40 +00007482/*
7483 * Pop one item from the replace stack.
7484 * return -1 if stack empty
7485 * return replaced character or NUL otherwise
7486 */
7487 static int
7488replace_pop()
7489{
7490 if (replace_stack_nr == 0)
7491 return -1;
7492 return (int)replace_stack[--replace_stack_nr];
7493}
7494
7495/*
7496 * Join the top two items on the replace stack. This removes to "off"'th NUL
7497 * encountered.
7498 */
7499 static void
7500replace_join(off)
7501 int off; /* offset for which NUL to remove */
7502{
7503 int i;
7504
7505 for (i = replace_stack_nr; --i >= 0; )
7506 if (replace_stack[i] == NUL && off-- <= 0)
7507 {
7508 --replace_stack_nr;
7509 mch_memmove(replace_stack + i, replace_stack + i + 1,
7510 (size_t)(replace_stack_nr - i));
7511 return;
7512 }
7513}
7514
7515/*
7516 * Pop bytes from the replace stack until a NUL is found, and insert them
7517 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7518 */
7519 static void
7520replace_pop_ins()
7521{
7522 int cc;
7523 int oldState = State;
7524
7525 State = NORMAL; /* don't want REPLACE here */
7526 while ((cc = replace_pop()) > 0)
7527 {
7528#ifdef FEAT_MBYTE
7529 mb_replace_pop_ins(cc);
7530#else
7531 ins_char(cc);
7532#endif
7533 dec_cursor();
7534 }
7535 State = oldState;
7536}
7537
7538#ifdef FEAT_MBYTE
7539/*
7540 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7541 * indicates a multi-byte char, pop the other bytes too.
7542 */
7543 static void
7544mb_replace_pop_ins(cc)
7545 int cc;
7546{
7547 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007548 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007549 int i;
7550 int c;
7551
7552 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7553 {
7554 buf[0] = cc;
7555 for (i = 1; i < n; ++i)
7556 buf[i] = replace_pop();
7557 ins_bytes_len(buf, n);
7558 }
7559 else
7560 ins_char(cc);
7561
7562 if (enc_utf8)
7563 /* Handle composing chars. */
7564 for (;;)
7565 {
7566 c = replace_pop();
7567 if (c == -1) /* stack empty */
7568 break;
7569 if ((n = MB_BYTE2LEN(c)) == 1)
7570 {
7571 /* Not a multi-byte char, put it back. */
7572 replace_push(c);
7573 break;
7574 }
7575 else
7576 {
7577 buf[0] = c;
7578 for (i = 1; i < n; ++i)
7579 buf[i] = replace_pop();
7580 if (utf_iscomposing(utf_ptr2char(buf)))
7581 ins_bytes_len(buf, n);
7582 else
7583 {
7584 /* Not a composing char, put it back. */
7585 for (i = n - 1; i >= 0; --i)
7586 replace_push(buf[i]);
7587 break;
7588 }
7589 }
7590 }
7591}
7592#endif
7593
7594/*
7595 * make the replace stack empty
7596 * (called when exiting replace mode)
7597 */
7598 static void
7599replace_flush()
7600{
7601 vim_free(replace_stack);
7602 replace_stack = NULL;
7603 replace_stack_len = 0;
7604 replace_stack_nr = 0;
7605}
7606
7607/*
7608 * Handle doing a BS for one character.
7609 * cc < 0: replace stack empty, just move cursor
7610 * cc == 0: character was inserted, delete it
7611 * cc > 0: character was replaced, put cc (first byte of original char) back
7612 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007613 * When "limit_col" is >= 0, don't delete before this column. Matters when
7614 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007615 */
7616 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007617replace_do_bs(limit_col)
7618 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007619{
7620 int cc;
7621#ifdef FEAT_VREPLACE
7622 int orig_len = 0;
7623 int ins_len;
7624 int orig_vcols = 0;
7625 colnr_T start_vcol;
7626 char_u *p;
7627 int i;
7628 int vcol;
7629#endif
7630
7631 cc = replace_pop();
7632 if (cc > 0)
7633 {
7634#ifdef FEAT_VREPLACE
7635 if (State & VREPLACE_FLAG)
7636 {
7637 /* Get the number of screen cells used by the character we are
7638 * going to delete. */
7639 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7640 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7641 }
7642#endif
7643#ifdef FEAT_MBYTE
7644 if (has_mbyte)
7645 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007646 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007647# ifdef FEAT_VREPLACE
7648 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007649 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007650# endif
7651 replace_push(cc);
7652 }
7653 else
7654#endif
7655 {
7656 pchar_cursor(cc);
7657#ifdef FEAT_VREPLACE
7658 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007659 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007660#endif
7661 }
7662 replace_pop_ins();
7663
7664#ifdef FEAT_VREPLACE
7665 if (State & VREPLACE_FLAG)
7666 {
7667 /* Get the number of screen cells used by the inserted characters */
7668 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007669 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007670 vcol = start_vcol;
7671 for (i = 0; i < ins_len; ++i)
7672 {
7673 vcol += chartabsize(p + i, vcol);
7674#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007675 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007676#endif
7677 }
7678 vcol -= start_vcol;
7679
7680 /* Delete spaces that were inserted after the cursor to keep the
7681 * text aligned. */
7682 curwin->w_cursor.col += ins_len;
7683 while (vcol > orig_vcols && gchar_cursor() == ' ')
7684 {
7685 del_char(FALSE);
7686 ++orig_vcols;
7687 }
7688 curwin->w_cursor.col -= ins_len;
7689 }
7690#endif
7691
7692 /* mark the buffer as changed and prepare for displaying */
7693 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7694 }
7695 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007696 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007697}
7698
7699#ifdef FEAT_CINDENT
7700/*
7701 * Return TRUE if C-indenting is on.
7702 */
7703 static int
7704cindent_on()
7705{
7706 return (!p_paste && (curbuf->b_p_cin
7707# ifdef FEAT_EVAL
7708 || *curbuf->b_p_inde != NUL
7709# endif
7710 ));
7711}
7712#endif
7713
7714#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7715/*
7716 * Re-indent the current line, based on the current contents of it and the
7717 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7718 * confused what all the part that handles Control-T is doing that I'm not.
7719 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7720 */
7721
7722 void
7723fixthisline(get_the_indent)
7724 int (*get_the_indent) __ARGS((void));
7725{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007726 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007727 if (linewhite(curwin->w_cursor.lnum))
7728 did_ai = TRUE; /* delete the indent if the line stays empty */
7729}
7730
7731 void
7732fix_indent()
7733{
7734 if (p_paste)
7735 return;
7736# ifdef FEAT_LISP
7737 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7738 fixthisline(get_lisp_indent);
7739# endif
7740# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7741 else
7742# endif
7743# ifdef FEAT_CINDENT
7744 if (cindent_on())
7745 do_c_expr_indent();
7746# endif
7747}
7748
7749#endif
7750
7751#ifdef FEAT_CINDENT
7752/*
7753 * return TRUE if 'cinkeys' contains the key "keytyped",
7754 * when == '*': Only if key is preceded with '*' (indent before insert)
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02007755 * when == '!': Only if key is preceded with '!' (don't insert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007756 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7757 *
7758 * "keytyped" can have a few special values:
7759 * KEY_OPEN_FORW
7760 * KEY_OPEN_BACK
7761 * KEY_COMPLETE just finished completion.
7762 *
7763 * If line_is_empty is TRUE accept keys with '0' before them.
7764 */
7765 int
7766in_cinkeys(keytyped, when, line_is_empty)
7767 int keytyped;
7768 int when;
7769 int line_is_empty;
7770{
7771 char_u *look;
7772 int try_match;
7773 int try_match_word;
7774 char_u *p;
7775 char_u *line;
7776 int icase;
7777 int i;
7778
Bram Moolenaar30848942009-12-24 14:46:12 +00007779 if (keytyped == NUL)
7780 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7781 return FALSE;
7782
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783#ifdef FEAT_EVAL
7784 if (*curbuf->b_p_inde != NUL)
7785 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7786 else
7787#endif
7788 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7789 while (*look)
7790 {
7791 /*
7792 * Find out if we want to try a match with this key, depending on
7793 * 'when' and a '*' or '!' before the key.
7794 */
7795 switch (when)
7796 {
7797 case '*': try_match = (*look == '*'); break;
7798 case '!': try_match = (*look == '!'); break;
7799 default: try_match = (*look != '*'); break;
7800 }
7801 if (*look == '*' || *look == '!')
7802 ++look;
7803
7804 /*
7805 * If there is a '0', only accept a match if the line is empty.
7806 * But may still match when typing last char of a word.
7807 */
7808 if (*look == '0')
7809 {
7810 try_match_word = try_match;
7811 if (!line_is_empty)
7812 try_match = FALSE;
7813 ++look;
7814 }
7815 else
7816 try_match_word = FALSE;
7817
7818 /*
7819 * does it look like a control character?
7820 */
7821 if (*look == '^'
7822#ifdef EBCDIC
7823 && (Ctrl_chr(look[1]) != 0)
7824#else
7825 && look[1] >= '?' && look[1] <= '_'
7826#endif
7827 )
7828 {
7829 if (try_match && keytyped == Ctrl_chr(look[1]))
7830 return TRUE;
7831 look += 2;
7832 }
7833 /*
7834 * 'o' means "o" command, open forward.
7835 * 'O' means "O" command, open backward.
7836 */
7837 else if (*look == 'o')
7838 {
7839 if (try_match && keytyped == KEY_OPEN_FORW)
7840 return TRUE;
7841 ++look;
7842 }
7843 else if (*look == 'O')
7844 {
7845 if (try_match && keytyped == KEY_OPEN_BACK)
7846 return TRUE;
7847 ++look;
7848 }
7849
7850 /*
7851 * 'e' means to check for "else" at start of line and just before the
7852 * cursor.
7853 */
7854 else if (*look == 'e')
7855 {
7856 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7857 {
7858 p = ml_get_curline();
7859 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7860 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7861 return TRUE;
7862 }
7863 ++look;
7864 }
7865
7866 /*
7867 * ':' only causes an indent if it is at the end of a label or case
7868 * statement, or when it was before typing the ':' (to fix
7869 * class::method for C++).
7870 */
7871 else if (*look == ':')
7872 {
7873 if (try_match && keytyped == ':')
7874 {
7875 p = ml_get_curline();
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007876 if (cin_iscase(p, FALSE) || cin_isscopedecl(p) || cin_islabel())
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007878 /* Need to get the line again after cin_islabel(). */
7879 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007880 if (curwin->w_cursor.col > 2
7881 && p[curwin->w_cursor.col - 1] == ':'
7882 && p[curwin->w_cursor.col - 2] == ':')
7883 {
7884 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007885 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar84dbb622013-11-06 04:01:36 +01007886 || cin_islabel());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 p = ml_get_curline();
7888 p[curwin->w_cursor.col - 1] = ':';
7889 if (i)
7890 return TRUE;
7891 }
7892 }
7893 ++look;
7894 }
7895
7896
7897 /*
7898 * Is it a key in <>, maybe?
7899 */
7900 else if (*look == '<')
7901 {
7902 if (try_match)
7903 {
7904 /*
7905 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7906 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7907 * >, *, : and ! keys if they really really want to.
7908 */
7909 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7910 && keytyped == look[1])
7911 return TRUE;
7912
7913 if (keytyped == get_special_key_code(look + 1))
7914 return TRUE;
7915 }
7916 while (*look && *look != '>')
7917 look++;
7918 while (*look == '>')
7919 look++;
7920 }
7921
7922 /*
7923 * Is it a word: "=word"?
7924 */
7925 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7926 {
7927 ++look;
7928 if (*look == '~')
7929 {
7930 icase = TRUE;
7931 ++look;
7932 }
7933 else
7934 icase = FALSE;
7935 p = vim_strchr(look, ',');
7936 if (p == NULL)
7937 p = look + STRLEN(look);
7938 if ((try_match || try_match_word)
7939 && curwin->w_cursor.col >= (colnr_T)(p - look))
7940 {
7941 int match = FALSE;
7942
7943#ifdef FEAT_INS_EXPAND
7944 if (keytyped == KEY_COMPLETE)
7945 {
7946 char_u *s;
7947
7948 /* Just completed a word, check if it starts with "look".
7949 * search back for the start of a word. */
7950 line = ml_get_curline();
7951# ifdef FEAT_MBYTE
7952 if (has_mbyte)
7953 {
7954 char_u *n;
7955
7956 for (s = line + curwin->w_cursor.col; s > line; s = n)
7957 {
7958 n = mb_prevptr(line, s);
7959 if (!vim_iswordp(n))
7960 break;
7961 }
7962 }
7963 else
7964# endif
7965 for (s = line + curwin->w_cursor.col; s > line; --s)
7966 if (!vim_iswordc(s[-1]))
7967 break;
7968 if (s + (p - look) <= line + curwin->w_cursor.col
7969 && (icase
7970 ? MB_STRNICMP(s, look, p - look)
7971 : STRNCMP(s, look, p - look)) == 0)
7972 match = TRUE;
7973 }
7974 else
7975#endif
7976 /* TODO: multi-byte */
7977 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7978 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7979 {
7980 line = ml_get_cursor();
7981 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7982 || !vim_iswordc(line[-(p - look) - 1]))
7983 && (icase
7984 ? MB_STRNICMP(line - (p - look), look, p - look)
7985 : STRNCMP(line - (p - look), look, p - look))
7986 == 0)
7987 match = TRUE;
7988 }
7989 if (match && try_match_word && !try_match)
7990 {
7991 /* "0=word": Check if there are only blanks before the
7992 * word. */
7993 line = ml_get_curline();
7994 if ((int)(skipwhite(line) - line) !=
7995 (int)(curwin->w_cursor.col - (p - look)))
7996 match = FALSE;
7997 }
7998 if (match)
7999 return TRUE;
8000 }
8001 look = p;
8002 }
8003
8004 /*
8005 * ok, it's a boring generic character.
8006 */
8007 else
8008 {
8009 if (try_match && *look == keytyped)
8010 return TRUE;
8011 ++look;
8012 }
8013
8014 /*
8015 * Skip over ", ".
8016 */
8017 look = skip_to_option_part(look);
8018 }
8019 return FALSE;
8020}
8021#endif /* FEAT_CINDENT */
8022
8023#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
8024/*
8025 * Map Hebrew keyboard when in hkmap mode.
8026 */
8027 int
8028hkmap(c)
8029 int c;
8030{
8031 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
8032 {
8033 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
8034 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
8035 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
8036 static char_u map[26] =
8037 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
8038 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
8039 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
8040 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
8041 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
8042 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
8043 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
8044 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
8045 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
8046
8047 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
8048 return (int)(map[CharOrd(c)] - 1 + p_aleph);
8049 /* '-1'='sofit' */
8050 else if (c == 'x')
8051 return 'X';
8052 else if (c == 'q')
8053 return '\''; /* {geresh}={'} */
8054 else if (c == 246)
8055 return ' '; /* \"o --> ' ' for a german keyboard */
8056 else if (c == 228)
8057 return ' '; /* \"a --> ' ' -- / -- */
8058 else if (c == 252)
8059 return ' '; /* \"u --> ' ' -- / -- */
8060#ifdef EBCDIC
8061 else if (islower(c))
8062#else
8063 /* NOTE: islower() does not do the right thing for us on Linux so we
8064 * do this the same was as 5.7 and previous, so it works correctly on
8065 * all systems. Specifically, the e.g. Delete and Arrow keys are
8066 * munged and won't work if e.g. searching for Hebrew text.
8067 */
8068 else if (c >= 'a' && c <= 'z')
8069#endif
8070 return (int)(map[CharOrdLow(c)] + p_aleph);
8071 else
8072 return c;
8073 }
8074 else
8075 {
8076 switch (c)
8077 {
8078 case '`': return ';';
8079 case '/': return '.';
8080 case '\'': return ',';
8081 case 'q': return '/';
8082 case 'w': return '\'';
8083
8084 /* Hebrew letters - set offset from 'a' */
8085 case ',': c = '{'; break;
8086 case '.': c = 'v'; break;
8087 case ';': c = 't'; break;
8088 default: {
8089 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
8090
8091#ifdef EBCDIC
8092 /* see note about islower() above */
8093 if (!islower(c))
8094#else
8095 if (c < 'a' || c > 'z')
8096#endif
8097 return c;
8098 c = str[CharOrdLow(c)];
8099 break;
8100 }
8101 }
8102
8103 return (int)(CharOrdLow(c) + p_aleph);
8104 }
8105}
8106#endif
8107
8108 static void
8109ins_reg()
8110{
8111 int need_redraw = FALSE;
8112 int regname;
8113 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008114 int vis_active = VIsual_active;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115
8116 /*
8117 * If we are going to wait for a character, show a '"'.
8118 */
8119 pc_status = PC_STATUS_UNSET;
8120 if (redrawing() && !char_avail())
8121 {
8122 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008123 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124
8125 edit_putchar('"', TRUE);
8126#ifdef FEAT_CMDL_INFO
8127 add_to_showcmd_c(Ctrl_R);
8128#endif
8129 }
8130
8131#ifdef USE_ON_FLY_SCROLL
8132 dont_scroll = TRUE; /* disallow scrolling here */
8133#endif
8134
8135 /*
8136 * Don't map the register name. This also prevents the mode message to be
8137 * deleted when ESC is hit.
8138 */
8139 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008140 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8143 {
8144 /* Get a third key for literal register insertion */
8145 literally = regname;
8146#ifdef FEAT_CMDL_INFO
8147 add_to_showcmd_c(literally);
8148#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008149 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008151 }
8152 --no_mapping;
8153
8154#ifdef FEAT_EVAL
Bram Moolenaardf9259a2013-06-15 17:54:43 +02008155 /* Don't call u_sync() while typing the expression or giving an error
8156 * message for it. Only call it explicitly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157 ++no_u_sync;
8158 if (regname == '=')
8159 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008160# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008162# endif
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008163 /* Sync undo when evaluating the expression calls setline() or
8164 * append(), so that it can be undone separately. */
8165 u_sync_once = 2;
Bram Moolenaar5737ca22013-06-27 22:21:24 +02008166
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008168# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 /* Restore the Input Method. */
8170 if (im_on)
8171 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008172# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008173 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008174 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8175 {
8176 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008177 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008178 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008179 else
8180 {
8181#endif
8182 if (literally == Ctrl_O || literally == Ctrl_P)
8183 {
8184 /* Append the command to the redo buffer. */
8185 AppendCharToRedobuff(Ctrl_R);
8186 AppendCharToRedobuff(literally);
8187 AppendCharToRedobuff(regname);
8188
8189 do_put(regname, BACKWARD, 1L,
8190 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8191 }
8192 else if (insert_reg(regname, literally) == FAIL)
8193 {
8194 vim_beep();
8195 need_redraw = TRUE; /* remove the '"' */
8196 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008197 else if (stop_insert_mode)
8198 /* When the '=' register was used and a function was invoked that
8199 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8200 * insert anything, need to remove the '"' */
8201 need_redraw = TRUE;
8202
Bram Moolenaar071d4272004-06-13 20:20:40 +00008203#ifdef FEAT_EVAL
8204 }
8205 --no_u_sync;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02008206 if (u_sync_once == 1)
8207 ins_need_undo = TRUE;
8208 u_sync_once = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209#endif
8210#ifdef FEAT_CMDL_INFO
8211 clear_showcmd();
8212#endif
8213
8214 /* If the inserted register is empty, we need to remove the '"' */
8215 if (need_redraw || stuff_empty())
8216 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008217
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008218 /* Disallow starting Visual mode here, would get a weird mode. */
8219 if (!vis_active && VIsual_active)
8220 end_visual_mode();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221}
8222
8223/*
8224 * CTRL-G commands in Insert mode.
8225 */
8226 static void
8227ins_ctrl_g()
8228{
8229 int c;
8230
8231#ifdef FEAT_INS_EXPAND
8232 /* Right after CTRL-X the cursor will be after the ruler. */
8233 setcursor();
8234#endif
8235
8236 /*
8237 * Don't map the second key. This also prevents the mode message to be
8238 * deleted when ESC is hit.
8239 */
8240 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008241 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 --no_mapping;
8243 switch (c)
8244 {
8245 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8246 case K_UP:
8247 case Ctrl_K:
8248 case 'k': ins_up(TRUE);
8249 break;
8250
8251 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8252 case K_DOWN:
8253 case Ctrl_J:
8254 case 'j': ins_down(TRUE);
8255 break;
8256
8257 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008258 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008260
8261 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008262 * a line to the previous one must save for undo. */
Bram Moolenaarb1d90a32014-02-22 23:03:55 +01008263 update_Insstart_orig = FALSE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008264 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 break;
8266
8267 /* Unknown CTRL-G command, reserved for future expansion. */
8268 default: vim_beep();
8269 }
8270}
8271
8272/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008273 * CTRL-^ in Insert mode.
8274 */
8275 static void
8276ins_ctrl_hat()
8277{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008278 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008279 {
8280 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8281 if (State & LANGMAP)
8282 {
8283 curbuf->b_p_iminsert = B_IMODE_NONE;
8284 State &= ~LANGMAP;
8285 }
8286 else
8287 {
8288 curbuf->b_p_iminsert = B_IMODE_LMAP;
8289 State |= LANGMAP;
8290#ifdef USE_IM_CONTROL
8291 im_set_active(FALSE);
8292#endif
8293 }
8294 }
8295#ifdef USE_IM_CONTROL
8296 else
8297 {
8298 /* There are no ":lmap" mappings, toggle IM */
8299 if (im_get_status())
8300 {
8301 curbuf->b_p_iminsert = B_IMODE_NONE;
8302 im_set_active(FALSE);
8303 }
8304 else
8305 {
8306 curbuf->b_p_iminsert = B_IMODE_IM;
8307 State &= ~LANGMAP;
8308 im_set_active(TRUE);
8309 }
8310 }
8311#endif
8312 set_iminsert_global();
8313 showmode();
8314#ifdef FEAT_GUI
8315 /* may show different cursor shape or color */
8316 if (gui.in_use)
8317 gui_update_cursor(TRUE, FALSE);
8318#endif
8319#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8320 /* Show/unshow value of 'keymap' in status lines. */
8321 status_redraw_curbuf();
8322#endif
8323}
8324
8325/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 * Handle ESC in insert mode.
8327 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8328 * insert.
8329 */
8330 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008331ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 long *count;
8333 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008334 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335{
8336 int temp;
8337 static int disabled_redraw = FALSE;
8338
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008339#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008340 check_spell_redraw();
8341#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342#if defined(FEAT_HANGULIN)
8343# if defined(ESC_CHG_TO_ENG_MODE)
8344 hangul_input_state_set(0);
8345# endif
8346 if (composing_hangul)
8347 {
8348 push_raw_key(composing_hangul_buffer, 2);
8349 composing_hangul = 0;
8350 }
8351#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352
8353 temp = curwin->w_cursor.col;
8354 if (disabled_redraw)
8355 {
8356 --RedrawingDisabled;
8357 disabled_redraw = FALSE;
8358 }
8359 if (!arrow_used)
8360 {
8361 /*
8362 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008363 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8364 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008365 */
8366 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008367 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008368
8369 /*
8370 * Repeating insert may take a long time. Check for
8371 * interrupt now and then.
8372 */
8373 if (*count > 0)
8374 {
8375 line_breakcheck();
8376 if (got_int)
8377 *count = 0;
8378 }
8379
8380 if (--*count > 0) /* repeat what was typed */
8381 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008382 /* Vi repeats the insert without replacing characters. */
8383 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8384 State &= ~REPLACE_FLAG;
8385
Bram Moolenaar071d4272004-06-13 20:20:40 +00008386 (void)start_redo_ins();
8387 if (cmdchar == 'r' || cmdchar == 'v')
8388 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8389 ++RedrawingDisabled;
8390 disabled_redraw = TRUE;
8391 return FALSE; /* repeat the insert */
8392 }
Bram Moolenaarf332a652013-11-04 04:20:33 +01008393 stop_insert(&curwin->w_cursor, TRUE, nomove);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 undisplay_dollar();
8395 }
8396
8397 /* When an autoindent was removed, curswant stays after the
8398 * indent */
8399 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8400 curwin->w_set_curswant = TRUE;
8401
8402 /* Remember the last Insert position in the '^ mark. */
8403 if (!cmdmod.keepjumps)
8404 curbuf->b_last_insert = curwin->w_cursor;
8405
8406 /*
8407 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008408 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008409 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008410 if (!nomove
8411 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008412#ifdef FEAT_VIRTUALEDIT
8413 || curwin->w_cursor.coladd > 0
8414#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008415 )
8416 && (restart_edit == NUL
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +01008417 || (gchar_cursor() == NUL && !VIsual_active))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008418#ifdef FEAT_RIGHTLEFT
8419 && !revins_on
8420#endif
8421 )
8422 {
8423#ifdef FEAT_VIRTUALEDIT
8424 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8425 {
8426 oneleft();
8427 if (restart_edit != NUL)
8428 ++curwin->w_cursor.coladd;
8429 }
8430 else
8431#endif
8432 {
8433 --curwin->w_cursor.col;
8434#ifdef FEAT_MBYTE
8435 /* Correct cursor for multi-byte character. */
8436 if (has_mbyte)
8437 mb_adjust_cursor();
8438#endif
8439 }
8440 }
8441
8442#ifdef USE_IM_CONTROL
8443 /* Disable IM to allow typing English directly for Normal mode commands.
8444 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8445 * well). */
8446 if (!(State & LANGMAP))
8447 im_save_status(&curbuf->b_p_iminsert);
8448 im_set_active(FALSE);
8449#endif
8450
8451 State = NORMAL;
8452 /* need to position cursor again (e.g. when on a TAB ) */
8453 changed_cline_bef_curs();
8454
8455#ifdef FEAT_MOUSE
8456 setmouse();
8457#endif
8458#ifdef CURSOR_SHAPE
8459 ui_cursor_shape(); /* may show different cursor shape */
8460#endif
8461
8462 /*
8463 * When recording or for CTRL-O, need to display the new mode.
8464 * Otherwise remove the mode message.
8465 */
8466 if (Recording || restart_edit != NUL)
8467 showmode();
8468 else if (p_smd)
8469 MSG("");
8470
8471 return TRUE; /* exit Insert mode */
8472}
8473
8474#ifdef FEAT_RIGHTLEFT
8475/*
8476 * Toggle language: hkmap and revins_on.
8477 * Move to end of reverse inserted text.
8478 */
8479 static void
8480ins_ctrl_()
8481{
8482 if (revins_on && revins_chars && revins_scol >= 0)
8483 {
8484 while (gchar_cursor() != NUL && revins_chars--)
8485 ++curwin->w_cursor.col;
8486 }
8487 p_ri = !p_ri;
8488 revins_on = (State == INSERT && p_ri);
8489 if (revins_on)
8490 {
8491 revins_scol = curwin->w_cursor.col;
8492 revins_legal++;
8493 revins_chars = 0;
8494 undisplay_dollar();
8495 }
8496 else
8497 revins_scol = -1;
8498#ifdef FEAT_FKMAP
8499 if (p_altkeymap)
8500 {
8501 /*
8502 * to be consistent also for redo command, using '.'
8503 * set arrow_used to true and stop it - causing to redo
8504 * characters entered in one mode (normal/reverse insert).
8505 */
8506 arrow_used = TRUE;
8507 (void)stop_arrow();
8508 p_fkmap = curwin->w_p_rl ^ p_ri;
8509 if (p_fkmap && p_ri)
8510 State = INSERT;
8511 }
8512 else
8513#endif
8514 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8515 showmode();
8516}
8517#endif
8518
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519/*
8520 * If 'keymodel' contains "startsel", may start selection.
8521 * Returns TRUE when a CTRL-O and other keys stuffed.
8522 */
8523 static int
8524ins_start_select(c)
8525 int c;
8526{
8527 if (km_startsel)
8528 switch (c)
8529 {
8530 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 case K_PAGEUP:
8533 case K_KPAGEUP:
8534 case K_PAGEDOWN:
8535 case K_KPAGEDOWN:
8536# ifdef MACOS
8537 case K_LEFT:
8538 case K_RIGHT:
8539 case K_UP:
8540 case K_DOWN:
8541 case K_END:
8542 case K_HOME:
8543# endif
8544 if (!(mod_mask & MOD_MASK_SHIFT))
8545 break;
8546 /* FALLTHROUGH */
8547 case K_S_LEFT:
8548 case K_S_RIGHT:
8549 case K_S_UP:
8550 case K_S_DOWN:
8551 case K_S_END:
8552 case K_S_HOME:
8553 /* Start selection right away, the cursor can move with
8554 * CTRL-O when beyond the end of the line. */
8555 start_selection();
8556
8557 /* Execute the key in (insert) Select mode. */
8558 stuffcharReadbuff(Ctrl_O);
8559 if (mod_mask)
8560 {
8561 char_u buf[4];
8562
8563 buf[0] = K_SPECIAL;
8564 buf[1] = KS_MODIFIER;
8565 buf[2] = mod_mask;
8566 buf[3] = NUL;
8567 stuffReadbuff(buf);
8568 }
8569 stuffcharReadbuff(c);
8570 return TRUE;
8571 }
8572 return FALSE;
8573}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574
8575/*
Bram Moolenaar84a05ac2013-05-06 04:24:17 +02008576 * <Insert> key in Insert mode: toggle insert/replace mode.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008577 */
8578 static void
8579ins_insert(replaceState)
8580 int replaceState;
8581{
8582#ifdef FEAT_FKMAP
8583 if (p_fkmap && p_ri)
8584 {
8585 beep_flush();
8586 EMSG(farsi_text_3); /* encoded in Farsi */
8587 return;
8588 }
8589#endif
8590
8591#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008592# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008593 set_vim_var_string(VV_INSERTMODE,
8594 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008595# ifdef FEAT_VREPLACE
8596 replaceState == VREPLACE ? "v" :
8597# endif
8598 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008599# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008600 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8601#endif
8602 if (State & REPLACE_FLAG)
8603 State = INSERT | (State & LANGMAP);
8604 else
8605 State = replaceState | (State & LANGMAP);
8606 AppendCharToRedobuff(K_INS);
8607 showmode();
8608#ifdef CURSOR_SHAPE
8609 ui_cursor_shape(); /* may show different cursor shape */
8610#endif
8611}
8612
8613/*
8614 * Pressed CTRL-O in Insert mode.
8615 */
8616 static void
8617ins_ctrl_o()
8618{
8619#ifdef FEAT_VREPLACE
8620 if (State & VREPLACE_FLAG)
8621 restart_edit = 'V';
8622 else
8623#endif
8624 if (State & REPLACE_FLAG)
8625 restart_edit = 'R';
8626 else
8627 restart_edit = 'I';
8628#ifdef FEAT_VIRTUALEDIT
8629 if (virtual_active())
8630 ins_at_eol = FALSE; /* cursor always keeps its column */
8631 else
8632#endif
8633 ins_at_eol = (gchar_cursor() == NUL);
8634}
8635
8636/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 * If the cursor is on an indent, ^T/^D insert/delete one
8638 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008639 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 * with vi. But vi only supports ^T and ^D after an
8641 * autoindent, we support it everywhere.
8642 */
8643 static void
8644ins_shift(c, lastc)
8645 int c;
8646 int lastc;
8647{
8648 if (stop_arrow() == FAIL)
8649 return;
8650 AppendCharToRedobuff(c);
8651
8652 /*
8653 * 0^D and ^^D: remove all indent.
8654 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008655 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8656 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008657 {
8658 --curwin->w_cursor.col;
8659 (void)del_char(FALSE); /* delete the '^' or '0' */
8660 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8661 if (State & REPLACE_FLAG)
8662 replace_pop_ins();
8663 if (lastc == '^')
8664 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008665 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008666 }
8667 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008668 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008669
8670 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8671 did_ai = FALSE;
8672#ifdef FEAT_SMARTINDENT
8673 did_si = FALSE;
8674 can_si = FALSE;
8675 can_si_back = FALSE;
8676#endif
8677#ifdef FEAT_CINDENT
8678 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8679#endif
8680}
8681
8682 static void
8683ins_del()
8684{
8685 int temp;
8686
8687 if (stop_arrow() == FAIL)
8688 return;
8689 if (gchar_cursor() == NUL) /* delete newline */
8690 {
8691 temp = curwin->w_cursor.col;
8692 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008693 || do_join(2, FALSE, TRUE, FALSE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 vim_beep();
8695 else
8696 curwin->w_cursor.col = temp;
8697 }
8698 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8699 vim_beep();
8700 did_ai = FALSE;
8701#ifdef FEAT_SMARTINDENT
8702 did_si = FALSE;
8703 can_si = FALSE;
8704 can_si_back = FALSE;
8705#endif
8706 AppendCharToRedobuff(K_DEL);
8707}
8708
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008709static void ins_bs_one __ARGS((colnr_T *vcolp));
8710
8711/*
8712 * Delete one character for ins_bs().
8713 */
8714 static void
8715ins_bs_one(vcolp)
8716 colnr_T *vcolp;
8717{
8718 dec_cursor();
8719 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8720 if (State & REPLACE_FLAG)
8721 {
8722 /* Don't delete characters before the insert point when in
8723 * Replace mode */
8724 if (curwin->w_cursor.lnum != Insstart.lnum
8725 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008726 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008727 }
8728 else
8729 (void)del_char(FALSE);
8730}
8731
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732/*
8733 * Handle Backspace, delete-word and delete-line in Insert mode.
8734 * Return TRUE when backspace was actually used.
8735 */
8736 static int
8737ins_bs(c, mode, inserted_space_p)
8738 int c;
8739 int mode;
8740 int *inserted_space_p;
8741{
8742 linenr_T lnum;
8743 int cc;
8744 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008745 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 colnr_T mincol;
8747 int did_backspace = FALSE;
8748 int in_indent;
8749 int oldState;
8750#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008751 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008752#endif
8753
8754 /*
8755 * can't delete anything in an empty file
8756 * can't backup past first character in buffer
8757 * can't backup past starting point unless 'backspace' > 1
8758 * can backup to a previous line if 'backspace' == 0
8759 */
8760 if ( bufempty()
8761 || (
8762#ifdef FEAT_RIGHTLEFT
8763 !revins_on &&
8764#endif
8765 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8766 || (!can_bs(BS_START)
8767 && (arrow_used
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008768 || (curwin->w_cursor.lnum == Insstart_orig.lnum
8769 && curwin->w_cursor.col <= Insstart_orig.col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008770 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8771 && curwin->w_cursor.col <= ai_col)
8772 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8773 {
8774 vim_beep();
8775 return FALSE;
8776 }
8777
8778 if (stop_arrow() == FAIL)
8779 return FALSE;
8780 in_indent = inindent(0);
8781#ifdef FEAT_CINDENT
8782 if (in_indent)
8783 can_cindent = FALSE;
8784#endif
8785#ifdef FEAT_COMMENTS
8786 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8787#endif
8788#ifdef FEAT_RIGHTLEFT
8789 if (revins_on) /* put cursor after last inserted char */
8790 inc_cursor();
8791#endif
8792
8793#ifdef FEAT_VIRTUALEDIT
8794 /* Virtualedit:
8795 * BACKSPACE_CHAR eats a virtual space
8796 * BACKSPACE_WORD eats all coladd
8797 * BACKSPACE_LINE eats all coladd and keeps going
8798 */
8799 if (curwin->w_cursor.coladd > 0)
8800 {
8801 if (mode == BACKSPACE_CHAR)
8802 {
8803 --curwin->w_cursor.coladd;
8804 return TRUE;
8805 }
8806 if (mode == BACKSPACE_WORD)
8807 {
8808 curwin->w_cursor.coladd = 0;
8809 return TRUE;
8810 }
8811 curwin->w_cursor.coladd = 0;
8812 }
8813#endif
8814
8815 /*
8816 * delete newline!
8817 */
8818 if (curwin->w_cursor.col == 0)
8819 {
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008820 lnum = Insstart_orig.lnum;
8821 if (curwin->w_cursor.lnum == lnum
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822#ifdef FEAT_RIGHTLEFT
8823 || revins_on
8824#endif
8825 )
8826 {
8827 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8828 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8829 return FALSE;
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008830 --Insstart_orig.lnum;
8831 Insstart_orig.col = MAXCOL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 }
8833 /*
8834 * In replace mode:
8835 * cc < 0: NL was inserted, delete it
8836 * cc >= 0: NL was replaced, put original characters back
8837 */
8838 cc = -1;
8839 if (State & REPLACE_FLAG)
8840 cc = replace_pop(); /* returns -1 if NL was inserted */
8841 /*
8842 * In replace mode, in the line we started replacing, we only move the
8843 * cursor.
8844 */
8845 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8846 {
8847 dec_cursor();
8848 }
8849 else
8850 {
8851#ifdef FEAT_VREPLACE
8852 if (!(State & VREPLACE_FLAG)
8853 || curwin->w_cursor.lnum > orig_line_count)
8854#endif
8855 {
8856 temp = gchar_cursor(); /* remember current char */
8857 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008858
8859 /* When "aw" is in 'formatoptions' we must delete the space at
8860 * the end of the line, otherwise the line will be broken
8861 * again when auto-formatting. */
8862 if (has_format_option(FO_AUTO)
8863 && has_format_option(FO_WHITE_PAR))
8864 {
8865 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8866 TRUE);
8867 int len;
8868
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008869 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008870 if (len > 0 && ptr[len - 1] == ' ')
8871 ptr[len - 1] = NUL;
8872 }
8873
Bram Moolenaard69bd9a2014-04-29 12:15:40 +02008874 (void)do_join(2, FALSE, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 if (temp == NUL && gchar_cursor() != NUL)
8876 inc_cursor();
8877 }
8878#ifdef FEAT_VREPLACE
8879 else
8880 dec_cursor();
8881#endif
8882
8883 /*
8884 * In REPLACE mode we have to put back the text that was replaced
8885 * by the NL. On the replace stack is first a NUL-terminated
8886 * sequence of characters that were deleted and then the
8887 * characters that NL replaced.
8888 */
8889 if (State & REPLACE_FLAG)
8890 {
8891 /*
8892 * Do the next ins_char() in NORMAL state, to
8893 * prevent ins_char() from replacing characters and
8894 * avoiding showmatch().
8895 */
8896 oldState = State;
8897 State = NORMAL;
8898 /*
8899 * restore characters (blanks) deleted after cursor
8900 */
8901 while (cc > 0)
8902 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008903 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904#ifdef FEAT_MBYTE
8905 mb_replace_pop_ins(cc);
8906#else
8907 ins_char(cc);
8908#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008909 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008910 cc = replace_pop();
8911 }
8912 /* restore the characters that NL replaced */
8913 replace_pop_ins();
8914 State = oldState;
8915 }
8916 }
8917 did_ai = FALSE;
8918 }
8919 else
8920 {
8921 /*
8922 * Delete character(s) before the cursor.
8923 */
8924#ifdef FEAT_RIGHTLEFT
8925 if (revins_on) /* put cursor on last inserted char */
8926 dec_cursor();
8927#endif
8928 mincol = 0;
8929 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008930 if (mode == BACKSPACE_LINE
8931 && (curbuf->b_p_ai
8932#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008933 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008934#endif
8935 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008936#ifdef FEAT_RIGHTLEFT
8937 && !revins_on
8938#endif
8939 )
8940 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008941 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008943 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008945 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 }
8947
8948 /*
8949 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8950 */
8951 if ( mode == BACKSPACE_CHAR
8952 && ((p_sta && in_indent)
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008953 || (get_sts_value() != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008954 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 && (*(ml_get_cursor() - 1) == TAB
8956 || (*(ml_get_cursor() - 1) == ' '
8957 && (!*inserted_space_p
8958 || arrow_used))))))
8959 {
8960 int ts;
8961 colnr_T vcol;
8962 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008963 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964
8965 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008966 if (p_sta && in_indent)
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01008967 ts = (int)get_sw_value(curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 else
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02008969 ts = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 /* Compute the virtual column where we want to be. Since
8971 * 'showbreak' may get in the way, need to get the last column of
8972 * the previous character. */
8973 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008974 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975 dec_cursor();
8976 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8977 inc_cursor();
8978 want_vcol = (want_vcol / ts) * ts;
8979
8980 /* delete characters until we are at or before want_vcol */
8981 while (vcol > want_vcol
8982 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008983 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008984
8985 /* insert extra spaces until we are at want_vcol */
8986 while (vcol < want_vcol)
8987 {
8988 /* Remember the first char we inserted */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02008989 if (curwin->w_cursor.lnum == Insstart_orig.lnum
8990 && curwin->w_cursor.col < Insstart_orig.col)
8991 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992
8993#ifdef FEAT_VREPLACE
8994 if (State & VREPLACE_FLAG)
8995 ins_char(' ');
8996 else
8997#endif
8998 {
8999 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009000 if ((State & REPLACE_FLAG))
9001 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009002 }
9003 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
9004 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00009005
9006 /* If we are now back where we started delete one character. Can
9007 * happen when using 'sts' and 'linebreak'. */
9008 if (vcol >= start_vcol)
9009 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010 }
9011
9012 /*
9013 * Delete upto starting point, start of line or previous word.
9014 */
9015 else do
9016 {
9017#ifdef FEAT_RIGHTLEFT
9018 if (!revins_on) /* put cursor on char to be deleted */
9019#endif
9020 dec_cursor();
9021
9022 /* start of word? */
9023 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
9024 {
9025 mode = BACKSPACE_WORD_NOT_SPACE;
9026 temp = vim_iswordc(gchar_cursor());
9027 }
9028 /* end of word? */
9029 else if (mode == BACKSPACE_WORD_NOT_SPACE
9030 && (vim_isspace(cc = gchar_cursor())
9031 || vim_iswordc(cc) != temp))
9032 {
9033#ifdef FEAT_RIGHTLEFT
9034 if (!revins_on)
9035#endif
9036 inc_cursor();
9037#ifdef FEAT_RIGHTLEFT
9038 else if (State & REPLACE_FLAG)
9039 dec_cursor();
9040#endif
9041 break;
9042 }
9043 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00009044 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045 else
9046 {
9047#ifdef FEAT_MBYTE
9048 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009049 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050#endif
9051 (void)del_char(FALSE);
9052#ifdef FEAT_MBYTE
9053 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009054 * If there are combining characters and 'delcombine' is set
9055 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00009056 * character.
9057 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00009058 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059 inc_cursor();
9060#endif
9061#ifdef FEAT_RIGHTLEFT
9062 if (revins_chars)
9063 {
9064 revins_chars--;
9065 revins_legal++;
9066 }
9067 if (revins_on && gchar_cursor() == NUL)
9068 break;
9069#endif
9070 }
9071 /* Just a single backspace?: */
9072 if (mode == BACKSPACE_CHAR)
9073 break;
9074 } while (
9075#ifdef FEAT_RIGHTLEFT
9076 revins_on ||
9077#endif
9078 (curwin->w_cursor.col > mincol
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009079 && (curwin->w_cursor.lnum != Insstart_orig.lnum
9080 || curwin->w_cursor.col != Insstart_orig.col)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009081 did_backspace = TRUE;
9082 }
9083#ifdef FEAT_SMARTINDENT
9084 did_si = FALSE;
9085 can_si = FALSE;
9086 can_si_back = FALSE;
9087#endif
9088 if (curwin->w_cursor.col <= 1)
9089 did_ai = FALSE;
9090 /*
9091 * It's a little strange to put backspaces into the redo
9092 * buffer, but it makes auto-indent a lot easier to deal
9093 * with.
9094 */
9095 AppendCharToRedobuff(c);
9096
9097 /* If deleted before the insertion point, adjust it */
Bram Moolenaar3d1956b2014-04-29 14:44:35 +02009098 if (curwin->w_cursor.lnum == Insstart_orig.lnum
9099 && curwin->w_cursor.col < Insstart_orig.col)
9100 Insstart_orig.col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101
9102 /* vi behaviour: the cursor moves backward but the character that
9103 * was there remains visible
9104 * Vim behaviour: the cursor moves backward and the character that
9105 * was there is erased from the screen.
9106 * We can emulate the vi behaviour by pretending there is a dollar
9107 * displayed even when there isn't.
9108 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009109 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009110 dollar_vcol = curwin->w_virtcol;
9111
Bram Moolenaarce3be472008-01-14 19:12:28 +00009112#ifdef FEAT_FOLDING
9113 /* When deleting a char the cursor line must never be in a closed fold.
9114 * E.g., when 'foldmethod' is indent and deleting the first non-white
9115 * char before a Tab. */
9116 if (did_backspace)
9117 foldOpenCursor();
9118#endif
9119
Bram Moolenaar071d4272004-06-13 20:20:40 +00009120 return did_backspace;
9121}
9122
9123#ifdef FEAT_MOUSE
9124 static void
9125ins_mouse(c)
9126 int c;
9127{
9128 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009129 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009130
9131# ifdef FEAT_GUI
9132 /* When GUI is active, also move/paste when 'mouse' is empty */
9133 if (!gui.in_use)
9134# endif
9135 if (!mouse_has(MOUSE_INSERT))
9136 return;
9137
9138 undisplay_dollar();
9139 tpos = curwin->w_cursor;
9140 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9141 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009142#ifdef FEAT_WINDOWS
9143 win_T *new_curwin = curwin;
9144
9145 if (curwin != old_curwin && win_valid(old_curwin))
9146 {
9147 /* Mouse took us to another window. We need to go back to the
9148 * previous one to stop insert there properly. */
9149 curwin = old_curwin;
9150 curbuf = curwin->w_buffer;
9151 }
9152#endif
9153 start_arrow(curwin == old_curwin ? &tpos : NULL);
9154#ifdef FEAT_WINDOWS
9155 if (curwin != new_curwin && win_valid(new_curwin))
9156 {
9157 curwin = new_curwin;
9158 curbuf = curwin->w_buffer;
9159 }
9160#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009161# ifdef FEAT_CINDENT
9162 can_cindent = TRUE;
9163# endif
9164 }
9165
9166#ifdef FEAT_WINDOWS
9167 /* redraw status lines (in case another window became active) */
9168 redraw_statuslines();
9169#endif
9170}
9171
9172 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009173ins_mousescroll(dir)
9174 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009175{
9176 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009177# if defined(FEAT_WINDOWS)
9178 win_T *old_curwin = curwin;
9179# endif
9180# ifdef FEAT_INS_EXPAND
9181 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009182# endif
9183
9184 tpos = curwin->w_cursor;
9185
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009186# ifdef FEAT_WINDOWS
9187 if (mouse_row >= 0 && mouse_col >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 {
9189 int row, col;
9190
9191 row = mouse_row;
9192 col = mouse_col;
9193
9194 /* find the window at the pointer coordinates */
9195 curwin = mouse_find_win(&row, &col);
9196 curbuf = curwin->w_buffer;
9197 }
9198 if (curwin == old_curwin)
9199# endif
9200 undisplay_dollar();
9201
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009202# ifdef FEAT_INS_EXPAND
9203 /* Don't scroll the window in which completion is being done. */
9204 if (!pum_visible()
9205# if defined(FEAT_WINDOWS)
9206 || curwin != old_curwin
9207# endif
9208 )
9209# endif
9210 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009211 if (dir == MSCR_DOWN || dir == MSCR_UP)
9212 {
9213 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9214 scroll_redraw(dir,
9215 (long)(curwin->w_botline - curwin->w_topline));
9216 else
9217 scroll_redraw(dir, 3L);
9218 }
9219#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009220 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009221 {
9222 int val, step = 6;
9223
9224 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9225 step = W_WIDTH(curwin);
9226 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9227 if (val < 0)
9228 val = 0;
9229 gui_do_horiz_scroll(val, TRUE);
9230 }
9231#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009232# ifdef FEAT_INS_EXPAND
9233 did_scroll = TRUE;
9234# endif
9235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009236
Bram Moolenaar40cf4b42013-02-26 13:30:32 +01009237# ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +00009238 curwin->w_redr_status = TRUE;
9239
9240 curwin = old_curwin;
9241 curbuf = curwin->w_buffer;
9242# endif
9243
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009244# ifdef FEAT_INS_EXPAND
9245 /* The popup menu may overlay the window, need to redraw it.
9246 * TODO: Would be more efficient to only redraw the windows that are
9247 * overlapped by the popup menu. */
9248 if (pum_visible() && did_scroll)
9249 {
9250 redraw_all_later(NOT_VALID);
9251 ins_compl_show_pum();
9252 }
9253# endif
9254
Bram Moolenaar071d4272004-06-13 20:20:40 +00009255 if (!equalpos(curwin->w_cursor, tpos))
9256 {
9257 start_arrow(&tpos);
9258# ifdef FEAT_CINDENT
9259 can_cindent = TRUE;
9260# endif
9261 }
9262}
9263#endif
9264
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009265#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009266 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009267ins_tabline(c)
9268 int c;
9269{
9270 /* We will be leaving the current window, unless closing another tab. */
9271 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9272 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9273 {
9274 undisplay_dollar();
9275 start_arrow(&curwin->w_cursor);
9276# ifdef FEAT_CINDENT
9277 can_cindent = TRUE;
9278# endif
9279 }
9280
9281 if (c == K_TABLINE)
9282 goto_tabpage(current_tab);
9283 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009284 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009285 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009286 redraw_statuslines(); /* will redraw the tabline when needed */
9287 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009288}
9289#endif
9290
9291#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009292 void
9293ins_scroll()
9294{
9295 pos_T tpos;
9296
9297 undisplay_dollar();
9298 tpos = curwin->w_cursor;
9299 if (gui_do_scroll())
9300 {
9301 start_arrow(&tpos);
9302# ifdef FEAT_CINDENT
9303 can_cindent = TRUE;
9304# endif
9305 }
9306}
9307
9308 void
9309ins_horscroll()
9310{
9311 pos_T tpos;
9312
9313 undisplay_dollar();
9314 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009315 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 {
9317 start_arrow(&tpos);
9318# ifdef FEAT_CINDENT
9319 can_cindent = TRUE;
9320# endif
9321 }
9322}
9323#endif
9324
9325 static void
9326ins_left()
9327{
9328 pos_T tpos;
9329
9330#ifdef FEAT_FOLDING
9331 if ((fdo_flags & FDO_HOR) && KeyTyped)
9332 foldOpenCursor();
9333#endif
9334 undisplay_dollar();
9335 tpos = curwin->w_cursor;
9336 if (oneleft() == OK)
9337 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009338#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9339 /* Only call start_arrow() when not busy with preediting, it will
9340 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9341 if (!im_is_preediting())
9342#endif
9343 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009344#ifdef FEAT_RIGHTLEFT
9345 /* If exit reversed string, position is fixed */
9346 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9347 revins_legal++;
9348 revins_chars++;
9349#endif
9350 }
9351
9352 /*
9353 * if 'whichwrap' set for cursor in insert mode may go to
9354 * previous line
9355 */
9356 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9357 {
9358 start_arrow(&tpos);
9359 --(curwin->w_cursor.lnum);
9360 coladvance((colnr_T)MAXCOL);
9361 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9362 }
9363 else
9364 vim_beep();
9365}
9366
9367 static void
9368ins_home(c)
9369 int c;
9370{
9371 pos_T tpos;
9372
9373#ifdef FEAT_FOLDING
9374 if ((fdo_flags & FDO_HOR) && KeyTyped)
9375 foldOpenCursor();
9376#endif
9377 undisplay_dollar();
9378 tpos = curwin->w_cursor;
9379 if (c == K_C_HOME)
9380 curwin->w_cursor.lnum = 1;
9381 curwin->w_cursor.col = 0;
9382#ifdef FEAT_VIRTUALEDIT
9383 curwin->w_cursor.coladd = 0;
9384#endif
9385 curwin->w_curswant = 0;
9386 start_arrow(&tpos);
9387}
9388
9389 static void
9390ins_end(c)
9391 int c;
9392{
9393 pos_T tpos;
9394
9395#ifdef FEAT_FOLDING
9396 if ((fdo_flags & FDO_HOR) && KeyTyped)
9397 foldOpenCursor();
9398#endif
9399 undisplay_dollar();
9400 tpos = curwin->w_cursor;
9401 if (c == K_C_END)
9402 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9403 coladvance((colnr_T)MAXCOL);
9404 curwin->w_curswant = MAXCOL;
9405
9406 start_arrow(&tpos);
9407}
9408
9409 static void
9410ins_s_left()
9411{
9412#ifdef FEAT_FOLDING
9413 if ((fdo_flags & FDO_HOR) && KeyTyped)
9414 foldOpenCursor();
9415#endif
9416 undisplay_dollar();
9417 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9418 {
9419 start_arrow(&curwin->w_cursor);
9420 (void)bck_word(1L, FALSE, FALSE);
9421 curwin->w_set_curswant = TRUE;
9422 }
9423 else
9424 vim_beep();
9425}
9426
9427 static void
9428ins_right()
9429{
9430#ifdef FEAT_FOLDING
9431 if ((fdo_flags & FDO_HOR) && KeyTyped)
9432 foldOpenCursor();
9433#endif
9434 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009435 if (gchar_cursor() != NUL
9436#ifdef FEAT_VIRTUALEDIT
9437 || virtual_active()
9438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009439 )
9440 {
9441 start_arrow(&curwin->w_cursor);
9442 curwin->w_set_curswant = TRUE;
9443#ifdef FEAT_VIRTUALEDIT
9444 if (virtual_active())
9445 oneright();
9446 else
9447#endif
9448 {
9449#ifdef FEAT_MBYTE
9450 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009451 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009452 else
9453#endif
9454 ++curwin->w_cursor.col;
9455 }
9456
9457#ifdef FEAT_RIGHTLEFT
9458 revins_legal++;
9459 if (revins_chars)
9460 revins_chars--;
9461#endif
9462 }
9463 /* if 'whichwrap' set for cursor in insert mode, may move the
9464 * cursor to the next line */
9465 else if (vim_strchr(p_ww, ']') != NULL
9466 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9467 {
9468 start_arrow(&curwin->w_cursor);
9469 curwin->w_set_curswant = TRUE;
9470 ++curwin->w_cursor.lnum;
9471 curwin->w_cursor.col = 0;
9472 }
9473 else
9474 vim_beep();
9475}
9476
9477 static void
9478ins_s_right()
9479{
9480#ifdef FEAT_FOLDING
9481 if ((fdo_flags & FDO_HOR) && KeyTyped)
9482 foldOpenCursor();
9483#endif
9484 undisplay_dollar();
9485 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9486 || gchar_cursor() != NUL)
9487 {
9488 start_arrow(&curwin->w_cursor);
9489 (void)fwd_word(1L, FALSE, 0);
9490 curwin->w_set_curswant = TRUE;
9491 }
9492 else
9493 vim_beep();
9494}
9495
9496 static void
9497ins_up(startcol)
9498 int startcol; /* when TRUE move to Insstart.col */
9499{
9500 pos_T tpos;
9501 linenr_T old_topline = curwin->w_topline;
9502#ifdef FEAT_DIFF
9503 int old_topfill = curwin->w_topfill;
9504#endif
9505
9506 undisplay_dollar();
9507 tpos = curwin->w_cursor;
9508 if (cursor_up(1L, TRUE) == OK)
9509 {
9510 if (startcol)
9511 coladvance(getvcol_nolist(&Insstart));
9512 if (old_topline != curwin->w_topline
9513#ifdef FEAT_DIFF
9514 || old_topfill != curwin->w_topfill
9515#endif
9516 )
9517 redraw_later(VALID);
9518 start_arrow(&tpos);
9519#ifdef FEAT_CINDENT
9520 can_cindent = TRUE;
9521#endif
9522 }
9523 else
9524 vim_beep();
9525}
9526
9527 static void
9528ins_pageup()
9529{
9530 pos_T tpos;
9531
9532 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009533
9534#ifdef FEAT_WINDOWS
9535 if (mod_mask & MOD_MASK_CTRL)
9536 {
9537 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009538 if (first_tabpage->tp_next != NULL)
9539 {
9540 start_arrow(&curwin->w_cursor);
9541 goto_tabpage(-1);
9542 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009543 return;
9544 }
9545#endif
9546
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547 tpos = curwin->w_cursor;
9548 if (onepage(BACKWARD, 1L) == OK)
9549 {
9550 start_arrow(&tpos);
9551#ifdef FEAT_CINDENT
9552 can_cindent = TRUE;
9553#endif
9554 }
9555 else
9556 vim_beep();
9557}
9558
9559 static void
9560ins_down(startcol)
9561 int startcol; /* when TRUE move to Insstart.col */
9562{
9563 pos_T tpos;
9564 linenr_T old_topline = curwin->w_topline;
9565#ifdef FEAT_DIFF
9566 int old_topfill = curwin->w_topfill;
9567#endif
9568
9569 undisplay_dollar();
9570 tpos = curwin->w_cursor;
9571 if (cursor_down(1L, TRUE) == OK)
9572 {
9573 if (startcol)
9574 coladvance(getvcol_nolist(&Insstart));
9575 if (old_topline != curwin->w_topline
9576#ifdef FEAT_DIFF
9577 || old_topfill != curwin->w_topfill
9578#endif
9579 )
9580 redraw_later(VALID);
9581 start_arrow(&tpos);
9582#ifdef FEAT_CINDENT
9583 can_cindent = TRUE;
9584#endif
9585 }
9586 else
9587 vim_beep();
9588}
9589
9590 static void
9591ins_pagedown()
9592{
9593 pos_T tpos;
9594
9595 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009596
9597#ifdef FEAT_WINDOWS
9598 if (mod_mask & MOD_MASK_CTRL)
9599 {
9600 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009601 if (first_tabpage->tp_next != NULL)
9602 {
9603 start_arrow(&curwin->w_cursor);
9604 goto_tabpage(0);
9605 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009606 return;
9607 }
9608#endif
9609
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 tpos = curwin->w_cursor;
9611 if (onepage(FORWARD, 1L) == OK)
9612 {
9613 start_arrow(&tpos);
9614#ifdef FEAT_CINDENT
9615 can_cindent = TRUE;
9616#endif
9617 }
9618 else
9619 vim_beep();
9620}
9621
9622#ifdef FEAT_DND
9623 static void
9624ins_drop()
9625{
9626 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9627}
9628#endif
9629
9630/*
9631 * Handle TAB in Insert or Replace mode.
9632 * Return TRUE when the TAB needs to be inserted like a normal character.
9633 */
9634 static int
9635ins_tab()
9636{
9637 int ind;
9638 int i;
9639 int temp;
9640
9641 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9642 Insstart_blank_vcol = get_nolist_virtcol();
9643 if (echeck_abbr(TAB + ABBR_OFF))
9644 return FALSE;
9645
9646 ind = inindent(0);
9647#ifdef FEAT_CINDENT
9648 if (ind)
9649 can_cindent = FALSE;
9650#endif
9651
9652 /*
9653 * When nothing special, insert TAB like a normal character
9654 */
9655 if (!curbuf->b_p_et
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009656 && !(p_sta && ind && curbuf->b_p_ts != get_sw_value(curbuf))
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009657 && get_sts_value() == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 return TRUE;
9659
9660 if (stop_arrow() == FAIL)
9661 return TRUE;
9662
9663 did_ai = FALSE;
9664#ifdef FEAT_SMARTINDENT
9665 did_si = FALSE;
9666 can_si = FALSE;
9667 can_si_back = FALSE;
9668#endif
9669 AppendToRedobuff((char_u *)"\t");
9670
9671 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +01009672 temp = (int)get_sw_value(curbuf);
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009673 else if (curbuf->b_p_sts != 0) /* use 'softtabstop' when set */
9674 temp = (int)get_sts_value();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009675 else /* otherwise use 'tabstop' */
9676 temp = (int)curbuf->b_p_ts;
9677 temp -= get_nolist_virtcol() % temp;
9678
9679 /*
9680 * Insert the first space with ins_char(). It will delete one char in
9681 * replace mode. Insert the rest with ins_str(); it will not delete any
9682 * chars. For VREPLACE mode, we use ins_char() for all characters.
9683 */
9684 ins_char(' ');
9685 while (--temp > 0)
9686 {
9687#ifdef FEAT_VREPLACE
9688 if (State & VREPLACE_FLAG)
9689 ins_char(' ');
9690 else
9691#endif
9692 {
9693 ins_str((char_u *)" ");
9694 if (State & REPLACE_FLAG) /* no char replaced */
9695 replace_push(NUL);
9696 }
9697 }
9698
9699 /*
9700 * When 'expandtab' not set: Replace spaces by TABs where possible.
9701 */
Bram Moolenaar9f340fa2012-10-21 00:10:39 +02009702 if (!curbuf->b_p_et && (get_sts_value() || (p_sta && ind)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009703 {
9704 char_u *ptr;
9705#ifdef FEAT_VREPLACE
9706 char_u *saved_line = NULL; /* init for GCC */
9707 pos_T pos;
9708#endif
9709 pos_T fpos;
9710 pos_T *cursor;
9711 colnr_T want_vcol, vcol;
9712 int change_col = -1;
9713 int save_list = curwin->w_p_list;
9714
9715 /*
9716 * Get the current line. For VREPLACE mode, don't make real changes
9717 * yet, just work on a copy of the line.
9718 */
9719#ifdef FEAT_VREPLACE
9720 if (State & VREPLACE_FLAG)
9721 {
9722 pos = curwin->w_cursor;
9723 cursor = &pos;
9724 saved_line = vim_strsave(ml_get_curline());
9725 if (saved_line == NULL)
9726 return FALSE;
9727 ptr = saved_line + pos.col;
9728 }
9729 else
9730#endif
9731 {
9732 ptr = ml_get_cursor();
9733 cursor = &curwin->w_cursor;
9734 }
9735
9736 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9737 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9738 curwin->w_p_list = FALSE;
9739
9740 /* Find first white before the cursor */
9741 fpos = curwin->w_cursor;
9742 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9743 {
9744 --fpos.col;
9745 --ptr;
9746 }
9747
9748 /* In Replace mode, don't change characters before the insert point. */
9749 if ((State & REPLACE_FLAG)
9750 && fpos.lnum == Insstart.lnum
9751 && fpos.col < Insstart.col)
9752 {
9753 ptr += Insstart.col - fpos.col;
9754 fpos.col = Insstart.col;
9755 }
9756
9757 /* compute virtual column numbers of first white and cursor */
9758 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9759 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9760
9761 /* Use as many TABs as possible. Beware of 'showbreak' and
9762 * 'linebreak' adding extra virtual columns. */
9763 while (vim_iswhite(*ptr))
9764 {
9765 i = lbr_chartabsize((char_u *)"\t", vcol);
9766 if (vcol + i > want_vcol)
9767 break;
9768 if (*ptr != TAB)
9769 {
9770 *ptr = TAB;
9771 if (change_col < 0)
9772 {
9773 change_col = fpos.col; /* Column of first change */
9774 /* May have to adjust Insstart */
9775 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9776 Insstart.col = fpos.col;
9777 }
9778 }
9779 ++fpos.col;
9780 ++ptr;
9781 vcol += i;
9782 }
9783
9784 if (change_col >= 0)
9785 {
9786 int repl_off = 0;
9787
9788 /* Skip over the spaces we need. */
9789 while (vcol < want_vcol && *ptr == ' ')
9790 {
9791 vcol += lbr_chartabsize(ptr, vcol);
9792 ++ptr;
9793 ++repl_off;
9794 }
9795 if (vcol > want_vcol)
9796 {
9797 /* Must have a char with 'showbreak' just before it. */
9798 --ptr;
9799 --repl_off;
9800 }
9801 fpos.col += repl_off;
9802
9803 /* Delete following spaces. */
9804 i = cursor->col - fpos.col;
9805 if (i > 0)
9806 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009807 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 /* correct replace stack. */
9809 if ((State & REPLACE_FLAG)
9810#ifdef FEAT_VREPLACE
9811 && !(State & VREPLACE_FLAG)
9812#endif
9813 )
9814 for (temp = i; --temp >= 0; )
9815 replace_join(repl_off);
9816 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009817#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009818 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009819 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009820 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009821 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9822 (char_u *)"\t", 1);
9823 }
9824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 cursor->col -= i;
9826
9827#ifdef FEAT_VREPLACE
9828 /*
9829 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9830 * backspacing over the changed spacing and then inserting the new
9831 * spacing.
9832 */
9833 if (State & VREPLACE_FLAG)
9834 {
9835 /* Backspace from real cursor to change_col */
9836 backspace_until_column(change_col);
9837
9838 /* Insert each char in saved_line from changed_col to
9839 * ptr-cursor */
9840 ins_bytes_len(saved_line + change_col,
9841 cursor->col - change_col);
9842 }
9843#endif
9844 }
9845
9846#ifdef FEAT_VREPLACE
9847 if (State & VREPLACE_FLAG)
9848 vim_free(saved_line);
9849#endif
9850 curwin->w_p_list = save_list;
9851 }
9852
9853 return FALSE;
9854}
9855
9856/*
9857 * Handle CR or NL in insert mode.
9858 * Return TRUE when out of memory or can't undo.
9859 */
9860 static int
9861ins_eol(c)
9862 int c;
9863{
9864 int i;
9865
9866 if (echeck_abbr(c + ABBR_OFF))
9867 return FALSE;
9868 if (stop_arrow() == FAIL)
9869 return TRUE;
9870 undisplay_dollar();
9871
9872 /*
9873 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9874 * character under the cursor. Only push a NUL on the replace stack,
9875 * nothing to put back when the NL is deleted.
9876 */
9877 if ((State & REPLACE_FLAG)
9878#ifdef FEAT_VREPLACE
9879 && !(State & VREPLACE_FLAG)
9880#endif
9881 )
9882 replace_push(NUL);
9883
9884 /*
9885 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9886 * replacing the next line, so we push all of the characters left on the
9887 * line onto the replace stack. This is not done here though, it is done
9888 * in open_line().
9889 */
9890
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009891#ifdef FEAT_VIRTUALEDIT
9892 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9893 * CTRL-O). */
9894 if (virtual_active() && curwin->w_cursor.coladd > 0)
9895 coladvance(getviscol());
9896#endif
9897
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898#ifdef FEAT_RIGHTLEFT
9899# ifdef FEAT_FKMAP
9900 if (p_altkeymap && p_fkmap)
9901 fkmap(NL);
9902# endif
9903 /* NL in reverse insert will always start in the end of
9904 * current line. */
9905 if (revins_on)
9906 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9907#endif
9908
9909 AppendToRedobuff(NL_STR);
9910 i = open_line(FORWARD,
9911#ifdef FEAT_COMMENTS
9912 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9913#endif
9914 0, old_indent);
9915 old_indent = 0;
9916#ifdef FEAT_CINDENT
9917 can_cindent = TRUE;
9918#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009919#ifdef FEAT_FOLDING
9920 /* When inserting a line the cursor line must never be in a closed fold. */
9921 foldOpenCursor();
9922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009923
9924 return (!i);
9925}
9926
9927#ifdef FEAT_DIGRAPHS
9928/*
9929 * Handle digraph in insert mode.
9930 * Returns character still to be inserted, or NUL when nothing remaining to be
9931 * done.
9932 */
9933 static int
9934ins_digraph()
9935{
9936 int c;
9937 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009938 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009939
9940 pc_status = PC_STATUS_UNSET;
9941 if (redrawing() && !char_avail())
9942 {
9943 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009944 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009945
9946 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009947 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009948#ifdef FEAT_CMDL_INFO
9949 add_to_showcmd_c(Ctrl_K);
9950#endif
9951 }
9952
9953#ifdef USE_ON_FLY_SCROLL
9954 dont_scroll = TRUE; /* disallow scrolling here */
9955#endif
9956
9957 /* don't map the digraph chars. This also prevents the
9958 * mode message to be deleted when ESC is hit */
9959 ++no_mapping;
9960 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009961 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009962 --no_mapping;
9963 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009964 if (did_putchar)
9965 /* when the line fits in 'columns' the '?' is at the start of the next
9966 * line and will not be removed by the redraw */
9967 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009968
Bram Moolenaar071d4272004-06-13 20:20:40 +00009969 if (IS_SPECIAL(c) || mod_mask) /* special key */
9970 {
9971#ifdef FEAT_CMDL_INFO
9972 clear_showcmd();
9973#endif
9974 insert_special(c, TRUE, FALSE);
9975 return NUL;
9976 }
9977 if (c != ESC)
9978 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009979 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009980 if (redrawing() && !char_avail())
9981 {
9982 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009983 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009984
9985 if (char2cells(c) == 1)
9986 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009987 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009988 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009989 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009990 }
9991#ifdef FEAT_CMDL_INFO
9992 add_to_showcmd_c(c);
9993#endif
9994 }
9995 ++no_mapping;
9996 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009997 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009998 --no_mapping;
9999 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +020010000 if (did_putchar)
10001 /* when the line fits in 'columns' the '?' is at the start of the
10002 * next line and will not be removed by a redraw */
10003 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010004 if (cc != ESC)
10005 {
10006 AppendToRedobuff((char_u *)CTRL_V_STR);
10007 c = getdigraph(c, cc, TRUE);
10008#ifdef FEAT_CMDL_INFO
10009 clear_showcmd();
10010#endif
10011 return c;
10012 }
10013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010014#ifdef FEAT_CMDL_INFO
10015 clear_showcmd();
10016#endif
10017 return NUL;
10018}
10019#endif
10020
10021/*
10022 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
10023 * Returns the char to be inserted, or NUL if none found.
10024 */
Bram Moolenaar8320da42012-04-30 18:18:47 +020010025 int
Bram Moolenaar071d4272004-06-13 20:20:40 +000010026ins_copychar(lnum)
10027 linenr_T lnum;
10028{
10029 int c;
10030 int temp;
10031 char_u *ptr, *prev_ptr;
10032
10033 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
10034 {
10035 vim_beep();
10036 return NUL;
10037 }
10038
10039 /* try to advance to the cursor column */
10040 temp = 0;
10041 ptr = ml_get(lnum);
10042 prev_ptr = ptr;
10043 validate_virtcol();
10044 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
10045 {
10046 prev_ptr = ptr;
10047 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
10048 }
10049 if ((colnr_T)temp > curwin->w_virtcol)
10050 ptr = prev_ptr;
10051
10052#ifdef FEAT_MBYTE
10053 c = (*mb_ptr2char)(ptr);
10054#else
10055 c = *ptr;
10056#endif
10057 if (c == NUL)
10058 vim_beep();
10059 return c;
10060}
10061
Bram Moolenaar4be06f92005-07-29 22:36:03 +000010062/*
10063 * CTRL-Y or CTRL-E typed in Insert mode.
10064 */
10065 static int
10066ins_ctrl_ey(tc)
10067 int tc;
10068{
10069 int c = tc;
10070
10071#ifdef FEAT_INS_EXPAND
10072 if (ctrl_x_mode == CTRL_X_SCROLL)
10073 {
10074 if (c == Ctrl_Y)
10075 scrolldown_clamp();
10076 else
10077 scrollup_clamp();
10078 redraw_later(VALID);
10079 }
10080 else
10081#endif
10082 {
10083 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
10084 if (c != NUL)
10085 {
10086 long tw_save;
10087
10088 /* The character must be taken literally, insert like it
10089 * was typed after a CTRL-V, and pretend 'textwidth'
10090 * wasn't set. Digits, 'o' and 'x' are special after a
10091 * CTRL-V, don't use it for these. */
10092 if (c < 256 && !isalnum(c))
10093 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
10094 tw_save = curbuf->b_p_tw;
10095 curbuf->b_p_tw = -1;
10096 insert_special(c, TRUE, FALSE);
10097 curbuf->b_p_tw = tw_save;
10098#ifdef FEAT_RIGHTLEFT
10099 revins_chars++;
10100 revins_legal++;
10101#endif
10102 c = Ctrl_V; /* pretend CTRL-V is last character */
10103 auto_format(FALSE, TRUE);
10104 }
10105 }
10106 return c;
10107}
10108
Bram Moolenaar071d4272004-06-13 20:20:40 +000010109#ifdef FEAT_SMARTINDENT
10110/*
10111 * Try to do some very smart auto-indenting.
10112 * Used when inserting a "normal" character.
10113 */
10114 static void
10115ins_try_si(c)
10116 int c;
10117{
10118 pos_T *pos, old_pos;
10119 char_u *ptr;
10120 int i;
10121 int temp;
10122
10123 /*
10124 * do some very smart indenting when entering '{' or '}'
10125 */
10126 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10127 {
10128 /*
10129 * for '}' set indent equal to indent of line containing matching '{'
10130 */
10131 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10132 {
10133 old_pos = curwin->w_cursor;
10134 /*
10135 * If the matching '{' has a ')' immediately before it (ignoring
10136 * white-space), then line up with the start of the line
10137 * containing the matching '(' if there is one. This handles the
10138 * case where an "if (..\n..) {" statement continues over multiple
10139 * lines -- webb
10140 */
10141 ptr = ml_get(pos->lnum);
10142 i = pos->col;
10143 if (i > 0) /* skip blanks before '{' */
10144 while (--i > 0 && vim_iswhite(ptr[i]))
10145 ;
10146 curwin->w_cursor.lnum = pos->lnum;
10147 curwin->w_cursor.col = i;
10148 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10149 curwin->w_cursor = *pos;
10150 i = get_indent();
10151 curwin->w_cursor = old_pos;
10152#ifdef FEAT_VREPLACE
10153 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010154 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155 else
10156#endif
10157 (void)set_indent(i, SIN_CHANGED);
10158 }
10159 else if (curwin->w_cursor.col > 0)
10160 {
10161 /*
10162 * when inserting '{' after "O" reduce indent, but not
10163 * more than indent of previous line
10164 */
10165 temp = TRUE;
10166 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10167 {
10168 old_pos = curwin->w_cursor;
10169 i = get_indent();
10170 while (curwin->w_cursor.lnum > 1)
10171 {
10172 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10173
10174 /* ignore empty lines and lines starting with '#'. */
10175 if (*ptr != '#' && *ptr != NUL)
10176 break;
10177 }
10178 if (get_indent() >= i)
10179 temp = FALSE;
10180 curwin->w_cursor = old_pos;
10181 }
10182 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010183 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010184 }
10185 }
10186
10187 /*
10188 * set indent of '#' always to 0
10189 */
10190 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10191 {
10192 /* remember current indent for next line */
10193 old_indent = get_indent();
10194 (void)set_indent(0, SIN_CHANGED);
10195 }
10196
10197 /* Adjust ai_col, the char at this position can be deleted. */
10198 if (ai_col > curwin->w_cursor.col)
10199 ai_col = curwin->w_cursor.col;
10200}
10201#endif
10202
10203/*
10204 * Get the value that w_virtcol would have when 'list' is off.
10205 * Unless 'cpo' contains the 'L' flag.
10206 */
10207 static colnr_T
10208get_nolist_virtcol()
10209{
10210 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10211 return getvcol_nolist(&curwin->w_cursor);
10212 validate_virtcol();
10213 return curwin->w_virtcol;
10214}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010215
10216#ifdef FEAT_AUTOCMD
10217/*
10218 * Handle the InsertCharPre autocommand.
10219 * "c" is the character that was typed.
10220 * Return a pointer to allocated memory with the replacement string.
10221 * Return NULL to continue inserting "c".
10222 */
10223 static char_u *
10224do_insert_char_pre(c)
10225 int c;
10226{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010227 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010228 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010229
10230 /* Return quickly when there is nothing to do. */
10231 if (!has_insertcharpre())
10232 return NULL;
10233
Bram Moolenaar704984a2012-06-01 14:57:51 +020010234#ifdef FEAT_MBYTE
10235 if (has_mbyte)
10236 buf[(*mb_char2bytes)(c, buf)] = NUL;
10237 else
10238#endif
10239 {
10240 buf[0] = c;
10241 buf[1] = NUL;
10242 }
10243
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010244 /* Lock the text to avoid weird things from happening. */
10245 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010246 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010247
Bram Moolenaar704984a2012-06-01 14:57:51 +020010248 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010249 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010250 {
10251 /* Get the value of v:char. It may be empty or more than one
10252 * character. Only use it when changed, otherwise continue with the
10253 * original character to avoid breaking autoindent. */
10254 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10255 res = vim_strsave(get_vim_var_str(VV_CHAR));
10256 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010257
10258 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10259 --textlock;
10260
10261 return res;
10262}
10263#endif