blob: c8320a6e23b5ca3c7824578b8b503fe22ed2cf13 [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
256static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000257static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258#ifdef FEAT_SMARTINDENT
259static void ins_try_si __ARGS((int c));
260#endif
261static colnr_T get_nolist_virtcol __ARGS((void));
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100262#ifdef FEAT_AUTOCMD
263static char_u *do_insert_char_pre __ARGS((int c));
264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
266static colnr_T Insstart_textlen; /* length of line when insert started */
267static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
268
269static char_u *last_insert = NULL; /* the text of the previous insert,
270 K_SPECIAL and CSI are escaped */
271static int last_insert_skip; /* nr of chars in front of previous insert */
272static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000273static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000274
275#ifdef FEAT_CINDENT
276static int can_cindent; /* may do cindenting on this line */
277#endif
278
279static int old_indent = 0; /* for ^^D command in insert mode */
280
281#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000282static int revins_on; /* reverse insert mode on */
283static int revins_chars; /* how much to skip after edit */
284static int revins_legal; /* was the last char 'legal'? */
285static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000286#endif
287
Bram Moolenaar071d4272004-06-13 20:20:40 +0000288static int ins_need_undo; /* call u_save() before inserting a
289 char. Set when edit() is called.
290 after that arrow_used is used. */
291
292static int did_add_space = FALSE; /* auto_format() added an extra space
293 under the cursor */
294
295/*
296 * edit(): Start inserting text.
297 *
298 * "cmdchar" can be:
299 * 'i' normal insert command
300 * 'a' normal append command
301 * 'R' replace command
302 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
303 * but still only one <CR> is inserted. The <Esc> is not used for redo.
304 * 'g' "gI" command.
305 * 'V' "gR" command for Virtual Replace mode.
306 * 'v' "gr" command for single character Virtual Replace mode.
307 *
308 * This function is not called recursively. For CTRL-O commands, it returns
309 * and lets the caller handle the Normal-mode command.
310 *
311 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
312 */
313 int
314edit(cmdchar, startln, count)
315 int cmdchar;
316 int startln; /* if set, insert at start of line */
317 long count;
318{
319 int c = 0;
320 char_u *ptr;
321 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000322 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000324 int i;
325 int did_backspace = TRUE; /* previous char was backspace */
326#ifdef FEAT_CINDENT
327 int line_is_white = FALSE; /* line is empty before insert */
328#endif
329 linenr_T old_topline = 0; /* topline before insertion */
330#ifdef FEAT_DIFF
331 int old_topfill = -1;
332#endif
333 int inserted_space = FALSE; /* just inserted a space */
334 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000335 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000336
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000337 /* Remember whether editing was restarted after CTRL-O. */
338 did_restart_edit = restart_edit;
339
Bram Moolenaar071d4272004-06-13 20:20:40 +0000340 /* sleep before redrawing, needed for "CTRL-O :" that results in an
341 * error message */
342 check_for_delay(TRUE);
343
344#ifdef HAVE_SANDBOX
345 /* Don't allow inserting in the sandbox. */
346 if (sandbox != 0)
347 {
348 EMSG(_(e_sandbox));
349 return FALSE;
350 }
351#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000352 /* Don't allow changes in the buffer while editing the cmdline. The
353 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000354 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000355 {
356 EMSG(_(e_secure));
357 return FALSE;
358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359
360#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000361 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000362 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000363 {
364 EMSG(_(e_secure));
365 return FALSE;
366 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000367 ins_compl_clear(); /* clear stuff for CTRL-X mode */
368#endif
369
Bram Moolenaar843ee412004-06-30 16:16:41 +0000370#ifdef FEAT_AUTOCMD
371 /*
372 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
373 */
374 if (cmdchar != 'r' && cmdchar != 'v')
375 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000376# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000377 if (cmdchar == 'R')
378 ptr = (char_u *)"r";
379 else if (cmdchar == 'V')
380 ptr = (char_u *)"v";
381 else
382 ptr = (char_u *)"i";
383 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000384# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000385 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
386 }
387#endif
388
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200389#ifdef FEAT_CONCEAL
390 /* Check if the cursor line needs redrawing before changing State. If
391 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200392 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200393#endif
394
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395#ifdef FEAT_MOUSE
396 /*
397 * When doing a paste with the middle mouse button, Insstart is set to
398 * where the paste started.
399 */
400 if (where_paste_started.lnum != 0)
401 Insstart = where_paste_started;
402 else
403#endif
404 {
405 Insstart = curwin->w_cursor;
406 if (startln)
407 Insstart.col = 0;
408 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000409 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000410 Insstart_blank_vcol = MAXCOL;
411 if (!did_ai)
412 ai_col = 0;
413
414 if (cmdchar != NUL && restart_edit == 0)
415 {
416 ResetRedobuff();
417 AppendNumberToRedobuff(count);
418#ifdef FEAT_VREPLACE
419 if (cmdchar == 'V' || cmdchar == 'v')
420 {
421 /* "gR" or "gr" command */
422 AppendCharToRedobuff('g');
423 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
424 }
425 else
426#endif
427 {
428 AppendCharToRedobuff(cmdchar);
429 if (cmdchar == 'g') /* "gI" command */
430 AppendCharToRedobuff('I');
431 else if (cmdchar == 'r') /* "r<CR>" command */
432 count = 1; /* insert only one <CR> */
433 }
434 }
435
436 if (cmdchar == 'R')
437 {
438#ifdef FEAT_FKMAP
439 if (p_fkmap && p_ri)
440 {
441 beep_flush();
442 EMSG(farsi_text_3); /* encoded in Farsi */
443 State = INSERT;
444 }
445 else
446#endif
447 State = REPLACE;
448 }
449#ifdef FEAT_VREPLACE
450 else if (cmdchar == 'V' || cmdchar == 'v')
451 {
452 State = VREPLACE;
453 replaceState = VREPLACE;
454 orig_line_count = curbuf->b_ml.ml_line_count;
455 vr_lines_changed = 1;
456 }
457#endif
458 else
459 State = INSERT;
460
461 stop_insert_mode = FALSE;
462
463 /*
464 * Need to recompute the cursor position, it might move when the cursor is
465 * on a TAB or special character.
466 */
467 curs_columns(TRUE);
468
469 /*
470 * Enable langmap or IME, indicated by 'iminsert'.
471 * Note that IME may enabled/disabled without us noticing here, thus the
472 * 'iminsert' value may not reflect what is actually used. It is updated
473 * when hitting <Esc>.
474 */
475 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
476 State |= LANGMAP;
477#ifdef USE_IM_CONTROL
478 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
479#endif
480
Bram Moolenaar071d4272004-06-13 20:20:40 +0000481#ifdef FEAT_MOUSE
482 setmouse();
483#endif
484#ifdef FEAT_CMDL_INFO
485 clear_showcmd();
486#endif
487#ifdef FEAT_RIGHTLEFT
488 /* there is no reverse replace mode */
489 revins_on = (State == INSERT && p_ri);
490 if (revins_on)
491 undisplay_dollar();
492 revins_chars = 0;
493 revins_legal = 0;
494 revins_scol = -1;
495#endif
496
497 /*
498 * Handle restarting Insert mode.
499 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
500 * restart_edit non-zero, and something in the stuff buffer.
501 */
502 if (restart_edit != 0 && stuff_empty())
503 {
504#ifdef FEAT_MOUSE
505 /*
506 * After a paste we consider text typed to be part of the insert for
507 * the pasted text. You can backspace over the pasted text too.
508 */
509 if (where_paste_started.lnum)
510 arrow_used = FALSE;
511 else
512#endif
513 arrow_used = TRUE;
514 restart_edit = 0;
515
516 /*
517 * If the cursor was after the end-of-line before the CTRL-O and it is
518 * now at the end-of-line, put it after the end-of-line (this is not
519 * correct in very rare cases).
520 * Also do this if curswant is greater than the current virtual
521 * column. Eg after "^O$" or "^O80|".
522 */
523 validate_virtcol();
524 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000525 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 || curwin->w_curswant > curwin->w_virtcol)
527 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
528 {
529 if (ptr[1] == NUL)
530 ++curwin->w_cursor.col;
531#ifdef FEAT_MBYTE
532 else if (has_mbyte)
533 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000534 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000535 if (ptr[i] == NUL)
536 curwin->w_cursor.col += i;
537 }
538#endif
539 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000540 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000541 }
542 else
543 arrow_used = FALSE;
544
545 /* we are in insert mode now, don't need to start it anymore */
546 need_start_insertmode = FALSE;
547
548 /* Need to save the line for undo before inserting the first char. */
549 ins_need_undo = TRUE;
550
551#ifdef FEAT_MOUSE
552 where_paste_started.lnum = 0;
553#endif
554#ifdef FEAT_CINDENT
555 can_cindent = TRUE;
556#endif
557#ifdef FEAT_FOLDING
558 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
559 * restarting. */
560 if (!p_im && did_restart_edit == 0)
561 foldOpenCursor();
562#endif
563
564 /*
565 * If 'showmode' is set, show the current (insert/replace/..) mode.
566 * A warning message for changing a readonly file is given here, before
567 * actually changing anything. It's put after the mode, if any.
568 */
569 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000570 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000571 i = showmode();
572
573 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000574 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000575
576#ifdef CURSOR_SHAPE
577 ui_cursor_shape(); /* may show different cursor shape */
578#endif
579#ifdef FEAT_DIGRAPHS
580 do_digraph(-1); /* clear digraphs */
581#endif
582
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000583 /*
584 * Get the current length of the redo buffer, those characters have to be
585 * skipped if we want to get to the inserted characters.
586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000587 ptr = get_inserted();
588 if (ptr == NULL)
589 new_insert_skip = 0;
590 else
591 {
592 new_insert_skip = (int)STRLEN(ptr);
593 vim_free(ptr);
594 }
595
596 old_indent = 0;
597
598 /*
599 * Main loop in Insert mode: repeat until Insert mode is left.
600 */
601 for (;;)
602 {
603#ifdef FEAT_RIGHTLEFT
604 if (!revins_legal)
605 revins_scol = -1; /* reset on illegal motions */
606 else
607 revins_legal = 0;
608#endif
609 if (arrow_used) /* don't repeat insert when arrow key used */
610 count = 0;
611
612 if (stop_insert_mode)
613 {
614 /* ":stopinsert" used or 'insertmode' reset */
615 count = 0;
616 goto doESCkey;
617 }
618
619 /* set curwin->w_curswant for next K_DOWN or K_UP */
620 if (!arrow_used)
621 curwin->w_set_curswant = TRUE;
622
623 /* If there is no typeahead may check for timestamps (e.g., for when a
624 * menu invoked a shell command). */
625 if (stuff_empty())
626 {
627 did_check_timestamps = FALSE;
628 if (need_check_timestamps)
629 check_timestamps(FALSE);
630 }
631
632 /*
633 * When emsg() was called msg_scroll will have been set.
634 */
635 msg_scroll = FALSE;
636
637#ifdef FEAT_GUI
638 /* When 'mousefocus' is set a mouse movement may have taken us to
639 * another window. "need_mouse_correct" may then be set because of an
640 * autocommand. */
641 if (need_mouse_correct)
642 gui_mouse_correct();
643#endif
644
645#ifdef FEAT_FOLDING
646 /* Open fold at the cursor line, according to 'foldopen'. */
647 if (fdo_flags & FDO_INSERT)
648 foldOpenCursor();
649 /* Close folds where the cursor isn't, according to 'foldclose' */
650 if (!char_avail())
651 foldCheckClose();
652#endif
653
654 /*
655 * If we inserted a character at the last position of the last line in
656 * the window, scroll the window one line up. This avoids an extra
657 * redraw.
658 * This is detected when the cursor column is smaller after inserting
659 * something.
660 * Don't do this when the topline changed already, it has
661 * already been adjusted (by insertchar() calling open_line())).
662 */
663 if (curbuf->b_mod_set
664 && curwin->w_p_wrap
665 && !did_backspace
666 && curwin->w_topline == old_topline
667#ifdef FEAT_DIFF
668 && curwin->w_topfill == old_topfill
669#endif
670 )
671 {
672 mincol = curwin->w_wcol;
673 validate_cursor_col();
674
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000675 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000676 && curwin->w_wrow == W_WINROW(curwin)
677 + curwin->w_height - 1 - p_so
678 && (curwin->w_cursor.lnum != curwin->w_topline
679#ifdef FEAT_DIFF
680 || curwin->w_topfill > 0
681#endif
682 ))
683 {
684#ifdef FEAT_DIFF
685 if (curwin->w_topfill > 0)
686 --curwin->w_topfill;
687 else
688#endif
689#ifdef FEAT_FOLDING
690 if (hasFolding(curwin->w_topline, NULL, &old_topline))
691 set_topline(curwin, old_topline + 1);
692 else
693#endif
694 set_topline(curwin, curwin->w_topline + 1);
695 }
696 }
697
698 /* May need to adjust w_topline to show the cursor. */
699 update_topline();
700
701 did_backspace = FALSE;
702
703 validate_cursor(); /* may set must_redraw */
704
705 /*
706 * Redraw the display when no characters are waiting.
707 * Also shows mode, ruler and positions cursor.
708 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000709 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710
711#ifdef FEAT_SCROLLBIND
712 if (curwin->w_p_scb)
713 do_check_scrollbind(TRUE);
714#endif
715
Bram Moolenaar860cae12010-06-05 23:22:07 +0200716#ifdef FEAT_CURSORBIND
717 if (curwin->w_p_crb)
718 do_check_cursorbind();
719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000720 update_curswant();
721 old_topline = curwin->w_topline;
722#ifdef FEAT_DIFF
723 old_topfill = curwin->w_topfill;
724#endif
725
726#ifdef USE_ON_FLY_SCROLL
727 dont_scroll = FALSE; /* allow scrolling here */
728#endif
729
730 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000731 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000732 */
733 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000734 do
735 {
736 c = safe_vgetc();
737 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000738
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000739#ifdef FEAT_AUTOCMD
740 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
741 did_cursorhold = TRUE;
742#endif
743
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744#ifdef FEAT_RIGHTLEFT
745 if (p_hkmap && KeyTyped)
746 c = hkmap(c); /* Hebrew mode mapping */
747#endif
748#ifdef FEAT_FKMAP
749 if (p_fkmap && KeyTyped)
750 c = fkmap(c); /* Farsi mode mapping */
751#endif
752
753#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000754 /*
755 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000756 * and the cursor is still in the completed word. Only when there is
757 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000758 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000759 if (compl_started
760 && pum_wanted()
761 && curwin->w_cursor.col >= compl_col
762 && (compl_shown_match == NULL
763 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000764 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000765 /* BS: Delete one character from "compl_leader". */
766 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000767 && curwin->w_cursor.col > compl_col
768 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000769 continue;
770
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000771 /* When no match was selected or it was edited. */
772 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000773 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000774 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000775 * "compl_leader". Except when at the original match and
776 * there is nothing to add, CTRL-L works like CTRL-P then. */
777 if (c == Ctrl_L
778 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000779 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000780 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000781 {
782 ins_compl_addfrommatch();
783 continue;
784 }
785
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000786 /* A non-white character that fits in with the current
787 * completion: Add to "compl_leader". */
788 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000789 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +0100790#ifdef FEAT_AUTOCMD
791 /* Trigger InsertCharPre. */
792 char_u *str = do_insert_char_pre(c);
793 char_u *p;
794
795 if (str != NULL)
796 {
797 for (p = str; *p != NUL; mb_ptr_adv(p))
798 ins_compl_addleader(PTR2CHAR(p));
799 vim_free(str);
800 }
801 else
802#endif
803 ins_compl_addleader(c);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000804 continue;
805 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000806
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000807 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000808 * compl_enter_selects is set the Enter key does the same. */
809 if (c == Ctrl_Y || (compl_enter_selects
810 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000811 {
812 ins_compl_delete();
813 ins_compl_insert();
814 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000815 }
816 }
817
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
819 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000820 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000821 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000822 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000823#endif
824
Bram Moolenaar488c6512005-08-11 20:09:58 +0000825 /* CTRL-\ CTRL-N goes to Normal mode,
826 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
827 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000828 if (c == Ctrl_BSL)
829 {
830 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000831 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 ++no_mapping;
833 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000834 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000835 --no_mapping;
836 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000837 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000839 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000840 vungetc(c);
841 c = Ctrl_BSL;
842 }
843 else if (c == Ctrl_G && p_im)
844 continue;
845 else
846 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000847 if (c == Ctrl_O)
848 {
849 ins_ctrl_o();
850 ins_at_eol = FALSE; /* cursor keeps its column */
851 nomove = TRUE;
852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000853 count = 0;
854 goto doESCkey;
855 }
856 }
857
858#ifdef FEAT_DIGRAPHS
859 c = do_digraph(c);
860#endif
861
862#ifdef FEAT_INS_EXPAND
863 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
864 goto docomplete;
865#endif
866 if (c == Ctrl_V || c == Ctrl_Q)
867 {
868 ins_ctrl_v();
869 c = Ctrl_V; /* pretend CTRL-V is last typed character */
870 continue;
871 }
872
873#ifdef FEAT_CINDENT
874 if (cindent_on()
875# ifdef FEAT_INS_EXPAND
876 && ctrl_x_mode == 0
877# endif
878 )
879 {
880 /* A key name preceded by a bang means this key is not to be
881 * inserted. Skip ahead to the re-indenting below.
882 * A key name preceded by a star means that indenting has to be
883 * done before inserting the key. */
884 line_is_white = inindent(0);
885 if (in_cinkeys(c, '!', line_is_white))
886 goto force_cindent;
887 if (can_cindent && in_cinkeys(c, '*', line_is_white)
888 && stop_arrow() == OK)
889 do_c_expr_indent();
890 }
891#endif
892
893#ifdef FEAT_RIGHTLEFT
894 if (curwin->w_p_rl)
895 switch (c)
896 {
897 case K_LEFT: c = K_RIGHT; break;
898 case K_S_LEFT: c = K_S_RIGHT; break;
899 case K_C_LEFT: c = K_C_RIGHT; break;
900 case K_RIGHT: c = K_LEFT; break;
901 case K_S_RIGHT: c = K_S_LEFT; break;
902 case K_C_RIGHT: c = K_C_LEFT; break;
903 }
904#endif
905
906#ifdef FEAT_VISUAL
907 /*
908 * If 'keymodel' contains "startsel", may start selection. If it
909 * does, a CTRL-O and c will be stuffed, we need to get these
910 * characters.
911 */
912 if (ins_start_select(c))
913 continue;
914#endif
915
916 /*
917 * The big switch to handle a character in insert mode.
918 */
919 switch (c)
920 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000921 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (echeck_abbr(ESC + ABBR_OFF))
923 break;
924 /*FALLTHROUGH*/
925
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000926 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000927#ifdef FEAT_CMDWIN
928 if (c == Ctrl_C && cmdwin_type != 0)
929 {
930 /* Close the cmdline window. */
931 cmdwin_result = K_IGNORE;
932 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000933 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000934 goto doESCkey;
935 }
936#endif
937
938#ifdef UNIX
939do_intr:
940#endif
941 /* when 'insertmode' set, and not halfway a mapping, don't leave
942 * Insert mode */
943 if (goto_im())
944 {
945 if (got_int)
946 {
947 (void)vgetc(); /* flush all buffers */
948 got_int = FALSE;
949 }
950 else
951 vim_beep();
952 break;
953 }
954doESCkey:
955 /*
956 * This is the ONLY return from edit()!
957 */
958 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
959 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000960 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 o_lnum = curwin->w_cursor.lnum;
962
Bram Moolenaar488c6512005-08-11 20:09:58 +0000963 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000964 {
965#ifdef FEAT_AUTOCMD
966 if (cmdchar != 'r' && cmdchar != 'v')
967 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
968 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000969 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000971 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000972 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000973 continue;
974
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000975 case Ctrl_Z: /* suspend when 'insertmode' set */
976 if (!p_im)
977 goto normalchar; /* insert CTRL-Z as normal char */
978 stuffReadbuff((char_u *)":st\r");
979 c = Ctrl_O;
980 /*FALLTHROUGH*/
981
982 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000983#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000984 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000985 goto docomplete;
986#endif
987 if (echeck_abbr(Ctrl_O + ABBR_OFF))
988 break;
989 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000990
991#ifdef FEAT_VIRTUALEDIT
992 /* don't move the cursor left when 'virtualedit' has "onemore". */
993 if (ve_flags & VE_ONEMORE)
994 {
995 ins_at_eol = FALSE;
996 nomove = TRUE;
997 }
998#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000999 count = 0;
1000 goto doESCkey;
1001
Bram Moolenaar572cb562005-08-05 21:35:02 +00001002 case K_INS: /* toggle insert/replace mode */
1003 case K_KINS:
1004 ins_insert(replaceState);
1005 break;
1006
1007 case K_SELECT: /* end of Select mode mapping - ignore */
1008 break;
1009
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001010#ifdef FEAT_SNIFF
1011 case K_SNIFF: /* Sniff command received */
1012 stuffcharReadbuff(K_SNIFF);
1013 goto doESCkey;
1014#endif
1015
1016 case K_HELP: /* Help key works like <ESC> <Help> */
1017 case K_F1:
1018 case K_XF1:
1019 stuffcharReadbuff(K_HELP);
1020 if (p_im)
1021 need_start_insertmode = TRUE;
1022 goto doESCkey;
1023
1024#ifdef FEAT_NETBEANS_INTG
1025 case K_F21: /* NetBeans command */
1026 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001027 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001028 --no_mapping;
1029 netbeans_keycommand(i);
1030 break;
1031#endif
1032
1033 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 case NUL:
1035 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001036 /* For ^@ the trailing ESC will end the insert, unless there is an
1037 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1039 && c != Ctrl_A && !p_im)
1040 goto doESCkey; /* quit insert mode */
1041 inserted_space = FALSE;
1042 break;
1043
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045 ins_reg();
1046 auto_format(FALSE, TRUE);
1047 inserted_space = FALSE;
1048 break;
1049
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001050 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051 ins_ctrl_g();
1052 break;
1053
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001054 case Ctrl_HAT: /* switch input mode and/or langmap */
1055 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 break;
1057
1058#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001059 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 if (!p_ari)
1061 goto normalchar;
1062 ins_ctrl_();
1063 break;
1064#endif
1065
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1068 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1069 goto docomplete;
1070#endif
1071 /* FALLTHROUGH */
1072
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001073 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074# ifdef FEAT_INS_EXPAND
1075 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1076 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001077 if (has_compl_option(FALSE))
1078 goto docomplete;
1079 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 }
1081# endif
1082 ins_shift(c, lastc);
1083 auto_format(FALSE, TRUE);
1084 inserted_space = FALSE;
1085 break;
1086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001087 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 case K_KDEL:
1089 ins_del();
1090 auto_format(FALSE, TRUE);
1091 break;
1092
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001093 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 case Ctrl_H:
1095 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1096 auto_format(FALSE, TRUE);
1097 break;
1098
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001099 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001100 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1101 auto_format(FALSE, TRUE);
1102 break;
1103
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001105# ifdef FEAT_COMPL_FUNC
1106 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001107 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001108 goto docomplete;
1109# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001110 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1111 auto_format(FALSE, TRUE);
1112 inserted_space = FALSE;
1113 break;
1114
1115#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001116 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 case K_LEFTMOUSE_NM:
1118 case K_LEFTDRAG:
1119 case K_LEFTRELEASE:
1120 case K_LEFTRELEASE_NM:
1121 case K_MIDDLEMOUSE:
1122 case K_MIDDLEDRAG:
1123 case K_MIDDLERELEASE:
1124 case K_RIGHTMOUSE:
1125 case K_RIGHTDRAG:
1126 case K_RIGHTRELEASE:
1127 case K_X1MOUSE:
1128 case K_X1DRAG:
1129 case K_X1RELEASE:
1130 case K_X2MOUSE:
1131 case K_X2DRAG:
1132 case K_X2RELEASE:
1133 ins_mouse(c);
1134 break;
1135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001136 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001137 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 break;
1139
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001140 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001141 ins_mousescroll(MSCR_UP);
1142 break;
1143
1144 case K_MOUSELEFT: /* Scroll wheel left */
1145 ins_mousescroll(MSCR_LEFT);
1146 break;
1147
1148 case K_MOUSERIGHT: /* Scroll wheel right */
1149 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 break;
1151#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001152#ifdef FEAT_GUI_TABLINE
1153 case K_TABLINE:
1154 case K_TABMENU:
1155 ins_tabline(c);
1156 break;
1157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001158
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001159 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 break;
1161
Bram Moolenaar754b5602006-02-09 23:53:20 +00001162#ifdef FEAT_AUTOCMD
1163 case K_CURSORHOLD: /* Didn't type something for a while. */
1164 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1165 did_cursorhold = TRUE;
1166 break;
1167#endif
1168
Bram Moolenaar4770d092006-01-12 23:22:24 +00001169#ifdef FEAT_GUI_W32
1170 /* On Win32 ignore <M-F4>, we get it when closing the window was
1171 * cancelled. */
1172 case K_F4:
1173 if (mod_mask != MOD_MASK_ALT)
1174 goto normalchar;
1175 break;
1176#endif
1177
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178#ifdef FEAT_GUI
1179 case K_VER_SCROLLBAR:
1180 ins_scroll();
1181 break;
1182
1183 case K_HOR_SCROLLBAR:
1184 ins_horscroll();
1185 break;
1186#endif
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001190 case K_S_HOME:
1191 case K_C_HOME:
1192 ins_home(c);
1193 break;
1194
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001195 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001196 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 case K_S_END:
1198 case K_C_END:
1199 ins_end(c);
1200 break;
1201
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001202 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001203 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1204 ins_s_left();
1205 else
1206 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001207 break;
1208
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001209 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001210 case K_C_LEFT:
1211 ins_s_left();
1212 break;
1213
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001214 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001215 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1216 ins_s_right();
1217 else
1218 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 break;
1220
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001221 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 case K_C_RIGHT:
1223 ins_s_right();
1224 break;
1225
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001226 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001227#ifdef FEAT_INS_EXPAND
1228 if (pum_visible())
1229 goto docomplete;
1230#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001231 if (mod_mask & MOD_MASK_SHIFT)
1232 ins_pageup();
1233 else
1234 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 break;
1236
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001237 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 case K_PAGEUP:
1239 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001240#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001241 if (pum_visible())
1242 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 ins_pageup();
1245 break;
1246
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001248#ifdef FEAT_INS_EXPAND
1249 if (pum_visible())
1250 goto docomplete;
1251#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001252 if (mod_mask & MOD_MASK_SHIFT)
1253 ins_pagedown();
1254 else
1255 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256 break;
1257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001258 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 case K_PAGEDOWN:
1260 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001261#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001262 if (pum_visible())
1263 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001264#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001265 ins_pagedown();
1266 break;
1267
1268#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001269 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001270 ins_drop();
1271 break;
1272#endif
1273
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001274 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001275 c = TAB;
1276 /* FALLTHROUGH */
1277
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001278 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1280 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1281 goto docomplete;
1282#endif
1283 inserted_space = FALSE;
1284 if (ins_tab())
1285 goto normalchar; /* insert TAB as a normal char */
1286 auto_format(FALSE, TRUE);
1287 break;
1288
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001290 c = CAR;
1291 /* FALLTHROUGH */
1292 case CAR:
1293 case NL:
1294#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1295 /* In a quickfix window a <CR> jumps to the error under the
1296 * cursor. */
1297 if (bt_quickfix(curbuf) && c == CAR)
1298 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001299 if (curwin->w_llist_ref == NULL) /* quickfix window */
1300 do_cmdline_cmd((char_u *)".cc");
1301 else /* location list window */
1302 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303 break;
1304 }
1305#endif
1306#ifdef FEAT_CMDWIN
1307 if (cmdwin_type != 0)
1308 {
1309 /* Execute the command in the cmdline window. */
1310 cmdwin_result = CAR;
1311 goto doESCkey;
1312 }
1313#endif
1314 if (ins_eol(c) && !p_im)
1315 goto doESCkey; /* out of memory */
1316 auto_format(FALSE, FALSE);
1317 inserted_space = FALSE;
1318 break;
1319
Bram Moolenaar860cae12010-06-05 23:22:07 +02001320#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001321 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322# ifdef FEAT_INS_EXPAND
1323 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1324 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001325 if (has_compl_option(TRUE))
1326 goto docomplete;
1327 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 }
1329# endif
1330# ifdef FEAT_DIGRAPHS
1331 c = ins_digraph();
1332 if (c == NUL)
1333 break;
1334# endif
1335 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337
1338#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001339 case Ctrl_X: /* Enter CTRL-X mode */
1340 ins_ctrl_x();
1341 break;
1342
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 if (ctrl_x_mode != CTRL_X_TAGS)
1345 goto normalchar;
1346 goto docomplete;
1347
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001348 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001349 if (ctrl_x_mode != CTRL_X_FILES)
1350 goto normalchar;
1351 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001352
1353 case 's': /* Spelling completion after ^X */
1354 case Ctrl_S:
1355 if (ctrl_x_mode != CTRL_X_SPELL)
1356 goto normalchar;
1357 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358#endif
1359
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001360 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361#ifdef FEAT_INS_EXPAND
1362 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1363#endif
1364 {
1365 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1366 if (p_im)
1367 {
1368 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1369 break;
1370 goto doESCkey;
1371 }
1372 goto normalchar;
1373 }
1374#ifdef FEAT_INS_EXPAND
1375 /* FALLTHROUGH */
1376
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001377 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 case Ctrl_N:
1379 /* if 'complete' is empty then plain ^P is no longer special,
1380 * but it is under other ^X modes */
1381 if (*curbuf->b_p_cpt == NUL
1382 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001383 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 goto normalchar;
1385
1386docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001387 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001389 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001390 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001391 break;
1392#endif /* FEAT_INS_EXPAND */
1393
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001394 case Ctrl_Y: /* copy from previous line or scroll down */
1395 case Ctrl_E: /* copy from next line or scroll up */
1396 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001397 break;
1398
1399 default:
1400#ifdef UNIX
1401 if (c == intr_char) /* special interrupt char */
1402 goto do_intr;
1403#endif
1404
Bram Moolenaare659c952011-05-19 17:25:41 +02001405normalchar:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 /*
1407 * Insert a nomal character.
1408 */
Bram Moolenaare659c952011-05-19 17:25:41 +02001409#ifdef FEAT_AUTOCMD
1410 if (!p_paste)
1411 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001412 /* Trigger InsertCharPre. */
1413 char_u *str = do_insert_char_pre(c);
1414 char_u *p;
1415
1416 if (str != NULL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001417 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001418 if (*str != NUL && stop_arrow() != FAIL)
Bram Moolenaare659c952011-05-19 17:25:41 +02001419 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001420 /* Insert the new value of v:char literally. */
1421 for (p = str; *p != NUL; mb_ptr_adv(p))
Bram Moolenaare659c952011-05-19 17:25:41 +02001422 {
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001423 c = PTR2CHAR(p);
1424 if (c == CAR || c == K_KENTER || c == NL)
1425 ins_eol(c);
1426 else
1427 ins_char(c);
Bram Moolenaare659c952011-05-19 17:25:41 +02001428 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001429 AppendToRedobuffLit(str, -1);
Bram Moolenaare659c952011-05-19 17:25:41 +02001430 }
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001431 vim_free(str);
1432 c = NUL;
Bram Moolenaare659c952011-05-19 17:25:41 +02001433 }
1434
Bram Moolenaarf5876f12012-02-29 18:22:08 +01001435 /* If the new value is already inserted or an empty string
1436 * then don't insert any character. */
Bram Moolenaare659c952011-05-19 17:25:41 +02001437 if (c == NUL)
1438 break;
1439 }
1440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001441#ifdef FEAT_SMARTINDENT
1442 /* Try to perform smart-indenting. */
1443 ins_try_si(c);
1444#endif
1445
1446 if (c == ' ')
1447 {
1448 inserted_space = TRUE;
1449#ifdef FEAT_CINDENT
1450 if (inindent(0))
1451 can_cindent = FALSE;
1452#endif
1453 if (Insstart_blank_vcol == MAXCOL
1454 && curwin->w_cursor.lnum == Insstart.lnum)
1455 Insstart_blank_vcol = get_nolist_virtcol();
1456 }
1457
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001458 /* Insert a normal character and check for abbreviations on a
1459 * special character. Let CTRL-] expand abbreviations without
1460 * inserting it. */
1461 if (vim_iswordc(c) || (!echeck_abbr(
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462#ifdef FEAT_MBYTE
1463 /* Add ABBR_OFF for characters above 0x100, this is
1464 * what check_abbr() expects. */
1465 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1466#endif
Bram Moolenaare0ebfd72012-04-05 16:07:06 +02001467 c) && c != Ctrl_RSB))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 {
1469 insert_special(c, FALSE, FALSE);
1470#ifdef FEAT_RIGHTLEFT
1471 revins_legal++;
1472 revins_chars++;
1473#endif
1474 }
1475
1476 auto_format(FALSE, TRUE);
1477
1478#ifdef FEAT_FOLDING
1479 /* When inserting a character the cursor line must never be in a
1480 * closed fold. */
1481 foldOpenCursor();
1482#endif
1483 break;
1484 } /* end of switch (c) */
1485
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001486#ifdef FEAT_AUTOCMD
1487 /* If typed something may trigger CursorHoldI again. */
1488 if (c != K_CURSORHOLD)
1489 did_cursorhold = FALSE;
1490#endif
1491
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 /* If the cursor was moved we didn't just insert a space */
1493 if (arrow_used)
1494 inserted_space = FALSE;
1495
1496#ifdef FEAT_CINDENT
1497 if (can_cindent && cindent_on()
1498# ifdef FEAT_INS_EXPAND
1499 && ctrl_x_mode == 0
1500# endif
1501 )
1502 {
1503force_cindent:
1504 /*
1505 * Indent now if a key was typed that is in 'cinkeys'.
1506 */
1507 if (in_cinkeys(c, ' ', line_is_white))
1508 {
1509 if (stop_arrow() == OK)
1510 /* re-indent the current line */
1511 do_c_expr_indent();
1512 }
1513 }
1514#endif /* FEAT_CINDENT */
1515
1516 } /* for (;;) */
1517 /* NOTREACHED */
1518}
1519
1520/*
1521 * Redraw for Insert mode.
1522 * This is postponed until getting the next character to make '$' in the 'cpo'
1523 * option work correctly.
1524 * Only redraw when there are no characters available. This speeds up
1525 * inserting sequences of characters (e.g., for CTRL-R).
1526 */
1527 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001528ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001529 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001530{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001531#ifdef FEAT_CONCEAL
1532 linenr_T conceal_old_cursor_line = 0;
1533 linenr_T conceal_new_cursor_line = 0;
1534 int conceal_update_lines = FALSE;
1535#endif
1536
Bram Moolenaar071d4272004-06-13 20:20:40 +00001537 if (!char_avail())
1538 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001539#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001540 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1541 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001542 if (ready && (
1543# ifdef FEAT_AUTOCMD
1544 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001545# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001546# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1547 ||
1548# endif
1549# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001550 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001551# endif
1552 )
1553 && !equalpos(last_cursormoved, curwin->w_cursor)
1554# ifdef FEAT_INS_EXPAND
1555 && !pum_visible()
1556# endif
1557 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001558 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001559# ifdef FEAT_SYN_HL
1560 /* Need to update the screen first, to make sure syntax
1561 * highlighting is correct after making a change (e.g., inserting
1562 * a "(". The autocommand may also require a redraw, so it's done
1563 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001564 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001565 update_screen(0);
1566# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001567# ifdef FEAT_AUTOCMD
1568 if (has_cursormovedI())
1569 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1570# endif
1571# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001572 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001573 {
1574 conceal_old_cursor_line = last_cursormoved.lnum;
1575 conceal_new_cursor_line = curwin->w_cursor.lnum;
1576 conceal_update_lines = TRUE;
1577 }
1578# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001579 last_cursormoved = curwin->w_cursor;
1580 }
1581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001582 if (must_redraw)
1583 update_screen(0);
1584 else if (clear_cmdline || redraw_cmdline)
1585 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001586# if defined(FEAT_CONCEAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001587 if ((conceal_update_lines
1588 && (conceal_old_cursor_line != conceal_new_cursor_line
1589 || conceal_cursor_line(curwin)))
1590 || need_cursor_line_redraw)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001591 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001592 if (conceal_old_cursor_line != conceal_new_cursor_line)
1593 update_single_line(curwin, conceal_old_cursor_line);
1594 update_single_line(curwin, conceal_new_cursor_line == 0
1595 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001596 curwin->w_valid &= ~VALID_CROW;
1597 }
1598# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001599 showruler(FALSE);
1600 setcursor();
1601 emsg_on_display = FALSE; /* may remove error message now */
1602 }
1603}
1604
1605/*
1606 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1607 */
1608 static void
1609ins_ctrl_v()
1610{
1611 int c;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001612 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001613
1614 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001615 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616
1617 if (redrawing() && !char_avail())
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001618 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619 edit_putchar('^', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001620 did_putchar = TRUE;
1621 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1623
1624#ifdef FEAT_CMDL_INFO
1625 add_to_showcmd_c(Ctrl_V);
1626#endif
1627
1628 c = get_literal();
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02001629 if (did_putchar)
1630 /* when the line fits in 'columns' the '^' is at the start of the next
1631 * line and will not removed by the redraw */
1632 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633#ifdef FEAT_CMDL_INFO
1634 clear_showcmd();
1635#endif
1636 insert_special(c, FALSE, TRUE);
1637#ifdef FEAT_RIGHTLEFT
1638 revins_chars++;
1639 revins_legal++;
1640#endif
1641}
1642
1643/*
1644 * Put a character directly onto the screen. It's not stored in a buffer.
1645 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1646 */
1647static int pc_status;
1648#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1649#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1650#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1651#define PC_STATUS_SET 3 /* pc_bytes was filled */
1652#ifdef FEAT_MBYTE
1653static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1654#else
1655static char_u pc_bytes[2]; /* saved bytes */
1656#endif
1657static int pc_attr;
1658static int pc_row;
1659static int pc_col;
1660
1661 void
1662edit_putchar(c, highlight)
1663 int c;
1664 int highlight;
1665{
1666 int attr;
1667
1668 if (ScreenLines != NULL)
1669 {
1670 update_topline(); /* just in case w_topline isn't valid */
1671 validate_cursor();
1672 if (highlight)
1673 attr = hl_attr(HLF_8);
1674 else
1675 attr = 0;
1676 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1677 pc_col = W_WINCOL(curwin);
1678#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1679 pc_status = PC_STATUS_UNSET;
1680#endif
1681#ifdef FEAT_RIGHTLEFT
1682 if (curwin->w_p_rl)
1683 {
1684 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1685# ifdef FEAT_MBYTE
1686 if (has_mbyte)
1687 {
1688 int fix_col = mb_fix_col(pc_col, pc_row);
1689
1690 if (fix_col != pc_col)
1691 {
1692 screen_putchar(' ', pc_row, fix_col, attr);
1693 --curwin->w_wcol;
1694 pc_status = PC_STATUS_RIGHT;
1695 }
1696 }
1697# endif
1698 }
1699 else
1700#endif
1701 {
1702 pc_col += curwin->w_wcol;
1703#ifdef FEAT_MBYTE
1704 if (mb_lefthalve(pc_row, pc_col))
1705 pc_status = PC_STATUS_LEFT;
1706#endif
1707 }
1708
1709 /* save the character to be able to put it back */
1710#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1711 if (pc_status == PC_STATUS_UNSET)
1712#endif
1713 {
1714 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1715 pc_status = PC_STATUS_SET;
1716 }
1717 screen_putchar(c, pc_row, pc_col, attr);
1718 }
1719}
1720
1721/*
1722 * Undo the previous edit_putchar().
1723 */
1724 void
1725edit_unputchar()
1726{
1727 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1728 {
1729#if defined(FEAT_MBYTE)
1730 if (pc_status == PC_STATUS_RIGHT)
1731 ++curwin->w_wcol;
1732 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1733 redrawWinline(curwin->w_cursor.lnum, FALSE);
1734 else
1735#endif
1736 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1737 }
1738}
1739
1740/*
1741 * Called when p_dollar is set: display a '$' at the end of the changed text
1742 * Only works when cursor is in the line that changes.
1743 */
1744 void
1745display_dollar(col)
1746 colnr_T col;
1747{
1748 colnr_T save_col;
1749
1750 if (!redrawing())
1751 return;
1752
1753 cursor_off();
1754 save_col = curwin->w_cursor.col;
1755 curwin->w_cursor.col = col;
1756#ifdef FEAT_MBYTE
1757 if (has_mbyte)
1758 {
1759 char_u *p;
1760
1761 /* If on the last byte of a multi-byte move to the first byte. */
1762 p = ml_get_curline();
1763 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1764 }
1765#endif
1766 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1767 if (curwin->w_wcol < W_WIDTH(curwin))
1768 {
1769 edit_putchar('$', FALSE);
1770 dollar_vcol = curwin->w_virtcol;
1771 }
1772 curwin->w_cursor.col = save_col;
1773}
1774
1775/*
1776 * Call this function before moving the cursor from the normal insert position
1777 * in insert mode.
1778 */
1779 static void
1780undisplay_dollar()
1781{
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001782 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 {
Bram Moolenaar76b9b362012-02-04 23:35:00 +01001784 dollar_vcol = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785 redrawWinline(curwin->w_cursor.lnum, FALSE);
1786 }
1787}
1788
1789/*
1790 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1791 * Keep the cursor on the same character.
1792 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1793 * type == INDENT_DEC decrease indent (for CTRL-D)
1794 * type == INDENT_SET set indent to "amount"
1795 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1796 */
1797 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001798change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 int type;
1800 int amount;
1801 int round;
1802 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001803 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001804{
1805 int vcol;
1806 int last_vcol;
1807 int insstart_less; /* reduction for Insstart.col */
1808 int new_cursor_col;
1809 int i;
1810 char_u *ptr;
1811 int save_p_list;
1812 int start_col;
1813 colnr_T vc;
1814#ifdef FEAT_VREPLACE
1815 colnr_T orig_col = 0; /* init for GCC */
1816 char_u *new_line, *orig_line = NULL; /* init for GCC */
1817
1818 /* VREPLACE mode needs to know what the line was like before changing */
1819 if (State & VREPLACE_FLAG)
1820 {
1821 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1822 orig_col = curwin->w_cursor.col;
1823 }
1824#endif
1825
1826 /* for the following tricks we don't want list mode */
1827 save_p_list = curwin->w_p_list;
1828 curwin->w_p_list = FALSE;
1829 vc = getvcol_nolist(&curwin->w_cursor);
1830 vcol = vc;
1831
1832 /*
1833 * For Replace mode we need to fix the replace stack later, which is only
1834 * possible when the cursor is in the indent. Remember the number of
1835 * characters before the cursor if it's possible.
1836 */
1837 start_col = curwin->w_cursor.col;
1838
1839 /* determine offset from first non-blank */
1840 new_cursor_col = curwin->w_cursor.col;
1841 beginline(BL_WHITE);
1842 new_cursor_col -= curwin->w_cursor.col;
1843
1844 insstart_less = curwin->w_cursor.col;
1845
1846 /*
1847 * If the cursor is in the indent, compute how many screen columns the
1848 * cursor is to the left of the first non-blank.
1849 */
1850 if (new_cursor_col < 0)
1851 vcol = get_indent() - vcol;
1852
1853 if (new_cursor_col > 0) /* can't fix replace stack */
1854 start_col = -1;
1855
1856 /*
1857 * Set the new indent. The cursor will be put on the first non-blank.
1858 */
1859 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001860 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 else
1862 {
1863#ifdef FEAT_VREPLACE
1864 int save_State = State;
1865
1866 /* Avoid being called recursively. */
1867 if (State & VREPLACE_FLAG)
1868 State = INSERT;
1869#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001870 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871#ifdef FEAT_VREPLACE
1872 State = save_State;
1873#endif
1874 }
1875 insstart_less -= curwin->w_cursor.col;
1876
1877 /*
1878 * Try to put cursor on same character.
1879 * If the cursor is at or after the first non-blank in the line,
1880 * compute the cursor column relative to the column of the first
1881 * non-blank character.
1882 * If we are not in insert mode, leave the cursor on the first non-blank.
1883 * If the cursor is before the first non-blank, position it relative
1884 * to the first non-blank, counted in screen columns.
1885 */
1886 if (new_cursor_col >= 0)
1887 {
1888 /*
1889 * When changing the indent while the cursor is touching it, reset
1890 * Insstart_col to 0.
1891 */
1892 if (new_cursor_col == 0)
1893 insstart_less = MAXCOL;
1894 new_cursor_col += curwin->w_cursor.col;
1895 }
1896 else if (!(State & INSERT))
1897 new_cursor_col = curwin->w_cursor.col;
1898 else
1899 {
1900 /*
1901 * Compute the screen column where the cursor should be.
1902 */
1903 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001904 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905
1906 /*
1907 * Advance the cursor until we reach the right screen column.
1908 */
1909 vcol = last_vcol = 0;
1910 new_cursor_col = -1;
1911 ptr = ml_get_curline();
1912 while (vcol <= (int)curwin->w_virtcol)
1913 {
1914 last_vcol = vcol;
1915#ifdef FEAT_MBYTE
1916 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001917 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 else
1919#endif
1920 ++new_cursor_col;
1921 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1922 }
1923 vcol = last_vcol;
1924
1925 /*
1926 * May need to insert spaces to be able to position the cursor on
1927 * the right screen column.
1928 */
1929 if (vcol != (int)curwin->w_virtcol)
1930 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001931 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001932 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001933 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 if (ptr != NULL)
1935 {
1936 new_cursor_col += i;
1937 ptr[i] = NUL;
1938 while (--i >= 0)
1939 ptr[i] = ' ';
1940 ins_str(ptr);
1941 vim_free(ptr);
1942 }
1943 }
1944
1945 /*
1946 * When changing the indent while the cursor is in it, reset
1947 * Insstart_col to 0.
1948 */
1949 insstart_less = MAXCOL;
1950 }
1951
1952 curwin->w_p_list = save_p_list;
1953
1954 if (new_cursor_col <= 0)
1955 curwin->w_cursor.col = 0;
1956 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001957 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 curwin->w_set_curswant = TRUE;
1959 changed_cline_bef_curs();
1960
1961 /*
1962 * May have to adjust the start of the insert.
1963 */
1964 if (State & INSERT)
1965 {
1966 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1967 {
1968 if ((int)Insstart.col <= insstart_less)
1969 Insstart.col = 0;
1970 else
1971 Insstart.col -= insstart_less;
1972 }
1973 if ((int)ai_col <= insstart_less)
1974 ai_col = 0;
1975 else
1976 ai_col -= insstart_less;
1977 }
1978
1979 /*
1980 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1981 * If the number of characters before the cursor decreased, need to pop a
1982 * few characters from the replace stack.
1983 * If the number of characters before the cursor increased, need to push a
1984 * few NULs onto the replace stack.
1985 */
1986 if (REPLACE_NORMAL(State) && start_col >= 0)
1987 {
1988 while (start_col > (int)curwin->w_cursor.col)
1989 {
1990 replace_join(0); /* remove a NUL from the replace stack */
1991 --start_col;
1992 }
1993 while (start_col < (int)curwin->w_cursor.col || replaced)
1994 {
1995 replace_push(NUL);
1996 if (replaced)
1997 {
1998 replace_push(replaced);
1999 replaced = NUL;
2000 }
2001 ++start_col;
2002 }
2003 }
2004
2005#ifdef FEAT_VREPLACE
2006 /*
2007 * For VREPLACE mode, we also have to fix the replace stack. In this case
2008 * it is always possible because we backspace over the whole line and then
2009 * put it back again the way we wanted it.
2010 */
2011 if (State & VREPLACE_FLAG)
2012 {
2013 /* If orig_line didn't allocate, just return. At least we did the job,
2014 * even if you can't backspace. */
2015 if (orig_line == NULL)
2016 return;
2017
2018 /* Save new line */
2019 new_line = vim_strsave(ml_get_curline());
2020 if (new_line == NULL)
2021 return;
2022
2023 /* We only put back the new line up to the cursor */
2024 new_line[curwin->w_cursor.col] = NUL;
2025
2026 /* Put back original line */
2027 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
2028 curwin->w_cursor.col = orig_col;
2029
2030 /* Backspace from cursor to start of line */
2031 backspace_until_column(0);
2032
2033 /* Insert new stuff into line again */
2034 ins_bytes(new_line);
2035
2036 vim_free(new_line);
2037 }
2038#endif
2039}
2040
2041/*
2042 * Truncate the space at the end of a line. This is to be used only in an
2043 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
2044 * modes.
2045 */
2046 void
2047truncate_spaces(line)
2048 char_u *line;
2049{
2050 int i;
2051
2052 /* find start of trailing white space */
2053 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
2054 {
2055 if (State & REPLACE_FLAG)
2056 replace_join(0); /* remove a NUL from the replace stack */
2057 }
2058 line[i + 1] = NUL;
2059}
2060
2061#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2062 || defined(FEAT_COMMENTS) || defined(PROTO)
2063/*
2064 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2065 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002066 * Will attempt not to go before "col" even when there is a composing
2067 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002068 */
2069 void
2070backspace_until_column(col)
2071 int col;
2072{
2073 while ((int)curwin->w_cursor.col > col)
2074 {
2075 curwin->w_cursor.col--;
2076 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002077 replace_do_bs(col);
2078 else if (!del_char_after_col(col))
2079 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 }
2081}
2082#endif
2083
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002084/*
2085 * Like del_char(), but make sure not to go before column "limit_col".
2086 * Only matters when there are composing characters.
2087 * Return TRUE when something was deleted.
2088 */
2089 static int
2090del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002091 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002092{
2093#ifdef FEAT_MBYTE
2094 if (enc_utf8 && limit_col >= 0)
2095 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002096 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002097
2098 /* Make sure the cursor is at the start of a character, but
2099 * skip forward again when going too far back because of a
2100 * composing character. */
2101 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002102 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002103 {
2104 int l = utf_ptr2len(ml_get_cursor());
2105
2106 if (l == 0) /* end of line */
2107 break;
2108 curwin->w_cursor.col += l;
2109 }
2110 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2111 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002112 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002113 }
2114 else
2115#endif
2116 (void)del_char(FALSE);
2117 return TRUE;
2118}
2119
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2121/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002122 * CTRL-X pressed in Insert mode.
2123 */
2124 static void
2125ins_ctrl_x()
2126{
2127 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2128 * CTRL-V works like CTRL-N */
2129 if (ctrl_x_mode != CTRL_X_CMDLINE)
2130 {
2131 /* if the next ^X<> won't ADD nothing, then reset
2132 * compl_cont_status */
2133 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002134 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002135 else
2136 compl_cont_status = 0;
2137 /* We're not sure which CTRL-X mode it will be yet */
2138 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2139 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2140 edit_submode_pre = NULL;
2141 showmode();
2142 }
2143}
2144
2145/*
2146 * Return TRUE if the 'dict' or 'tsr' option can be used.
2147 */
2148 static int
2149has_compl_option(dict_opt)
2150 int dict_opt;
2151{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002152 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002153# ifdef FEAT_SPELL
2154 && !curwin->w_p_spell
2155# endif
2156 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002157 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2158 {
2159 ctrl_x_mode = 0;
2160 edit_submode = NULL;
2161 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2162 : (char_u *)_("'thesaurus' option is empty"),
2163 hl_attr(HLF_E));
2164 if (emsg_silent == 0)
2165 {
2166 vim_beep();
2167 setcursor();
2168 out_flush();
2169 ui_delay(2000L, FALSE);
2170 }
2171 return FALSE;
2172 }
2173 return TRUE;
2174}
2175
2176/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2178 * This depends on the current mode.
2179 */
2180 int
2181vim_is_ctrl_x_key(c)
2182 int c;
2183{
2184 /* Always allow ^R - let it's results then be checked */
2185 if (c == Ctrl_R)
2186 return TRUE;
2187
Bram Moolenaare3226be2005-12-18 22:10:00 +00002188 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002189 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002190 return TRUE;
2191
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 switch (ctrl_x_mode)
2193 {
2194 case 0: /* Not in any CTRL-X mode */
2195 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2196 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002197 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2199 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2200 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002201 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
Bram Moolenaarbbd9fd72011-12-23 13:15:03 +01002202 || c == Ctrl_S || c == Ctrl_K || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 case CTRL_X_SCROLL:
2204 return (c == Ctrl_Y || c == Ctrl_E);
2205 case CTRL_X_WHOLE_LINE:
2206 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2207 case CTRL_X_FILES:
2208 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2209 case CTRL_X_DICTIONARY:
2210 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2211 case CTRL_X_THESAURUS:
2212 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2213 case CTRL_X_TAGS:
2214 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2215#ifdef FEAT_FIND_ID
2216 case CTRL_X_PATH_PATTERNS:
2217 return (c == Ctrl_P || c == Ctrl_N);
2218 case CTRL_X_PATH_DEFINES:
2219 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2220#endif
2221 case CTRL_X_CMDLINE:
2222 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2223 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002224#ifdef FEAT_COMPL_FUNC
2225 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002226 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002227 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002228 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002229#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002230 case CTRL_X_SPELL:
2231 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232 }
2233 EMSG(_(e_internal));
2234 return FALSE;
2235}
2236
2237/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002238 * Return TRUE when character "c" is part of the item currently being
2239 * completed. Used to decide whether to abandon complete mode when the menu
2240 * is visible.
2241 */
2242 static int
2243ins_compl_accept_char(c)
2244 int c;
2245{
2246 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2247 /* When expanding an identifier only accept identifier chars. */
2248 return vim_isIDc(c);
2249
2250 switch (ctrl_x_mode)
2251 {
2252 case CTRL_X_FILES:
2253 /* When expanding file name only accept file name chars. But not
2254 * path separators, so that "proto/<Tab>" expands files in
2255 * "proto", not "proto/" as a whole */
2256 return vim_isfilec(c) && !vim_ispathsep(c);
2257
2258 case CTRL_X_CMDLINE:
2259 case CTRL_X_OMNI:
2260 /* Command line and Omni completion can work with just about any
2261 * printable character, but do stop at white space. */
2262 return vim_isprintc(c) && !vim_iswhite(c);
2263
2264 case CTRL_X_WHOLE_LINE:
2265 /* For while line completion a space can be part of the line. */
2266 return vim_isprintc(c);
2267 }
2268 return vim_iswordc(c);
2269}
2270
2271/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002272 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002273 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002274 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002275 * the rest of the word to be in -- webb
2276 */
2277 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002278ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002279 char_u *str;
2280 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002281 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002282 char_u *fname;
2283 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002284 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002285{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002286 char_u *p;
2287 int i, c;
2288 int actual_len; /* Take multi-byte characters */
2289 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002290 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002291 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002292 int has_lower = FALSE;
2293 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002294
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002295 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002296 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002297 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002298
Bram Moolenaara2993e12007-08-12 14:38:46 +00002299 /* Find actual length of completion. */
2300#ifdef FEAT_MBYTE
2301 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002303 p = str;
2304 actual_len = 0;
2305 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002306 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002307 mb_ptr_adv(p);
2308 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002309 }
2310 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002311 else
2312#endif
2313 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaara2993e12007-08-12 14:38:46 +00002315 /* Find actual length of original text. */
2316#ifdef FEAT_MBYTE
2317 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002319 p = compl_orig_text;
2320 actual_compl_length = 0;
2321 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002323 mb_ptr_adv(p);
2324 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 }
2326 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002327 else
2328#endif
2329 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002331 /* "actual_len" may be smaller than "actual_compl_length" when using
2332 * thesaurus, only use the minimum when comparing. */
2333 min_len = actual_len < actual_compl_length
2334 ? actual_len : actual_compl_length;
2335
Bram Moolenaara2993e12007-08-12 14:38:46 +00002336 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002337 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002338 if (wca != NULL)
2339 {
2340 p = str;
2341 for (i = 0; i < actual_len; ++i)
2342#ifdef FEAT_MBYTE
2343 if (has_mbyte)
2344 wca[i] = mb_ptr2char_adv(&p);
2345 else
2346#endif
2347 wca[i] = *(p++);
2348
2349 /* Rule 1: Were any chars converted to lower? */
2350 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002351 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002352 {
2353#ifdef FEAT_MBYTE
2354 if (has_mbyte)
2355 c = mb_ptr2char_adv(&p);
2356 else
2357#endif
2358 c = *(p++);
2359 if (MB_ISLOWER(c))
2360 {
2361 has_lower = TRUE;
2362 if (MB_ISUPPER(wca[i]))
2363 {
2364 /* Rule 1 is satisfied. */
2365 for (i = actual_compl_length; i < actual_len; ++i)
2366 wca[i] = MB_TOLOWER(wca[i]);
2367 break;
2368 }
2369 }
2370 }
2371
2372 /*
2373 * Rule 2: No lower case, 2nd consecutive letter converted to
2374 * upper case.
2375 */
2376 if (!has_lower)
2377 {
2378 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002379 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002380 {
2381#ifdef FEAT_MBYTE
2382 if (has_mbyte)
2383 c = mb_ptr2char_adv(&p);
2384 else
2385#endif
2386 c = *(p++);
2387 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2388 {
2389 /* Rule 2 is satisfied. */
2390 for (i = actual_compl_length; i < actual_len; ++i)
2391 wca[i] = MB_TOUPPER(wca[i]);
2392 break;
2393 }
2394 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2395 }
2396 }
2397
2398 /* Copy the original case of the part we typed. */
2399 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002400 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002401 {
2402#ifdef FEAT_MBYTE
2403 if (has_mbyte)
2404 c = mb_ptr2char_adv(&p);
2405 else
2406#endif
2407 c = *(p++);
2408 if (MB_ISLOWER(c))
2409 wca[i] = MB_TOLOWER(wca[i]);
2410 else if (MB_ISUPPER(c))
2411 wca[i] = MB_TOUPPER(wca[i]);
2412 }
2413
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002414 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002415 * Generate encoding specific output from wide character array.
2416 * Multi-byte characters can occupy up to five bytes more than
2417 * ASCII characters, and we also need one byte for NUL, so stay
2418 * six bytes away from the edge of IObuff.
2419 */
2420 p = IObuff;
2421 i = 0;
2422 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2423#ifdef FEAT_MBYTE
2424 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002425 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002426 else
2427#endif
2428 *(p++) = wca[i++];
2429 *p = NUL;
2430
2431 vim_free(wca);
2432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002434 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2435 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002437 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002438}
2439
2440/*
2441 * Add a match to the list of matches.
2442 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002443 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002444 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002446 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002447ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002448 char_u *str;
2449 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002450 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002452 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002453 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002454 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002455 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002457 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002458 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002459
2460 ui_breakcheck();
2461 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002462 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002463 if (len < 0)
2464 len = (int)STRLEN(str);
2465
2466 /*
2467 * If the same match is already present, don't add it.
2468 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002469 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002471 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 do
2473 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002474 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002475 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002476 && match->cp_str[len] == NUL)
2477 return NOTDONE;
2478 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002479 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
2481
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002482 /* Remove any popup menu before changing the list of matches. */
2483 ins_compl_del_pum();
2484
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 /*
2486 * Allocate a new match structure.
2487 * Copy the values to the new match structure.
2488 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002489 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002490 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002491 return FAIL;
2492 match->cp_number = -1;
2493 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002494 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002495 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 {
2497 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002498 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002499 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002500 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002501
Bram Moolenaar071d4272004-06-13 20:20:40 +00002502 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002503 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002504 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2505 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002506 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002507 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002508 && compl_curr_match->cp_fname != NULL
2509 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002510 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002511 else if (fname != NULL)
2512 {
2513 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002514 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002516 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002517 match->cp_fname = NULL;
2518 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002519
2520 if (cptext != NULL)
2521 {
2522 int i;
2523
2524 for (i = 0; i < CPT_COUNT; ++i)
2525 if (cptext[i] != NULL && *cptext[i] != NUL)
2526 match->cp_text[i] = vim_strsave(cptext[i]);
2527 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528
2529 /*
2530 * Link the new match structure in the list of matches.
2531 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002532 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002533 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002534 else if (dir == FORWARD)
2535 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002536 match->cp_next = compl_curr_match->cp_next;
2537 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 }
2539 else /* BACKWARD */
2540 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002541 match->cp_next = compl_curr_match;
2542 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002544 if (match->cp_next)
2545 match->cp_next->cp_prev = match;
2546 if (match->cp_prev)
2547 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002549 compl_first_match = match;
2550 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002551
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002552 /*
2553 * Find the longest common string if still doing that.
2554 */
2555 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2556 ins_compl_longest_match(match);
2557
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 return OK;
2559}
2560
2561/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002562 * Return TRUE if "str[len]" matches with match->cp_str, considering
2563 * match->cp_icase.
2564 */
2565 static int
2566ins_compl_equal(match, str, len)
2567 compl_T *match;
2568 char_u *str;
2569 int len;
2570{
2571 if (match->cp_icase)
2572 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2573 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2574}
2575
2576/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002577 * Reduce the longest common string for match "match".
2578 */
2579 static void
2580ins_compl_longest_match(match)
2581 compl_T *match;
2582{
2583 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002584 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002585 int had_match;
2586
2587 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002588 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002589 /* First match, use it as a whole. */
2590 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002591 if (compl_leader != NULL)
2592 {
2593 had_match = (curwin->w_cursor.col > compl_col);
2594 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002595 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002596 ins_redraw(FALSE);
2597
2598 /* When the match isn't there (to avoid matching itself) remove it
2599 * again after redrawing. */
2600 if (!had_match)
2601 ins_compl_delete();
2602 compl_used_match = FALSE;
2603 }
2604 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002605 else
2606 {
2607 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002608 p = compl_leader;
2609 s = match->cp_str;
2610 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002611 {
2612#ifdef FEAT_MBYTE
2613 if (has_mbyte)
2614 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002615 c1 = mb_ptr2char(p);
2616 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002617 }
2618 else
2619#endif
2620 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002621 c1 = *p;
2622 c2 = *s;
2623 }
2624 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2625 : (c1 != c2))
2626 break;
2627#ifdef FEAT_MBYTE
2628 if (has_mbyte)
2629 {
2630 mb_ptr_adv(p);
2631 mb_ptr_adv(s);
2632 }
2633 else
2634#endif
2635 {
2636 ++p;
2637 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002638 }
2639 }
2640
2641 if (*p != NUL)
2642 {
2643 /* Leader was shortened, need to change the inserted text. */
2644 *p = NUL;
2645 had_match = (curwin->w_cursor.col > compl_col);
2646 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002647 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002648 ins_redraw(FALSE);
2649
2650 /* When the match isn't there (to avoid matching itself) remove it
2651 * again after redrawing. */
2652 if (!had_match)
2653 ins_compl_delete();
2654 }
2655
2656 compl_used_match = FALSE;
2657 }
2658}
2659
2660/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002661 * Add an array of matches to the list of matches.
2662 * Frees matches[].
2663 */
2664 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002665ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002666 int num_matches;
2667 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002668 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002669{
2670 int i;
2671 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002672 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002673
Bram Moolenaar572cb562005-08-05 21:35:02 +00002674 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002675 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002676 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002677 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002678 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002679 FreeWild(num_matches, matches);
2680}
2681
2682/* Make the completion list cyclic.
2683 * Return the number of matches (excluding the original).
2684 */
2685 static int
2686ins_compl_make_cyclic()
2687{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002688 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002689 int count = 0;
2690
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002691 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002692 {
2693 /*
2694 * Find the end of the list.
2695 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002696 match = compl_first_match;
2697 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002698 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002699 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002700 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002701 ++count;
2702 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002703 match->cp_next = compl_first_match;
2704 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002705 }
2706 return count;
2707}
2708
Bram Moolenaara94bc432006-03-10 21:42:59 +00002709/*
2710 * Start completion for the complete() function.
2711 * "startcol" is where the matched text starts (1 is first column).
2712 * "list" is the list of matches.
2713 */
2714 void
2715set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002716 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002717 list_T *list;
2718{
2719 /* If already doing completions stop it. */
2720 if (ctrl_x_mode != 0)
2721 ins_compl_prep(' ');
2722 ins_compl_clear();
2723
2724 if (stop_arrow() == FAIL)
2725 return;
2726
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002727 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002728 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002729 startcol = curwin->w_cursor.col;
2730 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002731 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002732 /* compl_pattern doesn't need to be set */
2733 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2734 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002735 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002736 return;
2737
2738 /* Handle like dictionary completion. */
2739 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2740
2741 ins_compl_add_list(list);
2742 compl_matches = ins_compl_make_cyclic();
2743 compl_started = TRUE;
2744 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002745 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002746
2747 compl_curr_match = compl_first_match;
2748 ins_complete(Ctrl_N);
2749 out_flush();
2750}
2751
2752
Bram Moolenaar9372a112005-12-06 19:59:18 +00002753/* "compl_match_array" points the currently displayed list of entries in the
2754 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002755static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002756static int compl_match_arraysize;
2757
2758/*
2759 * Update the screen and when there is any scrolling remove the popup menu.
2760 */
2761 static void
2762ins_compl_upd_pum()
2763{
2764 int h;
2765
2766 if (compl_match_array != NULL)
2767 {
2768 h = curwin->w_cline_height;
2769 update_screen(0);
2770 if (h != curwin->w_cline_height)
2771 ins_compl_del_pum();
2772 }
2773}
2774
2775/*
2776 * Remove any popup menu.
2777 */
2778 static void
2779ins_compl_del_pum()
2780{
2781 if (compl_match_array != NULL)
2782 {
2783 pum_undisplay();
2784 vim_free(compl_match_array);
2785 compl_match_array = NULL;
2786 }
2787}
2788
2789/*
2790 * Return TRUE if the popup menu should be displayed.
2791 */
2792 static int
2793pum_wanted()
2794{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002795 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002796 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002797 return FALSE;
2798
2799 /* The display looks bad on a B&W display. */
2800 if (t_colors < 8
2801#ifdef FEAT_GUI
2802 && !gui.in_use
2803#endif
2804 )
2805 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002806 return TRUE;
2807}
2808
2809/*
2810 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002811 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002812 */
2813 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002814pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002815{
2816 compl_T *compl;
2817 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002818
2819 /* Don't display the popup menu if there are no matches or there is only
2820 * one (ignoring the original text). */
2821 compl = compl_first_match;
2822 i = 0;
2823 do
2824 {
2825 if (compl == NULL
2826 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2827 break;
2828 compl = compl->cp_next;
2829 } while (compl != compl_first_match);
2830
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002831 if (strstr((char *)p_cot, "menuone") != NULL)
2832 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002833 return (i >= 2);
2834}
2835
2836/*
2837 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002838 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002839 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002840 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002841ins_compl_show_pum()
2842{
2843 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002844 compl_T *shown_compl = NULL;
2845 int did_find_shown_match = FALSE;
2846 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002847 int i;
2848 int cur = -1;
2849 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002850 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002851
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002852 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002853 return;
2854
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002855#if defined(FEAT_EVAL)
2856 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2857 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2858#endif
2859
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002860 /* Update the screen before drawing the popup menu over it. */
2861 update_screen(0);
2862
2863 if (compl_match_array == NULL)
2864 {
2865 /* Need to build the popup menu list. */
2866 compl_match_arraysize = 0;
2867 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002868 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002869 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002870 do
2871 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002872 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2873 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002874 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002875 ++compl_match_arraysize;
2876 compl = compl->cp_next;
2877 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002878 if (compl_match_arraysize == 0)
2879 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002880 compl_match_array = (pumitem_T *)alloc_clear(
2881 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002882 * compl_match_arraysize));
2883 if (compl_match_array != NULL)
2884 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002885 /* If the current match is the original text don't find the first
2886 * match after it, don't highlight anything. */
2887 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2888 shown_match_ok = TRUE;
2889
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002890 i = 0;
2891 compl = compl_first_match;
2892 do
2893 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002894 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2895 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002896 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002897 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002898 if (!shown_match_ok)
2899 {
2900 if (compl == compl_shown_match || did_find_shown_match)
2901 {
2902 /* This item is the shown match or this is the
2903 * first displayed item after the shown match. */
2904 compl_shown_match = compl;
2905 did_find_shown_match = TRUE;
2906 shown_match_ok = TRUE;
2907 }
2908 else
2909 /* Remember this displayed match for when the
2910 * shown match is just below it. */
2911 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002912 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002913 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002914
2915 if (compl->cp_text[CPT_ABBR] != NULL)
2916 compl_match_array[i].pum_text =
2917 compl->cp_text[CPT_ABBR];
2918 else
2919 compl_match_array[i].pum_text = compl->cp_str;
2920 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2921 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2922 if (compl->cp_text[CPT_MENU] != NULL)
2923 compl_match_array[i++].pum_extra =
2924 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002925 else
2926 compl_match_array[i++].pum_extra = compl->cp_fname;
2927 }
2928
2929 if (compl == compl_shown_match)
2930 {
2931 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002932
2933 /* When the original text is the shown match don't set
2934 * compl_shown_match. */
2935 if (compl->cp_flags & ORIGINAL_TEXT)
2936 shown_match_ok = TRUE;
2937
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002938 if (!shown_match_ok && shown_compl != NULL)
2939 {
2940 /* The shown match isn't displayed, set it to the
2941 * previously displayed match. */
2942 compl_shown_match = shown_compl;
2943 shown_match_ok = TRUE;
2944 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002945 }
2946 compl = compl->cp_next;
2947 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002948
2949 if (!shown_match_ok) /* no displayed match at all */
2950 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002951 }
2952 }
2953 else
2954 {
2955 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002956 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002957 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2958 || compl_match_array[i].pum_text
2959 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002960 {
2961 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002962 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002963 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002964 }
2965
2966 if (compl_match_array != NULL)
2967 {
2968 /* Compute the screen column of the start of the completed text.
2969 * Use the cursor to get all wrapping and other settings right. */
2970 col = curwin->w_cursor.col;
2971 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002972 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002973 curwin->w_cursor.col = col;
2974 }
2975}
2976
Bram Moolenaar071d4272004-06-13 20:20:40 +00002977#define DICT_FIRST (1) /* use just first element in "dict" */
2978#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002979
Bram Moolenaar071d4272004-06-13 20:20:40 +00002980/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002981 * Add any identifiers that match the given pattern in the list of dictionary
2982 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 */
2984 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002985ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2986 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002988 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002989 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002991 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 char_u *ptr;
2993 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002994 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002995 char_u **files;
2996 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002997 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002998 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002999
Bram Moolenaar0b238792006-03-02 22:49:12 +00003000 if (*dict == NUL)
3001 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003002#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003003 /* When 'dictionary' is empty and spell checking is enabled use
3004 * "spell". */
3005 if (!thesaurus && curwin->w_p_spell)
3006 dict = (char_u *)"spell";
3007 else
3008#endif
3009 return;
3010 }
3011
Bram Moolenaar071d4272004-06-13 20:20:40 +00003012 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003013 if (buf == NULL)
3014 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003015 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003016
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 /* If 'infercase' is set, don't use 'smartcase' here */
3018 save_p_scs = p_scs;
3019 if (curbuf->b_p_inf)
3020 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003021
3022 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
3023 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003024 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003025 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3026 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003027 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003028 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003029
3030 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00003031 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003032 len = STRLEN(pat_esc) + 10;
3033 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003034 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003035 {
3036 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00003037 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003038 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003039 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00003040 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
3041 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003042 vim_free(ptr);
3043 }
3044 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00003045 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003046 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003047 if (regmatch.regprog == NULL)
3048 goto theend;
3049 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003050
Bram Moolenaar071d4272004-06-13 20:20:40 +00003051 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
3052 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003053 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003054 {
3055 /* copy one dictionary file name into buf */
3056 if (flags == DICT_EXACT)
3057 {
3058 count = 1;
3059 files = &dict;
3060 }
3061 else
3062 {
3063 /* Expand wildcards in the dictionary name, but do not allow
3064 * backticks (for security, the 'dict' option may have been set in
3065 * a modeline). */
3066 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003067# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003068 if (!thesaurus && STRCMP(buf, "spell") == 0)
3069 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003070 else
3071# endif
3072 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003073 || expand_wildcards(1, &buf, &count, &files,
3074 EW_FILE|EW_SILENT) != OK)
3075 count = 0;
3076 }
3077
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003078# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003079 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003080 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003081 /* Complete from active spelling. Skip "\<" in the pattern, we
3082 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003083 if (pat[0] == '\\' && pat[1] == '<')
3084 ptr = pat + 2;
3085 else
3086 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003087 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003088 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003089 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003090# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003091 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003092 {
3093 ins_compl_files(count, files, thesaurus, flags,
3094 &regmatch, buf, &dir);
3095 if (flags != DICT_EXACT)
3096 FreeWild(count, files);
3097 }
3098 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003099 break;
3100 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003101
3102theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003103 p_scs = save_p_scs;
3104 vim_free(regmatch.regprog);
3105 vim_free(buf);
3106}
3107
Bram Moolenaar0b238792006-03-02 22:49:12 +00003108 static void
3109ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3110 int count;
3111 char_u **files;
3112 int thesaurus;
3113 int flags;
3114 regmatch_T *regmatch;
3115 char_u *buf;
3116 int *dir;
3117{
3118 char_u *ptr;
3119 int i;
3120 FILE *fp;
3121 int add_r;
3122
3123 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3124 {
3125 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3126 if (flags != DICT_EXACT)
3127 {
3128 vim_snprintf((char *)IObuff, IOSIZE,
3129 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003130 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003131 }
3132
3133 if (fp != NULL)
3134 {
3135 /*
3136 * Read dictionary file line by line.
3137 * Check each line for a match.
3138 */
3139 while (!got_int && !compl_interrupted
3140 && !vim_fgets(buf, LSIZE, fp))
3141 {
3142 ptr = buf;
3143 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3144 {
3145 ptr = regmatch->startp[0];
3146 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3147 ptr = find_line_end(ptr);
3148 else
3149 ptr = find_word_end(ptr);
3150 add_r = ins_compl_add_infercase(regmatch->startp[0],
3151 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003152 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003153 if (thesaurus)
3154 {
3155 char_u *wstart;
3156
3157 /*
3158 * Add the other matches on the line
3159 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003160 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003161 while (!got_int)
3162 {
3163 /* Find start of the next word. Skip white
3164 * space and punctuation. */
3165 ptr = find_word_start(ptr);
3166 if (*ptr == NUL || *ptr == NL)
3167 break;
3168 wstart = ptr;
3169
Bram Moolenaara2993e12007-08-12 14:38:46 +00003170 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003171#ifdef FEAT_MBYTE
3172 if (has_mbyte)
3173 /* Japanese words may have characters in
3174 * different classes, only separate words
3175 * with single-byte non-word characters. */
3176 while (*ptr != NUL)
3177 {
3178 int l = (*mb_ptr2len)(ptr);
3179
3180 if (l < 2 && !vim_iswordc(*ptr))
3181 break;
3182 ptr += l;
3183 }
3184 else
3185#endif
3186 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003187
3188 /* Add the word. Skip the regexp match. */
3189 if (wstart != regmatch->startp[0])
3190 add_r = ins_compl_add_infercase(wstart,
3191 (int)(ptr - wstart),
3192 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003193 }
3194 }
3195 if (add_r == OK)
3196 /* if dir was BACKWARD then honor it just once */
3197 *dir = FORWARD;
3198 else if (add_r == FAIL)
3199 break;
3200 /* avoid expensive call to vim_regexec() when at end
3201 * of line */
3202 if (*ptr == '\n' || got_int)
3203 break;
3204 }
3205 line_breakcheck();
3206 ins_compl_check_keys(50);
3207 }
3208 fclose(fp);
3209 }
3210 }
3211}
3212
Bram Moolenaar071d4272004-06-13 20:20:40 +00003213/*
3214 * Find the start of the next word.
3215 * Returns a pointer to the first char of the word. Also stops at a NUL.
3216 */
3217 char_u *
3218find_word_start(ptr)
3219 char_u *ptr;
3220{
3221#ifdef FEAT_MBYTE
3222 if (has_mbyte)
3223 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003224 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003225 else
3226#endif
3227 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3228 ++ptr;
3229 return ptr;
3230}
3231
3232/*
3233 * Find the end of the word. Assumes it starts inside a word.
3234 * Returns a pointer to just after the word.
3235 */
3236 char_u *
3237find_word_end(ptr)
3238 char_u *ptr;
3239{
3240#ifdef FEAT_MBYTE
3241 int start_class;
3242
3243 if (has_mbyte)
3244 {
3245 start_class = mb_get_class(ptr);
3246 if (start_class > 1)
3247 while (*ptr != NUL)
3248 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003249 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250 if (mb_get_class(ptr) != start_class)
3251 break;
3252 }
3253 }
3254 else
3255#endif
3256 while (vim_iswordc(*ptr))
3257 ++ptr;
3258 return ptr;
3259}
3260
3261/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003262 * Find the end of the line, omitting CR and NL at the end.
3263 * Returns a pointer to just after the line.
3264 */
3265 static char_u *
3266find_line_end(ptr)
3267 char_u *ptr;
3268{
3269 char_u *s;
3270
3271 s = ptr + STRLEN(ptr);
3272 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3273 --s;
3274 return s;
3275}
3276
3277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003278 * Free the list of completions
3279 */
3280 static void
3281ins_compl_free()
3282{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003283 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003284 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003285
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003286 vim_free(compl_pattern);
3287 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003288 vim_free(compl_leader);
3289 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003291 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003292 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003293
3294 ins_compl_del_pum();
3295 pum_clear();
3296
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003297 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003298 do
3299 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003300 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003301 compl_curr_match = compl_curr_match->cp_next;
3302 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003303 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003304 if (match->cp_flags & FREE_FNAME)
3305 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003306 for (i = 0; i < CPT_COUNT; ++i)
3307 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003308 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003309 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3310 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003311 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003312}
3313
3314 static void
3315ins_compl_clear()
3316{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003317 compl_cont_status = 0;
3318 compl_started = FALSE;
3319 compl_matches = 0;
3320 vim_free(compl_pattern);
3321 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003322 vim_free(compl_leader);
3323 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003324 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003325 vim_free(compl_orig_text);
3326 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003327 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003328}
3329
3330/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003331 * Return TRUE when Insert completion is active.
3332 */
3333 int
3334ins_compl_active()
3335{
3336 return compl_started;
3337}
3338
3339/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003340 * Delete one character before the cursor and show the subset of the matches
3341 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003342 * Returns the character to be used, NUL if the work is done and another char
3343 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003344 */
3345 static int
3346ins_compl_bs()
3347{
3348 char_u *line;
3349 char_u *p;
3350
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003351 line = ml_get_curline();
3352 p = line + curwin->w_cursor.col;
3353 mb_ptr_back(line, p);
3354
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003355 /* Stop completion when the whole word was deleted. For Omni completion
3356 * allow the word to be deleted, we won't match everything. */
3357 if ((int)(p - line) - (int)compl_col < 0
3358 || ((int)(p - line) - (int)compl_col == 0
3359 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003360 return K_BS;
3361
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003362 /* Deleted more than what was used to find matches or didn't finish
3363 * finding all matches: need to look for matches all over again. */
3364 if (curwin->w_cursor.col <= compl_col + compl_length
Bram Moolenaar82139082011-09-14 16:52:09 +02003365 || ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003366 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003367
Bram Moolenaara6557602006-02-04 22:43:20 +00003368 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003369 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003370 if (compl_leader != NULL)
3371 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003372 ins_compl_new_leader();
3373 return NUL;
3374 }
3375 return K_BS;
3376}
Bram Moolenaara6557602006-02-04 22:43:20 +00003377
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003378/*
Bram Moolenaar82139082011-09-14 16:52:09 +02003379 * Return TRUE when we need to find matches again, ins_compl_restart() is to
3380 * be called.
3381 */
3382 static int
3383ins_compl_need_restart()
3384{
3385 /* Return TRUE if we didn't complete finding matches or when the
3386 * 'completefunc' returned "always" in the "refresh" dictionary item. */
3387 return compl_was_interrupted
3388 || ((ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
3389 && compl_opt_refresh_always);
3390}
3391
3392/*
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003393 * Called after changing "compl_leader".
3394 * Show the popup menu with a different set of matches.
3395 * May also search for matches again if the previous search was interrupted.
3396 */
3397 static void
3398ins_compl_new_leader()
3399{
3400 ins_compl_del_pum();
3401 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003402 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003403 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003404
3405 if (compl_started)
3406 ins_compl_set_original_text(compl_leader);
3407 else
3408 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003409#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003410 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003411#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003412 /*
3413 * Matches were cleared, need to search for them now. First display
3414 * the changed text before the cursor. Set "compl_restarting" to
3415 * avoid that the first match is inserted.
3416 */
3417 update_screen(0);
3418#ifdef FEAT_GUI
3419 if (gui.in_use)
3420 {
3421 /* Show the cursor after the match, not after the redrawn text. */
3422 setcursor();
3423 out_flush();
3424 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003425 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003426#endif
3427 compl_restarting = TRUE;
3428 if (ins_complete(Ctrl_N) == FAIL)
3429 compl_cont_status = 0;
3430 compl_restarting = FALSE;
3431 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003432
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003433 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003434
3435 /* Show the popup menu with a different set of matches. */
3436 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003437
3438 /* Don't let Enter select the original text when there is no popup menu. */
3439 if (compl_match_array == NULL)
3440 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003441}
3442
3443/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003444 * Return the length of the completion, from the completion start column to
3445 * the cursor column. Making sure it never goes below zero.
3446 */
3447 static int
3448ins_compl_len()
3449{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003450 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003451
3452 if (off < 0)
3453 return 0;
3454 return off;
3455}
3456
3457/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003458 * Append one character to the match leader. May reduce the number of
3459 * matches.
3460 */
3461 static void
3462ins_compl_addleader(c)
3463 int c;
3464{
3465#ifdef FEAT_MBYTE
3466 int cc;
3467
3468 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3469 {
3470 char_u buf[MB_MAXBYTES + 1];
3471
3472 (*mb_char2bytes)(c, buf);
3473 buf[cc] = NUL;
3474 ins_char_bytes(buf, cc);
3475 }
3476 else
3477#endif
3478 ins_char(c);
3479
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003480 /* If we didn't complete finding matches we must search again. */
Bram Moolenaar82139082011-09-14 16:52:09 +02003481 if (ins_compl_need_restart())
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003482 ins_compl_restart();
3483
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003484 /* When 'always' is set, don't reset compl_leader. While completing,
3485 * cursor don't point original position, changing compl_leader would
3486 * break redo. */
3487 if (!compl_opt_refresh_always)
3488 {
3489 vim_free(compl_leader);
3490 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003491 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01003492 if (compl_leader != NULL)
3493 ins_compl_new_leader();
3494 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003495}
3496
3497/*
3498 * Setup for finding completions again without leaving CTRL-X mode. Used when
3499 * BS or a key was typed while still searching for matches.
3500 */
3501 static void
3502ins_compl_restart()
3503{
3504 ins_compl_free();
3505 compl_started = FALSE;
3506 compl_matches = 0;
3507 compl_cont_status = 0;
3508 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003509}
3510
3511/*
3512 * Set the first match, the original text.
3513 */
3514 static void
3515ins_compl_set_original_text(str)
3516 char_u *str;
3517{
3518 char_u *p;
3519
3520 /* Replace the original text entry. */
3521 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3522 {
3523 p = vim_strsave(str);
3524 if (p != NULL)
3525 {
3526 vim_free(compl_first_match->cp_str);
3527 compl_first_match->cp_str = p;
3528 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003529 }
3530}
3531
3532/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003533 * Append one character to the match leader. May reduce the number of
3534 * matches.
3535 */
3536 static void
3537ins_compl_addfrommatch()
3538{
3539 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003540 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003541 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003542 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003543
3544 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003545 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003546 {
3547 /* When still at the original match use the first entry that matches
3548 * the leader. */
3549 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3550 {
3551 p = NULL;
3552 for (cp = compl_shown_match->cp_next; cp != NULL
3553 && cp != compl_first_match; cp = cp->cp_next)
3554 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003555 if (compl_leader == NULL
3556 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003557 (int)STRLEN(compl_leader)))
3558 {
3559 p = cp->cp_str;
3560 break;
3561 }
3562 }
3563 if (p == NULL || (int)STRLEN(p) <= len)
3564 return;
3565 }
3566 else
3567 return;
3568 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003569 p += len;
Bram Moolenaare659c952011-05-19 17:25:41 +02003570 c = PTR2CHAR(p);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003571 ins_compl_addleader(c);
3572}
3573
3574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003576 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003577 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003579 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003580ins_compl_prep(c)
3581 int c;
3582{
3583 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003585 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586
3587 /* Forget any previous 'special' messages if this is actually
3588 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3589 */
3590 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3591 edit_submode_extra = NULL;
3592
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003593 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003594 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3595 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003596 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003598 /* Set "compl_get_longest" when finding the first matches. */
3599 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3600 || (ctrl_x_mode == 0 && !compl_started))
3601 {
3602 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3603 compl_used_match = TRUE;
3604 }
3605
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3607 {
3608 /*
3609 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3610 * it will be yet. Now we decide.
3611 */
3612 switch (c)
3613 {
3614 case Ctrl_E:
3615 case Ctrl_Y:
3616 ctrl_x_mode = CTRL_X_SCROLL;
3617 if (!(State & REPLACE_FLAG))
3618 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3619 else
3620 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3621 edit_submode_pre = NULL;
3622 showmode();
3623 break;
3624 case Ctrl_L:
3625 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3626 break;
3627 case Ctrl_F:
3628 ctrl_x_mode = CTRL_X_FILES;
3629 break;
3630 case Ctrl_K:
3631 ctrl_x_mode = CTRL_X_DICTIONARY;
3632 break;
3633 case Ctrl_R:
3634 /* Simply allow ^R to happen without affecting ^X mode */
3635 break;
3636 case Ctrl_T:
3637 ctrl_x_mode = CTRL_X_THESAURUS;
3638 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003639#ifdef FEAT_COMPL_FUNC
3640 case Ctrl_U:
3641 ctrl_x_mode = CTRL_X_FUNCTION;
3642 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003643 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003644 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003645 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003646#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003647 case 's':
3648 case Ctrl_S:
3649 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003650#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003651 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003652 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003653 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003654#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003655 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 case Ctrl_RSB:
3657 ctrl_x_mode = CTRL_X_TAGS;
3658 break;
3659#ifdef FEAT_FIND_ID
3660 case Ctrl_I:
3661 case K_S_TAB:
3662 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3663 break;
3664 case Ctrl_D:
3665 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3666 break;
3667#endif
3668 case Ctrl_V:
3669 case Ctrl_Q:
3670 ctrl_x_mode = CTRL_X_CMDLINE;
3671 break;
3672 case Ctrl_P:
3673 case Ctrl_N:
3674 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3675 * just started ^X mode, or there were enough ^X's to cancel
3676 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3677 * do normal expansion when interrupting a different mode (say
3678 * ^X^F^X^P or ^P^X^X^P, see below)
3679 * nothing changes if interrupting mode 0, (eg, the flag
3680 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003681 if (!(compl_cont_status & CONT_INTRPT))
3682 compl_cont_status |= CONT_LOCAL;
3683 else if (compl_cont_mode != 0)
3684 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 /* FALLTHROUGH */
3686 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003687 /* If we have typed at least 2 ^X's... for modes != 0, we set
3688 * compl_cont_status = 0 (eg, as if we had just started ^X
3689 * mode).
3690 * For mode 0, we set "compl_cont_mode" to an impossible
3691 * value, in both cases ^X^X can be used to restart the same
3692 * mode (avoiding ADDING mode).
3693 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3694 * 'complete' and local ^P expansions respectively.
3695 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3696 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003697 if (c == Ctrl_X)
3698 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003699 if (compl_cont_mode != 0)
3700 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003702 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 }
3704 ctrl_x_mode = 0;
3705 edit_submode = NULL;
3706 showmode();
3707 break;
3708 }
3709 }
3710 else if (ctrl_x_mode != 0)
3711 {
3712 /* We're already in CTRL-X mode, do we stay in it? */
3713 if (!vim_is_ctrl_x_key(c))
3714 {
3715 if (ctrl_x_mode == CTRL_X_SCROLL)
3716 ctrl_x_mode = 0;
3717 else
3718 ctrl_x_mode = CTRL_X_FINISHED;
3719 edit_submode = NULL;
3720 }
3721 showmode();
3722 }
3723
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003724 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 {
3726 /* Show error message from attempted keyword completion (probably
3727 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003728 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003730 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3731 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 || ctrl_x_mode == CTRL_X_FINISHED)
3733 {
3734 /* Get here when we have finished typing a sequence of ^N and
3735 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003736 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003737 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
3739 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003740 * If any of the original typed text has been changed, eg when
3741 * ignorecase is set, we must add back-spaces to the redo
3742 * buffer. We add as few as necessary to delete just the part
3743 * of the original text that has changed.
3744 * When using the longest match, edited the match or used
3745 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003747 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3748 ptr = compl_curr_match->cp_str;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003749 else
Bram Moolenaar62951b12011-09-21 18:23:05 +02003750 ptr = NULL;
3751 ins_compl_fixRedoBufForLeader(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 }
3753
3754#ifdef FEAT_CINDENT
3755 want_cindent = (can_cindent && cindent_on());
3756#endif
3757 /*
3758 * When completing whole lines: fix indent for 'cindent'.
3759 * Otherwise, break line if it's too long.
3760 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003761 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
3763#ifdef FEAT_CINDENT
3764 /* re-indent the current line */
3765 if (want_cindent)
3766 {
3767 do_c_expr_indent();
3768 want_cindent = FALSE; /* don't do it again */
3769 }
3770#endif
3771 }
3772 else
3773 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003774 int prev_col = curwin->w_cursor.col;
3775
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003777 if (prev_col > 0)
3778 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 if (stop_arrow() == OK)
3780 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003781 if (prev_col > 0
3782 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3783 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003784 }
3785
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003786 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003787 * the selection without inserting anything. When
3788 * compl_enter_selects is set the Enter key does the same. */
3789 if ((c == Ctrl_Y || (compl_enter_selects
3790 && (c == CAR || c == K_KENTER || c == NL)))
3791 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003792 retval = TRUE;
3793
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003794 /* CTRL-E means completion is Ended, go back to the typed text. */
3795 if (c == Ctrl_E)
3796 {
3797 ins_compl_delete();
3798 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003799 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003800 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003801 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003802 retval = TRUE;
3803 }
3804
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003805 auto_format(FALSE, TRUE);
3806
Bram Moolenaar071d4272004-06-13 20:20:40 +00003807 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003808 compl_started = FALSE;
3809 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003810 msg_clr_cmdline(); /* necessary for "noshowmode" */
3811 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003812 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 if (edit_submode != NULL)
3814 {
3815 edit_submode = NULL;
3816 showmode();
3817 }
3818
3819#ifdef FEAT_CINDENT
3820 /*
3821 * Indent now if a key was typed that is in 'cinkeys'.
3822 */
3823 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3824 do_c_expr_indent();
3825#endif
3826 }
3827 }
3828
3829 /* reset continue_* if we left expansion-mode, if we stay they'll be
3830 * (re)set properly in ins_complete() */
3831 if (!vim_is_ctrl_x_key(c))
3832 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003833 compl_cont_status = 0;
3834 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003835 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003836
3837 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003838}
3839
3840/*
Bram Moolenaar62951b12011-09-21 18:23:05 +02003841 * Fix the redo buffer for the completion leader replacing some of the typed
3842 * text. This inserts backspaces and appends the changed text.
3843 * "ptr" is the known leader text or NUL.
3844 */
3845 static void
3846ins_compl_fixRedoBufForLeader(ptr_arg)
3847 char_u *ptr_arg;
3848{
3849 int len;
3850 char_u *p;
3851 char_u *ptr = ptr_arg;
3852
3853 if (ptr == NULL)
3854 {
3855 if (compl_leader != NULL)
3856 ptr = compl_leader;
3857 else
3858 return; /* nothing to do */
3859 }
3860 if (compl_orig_text != NULL)
3861 {
3862 p = compl_orig_text;
3863 for (len = 0; p[len] != NUL && p[len] == ptr[len]; ++len)
3864 ;
3865#ifdef FEAT_MBYTE
3866 if (len > 0)
3867 len -= (*mb_head_off)(p, p + len);
3868#endif
3869 for (p += len; *p != NUL; mb_ptr_adv(p))
3870 AppendCharToRedobuff(K_BS);
3871 }
3872 else
3873 len = 0;
3874 if (ptr != NULL)
3875 AppendToRedobuffLit(ptr + len, -1);
3876}
3877
3878/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003879 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3880 * (depending on flag) starting from buf and looking for a non-scanned
3881 * buffer (other than curbuf). curbuf is special, if it is called with
3882 * buf=curbuf then it has to be the first call for a given flag/expansion.
3883 *
3884 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3885 */
3886 static buf_T *
3887ins_compl_next_buf(buf, flag)
3888 buf_T *buf;
3889 int flag;
3890{
3891#ifdef FEAT_WINDOWS
3892 static win_T *wp;
3893#endif
3894
3895 if (flag == 'w') /* just windows */
3896 {
3897#ifdef FEAT_WINDOWS
3898 if (buf == curbuf) /* first call for this flag/expansion */
3899 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003900 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 && wp->w_buffer->b_scanned)
3902 ;
3903 buf = wp->w_buffer;
3904#else
3905 buf = curbuf;
3906#endif
3907 }
3908 else
3909 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3910 * (unlisted buffers)
3911 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003912 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 && ((flag == 'U'
3914 ? buf->b_p_bl
3915 : (!buf->b_p_bl
3916 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003917 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 ;
3919 return buf;
3920}
3921
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003922#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003923static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003924
3925/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003926 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003927 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003928 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003929 static void
3930expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003931 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003932 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003933{
Bram Moolenaar82139082011-09-14 16:52:09 +02003934 list_T *matchlist = NULL;
3935 dict_T *matchdict = NULL;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003936 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003937 char_u *funcname;
3938 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003939 win_T *curwin_save;
3940 buf_T *curbuf_save;
Bram Moolenaar82139082011-09-14 16:52:09 +02003941 typval_T rettv;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003942
Bram Moolenaare344bea2005-09-01 20:46:49 +00003943 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3944 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003945 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003946
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003947 /* Call 'completefunc' to obtain the list of matches. */
3948 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003949 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003950
Bram Moolenaare344bea2005-09-01 20:46:49 +00003951 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003952 curwin_save = curwin;
3953 curbuf_save = curbuf;
Bram Moolenaar82139082011-09-14 16:52:09 +02003954
3955 /* Call a function, which returns a list or dict. */
3956 if (call_vim_function(funcname, 2, args, FALSE, &rettv) == OK)
3957 {
3958 switch (rettv.v_type)
3959 {
3960 case VAR_LIST:
3961 matchlist = rettv.vval.v_list;
3962 break;
3963 case VAR_DICT:
3964 matchdict = rettv.vval.v_dict;
3965 break;
3966 default:
3967 /* TODO: Give error message? */
3968 clear_tv(&rettv);
3969 break;
3970 }
3971 }
3972
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003973 if (curwin_save != curwin || curbuf_save != curbuf)
3974 {
3975 EMSG(_(e_complwin));
3976 goto theend;
3977 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00003978 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003979 check_cursor();
3980 if (!equalpos(curwin->w_cursor, pos))
3981 {
3982 EMSG(_(e_compldel));
3983 goto theend;
3984 }
Bram Moolenaar82139082011-09-14 16:52:09 +02003985
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003986 if (matchlist != NULL)
3987 ins_compl_add_list(matchlist);
Bram Moolenaar82139082011-09-14 16:52:09 +02003988 else if (matchdict != NULL)
3989 ins_compl_add_dict(matchdict);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003990
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003991theend:
Bram Moolenaar82139082011-09-14 16:52:09 +02003992 if (matchdict != NULL)
3993 dict_unref(matchdict);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003994 if (matchlist != NULL)
3995 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003996}
3997#endif /* FEAT_COMPL_FUNC */
3998
Bram Moolenaar39f05632006-03-19 22:15:26 +00003999#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00004000/*
4001 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00004002 */
4003 static void
4004ins_compl_add_list(list)
4005 list_T *list;
4006{
4007 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00004008 int dir = compl_direction;
4009
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004010 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004011 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004012 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00004013 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
4014 /* if dir was BACKWARD then honor it just once */
4015 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00004016 else if (did_emsg)
4017 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004018 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004019}
Bram Moolenaar39f05632006-03-19 22:15:26 +00004020
4021/*
Bram Moolenaar82139082011-09-14 16:52:09 +02004022 * Add completions from a dict.
4023 */
4024 static void
4025ins_compl_add_dict(dict)
4026 dict_T *dict;
4027{
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004028 dictitem_T *di_refresh;
4029 dictitem_T *di_words;
Bram Moolenaar82139082011-09-14 16:52:09 +02004030
4031 /* Check for optional "refresh" item. */
4032 compl_opt_refresh_always = FALSE;
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004033 di_refresh = dict_find(dict, (char_u *)"refresh", 7);
4034 if (di_refresh != NULL && di_refresh->di_tv.v_type == VAR_STRING)
Bram Moolenaar82139082011-09-14 16:52:09 +02004035 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004036 char_u *v = di_refresh->di_tv.vval.v_string;
Bram Moolenaar82139082011-09-14 16:52:09 +02004037
4038 if (v != NULL && STRCMP(v, (char_u *)"always") == 0)
4039 compl_opt_refresh_always = TRUE;
4040 }
4041
4042 /* Add completions from a "words" list. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +01004043 di_words = dict_find(dict, (char_u *)"words", 5);
4044 if (di_words != NULL && di_words->di_tv.v_type == VAR_LIST)
4045 ins_compl_add_list(di_words->di_tv.vval.v_list);
Bram Moolenaar82139082011-09-14 16:52:09 +02004046}
4047
4048/*
Bram Moolenaar39f05632006-03-19 22:15:26 +00004049 * Add a match to the list of matches from a typeval_T.
4050 * If the given string is already in the list of completions, then return
4051 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
4052 * maybe because alloc() returns NULL, then FAIL is returned.
4053 */
4054 int
4055ins_compl_add_tv(tv, dir)
4056 typval_T *tv;
4057 int dir;
4058{
4059 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00004060 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004061 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004062 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00004063 char_u *(cptext[CPT_COUNT]);
4064
4065 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
4066 {
4067 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
4068 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
4069 (char_u *)"abbr", FALSE);
4070 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
4071 (char_u *)"menu", FALSE);
4072 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
4073 (char_u *)"kind", FALSE);
4074 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
4075 (char_u *)"info", FALSE);
4076 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
4077 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00004078 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00004079 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004080 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
4081 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00004082 }
4083 else
4084 {
4085 word = get_tv_string_chk(tv);
4086 vim_memset(cptext, 0, sizeof(cptext));
4087 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01004088 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00004089 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00004090 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00004091}
Bram Moolenaara94bc432006-03-10 21:42:59 +00004092#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004093
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004094/*
4095 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004096 * The search starts at position "ini" in curbuf and in the direction
4097 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004098 * When "compl_started" is FALSE start at that position, otherwise continue
4099 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004100 * This may return before finding all the matches.
4101 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 */
4103 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004104ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106{
4107 static pos_T first_match_pos;
4108 static pos_T last_match_pos;
4109 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004110 static int found_all = FALSE; /* Found all matches of a
4111 certain type. */
4112 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113
Bram Moolenaar572cb562005-08-05 21:35:02 +00004114 pos_T *pos;
4115 char_u **matches;
4116 int save_p_scs;
4117 int save_p_ws;
4118 int save_p_ic;
4119 int i;
4120 int num_matches;
4121 int len;
4122 int found_new_match;
4123 int type = ctrl_x_mode;
4124 char_u *ptr;
4125 char_u *dict = NULL;
4126 int dict_f = 0;
4127 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004128
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004129 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 {
4131 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
4132 ins_buf->b_scanned = 0;
4133 found_all = FALSE;
4134 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004135 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004136 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 last_match_pos = first_match_pos = *ini;
4138 }
4139
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004140 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004141 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
4143 for (;;)
4144 {
4145 found_new_match = FAIL;
4146
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004147 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 * or if found_all says this entry is done. For ^X^L only use the
4149 * entries from 'complete' that look in loaded buffers. */
4150 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004151 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 {
4153 found_all = FALSE;
4154 while (*e_cpt == ',' || *e_cpt == ' ')
4155 e_cpt++;
4156 if (*e_cpt == '.' && !curbuf->b_scanned)
4157 {
4158 ins_buf = curbuf;
4159 first_match_pos = *ini;
4160 /* So that ^N can match word immediately after cursor */
4161 if (ctrl_x_mode == 0)
4162 dec(&first_match_pos);
4163 last_match_pos = first_match_pos;
4164 type = 0;
4165 }
4166 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4167 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4168 {
4169 /* Scan a buffer, but not the current one. */
4170 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4171 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004172 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 first_match_pos.col = last_match_pos.col = 0;
4174 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4175 last_match_pos.lnum = 0;
4176 type = 0;
4177 }
4178 else /* unloaded buffer, scan like dictionary */
4179 {
4180 found_all = TRUE;
4181 if (ins_buf->b_fname == NULL)
4182 continue;
4183 type = CTRL_X_DICTIONARY;
4184 dict = ins_buf->b_fname;
4185 dict_f = DICT_EXACT;
4186 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004187 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 ins_buf->b_fname == NULL
4189 ? buf_spname(ins_buf)
4190 : ins_buf->b_sfname == NULL
4191 ? (char *)ins_buf->b_fname
4192 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004193 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 }
4195 else if (*e_cpt == NUL)
4196 break;
4197 else
4198 {
4199 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4200 type = -1;
4201 else if (*e_cpt == 'k' || *e_cpt == 's')
4202 {
4203 if (*e_cpt == 'k')
4204 type = CTRL_X_DICTIONARY;
4205 else
4206 type = CTRL_X_THESAURUS;
4207 if (*++e_cpt != ',' && *e_cpt != NUL)
4208 {
4209 dict = e_cpt;
4210 dict_f = DICT_FIRST;
4211 }
4212 }
4213#ifdef FEAT_FIND_ID
4214 else if (*e_cpt == 'i')
4215 type = CTRL_X_PATH_PATTERNS;
4216 else if (*e_cpt == 'd')
4217 type = CTRL_X_PATH_DEFINES;
4218#endif
4219 else if (*e_cpt == ']' || *e_cpt == 't')
4220 {
4221 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004222 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004223 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 }
4225 else
4226 type = -1;
4227
4228 /* in any case e_cpt is advanced to the next entry */
4229 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4230
4231 found_all = TRUE;
4232 if (type == -1)
4233 continue;
4234 }
4235 }
4236
4237 switch (type)
4238 {
4239 case -1:
4240 break;
4241#ifdef FEAT_FIND_ID
4242 case CTRL_X_PATH_PATTERNS:
4243 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004244 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004245 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004247 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4249 (linenr_T)1, (linenr_T)MAXLNUM);
4250 break;
4251#endif
4252
4253 case CTRL_X_DICTIONARY:
4254 case CTRL_X_THESAURUS:
4255 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004256 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 : (type == CTRL_X_THESAURUS
4258 ? (*curbuf->b_p_tsr == NUL
4259 ? p_tsr
4260 : curbuf->b_p_tsr)
4261 : (*curbuf->b_p_dict == NUL
4262 ? p_dict
4263 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004264 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004265 dict != NULL ? dict_f
4266 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 dict = NULL;
4268 break;
4269
4270 case CTRL_X_TAGS:
4271 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4272 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004273 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004275 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004276 * of matches is found when compl_pattern is empty */
4277 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004278 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4279 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4280 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4281 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004282 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
4284 p_ic = save_p_ic;
4285 break;
4286
4287 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004288 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4290 {
4291
4292 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004293 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004294 ins_compl_add_matches(num_matches, matches,
4295#ifdef CASE_INSENSITIVE_FILENAME
4296 TRUE
4297#else
4298 FALSE
4299#endif
4300 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004301 }
4302 break;
4303
4304 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004305 if (expand_cmdline(&compl_xp, compl_pattern,
4306 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004308 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 break;
4310
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004311#ifdef FEAT_COMPL_FUNC
4312 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004313 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004314 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004315 break;
4316#endif
4317
Bram Moolenaar488c6512005-08-11 20:09:58 +00004318 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004319#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004320 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004321 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004322 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004323 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004324#endif
4325 break;
4326
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 default: /* normal ^P/^N and ^X^L */
4328 /*
4329 * If 'infercase' is set, don't use 'smartcase' here
4330 */
4331 save_p_scs = p_scs;
4332 if (ins_buf->b_p_inf)
4333 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004334
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 /* buffers other than curbuf are scanned from the beginning or the
4336 * end but never from the middle, thus setting nowrapscan in this
4337 * buffers is a good idea, on the other hand, we always set
4338 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4339 save_p_ws = p_ws;
4340 if (ins_buf != curbuf)
4341 p_ws = FALSE;
4342 else if (*e_cpt == '.')
4343 p_ws = TRUE;
4344 for (;;)
4345 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004346 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004347
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004348 ++msg_silent; /* Don't want messages for wrapscan. */
4349
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004350 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4351 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004355 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004357 found_new_match = searchit(NULL, ins_buf, pos,
4358 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004359 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004360 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004361 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004362 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004364 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004365 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 first_match_pos = *pos;
4367 last_match_pos = *pos;
4368 }
4369 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004370 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 found_new_match = FAIL;
4372 if (found_new_match == FAIL)
4373 {
4374 if (ins_buf == curbuf)
4375 found_all = TRUE;
4376 break;
4377 }
4378
4379 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004381 && ini->lnum == pos->lnum
4382 && ini->col == pos->col)
4383 continue;
4384 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4385 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4386 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004387 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 {
4389 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4390 continue;
4391 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4392 if (!p_paste)
4393 ptr = skipwhite(ptr);
4394 }
4395 len = (int)STRLEN(ptr);
4396 }
4397 else
4398 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004399 char_u *tmp_ptr = ptr;
4400
4401 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004403 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004404 /* Skip if already inside a word. */
4405 if (vim_iswordp(tmp_ptr))
4406 continue;
4407 /* Find start of next word. */
4408 tmp_ptr = find_word_start(tmp_ptr);
4409 }
4410 /* Find end of this word. */
4411 tmp_ptr = find_word_end(tmp_ptr);
4412 len = (int)(tmp_ptr - ptr);
4413
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004414 if ((compl_cont_status & CONT_ADDING)
4415 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004416 {
4417 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4418 {
4419 /* Try next line, if any. the new word will be
4420 * "join" as if the normal command "J" was used.
4421 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004422 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004423 * works -- Acevedo */
4424 STRNCPY(IObuff, ptr, len);
4425 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4426 tmp_ptr = ptr = skipwhite(ptr);
4427 /* Find start of next word. */
4428 tmp_ptr = find_word_start(tmp_ptr);
4429 /* Find end of next word. */
4430 tmp_ptr = find_word_end(tmp_ptr);
4431 if (tmp_ptr > ptr)
4432 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004433 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004435 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 IObuff[len++] = ' ';
4437 /* IObuf =~ "\k.* ", thus len >= 2 */
4438 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004439 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 || (vim_strchr(p_cpo, CPO_JOINSP)
4441 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004442 && (IObuff[len - 2] == '?'
4443 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 IObuff[len++] = ' ';
4445 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004446 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 if (tmp_ptr - ptr >= IOSIZE - len)
4448 tmp_ptr = ptr + IOSIZE - len - 1;
4449 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4450 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004451 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004452 }
4453 IObuff[len] = NUL;
4454 ptr = IObuff;
4455 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 continue;
4458 }
4459 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004460 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004461 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004462 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 {
4464 found_new_match = OK;
4465 break;
4466 }
4467 }
4468 p_scs = save_p_scs;
4469 p_ws = save_p_ws;
4470 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004471
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004472 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004473 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004474 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 found_new_match = OK;
4476
4477 /* break the loop for specialized modes (use 'complete' just for the
4478 * generic ctrl_x_mode == 0) or when we've found a new match */
4479 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004480 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004481 {
4482 if (got_int)
4483 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004484 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004485 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004486 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004487
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004488 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4489 || compl_interrupted)
4490 break;
4491 compl_started = TRUE;
4492 }
4493 else
4494 {
4495 /* Mark a buffer scanned when it has been scanned completely */
4496 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4497 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004498
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004499 compl_started = FALSE;
4500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004502 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503
4504 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4505 && *e_cpt == NUL) /* Got to end of 'complete' */
4506 found_new_match = FAIL;
4507
4508 i = -1; /* total of matches, unknown */
4509 if (found_new_match == FAIL
4510 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4511 i = ins_compl_make_cyclic();
4512
4513 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004514 * just been made cyclic then we have to move compl_curr_match to the next
4515 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004516 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4517 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004518 if (compl_curr_match == NULL)
4519 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 return i;
4521}
4522
4523/* Delete the old text being completed. */
4524 static void
4525ins_compl_delete()
4526{
4527 int i;
4528
4529 /*
4530 * In insert mode: Delete the typed part.
4531 * In replace mode: Put the old characters back, if any.
4532 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004533 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004534 backspace_until_column(i);
4535 changed_cline_bef_curs();
4536}
4537
4538/* Insert the new text being completed. */
4539 static void
4540ins_compl_insert()
4541{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004542 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004543 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4544 compl_used_match = FALSE;
4545 else
4546 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004547}
4548
4549/*
4550 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004551 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4552 * get more completions. If it is FALSE, then we just do nothing when there
4553 * are no more completions in a given direction. The latter case is used when
4554 * we are still in the middle of finding completions, to allow browsing
4555 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004556 * Return the total number of matches, or -1 if still unknown -- webb.
4557 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004558 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4559 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 *
4561 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004562 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4563 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 */
4565 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004566ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004568 int count; /* repeat completion this many times; should
4569 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004570 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571{
4572 int num_matches = -1;
4573 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004574 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004575 compl_T *found_compl = NULL;
4576 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004577 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578
Bram Moolenaar6d6cec82012-01-20 14:32:27 +01004579 /* When user complete function return -1 for findstart which is next
4580 * time of 'always', compl_shown_match become NULL. */
4581 if (compl_shown_match == NULL)
4582 return -1;
4583
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004584 if (compl_leader != NULL
4585 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004586 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004587 /* Set "compl_shown_match" to the actually shown match, it may differ
4588 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004589 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004590 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004591 && compl_shown_match->cp_next != NULL
4592 && compl_shown_match->cp_next != compl_first_match)
4593 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004594
4595 /* If we didn't find it searching forward, and compl_shows_dir is
4596 * backward, find the last match. */
4597 if (compl_shows_dir == BACKWARD
4598 && !ins_compl_equal(compl_shown_match,
4599 compl_leader, (int)STRLEN(compl_leader))
4600 && (compl_shown_match->cp_next == NULL
4601 || compl_shown_match->cp_next == compl_first_match))
4602 {
4603 while (!ins_compl_equal(compl_shown_match,
4604 compl_leader, (int)STRLEN(compl_leader))
4605 && compl_shown_match->cp_prev != NULL
4606 && compl_shown_match->cp_prev != compl_first_match)
4607 compl_shown_match = compl_shown_match->cp_prev;
4608 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004609 }
4610
4611 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004612 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /* Delete old text to be replaced */
4614 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004615
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004616 /* When finding the longest common text we stick at the original text,
4617 * don't let CTRL-N or CTRL-P move to the first match. */
4618 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4619
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004620 /* When restarting the search don't insert the first match either. */
4621 if (compl_restarting)
4622 {
4623 advance = FALSE;
4624 compl_restarting = FALSE;
4625 }
4626
Bram Moolenaare3226be2005-12-18 22:10:00 +00004627 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4628 * around. */
4629 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004631 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004632 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004633 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004634 found_end = (compl_first_match != NULL
4635 && (compl_shown_match->cp_next == compl_first_match
4636 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004637 }
4638 else if (compl_shows_dir == BACKWARD
4639 && compl_shown_match->cp_prev != NULL)
4640 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004641 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004642 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004643 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
4645 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004646 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004647 if (!allow_get_expansion)
4648 {
4649 if (advance)
4650 {
4651 if (compl_shows_dir == BACKWARD)
4652 compl_pending -= todo + 1;
4653 else
4654 compl_pending += todo + 1;
4655 }
4656 return -1;
4657 }
4658
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004659 if (advance)
4660 {
4661 if (compl_shows_dir == BACKWARD)
4662 --compl_pending;
4663 else
4664 ++compl_pending;
4665 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004666
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004667 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004668 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004669
4670 /* handle any pending completions */
4671 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004672 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004673 {
4674 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4675 {
4676 compl_shown_match = compl_shown_match->cp_next;
4677 --compl_pending;
4678 }
4679 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4680 {
4681 compl_shown_match = compl_shown_match->cp_prev;
4682 ++compl_pending;
4683 }
4684 else
4685 break;
4686 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004687 found_end = FALSE;
4688 }
4689 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4690 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004691 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004692 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004693 ++todo;
4694 else
4695 /* Remember a matching item. */
4696 found_compl = compl_shown_match;
4697
4698 /* Stop at the end of the list when we found a usable match. */
4699 if (found_end)
4700 {
4701 if (found_compl != NULL)
4702 {
4703 compl_shown_match = found_compl;
4704 break;
4705 }
4706 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004708 }
4709
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004710 /* Insert the text of the new completion, or the compl_leader. */
4711 if (insert_match)
4712 {
4713 if (!compl_get_longest || compl_used_match)
4714 ins_compl_insert();
4715 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004716 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004717 }
4718 else
4719 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004720
4721 if (!allow_get_expansion)
4722 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004723 /* may undisplay the popup menu first */
4724 ins_compl_upd_pum();
4725
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004726 /* redraw to show the user what was inserted */
4727 update_screen(0);
4728
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004729 /* display the updated popup menu */
4730 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004731#ifdef FEAT_GUI
4732 if (gui.in_use)
4733 {
4734 /* Show the cursor after the match, not after the redrawn text. */
4735 setcursor();
4736 out_flush();
4737 gui_update_cursor(FALSE, FALSE);
4738 }
4739#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004740
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 /* Delete old text to be replaced, since we're still searching and
4742 * don't want to match ourselves! */
4743 ins_compl_delete();
4744 }
4745
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004746 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004747 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004748 compl_enter_selects = !insert_match && compl_match_array != NULL;
4749
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 /*
4751 * Show the file name for the match (if any)
4752 * Truncate the file name to avoid a wait for return.
4753 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004754 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 {
4756 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004757 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 if (i <= 0)
4759 i = 0;
4760 else
4761 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004762 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 msg(IObuff);
4764 redraw_cmdline = FALSE; /* don't overwrite! */
4765 }
4766
4767 return num_matches;
4768}
4769
4770/*
4771 * Call this while finding completions, to check whether the user has hit a key
4772 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004773 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004775 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776 */
4777 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004778ins_compl_check_keys(frequency)
4779 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780{
4781 static int count = 0;
4782
4783 int c;
4784
4785 /* Don't check when reading keys from a script. That would break the test
4786 * scripts */
4787 if (using_script())
4788 return;
4789
4790 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004791 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 return;
4793 count = 0;
4794
Bram Moolenaara260a972006-06-23 19:36:29 +00004795 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4796 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 if (c != NUL)
4799 {
4800 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4801 {
4802 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004803 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004804 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4805 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004807 else
4808 {
4809 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004810 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004811 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004812 if (c != K_IGNORE)
4813 {
4814 /* Don't interrupt completion when the character wasn't typed,
4815 * e.g., when doing @q to replay keys. */
4816 if (c != Ctrl_R && KeyTyped)
4817 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004818
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004819 vungetc(c);
4820 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004823 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004824 {
4825 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4826
4827 compl_pending = 0;
4828 (void)ins_compl_next(FALSE, todo, TRUE);
4829 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004830}
4831
4832/*
4833 * Decide the direction of Insert mode complete from the key typed.
4834 * Returns BACKWARD or FORWARD.
4835 */
4836 static int
4837ins_compl_key2dir(c)
4838 int c;
4839{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004840 if (c == Ctrl_P || c == Ctrl_L
4841 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4842 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004843 return BACKWARD;
4844 return FORWARD;
4845}
4846
4847/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004848 * Return TRUE for keys that are used for completion only when the popup menu
4849 * is visible.
4850 */
4851 static int
4852ins_compl_pum_key(c)
4853 int c;
4854{
4855 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004856 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4857 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004858}
4859
4860/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004861 * Decide the number of completions to move forward.
4862 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4863 */
4864 static int
4865ins_compl_key2count(c)
4866 int c;
4867{
4868 int h;
4869
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004870 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004871 {
4872 h = pum_get_height();
4873 if (h > 3)
4874 h -= 2; /* keep some context */
4875 return h;
4876 }
4877 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878}
4879
4880/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004881 * Return TRUE if completion with "c" should insert the match, FALSE if only
4882 * to change the currently selected completion.
4883 */
4884 static int
4885ins_compl_use_match(c)
4886 int c;
4887{
4888 switch (c)
4889 {
4890 case K_UP:
4891 case K_DOWN:
4892 case K_PAGEDOWN:
4893 case K_KPAGEDOWN:
4894 case K_S_DOWN:
4895 case K_PAGEUP:
4896 case K_KPAGEUP:
4897 case K_S_UP:
4898 return FALSE;
4899 }
4900 return TRUE;
4901}
4902
4903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 * Do Insert mode completion.
4905 * Called when character "c" was typed, which has a meaning for completion.
4906 * Returns OK if completion was done, FAIL if something failed (out of mem).
4907 */
4908 static int
4909ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004910 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004912 char_u *line;
4913 int startcol = 0; /* column where searched text starts */
4914 colnr_T curs_col; /* cursor column */
4915 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004916 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917
Bram Moolenaare3226be2005-12-18 22:10:00 +00004918 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004919 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004920 {
4921 /* First time we hit ^N or ^P (in a row, I mean) */
4922
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 did_ai = FALSE;
4924#ifdef FEAT_SMARTINDENT
4925 did_si = FALSE;
4926 can_si = FALSE;
4927 can_si_back = FALSE;
4928#endif
4929 if (stop_arrow() == FAIL)
4930 return FAIL;
4931
4932 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004933 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004934 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004936 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004937 * "compl_startpos" to the cursor as a pattern to add a new word
4938 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004939 * "compl_startpos" is not in the same line as the cursor then fix it
4940 * (the line has been split because it was longer than 'tw'). if SOL
4941 * is set then skip the previous pattern, a word at the beginning of
4942 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004943 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4944 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
4946 /*
4947 * it is a continued search
4948 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004949 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004950 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4951 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4952 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004953 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004955 /* line (probably) wrapped, set compl_startpos to the
4956 * first non_blank in the line, if it is not a wordchar
4957 * include it to get a better pattern, but then we don't
4958 * want the "\\<" prefix, check it bellow */
4959 compl_col = (colnr_T)(skipwhite(line) - line);
4960 compl_startpos.col = compl_col;
4961 compl_startpos.lnum = curwin->w_cursor.lnum;
4962 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 }
4964 else
4965 {
4966 /* S_IPOS was set when we inserted a word that was at the
4967 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004968 * mode but first we need to redefine compl_startpos */
4969 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004971 compl_cont_status |= CONT_SOL;
4972 compl_startpos.col = (colnr_T)(skipwhite(
4973 line + compl_length
4974 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004976 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004977 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004978 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004979 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004980 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004982 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004984 compl_cont_status &= ~CONT_SOL;
4985 compl_length = (IOSIZE - MIN_SPACE);
4986 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004987 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004988 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4989 if (compl_length < 1)
4990 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004993 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004995 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004996 }
4997 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004998 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005000 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005002 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005004 compl_cont_status = 0;
5005 compl_cont_status |= CONT_N_ADDS;
5006 compl_startpos = curwin->w_cursor;
5007 startcol = (int)curs_col;
5008 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
5010
5011 /* Work out completion pattern and original text -- webb */
5012 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
5013 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005014 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005015 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
5016 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005017 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005019 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005021 compl_col += ++startcol;
5022 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005025 compl_pattern = str_foldcase(line + compl_col,
5026 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005028 compl_pattern = vim_strnsave(line + compl_col,
5029 compl_length);
5030 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005031 return FAIL;
5032 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005033 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 {
5035 char_u *prefix = (char_u *)"\\<";
5036
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005037 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005038 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005039 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005040 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005042 if (!vim_iswordp(line + compl_col)
5043 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 && (
5045#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005046 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005048 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005049#endif
5050 )))
5051 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005052 STRCPY((char *)compl_pattern, prefix);
5053 (void)quote_meta(compl_pattern + STRLEN(prefix),
5054 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005056 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005058 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005060 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061#endif
5062 )
5063 {
5064 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005065 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
5066 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005068 compl_col += curs_col;
5069 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 }
5071 else
5072 {
5073#ifdef FEAT_MBYTE
5074 /* Search the point of change class of multibyte character
5075 * or not a word single byte character backward. */
5076 if (has_mbyte)
5077 {
5078 int base_class;
5079 int head_off;
5080
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005081 startcol -= (*mb_head_off)(line, line + startcol);
5082 base_class = mb_get_class(line + startcol);
5083 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005085 head_off = (*mb_head_off)(line, line + startcol);
5086 if (base_class != mb_get_class(line + startcol
5087 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005089 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 }
5091 }
5092 else
5093#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005096 compl_col += ++startcol;
5097 compl_length = (int)curs_col - startcol;
5098 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 {
5100 /* Only match word with at least two chars -- webb
5101 * there's no need to call quote_meta,
5102 * alloc(7) is enough -- Acevedo
5103 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005104 compl_pattern = alloc(7);
5105 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005107 STRCPY((char *)compl_pattern, "\\<");
5108 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
5109 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 }
5111 else
5112 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005113 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005114 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005115 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005117 STRCPY((char *)compl_pattern, "\\<");
5118 (void)quote_meta(compl_pattern + 2, line + compl_col,
5119 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 }
5121 }
5122 }
5123 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5124 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005125 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005126 compl_length = (int)curs_col - (int)compl_col;
5127 if (compl_length < 0) /* cursor in indent: empty pattern */
5128 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005130 compl_pattern = str_foldcase(line + compl_col, compl_length,
5131 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5134 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005135 return FAIL;
5136 }
5137 else if (ctrl_x_mode == CTRL_X_FILES)
5138 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 compl_col += ++startcol;
5142 compl_length = (int)curs_col - startcol;
5143 compl_pattern = addstar(line + compl_col, compl_length,
5144 EXPAND_FILES);
5145 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 return FAIL;
5147 }
5148 else if (ctrl_x_mode == CTRL_X_CMDLINE)
5149 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005150 compl_pattern = vim_strnsave(line, curs_col);
5151 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005153 set_cmd_context(&compl_xp, compl_pattern,
5154 (int)STRLEN(compl_pattern), curs_col);
5155 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5156 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005157 /* No completion possible, use an empty pattern to get a
5158 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005159 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005160 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005161 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5162 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005164 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005165 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005166#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005167 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005168 * Call user defined function 'completefunc' with "a:findstart"
5169 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005170 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005171 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005172 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005173 char_u *funcname;
5174 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005175 win_T *curwin_save;
5176 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005177
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005178 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005179 * string */
5180 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5181 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5182 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005183 {
5184 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5185 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005186 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005187 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005188
5189 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005190 args[1] = NULL;
5191 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005192 curwin_save = curwin;
5193 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005194 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005195 if (curwin_save != curwin || curbuf_save != curbuf)
5196 {
5197 EMSG(_(e_complwin));
5198 return FAIL;
5199 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005200 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005201 check_cursor();
5202 if (!equalpos(curwin->w_cursor, pos))
5203 {
5204 EMSG(_(e_compldel));
5205 return FAIL;
5206 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005207
Bram Moolenaar6110a002012-01-26 18:58:38 +01005208 /* Return value -2 means the user complete function wants to
5209 * cancel the complete without an error. */
5210 if (col == -2)
5211 return FAIL;
5212
Bram Moolenaar82139082011-09-14 16:52:09 +02005213 /*
5214 * Reset extended parameters of completion, when start new
5215 * completion.
5216 */
5217 compl_opt_refresh_always = FALSE;
5218
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005219 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005220 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005221 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005222 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005223 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005225 /* Setup variables for completion. Need to obtain "line" again,
5226 * it may have become invalid. */
5227 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005228 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005229 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5230 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005231#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005232 return FAIL;
5233 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005234 else if (ctrl_x_mode == CTRL_X_SPELL)
5235 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005236#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005237 if (spell_bad_len > 0)
5238 compl_col = curs_col - spell_bad_len;
5239 else
5240 compl_col = spell_word_start(startcol);
5241 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005242 {
5243 compl_length = 0;
5244 compl_col = curs_col;
5245 }
5246 else
5247 {
5248 spell_expand_check_cap(compl_col);
5249 compl_length = (int)curs_col - compl_col;
5250 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005251 /* Need to obtain "line" again, it may have become invalid. */
5252 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005253 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5254 if (compl_pattern == NULL)
5255#endif
5256 return FAIL;
5257 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005258 else
5259 {
5260 EMSG2(_(e_intern2), "ins_complete()");
5261 return FAIL;
5262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005263
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005264 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005265 {
5266 edit_submode_pre = (char_u *)_(" Adding");
5267 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5268 {
5269 /* Insert a new line, keep indentation but ignore 'comments' */
5270#ifdef FEAT_COMMENTS
5271 char_u *old = curbuf->b_p_com;
5272
5273 curbuf->b_p_com = (char_u *)"";
5274#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005275 compl_startpos.lnum = curwin->w_cursor.lnum;
5276 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005277 ins_eol('\r');
5278#ifdef FEAT_COMMENTS
5279 curbuf->b_p_com = old;
5280#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 compl_length = 0;
5282 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005283 }
5284 }
5285 else
5286 {
5287 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005288 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 }
5290
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005291 if (compl_cont_status & CONT_LOCAL)
5292 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 else
5294 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5295
Bram Moolenaar62951b12011-09-21 18:23:05 +02005296 /* If any of the original typed text has been changed we need to fix
5297 * the redo buffer. */
5298 ins_compl_fixRedoBufForLeader(NULL);
5299
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005300 /* Always add completion for the original text. */
5301 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005302 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5303 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005304 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005306 vim_free(compl_pattern);
5307 compl_pattern = NULL;
5308 vim_free(compl_orig_text);
5309 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005310 return FAIL;
5311 }
5312
5313 /* showmode might reset the internal line pointers, so it must
5314 * be called before line = ml_get(), or when this address is no
5315 * longer needed. -- Acevedo.
5316 */
5317 edit_submode_extra = (char_u *)_("-- Searching...");
5318 edit_submode_highl = HLF_COUNT;
5319 showmode();
5320 edit_submode_extra = NULL;
5321 out_flush();
5322 }
5323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005324 compl_shown_match = compl_curr_match;
5325 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005326
5327 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005328 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005329 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005330 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005331 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005332
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005333 /* may undisplay the popup menu */
5334 ins_compl_upd_pum();
5335
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005336 if (n > 1) /* all matches have been found */
5337 compl_matches = n;
5338 compl_curr_match = compl_shown_match;
5339 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005340
Bram Moolenaard68071d2006-05-02 22:08:30 +00005341 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5342 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 if (got_int && !global_busy)
5344 {
5345 (void)vgetc();
5346 got_int = FALSE;
5347 }
5348
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005349 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005350 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005351 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005352 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5353 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005354 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5355 edit_submode_highl = HLF_E;
5356 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5357 * because we couldn't expand anything at first place, but if we used
5358 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5359 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005360 if ( compl_length > 1
5361 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 || (ctrl_x_mode != 0
5363 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5364 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005365 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005366 }
5367
Bram Moolenaar572cb562005-08-05 21:35:02 +00005368 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005369 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005370 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005371 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372
5373 if (edit_submode_extra == NULL)
5374 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005375 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005376 {
5377 edit_submode_extra = (char_u *)_("Back at original");
5378 edit_submode_highl = HLF_W;
5379 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005380 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005381 {
5382 edit_submode_extra = (char_u *)_("Word from other line");
5383 edit_submode_highl = HLF_COUNT;
5384 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005385 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005386 {
5387 edit_submode_extra = (char_u *)_("The only match");
5388 edit_submode_highl = HLF_COUNT;
5389 }
5390 else
5391 {
5392 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005393 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005395 int number = 0;
5396 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005397
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005398 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005399 {
5400 /* search backwards for the first valid (!= -1) number.
5401 * This should normally succeed already at the first loop
5402 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005403 for (match = compl_curr_match->cp_prev; match != NULL
5404 && match != compl_first_match;
5405 match = match->cp_prev)
5406 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005407 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005408 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005409 break;
5410 }
5411 if (match != NULL)
5412 /* go up and assign all numbers which are not assigned
5413 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005414 for (match = match->cp_next;
5415 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005416 match = match->cp_next)
5417 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005418 }
5419 else /* BACKWARD */
5420 {
5421 /* search forwards (upwards) for the first valid (!= -1)
5422 * number. This should normally succeed already at the
5423 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005424 for (match = compl_curr_match->cp_next; match != NULL
5425 && match != compl_first_match;
5426 match = match->cp_next)
5427 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005428 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005429 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005430 break;
5431 }
5432 if (match != NULL)
5433 /* go down and assign all numbers which are not
5434 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005435 for (match = match->cp_prev; match
5436 && match->cp_number == -1;
5437 match = match->cp_prev)
5438 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005439 }
5440 }
5441
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005442 /* The match should always have a sequence number now, this is
5443 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005444 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005445 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005446 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5447 * Translations may need more than twice that. */
5448 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005449
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005450 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005451 vim_snprintf((char *)match_ref, sizeof(match_ref),
5452 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005453 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005454 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005455 vim_snprintf((char *)match_ref, sizeof(match_ref),
5456 _("match %d"),
5457 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005458 edit_submode_extra = match_ref;
5459 edit_submode_highl = HLF_R;
Bram Moolenaar76b9b362012-02-04 23:35:00 +01005460 if (dollar_vcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005461 curs_columns(FALSE);
5462 }
5463 }
5464 }
5465
5466 /* Show a message about what (completion) mode we're in. */
5467 showmode();
5468 if (edit_submode_extra != NULL)
5469 {
5470 if (!p_smd)
5471 msg_attr(edit_submode_extra,
5472 edit_submode_highl < HLF_COUNT
5473 ? hl_attr(edit_submode_highl) : 0);
5474 }
5475 else
5476 msg_clr_cmdline(); /* necessary for "noshowmode" */
5477
Bram Moolenaard68071d2006-05-02 22:08:30 +00005478 /* Show the popup menu, unless we got interrupted. */
5479 if (!compl_interrupted)
5480 {
5481 /* RedrawingDisabled may be set when invoked through complete(). */
5482 n = RedrawingDisabled;
5483 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005484
5485 /* If the cursor moved we need to remove the pum first. */
5486 setcursor();
5487 if (save_w_wrow != curwin->w_wrow)
5488 ins_compl_del_pum();
5489
Bram Moolenaard68071d2006-05-02 22:08:30 +00005490 ins_compl_show_pum();
5491 setcursor();
5492 RedrawingDisabled = n;
5493 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005494 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005495 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005496
Bram Moolenaar071d4272004-06-13 20:20:40 +00005497 return OK;
5498}
5499
5500/*
5501 * Looks in the first "len" chars. of "src" for search-metachars.
5502 * If dest is not NULL the chars. are copied there quoting (with
5503 * a backslash) the metachars, and dest would be NUL terminated.
5504 * Returns the length (needed) of dest
5505 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005506 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507quote_meta(dest, src, len)
5508 char_u *dest;
5509 char_u *src;
5510 int len;
5511{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005512 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005513
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005514 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005515 {
5516 switch (*src)
5517 {
5518 case '.':
5519 case '*':
5520 case '[':
5521 if (ctrl_x_mode == CTRL_X_DICTIONARY
5522 || ctrl_x_mode == CTRL_X_THESAURUS)
5523 break;
5524 case '~':
5525 if (!p_magic) /* quote these only if magic is set */
5526 break;
5527 case '\\':
5528 if (ctrl_x_mode == CTRL_X_DICTIONARY
5529 || ctrl_x_mode == CTRL_X_THESAURUS)
5530 break;
5531 case '^': /* currently it's not needed. */
5532 case '$':
5533 m++;
5534 if (dest != NULL)
5535 *dest++ = '\\';
5536 break;
5537 }
5538 if (dest != NULL)
5539 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005540# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005541 /* Copy remaining bytes of a multibyte character. */
5542 if (has_mbyte)
5543 {
5544 int i, mb_len;
5545
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005546 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 if (mb_len > 0 && len >= mb_len)
5548 for (i = 0; i < mb_len; ++i)
5549 {
5550 --len;
5551 ++src;
5552 if (dest != NULL)
5553 *dest++ = *src;
5554 }
5555 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005556# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
5558 if (dest != NULL)
5559 *dest = NUL;
5560
5561 return m;
5562}
5563#endif /* FEAT_INS_EXPAND */
5564
5565/*
5566 * Next character is interpreted literally.
5567 * A one, two or three digit decimal number is interpreted as its byte value.
5568 * If one or two digits are entered, the next character is given to vungetc().
5569 * For Unicode a character > 255 may be returned.
5570 */
5571 int
5572get_literal()
5573{
5574 int cc;
5575 int nc;
5576 int i;
5577 int hex = FALSE;
5578 int octal = FALSE;
5579#ifdef FEAT_MBYTE
5580 int unicode = 0;
5581#endif
5582
5583 if (got_int)
5584 return Ctrl_C;
5585
5586#ifdef FEAT_GUI
5587 /*
5588 * In GUI there is no point inserting the internal code for a special key.
5589 * It is more useful to insert the string "<KEY>" instead. This would
5590 * probably be useful in a text window too, but it would not be
5591 * vi-compatible (maybe there should be an option for it?) -- webb
5592 */
5593 if (gui.in_use)
5594 ++allow_keys;
5595#endif
5596#ifdef USE_ON_FLY_SCROLL
5597 dont_scroll = TRUE; /* disallow scrolling here */
5598#endif
5599 ++no_mapping; /* don't map the next key hits */
5600 cc = 0;
5601 i = 0;
5602 for (;;)
5603 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005604 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005605#ifdef FEAT_CMDL_INFO
5606 if (!(State & CMDLINE)
5607# ifdef FEAT_MBYTE
5608 && MB_BYTE2LEN_CHECK(nc) == 1
5609# endif
5610 )
5611 add_to_showcmd(nc);
5612#endif
5613 if (nc == 'x' || nc == 'X')
5614 hex = TRUE;
5615 else if (nc == 'o' || nc == 'O')
5616 octal = TRUE;
5617#ifdef FEAT_MBYTE
5618 else if (nc == 'u' || nc == 'U')
5619 unicode = nc;
5620#endif
5621 else
5622 {
5623 if (hex
5624#ifdef FEAT_MBYTE
5625 || unicode != 0
5626#endif
5627 )
5628 {
5629 if (!vim_isxdigit(nc))
5630 break;
5631 cc = cc * 16 + hex2nr(nc);
5632 }
5633 else if (octal)
5634 {
5635 if (nc < '0' || nc > '7')
5636 break;
5637 cc = cc * 8 + nc - '0';
5638 }
5639 else
5640 {
5641 if (!VIM_ISDIGIT(nc))
5642 break;
5643 cc = cc * 10 + nc - '0';
5644 }
5645
5646 ++i;
5647 }
5648
5649 if (cc > 255
5650#ifdef FEAT_MBYTE
5651 && unicode == 0
5652#endif
5653 )
5654 cc = 255; /* limit range to 0-255 */
5655 nc = 0;
5656
5657 if (hex) /* hex: up to two chars */
5658 {
5659 if (i >= 2)
5660 break;
5661 }
5662#ifdef FEAT_MBYTE
5663 else if (unicode) /* Unicode: up to four or eight chars */
5664 {
5665 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5666 break;
5667 }
5668#endif
5669 else if (i >= 3) /* decimal or octal: up to three chars */
5670 break;
5671 }
5672 if (i == 0) /* no number entered */
5673 {
5674 if (nc == K_ZERO) /* NUL is stored as NL */
5675 {
5676 cc = '\n';
5677 nc = 0;
5678 }
5679 else
5680 {
5681 cc = nc;
5682 nc = 0;
5683 }
5684 }
5685
5686 if (cc == 0) /* NUL is stored as NL */
5687 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005688#ifdef FEAT_MBYTE
5689 if (enc_dbcs && (cc & 0xff) == 0)
5690 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5691 second byte will cause trouble! */
5692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
5694 --no_mapping;
5695#ifdef FEAT_GUI
5696 if (gui.in_use)
5697 --allow_keys;
5698#endif
5699 if (nc)
5700 vungetc(nc);
5701 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5702 return cc;
5703}
5704
5705/*
5706 * Insert character, taking care of special keys and mod_mask
5707 */
5708 static void
5709insert_special(c, allow_modmask, ctrlv)
5710 int c;
5711 int allow_modmask;
5712 int ctrlv; /* c was typed after CTRL-V */
5713{
5714 char_u *p;
5715 int len;
5716
5717 /*
5718 * Special function key, translate into "<Key>". Up to the last '>' is
5719 * inserted with ins_str(), so as not to replace characters in replace
5720 * mode.
5721 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5722 * unless 'allow_modmask' is TRUE.
5723 */
5724#ifdef MACOS
5725 /* Command-key never produces a normal key */
5726 if (mod_mask & MOD_MASK_CMD)
5727 allow_modmask = TRUE;
5728#endif
5729 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5730 {
5731 p = get_special_key_name(c, mod_mask);
5732 len = (int)STRLEN(p);
5733 c = p[len - 1];
5734 if (len > 2)
5735 {
5736 if (stop_arrow() == FAIL)
5737 return;
5738 p[len - 1] = NUL;
5739 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005740 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 ctrlv = FALSE;
5742 }
5743 }
5744 if (stop_arrow() == OK)
5745 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5746}
5747
5748/*
5749 * Special characters in this context are those that need processing other
5750 * than the simple insertion that can be performed here. This includes ESC
5751 * which terminates the insert, and CR/NL which need special processing to
5752 * open up a new line. This routine tries to optimize insertions performed by
5753 * the "redo", "undo" or "put" commands, so it needs to know when it should
5754 * stop and defer processing to the "normal" mechanism.
5755 * '0' and '^' are special, because they can be followed by CTRL-D.
5756 */
5757#ifdef EBCDIC
5758# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5759#else
5760# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5761#endif
5762
5763#ifdef FEAT_MBYTE
5764# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5765#else
5766# define WHITECHAR(cc) vim_iswhite(cc)
5767#endif
5768
5769 void
5770insertchar(c, flags, second_indent)
5771 int c; /* character to insert or NUL */
5772 int flags; /* INSCHAR_FORMAT, etc. */
5773 int second_indent; /* indent for second line if >= 0 */
5774{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775 int textwidth;
5776#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005778#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
5781 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5782 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
5784 /*
5785 * Try to break the line in two or more pieces when:
5786 * - Always do this if we have been called to do formatting only.
5787 * - Always do this when 'formatoptions' has the 'a' flag and the line
5788 * ends in white space.
5789 * - Otherwise:
5790 * - Don't do this if inserting a blank
5791 * - Don't do this if an existing character is being replaced, unless
5792 * we're in VREPLACE mode.
5793 * - Do this if the cursor is not on the line where insert started
5794 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5795 * before the insert.
5796 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5797 * before 'textwidth'
5798 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005799 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005800 && ((flags & INSCHAR_FORMAT)
5801 || (!vim_iswhite(c)
5802 && !((State & REPLACE_FLAG)
5803#ifdef FEAT_VREPLACE
5804 && !(State & VREPLACE_FLAG)
5805#endif
5806 && *ml_get_cursor() != NUL)
5807 && (curwin->w_cursor.lnum != Insstart.lnum
5808 || ((!has_format_option(FO_INS_LONG)
5809 || Insstart_textlen <= (colnr_T)textwidth)
5810 && (!fo_ins_blank
5811 || Insstart_blank_vcol <= (colnr_T)textwidth
5812 ))))))
5813 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005814 /* Format with 'formatexpr' when it's set. Use internal formatting
5815 * when 'formatexpr' isn't set or it returns non-zero. */
5816#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005817 int do_internal = TRUE;
5818
Bram Moolenaar81a82092008-03-12 16:27:00 +00005819 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005820 {
5821 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5822 /* It may be required to save for undo again, e.g. when setline()
5823 * was called. */
5824 ins_need_undo = TRUE;
5825 }
5826 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005828 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005830
Bram Moolenaar071d4272004-06-13 20:20:40 +00005831 if (c == NUL) /* only formatting was wanted */
5832 return;
5833
5834#ifdef FEAT_COMMENTS
5835 /* Check whether this character should end a comment. */
5836 if (did_ai && (int)c == end_comment_pending)
5837 {
5838 char_u *line;
5839 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5840 int middle_len, end_len;
5841 int i;
5842
5843 /*
5844 * Need to remove existing (middle) comment leader and insert end
5845 * comment leader. First, check what comment leader we can find.
5846 */
5847 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5848 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5849 {
5850 /* Skip middle-comment string */
5851 while (*p && p[-1] != ':') /* find end of middle flags */
5852 ++p;
5853 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5854 /* Don't count trailing white space for middle_len */
5855 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5856 --middle_len;
5857
5858 /* Find the end-comment string */
5859 while (*p && p[-1] != ':') /* find end of end flags */
5860 ++p;
5861 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5862
5863 /* Skip white space before the cursor */
5864 i = curwin->w_cursor.col;
5865 while (--i >= 0 && vim_iswhite(line[i]))
5866 ;
5867 i++;
5868
5869 /* Skip to before the middle leader */
5870 i -= middle_len;
5871
5872 /* Check some expected things before we go on */
5873 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5874 {
5875 /* Backspace over all the stuff we want to replace */
5876 backspace_until_column(i);
5877
5878 /*
5879 * Insert the end-comment string, except for the last
5880 * character, which will get inserted as normal later.
5881 */
5882 ins_bytes_len(lead_end, end_len - 1);
5883 }
5884 }
5885 }
5886 end_comment_pending = NUL;
5887#endif
5888
5889 did_ai = FALSE;
5890#ifdef FEAT_SMARTINDENT
5891 did_si = FALSE;
5892 can_si = FALSE;
5893 can_si_back = FALSE;
5894#endif
5895
5896 /*
5897 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5898 * This speeds up normal text input considerably.
5899 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5900 * need to re-indent at a ':', or any other character (but not what
5901 * 'paste' is set)..
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005902 * Don't do this when there an InsertCharPre autocommand is defined,
5903 * because we need to fire the event for every character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005904 */
5905#ifdef USE_ON_FLY_SCROLL
5906 dont_scroll = FALSE; /* allow scrolling here */
5907#endif
5908
5909 if ( !ISSPECIAL(c)
5910#ifdef FEAT_MBYTE
5911 && (!has_mbyte || (*mb_char2len)(c) == 1)
5912#endif
5913 && vpeekc() != NUL
5914 && !(State & REPLACE_FLAG)
5915#ifdef FEAT_CINDENT
5916 && !cindent_on()
5917#endif
5918#ifdef FEAT_RIGHTLEFT
5919 && !p_ri
5920#endif
Bram Moolenaarf5876f12012-02-29 18:22:08 +01005921#ifdef FEAT_AUTOCMD
5922 && !has_insertcharpre()
5923#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005924 )
5925 {
5926#define INPUT_BUFLEN 100
5927 char_u buf[INPUT_BUFLEN + 1];
5928 int i;
5929 colnr_T virtcol = 0;
5930
5931 buf[0] = c;
5932 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005933 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005934 virtcol = get_nolist_virtcol();
5935 /*
5936 * Stop the string when:
5937 * - no more chars available
5938 * - finding a special character (command key)
5939 * - buffer is full
5940 * - running into the 'textwidth' boundary
5941 * - need to check for abbreviation: A non-word char after a word-char
5942 */
5943 while ( (c = vpeekc()) != NUL
5944 && !ISSPECIAL(c)
5945#ifdef FEAT_MBYTE
5946 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5947#endif
5948 && i < INPUT_BUFLEN
5949 && (textwidth == 0
5950 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5951 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5952 {
5953#ifdef FEAT_RIGHTLEFT
5954 c = vgetc();
5955 if (p_hkmap && KeyTyped)
5956 c = hkmap(c); /* Hebrew mode mapping */
5957# ifdef FEAT_FKMAP
5958 if (p_fkmap && KeyTyped)
5959 c = fkmap(c); /* Farsi mode mapping */
5960# endif
5961 buf[i++] = c;
5962#else
5963 buf[i++] = vgetc();
5964#endif
5965 }
5966
5967#ifdef FEAT_DIGRAPHS
5968 do_digraph(-1); /* clear digraphs */
5969 do_digraph(buf[i-1]); /* may be the start of a digraph */
5970#endif
5971 buf[i] = NUL;
5972 ins_str(buf);
5973 if (flags & INSCHAR_CTRLV)
5974 {
5975 redo_literal(*buf);
5976 i = 1;
5977 }
5978 else
5979 i = 0;
5980 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005981 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005982 }
5983 else
5984 {
5985#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005986 int cc;
5987
Bram Moolenaar071d4272004-06-13 20:20:40 +00005988 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5989 {
5990 char_u buf[MB_MAXBYTES + 1];
5991
5992 (*mb_char2bytes)(c, buf);
5993 buf[cc] = NUL;
5994 ins_char_bytes(buf, cc);
5995 AppendCharToRedobuff(c);
5996 }
5997 else
5998#endif
5999 {
6000 ins_char(c);
6001 if (flags & INSCHAR_CTRLV)
6002 redo_literal(c);
6003 else
6004 AppendCharToRedobuff(c);
6005 }
6006 }
6007}
6008
6009/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006010 * Format text at the current insert position.
6011 */
6012 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00006013internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006014 int textwidth;
6015 int second_indent;
6016 int flags;
6017 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006018 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006019{
6020 int cc;
6021 int save_char = NUL;
6022 int haveto_redraw = FALSE;
6023 int fo_ins_blank = has_format_option(FO_INS_BLANK);
6024#ifdef FEAT_MBYTE
6025 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
6026#endif
6027 int fo_white_par = has_format_option(FO_WHITE_PAR);
6028 int first_line = TRUE;
6029#ifdef FEAT_COMMENTS
6030 colnr_T leader_len;
6031 int no_leader = FALSE;
6032 int do_comments = (flags & INSCHAR_DO_COM);
6033#endif
6034
6035 /*
6036 * When 'ai' is off we don't want a space under the cursor to be
6037 * deleted. Replace it with an 'x' temporarily.
6038 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006039 if (!curbuf->b_p_ai
6040#ifdef FEAT_VREPLACE
6041 && !(State & VREPLACE_FLAG)
6042#endif
6043 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006044 {
6045 cc = gchar_cursor();
6046 if (vim_iswhite(cc))
6047 {
6048 save_char = cc;
6049 pchar_cursor('x');
6050 }
6051 }
6052
6053 /*
6054 * Repeat breaking lines, until the current line is not too long.
6055 */
6056 while (!got_int)
6057 {
6058 int startcol; /* Cursor column at entry */
6059 int wantcol; /* column at textwidth border */
6060 int foundcol; /* column for start of spaces */
6061 int end_foundcol = 0; /* column for start of word */
6062 colnr_T len;
6063 colnr_T virtcol;
6064#ifdef FEAT_VREPLACE
6065 int orig_col = 0;
6066 char_u *saved_text = NULL;
6067#endif
6068 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006069 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006070
Bram Moolenaar97b98102009-11-17 16:41:01 +00006071 virtcol = get_nolist_virtcol()
6072 + char2cells(c != NUL ? c : gchar_cursor());
6073 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006074 break;
6075
6076#ifdef FEAT_COMMENTS
6077 if (no_leader)
6078 do_comments = FALSE;
6079 else if (!(flags & INSCHAR_FORMAT)
6080 && has_format_option(FO_WRAP_COMS))
6081 do_comments = TRUE;
6082
6083 /* Don't break until after the comment leader */
6084 if (do_comments)
6085 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
6086 else
6087 leader_len = 0;
6088
6089 /* If the line doesn't start with a comment leader, then don't
6090 * start one in a following broken line. Avoids that a %word
6091 * moved to the start of the next line causes all following lines
6092 * to start with %. */
6093 if (leader_len == 0)
6094 no_leader = TRUE;
6095#endif
6096 if (!(flags & INSCHAR_FORMAT)
6097#ifdef FEAT_COMMENTS
6098 && leader_len == 0
6099#endif
6100 && !has_format_option(FO_WRAP))
6101
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006102 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006103 if ((startcol = curwin->w_cursor.col) == 0)
6104 break;
6105
6106 /* find column of textwidth border */
6107 coladvance((colnr_T)textwidth);
6108 wantcol = curwin->w_cursor.col;
6109
Bram Moolenaar97b98102009-11-17 16:41:01 +00006110 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006111 foundcol = 0;
6112
6113 /*
6114 * Find position to break at.
6115 * Stop at first entered white when 'formatoptions' has 'v'
6116 */
6117 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
Bram Moolenaara27ad5a2011-10-26 17:04:29 +02006118 || (flags & INSCHAR_FORMAT)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006119 || curwin->w_cursor.lnum != Insstart.lnum
6120 || curwin->w_cursor.col >= Insstart.col)
6121 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00006122 if (curwin->w_cursor.col == startcol && c != NUL)
6123 cc = c;
6124 else
6125 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006126 if (WHITECHAR(cc))
6127 {
6128 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006129 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006130
6131 /* find start of sequence of blanks */
6132 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
6133 {
6134 dec_cursor();
6135 cc = gchar_cursor();
6136 }
6137 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
6138 break; /* only spaces in front of text */
6139#ifdef FEAT_COMMENTS
6140 /* Don't break until after the comment leader */
6141 if (curwin->w_cursor.col < leader_len)
6142 break;
6143#endif
6144 if (has_format_option(FO_ONE_LETTER))
6145 {
6146 /* do not break after one-letter words */
6147 if (curwin->w_cursor.col == 0)
6148 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006149#ifdef FEAT_COMMENTS
6150 /* do not break "#a b" when 'tw' is 2 */
6151 if (curwin->w_cursor.col <= leader_len)
6152 break;
6153#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006154 col = curwin->w_cursor.col;
6155 dec_cursor();
6156 cc = gchar_cursor();
6157
6158 if (WHITECHAR(cc))
6159 continue; /* one-letter, continue */
6160 curwin->w_cursor.col = col;
6161 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00006162
6163 inc_cursor();
6164
6165 end_foundcol = end_col + 1;
6166 foundcol = curwin->w_cursor.col;
6167 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006168 break;
6169 }
6170#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00006171 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006172 {
6173 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00006174 if (curwin->w_cursor.col != startcol)
6175 {
6176#ifdef FEAT_COMMENTS
6177 /* Don't break until after the comment leader */
6178 if (curwin->w_cursor.col < leader_len)
6179 break;
6180#endif
6181 col = curwin->w_cursor.col;
6182 inc_cursor();
6183 /* Don't change end_foundcol if already set. */
6184 if (foundcol != curwin->w_cursor.col)
6185 {
6186 foundcol = curwin->w_cursor.col;
6187 end_foundcol = foundcol;
6188 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6189 break;
6190 }
6191 curwin->w_cursor.col = col;
6192 }
6193
6194 if (curwin->w_cursor.col == 0)
6195 break;
6196
6197 col = curwin->w_cursor.col;
6198
6199 dec_cursor();
6200 cc = gchar_cursor();
6201
6202 if (WHITECHAR(cc))
6203 continue; /* break with space */
6204#ifdef FEAT_COMMENTS
6205 /* Don't break until after the comment leader */
6206 if (curwin->w_cursor.col < leader_len)
6207 break;
6208#endif
6209
6210 curwin->w_cursor.col = col;
6211
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006212 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006213 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006214 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6215 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006216 }
6217#endif
6218 if (curwin->w_cursor.col == 0)
6219 break;
6220 dec_cursor();
6221 }
6222
6223 if (foundcol == 0) /* no spaces, cannot break line */
6224 {
6225 curwin->w_cursor.col = startcol;
6226 break;
6227 }
6228
6229 /* Going to break the line, remove any "$" now. */
6230 undisplay_dollar();
6231
6232 /*
6233 * Offset between cursor position and line break is used by replace
6234 * stack functions. VREPLACE does not use this, and backspaces
6235 * over the text instead.
6236 */
6237#ifdef FEAT_VREPLACE
6238 if (State & VREPLACE_FLAG)
6239 orig_col = startcol; /* Will start backspacing from here */
6240 else
6241#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006242 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006243
6244 /*
6245 * adjust startcol for spaces that will be deleted and
6246 * characters that will remain on top line
6247 */
6248 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006249 while ((cc = gchar_cursor(), WHITECHAR(cc))
6250 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006251 inc_cursor();
6252 startcol -= curwin->w_cursor.col;
6253 if (startcol < 0)
6254 startcol = 0;
6255
6256#ifdef FEAT_VREPLACE
6257 if (State & VREPLACE_FLAG)
6258 {
6259 /*
6260 * In VREPLACE mode, we will backspace over the text to be
6261 * wrapped, so save a copy now to put on the next line.
6262 */
6263 saved_text = vim_strsave(ml_get_cursor());
6264 curwin->w_cursor.col = orig_col;
6265 if (saved_text == NULL)
6266 break; /* Can't do it, out of memory */
6267 saved_text[startcol] = NUL;
6268
6269 /* Backspace over characters that will move to the next line */
6270 if (!fo_white_par)
6271 backspace_until_column(foundcol);
6272 }
6273 else
6274#endif
6275 {
6276 /* put cursor after pos. to break line */
6277 if (!fo_white_par)
6278 curwin->w_cursor.col = foundcol;
6279 }
6280
6281 /*
6282 * Split the line just before the margin.
6283 * Only insert/delete lines, but don't really redraw the window.
6284 */
6285 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6286 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6287#ifdef FEAT_COMMENTS
6288 + (do_comments ? OPENLINE_DO_COM : 0)
6289#endif
6290 , old_indent);
6291 old_indent = 0;
6292
6293 replace_offset = 0;
6294 if (first_line)
6295 {
6296 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6297 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6298 if (second_indent >= 0)
6299 {
6300#ifdef FEAT_VREPLACE
6301 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006302 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006303 else
6304#endif
6305 (void)set_indent(second_indent, SIN_CHANGED);
6306 }
6307 first_line = FALSE;
6308 }
6309
6310#ifdef FEAT_VREPLACE
6311 if (State & VREPLACE_FLAG)
6312 {
6313 /*
6314 * In VREPLACE mode we have backspaced over the text to be
6315 * moved, now we re-insert it into the new line.
6316 */
6317 ins_bytes(saved_text);
6318 vim_free(saved_text);
6319 }
6320 else
6321#endif
6322 {
6323 /*
6324 * Check if cursor is not past the NUL off the line, cindent
6325 * may have added or removed indent.
6326 */
6327 curwin->w_cursor.col += startcol;
6328 len = (colnr_T)STRLEN(ml_get_curline());
6329 if (curwin->w_cursor.col > len)
6330 curwin->w_cursor.col = len;
6331 }
6332
6333 haveto_redraw = TRUE;
6334#ifdef FEAT_CINDENT
6335 can_cindent = TRUE;
6336#endif
6337 /* moved the cursor, don't autoindent or cindent now */
6338 did_ai = FALSE;
6339#ifdef FEAT_SMARTINDENT
6340 did_si = FALSE;
6341 can_si = FALSE;
6342 can_si_back = FALSE;
6343#endif
6344 line_breakcheck();
6345 }
6346
6347 if (save_char != NUL) /* put back space after cursor */
6348 pchar_cursor(save_char);
6349
6350 if (!format_only && haveto_redraw)
6351 {
6352 update_topline();
6353 redraw_curbuf_later(VALID);
6354 }
6355}
6356
6357/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 * Called after inserting or deleting text: When 'formatoptions' includes the
6359 * 'a' flag format from the current line until the end of the paragraph.
6360 * Keep the cursor at the same position relative to the text.
6361 * The caller must have saved the cursor line for undo, following ones will be
6362 * saved here.
6363 */
6364 void
6365auto_format(trailblank, prev_line)
6366 int trailblank; /* when TRUE also format with trailing blank */
6367 int prev_line; /* may start in previous line */
6368{
6369 pos_T pos;
6370 colnr_T len;
6371 char_u *old;
6372 char_u *new, *pnew;
6373 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006374 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006375
6376 if (!has_format_option(FO_AUTO))
6377 return;
6378
6379 pos = curwin->w_cursor;
6380 old = ml_get_curline();
6381
6382 /* may remove added space */
6383 check_auto_format(FALSE);
6384
6385 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6386 * user might insert normal text next. Also skip formatting when "1" is
6387 * in 'formatoptions' and there is a single character before the cursor.
6388 * Otherwise the line would be broken and when typing another non-white
6389 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006390 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 if (*old != NUL && !trailblank && wasatend)
6392 {
6393 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006394 cc = gchar_cursor();
6395 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6396 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006397 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006398 cc = gchar_cursor();
6399 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006400 {
6401 curwin->w_cursor = pos;
6402 return;
6403 }
6404 curwin->w_cursor = pos;
6405 }
6406
6407#ifdef FEAT_COMMENTS
6408 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6409 * comments. */
6410 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6411 && get_leader_len(old, NULL, FALSE) == 0)
6412 return;
6413#endif
6414
6415 /*
6416 * May start formatting in a previous line, so that after "x" a word is
6417 * moved to the previous line if it fits there now. Only when this is not
6418 * the start of a paragraph.
6419 */
6420 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6421 {
6422 --curwin->w_cursor.lnum;
6423 if (u_save_cursor() == FAIL)
6424 return;
6425 }
6426
6427 /*
6428 * Do the formatting and restore the cursor position. "saved_cursor" will
6429 * be adjusted for the text formatting.
6430 */
6431 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006432 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 curwin->w_cursor = saved_cursor;
6434 saved_cursor.lnum = 0;
6435
6436 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6437 {
6438 /* "cannot happen" */
6439 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6440 coladvance((colnr_T)MAXCOL);
6441 }
6442 else
6443 check_cursor_col();
6444
6445 /* Insert mode: If the cursor is now after the end of the line while it
6446 * previously wasn't, the line was broken. Because of the rule above we
6447 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6448 * formatted. */
6449 if (!wasatend && has_format_option(FO_WHITE_PAR))
6450 {
6451 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006452 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 if (curwin->w_cursor.col == len)
6454 {
6455 pnew = vim_strnsave(new, len + 2);
6456 pnew[len] = ' ';
6457 pnew[len + 1] = NUL;
6458 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6459 /* remove the space later */
6460 did_add_space = TRUE;
6461 }
6462 else
6463 /* may remove added space */
6464 check_auto_format(FALSE);
6465 }
6466
6467 check_cursor();
6468}
6469
6470/*
6471 * When an extra space was added to continue a paragraph for auto-formatting,
6472 * delete it now. The space must be under the cursor, just after the insert
6473 * position.
6474 */
6475 static void
6476check_auto_format(end_insert)
6477 int end_insert; /* TRUE when ending Insert mode */
6478{
6479 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006480 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006481
6482 if (did_add_space)
6483 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006484 cc = gchar_cursor();
6485 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006486 /* Somehow the space was removed already. */
6487 did_add_space = FALSE;
6488 else
6489 {
6490 if (!end_insert)
6491 {
6492 inc_cursor();
6493 c = gchar_cursor();
6494 dec_cursor();
6495 }
6496 if (c != NUL)
6497 {
6498 /* The space is no longer at the end of the line, delete it. */
6499 del_char(FALSE);
6500 did_add_space = FALSE;
6501 }
6502 }
6503 }
6504}
6505
6506/*
6507 * Find out textwidth to be used for formatting:
6508 * if 'textwidth' option is set, use it
6509 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6510 * if invalid value, use 0.
6511 * Set default to window width (maximum 79) for "gq" operator.
6512 */
6513 int
6514comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006515 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006516{
6517 int textwidth;
6518
6519 textwidth = curbuf->b_p_tw;
6520 if (textwidth == 0 && curbuf->b_p_wm)
6521 {
6522 /* The width is the window width minus 'wrapmargin' minus all the
6523 * things that add to the margin. */
6524 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6525#ifdef FEAT_CMDWIN
6526 if (cmdwin_type != 0)
6527 textwidth -= 1;
6528#endif
6529#ifdef FEAT_FOLDING
6530 textwidth -= curwin->w_p_fdc;
6531#endif
6532#ifdef FEAT_SIGNS
6533 if (curwin->w_buffer->b_signlist != NULL
6534# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006535 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006536# endif
6537 )
6538 textwidth -= 1;
6539#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006540 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006541 textwidth -= 8;
6542 }
6543 if (textwidth < 0)
6544 textwidth = 0;
6545 if (ff && textwidth == 0)
6546 {
6547 textwidth = W_WIDTH(curwin) - 1;
6548 if (textwidth > 79)
6549 textwidth = 79;
6550 }
6551 return textwidth;
6552}
6553
6554/*
6555 * Put a character in the redo buffer, for when just after a CTRL-V.
6556 */
6557 static void
6558redo_literal(c)
6559 int c;
6560{
6561 char_u buf[10];
6562
6563 /* Only digits need special treatment. Translate them into a string of
6564 * three digits. */
6565 if (VIM_ISDIGIT(c))
6566 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006567 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006568 AppendToRedobuff(buf);
6569 }
6570 else
6571 AppendCharToRedobuff(c);
6572}
6573
6574/*
6575 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006576 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006577 */
6578 static void
6579start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006580 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006581{
6582 if (!arrow_used) /* something has been inserted */
6583 {
6584 AppendToRedobuff(ESC_STR);
6585 stop_insert(end_insert_pos, FALSE);
6586 arrow_used = TRUE; /* this means we stopped the current insert */
6587 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006588#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006589 check_spell_redraw();
6590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591}
6592
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006593#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006594/*
6595 * If we skipped highlighting word at cursor, do it now.
6596 * It may be skipped again, thus reset spell_redraw_lnum first.
6597 */
6598 static void
6599check_spell_redraw()
6600{
6601 if (spell_redraw_lnum != 0)
6602 {
6603 linenr_T lnum = spell_redraw_lnum;
6604
6605 spell_redraw_lnum = 0;
6606 redrawWinline(lnum, FALSE);
6607 }
6608}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006609
6610/*
6611 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6612 * spelled word, if there is one.
6613 */
6614 static void
6615spell_back_to_badword()
6616{
6617 pos_T tpos = curwin->w_cursor;
6618
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006619 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006620 if (curwin->w_cursor.col != tpos.col)
6621 start_arrow(&tpos);
6622}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006623#endif
6624
Bram Moolenaar071d4272004-06-13 20:20:40 +00006625/*
6626 * stop_arrow() is called before a change is made in insert mode.
6627 * If an arrow key has been used, start a new insertion.
6628 * Returns FAIL if undo is impossible, shouldn't insert then.
6629 */
6630 int
6631stop_arrow()
6632{
6633 if (arrow_used)
6634 {
6635 if (u_save_cursor() == OK)
6636 {
6637 arrow_used = FALSE;
6638 ins_need_undo = FALSE;
6639 }
6640 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006641 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006642 ai_col = 0;
6643#ifdef FEAT_VREPLACE
6644 if (State & VREPLACE_FLAG)
6645 {
6646 orig_line_count = curbuf->b_ml.ml_line_count;
6647 vr_lines_changed = 1;
6648 }
6649#endif
6650 ResetRedobuff();
6651 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006652 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653 }
6654 else if (ins_need_undo)
6655 {
6656 if (u_save_cursor() == OK)
6657 ins_need_undo = FALSE;
6658 }
6659
6660#ifdef FEAT_FOLDING
6661 /* Always open fold at the cursor line when inserting something. */
6662 foldOpenCursor();
6663#endif
6664
6665 return (arrow_used || ins_need_undo ? FAIL : OK);
6666}
6667
6668/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006669 * Do a few things to stop inserting.
6670 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6671 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006672 */
6673 static void
6674stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006675 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006676 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006677{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006678 int cc;
6679 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006680
6681 stop_redo_ins();
6682 replace_flush(); /* abandon replace stack */
6683
6684 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006685 * Save the inserted text for later redo with ^@ and CTRL-A.
6686 * Don't do it when "restart_edit" was set and nothing was inserted,
6687 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006688 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006689 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006690 if (did_restart_edit == 0 || (ptr != NULL
6691 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006692 {
6693 vim_free(last_insert);
6694 last_insert = ptr;
6695 last_insert_skip = new_insert_skip;
6696 }
6697 else
6698 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006699
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006700 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006701 {
6702 /* Auto-format now. It may seem strange to do this when stopping an
6703 * insertion (or moving the cursor), but it's required when appending
6704 * a line and having it end in a space. But only do it when something
6705 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006706 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006708 pos_T tpos = curwin->w_cursor;
6709
Bram Moolenaar071d4272004-06-13 20:20:40 +00006710 /* When the cursor is at the end of the line after a space the
6711 * formatting will move it to the following word. Avoid that by
6712 * moving the cursor onto the space. */
6713 cc = 'x';
6714 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6715 {
6716 dec_cursor();
6717 cc = gchar_cursor();
6718 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006719 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006720 }
6721
6722 auto_format(TRUE, FALSE);
6723
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006724 if (vim_iswhite(cc))
6725 {
6726 if (gchar_cursor() != NUL)
6727 inc_cursor();
6728#ifdef FEAT_VIRTUALEDIT
6729 /* If the cursor is still at the same character, also keep
6730 * the "coladd". */
6731 if (gchar_cursor() == NUL
6732 && curwin->w_cursor.lnum == tpos.lnum
6733 && curwin->w_cursor.col == tpos.col)
6734 curwin->w_cursor.coladd = tpos.coladd;
6735#endif
6736 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006737 }
6738
6739 /* If a space was inserted for auto-formatting, remove it now. */
6740 check_auto_format(TRUE);
6741
6742 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006743 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006744 * Do this when ESC was used or moving the cursor up/down.
6745 * Check for the old position still being valid, just in case the text
6746 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006747 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006748 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6749 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006751 pos_T tpos = curwin->w_cursor;
6752
6753 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006754 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006755 for (;;)
6756 {
6757 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6758 --curwin->w_cursor.col;
6759 cc = gchar_cursor();
6760 if (!vim_iswhite(cc))
6761 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006762 if (del_char(TRUE) == FAIL)
6763 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006764 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006765 if (curwin->w_cursor.lnum != tpos.lnum)
6766 curwin->w_cursor = tpos;
6767 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006768 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6769
6770#ifdef FEAT_VISUAL
6771 /* <C-S-Right> may have started Visual mode, adjust the position for
6772 * deleted characters. */
6773 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6774 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006775 int len = (int)STRLEN(ml_get_curline());
6776
6777 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006778 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006779 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006780# ifdef FEAT_VIRTUALEDIT
6781 VIsual.coladd = 0;
6782# endif
6783 }
6784 }
6785#endif
6786 }
6787 }
6788 did_ai = FALSE;
6789#ifdef FEAT_SMARTINDENT
6790 did_si = FALSE;
6791 can_si = FALSE;
6792 can_si_back = FALSE;
6793#endif
6794
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006795 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6796 * now in a different buffer. */
6797 if (end_insert_pos != NULL)
6798 {
6799 curbuf->b_op_start = Insstart;
6800 curbuf->b_op_end = *end_insert_pos;
6801 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802}
6803
6804/*
6805 * Set the last inserted text to a single character.
6806 * Used for the replace command.
6807 */
6808 void
6809set_last_insert(c)
6810 int c;
6811{
6812 char_u *s;
6813
6814 vim_free(last_insert);
6815#ifdef FEAT_MBYTE
6816 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6817#else
6818 last_insert = alloc(6);
6819#endif
6820 if (last_insert != NULL)
6821 {
6822 s = last_insert;
6823 /* Use the CTRL-V only when entering a special char */
6824 if (c < ' ' || c == DEL)
6825 *s++ = Ctrl_V;
6826 s = add_char2buf(c, s);
6827 *s++ = ESC;
6828 *s++ = NUL;
6829 last_insert_skip = 0;
6830 }
6831}
6832
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006833#if defined(EXITFREE) || defined(PROTO)
6834 void
6835free_last_insert()
6836{
6837 vim_free(last_insert);
6838 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006839# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006840 vim_free(compl_orig_text);
6841 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006842# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006843}
6844#endif
6845
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846/*
6847 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6848 * and CSI. Handle multi-byte characters.
6849 * Returns a pointer to after the added bytes.
6850 */
6851 char_u *
6852add_char2buf(c, s)
6853 int c;
6854 char_u *s;
6855{
6856#ifdef FEAT_MBYTE
6857 char_u temp[MB_MAXBYTES];
6858 int i;
6859 int len;
6860
6861 len = (*mb_char2bytes)(c, temp);
6862 for (i = 0; i < len; ++i)
6863 {
6864 c = temp[i];
6865#endif
6866 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6867 if (c == K_SPECIAL)
6868 {
6869 *s++ = K_SPECIAL;
6870 *s++ = KS_SPECIAL;
6871 *s++ = KE_FILLER;
6872 }
6873#ifdef FEAT_GUI
6874 else if (c == CSI)
6875 {
6876 *s++ = CSI;
6877 *s++ = KS_EXTRA;
6878 *s++ = (int)KE_CSI;
6879 }
6880#endif
6881 else
6882 *s++ = c;
6883#ifdef FEAT_MBYTE
6884 }
6885#endif
6886 return s;
6887}
6888
6889/*
6890 * move cursor to start of line
6891 * if flags & BL_WHITE move to first non-white
6892 * if flags & BL_SOL move to first non-white if startofline is set,
6893 * otherwise keep "curswant" column
6894 * if flags & BL_FIX don't leave the cursor on a NUL.
6895 */
6896 void
6897beginline(flags)
6898 int flags;
6899{
6900 if ((flags & BL_SOL) && !p_sol)
6901 coladvance(curwin->w_curswant);
6902 else
6903 {
6904 curwin->w_cursor.col = 0;
6905#ifdef FEAT_VIRTUALEDIT
6906 curwin->w_cursor.coladd = 0;
6907#endif
6908
6909 if (flags & (BL_WHITE | BL_SOL))
6910 {
6911 char_u *ptr;
6912
6913 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6914 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6915 ++curwin->w_cursor.col;
6916 }
6917 curwin->w_set_curswant = TRUE;
6918 }
6919}
6920
6921/*
6922 * oneright oneleft cursor_down cursor_up
6923 *
6924 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006925 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006926 * Return OK when successful, FAIL when we hit a line of file boundary.
6927 */
6928
6929 int
6930oneright()
6931{
6932 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006933 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006934
6935#ifdef FEAT_VIRTUALEDIT
6936 if (virtual_active())
6937 {
6938 pos_T prevpos = curwin->w_cursor;
6939
6940 /* Adjust for multi-wide char (excluding TAB) */
6941 ptr = ml_get_cursor();
6942 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006943# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006944 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006945# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006946 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006947# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006948 ))
6949 ? ptr2cells(ptr) : 1));
6950 curwin->w_set_curswant = TRUE;
6951 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6952 return (prevpos.col != curwin->w_cursor.col
6953 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6954 }
6955#endif
6956
6957 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006958 if (*ptr == NUL)
6959 return FAIL; /* already at the very end */
6960
Bram Moolenaar071d4272004-06-13 20:20:40 +00006961#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006962 if (has_mbyte)
6963 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006964 else
6965#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006966 l = 1;
6967
6968 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6969 * contains "onemore". */
6970 if (ptr[l] == NUL
6971#ifdef FEAT_VIRTUALEDIT
6972 && (ve_flags & VE_ONEMORE) == 0
6973#endif
6974 )
6975 return FAIL;
6976 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
6978 curwin->w_set_curswant = TRUE;
6979 return OK;
6980}
6981
6982 int
6983oneleft()
6984{
6985#ifdef FEAT_VIRTUALEDIT
6986 if (virtual_active())
6987 {
6988 int width;
6989 int v = getviscol();
6990
6991 if (v == 0)
6992 return FAIL;
6993
6994# ifdef FEAT_LINEBREAK
6995 /* We might get stuck on 'showbreak', skip over it. */
6996 width = 1;
6997 for (;;)
6998 {
6999 coladvance(v - width);
7000 /* getviscol() is slow, skip it when 'showbreak' is empty and
7001 * there are no multi-byte characters */
7002 if ((*p_sbr == NUL
7003# ifdef FEAT_MBYTE
7004 && !has_mbyte
7005# endif
7006 ) || getviscol() < v)
7007 break;
7008 ++width;
7009 }
7010# else
7011 coladvance(v - 1);
7012# endif
7013
7014 if (curwin->w_cursor.coladd == 1)
7015 {
7016 char_u *ptr;
7017
7018 /* Adjust for multi-wide char (not a TAB) */
7019 ptr = ml_get_cursor();
7020 if (*ptr != TAB && vim_isprintc(
7021# ifdef FEAT_MBYTE
7022 (*mb_ptr2char)(ptr)
7023# else
7024 *ptr
7025# endif
7026 ) && ptr2cells(ptr) > 1)
7027 curwin->w_cursor.coladd = 0;
7028 }
7029
7030 curwin->w_set_curswant = TRUE;
7031 return OK;
7032 }
7033#endif
7034
7035 if (curwin->w_cursor.col == 0)
7036 return FAIL;
7037
7038 curwin->w_set_curswant = TRUE;
7039 --curwin->w_cursor.col;
7040
7041#ifdef FEAT_MBYTE
7042 /* if the character on the left of the current cursor is a multi-byte
7043 * character, move to its first byte */
7044 if (has_mbyte)
7045 mb_adjust_cursor();
7046#endif
7047 return OK;
7048}
7049
7050 int
7051cursor_up(n, upd_topline)
7052 long n;
7053 int upd_topline; /* When TRUE: update topline */
7054{
7055 linenr_T lnum;
7056
7057 if (n > 0)
7058 {
7059 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00007060 /* This fails if the cursor is already in the first line or the count
7061 * is larger than the line number and '-' is in 'cpoptions' */
7062 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007063 return FAIL;
7064 if (n >= lnum)
7065 lnum = 1;
7066 else
7067#ifdef FEAT_FOLDING
7068 if (hasAnyFolding(curwin))
7069 {
7070 /*
7071 * Count each sequence of folded lines as one logical line.
7072 */
7073 /* go to the the start of the current fold */
7074 (void)hasFolding(lnum, &lnum, NULL);
7075
7076 while (n--)
7077 {
7078 /* move up one line */
7079 --lnum;
7080 if (lnum <= 1)
7081 break;
7082 /* If we entered a fold, move to the beginning, unless in
7083 * Insert mode or when 'foldopen' contains "all": it will open
7084 * in a moment. */
7085 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
7086 (void)hasFolding(lnum, &lnum, NULL);
7087 }
7088 if (lnum < 1)
7089 lnum = 1;
7090 }
7091 else
7092#endif
7093 lnum -= n;
7094 curwin->w_cursor.lnum = lnum;
7095 }
7096
7097 /* try to advance to the column we want to be at */
7098 coladvance(curwin->w_curswant);
7099
7100 if (upd_topline)
7101 update_topline(); /* make sure curwin->w_topline is valid */
7102
7103 return OK;
7104}
7105
7106/*
7107 * Cursor down a number of logical lines.
7108 */
7109 int
7110cursor_down(n, upd_topline)
7111 long n;
7112 int upd_topline; /* When TRUE: update topline */
7113{
7114 linenr_T lnum;
7115
7116 if (n > 0)
7117 {
7118 lnum = curwin->w_cursor.lnum;
7119#ifdef FEAT_FOLDING
7120 /* Move to last line of fold, will fail if it's the end-of-file. */
7121 (void)hasFolding(lnum, NULL, &lnum);
7122#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00007123 /* This fails if the cursor is already in the last line or would move
7124 * beyound the last line and '-' is in 'cpoptions' */
7125 if (lnum >= curbuf->b_ml.ml_line_count
7126 || (lnum + n > curbuf->b_ml.ml_line_count
7127 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007128 return FAIL;
7129 if (lnum + n >= curbuf->b_ml.ml_line_count)
7130 lnum = curbuf->b_ml.ml_line_count;
7131 else
7132#ifdef FEAT_FOLDING
7133 if (hasAnyFolding(curwin))
7134 {
7135 linenr_T last;
7136
7137 /* count each sequence of folded lines as one logical line */
7138 while (n--)
7139 {
7140 if (hasFolding(lnum, NULL, &last))
7141 lnum = last + 1;
7142 else
7143 ++lnum;
7144 if (lnum >= curbuf->b_ml.ml_line_count)
7145 break;
7146 }
7147 if (lnum > curbuf->b_ml.ml_line_count)
7148 lnum = curbuf->b_ml.ml_line_count;
7149 }
7150 else
7151#endif
7152 lnum += n;
7153 curwin->w_cursor.lnum = lnum;
7154 }
7155
7156 /* try to advance to the column we want to be at */
7157 coladvance(curwin->w_curswant);
7158
7159 if (upd_topline)
7160 update_topline(); /* make sure curwin->w_topline is valid */
7161
7162 return OK;
7163}
7164
7165/*
7166 * Stuff the last inserted text in the read buffer.
7167 * Last_insert actually is a copy of the redo buffer, so we
7168 * first have to remove the command.
7169 */
7170 int
7171stuff_inserted(c, count, no_esc)
7172 int c; /* Command character to be inserted */
7173 long count; /* Repeat this many times */
7174 int no_esc; /* Don't add an ESC at the end */
7175{
7176 char_u *esc_ptr;
7177 char_u *ptr;
7178 char_u *last_ptr;
7179 char_u last = NUL;
7180
7181 ptr = get_last_insert();
7182 if (ptr == NULL)
7183 {
7184 EMSG(_(e_noinstext));
7185 return FAIL;
7186 }
7187
7188 /* may want to stuff the command character, to start Insert mode */
7189 if (c != NUL)
7190 stuffcharReadbuff(c);
7191 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7192 *esc_ptr = NUL; /* remove the ESC */
7193
7194 /* when the last char is either "0" or "^" it will be quoted if no ESC
7195 * comes after it OR if it will inserted more than once and "ptr"
7196 * starts with ^D. -- Acevedo
7197 */
7198 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7199 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7200 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7201 {
7202 last = *last_ptr;
7203 *last_ptr = NUL;
7204 }
7205
7206 do
7207 {
7208 stuffReadbuff(ptr);
7209 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7210 if (last)
7211 stuffReadbuff((char_u *)(last == '0'
7212 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7213 : IF_EB("\026^", CTRL_V_STR "^")));
7214 }
7215 while (--count > 0);
7216
7217 if (last)
7218 *last_ptr = last;
7219
7220 if (esc_ptr != NULL)
7221 *esc_ptr = ESC; /* put the ESC back */
7222
7223 /* may want to stuff a trailing ESC, to get out of Insert mode */
7224 if (!no_esc)
7225 stuffcharReadbuff(ESC);
7226
7227 return OK;
7228}
7229
7230 char_u *
7231get_last_insert()
7232{
7233 if (last_insert == NULL)
7234 return NULL;
7235 return last_insert + last_insert_skip;
7236}
7237
7238/*
7239 * Get last inserted string, and remove trailing <Esc>.
7240 * Returns pointer to allocated memory (must be freed) or NULL.
7241 */
7242 char_u *
7243get_last_insert_save()
7244{
7245 char_u *s;
7246 int len;
7247
7248 if (last_insert == NULL)
7249 return NULL;
7250 s = vim_strsave(last_insert + last_insert_skip);
7251 if (s != NULL)
7252 {
7253 len = (int)STRLEN(s);
7254 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7255 s[len - 1] = NUL;
7256 }
7257 return s;
7258}
7259
7260/*
7261 * Check the word in front of the cursor for an abbreviation.
7262 * Called when the non-id character "c" has been entered.
7263 * When an abbreviation is recognized it is removed from the text and
7264 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7265 */
7266 static int
7267echeck_abbr(c)
7268 int c;
7269{
7270 /* Don't check for abbreviation in paste mode, when disabled and just
7271 * after moving around with cursor keys. */
7272 if (p_paste || no_abbr || arrow_used)
7273 return FALSE;
7274
7275 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7276 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7277}
7278
7279/*
7280 * replace-stack functions
7281 *
7282 * When replacing characters, the replaced characters are remembered for each
7283 * new character. This is used to re-insert the old text when backspacing.
7284 *
7285 * There is a NUL headed list of characters for each character that is
7286 * currently in the file after the insertion point. When BS is used, one NUL
7287 * headed list is put back for the deleted character.
7288 *
7289 * For a newline, there are two NUL headed lists. One contains the characters
7290 * that the NL replaced. The extra one stores the characters after the cursor
7291 * that were deleted (always white space).
7292 *
7293 * Replace_offset is normally 0, in which case replace_push will add a new
7294 * character at the end of the stack. If replace_offset is not 0, that many
7295 * characters will be left on the stack above the newly inserted character.
7296 */
7297
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007298static char_u *replace_stack = NULL;
7299static long replace_stack_nr = 0; /* next entry in replace stack */
7300static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007301
7302 void
7303replace_push(c)
7304 int c; /* character that is replaced (NUL is none) */
7305{
7306 char_u *p;
7307
7308 if (replace_stack_nr < replace_offset) /* nothing to do */
7309 return;
7310 if (replace_stack_len <= replace_stack_nr)
7311 {
7312 replace_stack_len += 50;
7313 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7314 if (p == NULL) /* out of memory */
7315 {
7316 replace_stack_len -= 50;
7317 return;
7318 }
7319 if (replace_stack != NULL)
7320 {
7321 mch_memmove(p, replace_stack,
7322 (size_t)(replace_stack_nr * sizeof(char_u)));
7323 vim_free(replace_stack);
7324 }
7325 replace_stack = p;
7326 }
7327 p = replace_stack + replace_stack_nr - replace_offset;
7328 if (replace_offset)
7329 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7330 *p = c;
7331 ++replace_stack_nr;
7332}
7333
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007334#if defined(FEAT_MBYTE) || defined(PROTO)
7335/*
7336 * Push a character onto the replace stack. Handles a multi-byte character in
7337 * reverse byte order, so that the first byte is popped off first.
7338 * Return the number of bytes done (includes composing characters).
7339 */
7340 int
7341replace_push_mb(p)
7342 char_u *p;
7343{
7344 int l = (*mb_ptr2len)(p);
7345 int j;
7346
7347 for (j = l - 1; j >= 0; --j)
7348 replace_push(p[j]);
7349 return l;
7350}
7351#endif
7352
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353/*
7354 * Pop one item from the replace stack.
7355 * return -1 if stack empty
7356 * return replaced character or NUL otherwise
7357 */
7358 static int
7359replace_pop()
7360{
7361 if (replace_stack_nr == 0)
7362 return -1;
7363 return (int)replace_stack[--replace_stack_nr];
7364}
7365
7366/*
7367 * Join the top two items on the replace stack. This removes to "off"'th NUL
7368 * encountered.
7369 */
7370 static void
7371replace_join(off)
7372 int off; /* offset for which NUL to remove */
7373{
7374 int i;
7375
7376 for (i = replace_stack_nr; --i >= 0; )
7377 if (replace_stack[i] == NUL && off-- <= 0)
7378 {
7379 --replace_stack_nr;
7380 mch_memmove(replace_stack + i, replace_stack + i + 1,
7381 (size_t)(replace_stack_nr - i));
7382 return;
7383 }
7384}
7385
7386/*
7387 * Pop bytes from the replace stack until a NUL is found, and insert them
7388 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7389 */
7390 static void
7391replace_pop_ins()
7392{
7393 int cc;
7394 int oldState = State;
7395
7396 State = NORMAL; /* don't want REPLACE here */
7397 while ((cc = replace_pop()) > 0)
7398 {
7399#ifdef FEAT_MBYTE
7400 mb_replace_pop_ins(cc);
7401#else
7402 ins_char(cc);
7403#endif
7404 dec_cursor();
7405 }
7406 State = oldState;
7407}
7408
7409#ifdef FEAT_MBYTE
7410/*
7411 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7412 * indicates a multi-byte char, pop the other bytes too.
7413 */
7414 static void
7415mb_replace_pop_ins(cc)
7416 int cc;
7417{
7418 int n;
7419 char_u buf[MB_MAXBYTES];
7420 int i;
7421 int c;
7422
7423 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7424 {
7425 buf[0] = cc;
7426 for (i = 1; i < n; ++i)
7427 buf[i] = replace_pop();
7428 ins_bytes_len(buf, n);
7429 }
7430 else
7431 ins_char(cc);
7432
7433 if (enc_utf8)
7434 /* Handle composing chars. */
7435 for (;;)
7436 {
7437 c = replace_pop();
7438 if (c == -1) /* stack empty */
7439 break;
7440 if ((n = MB_BYTE2LEN(c)) == 1)
7441 {
7442 /* Not a multi-byte char, put it back. */
7443 replace_push(c);
7444 break;
7445 }
7446 else
7447 {
7448 buf[0] = c;
7449 for (i = 1; i < n; ++i)
7450 buf[i] = replace_pop();
7451 if (utf_iscomposing(utf_ptr2char(buf)))
7452 ins_bytes_len(buf, n);
7453 else
7454 {
7455 /* Not a composing char, put it back. */
7456 for (i = n - 1; i >= 0; --i)
7457 replace_push(buf[i]);
7458 break;
7459 }
7460 }
7461 }
7462}
7463#endif
7464
7465/*
7466 * make the replace stack empty
7467 * (called when exiting replace mode)
7468 */
7469 static void
7470replace_flush()
7471{
7472 vim_free(replace_stack);
7473 replace_stack = NULL;
7474 replace_stack_len = 0;
7475 replace_stack_nr = 0;
7476}
7477
7478/*
7479 * Handle doing a BS for one character.
7480 * cc < 0: replace stack empty, just move cursor
7481 * cc == 0: character was inserted, delete it
7482 * cc > 0: character was replaced, put cc (first byte of original char) back
7483 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007484 * When "limit_col" is >= 0, don't delete before this column. Matters when
7485 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007486 */
7487 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007488replace_do_bs(limit_col)
7489 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007490{
7491 int cc;
7492#ifdef FEAT_VREPLACE
7493 int orig_len = 0;
7494 int ins_len;
7495 int orig_vcols = 0;
7496 colnr_T start_vcol;
7497 char_u *p;
7498 int i;
7499 int vcol;
7500#endif
7501
7502 cc = replace_pop();
7503 if (cc > 0)
7504 {
7505#ifdef FEAT_VREPLACE
7506 if (State & VREPLACE_FLAG)
7507 {
7508 /* Get the number of screen cells used by the character we are
7509 * going to delete. */
7510 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7511 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7512 }
7513#endif
7514#ifdef FEAT_MBYTE
7515 if (has_mbyte)
7516 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007517 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007518# ifdef FEAT_VREPLACE
7519 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007520 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007521# endif
7522 replace_push(cc);
7523 }
7524 else
7525#endif
7526 {
7527 pchar_cursor(cc);
7528#ifdef FEAT_VREPLACE
7529 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007530 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007531#endif
7532 }
7533 replace_pop_ins();
7534
7535#ifdef FEAT_VREPLACE
7536 if (State & VREPLACE_FLAG)
7537 {
7538 /* Get the number of screen cells used by the inserted characters */
7539 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007540 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007541 vcol = start_vcol;
7542 for (i = 0; i < ins_len; ++i)
7543 {
7544 vcol += chartabsize(p + i, vcol);
7545#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007546 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007547#endif
7548 }
7549 vcol -= start_vcol;
7550
7551 /* Delete spaces that were inserted after the cursor to keep the
7552 * text aligned. */
7553 curwin->w_cursor.col += ins_len;
7554 while (vcol > orig_vcols && gchar_cursor() == ' ')
7555 {
7556 del_char(FALSE);
7557 ++orig_vcols;
7558 }
7559 curwin->w_cursor.col -= ins_len;
7560 }
7561#endif
7562
7563 /* mark the buffer as changed and prepare for displaying */
7564 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7565 }
7566 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007567 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007568}
7569
7570#ifdef FEAT_CINDENT
7571/*
7572 * Return TRUE if C-indenting is on.
7573 */
7574 static int
7575cindent_on()
7576{
7577 return (!p_paste && (curbuf->b_p_cin
7578# ifdef FEAT_EVAL
7579 || *curbuf->b_p_inde != NUL
7580# endif
7581 ));
7582}
7583#endif
7584
7585#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7586/*
7587 * Re-indent the current line, based on the current contents of it and the
7588 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7589 * confused what all the part that handles Control-T is doing that I'm not.
7590 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7591 */
7592
7593 void
7594fixthisline(get_the_indent)
7595 int (*get_the_indent) __ARGS((void));
7596{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007597 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007598 if (linewhite(curwin->w_cursor.lnum))
7599 did_ai = TRUE; /* delete the indent if the line stays empty */
7600}
7601
7602 void
7603fix_indent()
7604{
7605 if (p_paste)
7606 return;
7607# ifdef FEAT_LISP
7608 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7609 fixthisline(get_lisp_indent);
7610# endif
7611# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7612 else
7613# endif
7614# ifdef FEAT_CINDENT
7615 if (cindent_on())
7616 do_c_expr_indent();
7617# endif
7618}
7619
7620#endif
7621
7622#ifdef FEAT_CINDENT
7623/*
7624 * return TRUE if 'cinkeys' contains the key "keytyped",
7625 * when == '*': Only if key is preceded with '*' (indent before insert)
7626 * when == '!': Only if key is prededed with '!' (don't insert)
7627 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7628 *
7629 * "keytyped" can have a few special values:
7630 * KEY_OPEN_FORW
7631 * KEY_OPEN_BACK
7632 * KEY_COMPLETE just finished completion.
7633 *
7634 * If line_is_empty is TRUE accept keys with '0' before them.
7635 */
7636 int
7637in_cinkeys(keytyped, when, line_is_empty)
7638 int keytyped;
7639 int when;
7640 int line_is_empty;
7641{
7642 char_u *look;
7643 int try_match;
7644 int try_match_word;
7645 char_u *p;
7646 char_u *line;
7647 int icase;
7648 int i;
7649
Bram Moolenaar30848942009-12-24 14:46:12 +00007650 if (keytyped == NUL)
7651 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7652 return FALSE;
7653
Bram Moolenaar071d4272004-06-13 20:20:40 +00007654#ifdef FEAT_EVAL
7655 if (*curbuf->b_p_inde != NUL)
7656 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7657 else
7658#endif
7659 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7660 while (*look)
7661 {
7662 /*
7663 * Find out if we want to try a match with this key, depending on
7664 * 'when' and a '*' or '!' before the key.
7665 */
7666 switch (when)
7667 {
7668 case '*': try_match = (*look == '*'); break;
7669 case '!': try_match = (*look == '!'); break;
7670 default: try_match = (*look != '*'); break;
7671 }
7672 if (*look == '*' || *look == '!')
7673 ++look;
7674
7675 /*
7676 * If there is a '0', only accept a match if the line is empty.
7677 * But may still match when typing last char of a word.
7678 */
7679 if (*look == '0')
7680 {
7681 try_match_word = try_match;
7682 if (!line_is_empty)
7683 try_match = FALSE;
7684 ++look;
7685 }
7686 else
7687 try_match_word = FALSE;
7688
7689 /*
7690 * does it look like a control character?
7691 */
7692 if (*look == '^'
7693#ifdef EBCDIC
7694 && (Ctrl_chr(look[1]) != 0)
7695#else
7696 && look[1] >= '?' && look[1] <= '_'
7697#endif
7698 )
7699 {
7700 if (try_match && keytyped == Ctrl_chr(look[1]))
7701 return TRUE;
7702 look += 2;
7703 }
7704 /*
7705 * 'o' means "o" command, open forward.
7706 * 'O' means "O" command, open backward.
7707 */
7708 else if (*look == 'o')
7709 {
7710 if (try_match && keytyped == KEY_OPEN_FORW)
7711 return TRUE;
7712 ++look;
7713 }
7714 else if (*look == 'O')
7715 {
7716 if (try_match && keytyped == KEY_OPEN_BACK)
7717 return TRUE;
7718 ++look;
7719 }
7720
7721 /*
7722 * 'e' means to check for "else" at start of line and just before the
7723 * cursor.
7724 */
7725 else if (*look == 'e')
7726 {
7727 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7728 {
7729 p = ml_get_curline();
7730 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7731 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7732 return TRUE;
7733 }
7734 ++look;
7735 }
7736
7737 /*
7738 * ':' only causes an indent if it is at the end of a label or case
7739 * statement, or when it was before typing the ':' (to fix
7740 * class::method for C++).
7741 */
7742 else if (*look == ':')
7743 {
7744 if (try_match && keytyped == ':')
7745 {
7746 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007747 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7748 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007750 /* Need to get the line again after cin_islabel(). */
7751 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007752 if (curwin->w_cursor.col > 2
7753 && p[curwin->w_cursor.col - 1] == ':'
7754 && p[curwin->w_cursor.col - 2] == ':')
7755 {
7756 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007757 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758 || cin_islabel(30));
7759 p = ml_get_curline();
7760 p[curwin->w_cursor.col - 1] = ':';
7761 if (i)
7762 return TRUE;
7763 }
7764 }
7765 ++look;
7766 }
7767
7768
7769 /*
7770 * Is it a key in <>, maybe?
7771 */
7772 else if (*look == '<')
7773 {
7774 if (try_match)
7775 {
7776 /*
7777 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7778 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7779 * >, *, : and ! keys if they really really want to.
7780 */
7781 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7782 && keytyped == look[1])
7783 return TRUE;
7784
7785 if (keytyped == get_special_key_code(look + 1))
7786 return TRUE;
7787 }
7788 while (*look && *look != '>')
7789 look++;
7790 while (*look == '>')
7791 look++;
7792 }
7793
7794 /*
7795 * Is it a word: "=word"?
7796 */
7797 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7798 {
7799 ++look;
7800 if (*look == '~')
7801 {
7802 icase = TRUE;
7803 ++look;
7804 }
7805 else
7806 icase = FALSE;
7807 p = vim_strchr(look, ',');
7808 if (p == NULL)
7809 p = look + STRLEN(look);
7810 if ((try_match || try_match_word)
7811 && curwin->w_cursor.col >= (colnr_T)(p - look))
7812 {
7813 int match = FALSE;
7814
7815#ifdef FEAT_INS_EXPAND
7816 if (keytyped == KEY_COMPLETE)
7817 {
7818 char_u *s;
7819
7820 /* Just completed a word, check if it starts with "look".
7821 * search back for the start of a word. */
7822 line = ml_get_curline();
7823# ifdef FEAT_MBYTE
7824 if (has_mbyte)
7825 {
7826 char_u *n;
7827
7828 for (s = line + curwin->w_cursor.col; s > line; s = n)
7829 {
7830 n = mb_prevptr(line, s);
7831 if (!vim_iswordp(n))
7832 break;
7833 }
7834 }
7835 else
7836# endif
7837 for (s = line + curwin->w_cursor.col; s > line; --s)
7838 if (!vim_iswordc(s[-1]))
7839 break;
7840 if (s + (p - look) <= line + curwin->w_cursor.col
7841 && (icase
7842 ? MB_STRNICMP(s, look, p - look)
7843 : STRNCMP(s, look, p - look)) == 0)
7844 match = TRUE;
7845 }
7846 else
7847#endif
7848 /* TODO: multi-byte */
7849 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7850 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7851 {
7852 line = ml_get_cursor();
7853 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7854 || !vim_iswordc(line[-(p - look) - 1]))
7855 && (icase
7856 ? MB_STRNICMP(line - (p - look), look, p - look)
7857 : STRNCMP(line - (p - look), look, p - look))
7858 == 0)
7859 match = TRUE;
7860 }
7861 if (match && try_match_word && !try_match)
7862 {
7863 /* "0=word": Check if there are only blanks before the
7864 * word. */
7865 line = ml_get_curline();
7866 if ((int)(skipwhite(line) - line) !=
7867 (int)(curwin->w_cursor.col - (p - look)))
7868 match = FALSE;
7869 }
7870 if (match)
7871 return TRUE;
7872 }
7873 look = p;
7874 }
7875
7876 /*
7877 * ok, it's a boring generic character.
7878 */
7879 else
7880 {
7881 if (try_match && *look == keytyped)
7882 return TRUE;
7883 ++look;
7884 }
7885
7886 /*
7887 * Skip over ", ".
7888 */
7889 look = skip_to_option_part(look);
7890 }
7891 return FALSE;
7892}
7893#endif /* FEAT_CINDENT */
7894
7895#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7896/*
7897 * Map Hebrew keyboard when in hkmap mode.
7898 */
7899 int
7900hkmap(c)
7901 int c;
7902{
7903 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7904 {
7905 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7906 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7907 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7908 static char_u map[26] =
7909 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7910 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7911 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7912 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7913 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7914 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7915 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7916 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7917 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7918
7919 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7920 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7921 /* '-1'='sofit' */
7922 else if (c == 'x')
7923 return 'X';
7924 else if (c == 'q')
7925 return '\''; /* {geresh}={'} */
7926 else if (c == 246)
7927 return ' '; /* \"o --> ' ' for a german keyboard */
7928 else if (c == 228)
7929 return ' '; /* \"a --> ' ' -- / -- */
7930 else if (c == 252)
7931 return ' '; /* \"u --> ' ' -- / -- */
7932#ifdef EBCDIC
7933 else if (islower(c))
7934#else
7935 /* NOTE: islower() does not do the right thing for us on Linux so we
7936 * do this the same was as 5.7 and previous, so it works correctly on
7937 * all systems. Specifically, the e.g. Delete and Arrow keys are
7938 * munged and won't work if e.g. searching for Hebrew text.
7939 */
7940 else if (c >= 'a' && c <= 'z')
7941#endif
7942 return (int)(map[CharOrdLow(c)] + p_aleph);
7943 else
7944 return c;
7945 }
7946 else
7947 {
7948 switch (c)
7949 {
7950 case '`': return ';';
7951 case '/': return '.';
7952 case '\'': return ',';
7953 case 'q': return '/';
7954 case 'w': return '\'';
7955
7956 /* Hebrew letters - set offset from 'a' */
7957 case ',': c = '{'; break;
7958 case '.': c = 'v'; break;
7959 case ';': c = 't'; break;
7960 default: {
7961 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7962
7963#ifdef EBCDIC
7964 /* see note about islower() above */
7965 if (!islower(c))
7966#else
7967 if (c < 'a' || c > 'z')
7968#endif
7969 return c;
7970 c = str[CharOrdLow(c)];
7971 break;
7972 }
7973 }
7974
7975 return (int)(CharOrdLow(c) + p_aleph);
7976 }
7977}
7978#endif
7979
7980 static void
7981ins_reg()
7982{
7983 int need_redraw = FALSE;
7984 int regname;
7985 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007986#ifdef FEAT_VISUAL
7987 int vis_active = VIsual_active;
7988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007989
7990 /*
7991 * If we are going to wait for a character, show a '"'.
7992 */
7993 pc_status = PC_STATUS_UNSET;
7994 if (redrawing() && !char_avail())
7995 {
7996 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007997 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998
7999 edit_putchar('"', TRUE);
8000#ifdef FEAT_CMDL_INFO
8001 add_to_showcmd_c(Ctrl_R);
8002#endif
8003 }
8004
8005#ifdef USE_ON_FLY_SCROLL
8006 dont_scroll = TRUE; /* disallow scrolling here */
8007#endif
8008
8009 /*
8010 * Don't map the register name. This also prevents the mode message to be
8011 * deleted when ESC is hit.
8012 */
8013 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008014 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
8017 {
8018 /* Get a third key for literal register insertion */
8019 literally = regname;
8020#ifdef FEAT_CMDL_INFO
8021 add_to_showcmd_c(literally);
8022#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008023 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025 }
8026 --no_mapping;
8027
8028#ifdef FEAT_EVAL
8029 /*
8030 * Don't call u_sync() while getting the expression,
8031 * evaluating it or giving an error message for it!
8032 */
8033 ++no_u_sync;
8034 if (regname == '=')
8035 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008036# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008038# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008040# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041 /* Restore the Input Method. */
8042 if (im_on)
8043 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008044# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008045 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00008046 if (regname == NUL || !valid_yank_reg(regname, FALSE))
8047 {
8048 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008049 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00008050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008051 else
8052 {
8053#endif
8054 if (literally == Ctrl_O || literally == Ctrl_P)
8055 {
8056 /* Append the command to the redo buffer. */
8057 AppendCharToRedobuff(Ctrl_R);
8058 AppendCharToRedobuff(literally);
8059 AppendCharToRedobuff(regname);
8060
8061 do_put(regname, BACKWARD, 1L,
8062 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
8063 }
8064 else if (insert_reg(regname, literally) == FAIL)
8065 {
8066 vim_beep();
8067 need_redraw = TRUE; /* remove the '"' */
8068 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00008069 else if (stop_insert_mode)
8070 /* When the '=' register was used and a function was invoked that
8071 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
8072 * insert anything, need to remove the '"' */
8073 need_redraw = TRUE;
8074
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075#ifdef FEAT_EVAL
8076 }
8077 --no_u_sync;
8078#endif
8079#ifdef FEAT_CMDL_INFO
8080 clear_showcmd();
8081#endif
8082
8083 /* If the inserted register is empty, we need to remove the '"' */
8084 if (need_redraw || stuff_empty())
8085 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00008086
8087#ifdef FEAT_VISUAL
8088 /* Disallow starting Visual mode here, would get a weird mode. */
8089 if (!vis_active && VIsual_active)
8090 end_visual_mode();
8091#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092}
8093
8094/*
8095 * CTRL-G commands in Insert mode.
8096 */
8097 static void
8098ins_ctrl_g()
8099{
8100 int c;
8101
8102#ifdef FEAT_INS_EXPAND
8103 /* Right after CTRL-X the cursor will be after the ruler. */
8104 setcursor();
8105#endif
8106
8107 /*
8108 * Don't map the second key. This also prevents the mode message to be
8109 * deleted when ESC is hit.
8110 */
8111 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00008112 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113 --no_mapping;
8114 switch (c)
8115 {
8116 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
8117 case K_UP:
8118 case Ctrl_K:
8119 case 'k': ins_up(TRUE);
8120 break;
8121
8122 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
8123 case K_DOWN:
8124 case Ctrl_J:
8125 case 'j': ins_down(TRUE);
8126 break;
8127
8128 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00008129 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008130 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008131
8132 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00008133 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00008134 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 break;
8136
8137 /* Unknown CTRL-G command, reserved for future expansion. */
8138 default: vim_beep();
8139 }
8140}
8141
8142/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008143 * CTRL-^ in Insert mode.
8144 */
8145 static void
8146ins_ctrl_hat()
8147{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00008148 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008149 {
8150 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
8151 if (State & LANGMAP)
8152 {
8153 curbuf->b_p_iminsert = B_IMODE_NONE;
8154 State &= ~LANGMAP;
8155 }
8156 else
8157 {
8158 curbuf->b_p_iminsert = B_IMODE_LMAP;
8159 State |= LANGMAP;
8160#ifdef USE_IM_CONTROL
8161 im_set_active(FALSE);
8162#endif
8163 }
8164 }
8165#ifdef USE_IM_CONTROL
8166 else
8167 {
8168 /* There are no ":lmap" mappings, toggle IM */
8169 if (im_get_status())
8170 {
8171 curbuf->b_p_iminsert = B_IMODE_NONE;
8172 im_set_active(FALSE);
8173 }
8174 else
8175 {
8176 curbuf->b_p_iminsert = B_IMODE_IM;
8177 State &= ~LANGMAP;
8178 im_set_active(TRUE);
8179 }
8180 }
8181#endif
8182 set_iminsert_global();
8183 showmode();
8184#ifdef FEAT_GUI
8185 /* may show different cursor shape or color */
8186 if (gui.in_use)
8187 gui_update_cursor(TRUE, FALSE);
8188#endif
8189#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8190 /* Show/unshow value of 'keymap' in status lines. */
8191 status_redraw_curbuf();
8192#endif
8193}
8194
8195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 * Handle ESC in insert mode.
8197 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8198 * insert.
8199 */
8200 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008201ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 long *count;
8203 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008204 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008205{
8206 int temp;
8207 static int disabled_redraw = FALSE;
8208
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008209#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008210 check_spell_redraw();
8211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008212#if defined(FEAT_HANGULIN)
8213# if defined(ESC_CHG_TO_ENG_MODE)
8214 hangul_input_state_set(0);
8215# endif
8216 if (composing_hangul)
8217 {
8218 push_raw_key(composing_hangul_buffer, 2);
8219 composing_hangul = 0;
8220 }
8221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008222
8223 temp = curwin->w_cursor.col;
8224 if (disabled_redraw)
8225 {
8226 --RedrawingDisabled;
8227 disabled_redraw = FALSE;
8228 }
8229 if (!arrow_used)
8230 {
8231 /*
8232 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008233 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8234 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 */
8236 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008237 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238
8239 /*
8240 * Repeating insert may take a long time. Check for
8241 * interrupt now and then.
8242 */
8243 if (*count > 0)
8244 {
8245 line_breakcheck();
8246 if (got_int)
8247 *count = 0;
8248 }
8249
8250 if (--*count > 0) /* repeat what was typed */
8251 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008252 /* Vi repeats the insert without replacing characters. */
8253 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8254 State &= ~REPLACE_FLAG;
8255
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 (void)start_redo_ins();
8257 if (cmdchar == 'r' || cmdchar == 'v')
8258 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8259 ++RedrawingDisabled;
8260 disabled_redraw = TRUE;
8261 return FALSE; /* repeat the insert */
8262 }
8263 stop_insert(&curwin->w_cursor, TRUE);
8264 undisplay_dollar();
8265 }
8266
8267 /* When an autoindent was removed, curswant stays after the
8268 * indent */
8269 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8270 curwin->w_set_curswant = TRUE;
8271
8272 /* Remember the last Insert position in the '^ mark. */
8273 if (!cmdmod.keepjumps)
8274 curbuf->b_last_insert = curwin->w_cursor;
8275
8276 /*
8277 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008278 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008280 if (!nomove
8281 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282#ifdef FEAT_VIRTUALEDIT
8283 || curwin->w_cursor.coladd > 0
8284#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008285 )
8286 && (restart_edit == NUL
8287 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008288#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008289 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008291 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292#ifdef FEAT_RIGHTLEFT
8293 && !revins_on
8294#endif
8295 )
8296 {
8297#ifdef FEAT_VIRTUALEDIT
8298 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8299 {
8300 oneleft();
8301 if (restart_edit != NUL)
8302 ++curwin->w_cursor.coladd;
8303 }
8304 else
8305#endif
8306 {
8307 --curwin->w_cursor.col;
8308#ifdef FEAT_MBYTE
8309 /* Correct cursor for multi-byte character. */
8310 if (has_mbyte)
8311 mb_adjust_cursor();
8312#endif
8313 }
8314 }
8315
8316#ifdef USE_IM_CONTROL
8317 /* Disable IM to allow typing English directly for Normal mode commands.
8318 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8319 * well). */
8320 if (!(State & LANGMAP))
8321 im_save_status(&curbuf->b_p_iminsert);
8322 im_set_active(FALSE);
8323#endif
8324
8325 State = NORMAL;
8326 /* need to position cursor again (e.g. when on a TAB ) */
8327 changed_cline_bef_curs();
8328
8329#ifdef FEAT_MOUSE
8330 setmouse();
8331#endif
8332#ifdef CURSOR_SHAPE
8333 ui_cursor_shape(); /* may show different cursor shape */
8334#endif
8335
8336 /*
8337 * When recording or for CTRL-O, need to display the new mode.
8338 * Otherwise remove the mode message.
8339 */
8340 if (Recording || restart_edit != NUL)
8341 showmode();
8342 else if (p_smd)
8343 MSG("");
8344
8345 return TRUE; /* exit Insert mode */
8346}
8347
8348#ifdef FEAT_RIGHTLEFT
8349/*
8350 * Toggle language: hkmap and revins_on.
8351 * Move to end of reverse inserted text.
8352 */
8353 static void
8354ins_ctrl_()
8355{
8356 if (revins_on && revins_chars && revins_scol >= 0)
8357 {
8358 while (gchar_cursor() != NUL && revins_chars--)
8359 ++curwin->w_cursor.col;
8360 }
8361 p_ri = !p_ri;
8362 revins_on = (State == INSERT && p_ri);
8363 if (revins_on)
8364 {
8365 revins_scol = curwin->w_cursor.col;
8366 revins_legal++;
8367 revins_chars = 0;
8368 undisplay_dollar();
8369 }
8370 else
8371 revins_scol = -1;
8372#ifdef FEAT_FKMAP
8373 if (p_altkeymap)
8374 {
8375 /*
8376 * to be consistent also for redo command, using '.'
8377 * set arrow_used to true and stop it - causing to redo
8378 * characters entered in one mode (normal/reverse insert).
8379 */
8380 arrow_used = TRUE;
8381 (void)stop_arrow();
8382 p_fkmap = curwin->w_p_rl ^ p_ri;
8383 if (p_fkmap && p_ri)
8384 State = INSERT;
8385 }
8386 else
8387#endif
8388 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8389 showmode();
8390}
8391#endif
8392
8393#ifdef FEAT_VISUAL
8394/*
8395 * If 'keymodel' contains "startsel", may start selection.
8396 * Returns TRUE when a CTRL-O and other keys stuffed.
8397 */
8398 static int
8399ins_start_select(c)
8400 int c;
8401{
8402 if (km_startsel)
8403 switch (c)
8404 {
8405 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 case K_PAGEUP:
8408 case K_KPAGEUP:
8409 case K_PAGEDOWN:
8410 case K_KPAGEDOWN:
8411# ifdef MACOS
8412 case K_LEFT:
8413 case K_RIGHT:
8414 case K_UP:
8415 case K_DOWN:
8416 case K_END:
8417 case K_HOME:
8418# endif
8419 if (!(mod_mask & MOD_MASK_SHIFT))
8420 break;
8421 /* FALLTHROUGH */
8422 case K_S_LEFT:
8423 case K_S_RIGHT:
8424 case K_S_UP:
8425 case K_S_DOWN:
8426 case K_S_END:
8427 case K_S_HOME:
8428 /* Start selection right away, the cursor can move with
8429 * CTRL-O when beyond the end of the line. */
8430 start_selection();
8431
8432 /* Execute the key in (insert) Select mode. */
8433 stuffcharReadbuff(Ctrl_O);
8434 if (mod_mask)
8435 {
8436 char_u buf[4];
8437
8438 buf[0] = K_SPECIAL;
8439 buf[1] = KS_MODIFIER;
8440 buf[2] = mod_mask;
8441 buf[3] = NUL;
8442 stuffReadbuff(buf);
8443 }
8444 stuffcharReadbuff(c);
8445 return TRUE;
8446 }
8447 return FALSE;
8448}
8449#endif
8450
8451/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008452 * <Insert> key in Insert mode: toggle insert/remplace mode.
8453 */
8454 static void
8455ins_insert(replaceState)
8456 int replaceState;
8457{
8458#ifdef FEAT_FKMAP
8459 if (p_fkmap && p_ri)
8460 {
8461 beep_flush();
8462 EMSG(farsi_text_3); /* encoded in Farsi */
8463 return;
8464 }
8465#endif
8466
8467#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008468# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008469 set_vim_var_string(VV_INSERTMODE,
8470 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008471# ifdef FEAT_VREPLACE
8472 replaceState == VREPLACE ? "v" :
8473# endif
8474 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008475# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008476 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8477#endif
8478 if (State & REPLACE_FLAG)
8479 State = INSERT | (State & LANGMAP);
8480 else
8481 State = replaceState | (State & LANGMAP);
8482 AppendCharToRedobuff(K_INS);
8483 showmode();
8484#ifdef CURSOR_SHAPE
8485 ui_cursor_shape(); /* may show different cursor shape */
8486#endif
8487}
8488
8489/*
8490 * Pressed CTRL-O in Insert mode.
8491 */
8492 static void
8493ins_ctrl_o()
8494{
8495#ifdef FEAT_VREPLACE
8496 if (State & VREPLACE_FLAG)
8497 restart_edit = 'V';
8498 else
8499#endif
8500 if (State & REPLACE_FLAG)
8501 restart_edit = 'R';
8502 else
8503 restart_edit = 'I';
8504#ifdef FEAT_VIRTUALEDIT
8505 if (virtual_active())
8506 ins_at_eol = FALSE; /* cursor always keeps its column */
8507 else
8508#endif
8509 ins_at_eol = (gchar_cursor() == NUL);
8510}
8511
8512/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 * If the cursor is on an indent, ^T/^D insert/delete one
8514 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008515 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 * with vi. But vi only supports ^T and ^D after an
8517 * autoindent, we support it everywhere.
8518 */
8519 static void
8520ins_shift(c, lastc)
8521 int c;
8522 int lastc;
8523{
8524 if (stop_arrow() == FAIL)
8525 return;
8526 AppendCharToRedobuff(c);
8527
8528 /*
8529 * 0^D and ^^D: remove all indent.
8530 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008531 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8532 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 {
8534 --curwin->w_cursor.col;
8535 (void)del_char(FALSE); /* delete the '^' or '0' */
8536 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8537 if (State & REPLACE_FLAG)
8538 replace_pop_ins();
8539 if (lastc == '^')
8540 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008541 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542 }
8543 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008544 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008545
8546 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8547 did_ai = FALSE;
8548#ifdef FEAT_SMARTINDENT
8549 did_si = FALSE;
8550 can_si = FALSE;
8551 can_si_back = FALSE;
8552#endif
8553#ifdef FEAT_CINDENT
8554 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8555#endif
8556}
8557
8558 static void
8559ins_del()
8560{
8561 int temp;
8562
8563 if (stop_arrow() == FAIL)
8564 return;
8565 if (gchar_cursor() == NUL) /* delete newline */
8566 {
8567 temp = curwin->w_cursor.col;
8568 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008569 || do_join(2, FALSE, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 vim_beep();
8571 else
8572 curwin->w_cursor.col = temp;
8573 }
8574 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8575 vim_beep();
8576 did_ai = FALSE;
8577#ifdef FEAT_SMARTINDENT
8578 did_si = FALSE;
8579 can_si = FALSE;
8580 can_si_back = FALSE;
8581#endif
8582 AppendCharToRedobuff(K_DEL);
8583}
8584
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008585static void ins_bs_one __ARGS((colnr_T *vcolp));
8586
8587/*
8588 * Delete one character for ins_bs().
8589 */
8590 static void
8591ins_bs_one(vcolp)
8592 colnr_T *vcolp;
8593{
8594 dec_cursor();
8595 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8596 if (State & REPLACE_FLAG)
8597 {
8598 /* Don't delete characters before the insert point when in
8599 * Replace mode */
8600 if (curwin->w_cursor.lnum != Insstart.lnum
8601 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008602 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008603 }
8604 else
8605 (void)del_char(FALSE);
8606}
8607
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608/*
8609 * Handle Backspace, delete-word and delete-line in Insert mode.
8610 * Return TRUE when backspace was actually used.
8611 */
8612 static int
8613ins_bs(c, mode, inserted_space_p)
8614 int c;
8615 int mode;
8616 int *inserted_space_p;
8617{
8618 linenr_T lnum;
8619 int cc;
8620 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008621 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 colnr_T mincol;
8623 int did_backspace = FALSE;
8624 int in_indent;
8625 int oldState;
8626#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008627 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008628#endif
8629
8630 /*
8631 * can't delete anything in an empty file
8632 * can't backup past first character in buffer
8633 * can't backup past starting point unless 'backspace' > 1
8634 * can backup to a previous line if 'backspace' == 0
8635 */
8636 if ( bufempty()
8637 || (
8638#ifdef FEAT_RIGHTLEFT
8639 !revins_on &&
8640#endif
8641 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8642 || (!can_bs(BS_START)
8643 && (arrow_used
8644 || (curwin->w_cursor.lnum == Insstart.lnum
8645 && curwin->w_cursor.col <= Insstart.col)))
8646 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8647 && curwin->w_cursor.col <= ai_col)
8648 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8649 {
8650 vim_beep();
8651 return FALSE;
8652 }
8653
8654 if (stop_arrow() == FAIL)
8655 return FALSE;
8656 in_indent = inindent(0);
8657#ifdef FEAT_CINDENT
8658 if (in_indent)
8659 can_cindent = FALSE;
8660#endif
8661#ifdef FEAT_COMMENTS
8662 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8663#endif
8664#ifdef FEAT_RIGHTLEFT
8665 if (revins_on) /* put cursor after last inserted char */
8666 inc_cursor();
8667#endif
8668
8669#ifdef FEAT_VIRTUALEDIT
8670 /* Virtualedit:
8671 * BACKSPACE_CHAR eats a virtual space
8672 * BACKSPACE_WORD eats all coladd
8673 * BACKSPACE_LINE eats all coladd and keeps going
8674 */
8675 if (curwin->w_cursor.coladd > 0)
8676 {
8677 if (mode == BACKSPACE_CHAR)
8678 {
8679 --curwin->w_cursor.coladd;
8680 return TRUE;
8681 }
8682 if (mode == BACKSPACE_WORD)
8683 {
8684 curwin->w_cursor.coladd = 0;
8685 return TRUE;
8686 }
8687 curwin->w_cursor.coladd = 0;
8688 }
8689#endif
8690
8691 /*
8692 * delete newline!
8693 */
8694 if (curwin->w_cursor.col == 0)
8695 {
8696 lnum = Insstart.lnum;
8697 if (curwin->w_cursor.lnum == Insstart.lnum
8698#ifdef FEAT_RIGHTLEFT
8699 || revins_on
8700#endif
8701 )
8702 {
8703 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8704 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8705 return FALSE;
8706 --Insstart.lnum;
8707 Insstart.col = MAXCOL;
8708 }
8709 /*
8710 * In replace mode:
8711 * cc < 0: NL was inserted, delete it
8712 * cc >= 0: NL was replaced, put original characters back
8713 */
8714 cc = -1;
8715 if (State & REPLACE_FLAG)
8716 cc = replace_pop(); /* returns -1 if NL was inserted */
8717 /*
8718 * In replace mode, in the line we started replacing, we only move the
8719 * cursor.
8720 */
8721 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8722 {
8723 dec_cursor();
8724 }
8725 else
8726 {
8727#ifdef FEAT_VREPLACE
8728 if (!(State & VREPLACE_FLAG)
8729 || curwin->w_cursor.lnum > orig_line_count)
8730#endif
8731 {
8732 temp = gchar_cursor(); /* remember current char */
8733 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008734
8735 /* When "aw" is in 'formatoptions' we must delete the space at
8736 * the end of the line, otherwise the line will be broken
8737 * again when auto-formatting. */
8738 if (has_format_option(FO_AUTO)
8739 && has_format_option(FO_WHITE_PAR))
8740 {
8741 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8742 TRUE);
8743 int len;
8744
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008745 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008746 if (len > 0 && ptr[len - 1] == ' ')
8747 ptr[len - 1] = NUL;
8748 }
8749
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008750 (void)do_join(2, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 if (temp == NUL && gchar_cursor() != NUL)
8752 inc_cursor();
8753 }
8754#ifdef FEAT_VREPLACE
8755 else
8756 dec_cursor();
8757#endif
8758
8759 /*
8760 * In REPLACE mode we have to put back the text that was replaced
8761 * by the NL. On the replace stack is first a NUL-terminated
8762 * sequence of characters that were deleted and then the
8763 * characters that NL replaced.
8764 */
8765 if (State & REPLACE_FLAG)
8766 {
8767 /*
8768 * Do the next ins_char() in NORMAL state, to
8769 * prevent ins_char() from replacing characters and
8770 * avoiding showmatch().
8771 */
8772 oldState = State;
8773 State = NORMAL;
8774 /*
8775 * restore characters (blanks) deleted after cursor
8776 */
8777 while (cc > 0)
8778 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008779 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780#ifdef FEAT_MBYTE
8781 mb_replace_pop_ins(cc);
8782#else
8783 ins_char(cc);
8784#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008785 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008786 cc = replace_pop();
8787 }
8788 /* restore the characters that NL replaced */
8789 replace_pop_ins();
8790 State = oldState;
8791 }
8792 }
8793 did_ai = FALSE;
8794 }
8795 else
8796 {
8797 /*
8798 * Delete character(s) before the cursor.
8799 */
8800#ifdef FEAT_RIGHTLEFT
8801 if (revins_on) /* put cursor on last inserted char */
8802 dec_cursor();
8803#endif
8804 mincol = 0;
8805 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008806 if (mode == BACKSPACE_LINE
8807 && (curbuf->b_p_ai
8808#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008809 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008810#endif
8811 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008812#ifdef FEAT_RIGHTLEFT
8813 && !revins_on
8814#endif
8815 )
8816 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008817 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008819 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008821 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 }
8823
8824 /*
8825 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8826 */
8827 if ( mode == BACKSPACE_CHAR
8828 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008829 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008830 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 && (*(ml_get_cursor() - 1) == TAB
8832 || (*(ml_get_cursor() - 1) == ' '
8833 && (!*inserted_space_p
8834 || arrow_used))))))
8835 {
8836 int ts;
8837 colnr_T vcol;
8838 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008839 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840
8841 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008842 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008843 ts = curbuf->b_p_sw;
8844 else
8845 ts = curbuf->b_p_sts;
8846 /* Compute the virtual column where we want to be. Since
8847 * 'showbreak' may get in the way, need to get the last column of
8848 * the previous character. */
8849 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008850 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 dec_cursor();
8852 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8853 inc_cursor();
8854 want_vcol = (want_vcol / ts) * ts;
8855
8856 /* delete characters until we are at or before want_vcol */
8857 while (vcol > want_vcol
8858 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008859 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860
8861 /* insert extra spaces until we are at want_vcol */
8862 while (vcol < want_vcol)
8863 {
8864 /* Remember the first char we inserted */
8865 if (curwin->w_cursor.lnum == Insstart.lnum
8866 && curwin->w_cursor.col < Insstart.col)
8867 Insstart.col = curwin->w_cursor.col;
8868
8869#ifdef FEAT_VREPLACE
8870 if (State & VREPLACE_FLAG)
8871 ins_char(' ');
8872 else
8873#endif
8874 {
8875 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008876 if ((State & REPLACE_FLAG))
8877 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008878 }
8879 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8880 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008881
8882 /* If we are now back where we started delete one character. Can
8883 * happen when using 'sts' and 'linebreak'. */
8884 if (vcol >= start_vcol)
8885 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 }
8887
8888 /*
8889 * Delete upto starting point, start of line or previous word.
8890 */
8891 else do
8892 {
8893#ifdef FEAT_RIGHTLEFT
8894 if (!revins_on) /* put cursor on char to be deleted */
8895#endif
8896 dec_cursor();
8897
8898 /* start of word? */
8899 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8900 {
8901 mode = BACKSPACE_WORD_NOT_SPACE;
8902 temp = vim_iswordc(gchar_cursor());
8903 }
8904 /* end of word? */
8905 else if (mode == BACKSPACE_WORD_NOT_SPACE
8906 && (vim_isspace(cc = gchar_cursor())
8907 || vim_iswordc(cc) != temp))
8908 {
8909#ifdef FEAT_RIGHTLEFT
8910 if (!revins_on)
8911#endif
8912 inc_cursor();
8913#ifdef FEAT_RIGHTLEFT
8914 else if (State & REPLACE_FLAG)
8915 dec_cursor();
8916#endif
8917 break;
8918 }
8919 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008920 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 else
8922 {
8923#ifdef FEAT_MBYTE
8924 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008925 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008926#endif
8927 (void)del_char(FALSE);
8928#ifdef FEAT_MBYTE
8929 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008930 * If there are combining characters and 'delcombine' is set
8931 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 * character.
8933 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008934 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 inc_cursor();
8936#endif
8937#ifdef FEAT_RIGHTLEFT
8938 if (revins_chars)
8939 {
8940 revins_chars--;
8941 revins_legal++;
8942 }
8943 if (revins_on && gchar_cursor() == NUL)
8944 break;
8945#endif
8946 }
8947 /* Just a single backspace?: */
8948 if (mode == BACKSPACE_CHAR)
8949 break;
8950 } while (
8951#ifdef FEAT_RIGHTLEFT
8952 revins_on ||
8953#endif
8954 (curwin->w_cursor.col > mincol
8955 && (curwin->w_cursor.lnum != Insstart.lnum
8956 || curwin->w_cursor.col != Insstart.col)));
8957 did_backspace = TRUE;
8958 }
8959#ifdef FEAT_SMARTINDENT
8960 did_si = FALSE;
8961 can_si = FALSE;
8962 can_si_back = FALSE;
8963#endif
8964 if (curwin->w_cursor.col <= 1)
8965 did_ai = FALSE;
8966 /*
8967 * It's a little strange to put backspaces into the redo
8968 * buffer, but it makes auto-indent a lot easier to deal
8969 * with.
8970 */
8971 AppendCharToRedobuff(c);
8972
8973 /* If deleted before the insertion point, adjust it */
8974 if (curwin->w_cursor.lnum == Insstart.lnum
8975 && curwin->w_cursor.col < Insstart.col)
8976 Insstart.col = curwin->w_cursor.col;
8977
8978 /* vi behaviour: the cursor moves backward but the character that
8979 * was there remains visible
8980 * Vim behaviour: the cursor moves backward and the character that
8981 * was there is erased from the screen.
8982 * We can emulate the vi behaviour by pretending there is a dollar
8983 * displayed even when there isn't.
8984 * --pkv Sun Jan 19 01:56:40 EST 2003 */
Bram Moolenaar76b9b362012-02-04 23:35:00 +01008985 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008986 dollar_vcol = curwin->w_virtcol;
8987
Bram Moolenaarce3be472008-01-14 19:12:28 +00008988#ifdef FEAT_FOLDING
8989 /* When deleting a char the cursor line must never be in a closed fold.
8990 * E.g., when 'foldmethod' is indent and deleting the first non-white
8991 * char before a Tab. */
8992 if (did_backspace)
8993 foldOpenCursor();
8994#endif
8995
Bram Moolenaar071d4272004-06-13 20:20:40 +00008996 return did_backspace;
8997}
8998
8999#ifdef FEAT_MOUSE
9000 static void
9001ins_mouse(c)
9002 int c;
9003{
9004 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009005 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006
9007# ifdef FEAT_GUI
9008 /* When GUI is active, also move/paste when 'mouse' is empty */
9009 if (!gui.in_use)
9010# endif
9011 if (!mouse_has(MOUSE_INSERT))
9012 return;
9013
9014 undisplay_dollar();
9015 tpos = curwin->w_cursor;
9016 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
9017 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00009018#ifdef FEAT_WINDOWS
9019 win_T *new_curwin = curwin;
9020
9021 if (curwin != old_curwin && win_valid(old_curwin))
9022 {
9023 /* Mouse took us to another window. We need to go back to the
9024 * previous one to stop insert there properly. */
9025 curwin = old_curwin;
9026 curbuf = curwin->w_buffer;
9027 }
9028#endif
9029 start_arrow(curwin == old_curwin ? &tpos : NULL);
9030#ifdef FEAT_WINDOWS
9031 if (curwin != new_curwin && win_valid(new_curwin))
9032 {
9033 curwin = new_curwin;
9034 curbuf = curwin->w_buffer;
9035 }
9036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009037# ifdef FEAT_CINDENT
9038 can_cindent = TRUE;
9039# endif
9040 }
9041
9042#ifdef FEAT_WINDOWS
9043 /* redraw status lines (in case another window became active) */
9044 redraw_statuslines();
9045#endif
9046}
9047
9048 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009049ins_mousescroll(dir)
9050 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009051{
9052 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009053# if defined(FEAT_WINDOWS)
9054 win_T *old_curwin = curwin;
9055# endif
9056# ifdef FEAT_INS_EXPAND
9057 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009058# endif
9059
9060 tpos = curwin->w_cursor;
9061
9062# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009063 /* Currently the mouse coordinates are only known in the GUI. */
9064 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
9065 {
9066 int row, col;
9067
9068 row = mouse_row;
9069 col = mouse_col;
9070
9071 /* find the window at the pointer coordinates */
9072 curwin = mouse_find_win(&row, &col);
9073 curbuf = curwin->w_buffer;
9074 }
9075 if (curwin == old_curwin)
9076# endif
9077 undisplay_dollar();
9078
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009079# ifdef FEAT_INS_EXPAND
9080 /* Don't scroll the window in which completion is being done. */
9081 if (!pum_visible()
9082# if defined(FEAT_WINDOWS)
9083 || curwin != old_curwin
9084# endif
9085 )
9086# endif
9087 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009088 if (dir == MSCR_DOWN || dir == MSCR_UP)
9089 {
9090 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9091 scroll_redraw(dir,
9092 (long)(curwin->w_botline - curwin->w_topline));
9093 else
9094 scroll_redraw(dir, 3L);
9095 }
9096#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009097 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009098 {
9099 int val, step = 6;
9100
9101 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
9102 step = W_WIDTH(curwin);
9103 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
9104 if (val < 0)
9105 val = 0;
9106 gui_do_horiz_scroll(val, TRUE);
9107 }
9108#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009109# ifdef FEAT_INS_EXPAND
9110 did_scroll = TRUE;
9111# endif
9112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009113
9114# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
9115 curwin->w_redr_status = TRUE;
9116
9117 curwin = old_curwin;
9118 curbuf = curwin->w_buffer;
9119# endif
9120
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00009121# ifdef FEAT_INS_EXPAND
9122 /* The popup menu may overlay the window, need to redraw it.
9123 * TODO: Would be more efficient to only redraw the windows that are
9124 * overlapped by the popup menu. */
9125 if (pum_visible() && did_scroll)
9126 {
9127 redraw_all_later(NOT_VALID);
9128 ins_compl_show_pum();
9129 }
9130# endif
9131
Bram Moolenaar071d4272004-06-13 20:20:40 +00009132 if (!equalpos(curwin->w_cursor, tpos))
9133 {
9134 start_arrow(&tpos);
9135# ifdef FEAT_CINDENT
9136 can_cindent = TRUE;
9137# endif
9138 }
9139}
9140#endif
9141
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009142#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00009143 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009144ins_tabline(c)
9145 int c;
9146{
9147 /* We will be leaving the current window, unless closing another tab. */
9148 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
9149 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
9150 {
9151 undisplay_dollar();
9152 start_arrow(&curwin->w_cursor);
9153# ifdef FEAT_CINDENT
9154 can_cindent = TRUE;
9155# endif
9156 }
9157
9158 if (c == K_TABLINE)
9159 goto_tabpage(current_tab);
9160 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009161 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009162 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00009163 redraw_statuslines(); /* will redraw the tabline when needed */
9164 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00009165}
9166#endif
9167
9168#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 void
9170ins_scroll()
9171{
9172 pos_T tpos;
9173
9174 undisplay_dollar();
9175 tpos = curwin->w_cursor;
9176 if (gui_do_scroll())
9177 {
9178 start_arrow(&tpos);
9179# ifdef FEAT_CINDENT
9180 can_cindent = TRUE;
9181# endif
9182 }
9183}
9184
9185 void
9186ins_horscroll()
9187{
9188 pos_T tpos;
9189
9190 undisplay_dollar();
9191 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009192 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009193 {
9194 start_arrow(&tpos);
9195# ifdef FEAT_CINDENT
9196 can_cindent = TRUE;
9197# endif
9198 }
9199}
9200#endif
9201
9202 static void
9203ins_left()
9204{
9205 pos_T tpos;
9206
9207#ifdef FEAT_FOLDING
9208 if ((fdo_flags & FDO_HOR) && KeyTyped)
9209 foldOpenCursor();
9210#endif
9211 undisplay_dollar();
9212 tpos = curwin->w_cursor;
9213 if (oneleft() == OK)
9214 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009215#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9216 /* Only call start_arrow() when not busy with preediting, it will
9217 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9218 if (!im_is_preediting())
9219#endif
9220 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009221#ifdef FEAT_RIGHTLEFT
9222 /* If exit reversed string, position is fixed */
9223 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9224 revins_legal++;
9225 revins_chars++;
9226#endif
9227 }
9228
9229 /*
9230 * if 'whichwrap' set for cursor in insert mode may go to
9231 * previous line
9232 */
9233 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9234 {
9235 start_arrow(&tpos);
9236 --(curwin->w_cursor.lnum);
9237 coladvance((colnr_T)MAXCOL);
9238 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9239 }
9240 else
9241 vim_beep();
9242}
9243
9244 static void
9245ins_home(c)
9246 int c;
9247{
9248 pos_T tpos;
9249
9250#ifdef FEAT_FOLDING
9251 if ((fdo_flags & FDO_HOR) && KeyTyped)
9252 foldOpenCursor();
9253#endif
9254 undisplay_dollar();
9255 tpos = curwin->w_cursor;
9256 if (c == K_C_HOME)
9257 curwin->w_cursor.lnum = 1;
9258 curwin->w_cursor.col = 0;
9259#ifdef FEAT_VIRTUALEDIT
9260 curwin->w_cursor.coladd = 0;
9261#endif
9262 curwin->w_curswant = 0;
9263 start_arrow(&tpos);
9264}
9265
9266 static void
9267ins_end(c)
9268 int c;
9269{
9270 pos_T tpos;
9271
9272#ifdef FEAT_FOLDING
9273 if ((fdo_flags & FDO_HOR) && KeyTyped)
9274 foldOpenCursor();
9275#endif
9276 undisplay_dollar();
9277 tpos = curwin->w_cursor;
9278 if (c == K_C_END)
9279 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9280 coladvance((colnr_T)MAXCOL);
9281 curwin->w_curswant = MAXCOL;
9282
9283 start_arrow(&tpos);
9284}
9285
9286 static void
9287ins_s_left()
9288{
9289#ifdef FEAT_FOLDING
9290 if ((fdo_flags & FDO_HOR) && KeyTyped)
9291 foldOpenCursor();
9292#endif
9293 undisplay_dollar();
9294 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9295 {
9296 start_arrow(&curwin->w_cursor);
9297 (void)bck_word(1L, FALSE, FALSE);
9298 curwin->w_set_curswant = TRUE;
9299 }
9300 else
9301 vim_beep();
9302}
9303
9304 static void
9305ins_right()
9306{
9307#ifdef FEAT_FOLDING
9308 if ((fdo_flags & FDO_HOR) && KeyTyped)
9309 foldOpenCursor();
9310#endif
9311 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009312 if (gchar_cursor() != NUL
9313#ifdef FEAT_VIRTUALEDIT
9314 || virtual_active()
9315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009316 )
9317 {
9318 start_arrow(&curwin->w_cursor);
9319 curwin->w_set_curswant = TRUE;
9320#ifdef FEAT_VIRTUALEDIT
9321 if (virtual_active())
9322 oneright();
9323 else
9324#endif
9325 {
9326#ifdef FEAT_MBYTE
9327 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009328 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009329 else
9330#endif
9331 ++curwin->w_cursor.col;
9332 }
9333
9334#ifdef FEAT_RIGHTLEFT
9335 revins_legal++;
9336 if (revins_chars)
9337 revins_chars--;
9338#endif
9339 }
9340 /* if 'whichwrap' set for cursor in insert mode, may move the
9341 * cursor to the next line */
9342 else if (vim_strchr(p_ww, ']') != NULL
9343 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9344 {
9345 start_arrow(&curwin->w_cursor);
9346 curwin->w_set_curswant = TRUE;
9347 ++curwin->w_cursor.lnum;
9348 curwin->w_cursor.col = 0;
9349 }
9350 else
9351 vim_beep();
9352}
9353
9354 static void
9355ins_s_right()
9356{
9357#ifdef FEAT_FOLDING
9358 if ((fdo_flags & FDO_HOR) && KeyTyped)
9359 foldOpenCursor();
9360#endif
9361 undisplay_dollar();
9362 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9363 || gchar_cursor() != NUL)
9364 {
9365 start_arrow(&curwin->w_cursor);
9366 (void)fwd_word(1L, FALSE, 0);
9367 curwin->w_set_curswant = TRUE;
9368 }
9369 else
9370 vim_beep();
9371}
9372
9373 static void
9374ins_up(startcol)
9375 int startcol; /* when TRUE move to Insstart.col */
9376{
9377 pos_T tpos;
9378 linenr_T old_topline = curwin->w_topline;
9379#ifdef FEAT_DIFF
9380 int old_topfill = curwin->w_topfill;
9381#endif
9382
9383 undisplay_dollar();
9384 tpos = curwin->w_cursor;
9385 if (cursor_up(1L, TRUE) == OK)
9386 {
9387 if (startcol)
9388 coladvance(getvcol_nolist(&Insstart));
9389 if (old_topline != curwin->w_topline
9390#ifdef FEAT_DIFF
9391 || old_topfill != curwin->w_topfill
9392#endif
9393 )
9394 redraw_later(VALID);
9395 start_arrow(&tpos);
9396#ifdef FEAT_CINDENT
9397 can_cindent = TRUE;
9398#endif
9399 }
9400 else
9401 vim_beep();
9402}
9403
9404 static void
9405ins_pageup()
9406{
9407 pos_T tpos;
9408
9409 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009410
9411#ifdef FEAT_WINDOWS
9412 if (mod_mask & MOD_MASK_CTRL)
9413 {
9414 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009415 if (first_tabpage->tp_next != NULL)
9416 {
9417 start_arrow(&curwin->w_cursor);
9418 goto_tabpage(-1);
9419 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009420 return;
9421 }
9422#endif
9423
Bram Moolenaar071d4272004-06-13 20:20:40 +00009424 tpos = curwin->w_cursor;
9425 if (onepage(BACKWARD, 1L) == OK)
9426 {
9427 start_arrow(&tpos);
9428#ifdef FEAT_CINDENT
9429 can_cindent = TRUE;
9430#endif
9431 }
9432 else
9433 vim_beep();
9434}
9435
9436 static void
9437ins_down(startcol)
9438 int startcol; /* when TRUE move to Insstart.col */
9439{
9440 pos_T tpos;
9441 linenr_T old_topline = curwin->w_topline;
9442#ifdef FEAT_DIFF
9443 int old_topfill = curwin->w_topfill;
9444#endif
9445
9446 undisplay_dollar();
9447 tpos = curwin->w_cursor;
9448 if (cursor_down(1L, TRUE) == OK)
9449 {
9450 if (startcol)
9451 coladvance(getvcol_nolist(&Insstart));
9452 if (old_topline != curwin->w_topline
9453#ifdef FEAT_DIFF
9454 || old_topfill != curwin->w_topfill
9455#endif
9456 )
9457 redraw_later(VALID);
9458 start_arrow(&tpos);
9459#ifdef FEAT_CINDENT
9460 can_cindent = TRUE;
9461#endif
9462 }
9463 else
9464 vim_beep();
9465}
9466
9467 static void
9468ins_pagedown()
9469{
9470 pos_T tpos;
9471
9472 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009473
9474#ifdef FEAT_WINDOWS
9475 if (mod_mask & MOD_MASK_CTRL)
9476 {
9477 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009478 if (first_tabpage->tp_next != NULL)
9479 {
9480 start_arrow(&curwin->w_cursor);
9481 goto_tabpage(0);
9482 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009483 return;
9484 }
9485#endif
9486
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 tpos = curwin->w_cursor;
9488 if (onepage(FORWARD, 1L) == OK)
9489 {
9490 start_arrow(&tpos);
9491#ifdef FEAT_CINDENT
9492 can_cindent = TRUE;
9493#endif
9494 }
9495 else
9496 vim_beep();
9497}
9498
9499#ifdef FEAT_DND
9500 static void
9501ins_drop()
9502{
9503 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9504}
9505#endif
9506
9507/*
9508 * Handle TAB in Insert or Replace mode.
9509 * Return TRUE when the TAB needs to be inserted like a normal character.
9510 */
9511 static int
9512ins_tab()
9513{
9514 int ind;
9515 int i;
9516 int temp;
9517
9518 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9519 Insstart_blank_vcol = get_nolist_virtcol();
9520 if (echeck_abbr(TAB + ABBR_OFF))
9521 return FALSE;
9522
9523 ind = inindent(0);
9524#ifdef FEAT_CINDENT
9525 if (ind)
9526 can_cindent = FALSE;
9527#endif
9528
9529 /*
9530 * When nothing special, insert TAB like a normal character
9531 */
9532 if (!curbuf->b_p_et
9533 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9534 && curbuf->b_p_sts == 0)
9535 return TRUE;
9536
9537 if (stop_arrow() == FAIL)
9538 return TRUE;
9539
9540 did_ai = FALSE;
9541#ifdef FEAT_SMARTINDENT
9542 did_si = FALSE;
9543 can_si = FALSE;
9544 can_si_back = FALSE;
9545#endif
9546 AppendToRedobuff((char_u *)"\t");
9547
9548 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9549 temp = (int)curbuf->b_p_sw;
9550 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9551 temp = (int)curbuf->b_p_sts;
9552 else /* otherwise use 'tabstop' */
9553 temp = (int)curbuf->b_p_ts;
9554 temp -= get_nolist_virtcol() % temp;
9555
9556 /*
9557 * Insert the first space with ins_char(). It will delete one char in
9558 * replace mode. Insert the rest with ins_str(); it will not delete any
9559 * chars. For VREPLACE mode, we use ins_char() for all characters.
9560 */
9561 ins_char(' ');
9562 while (--temp > 0)
9563 {
9564#ifdef FEAT_VREPLACE
9565 if (State & VREPLACE_FLAG)
9566 ins_char(' ');
9567 else
9568#endif
9569 {
9570 ins_str((char_u *)" ");
9571 if (State & REPLACE_FLAG) /* no char replaced */
9572 replace_push(NUL);
9573 }
9574 }
9575
9576 /*
9577 * When 'expandtab' not set: Replace spaces by TABs where possible.
9578 */
9579 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9580 {
9581 char_u *ptr;
9582#ifdef FEAT_VREPLACE
9583 char_u *saved_line = NULL; /* init for GCC */
9584 pos_T pos;
9585#endif
9586 pos_T fpos;
9587 pos_T *cursor;
9588 colnr_T want_vcol, vcol;
9589 int change_col = -1;
9590 int save_list = curwin->w_p_list;
9591
9592 /*
9593 * Get the current line. For VREPLACE mode, don't make real changes
9594 * yet, just work on a copy of the line.
9595 */
9596#ifdef FEAT_VREPLACE
9597 if (State & VREPLACE_FLAG)
9598 {
9599 pos = curwin->w_cursor;
9600 cursor = &pos;
9601 saved_line = vim_strsave(ml_get_curline());
9602 if (saved_line == NULL)
9603 return FALSE;
9604 ptr = saved_line + pos.col;
9605 }
9606 else
9607#endif
9608 {
9609 ptr = ml_get_cursor();
9610 cursor = &curwin->w_cursor;
9611 }
9612
9613 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9614 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9615 curwin->w_p_list = FALSE;
9616
9617 /* Find first white before the cursor */
9618 fpos = curwin->w_cursor;
9619 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9620 {
9621 --fpos.col;
9622 --ptr;
9623 }
9624
9625 /* In Replace mode, don't change characters before the insert point. */
9626 if ((State & REPLACE_FLAG)
9627 && fpos.lnum == Insstart.lnum
9628 && fpos.col < Insstart.col)
9629 {
9630 ptr += Insstart.col - fpos.col;
9631 fpos.col = Insstart.col;
9632 }
9633
9634 /* compute virtual column numbers of first white and cursor */
9635 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9636 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9637
9638 /* Use as many TABs as possible. Beware of 'showbreak' and
9639 * 'linebreak' adding extra virtual columns. */
9640 while (vim_iswhite(*ptr))
9641 {
9642 i = lbr_chartabsize((char_u *)"\t", vcol);
9643 if (vcol + i > want_vcol)
9644 break;
9645 if (*ptr != TAB)
9646 {
9647 *ptr = TAB;
9648 if (change_col < 0)
9649 {
9650 change_col = fpos.col; /* Column of first change */
9651 /* May have to adjust Insstart */
9652 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9653 Insstart.col = fpos.col;
9654 }
9655 }
9656 ++fpos.col;
9657 ++ptr;
9658 vcol += i;
9659 }
9660
9661 if (change_col >= 0)
9662 {
9663 int repl_off = 0;
9664
9665 /* Skip over the spaces we need. */
9666 while (vcol < want_vcol && *ptr == ' ')
9667 {
9668 vcol += lbr_chartabsize(ptr, vcol);
9669 ++ptr;
9670 ++repl_off;
9671 }
9672 if (vcol > want_vcol)
9673 {
9674 /* Must have a char with 'showbreak' just before it. */
9675 --ptr;
9676 --repl_off;
9677 }
9678 fpos.col += repl_off;
9679
9680 /* Delete following spaces. */
9681 i = cursor->col - fpos.col;
9682 if (i > 0)
9683 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009684 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 /* correct replace stack. */
9686 if ((State & REPLACE_FLAG)
9687#ifdef FEAT_VREPLACE
9688 && !(State & VREPLACE_FLAG)
9689#endif
9690 )
9691 for (temp = i; --temp >= 0; )
9692 replace_join(repl_off);
9693 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009694#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009695 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009696 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009697 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009698 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9699 (char_u *)"\t", 1);
9700 }
9701#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702 cursor->col -= i;
9703
9704#ifdef FEAT_VREPLACE
9705 /*
9706 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9707 * backspacing over the changed spacing and then inserting the new
9708 * spacing.
9709 */
9710 if (State & VREPLACE_FLAG)
9711 {
9712 /* Backspace from real cursor to change_col */
9713 backspace_until_column(change_col);
9714
9715 /* Insert each char in saved_line from changed_col to
9716 * ptr-cursor */
9717 ins_bytes_len(saved_line + change_col,
9718 cursor->col - change_col);
9719 }
9720#endif
9721 }
9722
9723#ifdef FEAT_VREPLACE
9724 if (State & VREPLACE_FLAG)
9725 vim_free(saved_line);
9726#endif
9727 curwin->w_p_list = save_list;
9728 }
9729
9730 return FALSE;
9731}
9732
9733/*
9734 * Handle CR or NL in insert mode.
9735 * Return TRUE when out of memory or can't undo.
9736 */
9737 static int
9738ins_eol(c)
9739 int c;
9740{
9741 int i;
9742
9743 if (echeck_abbr(c + ABBR_OFF))
9744 return FALSE;
9745 if (stop_arrow() == FAIL)
9746 return TRUE;
9747 undisplay_dollar();
9748
9749 /*
9750 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9751 * character under the cursor. Only push a NUL on the replace stack,
9752 * nothing to put back when the NL is deleted.
9753 */
9754 if ((State & REPLACE_FLAG)
9755#ifdef FEAT_VREPLACE
9756 && !(State & VREPLACE_FLAG)
9757#endif
9758 )
9759 replace_push(NUL);
9760
9761 /*
9762 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9763 * replacing the next line, so we push all of the characters left on the
9764 * line onto the replace stack. This is not done here though, it is done
9765 * in open_line().
9766 */
9767
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009768#ifdef FEAT_VIRTUALEDIT
9769 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9770 * CTRL-O). */
9771 if (virtual_active() && curwin->w_cursor.coladd > 0)
9772 coladvance(getviscol());
9773#endif
9774
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775#ifdef FEAT_RIGHTLEFT
9776# ifdef FEAT_FKMAP
9777 if (p_altkeymap && p_fkmap)
9778 fkmap(NL);
9779# endif
9780 /* NL in reverse insert will always start in the end of
9781 * current line. */
9782 if (revins_on)
9783 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9784#endif
9785
9786 AppendToRedobuff(NL_STR);
9787 i = open_line(FORWARD,
9788#ifdef FEAT_COMMENTS
9789 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9790#endif
9791 0, old_indent);
9792 old_indent = 0;
9793#ifdef FEAT_CINDENT
9794 can_cindent = TRUE;
9795#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009796#ifdef FEAT_FOLDING
9797 /* When inserting a line the cursor line must never be in a closed fold. */
9798 foldOpenCursor();
9799#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009800
9801 return (!i);
9802}
9803
9804#ifdef FEAT_DIGRAPHS
9805/*
9806 * Handle digraph in insert mode.
9807 * Returns character still to be inserted, or NUL when nothing remaining to be
9808 * done.
9809 */
9810 static int
9811ins_digraph()
9812{
9813 int c;
9814 int cc;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009815 int did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009816
9817 pc_status = PC_STATUS_UNSET;
9818 if (redrawing() && !char_avail())
9819 {
9820 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009821 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822
9823 edit_putchar('?', TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009824 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825#ifdef FEAT_CMDL_INFO
9826 add_to_showcmd_c(Ctrl_K);
9827#endif
9828 }
9829
9830#ifdef USE_ON_FLY_SCROLL
9831 dont_scroll = TRUE; /* disallow scrolling here */
9832#endif
9833
9834 /* don't map the digraph chars. This also prevents the
9835 * mode message to be deleted when ESC is hit */
9836 ++no_mapping;
9837 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009838 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 --no_mapping;
9840 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009841 if (did_putchar)
9842 /* when the line fits in 'columns' the '?' is at the start of the next
9843 * line and will not be removed by the redraw */
9844 edit_unputchar();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009845
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 if (IS_SPECIAL(c) || mod_mask) /* special key */
9847 {
9848#ifdef FEAT_CMDL_INFO
9849 clear_showcmd();
9850#endif
9851 insert_special(c, TRUE, FALSE);
9852 return NUL;
9853 }
9854 if (c != ESC)
9855 {
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009856 did_putchar = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009857 if (redrawing() && !char_avail())
9858 {
9859 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009860 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009861
9862 if (char2cells(c) == 1)
9863 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00009864 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009865 edit_putchar(c, TRUE);
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009866 did_putchar = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009867 }
9868#ifdef FEAT_CMDL_INFO
9869 add_to_showcmd_c(c);
9870#endif
9871 }
9872 ++no_mapping;
9873 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009874 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009875 --no_mapping;
9876 --allow_keys;
Bram Moolenaar9c520cb2011-05-10 14:22:16 +02009877 if (did_putchar)
9878 /* when the line fits in 'columns' the '?' is at the start of the
9879 * next line and will not be removed by a redraw */
9880 edit_unputchar();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 if (cc != ESC)
9882 {
9883 AppendToRedobuff((char_u *)CTRL_V_STR);
9884 c = getdigraph(c, cc, TRUE);
9885#ifdef FEAT_CMDL_INFO
9886 clear_showcmd();
9887#endif
9888 return c;
9889 }
9890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891#ifdef FEAT_CMDL_INFO
9892 clear_showcmd();
9893#endif
9894 return NUL;
9895}
9896#endif
9897
9898/*
9899 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9900 * Returns the char to be inserted, or NUL if none found.
9901 */
9902 static int
9903ins_copychar(lnum)
9904 linenr_T lnum;
9905{
9906 int c;
9907 int temp;
9908 char_u *ptr, *prev_ptr;
9909
9910 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9911 {
9912 vim_beep();
9913 return NUL;
9914 }
9915
9916 /* try to advance to the cursor column */
9917 temp = 0;
9918 ptr = ml_get(lnum);
9919 prev_ptr = ptr;
9920 validate_virtcol();
9921 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9922 {
9923 prev_ptr = ptr;
9924 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9925 }
9926 if ((colnr_T)temp > curwin->w_virtcol)
9927 ptr = prev_ptr;
9928
9929#ifdef FEAT_MBYTE
9930 c = (*mb_ptr2char)(ptr);
9931#else
9932 c = *ptr;
9933#endif
9934 if (c == NUL)
9935 vim_beep();
9936 return c;
9937}
9938
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009939/*
9940 * CTRL-Y or CTRL-E typed in Insert mode.
9941 */
9942 static int
9943ins_ctrl_ey(tc)
9944 int tc;
9945{
9946 int c = tc;
9947
9948#ifdef FEAT_INS_EXPAND
9949 if (ctrl_x_mode == CTRL_X_SCROLL)
9950 {
9951 if (c == Ctrl_Y)
9952 scrolldown_clamp();
9953 else
9954 scrollup_clamp();
9955 redraw_later(VALID);
9956 }
9957 else
9958#endif
9959 {
9960 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9961 if (c != NUL)
9962 {
9963 long tw_save;
9964
9965 /* The character must be taken literally, insert like it
9966 * was typed after a CTRL-V, and pretend 'textwidth'
9967 * wasn't set. Digits, 'o' and 'x' are special after a
9968 * CTRL-V, don't use it for these. */
9969 if (c < 256 && !isalnum(c))
9970 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9971 tw_save = curbuf->b_p_tw;
9972 curbuf->b_p_tw = -1;
9973 insert_special(c, TRUE, FALSE);
9974 curbuf->b_p_tw = tw_save;
9975#ifdef FEAT_RIGHTLEFT
9976 revins_chars++;
9977 revins_legal++;
9978#endif
9979 c = Ctrl_V; /* pretend CTRL-V is last character */
9980 auto_format(FALSE, TRUE);
9981 }
9982 }
9983 return c;
9984}
9985
Bram Moolenaar071d4272004-06-13 20:20:40 +00009986#ifdef FEAT_SMARTINDENT
9987/*
9988 * Try to do some very smart auto-indenting.
9989 * Used when inserting a "normal" character.
9990 */
9991 static void
9992ins_try_si(c)
9993 int c;
9994{
9995 pos_T *pos, old_pos;
9996 char_u *ptr;
9997 int i;
9998 int temp;
9999
10000 /*
10001 * do some very smart indenting when entering '{' or '}'
10002 */
10003 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
10004 {
10005 /*
10006 * for '}' set indent equal to indent of line containing matching '{'
10007 */
10008 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
10009 {
10010 old_pos = curwin->w_cursor;
10011 /*
10012 * If the matching '{' has a ')' immediately before it (ignoring
10013 * white-space), then line up with the start of the line
10014 * containing the matching '(' if there is one. This handles the
10015 * case where an "if (..\n..) {" statement continues over multiple
10016 * lines -- webb
10017 */
10018 ptr = ml_get(pos->lnum);
10019 i = pos->col;
10020 if (i > 0) /* skip blanks before '{' */
10021 while (--i > 0 && vim_iswhite(ptr[i]))
10022 ;
10023 curwin->w_cursor.lnum = pos->lnum;
10024 curwin->w_cursor.col = i;
10025 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
10026 curwin->w_cursor = *pos;
10027 i = get_indent();
10028 curwin->w_cursor = old_pos;
10029#ifdef FEAT_VREPLACE
10030 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010031 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010032 else
10033#endif
10034 (void)set_indent(i, SIN_CHANGED);
10035 }
10036 else if (curwin->w_cursor.col > 0)
10037 {
10038 /*
10039 * when inserting '{' after "O" reduce indent, but not
10040 * more than indent of previous line
10041 */
10042 temp = TRUE;
10043 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
10044 {
10045 old_pos = curwin->w_cursor;
10046 i = get_indent();
10047 while (curwin->w_cursor.lnum > 1)
10048 {
10049 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
10050
10051 /* ignore empty lines and lines starting with '#'. */
10052 if (*ptr != '#' && *ptr != NUL)
10053 break;
10054 }
10055 if (get_indent() >= i)
10056 temp = FALSE;
10057 curwin->w_cursor = old_pos;
10058 }
10059 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +000010060 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010061 }
10062 }
10063
10064 /*
10065 * set indent of '#' always to 0
10066 */
10067 if (curwin->w_cursor.col > 0 && can_si && c == '#')
10068 {
10069 /* remember current indent for next line */
10070 old_indent = get_indent();
10071 (void)set_indent(0, SIN_CHANGED);
10072 }
10073
10074 /* Adjust ai_col, the char at this position can be deleted. */
10075 if (ai_col > curwin->w_cursor.col)
10076 ai_col = curwin->w_cursor.col;
10077}
10078#endif
10079
10080/*
10081 * Get the value that w_virtcol would have when 'list' is off.
10082 * Unless 'cpo' contains the 'L' flag.
10083 */
10084 static colnr_T
10085get_nolist_virtcol()
10086{
10087 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
10088 return getvcol_nolist(&curwin->w_cursor);
10089 validate_virtcol();
10090 return curwin->w_virtcol;
10091}
Bram Moolenaarf5876f12012-02-29 18:22:08 +010010092
10093#ifdef FEAT_AUTOCMD
10094/*
10095 * Handle the InsertCharPre autocommand.
10096 * "c" is the character that was typed.
10097 * Return a pointer to allocated memory with the replacement string.
10098 * Return NULL to continue inserting "c".
10099 */
10100 static char_u *
10101do_insert_char_pre(c)
10102 int c;
10103{
10104 char_u *res;
10105
10106 /* Return quickly when there is nothing to do. */
10107 if (!has_insertcharpre())
10108 return NULL;
10109
10110 /* Lock the text to avoid weird things from happening. */
10111 ++textlock;
10112 set_vim_var_char(c); /* set v:char */
10113
10114 if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
10115 /* Get the new value of v:char. It may be empty or more than one
10116 * character. */
10117 res = vim_strsave(get_vim_var_str(VV_CHAR));
10118 else
10119 res = NULL;
10120
10121 set_vim_var_string(VV_CHAR, NULL, -1); /* clear v:char */
10122 --textlock;
10123
10124 return res;
10125}
10126#endif