blob: 9f7106165e93a18d299867e6d7aa8344f12130f9 [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 Moolenaar071d4272004-06-13 20:20:40 +000061
62/*
63 * Structure used to store one match for insert completion.
64 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000065typedef struct compl_S compl_T;
66struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
Bram Moolenaar572cb562005-08-05 21:35:02 +000068 compl_T *cp_next;
69 compl_T *cp_prev;
70 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000071 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000072 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000073 char_u *cp_fname; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077};
78
Bram Moolenaar572cb562005-08-05 21:35:02 +000079#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define FREE_FNAME (2)
81
82/*
83 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000084 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000089static compl_T *compl_first_match = NULL;
90static compl_T *compl_curr_match = NULL;
91static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar779b74b2006-04-10 14:55:34 +000093/* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95static int compl_enter_selects = FALSE;
96
Bram Moolenaara6557602006-02-04 22:43:20 +000097/* When "compl_leader" is not NULL only matches that start with this string
98 * are used. */
99static char_u *compl_leader = NULL;
100
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000101static int compl_get_longest = FALSE; /* put longest common string
102 in compl_leader */
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104static int compl_used_match; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
107
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000108static int compl_was_interrupted = FALSE; /* didn't finish finding
109 completions. */
110
111static int compl_restarting = FALSE; /* don't insert match */
112
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000113/* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000117/* Set when doing something for completion that may call edit() recursively,
118 * which is not allowed. */
119static int compl_busy = FALSE;
120
Bram Moolenaar572cb562005-08-05 21:35:02 +0000121static int compl_matches = 0;
122static char_u *compl_pattern = NULL;
123static int compl_direction = FORWARD;
124static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000125static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000126static pos_T compl_startpos;
127static colnr_T compl_col = 0; /* column where the text starts
128 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000129static char_u *compl_orig_text = NULL; /* text as it was before
130 * completion started */
131static int compl_cont_mode = 0;
132static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000134static void ins_ctrl_x __ARGS((void));
135static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000136static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000137static 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 +0000138static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000139static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000140static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000142static void ins_compl_upd_pum __ARGS((void));
143static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000144static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000145static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000146static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000147static 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 +0000148static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149static void ins_compl_free __ARGS((void));
150static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000151static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000152static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000153static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000154static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000155static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000156static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000157static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000158static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000160#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
161static void ins_compl_add_list __ARGS((list_T *list));
162#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000163static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void ins_compl_delete __ARGS((void));
165static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000166static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000167static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000168static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000169static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000170static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000172static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif /* FEAT_INS_EXPAND */
174
175#define BACKSPACE_CHAR 1
176#define BACKSPACE_WORD 2
177#define BACKSPACE_WORD_NOT_SPACE 3
178#define BACKSPACE_LINE 4
179
Bram Moolenaar754b5602006-02-09 23:53:20 +0000180static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181static void ins_ctrl_v __ARGS((void));
182static void undisplay_dollar __ARGS((void));
183static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000184static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185static void check_auto_format __ARGS((int));
186static void redo_literal __ARGS((int c));
187static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000188#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000189static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000190static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000191static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
194static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000195#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198static int replace_pop __ARGS((void));
199static void replace_join __ARGS((int off));
200static void replace_pop_ins __ARGS((void));
201#ifdef FEAT_MBYTE
202static void mb_replace_pop_ins __ARGS((int cc));
203#endif
204static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000205static void replace_do_bs __ARGS((int limit_col));
206static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_CINDENT
208static int cindent_on __ARGS((void));
209#endif
210static void ins_reg __ARGS((void));
211static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000212static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000213static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifdef FEAT_RIGHTLEFT
215static void ins_ctrl_ __ARGS((void));
216#endif
217#ifdef FEAT_VISUAL
218static int ins_start_select __ARGS((int c));
219#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000220static void ins_insert __ARGS((int replaceState));
221static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ins_shift __ARGS((int c, int lastc));
223static void ins_del __ARGS((void));
224static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
225#ifdef FEAT_MOUSE
226static void ins_mouse __ARGS((int c));
227static void ins_mousescroll __ARGS((int up));
228#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000229#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
230static void ins_tabline __ARGS((int c));
231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232static void ins_left __ARGS((void));
233static void ins_home __ARGS((int c));
234static void ins_end __ARGS((int c));
235static void ins_s_left __ARGS((void));
236static void ins_right __ARGS((void));
237static void ins_s_right __ARGS((void));
238static void ins_up __ARGS((int startcol));
239static void ins_pageup __ARGS((void));
240static void ins_down __ARGS((int startcol));
241static void ins_pagedown __ARGS((void));
242#ifdef FEAT_DND
243static void ins_drop __ARGS((void));
244#endif
245static int ins_tab __ARGS((void));
246static int ins_eol __ARGS((int c));
247#ifdef FEAT_DIGRAPHS
248static int ins_digraph __ARGS((void));
249#endif
250static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000251static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#ifdef FEAT_SMARTINDENT
253static void ins_try_si __ARGS((int c));
254#endif
255static colnr_T get_nolist_virtcol __ARGS((void));
256
257static colnr_T Insstart_textlen; /* length of line when insert started */
258static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
259
260static char_u *last_insert = NULL; /* the text of the previous insert,
261 K_SPECIAL and CSI are escaped */
262static int last_insert_skip; /* nr of chars in front of previous insert */
263static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000264static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
266#ifdef FEAT_CINDENT
267static int can_cindent; /* may do cindenting on this line */
268#endif
269
270static int old_indent = 0; /* for ^^D command in insert mode */
271
272#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000273static int revins_on; /* reverse insert mode on */
274static int revins_chars; /* how much to skip after edit */
275static int revins_legal; /* was the last char 'legal'? */
276static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
278
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279static int ins_need_undo; /* call u_save() before inserting a
280 char. Set when edit() is called.
281 after that arrow_used is used. */
282
283static int did_add_space = FALSE; /* auto_format() added an extra space
284 under the cursor */
285
286/*
287 * edit(): Start inserting text.
288 *
289 * "cmdchar" can be:
290 * 'i' normal insert command
291 * 'a' normal append command
292 * 'R' replace command
293 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
294 * but still only one <CR> is inserted. The <Esc> is not used for redo.
295 * 'g' "gI" command.
296 * 'V' "gR" command for Virtual Replace mode.
297 * 'v' "gr" command for single character Virtual Replace mode.
298 *
299 * This function is not called recursively. For CTRL-O commands, it returns
300 * and lets the caller handle the Normal-mode command.
301 *
302 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
303 */
304 int
305edit(cmdchar, startln, count)
306 int cmdchar;
307 int startln; /* if set, insert at start of line */
308 long count;
309{
310 int c = 0;
311 char_u *ptr;
312 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000313 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 int i;
316 int did_backspace = TRUE; /* previous char was backspace */
317#ifdef FEAT_CINDENT
318 int line_is_white = FALSE; /* line is empty before insert */
319#endif
320 linenr_T old_topline = 0; /* topline before insertion */
321#ifdef FEAT_DIFF
322 int old_topfill = -1;
323#endif
324 int inserted_space = FALSE; /* just inserted a space */
325 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000326 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000328 /* Remember whether editing was restarted after CTRL-O. */
329 did_restart_edit = restart_edit;
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 /* sleep before redrawing, needed for "CTRL-O :" that results in an
332 * error message */
333 check_for_delay(TRUE);
334
335#ifdef HAVE_SANDBOX
336 /* Don't allow inserting in the sandbox. */
337 if (sandbox != 0)
338 {
339 EMSG(_(e_sandbox));
340 return FALSE;
341 }
342#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000343 /* Don't allow changes in the buffer while editing the cmdline. The
344 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000345 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000346 {
347 EMSG(_(e_secure));
348 return FALSE;
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
351#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000352 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000353 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000354 {
355 EMSG(_(e_secure));
356 return FALSE;
357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 ins_compl_clear(); /* clear stuff for CTRL-X mode */
359#endif
360
Bram Moolenaar843ee412004-06-30 16:16:41 +0000361#ifdef FEAT_AUTOCMD
362 /*
363 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
364 */
365 if (cmdchar != 'r' && cmdchar != 'v')
366 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000367# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000368 if (cmdchar == 'R')
369 ptr = (char_u *)"r";
370 else if (cmdchar == 'V')
371 ptr = (char_u *)"v";
372 else
373 ptr = (char_u *)"i";
374 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000375# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000376 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
377 }
378#endif
379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#ifdef FEAT_MOUSE
381 /*
382 * When doing a paste with the middle mouse button, Insstart is set to
383 * where the paste started.
384 */
385 if (where_paste_started.lnum != 0)
386 Insstart = where_paste_started;
387 else
388#endif
389 {
390 Insstart = curwin->w_cursor;
391 if (startln)
392 Insstart.col = 0;
393 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000394 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 Insstart_blank_vcol = MAXCOL;
396 if (!did_ai)
397 ai_col = 0;
398
399 if (cmdchar != NUL && restart_edit == 0)
400 {
401 ResetRedobuff();
402 AppendNumberToRedobuff(count);
403#ifdef FEAT_VREPLACE
404 if (cmdchar == 'V' || cmdchar == 'v')
405 {
406 /* "gR" or "gr" command */
407 AppendCharToRedobuff('g');
408 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
409 }
410 else
411#endif
412 {
413 AppendCharToRedobuff(cmdchar);
414 if (cmdchar == 'g') /* "gI" command */
415 AppendCharToRedobuff('I');
416 else if (cmdchar == 'r') /* "r<CR>" command */
417 count = 1; /* insert only one <CR> */
418 }
419 }
420
421 if (cmdchar == 'R')
422 {
423#ifdef FEAT_FKMAP
424 if (p_fkmap && p_ri)
425 {
426 beep_flush();
427 EMSG(farsi_text_3); /* encoded in Farsi */
428 State = INSERT;
429 }
430 else
431#endif
432 State = REPLACE;
433 }
434#ifdef FEAT_VREPLACE
435 else if (cmdchar == 'V' || cmdchar == 'v')
436 {
437 State = VREPLACE;
438 replaceState = VREPLACE;
439 orig_line_count = curbuf->b_ml.ml_line_count;
440 vr_lines_changed = 1;
441 }
442#endif
443 else
444 State = INSERT;
445
446 stop_insert_mode = FALSE;
447
448 /*
449 * Need to recompute the cursor position, it might move when the cursor is
450 * on a TAB or special character.
451 */
452 curs_columns(TRUE);
453
454 /*
455 * Enable langmap or IME, indicated by 'iminsert'.
456 * Note that IME may enabled/disabled without us noticing here, thus the
457 * 'iminsert' value may not reflect what is actually used. It is updated
458 * when hitting <Esc>.
459 */
460 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
461 State |= LANGMAP;
462#ifdef USE_IM_CONTROL
463 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#ifdef FEAT_MOUSE
467 setmouse();
468#endif
469#ifdef FEAT_CMDL_INFO
470 clear_showcmd();
471#endif
472#ifdef FEAT_RIGHTLEFT
473 /* there is no reverse replace mode */
474 revins_on = (State == INSERT && p_ri);
475 if (revins_on)
476 undisplay_dollar();
477 revins_chars = 0;
478 revins_legal = 0;
479 revins_scol = -1;
480#endif
481
482 /*
483 * Handle restarting Insert mode.
484 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
485 * restart_edit non-zero, and something in the stuff buffer.
486 */
487 if (restart_edit != 0 && stuff_empty())
488 {
489#ifdef FEAT_MOUSE
490 /*
491 * After a paste we consider text typed to be part of the insert for
492 * the pasted text. You can backspace over the pasted text too.
493 */
494 if (where_paste_started.lnum)
495 arrow_used = FALSE;
496 else
497#endif
498 arrow_used = TRUE;
499 restart_edit = 0;
500
501 /*
502 * If the cursor was after the end-of-line before the CTRL-O and it is
503 * now at the end-of-line, put it after the end-of-line (this is not
504 * correct in very rare cases).
505 * Also do this if curswant is greater than the current virtual
506 * column. Eg after "^O$" or "^O80|".
507 */
508 validate_virtcol();
509 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000510 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 || curwin->w_curswant > curwin->w_virtcol)
512 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
513 {
514 if (ptr[1] == NUL)
515 ++curwin->w_cursor.col;
516#ifdef FEAT_MBYTE
517 else if (has_mbyte)
518 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000519 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 if (ptr[i] == NUL)
521 curwin->w_cursor.col += i;
522 }
523#endif
524 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000525 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
527 else
528 arrow_used = FALSE;
529
530 /* we are in insert mode now, don't need to start it anymore */
531 need_start_insertmode = FALSE;
532
533 /* Need to save the line for undo before inserting the first char. */
534 ins_need_undo = TRUE;
535
536#ifdef FEAT_MOUSE
537 where_paste_started.lnum = 0;
538#endif
539#ifdef FEAT_CINDENT
540 can_cindent = TRUE;
541#endif
542#ifdef FEAT_FOLDING
543 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
544 * restarting. */
545 if (!p_im && did_restart_edit == 0)
546 foldOpenCursor();
547#endif
548
549 /*
550 * If 'showmode' is set, show the current (insert/replace/..) mode.
551 * A warning message for changing a readonly file is given here, before
552 * actually changing anything. It's put after the mode, if any.
553 */
554 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000555 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 i = showmode();
557
558 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000559 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561#ifdef CURSOR_SHAPE
562 ui_cursor_shape(); /* may show different cursor shape */
563#endif
564#ifdef FEAT_DIGRAPHS
565 do_digraph(-1); /* clear digraphs */
566#endif
567
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000568 /*
569 * Get the current length of the redo buffer, those characters have to be
570 * skipped if we want to get to the inserted characters.
571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 ptr = get_inserted();
573 if (ptr == NULL)
574 new_insert_skip = 0;
575 else
576 {
577 new_insert_skip = (int)STRLEN(ptr);
578 vim_free(ptr);
579 }
580
581 old_indent = 0;
582
583 /*
584 * Main loop in Insert mode: repeat until Insert mode is left.
585 */
586 for (;;)
587 {
588#ifdef FEAT_RIGHTLEFT
589 if (!revins_legal)
590 revins_scol = -1; /* reset on illegal motions */
591 else
592 revins_legal = 0;
593#endif
594 if (arrow_used) /* don't repeat insert when arrow key used */
595 count = 0;
596
597 if (stop_insert_mode)
598 {
599 /* ":stopinsert" used or 'insertmode' reset */
600 count = 0;
601 goto doESCkey;
602 }
603
604 /* set curwin->w_curswant for next K_DOWN or K_UP */
605 if (!arrow_used)
606 curwin->w_set_curswant = TRUE;
607
608 /* If there is no typeahead may check for timestamps (e.g., for when a
609 * menu invoked a shell command). */
610 if (stuff_empty())
611 {
612 did_check_timestamps = FALSE;
613 if (need_check_timestamps)
614 check_timestamps(FALSE);
615 }
616
617 /*
618 * When emsg() was called msg_scroll will have been set.
619 */
620 msg_scroll = FALSE;
621
622#ifdef FEAT_GUI
623 /* When 'mousefocus' is set a mouse movement may have taken us to
624 * another window. "need_mouse_correct" may then be set because of an
625 * autocommand. */
626 if (need_mouse_correct)
627 gui_mouse_correct();
628#endif
629
630#ifdef FEAT_FOLDING
631 /* Open fold at the cursor line, according to 'foldopen'. */
632 if (fdo_flags & FDO_INSERT)
633 foldOpenCursor();
634 /* Close folds where the cursor isn't, according to 'foldclose' */
635 if (!char_avail())
636 foldCheckClose();
637#endif
638
639 /*
640 * If we inserted a character at the last position of the last line in
641 * the window, scroll the window one line up. This avoids an extra
642 * redraw.
643 * This is detected when the cursor column is smaller after inserting
644 * something.
645 * Don't do this when the topline changed already, it has
646 * already been adjusted (by insertchar() calling open_line())).
647 */
648 if (curbuf->b_mod_set
649 && curwin->w_p_wrap
650 && !did_backspace
651 && curwin->w_topline == old_topline
652#ifdef FEAT_DIFF
653 && curwin->w_topfill == old_topfill
654#endif
655 )
656 {
657 mincol = curwin->w_wcol;
658 validate_cursor_col();
659
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000660 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 && curwin->w_wrow == W_WINROW(curwin)
662 + curwin->w_height - 1 - p_so
663 && (curwin->w_cursor.lnum != curwin->w_topline
664#ifdef FEAT_DIFF
665 || curwin->w_topfill > 0
666#endif
667 ))
668 {
669#ifdef FEAT_DIFF
670 if (curwin->w_topfill > 0)
671 --curwin->w_topfill;
672 else
673#endif
674#ifdef FEAT_FOLDING
675 if (hasFolding(curwin->w_topline, NULL, &old_topline))
676 set_topline(curwin, old_topline + 1);
677 else
678#endif
679 set_topline(curwin, curwin->w_topline + 1);
680 }
681 }
682
683 /* May need to adjust w_topline to show the cursor. */
684 update_topline();
685
686 did_backspace = FALSE;
687
688 validate_cursor(); /* may set must_redraw */
689
690 /*
691 * Redraw the display when no characters are waiting.
692 * Also shows mode, ruler and positions cursor.
693 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000694 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
696#ifdef FEAT_SCROLLBIND
697 if (curwin->w_p_scb)
698 do_check_scrollbind(TRUE);
699#endif
700
Bram Moolenaar860cae12010-06-05 23:22:07 +0200701#ifdef FEAT_CURSORBIND
702 if (curwin->w_p_crb)
703 do_check_cursorbind();
704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 update_curswant();
706 old_topline = curwin->w_topline;
707#ifdef FEAT_DIFF
708 old_topfill = curwin->w_topfill;
709#endif
710
711#ifdef USE_ON_FLY_SCROLL
712 dont_scroll = FALSE; /* allow scrolling here */
713#endif
714
715 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000716 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 */
718 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000719 do
720 {
721 c = safe_vgetc();
722 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000724#ifdef FEAT_AUTOCMD
725 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
726 did_cursorhold = TRUE;
727#endif
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729#ifdef FEAT_RIGHTLEFT
730 if (p_hkmap && KeyTyped)
731 c = hkmap(c); /* Hebrew mode mapping */
732#endif
733#ifdef FEAT_FKMAP
734 if (p_fkmap && KeyTyped)
735 c = fkmap(c); /* Farsi mode mapping */
736#endif
737
738#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000739 /*
740 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000741 * and the cursor is still in the completed word. Only when there is
742 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000743 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000744 if (compl_started
745 && pum_wanted()
746 && curwin->w_cursor.col >= compl_col
747 && (compl_shown_match == NULL
748 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000749 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000750 /* BS: Delete one character from "compl_leader". */
751 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000752 && curwin->w_cursor.col > compl_col
753 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000754 continue;
755
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000756 /* When no match was selected or it was edited. */
757 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000758 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000759 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000760 * "compl_leader". Except when at the original match and
761 * there is nothing to add, CTRL-L works like CTRL-P then. */
762 if (c == Ctrl_L
763 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000764 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000765 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000766 {
767 ins_compl_addfrommatch();
768 continue;
769 }
770
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000771 /* A non-white character that fits in with the current
772 * completion: Add to "compl_leader". */
773 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000774 {
775 ins_compl_addleader(c);
776 continue;
777 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000778
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000779 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000780 * compl_enter_selects is set the Enter key does the same. */
781 if (c == Ctrl_Y || (compl_enter_selects
782 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000783 {
784 ins_compl_delete();
785 ins_compl_insert();
786 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000787 }
788 }
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
791 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000792 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000793 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000794 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795#endif
796
Bram Moolenaar488c6512005-08-11 20:09:58 +0000797 /* CTRL-\ CTRL-N goes to Normal mode,
798 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
799 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (c == Ctrl_BSL)
801 {
802 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000803 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 ++no_mapping;
805 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000806 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 --no_mapping;
808 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000809 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000811 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 vungetc(c);
813 c = Ctrl_BSL;
814 }
815 else if (c == Ctrl_G && p_im)
816 continue;
817 else
818 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000819 if (c == Ctrl_O)
820 {
821 ins_ctrl_o();
822 ins_at_eol = FALSE; /* cursor keeps its column */
823 nomove = TRUE;
824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 count = 0;
826 goto doESCkey;
827 }
828 }
829
830#ifdef FEAT_DIGRAPHS
831 c = do_digraph(c);
832#endif
833
834#ifdef FEAT_INS_EXPAND
835 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
836 goto docomplete;
837#endif
838 if (c == Ctrl_V || c == Ctrl_Q)
839 {
840 ins_ctrl_v();
841 c = Ctrl_V; /* pretend CTRL-V is last typed character */
842 continue;
843 }
844
845#ifdef FEAT_CINDENT
846 if (cindent_on()
847# ifdef FEAT_INS_EXPAND
848 && ctrl_x_mode == 0
849# endif
850 )
851 {
852 /* A key name preceded by a bang means this key is not to be
853 * inserted. Skip ahead to the re-indenting below.
854 * A key name preceded by a star means that indenting has to be
855 * done before inserting the key. */
856 line_is_white = inindent(0);
857 if (in_cinkeys(c, '!', line_is_white))
858 goto force_cindent;
859 if (can_cindent && in_cinkeys(c, '*', line_is_white)
860 && stop_arrow() == OK)
861 do_c_expr_indent();
862 }
863#endif
864
865#ifdef FEAT_RIGHTLEFT
866 if (curwin->w_p_rl)
867 switch (c)
868 {
869 case K_LEFT: c = K_RIGHT; break;
870 case K_S_LEFT: c = K_S_RIGHT; break;
871 case K_C_LEFT: c = K_C_RIGHT; break;
872 case K_RIGHT: c = K_LEFT; break;
873 case K_S_RIGHT: c = K_S_LEFT; break;
874 case K_C_RIGHT: c = K_C_LEFT; break;
875 }
876#endif
877
878#ifdef FEAT_VISUAL
879 /*
880 * If 'keymodel' contains "startsel", may start selection. If it
881 * does, a CTRL-O and c will be stuffed, we need to get these
882 * characters.
883 */
884 if (ins_start_select(c))
885 continue;
886#endif
887
888 /*
889 * The big switch to handle a character in insert mode.
890 */
891 switch (c)
892 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000893 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (echeck_abbr(ESC + ABBR_OFF))
895 break;
896 /*FALLTHROUGH*/
897
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000898 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899#ifdef FEAT_CMDWIN
900 if (c == Ctrl_C && cmdwin_type != 0)
901 {
902 /* Close the cmdline window. */
903 cmdwin_result = K_IGNORE;
904 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000905 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 goto doESCkey;
907 }
908#endif
909
910#ifdef UNIX
911do_intr:
912#endif
913 /* when 'insertmode' set, and not halfway a mapping, don't leave
914 * Insert mode */
915 if (goto_im())
916 {
917 if (got_int)
918 {
919 (void)vgetc(); /* flush all buffers */
920 got_int = FALSE;
921 }
922 else
923 vim_beep();
924 break;
925 }
926doESCkey:
927 /*
928 * This is the ONLY return from edit()!
929 */
930 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
931 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000932 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 o_lnum = curwin->w_cursor.lnum;
934
Bram Moolenaar488c6512005-08-11 20:09:58 +0000935 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000936 {
937#ifdef FEAT_AUTOCMD
938 if (cmdchar != 'r' && cmdchar != 'v')
939 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
940 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000941 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 continue;
946
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000947 case Ctrl_Z: /* suspend when 'insertmode' set */
948 if (!p_im)
949 goto normalchar; /* insert CTRL-Z as normal char */
950 stuffReadbuff((char_u *)":st\r");
951 c = Ctrl_O;
952 /*FALLTHROUGH*/
953
954 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000955#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000956 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000957 goto docomplete;
958#endif
959 if (echeck_abbr(Ctrl_O + ABBR_OFF))
960 break;
961 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000962
963#ifdef FEAT_VIRTUALEDIT
964 /* don't move the cursor left when 'virtualedit' has "onemore". */
965 if (ve_flags & VE_ONEMORE)
966 {
967 ins_at_eol = FALSE;
968 nomove = TRUE;
969 }
970#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000971 count = 0;
972 goto doESCkey;
973
Bram Moolenaar572cb562005-08-05 21:35:02 +0000974 case K_INS: /* toggle insert/replace mode */
975 case K_KINS:
976 ins_insert(replaceState);
977 break;
978
979 case K_SELECT: /* end of Select mode mapping - ignore */
980 break;
981
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000982#ifdef FEAT_SNIFF
983 case K_SNIFF: /* Sniff command received */
984 stuffcharReadbuff(K_SNIFF);
985 goto doESCkey;
986#endif
987
988 case K_HELP: /* Help key works like <ESC> <Help> */
989 case K_F1:
990 case K_XF1:
991 stuffcharReadbuff(K_HELP);
992 if (p_im)
993 need_start_insertmode = TRUE;
994 goto doESCkey;
995
996#ifdef FEAT_NETBEANS_INTG
997 case K_F21: /* NetBeans command */
998 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000999 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 --no_mapping;
1001 netbeans_keycommand(i);
1002 break;
1003#endif
1004
1005 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 case NUL:
1007 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001008 /* For ^@ the trailing ESC will end the insert, unless there is an
1009 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1011 && c != Ctrl_A && !p_im)
1012 goto doESCkey; /* quit insert mode */
1013 inserted_space = FALSE;
1014 break;
1015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 ins_reg();
1018 auto_format(FALSE, TRUE);
1019 inserted_space = FALSE;
1020 break;
1021
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 ins_ctrl_g();
1024 break;
1025
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001026 case Ctrl_HAT: /* switch input mode and/or langmap */
1027 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 break;
1029
1030#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001031 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (!p_ari)
1033 goto normalchar;
1034 ins_ctrl_();
1035 break;
1036#endif
1037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1040 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1041 goto docomplete;
1042#endif
1043 /* FALLTHROUGH */
1044
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046# ifdef FEAT_INS_EXPAND
1047 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1048 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 if (has_compl_option(FALSE))
1050 goto docomplete;
1051 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 }
1053# endif
1054 ins_shift(c, lastc);
1055 auto_format(FALSE, TRUE);
1056 inserted_space = FALSE;
1057 break;
1058
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001059 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 case K_KDEL:
1061 ins_del();
1062 auto_format(FALSE, TRUE);
1063 break;
1064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 case Ctrl_H:
1067 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1068 auto_format(FALSE, TRUE);
1069 break;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1073 auto_format(FALSE, TRUE);
1074 break;
1075
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001076 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001077# ifdef FEAT_COMPL_FUNC
1078 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001080 goto docomplete;
1081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1083 auto_format(FALSE, TRUE);
1084 inserted_space = FALSE;
1085 break;
1086
1087#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 case K_LEFTMOUSE_NM:
1090 case K_LEFTDRAG:
1091 case K_LEFTRELEASE:
1092 case K_LEFTRELEASE_NM:
1093 case K_MIDDLEMOUSE:
1094 case K_MIDDLEDRAG:
1095 case K_MIDDLERELEASE:
1096 case K_RIGHTMOUSE:
1097 case K_RIGHTDRAG:
1098 case K_RIGHTRELEASE:
1099 case K_X1MOUSE:
1100 case K_X1DRAG:
1101 case K_X1RELEASE:
1102 case K_X2MOUSE:
1103 case K_X2DRAG:
1104 case K_X2RELEASE:
1105 ins_mouse(c);
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_mousescroll(FALSE);
1110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 ins_mousescroll(TRUE);
1114 break;
1115#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001116#ifdef FEAT_GUI_TABLINE
1117 case K_TABLINE:
1118 case K_TABMENU:
1119 ins_tabline(c);
1120 break;
1121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001123 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 break;
1125
Bram Moolenaar754b5602006-02-09 23:53:20 +00001126#ifdef FEAT_AUTOCMD
1127 case K_CURSORHOLD: /* Didn't type something for a while. */
1128 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1129 did_cursorhold = TRUE;
1130 break;
1131#endif
1132
Bram Moolenaar4770d092006-01-12 23:22:24 +00001133#ifdef FEAT_GUI_W32
1134 /* On Win32 ignore <M-F4>, we get it when closing the window was
1135 * cancelled. */
1136 case K_F4:
1137 if (mod_mask != MOD_MASK_ALT)
1138 goto normalchar;
1139 break;
1140#endif
1141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#ifdef FEAT_GUI
1143 case K_VER_SCROLLBAR:
1144 ins_scroll();
1145 break;
1146
1147 case K_HOR_SCROLLBAR:
1148 ins_horscroll();
1149 break;
1150#endif
1151
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001152 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 case K_S_HOME:
1155 case K_C_HOME:
1156 ins_home(c);
1157 break;
1158
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001159 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 case K_S_END:
1162 case K_C_END:
1163 ins_end(c);
1164 break;
1165
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001166 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001167 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1168 ins_s_left();
1169 else
1170 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 break;
1172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001173 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 case K_C_LEFT:
1175 ins_s_left();
1176 break;
1177
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001178 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001179 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1180 ins_s_right();
1181 else
1182 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 break;
1184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001185 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 case K_C_RIGHT:
1187 ins_s_right();
1188 break;
1189
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001190 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001191#ifdef FEAT_INS_EXPAND
1192 if (pum_visible())
1193 goto docomplete;
1194#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001195 if (mod_mask & MOD_MASK_SHIFT)
1196 ins_pageup();
1197 else
1198 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 break;
1200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 case K_PAGEUP:
1203 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001204#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001205 if (pum_visible())
1206 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 ins_pageup();
1209 break;
1210
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001211 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001212#ifdef FEAT_INS_EXPAND
1213 if (pum_visible())
1214 goto docomplete;
1215#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001216 if (mod_mask & MOD_MASK_SHIFT)
1217 ins_pagedown();
1218 else
1219 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 break;
1221
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001222 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 case K_PAGEDOWN:
1224 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001225#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001226 if (pum_visible())
1227 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 ins_pagedown();
1230 break;
1231
1232#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001233 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 ins_drop();
1235 break;
1236#endif
1237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 c = TAB;
1240 /* FALLTHROUGH */
1241
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001242 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1244 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1245 goto docomplete;
1246#endif
1247 inserted_space = FALSE;
1248 if (ins_tab())
1249 goto normalchar; /* insert TAB as a normal char */
1250 auto_format(FALSE, TRUE);
1251 break;
1252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 c = CAR;
1255 /* FALLTHROUGH */
1256 case CAR:
1257 case NL:
1258#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1259 /* In a quickfix window a <CR> jumps to the error under the
1260 * cursor. */
1261 if (bt_quickfix(curbuf) && c == CAR)
1262 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001263 if (curwin->w_llist_ref == NULL) /* quickfix window */
1264 do_cmdline_cmd((char_u *)".cc");
1265 else /* location list window */
1266 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 break;
1268 }
1269#endif
1270#ifdef FEAT_CMDWIN
1271 if (cmdwin_type != 0)
1272 {
1273 /* Execute the command in the cmdline window. */
1274 cmdwin_result = CAR;
1275 goto doESCkey;
1276 }
1277#endif
1278 if (ins_eol(c) && !p_im)
1279 goto doESCkey; /* out of memory */
1280 auto_format(FALSE, FALSE);
1281 inserted_space = FALSE;
1282 break;
1283
Bram Moolenaar860cae12010-06-05 23:22:07 +02001284#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286# ifdef FEAT_INS_EXPAND
1287 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1288 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 if (has_compl_option(TRUE))
1290 goto docomplete;
1291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293# endif
1294# ifdef FEAT_DIGRAPHS
1295 c = ins_digraph();
1296 if (c == NUL)
1297 break;
1298# endif
1299 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301
1302#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001303 case Ctrl_X: /* Enter CTRL-X mode */
1304 ins_ctrl_x();
1305 break;
1306
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001307 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if (ctrl_x_mode != CTRL_X_TAGS)
1309 goto normalchar;
1310 goto docomplete;
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 if (ctrl_x_mode != CTRL_X_FILES)
1314 goto normalchar;
1315 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001316
1317 case 's': /* Spelling completion after ^X */
1318 case Ctrl_S:
1319 if (ctrl_x_mode != CTRL_X_SPELL)
1320 goto normalchar;
1321 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#endif
1323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001324 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325#ifdef FEAT_INS_EXPAND
1326 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1327#endif
1328 {
1329 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1330 if (p_im)
1331 {
1332 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1333 break;
1334 goto doESCkey;
1335 }
1336 goto normalchar;
1337 }
1338#ifdef FEAT_INS_EXPAND
1339 /* FALLTHROUGH */
1340
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001341 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 case Ctrl_N:
1343 /* if 'complete' is empty then plain ^P is no longer special,
1344 * but it is under other ^X modes */
1345 if (*curbuf->b_p_cpt == NUL
1346 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001347 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 goto normalchar;
1349
1350docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001351 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001353 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001354 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 break;
1356#endif /* FEAT_INS_EXPAND */
1357
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001358 case Ctrl_Y: /* copy from previous line or scroll down */
1359 case Ctrl_E: /* copy from next line or scroll up */
1360 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 break;
1362
1363 default:
1364#ifdef UNIX
1365 if (c == intr_char) /* special interrupt char */
1366 goto do_intr;
1367#endif
1368
1369 /*
1370 * Insert a nomal character.
1371 */
1372normalchar:
1373#ifdef FEAT_SMARTINDENT
1374 /* Try to perform smart-indenting. */
1375 ins_try_si(c);
1376#endif
1377
1378 if (c == ' ')
1379 {
1380 inserted_space = TRUE;
1381#ifdef FEAT_CINDENT
1382 if (inindent(0))
1383 can_cindent = FALSE;
1384#endif
1385 if (Insstart_blank_vcol == MAXCOL
1386 && curwin->w_cursor.lnum == Insstart.lnum)
1387 Insstart_blank_vcol = get_nolist_virtcol();
1388 }
1389
1390 if (vim_iswordc(c) || !echeck_abbr(
1391#ifdef FEAT_MBYTE
1392 /* Add ABBR_OFF for characters above 0x100, this is
1393 * what check_abbr() expects. */
1394 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1395#endif
1396 c))
1397 {
1398 insert_special(c, FALSE, FALSE);
1399#ifdef FEAT_RIGHTLEFT
1400 revins_legal++;
1401 revins_chars++;
1402#endif
1403 }
1404
1405 auto_format(FALSE, TRUE);
1406
1407#ifdef FEAT_FOLDING
1408 /* When inserting a character the cursor line must never be in a
1409 * closed fold. */
1410 foldOpenCursor();
1411#endif
1412 break;
1413 } /* end of switch (c) */
1414
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001415#ifdef FEAT_AUTOCMD
1416 /* If typed something may trigger CursorHoldI again. */
1417 if (c != K_CURSORHOLD)
1418 did_cursorhold = FALSE;
1419#endif
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 /* If the cursor was moved we didn't just insert a space */
1422 if (arrow_used)
1423 inserted_space = FALSE;
1424
1425#ifdef FEAT_CINDENT
1426 if (can_cindent && cindent_on()
1427# ifdef FEAT_INS_EXPAND
1428 && ctrl_x_mode == 0
1429# endif
1430 )
1431 {
1432force_cindent:
1433 /*
1434 * Indent now if a key was typed that is in 'cinkeys'.
1435 */
1436 if (in_cinkeys(c, ' ', line_is_white))
1437 {
1438 if (stop_arrow() == OK)
1439 /* re-indent the current line */
1440 do_c_expr_indent();
1441 }
1442 }
1443#endif /* FEAT_CINDENT */
1444
1445 } /* for (;;) */
1446 /* NOTREACHED */
1447}
1448
1449/*
1450 * Redraw for Insert mode.
1451 * This is postponed until getting the next character to make '$' in the 'cpo'
1452 * option work correctly.
1453 * Only redraw when there are no characters available. This speeds up
1454 * inserting sequences of characters (e.g., for CTRL-R).
1455 */
1456 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001457ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001458 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
1460 if (!char_avail())
1461 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001462#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001463 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1464 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001465 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001466 && !equalpos(last_cursormoved, curwin->w_cursor)
1467# ifdef FEAT_INS_EXPAND
1468 && !pum_visible()
1469# endif
1470 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001471 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001472# ifdef FEAT_SYN_HL
1473 /* Need to update the screen first, to make sure syntax
1474 * highlighting is correct after making a change (e.g., inserting
1475 * a "(". The autocommand may also require a redraw, so it's done
1476 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001477 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001478 update_screen(0);
1479# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001480 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1481 last_cursormoved = curwin->w_cursor;
1482 }
1483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484 if (must_redraw)
1485 update_screen(0);
1486 else if (clear_cmdline || redraw_cmdline)
1487 showmode(); /* clear cmdline and show mode */
1488 showruler(FALSE);
1489 setcursor();
1490 emsg_on_display = FALSE; /* may remove error message now */
1491 }
1492}
1493
1494/*
1495 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1496 */
1497 static void
1498ins_ctrl_v()
1499{
1500 int c;
1501
1502 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001503 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504
1505 if (redrawing() && !char_avail())
1506 edit_putchar('^', TRUE);
1507 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1508
1509#ifdef FEAT_CMDL_INFO
1510 add_to_showcmd_c(Ctrl_V);
1511#endif
1512
1513 c = get_literal();
1514#ifdef FEAT_CMDL_INFO
1515 clear_showcmd();
1516#endif
1517 insert_special(c, FALSE, TRUE);
1518#ifdef FEAT_RIGHTLEFT
1519 revins_chars++;
1520 revins_legal++;
1521#endif
1522}
1523
1524/*
1525 * Put a character directly onto the screen. It's not stored in a buffer.
1526 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1527 */
1528static int pc_status;
1529#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1530#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1531#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1532#define PC_STATUS_SET 3 /* pc_bytes was filled */
1533#ifdef FEAT_MBYTE
1534static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1535#else
1536static char_u pc_bytes[2]; /* saved bytes */
1537#endif
1538static int pc_attr;
1539static int pc_row;
1540static int pc_col;
1541
1542 void
1543edit_putchar(c, highlight)
1544 int c;
1545 int highlight;
1546{
1547 int attr;
1548
1549 if (ScreenLines != NULL)
1550 {
1551 update_topline(); /* just in case w_topline isn't valid */
1552 validate_cursor();
1553 if (highlight)
1554 attr = hl_attr(HLF_8);
1555 else
1556 attr = 0;
1557 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1558 pc_col = W_WINCOL(curwin);
1559#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1560 pc_status = PC_STATUS_UNSET;
1561#endif
1562#ifdef FEAT_RIGHTLEFT
1563 if (curwin->w_p_rl)
1564 {
1565 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1566# ifdef FEAT_MBYTE
1567 if (has_mbyte)
1568 {
1569 int fix_col = mb_fix_col(pc_col, pc_row);
1570
1571 if (fix_col != pc_col)
1572 {
1573 screen_putchar(' ', pc_row, fix_col, attr);
1574 --curwin->w_wcol;
1575 pc_status = PC_STATUS_RIGHT;
1576 }
1577 }
1578# endif
1579 }
1580 else
1581#endif
1582 {
1583 pc_col += curwin->w_wcol;
1584#ifdef FEAT_MBYTE
1585 if (mb_lefthalve(pc_row, pc_col))
1586 pc_status = PC_STATUS_LEFT;
1587#endif
1588 }
1589
1590 /* save the character to be able to put it back */
1591#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1592 if (pc_status == PC_STATUS_UNSET)
1593#endif
1594 {
1595 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1596 pc_status = PC_STATUS_SET;
1597 }
1598 screen_putchar(c, pc_row, pc_col, attr);
1599 }
1600}
1601
1602/*
1603 * Undo the previous edit_putchar().
1604 */
1605 void
1606edit_unputchar()
1607{
1608 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1609 {
1610#if defined(FEAT_MBYTE)
1611 if (pc_status == PC_STATUS_RIGHT)
1612 ++curwin->w_wcol;
1613 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1614 redrawWinline(curwin->w_cursor.lnum, FALSE);
1615 else
1616#endif
1617 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1618 }
1619}
1620
1621/*
1622 * Called when p_dollar is set: display a '$' at the end of the changed text
1623 * Only works when cursor is in the line that changes.
1624 */
1625 void
1626display_dollar(col)
1627 colnr_T col;
1628{
1629 colnr_T save_col;
1630
1631 if (!redrawing())
1632 return;
1633
1634 cursor_off();
1635 save_col = curwin->w_cursor.col;
1636 curwin->w_cursor.col = col;
1637#ifdef FEAT_MBYTE
1638 if (has_mbyte)
1639 {
1640 char_u *p;
1641
1642 /* If on the last byte of a multi-byte move to the first byte. */
1643 p = ml_get_curline();
1644 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1645 }
1646#endif
1647 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1648 if (curwin->w_wcol < W_WIDTH(curwin))
1649 {
1650 edit_putchar('$', FALSE);
1651 dollar_vcol = curwin->w_virtcol;
1652 }
1653 curwin->w_cursor.col = save_col;
1654}
1655
1656/*
1657 * Call this function before moving the cursor from the normal insert position
1658 * in insert mode.
1659 */
1660 static void
1661undisplay_dollar()
1662{
1663 if (dollar_vcol)
1664 {
1665 dollar_vcol = 0;
1666 redrawWinline(curwin->w_cursor.lnum, FALSE);
1667 }
1668}
1669
1670/*
1671 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1672 * Keep the cursor on the same character.
1673 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1674 * type == INDENT_DEC decrease indent (for CTRL-D)
1675 * type == INDENT_SET set indent to "amount"
1676 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1677 */
1678 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001679change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 int type;
1681 int amount;
1682 int round;
1683 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001684 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685{
1686 int vcol;
1687 int last_vcol;
1688 int insstart_less; /* reduction for Insstart.col */
1689 int new_cursor_col;
1690 int i;
1691 char_u *ptr;
1692 int save_p_list;
1693 int start_col;
1694 colnr_T vc;
1695#ifdef FEAT_VREPLACE
1696 colnr_T orig_col = 0; /* init for GCC */
1697 char_u *new_line, *orig_line = NULL; /* init for GCC */
1698
1699 /* VREPLACE mode needs to know what the line was like before changing */
1700 if (State & VREPLACE_FLAG)
1701 {
1702 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1703 orig_col = curwin->w_cursor.col;
1704 }
1705#endif
1706
1707 /* for the following tricks we don't want list mode */
1708 save_p_list = curwin->w_p_list;
1709 curwin->w_p_list = FALSE;
1710 vc = getvcol_nolist(&curwin->w_cursor);
1711 vcol = vc;
1712
1713 /*
1714 * For Replace mode we need to fix the replace stack later, which is only
1715 * possible when the cursor is in the indent. Remember the number of
1716 * characters before the cursor if it's possible.
1717 */
1718 start_col = curwin->w_cursor.col;
1719
1720 /* determine offset from first non-blank */
1721 new_cursor_col = curwin->w_cursor.col;
1722 beginline(BL_WHITE);
1723 new_cursor_col -= curwin->w_cursor.col;
1724
1725 insstart_less = curwin->w_cursor.col;
1726
1727 /*
1728 * If the cursor is in the indent, compute how many screen columns the
1729 * cursor is to the left of the first non-blank.
1730 */
1731 if (new_cursor_col < 0)
1732 vcol = get_indent() - vcol;
1733
1734 if (new_cursor_col > 0) /* can't fix replace stack */
1735 start_col = -1;
1736
1737 /*
1738 * Set the new indent. The cursor will be put on the first non-blank.
1739 */
1740 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001741 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 else
1743 {
1744#ifdef FEAT_VREPLACE
1745 int save_State = State;
1746
1747 /* Avoid being called recursively. */
1748 if (State & VREPLACE_FLAG)
1749 State = INSERT;
1750#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001751 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001752#ifdef FEAT_VREPLACE
1753 State = save_State;
1754#endif
1755 }
1756 insstart_less -= curwin->w_cursor.col;
1757
1758 /*
1759 * Try to put cursor on same character.
1760 * If the cursor is at or after the first non-blank in the line,
1761 * compute the cursor column relative to the column of the first
1762 * non-blank character.
1763 * If we are not in insert mode, leave the cursor on the first non-blank.
1764 * If the cursor is before the first non-blank, position it relative
1765 * to the first non-blank, counted in screen columns.
1766 */
1767 if (new_cursor_col >= 0)
1768 {
1769 /*
1770 * When changing the indent while the cursor is touching it, reset
1771 * Insstart_col to 0.
1772 */
1773 if (new_cursor_col == 0)
1774 insstart_less = MAXCOL;
1775 new_cursor_col += curwin->w_cursor.col;
1776 }
1777 else if (!(State & INSERT))
1778 new_cursor_col = curwin->w_cursor.col;
1779 else
1780 {
1781 /*
1782 * Compute the screen column where the cursor should be.
1783 */
1784 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001785 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786
1787 /*
1788 * Advance the cursor until we reach the right screen column.
1789 */
1790 vcol = last_vcol = 0;
1791 new_cursor_col = -1;
1792 ptr = ml_get_curline();
1793 while (vcol <= (int)curwin->w_virtcol)
1794 {
1795 last_vcol = vcol;
1796#ifdef FEAT_MBYTE
1797 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001798 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 else
1800#endif
1801 ++new_cursor_col;
1802 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1803 }
1804 vcol = last_vcol;
1805
1806 /*
1807 * May need to insert spaces to be able to position the cursor on
1808 * the right screen column.
1809 */
1810 if (vcol != (int)curwin->w_virtcol)
1811 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001812 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001813 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001814 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001815 if (ptr != NULL)
1816 {
1817 new_cursor_col += i;
1818 ptr[i] = NUL;
1819 while (--i >= 0)
1820 ptr[i] = ' ';
1821 ins_str(ptr);
1822 vim_free(ptr);
1823 }
1824 }
1825
1826 /*
1827 * When changing the indent while the cursor is in it, reset
1828 * Insstart_col to 0.
1829 */
1830 insstart_less = MAXCOL;
1831 }
1832
1833 curwin->w_p_list = save_p_list;
1834
1835 if (new_cursor_col <= 0)
1836 curwin->w_cursor.col = 0;
1837 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001838 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001839 curwin->w_set_curswant = TRUE;
1840 changed_cline_bef_curs();
1841
1842 /*
1843 * May have to adjust the start of the insert.
1844 */
1845 if (State & INSERT)
1846 {
1847 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1848 {
1849 if ((int)Insstart.col <= insstart_less)
1850 Insstart.col = 0;
1851 else
1852 Insstart.col -= insstart_less;
1853 }
1854 if ((int)ai_col <= insstart_less)
1855 ai_col = 0;
1856 else
1857 ai_col -= insstart_less;
1858 }
1859
1860 /*
1861 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1862 * If the number of characters before the cursor decreased, need to pop a
1863 * few characters from the replace stack.
1864 * If the number of characters before the cursor increased, need to push a
1865 * few NULs onto the replace stack.
1866 */
1867 if (REPLACE_NORMAL(State) && start_col >= 0)
1868 {
1869 while (start_col > (int)curwin->w_cursor.col)
1870 {
1871 replace_join(0); /* remove a NUL from the replace stack */
1872 --start_col;
1873 }
1874 while (start_col < (int)curwin->w_cursor.col || replaced)
1875 {
1876 replace_push(NUL);
1877 if (replaced)
1878 {
1879 replace_push(replaced);
1880 replaced = NUL;
1881 }
1882 ++start_col;
1883 }
1884 }
1885
1886#ifdef FEAT_VREPLACE
1887 /*
1888 * For VREPLACE mode, we also have to fix the replace stack. In this case
1889 * it is always possible because we backspace over the whole line and then
1890 * put it back again the way we wanted it.
1891 */
1892 if (State & VREPLACE_FLAG)
1893 {
1894 /* If orig_line didn't allocate, just return. At least we did the job,
1895 * even if you can't backspace. */
1896 if (orig_line == NULL)
1897 return;
1898
1899 /* Save new line */
1900 new_line = vim_strsave(ml_get_curline());
1901 if (new_line == NULL)
1902 return;
1903
1904 /* We only put back the new line up to the cursor */
1905 new_line[curwin->w_cursor.col] = NUL;
1906
1907 /* Put back original line */
1908 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1909 curwin->w_cursor.col = orig_col;
1910
1911 /* Backspace from cursor to start of line */
1912 backspace_until_column(0);
1913
1914 /* Insert new stuff into line again */
1915 ins_bytes(new_line);
1916
1917 vim_free(new_line);
1918 }
1919#endif
1920}
1921
1922/*
1923 * Truncate the space at the end of a line. This is to be used only in an
1924 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1925 * modes.
1926 */
1927 void
1928truncate_spaces(line)
1929 char_u *line;
1930{
1931 int i;
1932
1933 /* find start of trailing white space */
1934 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1935 {
1936 if (State & REPLACE_FLAG)
1937 replace_join(0); /* remove a NUL from the replace stack */
1938 }
1939 line[i + 1] = NUL;
1940}
1941
1942#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1943 || defined(FEAT_COMMENTS) || defined(PROTO)
1944/*
1945 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1946 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001947 * Will attempt not to go before "col" even when there is a composing
1948 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 */
1950 void
1951backspace_until_column(col)
1952 int col;
1953{
1954 while ((int)curwin->w_cursor.col > col)
1955 {
1956 curwin->w_cursor.col--;
1957 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001958 replace_do_bs(col);
1959 else if (!del_char_after_col(col))
1960 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 }
1962}
1963#endif
1964
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001965/*
1966 * Like del_char(), but make sure not to go before column "limit_col".
1967 * Only matters when there are composing characters.
1968 * Return TRUE when something was deleted.
1969 */
1970 static int
1971del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001972 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001973{
1974#ifdef FEAT_MBYTE
1975 if (enc_utf8 && limit_col >= 0)
1976 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001977 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001978
1979 /* Make sure the cursor is at the start of a character, but
1980 * skip forward again when going too far back because of a
1981 * composing character. */
1982 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001983 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001984 {
1985 int l = utf_ptr2len(ml_get_cursor());
1986
1987 if (l == 0) /* end of line */
1988 break;
1989 curwin->w_cursor.col += l;
1990 }
1991 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1992 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001993 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001994 }
1995 else
1996#endif
1997 (void)del_char(FALSE);
1998 return TRUE;
1999}
2000
Bram Moolenaar071d4272004-06-13 20:20:40 +00002001#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2002/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002003 * CTRL-X pressed in Insert mode.
2004 */
2005 static void
2006ins_ctrl_x()
2007{
2008 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2009 * CTRL-V works like CTRL-N */
2010 if (ctrl_x_mode != CTRL_X_CMDLINE)
2011 {
2012 /* if the next ^X<> won't ADD nothing, then reset
2013 * compl_cont_status */
2014 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002015 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002016 else
2017 compl_cont_status = 0;
2018 /* We're not sure which CTRL-X mode it will be yet */
2019 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2020 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2021 edit_submode_pre = NULL;
2022 showmode();
2023 }
2024}
2025
2026/*
2027 * Return TRUE if the 'dict' or 'tsr' option can be used.
2028 */
2029 static int
2030has_compl_option(dict_opt)
2031 int dict_opt;
2032{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002033 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002034# ifdef FEAT_SPELL
2035 && !curwin->w_p_spell
2036# endif
2037 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002038 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2039 {
2040 ctrl_x_mode = 0;
2041 edit_submode = NULL;
2042 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2043 : (char_u *)_("'thesaurus' option is empty"),
2044 hl_attr(HLF_E));
2045 if (emsg_silent == 0)
2046 {
2047 vim_beep();
2048 setcursor();
2049 out_flush();
2050 ui_delay(2000L, FALSE);
2051 }
2052 return FALSE;
2053 }
2054 return TRUE;
2055}
2056
2057/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2059 * This depends on the current mode.
2060 */
2061 int
2062vim_is_ctrl_x_key(c)
2063 int c;
2064{
2065 /* Always allow ^R - let it's results then be checked */
2066 if (c == Ctrl_R)
2067 return TRUE;
2068
Bram Moolenaare3226be2005-12-18 22:10:00 +00002069 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002070 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002071 return TRUE;
2072
Bram Moolenaar071d4272004-06-13 20:20:40 +00002073 switch (ctrl_x_mode)
2074 {
2075 case 0: /* Not in any CTRL-X mode */
2076 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2077 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002078 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002079 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2080 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2081 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002082 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2083 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002084 case CTRL_X_SCROLL:
2085 return (c == Ctrl_Y || c == Ctrl_E);
2086 case CTRL_X_WHOLE_LINE:
2087 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2088 case CTRL_X_FILES:
2089 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2090 case CTRL_X_DICTIONARY:
2091 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2092 case CTRL_X_THESAURUS:
2093 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2094 case CTRL_X_TAGS:
2095 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2096#ifdef FEAT_FIND_ID
2097 case CTRL_X_PATH_PATTERNS:
2098 return (c == Ctrl_P || c == Ctrl_N);
2099 case CTRL_X_PATH_DEFINES:
2100 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2101#endif
2102 case CTRL_X_CMDLINE:
2103 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2104 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002105#ifdef FEAT_COMPL_FUNC
2106 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002107 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002108 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002109 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002110#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002111 case CTRL_X_SPELL:
2112 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002113 }
2114 EMSG(_(e_internal));
2115 return FALSE;
2116}
2117
2118/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002119 * Return TRUE when character "c" is part of the item currently being
2120 * completed. Used to decide whether to abandon complete mode when the menu
2121 * is visible.
2122 */
2123 static int
2124ins_compl_accept_char(c)
2125 int c;
2126{
2127 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2128 /* When expanding an identifier only accept identifier chars. */
2129 return vim_isIDc(c);
2130
2131 switch (ctrl_x_mode)
2132 {
2133 case CTRL_X_FILES:
2134 /* When expanding file name only accept file name chars. But not
2135 * path separators, so that "proto/<Tab>" expands files in
2136 * "proto", not "proto/" as a whole */
2137 return vim_isfilec(c) && !vim_ispathsep(c);
2138
2139 case CTRL_X_CMDLINE:
2140 case CTRL_X_OMNI:
2141 /* Command line and Omni completion can work with just about any
2142 * printable character, but do stop at white space. */
2143 return vim_isprintc(c) && !vim_iswhite(c);
2144
2145 case CTRL_X_WHOLE_LINE:
2146 /* For while line completion a space can be part of the line. */
2147 return vim_isprintc(c);
2148 }
2149 return vim_iswordc(c);
2150}
2151
2152/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002153 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002155 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 * the rest of the word to be in -- webb
2157 */
2158 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002159ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002160 char_u *str;
2161 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002162 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 char_u *fname;
2164 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002165 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002167 char_u *p;
2168 int i, c;
2169 int actual_len; /* Take multi-byte characters */
2170 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002171 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002172 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 int has_lower = FALSE;
2174 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002176 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002178 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179
Bram Moolenaara2993e12007-08-12 14:38:46 +00002180 /* Find actual length of completion. */
2181#ifdef FEAT_MBYTE
2182 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002184 p = str;
2185 actual_len = 0;
2186 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002187 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002188 mb_ptr_adv(p);
2189 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 }
2191 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002192 else
2193#endif
2194 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195
Bram Moolenaara2993e12007-08-12 14:38:46 +00002196 /* Find actual length of original text. */
2197#ifdef FEAT_MBYTE
2198 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002200 p = compl_orig_text;
2201 actual_compl_length = 0;
2202 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002203 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002204 mb_ptr_adv(p);
2205 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002206 }
2207 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002208 else
2209#endif
2210 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002212 /* "actual_len" may be smaller than "actual_compl_length" when using
2213 * thesaurus, only use the minimum when comparing. */
2214 min_len = actual_len < actual_compl_length
2215 ? actual_len : actual_compl_length;
2216
Bram Moolenaara2993e12007-08-12 14:38:46 +00002217 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002218 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002219 if (wca != NULL)
2220 {
2221 p = str;
2222 for (i = 0; i < actual_len; ++i)
2223#ifdef FEAT_MBYTE
2224 if (has_mbyte)
2225 wca[i] = mb_ptr2char_adv(&p);
2226 else
2227#endif
2228 wca[i] = *(p++);
2229
2230 /* Rule 1: Were any chars converted to lower? */
2231 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002232 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002233 {
2234#ifdef FEAT_MBYTE
2235 if (has_mbyte)
2236 c = mb_ptr2char_adv(&p);
2237 else
2238#endif
2239 c = *(p++);
2240 if (MB_ISLOWER(c))
2241 {
2242 has_lower = TRUE;
2243 if (MB_ISUPPER(wca[i]))
2244 {
2245 /* Rule 1 is satisfied. */
2246 for (i = actual_compl_length; i < actual_len; ++i)
2247 wca[i] = MB_TOLOWER(wca[i]);
2248 break;
2249 }
2250 }
2251 }
2252
2253 /*
2254 * Rule 2: No lower case, 2nd consecutive letter converted to
2255 * upper case.
2256 */
2257 if (!has_lower)
2258 {
2259 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002260 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002261 {
2262#ifdef FEAT_MBYTE
2263 if (has_mbyte)
2264 c = mb_ptr2char_adv(&p);
2265 else
2266#endif
2267 c = *(p++);
2268 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2269 {
2270 /* Rule 2 is satisfied. */
2271 for (i = actual_compl_length; i < actual_len; ++i)
2272 wca[i] = MB_TOUPPER(wca[i]);
2273 break;
2274 }
2275 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2276 }
2277 }
2278
2279 /* Copy the original case of the part we typed. */
2280 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002281 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002282 {
2283#ifdef FEAT_MBYTE
2284 if (has_mbyte)
2285 c = mb_ptr2char_adv(&p);
2286 else
2287#endif
2288 c = *(p++);
2289 if (MB_ISLOWER(c))
2290 wca[i] = MB_TOLOWER(wca[i]);
2291 else if (MB_ISUPPER(c))
2292 wca[i] = MB_TOUPPER(wca[i]);
2293 }
2294
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002295 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002296 * Generate encoding specific output from wide character array.
2297 * Multi-byte characters can occupy up to five bytes more than
2298 * ASCII characters, and we also need one byte for NUL, so stay
2299 * six bytes away from the edge of IObuff.
2300 */
2301 p = IObuff;
2302 i = 0;
2303 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2304#ifdef FEAT_MBYTE
2305 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002306 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002307 else
2308#endif
2309 *(p++) = wca[i++];
2310 *p = NUL;
2311
2312 vim_free(wca);
2313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002315 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2316 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002318 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002319}
2320
2321/*
2322 * Add a match to the list of matches.
2323 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002324 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002325 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002327 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002328ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 char_u *str;
2330 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002331 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002333 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002334 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002335 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002336 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002338 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002339 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340
2341 ui_breakcheck();
2342 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002343 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002344 if (len < 0)
2345 len = (int)STRLEN(str);
2346
2347 /*
2348 * If the same match is already present, don't add it.
2349 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002350 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002352 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 do
2354 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002355 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002356 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002357 && match->cp_str[len] == NUL)
2358 return NOTDONE;
2359 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002360 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002361 }
2362
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002363 /* Remove any popup menu before changing the list of matches. */
2364 ins_compl_del_pum();
2365
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 /*
2367 * Allocate a new match structure.
2368 * Copy the values to the new match structure.
2369 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002370 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002372 return FAIL;
2373 match->cp_number = -1;
2374 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002375 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002376 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002377 {
2378 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002379 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002381 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002382
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002384 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2386 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002387 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002388 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002389 && compl_curr_match->cp_fname != NULL
2390 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002391 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002392 else if (fname != NULL)
2393 {
2394 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002395 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002398 match->cp_fname = NULL;
2399 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002400
2401 if (cptext != NULL)
2402 {
2403 int i;
2404
2405 for (i = 0; i < CPT_COUNT; ++i)
2406 if (cptext[i] != NULL && *cptext[i] != NUL)
2407 match->cp_text[i] = vim_strsave(cptext[i]);
2408 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409
2410 /*
2411 * Link the new match structure in the list of matches.
2412 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002413 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002414 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 else if (dir == FORWARD)
2416 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002417 match->cp_next = compl_curr_match->cp_next;
2418 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 }
2420 else /* BACKWARD */
2421 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002422 match->cp_next = compl_curr_match;
2423 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002425 if (match->cp_next)
2426 match->cp_next->cp_prev = match;
2427 if (match->cp_prev)
2428 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002430 compl_first_match = match;
2431 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002432
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002433 /*
2434 * Find the longest common string if still doing that.
2435 */
2436 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2437 ins_compl_longest_match(match);
2438
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 return OK;
2440}
2441
2442/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002443 * Return TRUE if "str[len]" matches with match->cp_str, considering
2444 * match->cp_icase.
2445 */
2446 static int
2447ins_compl_equal(match, str, len)
2448 compl_T *match;
2449 char_u *str;
2450 int len;
2451{
2452 if (match->cp_icase)
2453 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2454 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2455}
2456
2457/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002458 * Reduce the longest common string for match "match".
2459 */
2460 static void
2461ins_compl_longest_match(match)
2462 compl_T *match;
2463{
2464 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002465 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002466 int had_match;
2467
2468 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002469 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002470 /* First match, use it as a whole. */
2471 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002472 if (compl_leader != NULL)
2473 {
2474 had_match = (curwin->w_cursor.col > compl_col);
2475 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002476 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002477 ins_redraw(FALSE);
2478
2479 /* When the match isn't there (to avoid matching itself) remove it
2480 * again after redrawing. */
2481 if (!had_match)
2482 ins_compl_delete();
2483 compl_used_match = FALSE;
2484 }
2485 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002486 else
2487 {
2488 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002489 p = compl_leader;
2490 s = match->cp_str;
2491 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002492 {
2493#ifdef FEAT_MBYTE
2494 if (has_mbyte)
2495 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002496 c1 = mb_ptr2char(p);
2497 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002498 }
2499 else
2500#endif
2501 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002502 c1 = *p;
2503 c2 = *s;
2504 }
2505 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2506 : (c1 != c2))
2507 break;
2508#ifdef FEAT_MBYTE
2509 if (has_mbyte)
2510 {
2511 mb_ptr_adv(p);
2512 mb_ptr_adv(s);
2513 }
2514 else
2515#endif
2516 {
2517 ++p;
2518 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002519 }
2520 }
2521
2522 if (*p != NUL)
2523 {
2524 /* Leader was shortened, need to change the inserted text. */
2525 *p = NUL;
2526 had_match = (curwin->w_cursor.col > compl_col);
2527 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002528 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002529 ins_redraw(FALSE);
2530
2531 /* When the match isn't there (to avoid matching itself) remove it
2532 * again after redrawing. */
2533 if (!had_match)
2534 ins_compl_delete();
2535 }
2536
2537 compl_used_match = FALSE;
2538 }
2539}
2540
2541/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 * Add an array of matches to the list of matches.
2543 * Frees matches[].
2544 */
2545 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002546ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002547 int num_matches;
2548 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002549 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550{
2551 int i;
2552 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002553 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554
Bram Moolenaar572cb562005-08-05 21:35:02 +00002555 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002556 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002557 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002559 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002560 FreeWild(num_matches, matches);
2561}
2562
2563/* Make the completion list cyclic.
2564 * Return the number of matches (excluding the original).
2565 */
2566 static int
2567ins_compl_make_cyclic()
2568{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002569 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002570 int count = 0;
2571
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002572 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002573 {
2574 /*
2575 * Find the end of the list.
2576 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002577 match = compl_first_match;
2578 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002579 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002581 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 ++count;
2583 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002584 match->cp_next = compl_first_match;
2585 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586 }
2587 return count;
2588}
2589
Bram Moolenaara94bc432006-03-10 21:42:59 +00002590/*
2591 * Start completion for the complete() function.
2592 * "startcol" is where the matched text starts (1 is first column).
2593 * "list" is the list of matches.
2594 */
2595 void
2596set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002597 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002598 list_T *list;
2599{
2600 /* If already doing completions stop it. */
2601 if (ctrl_x_mode != 0)
2602 ins_compl_prep(' ');
2603 ins_compl_clear();
2604
2605 if (stop_arrow() == FAIL)
2606 return;
2607
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002608 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002609 startcol = curwin->w_cursor.col;
2610 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002611 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002612 /* compl_pattern doesn't need to be set */
2613 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2614 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002615 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002616 return;
2617
2618 /* Handle like dictionary completion. */
2619 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2620
2621 ins_compl_add_list(list);
2622 compl_matches = ins_compl_make_cyclic();
2623 compl_started = TRUE;
2624 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002625 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002626
2627 compl_curr_match = compl_first_match;
2628 ins_complete(Ctrl_N);
2629 out_flush();
2630}
2631
2632
Bram Moolenaar9372a112005-12-06 19:59:18 +00002633/* "compl_match_array" points the currently displayed list of entries in the
2634 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002635static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002636static int compl_match_arraysize;
2637
2638/*
2639 * Update the screen and when there is any scrolling remove the popup menu.
2640 */
2641 static void
2642ins_compl_upd_pum()
2643{
2644 int h;
2645
2646 if (compl_match_array != NULL)
2647 {
2648 h = curwin->w_cline_height;
2649 update_screen(0);
2650 if (h != curwin->w_cline_height)
2651 ins_compl_del_pum();
2652 }
2653}
2654
2655/*
2656 * Remove any popup menu.
2657 */
2658 static void
2659ins_compl_del_pum()
2660{
2661 if (compl_match_array != NULL)
2662 {
2663 pum_undisplay();
2664 vim_free(compl_match_array);
2665 compl_match_array = NULL;
2666 }
2667}
2668
2669/*
2670 * Return TRUE if the popup menu should be displayed.
2671 */
2672 static int
2673pum_wanted()
2674{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002675 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002676 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002677 return FALSE;
2678
2679 /* The display looks bad on a B&W display. */
2680 if (t_colors < 8
2681#ifdef FEAT_GUI
2682 && !gui.in_use
2683#endif
2684 )
2685 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002686 return TRUE;
2687}
2688
2689/*
2690 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002691 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002692 */
2693 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002694pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002695{
2696 compl_T *compl;
2697 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002698
2699 /* Don't display the popup menu if there are no matches or there is only
2700 * one (ignoring the original text). */
2701 compl = compl_first_match;
2702 i = 0;
2703 do
2704 {
2705 if (compl == NULL
2706 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2707 break;
2708 compl = compl->cp_next;
2709 } while (compl != compl_first_match);
2710
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002711 if (strstr((char *)p_cot, "menuone") != NULL)
2712 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002713 return (i >= 2);
2714}
2715
2716/*
2717 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002718 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002719 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002720 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002721ins_compl_show_pum()
2722{
2723 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002724 compl_T *shown_compl = NULL;
2725 int did_find_shown_match = FALSE;
2726 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002727 int i;
2728 int cur = -1;
2729 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002730 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002731
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002732 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002733 return;
2734
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002735#if defined(FEAT_EVAL)
2736 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2737 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2738#endif
2739
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002740 /* Update the screen before drawing the popup menu over it. */
2741 update_screen(0);
2742
2743 if (compl_match_array == NULL)
2744 {
2745 /* Need to build the popup menu list. */
2746 compl_match_arraysize = 0;
2747 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002748 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002749 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002750 do
2751 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002752 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2753 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002754 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002755 ++compl_match_arraysize;
2756 compl = compl->cp_next;
2757 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002758 if (compl_match_arraysize == 0)
2759 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002760 compl_match_array = (pumitem_T *)alloc_clear(
2761 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002762 * compl_match_arraysize));
2763 if (compl_match_array != NULL)
2764 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002765 /* If the current match is the original text don't find the first
2766 * match after it, don't highlight anything. */
2767 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2768 shown_match_ok = TRUE;
2769
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002770 i = 0;
2771 compl = compl_first_match;
2772 do
2773 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002774 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2775 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002776 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002777 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002778 if (!shown_match_ok)
2779 {
2780 if (compl == compl_shown_match || did_find_shown_match)
2781 {
2782 /* This item is the shown match or this is the
2783 * first displayed item after the shown match. */
2784 compl_shown_match = compl;
2785 did_find_shown_match = TRUE;
2786 shown_match_ok = TRUE;
2787 }
2788 else
2789 /* Remember this displayed match for when the
2790 * shown match is just below it. */
2791 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002792 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002793 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002794
2795 if (compl->cp_text[CPT_ABBR] != NULL)
2796 compl_match_array[i].pum_text =
2797 compl->cp_text[CPT_ABBR];
2798 else
2799 compl_match_array[i].pum_text = compl->cp_str;
2800 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2801 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2802 if (compl->cp_text[CPT_MENU] != NULL)
2803 compl_match_array[i++].pum_extra =
2804 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002805 else
2806 compl_match_array[i++].pum_extra = compl->cp_fname;
2807 }
2808
2809 if (compl == compl_shown_match)
2810 {
2811 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002812
2813 /* When the original text is the shown match don't set
2814 * compl_shown_match. */
2815 if (compl->cp_flags & ORIGINAL_TEXT)
2816 shown_match_ok = TRUE;
2817
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002818 if (!shown_match_ok && shown_compl != NULL)
2819 {
2820 /* The shown match isn't displayed, set it to the
2821 * previously displayed match. */
2822 compl_shown_match = shown_compl;
2823 shown_match_ok = TRUE;
2824 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002825 }
2826 compl = compl->cp_next;
2827 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002828
2829 if (!shown_match_ok) /* no displayed match at all */
2830 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002831 }
2832 }
2833 else
2834 {
2835 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002836 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002837 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2838 || compl_match_array[i].pum_text
2839 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002840 {
2841 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002842 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002843 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002844 }
2845
2846 if (compl_match_array != NULL)
2847 {
2848 /* Compute the screen column of the start of the completed text.
2849 * Use the cursor to get all wrapping and other settings right. */
2850 col = curwin->w_cursor.col;
2851 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002852 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002853 curwin->w_cursor.col = col;
2854 }
2855}
2856
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857#define DICT_FIRST (1) /* use just first element in "dict" */
2858#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002859
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002861 * Add any identifiers that match the given pattern in the list of dictionary
2862 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 */
2864 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002865ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2866 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002867 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002868 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002869 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002871 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002872 char_u *ptr;
2873 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002874 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875 char_u **files;
2876 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002877 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002878 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879
Bram Moolenaar0b238792006-03-02 22:49:12 +00002880 if (*dict == NUL)
2881 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002882#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002883 /* When 'dictionary' is empty and spell checking is enabled use
2884 * "spell". */
2885 if (!thesaurus && curwin->w_p_spell)
2886 dict = (char_u *)"spell";
2887 else
2888#endif
2889 return;
2890 }
2891
Bram Moolenaar071d4272004-06-13 20:20:40 +00002892 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002893 if (buf == NULL)
2894 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002895 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002896
Bram Moolenaar071d4272004-06-13 20:20:40 +00002897 /* If 'infercase' is set, don't use 'smartcase' here */
2898 save_p_scs = p_scs;
2899 if (curbuf->b_p_inf)
2900 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002901
2902 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2903 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002904 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002905 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2906 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002907 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002908 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002909
2910 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002911 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002912 len = STRLEN(pat_esc) + 10;
2913 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002914 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002915 {
2916 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002917 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002918 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002919 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002920 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2921 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002922 vim_free(ptr);
2923 }
2924 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002925 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002926 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002927 if (regmatch.regprog == NULL)
2928 goto theend;
2929 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002930
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2932 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002933 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002934 {
2935 /* copy one dictionary file name into buf */
2936 if (flags == DICT_EXACT)
2937 {
2938 count = 1;
2939 files = &dict;
2940 }
2941 else
2942 {
2943 /* Expand wildcards in the dictionary name, but do not allow
2944 * backticks (for security, the 'dict' option may have been set in
2945 * a modeline). */
2946 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002947# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002948 if (!thesaurus && STRCMP(buf, "spell") == 0)
2949 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002950 else
2951# endif
2952 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 || expand_wildcards(1, &buf, &count, &files,
2954 EW_FILE|EW_SILENT) != OK)
2955 count = 0;
2956 }
2957
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002958# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002959 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002960 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002961 /* Complete from active spelling. Skip "\<" in the pattern, we
2962 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002963 if (pat[0] == '\\' && pat[1] == '<')
2964 ptr = pat + 2;
2965 else
2966 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02002967 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002969 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002970# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002971 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002972 {
2973 ins_compl_files(count, files, thesaurus, flags,
2974 &regmatch, buf, &dir);
2975 if (flags != DICT_EXACT)
2976 FreeWild(count, files);
2977 }
2978 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 break;
2980 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002981
2982theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002983 p_scs = save_p_scs;
2984 vim_free(regmatch.regprog);
2985 vim_free(buf);
2986}
2987
Bram Moolenaar0b238792006-03-02 22:49:12 +00002988 static void
2989ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2990 int count;
2991 char_u **files;
2992 int thesaurus;
2993 int flags;
2994 regmatch_T *regmatch;
2995 char_u *buf;
2996 int *dir;
2997{
2998 char_u *ptr;
2999 int i;
3000 FILE *fp;
3001 int add_r;
3002
3003 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3004 {
3005 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3006 if (flags != DICT_EXACT)
3007 {
3008 vim_snprintf((char *)IObuff, IOSIZE,
3009 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003010 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003011 }
3012
3013 if (fp != NULL)
3014 {
3015 /*
3016 * Read dictionary file line by line.
3017 * Check each line for a match.
3018 */
3019 while (!got_int && !compl_interrupted
3020 && !vim_fgets(buf, LSIZE, fp))
3021 {
3022 ptr = buf;
3023 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3024 {
3025 ptr = regmatch->startp[0];
3026 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3027 ptr = find_line_end(ptr);
3028 else
3029 ptr = find_word_end(ptr);
3030 add_r = ins_compl_add_infercase(regmatch->startp[0],
3031 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003032 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003033 if (thesaurus)
3034 {
3035 char_u *wstart;
3036
3037 /*
3038 * Add the other matches on the line
3039 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003040 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003041 while (!got_int)
3042 {
3043 /* Find start of the next word. Skip white
3044 * space and punctuation. */
3045 ptr = find_word_start(ptr);
3046 if (*ptr == NUL || *ptr == NL)
3047 break;
3048 wstart = ptr;
3049
Bram Moolenaara2993e12007-08-12 14:38:46 +00003050 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003051#ifdef FEAT_MBYTE
3052 if (has_mbyte)
3053 /* Japanese words may have characters in
3054 * different classes, only separate words
3055 * with single-byte non-word characters. */
3056 while (*ptr != NUL)
3057 {
3058 int l = (*mb_ptr2len)(ptr);
3059
3060 if (l < 2 && !vim_iswordc(*ptr))
3061 break;
3062 ptr += l;
3063 }
3064 else
3065#endif
3066 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003067
3068 /* Add the word. Skip the regexp match. */
3069 if (wstart != regmatch->startp[0])
3070 add_r = ins_compl_add_infercase(wstart,
3071 (int)(ptr - wstart),
3072 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003073 }
3074 }
3075 if (add_r == OK)
3076 /* if dir was BACKWARD then honor it just once */
3077 *dir = FORWARD;
3078 else if (add_r == FAIL)
3079 break;
3080 /* avoid expensive call to vim_regexec() when at end
3081 * of line */
3082 if (*ptr == '\n' || got_int)
3083 break;
3084 }
3085 line_breakcheck();
3086 ins_compl_check_keys(50);
3087 }
3088 fclose(fp);
3089 }
3090 }
3091}
3092
Bram Moolenaar071d4272004-06-13 20:20:40 +00003093/*
3094 * Find the start of the next word.
3095 * Returns a pointer to the first char of the word. Also stops at a NUL.
3096 */
3097 char_u *
3098find_word_start(ptr)
3099 char_u *ptr;
3100{
3101#ifdef FEAT_MBYTE
3102 if (has_mbyte)
3103 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003104 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003105 else
3106#endif
3107 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3108 ++ptr;
3109 return ptr;
3110}
3111
3112/*
3113 * Find the end of the word. Assumes it starts inside a word.
3114 * Returns a pointer to just after the word.
3115 */
3116 char_u *
3117find_word_end(ptr)
3118 char_u *ptr;
3119{
3120#ifdef FEAT_MBYTE
3121 int start_class;
3122
3123 if (has_mbyte)
3124 {
3125 start_class = mb_get_class(ptr);
3126 if (start_class > 1)
3127 while (*ptr != NUL)
3128 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003129 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003130 if (mb_get_class(ptr) != start_class)
3131 break;
3132 }
3133 }
3134 else
3135#endif
3136 while (vim_iswordc(*ptr))
3137 ++ptr;
3138 return ptr;
3139}
3140
3141/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003142 * Find the end of the line, omitting CR and NL at the end.
3143 * Returns a pointer to just after the line.
3144 */
3145 static char_u *
3146find_line_end(ptr)
3147 char_u *ptr;
3148{
3149 char_u *s;
3150
3151 s = ptr + STRLEN(ptr);
3152 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3153 --s;
3154 return s;
3155}
3156
3157/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 * Free the list of completions
3159 */
3160 static void
3161ins_compl_free()
3162{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003163 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003164 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003165
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003166 vim_free(compl_pattern);
3167 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003168 vim_free(compl_leader);
3169 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003170
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003171 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003172 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003173
3174 ins_compl_del_pum();
3175 pum_clear();
3176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003177 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 do
3179 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003180 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003181 compl_curr_match = compl_curr_match->cp_next;
3182 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003183 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003184 if (match->cp_flags & FREE_FNAME)
3185 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003186 for (i = 0; i < CPT_COUNT; ++i)
3187 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003189 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3190 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003191 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192}
3193
3194 static void
3195ins_compl_clear()
3196{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003197 compl_cont_status = 0;
3198 compl_started = FALSE;
3199 compl_matches = 0;
3200 vim_free(compl_pattern);
3201 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003202 vim_free(compl_leader);
3203 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003205 vim_free(compl_orig_text);
3206 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003207 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208}
3209
3210/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003211 * Return TRUE when Insert completion is active.
3212 */
3213 int
3214ins_compl_active()
3215{
3216 return compl_started;
3217}
3218
3219/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003220 * Delete one character before the cursor and show the subset of the matches
3221 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003222 * Returns the character to be used, NUL if the work is done and another char
3223 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003224 */
3225 static int
3226ins_compl_bs()
3227{
3228 char_u *line;
3229 char_u *p;
3230
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003231 line = ml_get_curline();
3232 p = line + curwin->w_cursor.col;
3233 mb_ptr_back(line, p);
3234
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003235 /* Stop completion when the whole word was deleted. For Omni completion
3236 * allow the word to be deleted, we won't match everything. */
3237 if ((int)(p - line) - (int)compl_col < 0
3238 || ((int)(p - line) - (int)compl_col == 0
3239 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003240 return K_BS;
3241
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003242 /* Deleted more than what was used to find matches or didn't finish
3243 * finding all matches: need to look for matches all over again. */
3244 if (curwin->w_cursor.col <= compl_col + compl_length
3245 || compl_was_interrupted)
3246 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003247
Bram Moolenaara6557602006-02-04 22:43:20 +00003248 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003249 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003250 if (compl_leader != NULL)
3251 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003252 ins_compl_new_leader();
3253 return NUL;
3254 }
3255 return K_BS;
3256}
Bram Moolenaara6557602006-02-04 22:43:20 +00003257
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003258/*
3259 * Called after changing "compl_leader".
3260 * Show the popup menu with a different set of matches.
3261 * May also search for matches again if the previous search was interrupted.
3262 */
3263 static void
3264ins_compl_new_leader()
3265{
3266 ins_compl_del_pum();
3267 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003268 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003269 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003270
3271 if (compl_started)
3272 ins_compl_set_original_text(compl_leader);
3273 else
3274 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003275#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003276 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003277#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003278 /*
3279 * Matches were cleared, need to search for them now. First display
3280 * the changed text before the cursor. Set "compl_restarting" to
3281 * avoid that the first match is inserted.
3282 */
3283 update_screen(0);
3284#ifdef FEAT_GUI
3285 if (gui.in_use)
3286 {
3287 /* Show the cursor after the match, not after the redrawn text. */
3288 setcursor();
3289 out_flush();
3290 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003291 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003292#endif
3293 compl_restarting = TRUE;
3294 if (ins_complete(Ctrl_N) == FAIL)
3295 compl_cont_status = 0;
3296 compl_restarting = FALSE;
3297 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003298
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003299#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003300 if (!compl_used_match)
3301 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003302 /* Go to the original text, since none of the matches is inserted. */
3303 if (compl_first_match->cp_prev != NULL
3304 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3305 compl_shown_match = compl_first_match->cp_prev;
3306 else
3307 compl_shown_match = compl_first_match;
3308 compl_curr_match = compl_shown_match;
3309 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003310 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003311#endif
3312 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003313
3314 /* Show the popup menu with a different set of matches. */
3315 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003316
3317 /* Don't let Enter select the original text when there is no popup menu. */
3318 if (compl_match_array == NULL)
3319 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003320}
3321
3322/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003323 * Return the length of the completion, from the completion start column to
3324 * the cursor column. Making sure it never goes below zero.
3325 */
3326 static int
3327ins_compl_len()
3328{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003329 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003330
3331 if (off < 0)
3332 return 0;
3333 return off;
3334}
3335
3336/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003337 * Append one character to the match leader. May reduce the number of
3338 * matches.
3339 */
3340 static void
3341ins_compl_addleader(c)
3342 int c;
3343{
3344#ifdef FEAT_MBYTE
3345 int cc;
3346
3347 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3348 {
3349 char_u buf[MB_MAXBYTES + 1];
3350
3351 (*mb_char2bytes)(c, buf);
3352 buf[cc] = NUL;
3353 ins_char_bytes(buf, cc);
3354 }
3355 else
3356#endif
3357 ins_char(c);
3358
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003359 /* If we didn't complete finding matches we must search again. */
3360 if (compl_was_interrupted)
3361 ins_compl_restart();
3362
Bram Moolenaara6557602006-02-04 22:43:20 +00003363 vim_free(compl_leader);
3364 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003365 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003366 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003367 ins_compl_new_leader();
3368}
3369
3370/*
3371 * Setup for finding completions again without leaving CTRL-X mode. Used when
3372 * BS or a key was typed while still searching for matches.
3373 */
3374 static void
3375ins_compl_restart()
3376{
3377 ins_compl_free();
3378 compl_started = FALSE;
3379 compl_matches = 0;
3380 compl_cont_status = 0;
3381 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003382}
3383
3384/*
3385 * Set the first match, the original text.
3386 */
3387 static void
3388ins_compl_set_original_text(str)
3389 char_u *str;
3390{
3391 char_u *p;
3392
3393 /* Replace the original text entry. */
3394 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3395 {
3396 p = vim_strsave(str);
3397 if (p != NULL)
3398 {
3399 vim_free(compl_first_match->cp_str);
3400 compl_first_match->cp_str = p;
3401 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003402 }
3403}
3404
3405/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003406 * Append one character to the match leader. May reduce the number of
3407 * matches.
3408 */
3409 static void
3410ins_compl_addfrommatch()
3411{
3412 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003413 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003414 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003415 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003416
3417 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003418 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003419 {
3420 /* When still at the original match use the first entry that matches
3421 * the leader. */
3422 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3423 {
3424 p = NULL;
3425 for (cp = compl_shown_match->cp_next; cp != NULL
3426 && cp != compl_first_match; cp = cp->cp_next)
3427 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003428 if (compl_leader == NULL
3429 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003430 (int)STRLEN(compl_leader)))
3431 {
3432 p = cp->cp_str;
3433 break;
3434 }
3435 }
3436 if (p == NULL || (int)STRLEN(p) <= len)
3437 return;
3438 }
3439 else
3440 return;
3441 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003442 p += len;
3443#ifdef FEAT_MBYTE
3444 c = mb_ptr2char(p);
3445#else
3446 c = *p;
3447#endif
3448 ins_compl_addleader(c);
3449}
3450
3451/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003453 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003454 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003456 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457ins_compl_prep(c)
3458 int c;
3459{
3460 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003462 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463
3464 /* Forget any previous 'special' messages if this is actually
3465 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3466 */
3467 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3468 edit_submode_extra = NULL;
3469
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003470 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3471 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003472 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003473
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003474 /* Set "compl_get_longest" when finding the first matches. */
3475 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3476 || (ctrl_x_mode == 0 && !compl_started))
3477 {
3478 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3479 compl_used_match = TRUE;
3480 }
3481
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3483 {
3484 /*
3485 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3486 * it will be yet. Now we decide.
3487 */
3488 switch (c)
3489 {
3490 case Ctrl_E:
3491 case Ctrl_Y:
3492 ctrl_x_mode = CTRL_X_SCROLL;
3493 if (!(State & REPLACE_FLAG))
3494 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3495 else
3496 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3497 edit_submode_pre = NULL;
3498 showmode();
3499 break;
3500 case Ctrl_L:
3501 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3502 break;
3503 case Ctrl_F:
3504 ctrl_x_mode = CTRL_X_FILES;
3505 break;
3506 case Ctrl_K:
3507 ctrl_x_mode = CTRL_X_DICTIONARY;
3508 break;
3509 case Ctrl_R:
3510 /* Simply allow ^R to happen without affecting ^X mode */
3511 break;
3512 case Ctrl_T:
3513 ctrl_x_mode = CTRL_X_THESAURUS;
3514 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003515#ifdef FEAT_COMPL_FUNC
3516 case Ctrl_U:
3517 ctrl_x_mode = CTRL_X_FUNCTION;
3518 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003519 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003520 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003521 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003522#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003523 case 's':
3524 case Ctrl_S:
3525 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003526#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003527 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003528 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003529 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003530#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003531 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 case Ctrl_RSB:
3533 ctrl_x_mode = CTRL_X_TAGS;
3534 break;
3535#ifdef FEAT_FIND_ID
3536 case Ctrl_I:
3537 case K_S_TAB:
3538 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3539 break;
3540 case Ctrl_D:
3541 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3542 break;
3543#endif
3544 case Ctrl_V:
3545 case Ctrl_Q:
3546 ctrl_x_mode = CTRL_X_CMDLINE;
3547 break;
3548 case Ctrl_P:
3549 case Ctrl_N:
3550 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3551 * just started ^X mode, or there were enough ^X's to cancel
3552 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3553 * do normal expansion when interrupting a different mode (say
3554 * ^X^F^X^P or ^P^X^X^P, see below)
3555 * nothing changes if interrupting mode 0, (eg, the flag
3556 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003557 if (!(compl_cont_status & CONT_INTRPT))
3558 compl_cont_status |= CONT_LOCAL;
3559 else if (compl_cont_mode != 0)
3560 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 /* FALLTHROUGH */
3562 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003563 /* If we have typed at least 2 ^X's... for modes != 0, we set
3564 * compl_cont_status = 0 (eg, as if we had just started ^X
3565 * mode).
3566 * For mode 0, we set "compl_cont_mode" to an impossible
3567 * value, in both cases ^X^X can be used to restart the same
3568 * mode (avoiding ADDING mode).
3569 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3570 * 'complete' and local ^P expansions respectively.
3571 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3572 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 if (c == Ctrl_X)
3574 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003575 if (compl_cont_mode != 0)
3576 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003578 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579 }
3580 ctrl_x_mode = 0;
3581 edit_submode = NULL;
3582 showmode();
3583 break;
3584 }
3585 }
3586 else if (ctrl_x_mode != 0)
3587 {
3588 /* We're already in CTRL-X mode, do we stay in it? */
3589 if (!vim_is_ctrl_x_key(c))
3590 {
3591 if (ctrl_x_mode == CTRL_X_SCROLL)
3592 ctrl_x_mode = 0;
3593 else
3594 ctrl_x_mode = CTRL_X_FINISHED;
3595 edit_submode = NULL;
3596 }
3597 showmode();
3598 }
3599
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003600 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 {
3602 /* Show error message from attempted keyword completion (probably
3603 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003604 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003605 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003606 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3607 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003608 || ctrl_x_mode == CTRL_X_FINISHED)
3609 {
3610 /* Get here when we have finished typing a sequence of ^N and
3611 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003612 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003613 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003615 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003616 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003617
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003619 * If any of the original typed text has been changed, eg when
3620 * ignorecase is set, we must add back-spaces to the redo
3621 * buffer. We add as few as necessary to delete just the part
3622 * of the original text that has changed.
3623 * When using the longest match, edited the match or used
3624 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003626 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3627 ptr = compl_curr_match->cp_str;
3628 else if (compl_leader != NULL)
3629 ptr = compl_leader;
3630 else
3631 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003632 if (compl_orig_text != NULL)
3633 {
3634 p = compl_orig_text;
3635 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3636 ++temp)
3637 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003638#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003639 if (temp > 0)
3640 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003641#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003642 for (p += temp; *p != NUL; mb_ptr_adv(p))
3643 AppendCharToRedobuff(K_BS);
3644 }
3645 if (ptr != NULL)
3646 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 }
3648
3649#ifdef FEAT_CINDENT
3650 want_cindent = (can_cindent && cindent_on());
3651#endif
3652 /*
3653 * When completing whole lines: fix indent for 'cindent'.
3654 * Otherwise, break line if it's too long.
3655 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003656 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 {
3658#ifdef FEAT_CINDENT
3659 /* re-indent the current line */
3660 if (want_cindent)
3661 {
3662 do_c_expr_indent();
3663 want_cindent = FALSE; /* don't do it again */
3664 }
3665#endif
3666 }
3667 else
3668 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003669 int prev_col = curwin->w_cursor.col;
3670
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003672 if (prev_col > 0)
3673 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674 if (stop_arrow() == OK)
3675 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003676 if (prev_col > 0
3677 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3678 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003679 }
3680
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003681 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003682 * the selection without inserting anything. When
3683 * compl_enter_selects is set the Enter key does the same. */
3684 if ((c == Ctrl_Y || (compl_enter_selects
3685 && (c == CAR || c == K_KENTER || c == NL)))
3686 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003687 retval = TRUE;
3688
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003689 /* CTRL-E means completion is Ended, go back to the typed text. */
3690 if (c == Ctrl_E)
3691 {
3692 ins_compl_delete();
3693 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003694 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003695 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003696 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003697 retval = TRUE;
3698 }
3699
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003700 auto_format(FALSE, TRUE);
3701
Bram Moolenaar071d4272004-06-13 20:20:40 +00003702 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003703 compl_started = FALSE;
3704 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003705 msg_clr_cmdline(); /* necessary for "noshowmode" */
3706 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003707 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003708 if (edit_submode != NULL)
3709 {
3710 edit_submode = NULL;
3711 showmode();
3712 }
3713
3714#ifdef FEAT_CINDENT
3715 /*
3716 * Indent now if a key was typed that is in 'cinkeys'.
3717 */
3718 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3719 do_c_expr_indent();
3720#endif
3721 }
3722 }
3723
3724 /* reset continue_* if we left expansion-mode, if we stay they'll be
3725 * (re)set properly in ins_complete() */
3726 if (!vim_is_ctrl_x_key(c))
3727 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003728 compl_cont_status = 0;
3729 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003730 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003731
3732 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003733}
3734
3735/*
3736 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3737 * (depending on flag) starting from buf and looking for a non-scanned
3738 * buffer (other than curbuf). curbuf is special, if it is called with
3739 * buf=curbuf then it has to be the first call for a given flag/expansion.
3740 *
3741 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3742 */
3743 static buf_T *
3744ins_compl_next_buf(buf, flag)
3745 buf_T *buf;
3746 int flag;
3747{
3748#ifdef FEAT_WINDOWS
3749 static win_T *wp;
3750#endif
3751
3752 if (flag == 'w') /* just windows */
3753 {
3754#ifdef FEAT_WINDOWS
3755 if (buf == curbuf) /* first call for this flag/expansion */
3756 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003757 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003758 && wp->w_buffer->b_scanned)
3759 ;
3760 buf = wp->w_buffer;
3761#else
3762 buf = curbuf;
3763#endif
3764 }
3765 else
3766 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3767 * (unlisted buffers)
3768 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003769 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003770 && ((flag == 'U'
3771 ? buf->b_p_bl
3772 : (!buf->b_p_bl
3773 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003774 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003775 ;
3776 return buf;
3777}
3778
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003779#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003780static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003781
3782/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003783 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003784 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003785 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003786 static void
3787expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003788 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003789 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003790{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003791 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003792 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003793 char_u *funcname;
3794 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003795
Bram Moolenaare344bea2005-09-01 20:46:49 +00003796 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3797 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003798 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003799
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003800 /* Call 'completefunc' to obtain the list of matches. */
3801 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003802 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003803
Bram Moolenaare344bea2005-09-01 20:46:49 +00003804 pos = curwin->w_cursor;
3805 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3806 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003807 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003808 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003809
Bram Moolenaara94bc432006-03-10 21:42:59 +00003810 ins_compl_add_list(matchlist);
3811 list_unref(matchlist);
3812}
3813#endif /* FEAT_COMPL_FUNC */
3814
Bram Moolenaar39f05632006-03-19 22:15:26 +00003815#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003816/*
3817 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003818 */
3819 static void
3820ins_compl_add_list(list)
3821 list_T *list;
3822{
3823 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003824 int dir = compl_direction;
3825
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003826 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003827 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003828 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003829 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3830 /* if dir was BACKWARD then honor it just once */
3831 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003832 else if (did_emsg)
3833 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003834 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003835}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003836
3837/*
3838 * Add a match to the list of matches from a typeval_T.
3839 * If the given string is already in the list of completions, then return
3840 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3841 * maybe because alloc() returns NULL, then FAIL is returned.
3842 */
3843 int
3844ins_compl_add_tv(tv, dir)
3845 typval_T *tv;
3846 int dir;
3847{
3848 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003849 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003850 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003851 char_u *(cptext[CPT_COUNT]);
3852
3853 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3854 {
3855 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3856 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3857 (char_u *)"abbr", FALSE);
3858 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3859 (char_u *)"menu", FALSE);
3860 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3861 (char_u *)"kind", FALSE);
3862 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3863 (char_u *)"info", FALSE);
3864 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3865 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003866 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003867 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003868 }
3869 else
3870 {
3871 word = get_tv_string_chk(tv);
3872 vim_memset(cptext, 0, sizeof(cptext));
3873 }
3874 if (word == NULL || *word == NUL)
3875 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003876 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003877}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003878#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003879
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003880/*
3881 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003882 * The search starts at position "ini" in curbuf and in the direction
3883 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003884 * When "compl_started" is FALSE start at that position, otherwise continue
3885 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003886 * This may return before finding all the matches.
3887 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888 */
3889 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003890ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003891 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003892{
3893 static pos_T first_match_pos;
3894 static pos_T last_match_pos;
3895 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003896 static int found_all = FALSE; /* Found all matches of a
3897 certain type. */
3898 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899
Bram Moolenaar572cb562005-08-05 21:35:02 +00003900 pos_T *pos;
3901 char_u **matches;
3902 int save_p_scs;
3903 int save_p_ws;
3904 int save_p_ic;
3905 int i;
3906 int num_matches;
3907 int len;
3908 int found_new_match;
3909 int type = ctrl_x_mode;
3910 char_u *ptr;
3911 char_u *dict = NULL;
3912 int dict_f = 0;
3913 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003915 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003916 {
3917 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3918 ins_buf->b_scanned = 0;
3919 found_all = FALSE;
3920 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003922 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 last_match_pos = first_match_pos = *ini;
3924 }
3925
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003926 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003927 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3929 for (;;)
3930 {
3931 found_new_match = FAIL;
3932
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003933 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 * or if found_all says this entry is done. For ^X^L only use the
3935 * entries from 'complete' that look in loaded buffers. */
3936 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003937 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003938 {
3939 found_all = FALSE;
3940 while (*e_cpt == ',' || *e_cpt == ' ')
3941 e_cpt++;
3942 if (*e_cpt == '.' && !curbuf->b_scanned)
3943 {
3944 ins_buf = curbuf;
3945 first_match_pos = *ini;
3946 /* So that ^N can match word immediately after cursor */
3947 if (ctrl_x_mode == 0)
3948 dec(&first_match_pos);
3949 last_match_pos = first_match_pos;
3950 type = 0;
3951 }
3952 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3953 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3954 {
3955 /* Scan a buffer, but not the current one. */
3956 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3957 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003958 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 first_match_pos.col = last_match_pos.col = 0;
3960 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3961 last_match_pos.lnum = 0;
3962 type = 0;
3963 }
3964 else /* unloaded buffer, scan like dictionary */
3965 {
3966 found_all = TRUE;
3967 if (ins_buf->b_fname == NULL)
3968 continue;
3969 type = CTRL_X_DICTIONARY;
3970 dict = ins_buf->b_fname;
3971 dict_f = DICT_EXACT;
3972 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003973 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 ins_buf->b_fname == NULL
3975 ? buf_spname(ins_buf)
3976 : ins_buf->b_sfname == NULL
3977 ? (char *)ins_buf->b_fname
3978 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003979 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 }
3981 else if (*e_cpt == NUL)
3982 break;
3983 else
3984 {
3985 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3986 type = -1;
3987 else if (*e_cpt == 'k' || *e_cpt == 's')
3988 {
3989 if (*e_cpt == 'k')
3990 type = CTRL_X_DICTIONARY;
3991 else
3992 type = CTRL_X_THESAURUS;
3993 if (*++e_cpt != ',' && *e_cpt != NUL)
3994 {
3995 dict = e_cpt;
3996 dict_f = DICT_FIRST;
3997 }
3998 }
3999#ifdef FEAT_FIND_ID
4000 else if (*e_cpt == 'i')
4001 type = CTRL_X_PATH_PATTERNS;
4002 else if (*e_cpt == 'd')
4003 type = CTRL_X_PATH_DEFINES;
4004#endif
4005 else if (*e_cpt == ']' || *e_cpt == 't')
4006 {
4007 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004008 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004009 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 }
4011 else
4012 type = -1;
4013
4014 /* in any case e_cpt is advanced to the next entry */
4015 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4016
4017 found_all = TRUE;
4018 if (type == -1)
4019 continue;
4020 }
4021 }
4022
4023 switch (type)
4024 {
4025 case -1:
4026 break;
4027#ifdef FEAT_FIND_ID
4028 case CTRL_X_PATH_PATTERNS:
4029 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004030 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004031 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004033 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4035 (linenr_T)1, (linenr_T)MAXLNUM);
4036 break;
4037#endif
4038
4039 case CTRL_X_DICTIONARY:
4040 case CTRL_X_THESAURUS:
4041 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004042 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004043 : (type == CTRL_X_THESAURUS
4044 ? (*curbuf->b_p_tsr == NUL
4045 ? p_tsr
4046 : curbuf->b_p_tsr)
4047 : (*curbuf->b_p_dict == NUL
4048 ? p_dict
4049 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004050 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004051 dict != NULL ? dict_f
4052 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 dict = NULL;
4054 break;
4055
4056 case CTRL_X_TAGS:
4057 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4058 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004059 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004061 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004062 * of matches is found when compl_pattern is empty */
4063 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4065 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4066 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4067 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004068 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004069 }
4070 p_ic = save_p_ic;
4071 break;
4072
4073 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004074 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4076 {
4077
4078 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004079 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004080 ins_compl_add_matches(num_matches, matches,
4081#ifdef CASE_INSENSITIVE_FILENAME
4082 TRUE
4083#else
4084 FALSE
4085#endif
4086 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087 }
4088 break;
4089
4090 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004091 if (expand_cmdline(&compl_xp, compl_pattern,
4092 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004094 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095 break;
4096
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004097#ifdef FEAT_COMPL_FUNC
4098 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004099 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004100 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004101 break;
4102#endif
4103
Bram Moolenaar488c6512005-08-11 20:09:58 +00004104 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004105#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004106 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004107 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004108 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004109 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004110#endif
4111 break;
4112
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 default: /* normal ^P/^N and ^X^L */
4114 /*
4115 * If 'infercase' is set, don't use 'smartcase' here
4116 */
4117 save_p_scs = p_scs;
4118 if (ins_buf->b_p_inf)
4119 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004120
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 /* buffers other than curbuf are scanned from the beginning or the
4122 * end but never from the middle, thus setting nowrapscan in this
4123 * buffers is a good idea, on the other hand, we always set
4124 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4125 save_p_ws = p_ws;
4126 if (ins_buf != curbuf)
4127 p_ws = FALSE;
4128 else if (*e_cpt == '.')
4129 p_ws = TRUE;
4130 for (;;)
4131 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004132 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004134 ++msg_silent; /* Don't want messages for wrapscan. */
4135
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004136 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4137 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004141 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004143 found_new_match = searchit(NULL, ins_buf, pos,
4144 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004145 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004146 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004147 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004148 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004150 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004151 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 first_match_pos = *pos;
4153 last_match_pos = *pos;
4154 }
4155 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004156 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 found_new_match = FAIL;
4158 if (found_new_match == FAIL)
4159 {
4160 if (ins_buf == curbuf)
4161 found_all = TRUE;
4162 break;
4163 }
4164
4165 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004166 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 && ini->lnum == pos->lnum
4168 && ini->col == pos->col)
4169 continue;
4170 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4171 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4172 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004173 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 {
4175 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4176 continue;
4177 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4178 if (!p_paste)
4179 ptr = skipwhite(ptr);
4180 }
4181 len = (int)STRLEN(ptr);
4182 }
4183 else
4184 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 char_u *tmp_ptr = ptr;
4186
4187 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004189 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 /* Skip if already inside a word. */
4191 if (vim_iswordp(tmp_ptr))
4192 continue;
4193 /* Find start of next word. */
4194 tmp_ptr = find_word_start(tmp_ptr);
4195 }
4196 /* Find end of this word. */
4197 tmp_ptr = find_word_end(tmp_ptr);
4198 len = (int)(tmp_ptr - ptr);
4199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004200 if ((compl_cont_status & CONT_ADDING)
4201 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 {
4203 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4204 {
4205 /* Try next line, if any. the new word will be
4206 * "join" as if the normal command "J" was used.
4207 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004208 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * works -- Acevedo */
4210 STRNCPY(IObuff, ptr, len);
4211 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4212 tmp_ptr = ptr = skipwhite(ptr);
4213 /* Find start of next word. */
4214 tmp_ptr = find_word_start(tmp_ptr);
4215 /* Find end of next word. */
4216 tmp_ptr = find_word_end(tmp_ptr);
4217 if (tmp_ptr > ptr)
4218 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004219 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004221 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 IObuff[len++] = ' ';
4223 /* IObuf =~ "\k.* ", thus len >= 2 */
4224 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004225 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 || (vim_strchr(p_cpo, CPO_JOINSP)
4227 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004228 && (IObuff[len - 2] == '?'
4229 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004230 IObuff[len++] = ' ';
4231 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004232 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 if (tmp_ptr - ptr >= IOSIZE - len)
4234 tmp_ptr = ptr + IOSIZE - len - 1;
4235 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4236 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004237 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239 IObuff[len] = NUL;
4240 ptr = IObuff;
4241 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004243 continue;
4244 }
4245 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004246 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004247 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004248 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 {
4250 found_new_match = OK;
4251 break;
4252 }
4253 }
4254 p_scs = save_p_scs;
4255 p_ws = save_p_ws;
4256 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004258 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004259 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004260 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 found_new_match = OK;
4262
4263 /* break the loop for specialized modes (use 'complete' just for the
4264 * generic ctrl_x_mode == 0) or when we've found a new match */
4265 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004266 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004267 {
4268 if (got_int)
4269 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004270 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004271 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004272 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004273
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004274 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4275 || compl_interrupted)
4276 break;
4277 compl_started = TRUE;
4278 }
4279 else
4280 {
4281 /* Mark a buffer scanned when it has been scanned completely */
4282 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4283 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004284
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004285 compl_started = FALSE;
4286 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004288 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289
4290 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4291 && *e_cpt == NUL) /* Got to end of 'complete' */
4292 found_new_match = FAIL;
4293
4294 i = -1; /* total of matches, unknown */
4295 if (found_new_match == FAIL
4296 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4297 i = ins_compl_make_cyclic();
4298
4299 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 * just been made cyclic then we have to move compl_curr_match to the next
4301 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004302 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4303 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004304 if (compl_curr_match == NULL)
4305 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 return i;
4307}
4308
4309/* Delete the old text being completed. */
4310 static void
4311ins_compl_delete()
4312{
4313 int i;
4314
4315 /*
4316 * In insert mode: Delete the typed part.
4317 * In replace mode: Put the old characters back, if any.
4318 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004319 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320 backspace_until_column(i);
4321 changed_cline_bef_curs();
4322}
4323
4324/* Insert the new text being completed. */
4325 static void
4326ins_compl_insert()
4327{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004328 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004329 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4330 compl_used_match = FALSE;
4331 else
4332 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333}
4334
4335/*
4336 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004337 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4338 * get more completions. If it is FALSE, then we just do nothing when there
4339 * are no more completions in a given direction. The latter case is used when
4340 * we are still in the middle of finding completions, to allow browsing
4341 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 * Return the total number of matches, or -1 if still unknown -- webb.
4343 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004344 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4345 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 *
4347 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004348 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4349 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004350 */
4351 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004352ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004354 int count; /* repeat completion this many times; should
4355 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004356 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
4358 int num_matches = -1;
4359 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004360 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004361 compl_T *found_compl = NULL;
4362 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004363 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004365 if (compl_leader != NULL
4366 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004368 /* Set "compl_shown_match" to the actually shown match, it may differ
4369 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004370 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004371 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004372 && compl_shown_match->cp_next != NULL
4373 && compl_shown_match->cp_next != compl_first_match)
4374 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004375
4376 /* If we didn't find it searching forward, and compl_shows_dir is
4377 * backward, find the last match. */
4378 if (compl_shows_dir == BACKWARD
4379 && !ins_compl_equal(compl_shown_match,
4380 compl_leader, (int)STRLEN(compl_leader))
4381 && (compl_shown_match->cp_next == NULL
4382 || compl_shown_match->cp_next == compl_first_match))
4383 {
4384 while (!ins_compl_equal(compl_shown_match,
4385 compl_leader, (int)STRLEN(compl_leader))
4386 && compl_shown_match->cp_prev != NULL
4387 && compl_shown_match->cp_prev != compl_first_match)
4388 compl_shown_match = compl_shown_match->cp_prev;
4389 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004390 }
4391
4392 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004393 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 /* Delete old text to be replaced */
4395 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004396
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004397 /* When finding the longest common text we stick at the original text,
4398 * don't let CTRL-N or CTRL-P move to the first match. */
4399 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4400
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004401 /* When restarting the search don't insert the first match either. */
4402 if (compl_restarting)
4403 {
4404 advance = FALSE;
4405 compl_restarting = FALSE;
4406 }
4407
Bram Moolenaare3226be2005-12-18 22:10:00 +00004408 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4409 * around. */
4410 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004412 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004413 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004414 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004415 found_end = (compl_first_match != NULL
4416 && (compl_shown_match->cp_next == compl_first_match
4417 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004418 }
4419 else if (compl_shows_dir == BACKWARD
4420 && compl_shown_match->cp_prev != NULL)
4421 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004422 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004423 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004424 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004425 }
4426 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004427 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004428 if (!allow_get_expansion)
4429 {
4430 if (advance)
4431 {
4432 if (compl_shows_dir == BACKWARD)
4433 compl_pending -= todo + 1;
4434 else
4435 compl_pending += todo + 1;
4436 }
4437 return -1;
4438 }
4439
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004440 if (advance)
4441 {
4442 if (compl_shows_dir == BACKWARD)
4443 --compl_pending;
4444 else
4445 ++compl_pending;
4446 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004447
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004448 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004449 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004450
4451 /* handle any pending completions */
4452 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004453 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004454 {
4455 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4456 {
4457 compl_shown_match = compl_shown_match->cp_next;
4458 --compl_pending;
4459 }
4460 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4461 {
4462 compl_shown_match = compl_shown_match->cp_prev;
4463 ++compl_pending;
4464 }
4465 else
4466 break;
4467 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004468 found_end = FALSE;
4469 }
4470 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4471 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004472 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004473 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004474 ++todo;
4475 else
4476 /* Remember a matching item. */
4477 found_compl = compl_shown_match;
4478
4479 /* Stop at the end of the list when we found a usable match. */
4480 if (found_end)
4481 {
4482 if (found_compl != NULL)
4483 {
4484 compl_shown_match = found_compl;
4485 break;
4486 }
4487 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004488 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004489 }
4490
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004491 /* Insert the text of the new completion, or the compl_leader. */
4492 if (insert_match)
4493 {
4494 if (!compl_get_longest || compl_used_match)
4495 ins_compl_insert();
4496 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004497 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004498 }
4499 else
4500 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004501
4502 if (!allow_get_expansion)
4503 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004504 /* may undisplay the popup menu first */
4505 ins_compl_upd_pum();
4506
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004507 /* redraw to show the user what was inserted */
4508 update_screen(0);
4509
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004510 /* display the updated popup menu */
4511 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004512#ifdef FEAT_GUI
4513 if (gui.in_use)
4514 {
4515 /* Show the cursor after the match, not after the redrawn text. */
4516 setcursor();
4517 out_flush();
4518 gui_update_cursor(FALSE, FALSE);
4519 }
4520#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004521
Bram Moolenaar071d4272004-06-13 20:20:40 +00004522 /* Delete old text to be replaced, since we're still searching and
4523 * don't want to match ourselves! */
4524 ins_compl_delete();
4525 }
4526
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004527 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004528 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004529 compl_enter_selects = !insert_match && compl_match_array != NULL;
4530
Bram Moolenaar071d4272004-06-13 20:20:40 +00004531 /*
4532 * Show the file name for the match (if any)
4533 * Truncate the file name to avoid a wait for return.
4534 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004535 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004536 {
4537 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004538 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 if (i <= 0)
4540 i = 0;
4541 else
4542 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004543 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 msg(IObuff);
4545 redraw_cmdline = FALSE; /* don't overwrite! */
4546 }
4547
4548 return num_matches;
4549}
4550
4551/*
4552 * Call this while finding completions, to check whether the user has hit a key
4553 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004554 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004555 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004556 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 */
4558 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004559ins_compl_check_keys(frequency)
4560 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004561{
4562 static int count = 0;
4563
4564 int c;
4565
4566 /* Don't check when reading keys from a script. That would break the test
4567 * scripts */
4568 if (using_script())
4569 return;
4570
4571 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004572 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004573 return;
4574 count = 0;
4575
Bram Moolenaara260a972006-06-23 19:36:29 +00004576 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4577 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004578 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 if (c != NUL)
4580 {
4581 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4582 {
4583 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004584 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004585 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4586 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004588 else
4589 {
4590 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004591 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004592 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004593 if (c != K_IGNORE)
4594 {
4595 /* Don't interrupt completion when the character wasn't typed,
4596 * e.g., when doing @q to replay keys. */
4597 if (c != Ctrl_R && KeyTyped)
4598 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004599
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004600 vungetc(c);
4601 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004603 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004604 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004605 {
4606 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4607
4608 compl_pending = 0;
4609 (void)ins_compl_next(FALSE, todo, TRUE);
4610 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004611}
4612
4613/*
4614 * Decide the direction of Insert mode complete from the key typed.
4615 * Returns BACKWARD or FORWARD.
4616 */
4617 static int
4618ins_compl_key2dir(c)
4619 int c;
4620{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004621 if (c == Ctrl_P || c == Ctrl_L
4622 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4623 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004624 return BACKWARD;
4625 return FORWARD;
4626}
4627
4628/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004629 * Return TRUE for keys that are used for completion only when the popup menu
4630 * is visible.
4631 */
4632 static int
4633ins_compl_pum_key(c)
4634 int c;
4635{
4636 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004637 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4638 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004639}
4640
4641/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004642 * Decide the number of completions to move forward.
4643 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4644 */
4645 static int
4646ins_compl_key2count(c)
4647 int c;
4648{
4649 int h;
4650
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004651 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004652 {
4653 h = pum_get_height();
4654 if (h > 3)
4655 h -= 2; /* keep some context */
4656 return h;
4657 }
4658 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659}
4660
4661/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004662 * Return TRUE if completion with "c" should insert the match, FALSE if only
4663 * to change the currently selected completion.
4664 */
4665 static int
4666ins_compl_use_match(c)
4667 int c;
4668{
4669 switch (c)
4670 {
4671 case K_UP:
4672 case K_DOWN:
4673 case K_PAGEDOWN:
4674 case K_KPAGEDOWN:
4675 case K_S_DOWN:
4676 case K_PAGEUP:
4677 case K_KPAGEUP:
4678 case K_S_UP:
4679 return FALSE;
4680 }
4681 return TRUE;
4682}
4683
4684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 * Do Insert mode completion.
4686 * Called when character "c" was typed, which has a meaning for completion.
4687 * Returns OK if completion was done, FAIL if something failed (out of mem).
4688 */
4689 static int
4690ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004691 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004692{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004693 char_u *line;
4694 int startcol = 0; /* column where searched text starts */
4695 colnr_T curs_col; /* cursor column */
4696 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004697 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004698
Bram Moolenaare3226be2005-12-18 22:10:00 +00004699 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004700 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004701 {
4702 /* First time we hit ^N or ^P (in a row, I mean) */
4703
Bram Moolenaar071d4272004-06-13 20:20:40 +00004704 did_ai = FALSE;
4705#ifdef FEAT_SMARTINDENT
4706 did_si = FALSE;
4707 can_si = FALSE;
4708 can_si_back = FALSE;
4709#endif
4710 if (stop_arrow() == FAIL)
4711 return FAIL;
4712
4713 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004714 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004715 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004717 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 * "compl_startpos" to the cursor as a pattern to add a new word
4719 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004720 * "compl_startpos" is not in the same line as the cursor then fix it
4721 * (the line has been split because it was longer than 'tw'). if SOL
4722 * is set then skip the previous pattern, a word at the beginning of
4723 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004724 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4725 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 {
4727 /*
4728 * it is a continued search
4729 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004730 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4732 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4733 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004734 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 /* line (probably) wrapped, set compl_startpos to the
4737 * first non_blank in the line, if it is not a wordchar
4738 * include it to get a better pattern, but then we don't
4739 * want the "\\<" prefix, check it bellow */
4740 compl_col = (colnr_T)(skipwhite(line) - line);
4741 compl_startpos.col = compl_col;
4742 compl_startpos.lnum = curwin->w_cursor.lnum;
4743 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 }
4745 else
4746 {
4747 /* S_IPOS was set when we inserted a word that was at the
4748 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004749 * mode but first we need to redefine compl_startpos */
4750 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004752 compl_cont_status |= CONT_SOL;
4753 compl_startpos.col = (colnr_T)(skipwhite(
4754 line + compl_length
4755 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004757 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004759 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004760 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004761 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004763 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004765 compl_cont_status &= ~CONT_SOL;
4766 compl_length = (IOSIZE - MIN_SPACE);
4767 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004769 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4770 if (compl_length < 1)
4771 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 }
4773 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004774 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004776 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004777 }
4778 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004779 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004781 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004783 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004785 compl_cont_status = 0;
4786 compl_cont_status |= CONT_N_ADDS;
4787 compl_startpos = curwin->w_cursor;
4788 startcol = (int)curs_col;
4789 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 }
4791
4792 /* Work out completion pattern and original text -- webb */
4793 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4794 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004795 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4797 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004798 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004800 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004802 compl_col += ++startcol;
4803 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
4805 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004806 compl_pattern = str_foldcase(line + compl_col,
4807 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004809 compl_pattern = vim_strnsave(line + compl_col,
4810 compl_length);
4811 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 return FAIL;
4813 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004814 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 {
4816 char_u *prefix = (char_u *)"\\<";
4817
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004818 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004819 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004820 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004821 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 if (!vim_iswordp(line + compl_col)
4824 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 && (
4826#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004827 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004829 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004830#endif
4831 )))
4832 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004833 STRCPY((char *)compl_pattern, prefix);
4834 (void)quote_meta(compl_pattern + STRLEN(prefix),
4835 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004837 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004839 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004841 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842#endif
4843 )
4844 {
4845 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004846 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4847 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004849 compl_col += curs_col;
4850 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 }
4852 else
4853 {
4854#ifdef FEAT_MBYTE
4855 /* Search the point of change class of multibyte character
4856 * or not a word single byte character backward. */
4857 if (has_mbyte)
4858 {
4859 int base_class;
4860 int head_off;
4861
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004862 startcol -= (*mb_head_off)(line, line + startcol);
4863 base_class = mb_get_class(line + startcol);
4864 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004866 head_off = (*mb_head_off)(line, line + startcol);
4867 if (base_class != mb_get_class(line + startcol
4868 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004870 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 }
4872 }
4873 else
4874#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004875 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004877 compl_col += ++startcol;
4878 compl_length = (int)curs_col - startcol;
4879 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 /* Only match word with at least two chars -- webb
4882 * there's no need to call quote_meta,
4883 * alloc(7) is enough -- Acevedo
4884 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004885 compl_pattern = alloc(7);
4886 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004888 STRCPY((char *)compl_pattern, "\\<");
4889 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4890 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 }
4892 else
4893 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004894 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004895 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004896 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004898 STRCPY((char *)compl_pattern, "\\<");
4899 (void)quote_meta(compl_pattern + 2, line + compl_col,
4900 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
4902 }
4903 }
4904 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4905 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004906 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004907 compl_length = (int)curs_col - (int)compl_col;
4908 if (compl_length < 0) /* cursor in indent: empty pattern */
4909 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004911 compl_pattern = str_foldcase(line + compl_col, compl_length,
4912 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004914 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4915 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 return FAIL;
4917 }
4918 else if (ctrl_x_mode == CTRL_X_FILES)
4919 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004920 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004922 compl_col += ++startcol;
4923 compl_length = (int)curs_col - startcol;
4924 compl_pattern = addstar(line + compl_col, compl_length,
4925 EXPAND_FILES);
4926 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 return FAIL;
4928 }
4929 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4930 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004931 compl_pattern = vim_strnsave(line, curs_col);
4932 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004934 set_cmd_context(&compl_xp, compl_pattern,
4935 (int)STRLEN(compl_pattern), curs_col);
4936 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4937 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004938 /* No completion possible, use an empty pattern to get a
4939 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004940 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004941 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004942 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4943 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004945 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004946 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004947#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004948 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004949 * Call user defined function 'completefunc' with "a:findstart"
4950 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004951 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004952 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004953 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004954 char_u *funcname;
4955 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004956
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004957 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004958 * string */
4959 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4960 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4961 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004962 {
4963 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4964 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004965 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004966 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004967
4968 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004969 args[1] = NULL;
4970 pos = curwin->w_cursor;
4971 col = call_func_retnr(funcname, 2, args, FALSE);
4972 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004973
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004974 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004975 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004976 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004977 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004978 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004979
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004980 /* Setup variables for completion. Need to obtain "line" again,
4981 * it may have become invalid. */
4982 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004983 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004984 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4985 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004986#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004987 return FAIL;
4988 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004989 else if (ctrl_x_mode == CTRL_X_SPELL)
4990 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004991#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004992 if (spell_bad_len > 0)
4993 compl_col = curs_col - spell_bad_len;
4994 else
4995 compl_col = spell_word_start(startcol);
4996 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004997 {
4998 compl_length = 0;
4999 compl_col = curs_col;
5000 }
5001 else
5002 {
5003 spell_expand_check_cap(compl_col);
5004 compl_length = (int)curs_col - compl_col;
5005 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005006 /* Need to obtain "line" again, it may have become invalid. */
5007 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005008 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5009 if (compl_pattern == NULL)
5010#endif
5011 return FAIL;
5012 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005013 else
5014 {
5015 EMSG2(_(e_intern2), "ins_complete()");
5016 return FAIL;
5017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005018
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005019 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 {
5021 edit_submode_pre = (char_u *)_(" Adding");
5022 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5023 {
5024 /* Insert a new line, keep indentation but ignore 'comments' */
5025#ifdef FEAT_COMMENTS
5026 char_u *old = curbuf->b_p_com;
5027
5028 curbuf->b_p_com = (char_u *)"";
5029#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005030 compl_startpos.lnum = curwin->w_cursor.lnum;
5031 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 ins_eol('\r');
5033#ifdef FEAT_COMMENTS
5034 curbuf->b_p_com = old;
5035#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005036 compl_length = 0;
5037 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 }
5039 }
5040 else
5041 {
5042 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005043 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 }
5045
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005046 if (compl_cont_status & CONT_LOCAL)
5047 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 else
5049 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5050
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005051 /* Always add completion for the original text. */
5052 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005053 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5054 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005055 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005057 vim_free(compl_pattern);
5058 compl_pattern = NULL;
5059 vim_free(compl_orig_text);
5060 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061 return FAIL;
5062 }
5063
5064 /* showmode might reset the internal line pointers, so it must
5065 * be called before line = ml_get(), or when this address is no
5066 * longer needed. -- Acevedo.
5067 */
5068 edit_submode_extra = (char_u *)_("-- Searching...");
5069 edit_submode_highl = HLF_COUNT;
5070 showmode();
5071 edit_submode_extra = NULL;
5072 out_flush();
5073 }
5074
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005075 compl_shown_match = compl_curr_match;
5076 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077
5078 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005079 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005081 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005082 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005083
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005084 /* may undisplay the popup menu */
5085 ins_compl_upd_pum();
5086
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005087 if (n > 1) /* all matches have been found */
5088 compl_matches = n;
5089 compl_curr_match = compl_shown_match;
5090 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
Bram Moolenaard68071d2006-05-02 22:08:30 +00005092 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5093 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 if (got_int && !global_busy)
5095 {
5096 (void)vgetc();
5097 got_int = FALSE;
5098 }
5099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005100 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005101 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005102 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5104 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5106 edit_submode_highl = HLF_E;
5107 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5108 * because we couldn't expand anything at first place, but if we used
5109 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5110 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 if ( compl_length > 1
5112 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 || (ctrl_x_mode != 0
5114 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5115 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 }
5118
Bram Moolenaar572cb562005-08-05 21:35:02 +00005119 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005120 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005122 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123
5124 if (edit_submode_extra == NULL)
5125 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005126 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127 {
5128 edit_submode_extra = (char_u *)_("Back at original");
5129 edit_submode_highl = HLF_W;
5130 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005131 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
5133 edit_submode_extra = (char_u *)_("Word from other line");
5134 edit_submode_highl = HLF_COUNT;
5135 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005136 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 {
5138 edit_submode_extra = (char_u *)_("The only match");
5139 edit_submode_highl = HLF_COUNT;
5140 }
5141 else
5142 {
5143 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005144 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005146 int number = 0;
5147 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005149 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 {
5151 /* search backwards for the first valid (!= -1) number.
5152 * This should normally succeed already at the first loop
5153 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005154 for (match = compl_curr_match->cp_prev; match != NULL
5155 && match != compl_first_match;
5156 match = match->cp_prev)
5157 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005159 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 break;
5161 }
5162 if (match != NULL)
5163 /* go up and assign all numbers which are not assigned
5164 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005165 for (match = match->cp_next;
5166 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005167 match = match->cp_next)
5168 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170 else /* BACKWARD */
5171 {
5172 /* search forwards (upwards) for the first valid (!= -1)
5173 * number. This should normally succeed already at the
5174 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005175 for (match = compl_curr_match->cp_next; match != NULL
5176 && match != compl_first_match;
5177 match = match->cp_next)
5178 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005180 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 break;
5182 }
5183 if (match != NULL)
5184 /* go down and assign all numbers which are not
5185 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005186 for (match = match->cp_prev; match
5187 && match->cp_number == -1;
5188 match = match->cp_prev)
5189 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 }
5191 }
5192
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005193 /* The match should always have a sequence number now, this is
5194 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005195 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005197 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5198 * Translations may need more than twice that. */
5199 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005201 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005202 vim_snprintf((char *)match_ref, sizeof(match_ref),
5203 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005204 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005206 vim_snprintf((char *)match_ref, sizeof(match_ref),
5207 _("match %d"),
5208 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 edit_submode_extra = match_ref;
5210 edit_submode_highl = HLF_R;
5211 if (dollar_vcol)
5212 curs_columns(FALSE);
5213 }
5214 }
5215 }
5216
5217 /* Show a message about what (completion) mode we're in. */
5218 showmode();
5219 if (edit_submode_extra != NULL)
5220 {
5221 if (!p_smd)
5222 msg_attr(edit_submode_extra,
5223 edit_submode_highl < HLF_COUNT
5224 ? hl_attr(edit_submode_highl) : 0);
5225 }
5226 else
5227 msg_clr_cmdline(); /* necessary for "noshowmode" */
5228
Bram Moolenaard68071d2006-05-02 22:08:30 +00005229 /* Show the popup menu, unless we got interrupted. */
5230 if (!compl_interrupted)
5231 {
5232 /* RedrawingDisabled may be set when invoked through complete(). */
5233 n = RedrawingDisabled;
5234 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005235
5236 /* If the cursor moved we need to remove the pum first. */
5237 setcursor();
5238 if (save_w_wrow != curwin->w_wrow)
5239 ins_compl_del_pum();
5240
Bram Moolenaard68071d2006-05-02 22:08:30 +00005241 ins_compl_show_pum();
5242 setcursor();
5243 RedrawingDisabled = n;
5244 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005245 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005246 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005247
Bram Moolenaar071d4272004-06-13 20:20:40 +00005248 return OK;
5249}
5250
5251/*
5252 * Looks in the first "len" chars. of "src" for search-metachars.
5253 * If dest is not NULL the chars. are copied there quoting (with
5254 * a backslash) the metachars, and dest would be NUL terminated.
5255 * Returns the length (needed) of dest
5256 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005257 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005258quote_meta(dest, src, len)
5259 char_u *dest;
5260 char_u *src;
5261 int len;
5262{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005263 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005264
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005265 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 {
5267 switch (*src)
5268 {
5269 case '.':
5270 case '*':
5271 case '[':
5272 if (ctrl_x_mode == CTRL_X_DICTIONARY
5273 || ctrl_x_mode == CTRL_X_THESAURUS)
5274 break;
5275 case '~':
5276 if (!p_magic) /* quote these only if magic is set */
5277 break;
5278 case '\\':
5279 if (ctrl_x_mode == CTRL_X_DICTIONARY
5280 || ctrl_x_mode == CTRL_X_THESAURUS)
5281 break;
5282 case '^': /* currently it's not needed. */
5283 case '$':
5284 m++;
5285 if (dest != NULL)
5286 *dest++ = '\\';
5287 break;
5288 }
5289 if (dest != NULL)
5290 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005291# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005292 /* Copy remaining bytes of a multibyte character. */
5293 if (has_mbyte)
5294 {
5295 int i, mb_len;
5296
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005297 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 if (mb_len > 0 && len >= mb_len)
5299 for (i = 0; i < mb_len; ++i)
5300 {
5301 --len;
5302 ++src;
5303 if (dest != NULL)
5304 *dest++ = *src;
5305 }
5306 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005307# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005308 }
5309 if (dest != NULL)
5310 *dest = NUL;
5311
5312 return m;
5313}
5314#endif /* FEAT_INS_EXPAND */
5315
5316/*
5317 * Next character is interpreted literally.
5318 * A one, two or three digit decimal number is interpreted as its byte value.
5319 * If one or two digits are entered, the next character is given to vungetc().
5320 * For Unicode a character > 255 may be returned.
5321 */
5322 int
5323get_literal()
5324{
5325 int cc;
5326 int nc;
5327 int i;
5328 int hex = FALSE;
5329 int octal = FALSE;
5330#ifdef FEAT_MBYTE
5331 int unicode = 0;
5332#endif
5333
5334 if (got_int)
5335 return Ctrl_C;
5336
5337#ifdef FEAT_GUI
5338 /*
5339 * In GUI there is no point inserting the internal code for a special key.
5340 * It is more useful to insert the string "<KEY>" instead. This would
5341 * probably be useful in a text window too, but it would not be
5342 * vi-compatible (maybe there should be an option for it?) -- webb
5343 */
5344 if (gui.in_use)
5345 ++allow_keys;
5346#endif
5347#ifdef USE_ON_FLY_SCROLL
5348 dont_scroll = TRUE; /* disallow scrolling here */
5349#endif
5350 ++no_mapping; /* don't map the next key hits */
5351 cc = 0;
5352 i = 0;
5353 for (;;)
5354 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005355 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356#ifdef FEAT_CMDL_INFO
5357 if (!(State & CMDLINE)
5358# ifdef FEAT_MBYTE
5359 && MB_BYTE2LEN_CHECK(nc) == 1
5360# endif
5361 )
5362 add_to_showcmd(nc);
5363#endif
5364 if (nc == 'x' || nc == 'X')
5365 hex = TRUE;
5366 else if (nc == 'o' || nc == 'O')
5367 octal = TRUE;
5368#ifdef FEAT_MBYTE
5369 else if (nc == 'u' || nc == 'U')
5370 unicode = nc;
5371#endif
5372 else
5373 {
5374 if (hex
5375#ifdef FEAT_MBYTE
5376 || unicode != 0
5377#endif
5378 )
5379 {
5380 if (!vim_isxdigit(nc))
5381 break;
5382 cc = cc * 16 + hex2nr(nc);
5383 }
5384 else if (octal)
5385 {
5386 if (nc < '0' || nc > '7')
5387 break;
5388 cc = cc * 8 + nc - '0';
5389 }
5390 else
5391 {
5392 if (!VIM_ISDIGIT(nc))
5393 break;
5394 cc = cc * 10 + nc - '0';
5395 }
5396
5397 ++i;
5398 }
5399
5400 if (cc > 255
5401#ifdef FEAT_MBYTE
5402 && unicode == 0
5403#endif
5404 )
5405 cc = 255; /* limit range to 0-255 */
5406 nc = 0;
5407
5408 if (hex) /* hex: up to two chars */
5409 {
5410 if (i >= 2)
5411 break;
5412 }
5413#ifdef FEAT_MBYTE
5414 else if (unicode) /* Unicode: up to four or eight chars */
5415 {
5416 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5417 break;
5418 }
5419#endif
5420 else if (i >= 3) /* decimal or octal: up to three chars */
5421 break;
5422 }
5423 if (i == 0) /* no number entered */
5424 {
5425 if (nc == K_ZERO) /* NUL is stored as NL */
5426 {
5427 cc = '\n';
5428 nc = 0;
5429 }
5430 else
5431 {
5432 cc = nc;
5433 nc = 0;
5434 }
5435 }
5436
5437 if (cc == 0) /* NUL is stored as NL */
5438 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005439#ifdef FEAT_MBYTE
5440 if (enc_dbcs && (cc & 0xff) == 0)
5441 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5442 second byte will cause trouble! */
5443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005444
5445 --no_mapping;
5446#ifdef FEAT_GUI
5447 if (gui.in_use)
5448 --allow_keys;
5449#endif
5450 if (nc)
5451 vungetc(nc);
5452 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5453 return cc;
5454}
5455
5456/*
5457 * Insert character, taking care of special keys and mod_mask
5458 */
5459 static void
5460insert_special(c, allow_modmask, ctrlv)
5461 int c;
5462 int allow_modmask;
5463 int ctrlv; /* c was typed after CTRL-V */
5464{
5465 char_u *p;
5466 int len;
5467
5468 /*
5469 * Special function key, translate into "<Key>". Up to the last '>' is
5470 * inserted with ins_str(), so as not to replace characters in replace
5471 * mode.
5472 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5473 * unless 'allow_modmask' is TRUE.
5474 */
5475#ifdef MACOS
5476 /* Command-key never produces a normal key */
5477 if (mod_mask & MOD_MASK_CMD)
5478 allow_modmask = TRUE;
5479#endif
5480 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5481 {
5482 p = get_special_key_name(c, mod_mask);
5483 len = (int)STRLEN(p);
5484 c = p[len - 1];
5485 if (len > 2)
5486 {
5487 if (stop_arrow() == FAIL)
5488 return;
5489 p[len - 1] = NUL;
5490 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005491 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005492 ctrlv = FALSE;
5493 }
5494 }
5495 if (stop_arrow() == OK)
5496 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5497}
5498
5499/*
5500 * Special characters in this context are those that need processing other
5501 * than the simple insertion that can be performed here. This includes ESC
5502 * which terminates the insert, and CR/NL which need special processing to
5503 * open up a new line. This routine tries to optimize insertions performed by
5504 * the "redo", "undo" or "put" commands, so it needs to know when it should
5505 * stop and defer processing to the "normal" mechanism.
5506 * '0' and '^' are special, because they can be followed by CTRL-D.
5507 */
5508#ifdef EBCDIC
5509# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5510#else
5511# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5512#endif
5513
5514#ifdef FEAT_MBYTE
5515# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5516#else
5517# define WHITECHAR(cc) vim_iswhite(cc)
5518#endif
5519
5520 void
5521insertchar(c, flags, second_indent)
5522 int c; /* character to insert or NUL */
5523 int flags; /* INSCHAR_FORMAT, etc. */
5524 int second_indent; /* indent for second line if >= 0 */
5525{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 int textwidth;
5527#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005531
5532 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5533 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005534
5535 /*
5536 * Try to break the line in two or more pieces when:
5537 * - Always do this if we have been called to do formatting only.
5538 * - Always do this when 'formatoptions' has the 'a' flag and the line
5539 * ends in white space.
5540 * - Otherwise:
5541 * - Don't do this if inserting a blank
5542 * - Don't do this if an existing character is being replaced, unless
5543 * we're in VREPLACE mode.
5544 * - Do this if the cursor is not on the line where insert started
5545 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5546 * before the insert.
5547 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5548 * before 'textwidth'
5549 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005550 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005551 && ((flags & INSCHAR_FORMAT)
5552 || (!vim_iswhite(c)
5553 && !((State & REPLACE_FLAG)
5554#ifdef FEAT_VREPLACE
5555 && !(State & VREPLACE_FLAG)
5556#endif
5557 && *ml_get_cursor() != NUL)
5558 && (curwin->w_cursor.lnum != Insstart.lnum
5559 || ((!has_format_option(FO_INS_LONG)
5560 || Insstart_textlen <= (colnr_T)textwidth)
5561 && (!fo_ins_blank
5562 || Insstart_blank_vcol <= (colnr_T)textwidth
5563 ))))))
5564 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005565 /* Format with 'formatexpr' when it's set. Use internal formatting
5566 * when 'formatexpr' isn't set or it returns non-zero. */
5567#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005568 int do_internal = TRUE;
5569
Bram Moolenaar81a82092008-03-12 16:27:00 +00005570 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005571 {
5572 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5573 /* It may be required to save for undo again, e.g. when setline()
5574 * was called. */
5575 ins_need_undo = TRUE;
5576 }
5577 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005579 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005580 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005581
Bram Moolenaar071d4272004-06-13 20:20:40 +00005582 if (c == NUL) /* only formatting was wanted */
5583 return;
5584
5585#ifdef FEAT_COMMENTS
5586 /* Check whether this character should end a comment. */
5587 if (did_ai && (int)c == end_comment_pending)
5588 {
5589 char_u *line;
5590 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5591 int middle_len, end_len;
5592 int i;
5593
5594 /*
5595 * Need to remove existing (middle) comment leader and insert end
5596 * comment leader. First, check what comment leader we can find.
5597 */
5598 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5599 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5600 {
5601 /* Skip middle-comment string */
5602 while (*p && p[-1] != ':') /* find end of middle flags */
5603 ++p;
5604 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5605 /* Don't count trailing white space for middle_len */
5606 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5607 --middle_len;
5608
5609 /* Find the end-comment string */
5610 while (*p && p[-1] != ':') /* find end of end flags */
5611 ++p;
5612 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5613
5614 /* Skip white space before the cursor */
5615 i = curwin->w_cursor.col;
5616 while (--i >= 0 && vim_iswhite(line[i]))
5617 ;
5618 i++;
5619
5620 /* Skip to before the middle leader */
5621 i -= middle_len;
5622
5623 /* Check some expected things before we go on */
5624 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5625 {
5626 /* Backspace over all the stuff we want to replace */
5627 backspace_until_column(i);
5628
5629 /*
5630 * Insert the end-comment string, except for the last
5631 * character, which will get inserted as normal later.
5632 */
5633 ins_bytes_len(lead_end, end_len - 1);
5634 }
5635 }
5636 }
5637 end_comment_pending = NUL;
5638#endif
5639
5640 did_ai = FALSE;
5641#ifdef FEAT_SMARTINDENT
5642 did_si = FALSE;
5643 can_si = FALSE;
5644 can_si_back = FALSE;
5645#endif
5646
5647 /*
5648 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5649 * This speeds up normal text input considerably.
5650 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5651 * need to re-indent at a ':', or any other character (but not what
5652 * 'paste' is set)..
5653 */
5654#ifdef USE_ON_FLY_SCROLL
5655 dont_scroll = FALSE; /* allow scrolling here */
5656#endif
5657
5658 if ( !ISSPECIAL(c)
5659#ifdef FEAT_MBYTE
5660 && (!has_mbyte || (*mb_char2len)(c) == 1)
5661#endif
5662 && vpeekc() != NUL
5663 && !(State & REPLACE_FLAG)
5664#ifdef FEAT_CINDENT
5665 && !cindent_on()
5666#endif
5667#ifdef FEAT_RIGHTLEFT
5668 && !p_ri
5669#endif
5670 )
5671 {
5672#define INPUT_BUFLEN 100
5673 char_u buf[INPUT_BUFLEN + 1];
5674 int i;
5675 colnr_T virtcol = 0;
5676
5677 buf[0] = c;
5678 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005679 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 virtcol = get_nolist_virtcol();
5681 /*
5682 * Stop the string when:
5683 * - no more chars available
5684 * - finding a special character (command key)
5685 * - buffer is full
5686 * - running into the 'textwidth' boundary
5687 * - need to check for abbreviation: A non-word char after a word-char
5688 */
5689 while ( (c = vpeekc()) != NUL
5690 && !ISSPECIAL(c)
5691#ifdef FEAT_MBYTE
5692 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5693#endif
5694 && i < INPUT_BUFLEN
5695 && (textwidth == 0
5696 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5697 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5698 {
5699#ifdef FEAT_RIGHTLEFT
5700 c = vgetc();
5701 if (p_hkmap && KeyTyped)
5702 c = hkmap(c); /* Hebrew mode mapping */
5703# ifdef FEAT_FKMAP
5704 if (p_fkmap && KeyTyped)
5705 c = fkmap(c); /* Farsi mode mapping */
5706# endif
5707 buf[i++] = c;
5708#else
5709 buf[i++] = vgetc();
5710#endif
5711 }
5712
5713#ifdef FEAT_DIGRAPHS
5714 do_digraph(-1); /* clear digraphs */
5715 do_digraph(buf[i-1]); /* may be the start of a digraph */
5716#endif
5717 buf[i] = NUL;
5718 ins_str(buf);
5719 if (flags & INSCHAR_CTRLV)
5720 {
5721 redo_literal(*buf);
5722 i = 1;
5723 }
5724 else
5725 i = 0;
5726 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005727 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005728 }
5729 else
5730 {
5731#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005732 int cc;
5733
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5735 {
5736 char_u buf[MB_MAXBYTES + 1];
5737
5738 (*mb_char2bytes)(c, buf);
5739 buf[cc] = NUL;
5740 ins_char_bytes(buf, cc);
5741 AppendCharToRedobuff(c);
5742 }
5743 else
5744#endif
5745 {
5746 ins_char(c);
5747 if (flags & INSCHAR_CTRLV)
5748 redo_literal(c);
5749 else
5750 AppendCharToRedobuff(c);
5751 }
5752 }
5753}
5754
5755/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005756 * Format text at the current insert position.
5757 */
5758 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00005759internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005760 int textwidth;
5761 int second_indent;
5762 int flags;
5763 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005764 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005765{
5766 int cc;
5767 int save_char = NUL;
5768 int haveto_redraw = FALSE;
5769 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5770#ifdef FEAT_MBYTE
5771 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5772#endif
5773 int fo_white_par = has_format_option(FO_WHITE_PAR);
5774 int first_line = TRUE;
5775#ifdef FEAT_COMMENTS
5776 colnr_T leader_len;
5777 int no_leader = FALSE;
5778 int do_comments = (flags & INSCHAR_DO_COM);
5779#endif
5780
5781 /*
5782 * When 'ai' is off we don't want a space under the cursor to be
5783 * deleted. Replace it with an 'x' temporarily.
5784 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005785 if (!curbuf->b_p_ai
5786#ifdef FEAT_VREPLACE
5787 && !(State & VREPLACE_FLAG)
5788#endif
5789 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005790 {
5791 cc = gchar_cursor();
5792 if (vim_iswhite(cc))
5793 {
5794 save_char = cc;
5795 pchar_cursor('x');
5796 }
5797 }
5798
5799 /*
5800 * Repeat breaking lines, until the current line is not too long.
5801 */
5802 while (!got_int)
5803 {
5804 int startcol; /* Cursor column at entry */
5805 int wantcol; /* column at textwidth border */
5806 int foundcol; /* column for start of spaces */
5807 int end_foundcol = 0; /* column for start of word */
5808 colnr_T len;
5809 colnr_T virtcol;
5810#ifdef FEAT_VREPLACE
5811 int orig_col = 0;
5812 char_u *saved_text = NULL;
5813#endif
5814 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005815 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005816
Bram Moolenaar97b98102009-11-17 16:41:01 +00005817 virtcol = get_nolist_virtcol()
5818 + char2cells(c != NUL ? c : gchar_cursor());
5819 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005820 break;
5821
5822#ifdef FEAT_COMMENTS
5823 if (no_leader)
5824 do_comments = FALSE;
5825 else if (!(flags & INSCHAR_FORMAT)
5826 && has_format_option(FO_WRAP_COMS))
5827 do_comments = TRUE;
5828
5829 /* Don't break until after the comment leader */
5830 if (do_comments)
5831 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5832 else
5833 leader_len = 0;
5834
5835 /* If the line doesn't start with a comment leader, then don't
5836 * start one in a following broken line. Avoids that a %word
5837 * moved to the start of the next line causes all following lines
5838 * to start with %. */
5839 if (leader_len == 0)
5840 no_leader = TRUE;
5841#endif
5842 if (!(flags & INSCHAR_FORMAT)
5843#ifdef FEAT_COMMENTS
5844 && leader_len == 0
5845#endif
5846 && !has_format_option(FO_WRAP))
5847
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005848 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005849 if ((startcol = curwin->w_cursor.col) == 0)
5850 break;
5851
5852 /* find column of textwidth border */
5853 coladvance((colnr_T)textwidth);
5854 wantcol = curwin->w_cursor.col;
5855
Bram Moolenaar97b98102009-11-17 16:41:01 +00005856 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005857 foundcol = 0;
5858
5859 /*
5860 * Find position to break at.
5861 * Stop at first entered white when 'formatoptions' has 'v'
5862 */
5863 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5864 || curwin->w_cursor.lnum != Insstart.lnum
5865 || curwin->w_cursor.col >= Insstart.col)
5866 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00005867 if (curwin->w_cursor.col == startcol && c != NUL)
5868 cc = c;
5869 else
5870 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005871 if (WHITECHAR(cc))
5872 {
5873 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005874 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005875
5876 /* find start of sequence of blanks */
5877 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5878 {
5879 dec_cursor();
5880 cc = gchar_cursor();
5881 }
5882 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5883 break; /* only spaces in front of text */
5884#ifdef FEAT_COMMENTS
5885 /* Don't break until after the comment leader */
5886 if (curwin->w_cursor.col < leader_len)
5887 break;
5888#endif
5889 if (has_format_option(FO_ONE_LETTER))
5890 {
5891 /* do not break after one-letter words */
5892 if (curwin->w_cursor.col == 0)
5893 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005894#ifdef FEAT_COMMENTS
5895 /* do not break "#a b" when 'tw' is 2 */
5896 if (curwin->w_cursor.col <= leader_len)
5897 break;
5898#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005899 col = curwin->w_cursor.col;
5900 dec_cursor();
5901 cc = gchar_cursor();
5902
5903 if (WHITECHAR(cc))
5904 continue; /* one-letter, continue */
5905 curwin->w_cursor.col = col;
5906 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00005907
5908 inc_cursor();
5909
5910 end_foundcol = end_col + 1;
5911 foundcol = curwin->w_cursor.col;
5912 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005913 break;
5914 }
5915#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00005916 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005917 {
5918 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005919 if (curwin->w_cursor.col != startcol)
5920 {
5921#ifdef FEAT_COMMENTS
5922 /* Don't break until after the comment leader */
5923 if (curwin->w_cursor.col < leader_len)
5924 break;
5925#endif
5926 col = curwin->w_cursor.col;
5927 inc_cursor();
5928 /* Don't change end_foundcol if already set. */
5929 if (foundcol != curwin->w_cursor.col)
5930 {
5931 foundcol = curwin->w_cursor.col;
5932 end_foundcol = foundcol;
5933 if (curwin->w_cursor.col <= (colnr_T)wantcol)
5934 break;
5935 }
5936 curwin->w_cursor.col = col;
5937 }
5938
5939 if (curwin->w_cursor.col == 0)
5940 break;
5941
5942 col = curwin->w_cursor.col;
5943
5944 dec_cursor();
5945 cc = gchar_cursor();
5946
5947 if (WHITECHAR(cc))
5948 continue; /* break with space */
5949#ifdef FEAT_COMMENTS
5950 /* Don't break until after the comment leader */
5951 if (curwin->w_cursor.col < leader_len)
5952 break;
5953#endif
5954
5955 curwin->w_cursor.col = col;
5956
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005957 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005958 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005959 if (curwin->w_cursor.col <= (colnr_T)wantcol)
5960 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005961 }
5962#endif
5963 if (curwin->w_cursor.col == 0)
5964 break;
5965 dec_cursor();
5966 }
5967
5968 if (foundcol == 0) /* no spaces, cannot break line */
5969 {
5970 curwin->w_cursor.col = startcol;
5971 break;
5972 }
5973
5974 /* Going to break the line, remove any "$" now. */
5975 undisplay_dollar();
5976
5977 /*
5978 * Offset between cursor position and line break is used by replace
5979 * stack functions. VREPLACE does not use this, and backspaces
5980 * over the text instead.
5981 */
5982#ifdef FEAT_VREPLACE
5983 if (State & VREPLACE_FLAG)
5984 orig_col = startcol; /* Will start backspacing from here */
5985 else
5986#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005987 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005988
5989 /*
5990 * adjust startcol for spaces that will be deleted and
5991 * characters that will remain on top line
5992 */
5993 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005994 while ((cc = gchar_cursor(), WHITECHAR(cc))
5995 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005996 inc_cursor();
5997 startcol -= curwin->w_cursor.col;
5998 if (startcol < 0)
5999 startcol = 0;
6000
6001#ifdef FEAT_VREPLACE
6002 if (State & VREPLACE_FLAG)
6003 {
6004 /*
6005 * In VREPLACE mode, we will backspace over the text to be
6006 * wrapped, so save a copy now to put on the next line.
6007 */
6008 saved_text = vim_strsave(ml_get_cursor());
6009 curwin->w_cursor.col = orig_col;
6010 if (saved_text == NULL)
6011 break; /* Can't do it, out of memory */
6012 saved_text[startcol] = NUL;
6013
6014 /* Backspace over characters that will move to the next line */
6015 if (!fo_white_par)
6016 backspace_until_column(foundcol);
6017 }
6018 else
6019#endif
6020 {
6021 /* put cursor after pos. to break line */
6022 if (!fo_white_par)
6023 curwin->w_cursor.col = foundcol;
6024 }
6025
6026 /*
6027 * Split the line just before the margin.
6028 * Only insert/delete lines, but don't really redraw the window.
6029 */
6030 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6031 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6032#ifdef FEAT_COMMENTS
6033 + (do_comments ? OPENLINE_DO_COM : 0)
6034#endif
6035 , old_indent);
6036 old_indent = 0;
6037
6038 replace_offset = 0;
6039 if (first_line)
6040 {
6041 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6042 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6043 if (second_indent >= 0)
6044 {
6045#ifdef FEAT_VREPLACE
6046 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006047 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006048 else
6049#endif
6050 (void)set_indent(second_indent, SIN_CHANGED);
6051 }
6052 first_line = FALSE;
6053 }
6054
6055#ifdef FEAT_VREPLACE
6056 if (State & VREPLACE_FLAG)
6057 {
6058 /*
6059 * In VREPLACE mode we have backspaced over the text to be
6060 * moved, now we re-insert it into the new line.
6061 */
6062 ins_bytes(saved_text);
6063 vim_free(saved_text);
6064 }
6065 else
6066#endif
6067 {
6068 /*
6069 * Check if cursor is not past the NUL off the line, cindent
6070 * may have added or removed indent.
6071 */
6072 curwin->w_cursor.col += startcol;
6073 len = (colnr_T)STRLEN(ml_get_curline());
6074 if (curwin->w_cursor.col > len)
6075 curwin->w_cursor.col = len;
6076 }
6077
6078 haveto_redraw = TRUE;
6079#ifdef FEAT_CINDENT
6080 can_cindent = TRUE;
6081#endif
6082 /* moved the cursor, don't autoindent or cindent now */
6083 did_ai = FALSE;
6084#ifdef FEAT_SMARTINDENT
6085 did_si = FALSE;
6086 can_si = FALSE;
6087 can_si_back = FALSE;
6088#endif
6089 line_breakcheck();
6090 }
6091
6092 if (save_char != NUL) /* put back space after cursor */
6093 pchar_cursor(save_char);
6094
6095 if (!format_only && haveto_redraw)
6096 {
6097 update_topline();
6098 redraw_curbuf_later(VALID);
6099 }
6100}
6101
6102/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006103 * Called after inserting or deleting text: When 'formatoptions' includes the
6104 * 'a' flag format from the current line until the end of the paragraph.
6105 * Keep the cursor at the same position relative to the text.
6106 * The caller must have saved the cursor line for undo, following ones will be
6107 * saved here.
6108 */
6109 void
6110auto_format(trailblank, prev_line)
6111 int trailblank; /* when TRUE also format with trailing blank */
6112 int prev_line; /* may start in previous line */
6113{
6114 pos_T pos;
6115 colnr_T len;
6116 char_u *old;
6117 char_u *new, *pnew;
6118 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006119 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006120
6121 if (!has_format_option(FO_AUTO))
6122 return;
6123
6124 pos = curwin->w_cursor;
6125 old = ml_get_curline();
6126
6127 /* may remove added space */
6128 check_auto_format(FALSE);
6129
6130 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6131 * user might insert normal text next. Also skip formatting when "1" is
6132 * in 'formatoptions' and there is a single character before the cursor.
6133 * Otherwise the line would be broken and when typing another non-white
6134 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006135 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006136 if (*old != NUL && !trailblank && wasatend)
6137 {
6138 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006139 cc = gchar_cursor();
6140 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6141 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006142 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006143 cc = gchar_cursor();
6144 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006145 {
6146 curwin->w_cursor = pos;
6147 return;
6148 }
6149 curwin->w_cursor = pos;
6150 }
6151
6152#ifdef FEAT_COMMENTS
6153 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6154 * comments. */
6155 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6156 && get_leader_len(old, NULL, FALSE) == 0)
6157 return;
6158#endif
6159
6160 /*
6161 * May start formatting in a previous line, so that after "x" a word is
6162 * moved to the previous line if it fits there now. Only when this is not
6163 * the start of a paragraph.
6164 */
6165 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6166 {
6167 --curwin->w_cursor.lnum;
6168 if (u_save_cursor() == FAIL)
6169 return;
6170 }
6171
6172 /*
6173 * Do the formatting and restore the cursor position. "saved_cursor" will
6174 * be adjusted for the text formatting.
6175 */
6176 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006177 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 curwin->w_cursor = saved_cursor;
6179 saved_cursor.lnum = 0;
6180
6181 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6182 {
6183 /* "cannot happen" */
6184 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6185 coladvance((colnr_T)MAXCOL);
6186 }
6187 else
6188 check_cursor_col();
6189
6190 /* Insert mode: If the cursor is now after the end of the line while it
6191 * previously wasn't, the line was broken. Because of the rule above we
6192 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6193 * formatted. */
6194 if (!wasatend && has_format_option(FO_WHITE_PAR))
6195 {
6196 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006197 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198 if (curwin->w_cursor.col == len)
6199 {
6200 pnew = vim_strnsave(new, len + 2);
6201 pnew[len] = ' ';
6202 pnew[len + 1] = NUL;
6203 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6204 /* remove the space later */
6205 did_add_space = TRUE;
6206 }
6207 else
6208 /* may remove added space */
6209 check_auto_format(FALSE);
6210 }
6211
6212 check_cursor();
6213}
6214
6215/*
6216 * When an extra space was added to continue a paragraph for auto-formatting,
6217 * delete it now. The space must be under the cursor, just after the insert
6218 * position.
6219 */
6220 static void
6221check_auto_format(end_insert)
6222 int end_insert; /* TRUE when ending Insert mode */
6223{
6224 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006225 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006226
6227 if (did_add_space)
6228 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006229 cc = gchar_cursor();
6230 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006231 /* Somehow the space was removed already. */
6232 did_add_space = FALSE;
6233 else
6234 {
6235 if (!end_insert)
6236 {
6237 inc_cursor();
6238 c = gchar_cursor();
6239 dec_cursor();
6240 }
6241 if (c != NUL)
6242 {
6243 /* The space is no longer at the end of the line, delete it. */
6244 del_char(FALSE);
6245 did_add_space = FALSE;
6246 }
6247 }
6248 }
6249}
6250
6251/*
6252 * Find out textwidth to be used for formatting:
6253 * if 'textwidth' option is set, use it
6254 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6255 * if invalid value, use 0.
6256 * Set default to window width (maximum 79) for "gq" operator.
6257 */
6258 int
6259comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006260 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006261{
6262 int textwidth;
6263
6264 textwidth = curbuf->b_p_tw;
6265 if (textwidth == 0 && curbuf->b_p_wm)
6266 {
6267 /* The width is the window width minus 'wrapmargin' minus all the
6268 * things that add to the margin. */
6269 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6270#ifdef FEAT_CMDWIN
6271 if (cmdwin_type != 0)
6272 textwidth -= 1;
6273#endif
6274#ifdef FEAT_FOLDING
6275 textwidth -= curwin->w_p_fdc;
6276#endif
6277#ifdef FEAT_SIGNS
6278 if (curwin->w_buffer->b_signlist != NULL
6279# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006280 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006281# endif
6282 )
6283 textwidth -= 1;
6284#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006285 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006286 textwidth -= 8;
6287 }
6288 if (textwidth < 0)
6289 textwidth = 0;
6290 if (ff && textwidth == 0)
6291 {
6292 textwidth = W_WIDTH(curwin) - 1;
6293 if (textwidth > 79)
6294 textwidth = 79;
6295 }
6296 return textwidth;
6297}
6298
6299/*
6300 * Put a character in the redo buffer, for when just after a CTRL-V.
6301 */
6302 static void
6303redo_literal(c)
6304 int c;
6305{
6306 char_u buf[10];
6307
6308 /* Only digits need special treatment. Translate them into a string of
6309 * three digits. */
6310 if (VIM_ISDIGIT(c))
6311 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006312 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006313 AppendToRedobuff(buf);
6314 }
6315 else
6316 AppendCharToRedobuff(c);
6317}
6318
6319/*
6320 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006321 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 */
6323 static void
6324start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006325 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006326{
6327 if (!arrow_used) /* something has been inserted */
6328 {
6329 AppendToRedobuff(ESC_STR);
6330 stop_insert(end_insert_pos, FALSE);
6331 arrow_used = TRUE; /* this means we stopped the current insert */
6332 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006333#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006334 check_spell_redraw();
6335#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006336}
6337
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006338#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006339/*
6340 * If we skipped highlighting word at cursor, do it now.
6341 * It may be skipped again, thus reset spell_redraw_lnum first.
6342 */
6343 static void
6344check_spell_redraw()
6345{
6346 if (spell_redraw_lnum != 0)
6347 {
6348 linenr_T lnum = spell_redraw_lnum;
6349
6350 spell_redraw_lnum = 0;
6351 redrawWinline(lnum, FALSE);
6352 }
6353}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006354
6355/*
6356 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6357 * spelled word, if there is one.
6358 */
6359 static void
6360spell_back_to_badword()
6361{
6362 pos_T tpos = curwin->w_cursor;
6363
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006364 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006365 if (curwin->w_cursor.col != tpos.col)
6366 start_arrow(&tpos);
6367}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006368#endif
6369
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370/*
6371 * stop_arrow() is called before a change is made in insert mode.
6372 * If an arrow key has been used, start a new insertion.
6373 * Returns FAIL if undo is impossible, shouldn't insert then.
6374 */
6375 int
6376stop_arrow()
6377{
6378 if (arrow_used)
6379 {
6380 if (u_save_cursor() == OK)
6381 {
6382 arrow_used = FALSE;
6383 ins_need_undo = FALSE;
6384 }
6385 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006386 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006387 ai_col = 0;
6388#ifdef FEAT_VREPLACE
6389 if (State & VREPLACE_FLAG)
6390 {
6391 orig_line_count = curbuf->b_ml.ml_line_count;
6392 vr_lines_changed = 1;
6393 }
6394#endif
6395 ResetRedobuff();
6396 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006397 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006398 }
6399 else if (ins_need_undo)
6400 {
6401 if (u_save_cursor() == OK)
6402 ins_need_undo = FALSE;
6403 }
6404
6405#ifdef FEAT_FOLDING
6406 /* Always open fold at the cursor line when inserting something. */
6407 foldOpenCursor();
6408#endif
6409
6410 return (arrow_used || ins_need_undo ? FAIL : OK);
6411}
6412
6413/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006414 * Do a few things to stop inserting.
6415 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6416 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006417 */
6418 static void
6419stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006420 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006421 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006422{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006423 int cc;
6424 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425
6426 stop_redo_ins();
6427 replace_flush(); /* abandon replace stack */
6428
6429 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006430 * Save the inserted text for later redo with ^@ and CTRL-A.
6431 * Don't do it when "restart_edit" was set and nothing was inserted,
6432 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006433 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006434 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006435 if (did_restart_edit == 0 || (ptr != NULL
6436 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006437 {
6438 vim_free(last_insert);
6439 last_insert = ptr;
6440 last_insert_skip = new_insert_skip;
6441 }
6442 else
6443 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006445 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006446 {
6447 /* Auto-format now. It may seem strange to do this when stopping an
6448 * insertion (or moving the cursor), but it's required when appending
6449 * a line and having it end in a space. But only do it when something
6450 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006451 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006453 pos_T tpos = curwin->w_cursor;
6454
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 /* When the cursor is at the end of the line after a space the
6456 * formatting will move it to the following word. Avoid that by
6457 * moving the cursor onto the space. */
6458 cc = 'x';
6459 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6460 {
6461 dec_cursor();
6462 cc = gchar_cursor();
6463 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006464 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006465 }
6466
6467 auto_format(TRUE, FALSE);
6468
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006469 if (vim_iswhite(cc))
6470 {
6471 if (gchar_cursor() != NUL)
6472 inc_cursor();
6473#ifdef FEAT_VIRTUALEDIT
6474 /* If the cursor is still at the same character, also keep
6475 * the "coladd". */
6476 if (gchar_cursor() == NUL
6477 && curwin->w_cursor.lnum == tpos.lnum
6478 && curwin->w_cursor.col == tpos.col)
6479 curwin->w_cursor.coladd = tpos.coladd;
6480#endif
6481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 }
6483
6484 /* If a space was inserted for auto-formatting, remove it now. */
6485 check_auto_format(TRUE);
6486
6487 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006488 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006489 * Do this when ESC was used or moving the cursor up/down.
6490 * Check for the old position still being valid, just in case the text
6491 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006492 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006493 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6494 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006495 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006496 pos_T tpos = curwin->w_cursor;
6497
6498 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006499 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006500 for (;;)
6501 {
6502 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6503 --curwin->w_cursor.col;
6504 cc = gchar_cursor();
6505 if (!vim_iswhite(cc))
6506 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006507 if (del_char(TRUE) == FAIL)
6508 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006509 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006510 if (curwin->w_cursor.lnum != tpos.lnum)
6511 curwin->w_cursor = tpos;
6512 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6514
6515#ifdef FEAT_VISUAL
6516 /* <C-S-Right> may have started Visual mode, adjust the position for
6517 * deleted characters. */
6518 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6519 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006520 int len = (int)STRLEN(ml_get_curline());
6521
6522 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006523 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006524 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006525# ifdef FEAT_VIRTUALEDIT
6526 VIsual.coladd = 0;
6527# endif
6528 }
6529 }
6530#endif
6531 }
6532 }
6533 did_ai = FALSE;
6534#ifdef FEAT_SMARTINDENT
6535 did_si = FALSE;
6536 can_si = FALSE;
6537 can_si_back = FALSE;
6538#endif
6539
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006540 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6541 * now in a different buffer. */
6542 if (end_insert_pos != NULL)
6543 {
6544 curbuf->b_op_start = Insstart;
6545 curbuf->b_op_end = *end_insert_pos;
6546 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006547}
6548
6549/*
6550 * Set the last inserted text to a single character.
6551 * Used for the replace command.
6552 */
6553 void
6554set_last_insert(c)
6555 int c;
6556{
6557 char_u *s;
6558
6559 vim_free(last_insert);
6560#ifdef FEAT_MBYTE
6561 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6562#else
6563 last_insert = alloc(6);
6564#endif
6565 if (last_insert != NULL)
6566 {
6567 s = last_insert;
6568 /* Use the CTRL-V only when entering a special char */
6569 if (c < ' ' || c == DEL)
6570 *s++ = Ctrl_V;
6571 s = add_char2buf(c, s);
6572 *s++ = ESC;
6573 *s++ = NUL;
6574 last_insert_skip = 0;
6575 }
6576}
6577
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006578#if defined(EXITFREE) || defined(PROTO)
6579 void
6580free_last_insert()
6581{
6582 vim_free(last_insert);
6583 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006584# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006585 vim_free(compl_orig_text);
6586 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006587# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006588}
6589#endif
6590
Bram Moolenaar071d4272004-06-13 20:20:40 +00006591/*
6592 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6593 * and CSI. Handle multi-byte characters.
6594 * Returns a pointer to after the added bytes.
6595 */
6596 char_u *
6597add_char2buf(c, s)
6598 int c;
6599 char_u *s;
6600{
6601#ifdef FEAT_MBYTE
6602 char_u temp[MB_MAXBYTES];
6603 int i;
6604 int len;
6605
6606 len = (*mb_char2bytes)(c, temp);
6607 for (i = 0; i < len; ++i)
6608 {
6609 c = temp[i];
6610#endif
6611 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6612 if (c == K_SPECIAL)
6613 {
6614 *s++ = K_SPECIAL;
6615 *s++ = KS_SPECIAL;
6616 *s++ = KE_FILLER;
6617 }
6618#ifdef FEAT_GUI
6619 else if (c == CSI)
6620 {
6621 *s++ = CSI;
6622 *s++ = KS_EXTRA;
6623 *s++ = (int)KE_CSI;
6624 }
6625#endif
6626 else
6627 *s++ = c;
6628#ifdef FEAT_MBYTE
6629 }
6630#endif
6631 return s;
6632}
6633
6634/*
6635 * move cursor to start of line
6636 * if flags & BL_WHITE move to first non-white
6637 * if flags & BL_SOL move to first non-white if startofline is set,
6638 * otherwise keep "curswant" column
6639 * if flags & BL_FIX don't leave the cursor on a NUL.
6640 */
6641 void
6642beginline(flags)
6643 int flags;
6644{
6645 if ((flags & BL_SOL) && !p_sol)
6646 coladvance(curwin->w_curswant);
6647 else
6648 {
6649 curwin->w_cursor.col = 0;
6650#ifdef FEAT_VIRTUALEDIT
6651 curwin->w_cursor.coladd = 0;
6652#endif
6653
6654 if (flags & (BL_WHITE | BL_SOL))
6655 {
6656 char_u *ptr;
6657
6658 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6659 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6660 ++curwin->w_cursor.col;
6661 }
6662 curwin->w_set_curswant = TRUE;
6663 }
6664}
6665
6666/*
6667 * oneright oneleft cursor_down cursor_up
6668 *
6669 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006670 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671 * Return OK when successful, FAIL when we hit a line of file boundary.
6672 */
6673
6674 int
6675oneright()
6676{
6677 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006678 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006679
6680#ifdef FEAT_VIRTUALEDIT
6681 if (virtual_active())
6682 {
6683 pos_T prevpos = curwin->w_cursor;
6684
6685 /* Adjust for multi-wide char (excluding TAB) */
6686 ptr = ml_get_cursor();
6687 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006688# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006690# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006691 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006692# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006693 ))
6694 ? ptr2cells(ptr) : 1));
6695 curwin->w_set_curswant = TRUE;
6696 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6697 return (prevpos.col != curwin->w_cursor.col
6698 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6699 }
6700#endif
6701
6702 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006703 if (*ptr == NUL)
6704 return FAIL; /* already at the very end */
6705
Bram Moolenaar071d4272004-06-13 20:20:40 +00006706#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006707 if (has_mbyte)
6708 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 else
6710#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006711 l = 1;
6712
6713 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6714 * contains "onemore". */
6715 if (ptr[l] == NUL
6716#ifdef FEAT_VIRTUALEDIT
6717 && (ve_flags & VE_ONEMORE) == 0
6718#endif
6719 )
6720 return FAIL;
6721 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006722
6723 curwin->w_set_curswant = TRUE;
6724 return OK;
6725}
6726
6727 int
6728oneleft()
6729{
6730#ifdef FEAT_VIRTUALEDIT
6731 if (virtual_active())
6732 {
6733 int width;
6734 int v = getviscol();
6735
6736 if (v == 0)
6737 return FAIL;
6738
6739# ifdef FEAT_LINEBREAK
6740 /* We might get stuck on 'showbreak', skip over it. */
6741 width = 1;
6742 for (;;)
6743 {
6744 coladvance(v - width);
6745 /* getviscol() is slow, skip it when 'showbreak' is empty and
6746 * there are no multi-byte characters */
6747 if ((*p_sbr == NUL
6748# ifdef FEAT_MBYTE
6749 && !has_mbyte
6750# endif
6751 ) || getviscol() < v)
6752 break;
6753 ++width;
6754 }
6755# else
6756 coladvance(v - 1);
6757# endif
6758
6759 if (curwin->w_cursor.coladd == 1)
6760 {
6761 char_u *ptr;
6762
6763 /* Adjust for multi-wide char (not a TAB) */
6764 ptr = ml_get_cursor();
6765 if (*ptr != TAB && vim_isprintc(
6766# ifdef FEAT_MBYTE
6767 (*mb_ptr2char)(ptr)
6768# else
6769 *ptr
6770# endif
6771 ) && ptr2cells(ptr) > 1)
6772 curwin->w_cursor.coladd = 0;
6773 }
6774
6775 curwin->w_set_curswant = TRUE;
6776 return OK;
6777 }
6778#endif
6779
6780 if (curwin->w_cursor.col == 0)
6781 return FAIL;
6782
6783 curwin->w_set_curswant = TRUE;
6784 --curwin->w_cursor.col;
6785
6786#ifdef FEAT_MBYTE
6787 /* if the character on the left of the current cursor is a multi-byte
6788 * character, move to its first byte */
6789 if (has_mbyte)
6790 mb_adjust_cursor();
6791#endif
6792 return OK;
6793}
6794
6795 int
6796cursor_up(n, upd_topline)
6797 long n;
6798 int upd_topline; /* When TRUE: update topline */
6799{
6800 linenr_T lnum;
6801
6802 if (n > 0)
6803 {
6804 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006805 /* This fails if the cursor is already in the first line or the count
6806 * is larger than the line number and '-' is in 'cpoptions' */
6807 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006808 return FAIL;
6809 if (n >= lnum)
6810 lnum = 1;
6811 else
6812#ifdef FEAT_FOLDING
6813 if (hasAnyFolding(curwin))
6814 {
6815 /*
6816 * Count each sequence of folded lines as one logical line.
6817 */
6818 /* go to the the start of the current fold */
6819 (void)hasFolding(lnum, &lnum, NULL);
6820
6821 while (n--)
6822 {
6823 /* move up one line */
6824 --lnum;
6825 if (lnum <= 1)
6826 break;
6827 /* If we entered a fold, move to the beginning, unless in
6828 * Insert mode or when 'foldopen' contains "all": it will open
6829 * in a moment. */
6830 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6831 (void)hasFolding(lnum, &lnum, NULL);
6832 }
6833 if (lnum < 1)
6834 lnum = 1;
6835 }
6836 else
6837#endif
6838 lnum -= n;
6839 curwin->w_cursor.lnum = lnum;
6840 }
6841
6842 /* try to advance to the column we want to be at */
6843 coladvance(curwin->w_curswant);
6844
6845 if (upd_topline)
6846 update_topline(); /* make sure curwin->w_topline is valid */
6847
6848 return OK;
6849}
6850
6851/*
6852 * Cursor down a number of logical lines.
6853 */
6854 int
6855cursor_down(n, upd_topline)
6856 long n;
6857 int upd_topline; /* When TRUE: update topline */
6858{
6859 linenr_T lnum;
6860
6861 if (n > 0)
6862 {
6863 lnum = curwin->w_cursor.lnum;
6864#ifdef FEAT_FOLDING
6865 /* Move to last line of fold, will fail if it's the end-of-file. */
6866 (void)hasFolding(lnum, NULL, &lnum);
6867#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006868 /* This fails if the cursor is already in the last line or would move
6869 * beyound the last line and '-' is in 'cpoptions' */
6870 if (lnum >= curbuf->b_ml.ml_line_count
6871 || (lnum + n > curbuf->b_ml.ml_line_count
6872 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006873 return FAIL;
6874 if (lnum + n >= curbuf->b_ml.ml_line_count)
6875 lnum = curbuf->b_ml.ml_line_count;
6876 else
6877#ifdef FEAT_FOLDING
6878 if (hasAnyFolding(curwin))
6879 {
6880 linenr_T last;
6881
6882 /* count each sequence of folded lines as one logical line */
6883 while (n--)
6884 {
6885 if (hasFolding(lnum, NULL, &last))
6886 lnum = last + 1;
6887 else
6888 ++lnum;
6889 if (lnum >= curbuf->b_ml.ml_line_count)
6890 break;
6891 }
6892 if (lnum > curbuf->b_ml.ml_line_count)
6893 lnum = curbuf->b_ml.ml_line_count;
6894 }
6895 else
6896#endif
6897 lnum += n;
6898 curwin->w_cursor.lnum = lnum;
6899 }
6900
6901 /* try to advance to the column we want to be at */
6902 coladvance(curwin->w_curswant);
6903
6904 if (upd_topline)
6905 update_topline(); /* make sure curwin->w_topline is valid */
6906
6907 return OK;
6908}
6909
6910/*
6911 * Stuff the last inserted text in the read buffer.
6912 * Last_insert actually is a copy of the redo buffer, so we
6913 * first have to remove the command.
6914 */
6915 int
6916stuff_inserted(c, count, no_esc)
6917 int c; /* Command character to be inserted */
6918 long count; /* Repeat this many times */
6919 int no_esc; /* Don't add an ESC at the end */
6920{
6921 char_u *esc_ptr;
6922 char_u *ptr;
6923 char_u *last_ptr;
6924 char_u last = NUL;
6925
6926 ptr = get_last_insert();
6927 if (ptr == NULL)
6928 {
6929 EMSG(_(e_noinstext));
6930 return FAIL;
6931 }
6932
6933 /* may want to stuff the command character, to start Insert mode */
6934 if (c != NUL)
6935 stuffcharReadbuff(c);
6936 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6937 *esc_ptr = NUL; /* remove the ESC */
6938
6939 /* when the last char is either "0" or "^" it will be quoted if no ESC
6940 * comes after it OR if it will inserted more than once and "ptr"
6941 * starts with ^D. -- Acevedo
6942 */
6943 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6944 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6945 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6946 {
6947 last = *last_ptr;
6948 *last_ptr = NUL;
6949 }
6950
6951 do
6952 {
6953 stuffReadbuff(ptr);
6954 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6955 if (last)
6956 stuffReadbuff((char_u *)(last == '0'
6957 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6958 : IF_EB("\026^", CTRL_V_STR "^")));
6959 }
6960 while (--count > 0);
6961
6962 if (last)
6963 *last_ptr = last;
6964
6965 if (esc_ptr != NULL)
6966 *esc_ptr = ESC; /* put the ESC back */
6967
6968 /* may want to stuff a trailing ESC, to get out of Insert mode */
6969 if (!no_esc)
6970 stuffcharReadbuff(ESC);
6971
6972 return OK;
6973}
6974
6975 char_u *
6976get_last_insert()
6977{
6978 if (last_insert == NULL)
6979 return NULL;
6980 return last_insert + last_insert_skip;
6981}
6982
6983/*
6984 * Get last inserted string, and remove trailing <Esc>.
6985 * Returns pointer to allocated memory (must be freed) or NULL.
6986 */
6987 char_u *
6988get_last_insert_save()
6989{
6990 char_u *s;
6991 int len;
6992
6993 if (last_insert == NULL)
6994 return NULL;
6995 s = vim_strsave(last_insert + last_insert_skip);
6996 if (s != NULL)
6997 {
6998 len = (int)STRLEN(s);
6999 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7000 s[len - 1] = NUL;
7001 }
7002 return s;
7003}
7004
7005/*
7006 * Check the word in front of the cursor for an abbreviation.
7007 * Called when the non-id character "c" has been entered.
7008 * When an abbreviation is recognized it is removed from the text and
7009 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7010 */
7011 static int
7012echeck_abbr(c)
7013 int c;
7014{
7015 /* Don't check for abbreviation in paste mode, when disabled and just
7016 * after moving around with cursor keys. */
7017 if (p_paste || no_abbr || arrow_used)
7018 return FALSE;
7019
7020 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7021 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7022}
7023
7024/*
7025 * replace-stack functions
7026 *
7027 * When replacing characters, the replaced characters are remembered for each
7028 * new character. This is used to re-insert the old text when backspacing.
7029 *
7030 * There is a NUL headed list of characters for each character that is
7031 * currently in the file after the insertion point. When BS is used, one NUL
7032 * headed list is put back for the deleted character.
7033 *
7034 * For a newline, there are two NUL headed lists. One contains the characters
7035 * that the NL replaced. The extra one stores the characters after the cursor
7036 * that were deleted (always white space).
7037 *
7038 * Replace_offset is normally 0, in which case replace_push will add a new
7039 * character at the end of the stack. If replace_offset is not 0, that many
7040 * characters will be left on the stack above the newly inserted character.
7041 */
7042
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007043static char_u *replace_stack = NULL;
7044static long replace_stack_nr = 0; /* next entry in replace stack */
7045static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007046
7047 void
7048replace_push(c)
7049 int c; /* character that is replaced (NUL is none) */
7050{
7051 char_u *p;
7052
7053 if (replace_stack_nr < replace_offset) /* nothing to do */
7054 return;
7055 if (replace_stack_len <= replace_stack_nr)
7056 {
7057 replace_stack_len += 50;
7058 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7059 if (p == NULL) /* out of memory */
7060 {
7061 replace_stack_len -= 50;
7062 return;
7063 }
7064 if (replace_stack != NULL)
7065 {
7066 mch_memmove(p, replace_stack,
7067 (size_t)(replace_stack_nr * sizeof(char_u)));
7068 vim_free(replace_stack);
7069 }
7070 replace_stack = p;
7071 }
7072 p = replace_stack + replace_stack_nr - replace_offset;
7073 if (replace_offset)
7074 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7075 *p = c;
7076 ++replace_stack_nr;
7077}
7078
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007079#if defined(FEAT_MBYTE) || defined(PROTO)
7080/*
7081 * Push a character onto the replace stack. Handles a multi-byte character in
7082 * reverse byte order, so that the first byte is popped off first.
7083 * Return the number of bytes done (includes composing characters).
7084 */
7085 int
7086replace_push_mb(p)
7087 char_u *p;
7088{
7089 int l = (*mb_ptr2len)(p);
7090 int j;
7091
7092 for (j = l - 1; j >= 0; --j)
7093 replace_push(p[j]);
7094 return l;
7095}
7096#endif
7097
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007098#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007099/*
7100 * call replace_push(c) with replace_offset set to the first NUL.
7101 */
7102 static void
7103replace_push_off(c)
7104 int c;
7105{
7106 char_u *p;
7107
7108 p = replace_stack + replace_stack_nr;
7109 for (replace_offset = 1; replace_offset < replace_stack_nr;
7110 ++replace_offset)
7111 if (*--p == NUL)
7112 break;
7113 replace_push(c);
7114 replace_offset = 0;
7115}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007116#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007117
7118/*
7119 * Pop one item from the replace stack.
7120 * return -1 if stack empty
7121 * return replaced character or NUL otherwise
7122 */
7123 static int
7124replace_pop()
7125{
7126 if (replace_stack_nr == 0)
7127 return -1;
7128 return (int)replace_stack[--replace_stack_nr];
7129}
7130
7131/*
7132 * Join the top two items on the replace stack. This removes to "off"'th NUL
7133 * encountered.
7134 */
7135 static void
7136replace_join(off)
7137 int off; /* offset for which NUL to remove */
7138{
7139 int i;
7140
7141 for (i = replace_stack_nr; --i >= 0; )
7142 if (replace_stack[i] == NUL && off-- <= 0)
7143 {
7144 --replace_stack_nr;
7145 mch_memmove(replace_stack + i, replace_stack + i + 1,
7146 (size_t)(replace_stack_nr - i));
7147 return;
7148 }
7149}
7150
7151/*
7152 * Pop bytes from the replace stack until a NUL is found, and insert them
7153 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7154 */
7155 static void
7156replace_pop_ins()
7157{
7158 int cc;
7159 int oldState = State;
7160
7161 State = NORMAL; /* don't want REPLACE here */
7162 while ((cc = replace_pop()) > 0)
7163 {
7164#ifdef FEAT_MBYTE
7165 mb_replace_pop_ins(cc);
7166#else
7167 ins_char(cc);
7168#endif
7169 dec_cursor();
7170 }
7171 State = oldState;
7172}
7173
7174#ifdef FEAT_MBYTE
7175/*
7176 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7177 * indicates a multi-byte char, pop the other bytes too.
7178 */
7179 static void
7180mb_replace_pop_ins(cc)
7181 int cc;
7182{
7183 int n;
7184 char_u buf[MB_MAXBYTES];
7185 int i;
7186 int c;
7187
7188 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7189 {
7190 buf[0] = cc;
7191 for (i = 1; i < n; ++i)
7192 buf[i] = replace_pop();
7193 ins_bytes_len(buf, n);
7194 }
7195 else
7196 ins_char(cc);
7197
7198 if (enc_utf8)
7199 /* Handle composing chars. */
7200 for (;;)
7201 {
7202 c = replace_pop();
7203 if (c == -1) /* stack empty */
7204 break;
7205 if ((n = MB_BYTE2LEN(c)) == 1)
7206 {
7207 /* Not a multi-byte char, put it back. */
7208 replace_push(c);
7209 break;
7210 }
7211 else
7212 {
7213 buf[0] = c;
7214 for (i = 1; i < n; ++i)
7215 buf[i] = replace_pop();
7216 if (utf_iscomposing(utf_ptr2char(buf)))
7217 ins_bytes_len(buf, n);
7218 else
7219 {
7220 /* Not a composing char, put it back. */
7221 for (i = n - 1; i >= 0; --i)
7222 replace_push(buf[i]);
7223 break;
7224 }
7225 }
7226 }
7227}
7228#endif
7229
7230/*
7231 * make the replace stack empty
7232 * (called when exiting replace mode)
7233 */
7234 static void
7235replace_flush()
7236{
7237 vim_free(replace_stack);
7238 replace_stack = NULL;
7239 replace_stack_len = 0;
7240 replace_stack_nr = 0;
7241}
7242
7243/*
7244 * Handle doing a BS for one character.
7245 * cc < 0: replace stack empty, just move cursor
7246 * cc == 0: character was inserted, delete it
7247 * cc > 0: character was replaced, put cc (first byte of original char) back
7248 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007249 * When "limit_col" is >= 0, don't delete before this column. Matters when
7250 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251 */
7252 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007253replace_do_bs(limit_col)
7254 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007255{
7256 int cc;
7257#ifdef FEAT_VREPLACE
7258 int orig_len = 0;
7259 int ins_len;
7260 int orig_vcols = 0;
7261 colnr_T start_vcol;
7262 char_u *p;
7263 int i;
7264 int vcol;
7265#endif
7266
7267 cc = replace_pop();
7268 if (cc > 0)
7269 {
7270#ifdef FEAT_VREPLACE
7271 if (State & VREPLACE_FLAG)
7272 {
7273 /* Get the number of screen cells used by the character we are
7274 * going to delete. */
7275 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7276 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7277 }
7278#endif
7279#ifdef FEAT_MBYTE
7280 if (has_mbyte)
7281 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007282 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007283# ifdef FEAT_VREPLACE
7284 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007285 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007286# endif
7287 replace_push(cc);
7288 }
7289 else
7290#endif
7291 {
7292 pchar_cursor(cc);
7293#ifdef FEAT_VREPLACE
7294 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007295 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007296#endif
7297 }
7298 replace_pop_ins();
7299
7300#ifdef FEAT_VREPLACE
7301 if (State & VREPLACE_FLAG)
7302 {
7303 /* Get the number of screen cells used by the inserted characters */
7304 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007305 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007306 vcol = start_vcol;
7307 for (i = 0; i < ins_len; ++i)
7308 {
7309 vcol += chartabsize(p + i, vcol);
7310#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007311 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312#endif
7313 }
7314 vcol -= start_vcol;
7315
7316 /* Delete spaces that were inserted after the cursor to keep the
7317 * text aligned. */
7318 curwin->w_cursor.col += ins_len;
7319 while (vcol > orig_vcols && gchar_cursor() == ' ')
7320 {
7321 del_char(FALSE);
7322 ++orig_vcols;
7323 }
7324 curwin->w_cursor.col -= ins_len;
7325 }
7326#endif
7327
7328 /* mark the buffer as changed and prepare for displaying */
7329 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7330 }
7331 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007332 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007333}
7334
7335#ifdef FEAT_CINDENT
7336/*
7337 * Return TRUE if C-indenting is on.
7338 */
7339 static int
7340cindent_on()
7341{
7342 return (!p_paste && (curbuf->b_p_cin
7343# ifdef FEAT_EVAL
7344 || *curbuf->b_p_inde != NUL
7345# endif
7346 ));
7347}
7348#endif
7349
7350#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7351/*
7352 * Re-indent the current line, based on the current contents of it and the
7353 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7354 * confused what all the part that handles Control-T is doing that I'm not.
7355 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7356 */
7357
7358 void
7359fixthisline(get_the_indent)
7360 int (*get_the_indent) __ARGS((void));
7361{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007362 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 if (linewhite(curwin->w_cursor.lnum))
7364 did_ai = TRUE; /* delete the indent if the line stays empty */
7365}
7366
7367 void
7368fix_indent()
7369{
7370 if (p_paste)
7371 return;
7372# ifdef FEAT_LISP
7373 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7374 fixthisline(get_lisp_indent);
7375# endif
7376# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7377 else
7378# endif
7379# ifdef FEAT_CINDENT
7380 if (cindent_on())
7381 do_c_expr_indent();
7382# endif
7383}
7384
7385#endif
7386
7387#ifdef FEAT_CINDENT
7388/*
7389 * return TRUE if 'cinkeys' contains the key "keytyped",
7390 * when == '*': Only if key is preceded with '*' (indent before insert)
7391 * when == '!': Only if key is prededed with '!' (don't insert)
7392 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7393 *
7394 * "keytyped" can have a few special values:
7395 * KEY_OPEN_FORW
7396 * KEY_OPEN_BACK
7397 * KEY_COMPLETE just finished completion.
7398 *
7399 * If line_is_empty is TRUE accept keys with '0' before them.
7400 */
7401 int
7402in_cinkeys(keytyped, when, line_is_empty)
7403 int keytyped;
7404 int when;
7405 int line_is_empty;
7406{
7407 char_u *look;
7408 int try_match;
7409 int try_match_word;
7410 char_u *p;
7411 char_u *line;
7412 int icase;
7413 int i;
7414
Bram Moolenaar30848942009-12-24 14:46:12 +00007415 if (keytyped == NUL)
7416 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7417 return FALSE;
7418
Bram Moolenaar071d4272004-06-13 20:20:40 +00007419#ifdef FEAT_EVAL
7420 if (*curbuf->b_p_inde != NUL)
7421 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7422 else
7423#endif
7424 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7425 while (*look)
7426 {
7427 /*
7428 * Find out if we want to try a match with this key, depending on
7429 * 'when' and a '*' or '!' before the key.
7430 */
7431 switch (when)
7432 {
7433 case '*': try_match = (*look == '*'); break;
7434 case '!': try_match = (*look == '!'); break;
7435 default: try_match = (*look != '*'); break;
7436 }
7437 if (*look == '*' || *look == '!')
7438 ++look;
7439
7440 /*
7441 * If there is a '0', only accept a match if the line is empty.
7442 * But may still match when typing last char of a word.
7443 */
7444 if (*look == '0')
7445 {
7446 try_match_word = try_match;
7447 if (!line_is_empty)
7448 try_match = FALSE;
7449 ++look;
7450 }
7451 else
7452 try_match_word = FALSE;
7453
7454 /*
7455 * does it look like a control character?
7456 */
7457 if (*look == '^'
7458#ifdef EBCDIC
7459 && (Ctrl_chr(look[1]) != 0)
7460#else
7461 && look[1] >= '?' && look[1] <= '_'
7462#endif
7463 )
7464 {
7465 if (try_match && keytyped == Ctrl_chr(look[1]))
7466 return TRUE;
7467 look += 2;
7468 }
7469 /*
7470 * 'o' means "o" command, open forward.
7471 * 'O' means "O" command, open backward.
7472 */
7473 else if (*look == 'o')
7474 {
7475 if (try_match && keytyped == KEY_OPEN_FORW)
7476 return TRUE;
7477 ++look;
7478 }
7479 else if (*look == 'O')
7480 {
7481 if (try_match && keytyped == KEY_OPEN_BACK)
7482 return TRUE;
7483 ++look;
7484 }
7485
7486 /*
7487 * 'e' means to check for "else" at start of line and just before the
7488 * cursor.
7489 */
7490 else if (*look == 'e')
7491 {
7492 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7493 {
7494 p = ml_get_curline();
7495 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7496 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7497 return TRUE;
7498 }
7499 ++look;
7500 }
7501
7502 /*
7503 * ':' only causes an indent if it is at the end of a label or case
7504 * statement, or when it was before typing the ':' (to fix
7505 * class::method for C++).
7506 */
7507 else if (*look == ':')
7508 {
7509 if (try_match && keytyped == ':')
7510 {
7511 p = ml_get_curline();
7512 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7513 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007514 /* Need to get the line again after cin_islabel(). */
7515 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007516 if (curwin->w_cursor.col > 2
7517 && p[curwin->w_cursor.col - 1] == ':'
7518 && p[curwin->w_cursor.col - 2] == ':')
7519 {
7520 p[curwin->w_cursor.col - 1] = ' ';
7521 i = (cin_iscase(p) || cin_isscopedecl(p)
7522 || cin_islabel(30));
7523 p = ml_get_curline();
7524 p[curwin->w_cursor.col - 1] = ':';
7525 if (i)
7526 return TRUE;
7527 }
7528 }
7529 ++look;
7530 }
7531
7532
7533 /*
7534 * Is it a key in <>, maybe?
7535 */
7536 else if (*look == '<')
7537 {
7538 if (try_match)
7539 {
7540 /*
7541 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7542 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7543 * >, *, : and ! keys if they really really want to.
7544 */
7545 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7546 && keytyped == look[1])
7547 return TRUE;
7548
7549 if (keytyped == get_special_key_code(look + 1))
7550 return TRUE;
7551 }
7552 while (*look && *look != '>')
7553 look++;
7554 while (*look == '>')
7555 look++;
7556 }
7557
7558 /*
7559 * Is it a word: "=word"?
7560 */
7561 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7562 {
7563 ++look;
7564 if (*look == '~')
7565 {
7566 icase = TRUE;
7567 ++look;
7568 }
7569 else
7570 icase = FALSE;
7571 p = vim_strchr(look, ',');
7572 if (p == NULL)
7573 p = look + STRLEN(look);
7574 if ((try_match || try_match_word)
7575 && curwin->w_cursor.col >= (colnr_T)(p - look))
7576 {
7577 int match = FALSE;
7578
7579#ifdef FEAT_INS_EXPAND
7580 if (keytyped == KEY_COMPLETE)
7581 {
7582 char_u *s;
7583
7584 /* Just completed a word, check if it starts with "look".
7585 * search back for the start of a word. */
7586 line = ml_get_curline();
7587# ifdef FEAT_MBYTE
7588 if (has_mbyte)
7589 {
7590 char_u *n;
7591
7592 for (s = line + curwin->w_cursor.col; s > line; s = n)
7593 {
7594 n = mb_prevptr(line, s);
7595 if (!vim_iswordp(n))
7596 break;
7597 }
7598 }
7599 else
7600# endif
7601 for (s = line + curwin->w_cursor.col; s > line; --s)
7602 if (!vim_iswordc(s[-1]))
7603 break;
7604 if (s + (p - look) <= line + curwin->w_cursor.col
7605 && (icase
7606 ? MB_STRNICMP(s, look, p - look)
7607 : STRNCMP(s, look, p - look)) == 0)
7608 match = TRUE;
7609 }
7610 else
7611#endif
7612 /* TODO: multi-byte */
7613 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7614 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7615 {
7616 line = ml_get_cursor();
7617 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7618 || !vim_iswordc(line[-(p - look) - 1]))
7619 && (icase
7620 ? MB_STRNICMP(line - (p - look), look, p - look)
7621 : STRNCMP(line - (p - look), look, p - look))
7622 == 0)
7623 match = TRUE;
7624 }
7625 if (match && try_match_word && !try_match)
7626 {
7627 /* "0=word": Check if there are only blanks before the
7628 * word. */
7629 line = ml_get_curline();
7630 if ((int)(skipwhite(line) - line) !=
7631 (int)(curwin->w_cursor.col - (p - look)))
7632 match = FALSE;
7633 }
7634 if (match)
7635 return TRUE;
7636 }
7637 look = p;
7638 }
7639
7640 /*
7641 * ok, it's a boring generic character.
7642 */
7643 else
7644 {
7645 if (try_match && *look == keytyped)
7646 return TRUE;
7647 ++look;
7648 }
7649
7650 /*
7651 * Skip over ", ".
7652 */
7653 look = skip_to_option_part(look);
7654 }
7655 return FALSE;
7656}
7657#endif /* FEAT_CINDENT */
7658
7659#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7660/*
7661 * Map Hebrew keyboard when in hkmap mode.
7662 */
7663 int
7664hkmap(c)
7665 int c;
7666{
7667 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7668 {
7669 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7670 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7671 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7672 static char_u map[26] =
7673 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7674 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7675 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7676 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7677 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7678 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7679 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7680 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7681 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7682
7683 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7684 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7685 /* '-1'='sofit' */
7686 else if (c == 'x')
7687 return 'X';
7688 else if (c == 'q')
7689 return '\''; /* {geresh}={'} */
7690 else if (c == 246)
7691 return ' '; /* \"o --> ' ' for a german keyboard */
7692 else if (c == 228)
7693 return ' '; /* \"a --> ' ' -- / -- */
7694 else if (c == 252)
7695 return ' '; /* \"u --> ' ' -- / -- */
7696#ifdef EBCDIC
7697 else if (islower(c))
7698#else
7699 /* NOTE: islower() does not do the right thing for us on Linux so we
7700 * do this the same was as 5.7 and previous, so it works correctly on
7701 * all systems. Specifically, the e.g. Delete and Arrow keys are
7702 * munged and won't work if e.g. searching for Hebrew text.
7703 */
7704 else if (c >= 'a' && c <= 'z')
7705#endif
7706 return (int)(map[CharOrdLow(c)] + p_aleph);
7707 else
7708 return c;
7709 }
7710 else
7711 {
7712 switch (c)
7713 {
7714 case '`': return ';';
7715 case '/': return '.';
7716 case '\'': return ',';
7717 case 'q': return '/';
7718 case 'w': return '\'';
7719
7720 /* Hebrew letters - set offset from 'a' */
7721 case ',': c = '{'; break;
7722 case '.': c = 'v'; break;
7723 case ';': c = 't'; break;
7724 default: {
7725 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7726
7727#ifdef EBCDIC
7728 /* see note about islower() above */
7729 if (!islower(c))
7730#else
7731 if (c < 'a' || c > 'z')
7732#endif
7733 return c;
7734 c = str[CharOrdLow(c)];
7735 break;
7736 }
7737 }
7738
7739 return (int)(CharOrdLow(c) + p_aleph);
7740 }
7741}
7742#endif
7743
7744 static void
7745ins_reg()
7746{
7747 int need_redraw = FALSE;
7748 int regname;
7749 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007750#ifdef FEAT_VISUAL
7751 int vis_active = VIsual_active;
7752#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007753
7754 /*
7755 * If we are going to wait for a character, show a '"'.
7756 */
7757 pc_status = PC_STATUS_UNSET;
7758 if (redrawing() && !char_avail())
7759 {
7760 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007761 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007762
7763 edit_putchar('"', TRUE);
7764#ifdef FEAT_CMDL_INFO
7765 add_to_showcmd_c(Ctrl_R);
7766#endif
7767 }
7768
7769#ifdef USE_ON_FLY_SCROLL
7770 dont_scroll = TRUE; /* disallow scrolling here */
7771#endif
7772
7773 /*
7774 * Don't map the register name. This also prevents the mode message to be
7775 * deleted when ESC is hit.
7776 */
7777 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007778 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007779 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007780 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7781 {
7782 /* Get a third key for literal register insertion */
7783 literally = regname;
7784#ifdef FEAT_CMDL_INFO
7785 add_to_showcmd_c(literally);
7786#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007787 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007788 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789 }
7790 --no_mapping;
7791
7792#ifdef FEAT_EVAL
7793 /*
7794 * Don't call u_sync() while getting the expression,
7795 * evaluating it or giving an error message for it!
7796 */
7797 ++no_u_sync;
7798 if (regname == '=')
7799 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007800# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007802# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007803 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007804# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 /* Restore the Input Method. */
7806 if (im_on)
7807 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007808# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007810 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7811 {
7812 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007813 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 else
7816 {
7817#endif
7818 if (literally == Ctrl_O || literally == Ctrl_P)
7819 {
7820 /* Append the command to the redo buffer. */
7821 AppendCharToRedobuff(Ctrl_R);
7822 AppendCharToRedobuff(literally);
7823 AppendCharToRedobuff(regname);
7824
7825 do_put(regname, BACKWARD, 1L,
7826 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7827 }
7828 else if (insert_reg(regname, literally) == FAIL)
7829 {
7830 vim_beep();
7831 need_redraw = TRUE; /* remove the '"' */
7832 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007833 else if (stop_insert_mode)
7834 /* When the '=' register was used and a function was invoked that
7835 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7836 * insert anything, need to remove the '"' */
7837 need_redraw = TRUE;
7838
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839#ifdef FEAT_EVAL
7840 }
7841 --no_u_sync;
7842#endif
7843#ifdef FEAT_CMDL_INFO
7844 clear_showcmd();
7845#endif
7846
7847 /* If the inserted register is empty, we need to remove the '"' */
7848 if (need_redraw || stuff_empty())
7849 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007850
7851#ifdef FEAT_VISUAL
7852 /* Disallow starting Visual mode here, would get a weird mode. */
7853 if (!vis_active && VIsual_active)
7854 end_visual_mode();
7855#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007856}
7857
7858/*
7859 * CTRL-G commands in Insert mode.
7860 */
7861 static void
7862ins_ctrl_g()
7863{
7864 int c;
7865
7866#ifdef FEAT_INS_EXPAND
7867 /* Right after CTRL-X the cursor will be after the ruler. */
7868 setcursor();
7869#endif
7870
7871 /*
7872 * Don't map the second key. This also prevents the mode message to be
7873 * deleted when ESC is hit.
7874 */
7875 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007876 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007877 --no_mapping;
7878 switch (c)
7879 {
7880 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7881 case K_UP:
7882 case Ctrl_K:
7883 case 'k': ins_up(TRUE);
7884 break;
7885
7886 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7887 case K_DOWN:
7888 case Ctrl_J:
7889 case 'j': ins_down(TRUE);
7890 break;
7891
7892 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007893 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007894 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007895
7896 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007897 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007898 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899 break;
7900
7901 /* Unknown CTRL-G command, reserved for future expansion. */
7902 default: vim_beep();
7903 }
7904}
7905
7906/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007907 * CTRL-^ in Insert mode.
7908 */
7909 static void
7910ins_ctrl_hat()
7911{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007912 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007913 {
7914 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7915 if (State & LANGMAP)
7916 {
7917 curbuf->b_p_iminsert = B_IMODE_NONE;
7918 State &= ~LANGMAP;
7919 }
7920 else
7921 {
7922 curbuf->b_p_iminsert = B_IMODE_LMAP;
7923 State |= LANGMAP;
7924#ifdef USE_IM_CONTROL
7925 im_set_active(FALSE);
7926#endif
7927 }
7928 }
7929#ifdef USE_IM_CONTROL
7930 else
7931 {
7932 /* There are no ":lmap" mappings, toggle IM */
7933 if (im_get_status())
7934 {
7935 curbuf->b_p_iminsert = B_IMODE_NONE;
7936 im_set_active(FALSE);
7937 }
7938 else
7939 {
7940 curbuf->b_p_iminsert = B_IMODE_IM;
7941 State &= ~LANGMAP;
7942 im_set_active(TRUE);
7943 }
7944 }
7945#endif
7946 set_iminsert_global();
7947 showmode();
7948#ifdef FEAT_GUI
7949 /* may show different cursor shape or color */
7950 if (gui.in_use)
7951 gui_update_cursor(TRUE, FALSE);
7952#endif
7953#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7954 /* Show/unshow value of 'keymap' in status lines. */
7955 status_redraw_curbuf();
7956#endif
7957}
7958
7959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 * Handle ESC in insert mode.
7961 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7962 * insert.
7963 */
7964 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007965ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007966 long *count;
7967 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007968 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007969{
7970 int temp;
7971 static int disabled_redraw = FALSE;
7972
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007973#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007974 check_spell_redraw();
7975#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976#if defined(FEAT_HANGULIN)
7977# if defined(ESC_CHG_TO_ENG_MODE)
7978 hangul_input_state_set(0);
7979# endif
7980 if (composing_hangul)
7981 {
7982 push_raw_key(composing_hangul_buffer, 2);
7983 composing_hangul = 0;
7984 }
7985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986
7987 temp = curwin->w_cursor.col;
7988 if (disabled_redraw)
7989 {
7990 --RedrawingDisabled;
7991 disabled_redraw = FALSE;
7992 }
7993 if (!arrow_used)
7994 {
7995 /*
7996 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007997 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7998 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 */
8000 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008001 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002
8003 /*
8004 * Repeating insert may take a long time. Check for
8005 * interrupt now and then.
8006 */
8007 if (*count > 0)
8008 {
8009 line_breakcheck();
8010 if (got_int)
8011 *count = 0;
8012 }
8013
8014 if (--*count > 0) /* repeat what was typed */
8015 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008016 /* Vi repeats the insert without replacing characters. */
8017 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8018 State &= ~REPLACE_FLAG;
8019
Bram Moolenaar071d4272004-06-13 20:20:40 +00008020 (void)start_redo_ins();
8021 if (cmdchar == 'r' || cmdchar == 'v')
8022 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8023 ++RedrawingDisabled;
8024 disabled_redraw = TRUE;
8025 return FALSE; /* repeat the insert */
8026 }
8027 stop_insert(&curwin->w_cursor, TRUE);
8028 undisplay_dollar();
8029 }
8030
8031 /* When an autoindent was removed, curswant stays after the
8032 * indent */
8033 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8034 curwin->w_set_curswant = TRUE;
8035
8036 /* Remember the last Insert position in the '^ mark. */
8037 if (!cmdmod.keepjumps)
8038 curbuf->b_last_insert = curwin->w_cursor;
8039
8040 /*
8041 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008042 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008043 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008044 if (!nomove
8045 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008046#ifdef FEAT_VIRTUALEDIT
8047 || curwin->w_cursor.coladd > 0
8048#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008049 )
8050 && (restart_edit == NUL
8051 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008053 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008054#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008055 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056#ifdef FEAT_RIGHTLEFT
8057 && !revins_on
8058#endif
8059 )
8060 {
8061#ifdef FEAT_VIRTUALEDIT
8062 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8063 {
8064 oneleft();
8065 if (restart_edit != NUL)
8066 ++curwin->w_cursor.coladd;
8067 }
8068 else
8069#endif
8070 {
8071 --curwin->w_cursor.col;
8072#ifdef FEAT_MBYTE
8073 /* Correct cursor for multi-byte character. */
8074 if (has_mbyte)
8075 mb_adjust_cursor();
8076#endif
8077 }
8078 }
8079
8080#ifdef USE_IM_CONTROL
8081 /* Disable IM to allow typing English directly for Normal mode commands.
8082 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8083 * well). */
8084 if (!(State & LANGMAP))
8085 im_save_status(&curbuf->b_p_iminsert);
8086 im_set_active(FALSE);
8087#endif
8088
8089 State = NORMAL;
8090 /* need to position cursor again (e.g. when on a TAB ) */
8091 changed_cline_bef_curs();
8092
8093#ifdef FEAT_MOUSE
8094 setmouse();
8095#endif
8096#ifdef CURSOR_SHAPE
8097 ui_cursor_shape(); /* may show different cursor shape */
8098#endif
8099
8100 /*
8101 * When recording or for CTRL-O, need to display the new mode.
8102 * Otherwise remove the mode message.
8103 */
8104 if (Recording || restart_edit != NUL)
8105 showmode();
8106 else if (p_smd)
8107 MSG("");
8108
8109 return TRUE; /* exit Insert mode */
8110}
8111
8112#ifdef FEAT_RIGHTLEFT
8113/*
8114 * Toggle language: hkmap and revins_on.
8115 * Move to end of reverse inserted text.
8116 */
8117 static void
8118ins_ctrl_()
8119{
8120 if (revins_on && revins_chars && revins_scol >= 0)
8121 {
8122 while (gchar_cursor() != NUL && revins_chars--)
8123 ++curwin->w_cursor.col;
8124 }
8125 p_ri = !p_ri;
8126 revins_on = (State == INSERT && p_ri);
8127 if (revins_on)
8128 {
8129 revins_scol = curwin->w_cursor.col;
8130 revins_legal++;
8131 revins_chars = 0;
8132 undisplay_dollar();
8133 }
8134 else
8135 revins_scol = -1;
8136#ifdef FEAT_FKMAP
8137 if (p_altkeymap)
8138 {
8139 /*
8140 * to be consistent also for redo command, using '.'
8141 * set arrow_used to true and stop it - causing to redo
8142 * characters entered in one mode (normal/reverse insert).
8143 */
8144 arrow_used = TRUE;
8145 (void)stop_arrow();
8146 p_fkmap = curwin->w_p_rl ^ p_ri;
8147 if (p_fkmap && p_ri)
8148 State = INSERT;
8149 }
8150 else
8151#endif
8152 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8153 showmode();
8154}
8155#endif
8156
8157#ifdef FEAT_VISUAL
8158/*
8159 * If 'keymodel' contains "startsel", may start selection.
8160 * Returns TRUE when a CTRL-O and other keys stuffed.
8161 */
8162 static int
8163ins_start_select(c)
8164 int c;
8165{
8166 if (km_startsel)
8167 switch (c)
8168 {
8169 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008170 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 case K_PAGEUP:
8172 case K_KPAGEUP:
8173 case K_PAGEDOWN:
8174 case K_KPAGEDOWN:
8175# ifdef MACOS
8176 case K_LEFT:
8177 case K_RIGHT:
8178 case K_UP:
8179 case K_DOWN:
8180 case K_END:
8181 case K_HOME:
8182# endif
8183 if (!(mod_mask & MOD_MASK_SHIFT))
8184 break;
8185 /* FALLTHROUGH */
8186 case K_S_LEFT:
8187 case K_S_RIGHT:
8188 case K_S_UP:
8189 case K_S_DOWN:
8190 case K_S_END:
8191 case K_S_HOME:
8192 /* Start selection right away, the cursor can move with
8193 * CTRL-O when beyond the end of the line. */
8194 start_selection();
8195
8196 /* Execute the key in (insert) Select mode. */
8197 stuffcharReadbuff(Ctrl_O);
8198 if (mod_mask)
8199 {
8200 char_u buf[4];
8201
8202 buf[0] = K_SPECIAL;
8203 buf[1] = KS_MODIFIER;
8204 buf[2] = mod_mask;
8205 buf[3] = NUL;
8206 stuffReadbuff(buf);
8207 }
8208 stuffcharReadbuff(c);
8209 return TRUE;
8210 }
8211 return FALSE;
8212}
8213#endif
8214
8215/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008216 * <Insert> key in Insert mode: toggle insert/remplace mode.
8217 */
8218 static void
8219ins_insert(replaceState)
8220 int replaceState;
8221{
8222#ifdef FEAT_FKMAP
8223 if (p_fkmap && p_ri)
8224 {
8225 beep_flush();
8226 EMSG(farsi_text_3); /* encoded in Farsi */
8227 return;
8228 }
8229#endif
8230
8231#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008232# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008233 set_vim_var_string(VV_INSERTMODE,
8234 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008235# ifdef FEAT_VREPLACE
8236 replaceState == VREPLACE ? "v" :
8237# endif
8238 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008239# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008240 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8241#endif
8242 if (State & REPLACE_FLAG)
8243 State = INSERT | (State & LANGMAP);
8244 else
8245 State = replaceState | (State & LANGMAP);
8246 AppendCharToRedobuff(K_INS);
8247 showmode();
8248#ifdef CURSOR_SHAPE
8249 ui_cursor_shape(); /* may show different cursor shape */
8250#endif
8251}
8252
8253/*
8254 * Pressed CTRL-O in Insert mode.
8255 */
8256 static void
8257ins_ctrl_o()
8258{
8259#ifdef FEAT_VREPLACE
8260 if (State & VREPLACE_FLAG)
8261 restart_edit = 'V';
8262 else
8263#endif
8264 if (State & REPLACE_FLAG)
8265 restart_edit = 'R';
8266 else
8267 restart_edit = 'I';
8268#ifdef FEAT_VIRTUALEDIT
8269 if (virtual_active())
8270 ins_at_eol = FALSE; /* cursor always keeps its column */
8271 else
8272#endif
8273 ins_at_eol = (gchar_cursor() == NUL);
8274}
8275
8276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 * If the cursor is on an indent, ^T/^D insert/delete one
8278 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008279 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 * with vi. But vi only supports ^T and ^D after an
8281 * autoindent, we support it everywhere.
8282 */
8283 static void
8284ins_shift(c, lastc)
8285 int c;
8286 int lastc;
8287{
8288 if (stop_arrow() == FAIL)
8289 return;
8290 AppendCharToRedobuff(c);
8291
8292 /*
8293 * 0^D and ^^D: remove all indent.
8294 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008295 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8296 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {
8298 --curwin->w_cursor.col;
8299 (void)del_char(FALSE); /* delete the '^' or '0' */
8300 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8301 if (State & REPLACE_FLAG)
8302 replace_pop_ins();
8303 if (lastc == '^')
8304 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008305 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008306 }
8307 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008308 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309
8310 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8311 did_ai = FALSE;
8312#ifdef FEAT_SMARTINDENT
8313 did_si = FALSE;
8314 can_si = FALSE;
8315 can_si_back = FALSE;
8316#endif
8317#ifdef FEAT_CINDENT
8318 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8319#endif
8320}
8321
8322 static void
8323ins_del()
8324{
8325 int temp;
8326
8327 if (stop_arrow() == FAIL)
8328 return;
8329 if (gchar_cursor() == NUL) /* delete newline */
8330 {
8331 temp = curwin->w_cursor.col;
8332 if (!can_bs(BS_EOL) /* only if "eol" included */
8333 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8334 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8335 || do_join(FALSE) == FAIL)
8336 vim_beep();
8337 else
8338 curwin->w_cursor.col = temp;
8339 }
8340 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8341 vim_beep();
8342 did_ai = FALSE;
8343#ifdef FEAT_SMARTINDENT
8344 did_si = FALSE;
8345 can_si = FALSE;
8346 can_si_back = FALSE;
8347#endif
8348 AppendCharToRedobuff(K_DEL);
8349}
8350
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008351static void ins_bs_one __ARGS((colnr_T *vcolp));
8352
8353/*
8354 * Delete one character for ins_bs().
8355 */
8356 static void
8357ins_bs_one(vcolp)
8358 colnr_T *vcolp;
8359{
8360 dec_cursor();
8361 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8362 if (State & REPLACE_FLAG)
8363 {
8364 /* Don't delete characters before the insert point when in
8365 * Replace mode */
8366 if (curwin->w_cursor.lnum != Insstart.lnum
8367 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008368 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008369 }
8370 else
8371 (void)del_char(FALSE);
8372}
8373
Bram Moolenaar071d4272004-06-13 20:20:40 +00008374/*
8375 * Handle Backspace, delete-word and delete-line in Insert mode.
8376 * Return TRUE when backspace was actually used.
8377 */
8378 static int
8379ins_bs(c, mode, inserted_space_p)
8380 int c;
8381 int mode;
8382 int *inserted_space_p;
8383{
8384 linenr_T lnum;
8385 int cc;
8386 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008387 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 colnr_T mincol;
8389 int did_backspace = FALSE;
8390 int in_indent;
8391 int oldState;
8392#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008393 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394#endif
8395
8396 /*
8397 * can't delete anything in an empty file
8398 * can't backup past first character in buffer
8399 * can't backup past starting point unless 'backspace' > 1
8400 * can backup to a previous line if 'backspace' == 0
8401 */
8402 if ( bufempty()
8403 || (
8404#ifdef FEAT_RIGHTLEFT
8405 !revins_on &&
8406#endif
8407 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8408 || (!can_bs(BS_START)
8409 && (arrow_used
8410 || (curwin->w_cursor.lnum == Insstart.lnum
8411 && curwin->w_cursor.col <= Insstart.col)))
8412 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8413 && curwin->w_cursor.col <= ai_col)
8414 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8415 {
8416 vim_beep();
8417 return FALSE;
8418 }
8419
8420 if (stop_arrow() == FAIL)
8421 return FALSE;
8422 in_indent = inindent(0);
8423#ifdef FEAT_CINDENT
8424 if (in_indent)
8425 can_cindent = FALSE;
8426#endif
8427#ifdef FEAT_COMMENTS
8428 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8429#endif
8430#ifdef FEAT_RIGHTLEFT
8431 if (revins_on) /* put cursor after last inserted char */
8432 inc_cursor();
8433#endif
8434
8435#ifdef FEAT_VIRTUALEDIT
8436 /* Virtualedit:
8437 * BACKSPACE_CHAR eats a virtual space
8438 * BACKSPACE_WORD eats all coladd
8439 * BACKSPACE_LINE eats all coladd and keeps going
8440 */
8441 if (curwin->w_cursor.coladd > 0)
8442 {
8443 if (mode == BACKSPACE_CHAR)
8444 {
8445 --curwin->w_cursor.coladd;
8446 return TRUE;
8447 }
8448 if (mode == BACKSPACE_WORD)
8449 {
8450 curwin->w_cursor.coladd = 0;
8451 return TRUE;
8452 }
8453 curwin->w_cursor.coladd = 0;
8454 }
8455#endif
8456
8457 /*
8458 * delete newline!
8459 */
8460 if (curwin->w_cursor.col == 0)
8461 {
8462 lnum = Insstart.lnum;
8463 if (curwin->w_cursor.lnum == Insstart.lnum
8464#ifdef FEAT_RIGHTLEFT
8465 || revins_on
8466#endif
8467 )
8468 {
8469 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8470 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8471 return FALSE;
8472 --Insstart.lnum;
8473 Insstart.col = MAXCOL;
8474 }
8475 /*
8476 * In replace mode:
8477 * cc < 0: NL was inserted, delete it
8478 * cc >= 0: NL was replaced, put original characters back
8479 */
8480 cc = -1;
8481 if (State & REPLACE_FLAG)
8482 cc = replace_pop(); /* returns -1 if NL was inserted */
8483 /*
8484 * In replace mode, in the line we started replacing, we only move the
8485 * cursor.
8486 */
8487 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8488 {
8489 dec_cursor();
8490 }
8491 else
8492 {
8493#ifdef FEAT_VREPLACE
8494 if (!(State & VREPLACE_FLAG)
8495 || curwin->w_cursor.lnum > orig_line_count)
8496#endif
8497 {
8498 temp = gchar_cursor(); /* remember current char */
8499 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008500
8501 /* When "aw" is in 'formatoptions' we must delete the space at
8502 * the end of the line, otherwise the line will be broken
8503 * again when auto-formatting. */
8504 if (has_format_option(FO_AUTO)
8505 && has_format_option(FO_WHITE_PAR))
8506 {
8507 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8508 TRUE);
8509 int len;
8510
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008511 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008512 if (len > 0 && ptr[len - 1] == ' ')
8513 ptr[len - 1] = NUL;
8514 }
8515
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 (void)do_join(FALSE);
8517 if (temp == NUL && gchar_cursor() != NUL)
8518 inc_cursor();
8519 }
8520#ifdef FEAT_VREPLACE
8521 else
8522 dec_cursor();
8523#endif
8524
8525 /*
8526 * In REPLACE mode we have to put back the text that was replaced
8527 * by the NL. On the replace stack is first a NUL-terminated
8528 * sequence of characters that were deleted and then the
8529 * characters that NL replaced.
8530 */
8531 if (State & REPLACE_FLAG)
8532 {
8533 /*
8534 * Do the next ins_char() in NORMAL state, to
8535 * prevent ins_char() from replacing characters and
8536 * avoiding showmatch().
8537 */
8538 oldState = State;
8539 State = NORMAL;
8540 /*
8541 * restore characters (blanks) deleted after cursor
8542 */
8543 while (cc > 0)
8544 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008545 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546#ifdef FEAT_MBYTE
8547 mb_replace_pop_ins(cc);
8548#else
8549 ins_char(cc);
8550#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008551 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 cc = replace_pop();
8553 }
8554 /* restore the characters that NL replaced */
8555 replace_pop_ins();
8556 State = oldState;
8557 }
8558 }
8559 did_ai = FALSE;
8560 }
8561 else
8562 {
8563 /*
8564 * Delete character(s) before the cursor.
8565 */
8566#ifdef FEAT_RIGHTLEFT
8567 if (revins_on) /* put cursor on last inserted char */
8568 dec_cursor();
8569#endif
8570 mincol = 0;
8571 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008572 if (mode == BACKSPACE_LINE
8573 && (curbuf->b_p_ai
8574#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008575 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008576#endif
8577 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578#ifdef FEAT_RIGHTLEFT
8579 && !revins_on
8580#endif
8581 )
8582 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008583 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008585 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008586 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008587 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 }
8589
8590 /*
8591 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8592 */
8593 if ( mode == BACKSPACE_CHAR
8594 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008595 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008596 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008597 && (*(ml_get_cursor() - 1) == TAB
8598 || (*(ml_get_cursor() - 1) == ' '
8599 && (!*inserted_space_p
8600 || arrow_used))))))
8601 {
8602 int ts;
8603 colnr_T vcol;
8604 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008605 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606
8607 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008608 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008609 ts = curbuf->b_p_sw;
8610 else
8611 ts = curbuf->b_p_sts;
8612 /* Compute the virtual column where we want to be. Since
8613 * 'showbreak' may get in the way, need to get the last column of
8614 * the previous character. */
8615 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008616 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008617 dec_cursor();
8618 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8619 inc_cursor();
8620 want_vcol = (want_vcol / ts) * ts;
8621
8622 /* delete characters until we are at or before want_vcol */
8623 while (vcol > want_vcol
8624 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008625 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626
8627 /* insert extra spaces until we are at want_vcol */
8628 while (vcol < want_vcol)
8629 {
8630 /* Remember the first char we inserted */
8631 if (curwin->w_cursor.lnum == Insstart.lnum
8632 && curwin->w_cursor.col < Insstart.col)
8633 Insstart.col = curwin->w_cursor.col;
8634
8635#ifdef FEAT_VREPLACE
8636 if (State & VREPLACE_FLAG)
8637 ins_char(' ');
8638 else
8639#endif
8640 {
8641 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008642 if ((State & REPLACE_FLAG))
8643 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 }
8645 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8646 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008647
8648 /* If we are now back where we started delete one character. Can
8649 * happen when using 'sts' and 'linebreak'. */
8650 if (vcol >= start_vcol)
8651 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008652 }
8653
8654 /*
8655 * Delete upto starting point, start of line or previous word.
8656 */
8657 else do
8658 {
8659#ifdef FEAT_RIGHTLEFT
8660 if (!revins_on) /* put cursor on char to be deleted */
8661#endif
8662 dec_cursor();
8663
8664 /* start of word? */
8665 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8666 {
8667 mode = BACKSPACE_WORD_NOT_SPACE;
8668 temp = vim_iswordc(gchar_cursor());
8669 }
8670 /* end of word? */
8671 else if (mode == BACKSPACE_WORD_NOT_SPACE
8672 && (vim_isspace(cc = gchar_cursor())
8673 || vim_iswordc(cc) != temp))
8674 {
8675#ifdef FEAT_RIGHTLEFT
8676 if (!revins_on)
8677#endif
8678 inc_cursor();
8679#ifdef FEAT_RIGHTLEFT
8680 else if (State & REPLACE_FLAG)
8681 dec_cursor();
8682#endif
8683 break;
8684 }
8685 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008686 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008687 else
8688 {
8689#ifdef FEAT_MBYTE
8690 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008691 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692#endif
8693 (void)del_char(FALSE);
8694#ifdef FEAT_MBYTE
8695 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008696 * If there are combining characters and 'delcombine' is set
8697 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698 * character.
8699 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008700 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701 inc_cursor();
8702#endif
8703#ifdef FEAT_RIGHTLEFT
8704 if (revins_chars)
8705 {
8706 revins_chars--;
8707 revins_legal++;
8708 }
8709 if (revins_on && gchar_cursor() == NUL)
8710 break;
8711#endif
8712 }
8713 /* Just a single backspace?: */
8714 if (mode == BACKSPACE_CHAR)
8715 break;
8716 } while (
8717#ifdef FEAT_RIGHTLEFT
8718 revins_on ||
8719#endif
8720 (curwin->w_cursor.col > mincol
8721 && (curwin->w_cursor.lnum != Insstart.lnum
8722 || curwin->w_cursor.col != Insstart.col)));
8723 did_backspace = TRUE;
8724 }
8725#ifdef FEAT_SMARTINDENT
8726 did_si = FALSE;
8727 can_si = FALSE;
8728 can_si_back = FALSE;
8729#endif
8730 if (curwin->w_cursor.col <= 1)
8731 did_ai = FALSE;
8732 /*
8733 * It's a little strange to put backspaces into the redo
8734 * buffer, but it makes auto-indent a lot easier to deal
8735 * with.
8736 */
8737 AppendCharToRedobuff(c);
8738
8739 /* If deleted before the insertion point, adjust it */
8740 if (curwin->w_cursor.lnum == Insstart.lnum
8741 && curwin->w_cursor.col < Insstart.col)
8742 Insstart.col = curwin->w_cursor.col;
8743
8744 /* vi behaviour: the cursor moves backward but the character that
8745 * was there remains visible
8746 * Vim behaviour: the cursor moves backward and the character that
8747 * was there is erased from the screen.
8748 * We can emulate the vi behaviour by pretending there is a dollar
8749 * displayed even when there isn't.
8750 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8751 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8752 dollar_vcol = curwin->w_virtcol;
8753
Bram Moolenaarce3be472008-01-14 19:12:28 +00008754#ifdef FEAT_FOLDING
8755 /* When deleting a char the cursor line must never be in a closed fold.
8756 * E.g., when 'foldmethod' is indent and deleting the first non-white
8757 * char before a Tab. */
8758 if (did_backspace)
8759 foldOpenCursor();
8760#endif
8761
Bram Moolenaar071d4272004-06-13 20:20:40 +00008762 return did_backspace;
8763}
8764
8765#ifdef FEAT_MOUSE
8766 static void
8767ins_mouse(c)
8768 int c;
8769{
8770 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008771 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008772
8773# ifdef FEAT_GUI
8774 /* When GUI is active, also move/paste when 'mouse' is empty */
8775 if (!gui.in_use)
8776# endif
8777 if (!mouse_has(MOUSE_INSERT))
8778 return;
8779
8780 undisplay_dollar();
8781 tpos = curwin->w_cursor;
8782 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8783 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008784#ifdef FEAT_WINDOWS
8785 win_T *new_curwin = curwin;
8786
8787 if (curwin != old_curwin && win_valid(old_curwin))
8788 {
8789 /* Mouse took us to another window. We need to go back to the
8790 * previous one to stop insert there properly. */
8791 curwin = old_curwin;
8792 curbuf = curwin->w_buffer;
8793 }
8794#endif
8795 start_arrow(curwin == old_curwin ? &tpos : NULL);
8796#ifdef FEAT_WINDOWS
8797 if (curwin != new_curwin && win_valid(new_curwin))
8798 {
8799 curwin = new_curwin;
8800 curbuf = curwin->w_buffer;
8801 }
8802#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008803# ifdef FEAT_CINDENT
8804 can_cindent = TRUE;
8805# endif
8806 }
8807
8808#ifdef FEAT_WINDOWS
8809 /* redraw status lines (in case another window became active) */
8810 redraw_statuslines();
8811#endif
8812}
8813
8814 static void
8815ins_mousescroll(up)
8816 int up;
8817{
8818 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008819# if defined(FEAT_WINDOWS)
8820 win_T *old_curwin = curwin;
8821# endif
8822# ifdef FEAT_INS_EXPAND
8823 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008824# endif
8825
8826 tpos = curwin->w_cursor;
8827
8828# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008829 /* Currently the mouse coordinates are only known in the GUI. */
8830 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8831 {
8832 int row, col;
8833
8834 row = mouse_row;
8835 col = mouse_col;
8836
8837 /* find the window at the pointer coordinates */
8838 curwin = mouse_find_win(&row, &col);
8839 curbuf = curwin->w_buffer;
8840 }
8841 if (curwin == old_curwin)
8842# endif
8843 undisplay_dollar();
8844
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008845# ifdef FEAT_INS_EXPAND
8846 /* Don't scroll the window in which completion is being done. */
8847 if (!pum_visible()
8848# if defined(FEAT_WINDOWS)
8849 || curwin != old_curwin
8850# endif
8851 )
8852# endif
8853 {
8854 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8855 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8856 else
8857 scroll_redraw(up, 3L);
8858# ifdef FEAT_INS_EXPAND
8859 did_scroll = TRUE;
8860# endif
8861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862
8863# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8864 curwin->w_redr_status = TRUE;
8865
8866 curwin = old_curwin;
8867 curbuf = curwin->w_buffer;
8868# endif
8869
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008870# ifdef FEAT_INS_EXPAND
8871 /* The popup menu may overlay the window, need to redraw it.
8872 * TODO: Would be more efficient to only redraw the windows that are
8873 * overlapped by the popup menu. */
8874 if (pum_visible() && did_scroll)
8875 {
8876 redraw_all_later(NOT_VALID);
8877 ins_compl_show_pum();
8878 }
8879# endif
8880
Bram Moolenaar071d4272004-06-13 20:20:40 +00008881 if (!equalpos(curwin->w_cursor, tpos))
8882 {
8883 start_arrow(&tpos);
8884# ifdef FEAT_CINDENT
8885 can_cindent = TRUE;
8886# endif
8887 }
8888}
8889#endif
8890
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008891#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008892 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008893ins_tabline(c)
8894 int c;
8895{
8896 /* We will be leaving the current window, unless closing another tab. */
8897 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8898 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8899 {
8900 undisplay_dollar();
8901 start_arrow(&curwin->w_cursor);
8902# ifdef FEAT_CINDENT
8903 can_cindent = TRUE;
8904# endif
8905 }
8906
8907 if (c == K_TABLINE)
8908 goto_tabpage(current_tab);
8909 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008910 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008911 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008912 redraw_statuslines(); /* will redraw the tabline when needed */
8913 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008914}
8915#endif
8916
8917#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 void
8919ins_scroll()
8920{
8921 pos_T tpos;
8922
8923 undisplay_dollar();
8924 tpos = curwin->w_cursor;
8925 if (gui_do_scroll())
8926 {
8927 start_arrow(&tpos);
8928# ifdef FEAT_CINDENT
8929 can_cindent = TRUE;
8930# endif
8931 }
8932}
8933
8934 void
8935ins_horscroll()
8936{
8937 pos_T tpos;
8938
8939 undisplay_dollar();
8940 tpos = curwin->w_cursor;
8941 if (gui_do_horiz_scroll())
8942 {
8943 start_arrow(&tpos);
8944# ifdef FEAT_CINDENT
8945 can_cindent = TRUE;
8946# endif
8947 }
8948}
8949#endif
8950
8951 static void
8952ins_left()
8953{
8954 pos_T tpos;
8955
8956#ifdef FEAT_FOLDING
8957 if ((fdo_flags & FDO_HOR) && KeyTyped)
8958 foldOpenCursor();
8959#endif
8960 undisplay_dollar();
8961 tpos = curwin->w_cursor;
8962 if (oneleft() == OK)
8963 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008964#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8965 /* Only call start_arrow() when not busy with preediting, it will
8966 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8967 if (!im_is_preediting())
8968#endif
8969 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970#ifdef FEAT_RIGHTLEFT
8971 /* If exit reversed string, position is fixed */
8972 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8973 revins_legal++;
8974 revins_chars++;
8975#endif
8976 }
8977
8978 /*
8979 * if 'whichwrap' set for cursor in insert mode may go to
8980 * previous line
8981 */
8982 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8983 {
8984 start_arrow(&tpos);
8985 --(curwin->w_cursor.lnum);
8986 coladvance((colnr_T)MAXCOL);
8987 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8988 }
8989 else
8990 vim_beep();
8991}
8992
8993 static void
8994ins_home(c)
8995 int c;
8996{
8997 pos_T tpos;
8998
8999#ifdef FEAT_FOLDING
9000 if ((fdo_flags & FDO_HOR) && KeyTyped)
9001 foldOpenCursor();
9002#endif
9003 undisplay_dollar();
9004 tpos = curwin->w_cursor;
9005 if (c == K_C_HOME)
9006 curwin->w_cursor.lnum = 1;
9007 curwin->w_cursor.col = 0;
9008#ifdef FEAT_VIRTUALEDIT
9009 curwin->w_cursor.coladd = 0;
9010#endif
9011 curwin->w_curswant = 0;
9012 start_arrow(&tpos);
9013}
9014
9015 static void
9016ins_end(c)
9017 int c;
9018{
9019 pos_T tpos;
9020
9021#ifdef FEAT_FOLDING
9022 if ((fdo_flags & FDO_HOR) && KeyTyped)
9023 foldOpenCursor();
9024#endif
9025 undisplay_dollar();
9026 tpos = curwin->w_cursor;
9027 if (c == K_C_END)
9028 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9029 coladvance((colnr_T)MAXCOL);
9030 curwin->w_curswant = MAXCOL;
9031
9032 start_arrow(&tpos);
9033}
9034
9035 static void
9036ins_s_left()
9037{
9038#ifdef FEAT_FOLDING
9039 if ((fdo_flags & FDO_HOR) && KeyTyped)
9040 foldOpenCursor();
9041#endif
9042 undisplay_dollar();
9043 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9044 {
9045 start_arrow(&curwin->w_cursor);
9046 (void)bck_word(1L, FALSE, FALSE);
9047 curwin->w_set_curswant = TRUE;
9048 }
9049 else
9050 vim_beep();
9051}
9052
9053 static void
9054ins_right()
9055{
9056#ifdef FEAT_FOLDING
9057 if ((fdo_flags & FDO_HOR) && KeyTyped)
9058 foldOpenCursor();
9059#endif
9060 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009061 if (gchar_cursor() != NUL
9062#ifdef FEAT_VIRTUALEDIT
9063 || virtual_active()
9064#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009065 )
9066 {
9067 start_arrow(&curwin->w_cursor);
9068 curwin->w_set_curswant = TRUE;
9069#ifdef FEAT_VIRTUALEDIT
9070 if (virtual_active())
9071 oneright();
9072 else
9073#endif
9074 {
9075#ifdef FEAT_MBYTE
9076 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009077 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078 else
9079#endif
9080 ++curwin->w_cursor.col;
9081 }
9082
9083#ifdef FEAT_RIGHTLEFT
9084 revins_legal++;
9085 if (revins_chars)
9086 revins_chars--;
9087#endif
9088 }
9089 /* if 'whichwrap' set for cursor in insert mode, may move the
9090 * cursor to the next line */
9091 else if (vim_strchr(p_ww, ']') != NULL
9092 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9093 {
9094 start_arrow(&curwin->w_cursor);
9095 curwin->w_set_curswant = TRUE;
9096 ++curwin->w_cursor.lnum;
9097 curwin->w_cursor.col = 0;
9098 }
9099 else
9100 vim_beep();
9101}
9102
9103 static void
9104ins_s_right()
9105{
9106#ifdef FEAT_FOLDING
9107 if ((fdo_flags & FDO_HOR) && KeyTyped)
9108 foldOpenCursor();
9109#endif
9110 undisplay_dollar();
9111 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9112 || gchar_cursor() != NUL)
9113 {
9114 start_arrow(&curwin->w_cursor);
9115 (void)fwd_word(1L, FALSE, 0);
9116 curwin->w_set_curswant = TRUE;
9117 }
9118 else
9119 vim_beep();
9120}
9121
9122 static void
9123ins_up(startcol)
9124 int startcol; /* when TRUE move to Insstart.col */
9125{
Bram Moolenaar860cae12010-06-05 23:22:07 +02009126#ifdef FEAT_CONCEAL
9127 linenr_T oldline = curwin->w_cursor.lnum;
9128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129 pos_T tpos;
9130 linenr_T old_topline = curwin->w_topline;
9131#ifdef FEAT_DIFF
9132 int old_topfill = curwin->w_topfill;
9133#endif
9134
9135 undisplay_dollar();
9136 tpos = curwin->w_cursor;
9137 if (cursor_up(1L, TRUE) == OK)
9138 {
9139 if (startcol)
9140 coladvance(getvcol_nolist(&Insstart));
9141 if (old_topline != curwin->w_topline
9142#ifdef FEAT_DIFF
9143 || old_topfill != curwin->w_topfill
9144#endif
9145 )
9146 redraw_later(VALID);
9147 start_arrow(&tpos);
9148#ifdef FEAT_CINDENT
9149 can_cindent = TRUE;
9150#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02009151#ifdef FEAT_CONCEAL
9152 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
9153 {
9154 update_single_line(curwin, oldline);
9155 update_single_line(curwin, curwin->w_cursor.lnum);
9156 }
9157#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 }
9159 else
9160 vim_beep();
9161}
9162
9163 static void
9164ins_pageup()
9165{
9166 pos_T tpos;
9167
9168 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009169
9170#ifdef FEAT_WINDOWS
9171 if (mod_mask & MOD_MASK_CTRL)
9172 {
9173 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009174 if (first_tabpage->tp_next != NULL)
9175 {
9176 start_arrow(&curwin->w_cursor);
9177 goto_tabpage(-1);
9178 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009179 return;
9180 }
9181#endif
9182
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183 tpos = curwin->w_cursor;
9184 if (onepage(BACKWARD, 1L) == OK)
9185 {
9186 start_arrow(&tpos);
9187#ifdef FEAT_CINDENT
9188 can_cindent = TRUE;
9189#endif
9190 }
9191 else
9192 vim_beep();
9193}
9194
9195 static void
9196ins_down(startcol)
9197 int startcol; /* when TRUE move to Insstart.col */
9198{
Bram Moolenaar860cae12010-06-05 23:22:07 +02009199#ifdef FEAT_CONCEAL
9200 linenr_T oldline = curwin->w_cursor.lnum;
9201 linenr_T oldbotline = curwin->w_botline;
9202#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203 pos_T tpos;
9204 linenr_T old_topline = curwin->w_topline;
9205#ifdef FEAT_DIFF
9206 int old_topfill = curwin->w_topfill;
9207#endif
9208
9209 undisplay_dollar();
9210 tpos = curwin->w_cursor;
9211 if (cursor_down(1L, TRUE) == OK)
9212 {
9213 if (startcol)
9214 coladvance(getvcol_nolist(&Insstart));
9215 if (old_topline != curwin->w_topline
9216#ifdef FEAT_DIFF
9217 || old_topfill != curwin->w_topfill
9218#endif
9219 )
9220 redraw_later(VALID);
9221 start_arrow(&tpos);
9222#ifdef FEAT_CINDENT
9223 can_cindent = TRUE;
9224#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +02009225#ifdef FEAT_CONCEAL
9226 if (curwin->w_p_conceal && oldline != curwin->w_cursor.lnum)
9227 {
9228 update_single_line(curwin, oldline);
9229 /* Don't do this if we've scrolled, the line is already
9230 * drawn */
9231 if (oldbotline == curwin->w_botline)
9232 update_single_line(curwin, curwin->w_cursor.lnum);
9233 }
9234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009235 }
9236 else
9237 vim_beep();
9238}
9239
9240 static void
9241ins_pagedown()
9242{
9243 pos_T tpos;
9244
9245 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009246
9247#ifdef FEAT_WINDOWS
9248 if (mod_mask & MOD_MASK_CTRL)
9249 {
9250 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009251 if (first_tabpage->tp_next != NULL)
9252 {
9253 start_arrow(&curwin->w_cursor);
9254 goto_tabpage(0);
9255 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009256 return;
9257 }
9258#endif
9259
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260 tpos = curwin->w_cursor;
9261 if (onepage(FORWARD, 1L) == OK)
9262 {
9263 start_arrow(&tpos);
9264#ifdef FEAT_CINDENT
9265 can_cindent = TRUE;
9266#endif
9267 }
9268 else
9269 vim_beep();
9270}
9271
9272#ifdef FEAT_DND
9273 static void
9274ins_drop()
9275{
9276 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9277}
9278#endif
9279
9280/*
9281 * Handle TAB in Insert or Replace mode.
9282 * Return TRUE when the TAB needs to be inserted like a normal character.
9283 */
9284 static int
9285ins_tab()
9286{
9287 int ind;
9288 int i;
9289 int temp;
9290
9291 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9292 Insstart_blank_vcol = get_nolist_virtcol();
9293 if (echeck_abbr(TAB + ABBR_OFF))
9294 return FALSE;
9295
9296 ind = inindent(0);
9297#ifdef FEAT_CINDENT
9298 if (ind)
9299 can_cindent = FALSE;
9300#endif
9301
9302 /*
9303 * When nothing special, insert TAB like a normal character
9304 */
9305 if (!curbuf->b_p_et
9306 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9307 && curbuf->b_p_sts == 0)
9308 return TRUE;
9309
9310 if (stop_arrow() == FAIL)
9311 return TRUE;
9312
9313 did_ai = FALSE;
9314#ifdef FEAT_SMARTINDENT
9315 did_si = FALSE;
9316 can_si = FALSE;
9317 can_si_back = FALSE;
9318#endif
9319 AppendToRedobuff((char_u *)"\t");
9320
9321 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9322 temp = (int)curbuf->b_p_sw;
9323 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9324 temp = (int)curbuf->b_p_sts;
9325 else /* otherwise use 'tabstop' */
9326 temp = (int)curbuf->b_p_ts;
9327 temp -= get_nolist_virtcol() % temp;
9328
9329 /*
9330 * Insert the first space with ins_char(). It will delete one char in
9331 * replace mode. Insert the rest with ins_str(); it will not delete any
9332 * chars. For VREPLACE mode, we use ins_char() for all characters.
9333 */
9334 ins_char(' ');
9335 while (--temp > 0)
9336 {
9337#ifdef FEAT_VREPLACE
9338 if (State & VREPLACE_FLAG)
9339 ins_char(' ');
9340 else
9341#endif
9342 {
9343 ins_str((char_u *)" ");
9344 if (State & REPLACE_FLAG) /* no char replaced */
9345 replace_push(NUL);
9346 }
9347 }
9348
9349 /*
9350 * When 'expandtab' not set: Replace spaces by TABs where possible.
9351 */
9352 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9353 {
9354 char_u *ptr;
9355#ifdef FEAT_VREPLACE
9356 char_u *saved_line = NULL; /* init for GCC */
9357 pos_T pos;
9358#endif
9359 pos_T fpos;
9360 pos_T *cursor;
9361 colnr_T want_vcol, vcol;
9362 int change_col = -1;
9363 int save_list = curwin->w_p_list;
9364
9365 /*
9366 * Get the current line. For VREPLACE mode, don't make real changes
9367 * yet, just work on a copy of the line.
9368 */
9369#ifdef FEAT_VREPLACE
9370 if (State & VREPLACE_FLAG)
9371 {
9372 pos = curwin->w_cursor;
9373 cursor = &pos;
9374 saved_line = vim_strsave(ml_get_curline());
9375 if (saved_line == NULL)
9376 return FALSE;
9377 ptr = saved_line + pos.col;
9378 }
9379 else
9380#endif
9381 {
9382 ptr = ml_get_cursor();
9383 cursor = &curwin->w_cursor;
9384 }
9385
9386 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9387 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9388 curwin->w_p_list = FALSE;
9389
9390 /* Find first white before the cursor */
9391 fpos = curwin->w_cursor;
9392 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9393 {
9394 --fpos.col;
9395 --ptr;
9396 }
9397
9398 /* In Replace mode, don't change characters before the insert point. */
9399 if ((State & REPLACE_FLAG)
9400 && fpos.lnum == Insstart.lnum
9401 && fpos.col < Insstart.col)
9402 {
9403 ptr += Insstart.col - fpos.col;
9404 fpos.col = Insstart.col;
9405 }
9406
9407 /* compute virtual column numbers of first white and cursor */
9408 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9409 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9410
9411 /* Use as many TABs as possible. Beware of 'showbreak' and
9412 * 'linebreak' adding extra virtual columns. */
9413 while (vim_iswhite(*ptr))
9414 {
9415 i = lbr_chartabsize((char_u *)"\t", vcol);
9416 if (vcol + i > want_vcol)
9417 break;
9418 if (*ptr != TAB)
9419 {
9420 *ptr = TAB;
9421 if (change_col < 0)
9422 {
9423 change_col = fpos.col; /* Column of first change */
9424 /* May have to adjust Insstart */
9425 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9426 Insstart.col = fpos.col;
9427 }
9428 }
9429 ++fpos.col;
9430 ++ptr;
9431 vcol += i;
9432 }
9433
9434 if (change_col >= 0)
9435 {
9436 int repl_off = 0;
9437
9438 /* Skip over the spaces we need. */
9439 while (vcol < want_vcol && *ptr == ' ')
9440 {
9441 vcol += lbr_chartabsize(ptr, vcol);
9442 ++ptr;
9443 ++repl_off;
9444 }
9445 if (vcol > want_vcol)
9446 {
9447 /* Must have a char with 'showbreak' just before it. */
9448 --ptr;
9449 --repl_off;
9450 }
9451 fpos.col += repl_off;
9452
9453 /* Delete following spaces. */
9454 i = cursor->col - fpos.col;
9455 if (i > 0)
9456 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009457 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009458 /* correct replace stack. */
9459 if ((State & REPLACE_FLAG)
9460#ifdef FEAT_VREPLACE
9461 && !(State & VREPLACE_FLAG)
9462#endif
9463 )
9464 for (temp = i; --temp >= 0; )
9465 replace_join(repl_off);
9466 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009467#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009468 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009469 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009470 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009471 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9472 (char_u *)"\t", 1);
9473 }
9474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009475 cursor->col -= i;
9476
9477#ifdef FEAT_VREPLACE
9478 /*
9479 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9480 * backspacing over the changed spacing and then inserting the new
9481 * spacing.
9482 */
9483 if (State & VREPLACE_FLAG)
9484 {
9485 /* Backspace from real cursor to change_col */
9486 backspace_until_column(change_col);
9487
9488 /* Insert each char in saved_line from changed_col to
9489 * ptr-cursor */
9490 ins_bytes_len(saved_line + change_col,
9491 cursor->col - change_col);
9492 }
9493#endif
9494 }
9495
9496#ifdef FEAT_VREPLACE
9497 if (State & VREPLACE_FLAG)
9498 vim_free(saved_line);
9499#endif
9500 curwin->w_p_list = save_list;
9501 }
9502
9503 return FALSE;
9504}
9505
9506/*
9507 * Handle CR or NL in insert mode.
9508 * Return TRUE when out of memory or can't undo.
9509 */
9510 static int
9511ins_eol(c)
9512 int c;
9513{
9514 int i;
9515
9516 if (echeck_abbr(c + ABBR_OFF))
9517 return FALSE;
9518 if (stop_arrow() == FAIL)
9519 return TRUE;
9520 undisplay_dollar();
9521
9522 /*
9523 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9524 * character under the cursor. Only push a NUL on the replace stack,
9525 * nothing to put back when the NL is deleted.
9526 */
9527 if ((State & REPLACE_FLAG)
9528#ifdef FEAT_VREPLACE
9529 && !(State & VREPLACE_FLAG)
9530#endif
9531 )
9532 replace_push(NUL);
9533
9534 /*
9535 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9536 * replacing the next line, so we push all of the characters left on the
9537 * line onto the replace stack. This is not done here though, it is done
9538 * in open_line().
9539 */
9540
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009541#ifdef FEAT_VIRTUALEDIT
9542 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9543 * CTRL-O). */
9544 if (virtual_active() && curwin->w_cursor.coladd > 0)
9545 coladvance(getviscol());
9546#endif
9547
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548#ifdef FEAT_RIGHTLEFT
9549# ifdef FEAT_FKMAP
9550 if (p_altkeymap && p_fkmap)
9551 fkmap(NL);
9552# endif
9553 /* NL in reverse insert will always start in the end of
9554 * current line. */
9555 if (revins_on)
9556 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9557#endif
9558
9559 AppendToRedobuff(NL_STR);
9560 i = open_line(FORWARD,
9561#ifdef FEAT_COMMENTS
9562 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9563#endif
9564 0, old_indent);
9565 old_indent = 0;
9566#ifdef FEAT_CINDENT
9567 can_cindent = TRUE;
9568#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009569#ifdef FEAT_FOLDING
9570 /* When inserting a line the cursor line must never be in a closed fold. */
9571 foldOpenCursor();
9572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009573
9574 return (!i);
9575}
9576
9577#ifdef FEAT_DIGRAPHS
9578/*
9579 * Handle digraph in insert mode.
9580 * Returns character still to be inserted, or NUL when nothing remaining to be
9581 * done.
9582 */
9583 static int
9584ins_digraph()
9585{
9586 int c;
9587 int cc;
9588
9589 pc_status = PC_STATUS_UNSET;
9590 if (redrawing() && !char_avail())
9591 {
9592 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009593 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009594
9595 edit_putchar('?', TRUE);
9596#ifdef FEAT_CMDL_INFO
9597 add_to_showcmd_c(Ctrl_K);
9598#endif
9599 }
9600
9601#ifdef USE_ON_FLY_SCROLL
9602 dont_scroll = TRUE; /* disallow scrolling here */
9603#endif
9604
9605 /* don't map the digraph chars. This also prevents the
9606 * mode message to be deleted when ESC is hit */
9607 ++no_mapping;
9608 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009609 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 --no_mapping;
9611 --allow_keys;
9612 if (IS_SPECIAL(c) || mod_mask) /* special key */
9613 {
9614#ifdef FEAT_CMDL_INFO
9615 clear_showcmd();
9616#endif
9617 insert_special(c, TRUE, FALSE);
9618 return NUL;
9619 }
9620 if (c != ESC)
9621 {
9622 if (redrawing() && !char_avail())
9623 {
9624 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009625 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626
9627 if (char2cells(c) == 1)
9628 {
9629 /* first remove the '?', otherwise it's restored when typing
9630 * an ESC next */
9631 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009632 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633 edit_putchar(c, TRUE);
9634 }
9635#ifdef FEAT_CMDL_INFO
9636 add_to_showcmd_c(c);
9637#endif
9638 }
9639 ++no_mapping;
9640 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009641 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642 --no_mapping;
9643 --allow_keys;
9644 if (cc != ESC)
9645 {
9646 AppendToRedobuff((char_u *)CTRL_V_STR);
9647 c = getdigraph(c, cc, TRUE);
9648#ifdef FEAT_CMDL_INFO
9649 clear_showcmd();
9650#endif
9651 return c;
9652 }
9653 }
9654 edit_unputchar();
9655#ifdef FEAT_CMDL_INFO
9656 clear_showcmd();
9657#endif
9658 return NUL;
9659}
9660#endif
9661
9662/*
9663 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9664 * Returns the char to be inserted, or NUL if none found.
9665 */
9666 static int
9667ins_copychar(lnum)
9668 linenr_T lnum;
9669{
9670 int c;
9671 int temp;
9672 char_u *ptr, *prev_ptr;
9673
9674 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9675 {
9676 vim_beep();
9677 return NUL;
9678 }
9679
9680 /* try to advance to the cursor column */
9681 temp = 0;
9682 ptr = ml_get(lnum);
9683 prev_ptr = ptr;
9684 validate_virtcol();
9685 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9686 {
9687 prev_ptr = ptr;
9688 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9689 }
9690 if ((colnr_T)temp > curwin->w_virtcol)
9691 ptr = prev_ptr;
9692
9693#ifdef FEAT_MBYTE
9694 c = (*mb_ptr2char)(ptr);
9695#else
9696 c = *ptr;
9697#endif
9698 if (c == NUL)
9699 vim_beep();
9700 return c;
9701}
9702
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009703/*
9704 * CTRL-Y or CTRL-E typed in Insert mode.
9705 */
9706 static int
9707ins_ctrl_ey(tc)
9708 int tc;
9709{
9710 int c = tc;
9711
9712#ifdef FEAT_INS_EXPAND
9713 if (ctrl_x_mode == CTRL_X_SCROLL)
9714 {
9715 if (c == Ctrl_Y)
9716 scrolldown_clamp();
9717 else
9718 scrollup_clamp();
9719 redraw_later(VALID);
9720 }
9721 else
9722#endif
9723 {
9724 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9725 if (c != NUL)
9726 {
9727 long tw_save;
9728
9729 /* The character must be taken literally, insert like it
9730 * was typed after a CTRL-V, and pretend 'textwidth'
9731 * wasn't set. Digits, 'o' and 'x' are special after a
9732 * CTRL-V, don't use it for these. */
9733 if (c < 256 && !isalnum(c))
9734 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9735 tw_save = curbuf->b_p_tw;
9736 curbuf->b_p_tw = -1;
9737 insert_special(c, TRUE, FALSE);
9738 curbuf->b_p_tw = tw_save;
9739#ifdef FEAT_RIGHTLEFT
9740 revins_chars++;
9741 revins_legal++;
9742#endif
9743 c = Ctrl_V; /* pretend CTRL-V is last character */
9744 auto_format(FALSE, TRUE);
9745 }
9746 }
9747 return c;
9748}
9749
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750#ifdef FEAT_SMARTINDENT
9751/*
9752 * Try to do some very smart auto-indenting.
9753 * Used when inserting a "normal" character.
9754 */
9755 static void
9756ins_try_si(c)
9757 int c;
9758{
9759 pos_T *pos, old_pos;
9760 char_u *ptr;
9761 int i;
9762 int temp;
9763
9764 /*
9765 * do some very smart indenting when entering '{' or '}'
9766 */
9767 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9768 {
9769 /*
9770 * for '}' set indent equal to indent of line containing matching '{'
9771 */
9772 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9773 {
9774 old_pos = curwin->w_cursor;
9775 /*
9776 * If the matching '{' has a ')' immediately before it (ignoring
9777 * white-space), then line up with the start of the line
9778 * containing the matching '(' if there is one. This handles the
9779 * case where an "if (..\n..) {" statement continues over multiple
9780 * lines -- webb
9781 */
9782 ptr = ml_get(pos->lnum);
9783 i = pos->col;
9784 if (i > 0) /* skip blanks before '{' */
9785 while (--i > 0 && vim_iswhite(ptr[i]))
9786 ;
9787 curwin->w_cursor.lnum = pos->lnum;
9788 curwin->w_cursor.col = i;
9789 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9790 curwin->w_cursor = *pos;
9791 i = get_indent();
9792 curwin->w_cursor = old_pos;
9793#ifdef FEAT_VREPLACE
9794 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009795 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 else
9797#endif
9798 (void)set_indent(i, SIN_CHANGED);
9799 }
9800 else if (curwin->w_cursor.col > 0)
9801 {
9802 /*
9803 * when inserting '{' after "O" reduce indent, but not
9804 * more than indent of previous line
9805 */
9806 temp = TRUE;
9807 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9808 {
9809 old_pos = curwin->w_cursor;
9810 i = get_indent();
9811 while (curwin->w_cursor.lnum > 1)
9812 {
9813 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9814
9815 /* ignore empty lines and lines starting with '#'. */
9816 if (*ptr != '#' && *ptr != NUL)
9817 break;
9818 }
9819 if (get_indent() >= i)
9820 temp = FALSE;
9821 curwin->w_cursor = old_pos;
9822 }
9823 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009824 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 }
9826 }
9827
9828 /*
9829 * set indent of '#' always to 0
9830 */
9831 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9832 {
9833 /* remember current indent for next line */
9834 old_indent = get_indent();
9835 (void)set_indent(0, SIN_CHANGED);
9836 }
9837
9838 /* Adjust ai_col, the char at this position can be deleted. */
9839 if (ai_col > curwin->w_cursor.col)
9840 ai_col = curwin->w_cursor.col;
9841}
9842#endif
9843
9844/*
9845 * Get the value that w_virtcol would have when 'list' is off.
9846 * Unless 'cpo' contains the 'L' flag.
9847 */
9848 static colnr_T
9849get_nolist_virtcol()
9850{
9851 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9852 return getvcol_nolist(&curwin->w_cursor);
9853 validate_virtcol();
9854 return curwin->w_virtcol;
9855}