blob: 17bbc6f2eec9316697e006d3b2ff0a4ebc31854d [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 Moolenaare0ebfd72012-04-05 16:07:06 +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 */
1651#ifdef FEAT_MBYTE
1652static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1653#else
1654static char_u pc_bytes[2]; /* saved bytes */
1655#endif
1656static int pc_attr;
1657static int pc_row;
1658static int pc_col;
1659
1660 void
1661edit_putchar(c, highlight)
1662 int c;
1663 int highlight;
1664{
1665 int attr;
1666
1667 if (ScreenLines != NULL)
1668 {
1669 update_topline(); /* just in case w_topline isn't valid */
1670 validate_cursor();
1671 if (highlight)
1672 attr = hl_attr(HLF_8);
1673 else
1674 attr = 0;
1675 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1676 pc_col = W_WINCOL(curwin);
1677#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1678 pc_status = PC_STATUS_UNSET;
1679#endif
1680#ifdef FEAT_RIGHTLEFT
1681 if (curwin->w_p_rl)
1682 {
1683 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1684# ifdef FEAT_MBYTE
1685 if (has_mbyte)
1686 {
1687 int fix_col = mb_fix_col(pc_col, pc_row);
1688
1689 if (fix_col != pc_col)
1690 {
1691 screen_putchar(' ', pc_row, fix_col, attr);
1692 --curwin->w_wcol;
1693 pc_status = PC_STATUS_RIGHT;
1694 }
1695 }
1696# endif
1697 }
1698 else
1699#endif
1700 {
1701 pc_col += curwin->w_wcol;
1702#ifdef FEAT_MBYTE
1703 if (mb_lefthalve(pc_row, pc_col))
1704 pc_status = PC_STATUS_LEFT;
1705#endif
1706 }
1707
1708 /* save the character to be able to put it back */
1709#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1710 if (pc_status == PC_STATUS_UNSET)
1711#endif
1712 {
1713 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1714 pc_status = PC_STATUS_SET;
1715 }
1716 screen_putchar(c, pc_row, pc_col, attr);
1717 }
1718}
1719
1720/*
1721 * Undo the previous edit_putchar().
1722 */
1723 void
1724edit_unputchar()
1725{
1726 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1727 {
1728#if defined(FEAT_MBYTE)
1729 if (pc_status == PC_STATUS_RIGHT)
1730 ++curwin->w_wcol;
1731 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1732 redrawWinline(curwin->w_cursor.lnum, FALSE);
1733 else
1734#endif
1735 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1736 }
1737}
1738
1739/*
1740 * Called when p_dollar is set: display a '$' at the end of the changed text
1741 * Only works when cursor is in the line that changes.
1742 */
1743 void
1744display_dollar(col)
1745 colnr_T col;
1746{
1747 colnr_T save_col;
1748
1749 if (!redrawing())
1750 return;
1751
1752 cursor_off();
1753 save_col = curwin->w_cursor.col;
1754 curwin->w_cursor.col = col;
1755#ifdef FEAT_MBYTE
1756 if (has_mbyte)
1757 {
1758 char_u *p;
1759
1760 /* If on the last byte of a multi-byte move to the first byte. */
1761 p = ml_get_curline();
1762 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1763 }
1764#endif
1765 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1766 if (curwin->w_wcol < W_WIDTH(curwin))
1767 {
1768 edit_putchar('$', FALSE);
1769 dollar_vcol = curwin->w_virtcol;
1770 }
1771 curwin->w_cursor.col = save_col;
1772}
1773
1774/*
1775 * Call this function before moving the cursor from the normal insert position
1776 * in insert mode.
1777 */
1778 static void
1779undisplay_dollar()
1780{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001781 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001783 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001784 redrawWinline(curwin->w_cursor.lnum, FALSE);
1785 }
1786}
1787
1788/*
1789 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1790 * Keep the cursor on the same character.
1791 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1792 * type == INDENT_DEC decrease indent (for CTRL-D)
1793 * type == INDENT_SET set indent to "amount"
1794 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1795 */
1796 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001797change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 int type;
1799 int amount;
1800 int round;
1801 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001802 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001803{
1804 int vcol;
1805 int last_vcol;
1806 int insstart_less; /* reduction for Insstart.col */
1807 int new_cursor_col;
1808 int i;
1809 char_u *ptr;
1810 int save_p_list;
1811 int start_col;
1812 colnr_T vc;
1813#ifdef FEAT_VREPLACE
1814 colnr_T orig_col = 0; /* init for GCC */
1815 char_u *new_line, *orig_line = NULL; /* init for GCC */
1816
1817 /* VREPLACE mode needs to know what the line was like before changing */
1818 if (State & VREPLACE_FLAG)
1819 {
1820 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1821 orig_col = curwin->w_cursor.col;
1822 }
1823#endif
1824
1825 /* for the following tricks we don't want list mode */
1826 save_p_list = curwin->w_p_list;
1827 curwin->w_p_list = FALSE;
1828 vc = getvcol_nolist(&curwin->w_cursor);
1829 vcol = vc;
1830
1831 /*
1832 * For Replace mode we need to fix the replace stack later, which is only
1833 * possible when the cursor is in the indent. Remember the number of
1834 * characters before the cursor if it's possible.
1835 */
1836 start_col = curwin->w_cursor.col;
1837
1838 /* determine offset from first non-blank */
1839 new_cursor_col = curwin->w_cursor.col;
1840 beginline(BL_WHITE);
1841 new_cursor_col -= curwin->w_cursor.col;
1842
1843 insstart_less = curwin->w_cursor.col;
1844
1845 /*
1846 * If the cursor is in the indent, compute how many screen columns the
1847 * cursor is to the left of the first non-blank.
1848 */
1849 if (new_cursor_col < 0)
1850 vcol = get_indent() - vcol;
1851
1852 if (new_cursor_col > 0) /* can't fix replace stack */
1853 start_col = -1;
1854
1855 /*
1856 * Set the new indent. The cursor will be put on the first non-blank.
1857 */
1858 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001859 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 else
1861 {
1862#ifdef FEAT_VREPLACE
1863 int save_State = State;
1864
1865 /* Avoid being called recursively. */
1866 if (State & VREPLACE_FLAG)
1867 State = INSERT;
1868#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001869 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870#ifdef FEAT_VREPLACE
1871 State = save_State;
1872#endif
1873 }
1874 insstart_less -= curwin->w_cursor.col;
1875
1876 /*
1877 * Try to put cursor on same character.
1878 * If the cursor is at or after the first non-blank in the line,
1879 * compute the cursor column relative to the column of the first
1880 * non-blank character.
1881 * If we are not in insert mode, leave the cursor on the first non-blank.
1882 * If the cursor is before the first non-blank, position it relative
1883 * to the first non-blank, counted in screen columns.
1884 */
1885 if (new_cursor_col >= 0)
1886 {
1887 /*
1888 * When changing the indent while the cursor is touching it, reset
1889 * Insstart_col to 0.
1890 */
1891 if (new_cursor_col == 0)
1892 insstart_less = MAXCOL;
1893 new_cursor_col += curwin->w_cursor.col;
1894 }
1895 else if (!(State & INSERT))
1896 new_cursor_col = curwin->w_cursor.col;
1897 else
1898 {
1899 /*
1900 * Compute the screen column where the cursor should be.
1901 */
1902 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001903 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904
1905 /*
1906 * Advance the cursor until we reach the right screen column.
1907 */
1908 vcol = last_vcol = 0;
1909 new_cursor_col = -1;
1910 ptr = ml_get_curline();
1911 while (vcol <= (int)curwin->w_virtcol)
1912 {
1913 last_vcol = vcol;
1914#ifdef FEAT_MBYTE
1915 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001916 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 else
1918#endif
1919 ++new_cursor_col;
1920 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1921 }
1922 vcol = last_vcol;
1923
1924 /*
1925 * May need to insert spaces to be able to position the cursor on
1926 * the right screen column.
1927 */
1928 if (vcol != (int)curwin->w_virtcol)
1929 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001930 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001931 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001932 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 if (ptr != NULL)
1934 {
1935 new_cursor_col += i;
1936 ptr[i] = NUL;
1937 while (--i >= 0)
1938 ptr[i] = ' ';
1939 ins_str(ptr);
1940 vim_free(ptr);
1941 }
1942 }
1943
1944 /*
1945 * When changing the indent while the cursor is in it, reset
1946 * Insstart_col to 0.
1947 */
1948 insstart_less = MAXCOL;
1949 }
1950
1951 curwin->w_p_list = save_p_list;
1952
1953 if (new_cursor_col <= 0)
1954 curwin->w_cursor.col = 0;
1955 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001956 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 curwin->w_set_curswant = TRUE;
1958 changed_cline_bef_curs();
1959
1960 /*
1961 * May have to adjust the start of the insert.
1962 */
1963 if (State & INSERT)
1964 {
1965 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1966 {
1967 if ((int)Insstart.col <= insstart_less)
1968 Insstart.col = 0;
1969 else
1970 Insstart.col -= insstart_less;
1971 }
1972 if ((int)ai_col <= insstart_less)
1973 ai_col = 0;
1974 else
1975 ai_col -= insstart_less;
1976 }
1977
1978 /*
1979 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1980 * If the number of characters before the cursor decreased, need to pop a
1981 * few characters from the replace stack.
1982 * If the number of characters before the cursor increased, need to push a
1983 * few NULs onto the replace stack.
1984 */
1985 if (REPLACE_NORMAL(State) && start_col >= 0)
1986 {
1987 while (start_col > (int)curwin->w_cursor.col)
1988 {
1989 replace_join(0); /* remove a NUL from the replace stack */
1990 --start_col;
1991 }
1992 while (start_col < (int)curwin->w_cursor.col || replaced)
1993 {
1994 replace_push(NUL);
1995 if (replaced)
1996 {
1997 replace_push(replaced);
1998 replaced = NUL;
1999 }
2000 ++start_col;
2001 }
2002 }
2003
2004#ifdef FEAT_VREPLACE
2005 /*
2006 * For VREPLACE mode, we also have to fix the replace stack. In this case
2007 * it is always possible because we backspace over the whole line and then
2008 * put it back again the way we wanted it.
2009 */
2010 if (State & VREPLACE_FLAG)
2011 {
2012 /* If orig_line didn't allocate, just return. At least we did the job,
2013 * even if you can't backspace. */
2014 if (orig_line == NULL)
2015 return;
2016
2017 /* Save new line */
2018 new_line = vim_strsave(ml_get_curline());
2019 if (new_line == NULL)
2020 return;
2021
2022 /* We only put back the new line up to the cursor */
2023 new_line[curwin->w_cursor.col] = NUL;
2024
2025 /* Put back original line */
2026 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2027 curwin->w_cursor.col = orig_col;
2028
2029 /* Backspace from cursor to start of line */
2030 backspace_until_column(0);
2031
2032 /* Insert new stuff into line again */
2033 ins_bytes(new_line);
2034
2035 vim_free(new_line);
2036 }
2037#endif
2038}
2039
2040/*
2041 * Truncate the space at the end of a line. This is to be used only in an
2042 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2043 * modes.
2044 */
2045 void
2046truncate_spaces(line)
2047 char_u *line;
2048{
2049 int i;
2050
2051 /* find start of trailing white space */
2052 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2053 {
2054 if (State & REPLACE_FLAG)
2055 replace_join(0); /* remove a NUL from the replace stack */
2056 }
2057 line[i + 1] = NUL;
2058}
2059
2060#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2061 || defined(FEAT_COMMENTS) || defined(PROTO)
2062/*
2063 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2064 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002065 * Will attempt not to go before "col" even when there is a composing
2066 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002067 */
2068 void
2069backspace_until_column(col)
2070 int col;
2071{
2072 while ((int)curwin->w_cursor.col > col)
2073 {
2074 curwin->w_cursor.col--;
2075 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002076 replace_do_bs(col);
2077 else if (!del_char_after_col(col))
2078 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 }
2080}
2081#endif
2082
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002083/*
2084 * Like del_char(), but make sure not to go before column "limit_col".
2085 * Only matters when there are composing characters.
2086 * Return TRUE when something was deleted.
2087 */
2088 static int
2089del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002090 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002091{
2092#ifdef FEAT_MBYTE
2093 if (enc_utf8 && limit_col >= 0)
2094 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002095 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002096
2097 /* Make sure the cursor is at the start of a character, but
2098 * skip forward again when going too far back because of a
2099 * composing character. */
2100 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002101 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002102 {
2103 int l = utf_ptr2len(ml_get_cursor());
2104
2105 if (l == 0) /* end of line */
2106 break;
2107 curwin->w_cursor.col += l;
2108 }
2109 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2110 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002111 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002112 }
2113 else
2114#endif
2115 (void)del_char(FALSE);
2116 return TRUE;
2117}
2118
Bram Moolenaar071d4272004-06-13 20:20:40 +00002119#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2120/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002121 * CTRL-X pressed in Insert mode.
2122 */
2123 static void
2124ins_ctrl_x()
2125{
2126 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2127 * CTRL-V works like CTRL-N */
2128 if (ctrl_x_mode != CTRL_X_CMDLINE)
2129 {
2130 /* if the next ^X<> won't ADD nothing, then reset
2131 * compl_cont_status */
2132 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002133 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002134 else
2135 compl_cont_status = 0;
2136 /* We're not sure which CTRL-X mode it will be yet */
2137 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2138 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2139 edit_submode_pre = NULL;
2140 showmode();
2141 }
2142}
2143
2144/*
2145 * Return TRUE if the 'dict' or 'tsr' option can be used.
2146 */
2147 static int
2148has_compl_option(dict_opt)
2149 int dict_opt;
2150{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002151 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002152# ifdef FEAT_SPELL
2153 && !curwin->w_p_spell
2154# endif
2155 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002156 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2157 {
2158 ctrl_x_mode = 0;
2159 edit_submode = NULL;
2160 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2161 : (char_u *)_("'thesaurus' option is empty"),
2162 hl_attr(HLF_E));
2163 if (emsg_silent == 0)
2164 {
2165 vim_beep();
2166 setcursor();
2167 out_flush();
2168 ui_delay(2000L, FALSE);
2169 }
2170 return FALSE;
2171 }
2172 return TRUE;
2173}
2174
2175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2177 * This depends on the current mode.
2178 */
2179 int
2180vim_is_ctrl_x_key(c)
2181 int c;
2182{
2183 /* Always allow ^R - let it's results then be checked */
2184 if (c == Ctrl_R)
2185 return TRUE;
2186
Bram Moolenaare3226be2005-12-18 22:10:00 +00002187 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002188 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002189 return TRUE;
2190
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191 switch (ctrl_x_mode)
2192 {
2193 case 0: /* Not in any CTRL-X mode */
2194 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2195 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002196 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2198 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2199 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002200 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002201 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 case CTRL_X_SCROLL:
2203 return (c == Ctrl_Y || c == Ctrl_E);
2204 case CTRL_X_WHOLE_LINE:
2205 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2206 case CTRL_X_FILES:
2207 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2208 case CTRL_X_DICTIONARY:
2209 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2210 case CTRL_X_THESAURUS:
2211 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2212 case CTRL_X_TAGS:
2213 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2214#ifdef FEAT_FIND_ID
2215 case CTRL_X_PATH_PATTERNS:
2216 return (c == Ctrl_P || c == Ctrl_N);
2217 case CTRL_X_PATH_DEFINES:
2218 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2219#endif
2220 case CTRL_X_CMDLINE:
2221 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2222 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002223#ifdef FEAT_COMPL_FUNC
2224 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002225 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002226 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002227 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002228#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002229 case CTRL_X_SPELL:
2230 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231 }
2232 EMSG(_(e_internal));
2233 return FALSE;
2234}
2235
2236/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002237 * Return TRUE when character "c" is part of the item currently being
2238 * completed. Used to decide whether to abandon complete mode when the menu
2239 * is visible.
2240 */
2241 static int
2242ins_compl_accept_char(c)
2243 int c;
2244{
2245 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2246 /* When expanding an identifier only accept identifier chars. */
2247 return vim_isIDc(c);
2248
2249 switch (ctrl_x_mode)
2250 {
2251 case CTRL_X_FILES:
2252 /* When expanding file name only accept file name chars. But not
2253 * path separators, so that "proto/<Tab>" expands files in
2254 * "proto", not "proto/" as a whole */
2255 return vim_isfilec(c) && !vim_ispathsep(c);
2256
2257 case CTRL_X_CMDLINE:
2258 case CTRL_X_OMNI:
2259 /* Command line and Omni completion can work with just about any
2260 * printable character, but do stop at white space. */
2261 return vim_isprintc(c) && !vim_iswhite(c);
2262
2263 case CTRL_X_WHOLE_LINE:
2264 /* For while line completion a space can be part of the line. */
2265 return vim_isprintc(c);
2266 }
2267 return vim_iswordc(c);
2268}
2269
2270/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002271 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002273 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002274 * the rest of the word to be in -- webb
2275 */
2276 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002277ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002278 char_u *str;
2279 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002280 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002281 char_u *fname;
2282 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002283 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002284{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002285 char_u *p;
2286 int i, c;
2287 int actual_len; /* Take multi-byte characters */
2288 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002289 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002290 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002291 int has_lower = FALSE;
2292 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002294 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002295 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002296 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002297
Bram Moolenaara2993e12007-08-12 14:38:46 +00002298 /* Find actual length of completion. */
2299#ifdef FEAT_MBYTE
2300 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002302 p = str;
2303 actual_len = 0;
2304 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002306 mb_ptr_adv(p);
2307 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002308 }
2309 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002310 else
2311#endif
2312 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313
Bram Moolenaara2993e12007-08-12 14:38:46 +00002314 /* Find actual length of original text. */
2315#ifdef FEAT_MBYTE
2316 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002318 p = compl_orig_text;
2319 actual_compl_length = 0;
2320 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002321 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002322 mb_ptr_adv(p);
2323 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002324 }
2325 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002326 else
2327#endif
2328 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002330 /* "actual_len" may be smaller than "actual_compl_length" when using
2331 * thesaurus, only use the minimum when comparing. */
2332 min_len = actual_len < actual_compl_length
2333 ? actual_len : actual_compl_length;
2334
Bram Moolenaara2993e12007-08-12 14:38:46 +00002335 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002336 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002337 if (wca != NULL)
2338 {
2339 p = str;
2340 for (i = 0; i < actual_len; ++i)
2341#ifdef FEAT_MBYTE
2342 if (has_mbyte)
2343 wca[i] = mb_ptr2char_adv(&p);
2344 else
2345#endif
2346 wca[i] = *(p++);
2347
2348 /* Rule 1: Were any chars converted to lower? */
2349 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002350 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002351 {
2352#ifdef FEAT_MBYTE
2353 if (has_mbyte)
2354 c = mb_ptr2char_adv(&p);
2355 else
2356#endif
2357 c = *(p++);
2358 if (MB_ISLOWER(c))
2359 {
2360 has_lower = TRUE;
2361 if (MB_ISUPPER(wca[i]))
2362 {
2363 /* Rule 1 is satisfied. */
2364 for (i = actual_compl_length; i < actual_len; ++i)
2365 wca[i] = MB_TOLOWER(wca[i]);
2366 break;
2367 }
2368 }
2369 }
2370
2371 /*
2372 * Rule 2: No lower case, 2nd consecutive letter converted to
2373 * upper case.
2374 */
2375 if (!has_lower)
2376 {
2377 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002378 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002379 {
2380#ifdef FEAT_MBYTE
2381 if (has_mbyte)
2382 c = mb_ptr2char_adv(&p);
2383 else
2384#endif
2385 c = *(p++);
2386 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2387 {
2388 /* Rule 2 is satisfied. */
2389 for (i = actual_compl_length; i < actual_len; ++i)
2390 wca[i] = MB_TOUPPER(wca[i]);
2391 break;
2392 }
2393 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2394 }
2395 }
2396
2397 /* Copy the original case of the part we typed. */
2398 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002399 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002400 {
2401#ifdef FEAT_MBYTE
2402 if (has_mbyte)
2403 c = mb_ptr2char_adv(&p);
2404 else
2405#endif
2406 c = *(p++);
2407 if (MB_ISLOWER(c))
2408 wca[i] = MB_TOLOWER(wca[i]);
2409 else if (MB_ISUPPER(c))
2410 wca[i] = MB_TOUPPER(wca[i]);
2411 }
2412
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002413 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002414 * Generate encoding specific output from wide character array.
2415 * Multi-byte characters can occupy up to five bytes more than
2416 * ASCII characters, and we also need one byte for NUL, so stay
2417 * six bytes away from the edge of IObuff.
2418 */
2419 p = IObuff;
2420 i = 0;
2421 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2422#ifdef FEAT_MBYTE
2423 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002424 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002425 else
2426#endif
2427 *(p++) = wca[i++];
2428 *p = NUL;
2429
2430 vim_free(wca);
2431 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002433 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2434 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002436 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437}
2438
2439/*
2440 * Add a match to the list of matches.
2441 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002442 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002443 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002444 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002445 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002446ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447 char_u *str;
2448 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002449 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002451 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002452 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002453 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002454 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002456 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002457 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002458
2459 ui_breakcheck();
2460 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002461 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 if (len < 0)
2463 len = (int)STRLEN(str);
2464
2465 /*
2466 * If the same match is already present, don't add it.
2467 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002468 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002470 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 do
2472 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002473 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002474 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002475 && match->cp_str[len] == NUL)
2476 return NOTDONE;
2477 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002478 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002479 }
2480
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002481 /* Remove any popup menu before changing the list of matches. */
2482 ins_compl_del_pum();
2483
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484 /*
2485 * Allocate a new match structure.
2486 * Copy the values to the new match structure.
2487 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002488 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002490 return FAIL;
2491 match->cp_number = -1;
2492 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002493 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002494 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 {
2496 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002497 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002498 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002499 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002500
Bram Moolenaar071d4272004-06-13 20:20:40 +00002501 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002502 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2504 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002505 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002506 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002507 && compl_curr_match->cp_fname != NULL
2508 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002509 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002510 else if (fname != NULL)
2511 {
2512 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002513 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002514 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002515 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002516 match->cp_fname = NULL;
2517 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002518
2519 if (cptext != NULL)
2520 {
2521 int i;
2522
2523 for (i = 0; i < CPT_COUNT; ++i)
2524 if (cptext[i] != NULL && *cptext[i] != NUL)
2525 match->cp_text[i] = vim_strsave(cptext[i]);
2526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527
2528 /*
2529 * Link the new match structure in the list of matches.
2530 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002531 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002532 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 else if (dir == FORWARD)
2534 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002535 match->cp_next = compl_curr_match->cp_next;
2536 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537 }
2538 else /* BACKWARD */
2539 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002540 match->cp_next = compl_curr_match;
2541 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002543 if (match->cp_next)
2544 match->cp_next->cp_prev = match;
2545 if (match->cp_prev)
2546 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002548 compl_first_match = match;
2549 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002551 /*
2552 * Find the longest common string if still doing that.
2553 */
2554 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2555 ins_compl_longest_match(match);
2556
Bram Moolenaar071d4272004-06-13 20:20:40 +00002557 return OK;
2558}
2559
2560/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002561 * Return TRUE if "str[len]" matches with match->cp_str, considering
2562 * match->cp_icase.
2563 */
2564 static int
2565ins_compl_equal(match, str, len)
2566 compl_T *match;
2567 char_u *str;
2568 int len;
2569{
2570 if (match->cp_icase)
2571 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2572 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2573}
2574
2575/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002576 * Reduce the longest common string for match "match".
2577 */
2578 static void
2579ins_compl_longest_match(match)
2580 compl_T *match;
2581{
2582 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002583 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002584 int had_match;
2585
2586 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002587 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002588 /* First match, use it as a whole. */
2589 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002590 if (compl_leader != NULL)
2591 {
2592 had_match = (curwin->w_cursor.col > compl_col);
2593 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002594 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002595 ins_redraw(FALSE);
2596
2597 /* When the match isn't there (to avoid matching itself) remove it
2598 * again after redrawing. */
2599 if (!had_match)
2600 ins_compl_delete();
2601 compl_used_match = FALSE;
2602 }
2603 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002604 else
2605 {
2606 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002607 p = compl_leader;
2608 s = match->cp_str;
2609 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002610 {
2611#ifdef FEAT_MBYTE
2612 if (has_mbyte)
2613 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002614 c1 = mb_ptr2char(p);
2615 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002616 }
2617 else
2618#endif
2619 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002620 c1 = *p;
2621 c2 = *s;
2622 }
2623 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2624 : (c1 != c2))
2625 break;
2626#ifdef FEAT_MBYTE
2627 if (has_mbyte)
2628 {
2629 mb_ptr_adv(p);
2630 mb_ptr_adv(s);
2631 }
2632 else
2633#endif
2634 {
2635 ++p;
2636 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002637 }
2638 }
2639
2640 if (*p != NUL)
2641 {
2642 /* Leader was shortened, need to change the inserted text. */
2643 *p = NUL;
2644 had_match = (curwin->w_cursor.col > compl_col);
2645 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002646 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002647 ins_redraw(FALSE);
2648
2649 /* When the match isn't there (to avoid matching itself) remove it
2650 * again after redrawing. */
2651 if (!had_match)
2652 ins_compl_delete();
2653 }
2654
2655 compl_used_match = FALSE;
2656 }
2657}
2658
2659/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002660 * Add an array of matches to the list of matches.
2661 * Frees matches[].
2662 */
2663 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002664ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002665 int num_matches;
2666 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002667 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002668{
2669 int i;
2670 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002671 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002672
Bram Moolenaar572cb562005-08-05 21:35:02 +00002673 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002674 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002675 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002676 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002677 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002678 FreeWild(num_matches, matches);
2679}
2680
2681/* Make the completion list cyclic.
2682 * Return the number of matches (excluding the original).
2683 */
2684 static int
2685ins_compl_make_cyclic()
2686{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002687 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002688 int count = 0;
2689
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002690 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002691 {
2692 /*
2693 * Find the end of the list.
2694 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002695 match = compl_first_match;
2696 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002697 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002698 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002699 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002700 ++count;
2701 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002702 match->cp_next = compl_first_match;
2703 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002704 }
2705 return count;
2706}
2707
Bram Moolenaara94bc432006-03-10 21:42:59 +00002708/*
2709 * Start completion for the complete() function.
2710 * "startcol" is where the matched text starts (1 is first column).
2711 * "list" is the list of matches.
2712 */
2713 void
2714set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002715 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002716 list_T *list;
2717{
2718 /* If already doing completions stop it. */
2719 if (ctrl_x_mode != 0)
2720 ins_compl_prep(' ');
2721 ins_compl_clear();
2722
2723 if (stop_arrow() == FAIL)
2724 return;
2725
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002726 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002727 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002728 startcol = curwin->w_cursor.col;
2729 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002730 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002731 /* compl_pattern doesn't need to be set */
2732 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2733 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002734 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002735 return;
2736
2737 /* Handle like dictionary completion. */
2738 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2739
2740 ins_compl_add_list(list);
2741 compl_matches = ins_compl_make_cyclic();
2742 compl_started = TRUE;
2743 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002744 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002745
2746 compl_curr_match = compl_first_match;
2747 ins_complete(Ctrl_N);
2748 out_flush();
2749}
2750
2751
Bram Moolenaar9372a112005-12-06 19:59:18 +00002752/* "compl_match_array" points the currently displayed list of entries in the
2753 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002754static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002755static int compl_match_arraysize;
2756
2757/*
2758 * Update the screen and when there is any scrolling remove the popup menu.
2759 */
2760 static void
2761ins_compl_upd_pum()
2762{
2763 int h;
2764
2765 if (compl_match_array != NULL)
2766 {
2767 h = curwin->w_cline_height;
2768 update_screen(0);
2769 if (h != curwin->w_cline_height)
2770 ins_compl_del_pum();
2771 }
2772}
2773
2774/*
2775 * Remove any popup menu.
2776 */
2777 static void
2778ins_compl_del_pum()
2779{
2780 if (compl_match_array != NULL)
2781 {
2782 pum_undisplay();
2783 vim_free(compl_match_array);
2784 compl_match_array = NULL;
2785 }
2786}
2787
2788/*
2789 * Return TRUE if the popup menu should be displayed.
2790 */
2791 static int
2792pum_wanted()
2793{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002794 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002795 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002796 return FALSE;
2797
2798 /* The display looks bad on a B&W display. */
2799 if (t_colors < 8
2800#ifdef FEAT_GUI
2801 && !gui.in_use
2802#endif
2803 )
2804 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002805 return TRUE;
2806}
2807
2808/*
2809 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002810 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002811 */
2812 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002813pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002814{
2815 compl_T *compl;
2816 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002817
2818 /* Don't display the popup menu if there are no matches or there is only
2819 * one (ignoring the original text). */
2820 compl = compl_first_match;
2821 i = 0;
2822 do
2823 {
2824 if (compl == NULL
2825 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2826 break;
2827 compl = compl->cp_next;
2828 } while (compl != compl_first_match);
2829
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002830 if (strstr((char *)p_cot, "menuone") != NULL)
2831 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002832 return (i >= 2);
2833}
2834
2835/*
2836 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002837 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002838 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002839 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002840ins_compl_show_pum()
2841{
2842 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002843 compl_T *shown_compl = NULL;
2844 int did_find_shown_match = FALSE;
2845 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002846 int i;
2847 int cur = -1;
2848 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002849 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002850
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002851 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002852 return;
2853
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002854#if defined(FEAT_EVAL)
2855 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2856 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2857#endif
2858
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002859 /* Update the screen before drawing the popup menu over it. */
2860 update_screen(0);
2861
2862 if (compl_match_array == NULL)
2863 {
2864 /* Need to build the popup menu list. */
2865 compl_match_arraysize = 0;
2866 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002867 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002868 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002869 do
2870 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002871 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2872 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002873 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002874 ++compl_match_arraysize;
2875 compl = compl->cp_next;
2876 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002877 if (compl_match_arraysize == 0)
2878 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002879 compl_match_array = (pumitem_T *)alloc_clear(
2880 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002881 * compl_match_arraysize));
2882 if (compl_match_array != NULL)
2883 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002884 /* If the current match is the original text don't find the first
2885 * match after it, don't highlight anything. */
2886 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2887 shown_match_ok = TRUE;
2888
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002889 i = 0;
2890 compl = compl_first_match;
2891 do
2892 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002893 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2894 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002895 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002896 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002897 if (!shown_match_ok)
2898 {
2899 if (compl == compl_shown_match || did_find_shown_match)
2900 {
2901 /* This item is the shown match or this is the
2902 * first displayed item after the shown match. */
2903 compl_shown_match = compl;
2904 did_find_shown_match = TRUE;
2905 shown_match_ok = TRUE;
2906 }
2907 else
2908 /* Remember this displayed match for when the
2909 * shown match is just below it. */
2910 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002911 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002912 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002913
2914 if (compl->cp_text[CPT_ABBR] != NULL)
2915 compl_match_array[i].pum_text =
2916 compl->cp_text[CPT_ABBR];
2917 else
2918 compl_match_array[i].pum_text = compl->cp_str;
2919 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2920 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2921 if (compl->cp_text[CPT_MENU] != NULL)
2922 compl_match_array[i++].pum_extra =
2923 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002924 else
2925 compl_match_array[i++].pum_extra = compl->cp_fname;
2926 }
2927
2928 if (compl == compl_shown_match)
2929 {
2930 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002931
2932 /* When the original text is the shown match don't set
2933 * compl_shown_match. */
2934 if (compl->cp_flags & ORIGINAL_TEXT)
2935 shown_match_ok = TRUE;
2936
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002937 if (!shown_match_ok && shown_compl != NULL)
2938 {
2939 /* The shown match isn't displayed, set it to the
2940 * previously displayed match. */
2941 compl_shown_match = shown_compl;
2942 shown_match_ok = TRUE;
2943 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002944 }
2945 compl = compl->cp_next;
2946 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002947
2948 if (!shown_match_ok) /* no displayed match at all */
2949 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002950 }
2951 }
2952 else
2953 {
2954 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002955 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002956 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2957 || compl_match_array[i].pum_text
2958 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002959 {
2960 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002961 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002962 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002963 }
2964
2965 if (compl_match_array != NULL)
2966 {
2967 /* Compute the screen column of the start of the completed text.
2968 * Use the cursor to get all wrapping and other settings right. */
2969 col = curwin->w_cursor.col;
2970 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002971 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002972 curwin->w_cursor.col = col;
2973 }
2974}
2975
Bram Moolenaar071d4272004-06-13 20:20:40 +00002976#define DICT_FIRST (1) /* use just first element in "dict" */
2977#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002978
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002980 * Add any identifiers that match the given pattern in the list of dictionary
2981 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002982 */
2983 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002984ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2985 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002986 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002987 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002988 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002990 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 char_u *ptr;
2992 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002993 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 char_u **files;
2995 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002997 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998
Bram Moolenaar0b238792006-03-02 22:49:12 +00002999 if (*dict == NUL)
3000 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003001#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003002 /* When 'dictionary' is empty and spell checking is enabled use
3003 * "spell". */
3004 if (!thesaurus && curwin->w_p_spell)
3005 dict = (char_u *)"spell";
3006 else
3007#endif
3008 return;
3009 }
3010
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003012 if (buf == NULL)
3013 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003014 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003015
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 /* If 'infercase' is set, don't use 'smartcase' here */
3017 save_p_scs = p_scs;
3018 if (curbuf->b_p_inf)
3019 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003020
3021 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3022 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003023 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003024 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3025 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003026 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003027 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003028
3029 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003030 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003031 len = STRLEN(pat_esc) + 10;
3032 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003033 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003034 {
3035 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003036 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003037 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003038 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003039 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3040 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003041 vim_free(ptr);
3042 }
3043 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003044 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003045 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003046 if (regmatch.regprog == NULL)
3047 goto theend;
3048 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003049
Bram Moolenaar071d4272004-06-13 20:20:40 +00003050 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3051 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003052 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003053 {
3054 /* copy one dictionary file name into buf */
3055 if (flags == DICT_EXACT)
3056 {
3057 count = 1;
3058 files = &dict;
3059 }
3060 else
3061 {
3062 /* Expand wildcards in the dictionary name, but do not allow
3063 * backticks (for security, the 'dict' option may have been set in
3064 * a modeline). */
3065 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003066# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003067 if (!thesaurus && STRCMP(buf, "spell") == 0)
3068 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003069 else
3070# endif
3071 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003072 || expand_wildcards(1, &buf, &count, &files,
3073 EW_FILE|EW_SILENT) != OK)
3074 count = 0;
3075 }
3076
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003077# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003078 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003080 /* Complete from active spelling. Skip "\<" in the pattern, we
3081 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003082 if (pat[0] == '\\' && pat[1] == '<')
3083 ptr = pat + 2;
3084 else
3085 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003086 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003087 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003088 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003089# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003090 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003091 {
3092 ins_compl_files(count, files, thesaurus, flags,
3093 &regmatch, buf, &dir);
3094 if (flags != DICT_EXACT)
3095 FreeWild(count, files);
3096 }
3097 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003098 break;
3099 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003100
3101theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003102 p_scs = save_p_scs;
3103 vim_free(regmatch.regprog);
3104 vim_free(buf);
3105}
3106
Bram Moolenaar0b238792006-03-02 22:49:12 +00003107 static void
3108ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3109 int count;
3110 char_u **files;
3111 int thesaurus;
3112 int flags;
3113 regmatch_T *regmatch;
3114 char_u *buf;
3115 int *dir;
3116{
3117 char_u *ptr;
3118 int i;
3119 FILE *fp;
3120 int add_r;
3121
3122 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3123 {
3124 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3125 if (flags != DICT_EXACT)
3126 {
3127 vim_snprintf((char *)IObuff, IOSIZE,
3128 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003129 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003130 }
3131
3132 if (fp != NULL)
3133 {
3134 /*
3135 * Read dictionary file line by line.
3136 * Check each line for a match.
3137 */
3138 while (!got_int && !compl_interrupted
3139 && !vim_fgets(buf, LSIZE, fp))
3140 {
3141 ptr = buf;
3142 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3143 {
3144 ptr = regmatch->startp[0];
3145 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3146 ptr = find_line_end(ptr);
3147 else
3148 ptr = find_word_end(ptr);
3149 add_r = ins_compl_add_infercase(regmatch->startp[0],
3150 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003151 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003152 if (thesaurus)
3153 {
3154 char_u *wstart;
3155
3156 /*
3157 * Add the other matches on the line
3158 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003159 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003160 while (!got_int)
3161 {
3162 /* Find start of the next word. Skip white
3163 * space and punctuation. */
3164 ptr = find_word_start(ptr);
3165 if (*ptr == NUL || *ptr == NL)
3166 break;
3167 wstart = ptr;
3168
Bram Moolenaara2993e12007-08-12 14:38:46 +00003169 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003170#ifdef FEAT_MBYTE
3171 if (has_mbyte)
3172 /* Japanese words may have characters in
3173 * different classes, only separate words
3174 * with single-byte non-word characters. */
3175 while (*ptr != NUL)
3176 {
3177 int l = (*mb_ptr2len)(ptr);
3178
3179 if (l < 2 && !vim_iswordc(*ptr))
3180 break;
3181 ptr += l;
3182 }
3183 else
3184#endif
3185 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003186
3187 /* Add the word. Skip the regexp match. */
3188 if (wstart != regmatch->startp[0])
3189 add_r = ins_compl_add_infercase(wstart,
3190 (int)(ptr - wstart),
3191 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003192 }
3193 }
3194 if (add_r == OK)
3195 /* if dir was BACKWARD then honor it just once */
3196 *dir = FORWARD;
3197 else if (add_r == FAIL)
3198 break;
3199 /* avoid expensive call to vim_regexec() when at end
3200 * of line */
3201 if (*ptr == '\n' || got_int)
3202 break;
3203 }
3204 line_breakcheck();
3205 ins_compl_check_keys(50);
3206 }
3207 fclose(fp);
3208 }
3209 }
3210}
3211
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212/*
3213 * Find the start of the next word.
3214 * Returns a pointer to the first char of the word. Also stops at a NUL.
3215 */
3216 char_u *
3217find_word_start(ptr)
3218 char_u *ptr;
3219{
3220#ifdef FEAT_MBYTE
3221 if (has_mbyte)
3222 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003223 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 else
3225#endif
3226 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3227 ++ptr;
3228 return ptr;
3229}
3230
3231/*
3232 * Find the end of the word. Assumes it starts inside a word.
3233 * Returns a pointer to just after the word.
3234 */
3235 char_u *
3236find_word_end(ptr)
3237 char_u *ptr;
3238{
3239#ifdef FEAT_MBYTE
3240 int start_class;
3241
3242 if (has_mbyte)
3243 {
3244 start_class = mb_get_class(ptr);
3245 if (start_class > 1)
3246 while (*ptr != NUL)
3247 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003248 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 if (mb_get_class(ptr) != start_class)
3250 break;
3251 }
3252 }
3253 else
3254#endif
3255 while (vim_iswordc(*ptr))
3256 ++ptr;
3257 return ptr;
3258}
3259
3260/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003261 * Find the end of the line, omitting CR and NL at the end.
3262 * Returns a pointer to just after the line.
3263 */
3264 static char_u *
3265find_line_end(ptr)
3266 char_u *ptr;
3267{
3268 char_u *s;
3269
3270 s = ptr + STRLEN(ptr);
3271 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3272 --s;
3273 return s;
3274}
3275
3276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003277 * Free the list of completions
3278 */
3279 static void
3280ins_compl_free()
3281{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003282 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003283 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003284
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003285 vim_free(compl_pattern);
3286 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003287 vim_free(compl_leader);
3288 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003290 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003291 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003292
3293 ins_compl_del_pum();
3294 pum_clear();
3295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003296 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003297 do
3298 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003299 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003300 compl_curr_match = compl_curr_match->cp_next;
3301 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003302 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003303 if (match->cp_flags & FREE_FNAME)
3304 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003305 for (i = 0; i < CPT_COUNT; ++i)
3306 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003307 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003308 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3309 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003310 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003311}
3312
3313 static void
3314ins_compl_clear()
3315{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003316 compl_cont_status = 0;
3317 compl_started = FALSE;
3318 compl_matches = 0;
3319 vim_free(compl_pattern);
3320 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003321 vim_free(compl_leader);
3322 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003323 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003324 vim_free(compl_orig_text);
3325 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003326 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003327}
3328
3329/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003330 * Return TRUE when Insert completion is active.
3331 */
3332 int
3333ins_compl_active()
3334{
3335 return compl_started;
3336}
3337
3338/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003339 * Delete one character before the cursor and show the subset of the matches
3340 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003341 * Returns the character to be used, NUL if the work is done and another char
3342 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003343 */
3344 static int
3345ins_compl_bs()
3346{
3347 char_u *line;
3348 char_u *p;
3349
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003350 line = ml_get_curline();
3351 p = line + curwin->w_cursor.col;
3352 mb_ptr_back(line, p);
3353
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003354 /* Stop completion when the whole word was deleted. For Omni completion
3355 * allow the word to be deleted, we won't match everything. */
3356 if ((int)(p - line) - (int)compl_col < 0
3357 || ((int)(p - line) - (int)compl_col == 0
3358 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003359 return K_BS;
3360
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003361 /* Deleted more than what was used to find matches or didn't finish
3362 * finding all matches: need to look for matches all over again. */
3363 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003364 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003365 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003366
Bram Moolenaara6557602006-02-04 22:43:20 +00003367 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003368 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003369 if (compl_leader != NULL)
3370 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003371 ins_compl_new_leader();
3372 return NUL;
3373 }
3374 return K_BS;
3375}
Bram Moolenaara6557602006-02-04 22:43:20 +00003376
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003377/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003378 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3379 * be called.
3380 */
3381 static int
3382ins_compl_need_restart()
3383{
3384 /* Return TRUE if we didn't complete finding matches or when the
3385 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3386 return compl_was_interrupted
3387 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3388 && compl_opt_refresh_always);
3389}
3390
3391/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003392 * Called after changing "compl_leader".
3393 * Show the popup menu with a different set of matches.
3394 * May also search for matches again if the previous search was interrupted.
3395 */
3396 static void
3397ins_compl_new_leader()
3398{
3399 ins_compl_del_pum();
3400 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003401 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003402 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003403
3404 if (compl_started)
3405 ins_compl_set_original_text(compl_leader);
3406 else
3407 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003408#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003409 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003410#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003411 /*
3412 * Matches were cleared, need to search for them now. First display
3413 * the changed text before the cursor. Set "compl_restarting" to
3414 * avoid that the first match is inserted.
3415 */
3416 update_screen(0);
3417#ifdef FEAT_GUI
3418 if (gui.in_use)
3419 {
3420 /* Show the cursor after the match, not after the redrawn text. */
3421 setcursor();
3422 out_flush();
3423 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003424 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003425#endif
3426 compl_restarting = TRUE;
3427 if (ins_complete(Ctrl_N) == FAIL)
3428 compl_cont_status = 0;
3429 compl_restarting = FALSE;
3430 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003431
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003432 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003433
3434 /* Show the popup menu with a different set of matches. */
3435 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003436
3437 /* Don't let Enter select the original text when there is no popup menu. */
3438 if (compl_match_array == NULL)
3439 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003440}
3441
3442/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003443 * Return the length of the completion, from the completion start column to
3444 * the cursor column. Making sure it never goes below zero.
3445 */
3446 static int
3447ins_compl_len()
3448{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003449 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003450
3451 if (off < 0)
3452 return 0;
3453 return off;
3454}
3455
3456/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003457 * Append one character to the match leader. May reduce the number of
3458 * matches.
3459 */
3460 static void
3461ins_compl_addleader(c)
3462 int c;
3463{
3464#ifdef FEAT_MBYTE
3465 int cc;
3466
3467 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3468 {
3469 char_u buf[MB_MAXBYTES + 1];
3470
3471 (*mb_char2bytes)(c, buf);
3472 buf[cc] = NUL;
3473 ins_char_bytes(buf, cc);
3474 }
3475 else
3476#endif
3477 ins_char(c);
3478
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003479 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003480 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003481 ins_compl_restart();
3482
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003483 /* When 'always' is set, don't reset compl_leader. While completing,
3484 * cursor don't point original position, changing compl_leader would
3485 * break redo. */
3486 if (!compl_opt_refresh_always)
3487 {
3488 vim_free(compl_leader);
3489 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003490 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003491 if (compl_leader != NULL)
3492 ins_compl_new_leader();
3493 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003494}
3495
3496/*
3497 * Setup for finding completions again without leaving CTRL-X mode. Used when
3498 * BS or a key was typed while still searching for matches.
3499 */
3500 static void
3501ins_compl_restart()
3502{
3503 ins_compl_free();
3504 compl_started = FALSE;
3505 compl_matches = 0;
3506 compl_cont_status = 0;
3507 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003508}
3509
3510/*
3511 * Set the first match, the original text.
3512 */
3513 static void
3514ins_compl_set_original_text(str)
3515 char_u *str;
3516{
3517 char_u *p;
3518
3519 /* Replace the original text entry. */
3520 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3521 {
3522 p = vim_strsave(str);
3523 if (p != NULL)
3524 {
3525 vim_free(compl_first_match->cp_str);
3526 compl_first_match->cp_str = p;
3527 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003528 }
3529}
3530
3531/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003532 * Append one character to the match leader. May reduce the number of
3533 * matches.
3534 */
3535 static void
3536ins_compl_addfrommatch()
3537{
3538 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003539 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003540 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003541 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003542
3543 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003544 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003545 {
3546 /* When still at the original match use the first entry that matches
3547 * the leader. */
3548 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3549 {
3550 p = NULL;
3551 for (cp = compl_shown_match->cp_next; cp != NULL
3552 && cp != compl_first_match; cp = cp->cp_next)
3553 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003554 if (compl_leader == NULL
3555 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003556 (int)STRLEN(compl_leader)))
3557 {
3558 p = cp->cp_str;
3559 break;
3560 }
3561 }
3562 if (p == NULL || (int)STRLEN(p) <= len)
3563 return;
3564 }
3565 else
3566 return;
3567 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003568 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003569 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003570 ins_compl_addleader(c);
3571}
3572
3573/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003575 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003576 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003578 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579ins_compl_prep(c)
3580 int c;
3581{
3582 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003583 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003584 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585
3586 /* Forget any previous 'special' messages if this is actually
3587 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3588 */
3589 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3590 edit_submode_extra = NULL;
3591
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003592 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003593 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3594 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003595 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003597 /* Set "compl_get_longest" when finding the first matches. */
3598 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3599 || (ctrl_x_mode == 0 && !compl_started))
3600 {
3601 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3602 compl_used_match = TRUE;
3603 }
3604
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3606 {
3607 /*
3608 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3609 * it will be yet. Now we decide.
3610 */
3611 switch (c)
3612 {
3613 case Ctrl_E:
3614 case Ctrl_Y:
3615 ctrl_x_mode = CTRL_X_SCROLL;
3616 if (!(State & REPLACE_FLAG))
3617 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3618 else
3619 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3620 edit_submode_pre = NULL;
3621 showmode();
3622 break;
3623 case Ctrl_L:
3624 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3625 break;
3626 case Ctrl_F:
3627 ctrl_x_mode = CTRL_X_FILES;
3628 break;
3629 case Ctrl_K:
3630 ctrl_x_mode = CTRL_X_DICTIONARY;
3631 break;
3632 case Ctrl_R:
3633 /* Simply allow ^R to happen without affecting ^X mode */
3634 break;
3635 case Ctrl_T:
3636 ctrl_x_mode = CTRL_X_THESAURUS;
3637 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003638#ifdef FEAT_COMPL_FUNC
3639 case Ctrl_U:
3640 ctrl_x_mode = CTRL_X_FUNCTION;
3641 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003642 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003643 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003644 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003645#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003646 case 's':
3647 case Ctrl_S:
3648 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003649#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003650 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003651 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003652 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003653#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003654 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 case Ctrl_RSB:
3656 ctrl_x_mode = CTRL_X_TAGS;
3657 break;
3658#ifdef FEAT_FIND_ID
3659 case Ctrl_I:
3660 case K_S_TAB:
3661 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3662 break;
3663 case Ctrl_D:
3664 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3665 break;
3666#endif
3667 case Ctrl_V:
3668 case Ctrl_Q:
3669 ctrl_x_mode = CTRL_X_CMDLINE;
3670 break;
3671 case Ctrl_P:
3672 case Ctrl_N:
3673 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3674 * just started ^X mode, or there were enough ^X's to cancel
3675 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3676 * do normal expansion when interrupting a different mode (say
3677 * ^X^F^X^P or ^P^X^X^P, see below)
3678 * nothing changes if interrupting mode 0, (eg, the flag
3679 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003680 if (!(compl_cont_status & CONT_INTRPT))
3681 compl_cont_status |= CONT_LOCAL;
3682 else if (compl_cont_mode != 0)
3683 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003684 /* FALLTHROUGH */
3685 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003686 /* If we have typed at least 2 ^X's... for modes != 0, we set
3687 * compl_cont_status = 0 (eg, as if we had just started ^X
3688 * mode).
3689 * For mode 0, we set "compl_cont_mode" to an impossible
3690 * value, in both cases ^X^X can be used to restart the same
3691 * mode (avoiding ADDING mode).
3692 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3693 * 'complete' and local ^P expansions respectively.
3694 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3695 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003696 if (c == Ctrl_X)
3697 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003698 if (compl_cont_mode != 0)
3699 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003700 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003701 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 }
3703 ctrl_x_mode = 0;
3704 edit_submode = NULL;
3705 showmode();
3706 break;
3707 }
3708 }
3709 else if (ctrl_x_mode != 0)
3710 {
3711 /* We're already in CTRL-X mode, do we stay in it? */
3712 if (!vim_is_ctrl_x_key(c))
3713 {
3714 if (ctrl_x_mode == CTRL_X_SCROLL)
3715 ctrl_x_mode = 0;
3716 else
3717 ctrl_x_mode = CTRL_X_FINISHED;
3718 edit_submode = NULL;
3719 }
3720 showmode();
3721 }
3722
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003723 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003724 {
3725 /* Show error message from attempted keyword completion (probably
3726 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003727 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003729 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3730 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 || ctrl_x_mode == CTRL_X_FINISHED)
3732 {
3733 /* Get here when we have finished typing a sequence of ^N and
3734 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003735 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003736 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 {
3738 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003739 * If any of the original typed text has been changed, eg when
3740 * ignorecase is set, we must add back-spaces to the redo
3741 * buffer. We add as few as necessary to delete just the part
3742 * of the original text that has changed.
3743 * When using the longest match, edited the match or used
3744 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003745 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003746 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3747 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003748 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003749 ptr = NULL;
3750 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 }
3752
3753#ifdef FEAT_CINDENT
3754 want_cindent = (can_cindent && cindent_on());
3755#endif
3756 /*
3757 * When completing whole lines: fix indent for 'cindent'.
3758 * Otherwise, break line if it's too long.
3759 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003760 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003761 {
3762#ifdef FEAT_CINDENT
3763 /* re-indent the current line */
3764 if (want_cindent)
3765 {
3766 do_c_expr_indent();
3767 want_cindent = FALSE; /* don't do it again */
3768 }
3769#endif
3770 }
3771 else
3772 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003773 int prev_col = curwin->w_cursor.col;
3774
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003776 if (prev_col > 0)
3777 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 if (stop_arrow() == OK)
3779 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003780 if (prev_col > 0
3781 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3782 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 }
3784
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003785 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003786 * the selection without inserting anything. When
3787 * compl_enter_selects is set the Enter key does the same. */
3788 if ((c == Ctrl_Y || (compl_enter_selects
3789 && (c == CAR || c == K_KENTER || c == NL)))
3790 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003791 retval = TRUE;
3792
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003793 /* CTRL-E means completion is Ended, go back to the typed text. */
3794 if (c == Ctrl_E)
3795 {
3796 ins_compl_delete();
3797 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003798 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003799 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003800 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003801 retval = TRUE;
3802 }
3803
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003804 auto_format(FALSE, TRUE);
3805
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003807 compl_started = FALSE;
3808 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003809 msg_clr_cmdline(); /* necessary for "noshowmode" */
3810 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003811 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003812 if (edit_submode != NULL)
3813 {
3814 edit_submode = NULL;
3815 showmode();
3816 }
3817
3818#ifdef FEAT_CINDENT
3819 /*
3820 * Indent now if a key was typed that is in 'cinkeys'.
3821 */
3822 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3823 do_c_expr_indent();
3824#endif
3825 }
3826 }
3827
3828 /* reset continue_* if we left expansion-mode, if we stay they'll be
3829 * (re)set properly in ins_complete() */
3830 if (!vim_is_ctrl_x_key(c))
3831 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003832 compl_cont_status = 0;
3833 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003834 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003835
3836 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003837}
3838
3839/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003840 * Fix the redo buffer for the completion leader replacing some of the typed
3841 * text. This inserts backspaces and appends the changed text.
3842 * "ptr" is the known leader text or NUL.
3843 */
3844 static void
3845ins_compl_fixRedoBufForLeader(ptr_arg)
3846 char_u *ptr_arg;
3847{
3848 int len;
3849 char_u *p;
3850 char_u *ptr = ptr_arg;
3851
3852 if (ptr == NULL)
3853 {
3854 if (compl_leader != NULL)
3855 ptr = compl_leader;
3856 else
3857 return; /* nothing to do */
3858 }
3859 if (compl_orig_text != NULL)
3860 {
3861 p = compl_orig_text;
3862 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3863 ;
3864#ifdef FEAT_MBYTE
3865 if (len > 0)
3866 len -= (*mb_head_off)(p, p + len);
3867#endif
3868 for (p += len; *p != NUL; mb_ptr_adv(p))
3869 AppendCharToRedobuff(K_BS);
3870 }
3871 else
3872 len = 0;
3873 if (ptr != NULL)
3874 AppendToRedobuffLit(ptr + len, -1);
3875}
3876
3877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003878 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3879 * (depending on flag) starting from buf and looking for a non-scanned
3880 * buffer (other than curbuf). curbuf is special, if it is called with
3881 * buf=curbuf then it has to be the first call for a given flag/expansion.
3882 *
3883 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3884 */
3885 static buf_T *
3886ins_compl_next_buf(buf, flag)
3887 buf_T *buf;
3888 int flag;
3889{
3890#ifdef FEAT_WINDOWS
3891 static win_T *wp;
3892#endif
3893
3894 if (flag == 'w') /* just windows */
3895 {
3896#ifdef FEAT_WINDOWS
3897 if (buf == curbuf) /* first call for this flag/expansion */
3898 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003899 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 && wp->w_buffer->b_scanned)
3901 ;
3902 buf = wp->w_buffer;
3903#else
3904 buf = curbuf;
3905#endif
3906 }
3907 else
3908 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3909 * (unlisted buffers)
3910 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003911 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 && ((flag == 'U'
3913 ? buf->b_p_bl
3914 : (!buf->b_p_bl
3915 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003916 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003917 ;
3918 return buf;
3919}
3920
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003921#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003922static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003923
3924/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003925 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003926 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003927 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003928 static void
3929expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003930 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003931 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003932{
Bram Moolenaar82139082011-09-14 16:52:09 +02003933 list_T *matchlist = NULL;
3934 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003935 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003936 char_u *funcname;
3937 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003938 win_T *curwin_save;
3939 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02003940 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003941
Bram Moolenaare344bea2005-09-01 20:46:49 +00003942 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3943 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003944 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003945
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003946 /* Call 'completefunc' to obtain the list of matches. */
3947 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003948 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003949
Bram Moolenaare344bea2005-09-01 20:46:49 +00003950 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003951 curwin_save = curwin;
3952 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02003953
3954 /* Call a function, which returns a list or dict. */
3955 if (call_vim_function(funcname, 2, args, FALSE, &rettv) == OK)
3956 {
3957 switch (rettv.v_type)
3958 {
3959 case VAR_LIST:
3960 matchlist = rettv.vval.v_list;
3961 break;
3962 case VAR_DICT:
3963 matchdict = rettv.vval.v_dict;
3964 break;
3965 default:
3966 /* TODO: Give error message? */
3967 clear_tv(&rettv);
3968 break;
3969 }
3970 }
3971
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003972 if (curwin_save != curwin || curbuf_save != curbuf)
3973 {
3974 EMSG(_(e_complwin));
3975 goto theend;
3976 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00003977 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003978 check_cursor();
3979 if (!equalpos(curwin->w_cursor, pos))
3980 {
3981 EMSG(_(e_compldel));
3982 goto theend;
3983 }
Bram Moolenaar82139082011-09-14 16:52:09 +02003984
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003985 if (matchlist != NULL)
3986 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02003987 else if (matchdict != NULL)
3988 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003989
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003990theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02003991 if (matchdict != NULL)
3992 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003993 if (matchlist != NULL)
3994 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003995}
3996#endif /* FEAT_COMPL_FUNC */
3997
Bram Moolenaar39f05632006-03-19 22:15:26 +00003998#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003999/*
4000 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004001 */
4002 static void
4003ins_compl_add_list(list)
4004 list_T *list;
4005{
4006 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004007 int dir = compl_direction;
4008
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004009 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004010 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004011 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004012 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4013 /* if dir was BACKWARD then honor it just once */
4014 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004015 else if (did_emsg)
4016 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004017 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004018}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004019
4020/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004021 * Add completions from a dict.
4022 */
4023 static void
4024ins_compl_add_dict(dict)
4025 dict_T *dict;
4026{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004027 dictitem_T *di_refresh;
4028 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004029
4030 /* Check for optional "refresh" item. */
4031 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004032 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4033 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004034 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004035 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004036
4037 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4038 compl_opt_refresh_always = TRUE;
4039 }
4040
4041 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004042 di_words = dict_find(dict, (char_u *)"words", 5);
4043 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4044 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004045}
4046
4047/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004048 * Add a match to the list of matches from a typeval_T.
4049 * If the given string is already in the list of completions, then return
4050 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4051 * maybe because alloc() returns NULL, then FAIL is returned.
4052 */
4053 int
4054ins_compl_add_tv(tv, dir)
4055 typval_T *tv;
4056 int dir;
4057{
4058 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004059 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004060 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004061 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004062 char_u *(cptext[CPT_COUNT]);
4063
4064 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4065 {
4066 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4067 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4068 (char_u *)"abbr", FALSE);
4069 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4070 (char_u *)"menu", FALSE);
4071 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4072 (char_u *)"kind", FALSE);
4073 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4074 (char_u *)"info", FALSE);
4075 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4076 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004077 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004078 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004079 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4080 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004081 }
4082 else
4083 {
4084 word = get_tv_string_chk(tv);
4085 vim_memset(cptext, 0, sizeof(cptext));
4086 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004087 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004088 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004089 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004090}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004091#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004093/*
4094 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004095 * The search starts at position "ini" in curbuf and in the direction
4096 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004097 * When "compl_started" is FALSE start at that position, otherwise continue
4098 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004099 * This may return before finding all the matches.
4100 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004101 */
4102 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004103ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004104 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105{
4106 static pos_T first_match_pos;
4107 static pos_T last_match_pos;
4108 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004109 static int found_all = FALSE; /* Found all matches of a
4110 certain type. */
4111 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
Bram Moolenaar572cb562005-08-05 21:35:02 +00004113 pos_T *pos;
4114 char_u **matches;
4115 int save_p_scs;
4116 int save_p_ws;
4117 int save_p_ic;
4118 int i;
4119 int num_matches;
4120 int len;
4121 int found_new_match;
4122 int type = ctrl_x_mode;
4123 char_u *ptr;
4124 char_u *dict = NULL;
4125 int dict_f = 0;
4126 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004128 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 {
4130 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4131 ins_buf->b_scanned = 0;
4132 found_all = FALSE;
4133 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004134 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004135 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 last_match_pos = first_match_pos = *ini;
4137 }
4138
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004140 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4142 for (;;)
4143 {
4144 found_new_match = FAIL;
4145
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004146 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 * or if found_all says this entry is done. For ^X^L only use the
4148 * entries from 'complete' that look in loaded buffers. */
4149 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004150 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 {
4152 found_all = FALSE;
4153 while (*e_cpt == ',' || *e_cpt == ' ')
4154 e_cpt++;
4155 if (*e_cpt == '.' && !curbuf->b_scanned)
4156 {
4157 ins_buf = curbuf;
4158 first_match_pos = *ini;
4159 /* So that ^N can match word immediately after cursor */
4160 if (ctrl_x_mode == 0)
4161 dec(&first_match_pos);
4162 last_match_pos = first_match_pos;
4163 type = 0;
4164 }
4165 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4166 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4167 {
4168 /* Scan a buffer, but not the current one. */
4169 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4170 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004171 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 first_match_pos.col = last_match_pos.col = 0;
4173 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4174 last_match_pos.lnum = 0;
4175 type = 0;
4176 }
4177 else /* unloaded buffer, scan like dictionary */
4178 {
4179 found_all = TRUE;
4180 if (ins_buf->b_fname == NULL)
4181 continue;
4182 type = CTRL_X_DICTIONARY;
4183 dict = ins_buf->b_fname;
4184 dict_f = DICT_EXACT;
4185 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004186 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 ins_buf->b_fname == NULL
4188 ? buf_spname(ins_buf)
4189 : ins_buf->b_sfname == NULL
4190 ? (char *)ins_buf->b_fname
4191 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004192 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 }
4194 else if (*e_cpt == NUL)
4195 break;
4196 else
4197 {
4198 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4199 type = -1;
4200 else if (*e_cpt == 'k' || *e_cpt == 's')
4201 {
4202 if (*e_cpt == 'k')
4203 type = CTRL_X_DICTIONARY;
4204 else
4205 type = CTRL_X_THESAURUS;
4206 if (*++e_cpt != ',' && *e_cpt != NUL)
4207 {
4208 dict = e_cpt;
4209 dict_f = DICT_FIRST;
4210 }
4211 }
4212#ifdef FEAT_FIND_ID
4213 else if (*e_cpt == 'i')
4214 type = CTRL_X_PATH_PATTERNS;
4215 else if (*e_cpt == 'd')
4216 type = CTRL_X_PATH_DEFINES;
4217#endif
4218 else if (*e_cpt == ']' || *e_cpt == 't')
4219 {
4220 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004221 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004222 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224 else
4225 type = -1;
4226
4227 /* in any case e_cpt is advanced to the next entry */
4228 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4229
4230 found_all = TRUE;
4231 if (type == -1)
4232 continue;
4233 }
4234 }
4235
4236 switch (type)
4237 {
4238 case -1:
4239 break;
4240#ifdef FEAT_FIND_ID
4241 case CTRL_X_PATH_PATTERNS:
4242 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004243 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004244 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4248 (linenr_T)1, (linenr_T)MAXLNUM);
4249 break;
4250#endif
4251
4252 case CTRL_X_DICTIONARY:
4253 case CTRL_X_THESAURUS:
4254 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004255 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 : (type == CTRL_X_THESAURUS
4257 ? (*curbuf->b_p_tsr == NUL
4258 ? p_tsr
4259 : curbuf->b_p_tsr)
4260 : (*curbuf->b_p_dict == NUL
4261 ? p_dict
4262 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004263 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004264 dict != NULL ? dict_f
4265 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 dict = NULL;
4267 break;
4268
4269 case CTRL_X_TAGS:
4270 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4271 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004274 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004275 * of matches is found when compl_pattern is empty */
4276 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4278 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4279 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4280 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004281 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004282 }
4283 p_ic = save_p_ic;
4284 break;
4285
4286 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004287 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4289 {
4290
4291 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004292 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004293 ins_compl_add_matches(num_matches, matches,
4294#ifdef CASE_INSENSITIVE_FILENAME
4295 TRUE
4296#else
4297 FALSE
4298#endif
4299 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 break;
4302
4303 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004304 if (expand_cmdline(&compl_xp, compl_pattern,
4305 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004307 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 break;
4309
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004310#ifdef FEAT_COMPL_FUNC
4311 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004312 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004313 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004314 break;
4315#endif
4316
Bram Moolenaar488c6512005-08-11 20:09:58 +00004317 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004318#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004319 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004320 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004321 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004322 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004323#endif
4324 break;
4325
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 default: /* normal ^P/^N and ^X^L */
4327 /*
4328 * If 'infercase' is set, don't use 'smartcase' here
4329 */
4330 save_p_scs = p_scs;
4331 if (ins_buf->b_p_inf)
4332 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004333
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 /* buffers other than curbuf are scanned from the beginning or the
4335 * end but never from the middle, thus setting nowrapscan in this
4336 * buffers is a good idea, on the other hand, we always set
4337 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4338 save_p_ws = p_ws;
4339 if (ins_buf != curbuf)
4340 p_ws = FALSE;
4341 else if (*e_cpt == '.')
4342 p_ws = TRUE;
4343 for (;;)
4344 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004345 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004347 ++msg_silent; /* Don't want messages for wrapscan. */
4348
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004349 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4350 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004352 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004354 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004356 found_new_match = searchit(NULL, ins_buf, pos,
4357 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004358 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004359 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004360 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004363 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004364 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 first_match_pos = *pos;
4366 last_match_pos = *pos;
4367 }
4368 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004369 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 found_new_match = FAIL;
4371 if (found_new_match == FAIL)
4372 {
4373 if (ins_buf == curbuf)
4374 found_all = TRUE;
4375 break;
4376 }
4377
4378 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004379 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 && ini->lnum == pos->lnum
4381 && ini->col == pos->col)
4382 continue;
4383 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4384 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4385 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004386 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004387 {
4388 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4389 continue;
4390 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4391 if (!p_paste)
4392 ptr = skipwhite(ptr);
4393 }
4394 len = (int)STRLEN(ptr);
4395 }
4396 else
4397 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004398 char_u *tmp_ptr = ptr;
4399
4400 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004402 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 /* Skip if already inside a word. */
4404 if (vim_iswordp(tmp_ptr))
4405 continue;
4406 /* Find start of next word. */
4407 tmp_ptr = find_word_start(tmp_ptr);
4408 }
4409 /* Find end of this word. */
4410 tmp_ptr = find_word_end(tmp_ptr);
4411 len = (int)(tmp_ptr - ptr);
4412
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004413 if ((compl_cont_status & CONT_ADDING)
4414 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 {
4416 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4417 {
4418 /* Try next line, if any. the new word will be
4419 * "join" as if the normal command "J" was used.
4420 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004421 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422 * works -- Acevedo */
4423 STRNCPY(IObuff, ptr, len);
4424 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4425 tmp_ptr = ptr = skipwhite(ptr);
4426 /* Find start of next word. */
4427 tmp_ptr = find_word_start(tmp_ptr);
4428 /* Find end of next word. */
4429 tmp_ptr = find_word_end(tmp_ptr);
4430 if (tmp_ptr > ptr)
4431 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004432 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004434 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004435 IObuff[len++] = ' ';
4436 /* IObuf =~ "\k.* ", thus len >= 2 */
4437 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004438 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439 || (vim_strchr(p_cpo, CPO_JOINSP)
4440 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004441 && (IObuff[len - 2] == '?'
4442 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004443 IObuff[len++] = ' ';
4444 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004445 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004446 if (tmp_ptr - ptr >= IOSIZE - len)
4447 tmp_ptr = ptr + IOSIZE - len - 1;
4448 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4449 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004450 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452 IObuff[len] = NUL;
4453 ptr = IObuff;
4454 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004455 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004456 continue;
4457 }
4458 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004459 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004460 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004461 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004462 {
4463 found_new_match = OK;
4464 break;
4465 }
4466 }
4467 p_scs = save_p_scs;
4468 p_ws = save_p_ws;
4469 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004470
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004471 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004472 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004473 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 found_new_match = OK;
4475
4476 /* break the loop for specialized modes (use 'complete' just for the
4477 * generic ctrl_x_mode == 0) or when we've found a new match */
4478 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004479 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004480 {
4481 if (got_int)
4482 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004483 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004484 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004485 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004486
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004487 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4488 || compl_interrupted)
4489 break;
4490 compl_started = TRUE;
4491 }
4492 else
4493 {
4494 /* Mark a buffer scanned when it has been scanned completely */
4495 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4496 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004498 compl_started = FALSE;
4499 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004500 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004501 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502
4503 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4504 && *e_cpt == NUL) /* Got to end of 'complete' */
4505 found_new_match = FAIL;
4506
4507 i = -1; /* total of matches, unknown */
4508 if (found_new_match == FAIL
4509 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4510 i = ins_compl_make_cyclic();
4511
4512 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004513 * just been made cyclic then we have to move compl_curr_match to the next
4514 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004515 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4516 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004517 if (compl_curr_match == NULL)
4518 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004519 return i;
4520}
4521
4522/* Delete the old text being completed. */
4523 static void
4524ins_compl_delete()
4525{
4526 int i;
4527
4528 /*
4529 * In insert mode: Delete the typed part.
4530 * In replace mode: Put the old characters back, if any.
4531 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004532 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004533 backspace_until_column(i);
4534 changed_cline_bef_curs();
4535}
4536
4537/* Insert the new text being completed. */
4538 static void
4539ins_compl_insert()
4540{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004541 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004542 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4543 compl_used_match = FALSE;
4544 else
4545 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546}
4547
4548/*
4549 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004550 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4551 * get more completions. If it is FALSE, then we just do nothing when there
4552 * are no more completions in a given direction. The latter case is used when
4553 * we are still in the middle of finding completions, to allow browsing
4554 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 * Return the total number of matches, or -1 if still unknown -- webb.
4556 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004557 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4558 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004559 *
4560 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004561 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4562 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 */
4564 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004565ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004567 int count; /* repeat completion this many times; should
4568 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004569 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570{
4571 int num_matches = -1;
4572 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004573 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004574 compl_T *found_compl = NULL;
4575 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004576 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004578 /* When user complete function return -1 for findstart which is next
4579 * time of 'always', compl_shown_match become NULL. */
4580 if (compl_shown_match == NULL)
4581 return -1;
4582
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004583 if (compl_leader != NULL
4584 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004585 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004586 /* Set "compl_shown_match" to the actually shown match, it may differ
4587 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004588 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004589 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004590 && compl_shown_match->cp_next != NULL
4591 && compl_shown_match->cp_next != compl_first_match)
4592 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004593
4594 /* If we didn't find it searching forward, and compl_shows_dir is
4595 * backward, find the last match. */
4596 if (compl_shows_dir == BACKWARD
4597 && !ins_compl_equal(compl_shown_match,
4598 compl_leader, (int)STRLEN(compl_leader))
4599 && (compl_shown_match->cp_next == NULL
4600 || compl_shown_match->cp_next == compl_first_match))
4601 {
4602 while (!ins_compl_equal(compl_shown_match,
4603 compl_leader, (int)STRLEN(compl_leader))
4604 && compl_shown_match->cp_prev != NULL
4605 && compl_shown_match->cp_prev != compl_first_match)
4606 compl_shown_match = compl_shown_match->cp_prev;
4607 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004608 }
4609
4610 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004611 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 /* Delete old text to be replaced */
4613 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004614
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004615 /* When finding the longest common text we stick at the original text,
4616 * don't let CTRL-N or CTRL-P move to the first match. */
4617 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4618
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004619 /* When restarting the search don't insert the first match either. */
4620 if (compl_restarting)
4621 {
4622 advance = FALSE;
4623 compl_restarting = FALSE;
4624 }
4625
Bram Moolenaare3226be2005-12-18 22:10:00 +00004626 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4627 * around. */
4628 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004630 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004631 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004632 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004633 found_end = (compl_first_match != NULL
4634 && (compl_shown_match->cp_next == compl_first_match
4635 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004636 }
4637 else if (compl_shows_dir == BACKWARD
4638 && compl_shown_match->cp_prev != NULL)
4639 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004640 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004641 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004642 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 }
4644 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004645 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004646 if (!allow_get_expansion)
4647 {
4648 if (advance)
4649 {
4650 if (compl_shows_dir == BACKWARD)
4651 compl_pending -= todo + 1;
4652 else
4653 compl_pending += todo + 1;
4654 }
4655 return -1;
4656 }
4657
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004658 if (advance)
4659 {
4660 if (compl_shows_dir == BACKWARD)
4661 --compl_pending;
4662 else
4663 ++compl_pending;
4664 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004665
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004666 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004667 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004668
4669 /* handle any pending completions */
4670 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004671 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004672 {
4673 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4674 {
4675 compl_shown_match = compl_shown_match->cp_next;
4676 --compl_pending;
4677 }
4678 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4679 {
4680 compl_shown_match = compl_shown_match->cp_prev;
4681 ++compl_pending;
4682 }
4683 else
4684 break;
4685 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004686 found_end = FALSE;
4687 }
4688 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4689 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004690 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004691 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004692 ++todo;
4693 else
4694 /* Remember a matching item. */
4695 found_compl = compl_shown_match;
4696
4697 /* Stop at the end of the list when we found a usable match. */
4698 if (found_end)
4699 {
4700 if (found_compl != NULL)
4701 {
4702 compl_shown_match = found_compl;
4703 break;
4704 }
4705 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 }
4708
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004709 /* Insert the text of the new completion, or the compl_leader. */
4710 if (insert_match)
4711 {
4712 if (!compl_get_longest || compl_used_match)
4713 ins_compl_insert();
4714 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004715 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004716 }
4717 else
4718 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719
4720 if (!allow_get_expansion)
4721 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004722 /* may undisplay the popup menu first */
4723 ins_compl_upd_pum();
4724
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004725 /* redraw to show the user what was inserted */
4726 update_screen(0);
4727
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004728 /* display the updated popup menu */
4729 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004730#ifdef FEAT_GUI
4731 if (gui.in_use)
4732 {
4733 /* Show the cursor after the match, not after the redrawn text. */
4734 setcursor();
4735 out_flush();
4736 gui_update_cursor(FALSE, FALSE);
4737 }
4738#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 /* Delete old text to be replaced, since we're still searching and
4741 * don't want to match ourselves! */
4742 ins_compl_delete();
4743 }
4744
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004745 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004746 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004747 compl_enter_selects = !insert_match && compl_match_array != NULL;
4748
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 /*
4750 * Show the file name for the match (if any)
4751 * Truncate the file name to avoid a wait for return.
4752 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004753 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 {
4755 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004756 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 if (i <= 0)
4758 i = 0;
4759 else
4760 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004761 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 msg(IObuff);
4763 redraw_cmdline = FALSE; /* don't overwrite! */
4764 }
4765
4766 return num_matches;
4767}
4768
4769/*
4770 * Call this while finding completions, to check whether the user has hit a key
4771 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004772 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004774 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 */
4776 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004777ins_compl_check_keys(frequency)
4778 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779{
4780 static int count = 0;
4781
4782 int c;
4783
4784 /* Don't check when reading keys from a script. That would break the test
4785 * scripts */
4786 if (using_script())
4787 return;
4788
4789 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004790 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 return;
4792 count = 0;
4793
Bram Moolenaara260a972006-06-23 19:36:29 +00004794 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4795 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 if (c != NUL)
4798 {
4799 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4800 {
4801 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004802 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004803 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4804 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004805 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004806 else
4807 {
4808 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004809 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004810 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004811 if (c != K_IGNORE)
4812 {
4813 /* Don't interrupt completion when the character wasn't typed,
4814 * e.g., when doing @q to replay keys. */
4815 if (c != Ctrl_R && KeyTyped)
4816 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004817
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004818 vungetc(c);
4819 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004822 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004823 {
4824 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4825
4826 compl_pending = 0;
4827 (void)ins_compl_next(FALSE, todo, TRUE);
4828 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004829}
4830
4831/*
4832 * Decide the direction of Insert mode complete from the key typed.
4833 * Returns BACKWARD or FORWARD.
4834 */
4835 static int
4836ins_compl_key2dir(c)
4837 int c;
4838{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004839 if (c == Ctrl_P || c == Ctrl_L
4840 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4841 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004842 return BACKWARD;
4843 return FORWARD;
4844}
4845
4846/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004847 * Return TRUE for keys that are used for completion only when the popup menu
4848 * is visible.
4849 */
4850 static int
4851ins_compl_pum_key(c)
4852 int c;
4853{
4854 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004855 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4856 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004857}
4858
4859/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004860 * Decide the number of completions to move forward.
4861 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4862 */
4863 static int
4864ins_compl_key2count(c)
4865 int c;
4866{
4867 int h;
4868
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004869 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004870 {
4871 h = pum_get_height();
4872 if (h > 3)
4873 h -= 2; /* keep some context */
4874 return h;
4875 }
4876 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877}
4878
4879/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004880 * Return TRUE if completion with "c" should insert the match, FALSE if only
4881 * to change the currently selected completion.
4882 */
4883 static int
4884ins_compl_use_match(c)
4885 int c;
4886{
4887 switch (c)
4888 {
4889 case K_UP:
4890 case K_DOWN:
4891 case K_PAGEDOWN:
4892 case K_KPAGEDOWN:
4893 case K_S_DOWN:
4894 case K_PAGEUP:
4895 case K_KPAGEUP:
4896 case K_S_UP:
4897 return FALSE;
4898 }
4899 return TRUE;
4900}
4901
4902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 * Do Insert mode completion.
4904 * Called when character "c" was typed, which has a meaning for completion.
4905 * Returns OK if completion was done, FAIL if something failed (out of mem).
4906 */
4907 static int
4908ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004909 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004911 char_u *line;
4912 int startcol = 0; /* column where searched text starts */
4913 colnr_T curs_col; /* cursor column */
4914 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004915 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916
Bram Moolenaare3226be2005-12-18 22:10:00 +00004917 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004918 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004919 {
4920 /* First time we hit ^N or ^P (in a row, I mean) */
4921
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 did_ai = FALSE;
4923#ifdef FEAT_SMARTINDENT
4924 did_si = FALSE;
4925 can_si = FALSE;
4926 can_si_back = FALSE;
4927#endif
4928 if (stop_arrow() == FAIL)
4929 return FAIL;
4930
4931 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004932 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004933 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004935 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004936 * "compl_startpos" to the cursor as a pattern to add a new word
4937 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004938 * "compl_startpos" is not in the same line as the cursor then fix it
4939 * (the line has been split because it was longer than 'tw'). if SOL
4940 * is set then skip the previous pattern, a word at the beginning of
4941 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004942 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4943 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 {
4945 /*
4946 * it is a continued search
4947 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004948 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4950 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4951 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004952 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004954 /* line (probably) wrapped, set compl_startpos to the
4955 * first non_blank in the line, if it is not a wordchar
4956 * include it to get a better pattern, but then we don't
4957 * want the "\\<" prefix, check it bellow */
4958 compl_col = (colnr_T)(skipwhite(line) - line);
4959 compl_startpos.col = compl_col;
4960 compl_startpos.lnum = curwin->w_cursor.lnum;
4961 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 }
4963 else
4964 {
4965 /* S_IPOS was set when we inserted a word that was at the
4966 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004967 * mode but first we need to redefine compl_startpos */
4968 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004970 compl_cont_status |= CONT_SOL;
4971 compl_startpos.col = (colnr_T)(skipwhite(
4972 line + compl_length
4973 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004975 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004977 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004978 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004979 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004981 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004983 compl_cont_status &= ~CONT_SOL;
4984 compl_length = (IOSIZE - MIN_SPACE);
4985 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004987 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4988 if (compl_length < 1)
4989 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 }
4991 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004992 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004994 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004995 }
4996 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004997 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004999 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005001 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005003 compl_cont_status = 0;
5004 compl_cont_status |= CONT_N_ADDS;
5005 compl_startpos = curwin->w_cursor;
5006 startcol = (int)curs_col;
5007 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005008 }
5009
5010 /* Work out completion pattern and original text -- webb */
5011 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5012 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005013 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5015 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005016 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005018 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005020 compl_col += ++startcol;
5021 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005024 compl_pattern = str_foldcase(line + compl_col,
5025 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005027 compl_pattern = vim_strnsave(line + compl_col,
5028 compl_length);
5029 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 return FAIL;
5031 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005032 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 {
5034 char_u *prefix = (char_u *)"\\<";
5035
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005036 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005037 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005038 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005039 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005041 if (!vim_iswordp(line + compl_col)
5042 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 && (
5044#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005045 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005047 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048#endif
5049 )))
5050 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005051 STRCPY((char *)compl_pattern, prefix);
5052 (void)quote_meta(compl_pattern + STRLEN(prefix),
5053 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005055 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005057 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005059 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060#endif
5061 )
5062 {
5063 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005064 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5065 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005067 compl_col += curs_col;
5068 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 }
5070 else
5071 {
5072#ifdef FEAT_MBYTE
5073 /* Search the point of change class of multibyte character
5074 * or not a word single byte character backward. */
5075 if (has_mbyte)
5076 {
5077 int base_class;
5078 int head_off;
5079
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005080 startcol -= (*mb_head_off)(line, line + startcol);
5081 base_class = mb_get_class(line + startcol);
5082 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 head_off = (*mb_head_off)(line, line + startcol);
5085 if (base_class != mb_get_class(line + startcol
5086 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005088 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 }
5090 }
5091 else
5092#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005093 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 compl_col += ++startcol;
5096 compl_length = (int)curs_col - startcol;
5097 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
5099 /* Only match word with at least two chars -- webb
5100 * there's no need to call quote_meta,
5101 * alloc(7) is enough -- Acevedo
5102 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 compl_pattern = alloc(7);
5104 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005106 STRCPY((char *)compl_pattern, "\\<");
5107 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5108 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 }
5110 else
5111 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005113 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005114 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 STRCPY((char *)compl_pattern, "\\<");
5117 (void)quote_meta(compl_pattern + 2, line + compl_col,
5118 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119 }
5120 }
5121 }
5122 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5123 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005124 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 compl_length = (int)curs_col - (int)compl_col;
5126 if (compl_length < 0) /* cursor in indent: empty pattern */
5127 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005129 compl_pattern = str_foldcase(line + compl_col, compl_length,
5130 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005132 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5133 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 return FAIL;
5135 }
5136 else if (ctrl_x_mode == CTRL_X_FILES)
5137 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005138 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005139 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005140 compl_col += ++startcol;
5141 compl_length = (int)curs_col - startcol;
5142 compl_pattern = addstar(line + compl_col, compl_length,
5143 EXPAND_FILES);
5144 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 return FAIL;
5146 }
5147 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5148 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005149 compl_pattern = vim_strnsave(line, curs_col);
5150 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 set_cmd_context(&compl_xp, compl_pattern,
5153 (int)STRLEN(compl_pattern), curs_col);
5154 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5155 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005156 /* No completion possible, use an empty pattern to get a
5157 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005158 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005159 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005160 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5161 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005163 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005164 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005165#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005166 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005167 * Call user defined function 'completefunc' with "a:findstart"
5168 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005169 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005170 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005171 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005172 char_u *funcname;
5173 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005174 win_T *curwin_save;
5175 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005176
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005177 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005178 * string */
5179 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5180 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5181 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005182 {
5183 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5184 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005185 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005186 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005187
5188 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005189 args[1] = NULL;
5190 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005191 curwin_save = curwin;
5192 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005193 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005194 if (curwin_save != curwin || curbuf_save != curbuf)
5195 {
5196 EMSG(_(e_complwin));
5197 return FAIL;
5198 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005199 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005200 check_cursor();
5201 if (!equalpos(curwin->w_cursor, pos))
5202 {
5203 EMSG(_(e_compldel));
5204 return FAIL;
5205 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005206
Bram Moolenaar6110a002012-01-26 18:58:38 +01005207 /* Return value -2 means the user complete function wants to
5208 * cancel the complete without an error. */
5209 if (col == -2)
5210 return FAIL;
5211
Bram Moolenaar82139082011-09-14 16:52:09 +02005212 /*
5213 * Reset extended parameters of completion, when start new
5214 * completion.
5215 */
5216 compl_opt_refresh_always = FALSE;
5217
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005218 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005219 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005220 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005221 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005222 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005223
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005224 /* Setup variables for completion. Need to obtain "line" again,
5225 * it may have become invalid. */
5226 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005227 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005228 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5229 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005230#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005231 return FAIL;
5232 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005233 else if (ctrl_x_mode == CTRL_X_SPELL)
5234 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005235#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005236 if (spell_bad_len > 0)
5237 compl_col = curs_col - spell_bad_len;
5238 else
5239 compl_col = spell_word_start(startcol);
5240 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005241 {
5242 compl_length = 0;
5243 compl_col = curs_col;
5244 }
5245 else
5246 {
5247 spell_expand_check_cap(compl_col);
5248 compl_length = (int)curs_col - compl_col;
5249 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005250 /* Need to obtain "line" again, it may have become invalid. */
5251 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005252 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5253 if (compl_pattern == NULL)
5254#endif
5255 return FAIL;
5256 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005257 else
5258 {
5259 EMSG2(_(e_intern2), "ins_complete()");
5260 return FAIL;
5261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005263 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264 {
5265 edit_submode_pre = (char_u *)_(" Adding");
5266 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5267 {
5268 /* Insert a new line, keep indentation but ignore 'comments' */
5269#ifdef FEAT_COMMENTS
5270 char_u *old = curbuf->b_p_com;
5271
5272 curbuf->b_p_com = (char_u *)"";
5273#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005274 compl_startpos.lnum = curwin->w_cursor.lnum;
5275 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 ins_eol('\r');
5277#ifdef FEAT_COMMENTS
5278 curbuf->b_p_com = old;
5279#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005280 compl_length = 0;
5281 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282 }
5283 }
5284 else
5285 {
5286 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005287 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 }
5289
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005290 if (compl_cont_status & CONT_LOCAL)
5291 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 else
5293 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5294
Bram Moolenaar62951b12011-09-21 18:23:05 +02005295 /* If any of the original typed text has been changed we need to fix
5296 * the redo buffer. */
5297 ins_compl_fixRedoBufForLeader(NULL);
5298
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005299 /* Always add completion for the original text. */
5300 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005301 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5302 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005303 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005305 vim_free(compl_pattern);
5306 compl_pattern = NULL;
5307 vim_free(compl_orig_text);
5308 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005309 return FAIL;
5310 }
5311
5312 /* showmode might reset the internal line pointers, so it must
5313 * be called before line = ml_get(), or when this address is no
5314 * longer needed. -- Acevedo.
5315 */
5316 edit_submode_extra = (char_u *)_("-- Searching...");
5317 edit_submode_highl = HLF_COUNT;
5318 showmode();
5319 edit_submode_extra = NULL;
5320 out_flush();
5321 }
5322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005323 compl_shown_match = compl_curr_match;
5324 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005325
5326 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005327 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005329 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005330 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005331
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005332 /* may undisplay the popup menu */
5333 ins_compl_upd_pum();
5334
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005335 if (n > 1) /* all matches have been found */
5336 compl_matches = n;
5337 compl_curr_match = compl_shown_match;
5338 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005339
Bram Moolenaard68071d2006-05-02 22:08:30 +00005340 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5341 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005342 if (got_int && !global_busy)
5343 {
5344 (void)vgetc();
5345 got_int = FALSE;
5346 }
5347
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005348 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005349 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005350 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005351 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5352 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005353 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5354 edit_submode_highl = HLF_E;
5355 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5356 * because we couldn't expand anything at first place, but if we used
5357 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5358 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005359 if ( compl_length > 1
5360 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005361 || (ctrl_x_mode != 0
5362 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5363 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005364 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 }
5366
Bram Moolenaar572cb562005-08-05 21:35:02 +00005367 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005368 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005369 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005370 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005371
5372 if (edit_submode_extra == NULL)
5373 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005374 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005375 {
5376 edit_submode_extra = (char_u *)_("Back at original");
5377 edit_submode_highl = HLF_W;
5378 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005379 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005380 {
5381 edit_submode_extra = (char_u *)_("Word from other line");
5382 edit_submode_highl = HLF_COUNT;
5383 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005384 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005385 {
5386 edit_submode_extra = (char_u *)_("The only match");
5387 edit_submode_highl = HLF_COUNT;
5388 }
5389 else
5390 {
5391 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005392 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005393 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005394 int number = 0;
5395 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005396
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005397 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005398 {
5399 /* search backwards for the first valid (!= -1) number.
5400 * This should normally succeed already at the first loop
5401 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005402 for (match = compl_curr_match->cp_prev; match != NULL
5403 && match != compl_first_match;
5404 match = match->cp_prev)
5405 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005406 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005407 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005408 break;
5409 }
5410 if (match != NULL)
5411 /* go up and assign all numbers which are not assigned
5412 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005413 for (match = match->cp_next;
5414 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005415 match = match->cp_next)
5416 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005417 }
5418 else /* BACKWARD */
5419 {
5420 /* search forwards (upwards) for the first valid (!= -1)
5421 * number. This should normally succeed already at the
5422 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005423 for (match = compl_curr_match->cp_next; match != NULL
5424 && match != compl_first_match;
5425 match = match->cp_next)
5426 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005427 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005428 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005429 break;
5430 }
5431 if (match != NULL)
5432 /* go down and assign all numbers which are not
5433 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005434 for (match = match->cp_prev; match
5435 && match->cp_number == -1;
5436 match = match->cp_prev)
5437 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005438 }
5439 }
5440
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005441 /* The match should always have a sequence number now, this is
5442 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005443 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005445 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5446 * Translations may need more than twice that. */
5447 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005448
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005449 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005450 vim_snprintf((char *)match_ref, sizeof(match_ref),
5451 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005452 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005453 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005454 vim_snprintf((char *)match_ref, sizeof(match_ref),
5455 _("match %d"),
5456 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005457 edit_submode_extra = match_ref;
5458 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005459 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005460 curs_columns(FALSE);
5461 }
5462 }
5463 }
5464
5465 /* Show a message about what (completion) mode we're in. */
5466 showmode();
5467 if (edit_submode_extra != NULL)
5468 {
5469 if (!p_smd)
5470 msg_attr(edit_submode_extra,
5471 edit_submode_highl < HLF_COUNT
5472 ? hl_attr(edit_submode_highl) : 0);
5473 }
5474 else
5475 msg_clr_cmdline(); /* necessary for "noshowmode" */
5476
Bram Moolenaard68071d2006-05-02 22:08:30 +00005477 /* Show the popup menu, unless we got interrupted. */
5478 if (!compl_interrupted)
5479 {
5480 /* RedrawingDisabled may be set when invoked through complete(). */
5481 n = RedrawingDisabled;
5482 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005483
5484 /* If the cursor moved we need to remove the pum first. */
5485 setcursor();
5486 if (save_w_wrow != curwin->w_wrow)
5487 ins_compl_del_pum();
5488
Bram Moolenaard68071d2006-05-02 22:08:30 +00005489 ins_compl_show_pum();
5490 setcursor();
5491 RedrawingDisabled = n;
5492 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005493 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005494 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005495
Bram Moolenaar071d4272004-06-13 20:20:40 +00005496 return OK;
5497}
5498
5499/*
5500 * Looks in the first "len" chars. of "src" for search-metachars.
5501 * If dest is not NULL the chars. are copied there quoting (with
5502 * a backslash) the metachars, and dest would be NUL terminated.
5503 * Returns the length (needed) of dest
5504 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005505 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506quote_meta(dest, src, len)
5507 char_u *dest;
5508 char_u *src;
5509 int len;
5510{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005511 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005513 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005514 {
5515 switch (*src)
5516 {
5517 case '.':
5518 case '*':
5519 case '[':
5520 if (ctrl_x_mode == CTRL_X_DICTIONARY
5521 || ctrl_x_mode == CTRL_X_THESAURUS)
5522 break;
5523 case '~':
5524 if (!p_magic) /* quote these only if magic is set */
5525 break;
5526 case '\\':
5527 if (ctrl_x_mode == CTRL_X_DICTIONARY
5528 || ctrl_x_mode == CTRL_X_THESAURUS)
5529 break;
5530 case '^': /* currently it's not needed. */
5531 case '$':
5532 m++;
5533 if (dest != NULL)
5534 *dest++ = '\\';
5535 break;
5536 }
5537 if (dest != NULL)
5538 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005539# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005540 /* Copy remaining bytes of a multibyte character. */
5541 if (has_mbyte)
5542 {
5543 int i, mb_len;
5544
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005545 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005546 if (mb_len > 0 && len >= mb_len)
5547 for (i = 0; i < mb_len; ++i)
5548 {
5549 --len;
5550 ++src;
5551 if (dest != NULL)
5552 *dest++ = *src;
5553 }
5554 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005555# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556 }
5557 if (dest != NULL)
5558 *dest = NUL;
5559
5560 return m;
5561}
5562#endif /* FEAT_INS_EXPAND */
5563
5564/*
5565 * Next character is interpreted literally.
5566 * A one, two or three digit decimal number is interpreted as its byte value.
5567 * If one or two digits are entered, the next character is given to vungetc().
5568 * For Unicode a character > 255 may be returned.
5569 */
5570 int
5571get_literal()
5572{
5573 int cc;
5574 int nc;
5575 int i;
5576 int hex = FALSE;
5577 int octal = FALSE;
5578#ifdef FEAT_MBYTE
5579 int unicode = 0;
5580#endif
5581
5582 if (got_int)
5583 return Ctrl_C;
5584
5585#ifdef FEAT_GUI
5586 /*
5587 * In GUI there is no point inserting the internal code for a special key.
5588 * It is more useful to insert the string "<KEY>" instead. This would
5589 * probably be useful in a text window too, but it would not be
5590 * vi-compatible (maybe there should be an option for it?) -- webb
5591 */
5592 if (gui.in_use)
5593 ++allow_keys;
5594#endif
5595#ifdef USE_ON_FLY_SCROLL
5596 dont_scroll = TRUE; /* disallow scrolling here */
5597#endif
5598 ++no_mapping; /* don't map the next key hits */
5599 cc = 0;
5600 i = 0;
5601 for (;;)
5602 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005603 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005604#ifdef FEAT_CMDL_INFO
5605 if (!(State & CMDLINE)
5606# ifdef FEAT_MBYTE
5607 && MB_BYTE2LEN_CHECK(nc) == 1
5608# endif
5609 )
5610 add_to_showcmd(nc);
5611#endif
5612 if (nc == 'x' || nc == 'X')
5613 hex = TRUE;
5614 else if (nc == 'o' || nc == 'O')
5615 octal = TRUE;
5616#ifdef FEAT_MBYTE
5617 else if (nc == 'u' || nc == 'U')
5618 unicode = nc;
5619#endif
5620 else
5621 {
5622 if (hex
5623#ifdef FEAT_MBYTE
5624 || unicode != 0
5625#endif
5626 )
5627 {
5628 if (!vim_isxdigit(nc))
5629 break;
5630 cc = cc * 16 + hex2nr(nc);
5631 }
5632 else if (octal)
5633 {
5634 if (nc < '0' || nc > '7')
5635 break;
5636 cc = cc * 8 + nc - '0';
5637 }
5638 else
5639 {
5640 if (!VIM_ISDIGIT(nc))
5641 break;
5642 cc = cc * 10 + nc - '0';
5643 }
5644
5645 ++i;
5646 }
5647
5648 if (cc > 255
5649#ifdef FEAT_MBYTE
5650 && unicode == 0
5651#endif
5652 )
5653 cc = 255; /* limit range to 0-255 */
5654 nc = 0;
5655
5656 if (hex) /* hex: up to two chars */
5657 {
5658 if (i >= 2)
5659 break;
5660 }
5661#ifdef FEAT_MBYTE
5662 else if (unicode) /* Unicode: up to four or eight chars */
5663 {
5664 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5665 break;
5666 }
5667#endif
5668 else if (i >= 3) /* decimal or octal: up to three chars */
5669 break;
5670 }
5671 if (i == 0) /* no number entered */
5672 {
5673 if (nc == K_ZERO) /* NUL is stored as NL */
5674 {
5675 cc = '\n';
5676 nc = 0;
5677 }
5678 else
5679 {
5680 cc = nc;
5681 nc = 0;
5682 }
5683 }
5684
5685 if (cc == 0) /* NUL is stored as NL */
5686 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005687#ifdef FEAT_MBYTE
5688 if (enc_dbcs && (cc & 0xff) == 0)
5689 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5690 second byte will cause trouble! */
5691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005692
5693 --no_mapping;
5694#ifdef FEAT_GUI
5695 if (gui.in_use)
5696 --allow_keys;
5697#endif
5698 if (nc)
5699 vungetc(nc);
5700 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5701 return cc;
5702}
5703
5704/*
5705 * Insert character, taking care of special keys and mod_mask
5706 */
5707 static void
5708insert_special(c, allow_modmask, ctrlv)
5709 int c;
5710 int allow_modmask;
5711 int ctrlv; /* c was typed after CTRL-V */
5712{
5713 char_u *p;
5714 int len;
5715
5716 /*
5717 * Special function key, translate into "<Key>". Up to the last '>' is
5718 * inserted with ins_str(), so as not to replace characters in replace
5719 * mode.
5720 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5721 * unless 'allow_modmask' is TRUE.
5722 */
5723#ifdef MACOS
5724 /* Command-key never produces a normal key */
5725 if (mod_mask & MOD_MASK_CMD)
5726 allow_modmask = TRUE;
5727#endif
5728 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5729 {
5730 p = get_special_key_name(c, mod_mask);
5731 len = (int)STRLEN(p);
5732 c = p[len - 1];
5733 if (len > 2)
5734 {
5735 if (stop_arrow() == FAIL)
5736 return;
5737 p[len - 1] = NUL;
5738 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005739 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005740 ctrlv = FALSE;
5741 }
5742 }
5743 if (stop_arrow() == OK)
5744 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5745}
5746
5747/*
5748 * Special characters in this context are those that need processing other
5749 * than the simple insertion that can be performed here. This includes ESC
5750 * which terminates the insert, and CR/NL which need special processing to
5751 * open up a new line. This routine tries to optimize insertions performed by
5752 * the "redo", "undo" or "put" commands, so it needs to know when it should
5753 * stop and defer processing to the "normal" mechanism.
5754 * '0' and '^' are special, because they can be followed by CTRL-D.
5755 */
5756#ifdef EBCDIC
5757# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5758#else
5759# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5760#endif
5761
5762#ifdef FEAT_MBYTE
5763# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5764#else
5765# define WHITECHAR(cc) vim_iswhite(cc)
5766#endif
5767
5768 void
5769insertchar(c, flags, second_indent)
5770 int c; /* character to insert or NUL */
5771 int flags; /* INSCHAR_FORMAT, etc. */
5772 int second_indent; /* indent for second line if >= 0 */
5773{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005774 int textwidth;
5775#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005776 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779
5780 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5781 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782
5783 /*
5784 * Try to break the line in two or more pieces when:
5785 * - Always do this if we have been called to do formatting only.
5786 * - Always do this when 'formatoptions' has the 'a' flag and the line
5787 * ends in white space.
5788 * - Otherwise:
5789 * - Don't do this if inserting a blank
5790 * - Don't do this if an existing character is being replaced, unless
5791 * we're in VREPLACE mode.
5792 * - Do this if the cursor is not on the line where insert started
5793 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5794 * before the insert.
5795 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5796 * before 'textwidth'
5797 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005798 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 && ((flags & INSCHAR_FORMAT)
5800 || (!vim_iswhite(c)
5801 && !((State & REPLACE_FLAG)
5802#ifdef FEAT_VREPLACE
5803 && !(State & VREPLACE_FLAG)
5804#endif
5805 && *ml_get_cursor() != NUL)
5806 && (curwin->w_cursor.lnum != Insstart.lnum
5807 || ((!has_format_option(FO_INS_LONG)
5808 || Insstart_textlen <= (colnr_T)textwidth)
5809 && (!fo_ins_blank
5810 || Insstart_blank_vcol <= (colnr_T)textwidth
5811 ))))))
5812 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005813 /* Format with 'formatexpr' when it's set. Use internal formatting
5814 * when 'formatexpr' isn't set or it returns non-zero. */
5815#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005816 int do_internal = TRUE;
5817
Bram Moolenaar81a82092008-03-12 16:27:00 +00005818 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005819 {
5820 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5821 /* It may be required to save for undo again, e.g. when setline()
5822 * was called. */
5823 ins_need_undo = TRUE;
5824 }
5825 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005826#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005827 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005829
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 if (c == NUL) /* only formatting was wanted */
5831 return;
5832
5833#ifdef FEAT_COMMENTS
5834 /* Check whether this character should end a comment. */
5835 if (did_ai && (int)c == end_comment_pending)
5836 {
5837 char_u *line;
5838 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5839 int middle_len, end_len;
5840 int i;
5841
5842 /*
5843 * Need to remove existing (middle) comment leader and insert end
5844 * comment leader. First, check what comment leader we can find.
5845 */
5846 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5847 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5848 {
5849 /* Skip middle-comment string */
5850 while (*p && p[-1] != ':') /* find end of middle flags */
5851 ++p;
5852 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5853 /* Don't count trailing white space for middle_len */
5854 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5855 --middle_len;
5856
5857 /* Find the end-comment string */
5858 while (*p && p[-1] != ':') /* find end of end flags */
5859 ++p;
5860 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5861
5862 /* Skip white space before the cursor */
5863 i = curwin->w_cursor.col;
5864 while (--i >= 0 && vim_iswhite(line[i]))
5865 ;
5866 i++;
5867
5868 /* Skip to before the middle leader */
5869 i -= middle_len;
5870
5871 /* Check some expected things before we go on */
5872 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5873 {
5874 /* Backspace over all the stuff we want to replace */
5875 backspace_until_column(i);
5876
5877 /*
5878 * Insert the end-comment string, except for the last
5879 * character, which will get inserted as normal later.
5880 */
5881 ins_bytes_len(lead_end, end_len - 1);
5882 }
5883 }
5884 }
5885 end_comment_pending = NUL;
5886#endif
5887
5888 did_ai = FALSE;
5889#ifdef FEAT_SMARTINDENT
5890 did_si = FALSE;
5891 can_si = FALSE;
5892 can_si_back = FALSE;
5893#endif
5894
5895 /*
5896 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5897 * This speeds up normal text input considerably.
5898 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5899 * need to re-indent at a ':', or any other character (but not what
5900 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005901 * Don't do this when there an InsertCharPre autocommand is defined,
5902 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005903 */
5904#ifdef USE_ON_FLY_SCROLL
5905 dont_scroll = FALSE; /* allow scrolling here */
5906#endif
5907
5908 if ( !ISSPECIAL(c)
5909#ifdef FEAT_MBYTE
5910 && (!has_mbyte || (*mb_char2len)(c) == 1)
5911#endif
5912 && vpeekc() != NUL
5913 && !(State & REPLACE_FLAG)
5914#ifdef FEAT_CINDENT
5915 && !cindent_on()
5916#endif
5917#ifdef FEAT_RIGHTLEFT
5918 && !p_ri
5919#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005920#ifdef FEAT_AUTOCMD
5921 && !has_insertcharpre()
5922#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005923 )
5924 {
5925#define INPUT_BUFLEN 100
5926 char_u buf[INPUT_BUFLEN + 1];
5927 int i;
5928 colnr_T virtcol = 0;
5929
5930 buf[0] = c;
5931 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005932 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005933 virtcol = get_nolist_virtcol();
5934 /*
5935 * Stop the string when:
5936 * - no more chars available
5937 * - finding a special character (command key)
5938 * - buffer is full
5939 * - running into the 'textwidth' boundary
5940 * - need to check for abbreviation: A non-word char after a word-char
5941 */
5942 while ( (c = vpeekc()) != NUL
5943 && !ISSPECIAL(c)
5944#ifdef FEAT_MBYTE
5945 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5946#endif
5947 && i < INPUT_BUFLEN
5948 && (textwidth == 0
5949 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5950 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5951 {
5952#ifdef FEAT_RIGHTLEFT
5953 c = vgetc();
5954 if (p_hkmap && KeyTyped)
5955 c = hkmap(c); /* Hebrew mode mapping */
5956# ifdef FEAT_FKMAP
5957 if (p_fkmap && KeyTyped)
5958 c = fkmap(c); /* Farsi mode mapping */
5959# endif
5960 buf[i++] = c;
5961#else
5962 buf[i++] = vgetc();
5963#endif
5964 }
5965
5966#ifdef FEAT_DIGRAPHS
5967 do_digraph(-1); /* clear digraphs */
5968 do_digraph(buf[i-1]); /* may be the start of a digraph */
5969#endif
5970 buf[i] = NUL;
5971 ins_str(buf);
5972 if (flags & INSCHAR_CTRLV)
5973 {
5974 redo_literal(*buf);
5975 i = 1;
5976 }
5977 else
5978 i = 0;
5979 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005980 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005981 }
5982 else
5983 {
5984#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005985 int cc;
5986
Bram Moolenaar071d4272004-06-13 20:20:40 +00005987 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5988 {
5989 char_u buf[MB_MAXBYTES + 1];
5990
5991 (*mb_char2bytes)(c, buf);
5992 buf[cc] = NUL;
5993 ins_char_bytes(buf, cc);
5994 AppendCharToRedobuff(c);
5995 }
5996 else
5997#endif
5998 {
5999 ins_char(c);
6000 if (flags & INSCHAR_CTRLV)
6001 redo_literal(c);
6002 else
6003 AppendCharToRedobuff(c);
6004 }
6005 }
6006}
6007
6008/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006009 * Format text at the current insert position.
6010 */
6011 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006012internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006013 int textwidth;
6014 int second_indent;
6015 int flags;
6016 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006017 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006018{
6019 int cc;
6020 int save_char = NUL;
6021 int haveto_redraw = FALSE;
6022 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6023#ifdef FEAT_MBYTE
6024 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6025#endif
6026 int fo_white_par = has_format_option(FO_WHITE_PAR);
6027 int first_line = TRUE;
6028#ifdef FEAT_COMMENTS
6029 colnr_T leader_len;
6030 int no_leader = FALSE;
6031 int do_comments = (flags & INSCHAR_DO_COM);
6032#endif
6033
6034 /*
6035 * When 'ai' is off we don't want a space under the cursor to be
6036 * deleted. Replace it with an 'x' temporarily.
6037 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006038 if (!curbuf->b_p_ai
6039#ifdef FEAT_VREPLACE
6040 && !(State & VREPLACE_FLAG)
6041#endif
6042 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006043 {
6044 cc = gchar_cursor();
6045 if (vim_iswhite(cc))
6046 {
6047 save_char = cc;
6048 pchar_cursor('x');
6049 }
6050 }
6051
6052 /*
6053 * Repeat breaking lines, until the current line is not too long.
6054 */
6055 while (!got_int)
6056 {
6057 int startcol; /* Cursor column at entry */
6058 int wantcol; /* column at textwidth border */
6059 int foundcol; /* column for start of spaces */
6060 int end_foundcol = 0; /* column for start of word */
6061 colnr_T len;
6062 colnr_T virtcol;
6063#ifdef FEAT_VREPLACE
6064 int orig_col = 0;
6065 char_u *saved_text = NULL;
6066#endif
6067 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006068 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006069
Bram Moolenaar97b98102009-11-17 16:41:01 +00006070 virtcol = get_nolist_virtcol()
6071 + char2cells(c != NUL ? c : gchar_cursor());
6072 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006073 break;
6074
6075#ifdef FEAT_COMMENTS
6076 if (no_leader)
6077 do_comments = FALSE;
6078 else if (!(flags & INSCHAR_FORMAT)
6079 && has_format_option(FO_WRAP_COMS))
6080 do_comments = TRUE;
6081
6082 /* Don't break until after the comment leader */
6083 if (do_comments)
6084 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
6085 else
6086 leader_len = 0;
6087
6088 /* If the line doesn't start with a comment leader, then don't
6089 * start one in a following broken line. Avoids that a %word
6090 * moved to the start of the next line causes all following lines
6091 * to start with %. */
6092 if (leader_len == 0)
6093 no_leader = TRUE;
6094#endif
6095 if (!(flags & INSCHAR_FORMAT)
6096#ifdef FEAT_COMMENTS
6097 && leader_len == 0
6098#endif
6099 && !has_format_option(FO_WRAP))
6100
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006101 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006102 if ((startcol = curwin->w_cursor.col) == 0)
6103 break;
6104
6105 /* find column of textwidth border */
6106 coladvance((colnr_T)textwidth);
6107 wantcol = curwin->w_cursor.col;
6108
Bram Moolenaar97b98102009-11-17 16:41:01 +00006109 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006110 foundcol = 0;
6111
6112 /*
6113 * Find position to break at.
6114 * Stop at first entered white when 'formatoptions' has 'v'
6115 */
6116 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006117 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006118 || curwin->w_cursor.lnum != Insstart.lnum
6119 || curwin->w_cursor.col >= Insstart.col)
6120 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006121 if (curwin->w_cursor.col == startcol && c != NUL)
6122 cc = c;
6123 else
6124 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006125 if (WHITECHAR(cc))
6126 {
6127 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006128 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006129
6130 /* find start of sequence of blanks */
6131 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6132 {
6133 dec_cursor();
6134 cc = gchar_cursor();
6135 }
6136 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6137 break; /* only spaces in front of text */
6138#ifdef FEAT_COMMENTS
6139 /* Don't break until after the comment leader */
6140 if (curwin->w_cursor.col < leader_len)
6141 break;
6142#endif
6143 if (has_format_option(FO_ONE_LETTER))
6144 {
6145 /* do not break after one-letter words */
6146 if (curwin->w_cursor.col == 0)
6147 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006148#ifdef FEAT_COMMENTS
6149 /* do not break "#a b" when 'tw' is 2 */
6150 if (curwin->w_cursor.col <= leader_len)
6151 break;
6152#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006153 col = curwin->w_cursor.col;
6154 dec_cursor();
6155 cc = gchar_cursor();
6156
6157 if (WHITECHAR(cc))
6158 continue; /* one-letter, continue */
6159 curwin->w_cursor.col = col;
6160 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006161
6162 inc_cursor();
6163
6164 end_foundcol = end_col + 1;
6165 foundcol = curwin->w_cursor.col;
6166 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006167 break;
6168 }
6169#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006170 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006171 {
6172 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006173 if (curwin->w_cursor.col != startcol)
6174 {
6175#ifdef FEAT_COMMENTS
6176 /* Don't break until after the comment leader */
6177 if (curwin->w_cursor.col < leader_len)
6178 break;
6179#endif
6180 col = curwin->w_cursor.col;
6181 inc_cursor();
6182 /* Don't change end_foundcol if already set. */
6183 if (foundcol != curwin->w_cursor.col)
6184 {
6185 foundcol = curwin->w_cursor.col;
6186 end_foundcol = foundcol;
6187 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6188 break;
6189 }
6190 curwin->w_cursor.col = col;
6191 }
6192
6193 if (curwin->w_cursor.col == 0)
6194 break;
6195
6196 col = curwin->w_cursor.col;
6197
6198 dec_cursor();
6199 cc = gchar_cursor();
6200
6201 if (WHITECHAR(cc))
6202 continue; /* break with space */
6203#ifdef FEAT_COMMENTS
6204 /* Don't break until after the comment leader */
6205 if (curwin->w_cursor.col < leader_len)
6206 break;
6207#endif
6208
6209 curwin->w_cursor.col = col;
6210
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006211 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006213 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6214 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006215 }
6216#endif
6217 if (curwin->w_cursor.col == 0)
6218 break;
6219 dec_cursor();
6220 }
6221
6222 if (foundcol == 0) /* no spaces, cannot break line */
6223 {
6224 curwin->w_cursor.col = startcol;
6225 break;
6226 }
6227
6228 /* Going to break the line, remove any "$" now. */
6229 undisplay_dollar();
6230
6231 /*
6232 * Offset between cursor position and line break is used by replace
6233 * stack functions. VREPLACE does not use this, and backspaces
6234 * over the text instead.
6235 */
6236#ifdef FEAT_VREPLACE
6237 if (State & VREPLACE_FLAG)
6238 orig_col = startcol; /* Will start backspacing from here */
6239 else
6240#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006241 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006242
6243 /*
6244 * adjust startcol for spaces that will be deleted and
6245 * characters that will remain on top line
6246 */
6247 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006248 while ((cc = gchar_cursor(), WHITECHAR(cc))
6249 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006250 inc_cursor();
6251 startcol -= curwin->w_cursor.col;
6252 if (startcol < 0)
6253 startcol = 0;
6254
6255#ifdef FEAT_VREPLACE
6256 if (State & VREPLACE_FLAG)
6257 {
6258 /*
6259 * In VREPLACE mode, we will backspace over the text to be
6260 * wrapped, so save a copy now to put on the next line.
6261 */
6262 saved_text = vim_strsave(ml_get_cursor());
6263 curwin->w_cursor.col = orig_col;
6264 if (saved_text == NULL)
6265 break; /* Can't do it, out of memory */
6266 saved_text[startcol] = NUL;
6267
6268 /* Backspace over characters that will move to the next line */
6269 if (!fo_white_par)
6270 backspace_until_column(foundcol);
6271 }
6272 else
6273#endif
6274 {
6275 /* put cursor after pos. to break line */
6276 if (!fo_white_par)
6277 curwin->w_cursor.col = foundcol;
6278 }
6279
6280 /*
6281 * Split the line just before the margin.
6282 * Only insert/delete lines, but don't really redraw the window.
6283 */
6284 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6285 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6286#ifdef FEAT_COMMENTS
6287 + (do_comments ? OPENLINE_DO_COM : 0)
6288#endif
6289 , old_indent);
6290 old_indent = 0;
6291
6292 replace_offset = 0;
6293 if (first_line)
6294 {
6295 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6296 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6297 if (second_indent >= 0)
6298 {
6299#ifdef FEAT_VREPLACE
6300 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006301 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006302 else
6303#endif
6304 (void)set_indent(second_indent, SIN_CHANGED);
6305 }
6306 first_line = FALSE;
6307 }
6308
6309#ifdef FEAT_VREPLACE
6310 if (State & VREPLACE_FLAG)
6311 {
6312 /*
6313 * In VREPLACE mode we have backspaced over the text to be
6314 * moved, now we re-insert it into the new line.
6315 */
6316 ins_bytes(saved_text);
6317 vim_free(saved_text);
6318 }
6319 else
6320#endif
6321 {
6322 /*
6323 * Check if cursor is not past the NUL off the line, cindent
6324 * may have added or removed indent.
6325 */
6326 curwin->w_cursor.col += startcol;
6327 len = (colnr_T)STRLEN(ml_get_curline());
6328 if (curwin->w_cursor.col > len)
6329 curwin->w_cursor.col = len;
6330 }
6331
6332 haveto_redraw = TRUE;
6333#ifdef FEAT_CINDENT
6334 can_cindent = TRUE;
6335#endif
6336 /* moved the cursor, don't autoindent or cindent now */
6337 did_ai = FALSE;
6338#ifdef FEAT_SMARTINDENT
6339 did_si = FALSE;
6340 can_si = FALSE;
6341 can_si_back = FALSE;
6342#endif
6343 line_breakcheck();
6344 }
6345
6346 if (save_char != NUL) /* put back space after cursor */
6347 pchar_cursor(save_char);
6348
6349 if (!format_only && haveto_redraw)
6350 {
6351 update_topline();
6352 redraw_curbuf_later(VALID);
6353 }
6354}
6355
6356/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006357 * Called after inserting or deleting text: When 'formatoptions' includes the
6358 * 'a' flag format from the current line until the end of the paragraph.
6359 * Keep the cursor at the same position relative to the text.
6360 * The caller must have saved the cursor line for undo, following ones will be
6361 * saved here.
6362 */
6363 void
6364auto_format(trailblank, prev_line)
6365 int trailblank; /* when TRUE also format with trailing blank */
6366 int prev_line; /* may start in previous line */
6367{
6368 pos_T pos;
6369 colnr_T len;
6370 char_u *old;
6371 char_u *new, *pnew;
6372 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006373 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374
6375 if (!has_format_option(FO_AUTO))
6376 return;
6377
6378 pos = curwin->w_cursor;
6379 old = ml_get_curline();
6380
6381 /* may remove added space */
6382 check_auto_format(FALSE);
6383
6384 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6385 * user might insert normal text next. Also skip formatting when "1" is
6386 * in 'formatoptions' and there is a single character before the cursor.
6387 * Otherwise the line would be broken and when typing another non-white
6388 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006389 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006390 if (*old != NUL && !trailblank && wasatend)
6391 {
6392 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006393 cc = gchar_cursor();
6394 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6395 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006396 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006397 cc = gchar_cursor();
6398 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006399 {
6400 curwin->w_cursor = pos;
6401 return;
6402 }
6403 curwin->w_cursor = pos;
6404 }
6405
6406#ifdef FEAT_COMMENTS
6407 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6408 * comments. */
6409 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6410 && get_leader_len(old, NULL, FALSE) == 0)
6411 return;
6412#endif
6413
6414 /*
6415 * May start formatting in a previous line, so that after "x" a word is
6416 * moved to the previous line if it fits there now. Only when this is not
6417 * the start of a paragraph.
6418 */
6419 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6420 {
6421 --curwin->w_cursor.lnum;
6422 if (u_save_cursor() == FAIL)
6423 return;
6424 }
6425
6426 /*
6427 * Do the formatting and restore the cursor position. "saved_cursor" will
6428 * be adjusted for the text formatting.
6429 */
6430 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006431 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006432 curwin->w_cursor = saved_cursor;
6433 saved_cursor.lnum = 0;
6434
6435 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6436 {
6437 /* "cannot happen" */
6438 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6439 coladvance((colnr_T)MAXCOL);
6440 }
6441 else
6442 check_cursor_col();
6443
6444 /* Insert mode: If the cursor is now after the end of the line while it
6445 * previously wasn't, the line was broken. Because of the rule above we
6446 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6447 * formatted. */
6448 if (!wasatend && has_format_option(FO_WHITE_PAR))
6449 {
6450 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006451 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (curwin->w_cursor.col == len)
6453 {
6454 pnew = vim_strnsave(new, len + 2);
6455 pnew[len] = ' ';
6456 pnew[len + 1] = NUL;
6457 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6458 /* remove the space later */
6459 did_add_space = TRUE;
6460 }
6461 else
6462 /* may remove added space */
6463 check_auto_format(FALSE);
6464 }
6465
6466 check_cursor();
6467}
6468
6469/*
6470 * When an extra space was added to continue a paragraph for auto-formatting,
6471 * delete it now. The space must be under the cursor, just after the insert
6472 * position.
6473 */
6474 static void
6475check_auto_format(end_insert)
6476 int end_insert; /* TRUE when ending Insert mode */
6477{
6478 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006479 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
6481 if (did_add_space)
6482 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006483 cc = gchar_cursor();
6484 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006485 /* Somehow the space was removed already. */
6486 did_add_space = FALSE;
6487 else
6488 {
6489 if (!end_insert)
6490 {
6491 inc_cursor();
6492 c = gchar_cursor();
6493 dec_cursor();
6494 }
6495 if (c != NUL)
6496 {
6497 /* The space is no longer at the end of the line, delete it. */
6498 del_char(FALSE);
6499 did_add_space = FALSE;
6500 }
6501 }
6502 }
6503}
6504
6505/*
6506 * Find out textwidth to be used for formatting:
6507 * if 'textwidth' option is set, use it
6508 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6509 * if invalid value, use 0.
6510 * Set default to window width (maximum 79) for "gq" operator.
6511 */
6512 int
6513comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006514 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006515{
6516 int textwidth;
6517
6518 textwidth = curbuf->b_p_tw;
6519 if (textwidth == 0 && curbuf->b_p_wm)
6520 {
6521 /* The width is the window width minus 'wrapmargin' minus all the
6522 * things that add to the margin. */
6523 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6524#ifdef FEAT_CMDWIN
6525 if (cmdwin_type != 0)
6526 textwidth -= 1;
6527#endif
6528#ifdef FEAT_FOLDING
6529 textwidth -= curwin->w_p_fdc;
6530#endif
6531#ifdef FEAT_SIGNS
6532 if (curwin->w_buffer->b_signlist != NULL
6533# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006534 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535# endif
6536 )
6537 textwidth -= 1;
6538#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006539 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006540 textwidth -= 8;
6541 }
6542 if (textwidth < 0)
6543 textwidth = 0;
6544 if (ff && textwidth == 0)
6545 {
6546 textwidth = W_WIDTH(curwin) - 1;
6547 if (textwidth > 79)
6548 textwidth = 79;
6549 }
6550 return textwidth;
6551}
6552
6553/*
6554 * Put a character in the redo buffer, for when just after a CTRL-V.
6555 */
6556 static void
6557redo_literal(c)
6558 int c;
6559{
6560 char_u buf[10];
6561
6562 /* Only digits need special treatment. Translate them into a string of
6563 * three digits. */
6564 if (VIM_ISDIGIT(c))
6565 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006566 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006567 AppendToRedobuff(buf);
6568 }
6569 else
6570 AppendCharToRedobuff(c);
6571}
6572
6573/*
6574 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006575 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006576 */
6577 static void
6578start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006579 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580{
6581 if (!arrow_used) /* something has been inserted */
6582 {
6583 AppendToRedobuff(ESC_STR);
6584 stop_insert(end_insert_pos, FALSE);
6585 arrow_used = TRUE; /* this means we stopped the current insert */
6586 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006587#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006588 check_spell_redraw();
6589#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006590}
6591
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006592#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006593/*
6594 * If we skipped highlighting word at cursor, do it now.
6595 * It may be skipped again, thus reset spell_redraw_lnum first.
6596 */
6597 static void
6598check_spell_redraw()
6599{
6600 if (spell_redraw_lnum != 0)
6601 {
6602 linenr_T lnum = spell_redraw_lnum;
6603
6604 spell_redraw_lnum = 0;
6605 redrawWinline(lnum, FALSE);
6606 }
6607}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006608
6609/*
6610 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6611 * spelled word, if there is one.
6612 */
6613 static void
6614spell_back_to_badword()
6615{
6616 pos_T tpos = curwin->w_cursor;
6617
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006618 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006619 if (curwin->w_cursor.col != tpos.col)
6620 start_arrow(&tpos);
6621}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006622#endif
6623
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624/*
6625 * stop_arrow() is called before a change is made in insert mode.
6626 * If an arrow key has been used, start a new insertion.
6627 * Returns FAIL if undo is impossible, shouldn't insert then.
6628 */
6629 int
6630stop_arrow()
6631{
6632 if (arrow_used)
6633 {
6634 if (u_save_cursor() == OK)
6635 {
6636 arrow_used = FALSE;
6637 ins_need_undo = FALSE;
6638 }
6639 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006640 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006641 ai_col = 0;
6642#ifdef FEAT_VREPLACE
6643 if (State & VREPLACE_FLAG)
6644 {
6645 orig_line_count = curbuf->b_ml.ml_line_count;
6646 vr_lines_changed = 1;
6647 }
6648#endif
6649 ResetRedobuff();
6650 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006651 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652 }
6653 else if (ins_need_undo)
6654 {
6655 if (u_save_cursor() == OK)
6656 ins_need_undo = FALSE;
6657 }
6658
6659#ifdef FEAT_FOLDING
6660 /* Always open fold at the cursor line when inserting something. */
6661 foldOpenCursor();
6662#endif
6663
6664 return (arrow_used || ins_need_undo ? FAIL : OK);
6665}
6666
6667/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006668 * Do a few things to stop inserting.
6669 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6670 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 */
6672 static void
6673stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006674 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006675 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006676{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006677 int cc;
6678 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
6680 stop_redo_ins();
6681 replace_flush(); /* abandon replace stack */
6682
6683 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006684 * Save the inserted text for later redo with ^@ and CTRL-A.
6685 * Don't do it when "restart_edit" was set and nothing was inserted,
6686 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006688 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006689 if (did_restart_edit == 0 || (ptr != NULL
6690 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006691 {
6692 vim_free(last_insert);
6693 last_insert = ptr;
6694 last_insert_skip = new_insert_skip;
6695 }
6696 else
6697 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006698
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006699 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006700 {
6701 /* Auto-format now. It may seem strange to do this when stopping an
6702 * insertion (or moving the cursor), but it's required when appending
6703 * a line and having it end in a space. But only do it when something
6704 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006705 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006707 pos_T tpos = curwin->w_cursor;
6708
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 /* When the cursor is at the end of the line after a space the
6710 * formatting will move it to the following word. Avoid that by
6711 * moving the cursor onto the space. */
6712 cc = 'x';
6713 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6714 {
6715 dec_cursor();
6716 cc = gchar_cursor();
6717 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006718 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006719 }
6720
6721 auto_format(TRUE, FALSE);
6722
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006723 if (vim_iswhite(cc))
6724 {
6725 if (gchar_cursor() != NUL)
6726 inc_cursor();
6727#ifdef FEAT_VIRTUALEDIT
6728 /* If the cursor is still at the same character, also keep
6729 * the "coladd". */
6730 if (gchar_cursor() == NUL
6731 && curwin->w_cursor.lnum == tpos.lnum
6732 && curwin->w_cursor.col == tpos.col)
6733 curwin->w_cursor.coladd = tpos.coladd;
6734#endif
6735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736 }
6737
6738 /* If a space was inserted for auto-formatting, remove it now. */
6739 check_auto_format(TRUE);
6740
6741 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006742 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006743 * Do this when ESC was used or moving the cursor up/down.
6744 * Check for the old position still being valid, just in case the text
6745 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006746 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006747 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6748 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006749 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006750 pos_T tpos = curwin->w_cursor;
6751
6752 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006753 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006754 for (;;)
6755 {
6756 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6757 --curwin->w_cursor.col;
6758 cc = gchar_cursor();
6759 if (!vim_iswhite(cc))
6760 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006761 if (del_char(TRUE) == FAIL)
6762 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006763 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006764 if (curwin->w_cursor.lnum != tpos.lnum)
6765 curwin->w_cursor = tpos;
6766 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006767 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6768
6769#ifdef FEAT_VISUAL
6770 /* <C-S-Right> may have started Visual mode, adjust the position for
6771 * deleted characters. */
6772 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6773 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006774 int len = (int)STRLEN(ml_get_curline());
6775
6776 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006777 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006778 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779# ifdef FEAT_VIRTUALEDIT
6780 VIsual.coladd = 0;
6781# endif
6782 }
6783 }
6784#endif
6785 }
6786 }
6787 did_ai = FALSE;
6788#ifdef FEAT_SMARTINDENT
6789 did_si = FALSE;
6790 can_si = FALSE;
6791 can_si_back = FALSE;
6792#endif
6793
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006794 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6795 * now in a different buffer. */
6796 if (end_insert_pos != NULL)
6797 {
6798 curbuf->b_op_start = Insstart;
6799 curbuf->b_op_end = *end_insert_pos;
6800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006801}
6802
6803/*
6804 * Set the last inserted text to a single character.
6805 * Used for the replace command.
6806 */
6807 void
6808set_last_insert(c)
6809 int c;
6810{
6811 char_u *s;
6812
6813 vim_free(last_insert);
6814#ifdef FEAT_MBYTE
6815 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6816#else
6817 last_insert = alloc(6);
6818#endif
6819 if (last_insert != NULL)
6820 {
6821 s = last_insert;
6822 /* Use the CTRL-V only when entering a special char */
6823 if (c < ' ' || c == DEL)
6824 *s++ = Ctrl_V;
6825 s = add_char2buf(c, s);
6826 *s++ = ESC;
6827 *s++ = NUL;
6828 last_insert_skip = 0;
6829 }
6830}
6831
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006832#if defined(EXITFREE) || defined(PROTO)
6833 void
6834free_last_insert()
6835{
6836 vim_free(last_insert);
6837 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006838# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006839 vim_free(compl_orig_text);
6840 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006841# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006842}
6843#endif
6844
Bram Moolenaar071d4272004-06-13 20:20:40 +00006845/*
6846 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6847 * and CSI. Handle multi-byte characters.
6848 * Returns a pointer to after the added bytes.
6849 */
6850 char_u *
6851add_char2buf(c, s)
6852 int c;
6853 char_u *s;
6854{
6855#ifdef FEAT_MBYTE
6856 char_u temp[MB_MAXBYTES];
6857 int i;
6858 int len;
6859
6860 len = (*mb_char2bytes)(c, temp);
6861 for (i = 0; i < len; ++i)
6862 {
6863 c = temp[i];
6864#endif
6865 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6866 if (c == K_SPECIAL)
6867 {
6868 *s++ = K_SPECIAL;
6869 *s++ = KS_SPECIAL;
6870 *s++ = KE_FILLER;
6871 }
6872#ifdef FEAT_GUI
6873 else if (c == CSI)
6874 {
6875 *s++ = CSI;
6876 *s++ = KS_EXTRA;
6877 *s++ = (int)KE_CSI;
6878 }
6879#endif
6880 else
6881 *s++ = c;
6882#ifdef FEAT_MBYTE
6883 }
6884#endif
6885 return s;
6886}
6887
6888/*
6889 * move cursor to start of line
6890 * if flags & BL_WHITE move to first non-white
6891 * if flags & BL_SOL move to first non-white if startofline is set,
6892 * otherwise keep "curswant" column
6893 * if flags & BL_FIX don't leave the cursor on a NUL.
6894 */
6895 void
6896beginline(flags)
6897 int flags;
6898{
6899 if ((flags & BL_SOL) && !p_sol)
6900 coladvance(curwin->w_curswant);
6901 else
6902 {
6903 curwin->w_cursor.col = 0;
6904#ifdef FEAT_VIRTUALEDIT
6905 curwin->w_cursor.coladd = 0;
6906#endif
6907
6908 if (flags & (BL_WHITE | BL_SOL))
6909 {
6910 char_u *ptr;
6911
6912 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6913 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6914 ++curwin->w_cursor.col;
6915 }
6916 curwin->w_set_curswant = TRUE;
6917 }
6918}
6919
6920/*
6921 * oneright oneleft cursor_down cursor_up
6922 *
6923 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006924 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006925 * Return OK when successful, FAIL when we hit a line of file boundary.
6926 */
6927
6928 int
6929oneright()
6930{
6931 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006932 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933
6934#ifdef FEAT_VIRTUALEDIT
6935 if (virtual_active())
6936 {
6937 pos_T prevpos = curwin->w_cursor;
6938
6939 /* Adjust for multi-wide char (excluding TAB) */
6940 ptr = ml_get_cursor();
6941 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006942# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006943 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006944# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006945 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006946# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006947 ))
6948 ? ptr2cells(ptr) : 1));
6949 curwin->w_set_curswant = TRUE;
6950 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6951 return (prevpos.col != curwin->w_cursor.col
6952 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6953 }
6954#endif
6955
6956 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006957 if (*ptr == NUL)
6958 return FAIL; /* already at the very end */
6959
Bram Moolenaar071d4272004-06-13 20:20:40 +00006960#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006961 if (has_mbyte)
6962 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006963 else
6964#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006965 l = 1;
6966
6967 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6968 * contains "onemore". */
6969 if (ptr[l] == NUL
6970#ifdef FEAT_VIRTUALEDIT
6971 && (ve_flags & VE_ONEMORE) == 0
6972#endif
6973 )
6974 return FAIL;
6975 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
6977 curwin->w_set_curswant = TRUE;
6978 return OK;
6979}
6980
6981 int
6982oneleft()
6983{
6984#ifdef FEAT_VIRTUALEDIT
6985 if (virtual_active())
6986 {
6987 int width;
6988 int v = getviscol();
6989
6990 if (v == 0)
6991 return FAIL;
6992
6993# ifdef FEAT_LINEBREAK
6994 /* We might get stuck on 'showbreak', skip over it. */
6995 width = 1;
6996 for (;;)
6997 {
6998 coladvance(v - width);
6999 /* getviscol() is slow, skip it when 'showbreak' is empty and
7000 * there are no multi-byte characters */
7001 if ((*p_sbr == NUL
7002# ifdef FEAT_MBYTE
7003 && !has_mbyte
7004# endif
7005 ) || getviscol() < v)
7006 break;
7007 ++width;
7008 }
7009# else
7010 coladvance(v - 1);
7011# endif
7012
7013 if (curwin->w_cursor.coladd == 1)
7014 {
7015 char_u *ptr;
7016
7017 /* Adjust for multi-wide char (not a TAB) */
7018 ptr = ml_get_cursor();
7019 if (*ptr != TAB && vim_isprintc(
7020# ifdef FEAT_MBYTE
7021 (*mb_ptr2char)(ptr)
7022# else
7023 *ptr
7024# endif
7025 ) && ptr2cells(ptr) > 1)
7026 curwin->w_cursor.coladd = 0;
7027 }
7028
7029 curwin->w_set_curswant = TRUE;
7030 return OK;
7031 }
7032#endif
7033
7034 if (curwin->w_cursor.col == 0)
7035 return FAIL;
7036
7037 curwin->w_set_curswant = TRUE;
7038 --curwin->w_cursor.col;
7039
7040#ifdef FEAT_MBYTE
7041 /* if the character on the left of the current cursor is a multi-byte
7042 * character, move to its first byte */
7043 if (has_mbyte)
7044 mb_adjust_cursor();
7045#endif
7046 return OK;
7047}
7048
7049 int
7050cursor_up(n, upd_topline)
7051 long n;
7052 int upd_topline; /* When TRUE: update topline */
7053{
7054 linenr_T lnum;
7055
7056 if (n > 0)
7057 {
7058 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007059 /* This fails if the cursor is already in the first line or the count
7060 * is larger than the line number and '-' is in 'cpoptions' */
7061 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007062 return FAIL;
7063 if (n >= lnum)
7064 lnum = 1;
7065 else
7066#ifdef FEAT_FOLDING
7067 if (hasAnyFolding(curwin))
7068 {
7069 /*
7070 * Count each sequence of folded lines as one logical line.
7071 */
7072 /* go to the the start of the current fold */
7073 (void)hasFolding(lnum, &lnum, NULL);
7074
7075 while (n--)
7076 {
7077 /* move up one line */
7078 --lnum;
7079 if (lnum <= 1)
7080 break;
7081 /* If we entered a fold, move to the beginning, unless in
7082 * Insert mode or when 'foldopen' contains "all": it will open
7083 * in a moment. */
7084 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7085 (void)hasFolding(lnum, &lnum, NULL);
7086 }
7087 if (lnum < 1)
7088 lnum = 1;
7089 }
7090 else
7091#endif
7092 lnum -= n;
7093 curwin->w_cursor.lnum = lnum;
7094 }
7095
7096 /* try to advance to the column we want to be at */
7097 coladvance(curwin->w_curswant);
7098
7099 if (upd_topline)
7100 update_topline(); /* make sure curwin->w_topline is valid */
7101
7102 return OK;
7103}
7104
7105/*
7106 * Cursor down a number of logical lines.
7107 */
7108 int
7109cursor_down(n, upd_topline)
7110 long n;
7111 int upd_topline; /* When TRUE: update topline */
7112{
7113 linenr_T lnum;
7114
7115 if (n > 0)
7116 {
7117 lnum = curwin->w_cursor.lnum;
7118#ifdef FEAT_FOLDING
7119 /* Move to last line of fold, will fail if it's the end-of-file. */
7120 (void)hasFolding(lnum, NULL, &lnum);
7121#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007122 /* This fails if the cursor is already in the last line or would move
7123 * beyound the last line and '-' is in 'cpoptions' */
7124 if (lnum >= curbuf->b_ml.ml_line_count
7125 || (lnum + n > curbuf->b_ml.ml_line_count
7126 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007127 return FAIL;
7128 if (lnum + n >= curbuf->b_ml.ml_line_count)
7129 lnum = curbuf->b_ml.ml_line_count;
7130 else
7131#ifdef FEAT_FOLDING
7132 if (hasAnyFolding(curwin))
7133 {
7134 linenr_T last;
7135
7136 /* count each sequence of folded lines as one logical line */
7137 while (n--)
7138 {
7139 if (hasFolding(lnum, NULL, &last))
7140 lnum = last + 1;
7141 else
7142 ++lnum;
7143 if (lnum >= curbuf->b_ml.ml_line_count)
7144 break;
7145 }
7146 if (lnum > curbuf->b_ml.ml_line_count)
7147 lnum = curbuf->b_ml.ml_line_count;
7148 }
7149 else
7150#endif
7151 lnum += n;
7152 curwin->w_cursor.lnum = lnum;
7153 }
7154
7155 /* try to advance to the column we want to be at */
7156 coladvance(curwin->w_curswant);
7157
7158 if (upd_topline)
7159 update_topline(); /* make sure curwin->w_topline is valid */
7160
7161 return OK;
7162}
7163
7164/*
7165 * Stuff the last inserted text in the read buffer.
7166 * Last_insert actually is a copy of the redo buffer, so we
7167 * first have to remove the command.
7168 */
7169 int
7170stuff_inserted(c, count, no_esc)
7171 int c; /* Command character to be inserted */
7172 long count; /* Repeat this many times */
7173 int no_esc; /* Don't add an ESC at the end */
7174{
7175 char_u *esc_ptr;
7176 char_u *ptr;
7177 char_u *last_ptr;
7178 char_u last = NUL;
7179
7180 ptr = get_last_insert();
7181 if (ptr == NULL)
7182 {
7183 EMSG(_(e_noinstext));
7184 return FAIL;
7185 }
7186
7187 /* may want to stuff the command character, to start Insert mode */
7188 if (c != NUL)
7189 stuffcharReadbuff(c);
7190 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7191 *esc_ptr = NUL; /* remove the ESC */
7192
7193 /* when the last char is either "0" or "^" it will be quoted if no ESC
7194 * comes after it OR if it will inserted more than once and "ptr"
7195 * starts with ^D. -- Acevedo
7196 */
7197 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7198 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7199 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7200 {
7201 last = *last_ptr;
7202 *last_ptr = NUL;
7203 }
7204
7205 do
7206 {
7207 stuffReadbuff(ptr);
7208 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7209 if (last)
7210 stuffReadbuff((char_u *)(last == '0'
7211 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7212 : IF_EB("\026^", CTRL_V_STR "^")));
7213 }
7214 while (--count > 0);
7215
7216 if (last)
7217 *last_ptr = last;
7218
7219 if (esc_ptr != NULL)
7220 *esc_ptr = ESC; /* put the ESC back */
7221
7222 /* may want to stuff a trailing ESC, to get out of Insert mode */
7223 if (!no_esc)
7224 stuffcharReadbuff(ESC);
7225
7226 return OK;
7227}
7228
7229 char_u *
7230get_last_insert()
7231{
7232 if (last_insert == NULL)
7233 return NULL;
7234 return last_insert + last_insert_skip;
7235}
7236
7237/*
7238 * Get last inserted string, and remove trailing <Esc>.
7239 * Returns pointer to allocated memory (must be freed) or NULL.
7240 */
7241 char_u *
7242get_last_insert_save()
7243{
7244 char_u *s;
7245 int len;
7246
7247 if (last_insert == NULL)
7248 return NULL;
7249 s = vim_strsave(last_insert + last_insert_skip);
7250 if (s != NULL)
7251 {
7252 len = (int)STRLEN(s);
7253 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7254 s[len - 1] = NUL;
7255 }
7256 return s;
7257}
7258
7259/*
7260 * Check the word in front of the cursor for an abbreviation.
7261 * Called when the non-id character "c" has been entered.
7262 * When an abbreviation is recognized it is removed from the text and
7263 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7264 */
7265 static int
7266echeck_abbr(c)
7267 int c;
7268{
7269 /* Don't check for abbreviation in paste mode, when disabled and just
7270 * after moving around with cursor keys. */
7271 if (p_paste || no_abbr || arrow_used)
7272 return FALSE;
7273
7274 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7275 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7276}
7277
7278/*
7279 * replace-stack functions
7280 *
7281 * When replacing characters, the replaced characters are remembered for each
7282 * new character. This is used to re-insert the old text when backspacing.
7283 *
7284 * There is a NUL headed list of characters for each character that is
7285 * currently in the file after the insertion point. When BS is used, one NUL
7286 * headed list is put back for the deleted character.
7287 *
7288 * For a newline, there are two NUL headed lists. One contains the characters
7289 * that the NL replaced. The extra one stores the characters after the cursor
7290 * that were deleted (always white space).
7291 *
7292 * Replace_offset is normally 0, in which case replace_push will add a new
7293 * character at the end of the stack. If replace_offset is not 0, that many
7294 * characters will be left on the stack above the newly inserted character.
7295 */
7296
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007297static char_u *replace_stack = NULL;
7298static long replace_stack_nr = 0; /* next entry in replace stack */
7299static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007300
7301 void
7302replace_push(c)
7303 int c; /* character that is replaced (NUL is none) */
7304{
7305 char_u *p;
7306
7307 if (replace_stack_nr < replace_offset) /* nothing to do */
7308 return;
7309 if (replace_stack_len <= replace_stack_nr)
7310 {
7311 replace_stack_len += 50;
7312 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7313 if (p == NULL) /* out of memory */
7314 {
7315 replace_stack_len -= 50;
7316 return;
7317 }
7318 if (replace_stack != NULL)
7319 {
7320 mch_memmove(p, replace_stack,
7321 (size_t)(replace_stack_nr * sizeof(char_u)));
7322 vim_free(replace_stack);
7323 }
7324 replace_stack = p;
7325 }
7326 p = replace_stack + replace_stack_nr - replace_offset;
7327 if (replace_offset)
7328 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7329 *p = c;
7330 ++replace_stack_nr;
7331}
7332
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007333#if defined(FEAT_MBYTE) || defined(PROTO)
7334/*
7335 * Push a character onto the replace stack. Handles a multi-byte character in
7336 * reverse byte order, so that the first byte is popped off first.
7337 * Return the number of bytes done (includes composing characters).
7338 */
7339 int
7340replace_push_mb(p)
7341 char_u *p;
7342{
7343 int l = (*mb_ptr2len)(p);
7344 int j;
7345
7346 for (j = l - 1; j >= 0; --j)
7347 replace_push(p[j]);
7348 return l;
7349}
7350#endif
7351
Bram Moolenaar071d4272004-06-13 20:20:40 +00007352/*
7353 * Pop one item from the replace stack.
7354 * return -1 if stack empty
7355 * return replaced character or NUL otherwise
7356 */
7357 static int
7358replace_pop()
7359{
7360 if (replace_stack_nr == 0)
7361 return -1;
7362 return (int)replace_stack[--replace_stack_nr];
7363}
7364
7365/*
7366 * Join the top two items on the replace stack. This removes to "off"'th NUL
7367 * encountered.
7368 */
7369 static void
7370replace_join(off)
7371 int off; /* offset for which NUL to remove */
7372{
7373 int i;
7374
7375 for (i = replace_stack_nr; --i >= 0; )
7376 if (replace_stack[i] == NUL && off-- <= 0)
7377 {
7378 --replace_stack_nr;
7379 mch_memmove(replace_stack + i, replace_stack + i + 1,
7380 (size_t)(replace_stack_nr - i));
7381 return;
7382 }
7383}
7384
7385/*
7386 * Pop bytes from the replace stack until a NUL is found, and insert them
7387 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7388 */
7389 static void
7390replace_pop_ins()
7391{
7392 int cc;
7393 int oldState = State;
7394
7395 State = NORMAL; /* don't want REPLACE here */
7396 while ((cc = replace_pop()) > 0)
7397 {
7398#ifdef FEAT_MBYTE
7399 mb_replace_pop_ins(cc);
7400#else
7401 ins_char(cc);
7402#endif
7403 dec_cursor();
7404 }
7405 State = oldState;
7406}
7407
7408#ifdef FEAT_MBYTE
7409/*
7410 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7411 * indicates a multi-byte char, pop the other bytes too.
7412 */
7413 static void
7414mb_replace_pop_ins(cc)
7415 int cc;
7416{
7417 int n;
7418 char_u buf[MB_MAXBYTES];
7419 int i;
7420 int c;
7421
7422 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7423 {
7424 buf[0] = cc;
7425 for (i = 1; i < n; ++i)
7426 buf[i] = replace_pop();
7427 ins_bytes_len(buf, n);
7428 }
7429 else
7430 ins_char(cc);
7431
7432 if (enc_utf8)
7433 /* Handle composing chars. */
7434 for (;;)
7435 {
7436 c = replace_pop();
7437 if (c == -1) /* stack empty */
7438 break;
7439 if ((n = MB_BYTE2LEN(c)) == 1)
7440 {
7441 /* Not a multi-byte char, put it back. */
7442 replace_push(c);
7443 break;
7444 }
7445 else
7446 {
7447 buf[0] = c;
7448 for (i = 1; i < n; ++i)
7449 buf[i] = replace_pop();
7450 if (utf_iscomposing(utf_ptr2char(buf)))
7451 ins_bytes_len(buf, n);
7452 else
7453 {
7454 /* Not a composing char, put it back. */
7455 for (i = n - 1; i >= 0; --i)
7456 replace_push(buf[i]);
7457 break;
7458 }
7459 }
7460 }
7461}
7462#endif
7463
7464/*
7465 * make the replace stack empty
7466 * (called when exiting replace mode)
7467 */
7468 static void
7469replace_flush()
7470{
7471 vim_free(replace_stack);
7472 replace_stack = NULL;
7473 replace_stack_len = 0;
7474 replace_stack_nr = 0;
7475}
7476
7477/*
7478 * Handle doing a BS for one character.
7479 * cc < 0: replace stack empty, just move cursor
7480 * cc == 0: character was inserted, delete it
7481 * cc > 0: character was replaced, put cc (first byte of original char) back
7482 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007483 * When "limit_col" is >= 0, don't delete before this column. Matters when
7484 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007485 */
7486 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007487replace_do_bs(limit_col)
7488 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007489{
7490 int cc;
7491#ifdef FEAT_VREPLACE
7492 int orig_len = 0;
7493 int ins_len;
7494 int orig_vcols = 0;
7495 colnr_T start_vcol;
7496 char_u *p;
7497 int i;
7498 int vcol;
7499#endif
7500
7501 cc = replace_pop();
7502 if (cc > 0)
7503 {
7504#ifdef FEAT_VREPLACE
7505 if (State & VREPLACE_FLAG)
7506 {
7507 /* Get the number of screen cells used by the character we are
7508 * going to delete. */
7509 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7510 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7511 }
7512#endif
7513#ifdef FEAT_MBYTE
7514 if (has_mbyte)
7515 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007516 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007517# ifdef FEAT_VREPLACE
7518 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007519 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007520# endif
7521 replace_push(cc);
7522 }
7523 else
7524#endif
7525 {
7526 pchar_cursor(cc);
7527#ifdef FEAT_VREPLACE
7528 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007529 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007530#endif
7531 }
7532 replace_pop_ins();
7533
7534#ifdef FEAT_VREPLACE
7535 if (State & VREPLACE_FLAG)
7536 {
7537 /* Get the number of screen cells used by the inserted characters */
7538 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007539 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007540 vcol = start_vcol;
7541 for (i = 0; i < ins_len; ++i)
7542 {
7543 vcol += chartabsize(p + i, vcol);
7544#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007545 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007546#endif
7547 }
7548 vcol -= start_vcol;
7549
7550 /* Delete spaces that were inserted after the cursor to keep the
7551 * text aligned. */
7552 curwin->w_cursor.col += ins_len;
7553 while (vcol > orig_vcols && gchar_cursor() == ' ')
7554 {
7555 del_char(FALSE);
7556 ++orig_vcols;
7557 }
7558 curwin->w_cursor.col -= ins_len;
7559 }
7560#endif
7561
7562 /* mark the buffer as changed and prepare for displaying */
7563 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7564 }
7565 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007566 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007567}
7568
7569#ifdef FEAT_CINDENT
7570/*
7571 * Return TRUE if C-indenting is on.
7572 */
7573 static int
7574cindent_on()
7575{
7576 return (!p_paste && (curbuf->b_p_cin
7577# ifdef FEAT_EVAL
7578 || *curbuf->b_p_inde != NUL
7579# endif
7580 ));
7581}
7582#endif
7583
7584#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7585/*
7586 * Re-indent the current line, based on the current contents of it and the
7587 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7588 * confused what all the part that handles Control-T is doing that I'm not.
7589 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7590 */
7591
7592 void
7593fixthisline(get_the_indent)
7594 int (*get_the_indent) __ARGS((void));
7595{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007596 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007597 if (linewhite(curwin->w_cursor.lnum))
7598 did_ai = TRUE; /* delete the indent if the line stays empty */
7599}
7600
7601 void
7602fix_indent()
7603{
7604 if (p_paste)
7605 return;
7606# ifdef FEAT_LISP
7607 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7608 fixthisline(get_lisp_indent);
7609# endif
7610# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7611 else
7612# endif
7613# ifdef FEAT_CINDENT
7614 if (cindent_on())
7615 do_c_expr_indent();
7616# endif
7617}
7618
7619#endif
7620
7621#ifdef FEAT_CINDENT
7622/*
7623 * return TRUE if 'cinkeys' contains the key "keytyped",
7624 * when == '*': Only if key is preceded with '*' (indent before insert)
7625 * when == '!': Only if key is prededed with '!' (don't insert)
7626 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7627 *
7628 * "keytyped" can have a few special values:
7629 * KEY_OPEN_FORW
7630 * KEY_OPEN_BACK
7631 * KEY_COMPLETE just finished completion.
7632 *
7633 * If line_is_empty is TRUE accept keys with '0' before them.
7634 */
7635 int
7636in_cinkeys(keytyped, when, line_is_empty)
7637 int keytyped;
7638 int when;
7639 int line_is_empty;
7640{
7641 char_u *look;
7642 int try_match;
7643 int try_match_word;
7644 char_u *p;
7645 char_u *line;
7646 int icase;
7647 int i;
7648
Bram Moolenaar30848942009-12-24 14:46:12 +00007649 if (keytyped == NUL)
7650 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7651 return FALSE;
7652
Bram Moolenaar071d4272004-06-13 20:20:40 +00007653#ifdef FEAT_EVAL
7654 if (*curbuf->b_p_inde != NUL)
7655 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7656 else
7657#endif
7658 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7659 while (*look)
7660 {
7661 /*
7662 * Find out if we want to try a match with this key, depending on
7663 * 'when' and a '*' or '!' before the key.
7664 */
7665 switch (when)
7666 {
7667 case '*': try_match = (*look == '*'); break;
7668 case '!': try_match = (*look == '!'); break;
7669 default: try_match = (*look != '*'); break;
7670 }
7671 if (*look == '*' || *look == '!')
7672 ++look;
7673
7674 /*
7675 * If there is a '0', only accept a match if the line is empty.
7676 * But may still match when typing last char of a word.
7677 */
7678 if (*look == '0')
7679 {
7680 try_match_word = try_match;
7681 if (!line_is_empty)
7682 try_match = FALSE;
7683 ++look;
7684 }
7685 else
7686 try_match_word = FALSE;
7687
7688 /*
7689 * does it look like a control character?
7690 */
7691 if (*look == '^'
7692#ifdef EBCDIC
7693 && (Ctrl_chr(look[1]) != 0)
7694#else
7695 && look[1] >= '?' && look[1] <= '_'
7696#endif
7697 )
7698 {
7699 if (try_match && keytyped == Ctrl_chr(look[1]))
7700 return TRUE;
7701 look += 2;
7702 }
7703 /*
7704 * 'o' means "o" command, open forward.
7705 * 'O' means "O" command, open backward.
7706 */
7707 else if (*look == 'o')
7708 {
7709 if (try_match && keytyped == KEY_OPEN_FORW)
7710 return TRUE;
7711 ++look;
7712 }
7713 else if (*look == 'O')
7714 {
7715 if (try_match && keytyped == KEY_OPEN_BACK)
7716 return TRUE;
7717 ++look;
7718 }
7719
7720 /*
7721 * 'e' means to check for "else" at start of line and just before the
7722 * cursor.
7723 */
7724 else if (*look == 'e')
7725 {
7726 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7727 {
7728 p = ml_get_curline();
7729 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7730 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7731 return TRUE;
7732 }
7733 ++look;
7734 }
7735
7736 /*
7737 * ':' only causes an indent if it is at the end of a label or case
7738 * statement, or when it was before typing the ':' (to fix
7739 * class::method for C++).
7740 */
7741 else if (*look == ':')
7742 {
7743 if (try_match && keytyped == ':')
7744 {
7745 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007746 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7747 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007748 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007749 /* Need to get the line again after cin_islabel(). */
7750 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007751 if (curwin->w_cursor.col > 2
7752 && p[curwin->w_cursor.col - 1] == ':'
7753 && p[curwin->w_cursor.col - 2] == ':')
7754 {
7755 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007756 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007757 || cin_islabel(30));
7758 p = ml_get_curline();
7759 p[curwin->w_cursor.col - 1] = ':';
7760 if (i)
7761 return TRUE;
7762 }
7763 }
7764 ++look;
7765 }
7766
7767
7768 /*
7769 * Is it a key in <>, maybe?
7770 */
7771 else if (*look == '<')
7772 {
7773 if (try_match)
7774 {
7775 /*
7776 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7777 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7778 * >, *, : and ! keys if they really really want to.
7779 */
7780 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7781 && keytyped == look[1])
7782 return TRUE;
7783
7784 if (keytyped == get_special_key_code(look + 1))
7785 return TRUE;
7786 }
7787 while (*look && *look != '>')
7788 look++;
7789 while (*look == '>')
7790 look++;
7791 }
7792
7793 /*
7794 * Is it a word: "=word"?
7795 */
7796 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7797 {
7798 ++look;
7799 if (*look == '~')
7800 {
7801 icase = TRUE;
7802 ++look;
7803 }
7804 else
7805 icase = FALSE;
7806 p = vim_strchr(look, ',');
7807 if (p == NULL)
7808 p = look + STRLEN(look);
7809 if ((try_match || try_match_word)
7810 && curwin->w_cursor.col >= (colnr_T)(p - look))
7811 {
7812 int match = FALSE;
7813
7814#ifdef FEAT_INS_EXPAND
7815 if (keytyped == KEY_COMPLETE)
7816 {
7817 char_u *s;
7818
7819 /* Just completed a word, check if it starts with "look".
7820 * search back for the start of a word. */
7821 line = ml_get_curline();
7822# ifdef FEAT_MBYTE
7823 if (has_mbyte)
7824 {
7825 char_u *n;
7826
7827 for (s = line + curwin->w_cursor.col; s > line; s = n)
7828 {
7829 n = mb_prevptr(line, s);
7830 if (!vim_iswordp(n))
7831 break;
7832 }
7833 }
7834 else
7835# endif
7836 for (s = line + curwin->w_cursor.col; s > line; --s)
7837 if (!vim_iswordc(s[-1]))
7838 break;
7839 if (s + (p - look) <= line + curwin->w_cursor.col
7840 && (icase
7841 ? MB_STRNICMP(s, look, p - look)
7842 : STRNCMP(s, look, p - look)) == 0)
7843 match = TRUE;
7844 }
7845 else
7846#endif
7847 /* TODO: multi-byte */
7848 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7849 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7850 {
7851 line = ml_get_cursor();
7852 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7853 || !vim_iswordc(line[-(p - look) - 1]))
7854 && (icase
7855 ? MB_STRNICMP(line - (p - look), look, p - look)
7856 : STRNCMP(line - (p - look), look, p - look))
7857 == 0)
7858 match = TRUE;
7859 }
7860 if (match && try_match_word && !try_match)
7861 {
7862 /* "0=word": Check if there are only blanks before the
7863 * word. */
7864 line = ml_get_curline();
7865 if ((int)(skipwhite(line) - line) !=
7866 (int)(curwin->w_cursor.col - (p - look)))
7867 match = FALSE;
7868 }
7869 if (match)
7870 return TRUE;
7871 }
7872 look = p;
7873 }
7874
7875 /*
7876 * ok, it's a boring generic character.
7877 */
7878 else
7879 {
7880 if (try_match && *look == keytyped)
7881 return TRUE;
7882 ++look;
7883 }
7884
7885 /*
7886 * Skip over ", ".
7887 */
7888 look = skip_to_option_part(look);
7889 }
7890 return FALSE;
7891}
7892#endif /* FEAT_CINDENT */
7893
7894#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7895/*
7896 * Map Hebrew keyboard when in hkmap mode.
7897 */
7898 int
7899hkmap(c)
7900 int c;
7901{
7902 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7903 {
7904 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7905 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7906 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7907 static char_u map[26] =
7908 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7909 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7910 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7911 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7912 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7913 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7914 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7915 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7916 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7917
7918 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7919 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7920 /* '-1'='sofit' */
7921 else if (c == 'x')
7922 return 'X';
7923 else if (c == 'q')
7924 return '\''; /* {geresh}={'} */
7925 else if (c == 246)
7926 return ' '; /* \"o --> ' ' for a german keyboard */
7927 else if (c == 228)
7928 return ' '; /* \"a --> ' ' -- / -- */
7929 else if (c == 252)
7930 return ' '; /* \"u --> ' ' -- / -- */
7931#ifdef EBCDIC
7932 else if (islower(c))
7933#else
7934 /* NOTE: islower() does not do the right thing for us on Linux so we
7935 * do this the same was as 5.7 and previous, so it works correctly on
7936 * all systems. Specifically, the e.g. Delete and Arrow keys are
7937 * munged and won't work if e.g. searching for Hebrew text.
7938 */
7939 else if (c >= 'a' && c <= 'z')
7940#endif
7941 return (int)(map[CharOrdLow(c)] + p_aleph);
7942 else
7943 return c;
7944 }
7945 else
7946 {
7947 switch (c)
7948 {
7949 case '`': return ';';
7950 case '/': return '.';
7951 case '\'': return ',';
7952 case 'q': return '/';
7953 case 'w': return '\'';
7954
7955 /* Hebrew letters - set offset from 'a' */
7956 case ',': c = '{'; break;
7957 case '.': c = 'v'; break;
7958 case ';': c = 't'; break;
7959 default: {
7960 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7961
7962#ifdef EBCDIC
7963 /* see note about islower() above */
7964 if (!islower(c))
7965#else
7966 if (c < 'a' || c > 'z')
7967#endif
7968 return c;
7969 c = str[CharOrdLow(c)];
7970 break;
7971 }
7972 }
7973
7974 return (int)(CharOrdLow(c) + p_aleph);
7975 }
7976}
7977#endif
7978
7979 static void
7980ins_reg()
7981{
7982 int need_redraw = FALSE;
7983 int regname;
7984 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007985#ifdef FEAT_VISUAL
7986 int vis_active = VIsual_active;
7987#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007988
7989 /*
7990 * If we are going to wait for a character, show a '"'.
7991 */
7992 pc_status = PC_STATUS_UNSET;
7993 if (redrawing() && !char_avail())
7994 {
7995 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007996 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007997
7998 edit_putchar('"', TRUE);
7999#ifdef FEAT_CMDL_INFO
8000 add_to_showcmd_c(Ctrl_R);
8001#endif
8002 }
8003
8004#ifdef USE_ON_FLY_SCROLL
8005 dont_scroll = TRUE; /* disallow scrolling here */
8006#endif
8007
8008 /*
8009 * Don't map the register name. This also prevents the mode message to be
8010 * deleted when ESC is hit.
8011 */
8012 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008013 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008014 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8016 {
8017 /* Get a third key for literal register insertion */
8018 literally = regname;
8019#ifdef FEAT_CMDL_INFO
8020 add_to_showcmd_c(literally);
8021#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008022 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008023 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 }
8025 --no_mapping;
8026
8027#ifdef FEAT_EVAL
8028 /*
8029 * Don't call u_sync() while getting the expression,
8030 * evaluating it or giving an error message for it!
8031 */
8032 ++no_u_sync;
8033 if (regname == '=')
8034 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008035# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008036 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008037# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008039# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008040 /* Restore the Input Method. */
8041 if (im_on)
8042 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008043# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008045 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8046 {
8047 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008049 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050 else
8051 {
8052#endif
8053 if (literally == Ctrl_O || literally == Ctrl_P)
8054 {
8055 /* Append the command to the redo buffer. */
8056 AppendCharToRedobuff(Ctrl_R);
8057 AppendCharToRedobuff(literally);
8058 AppendCharToRedobuff(regname);
8059
8060 do_put(regname, BACKWARD, 1L,
8061 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8062 }
8063 else if (insert_reg(regname, literally) == FAIL)
8064 {
8065 vim_beep();
8066 need_redraw = TRUE; /* remove the '"' */
8067 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008068 else if (stop_insert_mode)
8069 /* When the '=' register was used and a function was invoked that
8070 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8071 * insert anything, need to remove the '"' */
8072 need_redraw = TRUE;
8073
Bram Moolenaar071d4272004-06-13 20:20:40 +00008074#ifdef FEAT_EVAL
8075 }
8076 --no_u_sync;
8077#endif
8078#ifdef FEAT_CMDL_INFO
8079 clear_showcmd();
8080#endif
8081
8082 /* If the inserted register is empty, we need to remove the '"' */
8083 if (need_redraw || stuff_empty())
8084 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008085
8086#ifdef FEAT_VISUAL
8087 /* Disallow starting Visual mode here, would get a weird mode. */
8088 if (!vis_active && VIsual_active)
8089 end_visual_mode();
8090#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091}
8092
8093/*
8094 * CTRL-G commands in Insert mode.
8095 */
8096 static void
8097ins_ctrl_g()
8098{
8099 int c;
8100
8101#ifdef FEAT_INS_EXPAND
8102 /* Right after CTRL-X the cursor will be after the ruler. */
8103 setcursor();
8104#endif
8105
8106 /*
8107 * Don't map the second key. This also prevents the mode message to be
8108 * deleted when ESC is hit.
8109 */
8110 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008111 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112 --no_mapping;
8113 switch (c)
8114 {
8115 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8116 case K_UP:
8117 case Ctrl_K:
8118 case 'k': ins_up(TRUE);
8119 break;
8120
8121 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8122 case K_DOWN:
8123 case Ctrl_J:
8124 case 'j': ins_down(TRUE);
8125 break;
8126
8127 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008128 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008130
8131 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008132 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008133 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008134 break;
8135
8136 /* Unknown CTRL-G command, reserved for future expansion. */
8137 default: vim_beep();
8138 }
8139}
8140
8141/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008142 * CTRL-^ in Insert mode.
8143 */
8144 static void
8145ins_ctrl_hat()
8146{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008147 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008148 {
8149 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8150 if (State & LANGMAP)
8151 {
8152 curbuf->b_p_iminsert = B_IMODE_NONE;
8153 State &= ~LANGMAP;
8154 }
8155 else
8156 {
8157 curbuf->b_p_iminsert = B_IMODE_LMAP;
8158 State |= LANGMAP;
8159#ifdef USE_IM_CONTROL
8160 im_set_active(FALSE);
8161#endif
8162 }
8163 }
8164#ifdef USE_IM_CONTROL
8165 else
8166 {
8167 /* There are no ":lmap" mappings, toggle IM */
8168 if (im_get_status())
8169 {
8170 curbuf->b_p_iminsert = B_IMODE_NONE;
8171 im_set_active(FALSE);
8172 }
8173 else
8174 {
8175 curbuf->b_p_iminsert = B_IMODE_IM;
8176 State &= ~LANGMAP;
8177 im_set_active(TRUE);
8178 }
8179 }
8180#endif
8181 set_iminsert_global();
8182 showmode();
8183#ifdef FEAT_GUI
8184 /* may show different cursor shape or color */
8185 if (gui.in_use)
8186 gui_update_cursor(TRUE, FALSE);
8187#endif
8188#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8189 /* Show/unshow value of 'keymap' in status lines. */
8190 status_redraw_curbuf();
8191#endif
8192}
8193
8194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 * Handle ESC in insert mode.
8196 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8197 * insert.
8198 */
8199 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008200ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008201 long *count;
8202 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008203 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204{
8205 int temp;
8206 static int disabled_redraw = FALSE;
8207
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008208#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008209 check_spell_redraw();
8210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008211#if defined(FEAT_HANGULIN)
8212# if defined(ESC_CHG_TO_ENG_MODE)
8213 hangul_input_state_set(0);
8214# endif
8215 if (composing_hangul)
8216 {
8217 push_raw_key(composing_hangul_buffer, 2);
8218 composing_hangul = 0;
8219 }
8220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221
8222 temp = curwin->w_cursor.col;
8223 if (disabled_redraw)
8224 {
8225 --RedrawingDisabled;
8226 disabled_redraw = FALSE;
8227 }
8228 if (!arrow_used)
8229 {
8230 /*
8231 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008232 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8233 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 */
8235 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008236 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008237
8238 /*
8239 * Repeating insert may take a long time. Check for
8240 * interrupt now and then.
8241 */
8242 if (*count > 0)
8243 {
8244 line_breakcheck();
8245 if (got_int)
8246 *count = 0;
8247 }
8248
8249 if (--*count > 0) /* repeat what was typed */
8250 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008251 /* Vi repeats the insert without replacing characters. */
8252 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8253 State &= ~REPLACE_FLAG;
8254
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 (void)start_redo_ins();
8256 if (cmdchar == 'r' || cmdchar == 'v')
8257 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8258 ++RedrawingDisabled;
8259 disabled_redraw = TRUE;
8260 return FALSE; /* repeat the insert */
8261 }
8262 stop_insert(&curwin->w_cursor, TRUE);
8263 undisplay_dollar();
8264 }
8265
8266 /* When an autoindent was removed, curswant stays after the
8267 * indent */
8268 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8269 curwin->w_set_curswant = TRUE;
8270
8271 /* Remember the last Insert position in the '^ mark. */
8272 if (!cmdmod.keepjumps)
8273 curbuf->b_last_insert = curwin->w_cursor;
8274
8275 /*
8276 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008277 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008278 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008279 if (!nomove
8280 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281#ifdef FEAT_VIRTUALEDIT
8282 || curwin->w_cursor.coladd > 0
8283#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008284 )
8285 && (restart_edit == NUL
8286 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008288 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008290 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008291#ifdef FEAT_RIGHTLEFT
8292 && !revins_on
8293#endif
8294 )
8295 {
8296#ifdef FEAT_VIRTUALEDIT
8297 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8298 {
8299 oneleft();
8300 if (restart_edit != NUL)
8301 ++curwin->w_cursor.coladd;
8302 }
8303 else
8304#endif
8305 {
8306 --curwin->w_cursor.col;
8307#ifdef FEAT_MBYTE
8308 /* Correct cursor for multi-byte character. */
8309 if (has_mbyte)
8310 mb_adjust_cursor();
8311#endif
8312 }
8313 }
8314
8315#ifdef USE_IM_CONTROL
8316 /* Disable IM to allow typing English directly for Normal mode commands.
8317 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8318 * well). */
8319 if (!(State & LANGMAP))
8320 im_save_status(&curbuf->b_p_iminsert);
8321 im_set_active(FALSE);
8322#endif
8323
8324 State = NORMAL;
8325 /* need to position cursor again (e.g. when on a TAB ) */
8326 changed_cline_bef_curs();
8327
8328#ifdef FEAT_MOUSE
8329 setmouse();
8330#endif
8331#ifdef CURSOR_SHAPE
8332 ui_cursor_shape(); /* may show different cursor shape */
8333#endif
8334
8335 /*
8336 * When recording or for CTRL-O, need to display the new mode.
8337 * Otherwise remove the mode message.
8338 */
8339 if (Recording || restart_edit != NUL)
8340 showmode();
8341 else if (p_smd)
8342 MSG("");
8343
8344 return TRUE; /* exit Insert mode */
8345}
8346
8347#ifdef FEAT_RIGHTLEFT
8348/*
8349 * Toggle language: hkmap and revins_on.
8350 * Move to end of reverse inserted text.
8351 */
8352 static void
8353ins_ctrl_()
8354{
8355 if (revins_on && revins_chars && revins_scol >= 0)
8356 {
8357 while (gchar_cursor() != NUL && revins_chars--)
8358 ++curwin->w_cursor.col;
8359 }
8360 p_ri = !p_ri;
8361 revins_on = (State == INSERT && p_ri);
8362 if (revins_on)
8363 {
8364 revins_scol = curwin->w_cursor.col;
8365 revins_legal++;
8366 revins_chars = 0;
8367 undisplay_dollar();
8368 }
8369 else
8370 revins_scol = -1;
8371#ifdef FEAT_FKMAP
8372 if (p_altkeymap)
8373 {
8374 /*
8375 * to be consistent also for redo command, using '.'
8376 * set arrow_used to true and stop it - causing to redo
8377 * characters entered in one mode (normal/reverse insert).
8378 */
8379 arrow_used = TRUE;
8380 (void)stop_arrow();
8381 p_fkmap = curwin->w_p_rl ^ p_ri;
8382 if (p_fkmap && p_ri)
8383 State = INSERT;
8384 }
8385 else
8386#endif
8387 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8388 showmode();
8389}
8390#endif
8391
8392#ifdef FEAT_VISUAL
8393/*
8394 * If 'keymodel' contains "startsel", may start selection.
8395 * Returns TRUE when a CTRL-O and other keys stuffed.
8396 */
8397 static int
8398ins_start_select(c)
8399 int c;
8400{
8401 if (km_startsel)
8402 switch (c)
8403 {
8404 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 case K_PAGEUP:
8407 case K_KPAGEUP:
8408 case K_PAGEDOWN:
8409 case K_KPAGEDOWN:
8410# ifdef MACOS
8411 case K_LEFT:
8412 case K_RIGHT:
8413 case K_UP:
8414 case K_DOWN:
8415 case K_END:
8416 case K_HOME:
8417# endif
8418 if (!(mod_mask & MOD_MASK_SHIFT))
8419 break;
8420 /* FALLTHROUGH */
8421 case K_S_LEFT:
8422 case K_S_RIGHT:
8423 case K_S_UP:
8424 case K_S_DOWN:
8425 case K_S_END:
8426 case K_S_HOME:
8427 /* Start selection right away, the cursor can move with
8428 * CTRL-O when beyond the end of the line. */
8429 start_selection();
8430
8431 /* Execute the key in (insert) Select mode. */
8432 stuffcharReadbuff(Ctrl_O);
8433 if (mod_mask)
8434 {
8435 char_u buf[4];
8436
8437 buf[0] = K_SPECIAL;
8438 buf[1] = KS_MODIFIER;
8439 buf[2] = mod_mask;
8440 buf[3] = NUL;
8441 stuffReadbuff(buf);
8442 }
8443 stuffcharReadbuff(c);
8444 return TRUE;
8445 }
8446 return FALSE;
8447}
8448#endif
8449
8450/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008451 * <Insert> key in Insert mode: toggle insert/remplace mode.
8452 */
8453 static void
8454ins_insert(replaceState)
8455 int replaceState;
8456{
8457#ifdef FEAT_FKMAP
8458 if (p_fkmap && p_ri)
8459 {
8460 beep_flush();
8461 EMSG(farsi_text_3); /* encoded in Farsi */
8462 return;
8463 }
8464#endif
8465
8466#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008467# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008468 set_vim_var_string(VV_INSERTMODE,
8469 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008470# ifdef FEAT_VREPLACE
8471 replaceState == VREPLACE ? "v" :
8472# endif
8473 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008474# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008475 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8476#endif
8477 if (State & REPLACE_FLAG)
8478 State = INSERT | (State & LANGMAP);
8479 else
8480 State = replaceState | (State & LANGMAP);
8481 AppendCharToRedobuff(K_INS);
8482 showmode();
8483#ifdef CURSOR_SHAPE
8484 ui_cursor_shape(); /* may show different cursor shape */
8485#endif
8486}
8487
8488/*
8489 * Pressed CTRL-O in Insert mode.
8490 */
8491 static void
8492ins_ctrl_o()
8493{
8494#ifdef FEAT_VREPLACE
8495 if (State & VREPLACE_FLAG)
8496 restart_edit = 'V';
8497 else
8498#endif
8499 if (State & REPLACE_FLAG)
8500 restart_edit = 'R';
8501 else
8502 restart_edit = 'I';
8503#ifdef FEAT_VIRTUALEDIT
8504 if (virtual_active())
8505 ins_at_eol = FALSE; /* cursor always keeps its column */
8506 else
8507#endif
8508 ins_at_eol = (gchar_cursor() == NUL);
8509}
8510
8511/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 * If the cursor is on an indent, ^T/^D insert/delete one
8513 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008514 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 * with vi. But vi only supports ^T and ^D after an
8516 * autoindent, we support it everywhere.
8517 */
8518 static void
8519ins_shift(c, lastc)
8520 int c;
8521 int lastc;
8522{
8523 if (stop_arrow() == FAIL)
8524 return;
8525 AppendCharToRedobuff(c);
8526
8527 /*
8528 * 0^D and ^^D: remove all indent.
8529 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008530 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8531 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532 {
8533 --curwin->w_cursor.col;
8534 (void)del_char(FALSE); /* delete the '^' or '0' */
8535 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8536 if (State & REPLACE_FLAG)
8537 replace_pop_ins();
8538 if (lastc == '^')
8539 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008540 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541 }
8542 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008543 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544
8545 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8546 did_ai = FALSE;
8547#ifdef FEAT_SMARTINDENT
8548 did_si = FALSE;
8549 can_si = FALSE;
8550 can_si_back = FALSE;
8551#endif
8552#ifdef FEAT_CINDENT
8553 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8554#endif
8555}
8556
8557 static void
8558ins_del()
8559{
8560 int temp;
8561
8562 if (stop_arrow() == FAIL)
8563 return;
8564 if (gchar_cursor() == NUL) /* delete newline */
8565 {
8566 temp = curwin->w_cursor.col;
8567 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008568 || do_join(2, FALSE, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 vim_beep();
8570 else
8571 curwin->w_cursor.col = temp;
8572 }
8573 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8574 vim_beep();
8575 did_ai = FALSE;
8576#ifdef FEAT_SMARTINDENT
8577 did_si = FALSE;
8578 can_si = FALSE;
8579 can_si_back = FALSE;
8580#endif
8581 AppendCharToRedobuff(K_DEL);
8582}
8583
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008584static void ins_bs_one __ARGS((colnr_T *vcolp));
8585
8586/*
8587 * Delete one character for ins_bs().
8588 */
8589 static void
8590ins_bs_one(vcolp)
8591 colnr_T *vcolp;
8592{
8593 dec_cursor();
8594 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8595 if (State & REPLACE_FLAG)
8596 {
8597 /* Don't delete characters before the insert point when in
8598 * Replace mode */
8599 if (curwin->w_cursor.lnum != Insstart.lnum
8600 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008601 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008602 }
8603 else
8604 (void)del_char(FALSE);
8605}
8606
Bram Moolenaar071d4272004-06-13 20:20:40 +00008607/*
8608 * Handle Backspace, delete-word and delete-line in Insert mode.
8609 * Return TRUE when backspace was actually used.
8610 */
8611 static int
8612ins_bs(c, mode, inserted_space_p)
8613 int c;
8614 int mode;
8615 int *inserted_space_p;
8616{
8617 linenr_T lnum;
8618 int cc;
8619 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008620 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 colnr_T mincol;
8622 int did_backspace = FALSE;
8623 int in_indent;
8624 int oldState;
8625#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008626 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627#endif
8628
8629 /*
8630 * can't delete anything in an empty file
8631 * can't backup past first character in buffer
8632 * can't backup past starting point unless 'backspace' > 1
8633 * can backup to a previous line if 'backspace' == 0
8634 */
8635 if ( bufempty()
8636 || (
8637#ifdef FEAT_RIGHTLEFT
8638 !revins_on &&
8639#endif
8640 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8641 || (!can_bs(BS_START)
8642 && (arrow_used
8643 || (curwin->w_cursor.lnum == Insstart.lnum
8644 && curwin->w_cursor.col <= Insstart.col)))
8645 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8646 && curwin->w_cursor.col <= ai_col)
8647 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8648 {
8649 vim_beep();
8650 return FALSE;
8651 }
8652
8653 if (stop_arrow() == FAIL)
8654 return FALSE;
8655 in_indent = inindent(0);
8656#ifdef FEAT_CINDENT
8657 if (in_indent)
8658 can_cindent = FALSE;
8659#endif
8660#ifdef FEAT_COMMENTS
8661 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8662#endif
8663#ifdef FEAT_RIGHTLEFT
8664 if (revins_on) /* put cursor after last inserted char */
8665 inc_cursor();
8666#endif
8667
8668#ifdef FEAT_VIRTUALEDIT
8669 /* Virtualedit:
8670 * BACKSPACE_CHAR eats a virtual space
8671 * BACKSPACE_WORD eats all coladd
8672 * BACKSPACE_LINE eats all coladd and keeps going
8673 */
8674 if (curwin->w_cursor.coladd > 0)
8675 {
8676 if (mode == BACKSPACE_CHAR)
8677 {
8678 --curwin->w_cursor.coladd;
8679 return TRUE;
8680 }
8681 if (mode == BACKSPACE_WORD)
8682 {
8683 curwin->w_cursor.coladd = 0;
8684 return TRUE;
8685 }
8686 curwin->w_cursor.coladd = 0;
8687 }
8688#endif
8689
8690 /*
8691 * delete newline!
8692 */
8693 if (curwin->w_cursor.col == 0)
8694 {
8695 lnum = Insstart.lnum;
8696 if (curwin->w_cursor.lnum == Insstart.lnum
8697#ifdef FEAT_RIGHTLEFT
8698 || revins_on
8699#endif
8700 )
8701 {
8702 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8703 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8704 return FALSE;
8705 --Insstart.lnum;
8706 Insstart.col = MAXCOL;
8707 }
8708 /*
8709 * In replace mode:
8710 * cc < 0: NL was inserted, delete it
8711 * cc >= 0: NL was replaced, put original characters back
8712 */
8713 cc = -1;
8714 if (State & REPLACE_FLAG)
8715 cc = replace_pop(); /* returns -1 if NL was inserted */
8716 /*
8717 * In replace mode, in the line we started replacing, we only move the
8718 * cursor.
8719 */
8720 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8721 {
8722 dec_cursor();
8723 }
8724 else
8725 {
8726#ifdef FEAT_VREPLACE
8727 if (!(State & VREPLACE_FLAG)
8728 || curwin->w_cursor.lnum > orig_line_count)
8729#endif
8730 {
8731 temp = gchar_cursor(); /* remember current char */
8732 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008733
8734 /* When "aw" is in 'formatoptions' we must delete the space at
8735 * the end of the line, otherwise the line will be broken
8736 * again when auto-formatting. */
8737 if (has_format_option(FO_AUTO)
8738 && has_format_option(FO_WHITE_PAR))
8739 {
8740 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8741 TRUE);
8742 int len;
8743
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008744 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008745 if (len > 0 && ptr[len - 1] == ' ')
8746 ptr[len - 1] = NUL;
8747 }
8748
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008749 (void)do_join(2, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750 if (temp == NUL && gchar_cursor() != NUL)
8751 inc_cursor();
8752 }
8753#ifdef FEAT_VREPLACE
8754 else
8755 dec_cursor();
8756#endif
8757
8758 /*
8759 * In REPLACE mode we have to put back the text that was replaced
8760 * by the NL. On the replace stack is first a NUL-terminated
8761 * sequence of characters that were deleted and then the
8762 * characters that NL replaced.
8763 */
8764 if (State & REPLACE_FLAG)
8765 {
8766 /*
8767 * Do the next ins_char() in NORMAL state, to
8768 * prevent ins_char() from replacing characters and
8769 * avoiding showmatch().
8770 */
8771 oldState = State;
8772 State = NORMAL;
8773 /*
8774 * restore characters (blanks) deleted after cursor
8775 */
8776 while (cc > 0)
8777 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008778 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008779#ifdef FEAT_MBYTE
8780 mb_replace_pop_ins(cc);
8781#else
8782 ins_char(cc);
8783#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008784 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008785 cc = replace_pop();
8786 }
8787 /* restore the characters that NL replaced */
8788 replace_pop_ins();
8789 State = oldState;
8790 }
8791 }
8792 did_ai = FALSE;
8793 }
8794 else
8795 {
8796 /*
8797 * Delete character(s) before the cursor.
8798 */
8799#ifdef FEAT_RIGHTLEFT
8800 if (revins_on) /* put cursor on last inserted char */
8801 dec_cursor();
8802#endif
8803 mincol = 0;
8804 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008805 if (mode == BACKSPACE_LINE
8806 && (curbuf->b_p_ai
8807#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008808 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008809#endif
8810 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008811#ifdef FEAT_RIGHTLEFT
8812 && !revins_on
8813#endif
8814 )
8815 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008816 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008817 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008818 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008820 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 }
8822
8823 /*
8824 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8825 */
8826 if ( mode == BACKSPACE_CHAR
8827 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008828 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008829 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 && (*(ml_get_cursor() - 1) == TAB
8831 || (*(ml_get_cursor() - 1) == ' '
8832 && (!*inserted_space_p
8833 || arrow_used))))))
8834 {
8835 int ts;
8836 colnr_T vcol;
8837 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008838 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839
8840 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008841 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008842 ts = curbuf->b_p_sw;
8843 else
8844 ts = curbuf->b_p_sts;
8845 /* Compute the virtual column where we want to be. Since
8846 * 'showbreak' may get in the way, need to get the last column of
8847 * the previous character. */
8848 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008849 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 dec_cursor();
8851 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8852 inc_cursor();
8853 want_vcol = (want_vcol / ts) * ts;
8854
8855 /* delete characters until we are at or before want_vcol */
8856 while (vcol > want_vcol
8857 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008858 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859
8860 /* insert extra spaces until we are at want_vcol */
8861 while (vcol < want_vcol)
8862 {
8863 /* Remember the first char we inserted */
8864 if (curwin->w_cursor.lnum == Insstart.lnum
8865 && curwin->w_cursor.col < Insstart.col)
8866 Insstart.col = curwin->w_cursor.col;
8867
8868#ifdef FEAT_VREPLACE
8869 if (State & VREPLACE_FLAG)
8870 ins_char(' ');
8871 else
8872#endif
8873 {
8874 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008875 if ((State & REPLACE_FLAG))
8876 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 }
8878 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8879 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008880
8881 /* If we are now back where we started delete one character. Can
8882 * happen when using 'sts' and 'linebreak'. */
8883 if (vcol >= start_vcol)
8884 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 }
8886
8887 /*
8888 * Delete upto starting point, start of line or previous word.
8889 */
8890 else do
8891 {
8892#ifdef FEAT_RIGHTLEFT
8893 if (!revins_on) /* put cursor on char to be deleted */
8894#endif
8895 dec_cursor();
8896
8897 /* start of word? */
8898 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8899 {
8900 mode = BACKSPACE_WORD_NOT_SPACE;
8901 temp = vim_iswordc(gchar_cursor());
8902 }
8903 /* end of word? */
8904 else if (mode == BACKSPACE_WORD_NOT_SPACE
8905 && (vim_isspace(cc = gchar_cursor())
8906 || vim_iswordc(cc) != temp))
8907 {
8908#ifdef FEAT_RIGHTLEFT
8909 if (!revins_on)
8910#endif
8911 inc_cursor();
8912#ifdef FEAT_RIGHTLEFT
8913 else if (State & REPLACE_FLAG)
8914 dec_cursor();
8915#endif
8916 break;
8917 }
8918 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008919 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 else
8921 {
8922#ifdef FEAT_MBYTE
8923 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008924 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008925#endif
8926 (void)del_char(FALSE);
8927#ifdef FEAT_MBYTE
8928 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008929 * If there are combining characters and 'delcombine' is set
8930 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008931 * character.
8932 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008933 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008934 inc_cursor();
8935#endif
8936#ifdef FEAT_RIGHTLEFT
8937 if (revins_chars)
8938 {
8939 revins_chars--;
8940 revins_legal++;
8941 }
8942 if (revins_on && gchar_cursor() == NUL)
8943 break;
8944#endif
8945 }
8946 /* Just a single backspace?: */
8947 if (mode == BACKSPACE_CHAR)
8948 break;
8949 } while (
8950#ifdef FEAT_RIGHTLEFT
8951 revins_on ||
8952#endif
8953 (curwin->w_cursor.col > mincol
8954 && (curwin->w_cursor.lnum != Insstart.lnum
8955 || curwin->w_cursor.col != Insstart.col)));
8956 did_backspace = TRUE;
8957 }
8958#ifdef FEAT_SMARTINDENT
8959 did_si = FALSE;
8960 can_si = FALSE;
8961 can_si_back = FALSE;
8962#endif
8963 if (curwin->w_cursor.col <= 1)
8964 did_ai = FALSE;
8965 /*
8966 * It's a little strange to put backspaces into the redo
8967 * buffer, but it makes auto-indent a lot easier to deal
8968 * with.
8969 */
8970 AppendCharToRedobuff(c);
8971
8972 /* If deleted before the insertion point, adjust it */
8973 if (curwin->w_cursor.lnum == Insstart.lnum
8974 && curwin->w_cursor.col < Insstart.col)
8975 Insstart.col = curwin->w_cursor.col;
8976
8977 /* vi behaviour: the cursor moves backward but the character that
8978 * was there remains visible
8979 * Vim behaviour: the cursor moves backward and the character that
8980 * was there is erased from the screen.
8981 * We can emulate the vi behaviour by pretending there is a dollar
8982 * displayed even when there isn't.
8983 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01008984 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008985 dollar_vcol = curwin->w_virtcol;
8986
Bram Moolenaarce3be472008-01-14 19:12:28 +00008987#ifdef FEAT_FOLDING
8988 /* When deleting a char the cursor line must never be in a closed fold.
8989 * E.g., when 'foldmethod' is indent and deleting the first non-white
8990 * char before a Tab. */
8991 if (did_backspace)
8992 foldOpenCursor();
8993#endif
8994
Bram Moolenaar071d4272004-06-13 20:20:40 +00008995 return did_backspace;
8996}
8997
8998#ifdef FEAT_MOUSE
8999 static void
9000ins_mouse(c)
9001 int c;
9002{
9003 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009004 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009005
9006# ifdef FEAT_GUI
9007 /* When GUI is active, also move/paste when 'mouse' is empty */
9008 if (!gui.in_use)
9009# endif
9010 if (!mouse_has(MOUSE_INSERT))
9011 return;
9012
9013 undisplay_dollar();
9014 tpos = curwin->w_cursor;
9015 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9016 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009017#ifdef FEAT_WINDOWS
9018 win_T *new_curwin = curwin;
9019
9020 if (curwin != old_curwin && win_valid(old_curwin))
9021 {
9022 /* Mouse took us to another window. We need to go back to the
9023 * previous one to stop insert there properly. */
9024 curwin = old_curwin;
9025 curbuf = curwin->w_buffer;
9026 }
9027#endif
9028 start_arrow(curwin == old_curwin ? &tpos : NULL);
9029#ifdef FEAT_WINDOWS
9030 if (curwin != new_curwin && win_valid(new_curwin))
9031 {
9032 curwin = new_curwin;
9033 curbuf = curwin->w_buffer;
9034 }
9035#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036# ifdef FEAT_CINDENT
9037 can_cindent = TRUE;
9038# endif
9039 }
9040
9041#ifdef FEAT_WINDOWS
9042 /* redraw status lines (in case another window became active) */
9043 redraw_statuslines();
9044#endif
9045}
9046
9047 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009048ins_mousescroll(dir)
9049 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050{
9051 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009052# if defined(FEAT_WINDOWS)
9053 win_T *old_curwin = curwin;
9054# endif
9055# ifdef FEAT_INS_EXPAND
9056 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009057# endif
9058
9059 tpos = curwin->w_cursor;
9060
9061# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062 /* Currently the mouse coordinates are only known in the GUI. */
9063 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
9064 {
9065 int row, col;
9066
9067 row = mouse_row;
9068 col = mouse_col;
9069
9070 /* find the window at the pointer coordinates */
9071 curwin = mouse_find_win(&row, &col);
9072 curbuf = curwin->w_buffer;
9073 }
9074 if (curwin == old_curwin)
9075# endif
9076 undisplay_dollar();
9077
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009078# ifdef FEAT_INS_EXPAND
9079 /* Don't scroll the window in which completion is being done. */
9080 if (!pum_visible()
9081# if defined(FEAT_WINDOWS)
9082 || curwin != old_curwin
9083# endif
9084 )
9085# endif
9086 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009087 if (dir == MSCR_DOWN || dir == MSCR_UP)
9088 {
9089 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9090 scroll_redraw(dir,
9091 (long)(curwin->w_botline - curwin->w_topline));
9092 else
9093 scroll_redraw(dir, 3L);
9094 }
9095#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009096 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009097 {
9098 int val, step = 6;
9099
9100 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9101 step = W_WIDTH(curwin);
9102 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9103 if (val < 0)
9104 val = 0;
9105 gui_do_horiz_scroll(val, TRUE);
9106 }
9107#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009108# ifdef FEAT_INS_EXPAND
9109 did_scroll = TRUE;
9110# endif
9111 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009112
9113# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
9114 curwin->w_redr_status = TRUE;
9115
9116 curwin = old_curwin;
9117 curbuf = curwin->w_buffer;
9118# endif
9119
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009120# ifdef FEAT_INS_EXPAND
9121 /* The popup menu may overlay the window, need to redraw it.
9122 * TODO: Would be more efficient to only redraw the windows that are
9123 * overlapped by the popup menu. */
9124 if (pum_visible() && did_scroll)
9125 {
9126 redraw_all_later(NOT_VALID);
9127 ins_compl_show_pum();
9128 }
9129# endif
9130
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 if (!equalpos(curwin->w_cursor, tpos))
9132 {
9133 start_arrow(&tpos);
9134# ifdef FEAT_CINDENT
9135 can_cindent = TRUE;
9136# endif
9137 }
9138}
9139#endif
9140
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009141#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009142 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009143ins_tabline(c)
9144 int c;
9145{
9146 /* We will be leaving the current window, unless closing another tab. */
9147 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9148 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9149 {
9150 undisplay_dollar();
9151 start_arrow(&curwin->w_cursor);
9152# ifdef FEAT_CINDENT
9153 can_cindent = TRUE;
9154# endif
9155 }
9156
9157 if (c == K_TABLINE)
9158 goto_tabpage(current_tab);
9159 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009160 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009161 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009162 redraw_statuslines(); /* will redraw the tabline when needed */
9163 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009164}
9165#endif
9166
9167#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009168 void
9169ins_scroll()
9170{
9171 pos_T tpos;
9172
9173 undisplay_dollar();
9174 tpos = curwin->w_cursor;
9175 if (gui_do_scroll())
9176 {
9177 start_arrow(&tpos);
9178# ifdef FEAT_CINDENT
9179 can_cindent = TRUE;
9180# endif
9181 }
9182}
9183
9184 void
9185ins_horscroll()
9186{
9187 pos_T tpos;
9188
9189 undisplay_dollar();
9190 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009191 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 {
9193 start_arrow(&tpos);
9194# ifdef FEAT_CINDENT
9195 can_cindent = TRUE;
9196# endif
9197 }
9198}
9199#endif
9200
9201 static void
9202ins_left()
9203{
9204 pos_T tpos;
9205
9206#ifdef FEAT_FOLDING
9207 if ((fdo_flags & FDO_HOR) && KeyTyped)
9208 foldOpenCursor();
9209#endif
9210 undisplay_dollar();
9211 tpos = curwin->w_cursor;
9212 if (oneleft() == OK)
9213 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009214#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9215 /* Only call start_arrow() when not busy with preediting, it will
9216 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9217 if (!im_is_preediting())
9218#endif
9219 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009220#ifdef FEAT_RIGHTLEFT
9221 /* If exit reversed string, position is fixed */
9222 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9223 revins_legal++;
9224 revins_chars++;
9225#endif
9226 }
9227
9228 /*
9229 * if 'whichwrap' set for cursor in insert mode may go to
9230 * previous line
9231 */
9232 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9233 {
9234 start_arrow(&tpos);
9235 --(curwin->w_cursor.lnum);
9236 coladvance((colnr_T)MAXCOL);
9237 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9238 }
9239 else
9240 vim_beep();
9241}
9242
9243 static void
9244ins_home(c)
9245 int c;
9246{
9247 pos_T tpos;
9248
9249#ifdef FEAT_FOLDING
9250 if ((fdo_flags & FDO_HOR) && KeyTyped)
9251 foldOpenCursor();
9252#endif
9253 undisplay_dollar();
9254 tpos = curwin->w_cursor;
9255 if (c == K_C_HOME)
9256 curwin->w_cursor.lnum = 1;
9257 curwin->w_cursor.col = 0;
9258#ifdef FEAT_VIRTUALEDIT
9259 curwin->w_cursor.coladd = 0;
9260#endif
9261 curwin->w_curswant = 0;
9262 start_arrow(&tpos);
9263}
9264
9265 static void
9266ins_end(c)
9267 int c;
9268{
9269 pos_T tpos;
9270
9271#ifdef FEAT_FOLDING
9272 if ((fdo_flags & FDO_HOR) && KeyTyped)
9273 foldOpenCursor();
9274#endif
9275 undisplay_dollar();
9276 tpos = curwin->w_cursor;
9277 if (c == K_C_END)
9278 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9279 coladvance((colnr_T)MAXCOL);
9280 curwin->w_curswant = MAXCOL;
9281
9282 start_arrow(&tpos);
9283}
9284
9285 static void
9286ins_s_left()
9287{
9288#ifdef FEAT_FOLDING
9289 if ((fdo_flags & FDO_HOR) && KeyTyped)
9290 foldOpenCursor();
9291#endif
9292 undisplay_dollar();
9293 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9294 {
9295 start_arrow(&curwin->w_cursor);
9296 (void)bck_word(1L, FALSE, FALSE);
9297 curwin->w_set_curswant = TRUE;
9298 }
9299 else
9300 vim_beep();
9301}
9302
9303 static void
9304ins_right()
9305{
9306#ifdef FEAT_FOLDING
9307 if ((fdo_flags & FDO_HOR) && KeyTyped)
9308 foldOpenCursor();
9309#endif
9310 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009311 if (gchar_cursor() != NUL
9312#ifdef FEAT_VIRTUALEDIT
9313 || virtual_active()
9314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315 )
9316 {
9317 start_arrow(&curwin->w_cursor);
9318 curwin->w_set_curswant = TRUE;
9319#ifdef FEAT_VIRTUALEDIT
9320 if (virtual_active())
9321 oneright();
9322 else
9323#endif
9324 {
9325#ifdef FEAT_MBYTE
9326 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009327 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009328 else
9329#endif
9330 ++curwin->w_cursor.col;
9331 }
9332
9333#ifdef FEAT_RIGHTLEFT
9334 revins_legal++;
9335 if (revins_chars)
9336 revins_chars--;
9337#endif
9338 }
9339 /* if 'whichwrap' set for cursor in insert mode, may move the
9340 * cursor to the next line */
9341 else if (vim_strchr(p_ww, ']') != NULL
9342 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9343 {
9344 start_arrow(&curwin->w_cursor);
9345 curwin->w_set_curswant = TRUE;
9346 ++curwin->w_cursor.lnum;
9347 curwin->w_cursor.col = 0;
9348 }
9349 else
9350 vim_beep();
9351}
9352
9353 static void
9354ins_s_right()
9355{
9356#ifdef FEAT_FOLDING
9357 if ((fdo_flags & FDO_HOR) && KeyTyped)
9358 foldOpenCursor();
9359#endif
9360 undisplay_dollar();
9361 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9362 || gchar_cursor() != NUL)
9363 {
9364 start_arrow(&curwin->w_cursor);
9365 (void)fwd_word(1L, FALSE, 0);
9366 curwin->w_set_curswant = TRUE;
9367 }
9368 else
9369 vim_beep();
9370}
9371
9372 static void
9373ins_up(startcol)
9374 int startcol; /* when TRUE move to Insstart.col */
9375{
9376 pos_T tpos;
9377 linenr_T old_topline = curwin->w_topline;
9378#ifdef FEAT_DIFF
9379 int old_topfill = curwin->w_topfill;
9380#endif
9381
9382 undisplay_dollar();
9383 tpos = curwin->w_cursor;
9384 if (cursor_up(1L, TRUE) == OK)
9385 {
9386 if (startcol)
9387 coladvance(getvcol_nolist(&Insstart));
9388 if (old_topline != curwin->w_topline
9389#ifdef FEAT_DIFF
9390 || old_topfill != curwin->w_topfill
9391#endif
9392 )
9393 redraw_later(VALID);
9394 start_arrow(&tpos);
9395#ifdef FEAT_CINDENT
9396 can_cindent = TRUE;
9397#endif
9398 }
9399 else
9400 vim_beep();
9401}
9402
9403 static void
9404ins_pageup()
9405{
9406 pos_T tpos;
9407
9408 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009409
9410#ifdef FEAT_WINDOWS
9411 if (mod_mask & MOD_MASK_CTRL)
9412 {
9413 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009414 if (first_tabpage->tp_next != NULL)
9415 {
9416 start_arrow(&curwin->w_cursor);
9417 goto_tabpage(-1);
9418 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009419 return;
9420 }
9421#endif
9422
Bram Moolenaar071d4272004-06-13 20:20:40 +00009423 tpos = curwin->w_cursor;
9424 if (onepage(BACKWARD, 1L) == OK)
9425 {
9426 start_arrow(&tpos);
9427#ifdef FEAT_CINDENT
9428 can_cindent = TRUE;
9429#endif
9430 }
9431 else
9432 vim_beep();
9433}
9434
9435 static void
9436ins_down(startcol)
9437 int startcol; /* when TRUE move to Insstart.col */
9438{
9439 pos_T tpos;
9440 linenr_T old_topline = curwin->w_topline;
9441#ifdef FEAT_DIFF
9442 int old_topfill = curwin->w_topfill;
9443#endif
9444
9445 undisplay_dollar();
9446 tpos = curwin->w_cursor;
9447 if (cursor_down(1L, TRUE) == OK)
9448 {
9449 if (startcol)
9450 coladvance(getvcol_nolist(&Insstart));
9451 if (old_topline != curwin->w_topline
9452#ifdef FEAT_DIFF
9453 || old_topfill != curwin->w_topfill
9454#endif
9455 )
9456 redraw_later(VALID);
9457 start_arrow(&tpos);
9458#ifdef FEAT_CINDENT
9459 can_cindent = TRUE;
9460#endif
9461 }
9462 else
9463 vim_beep();
9464}
9465
9466 static void
9467ins_pagedown()
9468{
9469 pos_T tpos;
9470
9471 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009472
9473#ifdef FEAT_WINDOWS
9474 if (mod_mask & MOD_MASK_CTRL)
9475 {
9476 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009477 if (first_tabpage->tp_next != NULL)
9478 {
9479 start_arrow(&curwin->w_cursor);
9480 goto_tabpage(0);
9481 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009482 return;
9483 }
9484#endif
9485
Bram Moolenaar071d4272004-06-13 20:20:40 +00009486 tpos = curwin->w_cursor;
9487 if (onepage(FORWARD, 1L) == OK)
9488 {
9489 start_arrow(&tpos);
9490#ifdef FEAT_CINDENT
9491 can_cindent = TRUE;
9492#endif
9493 }
9494 else
9495 vim_beep();
9496}
9497
9498#ifdef FEAT_DND
9499 static void
9500ins_drop()
9501{
9502 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9503}
9504#endif
9505
9506/*
9507 * Handle TAB in Insert or Replace mode.
9508 * Return TRUE when the TAB needs to be inserted like a normal character.
9509 */
9510 static int
9511ins_tab()
9512{
9513 int ind;
9514 int i;
9515 int temp;
9516
9517 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9518 Insstart_blank_vcol = get_nolist_virtcol();
9519 if (echeck_abbr(TAB + ABBR_OFF))
9520 return FALSE;
9521
9522 ind = inindent(0);
9523#ifdef FEAT_CINDENT
9524 if (ind)
9525 can_cindent = FALSE;
9526#endif
9527
9528 /*
9529 * When nothing special, insert TAB like a normal character
9530 */
9531 if (!curbuf->b_p_et
9532 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9533 && curbuf->b_p_sts == 0)
9534 return TRUE;
9535
9536 if (stop_arrow() == FAIL)
9537 return TRUE;
9538
9539 did_ai = FALSE;
9540#ifdef FEAT_SMARTINDENT
9541 did_si = FALSE;
9542 can_si = FALSE;
9543 can_si_back = FALSE;
9544#endif
9545 AppendToRedobuff((char_u *)"\t");
9546
9547 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9548 temp = (int)curbuf->b_p_sw;
9549 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9550 temp = (int)curbuf->b_p_sts;
9551 else /* otherwise use 'tabstop' */
9552 temp = (int)curbuf->b_p_ts;
9553 temp -= get_nolist_virtcol() % temp;
9554
9555 /*
9556 * Insert the first space with ins_char(). It will delete one char in
9557 * replace mode. Insert the rest with ins_str(); it will not delete any
9558 * chars. For VREPLACE mode, we use ins_char() for all characters.
9559 */
9560 ins_char(' ');
9561 while (--temp > 0)
9562 {
9563#ifdef FEAT_VREPLACE
9564 if (State & VREPLACE_FLAG)
9565 ins_char(' ');
9566 else
9567#endif
9568 {
9569 ins_str((char_u *)" ");
9570 if (State & REPLACE_FLAG) /* no char replaced */
9571 replace_push(NUL);
9572 }
9573 }
9574
9575 /*
9576 * When 'expandtab' not set: Replace spaces by TABs where possible.
9577 */
9578 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9579 {
9580 char_u *ptr;
9581#ifdef FEAT_VREPLACE
9582 char_u *saved_line = NULL; /* init for GCC */
9583 pos_T pos;
9584#endif
9585 pos_T fpos;
9586 pos_T *cursor;
9587 colnr_T want_vcol, vcol;
9588 int change_col = -1;
9589 int save_list = curwin->w_p_list;
9590
9591 /*
9592 * Get the current line. For VREPLACE mode, don't make real changes
9593 * yet, just work on a copy of the line.
9594 */
9595#ifdef FEAT_VREPLACE
9596 if (State & VREPLACE_FLAG)
9597 {
9598 pos = curwin->w_cursor;
9599 cursor = &pos;
9600 saved_line = vim_strsave(ml_get_curline());
9601 if (saved_line == NULL)
9602 return FALSE;
9603 ptr = saved_line + pos.col;
9604 }
9605 else
9606#endif
9607 {
9608 ptr = ml_get_cursor();
9609 cursor = &curwin->w_cursor;
9610 }
9611
9612 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9613 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9614 curwin->w_p_list = FALSE;
9615
9616 /* Find first white before the cursor */
9617 fpos = curwin->w_cursor;
9618 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9619 {
9620 --fpos.col;
9621 --ptr;
9622 }
9623
9624 /* In Replace mode, don't change characters before the insert point. */
9625 if ((State & REPLACE_FLAG)
9626 && fpos.lnum == Insstart.lnum
9627 && fpos.col < Insstart.col)
9628 {
9629 ptr += Insstart.col - fpos.col;
9630 fpos.col = Insstart.col;
9631 }
9632
9633 /* compute virtual column numbers of first white and cursor */
9634 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9635 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9636
9637 /* Use as many TABs as possible. Beware of 'showbreak' and
9638 * 'linebreak' adding extra virtual columns. */
9639 while (vim_iswhite(*ptr))
9640 {
9641 i = lbr_chartabsize((char_u *)"\t", vcol);
9642 if (vcol + i > want_vcol)
9643 break;
9644 if (*ptr != TAB)
9645 {
9646 *ptr = TAB;
9647 if (change_col < 0)
9648 {
9649 change_col = fpos.col; /* Column of first change */
9650 /* May have to adjust Insstart */
9651 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9652 Insstart.col = fpos.col;
9653 }
9654 }
9655 ++fpos.col;
9656 ++ptr;
9657 vcol += i;
9658 }
9659
9660 if (change_col >= 0)
9661 {
9662 int repl_off = 0;
9663
9664 /* Skip over the spaces we need. */
9665 while (vcol < want_vcol && *ptr == ' ')
9666 {
9667 vcol += lbr_chartabsize(ptr, vcol);
9668 ++ptr;
9669 ++repl_off;
9670 }
9671 if (vcol > want_vcol)
9672 {
9673 /* Must have a char with 'showbreak' just before it. */
9674 --ptr;
9675 --repl_off;
9676 }
9677 fpos.col += repl_off;
9678
9679 /* Delete following spaces. */
9680 i = cursor->col - fpos.col;
9681 if (i > 0)
9682 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009683 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009684 /* correct replace stack. */
9685 if ((State & REPLACE_FLAG)
9686#ifdef FEAT_VREPLACE
9687 && !(State & VREPLACE_FLAG)
9688#endif
9689 )
9690 for (temp = i; --temp >= 0; )
9691 replace_join(repl_off);
9692 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009693#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009694 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009695 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009696 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009697 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9698 (char_u *)"\t", 1);
9699 }
9700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 cursor->col -= i;
9702
9703#ifdef FEAT_VREPLACE
9704 /*
9705 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9706 * backspacing over the changed spacing and then inserting the new
9707 * spacing.
9708 */
9709 if (State & VREPLACE_FLAG)
9710 {
9711 /* Backspace from real cursor to change_col */
9712 backspace_until_column(change_col);
9713
9714 /* Insert each char in saved_line from changed_col to
9715 * ptr-cursor */
9716 ins_bytes_len(saved_line + change_col,
9717 cursor->col - change_col);
9718 }
9719#endif
9720 }
9721
9722#ifdef FEAT_VREPLACE
9723 if (State & VREPLACE_FLAG)
9724 vim_free(saved_line);
9725#endif
9726 curwin->w_p_list = save_list;
9727 }
9728
9729 return FALSE;
9730}
9731
9732/*
9733 * Handle CR or NL in insert mode.
9734 * Return TRUE when out of memory or can't undo.
9735 */
9736 static int
9737ins_eol(c)
9738 int c;
9739{
9740 int i;
9741
9742 if (echeck_abbr(c + ABBR_OFF))
9743 return FALSE;
9744 if (stop_arrow() == FAIL)
9745 return TRUE;
9746 undisplay_dollar();
9747
9748 /*
9749 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9750 * character under the cursor. Only push a NUL on the replace stack,
9751 * nothing to put back when the NL is deleted.
9752 */
9753 if ((State & REPLACE_FLAG)
9754#ifdef FEAT_VREPLACE
9755 && !(State & VREPLACE_FLAG)
9756#endif
9757 )
9758 replace_push(NUL);
9759
9760 /*
9761 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9762 * replacing the next line, so we push all of the characters left on the
9763 * line onto the replace stack. This is not done here though, it is done
9764 * in open_line().
9765 */
9766
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009767#ifdef FEAT_VIRTUALEDIT
9768 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9769 * CTRL-O). */
9770 if (virtual_active() && curwin->w_cursor.coladd > 0)
9771 coladvance(getviscol());
9772#endif
9773
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774#ifdef FEAT_RIGHTLEFT
9775# ifdef FEAT_FKMAP
9776 if (p_altkeymap && p_fkmap)
9777 fkmap(NL);
9778# endif
9779 /* NL in reverse insert will always start in the end of
9780 * current line. */
9781 if (revins_on)
9782 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9783#endif
9784
9785 AppendToRedobuff(NL_STR);
9786 i = open_line(FORWARD,
9787#ifdef FEAT_COMMENTS
9788 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9789#endif
9790 0, old_indent);
9791 old_indent = 0;
9792#ifdef FEAT_CINDENT
9793 can_cindent = TRUE;
9794#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009795#ifdef FEAT_FOLDING
9796 /* When inserting a line the cursor line must never be in a closed fold. */
9797 foldOpenCursor();
9798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799
9800 return (!i);
9801}
9802
9803#ifdef FEAT_DIGRAPHS
9804/*
9805 * Handle digraph in insert mode.
9806 * Returns character still to be inserted, or NUL when nothing remaining to be
9807 * done.
9808 */
9809 static int
9810ins_digraph()
9811{
9812 int c;
9813 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009814 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815
9816 pc_status = PC_STATUS_UNSET;
9817 if (redrawing() && !char_avail())
9818 {
9819 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009820 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821
9822 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009823 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009824#ifdef FEAT_CMDL_INFO
9825 add_to_showcmd_c(Ctrl_K);
9826#endif
9827 }
9828
9829#ifdef USE_ON_FLY_SCROLL
9830 dont_scroll = TRUE; /* disallow scrolling here */
9831#endif
9832
9833 /* don't map the digraph chars. This also prevents the
9834 * mode message to be deleted when ESC is hit */
9835 ++no_mapping;
9836 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009837 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838 --no_mapping;
9839 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009840 if (did_putchar)
9841 /* when the line fits in 'columns' the '?' is at the start of the next
9842 * line and will not be removed by the redraw */
9843 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009844
Bram Moolenaar071d4272004-06-13 20:20:40 +00009845 if (IS_SPECIAL(c) || mod_mask) /* special key */
9846 {
9847#ifdef FEAT_CMDL_INFO
9848 clear_showcmd();
9849#endif
9850 insert_special(c, TRUE, FALSE);
9851 return NUL;
9852 }
9853 if (c != ESC)
9854 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009855 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009856 if (redrawing() && !char_avail())
9857 {
9858 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009859 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860
9861 if (char2cells(c) == 1)
9862 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009863 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009865 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009866 }
9867#ifdef FEAT_CMDL_INFO
9868 add_to_showcmd_c(c);
9869#endif
9870 }
9871 ++no_mapping;
9872 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009873 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009874 --no_mapping;
9875 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009876 if (did_putchar)
9877 /* when the line fits in 'columns' the '?' is at the start of the
9878 * next line and will not be removed by a redraw */
9879 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880 if (cc != ESC)
9881 {
9882 AppendToRedobuff((char_u *)CTRL_V_STR);
9883 c = getdigraph(c, cc, TRUE);
9884#ifdef FEAT_CMDL_INFO
9885 clear_showcmd();
9886#endif
9887 return c;
9888 }
9889 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890#ifdef FEAT_CMDL_INFO
9891 clear_showcmd();
9892#endif
9893 return NUL;
9894}
9895#endif
9896
9897/*
9898 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9899 * Returns the char to be inserted, or NUL if none found.
9900 */
Bram Moolenaar8320da42012-04-30 18:18:47 +02009901 int
Bram Moolenaar071d4272004-06-13 20:20:40 +00009902ins_copychar(lnum)
9903 linenr_T lnum;
9904{
9905 int c;
9906 int temp;
9907 char_u *ptr, *prev_ptr;
9908
9909 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9910 {
9911 vim_beep();
9912 return NUL;
9913 }
9914
9915 /* try to advance to the cursor column */
9916 temp = 0;
9917 ptr = ml_get(lnum);
9918 prev_ptr = ptr;
9919 validate_virtcol();
9920 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9921 {
9922 prev_ptr = ptr;
9923 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9924 }
9925 if ((colnr_T)temp > curwin->w_virtcol)
9926 ptr = prev_ptr;
9927
9928#ifdef FEAT_MBYTE
9929 c = (*mb_ptr2char)(ptr);
9930#else
9931 c = *ptr;
9932#endif
9933 if (c == NUL)
9934 vim_beep();
9935 return c;
9936}
9937
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009938/*
9939 * CTRL-Y or CTRL-E typed in Insert mode.
9940 */
9941 static int
9942ins_ctrl_ey(tc)
9943 int tc;
9944{
9945 int c = tc;
9946
9947#ifdef FEAT_INS_EXPAND
9948 if (ctrl_x_mode == CTRL_X_SCROLL)
9949 {
9950 if (c == Ctrl_Y)
9951 scrolldown_clamp();
9952 else
9953 scrollup_clamp();
9954 redraw_later(VALID);
9955 }
9956 else
9957#endif
9958 {
9959 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9960 if (c != NUL)
9961 {
9962 long tw_save;
9963
9964 /* The character must be taken literally, insert like it
9965 * was typed after a CTRL-V, and pretend 'textwidth'
9966 * wasn't set. Digits, 'o' and 'x' are special after a
9967 * CTRL-V, don't use it for these. */
9968 if (c < 256 && !isalnum(c))
9969 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9970 tw_save = curbuf->b_p_tw;
9971 curbuf->b_p_tw = -1;
9972 insert_special(c, TRUE, FALSE);
9973 curbuf->b_p_tw = tw_save;
9974#ifdef FEAT_RIGHTLEFT
9975 revins_chars++;
9976 revins_legal++;
9977#endif
9978 c = Ctrl_V; /* pretend CTRL-V is last character */
9979 auto_format(FALSE, TRUE);
9980 }
9981 }
9982 return c;
9983}
9984
Bram Moolenaar071d4272004-06-13 20:20:40 +00009985#ifdef FEAT_SMARTINDENT
9986/*
9987 * Try to do some very smart auto-indenting.
9988 * Used when inserting a "normal" character.
9989 */
9990 static void
9991ins_try_si(c)
9992 int c;
9993{
9994 pos_T *pos, old_pos;
9995 char_u *ptr;
9996 int i;
9997 int temp;
9998
9999 /*
10000 * do some very smart indenting when entering '{' or '}'
10001 */
10002 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10003 {
10004 /*
10005 * for '}' set indent equal to indent of line containing matching '{'
10006 */
10007 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10008 {
10009 old_pos = curwin->w_cursor;
10010 /*
10011 * If the matching '{' has a ')' immediately before it (ignoring
10012 * white-space), then line up with the start of the line
10013 * containing the matching '(' if there is one. This handles the
10014 * case where an "if (..\n..) {" statement continues over multiple
10015 * lines -- webb
10016 */
10017 ptr = ml_get(pos->lnum);
10018 i = pos->col;
10019 if (i > 0) /* skip blanks before '{' */
10020 while (--i > 0 && vim_iswhite(ptr[i]))
10021 ;
10022 curwin->w_cursor.lnum = pos->lnum;
10023 curwin->w_cursor.col = i;
10024 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10025 curwin->w_cursor = *pos;
10026 i = get_indent();
10027 curwin->w_cursor = old_pos;
10028#ifdef FEAT_VREPLACE
10029 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010030 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010031 else
10032#endif
10033 (void)set_indent(i, SIN_CHANGED);
10034 }
10035 else if (curwin->w_cursor.col > 0)
10036 {
10037 /*
10038 * when inserting '{' after "O" reduce indent, but not
10039 * more than indent of previous line
10040 */
10041 temp = TRUE;
10042 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10043 {
10044 old_pos = curwin->w_cursor;
10045 i = get_indent();
10046 while (curwin->w_cursor.lnum > 1)
10047 {
10048 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10049
10050 /* ignore empty lines and lines starting with '#'. */
10051 if (*ptr != '#' && *ptr != NUL)
10052 break;
10053 }
10054 if (get_indent() >= i)
10055 temp = FALSE;
10056 curwin->w_cursor = old_pos;
10057 }
10058 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010059 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010060 }
10061 }
10062
10063 /*
10064 * set indent of '#' always to 0
10065 */
10066 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10067 {
10068 /* remember current indent for next line */
10069 old_indent = get_indent();
10070 (void)set_indent(0, SIN_CHANGED);
10071 }
10072
10073 /* Adjust ai_col, the char at this position can be deleted. */
10074 if (ai_col > curwin->w_cursor.col)
10075 ai_col = curwin->w_cursor.col;
10076}
10077#endif
10078
10079/*
10080 * Get the value that w_virtcol would have when 'list' is off.
10081 * Unless 'cpo' contains the 'L' flag.
10082 */
10083 static colnr_T
10084get_nolist_virtcol()
10085{
10086 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10087 return getvcol_nolist(&curwin->w_cursor);
10088 validate_virtcol();
10089 return curwin->w_virtcol;
10090}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010091
10092#ifdef FEAT_AUTOCMD
10093/*
10094 * Handle the InsertCharPre autocommand.
10095 * "c" is the character that was typed.
10096 * Return a pointer to allocated memory with the replacement string.
10097 * Return NULL to continue inserting "c".
10098 */
10099 static char_u *
10100do_insert_char_pre(c)
10101 int c;
10102{
10103 char_u *res;
10104
10105 /* Return quickly when there is nothing to do. */
10106 if (!has_insertcharpre())
10107 return NULL;
10108
10109 /* Lock the text to avoid weird things from happening. */
10110 ++textlock;
10111 set_vim_var_char(c); /* set v:char */
10112
10113 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
10114 /* Get the new value of v:char. It may be empty or more than one
10115 * character. */
10116 res = vim_strsave(get_vim_var_str(VV_CHAR));
10117 else
10118 res = NULL;
10119
10120 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10121 --textlock;
10122
10123 return res;
10124}
10125#endif