blob: 013fc5abcb7742995f5cb5ac47a36e4f832eaea8 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000056 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000060static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar37dd0182010-11-10 16:54:20 +010061#ifdef FEAT_COMPL_FUNC
62static char e_complwin[] = N_("E839: Completion function changed window");
63static char e_compldel[] = N_("E840: Completion function deleted text");
64#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66/*
67 * Structure used to store one match for insert completion.
68 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000069typedef struct compl_S compl_T;
70struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
Bram Moolenaar572cb562005-08-05 21:35:02 +000072 compl_T *cp_next;
73 compl_T *cp_prev;
74 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000075 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000076 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000077 char_u *cp_fname; /* file containing the match, allocated when
78 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000079 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
80 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081};
82
Bram Moolenaar572cb562005-08-05 21:35:02 +000083#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#define FREE_FNAME (2)
85
86/*
87 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000088 * "compl_first_match" points to the start of the list.
89 * "compl_curr_match" points to the currently selected entry.
90 * "compl_shown_match" is different from compl_curr_match during
91 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000093static compl_T *compl_first_match = NULL;
94static compl_T *compl_curr_match = NULL;
95static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar779b74b2006-04-10 14:55:34 +000097/* After using a cursor key <Enter> selects a match in the popup menu,
98 * otherwise it inserts a line break. */
99static int compl_enter_selects = FALSE;
100
Bram Moolenaara6557602006-02-04 22:43:20 +0000101/* When "compl_leader" is not NULL only matches that start with this string
102 * are used. */
103static char_u *compl_leader = NULL;
104
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000105static int compl_get_longest = FALSE; /* put longest common string
106 in compl_leader */
107
Bram Moolenaara6557602006-02-04 22:43:20 +0000108static int compl_used_match; /* Selected one of the matches. When
109 FALSE the match was edited or using
110 the longest common string. */
111
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000112static int compl_was_interrupted = FALSE; /* didn't finish finding
113 completions. */
114
115static int compl_restarting = FALSE; /* don't insert match */
116
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000117/* When the first completion is done "compl_started" is set. When it's
118 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000119static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000120
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000121/* Set when doing something for completion that may call edit() recursively,
122 * which is not allowed. */
123static int compl_busy = FALSE;
124
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static int compl_matches = 0;
126static char_u *compl_pattern = NULL;
127static int compl_direction = FORWARD;
128static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000129static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000130static pos_T compl_startpos;
131static colnr_T compl_col = 0; /* column where the text starts
132 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static char_u *compl_orig_text = NULL; /* text as it was before
134 * completion started */
135static int compl_cont_mode = 0;
136static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaar82139082011-09-14 16:52:09 +0200138static int compl_opt_refresh_always = FALSE;
139
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000140static void ins_ctrl_x __ARGS((void));
141static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000142static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000143static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000144static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000145static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000146static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000147static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000148static void ins_compl_upd_pum __ARGS((void));
149static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000150static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000151static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000152static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000153static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000154static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static void ins_compl_free __ARGS((void));
156static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000157static int ins_compl_bs __ARGS((void));
Bram Moolenaar82139082011-09-14 16:52:09 +0200158static int ins_compl_need_restart __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000159static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000160static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar82139082011-09-14 16:52:09 +0200161static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000162static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000163static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000164static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000165static int ins_compl_prep __ARGS((int c));
Bram Moolenaar62951b12011-09-21 18:23:05 +0200166static void ins_compl_fixRedoBufForLeader __ARGS((char_u *ptr_arg));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000168#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
169static void ins_compl_add_list __ARGS((list_T *list));
Bram Moolenaar82139082011-09-14 16:52:09 +0200170static void ins_compl_add_dict __ARGS((dict_T *dict));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000171#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000172static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173static void ins_compl_delete __ARGS((void));
174static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000175static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000176static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000177static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000178static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000179static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000180static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000181static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000182#endif /* FEAT_INS_EXPAND */
183
184#define BACKSPACE_CHAR 1
185#define BACKSPACE_WORD 2
186#define BACKSPACE_WORD_NOT_SPACE 3
187#define BACKSPACE_LINE 4
188
Bram Moolenaar754b5602006-02-09 23:53:20 +0000189static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000190static void ins_ctrl_v __ARGS((void));
191static void undisplay_dollar __ARGS((void));
192static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000193static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194static void check_auto_format __ARGS((int));
195static void redo_literal __ARGS((int c));
196static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000197#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000198static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000199static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000200static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000201#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000202static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
203static int echeck_abbr __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000204static int replace_pop __ARGS((void));
205static void replace_join __ARGS((int off));
206static void replace_pop_ins __ARGS((void));
207#ifdef FEAT_MBYTE
208static void mb_replace_pop_ins __ARGS((int cc));
209#endif
210static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000211static void replace_do_bs __ARGS((int limit_col));
212static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000213#ifdef FEAT_CINDENT
214static int cindent_on __ARGS((void));
215#endif
216static void ins_reg __ARGS((void));
217static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000218static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000219static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000220#ifdef FEAT_RIGHTLEFT
221static void ins_ctrl_ __ARGS((void));
222#endif
223#ifdef FEAT_VISUAL
224static int ins_start_select __ARGS((int c));
225#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000226static void ins_insert __ARGS((int replaceState));
227static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static void ins_shift __ARGS((int c, int lastc));
229static void ins_del __ARGS((void));
230static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
231#ifdef FEAT_MOUSE
232static void ins_mouse __ARGS((int c));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200233static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000234#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000235#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
236static void ins_tabline __ARGS((int c));
237#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238static void ins_left __ARGS((void));
239static void ins_home __ARGS((int c));
240static void ins_end __ARGS((int c));
241static void ins_s_left __ARGS((void));
242static void ins_right __ARGS((void));
243static void ins_s_right __ARGS((void));
244static void ins_up __ARGS((int startcol));
245static void ins_pageup __ARGS((void));
246static void ins_down __ARGS((int startcol));
247static void ins_pagedown __ARGS((void));
248#ifdef FEAT_DND
249static void ins_drop __ARGS((void));
250#endif
251static int ins_tab __ARGS((void));
252static int ins_eol __ARGS((int c));
253#ifdef FEAT_DIGRAPHS
254static int ins_digraph __ARGS((void));
255#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000256static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257#ifdef FEAT_SMARTINDENT
258static void ins_try_si __ARGS((int c));
259#endif
260static colnr_T get_nolist_virtcol __ARGS((void));
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100261#ifdef FEAT_AUTOCMD
262static char_u *do_insert_char_pre __ARGS((int c));
263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000264
265static colnr_T Insstart_textlen; /* length of line when insert started */
266static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
267
268static char_u *last_insert = NULL; /* the text of the previous insert,
269 K_SPECIAL and CSI are escaped */
270static int last_insert_skip; /* nr of chars in front of previous insert */
271static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000272static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273
274#ifdef FEAT_CINDENT
275static int can_cindent; /* may do cindenting on this line */
276#endif
277
278static int old_indent = 0; /* for ^^D command in insert mode */
279
280#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000281static int revins_on; /* reverse insert mode on */
282static int revins_chars; /* how much to skip after edit */
283static int revins_legal; /* was the last char 'legal'? */
284static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000285#endif
286
Bram Moolenaar071d4272004-06-13 20:20:40 +0000287static int ins_need_undo; /* call u_save() before inserting a
288 char. Set when edit() is called.
289 after that arrow_used is used. */
290
291static int did_add_space = FALSE; /* auto_format() added an extra space
292 under the cursor */
293
294/*
295 * edit(): Start inserting text.
296 *
297 * "cmdchar" can be:
298 * 'i' normal insert command
299 * 'a' normal append command
300 * 'R' replace command
301 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
302 * but still only one <CR> is inserted. The <Esc> is not used for redo.
303 * 'g' "gI" command.
304 * 'V' "gR" command for Virtual Replace mode.
305 * 'v' "gr" command for single character Virtual Replace mode.
306 *
307 * This function is not called recursively. For CTRL-O commands, it returns
308 * and lets the caller handle the Normal-mode command.
309 *
310 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
311 */
312 int
313edit(cmdchar, startln, count)
314 int cmdchar;
315 int startln; /* if set, insert at start of line */
316 long count;
317{
318 int c = 0;
319 char_u *ptr;
320 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000321 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000322 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 int i;
324 int did_backspace = TRUE; /* previous char was backspace */
325#ifdef FEAT_CINDENT
326 int line_is_white = FALSE; /* line is empty before insert */
327#endif
328 linenr_T old_topline = 0; /* topline before insertion */
329#ifdef FEAT_DIFF
330 int old_topfill = -1;
331#endif
332 int inserted_space = FALSE; /* just inserted a space */
333 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000334 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000335
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000336 /* Remember whether editing was restarted after CTRL-O. */
337 did_restart_edit = restart_edit;
338
Bram Moolenaar071d4272004-06-13 20:20:40 +0000339 /* sleep before redrawing, needed for "CTRL-O :" that results in an
340 * error message */
341 check_for_delay(TRUE);
342
343#ifdef HAVE_SANDBOX
344 /* Don't allow inserting in the sandbox. */
345 if (sandbox != 0)
346 {
347 EMSG(_(e_sandbox));
348 return FALSE;
349 }
350#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000351 /* Don't allow changes in the buffer while editing the cmdline. The
352 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000353 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000354 {
355 EMSG(_(e_secure));
356 return FALSE;
357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358
359#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000360 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000361 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000362 {
363 EMSG(_(e_secure));
364 return FALSE;
365 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000366 ins_compl_clear(); /* clear stuff for CTRL-X mode */
367#endif
368
Bram Moolenaar843ee412004-06-30 16:16:41 +0000369#ifdef FEAT_AUTOCMD
370 /*
371 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
372 */
373 if (cmdchar != 'r' && cmdchar != 'v')
374 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000375# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000376 if (cmdchar == 'R')
377 ptr = (char_u *)"r";
378 else if (cmdchar == 'V')
379 ptr = (char_u *)"v";
380 else
381 ptr = (char_u *)"i";
382 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000383# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000384 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
385 }
386#endif
387
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200388#ifdef FEAT_CONCEAL
389 /* Check if the cursor line needs redrawing before changing State. If
390 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200391 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200392#endif
393
Bram Moolenaar071d4272004-06-13 20:20:40 +0000394#ifdef FEAT_MOUSE
395 /*
396 * When doing a paste with the middle mouse button, Insstart is set to
397 * where the paste started.
398 */
399 if (where_paste_started.lnum != 0)
400 Insstart = where_paste_started;
401 else
402#endif
403 {
404 Insstart = curwin->w_cursor;
405 if (startln)
406 Insstart.col = 0;
407 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000408 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000409 Insstart_blank_vcol = MAXCOL;
410 if (!did_ai)
411 ai_col = 0;
412
413 if (cmdchar != NUL && restart_edit == 0)
414 {
415 ResetRedobuff();
416 AppendNumberToRedobuff(count);
417#ifdef FEAT_VREPLACE
418 if (cmdchar == 'V' || cmdchar == 'v')
419 {
420 /* "gR" or "gr" command */
421 AppendCharToRedobuff('g');
422 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
423 }
424 else
425#endif
426 {
427 AppendCharToRedobuff(cmdchar);
428 if (cmdchar == 'g') /* "gI" command */
429 AppendCharToRedobuff('I');
430 else if (cmdchar == 'r') /* "r<CR>" command */
431 count = 1; /* insert only one <CR> */
432 }
433 }
434
435 if (cmdchar == 'R')
436 {
437#ifdef FEAT_FKMAP
438 if (p_fkmap && p_ri)
439 {
440 beep_flush();
441 EMSG(farsi_text_3); /* encoded in Farsi */
442 State = INSERT;
443 }
444 else
445#endif
446 State = REPLACE;
447 }
448#ifdef FEAT_VREPLACE
449 else if (cmdchar == 'V' || cmdchar == 'v')
450 {
451 State = VREPLACE;
452 replaceState = VREPLACE;
453 orig_line_count = curbuf->b_ml.ml_line_count;
454 vr_lines_changed = 1;
455 }
456#endif
457 else
458 State = INSERT;
459
460 stop_insert_mode = FALSE;
461
462 /*
463 * Need to recompute the cursor position, it might move when the cursor is
464 * on a TAB or special character.
465 */
466 curs_columns(TRUE);
467
468 /*
469 * Enable langmap or IME, indicated by 'iminsert'.
470 * Note that IME may enabled/disabled without us noticing here, thus the
471 * 'iminsert' value may not reflect what is actually used. It is updated
472 * when hitting <Esc>.
473 */
474 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
475 State |= LANGMAP;
476#ifdef USE_IM_CONTROL
477 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
478#endif
479
Bram Moolenaar071d4272004-06-13 20:20:40 +0000480#ifdef FEAT_MOUSE
481 setmouse();
482#endif
483#ifdef FEAT_CMDL_INFO
484 clear_showcmd();
485#endif
486#ifdef FEAT_RIGHTLEFT
487 /* there is no reverse replace mode */
488 revins_on = (State == INSERT && p_ri);
489 if (revins_on)
490 undisplay_dollar();
491 revins_chars = 0;
492 revins_legal = 0;
493 revins_scol = -1;
494#endif
495
496 /*
497 * Handle restarting Insert mode.
498 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
499 * restart_edit non-zero, and something in the stuff buffer.
500 */
501 if (restart_edit != 0 && stuff_empty())
502 {
503#ifdef FEAT_MOUSE
504 /*
505 * After a paste we consider text typed to be part of the insert for
506 * the pasted text. You can backspace over the pasted text too.
507 */
508 if (where_paste_started.lnum)
509 arrow_used = FALSE;
510 else
511#endif
512 arrow_used = TRUE;
513 restart_edit = 0;
514
515 /*
516 * If the cursor was after the end-of-line before the CTRL-O and it is
517 * now at the end-of-line, put it after the end-of-line (this is not
518 * correct in very rare cases).
519 * Also do this if curswant is greater than the current virtual
520 * column. Eg after "^O$" or "^O80|".
521 */
522 validate_virtcol();
523 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000524 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000525 || curwin->w_curswant > curwin->w_virtcol)
526 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
527 {
528 if (ptr[1] == NUL)
529 ++curwin->w_cursor.col;
530#ifdef FEAT_MBYTE
531 else if (has_mbyte)
532 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000533 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000534 if (ptr[i] == NUL)
535 curwin->w_cursor.col += i;
536 }
537#endif
538 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000539 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000540 }
541 else
542 arrow_used = FALSE;
543
544 /* we are in insert mode now, don't need to start it anymore */
545 need_start_insertmode = FALSE;
546
547 /* Need to save the line for undo before inserting the first char. */
548 ins_need_undo = TRUE;
549
550#ifdef FEAT_MOUSE
551 where_paste_started.lnum = 0;
552#endif
553#ifdef FEAT_CINDENT
554 can_cindent = TRUE;
555#endif
556#ifdef FEAT_FOLDING
557 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
558 * restarting. */
559 if (!p_im && did_restart_edit == 0)
560 foldOpenCursor();
561#endif
562
563 /*
564 * If 'showmode' is set, show the current (insert/replace/..) mode.
565 * A warning message for changing a readonly file is given here, before
566 * actually changing anything. It's put after the mode, if any.
567 */
568 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000569 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000570 i = showmode();
571
572 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000573 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000574
575#ifdef CURSOR_SHAPE
576 ui_cursor_shape(); /* may show different cursor shape */
577#endif
578#ifdef FEAT_DIGRAPHS
579 do_digraph(-1); /* clear digraphs */
580#endif
581
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000582 /*
583 * Get the current length of the redo buffer, those characters have to be
584 * skipped if we want to get to the inserted characters.
585 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000586 ptr = get_inserted();
587 if (ptr == NULL)
588 new_insert_skip = 0;
589 else
590 {
591 new_insert_skip = (int)STRLEN(ptr);
592 vim_free(ptr);
593 }
594
595 old_indent = 0;
596
597 /*
598 * Main loop in Insert mode: repeat until Insert mode is left.
599 */
600 for (;;)
601 {
602#ifdef FEAT_RIGHTLEFT
603 if (!revins_legal)
604 revins_scol = -1; /* reset on illegal motions */
605 else
606 revins_legal = 0;
607#endif
608 if (arrow_used) /* don't repeat insert when arrow key used */
609 count = 0;
610
611 if (stop_insert_mode)
612 {
613 /* ":stopinsert" used or 'insertmode' reset */
614 count = 0;
615 goto doESCkey;
616 }
617
618 /* set curwin->w_curswant for next K_DOWN or K_UP */
619 if (!arrow_used)
620 curwin->w_set_curswant = TRUE;
621
622 /* If there is no typeahead may check for timestamps (e.g., for when a
623 * menu invoked a shell command). */
624 if (stuff_empty())
625 {
626 did_check_timestamps = FALSE;
627 if (need_check_timestamps)
628 check_timestamps(FALSE);
629 }
630
631 /*
632 * When emsg() was called msg_scroll will have been set.
633 */
634 msg_scroll = FALSE;
635
636#ifdef FEAT_GUI
637 /* When 'mousefocus' is set a mouse movement may have taken us to
638 * another window. "need_mouse_correct" may then be set because of an
639 * autocommand. */
640 if (need_mouse_correct)
641 gui_mouse_correct();
642#endif
643
644#ifdef FEAT_FOLDING
645 /* Open fold at the cursor line, according to 'foldopen'. */
646 if (fdo_flags & FDO_INSERT)
647 foldOpenCursor();
648 /* Close folds where the cursor isn't, according to 'foldclose' */
649 if (!char_avail())
650 foldCheckClose();
651#endif
652
653 /*
654 * If we inserted a character at the last position of the last line in
655 * the window, scroll the window one line up. This avoids an extra
656 * redraw.
657 * This is detected when the cursor column is smaller after inserting
658 * something.
659 * Don't do this when the topline changed already, it has
660 * already been adjusted (by insertchar() calling open_line())).
661 */
662 if (curbuf->b_mod_set
663 && curwin->w_p_wrap
664 && !did_backspace
665 && curwin->w_topline == old_topline
666#ifdef FEAT_DIFF
667 && curwin->w_topfill == old_topfill
668#endif
669 )
670 {
671 mincol = curwin->w_wcol;
672 validate_cursor_col();
673
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000674 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000675 && curwin->w_wrow == W_WINROW(curwin)
676 + curwin->w_height - 1 - p_so
677 && (curwin->w_cursor.lnum != curwin->w_topline
678#ifdef FEAT_DIFF
679 || curwin->w_topfill > 0
680#endif
681 ))
682 {
683#ifdef FEAT_DIFF
684 if (curwin->w_topfill > 0)
685 --curwin->w_topfill;
686 else
687#endif
688#ifdef FEAT_FOLDING
689 if (hasFolding(curwin->w_topline, NULL, &old_topline))
690 set_topline(curwin, old_topline + 1);
691 else
692#endif
693 set_topline(curwin, curwin->w_topline + 1);
694 }
695 }
696
697 /* May need to adjust w_topline to show the cursor. */
698 update_topline();
699
700 did_backspace = FALSE;
701
702 validate_cursor(); /* may set must_redraw */
703
704 /*
705 * Redraw the display when no characters are waiting.
706 * Also shows mode, ruler and positions cursor.
707 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000708 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709
710#ifdef FEAT_SCROLLBIND
711 if (curwin->w_p_scb)
712 do_check_scrollbind(TRUE);
713#endif
714
Bram Moolenaar860cae12010-06-05 23:22:07 +0200715#ifdef FEAT_CURSORBIND
716 if (curwin->w_p_crb)
717 do_check_cursorbind();
718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719 update_curswant();
720 old_topline = curwin->w_topline;
721#ifdef FEAT_DIFF
722 old_topfill = curwin->w_topfill;
723#endif
724
725#ifdef USE_ON_FLY_SCROLL
726 dont_scroll = FALSE; /* allow scrolling here */
727#endif
728
729 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000730 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 */
732 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000733 do
734 {
735 c = safe_vgetc();
736 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000738#ifdef FEAT_AUTOCMD
739 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
740 did_cursorhold = TRUE;
741#endif
742
Bram Moolenaar071d4272004-06-13 20:20:40 +0000743#ifdef FEAT_RIGHTLEFT
744 if (p_hkmap && KeyTyped)
745 c = hkmap(c); /* Hebrew mode mapping */
746#endif
747#ifdef FEAT_FKMAP
748 if (p_fkmap && KeyTyped)
749 c = fkmap(c); /* Farsi mode mapping */
750#endif
751
752#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000753 /*
754 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000755 * and the cursor is still in the completed word. Only when there is
756 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000757 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000758 if (compl_started
759 && pum_wanted()
760 && curwin->w_cursor.col >= compl_col
761 && (compl_shown_match == NULL
762 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000763 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000764 /* BS: Delete one character from "compl_leader". */
765 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000766 && curwin->w_cursor.col > compl_col
767 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000768 continue;
769
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000770 /* When no match was selected or it was edited. */
771 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000772 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000773 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000774 * "compl_leader". Except when at the original match and
775 * there is nothing to add, CTRL-L works like CTRL-P then. */
776 if (c == Ctrl_L
777 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000778 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000779 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000780 {
781 ins_compl_addfrommatch();
782 continue;
783 }
784
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000785 /* A non-white character that fits in with the current
786 * completion: Add to "compl_leader". */
787 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000788 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100789#ifdef FEAT_AUTOCMD
790 /* Trigger InsertCharPre. */
791 char_u *str = do_insert_char_pre(c);
792 char_u *p;
793
794 if (str != NULL)
795 {
796 for (p = str; *p != NUL; mb_ptr_adv(p))
797 ins_compl_addleader(PTR2CHAR(p));
798 vim_free(str);
799 }
800 else
801#endif
802 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000803 continue;
804 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000805
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000806 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000807 * compl_enter_selects is set the Enter key does the same. */
808 if (c == Ctrl_Y || (compl_enter_selects
809 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000810 {
811 ins_compl_delete();
812 ins_compl_insert();
813 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000814 }
815 }
816
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
818 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000819 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000820 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000821 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000822#endif
823
Bram Moolenaar488c6512005-08-11 20:09:58 +0000824 /* CTRL-\ CTRL-N goes to Normal mode,
825 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
826 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000827 if (c == Ctrl_BSL)
828 {
829 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000830 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 ++no_mapping;
832 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000833 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000834 --no_mapping;
835 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000836 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000837 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000838 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 vungetc(c);
840 c = Ctrl_BSL;
841 }
842 else if (c == Ctrl_G && p_im)
843 continue;
844 else
845 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000846 if (c == Ctrl_O)
847 {
848 ins_ctrl_o();
849 ins_at_eol = FALSE; /* cursor keeps its column */
850 nomove = TRUE;
851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000852 count = 0;
853 goto doESCkey;
854 }
855 }
856
857#ifdef FEAT_DIGRAPHS
858 c = do_digraph(c);
859#endif
860
861#ifdef FEAT_INS_EXPAND
862 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
863 goto docomplete;
864#endif
865 if (c == Ctrl_V || c == Ctrl_Q)
866 {
867 ins_ctrl_v();
868 c = Ctrl_V; /* pretend CTRL-V is last typed character */
869 continue;
870 }
871
872#ifdef FEAT_CINDENT
873 if (cindent_on()
874# ifdef FEAT_INS_EXPAND
875 && ctrl_x_mode == 0
876# endif
877 )
878 {
879 /* A key name preceded by a bang means this key is not to be
880 * inserted. Skip ahead to the re-indenting below.
881 * A key name preceded by a star means that indenting has to be
882 * done before inserting the key. */
883 line_is_white = inindent(0);
884 if (in_cinkeys(c, '!', line_is_white))
885 goto force_cindent;
886 if (can_cindent && in_cinkeys(c, '*', line_is_white)
887 && stop_arrow() == OK)
888 do_c_expr_indent();
889 }
890#endif
891
892#ifdef FEAT_RIGHTLEFT
893 if (curwin->w_p_rl)
894 switch (c)
895 {
896 case K_LEFT: c = K_RIGHT; break;
897 case K_S_LEFT: c = K_S_RIGHT; break;
898 case K_C_LEFT: c = K_C_RIGHT; break;
899 case K_RIGHT: c = K_LEFT; break;
900 case K_S_RIGHT: c = K_S_LEFT; break;
901 case K_C_RIGHT: c = K_C_LEFT; break;
902 }
903#endif
904
905#ifdef FEAT_VISUAL
906 /*
907 * If 'keymodel' contains "startsel", may start selection. If it
908 * does, a CTRL-O and c will be stuffed, we need to get these
909 * characters.
910 */
911 if (ins_start_select(c))
912 continue;
913#endif
914
915 /*
916 * The big switch to handle a character in insert mode.
917 */
918 switch (c)
919 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000920 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000921 if (echeck_abbr(ESC + ABBR_OFF))
922 break;
923 /*FALLTHROUGH*/
924
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000925 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000926#ifdef FEAT_CMDWIN
927 if (c == Ctrl_C && cmdwin_type != 0)
928 {
929 /* Close the cmdline window. */
930 cmdwin_result = K_IGNORE;
931 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000932 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 goto doESCkey;
934 }
935#endif
936
937#ifdef UNIX
938do_intr:
939#endif
940 /* when 'insertmode' set, and not halfway a mapping, don't leave
941 * Insert mode */
942 if (goto_im())
943 {
944 if (got_int)
945 {
946 (void)vgetc(); /* flush all buffers */
947 got_int = FALSE;
948 }
949 else
950 vim_beep();
951 break;
952 }
953doESCkey:
954 /*
955 * This is the ONLY return from edit()!
956 */
957 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
958 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000959 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000960 o_lnum = curwin->w_cursor.lnum;
961
Bram Moolenaar488c6512005-08-11 20:09:58 +0000962 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000963 {
964#ifdef FEAT_AUTOCMD
965 if (cmdchar != 'r' && cmdchar != 'v')
966 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
967 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000968 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000969#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000970 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000972 continue;
973
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000974 case Ctrl_Z: /* suspend when 'insertmode' set */
975 if (!p_im)
976 goto normalchar; /* insert CTRL-Z as normal char */
977 stuffReadbuff((char_u *)":st\r");
978 c = Ctrl_O;
979 /*FALLTHROUGH*/
980
981 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000982#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000983 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000984 goto docomplete;
985#endif
986 if (echeck_abbr(Ctrl_O + ABBR_OFF))
987 break;
988 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000989
990#ifdef FEAT_VIRTUALEDIT
991 /* don't move the cursor left when 'virtualedit' has "onemore". */
992 if (ve_flags & VE_ONEMORE)
993 {
994 ins_at_eol = FALSE;
995 nomove = TRUE;
996 }
997#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000998 count = 0;
999 goto doESCkey;
1000
Bram Moolenaar572cb562005-08-05 21:35:02 +00001001 case K_INS: /* toggle insert/replace mode */
1002 case K_KINS:
1003 ins_insert(replaceState);
1004 break;
1005
1006 case K_SELECT: /* end of Select mode mapping - ignore */
1007 break;
1008
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001009#ifdef FEAT_SNIFF
1010 case K_SNIFF: /* Sniff command received */
1011 stuffcharReadbuff(K_SNIFF);
1012 goto doESCkey;
1013#endif
1014
1015 case K_HELP: /* Help key works like <ESC> <Help> */
1016 case K_F1:
1017 case K_XF1:
1018 stuffcharReadbuff(K_HELP);
1019 if (p_im)
1020 need_start_insertmode = TRUE;
1021 goto doESCkey;
1022
1023#ifdef FEAT_NETBEANS_INTG
1024 case K_F21: /* NetBeans command */
1025 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001026 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001027 --no_mapping;
1028 netbeans_keycommand(i);
1029 break;
1030#endif
1031
1032 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033 case NUL:
1034 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001035 /* For ^@ the trailing ESC will end the insert, unless there is an
1036 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1038 && c != Ctrl_A && !p_im)
1039 goto doESCkey; /* quit insert mode */
1040 inserted_space = FALSE;
1041 break;
1042
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001043 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 ins_reg();
1045 auto_format(FALSE, TRUE);
1046 inserted_space = FALSE;
1047 break;
1048
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050 ins_ctrl_g();
1051 break;
1052
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001053 case Ctrl_HAT: /* switch input mode and/or langmap */
1054 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055 break;
1056
1057#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001058 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 if (!p_ari)
1060 goto normalchar;
1061 ins_ctrl_();
1062 break;
1063#endif
1064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1067 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1068 goto docomplete;
1069#endif
1070 /* FALLTHROUGH */
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073# ifdef FEAT_INS_EXPAND
1074 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1075 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001076 if (has_compl_option(FALSE))
1077 goto docomplete;
1078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 }
1080# endif
1081 ins_shift(c, lastc);
1082 auto_format(FALSE, TRUE);
1083 inserted_space = FALSE;
1084 break;
1085
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 case K_KDEL:
1088 ins_del();
1089 auto_format(FALSE, TRUE);
1090 break;
1091
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001092 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 case Ctrl_H:
1094 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1095 auto_format(FALSE, TRUE);
1096 break;
1097
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001098 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1100 auto_format(FALSE, TRUE);
1101 break;
1102
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001103 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001104# ifdef FEAT_COMPL_FUNC
1105 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001106 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001107 goto docomplete;
1108# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1110 auto_format(FALSE, TRUE);
1111 inserted_space = FALSE;
1112 break;
1113
1114#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 case K_LEFTMOUSE_NM:
1117 case K_LEFTDRAG:
1118 case K_LEFTRELEASE:
1119 case K_LEFTRELEASE_NM:
1120 case K_MIDDLEMOUSE:
1121 case K_MIDDLEDRAG:
1122 case K_MIDDLERELEASE:
1123 case K_RIGHTMOUSE:
1124 case K_RIGHTDRAG:
1125 case K_RIGHTRELEASE:
1126 case K_X1MOUSE:
1127 case K_X1DRAG:
1128 case K_X1RELEASE:
1129 case K_X2MOUSE:
1130 case K_X2DRAG:
1131 case K_X2RELEASE:
1132 ins_mouse(c);
1133 break;
1134
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001135 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001136 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137 break;
1138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001139 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001140 ins_mousescroll(MSCR_UP);
1141 break;
1142
1143 case K_MOUSELEFT: /* Scroll wheel left */
1144 ins_mousescroll(MSCR_LEFT);
1145 break;
1146
1147 case K_MOUSERIGHT: /* Scroll wheel right */
1148 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 break;
1150#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001151#ifdef FEAT_GUI_TABLINE
1152 case K_TABLINE:
1153 case K_TABMENU:
1154 ins_tabline(c);
1155 break;
1156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001159 break;
1160
Bram Moolenaar754b5602006-02-09 23:53:20 +00001161#ifdef FEAT_AUTOCMD
1162 case K_CURSORHOLD: /* Didn't type something for a while. */
1163 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1164 did_cursorhold = TRUE;
1165 break;
1166#endif
1167
Bram Moolenaar4770d092006-01-12 23:22:24 +00001168#ifdef FEAT_GUI_W32
1169 /* On Win32 ignore <M-F4>, we get it when closing the window was
1170 * cancelled. */
1171 case K_F4:
1172 if (mod_mask != MOD_MASK_ALT)
1173 goto normalchar;
1174 break;
1175#endif
1176
Bram Moolenaar071d4272004-06-13 20:20:40 +00001177#ifdef FEAT_GUI
1178 case K_VER_SCROLLBAR:
1179 ins_scroll();
1180 break;
1181
1182 case K_HOR_SCROLLBAR:
1183 ins_horscroll();
1184 break;
1185#endif
1186
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001187 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 case K_S_HOME:
1190 case K_C_HOME:
1191 ins_home(c);
1192 break;
1193
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001194 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 case K_S_END:
1197 case K_C_END:
1198 ins_end(c);
1199 break;
1200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001202 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1203 ins_s_left();
1204 else
1205 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001206 break;
1207
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001208 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001209 case K_C_LEFT:
1210 ins_s_left();
1211 break;
1212
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001213 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001214 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1215 ins_s_right();
1216 else
1217 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001218 break;
1219
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001220 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 case K_C_RIGHT:
1222 ins_s_right();
1223 break;
1224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001226#ifdef FEAT_INS_EXPAND
1227 if (pum_visible())
1228 goto docomplete;
1229#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001230 if (mod_mask & MOD_MASK_SHIFT)
1231 ins_pageup();
1232 else
1233 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 break;
1235
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001236 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 case K_PAGEUP:
1238 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001239#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001240 if (pum_visible())
1241 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 ins_pageup();
1244 break;
1245
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001246 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001247#ifdef FEAT_INS_EXPAND
1248 if (pum_visible())
1249 goto docomplete;
1250#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001251 if (mod_mask & MOD_MASK_SHIFT)
1252 ins_pagedown();
1253 else
1254 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255 break;
1256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001257 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258 case K_PAGEDOWN:
1259 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001260#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001261 if (pum_visible())
1262 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001263#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001264 ins_pagedown();
1265 break;
1266
1267#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001268 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 ins_drop();
1270 break;
1271#endif
1272
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001273 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274 c = TAB;
1275 /* FALLTHROUGH */
1276
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1279 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1280 goto docomplete;
1281#endif
1282 inserted_space = FALSE;
1283 if (ins_tab())
1284 goto normalchar; /* insert TAB as a normal char */
1285 auto_format(FALSE, TRUE);
1286 break;
1287
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001288 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001289 c = CAR;
1290 /* FALLTHROUGH */
1291 case CAR:
1292 case NL:
1293#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1294 /* In a quickfix window a <CR> jumps to the error under the
1295 * cursor. */
1296 if (bt_quickfix(curbuf) && c == CAR)
1297 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001298 if (curwin->w_llist_ref == NULL) /* quickfix window */
1299 do_cmdline_cmd((char_u *)".cc");
1300 else /* location list window */
1301 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302 break;
1303 }
1304#endif
1305#ifdef FEAT_CMDWIN
1306 if (cmdwin_type != 0)
1307 {
1308 /* Execute the command in the cmdline window. */
1309 cmdwin_result = CAR;
1310 goto doESCkey;
1311 }
1312#endif
1313 if (ins_eol(c) && !p_im)
1314 goto doESCkey; /* out of memory */
1315 auto_format(FALSE, FALSE);
1316 inserted_space = FALSE;
1317 break;
1318
Bram Moolenaar860cae12010-06-05 23:22:07 +02001319#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001320 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321# ifdef FEAT_INS_EXPAND
1322 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1323 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001324 if (has_compl_option(TRUE))
1325 goto docomplete;
1326 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 }
1328# endif
1329# ifdef FEAT_DIGRAPHS
1330 c = ins_digraph();
1331 if (c == NUL)
1332 break;
1333# endif
1334 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336
1337#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001338 case Ctrl_X: /* Enter CTRL-X mode */
1339 ins_ctrl_x();
1340 break;
1341
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001342 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343 if (ctrl_x_mode != CTRL_X_TAGS)
1344 goto normalchar;
1345 goto docomplete;
1346
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001347 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 if (ctrl_x_mode != CTRL_X_FILES)
1349 goto normalchar;
1350 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001351
1352 case 's': /* Spelling completion after ^X */
1353 case Ctrl_S:
1354 if (ctrl_x_mode != CTRL_X_SPELL)
1355 goto normalchar;
1356 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357#endif
1358
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001359 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360#ifdef FEAT_INS_EXPAND
1361 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1362#endif
1363 {
1364 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1365 if (p_im)
1366 {
1367 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1368 break;
1369 goto doESCkey;
1370 }
1371 goto normalchar;
1372 }
1373#ifdef FEAT_INS_EXPAND
1374 /* FALLTHROUGH */
1375
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001376 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 case Ctrl_N:
1378 /* if 'complete' is empty then plain ^P is no longer special,
1379 * but it is under other ^X modes */
1380 if (*curbuf->b_p_cpt == NUL
1381 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 goto normalchar;
1384
1385docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001386 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001387 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001388 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001389 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 break;
1391#endif /* FEAT_INS_EXPAND */
1392
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001393 case Ctrl_Y: /* copy from previous line or scroll down */
1394 case Ctrl_E: /* copy from next line or scroll up */
1395 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001396 break;
1397
1398 default:
1399#ifdef UNIX
1400 if (c == intr_char) /* special interrupt char */
1401 goto do_intr;
1402#endif
1403
Bram Moolenaare659c952011-05-19 17:25:41 +02001404normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001405 /*
1406 * Insert a nomal character.
1407 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001408#ifdef FEAT_AUTOCMD
1409 if (!p_paste)
1410 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001411 /* Trigger InsertCharPre. */
1412 char_u *str = do_insert_char_pre(c);
1413 char_u *p;
1414
1415 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001416 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001417 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001418 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001419 /* Insert the new value of v:char literally. */
1420 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001421 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001422 c = PTR2CHAR(p);
1423 if (c == CAR || c == K_KENTER || c == NL)
1424 ins_eol(c);
1425 else
1426 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001427 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001428 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001429 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001430 vim_free(str);
1431 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001432 }
1433
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001434 /* If the new value is already inserted or an empty string
1435 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001436 if (c == NUL)
1437 break;
1438 }
1439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440#ifdef FEAT_SMARTINDENT
1441 /* Try to perform smart-indenting. */
1442 ins_try_si(c);
1443#endif
1444
1445 if (c == ' ')
1446 {
1447 inserted_space = TRUE;
1448#ifdef FEAT_CINDENT
1449 if (inindent(0))
1450 can_cindent = FALSE;
1451#endif
1452 if (Insstart_blank_vcol == MAXCOL
1453 && curwin->w_cursor.lnum == Insstart.lnum)
1454 Insstart_blank_vcol = get_nolist_virtcol();
1455 }
1456
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001457 /* Insert a normal character and check for abbreviations on a
1458 * special character. Let CTRL-] expand abbreviations without
1459 * inserting it. */
1460 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461#ifdef FEAT_MBYTE
1462 /* Add ABBR_OFF for characters above 0x100, this is
1463 * what check_abbr() expects. */
1464 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1465#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02001466 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467 {
1468 insert_special(c, FALSE, FALSE);
1469#ifdef FEAT_RIGHTLEFT
1470 revins_legal++;
1471 revins_chars++;
1472#endif
1473 }
1474
1475 auto_format(FALSE, TRUE);
1476
1477#ifdef FEAT_FOLDING
1478 /* When inserting a character the cursor line must never be in a
1479 * closed fold. */
1480 foldOpenCursor();
1481#endif
1482 break;
1483 } /* end of switch (c) */
1484
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001485#ifdef FEAT_AUTOCMD
1486 /* If typed something may trigger CursorHoldI again. */
1487 if (c != K_CURSORHOLD)
1488 did_cursorhold = FALSE;
1489#endif
1490
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491 /* If the cursor was moved we didn't just insert a space */
1492 if (arrow_used)
1493 inserted_space = FALSE;
1494
1495#ifdef FEAT_CINDENT
1496 if (can_cindent && cindent_on()
1497# ifdef FEAT_INS_EXPAND
1498 && ctrl_x_mode == 0
1499# endif
1500 )
1501 {
1502force_cindent:
1503 /*
1504 * Indent now if a key was typed that is in 'cinkeys'.
1505 */
1506 if (in_cinkeys(c, ' ', line_is_white))
1507 {
1508 if (stop_arrow() == OK)
1509 /* re-indent the current line */
1510 do_c_expr_indent();
1511 }
1512 }
1513#endif /* FEAT_CINDENT */
1514
1515 } /* for (;;) */
1516 /* NOTREACHED */
1517}
1518
1519/*
1520 * Redraw for Insert mode.
1521 * This is postponed until getting the next character to make '$' in the 'cpo'
1522 * option work correctly.
1523 * Only redraw when there are no characters available. This speeds up
1524 * inserting sequences of characters (e.g., for CTRL-R).
1525 */
1526 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001527ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001528 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001529{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001530#ifdef FEAT_CONCEAL
1531 linenr_T conceal_old_cursor_line = 0;
1532 linenr_T conceal_new_cursor_line = 0;
1533 int conceal_update_lines = FALSE;
1534#endif
1535
Bram Moolenaar071d4272004-06-13 20:20:40 +00001536 if (!char_avail())
1537 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001538#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001539 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1540 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001541 if (ready && (
1542# ifdef FEAT_AUTOCMD
1543 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001544# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001545# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1546 ||
1547# endif
1548# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001549 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001550# endif
1551 )
1552 && !equalpos(last_cursormoved, curwin->w_cursor)
1553# ifdef FEAT_INS_EXPAND
1554 && !pum_visible()
1555# endif
1556 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001557 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001558# ifdef FEAT_SYN_HL
1559 /* Need to update the screen first, to make sure syntax
1560 * highlighting is correct after making a change (e.g., inserting
1561 * a "(". The autocommand may also require a redraw, so it's done
1562 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001563 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001564 update_screen(0);
1565# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001566# ifdef FEAT_AUTOCMD
1567 if (has_cursormovedI())
1568 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1569# endif
1570# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001571 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001572 {
1573 conceal_old_cursor_line = last_cursormoved.lnum;
1574 conceal_new_cursor_line = curwin->w_cursor.lnum;
1575 conceal_update_lines = TRUE;
1576 }
1577# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001578 last_cursormoved = curwin->w_cursor;
1579 }
1580#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001581 if (must_redraw)
1582 update_screen(0);
1583 else if (clear_cmdline || redraw_cmdline)
1584 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001585# if defined(FEAT_CONCEAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001586 if ((conceal_update_lines
1587 && (conceal_old_cursor_line != conceal_new_cursor_line
1588 || conceal_cursor_line(curwin)))
1589 || need_cursor_line_redraw)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001590 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001591 if (conceal_old_cursor_line != conceal_new_cursor_line)
1592 update_single_line(curwin, conceal_old_cursor_line);
1593 update_single_line(curwin, conceal_new_cursor_line == 0
1594 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001595 curwin->w_valid &= ~VALID_CROW;
1596 }
1597# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001598 showruler(FALSE);
1599 setcursor();
1600 emsg_on_display = FALSE; /* may remove error message now */
1601 }
1602}
1603
1604/*
1605 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1606 */
1607 static void
1608ins_ctrl_v()
1609{
1610 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001611 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001612
1613 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001614 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001615
1616 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001617 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001619 did_putchar = TRUE;
1620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1622
1623#ifdef FEAT_CMDL_INFO
1624 add_to_showcmd_c(Ctrl_V);
1625#endif
1626
1627 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001628 if (did_putchar)
1629 /* when the line fits in 'columns' the '^' is at the start of the next
1630 * line and will not removed by the redraw */
1631 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632#ifdef FEAT_CMDL_INFO
1633 clear_showcmd();
1634#endif
1635 insert_special(c, FALSE, TRUE);
1636#ifdef FEAT_RIGHTLEFT
1637 revins_chars++;
1638 revins_legal++;
1639#endif
1640}
1641
1642/*
1643 * Put a character directly onto the screen. It's not stored in a buffer.
1644 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1645 */
1646static int pc_status;
1647#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1648#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1649#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1650#define PC_STATUS_SET 3 /* pc_bytes was filled */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652static int pc_attr;
1653static int pc_row;
1654static int pc_col;
1655
1656 void
1657edit_putchar(c, highlight)
1658 int c;
1659 int highlight;
1660{
1661 int attr;
1662
1663 if (ScreenLines != NULL)
1664 {
1665 update_topline(); /* just in case w_topline isn't valid */
1666 validate_cursor();
1667 if (highlight)
1668 attr = hl_attr(HLF_8);
1669 else
1670 attr = 0;
1671 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1672 pc_col = W_WINCOL(curwin);
1673#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1674 pc_status = PC_STATUS_UNSET;
1675#endif
1676#ifdef FEAT_RIGHTLEFT
1677 if (curwin->w_p_rl)
1678 {
1679 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1680# ifdef FEAT_MBYTE
1681 if (has_mbyte)
1682 {
1683 int fix_col = mb_fix_col(pc_col, pc_row);
1684
1685 if (fix_col != pc_col)
1686 {
1687 screen_putchar(' ', pc_row, fix_col, attr);
1688 --curwin->w_wcol;
1689 pc_status = PC_STATUS_RIGHT;
1690 }
1691 }
1692# endif
1693 }
1694 else
1695#endif
1696 {
1697 pc_col += curwin->w_wcol;
1698#ifdef FEAT_MBYTE
1699 if (mb_lefthalve(pc_row, pc_col))
1700 pc_status = PC_STATUS_LEFT;
1701#endif
1702 }
1703
1704 /* save the character to be able to put it back */
1705#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1706 if (pc_status == PC_STATUS_UNSET)
1707#endif
1708 {
1709 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1710 pc_status = PC_STATUS_SET;
1711 }
1712 screen_putchar(c, pc_row, pc_col, attr);
1713 }
1714}
1715
1716/*
1717 * Undo the previous edit_putchar().
1718 */
1719 void
1720edit_unputchar()
1721{
1722 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1723 {
1724#if defined(FEAT_MBYTE)
1725 if (pc_status == PC_STATUS_RIGHT)
1726 ++curwin->w_wcol;
1727 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1728 redrawWinline(curwin->w_cursor.lnum, FALSE);
1729 else
1730#endif
1731 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1732 }
1733}
1734
1735/*
1736 * Called when p_dollar is set: display a '$' at the end of the changed text
1737 * Only works when cursor is in the line that changes.
1738 */
1739 void
1740display_dollar(col)
1741 colnr_T col;
1742{
1743 colnr_T save_col;
1744
1745 if (!redrawing())
1746 return;
1747
1748 cursor_off();
1749 save_col = curwin->w_cursor.col;
1750 curwin->w_cursor.col = col;
1751#ifdef FEAT_MBYTE
1752 if (has_mbyte)
1753 {
1754 char_u *p;
1755
1756 /* If on the last byte of a multi-byte move to the first byte. */
1757 p = ml_get_curline();
1758 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1759 }
1760#endif
1761 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1762 if (curwin->w_wcol < W_WIDTH(curwin))
1763 {
1764 edit_putchar('$', FALSE);
1765 dollar_vcol = curwin->w_virtcol;
1766 }
1767 curwin->w_cursor.col = save_col;
1768}
1769
1770/*
1771 * Call this function before moving the cursor from the normal insert position
1772 * in insert mode.
1773 */
1774 static void
1775undisplay_dollar()
1776{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001777 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001779 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 redrawWinline(curwin->w_cursor.lnum, FALSE);
1781 }
1782}
1783
1784/*
1785 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1786 * Keep the cursor on the same character.
1787 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1788 * type == INDENT_DEC decrease indent (for CTRL-D)
1789 * type == INDENT_SET set indent to "amount"
1790 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1791 */
1792 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001793change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001794 int type;
1795 int amount;
1796 int round;
1797 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001798 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799{
1800 int vcol;
1801 int last_vcol;
1802 int insstart_less; /* reduction for Insstart.col */
1803 int new_cursor_col;
1804 int i;
1805 char_u *ptr;
1806 int save_p_list;
1807 int start_col;
1808 colnr_T vc;
1809#ifdef FEAT_VREPLACE
1810 colnr_T orig_col = 0; /* init for GCC */
1811 char_u *new_line, *orig_line = NULL; /* init for GCC */
1812
1813 /* VREPLACE mode needs to know what the line was like before changing */
1814 if (State & VREPLACE_FLAG)
1815 {
1816 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1817 orig_col = curwin->w_cursor.col;
1818 }
1819#endif
1820
1821 /* for the following tricks we don't want list mode */
1822 save_p_list = curwin->w_p_list;
1823 curwin->w_p_list = FALSE;
1824 vc = getvcol_nolist(&curwin->w_cursor);
1825 vcol = vc;
1826
1827 /*
1828 * For Replace mode we need to fix the replace stack later, which is only
1829 * possible when the cursor is in the indent. Remember the number of
1830 * characters before the cursor if it's possible.
1831 */
1832 start_col = curwin->w_cursor.col;
1833
1834 /* determine offset from first non-blank */
1835 new_cursor_col = curwin->w_cursor.col;
1836 beginline(BL_WHITE);
1837 new_cursor_col -= curwin->w_cursor.col;
1838
1839 insstart_less = curwin->w_cursor.col;
1840
1841 /*
1842 * If the cursor is in the indent, compute how many screen columns the
1843 * cursor is to the left of the first non-blank.
1844 */
1845 if (new_cursor_col < 0)
1846 vcol = get_indent() - vcol;
1847
1848 if (new_cursor_col > 0) /* can't fix replace stack */
1849 start_col = -1;
1850
1851 /*
1852 * Set the new indent. The cursor will be put on the first non-blank.
1853 */
1854 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001855 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else
1857 {
1858#ifdef FEAT_VREPLACE
1859 int save_State = State;
1860
1861 /* Avoid being called recursively. */
1862 if (State & VREPLACE_FLAG)
1863 State = INSERT;
1864#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001865 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866#ifdef FEAT_VREPLACE
1867 State = save_State;
1868#endif
1869 }
1870 insstart_less -= curwin->w_cursor.col;
1871
1872 /*
1873 * Try to put cursor on same character.
1874 * If the cursor is at or after the first non-blank in the line,
1875 * compute the cursor column relative to the column of the first
1876 * non-blank character.
1877 * If we are not in insert mode, leave the cursor on the first non-blank.
1878 * If the cursor is before the first non-blank, position it relative
1879 * to the first non-blank, counted in screen columns.
1880 */
1881 if (new_cursor_col >= 0)
1882 {
1883 /*
1884 * When changing the indent while the cursor is touching it, reset
1885 * Insstart_col to 0.
1886 */
1887 if (new_cursor_col == 0)
1888 insstart_less = MAXCOL;
1889 new_cursor_col += curwin->w_cursor.col;
1890 }
1891 else if (!(State & INSERT))
1892 new_cursor_col = curwin->w_cursor.col;
1893 else
1894 {
1895 /*
1896 * Compute the screen column where the cursor should be.
1897 */
1898 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001899 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900
1901 /*
1902 * Advance the cursor until we reach the right screen column.
1903 */
1904 vcol = last_vcol = 0;
1905 new_cursor_col = -1;
1906 ptr = ml_get_curline();
1907 while (vcol <= (int)curwin->w_virtcol)
1908 {
1909 last_vcol = vcol;
1910#ifdef FEAT_MBYTE
1911 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001912 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001913 else
1914#endif
1915 ++new_cursor_col;
1916 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1917 }
1918 vcol = last_vcol;
1919
1920 /*
1921 * May need to insert spaces to be able to position the cursor on
1922 * the right screen column.
1923 */
1924 if (vcol != (int)curwin->w_virtcol)
1925 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001926 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001928 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001929 if (ptr != NULL)
1930 {
1931 new_cursor_col += i;
1932 ptr[i] = NUL;
1933 while (--i >= 0)
1934 ptr[i] = ' ';
1935 ins_str(ptr);
1936 vim_free(ptr);
1937 }
1938 }
1939
1940 /*
1941 * When changing the indent while the cursor is in it, reset
1942 * Insstart_col to 0.
1943 */
1944 insstart_less = MAXCOL;
1945 }
1946
1947 curwin->w_p_list = save_p_list;
1948
1949 if (new_cursor_col <= 0)
1950 curwin->w_cursor.col = 0;
1951 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001952 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001953 curwin->w_set_curswant = TRUE;
1954 changed_cline_bef_curs();
1955
1956 /*
1957 * May have to adjust the start of the insert.
1958 */
1959 if (State & INSERT)
1960 {
1961 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1962 {
1963 if ((int)Insstart.col <= insstart_less)
1964 Insstart.col = 0;
1965 else
1966 Insstart.col -= insstart_less;
1967 }
1968 if ((int)ai_col <= insstart_less)
1969 ai_col = 0;
1970 else
1971 ai_col -= insstart_less;
1972 }
1973
1974 /*
1975 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1976 * If the number of characters before the cursor decreased, need to pop a
1977 * few characters from the replace stack.
1978 * If the number of characters before the cursor increased, need to push a
1979 * few NULs onto the replace stack.
1980 */
1981 if (REPLACE_NORMAL(State) && start_col >= 0)
1982 {
1983 while (start_col > (int)curwin->w_cursor.col)
1984 {
1985 replace_join(0); /* remove a NUL from the replace stack */
1986 --start_col;
1987 }
1988 while (start_col < (int)curwin->w_cursor.col || replaced)
1989 {
1990 replace_push(NUL);
1991 if (replaced)
1992 {
1993 replace_push(replaced);
1994 replaced = NUL;
1995 }
1996 ++start_col;
1997 }
1998 }
1999
2000#ifdef FEAT_VREPLACE
2001 /*
2002 * For VREPLACE mode, we also have to fix the replace stack. In this case
2003 * it is always possible because we backspace over the whole line and then
2004 * put it back again the way we wanted it.
2005 */
2006 if (State & VREPLACE_FLAG)
2007 {
2008 /* If orig_line didn't allocate, just return. At least we did the job,
2009 * even if you can't backspace. */
2010 if (orig_line == NULL)
2011 return;
2012
2013 /* Save new line */
2014 new_line = vim_strsave(ml_get_curline());
2015 if (new_line == NULL)
2016 return;
2017
2018 /* We only put back the new line up to the cursor */
2019 new_line[curwin->w_cursor.col] = NUL;
2020
2021 /* Put back original line */
2022 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2023 curwin->w_cursor.col = orig_col;
2024
2025 /* Backspace from cursor to start of line */
2026 backspace_until_column(0);
2027
2028 /* Insert new stuff into line again */
2029 ins_bytes(new_line);
2030
2031 vim_free(new_line);
2032 }
2033#endif
2034}
2035
2036/*
2037 * Truncate the space at the end of a line. This is to be used only in an
2038 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2039 * modes.
2040 */
2041 void
2042truncate_spaces(line)
2043 char_u *line;
2044{
2045 int i;
2046
2047 /* find start of trailing white space */
2048 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2049 {
2050 if (State & REPLACE_FLAG)
2051 replace_join(0); /* remove a NUL from the replace stack */
2052 }
2053 line[i + 1] = NUL;
2054}
2055
2056#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2057 || defined(FEAT_COMMENTS) || defined(PROTO)
2058/*
2059 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2060 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002061 * Will attempt not to go before "col" even when there is a composing
2062 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002063 */
2064 void
2065backspace_until_column(col)
2066 int col;
2067{
2068 while ((int)curwin->w_cursor.col > col)
2069 {
2070 curwin->w_cursor.col--;
2071 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002072 replace_do_bs(col);
2073 else if (!del_char_after_col(col))
2074 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 }
2076}
2077#endif
2078
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002079/*
2080 * Like del_char(), but make sure not to go before column "limit_col".
2081 * Only matters when there are composing characters.
2082 * Return TRUE when something was deleted.
2083 */
2084 static int
2085del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002086 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002087{
2088#ifdef FEAT_MBYTE
2089 if (enc_utf8 && limit_col >= 0)
2090 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002091 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002092
2093 /* Make sure the cursor is at the start of a character, but
2094 * skip forward again when going too far back because of a
2095 * composing character. */
2096 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002097 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002098 {
2099 int l = utf_ptr2len(ml_get_cursor());
2100
2101 if (l == 0) /* end of line */
2102 break;
2103 curwin->w_cursor.col += l;
2104 }
2105 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2106 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002107 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002108 }
2109 else
2110#endif
2111 (void)del_char(FALSE);
2112 return TRUE;
2113}
2114
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2116/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002117 * CTRL-X pressed in Insert mode.
2118 */
2119 static void
2120ins_ctrl_x()
2121{
2122 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2123 * CTRL-V works like CTRL-N */
2124 if (ctrl_x_mode != CTRL_X_CMDLINE)
2125 {
2126 /* if the next ^X<> won't ADD nothing, then reset
2127 * compl_cont_status */
2128 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002129 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002130 else
2131 compl_cont_status = 0;
2132 /* We're not sure which CTRL-X mode it will be yet */
2133 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2134 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2135 edit_submode_pre = NULL;
2136 showmode();
2137 }
2138}
2139
2140/*
2141 * Return TRUE if the 'dict' or 'tsr' option can be used.
2142 */
2143 static int
2144has_compl_option(dict_opt)
2145 int dict_opt;
2146{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002147 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002148# ifdef FEAT_SPELL
2149 && !curwin->w_p_spell
2150# endif
2151 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002152 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2153 {
2154 ctrl_x_mode = 0;
2155 edit_submode = NULL;
2156 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2157 : (char_u *)_("'thesaurus' option is empty"),
2158 hl_attr(HLF_E));
2159 if (emsg_silent == 0)
2160 {
2161 vim_beep();
2162 setcursor();
2163 out_flush();
2164 ui_delay(2000L, FALSE);
2165 }
2166 return FALSE;
2167 }
2168 return TRUE;
2169}
2170
2171/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002172 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2173 * This depends on the current mode.
2174 */
2175 int
2176vim_is_ctrl_x_key(c)
2177 int c;
2178{
2179 /* Always allow ^R - let it's results then be checked */
2180 if (c == Ctrl_R)
2181 return TRUE;
2182
Bram Moolenaare3226be2005-12-18 22:10:00 +00002183 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002184 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002185 return TRUE;
2186
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 switch (ctrl_x_mode)
2188 {
2189 case 0: /* Not in any CTRL-X mode */
2190 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2191 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002192 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2194 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2195 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002196 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002197 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 case CTRL_X_SCROLL:
2199 return (c == Ctrl_Y || c == Ctrl_E);
2200 case CTRL_X_WHOLE_LINE:
2201 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2202 case CTRL_X_FILES:
2203 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2204 case CTRL_X_DICTIONARY:
2205 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2206 case CTRL_X_THESAURUS:
2207 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2208 case CTRL_X_TAGS:
2209 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2210#ifdef FEAT_FIND_ID
2211 case CTRL_X_PATH_PATTERNS:
2212 return (c == Ctrl_P || c == Ctrl_N);
2213 case CTRL_X_PATH_DEFINES:
2214 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2215#endif
2216 case CTRL_X_CMDLINE:
2217 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2218 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002219#ifdef FEAT_COMPL_FUNC
2220 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002221 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002222 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002223 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002224#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002225 case CTRL_X_SPELL:
2226 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002227 }
2228 EMSG(_(e_internal));
2229 return FALSE;
2230}
2231
2232/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002233 * Return TRUE when character "c" is part of the item currently being
2234 * completed. Used to decide whether to abandon complete mode when the menu
2235 * is visible.
2236 */
2237 static int
2238ins_compl_accept_char(c)
2239 int c;
2240{
2241 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2242 /* When expanding an identifier only accept identifier chars. */
2243 return vim_isIDc(c);
2244
2245 switch (ctrl_x_mode)
2246 {
2247 case CTRL_X_FILES:
2248 /* When expanding file name only accept file name chars. But not
2249 * path separators, so that "proto/<Tab>" expands files in
2250 * "proto", not "proto/" as a whole */
2251 return vim_isfilec(c) && !vim_ispathsep(c);
2252
2253 case CTRL_X_CMDLINE:
2254 case CTRL_X_OMNI:
2255 /* Command line and Omni completion can work with just about any
2256 * printable character, but do stop at white space. */
2257 return vim_isprintc(c) && !vim_iswhite(c);
2258
2259 case CTRL_X_WHOLE_LINE:
2260 /* For while line completion a space can be part of the line. */
2261 return vim_isprintc(c);
2262 }
2263 return vim_iswordc(c);
2264}
2265
2266/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002267 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002269 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002270 * the rest of the word to be in -- webb
2271 */
2272 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002273ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 char_u *str;
2275 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002276 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002277 char_u *fname;
2278 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002279 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002280{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002281 char_u *p;
2282 int i, c;
2283 int actual_len; /* Take multi-byte characters */
2284 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002285 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002286 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002287 int has_lower = FALSE;
2288 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002289
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002290 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002292 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
Bram Moolenaara2993e12007-08-12 14:38:46 +00002294 /* Find actual length of completion. */
2295#ifdef FEAT_MBYTE
2296 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002298 p = str;
2299 actual_len = 0;
2300 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002302 mb_ptr_adv(p);
2303 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304 }
2305 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002306 else
2307#endif
2308 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309
Bram Moolenaara2993e12007-08-12 14:38:46 +00002310 /* Find actual length of original text. */
2311#ifdef FEAT_MBYTE
2312 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002314 p = compl_orig_text;
2315 actual_compl_length = 0;
2316 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002318 mb_ptr_adv(p);
2319 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002320 }
2321 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002322 else
2323#endif
2324 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002326 /* "actual_len" may be smaller than "actual_compl_length" when using
2327 * thesaurus, only use the minimum when comparing. */
2328 min_len = actual_len < actual_compl_length
2329 ? actual_len : actual_compl_length;
2330
Bram Moolenaara2993e12007-08-12 14:38:46 +00002331 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002332 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002333 if (wca != NULL)
2334 {
2335 p = str;
2336 for (i = 0; i < actual_len; ++i)
2337#ifdef FEAT_MBYTE
2338 if (has_mbyte)
2339 wca[i] = mb_ptr2char_adv(&p);
2340 else
2341#endif
2342 wca[i] = *(p++);
2343
2344 /* Rule 1: Were any chars converted to lower? */
2345 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002346 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002347 {
2348#ifdef FEAT_MBYTE
2349 if (has_mbyte)
2350 c = mb_ptr2char_adv(&p);
2351 else
2352#endif
2353 c = *(p++);
2354 if (MB_ISLOWER(c))
2355 {
2356 has_lower = TRUE;
2357 if (MB_ISUPPER(wca[i]))
2358 {
2359 /* Rule 1 is satisfied. */
2360 for (i = actual_compl_length; i < actual_len; ++i)
2361 wca[i] = MB_TOLOWER(wca[i]);
2362 break;
2363 }
2364 }
2365 }
2366
2367 /*
2368 * Rule 2: No lower case, 2nd consecutive letter converted to
2369 * upper case.
2370 */
2371 if (!has_lower)
2372 {
2373 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002374 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002375 {
2376#ifdef FEAT_MBYTE
2377 if (has_mbyte)
2378 c = mb_ptr2char_adv(&p);
2379 else
2380#endif
2381 c = *(p++);
2382 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2383 {
2384 /* Rule 2 is satisfied. */
2385 for (i = actual_compl_length; i < actual_len; ++i)
2386 wca[i] = MB_TOUPPER(wca[i]);
2387 break;
2388 }
2389 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2390 }
2391 }
2392
2393 /* Copy the original case of the part we typed. */
2394 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002395 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002396 {
2397#ifdef FEAT_MBYTE
2398 if (has_mbyte)
2399 c = mb_ptr2char_adv(&p);
2400 else
2401#endif
2402 c = *(p++);
2403 if (MB_ISLOWER(c))
2404 wca[i] = MB_TOLOWER(wca[i]);
2405 else if (MB_ISUPPER(c))
2406 wca[i] = MB_TOUPPER(wca[i]);
2407 }
2408
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002409 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002410 * Generate encoding specific output from wide character array.
2411 * Multi-byte characters can occupy up to five bytes more than
2412 * ASCII characters, and we also need one byte for NUL, so stay
2413 * six bytes away from the edge of IObuff.
2414 */
2415 p = IObuff;
2416 i = 0;
2417 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2418#ifdef FEAT_MBYTE
2419 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002420 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002421 else
2422#endif
2423 *(p++) = wca[i++];
2424 *p = NUL;
2425
2426 vim_free(wca);
2427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002429 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2430 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002431 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002432 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433}
2434
2435/*
2436 * Add a match to the list of matches.
2437 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002438 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002439 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002441 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002442ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002443 char_u *str;
2444 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002445 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002447 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002448 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002449 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002450 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002452 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002453 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454
2455 ui_breakcheck();
2456 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002457 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458 if (len < 0)
2459 len = (int)STRLEN(str);
2460
2461 /*
2462 * If the same match is already present, don't add it.
2463 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002464 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002466 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 do
2468 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002469 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002470 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002471 && match->cp_str[len] == NUL)
2472 return NOTDONE;
2473 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002474 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 }
2476
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002477 /* Remove any popup menu before changing the list of matches. */
2478 ins_compl_del_pum();
2479
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 /*
2481 * Allocate a new match structure.
2482 * Copy the values to the new match structure.
2483 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002484 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002486 return FAIL;
2487 match->cp_number = -1;
2488 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002489 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002490 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002491 {
2492 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002493 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002494 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002495 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002496
Bram Moolenaar071d4272004-06-13 20:20:40 +00002497 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002498 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2500 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002501 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002502 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002503 && compl_curr_match->cp_fname != NULL
2504 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002505 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002506 else if (fname != NULL)
2507 {
2508 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002509 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002510 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002512 match->cp_fname = NULL;
2513 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002514
2515 if (cptext != NULL)
2516 {
2517 int i;
2518
2519 for (i = 0; i < CPT_COUNT; ++i)
2520 if (cptext[i] != NULL && *cptext[i] != NUL)
2521 match->cp_text[i] = vim_strsave(cptext[i]);
2522 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002523
2524 /*
2525 * Link the new match structure in the list of matches.
2526 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002527 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002528 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002529 else if (dir == FORWARD)
2530 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002531 match->cp_next = compl_curr_match->cp_next;
2532 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 }
2534 else /* BACKWARD */
2535 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002536 match->cp_next = compl_curr_match;
2537 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002539 if (match->cp_next)
2540 match->cp_next->cp_prev = match;
2541 if (match->cp_prev)
2542 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002544 compl_first_match = match;
2545 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002547 /*
2548 * Find the longest common string if still doing that.
2549 */
2550 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2551 ins_compl_longest_match(match);
2552
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 return OK;
2554}
2555
2556/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002557 * Return TRUE if "str[len]" matches with match->cp_str, considering
2558 * match->cp_icase.
2559 */
2560 static int
2561ins_compl_equal(match, str, len)
2562 compl_T *match;
2563 char_u *str;
2564 int len;
2565{
2566 if (match->cp_icase)
2567 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2568 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2569}
2570
2571/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002572 * Reduce the longest common string for match "match".
2573 */
2574 static void
2575ins_compl_longest_match(match)
2576 compl_T *match;
2577{
2578 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002579 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002580 int had_match;
2581
2582 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002583 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002584 /* First match, use it as a whole. */
2585 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002586 if (compl_leader != NULL)
2587 {
2588 had_match = (curwin->w_cursor.col > compl_col);
2589 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002590 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002591 ins_redraw(FALSE);
2592
2593 /* When the match isn't there (to avoid matching itself) remove it
2594 * again after redrawing. */
2595 if (!had_match)
2596 ins_compl_delete();
2597 compl_used_match = FALSE;
2598 }
2599 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002600 else
2601 {
2602 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002603 p = compl_leader;
2604 s = match->cp_str;
2605 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002606 {
2607#ifdef FEAT_MBYTE
2608 if (has_mbyte)
2609 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002610 c1 = mb_ptr2char(p);
2611 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002612 }
2613 else
2614#endif
2615 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002616 c1 = *p;
2617 c2 = *s;
2618 }
2619 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2620 : (c1 != c2))
2621 break;
2622#ifdef FEAT_MBYTE
2623 if (has_mbyte)
2624 {
2625 mb_ptr_adv(p);
2626 mb_ptr_adv(s);
2627 }
2628 else
2629#endif
2630 {
2631 ++p;
2632 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002633 }
2634 }
2635
2636 if (*p != NUL)
2637 {
2638 /* Leader was shortened, need to change the inserted text. */
2639 *p = NUL;
2640 had_match = (curwin->w_cursor.col > compl_col);
2641 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002642 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002643 ins_redraw(FALSE);
2644
2645 /* When the match isn't there (to avoid matching itself) remove it
2646 * again after redrawing. */
2647 if (!had_match)
2648 ins_compl_delete();
2649 }
2650
2651 compl_used_match = FALSE;
2652 }
2653}
2654
2655/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002656 * Add an array of matches to the list of matches.
2657 * Frees matches[].
2658 */
2659 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002660ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 int num_matches;
2662 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002663 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002664{
2665 int i;
2666 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002667 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668
Bram Moolenaar572cb562005-08-05 21:35:02 +00002669 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002670 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002671 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002673 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002674 FreeWild(num_matches, matches);
2675}
2676
2677/* Make the completion list cyclic.
2678 * Return the number of matches (excluding the original).
2679 */
2680 static int
2681ins_compl_make_cyclic()
2682{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002683 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002684 int count = 0;
2685
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002686 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002687 {
2688 /*
2689 * Find the end of the list.
2690 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002691 match = compl_first_match;
2692 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002693 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002694 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002695 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002696 ++count;
2697 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002698 match->cp_next = compl_first_match;
2699 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 }
2701 return count;
2702}
2703
Bram Moolenaara94bc432006-03-10 21:42:59 +00002704/*
2705 * Start completion for the complete() function.
2706 * "startcol" is where the matched text starts (1 is first column).
2707 * "list" is the list of matches.
2708 */
2709 void
2710set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002711 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002712 list_T *list;
2713{
2714 /* If already doing completions stop it. */
2715 if (ctrl_x_mode != 0)
2716 ins_compl_prep(' ');
2717 ins_compl_clear();
2718
2719 if (stop_arrow() == FAIL)
2720 return;
2721
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002722 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002723 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002724 startcol = curwin->w_cursor.col;
2725 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002726 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002727 /* compl_pattern doesn't need to be set */
2728 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2729 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002730 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002731 return;
2732
2733 /* Handle like dictionary completion. */
2734 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2735
2736 ins_compl_add_list(list);
2737 compl_matches = ins_compl_make_cyclic();
2738 compl_started = TRUE;
2739 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002740 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002741
2742 compl_curr_match = compl_first_match;
2743 ins_complete(Ctrl_N);
2744 out_flush();
2745}
2746
2747
Bram Moolenaar9372a112005-12-06 19:59:18 +00002748/* "compl_match_array" points the currently displayed list of entries in the
2749 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002750static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002751static int compl_match_arraysize;
2752
2753/*
2754 * Update the screen and when there is any scrolling remove the popup menu.
2755 */
2756 static void
2757ins_compl_upd_pum()
2758{
2759 int h;
2760
2761 if (compl_match_array != NULL)
2762 {
2763 h = curwin->w_cline_height;
2764 update_screen(0);
2765 if (h != curwin->w_cline_height)
2766 ins_compl_del_pum();
2767 }
2768}
2769
2770/*
2771 * Remove any popup menu.
2772 */
2773 static void
2774ins_compl_del_pum()
2775{
2776 if (compl_match_array != NULL)
2777 {
2778 pum_undisplay();
2779 vim_free(compl_match_array);
2780 compl_match_array = NULL;
2781 }
2782}
2783
2784/*
2785 * Return TRUE if the popup menu should be displayed.
2786 */
2787 static int
2788pum_wanted()
2789{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002790 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002791 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002792 return FALSE;
2793
2794 /* The display looks bad on a B&W display. */
2795 if (t_colors < 8
2796#ifdef FEAT_GUI
2797 && !gui.in_use
2798#endif
2799 )
2800 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002801 return TRUE;
2802}
2803
2804/*
2805 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002806 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002807 */
2808 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002809pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002810{
2811 compl_T *compl;
2812 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002813
2814 /* Don't display the popup menu if there are no matches or there is only
2815 * one (ignoring the original text). */
2816 compl = compl_first_match;
2817 i = 0;
2818 do
2819 {
2820 if (compl == NULL
2821 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2822 break;
2823 compl = compl->cp_next;
2824 } while (compl != compl_first_match);
2825
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002826 if (strstr((char *)p_cot, "menuone") != NULL)
2827 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002828 return (i >= 2);
2829}
2830
2831/*
2832 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002833 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002834 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002835 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002836ins_compl_show_pum()
2837{
2838 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002839 compl_T *shown_compl = NULL;
2840 int did_find_shown_match = FALSE;
2841 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002842 int i;
2843 int cur = -1;
2844 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002845 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002846
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002847 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002848 return;
2849
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002850#if defined(FEAT_EVAL)
2851 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2852 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2853#endif
2854
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002855 /* Update the screen before drawing the popup menu over it. */
2856 update_screen(0);
2857
2858 if (compl_match_array == NULL)
2859 {
2860 /* Need to build the popup menu list. */
2861 compl_match_arraysize = 0;
2862 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002863 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002864 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002865 do
2866 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002867 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2868 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002869 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002870 ++compl_match_arraysize;
2871 compl = compl->cp_next;
2872 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002873 if (compl_match_arraysize == 0)
2874 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002875 compl_match_array = (pumitem_T *)alloc_clear(
2876 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002877 * compl_match_arraysize));
2878 if (compl_match_array != NULL)
2879 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002880 /* If the current match is the original text don't find the first
2881 * match after it, don't highlight anything. */
2882 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2883 shown_match_ok = TRUE;
2884
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002885 i = 0;
2886 compl = compl_first_match;
2887 do
2888 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002889 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2890 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002891 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002892 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002893 if (!shown_match_ok)
2894 {
2895 if (compl == compl_shown_match || did_find_shown_match)
2896 {
2897 /* This item is the shown match or this is the
2898 * first displayed item after the shown match. */
2899 compl_shown_match = compl;
2900 did_find_shown_match = TRUE;
2901 shown_match_ok = TRUE;
2902 }
2903 else
2904 /* Remember this displayed match for when the
2905 * shown match is just below it. */
2906 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002907 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002908 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002909
2910 if (compl->cp_text[CPT_ABBR] != NULL)
2911 compl_match_array[i].pum_text =
2912 compl->cp_text[CPT_ABBR];
2913 else
2914 compl_match_array[i].pum_text = compl->cp_str;
2915 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2916 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2917 if (compl->cp_text[CPT_MENU] != NULL)
2918 compl_match_array[i++].pum_extra =
2919 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002920 else
2921 compl_match_array[i++].pum_extra = compl->cp_fname;
2922 }
2923
2924 if (compl == compl_shown_match)
2925 {
2926 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002927
2928 /* When the original text is the shown match don't set
2929 * compl_shown_match. */
2930 if (compl->cp_flags & ORIGINAL_TEXT)
2931 shown_match_ok = TRUE;
2932
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002933 if (!shown_match_ok && shown_compl != NULL)
2934 {
2935 /* The shown match isn't displayed, set it to the
2936 * previously displayed match. */
2937 compl_shown_match = shown_compl;
2938 shown_match_ok = TRUE;
2939 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002940 }
2941 compl = compl->cp_next;
2942 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002943
2944 if (!shown_match_ok) /* no displayed match at all */
2945 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002946 }
2947 }
2948 else
2949 {
2950 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002951 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002952 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2953 || compl_match_array[i].pum_text
2954 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002955 {
2956 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002957 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002958 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002959 }
2960
2961 if (compl_match_array != NULL)
2962 {
2963 /* Compute the screen column of the start of the completed text.
2964 * Use the cursor to get all wrapping and other settings right. */
2965 col = curwin->w_cursor.col;
2966 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002967 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002968 curwin->w_cursor.col = col;
2969 }
2970}
2971
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972#define DICT_FIRST (1) /* use just first element in "dict" */
2973#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002974
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002976 * Add any identifiers that match the given pattern in the list of dictionary
2977 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002978 */
2979 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002980ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2981 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002983 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002984 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002985{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002986 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 char_u *ptr;
2988 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 char_u **files;
2991 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002993 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994
Bram Moolenaar0b238792006-03-02 22:49:12 +00002995 if (*dict == NUL)
2996 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002997#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002998 /* When 'dictionary' is empty and spell checking is enabled use
2999 * "spell". */
3000 if (!thesaurus && curwin->w_p_spell)
3001 dict = (char_u *)"spell";
3002 else
3003#endif
3004 return;
3005 }
3006
Bram Moolenaar071d4272004-06-13 20:20:40 +00003007 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003008 if (buf == NULL)
3009 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003010 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 /* If 'infercase' is set, don't use 'smartcase' here */
3013 save_p_scs = p_scs;
3014 if (curbuf->b_p_inf)
3015 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003016
3017 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3018 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003019 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003020 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3021 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003022 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003023 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003024
3025 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003026 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003027 len = STRLEN(pat_esc) + 10;
3028 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003029 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003030 {
3031 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003032 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003033 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003034 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003035 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3036 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003037 vim_free(ptr);
3038 }
3039 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003040 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003041 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003042 if (regmatch.regprog == NULL)
3043 goto theend;
3044 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003045
Bram Moolenaar071d4272004-06-13 20:20:40 +00003046 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3047 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003048 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003049 {
3050 /* copy one dictionary file name into buf */
3051 if (flags == DICT_EXACT)
3052 {
3053 count = 1;
3054 files = &dict;
3055 }
3056 else
3057 {
3058 /* Expand wildcards in the dictionary name, but do not allow
3059 * backticks (for security, the 'dict' option may have been set in
3060 * a modeline). */
3061 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003062# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003063 if (!thesaurus && STRCMP(buf, "spell") == 0)
3064 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003065 else
3066# endif
3067 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003068 || expand_wildcards(1, &buf, &count, &files,
3069 EW_FILE|EW_SILENT) != OK)
3070 count = 0;
3071 }
3072
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003073# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003074 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003075 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003076 /* Complete from active spelling. Skip "\<" in the pattern, we
3077 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003078 if (pat[0] == '\\' && pat[1] == '<')
3079 ptr = pat + 2;
3080 else
3081 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003082 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003083 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003084 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003085# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003086 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003087 {
3088 ins_compl_files(count, files, thesaurus, flags,
3089 &regmatch, buf, &dir);
3090 if (flags != DICT_EXACT)
3091 FreeWild(count, files);
3092 }
3093 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003094 break;
3095 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003096
3097theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 p_scs = save_p_scs;
3099 vim_free(regmatch.regprog);
3100 vim_free(buf);
3101}
3102
Bram Moolenaar0b238792006-03-02 22:49:12 +00003103 static void
3104ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3105 int count;
3106 char_u **files;
3107 int thesaurus;
3108 int flags;
3109 regmatch_T *regmatch;
3110 char_u *buf;
3111 int *dir;
3112{
3113 char_u *ptr;
3114 int i;
3115 FILE *fp;
3116 int add_r;
3117
3118 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3119 {
3120 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3121 if (flags != DICT_EXACT)
3122 {
3123 vim_snprintf((char *)IObuff, IOSIZE,
3124 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003125 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003126 }
3127
3128 if (fp != NULL)
3129 {
3130 /*
3131 * Read dictionary file line by line.
3132 * Check each line for a match.
3133 */
3134 while (!got_int && !compl_interrupted
3135 && !vim_fgets(buf, LSIZE, fp))
3136 {
3137 ptr = buf;
3138 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3139 {
3140 ptr = regmatch->startp[0];
3141 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3142 ptr = find_line_end(ptr);
3143 else
3144 ptr = find_word_end(ptr);
3145 add_r = ins_compl_add_infercase(regmatch->startp[0],
3146 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003147 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003148 if (thesaurus)
3149 {
3150 char_u *wstart;
3151
3152 /*
3153 * Add the other matches on the line
3154 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003155 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003156 while (!got_int)
3157 {
3158 /* Find start of the next word. Skip white
3159 * space and punctuation. */
3160 ptr = find_word_start(ptr);
3161 if (*ptr == NUL || *ptr == NL)
3162 break;
3163 wstart = ptr;
3164
Bram Moolenaara2993e12007-08-12 14:38:46 +00003165 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003166#ifdef FEAT_MBYTE
3167 if (has_mbyte)
3168 /* Japanese words may have characters in
3169 * different classes, only separate words
3170 * with single-byte non-word characters. */
3171 while (*ptr != NUL)
3172 {
3173 int l = (*mb_ptr2len)(ptr);
3174
3175 if (l < 2 && !vim_iswordc(*ptr))
3176 break;
3177 ptr += l;
3178 }
3179 else
3180#endif
3181 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003182
3183 /* Add the word. Skip the regexp match. */
3184 if (wstart != regmatch->startp[0])
3185 add_r = ins_compl_add_infercase(wstart,
3186 (int)(ptr - wstart),
3187 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003188 }
3189 }
3190 if (add_r == OK)
3191 /* if dir was BACKWARD then honor it just once */
3192 *dir = FORWARD;
3193 else if (add_r == FAIL)
3194 break;
3195 /* avoid expensive call to vim_regexec() when at end
3196 * of line */
3197 if (*ptr == '\n' || got_int)
3198 break;
3199 }
3200 line_breakcheck();
3201 ins_compl_check_keys(50);
3202 }
3203 fclose(fp);
3204 }
3205 }
3206}
3207
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208/*
3209 * Find the start of the next word.
3210 * Returns a pointer to the first char of the word. Also stops at a NUL.
3211 */
3212 char_u *
3213find_word_start(ptr)
3214 char_u *ptr;
3215{
3216#ifdef FEAT_MBYTE
3217 if (has_mbyte)
3218 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003219 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 else
3221#endif
3222 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3223 ++ptr;
3224 return ptr;
3225}
3226
3227/*
3228 * Find the end of the word. Assumes it starts inside a word.
3229 * Returns a pointer to just after the word.
3230 */
3231 char_u *
3232find_word_end(ptr)
3233 char_u *ptr;
3234{
3235#ifdef FEAT_MBYTE
3236 int start_class;
3237
3238 if (has_mbyte)
3239 {
3240 start_class = mb_get_class(ptr);
3241 if (start_class > 1)
3242 while (*ptr != NUL)
3243 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003244 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003245 if (mb_get_class(ptr) != start_class)
3246 break;
3247 }
3248 }
3249 else
3250#endif
3251 while (vim_iswordc(*ptr))
3252 ++ptr;
3253 return ptr;
3254}
3255
3256/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003257 * Find the end of the line, omitting CR and NL at the end.
3258 * Returns a pointer to just after the line.
3259 */
3260 static char_u *
3261find_line_end(ptr)
3262 char_u *ptr;
3263{
3264 char_u *s;
3265
3266 s = ptr + STRLEN(ptr);
3267 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3268 --s;
3269 return s;
3270}
3271
3272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003273 * Free the list of completions
3274 */
3275 static void
3276ins_compl_free()
3277{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003278 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003279 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003280
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003281 vim_free(compl_pattern);
3282 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003283 vim_free(compl_leader);
3284 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003286 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003287 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003288
3289 ins_compl_del_pum();
3290 pum_clear();
3291
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003292 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003293 do
3294 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003295 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003296 compl_curr_match = compl_curr_match->cp_next;
3297 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003299 if (match->cp_flags & FREE_FNAME)
3300 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003301 for (i = 0; i < CPT_COUNT; ++i)
3302 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003304 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3305 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003306 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307}
3308
3309 static void
3310ins_compl_clear()
3311{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003312 compl_cont_status = 0;
3313 compl_started = FALSE;
3314 compl_matches = 0;
3315 vim_free(compl_pattern);
3316 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003317 vim_free(compl_leader);
3318 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003319 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003320 vim_free(compl_orig_text);
3321 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003322 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323}
3324
3325/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003326 * Return TRUE when Insert completion is active.
3327 */
3328 int
3329ins_compl_active()
3330{
3331 return compl_started;
3332}
3333
3334/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003335 * Delete one character before the cursor and show the subset of the matches
3336 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003337 * Returns the character to be used, NUL if the work is done and another char
3338 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003339 */
3340 static int
3341ins_compl_bs()
3342{
3343 char_u *line;
3344 char_u *p;
3345
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003346 line = ml_get_curline();
3347 p = line + curwin->w_cursor.col;
3348 mb_ptr_back(line, p);
3349
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003350 /* Stop completion when the whole word was deleted. For Omni completion
3351 * allow the word to be deleted, we won't match everything. */
3352 if ((int)(p - line) - (int)compl_col < 0
3353 || ((int)(p - line) - (int)compl_col == 0
3354 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003355 return K_BS;
3356
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003357 /* Deleted more than what was used to find matches or didn't finish
3358 * finding all matches: need to look for matches all over again. */
3359 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003360 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003361 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003362
Bram Moolenaara6557602006-02-04 22:43:20 +00003363 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003364 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003365 if (compl_leader != NULL)
3366 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003367 ins_compl_new_leader();
3368 return NUL;
3369 }
3370 return K_BS;
3371}
Bram Moolenaara6557602006-02-04 22:43:20 +00003372
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003373/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003374 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3375 * be called.
3376 */
3377 static int
3378ins_compl_need_restart()
3379{
3380 /* Return TRUE if we didn't complete finding matches or when the
3381 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3382 return compl_was_interrupted
3383 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3384 && compl_opt_refresh_always);
3385}
3386
3387/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003388 * Called after changing "compl_leader".
3389 * Show the popup menu with a different set of matches.
3390 * May also search for matches again if the previous search was interrupted.
3391 */
3392 static void
3393ins_compl_new_leader()
3394{
3395 ins_compl_del_pum();
3396 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003397 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003398 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003399
3400 if (compl_started)
3401 ins_compl_set_original_text(compl_leader);
3402 else
3403 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003404#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003405 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003406#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003407 /*
3408 * Matches were cleared, need to search for them now. First display
3409 * the changed text before the cursor. Set "compl_restarting" to
3410 * avoid that the first match is inserted.
3411 */
3412 update_screen(0);
3413#ifdef FEAT_GUI
3414 if (gui.in_use)
3415 {
3416 /* Show the cursor after the match, not after the redrawn text. */
3417 setcursor();
3418 out_flush();
3419 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003420 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003421#endif
3422 compl_restarting = TRUE;
3423 if (ins_complete(Ctrl_N) == FAIL)
3424 compl_cont_status = 0;
3425 compl_restarting = FALSE;
3426 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003427
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003428 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003429
3430 /* Show the popup menu with a different set of matches. */
3431 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003432
3433 /* Don't let Enter select the original text when there is no popup menu. */
3434 if (compl_match_array == NULL)
3435 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003436}
3437
3438/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003439 * Return the length of the completion, from the completion start column to
3440 * the cursor column. Making sure it never goes below zero.
3441 */
3442 static int
3443ins_compl_len()
3444{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003445 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003446
3447 if (off < 0)
3448 return 0;
3449 return off;
3450}
3451
3452/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003453 * Append one character to the match leader. May reduce the number of
3454 * matches.
3455 */
3456 static void
3457ins_compl_addleader(c)
3458 int c;
3459{
3460#ifdef FEAT_MBYTE
3461 int cc;
3462
3463 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3464 {
3465 char_u buf[MB_MAXBYTES + 1];
3466
3467 (*mb_char2bytes)(c, buf);
3468 buf[cc] = NUL;
3469 ins_char_bytes(buf, cc);
3470 }
3471 else
3472#endif
3473 ins_char(c);
3474
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003475 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003476 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003477 ins_compl_restart();
3478
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003479 /* When 'always' is set, don't reset compl_leader. While completing,
3480 * cursor don't point original position, changing compl_leader would
3481 * break redo. */
3482 if (!compl_opt_refresh_always)
3483 {
3484 vim_free(compl_leader);
3485 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003486 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003487 if (compl_leader != NULL)
3488 ins_compl_new_leader();
3489 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003490}
3491
3492/*
3493 * Setup for finding completions again without leaving CTRL-X mode. Used when
3494 * BS or a key was typed while still searching for matches.
3495 */
3496 static void
3497ins_compl_restart()
3498{
3499 ins_compl_free();
3500 compl_started = FALSE;
3501 compl_matches = 0;
3502 compl_cont_status = 0;
3503 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003504}
3505
3506/*
3507 * Set the first match, the original text.
3508 */
3509 static void
3510ins_compl_set_original_text(str)
3511 char_u *str;
3512{
3513 char_u *p;
3514
3515 /* Replace the original text entry. */
3516 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3517 {
3518 p = vim_strsave(str);
3519 if (p != NULL)
3520 {
3521 vim_free(compl_first_match->cp_str);
3522 compl_first_match->cp_str = p;
3523 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003524 }
3525}
3526
3527/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003528 * Append one character to the match leader. May reduce the number of
3529 * matches.
3530 */
3531 static void
3532ins_compl_addfrommatch()
3533{
3534 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003535 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003536 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003537 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003538
3539 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003540 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003541 {
3542 /* When still at the original match use the first entry that matches
3543 * the leader. */
3544 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3545 {
3546 p = NULL;
3547 for (cp = compl_shown_match->cp_next; cp != NULL
3548 && cp != compl_first_match; cp = cp->cp_next)
3549 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003550 if (compl_leader == NULL
3551 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003552 (int)STRLEN(compl_leader)))
3553 {
3554 p = cp->cp_str;
3555 break;
3556 }
3557 }
3558 if (p == NULL || (int)STRLEN(p) <= len)
3559 return;
3560 }
3561 else
3562 return;
3563 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003564 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003565 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003566 ins_compl_addleader(c);
3567}
3568
3569/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003571 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003572 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003574 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575ins_compl_prep(c)
3576 int c;
3577{
3578 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003580 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003581
3582 /* Forget any previous 'special' messages if this is actually
3583 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3584 */
3585 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3586 edit_submode_extra = NULL;
3587
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003588 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003589 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3590 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003591 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003593 /* Set "compl_get_longest" when finding the first matches. */
3594 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3595 || (ctrl_x_mode == 0 && !compl_started))
3596 {
3597 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3598 compl_used_match = TRUE;
3599 }
3600
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3602 {
3603 /*
3604 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3605 * it will be yet. Now we decide.
3606 */
3607 switch (c)
3608 {
3609 case Ctrl_E:
3610 case Ctrl_Y:
3611 ctrl_x_mode = CTRL_X_SCROLL;
3612 if (!(State & REPLACE_FLAG))
3613 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3614 else
3615 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3616 edit_submode_pre = NULL;
3617 showmode();
3618 break;
3619 case Ctrl_L:
3620 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3621 break;
3622 case Ctrl_F:
3623 ctrl_x_mode = CTRL_X_FILES;
3624 break;
3625 case Ctrl_K:
3626 ctrl_x_mode = CTRL_X_DICTIONARY;
3627 break;
3628 case Ctrl_R:
3629 /* Simply allow ^R to happen without affecting ^X mode */
3630 break;
3631 case Ctrl_T:
3632 ctrl_x_mode = CTRL_X_THESAURUS;
3633 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003634#ifdef FEAT_COMPL_FUNC
3635 case Ctrl_U:
3636 ctrl_x_mode = CTRL_X_FUNCTION;
3637 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003638 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003639 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003640 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003641#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003642 case 's':
3643 case Ctrl_S:
3644 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003645#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003646 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003647 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003648 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003649#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003650 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 case Ctrl_RSB:
3652 ctrl_x_mode = CTRL_X_TAGS;
3653 break;
3654#ifdef FEAT_FIND_ID
3655 case Ctrl_I:
3656 case K_S_TAB:
3657 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3658 break;
3659 case Ctrl_D:
3660 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3661 break;
3662#endif
3663 case Ctrl_V:
3664 case Ctrl_Q:
3665 ctrl_x_mode = CTRL_X_CMDLINE;
3666 break;
3667 case Ctrl_P:
3668 case Ctrl_N:
3669 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3670 * just started ^X mode, or there were enough ^X's to cancel
3671 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3672 * do normal expansion when interrupting a different mode (say
3673 * ^X^F^X^P or ^P^X^X^P, see below)
3674 * nothing changes if interrupting mode 0, (eg, the flag
3675 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003676 if (!(compl_cont_status & CONT_INTRPT))
3677 compl_cont_status |= CONT_LOCAL;
3678 else if (compl_cont_mode != 0)
3679 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003680 /* FALLTHROUGH */
3681 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003682 /* If we have typed at least 2 ^X's... for modes != 0, we set
3683 * compl_cont_status = 0 (eg, as if we had just started ^X
3684 * mode).
3685 * For mode 0, we set "compl_cont_mode" to an impossible
3686 * value, in both cases ^X^X can be used to restart the same
3687 * mode (avoiding ADDING mode).
3688 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3689 * 'complete' and local ^P expansions respectively.
3690 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3691 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 if (c == Ctrl_X)
3693 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003694 if (compl_cont_mode != 0)
3695 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003697 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 }
3699 ctrl_x_mode = 0;
3700 edit_submode = NULL;
3701 showmode();
3702 break;
3703 }
3704 }
3705 else if (ctrl_x_mode != 0)
3706 {
3707 /* We're already in CTRL-X mode, do we stay in it? */
3708 if (!vim_is_ctrl_x_key(c))
3709 {
3710 if (ctrl_x_mode == CTRL_X_SCROLL)
3711 ctrl_x_mode = 0;
3712 else
3713 ctrl_x_mode = CTRL_X_FINISHED;
3714 edit_submode = NULL;
3715 }
3716 showmode();
3717 }
3718
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003719 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 {
3721 /* Show error message from attempted keyword completion (probably
3722 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003723 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003725 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3726 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003727 || ctrl_x_mode == CTRL_X_FINISHED)
3728 {
3729 /* Get here when we have finished typing a sequence of ^N and
3730 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003731 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003732 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733 {
3734 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003735 * If any of the original typed text has been changed, eg when
3736 * ignorecase is set, we must add back-spaces to the redo
3737 * buffer. We add as few as necessary to delete just the part
3738 * of the original text that has changed.
3739 * When using the longest match, edited the match or used
3740 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003742 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3743 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003744 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003745 ptr = NULL;
3746 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747 }
3748
3749#ifdef FEAT_CINDENT
3750 want_cindent = (can_cindent && cindent_on());
3751#endif
3752 /*
3753 * When completing whole lines: fix indent for 'cindent'.
3754 * Otherwise, break line if it's too long.
3755 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003756 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003757 {
3758#ifdef FEAT_CINDENT
3759 /* re-indent the current line */
3760 if (want_cindent)
3761 {
3762 do_c_expr_indent();
3763 want_cindent = FALSE; /* don't do it again */
3764 }
3765#endif
3766 }
3767 else
3768 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003769 int prev_col = curwin->w_cursor.col;
3770
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003772 if (prev_col > 0)
3773 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 if (stop_arrow() == OK)
3775 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003776 if (prev_col > 0
3777 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3778 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
3780
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003781 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003782 * the selection without inserting anything. When
3783 * compl_enter_selects is set the Enter key does the same. */
3784 if ((c == Ctrl_Y || (compl_enter_selects
3785 && (c == CAR || c == K_KENTER || c == NL)))
3786 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003787 retval = TRUE;
3788
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003789 /* CTRL-E means completion is Ended, go back to the typed text. */
3790 if (c == Ctrl_E)
3791 {
3792 ins_compl_delete();
3793 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003794 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003795 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003796 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003797 retval = TRUE;
3798 }
3799
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003800 auto_format(FALSE, TRUE);
3801
Bram Moolenaar071d4272004-06-13 20:20:40 +00003802 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003803 compl_started = FALSE;
3804 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003805 msg_clr_cmdline(); /* necessary for "noshowmode" */
3806 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003807 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 if (edit_submode != NULL)
3809 {
3810 edit_submode = NULL;
3811 showmode();
3812 }
3813
3814#ifdef FEAT_CINDENT
3815 /*
3816 * Indent now if a key was typed that is in 'cinkeys'.
3817 */
3818 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3819 do_c_expr_indent();
3820#endif
3821 }
3822 }
3823
3824 /* reset continue_* if we left expansion-mode, if we stay they'll be
3825 * (re)set properly in ins_complete() */
3826 if (!vim_is_ctrl_x_key(c))
3827 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003828 compl_cont_status = 0;
3829 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003830 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003831
3832 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003833}
3834
3835/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003836 * Fix the redo buffer for the completion leader replacing some of the typed
3837 * text. This inserts backspaces and appends the changed text.
3838 * "ptr" is the known leader text or NUL.
3839 */
3840 static void
3841ins_compl_fixRedoBufForLeader(ptr_arg)
3842 char_u *ptr_arg;
3843{
3844 int len;
3845 char_u *p;
3846 char_u *ptr = ptr_arg;
3847
3848 if (ptr == NULL)
3849 {
3850 if (compl_leader != NULL)
3851 ptr = compl_leader;
3852 else
3853 return; /* nothing to do */
3854 }
3855 if (compl_orig_text != NULL)
3856 {
3857 p = compl_orig_text;
3858 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3859 ;
3860#ifdef FEAT_MBYTE
3861 if (len > 0)
3862 len -= (*mb_head_off)(p, p + len);
3863#endif
3864 for (p += len; *p != NUL; mb_ptr_adv(p))
3865 AppendCharToRedobuff(K_BS);
3866 }
3867 else
3868 len = 0;
3869 if (ptr != NULL)
3870 AppendToRedobuffLit(ptr + len, -1);
3871}
3872
3873/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003874 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3875 * (depending on flag) starting from buf and looking for a non-scanned
3876 * buffer (other than curbuf). curbuf is special, if it is called with
3877 * buf=curbuf then it has to be the first call for a given flag/expansion.
3878 *
3879 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3880 */
3881 static buf_T *
3882ins_compl_next_buf(buf, flag)
3883 buf_T *buf;
3884 int flag;
3885{
3886#ifdef FEAT_WINDOWS
3887 static win_T *wp;
3888#endif
3889
3890 if (flag == 'w') /* just windows */
3891 {
3892#ifdef FEAT_WINDOWS
3893 if (buf == curbuf) /* first call for this flag/expansion */
3894 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003895 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003896 && wp->w_buffer->b_scanned)
3897 ;
3898 buf = wp->w_buffer;
3899#else
3900 buf = curbuf;
3901#endif
3902 }
3903 else
3904 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3905 * (unlisted buffers)
3906 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003907 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 && ((flag == 'U'
3909 ? buf->b_p_bl
3910 : (!buf->b_p_bl
3911 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003912 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 ;
3914 return buf;
3915}
3916
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003917#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003918static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003919
3920/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003921 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003922 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003923 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003924 static void
3925expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003926 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003927 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003928{
Bram Moolenaar82139082011-09-14 16:52:09 +02003929 list_T *matchlist = NULL;
3930 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003931 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003932 char_u *funcname;
3933 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003934 win_T *curwin_save;
3935 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02003936 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003937
Bram Moolenaare344bea2005-09-01 20:46:49 +00003938 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3939 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003940 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003941
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003942 /* Call 'completefunc' to obtain the list of matches. */
3943 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003944 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003945
Bram Moolenaare344bea2005-09-01 20:46:49 +00003946 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003947 curwin_save = curwin;
3948 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02003949
3950 /* Call a function, which returns a list or dict. */
3951 if (call_vim_function(funcname, 2, args, FALSE, &rettv) == OK)
3952 {
3953 switch (rettv.v_type)
3954 {
3955 case VAR_LIST:
3956 matchlist = rettv.vval.v_list;
3957 break;
3958 case VAR_DICT:
3959 matchdict = rettv.vval.v_dict;
3960 break;
3961 default:
3962 /* TODO: Give error message? */
3963 clear_tv(&rettv);
3964 break;
3965 }
3966 }
3967
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003968 if (curwin_save != curwin || curbuf_save != curbuf)
3969 {
3970 EMSG(_(e_complwin));
3971 goto theend;
3972 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00003973 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003974 check_cursor();
3975 if (!equalpos(curwin->w_cursor, pos))
3976 {
3977 EMSG(_(e_compldel));
3978 goto theend;
3979 }
Bram Moolenaar82139082011-09-14 16:52:09 +02003980
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003981 if (matchlist != NULL)
3982 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02003983 else if (matchdict != NULL)
3984 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003985
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003986theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02003987 if (matchdict != NULL)
3988 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003989 if (matchlist != NULL)
3990 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003991}
3992#endif /* FEAT_COMPL_FUNC */
3993
Bram Moolenaar39f05632006-03-19 22:15:26 +00003994#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003995/*
3996 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003997 */
3998 static void
3999ins_compl_add_list(list)
4000 list_T *list;
4001{
4002 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004003 int dir = compl_direction;
4004
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004005 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004006 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004007 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004008 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4009 /* if dir was BACKWARD then honor it just once */
4010 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004011 else if (did_emsg)
4012 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004013 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004014}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004015
4016/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004017 * Add completions from a dict.
4018 */
4019 static void
4020ins_compl_add_dict(dict)
4021 dict_T *dict;
4022{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004023 dictitem_T *di_refresh;
4024 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004025
4026 /* Check for optional "refresh" item. */
4027 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004028 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4029 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004030 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004031 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004032
4033 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4034 compl_opt_refresh_always = TRUE;
4035 }
4036
4037 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004038 di_words = dict_find(dict, (char_u *)"words", 5);
4039 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4040 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004041}
4042
4043/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004044 * Add a match to the list of matches from a typeval_T.
4045 * If the given string is already in the list of completions, then return
4046 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4047 * maybe because alloc() returns NULL, then FAIL is returned.
4048 */
4049 int
4050ins_compl_add_tv(tv, dir)
4051 typval_T *tv;
4052 int dir;
4053{
4054 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004055 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004056 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004057 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004058 char_u *(cptext[CPT_COUNT]);
4059
4060 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4061 {
4062 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4063 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4064 (char_u *)"abbr", FALSE);
4065 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4066 (char_u *)"menu", FALSE);
4067 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4068 (char_u *)"kind", FALSE);
4069 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4070 (char_u *)"info", FALSE);
4071 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4072 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004073 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004074 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004075 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4076 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004077 }
4078 else
4079 {
4080 word = get_tv_string_chk(tv);
4081 vim_memset(cptext, 0, sizeof(cptext));
4082 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004083 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004084 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004085 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004086}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004087#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004088
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004089/*
4090 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004091 * The search starts at position "ini" in curbuf and in the direction
4092 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004093 * When "compl_started" is FALSE start at that position, otherwise continue
4094 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004095 * This may return before finding all the matches.
4096 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 */
4098 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004099ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101{
4102 static pos_T first_match_pos;
4103 static pos_T last_match_pos;
4104 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004105 static int found_all = FALSE; /* Found all matches of a
4106 certain type. */
4107 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108
Bram Moolenaar572cb562005-08-05 21:35:02 +00004109 pos_T *pos;
4110 char_u **matches;
4111 int save_p_scs;
4112 int save_p_ws;
4113 int save_p_ic;
4114 int i;
4115 int num_matches;
4116 int len;
4117 int found_new_match;
4118 int type = ctrl_x_mode;
4119 char_u *ptr;
4120 char_u *dict = NULL;
4121 int dict_f = 0;
4122 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 {
4126 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4127 ins_buf->b_scanned = 0;
4128 found_all = FALSE;
4129 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004131 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 last_match_pos = first_match_pos = *ini;
4133 }
4134
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004135 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004136 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4138 for (;;)
4139 {
4140 found_new_match = FAIL;
4141
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004142 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 * or if found_all says this entry is done. For ^X^L only use the
4144 * entries from 'complete' that look in loaded buffers. */
4145 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004146 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 {
4148 found_all = FALSE;
4149 while (*e_cpt == ',' || *e_cpt == ' ')
4150 e_cpt++;
4151 if (*e_cpt == '.' && !curbuf->b_scanned)
4152 {
4153 ins_buf = curbuf;
4154 first_match_pos = *ini;
4155 /* So that ^N can match word immediately after cursor */
4156 if (ctrl_x_mode == 0)
4157 dec(&first_match_pos);
4158 last_match_pos = first_match_pos;
4159 type = 0;
4160 }
4161 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4162 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4163 {
4164 /* Scan a buffer, but not the current one. */
4165 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4166 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004167 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004168 first_match_pos.col = last_match_pos.col = 0;
4169 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4170 last_match_pos.lnum = 0;
4171 type = 0;
4172 }
4173 else /* unloaded buffer, scan like dictionary */
4174 {
4175 found_all = TRUE;
4176 if (ins_buf->b_fname == NULL)
4177 continue;
4178 type = CTRL_X_DICTIONARY;
4179 dict = ins_buf->b_fname;
4180 dict_f = DICT_EXACT;
4181 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004182 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 ins_buf->b_fname == NULL
4184 ? buf_spname(ins_buf)
4185 : ins_buf->b_sfname == NULL
4186 ? (char *)ins_buf->b_fname
4187 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004188 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 }
4190 else if (*e_cpt == NUL)
4191 break;
4192 else
4193 {
4194 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4195 type = -1;
4196 else if (*e_cpt == 'k' || *e_cpt == 's')
4197 {
4198 if (*e_cpt == 'k')
4199 type = CTRL_X_DICTIONARY;
4200 else
4201 type = CTRL_X_THESAURUS;
4202 if (*++e_cpt != ',' && *e_cpt != NUL)
4203 {
4204 dict = e_cpt;
4205 dict_f = DICT_FIRST;
4206 }
4207 }
4208#ifdef FEAT_FIND_ID
4209 else if (*e_cpt == 'i')
4210 type = CTRL_X_PATH_PATTERNS;
4211 else if (*e_cpt == 'd')
4212 type = CTRL_X_PATH_DEFINES;
4213#endif
4214 else if (*e_cpt == ']' || *e_cpt == 't')
4215 {
4216 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004217 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004218 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 }
4220 else
4221 type = -1;
4222
4223 /* in any case e_cpt is advanced to the next entry */
4224 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4225
4226 found_all = TRUE;
4227 if (type == -1)
4228 continue;
4229 }
4230 }
4231
4232 switch (type)
4233 {
4234 case -1:
4235 break;
4236#ifdef FEAT_FIND_ID
4237 case CTRL_X_PATH_PATTERNS:
4238 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004239 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004240 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4244 (linenr_T)1, (linenr_T)MAXLNUM);
4245 break;
4246#endif
4247
4248 case CTRL_X_DICTIONARY:
4249 case CTRL_X_THESAURUS:
4250 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004251 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 : (type == CTRL_X_THESAURUS
4253 ? (*curbuf->b_p_tsr == NUL
4254 ? p_tsr
4255 : curbuf->b_p_tsr)
4256 : (*curbuf->b_p_dict == NUL
4257 ? p_dict
4258 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004259 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004260 dict != NULL ? dict_f
4261 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 dict = NULL;
4263 break;
4264
4265 case CTRL_X_TAGS:
4266 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4267 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004268 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004270 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004271 * of matches is found when compl_pattern is empty */
4272 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4274 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4275 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4276 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004277 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 }
4279 p_ic = save_p_ic;
4280 break;
4281
4282 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004283 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4285 {
4286
4287 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004288 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004289 ins_compl_add_matches(num_matches, matches,
4290#ifdef CASE_INSENSITIVE_FILENAME
4291 TRUE
4292#else
4293 FALSE
4294#endif
4295 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 break;
4298
4299 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 if (expand_cmdline(&compl_xp, compl_pattern,
4301 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004303 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 break;
4305
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004306#ifdef FEAT_COMPL_FUNC
4307 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004308 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004309 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004310 break;
4311#endif
4312
Bram Moolenaar488c6512005-08-11 20:09:58 +00004313 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004314#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004315 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004316 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004317 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004318 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004319#endif
4320 break;
4321
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322 default: /* normal ^P/^N and ^X^L */
4323 /*
4324 * If 'infercase' is set, don't use 'smartcase' here
4325 */
4326 save_p_scs = p_scs;
4327 if (ins_buf->b_p_inf)
4328 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 /* buffers other than curbuf are scanned from the beginning or the
4331 * end but never from the middle, thus setting nowrapscan in this
4332 * buffers is a good idea, on the other hand, we always set
4333 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4334 save_p_ws = p_ws;
4335 if (ins_buf != curbuf)
4336 p_ws = FALSE;
4337 else if (*e_cpt == '.')
4338 p_ws = TRUE;
4339 for (;;)
4340 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004341 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004343 ++msg_silent; /* Don't want messages for wrapscan. */
4344
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004345 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4346 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004348 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004350 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004352 found_new_match = searchit(NULL, ins_buf, pos,
4353 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004354 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004355 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004356 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004357 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004359 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004360 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 first_match_pos = *pos;
4362 last_match_pos = *pos;
4363 }
4364 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004365 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 found_new_match = FAIL;
4367 if (found_new_match == FAIL)
4368 {
4369 if (ins_buf == curbuf)
4370 found_all = TRUE;
4371 break;
4372 }
4373
4374 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004375 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 && ini->lnum == pos->lnum
4377 && ini->col == pos->col)
4378 continue;
4379 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4380 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4381 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 {
4384 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4385 continue;
4386 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4387 if (!p_paste)
4388 ptr = skipwhite(ptr);
4389 }
4390 len = (int)STRLEN(ptr);
4391 }
4392 else
4393 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004394 char_u *tmp_ptr = ptr;
4395
4396 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 /* Skip if already inside a word. */
4400 if (vim_iswordp(tmp_ptr))
4401 continue;
4402 /* Find start of next word. */
4403 tmp_ptr = find_word_start(tmp_ptr);
4404 }
4405 /* Find end of this word. */
4406 tmp_ptr = find_word_end(tmp_ptr);
4407 len = (int)(tmp_ptr - ptr);
4408
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 if ((compl_cont_status & CONT_ADDING)
4410 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 {
4412 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4413 {
4414 /* Try next line, if any. the new word will be
4415 * "join" as if the normal command "J" was used.
4416 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004417 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 * works -- Acevedo */
4419 STRNCPY(IObuff, ptr, len);
4420 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4421 tmp_ptr = ptr = skipwhite(ptr);
4422 /* Find start of next word. */
4423 tmp_ptr = find_word_start(tmp_ptr);
4424 /* Find end of next word. */
4425 tmp_ptr = find_word_end(tmp_ptr);
4426 if (tmp_ptr > ptr)
4427 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004428 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004430 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004431 IObuff[len++] = ' ';
4432 /* IObuf =~ "\k.* ", thus len >= 2 */
4433 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004434 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 || (vim_strchr(p_cpo, CPO_JOINSP)
4436 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004437 && (IObuff[len - 2] == '?'
4438 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 IObuff[len++] = ' ';
4440 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004441 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004442 if (tmp_ptr - ptr >= IOSIZE - len)
4443 tmp_ptr = ptr + IOSIZE - len - 1;
4444 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4445 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004446 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 }
4448 IObuff[len] = NUL;
4449 ptr = IObuff;
4450 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004451 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 continue;
4453 }
4454 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004455 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004456 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004457 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458 {
4459 found_new_match = OK;
4460 break;
4461 }
4462 }
4463 p_scs = save_p_scs;
4464 p_ws = save_p_ws;
4465 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004466
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004467 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004468 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004469 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 found_new_match = OK;
4471
4472 /* break the loop for specialized modes (use 'complete' just for the
4473 * generic ctrl_x_mode == 0) or when we've found a new match */
4474 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004475 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004476 {
4477 if (got_int)
4478 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004479 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004480 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004481 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004482
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004483 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4484 || compl_interrupted)
4485 break;
4486 compl_started = TRUE;
4487 }
4488 else
4489 {
4490 /* Mark a buffer scanned when it has been scanned completely */
4491 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4492 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004494 compl_started = FALSE;
4495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004496 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004497 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
4499 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4500 && *e_cpt == NUL) /* Got to end of 'complete' */
4501 found_new_match = FAIL;
4502
4503 i = -1; /* total of matches, unknown */
4504 if (found_new_match == FAIL
4505 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4506 i = ins_compl_make_cyclic();
4507
4508 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004509 * just been made cyclic then we have to move compl_curr_match to the next
4510 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004511 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4512 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513 if (compl_curr_match == NULL)
4514 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 return i;
4516}
4517
4518/* Delete the old text being completed. */
4519 static void
4520ins_compl_delete()
4521{
4522 int i;
4523
4524 /*
4525 * In insert mode: Delete the typed part.
4526 * In replace mode: Put the old characters back, if any.
4527 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004528 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 backspace_until_column(i);
4530 changed_cline_bef_curs();
4531}
4532
4533/* Insert the new text being completed. */
4534 static void
4535ins_compl_insert()
4536{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004537 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004538 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4539 compl_used_match = FALSE;
4540 else
4541 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542}
4543
4544/*
4545 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004546 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4547 * get more completions. If it is FALSE, then we just do nothing when there
4548 * are no more completions in a given direction. The latter case is used when
4549 * we are still in the middle of finding completions, to allow browsing
4550 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 * Return the total number of matches, or -1 if still unknown -- webb.
4552 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004553 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4554 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 *
4556 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004557 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4558 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 */
4560 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004561ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004563 int count; /* repeat completion this many times; should
4564 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004565 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566{
4567 int num_matches = -1;
4568 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004569 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004570 compl_T *found_compl = NULL;
4571 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004572 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004574 /* When user complete function return -1 for findstart which is next
4575 * time of 'always', compl_shown_match become NULL. */
4576 if (compl_shown_match == NULL)
4577 return -1;
4578
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004579 if (compl_leader != NULL
4580 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004581 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004582 /* Set "compl_shown_match" to the actually shown match, it may differ
4583 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004584 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004585 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004586 && compl_shown_match->cp_next != NULL
4587 && compl_shown_match->cp_next != compl_first_match)
4588 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004589
4590 /* If we didn't find it searching forward, and compl_shows_dir is
4591 * backward, find the last match. */
4592 if (compl_shows_dir == BACKWARD
4593 && !ins_compl_equal(compl_shown_match,
4594 compl_leader, (int)STRLEN(compl_leader))
4595 && (compl_shown_match->cp_next == NULL
4596 || compl_shown_match->cp_next == compl_first_match))
4597 {
4598 while (!ins_compl_equal(compl_shown_match,
4599 compl_leader, (int)STRLEN(compl_leader))
4600 && compl_shown_match->cp_prev != NULL
4601 && compl_shown_match->cp_prev != compl_first_match)
4602 compl_shown_match = compl_shown_match->cp_prev;
4603 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004604 }
4605
4606 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004607 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004608 /* Delete old text to be replaced */
4609 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004610
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004611 /* When finding the longest common text we stick at the original text,
4612 * don't let CTRL-N or CTRL-P move to the first match. */
4613 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4614
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004615 /* When restarting the search don't insert the first match either. */
4616 if (compl_restarting)
4617 {
4618 advance = FALSE;
4619 compl_restarting = FALSE;
4620 }
4621
Bram Moolenaare3226be2005-12-18 22:10:00 +00004622 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4623 * around. */
4624 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004626 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004627 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004628 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004629 found_end = (compl_first_match != NULL
4630 && (compl_shown_match->cp_next == compl_first_match
4631 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004632 }
4633 else if (compl_shows_dir == BACKWARD
4634 && compl_shown_match->cp_prev != NULL)
4635 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004636 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004637 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004638 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
4640 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004641 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004642 if (!allow_get_expansion)
4643 {
4644 if (advance)
4645 {
4646 if (compl_shows_dir == BACKWARD)
4647 compl_pending -= todo + 1;
4648 else
4649 compl_pending += todo + 1;
4650 }
4651 return -1;
4652 }
4653
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004654 if (advance)
4655 {
4656 if (compl_shows_dir == BACKWARD)
4657 --compl_pending;
4658 else
4659 ++compl_pending;
4660 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004661
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004662 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004663 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004664
4665 /* handle any pending completions */
4666 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004667 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004668 {
4669 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4670 {
4671 compl_shown_match = compl_shown_match->cp_next;
4672 --compl_pending;
4673 }
4674 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4675 {
4676 compl_shown_match = compl_shown_match->cp_prev;
4677 ++compl_pending;
4678 }
4679 else
4680 break;
4681 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004682 found_end = FALSE;
4683 }
4684 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4685 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004686 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004687 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004688 ++todo;
4689 else
4690 /* Remember a matching item. */
4691 found_compl = compl_shown_match;
4692
4693 /* Stop at the end of the list when we found a usable match. */
4694 if (found_end)
4695 {
4696 if (found_compl != NULL)
4697 {
4698 compl_shown_match = found_compl;
4699 break;
4700 }
4701 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004703 }
4704
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004705 /* Insert the text of the new completion, or the compl_leader. */
4706 if (insert_match)
4707 {
4708 if (!compl_get_longest || compl_used_match)
4709 ins_compl_insert();
4710 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004711 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004712 }
4713 else
4714 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715
4716 if (!allow_get_expansion)
4717 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004718 /* may undisplay the popup menu first */
4719 ins_compl_upd_pum();
4720
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004721 /* redraw to show the user what was inserted */
4722 update_screen(0);
4723
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004724 /* display the updated popup menu */
4725 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004726#ifdef FEAT_GUI
4727 if (gui.in_use)
4728 {
4729 /* Show the cursor after the match, not after the redrawn text. */
4730 setcursor();
4731 out_flush();
4732 gui_update_cursor(FALSE, FALSE);
4733 }
4734#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004735
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736 /* Delete old text to be replaced, since we're still searching and
4737 * don't want to match ourselves! */
4738 ins_compl_delete();
4739 }
4740
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004741 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004742 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004743 compl_enter_selects = !insert_match && compl_match_array != NULL;
4744
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 /*
4746 * Show the file name for the match (if any)
4747 * Truncate the file name to avoid a wait for return.
4748 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004749 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 {
4751 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004752 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 if (i <= 0)
4754 i = 0;
4755 else
4756 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004757 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 msg(IObuff);
4759 redraw_cmdline = FALSE; /* don't overwrite! */
4760 }
4761
4762 return num_matches;
4763}
4764
4765/*
4766 * Call this while finding completions, to check whether the user has hit a key
4767 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004768 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004770 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 */
4772 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004773ins_compl_check_keys(frequency)
4774 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775{
4776 static int count = 0;
4777
4778 int c;
4779
4780 /* Don't check when reading keys from a script. That would break the test
4781 * scripts */
4782 if (using_script())
4783 return;
4784
4785 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004786 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 return;
4788 count = 0;
4789
Bram Moolenaara260a972006-06-23 19:36:29 +00004790 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4791 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004793 if (c != NUL)
4794 {
4795 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4796 {
4797 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004798 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004799 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4800 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004802 else
4803 {
4804 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004805 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004806 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004807 if (c != K_IGNORE)
4808 {
4809 /* Don't interrupt completion when the character wasn't typed,
4810 * e.g., when doing @q to replay keys. */
4811 if (c != Ctrl_R && KeyTyped)
4812 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004813
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004814 vungetc(c);
4815 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004817 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004818 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004819 {
4820 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4821
4822 compl_pending = 0;
4823 (void)ins_compl_next(FALSE, todo, TRUE);
4824 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004825}
4826
4827/*
4828 * Decide the direction of Insert mode complete from the key typed.
4829 * Returns BACKWARD or FORWARD.
4830 */
4831 static int
4832ins_compl_key2dir(c)
4833 int c;
4834{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004835 if (c == Ctrl_P || c == Ctrl_L
4836 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4837 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004838 return BACKWARD;
4839 return FORWARD;
4840}
4841
4842/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004843 * Return TRUE for keys that are used for completion only when the popup menu
4844 * is visible.
4845 */
4846 static int
4847ins_compl_pum_key(c)
4848 int c;
4849{
4850 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004851 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4852 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004853}
4854
4855/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004856 * Decide the number of completions to move forward.
4857 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4858 */
4859 static int
4860ins_compl_key2count(c)
4861 int c;
4862{
4863 int h;
4864
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004865 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004866 {
4867 h = pum_get_height();
4868 if (h > 3)
4869 h -= 2; /* keep some context */
4870 return h;
4871 }
4872 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873}
4874
4875/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004876 * Return TRUE if completion with "c" should insert the match, FALSE if only
4877 * to change the currently selected completion.
4878 */
4879 static int
4880ins_compl_use_match(c)
4881 int c;
4882{
4883 switch (c)
4884 {
4885 case K_UP:
4886 case K_DOWN:
4887 case K_PAGEDOWN:
4888 case K_KPAGEDOWN:
4889 case K_S_DOWN:
4890 case K_PAGEUP:
4891 case K_KPAGEUP:
4892 case K_S_UP:
4893 return FALSE;
4894 }
4895 return TRUE;
4896}
4897
4898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 * Do Insert mode completion.
4900 * Called when character "c" was typed, which has a meaning for completion.
4901 * Returns OK if completion was done, FAIL if something failed (out of mem).
4902 */
4903 static int
4904ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004905 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004907 char_u *line;
4908 int startcol = 0; /* column where searched text starts */
4909 colnr_T curs_col; /* cursor column */
4910 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004911 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912
Bram Moolenaare3226be2005-12-18 22:10:00 +00004913 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004914 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 {
4916 /* First time we hit ^N or ^P (in a row, I mean) */
4917
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 did_ai = FALSE;
4919#ifdef FEAT_SMARTINDENT
4920 did_si = FALSE;
4921 can_si = FALSE;
4922 can_si_back = FALSE;
4923#endif
4924 if (stop_arrow() == FAIL)
4925 return FAIL;
4926
4927 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004928 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004929 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004931 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004932 * "compl_startpos" to the cursor as a pattern to add a new word
4933 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004934 * "compl_startpos" is not in the same line as the cursor then fix it
4935 * (the line has been split because it was longer than 'tw'). if SOL
4936 * is set then skip the previous pattern, a word at the beginning of
4937 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004938 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4939 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 {
4941 /*
4942 * it is a continued search
4943 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004944 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4946 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4947 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004948 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004950 /* line (probably) wrapped, set compl_startpos to the
4951 * first non_blank in the line, if it is not a wordchar
4952 * include it to get a better pattern, but then we don't
4953 * want the "\\<" prefix, check it bellow */
4954 compl_col = (colnr_T)(skipwhite(line) - line);
4955 compl_startpos.col = compl_col;
4956 compl_startpos.lnum = curwin->w_cursor.lnum;
4957 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959 else
4960 {
4961 /* S_IPOS was set when we inserted a word that was at the
4962 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004963 * mode but first we need to redefine compl_startpos */
4964 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004966 compl_cont_status |= CONT_SOL;
4967 compl_startpos.col = (colnr_T)(skipwhite(
4968 line + compl_length
4969 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004971 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004973 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004974 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004975 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004977 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004979 compl_cont_status &= ~CONT_SOL;
4980 compl_length = (IOSIZE - MIN_SPACE);
4981 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004983 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4984 if (compl_length < 1)
4985 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
4987 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004988 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004989 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004990 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004993 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004995 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004997 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004999 compl_cont_status = 0;
5000 compl_cont_status |= CONT_N_ADDS;
5001 compl_startpos = curwin->w_cursor;
5002 startcol = (int)curs_col;
5003 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 }
5005
5006 /* Work out completion pattern and original text -- webb */
5007 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5008 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005009 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5011 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005012 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005014 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005016 compl_col += ++startcol;
5017 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 }
5019 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005020 compl_pattern = str_foldcase(line + compl_col,
5021 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005023 compl_pattern = vim_strnsave(line + compl_col,
5024 compl_length);
5025 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 return FAIL;
5027 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005028 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 {
5030 char_u *prefix = (char_u *)"\\<";
5031
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005032 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005033 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005034 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005035 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005037 if (!vim_iswordp(line + compl_col)
5038 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 && (
5040#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005041 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005043 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044#endif
5045 )))
5046 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005047 STRCPY((char *)compl_pattern, prefix);
5048 (void)quote_meta(compl_pattern + STRLEN(prefix),
5049 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005050 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005051 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005053 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005055 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056#endif
5057 )
5058 {
5059 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005060 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5061 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005063 compl_col += curs_col;
5064 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 }
5066 else
5067 {
5068#ifdef FEAT_MBYTE
5069 /* Search the point of change class of multibyte character
5070 * or not a word single byte character backward. */
5071 if (has_mbyte)
5072 {
5073 int base_class;
5074 int head_off;
5075
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005076 startcol -= (*mb_head_off)(line, line + startcol);
5077 base_class = mb_get_class(line + startcol);
5078 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005080 head_off = (*mb_head_off)(line, line + startcol);
5081 if (base_class != mb_get_class(line + startcol
5082 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 }
5086 }
5087 else
5088#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005089 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005091 compl_col += ++startcol;
5092 compl_length = (int)curs_col - startcol;
5093 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
5095 /* Only match word with at least two chars -- webb
5096 * there's no need to call quote_meta,
5097 * alloc(7) is enough -- Acevedo
5098 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005099 compl_pattern = alloc(7);
5100 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005102 STRCPY((char *)compl_pattern, "\\<");
5103 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5104 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 }
5106 else
5107 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005108 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005109 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005110 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 STRCPY((char *)compl_pattern, "\\<");
5113 (void)quote_meta(compl_pattern + 2, line + compl_col,
5114 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 }
5116 }
5117 }
5118 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5119 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005120 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005121 compl_length = (int)curs_col - (int)compl_col;
5122 if (compl_length < 0) /* cursor in indent: empty pattern */
5123 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 compl_pattern = str_foldcase(line + compl_col, compl_length,
5126 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005128 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5129 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 return FAIL;
5131 }
5132 else if (ctrl_x_mode == CTRL_X_FILES)
5133 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005134 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005136 compl_col += ++startcol;
5137 compl_length = (int)curs_col - startcol;
5138 compl_pattern = addstar(line + compl_col, compl_length,
5139 EXPAND_FILES);
5140 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 return FAIL;
5142 }
5143 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5144 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005145 compl_pattern = vim_strnsave(line, curs_col);
5146 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005148 set_cmd_context(&compl_xp, compl_pattern,
5149 (int)STRLEN(compl_pattern), curs_col);
5150 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5151 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005152 /* No completion possible, use an empty pattern to get a
5153 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005154 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005155 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005156 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5157 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005159 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005160 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005161#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005162 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005163 * Call user defined function 'completefunc' with "a:findstart"
5164 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005165 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005166 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005167 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005168 char_u *funcname;
5169 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005170 win_T *curwin_save;
5171 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005172
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005173 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005174 * string */
5175 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5176 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5177 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005178 {
5179 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5180 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005181 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005182 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005183
5184 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005185 args[1] = NULL;
5186 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005187 curwin_save = curwin;
5188 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005189 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005190 if (curwin_save != curwin || curbuf_save != curbuf)
5191 {
5192 EMSG(_(e_complwin));
5193 return FAIL;
5194 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005195 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005196 check_cursor();
5197 if (!equalpos(curwin->w_cursor, pos))
5198 {
5199 EMSG(_(e_compldel));
5200 return FAIL;
5201 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005202
Bram Moolenaar6110a002012-01-26 18:58:38 +01005203 /* Return value -2 means the user complete function wants to
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005204 * cancel the complete without an error.
5205 * Return value -3 does the same as -2 and leaves CTRL-X mode.*/
Bram Moolenaar6110a002012-01-26 18:58:38 +01005206 if (col == -2)
5207 return FAIL;
Bram Moolenaar8a4c1362012-05-18 16:35:21 +02005208 if (col == -3)
5209 {
5210 ctrl_x_mode = 0;
5211 edit_submode = NULL;
5212 msg_clr_cmdline();
5213 return FAIL;
5214 }
Bram Moolenaar6110a002012-01-26 18:58:38 +01005215
Bram Moolenaar82139082011-09-14 16:52:09 +02005216 /*
5217 * Reset extended parameters of completion, when start new
5218 * completion.
5219 */
5220 compl_opt_refresh_always = FALSE;
5221
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005222 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005223 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005224 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005225 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005226 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005227
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 /* Setup variables for completion. Need to obtain "line" again,
5229 * it may have become invalid. */
5230 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005231 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005232 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5233 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005234#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005235 return FAIL;
5236 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005237 else if (ctrl_x_mode == CTRL_X_SPELL)
5238 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005239#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005240 if (spell_bad_len > 0)
5241 compl_col = curs_col - spell_bad_len;
5242 else
5243 compl_col = spell_word_start(startcol);
5244 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005245 {
5246 compl_length = 0;
5247 compl_col = curs_col;
5248 }
5249 else
5250 {
5251 spell_expand_check_cap(compl_col);
5252 compl_length = (int)curs_col - compl_col;
5253 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005254 /* Need to obtain "line" again, it may have become invalid. */
5255 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005256 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5257 if (compl_pattern == NULL)
5258#endif
5259 return FAIL;
5260 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005261 else
5262 {
5263 EMSG2(_(e_intern2), "ins_complete()");
5264 return FAIL;
5265 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005267 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005268 {
5269 edit_submode_pre = (char_u *)_(" Adding");
5270 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5271 {
5272 /* Insert a new line, keep indentation but ignore 'comments' */
5273#ifdef FEAT_COMMENTS
5274 char_u *old = curbuf->b_p_com;
5275
5276 curbuf->b_p_com = (char_u *)"";
5277#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005278 compl_startpos.lnum = curwin->w_cursor.lnum;
5279 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280 ins_eol('\r');
5281#ifdef FEAT_COMMENTS
5282 curbuf->b_p_com = old;
5283#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005284 compl_length = 0;
5285 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
5287 }
5288 else
5289 {
5290 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 }
5293
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005294 if (compl_cont_status & CONT_LOCAL)
5295 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 else
5297 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5298
Bram Moolenaar62951b12011-09-21 18:23:05 +02005299 /* If any of the original typed text has been changed we need to fix
5300 * the redo buffer. */
5301 ins_compl_fixRedoBufForLeader(NULL);
5302
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005303 /* Always add completion for the original text. */
5304 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5306 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005307 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005309 vim_free(compl_pattern);
5310 compl_pattern = NULL;
5311 vim_free(compl_orig_text);
5312 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005313 return FAIL;
5314 }
5315
5316 /* showmode might reset the internal line pointers, so it must
5317 * be called before line = ml_get(), or when this address is no
5318 * longer needed. -- Acevedo.
5319 */
5320 edit_submode_extra = (char_u *)_("-- Searching...");
5321 edit_submode_highl = HLF_COUNT;
5322 showmode();
5323 edit_submode_extra = NULL;
5324 out_flush();
5325 }
5326
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005327 compl_shown_match = compl_curr_match;
5328 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329
5330 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005331 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005333 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005334 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005336 /* may undisplay the popup menu */
5337 ins_compl_upd_pum();
5338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005339 if (n > 1) /* all matches have been found */
5340 compl_matches = n;
5341 compl_curr_match = compl_shown_match;
5342 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343
Bram Moolenaard68071d2006-05-02 22:08:30 +00005344 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5345 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 if (got_int && !global_busy)
5347 {
5348 (void)vgetc();
5349 got_int = FALSE;
5350 }
5351
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005353 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005355 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5356 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005357 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5358 edit_submode_highl = HLF_E;
5359 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5360 * because we couldn't expand anything at first place, but if we used
5361 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5362 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005363 if ( compl_length > 1
5364 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 || (ctrl_x_mode != 0
5366 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5367 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005368 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 }
5370
Bram Moolenaar572cb562005-08-05 21:35:02 +00005371 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005372 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005373 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005374 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375
5376 if (edit_submode_extra == NULL)
5377 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005378 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005379 {
5380 edit_submode_extra = (char_u *)_("Back at original");
5381 edit_submode_highl = HLF_W;
5382 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005383 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005384 {
5385 edit_submode_extra = (char_u *)_("Word from other line");
5386 edit_submode_highl = HLF_COUNT;
5387 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005388 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005389 {
5390 edit_submode_extra = (char_u *)_("The only match");
5391 edit_submode_highl = HLF_COUNT;
5392 }
5393 else
5394 {
5395 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005396 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005398 int number = 0;
5399 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005400
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005401 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005402 {
5403 /* search backwards for the first valid (!= -1) number.
5404 * This should normally succeed already at the first loop
5405 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005406 for (match = compl_curr_match->cp_prev; match != NULL
5407 && match != compl_first_match;
5408 match = match->cp_prev)
5409 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005410 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005411 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005412 break;
5413 }
5414 if (match != NULL)
5415 /* go up and assign all numbers which are not assigned
5416 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005417 for (match = match->cp_next;
5418 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005419 match = match->cp_next)
5420 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421 }
5422 else /* BACKWARD */
5423 {
5424 /* search forwards (upwards) for the first valid (!= -1)
5425 * number. This should normally succeed already at the
5426 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005427 for (match = compl_curr_match->cp_next; match != NULL
5428 && match != compl_first_match;
5429 match = match->cp_next)
5430 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005431 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005432 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005433 break;
5434 }
5435 if (match != NULL)
5436 /* go down and assign all numbers which are not
5437 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005438 for (match = match->cp_prev; match
5439 && match->cp_number == -1;
5440 match = match->cp_prev)
5441 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005442 }
5443 }
5444
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005445 /* The match should always have a sequence number now, this is
5446 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005447 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005449 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5450 * Translations may need more than twice that. */
5451 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005452
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005453 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005454 vim_snprintf((char *)match_ref, sizeof(match_ref),
5455 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005456 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005458 vim_snprintf((char *)match_ref, sizeof(match_ref),
5459 _("match %d"),
5460 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 edit_submode_extra = match_ref;
5462 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005463 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005464 curs_columns(FALSE);
5465 }
5466 }
5467 }
5468
5469 /* Show a message about what (completion) mode we're in. */
5470 showmode();
5471 if (edit_submode_extra != NULL)
5472 {
5473 if (!p_smd)
5474 msg_attr(edit_submode_extra,
5475 edit_submode_highl < HLF_COUNT
5476 ? hl_attr(edit_submode_highl) : 0);
5477 }
5478 else
5479 msg_clr_cmdline(); /* necessary for "noshowmode" */
5480
Bram Moolenaard68071d2006-05-02 22:08:30 +00005481 /* Show the popup menu, unless we got interrupted. */
5482 if (!compl_interrupted)
5483 {
5484 /* RedrawingDisabled may be set when invoked through complete(). */
5485 n = RedrawingDisabled;
5486 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005487
5488 /* If the cursor moved we need to remove the pum first. */
5489 setcursor();
5490 if (save_w_wrow != curwin->w_wrow)
5491 ins_compl_del_pum();
5492
Bram Moolenaard68071d2006-05-02 22:08:30 +00005493 ins_compl_show_pum();
5494 setcursor();
5495 RedrawingDisabled = n;
5496 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005497 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005498 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005499
Bram Moolenaar071d4272004-06-13 20:20:40 +00005500 return OK;
5501}
5502
5503/*
5504 * Looks in the first "len" chars. of "src" for search-metachars.
5505 * If dest is not NULL the chars. are copied there quoting (with
5506 * a backslash) the metachars, and dest would be NUL terminated.
5507 * Returns the length (needed) of dest
5508 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005509 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005510quote_meta(dest, src, len)
5511 char_u *dest;
5512 char_u *src;
5513 int len;
5514{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005515 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005516
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005517 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005518 {
5519 switch (*src)
5520 {
5521 case '.':
5522 case '*':
5523 case '[':
5524 if (ctrl_x_mode == CTRL_X_DICTIONARY
5525 || ctrl_x_mode == CTRL_X_THESAURUS)
5526 break;
5527 case '~':
5528 if (!p_magic) /* quote these only if magic is set */
5529 break;
5530 case '\\':
5531 if (ctrl_x_mode == CTRL_X_DICTIONARY
5532 || ctrl_x_mode == CTRL_X_THESAURUS)
5533 break;
5534 case '^': /* currently it's not needed. */
5535 case '$':
5536 m++;
5537 if (dest != NULL)
5538 *dest++ = '\\';
5539 break;
5540 }
5541 if (dest != NULL)
5542 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005543# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005544 /* Copy remaining bytes of a multibyte character. */
5545 if (has_mbyte)
5546 {
5547 int i, mb_len;
5548
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005549 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005550 if (mb_len > 0 && len >= mb_len)
5551 for (i = 0; i < mb_len; ++i)
5552 {
5553 --len;
5554 ++src;
5555 if (dest != NULL)
5556 *dest++ = *src;
5557 }
5558 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005559# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 }
5561 if (dest != NULL)
5562 *dest = NUL;
5563
5564 return m;
5565}
5566#endif /* FEAT_INS_EXPAND */
5567
5568/*
5569 * Next character is interpreted literally.
5570 * A one, two or three digit decimal number is interpreted as its byte value.
5571 * If one or two digits are entered, the next character is given to vungetc().
5572 * For Unicode a character > 255 may be returned.
5573 */
5574 int
5575get_literal()
5576{
5577 int cc;
5578 int nc;
5579 int i;
5580 int hex = FALSE;
5581 int octal = FALSE;
5582#ifdef FEAT_MBYTE
5583 int unicode = 0;
5584#endif
5585
5586 if (got_int)
5587 return Ctrl_C;
5588
5589#ifdef FEAT_GUI
5590 /*
5591 * In GUI there is no point inserting the internal code for a special key.
5592 * It is more useful to insert the string "<KEY>" instead. This would
5593 * probably be useful in a text window too, but it would not be
5594 * vi-compatible (maybe there should be an option for it?) -- webb
5595 */
5596 if (gui.in_use)
5597 ++allow_keys;
5598#endif
5599#ifdef USE_ON_FLY_SCROLL
5600 dont_scroll = TRUE; /* disallow scrolling here */
5601#endif
5602 ++no_mapping; /* don't map the next key hits */
5603 cc = 0;
5604 i = 0;
5605 for (;;)
5606 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005607 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608#ifdef FEAT_CMDL_INFO
5609 if (!(State & CMDLINE)
5610# ifdef FEAT_MBYTE
5611 && MB_BYTE2LEN_CHECK(nc) == 1
5612# endif
5613 )
5614 add_to_showcmd(nc);
5615#endif
5616 if (nc == 'x' || nc == 'X')
5617 hex = TRUE;
5618 else if (nc == 'o' || nc == 'O')
5619 octal = TRUE;
5620#ifdef FEAT_MBYTE
5621 else if (nc == 'u' || nc == 'U')
5622 unicode = nc;
5623#endif
5624 else
5625 {
5626 if (hex
5627#ifdef FEAT_MBYTE
5628 || unicode != 0
5629#endif
5630 )
5631 {
5632 if (!vim_isxdigit(nc))
5633 break;
5634 cc = cc * 16 + hex2nr(nc);
5635 }
5636 else if (octal)
5637 {
5638 if (nc < '0' || nc > '7')
5639 break;
5640 cc = cc * 8 + nc - '0';
5641 }
5642 else
5643 {
5644 if (!VIM_ISDIGIT(nc))
5645 break;
5646 cc = cc * 10 + nc - '0';
5647 }
5648
5649 ++i;
5650 }
5651
5652 if (cc > 255
5653#ifdef FEAT_MBYTE
5654 && unicode == 0
5655#endif
5656 )
5657 cc = 255; /* limit range to 0-255 */
5658 nc = 0;
5659
5660 if (hex) /* hex: up to two chars */
5661 {
5662 if (i >= 2)
5663 break;
5664 }
5665#ifdef FEAT_MBYTE
5666 else if (unicode) /* Unicode: up to four or eight chars */
5667 {
5668 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5669 break;
5670 }
5671#endif
5672 else if (i >= 3) /* decimal or octal: up to three chars */
5673 break;
5674 }
5675 if (i == 0) /* no number entered */
5676 {
5677 if (nc == K_ZERO) /* NUL is stored as NL */
5678 {
5679 cc = '\n';
5680 nc = 0;
5681 }
5682 else
5683 {
5684 cc = nc;
5685 nc = 0;
5686 }
5687 }
5688
5689 if (cc == 0) /* NUL is stored as NL */
5690 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005691#ifdef FEAT_MBYTE
5692 if (enc_dbcs && (cc & 0xff) == 0)
5693 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5694 second byte will cause trouble! */
5695#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
5697 --no_mapping;
5698#ifdef FEAT_GUI
5699 if (gui.in_use)
5700 --allow_keys;
5701#endif
5702 if (nc)
5703 vungetc(nc);
5704 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5705 return cc;
5706}
5707
5708/*
5709 * Insert character, taking care of special keys and mod_mask
5710 */
5711 static void
5712insert_special(c, allow_modmask, ctrlv)
5713 int c;
5714 int allow_modmask;
5715 int ctrlv; /* c was typed after CTRL-V */
5716{
5717 char_u *p;
5718 int len;
5719
5720 /*
5721 * Special function key, translate into "<Key>". Up to the last '>' is
5722 * inserted with ins_str(), so as not to replace characters in replace
5723 * mode.
5724 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5725 * unless 'allow_modmask' is TRUE.
5726 */
5727#ifdef MACOS
5728 /* Command-key never produces a normal key */
5729 if (mod_mask & MOD_MASK_CMD)
5730 allow_modmask = TRUE;
5731#endif
5732 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5733 {
5734 p = get_special_key_name(c, mod_mask);
5735 len = (int)STRLEN(p);
5736 c = p[len - 1];
5737 if (len > 2)
5738 {
5739 if (stop_arrow() == FAIL)
5740 return;
5741 p[len - 1] = NUL;
5742 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005743 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 ctrlv = FALSE;
5745 }
5746 }
5747 if (stop_arrow() == OK)
5748 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5749}
5750
5751/*
5752 * Special characters in this context are those that need processing other
5753 * than the simple insertion that can be performed here. This includes ESC
5754 * which terminates the insert, and CR/NL which need special processing to
5755 * open up a new line. This routine tries to optimize insertions performed by
5756 * the "redo", "undo" or "put" commands, so it needs to know when it should
5757 * stop and defer processing to the "normal" mechanism.
5758 * '0' and '^' are special, because they can be followed by CTRL-D.
5759 */
5760#ifdef EBCDIC
5761# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5762#else
5763# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5764#endif
5765
5766#ifdef FEAT_MBYTE
5767# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5768#else
5769# define WHITECHAR(cc) vim_iswhite(cc)
5770#endif
5771
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02005772/*
5773 * "flags": INSCHAR_FORMAT - force formatting
5774 * INSCHAR_CTRLV - char typed just after CTRL-V
5775 * INSCHAR_NO_FEX - don't use 'formatexpr'
5776 *
5777 * NOTE: passes the flags value straight through to internal_format() which,
5778 * beside INSCHAR_FORMAT (above), is also looking for these:
5779 * INSCHAR_DO_COM - format comments
5780 * INSCHAR_COM_LIST - format comments with num list or 2nd line indent
5781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 void
5783insertchar(c, flags, second_indent)
5784 int c; /* character to insert or NUL */
5785 int flags; /* INSCHAR_FORMAT, etc. */
5786 int second_indent; /* indent for second line if >= 0 */
5787{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 int textwidth;
5789#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005792 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5795 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
5797 /*
5798 * Try to break the line in two or more pieces when:
5799 * - Always do this if we have been called to do formatting only.
5800 * - Always do this when 'formatoptions' has the 'a' flag and the line
5801 * ends in white space.
5802 * - Otherwise:
5803 * - Don't do this if inserting a blank
5804 * - Don't do this if an existing character is being replaced, unless
5805 * we're in VREPLACE mode.
5806 * - Do this if the cursor is not on the line where insert started
5807 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5808 * before the insert.
5809 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5810 * before 'textwidth'
5811 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005812 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813 && ((flags & INSCHAR_FORMAT)
5814 || (!vim_iswhite(c)
5815 && !((State & REPLACE_FLAG)
5816#ifdef FEAT_VREPLACE
5817 && !(State & VREPLACE_FLAG)
5818#endif
5819 && *ml_get_cursor() != NUL)
5820 && (curwin->w_cursor.lnum != Insstart.lnum
5821 || ((!has_format_option(FO_INS_LONG)
5822 || Insstart_textlen <= (colnr_T)textwidth)
5823 && (!fo_ins_blank
5824 || Insstart_blank_vcol <= (colnr_T)textwidth
5825 ))))))
5826 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005827 /* Format with 'formatexpr' when it's set. Use internal formatting
5828 * when 'formatexpr' isn't set or it returns non-zero. */
5829#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005830 int do_internal = TRUE;
5831
Bram Moolenaar81a82092008-03-12 16:27:00 +00005832 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005833 {
5834 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5835 /* It may be required to save for undo again, e.g. when setline()
5836 * was called. */
5837 ins_need_undo = TRUE;
5838 }
5839 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005841 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005843
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 if (c == NUL) /* only formatting was wanted */
5845 return;
5846
5847#ifdef FEAT_COMMENTS
5848 /* Check whether this character should end a comment. */
5849 if (did_ai && (int)c == end_comment_pending)
5850 {
5851 char_u *line;
5852 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5853 int middle_len, end_len;
5854 int i;
5855
5856 /*
5857 * Need to remove existing (middle) comment leader and insert end
5858 * comment leader. First, check what comment leader we can find.
5859 */
Bram Moolenaar81340392012-06-06 16:12:59 +02005860 i = get_leader_len(line = ml_get_curline(), &p, FALSE, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5862 {
5863 /* Skip middle-comment string */
5864 while (*p && p[-1] != ':') /* find end of middle flags */
5865 ++p;
5866 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5867 /* Don't count trailing white space for middle_len */
5868 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5869 --middle_len;
5870
5871 /* Find the end-comment string */
5872 while (*p && p[-1] != ':') /* find end of end flags */
5873 ++p;
5874 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5875
5876 /* Skip white space before the cursor */
5877 i = curwin->w_cursor.col;
5878 while (--i >= 0 && vim_iswhite(line[i]))
5879 ;
5880 i++;
5881
5882 /* Skip to before the middle leader */
5883 i -= middle_len;
5884
5885 /* Check some expected things before we go on */
5886 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5887 {
5888 /* Backspace over all the stuff we want to replace */
5889 backspace_until_column(i);
5890
5891 /*
5892 * Insert the end-comment string, except for the last
5893 * character, which will get inserted as normal later.
5894 */
5895 ins_bytes_len(lead_end, end_len - 1);
5896 }
5897 }
5898 }
5899 end_comment_pending = NUL;
5900#endif
5901
5902 did_ai = FALSE;
5903#ifdef FEAT_SMARTINDENT
5904 did_si = FALSE;
5905 can_si = FALSE;
5906 can_si_back = FALSE;
5907#endif
5908
5909 /*
5910 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5911 * This speeds up normal text input considerably.
5912 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5913 * need to re-indent at a ':', or any other character (but not what
5914 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005915 * Don't do this when there an InsertCharPre autocommand is defined,
5916 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005917 */
5918#ifdef USE_ON_FLY_SCROLL
5919 dont_scroll = FALSE; /* allow scrolling here */
5920#endif
5921
5922 if ( !ISSPECIAL(c)
5923#ifdef FEAT_MBYTE
5924 && (!has_mbyte || (*mb_char2len)(c) == 1)
5925#endif
5926 && vpeekc() != NUL
5927 && !(State & REPLACE_FLAG)
5928#ifdef FEAT_CINDENT
5929 && !cindent_on()
5930#endif
5931#ifdef FEAT_RIGHTLEFT
5932 && !p_ri
5933#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005934#ifdef FEAT_AUTOCMD
5935 && !has_insertcharpre()
5936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005937 )
5938 {
5939#define INPUT_BUFLEN 100
5940 char_u buf[INPUT_BUFLEN + 1];
5941 int i;
5942 colnr_T virtcol = 0;
5943
5944 buf[0] = c;
5945 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005946 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005947 virtcol = get_nolist_virtcol();
5948 /*
5949 * Stop the string when:
5950 * - no more chars available
5951 * - finding a special character (command key)
5952 * - buffer is full
5953 * - running into the 'textwidth' boundary
5954 * - need to check for abbreviation: A non-word char after a word-char
5955 */
5956 while ( (c = vpeekc()) != NUL
5957 && !ISSPECIAL(c)
5958#ifdef FEAT_MBYTE
5959 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5960#endif
5961 && i < INPUT_BUFLEN
5962 && (textwidth == 0
5963 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5964 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5965 {
5966#ifdef FEAT_RIGHTLEFT
5967 c = vgetc();
5968 if (p_hkmap && KeyTyped)
5969 c = hkmap(c); /* Hebrew mode mapping */
5970# ifdef FEAT_FKMAP
5971 if (p_fkmap && KeyTyped)
5972 c = fkmap(c); /* Farsi mode mapping */
5973# endif
5974 buf[i++] = c;
5975#else
5976 buf[i++] = vgetc();
5977#endif
5978 }
5979
5980#ifdef FEAT_DIGRAPHS
5981 do_digraph(-1); /* clear digraphs */
5982 do_digraph(buf[i-1]); /* may be the start of a digraph */
5983#endif
5984 buf[i] = NUL;
5985 ins_str(buf);
5986 if (flags & INSCHAR_CTRLV)
5987 {
5988 redo_literal(*buf);
5989 i = 1;
5990 }
5991 else
5992 i = 0;
5993 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005994 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005995 }
5996 else
5997 {
5998#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999 int cc;
6000
Bram Moolenaar071d4272004-06-13 20:20:40 +00006001 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
6002 {
6003 char_u buf[MB_MAXBYTES + 1];
6004
6005 (*mb_char2bytes)(c, buf);
6006 buf[cc] = NUL;
6007 ins_char_bytes(buf, cc);
6008 AppendCharToRedobuff(c);
6009 }
6010 else
6011#endif
6012 {
6013 ins_char(c);
6014 if (flags & INSCHAR_CTRLV)
6015 redo_literal(c);
6016 else
6017 AppendCharToRedobuff(c);
6018 }
6019 }
6020}
6021
6022/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006023 * Format text at the current insert position.
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006024 *
6025 * If the INSCHAR_COM_LIST flag is present, then the value of second_indent
6026 * will be the comment leader length sent to open_line().
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006027 */
6028 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006029internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006030 int textwidth;
6031 int second_indent;
6032 int flags;
6033 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006034 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006035{
6036 int cc;
6037 int save_char = NUL;
6038 int haveto_redraw = FALSE;
6039 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6040#ifdef FEAT_MBYTE
6041 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6042#endif
6043 int fo_white_par = has_format_option(FO_WHITE_PAR);
6044 int first_line = TRUE;
6045#ifdef FEAT_COMMENTS
6046 colnr_T leader_len;
6047 int no_leader = FALSE;
6048 int do_comments = (flags & INSCHAR_DO_COM);
6049#endif
6050
6051 /*
6052 * When 'ai' is off we don't want a space under the cursor to be
6053 * deleted. Replace it with an 'x' temporarily.
6054 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006055 if (!curbuf->b_p_ai
6056#ifdef FEAT_VREPLACE
6057 && !(State & VREPLACE_FLAG)
6058#endif
6059 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006060 {
6061 cc = gchar_cursor();
6062 if (vim_iswhite(cc))
6063 {
6064 save_char = cc;
6065 pchar_cursor('x');
6066 }
6067 }
6068
6069 /*
6070 * Repeat breaking lines, until the current line is not too long.
6071 */
6072 while (!got_int)
6073 {
6074 int startcol; /* Cursor column at entry */
6075 int wantcol; /* column at textwidth border */
6076 int foundcol; /* column for start of spaces */
6077 int end_foundcol = 0; /* column for start of word */
6078 colnr_T len;
6079 colnr_T virtcol;
6080#ifdef FEAT_VREPLACE
6081 int orig_col = 0;
6082 char_u *saved_text = NULL;
6083#endif
6084 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006085 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006086
Bram Moolenaar97b98102009-11-17 16:41:01 +00006087 virtcol = get_nolist_virtcol()
6088 + char2cells(c != NUL ? c : gchar_cursor());
6089 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006090 break;
6091
6092#ifdef FEAT_COMMENTS
6093 if (no_leader)
6094 do_comments = FALSE;
6095 else if (!(flags & INSCHAR_FORMAT)
6096 && has_format_option(FO_WRAP_COMS))
6097 do_comments = TRUE;
6098
6099 /* Don't break until after the comment leader */
6100 if (do_comments)
Bram Moolenaar81340392012-06-06 16:12:59 +02006101 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006102 else
6103 leader_len = 0;
6104
6105 /* If the line doesn't start with a comment leader, then don't
6106 * start one in a following broken line. Avoids that a %word
6107 * moved to the start of the next line causes all following lines
6108 * to start with %. */
6109 if (leader_len == 0)
6110 no_leader = TRUE;
6111#endif
6112 if (!(flags & INSCHAR_FORMAT)
6113#ifdef FEAT_COMMENTS
6114 && leader_len == 0
6115#endif
6116 && !has_format_option(FO_WRAP))
6117
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006118 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006119 if ((startcol = curwin->w_cursor.col) == 0)
6120 break;
6121
6122 /* find column of textwidth border */
6123 coladvance((colnr_T)textwidth);
6124 wantcol = curwin->w_cursor.col;
6125
Bram Moolenaar97b98102009-11-17 16:41:01 +00006126 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006127 foundcol = 0;
6128
6129 /*
6130 * Find position to break at.
6131 * Stop at first entered white when 'formatoptions' has 'v'
6132 */
6133 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006134 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006135 || curwin->w_cursor.lnum != Insstart.lnum
6136 || curwin->w_cursor.col >= Insstart.col)
6137 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006138 if (curwin->w_cursor.col == startcol && c != NUL)
6139 cc = c;
6140 else
6141 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006142 if (WHITECHAR(cc))
6143 {
6144 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006145 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006146
6147 /* find start of sequence of blanks */
6148 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6149 {
6150 dec_cursor();
6151 cc = gchar_cursor();
6152 }
6153 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6154 break; /* only spaces in front of text */
6155#ifdef FEAT_COMMENTS
6156 /* Don't break until after the comment leader */
6157 if (curwin->w_cursor.col < leader_len)
6158 break;
6159#endif
6160 if (has_format_option(FO_ONE_LETTER))
6161 {
6162 /* do not break after one-letter words */
6163 if (curwin->w_cursor.col == 0)
6164 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006165#ifdef FEAT_COMMENTS
6166 /* do not break "#a b" when 'tw' is 2 */
6167 if (curwin->w_cursor.col <= leader_len)
6168 break;
6169#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006170 col = curwin->w_cursor.col;
6171 dec_cursor();
6172 cc = gchar_cursor();
6173
6174 if (WHITECHAR(cc))
6175 continue; /* one-letter, continue */
6176 curwin->w_cursor.col = col;
6177 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006178
6179 inc_cursor();
6180
6181 end_foundcol = end_col + 1;
6182 foundcol = curwin->w_cursor.col;
6183 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006184 break;
6185 }
6186#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006187 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006188 {
6189 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006190 if (curwin->w_cursor.col != startcol)
6191 {
6192#ifdef FEAT_COMMENTS
6193 /* Don't break until after the comment leader */
6194 if (curwin->w_cursor.col < leader_len)
6195 break;
6196#endif
6197 col = curwin->w_cursor.col;
6198 inc_cursor();
6199 /* Don't change end_foundcol if already set. */
6200 if (foundcol != curwin->w_cursor.col)
6201 {
6202 foundcol = curwin->w_cursor.col;
6203 end_foundcol = foundcol;
6204 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6205 break;
6206 }
6207 curwin->w_cursor.col = col;
6208 }
6209
6210 if (curwin->w_cursor.col == 0)
6211 break;
6212
6213 col = curwin->w_cursor.col;
6214
6215 dec_cursor();
6216 cc = gchar_cursor();
6217
6218 if (WHITECHAR(cc))
6219 continue; /* break with space */
6220#ifdef FEAT_COMMENTS
6221 /* Don't break until after the comment leader */
6222 if (curwin->w_cursor.col < leader_len)
6223 break;
6224#endif
6225
6226 curwin->w_cursor.col = col;
6227
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006228 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006229 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006230 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6231 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006232 }
6233#endif
6234 if (curwin->w_cursor.col == 0)
6235 break;
6236 dec_cursor();
6237 }
6238
6239 if (foundcol == 0) /* no spaces, cannot break line */
6240 {
6241 curwin->w_cursor.col = startcol;
6242 break;
6243 }
6244
6245 /* Going to break the line, remove any "$" now. */
6246 undisplay_dollar();
6247
6248 /*
6249 * Offset between cursor position and line break is used by replace
6250 * stack functions. VREPLACE does not use this, and backspaces
6251 * over the text instead.
6252 */
6253#ifdef FEAT_VREPLACE
6254 if (State & VREPLACE_FLAG)
6255 orig_col = startcol; /* Will start backspacing from here */
6256 else
6257#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006258 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006259
6260 /*
6261 * adjust startcol for spaces that will be deleted and
6262 * characters that will remain on top line
6263 */
6264 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006265 while ((cc = gchar_cursor(), WHITECHAR(cc))
6266 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006267 inc_cursor();
6268 startcol -= curwin->w_cursor.col;
6269 if (startcol < 0)
6270 startcol = 0;
6271
6272#ifdef FEAT_VREPLACE
6273 if (State & VREPLACE_FLAG)
6274 {
6275 /*
6276 * In VREPLACE mode, we will backspace over the text to be
6277 * wrapped, so save a copy now to put on the next line.
6278 */
6279 saved_text = vim_strsave(ml_get_cursor());
6280 curwin->w_cursor.col = orig_col;
6281 if (saved_text == NULL)
6282 break; /* Can't do it, out of memory */
6283 saved_text[startcol] = NUL;
6284
6285 /* Backspace over characters that will move to the next line */
6286 if (!fo_white_par)
6287 backspace_until_column(foundcol);
6288 }
6289 else
6290#endif
6291 {
6292 /* put cursor after pos. to break line */
6293 if (!fo_white_par)
6294 curwin->w_cursor.col = foundcol;
6295 }
6296
6297 /*
6298 * Split the line just before the margin.
6299 * Only insert/delete lines, but don't really redraw the window.
6300 */
6301 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6302 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6303#ifdef FEAT_COMMENTS
6304 + (do_comments ? OPENLINE_DO_COM : 0)
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006305 + ((flags & INSCHAR_COM_LIST) ? OPENLINE_COM_LIST : 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006306#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006307 , ((flags & INSCHAR_COM_LIST) ? second_indent : old_indent));
6308 if (!(flags & INSCHAR_COM_LIST))
6309 old_indent = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006310
6311 replace_offset = 0;
6312 if (first_line)
6313 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006314 if (!(flags & INSCHAR_COM_LIST))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006315 {
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006316 /*
6317 * This section is for numeric lists w/o comments. If comment
6318 * indents are needed with numeric lists (formatoptions=nq),
6319 * then the INSCHAR_COM_LIST flag will cause the corresponding
6320 * OPENLINE_COM_LIST flag to be passed through to open_line()
6321 * (as seen above)...
6322 */
6323 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6324 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6325 if (second_indent >= 0)
6326 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006327#ifdef FEAT_VREPLACE
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006328 if (State & VREPLACE_FLAG)
6329 change_indent(INDENT_SET, second_indent,
6330 FALSE, NUL, TRUE);
6331 else
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006332#endif
Bram Moolenaarbfe3bf82012-06-13 17:28:55 +02006333 (void)set_indent(second_indent, SIN_CHANGED);
6334 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006335 }
6336 first_line = FALSE;
6337 }
6338
6339#ifdef FEAT_VREPLACE
6340 if (State & VREPLACE_FLAG)
6341 {
6342 /*
6343 * In VREPLACE mode we have backspaced over the text to be
6344 * moved, now we re-insert it into the new line.
6345 */
6346 ins_bytes(saved_text);
6347 vim_free(saved_text);
6348 }
6349 else
6350#endif
6351 {
6352 /*
6353 * Check if cursor is not past the NUL off the line, cindent
6354 * may have added or removed indent.
6355 */
6356 curwin->w_cursor.col += startcol;
6357 len = (colnr_T)STRLEN(ml_get_curline());
6358 if (curwin->w_cursor.col > len)
6359 curwin->w_cursor.col = len;
6360 }
6361
6362 haveto_redraw = TRUE;
6363#ifdef FEAT_CINDENT
6364 can_cindent = TRUE;
6365#endif
6366 /* moved the cursor, don't autoindent or cindent now */
6367 did_ai = FALSE;
6368#ifdef FEAT_SMARTINDENT
6369 did_si = FALSE;
6370 can_si = FALSE;
6371 can_si_back = FALSE;
6372#endif
6373 line_breakcheck();
6374 }
6375
6376 if (save_char != NUL) /* put back space after cursor */
6377 pchar_cursor(save_char);
6378
6379 if (!format_only && haveto_redraw)
6380 {
6381 update_topline();
6382 redraw_curbuf_later(VALID);
6383 }
6384}
6385
6386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 * Called after inserting or deleting text: When 'formatoptions' includes the
6388 * 'a' flag format from the current line until the end of the paragraph.
6389 * Keep the cursor at the same position relative to the text.
6390 * The caller must have saved the cursor line for undo, following ones will be
6391 * saved here.
6392 */
6393 void
6394auto_format(trailblank, prev_line)
6395 int trailblank; /* when TRUE also format with trailing blank */
6396 int prev_line; /* may start in previous line */
6397{
6398 pos_T pos;
6399 colnr_T len;
6400 char_u *old;
6401 char_u *new, *pnew;
6402 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006403 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006404
6405 if (!has_format_option(FO_AUTO))
6406 return;
6407
6408 pos = curwin->w_cursor;
6409 old = ml_get_curline();
6410
6411 /* may remove added space */
6412 check_auto_format(FALSE);
6413
6414 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6415 * user might insert normal text next. Also skip formatting when "1" is
6416 * in 'formatoptions' and there is a single character before the cursor.
6417 * Otherwise the line would be broken and when typing another non-white
6418 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006419 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006420 if (*old != NUL && !trailblank && wasatend)
6421 {
6422 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006423 cc = gchar_cursor();
6424 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6425 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006426 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006427 cc = gchar_cursor();
6428 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
6430 curwin->w_cursor = pos;
6431 return;
6432 }
6433 curwin->w_cursor = pos;
6434 }
6435
6436#ifdef FEAT_COMMENTS
6437 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6438 * comments. */
6439 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
Bram Moolenaar81340392012-06-06 16:12:59 +02006440 && get_leader_len(old, NULL, FALSE, TRUE) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006441 return;
6442#endif
6443
6444 /*
6445 * May start formatting in a previous line, so that after "x" a word is
6446 * moved to the previous line if it fits there now. Only when this is not
6447 * the start of a paragraph.
6448 */
6449 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6450 {
6451 --curwin->w_cursor.lnum;
6452 if (u_save_cursor() == FAIL)
6453 return;
6454 }
6455
6456 /*
6457 * Do the formatting and restore the cursor position. "saved_cursor" will
6458 * be adjusted for the text formatting.
6459 */
6460 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006461 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006462 curwin->w_cursor = saved_cursor;
6463 saved_cursor.lnum = 0;
6464
6465 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6466 {
6467 /* "cannot happen" */
6468 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6469 coladvance((colnr_T)MAXCOL);
6470 }
6471 else
6472 check_cursor_col();
6473
6474 /* Insert mode: If the cursor is now after the end of the line while it
6475 * previously wasn't, the line was broken. Because of the rule above we
6476 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6477 * formatted. */
6478 if (!wasatend && has_format_option(FO_WHITE_PAR))
6479 {
6480 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006481 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 if (curwin->w_cursor.col == len)
6483 {
6484 pnew = vim_strnsave(new, len + 2);
6485 pnew[len] = ' ';
6486 pnew[len + 1] = NUL;
6487 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6488 /* remove the space later */
6489 did_add_space = TRUE;
6490 }
6491 else
6492 /* may remove added space */
6493 check_auto_format(FALSE);
6494 }
6495
6496 check_cursor();
6497}
6498
6499/*
6500 * When an extra space was added to continue a paragraph for auto-formatting,
6501 * delete it now. The space must be under the cursor, just after the insert
6502 * position.
6503 */
6504 static void
6505check_auto_format(end_insert)
6506 int end_insert; /* TRUE when ending Insert mode */
6507{
6508 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006509 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006510
6511 if (did_add_space)
6512 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006513 cc = gchar_cursor();
6514 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515 /* Somehow the space was removed already. */
6516 did_add_space = FALSE;
6517 else
6518 {
6519 if (!end_insert)
6520 {
6521 inc_cursor();
6522 c = gchar_cursor();
6523 dec_cursor();
6524 }
6525 if (c != NUL)
6526 {
6527 /* The space is no longer at the end of the line, delete it. */
6528 del_char(FALSE);
6529 did_add_space = FALSE;
6530 }
6531 }
6532 }
6533}
6534
6535/*
6536 * Find out textwidth to be used for formatting:
6537 * if 'textwidth' option is set, use it
6538 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6539 * if invalid value, use 0.
6540 * Set default to window width (maximum 79) for "gq" operator.
6541 */
6542 int
6543comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006544 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545{
6546 int textwidth;
6547
6548 textwidth = curbuf->b_p_tw;
6549 if (textwidth == 0 && curbuf->b_p_wm)
6550 {
6551 /* The width is the window width minus 'wrapmargin' minus all the
6552 * things that add to the margin. */
6553 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6554#ifdef FEAT_CMDWIN
6555 if (cmdwin_type != 0)
6556 textwidth -= 1;
6557#endif
6558#ifdef FEAT_FOLDING
6559 textwidth -= curwin->w_p_fdc;
6560#endif
6561#ifdef FEAT_SIGNS
6562 if (curwin->w_buffer->b_signlist != NULL
6563# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006564 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006565# endif
6566 )
6567 textwidth -= 1;
6568#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006569 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 textwidth -= 8;
6571 }
6572 if (textwidth < 0)
6573 textwidth = 0;
6574 if (ff && textwidth == 0)
6575 {
6576 textwidth = W_WIDTH(curwin) - 1;
6577 if (textwidth > 79)
6578 textwidth = 79;
6579 }
6580 return textwidth;
6581}
6582
6583/*
6584 * Put a character in the redo buffer, for when just after a CTRL-V.
6585 */
6586 static void
6587redo_literal(c)
6588 int c;
6589{
6590 char_u buf[10];
6591
6592 /* Only digits need special treatment. Translate them into a string of
6593 * three digits. */
6594 if (VIM_ISDIGIT(c))
6595 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006596 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006597 AppendToRedobuff(buf);
6598 }
6599 else
6600 AppendCharToRedobuff(c);
6601}
6602
6603/*
6604 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006605 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006606 */
6607 static void
6608start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006609 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610{
6611 if (!arrow_used) /* something has been inserted */
6612 {
6613 AppendToRedobuff(ESC_STR);
6614 stop_insert(end_insert_pos, FALSE);
6615 arrow_used = TRUE; /* this means we stopped the current insert */
6616 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006617#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006618 check_spell_redraw();
6619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620}
6621
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006622#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006623/*
6624 * If we skipped highlighting word at cursor, do it now.
6625 * It may be skipped again, thus reset spell_redraw_lnum first.
6626 */
6627 static void
6628check_spell_redraw()
6629{
6630 if (spell_redraw_lnum != 0)
6631 {
6632 linenr_T lnum = spell_redraw_lnum;
6633
6634 spell_redraw_lnum = 0;
6635 redrawWinline(lnum, FALSE);
6636 }
6637}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006638
6639/*
6640 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6641 * spelled word, if there is one.
6642 */
6643 static void
6644spell_back_to_badword()
6645{
6646 pos_T tpos = curwin->w_cursor;
6647
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006648 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006649 if (curwin->w_cursor.col != tpos.col)
6650 start_arrow(&tpos);
6651}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006652#endif
6653
Bram Moolenaar071d4272004-06-13 20:20:40 +00006654/*
6655 * stop_arrow() is called before a change is made in insert mode.
6656 * If an arrow key has been used, start a new insertion.
6657 * Returns FAIL if undo is impossible, shouldn't insert then.
6658 */
6659 int
6660stop_arrow()
6661{
6662 if (arrow_used)
6663 {
6664 if (u_save_cursor() == OK)
6665 {
6666 arrow_used = FALSE;
6667 ins_need_undo = FALSE;
6668 }
6669 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006670 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 ai_col = 0;
6672#ifdef FEAT_VREPLACE
6673 if (State & VREPLACE_FLAG)
6674 {
6675 orig_line_count = curbuf->b_ml.ml_line_count;
6676 vr_lines_changed = 1;
6677 }
6678#endif
6679 ResetRedobuff();
6680 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006681 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006682 }
6683 else if (ins_need_undo)
6684 {
6685 if (u_save_cursor() == OK)
6686 ins_need_undo = FALSE;
6687 }
6688
6689#ifdef FEAT_FOLDING
6690 /* Always open fold at the cursor line when inserting something. */
6691 foldOpenCursor();
6692#endif
6693
6694 return (arrow_used || ins_need_undo ? FAIL : OK);
6695}
6696
6697/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006698 * Do a few things to stop inserting.
6699 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6700 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 */
6702 static void
6703stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006704 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006705 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006707 int cc;
6708 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709
6710 stop_redo_ins();
6711 replace_flush(); /* abandon replace stack */
6712
6713 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006714 * Save the inserted text for later redo with ^@ and CTRL-A.
6715 * Don't do it when "restart_edit" was set and nothing was inserted,
6716 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006718 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006719 if (did_restart_edit == 0 || (ptr != NULL
6720 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006721 {
6722 vim_free(last_insert);
6723 last_insert = ptr;
6724 last_insert_skip = new_insert_skip;
6725 }
6726 else
6727 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006729 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006730 {
6731 /* Auto-format now. It may seem strange to do this when stopping an
6732 * insertion (or moving the cursor), but it's required when appending
6733 * a line and having it end in a space. But only do it when something
6734 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006735 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006737 pos_T tpos = curwin->w_cursor;
6738
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 /* When the cursor is at the end of the line after a space the
6740 * formatting will move it to the following word. Avoid that by
6741 * moving the cursor onto the space. */
6742 cc = 'x';
6743 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6744 {
6745 dec_cursor();
6746 cc = gchar_cursor();
6747 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006748 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 }
6750
6751 auto_format(TRUE, FALSE);
6752
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006753 if (vim_iswhite(cc))
6754 {
6755 if (gchar_cursor() != NUL)
6756 inc_cursor();
6757#ifdef FEAT_VIRTUALEDIT
6758 /* If the cursor is still at the same character, also keep
6759 * the "coladd". */
6760 if (gchar_cursor() == NUL
6761 && curwin->w_cursor.lnum == tpos.lnum
6762 && curwin->w_cursor.col == tpos.col)
6763 curwin->w_cursor.coladd = tpos.coladd;
6764#endif
6765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 }
6767
6768 /* If a space was inserted for auto-formatting, remove it now. */
6769 check_auto_format(TRUE);
6770
6771 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006772 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006773 * Do this when ESC was used or moving the cursor up/down.
6774 * Check for the old position still being valid, just in case the text
6775 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006776 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006777 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6778 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006780 pos_T tpos = curwin->w_cursor;
6781
6782 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006783 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006784 for (;;)
6785 {
6786 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6787 --curwin->w_cursor.col;
6788 cc = gchar_cursor();
6789 if (!vim_iswhite(cc))
6790 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006791 if (del_char(TRUE) == FAIL)
6792 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006793 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006794 if (curwin->w_cursor.lnum != tpos.lnum)
6795 curwin->w_cursor = tpos;
6796 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006797 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6798
6799#ifdef FEAT_VISUAL
6800 /* <C-S-Right> may have started Visual mode, adjust the position for
6801 * deleted characters. */
6802 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6803 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006804 int len = (int)STRLEN(ml_get_curline());
6805
6806 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006807 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006808 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006809# ifdef FEAT_VIRTUALEDIT
6810 VIsual.coladd = 0;
6811# endif
6812 }
6813 }
6814#endif
6815 }
6816 }
6817 did_ai = FALSE;
6818#ifdef FEAT_SMARTINDENT
6819 did_si = FALSE;
6820 can_si = FALSE;
6821 can_si_back = FALSE;
6822#endif
6823
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006824 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6825 * now in a different buffer. */
6826 if (end_insert_pos != NULL)
6827 {
6828 curbuf->b_op_start = Insstart;
6829 curbuf->b_op_end = *end_insert_pos;
6830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006831}
6832
6833/*
6834 * Set the last inserted text to a single character.
6835 * Used for the replace command.
6836 */
6837 void
6838set_last_insert(c)
6839 int c;
6840{
6841 char_u *s;
6842
6843 vim_free(last_insert);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 last_insert = alloc(MB_MAXBYTES * 3 + 5);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845 if (last_insert != NULL)
6846 {
6847 s = last_insert;
6848 /* Use the CTRL-V only when entering a special char */
6849 if (c < ' ' || c == DEL)
6850 *s++ = Ctrl_V;
6851 s = add_char2buf(c, s);
6852 *s++ = ESC;
6853 *s++ = NUL;
6854 last_insert_skip = 0;
6855 }
6856}
6857
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006858#if defined(EXITFREE) || defined(PROTO)
6859 void
6860free_last_insert()
6861{
6862 vim_free(last_insert);
6863 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006864# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006865 vim_free(compl_orig_text);
6866 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006867# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006868}
6869#endif
6870
Bram Moolenaar071d4272004-06-13 20:20:40 +00006871/*
6872 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6873 * and CSI. Handle multi-byte characters.
6874 * Returns a pointer to after the added bytes.
6875 */
6876 char_u *
6877add_char2buf(c, s)
6878 int c;
6879 char_u *s;
6880{
6881#ifdef FEAT_MBYTE
Bram Moolenaar9a920d82012-06-01 15:21:02 +02006882 char_u temp[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00006883 int i;
6884 int len;
6885
6886 len = (*mb_char2bytes)(c, temp);
6887 for (i = 0; i < len; ++i)
6888 {
6889 c = temp[i];
6890#endif
6891 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6892 if (c == K_SPECIAL)
6893 {
6894 *s++ = K_SPECIAL;
6895 *s++ = KS_SPECIAL;
6896 *s++ = KE_FILLER;
6897 }
6898#ifdef FEAT_GUI
6899 else if (c == CSI)
6900 {
6901 *s++ = CSI;
6902 *s++ = KS_EXTRA;
6903 *s++ = (int)KE_CSI;
6904 }
6905#endif
6906 else
6907 *s++ = c;
6908#ifdef FEAT_MBYTE
6909 }
6910#endif
6911 return s;
6912}
6913
6914/*
6915 * move cursor to start of line
6916 * if flags & BL_WHITE move to first non-white
6917 * if flags & BL_SOL move to first non-white if startofline is set,
6918 * otherwise keep "curswant" column
6919 * if flags & BL_FIX don't leave the cursor on a NUL.
6920 */
6921 void
6922beginline(flags)
6923 int flags;
6924{
6925 if ((flags & BL_SOL) && !p_sol)
6926 coladvance(curwin->w_curswant);
6927 else
6928 {
6929 curwin->w_cursor.col = 0;
6930#ifdef FEAT_VIRTUALEDIT
6931 curwin->w_cursor.coladd = 0;
6932#endif
6933
6934 if (flags & (BL_WHITE | BL_SOL))
6935 {
6936 char_u *ptr;
6937
6938 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6939 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6940 ++curwin->w_cursor.col;
6941 }
6942 curwin->w_set_curswant = TRUE;
6943 }
6944}
6945
6946/*
6947 * oneright oneleft cursor_down cursor_up
6948 *
6949 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006950 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006951 * Return OK when successful, FAIL when we hit a line of file boundary.
6952 */
6953
6954 int
6955oneright()
6956{
6957 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006958 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006959
6960#ifdef FEAT_VIRTUALEDIT
6961 if (virtual_active())
6962 {
6963 pos_T prevpos = curwin->w_cursor;
6964
6965 /* Adjust for multi-wide char (excluding TAB) */
6966 ptr = ml_get_cursor();
6967 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006968# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006969 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006970# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006971 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006972# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006973 ))
6974 ? ptr2cells(ptr) : 1));
6975 curwin->w_set_curswant = TRUE;
6976 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6977 return (prevpos.col != curwin->w_cursor.col
6978 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6979 }
6980#endif
6981
6982 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006983 if (*ptr == NUL)
6984 return FAIL; /* already at the very end */
6985
Bram Moolenaar071d4272004-06-13 20:20:40 +00006986#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006987 if (has_mbyte)
6988 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006989 else
6990#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006991 l = 1;
6992
6993 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6994 * contains "onemore". */
6995 if (ptr[l] == NUL
6996#ifdef FEAT_VIRTUALEDIT
6997 && (ve_flags & VE_ONEMORE) == 0
6998#endif
6999 )
7000 return FAIL;
7001 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007002
7003 curwin->w_set_curswant = TRUE;
7004 return OK;
7005}
7006
7007 int
7008oneleft()
7009{
7010#ifdef FEAT_VIRTUALEDIT
7011 if (virtual_active())
7012 {
7013 int width;
7014 int v = getviscol();
7015
7016 if (v == 0)
7017 return FAIL;
7018
7019# ifdef FEAT_LINEBREAK
7020 /* We might get stuck on 'showbreak', skip over it. */
7021 width = 1;
7022 for (;;)
7023 {
7024 coladvance(v - width);
7025 /* getviscol() is slow, skip it when 'showbreak' is empty and
7026 * there are no multi-byte characters */
7027 if ((*p_sbr == NUL
7028# ifdef FEAT_MBYTE
7029 && !has_mbyte
7030# endif
7031 ) || getviscol() < v)
7032 break;
7033 ++width;
7034 }
7035# else
7036 coladvance(v - 1);
7037# endif
7038
7039 if (curwin->w_cursor.coladd == 1)
7040 {
7041 char_u *ptr;
7042
7043 /* Adjust for multi-wide char (not a TAB) */
7044 ptr = ml_get_cursor();
7045 if (*ptr != TAB && vim_isprintc(
7046# ifdef FEAT_MBYTE
7047 (*mb_ptr2char)(ptr)
7048# else
7049 *ptr
7050# endif
7051 ) && ptr2cells(ptr) > 1)
7052 curwin->w_cursor.coladd = 0;
7053 }
7054
7055 curwin->w_set_curswant = TRUE;
7056 return OK;
7057 }
7058#endif
7059
7060 if (curwin->w_cursor.col == 0)
7061 return FAIL;
7062
7063 curwin->w_set_curswant = TRUE;
7064 --curwin->w_cursor.col;
7065
7066#ifdef FEAT_MBYTE
7067 /* if the character on the left of the current cursor is a multi-byte
7068 * character, move to its first byte */
7069 if (has_mbyte)
7070 mb_adjust_cursor();
7071#endif
7072 return OK;
7073}
7074
7075 int
7076cursor_up(n, upd_topline)
7077 long n;
7078 int upd_topline; /* When TRUE: update topline */
7079{
7080 linenr_T lnum;
7081
7082 if (n > 0)
7083 {
7084 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007085 /* This fails if the cursor is already in the first line or the count
7086 * is larger than the line number and '-' is in 'cpoptions' */
7087 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007088 return FAIL;
7089 if (n >= lnum)
7090 lnum = 1;
7091 else
7092#ifdef FEAT_FOLDING
7093 if (hasAnyFolding(curwin))
7094 {
7095 /*
7096 * Count each sequence of folded lines as one logical line.
7097 */
7098 /* go to the the start of the current fold */
7099 (void)hasFolding(lnum, &lnum, NULL);
7100
7101 while (n--)
7102 {
7103 /* move up one line */
7104 --lnum;
7105 if (lnum <= 1)
7106 break;
7107 /* If we entered a fold, move to the beginning, unless in
7108 * Insert mode or when 'foldopen' contains "all": it will open
7109 * in a moment. */
7110 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7111 (void)hasFolding(lnum, &lnum, NULL);
7112 }
7113 if (lnum < 1)
7114 lnum = 1;
7115 }
7116 else
7117#endif
7118 lnum -= n;
7119 curwin->w_cursor.lnum = lnum;
7120 }
7121
7122 /* try to advance to the column we want to be at */
7123 coladvance(curwin->w_curswant);
7124
7125 if (upd_topline)
7126 update_topline(); /* make sure curwin->w_topline is valid */
7127
7128 return OK;
7129}
7130
7131/*
7132 * Cursor down a number of logical lines.
7133 */
7134 int
7135cursor_down(n, upd_topline)
7136 long n;
7137 int upd_topline; /* When TRUE: update topline */
7138{
7139 linenr_T lnum;
7140
7141 if (n > 0)
7142 {
7143 lnum = curwin->w_cursor.lnum;
7144#ifdef FEAT_FOLDING
7145 /* Move to last line of fold, will fail if it's the end-of-file. */
7146 (void)hasFolding(lnum, NULL, &lnum);
7147#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007148 /* This fails if the cursor is already in the last line or would move
7149 * beyound the last line and '-' is in 'cpoptions' */
7150 if (lnum >= curbuf->b_ml.ml_line_count
7151 || (lnum + n > curbuf->b_ml.ml_line_count
7152 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153 return FAIL;
7154 if (lnum + n >= curbuf->b_ml.ml_line_count)
7155 lnum = curbuf->b_ml.ml_line_count;
7156 else
7157#ifdef FEAT_FOLDING
7158 if (hasAnyFolding(curwin))
7159 {
7160 linenr_T last;
7161
7162 /* count each sequence of folded lines as one logical line */
7163 while (n--)
7164 {
7165 if (hasFolding(lnum, NULL, &last))
7166 lnum = last + 1;
7167 else
7168 ++lnum;
7169 if (lnum >= curbuf->b_ml.ml_line_count)
7170 break;
7171 }
7172 if (lnum > curbuf->b_ml.ml_line_count)
7173 lnum = curbuf->b_ml.ml_line_count;
7174 }
7175 else
7176#endif
7177 lnum += n;
7178 curwin->w_cursor.lnum = lnum;
7179 }
7180
7181 /* try to advance to the column we want to be at */
7182 coladvance(curwin->w_curswant);
7183
7184 if (upd_topline)
7185 update_topline(); /* make sure curwin->w_topline is valid */
7186
7187 return OK;
7188}
7189
7190/*
7191 * Stuff the last inserted text in the read buffer.
7192 * Last_insert actually is a copy of the redo buffer, so we
7193 * first have to remove the command.
7194 */
7195 int
7196stuff_inserted(c, count, no_esc)
7197 int c; /* Command character to be inserted */
7198 long count; /* Repeat this many times */
7199 int no_esc; /* Don't add an ESC at the end */
7200{
7201 char_u *esc_ptr;
7202 char_u *ptr;
7203 char_u *last_ptr;
7204 char_u last = NUL;
7205
7206 ptr = get_last_insert();
7207 if (ptr == NULL)
7208 {
7209 EMSG(_(e_noinstext));
7210 return FAIL;
7211 }
7212
7213 /* may want to stuff the command character, to start Insert mode */
7214 if (c != NUL)
7215 stuffcharReadbuff(c);
7216 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7217 *esc_ptr = NUL; /* remove the ESC */
7218
7219 /* when the last char is either "0" or "^" it will be quoted if no ESC
7220 * comes after it OR if it will inserted more than once and "ptr"
7221 * starts with ^D. -- Acevedo
7222 */
7223 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7224 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7225 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7226 {
7227 last = *last_ptr;
7228 *last_ptr = NUL;
7229 }
7230
7231 do
7232 {
7233 stuffReadbuff(ptr);
7234 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7235 if (last)
7236 stuffReadbuff((char_u *)(last == '0'
7237 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7238 : IF_EB("\026^", CTRL_V_STR "^")));
7239 }
7240 while (--count > 0);
7241
7242 if (last)
7243 *last_ptr = last;
7244
7245 if (esc_ptr != NULL)
7246 *esc_ptr = ESC; /* put the ESC back */
7247
7248 /* may want to stuff a trailing ESC, to get out of Insert mode */
7249 if (!no_esc)
7250 stuffcharReadbuff(ESC);
7251
7252 return OK;
7253}
7254
7255 char_u *
7256get_last_insert()
7257{
7258 if (last_insert == NULL)
7259 return NULL;
7260 return last_insert + last_insert_skip;
7261}
7262
7263/*
7264 * Get last inserted string, and remove trailing <Esc>.
7265 * Returns pointer to allocated memory (must be freed) or NULL.
7266 */
7267 char_u *
7268get_last_insert_save()
7269{
7270 char_u *s;
7271 int len;
7272
7273 if (last_insert == NULL)
7274 return NULL;
7275 s = vim_strsave(last_insert + last_insert_skip);
7276 if (s != NULL)
7277 {
7278 len = (int)STRLEN(s);
7279 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7280 s[len - 1] = NUL;
7281 }
7282 return s;
7283}
7284
7285/*
7286 * Check the word in front of the cursor for an abbreviation.
7287 * Called when the non-id character "c" has been entered.
7288 * When an abbreviation is recognized it is removed from the text and
7289 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7290 */
7291 static int
7292echeck_abbr(c)
7293 int c;
7294{
7295 /* Don't check for abbreviation in paste mode, when disabled and just
7296 * after moving around with cursor keys. */
7297 if (p_paste || no_abbr || arrow_used)
7298 return FALSE;
7299
7300 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7301 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7302}
7303
7304/*
7305 * replace-stack functions
7306 *
7307 * When replacing characters, the replaced characters are remembered for each
7308 * new character. This is used to re-insert the old text when backspacing.
7309 *
7310 * There is a NUL headed list of characters for each character that is
7311 * currently in the file after the insertion point. When BS is used, one NUL
7312 * headed list is put back for the deleted character.
7313 *
7314 * For a newline, there are two NUL headed lists. One contains the characters
7315 * that the NL replaced. The extra one stores the characters after the cursor
7316 * that were deleted (always white space).
7317 *
7318 * Replace_offset is normally 0, in which case replace_push will add a new
7319 * character at the end of the stack. If replace_offset is not 0, that many
7320 * characters will be left on the stack above the newly inserted character.
7321 */
7322
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007323static char_u *replace_stack = NULL;
7324static long replace_stack_nr = 0; /* next entry in replace stack */
7325static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007326
7327 void
7328replace_push(c)
7329 int c; /* character that is replaced (NUL is none) */
7330{
7331 char_u *p;
7332
7333 if (replace_stack_nr < replace_offset) /* nothing to do */
7334 return;
7335 if (replace_stack_len <= replace_stack_nr)
7336 {
7337 replace_stack_len += 50;
7338 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7339 if (p == NULL) /* out of memory */
7340 {
7341 replace_stack_len -= 50;
7342 return;
7343 }
7344 if (replace_stack != NULL)
7345 {
7346 mch_memmove(p, replace_stack,
7347 (size_t)(replace_stack_nr * sizeof(char_u)));
7348 vim_free(replace_stack);
7349 }
7350 replace_stack = p;
7351 }
7352 p = replace_stack + replace_stack_nr - replace_offset;
7353 if (replace_offset)
7354 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7355 *p = c;
7356 ++replace_stack_nr;
7357}
7358
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007359#if defined(FEAT_MBYTE) || defined(PROTO)
7360/*
7361 * Push a character onto the replace stack. Handles a multi-byte character in
7362 * reverse byte order, so that the first byte is popped off first.
7363 * Return the number of bytes done (includes composing characters).
7364 */
7365 int
7366replace_push_mb(p)
7367 char_u *p;
7368{
7369 int l = (*mb_ptr2len)(p);
7370 int j;
7371
7372 for (j = l - 1; j >= 0; --j)
7373 replace_push(p[j]);
7374 return l;
7375}
7376#endif
7377
Bram Moolenaar071d4272004-06-13 20:20:40 +00007378/*
7379 * Pop one item from the replace stack.
7380 * return -1 if stack empty
7381 * return replaced character or NUL otherwise
7382 */
7383 static int
7384replace_pop()
7385{
7386 if (replace_stack_nr == 0)
7387 return -1;
7388 return (int)replace_stack[--replace_stack_nr];
7389}
7390
7391/*
7392 * Join the top two items on the replace stack. This removes to "off"'th NUL
7393 * encountered.
7394 */
7395 static void
7396replace_join(off)
7397 int off; /* offset for which NUL to remove */
7398{
7399 int i;
7400
7401 for (i = replace_stack_nr; --i >= 0; )
7402 if (replace_stack[i] == NUL && off-- <= 0)
7403 {
7404 --replace_stack_nr;
7405 mch_memmove(replace_stack + i, replace_stack + i + 1,
7406 (size_t)(replace_stack_nr - i));
7407 return;
7408 }
7409}
7410
7411/*
7412 * Pop bytes from the replace stack until a NUL is found, and insert them
7413 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7414 */
7415 static void
7416replace_pop_ins()
7417{
7418 int cc;
7419 int oldState = State;
7420
7421 State = NORMAL; /* don't want REPLACE here */
7422 while ((cc = replace_pop()) > 0)
7423 {
7424#ifdef FEAT_MBYTE
7425 mb_replace_pop_ins(cc);
7426#else
7427 ins_char(cc);
7428#endif
7429 dec_cursor();
7430 }
7431 State = oldState;
7432}
7433
7434#ifdef FEAT_MBYTE
7435/*
7436 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7437 * indicates a multi-byte char, pop the other bytes too.
7438 */
7439 static void
7440mb_replace_pop_ins(cc)
7441 int cc;
7442{
7443 int n;
Bram Moolenaar9a920d82012-06-01 15:21:02 +02007444 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaar071d4272004-06-13 20:20:40 +00007445 int i;
7446 int c;
7447
7448 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7449 {
7450 buf[0] = cc;
7451 for (i = 1; i < n; ++i)
7452 buf[i] = replace_pop();
7453 ins_bytes_len(buf, n);
7454 }
7455 else
7456 ins_char(cc);
7457
7458 if (enc_utf8)
7459 /* Handle composing chars. */
7460 for (;;)
7461 {
7462 c = replace_pop();
7463 if (c == -1) /* stack empty */
7464 break;
7465 if ((n = MB_BYTE2LEN(c)) == 1)
7466 {
7467 /* Not a multi-byte char, put it back. */
7468 replace_push(c);
7469 break;
7470 }
7471 else
7472 {
7473 buf[0] = c;
7474 for (i = 1; i < n; ++i)
7475 buf[i] = replace_pop();
7476 if (utf_iscomposing(utf_ptr2char(buf)))
7477 ins_bytes_len(buf, n);
7478 else
7479 {
7480 /* Not a composing char, put it back. */
7481 for (i = n - 1; i >= 0; --i)
7482 replace_push(buf[i]);
7483 break;
7484 }
7485 }
7486 }
7487}
7488#endif
7489
7490/*
7491 * make the replace stack empty
7492 * (called when exiting replace mode)
7493 */
7494 static void
7495replace_flush()
7496{
7497 vim_free(replace_stack);
7498 replace_stack = NULL;
7499 replace_stack_len = 0;
7500 replace_stack_nr = 0;
7501}
7502
7503/*
7504 * Handle doing a BS for one character.
7505 * cc < 0: replace stack empty, just move cursor
7506 * cc == 0: character was inserted, delete it
7507 * cc > 0: character was replaced, put cc (first byte of original char) back
7508 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007509 * When "limit_col" is >= 0, don't delete before this column. Matters when
7510 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007511 */
7512 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007513replace_do_bs(limit_col)
7514 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007515{
7516 int cc;
7517#ifdef FEAT_VREPLACE
7518 int orig_len = 0;
7519 int ins_len;
7520 int orig_vcols = 0;
7521 colnr_T start_vcol;
7522 char_u *p;
7523 int i;
7524 int vcol;
7525#endif
7526
7527 cc = replace_pop();
7528 if (cc > 0)
7529 {
7530#ifdef FEAT_VREPLACE
7531 if (State & VREPLACE_FLAG)
7532 {
7533 /* Get the number of screen cells used by the character we are
7534 * going to delete. */
7535 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7536 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7537 }
7538#endif
7539#ifdef FEAT_MBYTE
7540 if (has_mbyte)
7541 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007542 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007543# ifdef FEAT_VREPLACE
7544 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007545 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546# endif
7547 replace_push(cc);
7548 }
7549 else
7550#endif
7551 {
7552 pchar_cursor(cc);
7553#ifdef FEAT_VREPLACE
7554 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007555 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007556#endif
7557 }
7558 replace_pop_ins();
7559
7560#ifdef FEAT_VREPLACE
7561 if (State & VREPLACE_FLAG)
7562 {
7563 /* Get the number of screen cells used by the inserted characters */
7564 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007565 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007566 vcol = start_vcol;
7567 for (i = 0; i < ins_len; ++i)
7568 {
7569 vcol += chartabsize(p + i, vcol);
7570#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007571 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007572#endif
7573 }
7574 vcol -= start_vcol;
7575
7576 /* Delete spaces that were inserted after the cursor to keep the
7577 * text aligned. */
7578 curwin->w_cursor.col += ins_len;
7579 while (vcol > orig_vcols && gchar_cursor() == ' ')
7580 {
7581 del_char(FALSE);
7582 ++orig_vcols;
7583 }
7584 curwin->w_cursor.col -= ins_len;
7585 }
7586#endif
7587
7588 /* mark the buffer as changed and prepare for displaying */
7589 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7590 }
7591 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007592 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007593}
7594
7595#ifdef FEAT_CINDENT
7596/*
7597 * Return TRUE if C-indenting is on.
7598 */
7599 static int
7600cindent_on()
7601{
7602 return (!p_paste && (curbuf->b_p_cin
7603# ifdef FEAT_EVAL
7604 || *curbuf->b_p_inde != NUL
7605# endif
7606 ));
7607}
7608#endif
7609
7610#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7611/*
7612 * Re-indent the current line, based on the current contents of it and the
7613 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7614 * confused what all the part that handles Control-T is doing that I'm not.
7615 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7616 */
7617
7618 void
7619fixthisline(get_the_indent)
7620 int (*get_the_indent) __ARGS((void));
7621{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007622 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007623 if (linewhite(curwin->w_cursor.lnum))
7624 did_ai = TRUE; /* delete the indent if the line stays empty */
7625}
7626
7627 void
7628fix_indent()
7629{
7630 if (p_paste)
7631 return;
7632# ifdef FEAT_LISP
7633 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7634 fixthisline(get_lisp_indent);
7635# endif
7636# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7637 else
7638# endif
7639# ifdef FEAT_CINDENT
7640 if (cindent_on())
7641 do_c_expr_indent();
7642# endif
7643}
7644
7645#endif
7646
7647#ifdef FEAT_CINDENT
7648/*
7649 * return TRUE if 'cinkeys' contains the key "keytyped",
7650 * when == '*': Only if key is preceded with '*' (indent before insert)
7651 * when == '!': Only if key is prededed with '!' (don't insert)
7652 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7653 *
7654 * "keytyped" can have a few special values:
7655 * KEY_OPEN_FORW
7656 * KEY_OPEN_BACK
7657 * KEY_COMPLETE just finished completion.
7658 *
7659 * If line_is_empty is TRUE accept keys with '0' before them.
7660 */
7661 int
7662in_cinkeys(keytyped, when, line_is_empty)
7663 int keytyped;
7664 int when;
7665 int line_is_empty;
7666{
7667 char_u *look;
7668 int try_match;
7669 int try_match_word;
7670 char_u *p;
7671 char_u *line;
7672 int icase;
7673 int i;
7674
Bram Moolenaar30848942009-12-24 14:46:12 +00007675 if (keytyped == NUL)
7676 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7677 return FALSE;
7678
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679#ifdef FEAT_EVAL
7680 if (*curbuf->b_p_inde != NUL)
7681 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7682 else
7683#endif
7684 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7685 while (*look)
7686 {
7687 /*
7688 * Find out if we want to try a match with this key, depending on
7689 * 'when' and a '*' or '!' before the key.
7690 */
7691 switch (when)
7692 {
7693 case '*': try_match = (*look == '*'); break;
7694 case '!': try_match = (*look == '!'); break;
7695 default: try_match = (*look != '*'); break;
7696 }
7697 if (*look == '*' || *look == '!')
7698 ++look;
7699
7700 /*
7701 * If there is a '0', only accept a match if the line is empty.
7702 * But may still match when typing last char of a word.
7703 */
7704 if (*look == '0')
7705 {
7706 try_match_word = try_match;
7707 if (!line_is_empty)
7708 try_match = FALSE;
7709 ++look;
7710 }
7711 else
7712 try_match_word = FALSE;
7713
7714 /*
7715 * does it look like a control character?
7716 */
7717 if (*look == '^'
7718#ifdef EBCDIC
7719 && (Ctrl_chr(look[1]) != 0)
7720#else
7721 && look[1] >= '?' && look[1] <= '_'
7722#endif
7723 )
7724 {
7725 if (try_match && keytyped == Ctrl_chr(look[1]))
7726 return TRUE;
7727 look += 2;
7728 }
7729 /*
7730 * 'o' means "o" command, open forward.
7731 * 'O' means "O" command, open backward.
7732 */
7733 else if (*look == 'o')
7734 {
7735 if (try_match && keytyped == KEY_OPEN_FORW)
7736 return TRUE;
7737 ++look;
7738 }
7739 else if (*look == 'O')
7740 {
7741 if (try_match && keytyped == KEY_OPEN_BACK)
7742 return TRUE;
7743 ++look;
7744 }
7745
7746 /*
7747 * 'e' means to check for "else" at start of line and just before the
7748 * cursor.
7749 */
7750 else if (*look == 'e')
7751 {
7752 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7753 {
7754 p = ml_get_curline();
7755 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7756 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7757 return TRUE;
7758 }
7759 ++look;
7760 }
7761
7762 /*
7763 * ':' only causes an indent if it is at the end of a label or case
7764 * statement, or when it was before typing the ':' (to fix
7765 * class::method for C++).
7766 */
7767 else if (*look == ':')
7768 {
7769 if (try_match && keytyped == ':')
7770 {
7771 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007772 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7773 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007774 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007775 /* Need to get the line again after cin_islabel(). */
7776 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007777 if (curwin->w_cursor.col > 2
7778 && p[curwin->w_cursor.col - 1] == ':'
7779 && p[curwin->w_cursor.col - 2] == ':')
7780 {
7781 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007782 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783 || cin_islabel(30));
7784 p = ml_get_curline();
7785 p[curwin->w_cursor.col - 1] = ':';
7786 if (i)
7787 return TRUE;
7788 }
7789 }
7790 ++look;
7791 }
7792
7793
7794 /*
7795 * Is it a key in <>, maybe?
7796 */
7797 else if (*look == '<')
7798 {
7799 if (try_match)
7800 {
7801 /*
7802 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7803 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7804 * >, *, : and ! keys if they really really want to.
7805 */
7806 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7807 && keytyped == look[1])
7808 return TRUE;
7809
7810 if (keytyped == get_special_key_code(look + 1))
7811 return TRUE;
7812 }
7813 while (*look && *look != '>')
7814 look++;
7815 while (*look == '>')
7816 look++;
7817 }
7818
7819 /*
7820 * Is it a word: "=word"?
7821 */
7822 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7823 {
7824 ++look;
7825 if (*look == '~')
7826 {
7827 icase = TRUE;
7828 ++look;
7829 }
7830 else
7831 icase = FALSE;
7832 p = vim_strchr(look, ',');
7833 if (p == NULL)
7834 p = look + STRLEN(look);
7835 if ((try_match || try_match_word)
7836 && curwin->w_cursor.col >= (colnr_T)(p - look))
7837 {
7838 int match = FALSE;
7839
7840#ifdef FEAT_INS_EXPAND
7841 if (keytyped == KEY_COMPLETE)
7842 {
7843 char_u *s;
7844
7845 /* Just completed a word, check if it starts with "look".
7846 * search back for the start of a word. */
7847 line = ml_get_curline();
7848# ifdef FEAT_MBYTE
7849 if (has_mbyte)
7850 {
7851 char_u *n;
7852
7853 for (s = line + curwin->w_cursor.col; s > line; s = n)
7854 {
7855 n = mb_prevptr(line, s);
7856 if (!vim_iswordp(n))
7857 break;
7858 }
7859 }
7860 else
7861# endif
7862 for (s = line + curwin->w_cursor.col; s > line; --s)
7863 if (!vim_iswordc(s[-1]))
7864 break;
7865 if (s + (p - look) <= line + curwin->w_cursor.col
7866 && (icase
7867 ? MB_STRNICMP(s, look, p - look)
7868 : STRNCMP(s, look, p - look)) == 0)
7869 match = TRUE;
7870 }
7871 else
7872#endif
7873 /* TODO: multi-byte */
7874 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7875 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7876 {
7877 line = ml_get_cursor();
7878 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7879 || !vim_iswordc(line[-(p - look) - 1]))
7880 && (icase
7881 ? MB_STRNICMP(line - (p - look), look, p - look)
7882 : STRNCMP(line - (p - look), look, p - look))
7883 == 0)
7884 match = TRUE;
7885 }
7886 if (match && try_match_word && !try_match)
7887 {
7888 /* "0=word": Check if there are only blanks before the
7889 * word. */
7890 line = ml_get_curline();
7891 if ((int)(skipwhite(line) - line) !=
7892 (int)(curwin->w_cursor.col - (p - look)))
7893 match = FALSE;
7894 }
7895 if (match)
7896 return TRUE;
7897 }
7898 look = p;
7899 }
7900
7901 /*
7902 * ok, it's a boring generic character.
7903 */
7904 else
7905 {
7906 if (try_match && *look == keytyped)
7907 return TRUE;
7908 ++look;
7909 }
7910
7911 /*
7912 * Skip over ", ".
7913 */
7914 look = skip_to_option_part(look);
7915 }
7916 return FALSE;
7917}
7918#endif /* FEAT_CINDENT */
7919
7920#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7921/*
7922 * Map Hebrew keyboard when in hkmap mode.
7923 */
7924 int
7925hkmap(c)
7926 int c;
7927{
7928 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7929 {
7930 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7931 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7932 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7933 static char_u map[26] =
7934 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7935 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7936 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7937 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7938 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7939 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7940 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7941 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7942 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7943
7944 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7945 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7946 /* '-1'='sofit' */
7947 else if (c == 'x')
7948 return 'X';
7949 else if (c == 'q')
7950 return '\''; /* {geresh}={'} */
7951 else if (c == 246)
7952 return ' '; /* \"o --> ' ' for a german keyboard */
7953 else if (c == 228)
7954 return ' '; /* \"a --> ' ' -- / -- */
7955 else if (c == 252)
7956 return ' '; /* \"u --> ' ' -- / -- */
7957#ifdef EBCDIC
7958 else if (islower(c))
7959#else
7960 /* NOTE: islower() does not do the right thing for us on Linux so we
7961 * do this the same was as 5.7 and previous, so it works correctly on
7962 * all systems. Specifically, the e.g. Delete and Arrow keys are
7963 * munged and won't work if e.g. searching for Hebrew text.
7964 */
7965 else if (c >= 'a' && c <= 'z')
7966#endif
7967 return (int)(map[CharOrdLow(c)] + p_aleph);
7968 else
7969 return c;
7970 }
7971 else
7972 {
7973 switch (c)
7974 {
7975 case '`': return ';';
7976 case '/': return '.';
7977 case '\'': return ',';
7978 case 'q': return '/';
7979 case 'w': return '\'';
7980
7981 /* Hebrew letters - set offset from 'a' */
7982 case ',': c = '{'; break;
7983 case '.': c = 'v'; break;
7984 case ';': c = 't'; break;
7985 default: {
7986 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7987
7988#ifdef EBCDIC
7989 /* see note about islower() above */
7990 if (!islower(c))
7991#else
7992 if (c < 'a' || c > 'z')
7993#endif
7994 return c;
7995 c = str[CharOrdLow(c)];
7996 break;
7997 }
7998 }
7999
8000 return (int)(CharOrdLow(c) + p_aleph);
8001 }
8002}
8003#endif
8004
8005 static void
8006ins_reg()
8007{
8008 int need_redraw = FALSE;
8009 int regname;
8010 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008011#ifdef FEAT_VISUAL
8012 int vis_active = VIsual_active;
8013#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014
8015 /*
8016 * If we are going to wait for a character, show a '"'.
8017 */
8018 pc_status = PC_STATUS_UNSET;
8019 if (redrawing() && !char_avail())
8020 {
8021 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00008022 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023
8024 edit_putchar('"', TRUE);
8025#ifdef FEAT_CMDL_INFO
8026 add_to_showcmd_c(Ctrl_R);
8027#endif
8028 }
8029
8030#ifdef USE_ON_FLY_SCROLL
8031 dont_scroll = TRUE; /* disallow scrolling here */
8032#endif
8033
8034 /*
8035 * Don't map the register name. This also prevents the mode message to be
8036 * deleted when ESC is hit.
8037 */
8038 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008039 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8042 {
8043 /* Get a third key for literal register insertion */
8044 literally = regname;
8045#ifdef FEAT_CMDL_INFO
8046 add_to_showcmd_c(literally);
8047#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008048 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 }
8051 --no_mapping;
8052
8053#ifdef FEAT_EVAL
8054 /*
8055 * Don't call u_sync() while getting the expression,
8056 * evaluating it or giving an error message for it!
8057 */
8058 ++no_u_sync;
8059 if (regname == '=')
8060 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008061# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008062 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008063# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008064 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008065# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008066 /* Restore the Input Method. */
8067 if (im_on)
8068 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008069# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008070 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008071 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8072 {
8073 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008076 else
8077 {
8078#endif
8079 if (literally == Ctrl_O || literally == Ctrl_P)
8080 {
8081 /* Append the command to the redo buffer. */
8082 AppendCharToRedobuff(Ctrl_R);
8083 AppendCharToRedobuff(literally);
8084 AppendCharToRedobuff(regname);
8085
8086 do_put(regname, BACKWARD, 1L,
8087 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8088 }
8089 else if (insert_reg(regname, literally) == FAIL)
8090 {
8091 vim_beep();
8092 need_redraw = TRUE; /* remove the '"' */
8093 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008094 else if (stop_insert_mode)
8095 /* When the '=' register was used and a function was invoked that
8096 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8097 * insert anything, need to remove the '"' */
8098 need_redraw = TRUE;
8099
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100#ifdef FEAT_EVAL
8101 }
8102 --no_u_sync;
8103#endif
8104#ifdef FEAT_CMDL_INFO
8105 clear_showcmd();
8106#endif
8107
8108 /* If the inserted register is empty, we need to remove the '"' */
8109 if (need_redraw || stuff_empty())
8110 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008111
8112#ifdef FEAT_VISUAL
8113 /* Disallow starting Visual mode here, would get a weird mode. */
8114 if (!vis_active && VIsual_active)
8115 end_visual_mode();
8116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117}
8118
8119/*
8120 * CTRL-G commands in Insert mode.
8121 */
8122 static void
8123ins_ctrl_g()
8124{
8125 int c;
8126
8127#ifdef FEAT_INS_EXPAND
8128 /* Right after CTRL-X the cursor will be after the ruler. */
8129 setcursor();
8130#endif
8131
8132 /*
8133 * Don't map the second key. This also prevents the mode message to be
8134 * deleted when ESC is hit.
8135 */
8136 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008137 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008138 --no_mapping;
8139 switch (c)
8140 {
8141 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8142 case K_UP:
8143 case Ctrl_K:
8144 case 'k': ins_up(TRUE);
8145 break;
8146
8147 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8148 case K_DOWN:
8149 case Ctrl_J:
8150 case 'j': ins_down(TRUE);
8151 break;
8152
8153 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008154 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008156
8157 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008158 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008159 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160 break;
8161
8162 /* Unknown CTRL-G command, reserved for future expansion. */
8163 default: vim_beep();
8164 }
8165}
8166
8167/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008168 * CTRL-^ in Insert mode.
8169 */
8170 static void
8171ins_ctrl_hat()
8172{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008173 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008174 {
8175 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8176 if (State & LANGMAP)
8177 {
8178 curbuf->b_p_iminsert = B_IMODE_NONE;
8179 State &= ~LANGMAP;
8180 }
8181 else
8182 {
8183 curbuf->b_p_iminsert = B_IMODE_LMAP;
8184 State |= LANGMAP;
8185#ifdef USE_IM_CONTROL
8186 im_set_active(FALSE);
8187#endif
8188 }
8189 }
8190#ifdef USE_IM_CONTROL
8191 else
8192 {
8193 /* There are no ":lmap" mappings, toggle IM */
8194 if (im_get_status())
8195 {
8196 curbuf->b_p_iminsert = B_IMODE_NONE;
8197 im_set_active(FALSE);
8198 }
8199 else
8200 {
8201 curbuf->b_p_iminsert = B_IMODE_IM;
8202 State &= ~LANGMAP;
8203 im_set_active(TRUE);
8204 }
8205 }
8206#endif
8207 set_iminsert_global();
8208 showmode();
8209#ifdef FEAT_GUI
8210 /* may show different cursor shape or color */
8211 if (gui.in_use)
8212 gui_update_cursor(TRUE, FALSE);
8213#endif
8214#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8215 /* Show/unshow value of 'keymap' in status lines. */
8216 status_redraw_curbuf();
8217#endif
8218}
8219
8220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 * Handle ESC in insert mode.
8222 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8223 * insert.
8224 */
8225 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008226ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 long *count;
8228 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008229 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008230{
8231 int temp;
8232 static int disabled_redraw = FALSE;
8233
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008234#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008235 check_spell_redraw();
8236#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237#if defined(FEAT_HANGULIN)
8238# if defined(ESC_CHG_TO_ENG_MODE)
8239 hangul_input_state_set(0);
8240# endif
8241 if (composing_hangul)
8242 {
8243 push_raw_key(composing_hangul_buffer, 2);
8244 composing_hangul = 0;
8245 }
8246#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247
8248 temp = curwin->w_cursor.col;
8249 if (disabled_redraw)
8250 {
8251 --RedrawingDisabled;
8252 disabled_redraw = FALSE;
8253 }
8254 if (!arrow_used)
8255 {
8256 /*
8257 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008258 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8259 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008260 */
8261 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008262 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263
8264 /*
8265 * Repeating insert may take a long time. Check for
8266 * interrupt now and then.
8267 */
8268 if (*count > 0)
8269 {
8270 line_breakcheck();
8271 if (got_int)
8272 *count = 0;
8273 }
8274
8275 if (--*count > 0) /* repeat what was typed */
8276 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008277 /* Vi repeats the insert without replacing characters. */
8278 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8279 State &= ~REPLACE_FLAG;
8280
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 (void)start_redo_ins();
8282 if (cmdchar == 'r' || cmdchar == 'v')
8283 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8284 ++RedrawingDisabled;
8285 disabled_redraw = TRUE;
8286 return FALSE; /* repeat the insert */
8287 }
8288 stop_insert(&curwin->w_cursor, TRUE);
8289 undisplay_dollar();
8290 }
8291
8292 /* When an autoindent was removed, curswant stays after the
8293 * indent */
8294 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8295 curwin->w_set_curswant = TRUE;
8296
8297 /* Remember the last Insert position in the '^ mark. */
8298 if (!cmdmod.keepjumps)
8299 curbuf->b_last_insert = curwin->w_cursor;
8300
8301 /*
8302 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008303 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008305 if (!nomove
8306 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307#ifdef FEAT_VIRTUALEDIT
8308 || curwin->w_cursor.coladd > 0
8309#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008310 )
8311 && (restart_edit == NUL
8312 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008314 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008316 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317#ifdef FEAT_RIGHTLEFT
8318 && !revins_on
8319#endif
8320 )
8321 {
8322#ifdef FEAT_VIRTUALEDIT
8323 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8324 {
8325 oneleft();
8326 if (restart_edit != NUL)
8327 ++curwin->w_cursor.coladd;
8328 }
8329 else
8330#endif
8331 {
8332 --curwin->w_cursor.col;
8333#ifdef FEAT_MBYTE
8334 /* Correct cursor for multi-byte character. */
8335 if (has_mbyte)
8336 mb_adjust_cursor();
8337#endif
8338 }
8339 }
8340
8341#ifdef USE_IM_CONTROL
8342 /* Disable IM to allow typing English directly for Normal mode commands.
8343 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8344 * well). */
8345 if (!(State & LANGMAP))
8346 im_save_status(&curbuf->b_p_iminsert);
8347 im_set_active(FALSE);
8348#endif
8349
8350 State = NORMAL;
8351 /* need to position cursor again (e.g. when on a TAB ) */
8352 changed_cline_bef_curs();
8353
8354#ifdef FEAT_MOUSE
8355 setmouse();
8356#endif
8357#ifdef CURSOR_SHAPE
8358 ui_cursor_shape(); /* may show different cursor shape */
8359#endif
8360
8361 /*
8362 * When recording or for CTRL-O, need to display the new mode.
8363 * Otherwise remove the mode message.
8364 */
8365 if (Recording || restart_edit != NUL)
8366 showmode();
8367 else if (p_smd)
8368 MSG("");
8369
8370 return TRUE; /* exit Insert mode */
8371}
8372
8373#ifdef FEAT_RIGHTLEFT
8374/*
8375 * Toggle language: hkmap and revins_on.
8376 * Move to end of reverse inserted text.
8377 */
8378 static void
8379ins_ctrl_()
8380{
8381 if (revins_on && revins_chars && revins_scol >= 0)
8382 {
8383 while (gchar_cursor() != NUL && revins_chars--)
8384 ++curwin->w_cursor.col;
8385 }
8386 p_ri = !p_ri;
8387 revins_on = (State == INSERT && p_ri);
8388 if (revins_on)
8389 {
8390 revins_scol = curwin->w_cursor.col;
8391 revins_legal++;
8392 revins_chars = 0;
8393 undisplay_dollar();
8394 }
8395 else
8396 revins_scol = -1;
8397#ifdef FEAT_FKMAP
8398 if (p_altkeymap)
8399 {
8400 /*
8401 * to be consistent also for redo command, using '.'
8402 * set arrow_used to true and stop it - causing to redo
8403 * characters entered in one mode (normal/reverse insert).
8404 */
8405 arrow_used = TRUE;
8406 (void)stop_arrow();
8407 p_fkmap = curwin->w_p_rl ^ p_ri;
8408 if (p_fkmap && p_ri)
8409 State = INSERT;
8410 }
8411 else
8412#endif
8413 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8414 showmode();
8415}
8416#endif
8417
8418#ifdef FEAT_VISUAL
8419/*
8420 * If 'keymodel' contains "startsel", may start selection.
8421 * Returns TRUE when a CTRL-O and other keys stuffed.
8422 */
8423 static int
8424ins_start_select(c)
8425 int c;
8426{
8427 if (km_startsel)
8428 switch (c)
8429 {
8430 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008432 case K_PAGEUP:
8433 case K_KPAGEUP:
8434 case K_PAGEDOWN:
8435 case K_KPAGEDOWN:
8436# ifdef MACOS
8437 case K_LEFT:
8438 case K_RIGHT:
8439 case K_UP:
8440 case K_DOWN:
8441 case K_END:
8442 case K_HOME:
8443# endif
8444 if (!(mod_mask & MOD_MASK_SHIFT))
8445 break;
8446 /* FALLTHROUGH */
8447 case K_S_LEFT:
8448 case K_S_RIGHT:
8449 case K_S_UP:
8450 case K_S_DOWN:
8451 case K_S_END:
8452 case K_S_HOME:
8453 /* Start selection right away, the cursor can move with
8454 * CTRL-O when beyond the end of the line. */
8455 start_selection();
8456
8457 /* Execute the key in (insert) Select mode. */
8458 stuffcharReadbuff(Ctrl_O);
8459 if (mod_mask)
8460 {
8461 char_u buf[4];
8462
8463 buf[0] = K_SPECIAL;
8464 buf[1] = KS_MODIFIER;
8465 buf[2] = mod_mask;
8466 buf[3] = NUL;
8467 stuffReadbuff(buf);
8468 }
8469 stuffcharReadbuff(c);
8470 return TRUE;
8471 }
8472 return FALSE;
8473}
8474#endif
8475
8476/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008477 * <Insert> key in Insert mode: toggle insert/remplace mode.
8478 */
8479 static void
8480ins_insert(replaceState)
8481 int replaceState;
8482{
8483#ifdef FEAT_FKMAP
8484 if (p_fkmap && p_ri)
8485 {
8486 beep_flush();
8487 EMSG(farsi_text_3); /* encoded in Farsi */
8488 return;
8489 }
8490#endif
8491
8492#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008493# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008494 set_vim_var_string(VV_INSERTMODE,
8495 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008496# ifdef FEAT_VREPLACE
8497 replaceState == VREPLACE ? "v" :
8498# endif
8499 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008500# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008501 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8502#endif
8503 if (State & REPLACE_FLAG)
8504 State = INSERT | (State & LANGMAP);
8505 else
8506 State = replaceState | (State & LANGMAP);
8507 AppendCharToRedobuff(K_INS);
8508 showmode();
8509#ifdef CURSOR_SHAPE
8510 ui_cursor_shape(); /* may show different cursor shape */
8511#endif
8512}
8513
8514/*
8515 * Pressed CTRL-O in Insert mode.
8516 */
8517 static void
8518ins_ctrl_o()
8519{
8520#ifdef FEAT_VREPLACE
8521 if (State & VREPLACE_FLAG)
8522 restart_edit = 'V';
8523 else
8524#endif
8525 if (State & REPLACE_FLAG)
8526 restart_edit = 'R';
8527 else
8528 restart_edit = 'I';
8529#ifdef FEAT_VIRTUALEDIT
8530 if (virtual_active())
8531 ins_at_eol = FALSE; /* cursor always keeps its column */
8532 else
8533#endif
8534 ins_at_eol = (gchar_cursor() == NUL);
8535}
8536
8537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 * If the cursor is on an indent, ^T/^D insert/delete one
8539 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008540 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 * with vi. But vi only supports ^T and ^D after an
8542 * autoindent, we support it everywhere.
8543 */
8544 static void
8545ins_shift(c, lastc)
8546 int c;
8547 int lastc;
8548{
8549 if (stop_arrow() == FAIL)
8550 return;
8551 AppendCharToRedobuff(c);
8552
8553 /*
8554 * 0^D and ^^D: remove all indent.
8555 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008556 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8557 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008558 {
8559 --curwin->w_cursor.col;
8560 (void)del_char(FALSE); /* delete the '^' or '0' */
8561 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8562 if (State & REPLACE_FLAG)
8563 replace_pop_ins();
8564 if (lastc == '^')
8565 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008566 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567 }
8568 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008569 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570
8571 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8572 did_ai = FALSE;
8573#ifdef FEAT_SMARTINDENT
8574 did_si = FALSE;
8575 can_si = FALSE;
8576 can_si_back = FALSE;
8577#endif
8578#ifdef FEAT_CINDENT
8579 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8580#endif
8581}
8582
8583 static void
8584ins_del()
8585{
8586 int temp;
8587
8588 if (stop_arrow() == FAIL)
8589 return;
8590 if (gchar_cursor() == NUL) /* delete newline */
8591 {
8592 temp = curwin->w_cursor.col;
8593 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar81340392012-06-06 16:12:59 +02008594 || do_join(2, FALSE, TRUE, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008595 vim_beep();
8596 else
8597 curwin->w_cursor.col = temp;
8598 }
8599 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8600 vim_beep();
8601 did_ai = FALSE;
8602#ifdef FEAT_SMARTINDENT
8603 did_si = FALSE;
8604 can_si = FALSE;
8605 can_si_back = FALSE;
8606#endif
8607 AppendCharToRedobuff(K_DEL);
8608}
8609
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008610static void ins_bs_one __ARGS((colnr_T *vcolp));
8611
8612/*
8613 * Delete one character for ins_bs().
8614 */
8615 static void
8616ins_bs_one(vcolp)
8617 colnr_T *vcolp;
8618{
8619 dec_cursor();
8620 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8621 if (State & REPLACE_FLAG)
8622 {
8623 /* Don't delete characters before the insert point when in
8624 * Replace mode */
8625 if (curwin->w_cursor.lnum != Insstart.lnum
8626 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008627 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008628 }
8629 else
8630 (void)del_char(FALSE);
8631}
8632
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633/*
8634 * Handle Backspace, delete-word and delete-line in Insert mode.
8635 * Return TRUE when backspace was actually used.
8636 */
8637 static int
8638ins_bs(c, mode, inserted_space_p)
8639 int c;
8640 int mode;
8641 int *inserted_space_p;
8642{
8643 linenr_T lnum;
8644 int cc;
8645 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008646 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 colnr_T mincol;
8648 int did_backspace = FALSE;
8649 int in_indent;
8650 int oldState;
8651#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008652 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653#endif
8654
8655 /*
8656 * can't delete anything in an empty file
8657 * can't backup past first character in buffer
8658 * can't backup past starting point unless 'backspace' > 1
8659 * can backup to a previous line if 'backspace' == 0
8660 */
8661 if ( bufempty()
8662 || (
8663#ifdef FEAT_RIGHTLEFT
8664 !revins_on &&
8665#endif
8666 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8667 || (!can_bs(BS_START)
8668 && (arrow_used
8669 || (curwin->w_cursor.lnum == Insstart.lnum
8670 && curwin->w_cursor.col <= Insstart.col)))
8671 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8672 && curwin->w_cursor.col <= ai_col)
8673 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8674 {
8675 vim_beep();
8676 return FALSE;
8677 }
8678
8679 if (stop_arrow() == FAIL)
8680 return FALSE;
8681 in_indent = inindent(0);
8682#ifdef FEAT_CINDENT
8683 if (in_indent)
8684 can_cindent = FALSE;
8685#endif
8686#ifdef FEAT_COMMENTS
8687 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8688#endif
8689#ifdef FEAT_RIGHTLEFT
8690 if (revins_on) /* put cursor after last inserted char */
8691 inc_cursor();
8692#endif
8693
8694#ifdef FEAT_VIRTUALEDIT
8695 /* Virtualedit:
8696 * BACKSPACE_CHAR eats a virtual space
8697 * BACKSPACE_WORD eats all coladd
8698 * BACKSPACE_LINE eats all coladd and keeps going
8699 */
8700 if (curwin->w_cursor.coladd > 0)
8701 {
8702 if (mode == BACKSPACE_CHAR)
8703 {
8704 --curwin->w_cursor.coladd;
8705 return TRUE;
8706 }
8707 if (mode == BACKSPACE_WORD)
8708 {
8709 curwin->w_cursor.coladd = 0;
8710 return TRUE;
8711 }
8712 curwin->w_cursor.coladd = 0;
8713 }
8714#endif
8715
8716 /*
8717 * delete newline!
8718 */
8719 if (curwin->w_cursor.col == 0)
8720 {
8721 lnum = Insstart.lnum;
8722 if (curwin->w_cursor.lnum == Insstart.lnum
8723#ifdef FEAT_RIGHTLEFT
8724 || revins_on
8725#endif
8726 )
8727 {
8728 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8729 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8730 return FALSE;
8731 --Insstart.lnum;
8732 Insstart.col = MAXCOL;
8733 }
8734 /*
8735 * In replace mode:
8736 * cc < 0: NL was inserted, delete it
8737 * cc >= 0: NL was replaced, put original characters back
8738 */
8739 cc = -1;
8740 if (State & REPLACE_FLAG)
8741 cc = replace_pop(); /* returns -1 if NL was inserted */
8742 /*
8743 * In replace mode, in the line we started replacing, we only move the
8744 * cursor.
8745 */
8746 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8747 {
8748 dec_cursor();
8749 }
8750 else
8751 {
8752#ifdef FEAT_VREPLACE
8753 if (!(State & VREPLACE_FLAG)
8754 || curwin->w_cursor.lnum > orig_line_count)
8755#endif
8756 {
8757 temp = gchar_cursor(); /* remember current char */
8758 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008759
8760 /* When "aw" is in 'formatoptions' we must delete the space at
8761 * the end of the line, otherwise the line will be broken
8762 * again when auto-formatting. */
8763 if (has_format_option(FO_AUTO)
8764 && has_format_option(FO_WHITE_PAR))
8765 {
8766 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8767 TRUE);
8768 int len;
8769
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008770 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008771 if (len > 0 && ptr[len - 1] == ' ')
8772 ptr[len - 1] = NUL;
8773 }
8774
Bram Moolenaar81340392012-06-06 16:12:59 +02008775 (void)do_join(2, FALSE, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008776 if (temp == NUL && gchar_cursor() != NUL)
8777 inc_cursor();
8778 }
8779#ifdef FEAT_VREPLACE
8780 else
8781 dec_cursor();
8782#endif
8783
8784 /*
8785 * In REPLACE mode we have to put back the text that was replaced
8786 * by the NL. On the replace stack is first a NUL-terminated
8787 * sequence of characters that were deleted and then the
8788 * characters that NL replaced.
8789 */
8790 if (State & REPLACE_FLAG)
8791 {
8792 /*
8793 * Do the next ins_char() in NORMAL state, to
8794 * prevent ins_char() from replacing characters and
8795 * avoiding showmatch().
8796 */
8797 oldState = State;
8798 State = NORMAL;
8799 /*
8800 * restore characters (blanks) deleted after cursor
8801 */
8802 while (cc > 0)
8803 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008804 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008805#ifdef FEAT_MBYTE
8806 mb_replace_pop_ins(cc);
8807#else
8808 ins_char(cc);
8809#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008810 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811 cc = replace_pop();
8812 }
8813 /* restore the characters that NL replaced */
8814 replace_pop_ins();
8815 State = oldState;
8816 }
8817 }
8818 did_ai = FALSE;
8819 }
8820 else
8821 {
8822 /*
8823 * Delete character(s) before the cursor.
8824 */
8825#ifdef FEAT_RIGHTLEFT
8826 if (revins_on) /* put cursor on last inserted char */
8827 dec_cursor();
8828#endif
8829 mincol = 0;
8830 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008831 if (mode == BACKSPACE_LINE
8832 && (curbuf->b_p_ai
8833#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008834 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008835#endif
8836 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837#ifdef FEAT_RIGHTLEFT
8838 && !revins_on
8839#endif
8840 )
8841 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008842 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008844 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008846 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 }
8848
8849 /*
8850 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8851 */
8852 if ( mode == BACKSPACE_CHAR
8853 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008854 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008855 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 && (*(ml_get_cursor() - 1) == TAB
8857 || (*(ml_get_cursor() - 1) == ' '
8858 && (!*inserted_space_p
8859 || arrow_used))))))
8860 {
8861 int ts;
8862 colnr_T vcol;
8863 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008864 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865
8866 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008867 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 ts = curbuf->b_p_sw;
8869 else
8870 ts = curbuf->b_p_sts;
8871 /* Compute the virtual column where we want to be. Since
8872 * 'showbreak' may get in the way, need to get the last column of
8873 * the previous character. */
8874 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008875 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 dec_cursor();
8877 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8878 inc_cursor();
8879 want_vcol = (want_vcol / ts) * ts;
8880
8881 /* delete characters until we are at or before want_vcol */
8882 while (vcol > want_vcol
8883 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008884 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885
8886 /* insert extra spaces until we are at want_vcol */
8887 while (vcol < want_vcol)
8888 {
8889 /* Remember the first char we inserted */
8890 if (curwin->w_cursor.lnum == Insstart.lnum
8891 && curwin->w_cursor.col < Insstart.col)
8892 Insstart.col = curwin->w_cursor.col;
8893
8894#ifdef FEAT_VREPLACE
8895 if (State & VREPLACE_FLAG)
8896 ins_char(' ');
8897 else
8898#endif
8899 {
8900 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008901 if ((State & REPLACE_FLAG))
8902 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 }
8904 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8905 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008906
8907 /* If we are now back where we started delete one character. Can
8908 * happen when using 'sts' and 'linebreak'. */
8909 if (vcol >= start_vcol)
8910 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008911 }
8912
8913 /*
8914 * Delete upto starting point, start of line or previous word.
8915 */
8916 else do
8917 {
8918#ifdef FEAT_RIGHTLEFT
8919 if (!revins_on) /* put cursor on char to be deleted */
8920#endif
8921 dec_cursor();
8922
8923 /* start of word? */
8924 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8925 {
8926 mode = BACKSPACE_WORD_NOT_SPACE;
8927 temp = vim_iswordc(gchar_cursor());
8928 }
8929 /* end of word? */
8930 else if (mode == BACKSPACE_WORD_NOT_SPACE
8931 && (vim_isspace(cc = gchar_cursor())
8932 || vim_iswordc(cc) != temp))
8933 {
8934#ifdef FEAT_RIGHTLEFT
8935 if (!revins_on)
8936#endif
8937 inc_cursor();
8938#ifdef FEAT_RIGHTLEFT
8939 else if (State & REPLACE_FLAG)
8940 dec_cursor();
8941#endif
8942 break;
8943 }
8944 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008945 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 else
8947 {
8948#ifdef FEAT_MBYTE
8949 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008950 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951#endif
8952 (void)del_char(FALSE);
8953#ifdef FEAT_MBYTE
8954 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008955 * If there are combining characters and 'delcombine' is set
8956 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 * character.
8958 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008959 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008960 inc_cursor();
8961#endif
8962#ifdef FEAT_RIGHTLEFT
8963 if (revins_chars)
8964 {
8965 revins_chars--;
8966 revins_legal++;
8967 }
8968 if (revins_on && gchar_cursor() == NUL)
8969 break;
8970#endif
8971 }
8972 /* Just a single backspace?: */
8973 if (mode == BACKSPACE_CHAR)
8974 break;
8975 } while (
8976#ifdef FEAT_RIGHTLEFT
8977 revins_on ||
8978#endif
8979 (curwin->w_cursor.col > mincol
8980 && (curwin->w_cursor.lnum != Insstart.lnum
8981 || curwin->w_cursor.col != Insstart.col)));
8982 did_backspace = TRUE;
8983 }
8984#ifdef FEAT_SMARTINDENT
8985 did_si = FALSE;
8986 can_si = FALSE;
8987 can_si_back = FALSE;
8988#endif
8989 if (curwin->w_cursor.col <= 1)
8990 did_ai = FALSE;
8991 /*
8992 * It's a little strange to put backspaces into the redo
8993 * buffer, but it makes auto-indent a lot easier to deal
8994 * with.
8995 */
8996 AppendCharToRedobuff(c);
8997
8998 /* If deleted before the insertion point, adjust it */
8999 if (curwin->w_cursor.lnum == Insstart.lnum
9000 && curwin->w_cursor.col < Insstart.col)
9001 Insstart.col = curwin->w_cursor.col;
9002
9003 /* vi behaviour: the cursor moves backward but the character that
9004 * was there remains visible
9005 * Vim behaviour: the cursor moves backward and the character that
9006 * was there is erased from the screen.
9007 * We can emulate the vi behaviour by pretending there is a dollar
9008 * displayed even when there isn't.
9009 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01009010 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009011 dollar_vcol = curwin->w_virtcol;
9012
Bram Moolenaarce3be472008-01-14 19:12:28 +00009013#ifdef FEAT_FOLDING
9014 /* When deleting a char the cursor line must never be in a closed fold.
9015 * E.g., when 'foldmethod' is indent and deleting the first non-white
9016 * char before a Tab. */
9017 if (did_backspace)
9018 foldOpenCursor();
9019#endif
9020
Bram Moolenaar071d4272004-06-13 20:20:40 +00009021 return did_backspace;
9022}
9023
9024#ifdef FEAT_MOUSE
9025 static void
9026ins_mouse(c)
9027 int c;
9028{
9029 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009030 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009031
9032# ifdef FEAT_GUI
9033 /* When GUI is active, also move/paste when 'mouse' is empty */
9034 if (!gui.in_use)
9035# endif
9036 if (!mouse_has(MOUSE_INSERT))
9037 return;
9038
9039 undisplay_dollar();
9040 tpos = curwin->w_cursor;
9041 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9042 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009043#ifdef FEAT_WINDOWS
9044 win_T *new_curwin = curwin;
9045
9046 if (curwin != old_curwin && win_valid(old_curwin))
9047 {
9048 /* Mouse took us to another window. We need to go back to the
9049 * previous one to stop insert there properly. */
9050 curwin = old_curwin;
9051 curbuf = curwin->w_buffer;
9052 }
9053#endif
9054 start_arrow(curwin == old_curwin ? &tpos : NULL);
9055#ifdef FEAT_WINDOWS
9056 if (curwin != new_curwin && win_valid(new_curwin))
9057 {
9058 curwin = new_curwin;
9059 curbuf = curwin->w_buffer;
9060 }
9061#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062# ifdef FEAT_CINDENT
9063 can_cindent = TRUE;
9064# endif
9065 }
9066
9067#ifdef FEAT_WINDOWS
9068 /* redraw status lines (in case another window became active) */
9069 redraw_statuslines();
9070#endif
9071}
9072
9073 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009074ins_mousescroll(dir)
9075 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076{
9077 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009078# if defined(FEAT_WINDOWS)
9079 win_T *old_curwin = curwin;
9080# endif
9081# ifdef FEAT_INS_EXPAND
9082 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083# endif
9084
9085 tpos = curwin->w_cursor;
9086
9087# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009088 /* Currently the mouse coordinates are only known in the GUI. */
9089 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
9090 {
9091 int row, col;
9092
9093 row = mouse_row;
9094 col = mouse_col;
9095
9096 /* find the window at the pointer coordinates */
9097 curwin = mouse_find_win(&row, &col);
9098 curbuf = curwin->w_buffer;
9099 }
9100 if (curwin == old_curwin)
9101# endif
9102 undisplay_dollar();
9103
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009104# ifdef FEAT_INS_EXPAND
9105 /* Don't scroll the window in which completion is being done. */
9106 if (!pum_visible()
9107# if defined(FEAT_WINDOWS)
9108 || curwin != old_curwin
9109# endif
9110 )
9111# endif
9112 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009113 if (dir == MSCR_DOWN || dir == MSCR_UP)
9114 {
9115 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9116 scroll_redraw(dir,
9117 (long)(curwin->w_botline - curwin->w_topline));
9118 else
9119 scroll_redraw(dir, 3L);
9120 }
9121#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009122 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009123 {
9124 int val, step = 6;
9125
9126 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9127 step = W_WIDTH(curwin);
9128 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9129 if (val < 0)
9130 val = 0;
9131 gui_do_horiz_scroll(val, TRUE);
9132 }
9133#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009134# ifdef FEAT_INS_EXPAND
9135 did_scroll = TRUE;
9136# endif
9137 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138
9139# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
9140 curwin->w_redr_status = TRUE;
9141
9142 curwin = old_curwin;
9143 curbuf = curwin->w_buffer;
9144# endif
9145
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009146# ifdef FEAT_INS_EXPAND
9147 /* The popup menu may overlay the window, need to redraw it.
9148 * TODO: Would be more efficient to only redraw the windows that are
9149 * overlapped by the popup menu. */
9150 if (pum_visible() && did_scroll)
9151 {
9152 redraw_all_later(NOT_VALID);
9153 ins_compl_show_pum();
9154 }
9155# endif
9156
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 if (!equalpos(curwin->w_cursor, tpos))
9158 {
9159 start_arrow(&tpos);
9160# ifdef FEAT_CINDENT
9161 can_cindent = TRUE;
9162# endif
9163 }
9164}
9165#endif
9166
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009167#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009168 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009169ins_tabline(c)
9170 int c;
9171{
9172 /* We will be leaving the current window, unless closing another tab. */
9173 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9174 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9175 {
9176 undisplay_dollar();
9177 start_arrow(&curwin->w_cursor);
9178# ifdef FEAT_CINDENT
9179 can_cindent = TRUE;
9180# endif
9181 }
9182
9183 if (c == K_TABLINE)
9184 goto_tabpage(current_tab);
9185 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009186 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009187 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009188 redraw_statuslines(); /* will redraw the tabline when needed */
9189 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009190}
9191#endif
9192
9193#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194 void
9195ins_scroll()
9196{
9197 pos_T tpos;
9198
9199 undisplay_dollar();
9200 tpos = curwin->w_cursor;
9201 if (gui_do_scroll())
9202 {
9203 start_arrow(&tpos);
9204# ifdef FEAT_CINDENT
9205 can_cindent = TRUE;
9206# endif
9207 }
9208}
9209
9210 void
9211ins_horscroll()
9212{
9213 pos_T tpos;
9214
9215 undisplay_dollar();
9216 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009217 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009218 {
9219 start_arrow(&tpos);
9220# ifdef FEAT_CINDENT
9221 can_cindent = TRUE;
9222# endif
9223 }
9224}
9225#endif
9226
9227 static void
9228ins_left()
9229{
9230 pos_T tpos;
9231
9232#ifdef FEAT_FOLDING
9233 if ((fdo_flags & FDO_HOR) && KeyTyped)
9234 foldOpenCursor();
9235#endif
9236 undisplay_dollar();
9237 tpos = curwin->w_cursor;
9238 if (oneleft() == OK)
9239 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009240#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9241 /* Only call start_arrow() when not busy with preediting, it will
9242 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9243 if (!im_is_preediting())
9244#endif
9245 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246#ifdef FEAT_RIGHTLEFT
9247 /* If exit reversed string, position is fixed */
9248 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9249 revins_legal++;
9250 revins_chars++;
9251#endif
9252 }
9253
9254 /*
9255 * if 'whichwrap' set for cursor in insert mode may go to
9256 * previous line
9257 */
9258 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9259 {
9260 start_arrow(&tpos);
9261 --(curwin->w_cursor.lnum);
9262 coladvance((colnr_T)MAXCOL);
9263 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9264 }
9265 else
9266 vim_beep();
9267}
9268
9269 static void
9270ins_home(c)
9271 int c;
9272{
9273 pos_T tpos;
9274
9275#ifdef FEAT_FOLDING
9276 if ((fdo_flags & FDO_HOR) && KeyTyped)
9277 foldOpenCursor();
9278#endif
9279 undisplay_dollar();
9280 tpos = curwin->w_cursor;
9281 if (c == K_C_HOME)
9282 curwin->w_cursor.lnum = 1;
9283 curwin->w_cursor.col = 0;
9284#ifdef FEAT_VIRTUALEDIT
9285 curwin->w_cursor.coladd = 0;
9286#endif
9287 curwin->w_curswant = 0;
9288 start_arrow(&tpos);
9289}
9290
9291 static void
9292ins_end(c)
9293 int c;
9294{
9295 pos_T tpos;
9296
9297#ifdef FEAT_FOLDING
9298 if ((fdo_flags & FDO_HOR) && KeyTyped)
9299 foldOpenCursor();
9300#endif
9301 undisplay_dollar();
9302 tpos = curwin->w_cursor;
9303 if (c == K_C_END)
9304 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9305 coladvance((colnr_T)MAXCOL);
9306 curwin->w_curswant = MAXCOL;
9307
9308 start_arrow(&tpos);
9309}
9310
9311 static void
9312ins_s_left()
9313{
9314#ifdef FEAT_FOLDING
9315 if ((fdo_flags & FDO_HOR) && KeyTyped)
9316 foldOpenCursor();
9317#endif
9318 undisplay_dollar();
9319 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9320 {
9321 start_arrow(&curwin->w_cursor);
9322 (void)bck_word(1L, FALSE, FALSE);
9323 curwin->w_set_curswant = TRUE;
9324 }
9325 else
9326 vim_beep();
9327}
9328
9329 static void
9330ins_right()
9331{
9332#ifdef FEAT_FOLDING
9333 if ((fdo_flags & FDO_HOR) && KeyTyped)
9334 foldOpenCursor();
9335#endif
9336 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009337 if (gchar_cursor() != NUL
9338#ifdef FEAT_VIRTUALEDIT
9339 || virtual_active()
9340#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009341 )
9342 {
9343 start_arrow(&curwin->w_cursor);
9344 curwin->w_set_curswant = TRUE;
9345#ifdef FEAT_VIRTUALEDIT
9346 if (virtual_active())
9347 oneright();
9348 else
9349#endif
9350 {
9351#ifdef FEAT_MBYTE
9352 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009353 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009354 else
9355#endif
9356 ++curwin->w_cursor.col;
9357 }
9358
9359#ifdef FEAT_RIGHTLEFT
9360 revins_legal++;
9361 if (revins_chars)
9362 revins_chars--;
9363#endif
9364 }
9365 /* if 'whichwrap' set for cursor in insert mode, may move the
9366 * cursor to the next line */
9367 else if (vim_strchr(p_ww, ']') != NULL
9368 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9369 {
9370 start_arrow(&curwin->w_cursor);
9371 curwin->w_set_curswant = TRUE;
9372 ++curwin->w_cursor.lnum;
9373 curwin->w_cursor.col = 0;
9374 }
9375 else
9376 vim_beep();
9377}
9378
9379 static void
9380ins_s_right()
9381{
9382#ifdef FEAT_FOLDING
9383 if ((fdo_flags & FDO_HOR) && KeyTyped)
9384 foldOpenCursor();
9385#endif
9386 undisplay_dollar();
9387 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9388 || gchar_cursor() != NUL)
9389 {
9390 start_arrow(&curwin->w_cursor);
9391 (void)fwd_word(1L, FALSE, 0);
9392 curwin->w_set_curswant = TRUE;
9393 }
9394 else
9395 vim_beep();
9396}
9397
9398 static void
9399ins_up(startcol)
9400 int startcol; /* when TRUE move to Insstart.col */
9401{
9402 pos_T tpos;
9403 linenr_T old_topline = curwin->w_topline;
9404#ifdef FEAT_DIFF
9405 int old_topfill = curwin->w_topfill;
9406#endif
9407
9408 undisplay_dollar();
9409 tpos = curwin->w_cursor;
9410 if (cursor_up(1L, TRUE) == OK)
9411 {
9412 if (startcol)
9413 coladvance(getvcol_nolist(&Insstart));
9414 if (old_topline != curwin->w_topline
9415#ifdef FEAT_DIFF
9416 || old_topfill != curwin->w_topfill
9417#endif
9418 )
9419 redraw_later(VALID);
9420 start_arrow(&tpos);
9421#ifdef FEAT_CINDENT
9422 can_cindent = TRUE;
9423#endif
9424 }
9425 else
9426 vim_beep();
9427}
9428
9429 static void
9430ins_pageup()
9431{
9432 pos_T tpos;
9433
9434 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009435
9436#ifdef FEAT_WINDOWS
9437 if (mod_mask & MOD_MASK_CTRL)
9438 {
9439 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009440 if (first_tabpage->tp_next != NULL)
9441 {
9442 start_arrow(&curwin->w_cursor);
9443 goto_tabpage(-1);
9444 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009445 return;
9446 }
9447#endif
9448
Bram Moolenaar071d4272004-06-13 20:20:40 +00009449 tpos = curwin->w_cursor;
9450 if (onepage(BACKWARD, 1L) == OK)
9451 {
9452 start_arrow(&tpos);
9453#ifdef FEAT_CINDENT
9454 can_cindent = TRUE;
9455#endif
9456 }
9457 else
9458 vim_beep();
9459}
9460
9461 static void
9462ins_down(startcol)
9463 int startcol; /* when TRUE move to Insstart.col */
9464{
9465 pos_T tpos;
9466 linenr_T old_topline = curwin->w_topline;
9467#ifdef FEAT_DIFF
9468 int old_topfill = curwin->w_topfill;
9469#endif
9470
9471 undisplay_dollar();
9472 tpos = curwin->w_cursor;
9473 if (cursor_down(1L, TRUE) == OK)
9474 {
9475 if (startcol)
9476 coladvance(getvcol_nolist(&Insstart));
9477 if (old_topline != curwin->w_topline
9478#ifdef FEAT_DIFF
9479 || old_topfill != curwin->w_topfill
9480#endif
9481 )
9482 redraw_later(VALID);
9483 start_arrow(&tpos);
9484#ifdef FEAT_CINDENT
9485 can_cindent = TRUE;
9486#endif
9487 }
9488 else
9489 vim_beep();
9490}
9491
9492 static void
9493ins_pagedown()
9494{
9495 pos_T tpos;
9496
9497 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009498
9499#ifdef FEAT_WINDOWS
9500 if (mod_mask & MOD_MASK_CTRL)
9501 {
9502 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009503 if (first_tabpage->tp_next != NULL)
9504 {
9505 start_arrow(&curwin->w_cursor);
9506 goto_tabpage(0);
9507 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009508 return;
9509 }
9510#endif
9511
Bram Moolenaar071d4272004-06-13 20:20:40 +00009512 tpos = curwin->w_cursor;
9513 if (onepage(FORWARD, 1L) == OK)
9514 {
9515 start_arrow(&tpos);
9516#ifdef FEAT_CINDENT
9517 can_cindent = TRUE;
9518#endif
9519 }
9520 else
9521 vim_beep();
9522}
9523
9524#ifdef FEAT_DND
9525 static void
9526ins_drop()
9527{
9528 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9529}
9530#endif
9531
9532/*
9533 * Handle TAB in Insert or Replace mode.
9534 * Return TRUE when the TAB needs to be inserted like a normal character.
9535 */
9536 static int
9537ins_tab()
9538{
9539 int ind;
9540 int i;
9541 int temp;
9542
9543 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9544 Insstart_blank_vcol = get_nolist_virtcol();
9545 if (echeck_abbr(TAB + ABBR_OFF))
9546 return FALSE;
9547
9548 ind = inindent(0);
9549#ifdef FEAT_CINDENT
9550 if (ind)
9551 can_cindent = FALSE;
9552#endif
9553
9554 /*
9555 * When nothing special, insert TAB like a normal character
9556 */
9557 if (!curbuf->b_p_et
9558 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9559 && curbuf->b_p_sts == 0)
9560 return TRUE;
9561
9562 if (stop_arrow() == FAIL)
9563 return TRUE;
9564
9565 did_ai = FALSE;
9566#ifdef FEAT_SMARTINDENT
9567 did_si = FALSE;
9568 can_si = FALSE;
9569 can_si_back = FALSE;
9570#endif
9571 AppendToRedobuff((char_u *)"\t");
9572
9573 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9574 temp = (int)curbuf->b_p_sw;
9575 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9576 temp = (int)curbuf->b_p_sts;
9577 else /* otherwise use 'tabstop' */
9578 temp = (int)curbuf->b_p_ts;
9579 temp -= get_nolist_virtcol() % temp;
9580
9581 /*
9582 * Insert the first space with ins_char(). It will delete one char in
9583 * replace mode. Insert the rest with ins_str(); it will not delete any
9584 * chars. For VREPLACE mode, we use ins_char() for all characters.
9585 */
9586 ins_char(' ');
9587 while (--temp > 0)
9588 {
9589#ifdef FEAT_VREPLACE
9590 if (State & VREPLACE_FLAG)
9591 ins_char(' ');
9592 else
9593#endif
9594 {
9595 ins_str((char_u *)" ");
9596 if (State & REPLACE_FLAG) /* no char replaced */
9597 replace_push(NUL);
9598 }
9599 }
9600
9601 /*
9602 * When 'expandtab' not set: Replace spaces by TABs where possible.
9603 */
9604 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9605 {
9606 char_u *ptr;
9607#ifdef FEAT_VREPLACE
9608 char_u *saved_line = NULL; /* init for GCC */
9609 pos_T pos;
9610#endif
9611 pos_T fpos;
9612 pos_T *cursor;
9613 colnr_T want_vcol, vcol;
9614 int change_col = -1;
9615 int save_list = curwin->w_p_list;
9616
9617 /*
9618 * Get the current line. For VREPLACE mode, don't make real changes
9619 * yet, just work on a copy of the line.
9620 */
9621#ifdef FEAT_VREPLACE
9622 if (State & VREPLACE_FLAG)
9623 {
9624 pos = curwin->w_cursor;
9625 cursor = &pos;
9626 saved_line = vim_strsave(ml_get_curline());
9627 if (saved_line == NULL)
9628 return FALSE;
9629 ptr = saved_line + pos.col;
9630 }
9631 else
9632#endif
9633 {
9634 ptr = ml_get_cursor();
9635 cursor = &curwin->w_cursor;
9636 }
9637
9638 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9639 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9640 curwin->w_p_list = FALSE;
9641
9642 /* Find first white before the cursor */
9643 fpos = curwin->w_cursor;
9644 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9645 {
9646 --fpos.col;
9647 --ptr;
9648 }
9649
9650 /* In Replace mode, don't change characters before the insert point. */
9651 if ((State & REPLACE_FLAG)
9652 && fpos.lnum == Insstart.lnum
9653 && fpos.col < Insstart.col)
9654 {
9655 ptr += Insstart.col - fpos.col;
9656 fpos.col = Insstart.col;
9657 }
9658
9659 /* compute virtual column numbers of first white and cursor */
9660 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9661 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9662
9663 /* Use as many TABs as possible. Beware of 'showbreak' and
9664 * 'linebreak' adding extra virtual columns. */
9665 while (vim_iswhite(*ptr))
9666 {
9667 i = lbr_chartabsize((char_u *)"\t", vcol);
9668 if (vcol + i > want_vcol)
9669 break;
9670 if (*ptr != TAB)
9671 {
9672 *ptr = TAB;
9673 if (change_col < 0)
9674 {
9675 change_col = fpos.col; /* Column of first change */
9676 /* May have to adjust Insstart */
9677 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9678 Insstart.col = fpos.col;
9679 }
9680 }
9681 ++fpos.col;
9682 ++ptr;
9683 vcol += i;
9684 }
9685
9686 if (change_col >= 0)
9687 {
9688 int repl_off = 0;
9689
9690 /* Skip over the spaces we need. */
9691 while (vcol < want_vcol && *ptr == ' ')
9692 {
9693 vcol += lbr_chartabsize(ptr, vcol);
9694 ++ptr;
9695 ++repl_off;
9696 }
9697 if (vcol > want_vcol)
9698 {
9699 /* Must have a char with 'showbreak' just before it. */
9700 --ptr;
9701 --repl_off;
9702 }
9703 fpos.col += repl_off;
9704
9705 /* Delete following spaces. */
9706 i = cursor->col - fpos.col;
9707 if (i > 0)
9708 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009709 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 /* correct replace stack. */
9711 if ((State & REPLACE_FLAG)
9712#ifdef FEAT_VREPLACE
9713 && !(State & VREPLACE_FLAG)
9714#endif
9715 )
9716 for (temp = i; --temp >= 0; )
9717 replace_join(repl_off);
9718 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009719#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009720 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009721 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009722 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009723 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9724 (char_u *)"\t", 1);
9725 }
9726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 cursor->col -= i;
9728
9729#ifdef FEAT_VREPLACE
9730 /*
9731 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9732 * backspacing over the changed spacing and then inserting the new
9733 * spacing.
9734 */
9735 if (State & VREPLACE_FLAG)
9736 {
9737 /* Backspace from real cursor to change_col */
9738 backspace_until_column(change_col);
9739
9740 /* Insert each char in saved_line from changed_col to
9741 * ptr-cursor */
9742 ins_bytes_len(saved_line + change_col,
9743 cursor->col - change_col);
9744 }
9745#endif
9746 }
9747
9748#ifdef FEAT_VREPLACE
9749 if (State & VREPLACE_FLAG)
9750 vim_free(saved_line);
9751#endif
9752 curwin->w_p_list = save_list;
9753 }
9754
9755 return FALSE;
9756}
9757
9758/*
9759 * Handle CR or NL in insert mode.
9760 * Return TRUE when out of memory or can't undo.
9761 */
9762 static int
9763ins_eol(c)
9764 int c;
9765{
9766 int i;
9767
9768 if (echeck_abbr(c + ABBR_OFF))
9769 return FALSE;
9770 if (stop_arrow() == FAIL)
9771 return TRUE;
9772 undisplay_dollar();
9773
9774 /*
9775 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9776 * character under the cursor. Only push a NUL on the replace stack,
9777 * nothing to put back when the NL is deleted.
9778 */
9779 if ((State & REPLACE_FLAG)
9780#ifdef FEAT_VREPLACE
9781 && !(State & VREPLACE_FLAG)
9782#endif
9783 )
9784 replace_push(NUL);
9785
9786 /*
9787 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9788 * replacing the next line, so we push all of the characters left on the
9789 * line onto the replace stack. This is not done here though, it is done
9790 * in open_line().
9791 */
9792
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009793#ifdef FEAT_VIRTUALEDIT
9794 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9795 * CTRL-O). */
9796 if (virtual_active() && curwin->w_cursor.coladd > 0)
9797 coladvance(getviscol());
9798#endif
9799
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800#ifdef FEAT_RIGHTLEFT
9801# ifdef FEAT_FKMAP
9802 if (p_altkeymap && p_fkmap)
9803 fkmap(NL);
9804# endif
9805 /* NL in reverse insert will always start in the end of
9806 * current line. */
9807 if (revins_on)
9808 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9809#endif
9810
9811 AppendToRedobuff(NL_STR);
9812 i = open_line(FORWARD,
9813#ifdef FEAT_COMMENTS
9814 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9815#endif
9816 0, old_indent);
9817 old_indent = 0;
9818#ifdef FEAT_CINDENT
9819 can_cindent = TRUE;
9820#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009821#ifdef FEAT_FOLDING
9822 /* When inserting a line the cursor line must never be in a closed fold. */
9823 foldOpenCursor();
9824#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825
9826 return (!i);
9827}
9828
9829#ifdef FEAT_DIGRAPHS
9830/*
9831 * Handle digraph in insert mode.
9832 * Returns character still to be inserted, or NUL when nothing remaining to be
9833 * done.
9834 */
9835 static int
9836ins_digraph()
9837{
9838 int c;
9839 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009840 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841
9842 pc_status = PC_STATUS_UNSET;
9843 if (redrawing() && !char_avail())
9844 {
9845 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009846 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009847
9848 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009849 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009850#ifdef FEAT_CMDL_INFO
9851 add_to_showcmd_c(Ctrl_K);
9852#endif
9853 }
9854
9855#ifdef USE_ON_FLY_SCROLL
9856 dont_scroll = TRUE; /* disallow scrolling here */
9857#endif
9858
9859 /* don't map the digraph chars. This also prevents the
9860 * mode message to be deleted when ESC is hit */
9861 ++no_mapping;
9862 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009863 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 --no_mapping;
9865 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009866 if (did_putchar)
9867 /* when the line fits in 'columns' the '?' is at the start of the next
9868 * line and will not be removed by the redraw */
9869 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009870
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 if (IS_SPECIAL(c) || mod_mask) /* special key */
9872 {
9873#ifdef FEAT_CMDL_INFO
9874 clear_showcmd();
9875#endif
9876 insert_special(c, TRUE, FALSE);
9877 return NUL;
9878 }
9879 if (c != ESC)
9880 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009881 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009882 if (redrawing() && !char_avail())
9883 {
9884 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009885 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009886
9887 if (char2cells(c) == 1)
9888 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009889 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009891 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009892 }
9893#ifdef FEAT_CMDL_INFO
9894 add_to_showcmd_c(c);
9895#endif
9896 }
9897 ++no_mapping;
9898 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009899 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900 --no_mapping;
9901 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009902 if (did_putchar)
9903 /* when the line fits in 'columns' the '?' is at the start of the
9904 * next line and will not be removed by a redraw */
9905 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009906 if (cc != ESC)
9907 {
9908 AppendToRedobuff((char_u *)CTRL_V_STR);
9909 c = getdigraph(c, cc, TRUE);
9910#ifdef FEAT_CMDL_INFO
9911 clear_showcmd();
9912#endif
9913 return c;
9914 }
9915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916#ifdef FEAT_CMDL_INFO
9917 clear_showcmd();
9918#endif
9919 return NUL;
9920}
9921#endif
9922
9923/*
9924 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9925 * Returns the char to be inserted, or NUL if none found.
9926 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02009927 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009928ins_copychar(lnum)
9929 linenr_T lnum;
9930{
9931 int c;
9932 int temp;
9933 char_u *ptr, *prev_ptr;
9934
9935 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9936 {
9937 vim_beep();
9938 return NUL;
9939 }
9940
9941 /* try to advance to the cursor column */
9942 temp = 0;
9943 ptr = ml_get(lnum);
9944 prev_ptr = ptr;
9945 validate_virtcol();
9946 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9947 {
9948 prev_ptr = ptr;
9949 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9950 }
9951 if ((colnr_T)temp > curwin->w_virtcol)
9952 ptr = prev_ptr;
9953
9954#ifdef FEAT_MBYTE
9955 c = (*mb_ptr2char)(ptr);
9956#else
9957 c = *ptr;
9958#endif
9959 if (c == NUL)
9960 vim_beep();
9961 return c;
9962}
9963
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009964/*
9965 * CTRL-Y or CTRL-E typed in Insert mode.
9966 */
9967 static int
9968ins_ctrl_ey(tc)
9969 int tc;
9970{
9971 int c = tc;
9972
9973#ifdef FEAT_INS_EXPAND
9974 if (ctrl_x_mode == CTRL_X_SCROLL)
9975 {
9976 if (c == Ctrl_Y)
9977 scrolldown_clamp();
9978 else
9979 scrollup_clamp();
9980 redraw_later(VALID);
9981 }
9982 else
9983#endif
9984 {
9985 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9986 if (c != NUL)
9987 {
9988 long tw_save;
9989
9990 /* The character must be taken literally, insert like it
9991 * was typed after a CTRL-V, and pretend 'textwidth'
9992 * wasn't set. Digits, 'o' and 'x' are special after a
9993 * CTRL-V, don't use it for these. */
9994 if (c < 256 && !isalnum(c))
9995 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9996 tw_save = curbuf->b_p_tw;
9997 curbuf->b_p_tw = -1;
9998 insert_special(c, TRUE, FALSE);
9999 curbuf->b_p_tw = tw_save;
10000#ifdef FEAT_RIGHTLEFT
10001 revins_chars++;
10002 revins_legal++;
10003#endif
10004 c = Ctrl_V; /* pretend CTRL-V is last character */
10005 auto_format(FALSE, TRUE);
10006 }
10007 }
10008 return c;
10009}
10010
Bram Moolenaar071d4272004-06-13 20:20:40 +000010011#ifdef FEAT_SMARTINDENT
10012/*
10013 * Try to do some very smart auto-indenting.
10014 * Used when inserting a "normal" character.
10015 */
10016 static void
10017ins_try_si(c)
10018 int c;
10019{
10020 pos_T *pos, old_pos;
10021 char_u *ptr;
10022 int i;
10023 int temp;
10024
10025 /*
10026 * do some very smart indenting when entering '{' or '}'
10027 */
10028 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10029 {
10030 /*
10031 * for '}' set indent equal to indent of line containing matching '{'
10032 */
10033 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10034 {
10035 old_pos = curwin->w_cursor;
10036 /*
10037 * If the matching '{' has a ')' immediately before it (ignoring
10038 * white-space), then line up with the start of the line
10039 * containing the matching '(' if there is one. This handles the
10040 * case where an "if (..\n..) {" statement continues over multiple
10041 * lines -- webb
10042 */
10043 ptr = ml_get(pos->lnum);
10044 i = pos->col;
10045 if (i > 0) /* skip blanks before '{' */
10046 while (--i > 0 && vim_iswhite(ptr[i]))
10047 ;
10048 curwin->w_cursor.lnum = pos->lnum;
10049 curwin->w_cursor.col = i;
10050 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10051 curwin->w_cursor = *pos;
10052 i = get_indent();
10053 curwin->w_cursor = old_pos;
10054#ifdef FEAT_VREPLACE
10055 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010056 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010057 else
10058#endif
10059 (void)set_indent(i, SIN_CHANGED);
10060 }
10061 else if (curwin->w_cursor.col > 0)
10062 {
10063 /*
10064 * when inserting '{' after "O" reduce indent, but not
10065 * more than indent of previous line
10066 */
10067 temp = TRUE;
10068 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10069 {
10070 old_pos = curwin->w_cursor;
10071 i = get_indent();
10072 while (curwin->w_cursor.lnum > 1)
10073 {
10074 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10075
10076 /* ignore empty lines and lines starting with '#'. */
10077 if (*ptr != '#' && *ptr != NUL)
10078 break;
10079 }
10080 if (get_indent() >= i)
10081 temp = FALSE;
10082 curwin->w_cursor = old_pos;
10083 }
10084 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010085 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010086 }
10087 }
10088
10089 /*
10090 * set indent of '#' always to 0
10091 */
10092 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10093 {
10094 /* remember current indent for next line */
10095 old_indent = get_indent();
10096 (void)set_indent(0, SIN_CHANGED);
10097 }
10098
10099 /* Adjust ai_col, the char at this position can be deleted. */
10100 if (ai_col > curwin->w_cursor.col)
10101 ai_col = curwin->w_cursor.col;
10102}
10103#endif
10104
10105/*
10106 * Get the value that w_virtcol would have when 'list' is off.
10107 * Unless 'cpo' contains the 'L' flag.
10108 */
10109 static colnr_T
10110get_nolist_virtcol()
10111{
10112 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10113 return getvcol_nolist(&curwin->w_cursor);
10114 validate_virtcol();
10115 return curwin->w_virtcol;
10116}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010117
10118#ifdef FEAT_AUTOCMD
10119/*
10120 * Handle the InsertCharPre autocommand.
10121 * "c" is the character that was typed.
10122 * Return a pointer to allocated memory with the replacement string.
10123 * Return NULL to continue inserting "c".
10124 */
10125 static char_u *
10126do_insert_char_pre(c)
10127 int c;
10128{
Bram Moolenaar704984a2012-06-01 14:57:51 +020010129 char_u *res;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010130 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010131
10132 /* Return quickly when there is nothing to do. */
10133 if (!has_insertcharpre())
10134 return NULL;
10135
Bram Moolenaar704984a2012-06-01 14:57:51 +020010136#ifdef FEAT_MBYTE
10137 if (has_mbyte)
10138 buf[(*mb_char2bytes)(c, buf)] = NUL;
10139 else
10140#endif
10141 {
10142 buf[0] = c;
10143 buf[1] = NUL;
10144 }
10145
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010146 /* Lock the text to avoid weird things from happening. */
10147 ++textlock;
Bram Moolenaar704984a2012-06-01 14:57:51 +020010148 set_vim_var_string(VV_CHAR, buf, -1); /* set v:char */
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010149
Bram Moolenaar704984a2012-06-01 14:57:51 +020010150 res = NULL;
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010151 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Bram Moolenaar704984a2012-06-01 14:57:51 +020010152 {
10153 /* Get the value of v:char. It may be empty or more than one
10154 * character. Only use it when changed, otherwise continue with the
10155 * original character to avoid breaking autoindent. */
10156 if (STRCMP(buf, get_vim_var_str(VV_CHAR)) != 0)
10157 res = vim_strsave(get_vim_var_str(VV_CHAR));
10158 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010159
10160 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10161 --textlock;
10162
10163 return res;
10164}
10165#endif