blob: f465f20177a0c66d61505fa480146a537ad192f4 [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{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001460#ifdef FEAT_CONCEAL
1461 linenr_T conceal_old_cursor_line = 0;
1462 linenr_T conceal_new_cursor_line = 0;
1463 int conceal_update_lines = FALSE;
1464#endif
1465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (!char_avail())
1467 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001468#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001469 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1470 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001471 if (ready && (
1472# ifdef FEAT_AUTOCMD
1473 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001474# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001475# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1476 ||
1477# endif
1478# ifdef FEAT_CONCEAL
1479 curwin->w_p_conceal
1480# endif
1481 )
1482 && !equalpos(last_cursormoved, curwin->w_cursor)
1483# ifdef FEAT_INS_EXPAND
1484 && !pum_visible()
1485# endif
1486 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001487 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001488# ifdef FEAT_SYN_HL
1489 /* Need to update the screen first, to make sure syntax
1490 * highlighting is correct after making a change (e.g., inserting
1491 * a "(". The autocommand may also require a redraw, so it's done
1492 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001493 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001494 update_screen(0);
1495# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001496# ifdef FEAT_AUTOCMD
1497 if (has_cursormovedI())
1498 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1499# endif
1500# ifdef FEAT_CONCEAL
1501 if (curwin->w_p_conceal)
1502 {
1503 conceal_old_cursor_line = last_cursormoved.lnum;
1504 conceal_new_cursor_line = curwin->w_cursor.lnum;
1505 conceal_update_lines = TRUE;
1506 }
1507# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001508 last_cursormoved = curwin->w_cursor;
1509 }
1510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (must_redraw)
1512 update_screen(0);
1513 else if (clear_cmdline || redraw_cmdline)
1514 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001515# if defined(FEAT_CONCEAL)
1516 if (conceal_update_lines
1517 && conceal_old_cursor_line != conceal_new_cursor_line)
1518 {
1519 update_single_line(curwin, conceal_old_cursor_line);
1520 update_single_line(curwin, conceal_new_cursor_line);
1521 curwin->w_valid &= ~VALID_CROW;
1522 }
1523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 showruler(FALSE);
1525 setcursor();
1526 emsg_on_display = FALSE; /* may remove error message now */
1527 }
1528}
1529
1530/*
1531 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1532 */
1533 static void
1534ins_ctrl_v()
1535{
1536 int c;
1537
1538 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001539 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541 if (redrawing() && !char_avail())
1542 edit_putchar('^', TRUE);
1543 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1544
1545#ifdef FEAT_CMDL_INFO
1546 add_to_showcmd_c(Ctrl_V);
1547#endif
1548
1549 c = get_literal();
1550#ifdef FEAT_CMDL_INFO
1551 clear_showcmd();
1552#endif
1553 insert_special(c, FALSE, TRUE);
1554#ifdef FEAT_RIGHTLEFT
1555 revins_chars++;
1556 revins_legal++;
1557#endif
1558}
1559
1560/*
1561 * Put a character directly onto the screen. It's not stored in a buffer.
1562 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1563 */
1564static int pc_status;
1565#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1566#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1567#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1568#define PC_STATUS_SET 3 /* pc_bytes was filled */
1569#ifdef FEAT_MBYTE
1570static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1571#else
1572static char_u pc_bytes[2]; /* saved bytes */
1573#endif
1574static int pc_attr;
1575static int pc_row;
1576static int pc_col;
1577
1578 void
1579edit_putchar(c, highlight)
1580 int c;
1581 int highlight;
1582{
1583 int attr;
1584
1585 if (ScreenLines != NULL)
1586 {
1587 update_topline(); /* just in case w_topline isn't valid */
1588 validate_cursor();
1589 if (highlight)
1590 attr = hl_attr(HLF_8);
1591 else
1592 attr = 0;
1593 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1594 pc_col = W_WINCOL(curwin);
1595#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1596 pc_status = PC_STATUS_UNSET;
1597#endif
1598#ifdef FEAT_RIGHTLEFT
1599 if (curwin->w_p_rl)
1600 {
1601 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1602# ifdef FEAT_MBYTE
1603 if (has_mbyte)
1604 {
1605 int fix_col = mb_fix_col(pc_col, pc_row);
1606
1607 if (fix_col != pc_col)
1608 {
1609 screen_putchar(' ', pc_row, fix_col, attr);
1610 --curwin->w_wcol;
1611 pc_status = PC_STATUS_RIGHT;
1612 }
1613 }
1614# endif
1615 }
1616 else
1617#endif
1618 {
1619 pc_col += curwin->w_wcol;
1620#ifdef FEAT_MBYTE
1621 if (mb_lefthalve(pc_row, pc_col))
1622 pc_status = PC_STATUS_LEFT;
1623#endif
1624 }
1625
1626 /* save the character to be able to put it back */
1627#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1628 if (pc_status == PC_STATUS_UNSET)
1629#endif
1630 {
1631 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1632 pc_status = PC_STATUS_SET;
1633 }
1634 screen_putchar(c, pc_row, pc_col, attr);
1635 }
1636}
1637
1638/*
1639 * Undo the previous edit_putchar().
1640 */
1641 void
1642edit_unputchar()
1643{
1644 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1645 {
1646#if defined(FEAT_MBYTE)
1647 if (pc_status == PC_STATUS_RIGHT)
1648 ++curwin->w_wcol;
1649 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1650 redrawWinline(curwin->w_cursor.lnum, FALSE);
1651 else
1652#endif
1653 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1654 }
1655}
1656
1657/*
1658 * Called when p_dollar is set: display a '$' at the end of the changed text
1659 * Only works when cursor is in the line that changes.
1660 */
1661 void
1662display_dollar(col)
1663 colnr_T col;
1664{
1665 colnr_T save_col;
1666
1667 if (!redrawing())
1668 return;
1669
1670 cursor_off();
1671 save_col = curwin->w_cursor.col;
1672 curwin->w_cursor.col = col;
1673#ifdef FEAT_MBYTE
1674 if (has_mbyte)
1675 {
1676 char_u *p;
1677
1678 /* If on the last byte of a multi-byte move to the first byte. */
1679 p = ml_get_curline();
1680 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1681 }
1682#endif
1683 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1684 if (curwin->w_wcol < W_WIDTH(curwin))
1685 {
1686 edit_putchar('$', FALSE);
1687 dollar_vcol = curwin->w_virtcol;
1688 }
1689 curwin->w_cursor.col = save_col;
1690}
1691
1692/*
1693 * Call this function before moving the cursor from the normal insert position
1694 * in insert mode.
1695 */
1696 static void
1697undisplay_dollar()
1698{
1699 if (dollar_vcol)
1700 {
1701 dollar_vcol = 0;
1702 redrawWinline(curwin->w_cursor.lnum, FALSE);
1703 }
1704}
1705
1706/*
1707 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1708 * Keep the cursor on the same character.
1709 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1710 * type == INDENT_DEC decrease indent (for CTRL-D)
1711 * type == INDENT_SET set indent to "amount"
1712 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1713 */
1714 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001715change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001716 int type;
1717 int amount;
1718 int round;
1719 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001720 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001721{
1722 int vcol;
1723 int last_vcol;
1724 int insstart_less; /* reduction for Insstart.col */
1725 int new_cursor_col;
1726 int i;
1727 char_u *ptr;
1728 int save_p_list;
1729 int start_col;
1730 colnr_T vc;
1731#ifdef FEAT_VREPLACE
1732 colnr_T orig_col = 0; /* init for GCC */
1733 char_u *new_line, *orig_line = NULL; /* init for GCC */
1734
1735 /* VREPLACE mode needs to know what the line was like before changing */
1736 if (State & VREPLACE_FLAG)
1737 {
1738 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1739 orig_col = curwin->w_cursor.col;
1740 }
1741#endif
1742
1743 /* for the following tricks we don't want list mode */
1744 save_p_list = curwin->w_p_list;
1745 curwin->w_p_list = FALSE;
1746 vc = getvcol_nolist(&curwin->w_cursor);
1747 vcol = vc;
1748
1749 /*
1750 * For Replace mode we need to fix the replace stack later, which is only
1751 * possible when the cursor is in the indent. Remember the number of
1752 * characters before the cursor if it's possible.
1753 */
1754 start_col = curwin->w_cursor.col;
1755
1756 /* determine offset from first non-blank */
1757 new_cursor_col = curwin->w_cursor.col;
1758 beginline(BL_WHITE);
1759 new_cursor_col -= curwin->w_cursor.col;
1760
1761 insstart_less = curwin->w_cursor.col;
1762
1763 /*
1764 * If the cursor is in the indent, compute how many screen columns the
1765 * cursor is to the left of the first non-blank.
1766 */
1767 if (new_cursor_col < 0)
1768 vcol = get_indent() - vcol;
1769
1770 if (new_cursor_col > 0) /* can't fix replace stack */
1771 start_col = -1;
1772
1773 /*
1774 * Set the new indent. The cursor will be put on the first non-blank.
1775 */
1776 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001777 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778 else
1779 {
1780#ifdef FEAT_VREPLACE
1781 int save_State = State;
1782
1783 /* Avoid being called recursively. */
1784 if (State & VREPLACE_FLAG)
1785 State = INSERT;
1786#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001787 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788#ifdef FEAT_VREPLACE
1789 State = save_State;
1790#endif
1791 }
1792 insstart_less -= curwin->w_cursor.col;
1793
1794 /*
1795 * Try to put cursor on same character.
1796 * If the cursor is at or after the first non-blank in the line,
1797 * compute the cursor column relative to the column of the first
1798 * non-blank character.
1799 * If we are not in insert mode, leave the cursor on the first non-blank.
1800 * If the cursor is before the first non-blank, position it relative
1801 * to the first non-blank, counted in screen columns.
1802 */
1803 if (new_cursor_col >= 0)
1804 {
1805 /*
1806 * When changing the indent while the cursor is touching it, reset
1807 * Insstart_col to 0.
1808 */
1809 if (new_cursor_col == 0)
1810 insstart_less = MAXCOL;
1811 new_cursor_col += curwin->w_cursor.col;
1812 }
1813 else if (!(State & INSERT))
1814 new_cursor_col = curwin->w_cursor.col;
1815 else
1816 {
1817 /*
1818 * Compute the screen column where the cursor should be.
1819 */
1820 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001821 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001822
1823 /*
1824 * Advance the cursor until we reach the right screen column.
1825 */
1826 vcol = last_vcol = 0;
1827 new_cursor_col = -1;
1828 ptr = ml_get_curline();
1829 while (vcol <= (int)curwin->w_virtcol)
1830 {
1831 last_vcol = vcol;
1832#ifdef FEAT_MBYTE
1833 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001834 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 else
1836#endif
1837 ++new_cursor_col;
1838 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1839 }
1840 vcol = last_vcol;
1841
1842 /*
1843 * May need to insert spaces to be able to position the cursor on
1844 * the right screen column.
1845 */
1846 if (vcol != (int)curwin->w_virtcol)
1847 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001848 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001850 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 if (ptr != NULL)
1852 {
1853 new_cursor_col += i;
1854 ptr[i] = NUL;
1855 while (--i >= 0)
1856 ptr[i] = ' ';
1857 ins_str(ptr);
1858 vim_free(ptr);
1859 }
1860 }
1861
1862 /*
1863 * When changing the indent while the cursor is in it, reset
1864 * Insstart_col to 0.
1865 */
1866 insstart_less = MAXCOL;
1867 }
1868
1869 curwin->w_p_list = save_p_list;
1870
1871 if (new_cursor_col <= 0)
1872 curwin->w_cursor.col = 0;
1873 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001874 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 curwin->w_set_curswant = TRUE;
1876 changed_cline_bef_curs();
1877
1878 /*
1879 * May have to adjust the start of the insert.
1880 */
1881 if (State & INSERT)
1882 {
1883 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1884 {
1885 if ((int)Insstart.col <= insstart_less)
1886 Insstart.col = 0;
1887 else
1888 Insstart.col -= insstart_less;
1889 }
1890 if ((int)ai_col <= insstart_less)
1891 ai_col = 0;
1892 else
1893 ai_col -= insstart_less;
1894 }
1895
1896 /*
1897 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1898 * If the number of characters before the cursor decreased, need to pop a
1899 * few characters from the replace stack.
1900 * If the number of characters before the cursor increased, need to push a
1901 * few NULs onto the replace stack.
1902 */
1903 if (REPLACE_NORMAL(State) && start_col >= 0)
1904 {
1905 while (start_col > (int)curwin->w_cursor.col)
1906 {
1907 replace_join(0); /* remove a NUL from the replace stack */
1908 --start_col;
1909 }
1910 while (start_col < (int)curwin->w_cursor.col || replaced)
1911 {
1912 replace_push(NUL);
1913 if (replaced)
1914 {
1915 replace_push(replaced);
1916 replaced = NUL;
1917 }
1918 ++start_col;
1919 }
1920 }
1921
1922#ifdef FEAT_VREPLACE
1923 /*
1924 * For VREPLACE mode, we also have to fix the replace stack. In this case
1925 * it is always possible because we backspace over the whole line and then
1926 * put it back again the way we wanted it.
1927 */
1928 if (State & VREPLACE_FLAG)
1929 {
1930 /* If orig_line didn't allocate, just return. At least we did the job,
1931 * even if you can't backspace. */
1932 if (orig_line == NULL)
1933 return;
1934
1935 /* Save new line */
1936 new_line = vim_strsave(ml_get_curline());
1937 if (new_line == NULL)
1938 return;
1939
1940 /* We only put back the new line up to the cursor */
1941 new_line[curwin->w_cursor.col] = NUL;
1942
1943 /* Put back original line */
1944 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1945 curwin->w_cursor.col = orig_col;
1946
1947 /* Backspace from cursor to start of line */
1948 backspace_until_column(0);
1949
1950 /* Insert new stuff into line again */
1951 ins_bytes(new_line);
1952
1953 vim_free(new_line);
1954 }
1955#endif
1956}
1957
1958/*
1959 * Truncate the space at the end of a line. This is to be used only in an
1960 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1961 * modes.
1962 */
1963 void
1964truncate_spaces(line)
1965 char_u *line;
1966{
1967 int i;
1968
1969 /* find start of trailing white space */
1970 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1971 {
1972 if (State & REPLACE_FLAG)
1973 replace_join(0); /* remove a NUL from the replace stack */
1974 }
1975 line[i + 1] = NUL;
1976}
1977
1978#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1979 || defined(FEAT_COMMENTS) || defined(PROTO)
1980/*
1981 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1982 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001983 * Will attempt not to go before "col" even when there is a composing
1984 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001985 */
1986 void
1987backspace_until_column(col)
1988 int col;
1989{
1990 while ((int)curwin->w_cursor.col > col)
1991 {
1992 curwin->w_cursor.col--;
1993 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001994 replace_do_bs(col);
1995 else if (!del_char_after_col(col))
1996 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997 }
1998}
1999#endif
2000
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002001/*
2002 * Like del_char(), but make sure not to go before column "limit_col".
2003 * Only matters when there are composing characters.
2004 * Return TRUE when something was deleted.
2005 */
2006 static int
2007del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002008 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002009{
2010#ifdef FEAT_MBYTE
2011 if (enc_utf8 && limit_col >= 0)
2012 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002013 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002014
2015 /* Make sure the cursor is at the start of a character, but
2016 * skip forward again when going too far back because of a
2017 * composing character. */
2018 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002019 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002020 {
2021 int l = utf_ptr2len(ml_get_cursor());
2022
2023 if (l == 0) /* end of line */
2024 break;
2025 curwin->w_cursor.col += l;
2026 }
2027 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2028 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002029 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002030 }
2031 else
2032#endif
2033 (void)del_char(FALSE);
2034 return TRUE;
2035}
2036
Bram Moolenaar071d4272004-06-13 20:20:40 +00002037#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2038/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002039 * CTRL-X pressed in Insert mode.
2040 */
2041 static void
2042ins_ctrl_x()
2043{
2044 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2045 * CTRL-V works like CTRL-N */
2046 if (ctrl_x_mode != CTRL_X_CMDLINE)
2047 {
2048 /* if the next ^X<> won't ADD nothing, then reset
2049 * compl_cont_status */
2050 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002051 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002052 else
2053 compl_cont_status = 0;
2054 /* We're not sure which CTRL-X mode it will be yet */
2055 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2056 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2057 edit_submode_pre = NULL;
2058 showmode();
2059 }
2060}
2061
2062/*
2063 * Return TRUE if the 'dict' or 'tsr' option can be used.
2064 */
2065 static int
2066has_compl_option(dict_opt)
2067 int dict_opt;
2068{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002069 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002070# ifdef FEAT_SPELL
2071 && !curwin->w_p_spell
2072# endif
2073 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002074 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2075 {
2076 ctrl_x_mode = 0;
2077 edit_submode = NULL;
2078 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2079 : (char_u *)_("'thesaurus' option is empty"),
2080 hl_attr(HLF_E));
2081 if (emsg_silent == 0)
2082 {
2083 vim_beep();
2084 setcursor();
2085 out_flush();
2086 ui_delay(2000L, FALSE);
2087 }
2088 return FALSE;
2089 }
2090 return TRUE;
2091}
2092
2093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002094 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2095 * This depends on the current mode.
2096 */
2097 int
2098vim_is_ctrl_x_key(c)
2099 int c;
2100{
2101 /* Always allow ^R - let it's results then be checked */
2102 if (c == Ctrl_R)
2103 return TRUE;
2104
Bram Moolenaare3226be2005-12-18 22:10:00 +00002105 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002106 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002107 return TRUE;
2108
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 switch (ctrl_x_mode)
2110 {
2111 case 0: /* Not in any CTRL-X mode */
2112 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2113 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002114 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2116 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2117 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002118 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2119 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002120 case CTRL_X_SCROLL:
2121 return (c == Ctrl_Y || c == Ctrl_E);
2122 case CTRL_X_WHOLE_LINE:
2123 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2124 case CTRL_X_FILES:
2125 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2126 case CTRL_X_DICTIONARY:
2127 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2128 case CTRL_X_THESAURUS:
2129 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2130 case CTRL_X_TAGS:
2131 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2132#ifdef FEAT_FIND_ID
2133 case CTRL_X_PATH_PATTERNS:
2134 return (c == Ctrl_P || c == Ctrl_N);
2135 case CTRL_X_PATH_DEFINES:
2136 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2137#endif
2138 case CTRL_X_CMDLINE:
2139 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2140 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002141#ifdef FEAT_COMPL_FUNC
2142 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002143 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002144 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002145 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002146#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002147 case CTRL_X_SPELL:
2148 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002149 }
2150 EMSG(_(e_internal));
2151 return FALSE;
2152}
2153
2154/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002155 * Return TRUE when character "c" is part of the item currently being
2156 * completed. Used to decide whether to abandon complete mode when the menu
2157 * is visible.
2158 */
2159 static int
2160ins_compl_accept_char(c)
2161 int c;
2162{
2163 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2164 /* When expanding an identifier only accept identifier chars. */
2165 return vim_isIDc(c);
2166
2167 switch (ctrl_x_mode)
2168 {
2169 case CTRL_X_FILES:
2170 /* When expanding file name only accept file name chars. But not
2171 * path separators, so that "proto/<Tab>" expands files in
2172 * "proto", not "proto/" as a whole */
2173 return vim_isfilec(c) && !vim_ispathsep(c);
2174
2175 case CTRL_X_CMDLINE:
2176 case CTRL_X_OMNI:
2177 /* Command line and Omni completion can work with just about any
2178 * printable character, but do stop at white space. */
2179 return vim_isprintc(c) && !vim_iswhite(c);
2180
2181 case CTRL_X_WHOLE_LINE:
2182 /* For while line completion a space can be part of the line. */
2183 return vim_isprintc(c);
2184 }
2185 return vim_iswordc(c);
2186}
2187
2188/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002189 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002191 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 * the rest of the word to be in -- webb
2193 */
2194 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002195ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 char_u *str;
2197 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002198 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 char_u *fname;
2200 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002201 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002203 char_u *p;
2204 int i, c;
2205 int actual_len; /* Take multi-byte characters */
2206 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002207 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002208 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002209 int has_lower = FALSE;
2210 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002212 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002214 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215
Bram Moolenaara2993e12007-08-12 14:38:46 +00002216 /* Find actual length of completion. */
2217#ifdef FEAT_MBYTE
2218 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002220 p = str;
2221 actual_len = 0;
2222 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002224 mb_ptr_adv(p);
2225 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002226 }
2227 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002228 else
2229#endif
2230 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
Bram Moolenaara2993e12007-08-12 14:38:46 +00002232 /* Find actual length of original text. */
2233#ifdef FEAT_MBYTE
2234 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002236 p = compl_orig_text;
2237 actual_compl_length = 0;
2238 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002240 mb_ptr_adv(p);
2241 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002242 }
2243 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002244 else
2245#endif
2246 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002248 /* "actual_len" may be smaller than "actual_compl_length" when using
2249 * thesaurus, only use the minimum when comparing. */
2250 min_len = actual_len < actual_compl_length
2251 ? actual_len : actual_compl_length;
2252
Bram Moolenaara2993e12007-08-12 14:38:46 +00002253 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002254 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002255 if (wca != NULL)
2256 {
2257 p = str;
2258 for (i = 0; i < actual_len; ++i)
2259#ifdef FEAT_MBYTE
2260 if (has_mbyte)
2261 wca[i] = mb_ptr2char_adv(&p);
2262 else
2263#endif
2264 wca[i] = *(p++);
2265
2266 /* Rule 1: Were any chars converted to lower? */
2267 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002268 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002269 {
2270#ifdef FEAT_MBYTE
2271 if (has_mbyte)
2272 c = mb_ptr2char_adv(&p);
2273 else
2274#endif
2275 c = *(p++);
2276 if (MB_ISLOWER(c))
2277 {
2278 has_lower = TRUE;
2279 if (MB_ISUPPER(wca[i]))
2280 {
2281 /* Rule 1 is satisfied. */
2282 for (i = actual_compl_length; i < actual_len; ++i)
2283 wca[i] = MB_TOLOWER(wca[i]);
2284 break;
2285 }
2286 }
2287 }
2288
2289 /*
2290 * Rule 2: No lower case, 2nd consecutive letter converted to
2291 * upper case.
2292 */
2293 if (!has_lower)
2294 {
2295 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002296 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002297 {
2298#ifdef FEAT_MBYTE
2299 if (has_mbyte)
2300 c = mb_ptr2char_adv(&p);
2301 else
2302#endif
2303 c = *(p++);
2304 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2305 {
2306 /* Rule 2 is satisfied. */
2307 for (i = actual_compl_length; i < actual_len; ++i)
2308 wca[i] = MB_TOUPPER(wca[i]);
2309 break;
2310 }
2311 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2312 }
2313 }
2314
2315 /* Copy the original case of the part we typed. */
2316 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002317 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002318 {
2319#ifdef FEAT_MBYTE
2320 if (has_mbyte)
2321 c = mb_ptr2char_adv(&p);
2322 else
2323#endif
2324 c = *(p++);
2325 if (MB_ISLOWER(c))
2326 wca[i] = MB_TOLOWER(wca[i]);
2327 else if (MB_ISUPPER(c))
2328 wca[i] = MB_TOUPPER(wca[i]);
2329 }
2330
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002331 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002332 * Generate encoding specific output from wide character array.
2333 * Multi-byte characters can occupy up to five bytes more than
2334 * ASCII characters, and we also need one byte for NUL, so stay
2335 * six bytes away from the edge of IObuff.
2336 */
2337 p = IObuff;
2338 i = 0;
2339 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2340#ifdef FEAT_MBYTE
2341 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002342 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002343 else
2344#endif
2345 *(p++) = wca[i++];
2346 *p = NUL;
2347
2348 vim_free(wca);
2349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002350
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002351 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2352 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002353 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002354 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355}
2356
2357/*
2358 * Add a match to the list of matches.
2359 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002360 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002361 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002363 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002364ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 char_u *str;
2366 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002367 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002369 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002370 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002371 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002372 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002374 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002375 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376
2377 ui_breakcheck();
2378 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002379 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002380 if (len < 0)
2381 len = (int)STRLEN(str);
2382
2383 /*
2384 * If the same match is already present, don't add it.
2385 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002386 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002387 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002388 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 do
2390 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002391 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002392 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002393 && match->cp_str[len] == NUL)
2394 return NOTDONE;
2395 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002396 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397 }
2398
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002399 /* Remove any popup menu before changing the list of matches. */
2400 ins_compl_del_pum();
2401
Bram Moolenaar071d4272004-06-13 20:20:40 +00002402 /*
2403 * Allocate a new match structure.
2404 * Copy the values to the new match structure.
2405 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002406 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002408 return FAIL;
2409 match->cp_number = -1;
2410 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002411 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002412 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002413 {
2414 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002415 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002416 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002417 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002418
Bram Moolenaar071d4272004-06-13 20:20:40 +00002419 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002420 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2422 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002423 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002424 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002425 && compl_curr_match->cp_fname != NULL
2426 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002427 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002428 else if (fname != NULL)
2429 {
2430 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002431 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002432 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002434 match->cp_fname = NULL;
2435 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002436
2437 if (cptext != NULL)
2438 {
2439 int i;
2440
2441 for (i = 0; i < CPT_COUNT; ++i)
2442 if (cptext[i] != NULL && *cptext[i] != NUL)
2443 match->cp_text[i] = vim_strsave(cptext[i]);
2444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002445
2446 /*
2447 * Link the new match structure in the list of matches.
2448 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002449 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002450 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002451 else if (dir == FORWARD)
2452 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002453 match->cp_next = compl_curr_match->cp_next;
2454 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002455 }
2456 else /* BACKWARD */
2457 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002458 match->cp_next = compl_curr_match;
2459 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002460 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002461 if (match->cp_next)
2462 match->cp_next->cp_prev = match;
2463 if (match->cp_prev)
2464 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002466 compl_first_match = match;
2467 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002468
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002469 /*
2470 * Find the longest common string if still doing that.
2471 */
2472 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2473 ins_compl_longest_match(match);
2474
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 return OK;
2476}
2477
2478/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002479 * Return TRUE if "str[len]" matches with match->cp_str, considering
2480 * match->cp_icase.
2481 */
2482 static int
2483ins_compl_equal(match, str, len)
2484 compl_T *match;
2485 char_u *str;
2486 int len;
2487{
2488 if (match->cp_icase)
2489 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2490 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2491}
2492
2493/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002494 * Reduce the longest common string for match "match".
2495 */
2496 static void
2497ins_compl_longest_match(match)
2498 compl_T *match;
2499{
2500 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002501 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002502 int had_match;
2503
2504 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002505 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002506 /* First match, use it as a whole. */
2507 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002508 if (compl_leader != NULL)
2509 {
2510 had_match = (curwin->w_cursor.col > compl_col);
2511 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002512 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002513 ins_redraw(FALSE);
2514
2515 /* When the match isn't there (to avoid matching itself) remove it
2516 * again after redrawing. */
2517 if (!had_match)
2518 ins_compl_delete();
2519 compl_used_match = FALSE;
2520 }
2521 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002522 else
2523 {
2524 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002525 p = compl_leader;
2526 s = match->cp_str;
2527 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002528 {
2529#ifdef FEAT_MBYTE
2530 if (has_mbyte)
2531 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002532 c1 = mb_ptr2char(p);
2533 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002534 }
2535 else
2536#endif
2537 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002538 c1 = *p;
2539 c2 = *s;
2540 }
2541 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2542 : (c1 != c2))
2543 break;
2544#ifdef FEAT_MBYTE
2545 if (has_mbyte)
2546 {
2547 mb_ptr_adv(p);
2548 mb_ptr_adv(s);
2549 }
2550 else
2551#endif
2552 {
2553 ++p;
2554 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002555 }
2556 }
2557
2558 if (*p != NUL)
2559 {
2560 /* Leader was shortened, need to change the inserted text. */
2561 *p = NUL;
2562 had_match = (curwin->w_cursor.col > compl_col);
2563 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002564 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002565 ins_redraw(FALSE);
2566
2567 /* When the match isn't there (to avoid matching itself) remove it
2568 * again after redrawing. */
2569 if (!had_match)
2570 ins_compl_delete();
2571 }
2572
2573 compl_used_match = FALSE;
2574 }
2575}
2576
2577/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 * Add an array of matches to the list of matches.
2579 * Frees matches[].
2580 */
2581 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002582ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002583 int num_matches;
2584 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002585 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002586{
2587 int i;
2588 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002589 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590
Bram Moolenaar572cb562005-08-05 21:35:02 +00002591 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002592 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002593 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002595 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 FreeWild(num_matches, matches);
2597}
2598
2599/* Make the completion list cyclic.
2600 * Return the number of matches (excluding the original).
2601 */
2602 static int
2603ins_compl_make_cyclic()
2604{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002605 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606 int count = 0;
2607
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002608 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002609 {
2610 /*
2611 * Find the end of the list.
2612 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002613 match = compl_first_match;
2614 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002615 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002617 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 ++count;
2619 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002620 match->cp_next = compl_first_match;
2621 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002622 }
2623 return count;
2624}
2625
Bram Moolenaara94bc432006-03-10 21:42:59 +00002626/*
2627 * Start completion for the complete() function.
2628 * "startcol" is where the matched text starts (1 is first column).
2629 * "list" is the list of matches.
2630 */
2631 void
2632set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002633 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002634 list_T *list;
2635{
2636 /* If already doing completions stop it. */
2637 if (ctrl_x_mode != 0)
2638 ins_compl_prep(' ');
2639 ins_compl_clear();
2640
2641 if (stop_arrow() == FAIL)
2642 return;
2643
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002644 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002645 startcol = curwin->w_cursor.col;
2646 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002647 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002648 /* compl_pattern doesn't need to be set */
2649 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2650 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002651 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002652 return;
2653
2654 /* Handle like dictionary completion. */
2655 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2656
2657 ins_compl_add_list(list);
2658 compl_matches = ins_compl_make_cyclic();
2659 compl_started = TRUE;
2660 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002661 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002662
2663 compl_curr_match = compl_first_match;
2664 ins_complete(Ctrl_N);
2665 out_flush();
2666}
2667
2668
Bram Moolenaar9372a112005-12-06 19:59:18 +00002669/* "compl_match_array" points the currently displayed list of entries in the
2670 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002671static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002672static int compl_match_arraysize;
2673
2674/*
2675 * Update the screen and when there is any scrolling remove the popup menu.
2676 */
2677 static void
2678ins_compl_upd_pum()
2679{
2680 int h;
2681
2682 if (compl_match_array != NULL)
2683 {
2684 h = curwin->w_cline_height;
2685 update_screen(0);
2686 if (h != curwin->w_cline_height)
2687 ins_compl_del_pum();
2688 }
2689}
2690
2691/*
2692 * Remove any popup menu.
2693 */
2694 static void
2695ins_compl_del_pum()
2696{
2697 if (compl_match_array != NULL)
2698 {
2699 pum_undisplay();
2700 vim_free(compl_match_array);
2701 compl_match_array = NULL;
2702 }
2703}
2704
2705/*
2706 * Return TRUE if the popup menu should be displayed.
2707 */
2708 static int
2709pum_wanted()
2710{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002711 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002712 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002713 return FALSE;
2714
2715 /* The display looks bad on a B&W display. */
2716 if (t_colors < 8
2717#ifdef FEAT_GUI
2718 && !gui.in_use
2719#endif
2720 )
2721 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002722 return TRUE;
2723}
2724
2725/*
2726 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002727 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002728 */
2729 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002730pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002731{
2732 compl_T *compl;
2733 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002734
2735 /* Don't display the popup menu if there are no matches or there is only
2736 * one (ignoring the original text). */
2737 compl = compl_first_match;
2738 i = 0;
2739 do
2740 {
2741 if (compl == NULL
2742 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2743 break;
2744 compl = compl->cp_next;
2745 } while (compl != compl_first_match);
2746
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002747 if (strstr((char *)p_cot, "menuone") != NULL)
2748 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002749 return (i >= 2);
2750}
2751
2752/*
2753 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002754 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002755 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002756 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002757ins_compl_show_pum()
2758{
2759 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002760 compl_T *shown_compl = NULL;
2761 int did_find_shown_match = FALSE;
2762 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002763 int i;
2764 int cur = -1;
2765 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002766 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002767
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002768 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002769 return;
2770
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002771#if defined(FEAT_EVAL)
2772 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2773 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2774#endif
2775
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002776 /* Update the screen before drawing the popup menu over it. */
2777 update_screen(0);
2778
2779 if (compl_match_array == NULL)
2780 {
2781 /* Need to build the popup menu list. */
2782 compl_match_arraysize = 0;
2783 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002784 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002785 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002786 do
2787 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002788 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2789 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002790 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002791 ++compl_match_arraysize;
2792 compl = compl->cp_next;
2793 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002794 if (compl_match_arraysize == 0)
2795 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002796 compl_match_array = (pumitem_T *)alloc_clear(
2797 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002798 * compl_match_arraysize));
2799 if (compl_match_array != NULL)
2800 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002801 /* If the current match is the original text don't find the first
2802 * match after it, don't highlight anything. */
2803 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2804 shown_match_ok = TRUE;
2805
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002806 i = 0;
2807 compl = compl_first_match;
2808 do
2809 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002810 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2811 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002812 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002813 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002814 if (!shown_match_ok)
2815 {
2816 if (compl == compl_shown_match || did_find_shown_match)
2817 {
2818 /* This item is the shown match or this is the
2819 * first displayed item after the shown match. */
2820 compl_shown_match = compl;
2821 did_find_shown_match = TRUE;
2822 shown_match_ok = TRUE;
2823 }
2824 else
2825 /* Remember this displayed match for when the
2826 * shown match is just below it. */
2827 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002828 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002829 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002830
2831 if (compl->cp_text[CPT_ABBR] != NULL)
2832 compl_match_array[i].pum_text =
2833 compl->cp_text[CPT_ABBR];
2834 else
2835 compl_match_array[i].pum_text = compl->cp_str;
2836 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2837 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2838 if (compl->cp_text[CPT_MENU] != NULL)
2839 compl_match_array[i++].pum_extra =
2840 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002841 else
2842 compl_match_array[i++].pum_extra = compl->cp_fname;
2843 }
2844
2845 if (compl == compl_shown_match)
2846 {
2847 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002848
2849 /* When the original text is the shown match don't set
2850 * compl_shown_match. */
2851 if (compl->cp_flags & ORIGINAL_TEXT)
2852 shown_match_ok = TRUE;
2853
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002854 if (!shown_match_ok && shown_compl != NULL)
2855 {
2856 /* The shown match isn't displayed, set it to the
2857 * previously displayed match. */
2858 compl_shown_match = shown_compl;
2859 shown_match_ok = TRUE;
2860 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002861 }
2862 compl = compl->cp_next;
2863 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002864
2865 if (!shown_match_ok) /* no displayed match at all */
2866 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002867 }
2868 }
2869 else
2870 {
2871 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002872 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002873 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2874 || compl_match_array[i].pum_text
2875 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002876 {
2877 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002878 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002879 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002880 }
2881
2882 if (compl_match_array != NULL)
2883 {
2884 /* Compute the screen column of the start of the completed text.
2885 * Use the cursor to get all wrapping and other settings right. */
2886 col = curwin->w_cursor.col;
2887 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002888 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002889 curwin->w_cursor.col = col;
2890 }
2891}
2892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893#define DICT_FIRST (1) /* use just first element in "dict" */
2894#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002895
Bram Moolenaar071d4272004-06-13 20:20:40 +00002896/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002897 * Add any identifiers that match the given pattern in the list of dictionary
2898 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002899 */
2900 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002901ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2902 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002903 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002904 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002905 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002906{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002907 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908 char_u *ptr;
2909 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002911 char_u **files;
2912 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002914 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915
Bram Moolenaar0b238792006-03-02 22:49:12 +00002916 if (*dict == NUL)
2917 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002918#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002919 /* When 'dictionary' is empty and spell checking is enabled use
2920 * "spell". */
2921 if (!thesaurus && curwin->w_p_spell)
2922 dict = (char_u *)"spell";
2923 else
2924#endif
2925 return;
2926 }
2927
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002929 if (buf == NULL)
2930 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002931 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002932
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 /* If 'infercase' is set, don't use 'smartcase' here */
2934 save_p_scs = p_scs;
2935 if (curbuf->b_p_inf)
2936 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002937
2938 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2939 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002940 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002941 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2942 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002943 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002944 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002945
2946 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002947 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002948 len = STRLEN(pat_esc) + 10;
2949 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002950 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002951 {
2952 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002953 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002954 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002955 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002956 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2957 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002958 vim_free(ptr);
2959 }
2960 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002961 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002962 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002963 if (regmatch.regprog == NULL)
2964 goto theend;
2965 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002966
Bram Moolenaar071d4272004-06-13 20:20:40 +00002967 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2968 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002969 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002970 {
2971 /* copy one dictionary file name into buf */
2972 if (flags == DICT_EXACT)
2973 {
2974 count = 1;
2975 files = &dict;
2976 }
2977 else
2978 {
2979 /* Expand wildcards in the dictionary name, but do not allow
2980 * backticks (for security, the 'dict' option may have been set in
2981 * a modeline). */
2982 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002983# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002984 if (!thesaurus && STRCMP(buf, "spell") == 0)
2985 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002986 else
2987# endif
2988 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 || expand_wildcards(1, &buf, &count, &files,
2990 EW_FILE|EW_SILENT) != OK)
2991 count = 0;
2992 }
2993
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002994# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002995 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002996 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002997 /* Complete from active spelling. Skip "\<" in the pattern, we
2998 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002999 if (pat[0] == '\\' && pat[1] == '<')
3000 ptr = pat + 2;
3001 else
3002 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003003 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003004 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003005 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003006# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003007 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003008 {
3009 ins_compl_files(count, files, thesaurus, flags,
3010 &regmatch, buf, &dir);
3011 if (flags != DICT_EXACT)
3012 FreeWild(count, files);
3013 }
3014 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003015 break;
3016 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003017
3018theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003019 p_scs = save_p_scs;
3020 vim_free(regmatch.regprog);
3021 vim_free(buf);
3022}
3023
Bram Moolenaar0b238792006-03-02 22:49:12 +00003024 static void
3025ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3026 int count;
3027 char_u **files;
3028 int thesaurus;
3029 int flags;
3030 regmatch_T *regmatch;
3031 char_u *buf;
3032 int *dir;
3033{
3034 char_u *ptr;
3035 int i;
3036 FILE *fp;
3037 int add_r;
3038
3039 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3040 {
3041 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3042 if (flags != DICT_EXACT)
3043 {
3044 vim_snprintf((char *)IObuff, IOSIZE,
3045 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003046 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003047 }
3048
3049 if (fp != NULL)
3050 {
3051 /*
3052 * Read dictionary file line by line.
3053 * Check each line for a match.
3054 */
3055 while (!got_int && !compl_interrupted
3056 && !vim_fgets(buf, LSIZE, fp))
3057 {
3058 ptr = buf;
3059 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3060 {
3061 ptr = regmatch->startp[0];
3062 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3063 ptr = find_line_end(ptr);
3064 else
3065 ptr = find_word_end(ptr);
3066 add_r = ins_compl_add_infercase(regmatch->startp[0],
3067 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003068 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003069 if (thesaurus)
3070 {
3071 char_u *wstart;
3072
3073 /*
3074 * Add the other matches on the line
3075 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003076 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003077 while (!got_int)
3078 {
3079 /* Find start of the next word. Skip white
3080 * space and punctuation. */
3081 ptr = find_word_start(ptr);
3082 if (*ptr == NUL || *ptr == NL)
3083 break;
3084 wstart = ptr;
3085
Bram Moolenaara2993e12007-08-12 14:38:46 +00003086 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003087#ifdef FEAT_MBYTE
3088 if (has_mbyte)
3089 /* Japanese words may have characters in
3090 * different classes, only separate words
3091 * with single-byte non-word characters. */
3092 while (*ptr != NUL)
3093 {
3094 int l = (*mb_ptr2len)(ptr);
3095
3096 if (l < 2 && !vim_iswordc(*ptr))
3097 break;
3098 ptr += l;
3099 }
3100 else
3101#endif
3102 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003103
3104 /* Add the word. Skip the regexp match. */
3105 if (wstart != regmatch->startp[0])
3106 add_r = ins_compl_add_infercase(wstart,
3107 (int)(ptr - wstart),
3108 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003109 }
3110 }
3111 if (add_r == OK)
3112 /* if dir was BACKWARD then honor it just once */
3113 *dir = FORWARD;
3114 else if (add_r == FAIL)
3115 break;
3116 /* avoid expensive call to vim_regexec() when at end
3117 * of line */
3118 if (*ptr == '\n' || got_int)
3119 break;
3120 }
3121 line_breakcheck();
3122 ins_compl_check_keys(50);
3123 }
3124 fclose(fp);
3125 }
3126 }
3127}
3128
Bram Moolenaar071d4272004-06-13 20:20:40 +00003129/*
3130 * Find the start of the next word.
3131 * Returns a pointer to the first char of the word. Also stops at a NUL.
3132 */
3133 char_u *
3134find_word_start(ptr)
3135 char_u *ptr;
3136{
3137#ifdef FEAT_MBYTE
3138 if (has_mbyte)
3139 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003140 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003141 else
3142#endif
3143 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3144 ++ptr;
3145 return ptr;
3146}
3147
3148/*
3149 * Find the end of the word. Assumes it starts inside a word.
3150 * Returns a pointer to just after the word.
3151 */
3152 char_u *
3153find_word_end(ptr)
3154 char_u *ptr;
3155{
3156#ifdef FEAT_MBYTE
3157 int start_class;
3158
3159 if (has_mbyte)
3160 {
3161 start_class = mb_get_class(ptr);
3162 if (start_class > 1)
3163 while (*ptr != NUL)
3164 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003165 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 if (mb_get_class(ptr) != start_class)
3167 break;
3168 }
3169 }
3170 else
3171#endif
3172 while (vim_iswordc(*ptr))
3173 ++ptr;
3174 return ptr;
3175}
3176
3177/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003178 * Find the end of the line, omitting CR and NL at the end.
3179 * Returns a pointer to just after the line.
3180 */
3181 static char_u *
3182find_line_end(ptr)
3183 char_u *ptr;
3184{
3185 char_u *s;
3186
3187 s = ptr + STRLEN(ptr);
3188 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3189 --s;
3190 return s;
3191}
3192
3193/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003194 * Free the list of completions
3195 */
3196 static void
3197ins_compl_free()
3198{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003199 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003200 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003201
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003202 vim_free(compl_pattern);
3203 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003204 vim_free(compl_leader);
3205 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003206
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003207 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003209
3210 ins_compl_del_pum();
3211 pum_clear();
3212
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003213 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 do
3215 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003216 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003217 compl_curr_match = compl_curr_match->cp_next;
3218 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003219 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003220 if (match->cp_flags & FREE_FNAME)
3221 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003222 for (i = 0; i < CPT_COUNT; ++i)
3223 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003224 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003225 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3226 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003227 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228}
3229
3230 static void
3231ins_compl_clear()
3232{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003233 compl_cont_status = 0;
3234 compl_started = FALSE;
3235 compl_matches = 0;
3236 vim_free(compl_pattern);
3237 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003238 vim_free(compl_leader);
3239 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003240 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003241 vim_free(compl_orig_text);
3242 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003243 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244}
3245
3246/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003247 * Return TRUE when Insert completion is active.
3248 */
3249 int
3250ins_compl_active()
3251{
3252 return compl_started;
3253}
3254
3255/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003256 * Delete one character before the cursor and show the subset of the matches
3257 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003258 * Returns the character to be used, NUL if the work is done and another char
3259 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003260 */
3261 static int
3262ins_compl_bs()
3263{
3264 char_u *line;
3265 char_u *p;
3266
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003267 line = ml_get_curline();
3268 p = line + curwin->w_cursor.col;
3269 mb_ptr_back(line, p);
3270
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003271 /* Stop completion when the whole word was deleted. For Omni completion
3272 * allow the word to be deleted, we won't match everything. */
3273 if ((int)(p - line) - (int)compl_col < 0
3274 || ((int)(p - line) - (int)compl_col == 0
3275 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003276 return K_BS;
3277
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003278 /* Deleted more than what was used to find matches or didn't finish
3279 * finding all matches: need to look for matches all over again. */
3280 if (curwin->w_cursor.col <= compl_col + compl_length
3281 || compl_was_interrupted)
3282 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003283
Bram Moolenaara6557602006-02-04 22:43:20 +00003284 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003285 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003286 if (compl_leader != NULL)
3287 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003288 ins_compl_new_leader();
3289 return NUL;
3290 }
3291 return K_BS;
3292}
Bram Moolenaara6557602006-02-04 22:43:20 +00003293
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003294/*
3295 * Called after changing "compl_leader".
3296 * Show the popup menu with a different set of matches.
3297 * May also search for matches again if the previous search was interrupted.
3298 */
3299 static void
3300ins_compl_new_leader()
3301{
3302 ins_compl_del_pum();
3303 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003304 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003305 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003306
3307 if (compl_started)
3308 ins_compl_set_original_text(compl_leader);
3309 else
3310 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003311#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003312 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003313#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003314 /*
3315 * Matches were cleared, need to search for them now. First display
3316 * the changed text before the cursor. Set "compl_restarting" to
3317 * avoid that the first match is inserted.
3318 */
3319 update_screen(0);
3320#ifdef FEAT_GUI
3321 if (gui.in_use)
3322 {
3323 /* Show the cursor after the match, not after the redrawn text. */
3324 setcursor();
3325 out_flush();
3326 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003327 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003328#endif
3329 compl_restarting = TRUE;
3330 if (ins_complete(Ctrl_N) == FAIL)
3331 compl_cont_status = 0;
3332 compl_restarting = FALSE;
3333 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003334
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003335#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003336 if (!compl_used_match)
3337 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003338 /* Go to the original text, since none of the matches is inserted. */
3339 if (compl_first_match->cp_prev != NULL
3340 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3341 compl_shown_match = compl_first_match->cp_prev;
3342 else
3343 compl_shown_match = compl_first_match;
3344 compl_curr_match = compl_shown_match;
3345 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003346 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003347#endif
3348 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003349
3350 /* Show the popup menu with a different set of matches. */
3351 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003352
3353 /* Don't let Enter select the original text when there is no popup menu. */
3354 if (compl_match_array == NULL)
3355 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003356}
3357
3358/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003359 * Return the length of the completion, from the completion start column to
3360 * the cursor column. Making sure it never goes below zero.
3361 */
3362 static int
3363ins_compl_len()
3364{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003365 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003366
3367 if (off < 0)
3368 return 0;
3369 return off;
3370}
3371
3372/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003373 * Append one character to the match leader. May reduce the number of
3374 * matches.
3375 */
3376 static void
3377ins_compl_addleader(c)
3378 int c;
3379{
3380#ifdef FEAT_MBYTE
3381 int cc;
3382
3383 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3384 {
3385 char_u buf[MB_MAXBYTES + 1];
3386
3387 (*mb_char2bytes)(c, buf);
3388 buf[cc] = NUL;
3389 ins_char_bytes(buf, cc);
3390 }
3391 else
3392#endif
3393 ins_char(c);
3394
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003395 /* If we didn't complete finding matches we must search again. */
3396 if (compl_was_interrupted)
3397 ins_compl_restart();
3398
Bram Moolenaara6557602006-02-04 22:43:20 +00003399 vim_free(compl_leader);
3400 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003401 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003402 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003403 ins_compl_new_leader();
3404}
3405
3406/*
3407 * Setup for finding completions again without leaving CTRL-X mode. Used when
3408 * BS or a key was typed while still searching for matches.
3409 */
3410 static void
3411ins_compl_restart()
3412{
3413 ins_compl_free();
3414 compl_started = FALSE;
3415 compl_matches = 0;
3416 compl_cont_status = 0;
3417 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003418}
3419
3420/*
3421 * Set the first match, the original text.
3422 */
3423 static void
3424ins_compl_set_original_text(str)
3425 char_u *str;
3426{
3427 char_u *p;
3428
3429 /* Replace the original text entry. */
3430 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3431 {
3432 p = vim_strsave(str);
3433 if (p != NULL)
3434 {
3435 vim_free(compl_first_match->cp_str);
3436 compl_first_match->cp_str = p;
3437 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003438 }
3439}
3440
3441/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003442 * Append one character to the match leader. May reduce the number of
3443 * matches.
3444 */
3445 static void
3446ins_compl_addfrommatch()
3447{
3448 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003449 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003450 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003451 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003452
3453 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003454 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003455 {
3456 /* When still at the original match use the first entry that matches
3457 * the leader. */
3458 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3459 {
3460 p = NULL;
3461 for (cp = compl_shown_match->cp_next; cp != NULL
3462 && cp != compl_first_match; cp = cp->cp_next)
3463 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003464 if (compl_leader == NULL
3465 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003466 (int)STRLEN(compl_leader)))
3467 {
3468 p = cp->cp_str;
3469 break;
3470 }
3471 }
3472 if (p == NULL || (int)STRLEN(p) <= len)
3473 return;
3474 }
3475 else
3476 return;
3477 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003478 p += len;
3479#ifdef FEAT_MBYTE
3480 c = mb_ptr2char(p);
3481#else
3482 c = *p;
3483#endif
3484 ins_compl_addleader(c);
3485}
3486
3487/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003488 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003489 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003490 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003492 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493ins_compl_prep(c)
3494 int c;
3495{
3496 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003498 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
3500 /* Forget any previous 'special' messages if this is actually
3501 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3502 */
3503 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3504 edit_submode_extra = NULL;
3505
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003506 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3507 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003508 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003509
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003510 /* Set "compl_get_longest" when finding the first matches. */
3511 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3512 || (ctrl_x_mode == 0 && !compl_started))
3513 {
3514 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3515 compl_used_match = TRUE;
3516 }
3517
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3519 {
3520 /*
3521 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3522 * it will be yet. Now we decide.
3523 */
3524 switch (c)
3525 {
3526 case Ctrl_E:
3527 case Ctrl_Y:
3528 ctrl_x_mode = CTRL_X_SCROLL;
3529 if (!(State & REPLACE_FLAG))
3530 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3531 else
3532 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3533 edit_submode_pre = NULL;
3534 showmode();
3535 break;
3536 case Ctrl_L:
3537 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3538 break;
3539 case Ctrl_F:
3540 ctrl_x_mode = CTRL_X_FILES;
3541 break;
3542 case Ctrl_K:
3543 ctrl_x_mode = CTRL_X_DICTIONARY;
3544 break;
3545 case Ctrl_R:
3546 /* Simply allow ^R to happen without affecting ^X mode */
3547 break;
3548 case Ctrl_T:
3549 ctrl_x_mode = CTRL_X_THESAURUS;
3550 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003551#ifdef FEAT_COMPL_FUNC
3552 case Ctrl_U:
3553 ctrl_x_mode = CTRL_X_FUNCTION;
3554 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003555 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003556 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003557 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003558#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003559 case 's':
3560 case Ctrl_S:
3561 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003562#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003563 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003564 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003565 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003566#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003567 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 case Ctrl_RSB:
3569 ctrl_x_mode = CTRL_X_TAGS;
3570 break;
3571#ifdef FEAT_FIND_ID
3572 case Ctrl_I:
3573 case K_S_TAB:
3574 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3575 break;
3576 case Ctrl_D:
3577 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3578 break;
3579#endif
3580 case Ctrl_V:
3581 case Ctrl_Q:
3582 ctrl_x_mode = CTRL_X_CMDLINE;
3583 break;
3584 case Ctrl_P:
3585 case Ctrl_N:
3586 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3587 * just started ^X mode, or there were enough ^X's to cancel
3588 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3589 * do normal expansion when interrupting a different mode (say
3590 * ^X^F^X^P or ^P^X^X^P, see below)
3591 * nothing changes if interrupting mode 0, (eg, the flag
3592 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003593 if (!(compl_cont_status & CONT_INTRPT))
3594 compl_cont_status |= CONT_LOCAL;
3595 else if (compl_cont_mode != 0)
3596 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 /* FALLTHROUGH */
3598 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003599 /* If we have typed at least 2 ^X's... for modes != 0, we set
3600 * compl_cont_status = 0 (eg, as if we had just started ^X
3601 * mode).
3602 * For mode 0, we set "compl_cont_mode" to an impossible
3603 * value, in both cases ^X^X can be used to restart the same
3604 * mode (avoiding ADDING mode).
3605 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3606 * 'complete' and local ^P expansions respectively.
3607 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3608 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 if (c == Ctrl_X)
3610 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003611 if (compl_cont_mode != 0)
3612 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003614 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 }
3616 ctrl_x_mode = 0;
3617 edit_submode = NULL;
3618 showmode();
3619 break;
3620 }
3621 }
3622 else if (ctrl_x_mode != 0)
3623 {
3624 /* We're already in CTRL-X mode, do we stay in it? */
3625 if (!vim_is_ctrl_x_key(c))
3626 {
3627 if (ctrl_x_mode == CTRL_X_SCROLL)
3628 ctrl_x_mode = 0;
3629 else
3630 ctrl_x_mode = CTRL_X_FINISHED;
3631 edit_submode = NULL;
3632 }
3633 showmode();
3634 }
3635
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003636 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 {
3638 /* Show error message from attempted keyword completion (probably
3639 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003640 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003642 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3643 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 || ctrl_x_mode == CTRL_X_FINISHED)
3645 {
3646 /* Get here when we have finished typing a sequence of ^N and
3647 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003648 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003649 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003651 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003652 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003655 * If any of the original typed text has been changed, eg when
3656 * ignorecase is set, we must add back-spaces to the redo
3657 * buffer. We add as few as necessary to delete just the part
3658 * of the original text that has changed.
3659 * When using the longest match, edited the match or used
3660 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003662 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3663 ptr = compl_curr_match->cp_str;
3664 else if (compl_leader != NULL)
3665 ptr = compl_leader;
3666 else
3667 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003668 if (compl_orig_text != NULL)
3669 {
3670 p = compl_orig_text;
3671 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3672 ++temp)
3673 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003674#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003675 if (temp > 0)
3676 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003677#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003678 for (p += temp; *p != NUL; mb_ptr_adv(p))
3679 AppendCharToRedobuff(K_BS);
3680 }
3681 if (ptr != NULL)
3682 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003683 }
3684
3685#ifdef FEAT_CINDENT
3686 want_cindent = (can_cindent && cindent_on());
3687#endif
3688 /*
3689 * When completing whole lines: fix indent for 'cindent'.
3690 * Otherwise, break line if it's too long.
3691 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003692 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 {
3694#ifdef FEAT_CINDENT
3695 /* re-indent the current line */
3696 if (want_cindent)
3697 {
3698 do_c_expr_indent();
3699 want_cindent = FALSE; /* don't do it again */
3700 }
3701#endif
3702 }
3703 else
3704 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003705 int prev_col = curwin->w_cursor.col;
3706
Bram Moolenaar071d4272004-06-13 20:20:40 +00003707 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003708 if (prev_col > 0)
3709 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003710 if (stop_arrow() == OK)
3711 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003712 if (prev_col > 0
3713 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3714 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
3716
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003717 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003718 * the selection without inserting anything. When
3719 * compl_enter_selects is set the Enter key does the same. */
3720 if ((c == Ctrl_Y || (compl_enter_selects
3721 && (c == CAR || c == K_KENTER || c == NL)))
3722 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003723 retval = TRUE;
3724
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003725 /* CTRL-E means completion is Ended, go back to the typed text. */
3726 if (c == Ctrl_E)
3727 {
3728 ins_compl_delete();
3729 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003730 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003731 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003732 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003733 retval = TRUE;
3734 }
3735
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003736 auto_format(FALSE, TRUE);
3737
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003739 compl_started = FALSE;
3740 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741 msg_clr_cmdline(); /* necessary for "noshowmode" */
3742 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003743 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 if (edit_submode != NULL)
3745 {
3746 edit_submode = NULL;
3747 showmode();
3748 }
3749
3750#ifdef FEAT_CINDENT
3751 /*
3752 * Indent now if a key was typed that is in 'cinkeys'.
3753 */
3754 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3755 do_c_expr_indent();
3756#endif
3757 }
3758 }
3759
3760 /* reset continue_* if we left expansion-mode, if we stay they'll be
3761 * (re)set properly in ins_complete() */
3762 if (!vim_is_ctrl_x_key(c))
3763 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003764 compl_cont_status = 0;
3765 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003767
3768 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003769}
3770
3771/*
3772 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3773 * (depending on flag) starting from buf and looking for a non-scanned
3774 * buffer (other than curbuf). curbuf is special, if it is called with
3775 * buf=curbuf then it has to be the first call for a given flag/expansion.
3776 *
3777 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3778 */
3779 static buf_T *
3780ins_compl_next_buf(buf, flag)
3781 buf_T *buf;
3782 int flag;
3783{
3784#ifdef FEAT_WINDOWS
3785 static win_T *wp;
3786#endif
3787
3788 if (flag == 'w') /* just windows */
3789 {
3790#ifdef FEAT_WINDOWS
3791 if (buf == curbuf) /* first call for this flag/expansion */
3792 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003793 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003794 && wp->w_buffer->b_scanned)
3795 ;
3796 buf = wp->w_buffer;
3797#else
3798 buf = curbuf;
3799#endif
3800 }
3801 else
3802 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3803 * (unlisted buffers)
3804 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003805 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003806 && ((flag == 'U'
3807 ? buf->b_p_bl
3808 : (!buf->b_p_bl
3809 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003810 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003811 ;
3812 return buf;
3813}
3814
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003815#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003816static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003817
3818/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003819 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003820 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003821 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003822 static void
3823expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003824 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003825 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003826{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003827 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003828 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003829 char_u *funcname;
3830 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003831
Bram Moolenaare344bea2005-09-01 20:46:49 +00003832 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3833 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003834 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003835
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003836 /* Call 'completefunc' to obtain the list of matches. */
3837 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003838 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003839
Bram Moolenaare344bea2005-09-01 20:46:49 +00003840 pos = curwin->w_cursor;
3841 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3842 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003843 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003844 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003845
Bram Moolenaara94bc432006-03-10 21:42:59 +00003846 ins_compl_add_list(matchlist);
3847 list_unref(matchlist);
3848}
3849#endif /* FEAT_COMPL_FUNC */
3850
Bram Moolenaar39f05632006-03-19 22:15:26 +00003851#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003852/*
3853 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003854 */
3855 static void
3856ins_compl_add_list(list)
3857 list_T *list;
3858{
3859 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003860 int dir = compl_direction;
3861
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003862 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003863 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003864 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003865 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3866 /* if dir was BACKWARD then honor it just once */
3867 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003868 else if (did_emsg)
3869 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003870 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003871}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003872
3873/*
3874 * Add a match to the list of matches from a typeval_T.
3875 * If the given string is already in the list of completions, then return
3876 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3877 * maybe because alloc() returns NULL, then FAIL is returned.
3878 */
3879 int
3880ins_compl_add_tv(tv, dir)
3881 typval_T *tv;
3882 int dir;
3883{
3884 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003885 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003886 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003887 char_u *(cptext[CPT_COUNT]);
3888
3889 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3890 {
3891 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3892 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3893 (char_u *)"abbr", FALSE);
3894 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3895 (char_u *)"menu", FALSE);
3896 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3897 (char_u *)"kind", FALSE);
3898 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3899 (char_u *)"info", FALSE);
3900 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3901 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003902 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003903 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003904 }
3905 else
3906 {
3907 word = get_tv_string_chk(tv);
3908 vim_memset(cptext, 0, sizeof(cptext));
3909 }
3910 if (word == NULL || *word == NUL)
3911 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003912 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003913}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003914#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003915
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003916/*
3917 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003918 * The search starts at position "ini" in curbuf and in the direction
3919 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003920 * When "compl_started" is FALSE start at that position, otherwise continue
3921 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003922 * This may return before finding all the matches.
3923 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 */
3925 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003926ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003927 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003928{
3929 static pos_T first_match_pos;
3930 static pos_T last_match_pos;
3931 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003932 static int found_all = FALSE; /* Found all matches of a
3933 certain type. */
3934 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003935
Bram Moolenaar572cb562005-08-05 21:35:02 +00003936 pos_T *pos;
3937 char_u **matches;
3938 int save_p_scs;
3939 int save_p_ws;
3940 int save_p_ic;
3941 int i;
3942 int num_matches;
3943 int len;
3944 int found_new_match;
3945 int type = ctrl_x_mode;
3946 char_u *ptr;
3947 char_u *dict = NULL;
3948 int dict_f = 0;
3949 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003950
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952 {
3953 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3954 ins_buf->b_scanned = 0;
3955 found_all = FALSE;
3956 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003957 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003958 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 last_match_pos = first_match_pos = *ini;
3960 }
3961
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003962 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003963 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3965 for (;;)
3966 {
3967 found_new_match = FAIL;
3968
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003969 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 * or if found_all says this entry is done. For ^X^L only use the
3971 * entries from 'complete' that look in loaded buffers. */
3972 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003973 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974 {
3975 found_all = FALSE;
3976 while (*e_cpt == ',' || *e_cpt == ' ')
3977 e_cpt++;
3978 if (*e_cpt == '.' && !curbuf->b_scanned)
3979 {
3980 ins_buf = curbuf;
3981 first_match_pos = *ini;
3982 /* So that ^N can match word immediately after cursor */
3983 if (ctrl_x_mode == 0)
3984 dec(&first_match_pos);
3985 last_match_pos = first_match_pos;
3986 type = 0;
3987 }
3988 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3989 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3990 {
3991 /* Scan a buffer, but not the current one. */
3992 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3993 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003994 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 first_match_pos.col = last_match_pos.col = 0;
3996 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3997 last_match_pos.lnum = 0;
3998 type = 0;
3999 }
4000 else /* unloaded buffer, scan like dictionary */
4001 {
4002 found_all = TRUE;
4003 if (ins_buf->b_fname == NULL)
4004 continue;
4005 type = CTRL_X_DICTIONARY;
4006 dict = ins_buf->b_fname;
4007 dict_f = DICT_EXACT;
4008 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004009 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004010 ins_buf->b_fname == NULL
4011 ? buf_spname(ins_buf)
4012 : ins_buf->b_sfname == NULL
4013 ? (char *)ins_buf->b_fname
4014 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004015 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 }
4017 else if (*e_cpt == NUL)
4018 break;
4019 else
4020 {
4021 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4022 type = -1;
4023 else if (*e_cpt == 'k' || *e_cpt == 's')
4024 {
4025 if (*e_cpt == 'k')
4026 type = CTRL_X_DICTIONARY;
4027 else
4028 type = CTRL_X_THESAURUS;
4029 if (*++e_cpt != ',' && *e_cpt != NUL)
4030 {
4031 dict = e_cpt;
4032 dict_f = DICT_FIRST;
4033 }
4034 }
4035#ifdef FEAT_FIND_ID
4036 else if (*e_cpt == 'i')
4037 type = CTRL_X_PATH_PATTERNS;
4038 else if (*e_cpt == 'd')
4039 type = CTRL_X_PATH_DEFINES;
4040#endif
4041 else if (*e_cpt == ']' || *e_cpt == 't')
4042 {
4043 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004044 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004045 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004046 }
4047 else
4048 type = -1;
4049
4050 /* in any case e_cpt is advanced to the next entry */
4051 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4052
4053 found_all = TRUE;
4054 if (type == -1)
4055 continue;
4056 }
4057 }
4058
4059 switch (type)
4060 {
4061 case -1:
4062 break;
4063#ifdef FEAT_FIND_ID
4064 case CTRL_X_PATH_PATTERNS:
4065 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004066 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004067 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004069 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4071 (linenr_T)1, (linenr_T)MAXLNUM);
4072 break;
4073#endif
4074
4075 case CTRL_X_DICTIONARY:
4076 case CTRL_X_THESAURUS:
4077 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004078 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 : (type == CTRL_X_THESAURUS
4080 ? (*curbuf->b_p_tsr == NUL
4081 ? p_tsr
4082 : curbuf->b_p_tsr)
4083 : (*curbuf->b_p_dict == NUL
4084 ? p_dict
4085 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004086 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004087 dict != NULL ? dict_f
4088 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 dict = NULL;
4090 break;
4091
4092 case CTRL_X_TAGS:
4093 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4094 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004095 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004096
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004097 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004098 * of matches is found when compl_pattern is empty */
4099 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4101 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4102 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4103 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004104 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 }
4106 p_ic = save_p_ic;
4107 break;
4108
4109 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004110 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004111 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4112 {
4113
4114 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004115 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004116 ins_compl_add_matches(num_matches, matches,
4117#ifdef CASE_INSENSITIVE_FILENAME
4118 TRUE
4119#else
4120 FALSE
4121#endif
4122 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 }
4124 break;
4125
4126 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127 if (expand_cmdline(&compl_xp, compl_pattern,
4128 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004130 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 break;
4132
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004133#ifdef FEAT_COMPL_FUNC
4134 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004135 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004136 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004137 break;
4138#endif
4139
Bram Moolenaar488c6512005-08-11 20:09:58 +00004140 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004141#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004142 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004143 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004144 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004145 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004146#endif
4147 break;
4148
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 default: /* normal ^P/^N and ^X^L */
4150 /*
4151 * If 'infercase' is set, don't use 'smartcase' here
4152 */
4153 save_p_scs = p_scs;
4154 if (ins_buf->b_p_inf)
4155 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004156
Bram Moolenaar071d4272004-06-13 20:20:40 +00004157 /* buffers other than curbuf are scanned from the beginning or the
4158 * end but never from the middle, thus setting nowrapscan in this
4159 * buffers is a good idea, on the other hand, we always set
4160 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4161 save_p_ws = p_ws;
4162 if (ins_buf != curbuf)
4163 p_ws = FALSE;
4164 else if (*e_cpt == '.')
4165 p_ws = TRUE;
4166 for (;;)
4167 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004168 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004170 ++msg_silent; /* Don't want messages for wrapscan. */
4171
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004172 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4173 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004175 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004177 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004179 found_new_match = searchit(NULL, ins_buf, pos,
4180 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004181 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004182 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004183 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004184 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004186 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004187 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004188 first_match_pos = *pos;
4189 last_match_pos = *pos;
4190 }
4191 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004192 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 found_new_match = FAIL;
4194 if (found_new_match == FAIL)
4195 {
4196 if (ins_buf == curbuf)
4197 found_all = TRUE;
4198 break;
4199 }
4200
4201 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004202 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 && ini->lnum == pos->lnum
4204 && ini->col == pos->col)
4205 continue;
4206 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4207 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4208 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004209 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 {
4211 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4212 continue;
4213 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4214 if (!p_paste)
4215 ptr = skipwhite(ptr);
4216 }
4217 len = (int)STRLEN(ptr);
4218 }
4219 else
4220 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004221 char_u *tmp_ptr = ptr;
4222
4223 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004225 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 /* Skip if already inside a word. */
4227 if (vim_iswordp(tmp_ptr))
4228 continue;
4229 /* Find start of next word. */
4230 tmp_ptr = find_word_start(tmp_ptr);
4231 }
4232 /* Find end of this word. */
4233 tmp_ptr = find_word_end(tmp_ptr);
4234 len = (int)(tmp_ptr - ptr);
4235
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004236 if ((compl_cont_status & CONT_ADDING)
4237 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 {
4239 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4240 {
4241 /* Try next line, if any. the new word will be
4242 * "join" as if the normal command "J" was used.
4243 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004244 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 * works -- Acevedo */
4246 STRNCPY(IObuff, ptr, len);
4247 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4248 tmp_ptr = ptr = skipwhite(ptr);
4249 /* Find start of next word. */
4250 tmp_ptr = find_word_start(tmp_ptr);
4251 /* Find end of next word. */
4252 tmp_ptr = find_word_end(tmp_ptr);
4253 if (tmp_ptr > ptr)
4254 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004255 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004257 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 IObuff[len++] = ' ';
4259 /* IObuf =~ "\k.* ", thus len >= 2 */
4260 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004261 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004262 || (vim_strchr(p_cpo, CPO_JOINSP)
4263 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004264 && (IObuff[len - 2] == '?'
4265 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 IObuff[len++] = ' ';
4267 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004268 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 if (tmp_ptr - ptr >= IOSIZE - len)
4270 tmp_ptr = ptr + IOSIZE - len - 1;
4271 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4272 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004273 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 }
4275 IObuff[len] = NUL;
4276 ptr = IObuff;
4277 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004278 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 continue;
4280 }
4281 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004282 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004283 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004284 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
4286 found_new_match = OK;
4287 break;
4288 }
4289 }
4290 p_scs = save_p_scs;
4291 p_ws = save_p_ws;
4292 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004293
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004294 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004295 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004296 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 found_new_match = OK;
4298
4299 /* break the loop for specialized modes (use 'complete' just for the
4300 * generic ctrl_x_mode == 0) or when we've found a new match */
4301 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004302 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004303 {
4304 if (got_int)
4305 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004306 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004307 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004308 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004309
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004310 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4311 || compl_interrupted)
4312 break;
4313 compl_started = TRUE;
4314 }
4315 else
4316 {
4317 /* Mark a buffer scanned when it has been scanned completely */
4318 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4319 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004321 compl_started = FALSE;
4322 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004324 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325
4326 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4327 && *e_cpt == NUL) /* Got to end of 'complete' */
4328 found_new_match = FAIL;
4329
4330 i = -1; /* total of matches, unknown */
4331 if (found_new_match == FAIL
4332 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4333 i = ins_compl_make_cyclic();
4334
4335 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004336 * just been made cyclic then we have to move compl_curr_match to the next
4337 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004338 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4339 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340 if (compl_curr_match == NULL)
4341 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 return i;
4343}
4344
4345/* Delete the old text being completed. */
4346 static void
4347ins_compl_delete()
4348{
4349 int i;
4350
4351 /*
4352 * In insert mode: Delete the typed part.
4353 * In replace mode: Put the old characters back, if any.
4354 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004355 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356 backspace_until_column(i);
4357 changed_cline_bef_curs();
4358}
4359
4360/* Insert the new text being completed. */
4361 static void
4362ins_compl_insert()
4363{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004364 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004365 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4366 compl_used_match = FALSE;
4367 else
4368 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004369}
4370
4371/*
4372 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004373 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4374 * get more completions. If it is FALSE, then we just do nothing when there
4375 * are no more completions in a given direction. The latter case is used when
4376 * we are still in the middle of finding completions, to allow browsing
4377 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 * Return the total number of matches, or -1 if still unknown -- webb.
4379 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004380 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4381 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 *
4383 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004384 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4385 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 */
4387 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004388ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004389 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004390 int count; /* repeat completion this many times; should
4391 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004392 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393{
4394 int num_matches = -1;
4395 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004396 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004397 compl_T *found_compl = NULL;
4398 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004399 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004400
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004401 if (compl_leader != NULL
4402 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004404 /* Set "compl_shown_match" to the actually shown match, it may differ
4405 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004406 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004407 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004408 && compl_shown_match->cp_next != NULL
4409 && compl_shown_match->cp_next != compl_first_match)
4410 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004411
4412 /* If we didn't find it searching forward, and compl_shows_dir is
4413 * backward, find the last match. */
4414 if (compl_shows_dir == BACKWARD
4415 && !ins_compl_equal(compl_shown_match,
4416 compl_leader, (int)STRLEN(compl_leader))
4417 && (compl_shown_match->cp_next == NULL
4418 || compl_shown_match->cp_next == compl_first_match))
4419 {
4420 while (!ins_compl_equal(compl_shown_match,
4421 compl_leader, (int)STRLEN(compl_leader))
4422 && compl_shown_match->cp_prev != NULL
4423 && compl_shown_match->cp_prev != compl_first_match)
4424 compl_shown_match = compl_shown_match->cp_prev;
4425 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004426 }
4427
4428 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004429 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 /* Delete old text to be replaced */
4431 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004432
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004433 /* When finding the longest common text we stick at the original text,
4434 * don't let CTRL-N or CTRL-P move to the first match. */
4435 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4436
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004437 /* When restarting the search don't insert the first match either. */
4438 if (compl_restarting)
4439 {
4440 advance = FALSE;
4441 compl_restarting = FALSE;
4442 }
4443
Bram Moolenaare3226be2005-12-18 22:10:00 +00004444 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4445 * around. */
4446 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004447 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004448 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004450 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004451 found_end = (compl_first_match != NULL
4452 && (compl_shown_match->cp_next == compl_first_match
4453 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004454 }
4455 else if (compl_shows_dir == BACKWARD
4456 && compl_shown_match->cp_prev != NULL)
4457 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004458 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004459 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004460 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 }
4462 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004463 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004464 if (!allow_get_expansion)
4465 {
4466 if (advance)
4467 {
4468 if (compl_shows_dir == BACKWARD)
4469 compl_pending -= todo + 1;
4470 else
4471 compl_pending += todo + 1;
4472 }
4473 return -1;
4474 }
4475
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004476 if (advance)
4477 {
4478 if (compl_shows_dir == BACKWARD)
4479 --compl_pending;
4480 else
4481 ++compl_pending;
4482 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004483
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004484 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004485 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004486
4487 /* handle any pending completions */
4488 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004489 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004490 {
4491 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4492 {
4493 compl_shown_match = compl_shown_match->cp_next;
4494 --compl_pending;
4495 }
4496 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4497 {
4498 compl_shown_match = compl_shown_match->cp_prev;
4499 ++compl_pending;
4500 }
4501 else
4502 break;
4503 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004504 found_end = FALSE;
4505 }
4506 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4507 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004508 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004509 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004510 ++todo;
4511 else
4512 /* Remember a matching item. */
4513 found_compl = compl_shown_match;
4514
4515 /* Stop at the end of the list when we found a usable match. */
4516 if (found_end)
4517 {
4518 if (found_compl != NULL)
4519 {
4520 compl_shown_match = found_compl;
4521 break;
4522 }
4523 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004524 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 }
4526
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004527 /* Insert the text of the new completion, or the compl_leader. */
4528 if (insert_match)
4529 {
4530 if (!compl_get_longest || compl_used_match)
4531 ins_compl_insert();
4532 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004533 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004534 }
4535 else
4536 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004537
4538 if (!allow_get_expansion)
4539 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004540 /* may undisplay the popup menu first */
4541 ins_compl_upd_pum();
4542
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004543 /* redraw to show the user what was inserted */
4544 update_screen(0);
4545
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004546 /* display the updated popup menu */
4547 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004548#ifdef FEAT_GUI
4549 if (gui.in_use)
4550 {
4551 /* Show the cursor after the match, not after the redrawn text. */
4552 setcursor();
4553 out_flush();
4554 gui_update_cursor(FALSE, FALSE);
4555 }
4556#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004557
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 /* Delete old text to be replaced, since we're still searching and
4559 * don't want to match ourselves! */
4560 ins_compl_delete();
4561 }
4562
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004563 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004564 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004565 compl_enter_selects = !insert_match && compl_match_array != NULL;
4566
Bram Moolenaar071d4272004-06-13 20:20:40 +00004567 /*
4568 * Show the file name for the match (if any)
4569 * Truncate the file name to avoid a wait for return.
4570 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004571 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 {
4573 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004574 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (i <= 0)
4576 i = 0;
4577 else
4578 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004579 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004580 msg(IObuff);
4581 redraw_cmdline = FALSE; /* don't overwrite! */
4582 }
4583
4584 return num_matches;
4585}
4586
4587/*
4588 * Call this while finding completions, to check whether the user has hit a key
4589 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004590 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004591 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004592 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 */
4594 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004595ins_compl_check_keys(frequency)
4596 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004597{
4598 static int count = 0;
4599
4600 int c;
4601
4602 /* Don't check when reading keys from a script. That would break the test
4603 * scripts */
4604 if (using_script())
4605 return;
4606
4607 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004608 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 return;
4610 count = 0;
4611
Bram Moolenaara260a972006-06-23 19:36:29 +00004612 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4613 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 if (c != NUL)
4616 {
4617 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4618 {
4619 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004620 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004621 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4622 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004623 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004624 else
4625 {
4626 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004627 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004628 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004629 if (c != K_IGNORE)
4630 {
4631 /* Don't interrupt completion when the character wasn't typed,
4632 * e.g., when doing @q to replay keys. */
4633 if (c != Ctrl_R && KeyTyped)
4634 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004635
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004636 vungetc(c);
4637 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004638 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004640 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004641 {
4642 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4643
4644 compl_pending = 0;
4645 (void)ins_compl_next(FALSE, todo, TRUE);
4646 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004647}
4648
4649/*
4650 * Decide the direction of Insert mode complete from the key typed.
4651 * Returns BACKWARD or FORWARD.
4652 */
4653 static int
4654ins_compl_key2dir(c)
4655 int c;
4656{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004657 if (c == Ctrl_P || c == Ctrl_L
4658 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4659 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004660 return BACKWARD;
4661 return FORWARD;
4662}
4663
4664/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004665 * Return TRUE for keys that are used for completion only when the popup menu
4666 * is visible.
4667 */
4668 static int
4669ins_compl_pum_key(c)
4670 int c;
4671{
4672 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004673 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4674 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004675}
4676
4677/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004678 * Decide the number of completions to move forward.
4679 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4680 */
4681 static int
4682ins_compl_key2count(c)
4683 int c;
4684{
4685 int h;
4686
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004687 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004688 {
4689 h = pum_get_height();
4690 if (h > 3)
4691 h -= 2; /* keep some context */
4692 return h;
4693 }
4694 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004695}
4696
4697/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004698 * Return TRUE if completion with "c" should insert the match, FALSE if only
4699 * to change the currently selected completion.
4700 */
4701 static int
4702ins_compl_use_match(c)
4703 int c;
4704{
4705 switch (c)
4706 {
4707 case K_UP:
4708 case K_DOWN:
4709 case K_PAGEDOWN:
4710 case K_KPAGEDOWN:
4711 case K_S_DOWN:
4712 case K_PAGEUP:
4713 case K_KPAGEUP:
4714 case K_S_UP:
4715 return FALSE;
4716 }
4717 return TRUE;
4718}
4719
4720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 * Do Insert mode completion.
4722 * Called when character "c" was typed, which has a meaning for completion.
4723 * Returns OK if completion was done, FAIL if something failed (out of mem).
4724 */
4725 static int
4726ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004727 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004729 char_u *line;
4730 int startcol = 0; /* column where searched text starts */
4731 colnr_T curs_col; /* cursor column */
4732 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004733 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734
Bram Moolenaare3226be2005-12-18 22:10:00 +00004735 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004737 {
4738 /* First time we hit ^N or ^P (in a row, I mean) */
4739
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 did_ai = FALSE;
4741#ifdef FEAT_SMARTINDENT
4742 did_si = FALSE;
4743 can_si = FALSE;
4744 can_si_back = FALSE;
4745#endif
4746 if (stop_arrow() == FAIL)
4747 return FAIL;
4748
4749 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004750 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004751 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004753 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004754 * "compl_startpos" to the cursor as a pattern to add a new word
4755 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004756 * "compl_startpos" is not in the same line as the cursor then fix it
4757 * (the line has been split because it was longer than 'tw'). if SOL
4758 * is set then skip the previous pattern, a word at the beginning of
4759 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004760 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4761 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
4763 /*
4764 * it is a continued search
4765 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004766 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4768 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4769 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004770 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004772 /* line (probably) wrapped, set compl_startpos to the
4773 * first non_blank in the line, if it is not a wordchar
4774 * include it to get a better pattern, but then we don't
4775 * want the "\\<" prefix, check it bellow */
4776 compl_col = (colnr_T)(skipwhite(line) - line);
4777 compl_startpos.col = compl_col;
4778 compl_startpos.lnum = curwin->w_cursor.lnum;
4779 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 }
4781 else
4782 {
4783 /* S_IPOS was set when we inserted a word that was at the
4784 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004785 * mode but first we need to redefine compl_startpos */
4786 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004788 compl_cont_status |= CONT_SOL;
4789 compl_startpos.col = (colnr_T)(skipwhite(
4790 line + compl_length
4791 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004793 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004795 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004796 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004797 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004799 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004801 compl_cont_status &= ~CONT_SOL;
4802 compl_length = (IOSIZE - MIN_SPACE);
4803 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004805 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4806 if (compl_length < 1)
4807 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 }
4809 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004810 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004812 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
4814 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004815 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004817 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004819 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004821 compl_cont_status = 0;
4822 compl_cont_status |= CONT_N_ADDS;
4823 compl_startpos = curwin->w_cursor;
4824 startcol = (int)curs_col;
4825 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826 }
4827
4828 /* Work out completion pattern and original text -- webb */
4829 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4830 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004831 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4833 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004834 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004836 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004838 compl_col += ++startcol;
4839 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 }
4841 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004842 compl_pattern = str_foldcase(line + compl_col,
4843 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004845 compl_pattern = vim_strnsave(line + compl_col,
4846 compl_length);
4847 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004848 return FAIL;
4849 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004850 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 {
4852 char_u *prefix = (char_u *)"\\<";
4853
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004854 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004855 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004856 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004857 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004859 if (!vim_iswordp(line + compl_col)
4860 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 && (
4862#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004863 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004865 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866#endif
4867 )))
4868 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004869 STRCPY((char *)compl_pattern, prefix);
4870 (void)quote_meta(compl_pattern + STRLEN(prefix),
4871 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004873 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004875 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004877 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#endif
4879 )
4880 {
4881 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004882 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4883 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004885 compl_col += curs_col;
4886 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 else
4889 {
4890#ifdef FEAT_MBYTE
4891 /* Search the point of change class of multibyte character
4892 * or not a word single byte character backward. */
4893 if (has_mbyte)
4894 {
4895 int base_class;
4896 int head_off;
4897
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004898 startcol -= (*mb_head_off)(line, line + startcol);
4899 base_class = mb_get_class(line + startcol);
4900 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004902 head_off = (*mb_head_off)(line, line + startcol);
4903 if (base_class != mb_get_class(line + startcol
4904 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004906 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 }
4908 }
4909 else
4910#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004911 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004913 compl_col += ++startcol;
4914 compl_length = (int)curs_col - startcol;
4915 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 {
4917 /* Only match word with at least two chars -- webb
4918 * there's no need to call quote_meta,
4919 * alloc(7) is enough -- Acevedo
4920 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004921 compl_pattern = alloc(7);
4922 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004924 STRCPY((char *)compl_pattern, "\\<");
4925 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4926 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004927 }
4928 else
4929 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004930 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004931 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004932 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004934 STRCPY((char *)compl_pattern, "\\<");
4935 (void)quote_meta(compl_pattern + 2, line + compl_col,
4936 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 }
4938 }
4939 }
4940 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4941 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004942 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004943 compl_length = (int)curs_col - (int)compl_col;
4944 if (compl_length < 0) /* cursor in indent: empty pattern */
4945 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004946 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004947 compl_pattern = str_foldcase(line + compl_col, compl_length,
4948 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004949 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004950 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4951 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 return FAIL;
4953 }
4954 else if (ctrl_x_mode == CTRL_X_FILES)
4955 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004956 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004958 compl_col += ++startcol;
4959 compl_length = (int)curs_col - startcol;
4960 compl_pattern = addstar(line + compl_col, compl_length,
4961 EXPAND_FILES);
4962 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 return FAIL;
4964 }
4965 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4966 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004967 compl_pattern = vim_strnsave(line, curs_col);
4968 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004969 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004970 set_cmd_context(&compl_xp, compl_pattern,
4971 (int)STRLEN(compl_pattern), curs_col);
4972 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4973 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004974 /* No completion possible, use an empty pattern to get a
4975 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004976 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004977 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004978 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4979 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004980 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004981 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004982 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004983#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004984 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004985 * Call user defined function 'completefunc' with "a:findstart"
4986 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004987 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004988 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004989 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004990 char_u *funcname;
4991 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004992
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004993 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004994 * string */
4995 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4996 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4997 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004998 {
4999 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5000 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005001 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005002 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005003
5004 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005005 args[1] = NULL;
5006 pos = curwin->w_cursor;
5007 col = call_func_retnr(funcname, 2, args, FALSE);
5008 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005009
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005010 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005011 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005012 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005013 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005014 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005016 /* Setup variables for completion. Need to obtain "line" again,
5017 * it may have become invalid. */
5018 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005019 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005020 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5021 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005022#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005023 return FAIL;
5024 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005025 else if (ctrl_x_mode == CTRL_X_SPELL)
5026 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005027#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005028 if (spell_bad_len > 0)
5029 compl_col = curs_col - spell_bad_len;
5030 else
5031 compl_col = spell_word_start(startcol);
5032 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005033 {
5034 compl_length = 0;
5035 compl_col = curs_col;
5036 }
5037 else
5038 {
5039 spell_expand_check_cap(compl_col);
5040 compl_length = (int)curs_col - compl_col;
5041 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005042 /* Need to obtain "line" again, it may have become invalid. */
5043 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005044 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5045 if (compl_pattern == NULL)
5046#endif
5047 return FAIL;
5048 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005049 else
5050 {
5051 EMSG2(_(e_intern2), "ins_complete()");
5052 return FAIL;
5053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005055 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056 {
5057 edit_submode_pre = (char_u *)_(" Adding");
5058 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5059 {
5060 /* Insert a new line, keep indentation but ignore 'comments' */
5061#ifdef FEAT_COMMENTS
5062 char_u *old = curbuf->b_p_com;
5063
5064 curbuf->b_p_com = (char_u *)"";
5065#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005066 compl_startpos.lnum = curwin->w_cursor.lnum;
5067 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 ins_eol('\r');
5069#ifdef FEAT_COMMENTS
5070 curbuf->b_p_com = old;
5071#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005072 compl_length = 0;
5073 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074 }
5075 }
5076 else
5077 {
5078 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005079 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 }
5081
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005082 if (compl_cont_status & CONT_LOCAL)
5083 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084 else
5085 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5086
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005087 /* Always add completion for the original text. */
5088 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005089 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5090 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005091 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005093 vim_free(compl_pattern);
5094 compl_pattern = NULL;
5095 vim_free(compl_orig_text);
5096 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 return FAIL;
5098 }
5099
5100 /* showmode might reset the internal line pointers, so it must
5101 * be called before line = ml_get(), or when this address is no
5102 * longer needed. -- Acevedo.
5103 */
5104 edit_submode_extra = (char_u *)_("-- Searching...");
5105 edit_submode_highl = HLF_COUNT;
5106 showmode();
5107 edit_submode_extra = NULL;
5108 out_flush();
5109 }
5110
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005111 compl_shown_match = compl_curr_match;
5112 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113
5114 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005115 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005117 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005118 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005120 /* may undisplay the popup menu */
5121 ins_compl_upd_pum();
5122
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005123 if (n > 1) /* all matches have been found */
5124 compl_matches = n;
5125 compl_curr_match = compl_shown_match;
5126 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005127
Bram Moolenaard68071d2006-05-02 22:08:30 +00005128 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5129 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005130 if (got_int && !global_busy)
5131 {
5132 (void)vgetc();
5133 got_int = FALSE;
5134 }
5135
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005136 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005137 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005138 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005139 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5140 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5142 edit_submode_highl = HLF_E;
5143 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5144 * because we couldn't expand anything at first place, but if we used
5145 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5146 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005147 if ( compl_length > 1
5148 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005149 || (ctrl_x_mode != 0
5150 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5151 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005152 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154
Bram Moolenaar572cb562005-08-05 21:35:02 +00005155 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005156 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005158 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159
5160 if (edit_submode_extra == NULL)
5161 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005162 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
5164 edit_submode_extra = (char_u *)_("Back at original");
5165 edit_submode_highl = HLF_W;
5166 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005168 {
5169 edit_submode_extra = (char_u *)_("Word from other line");
5170 edit_submode_highl = HLF_COUNT;
5171 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005172 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 {
5174 edit_submode_extra = (char_u *)_("The only match");
5175 edit_submode_highl = HLF_COUNT;
5176 }
5177 else
5178 {
5179 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005180 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005182 int number = 0;
5183 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005185 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 {
5187 /* search backwards for the first valid (!= -1) number.
5188 * This should normally succeed already at the first loop
5189 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005190 for (match = compl_curr_match->cp_prev; match != NULL
5191 && match != compl_first_match;
5192 match = match->cp_prev)
5193 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005195 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 break;
5197 }
5198 if (match != NULL)
5199 /* go up and assign all numbers which are not assigned
5200 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005201 for (match = match->cp_next;
5202 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005203 match = match->cp_next)
5204 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 }
5206 else /* BACKWARD */
5207 {
5208 /* search forwards (upwards) for the first valid (!= -1)
5209 * number. This should normally succeed already at the
5210 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005211 for (match = compl_curr_match->cp_next; match != NULL
5212 && match != compl_first_match;
5213 match = match->cp_next)
5214 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005216 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 break;
5218 }
5219 if (match != NULL)
5220 /* go down and assign all numbers which are not
5221 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005222 for (match = match->cp_prev; match
5223 && match->cp_number == -1;
5224 match = match->cp_prev)
5225 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227 }
5228
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005229 /* The match should always have a sequence number now, this is
5230 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005231 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005232 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005233 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5234 * Translations may need more than twice that. */
5235 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005237 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005238 vim_snprintf((char *)match_ref, sizeof(match_ref),
5239 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005240 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005241 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005242 vim_snprintf((char *)match_ref, sizeof(match_ref),
5243 _("match %d"),
5244 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005245 edit_submode_extra = match_ref;
5246 edit_submode_highl = HLF_R;
5247 if (dollar_vcol)
5248 curs_columns(FALSE);
5249 }
5250 }
5251 }
5252
5253 /* Show a message about what (completion) mode we're in. */
5254 showmode();
5255 if (edit_submode_extra != NULL)
5256 {
5257 if (!p_smd)
5258 msg_attr(edit_submode_extra,
5259 edit_submode_highl < HLF_COUNT
5260 ? hl_attr(edit_submode_highl) : 0);
5261 }
5262 else
5263 msg_clr_cmdline(); /* necessary for "noshowmode" */
5264
Bram Moolenaard68071d2006-05-02 22:08:30 +00005265 /* Show the popup menu, unless we got interrupted. */
5266 if (!compl_interrupted)
5267 {
5268 /* RedrawingDisabled may be set when invoked through complete(). */
5269 n = RedrawingDisabled;
5270 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005271
5272 /* If the cursor moved we need to remove the pum first. */
5273 setcursor();
5274 if (save_w_wrow != curwin->w_wrow)
5275 ins_compl_del_pum();
5276
Bram Moolenaard68071d2006-05-02 22:08:30 +00005277 ins_compl_show_pum();
5278 setcursor();
5279 RedrawingDisabled = n;
5280 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005281 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005282 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005283
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284 return OK;
5285}
5286
5287/*
5288 * Looks in the first "len" chars. of "src" for search-metachars.
5289 * If dest is not NULL the chars. are copied there quoting (with
5290 * a backslash) the metachars, and dest would be NUL terminated.
5291 * Returns the length (needed) of dest
5292 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005293 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294quote_meta(dest, src, len)
5295 char_u *dest;
5296 char_u *src;
5297 int len;
5298{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005299 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005301 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 {
5303 switch (*src)
5304 {
5305 case '.':
5306 case '*':
5307 case '[':
5308 if (ctrl_x_mode == CTRL_X_DICTIONARY
5309 || ctrl_x_mode == CTRL_X_THESAURUS)
5310 break;
5311 case '~':
5312 if (!p_magic) /* quote these only if magic is set */
5313 break;
5314 case '\\':
5315 if (ctrl_x_mode == CTRL_X_DICTIONARY
5316 || ctrl_x_mode == CTRL_X_THESAURUS)
5317 break;
5318 case '^': /* currently it's not needed. */
5319 case '$':
5320 m++;
5321 if (dest != NULL)
5322 *dest++ = '\\';
5323 break;
5324 }
5325 if (dest != NULL)
5326 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005327# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 /* Copy remaining bytes of a multibyte character. */
5329 if (has_mbyte)
5330 {
5331 int i, mb_len;
5332
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005333 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334 if (mb_len > 0 && len >= mb_len)
5335 for (i = 0; i < mb_len; ++i)
5336 {
5337 --len;
5338 ++src;
5339 if (dest != NULL)
5340 *dest++ = *src;
5341 }
5342 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005343# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344 }
5345 if (dest != NULL)
5346 *dest = NUL;
5347
5348 return m;
5349}
5350#endif /* FEAT_INS_EXPAND */
5351
5352/*
5353 * Next character is interpreted literally.
5354 * A one, two or three digit decimal number is interpreted as its byte value.
5355 * If one or two digits are entered, the next character is given to vungetc().
5356 * For Unicode a character > 255 may be returned.
5357 */
5358 int
5359get_literal()
5360{
5361 int cc;
5362 int nc;
5363 int i;
5364 int hex = FALSE;
5365 int octal = FALSE;
5366#ifdef FEAT_MBYTE
5367 int unicode = 0;
5368#endif
5369
5370 if (got_int)
5371 return Ctrl_C;
5372
5373#ifdef FEAT_GUI
5374 /*
5375 * In GUI there is no point inserting the internal code for a special key.
5376 * It is more useful to insert the string "<KEY>" instead. This would
5377 * probably be useful in a text window too, but it would not be
5378 * vi-compatible (maybe there should be an option for it?) -- webb
5379 */
5380 if (gui.in_use)
5381 ++allow_keys;
5382#endif
5383#ifdef USE_ON_FLY_SCROLL
5384 dont_scroll = TRUE; /* disallow scrolling here */
5385#endif
5386 ++no_mapping; /* don't map the next key hits */
5387 cc = 0;
5388 i = 0;
5389 for (;;)
5390 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005391 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005392#ifdef FEAT_CMDL_INFO
5393 if (!(State & CMDLINE)
5394# ifdef FEAT_MBYTE
5395 && MB_BYTE2LEN_CHECK(nc) == 1
5396# endif
5397 )
5398 add_to_showcmd(nc);
5399#endif
5400 if (nc == 'x' || nc == 'X')
5401 hex = TRUE;
5402 else if (nc == 'o' || nc == 'O')
5403 octal = TRUE;
5404#ifdef FEAT_MBYTE
5405 else if (nc == 'u' || nc == 'U')
5406 unicode = nc;
5407#endif
5408 else
5409 {
5410 if (hex
5411#ifdef FEAT_MBYTE
5412 || unicode != 0
5413#endif
5414 )
5415 {
5416 if (!vim_isxdigit(nc))
5417 break;
5418 cc = cc * 16 + hex2nr(nc);
5419 }
5420 else if (octal)
5421 {
5422 if (nc < '0' || nc > '7')
5423 break;
5424 cc = cc * 8 + nc - '0';
5425 }
5426 else
5427 {
5428 if (!VIM_ISDIGIT(nc))
5429 break;
5430 cc = cc * 10 + nc - '0';
5431 }
5432
5433 ++i;
5434 }
5435
5436 if (cc > 255
5437#ifdef FEAT_MBYTE
5438 && unicode == 0
5439#endif
5440 )
5441 cc = 255; /* limit range to 0-255 */
5442 nc = 0;
5443
5444 if (hex) /* hex: up to two chars */
5445 {
5446 if (i >= 2)
5447 break;
5448 }
5449#ifdef FEAT_MBYTE
5450 else if (unicode) /* Unicode: up to four or eight chars */
5451 {
5452 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5453 break;
5454 }
5455#endif
5456 else if (i >= 3) /* decimal or octal: up to three chars */
5457 break;
5458 }
5459 if (i == 0) /* no number entered */
5460 {
5461 if (nc == K_ZERO) /* NUL is stored as NL */
5462 {
5463 cc = '\n';
5464 nc = 0;
5465 }
5466 else
5467 {
5468 cc = nc;
5469 nc = 0;
5470 }
5471 }
5472
5473 if (cc == 0) /* NUL is stored as NL */
5474 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005475#ifdef FEAT_MBYTE
5476 if (enc_dbcs && (cc & 0xff) == 0)
5477 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5478 second byte will cause trouble! */
5479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005480
5481 --no_mapping;
5482#ifdef FEAT_GUI
5483 if (gui.in_use)
5484 --allow_keys;
5485#endif
5486 if (nc)
5487 vungetc(nc);
5488 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5489 return cc;
5490}
5491
5492/*
5493 * Insert character, taking care of special keys and mod_mask
5494 */
5495 static void
5496insert_special(c, allow_modmask, ctrlv)
5497 int c;
5498 int allow_modmask;
5499 int ctrlv; /* c was typed after CTRL-V */
5500{
5501 char_u *p;
5502 int len;
5503
5504 /*
5505 * Special function key, translate into "<Key>". Up to the last '>' is
5506 * inserted with ins_str(), so as not to replace characters in replace
5507 * mode.
5508 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5509 * unless 'allow_modmask' is TRUE.
5510 */
5511#ifdef MACOS
5512 /* Command-key never produces a normal key */
5513 if (mod_mask & MOD_MASK_CMD)
5514 allow_modmask = TRUE;
5515#endif
5516 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5517 {
5518 p = get_special_key_name(c, mod_mask);
5519 len = (int)STRLEN(p);
5520 c = p[len - 1];
5521 if (len > 2)
5522 {
5523 if (stop_arrow() == FAIL)
5524 return;
5525 p[len - 1] = NUL;
5526 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005527 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 ctrlv = FALSE;
5529 }
5530 }
5531 if (stop_arrow() == OK)
5532 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5533}
5534
5535/*
5536 * Special characters in this context are those that need processing other
5537 * than the simple insertion that can be performed here. This includes ESC
5538 * which terminates the insert, and CR/NL which need special processing to
5539 * open up a new line. This routine tries to optimize insertions performed by
5540 * the "redo", "undo" or "put" commands, so it needs to know when it should
5541 * stop and defer processing to the "normal" mechanism.
5542 * '0' and '^' are special, because they can be followed by CTRL-D.
5543 */
5544#ifdef EBCDIC
5545# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5546#else
5547# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5548#endif
5549
5550#ifdef FEAT_MBYTE
5551# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5552#else
5553# define WHITECHAR(cc) vim_iswhite(cc)
5554#endif
5555
5556 void
5557insertchar(c, flags, second_indent)
5558 int c; /* character to insert or NUL */
5559 int flags; /* INSCHAR_FORMAT, etc. */
5560 int second_indent; /* indent for second line if >= 0 */
5561{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005562 int textwidth;
5563#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005565#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567
5568 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5569 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005570
5571 /*
5572 * Try to break the line in two or more pieces when:
5573 * - Always do this if we have been called to do formatting only.
5574 * - Always do this when 'formatoptions' has the 'a' flag and the line
5575 * ends in white space.
5576 * - Otherwise:
5577 * - Don't do this if inserting a blank
5578 * - Don't do this if an existing character is being replaced, unless
5579 * we're in VREPLACE mode.
5580 * - Do this if the cursor is not on the line where insert started
5581 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5582 * before the insert.
5583 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5584 * before 'textwidth'
5585 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005586 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 && ((flags & INSCHAR_FORMAT)
5588 || (!vim_iswhite(c)
5589 && !((State & REPLACE_FLAG)
5590#ifdef FEAT_VREPLACE
5591 && !(State & VREPLACE_FLAG)
5592#endif
5593 && *ml_get_cursor() != NUL)
5594 && (curwin->w_cursor.lnum != Insstart.lnum
5595 || ((!has_format_option(FO_INS_LONG)
5596 || Insstart_textlen <= (colnr_T)textwidth)
5597 && (!fo_ins_blank
5598 || Insstart_blank_vcol <= (colnr_T)textwidth
5599 ))))))
5600 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005601 /* Format with 'formatexpr' when it's set. Use internal formatting
5602 * when 'formatexpr' isn't set or it returns non-zero. */
5603#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005604 int do_internal = TRUE;
5605
Bram Moolenaar81a82092008-03-12 16:27:00 +00005606 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005607 {
5608 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5609 /* It may be required to save for undo again, e.g. when setline()
5610 * was called. */
5611 ins_need_undo = TRUE;
5612 }
5613 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005615 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005617
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 if (c == NUL) /* only formatting was wanted */
5619 return;
5620
5621#ifdef FEAT_COMMENTS
5622 /* Check whether this character should end a comment. */
5623 if (did_ai && (int)c == end_comment_pending)
5624 {
5625 char_u *line;
5626 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5627 int middle_len, end_len;
5628 int i;
5629
5630 /*
5631 * Need to remove existing (middle) comment leader and insert end
5632 * comment leader. First, check what comment leader we can find.
5633 */
5634 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5635 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5636 {
5637 /* Skip middle-comment string */
5638 while (*p && p[-1] != ':') /* find end of middle flags */
5639 ++p;
5640 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5641 /* Don't count trailing white space for middle_len */
5642 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5643 --middle_len;
5644
5645 /* Find the end-comment string */
5646 while (*p && p[-1] != ':') /* find end of end flags */
5647 ++p;
5648 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5649
5650 /* Skip white space before the cursor */
5651 i = curwin->w_cursor.col;
5652 while (--i >= 0 && vim_iswhite(line[i]))
5653 ;
5654 i++;
5655
5656 /* Skip to before the middle leader */
5657 i -= middle_len;
5658
5659 /* Check some expected things before we go on */
5660 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5661 {
5662 /* Backspace over all the stuff we want to replace */
5663 backspace_until_column(i);
5664
5665 /*
5666 * Insert the end-comment string, except for the last
5667 * character, which will get inserted as normal later.
5668 */
5669 ins_bytes_len(lead_end, end_len - 1);
5670 }
5671 }
5672 }
5673 end_comment_pending = NUL;
5674#endif
5675
5676 did_ai = FALSE;
5677#ifdef FEAT_SMARTINDENT
5678 did_si = FALSE;
5679 can_si = FALSE;
5680 can_si_back = FALSE;
5681#endif
5682
5683 /*
5684 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5685 * This speeds up normal text input considerably.
5686 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5687 * need to re-indent at a ':', or any other character (but not what
5688 * 'paste' is set)..
5689 */
5690#ifdef USE_ON_FLY_SCROLL
5691 dont_scroll = FALSE; /* allow scrolling here */
5692#endif
5693
5694 if ( !ISSPECIAL(c)
5695#ifdef FEAT_MBYTE
5696 && (!has_mbyte || (*mb_char2len)(c) == 1)
5697#endif
5698 && vpeekc() != NUL
5699 && !(State & REPLACE_FLAG)
5700#ifdef FEAT_CINDENT
5701 && !cindent_on()
5702#endif
5703#ifdef FEAT_RIGHTLEFT
5704 && !p_ri
5705#endif
5706 )
5707 {
5708#define INPUT_BUFLEN 100
5709 char_u buf[INPUT_BUFLEN + 1];
5710 int i;
5711 colnr_T virtcol = 0;
5712
5713 buf[0] = c;
5714 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005715 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 virtcol = get_nolist_virtcol();
5717 /*
5718 * Stop the string when:
5719 * - no more chars available
5720 * - finding a special character (command key)
5721 * - buffer is full
5722 * - running into the 'textwidth' boundary
5723 * - need to check for abbreviation: A non-word char after a word-char
5724 */
5725 while ( (c = vpeekc()) != NUL
5726 && !ISSPECIAL(c)
5727#ifdef FEAT_MBYTE
5728 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5729#endif
5730 && i < INPUT_BUFLEN
5731 && (textwidth == 0
5732 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5733 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5734 {
5735#ifdef FEAT_RIGHTLEFT
5736 c = vgetc();
5737 if (p_hkmap && KeyTyped)
5738 c = hkmap(c); /* Hebrew mode mapping */
5739# ifdef FEAT_FKMAP
5740 if (p_fkmap && KeyTyped)
5741 c = fkmap(c); /* Farsi mode mapping */
5742# endif
5743 buf[i++] = c;
5744#else
5745 buf[i++] = vgetc();
5746#endif
5747 }
5748
5749#ifdef FEAT_DIGRAPHS
5750 do_digraph(-1); /* clear digraphs */
5751 do_digraph(buf[i-1]); /* may be the start of a digraph */
5752#endif
5753 buf[i] = NUL;
5754 ins_str(buf);
5755 if (flags & INSCHAR_CTRLV)
5756 {
5757 redo_literal(*buf);
5758 i = 1;
5759 }
5760 else
5761 i = 0;
5762 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005763 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 }
5765 else
5766 {
5767#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005768 int cc;
5769
Bram Moolenaar071d4272004-06-13 20:20:40 +00005770 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5771 {
5772 char_u buf[MB_MAXBYTES + 1];
5773
5774 (*mb_char2bytes)(c, buf);
5775 buf[cc] = NUL;
5776 ins_char_bytes(buf, cc);
5777 AppendCharToRedobuff(c);
5778 }
5779 else
5780#endif
5781 {
5782 ins_char(c);
5783 if (flags & INSCHAR_CTRLV)
5784 redo_literal(c);
5785 else
5786 AppendCharToRedobuff(c);
5787 }
5788 }
5789}
5790
5791/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005792 * Format text at the current insert position.
5793 */
5794 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00005795internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005796 int textwidth;
5797 int second_indent;
5798 int flags;
5799 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005800 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005801{
5802 int cc;
5803 int save_char = NUL;
5804 int haveto_redraw = FALSE;
5805 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5806#ifdef FEAT_MBYTE
5807 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5808#endif
5809 int fo_white_par = has_format_option(FO_WHITE_PAR);
5810 int first_line = TRUE;
5811#ifdef FEAT_COMMENTS
5812 colnr_T leader_len;
5813 int no_leader = FALSE;
5814 int do_comments = (flags & INSCHAR_DO_COM);
5815#endif
5816
5817 /*
5818 * When 'ai' is off we don't want a space under the cursor to be
5819 * deleted. Replace it with an 'x' temporarily.
5820 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005821 if (!curbuf->b_p_ai
5822#ifdef FEAT_VREPLACE
5823 && !(State & VREPLACE_FLAG)
5824#endif
5825 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005826 {
5827 cc = gchar_cursor();
5828 if (vim_iswhite(cc))
5829 {
5830 save_char = cc;
5831 pchar_cursor('x');
5832 }
5833 }
5834
5835 /*
5836 * Repeat breaking lines, until the current line is not too long.
5837 */
5838 while (!got_int)
5839 {
5840 int startcol; /* Cursor column at entry */
5841 int wantcol; /* column at textwidth border */
5842 int foundcol; /* column for start of spaces */
5843 int end_foundcol = 0; /* column for start of word */
5844 colnr_T len;
5845 colnr_T virtcol;
5846#ifdef FEAT_VREPLACE
5847 int orig_col = 0;
5848 char_u *saved_text = NULL;
5849#endif
5850 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005851 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005852
Bram Moolenaar97b98102009-11-17 16:41:01 +00005853 virtcol = get_nolist_virtcol()
5854 + char2cells(c != NUL ? c : gchar_cursor());
5855 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005856 break;
5857
5858#ifdef FEAT_COMMENTS
5859 if (no_leader)
5860 do_comments = FALSE;
5861 else if (!(flags & INSCHAR_FORMAT)
5862 && has_format_option(FO_WRAP_COMS))
5863 do_comments = TRUE;
5864
5865 /* Don't break until after the comment leader */
5866 if (do_comments)
5867 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5868 else
5869 leader_len = 0;
5870
5871 /* If the line doesn't start with a comment leader, then don't
5872 * start one in a following broken line. Avoids that a %word
5873 * moved to the start of the next line causes all following lines
5874 * to start with %. */
5875 if (leader_len == 0)
5876 no_leader = TRUE;
5877#endif
5878 if (!(flags & INSCHAR_FORMAT)
5879#ifdef FEAT_COMMENTS
5880 && leader_len == 0
5881#endif
5882 && !has_format_option(FO_WRAP))
5883
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005884 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005885 if ((startcol = curwin->w_cursor.col) == 0)
5886 break;
5887
5888 /* find column of textwidth border */
5889 coladvance((colnr_T)textwidth);
5890 wantcol = curwin->w_cursor.col;
5891
Bram Moolenaar97b98102009-11-17 16:41:01 +00005892 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005893 foundcol = 0;
5894
5895 /*
5896 * Find position to break at.
5897 * Stop at first entered white when 'formatoptions' has 'v'
5898 */
5899 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5900 || curwin->w_cursor.lnum != Insstart.lnum
5901 || curwin->w_cursor.col >= Insstart.col)
5902 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00005903 if (curwin->w_cursor.col == startcol && c != NUL)
5904 cc = c;
5905 else
5906 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005907 if (WHITECHAR(cc))
5908 {
5909 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005910 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005911
5912 /* find start of sequence of blanks */
5913 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5914 {
5915 dec_cursor();
5916 cc = gchar_cursor();
5917 }
5918 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5919 break; /* only spaces in front of text */
5920#ifdef FEAT_COMMENTS
5921 /* Don't break until after the comment leader */
5922 if (curwin->w_cursor.col < leader_len)
5923 break;
5924#endif
5925 if (has_format_option(FO_ONE_LETTER))
5926 {
5927 /* do not break after one-letter words */
5928 if (curwin->w_cursor.col == 0)
5929 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005930#ifdef FEAT_COMMENTS
5931 /* do not break "#a b" when 'tw' is 2 */
5932 if (curwin->w_cursor.col <= leader_len)
5933 break;
5934#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005935 col = curwin->w_cursor.col;
5936 dec_cursor();
5937 cc = gchar_cursor();
5938
5939 if (WHITECHAR(cc))
5940 continue; /* one-letter, continue */
5941 curwin->w_cursor.col = col;
5942 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00005943
5944 inc_cursor();
5945
5946 end_foundcol = end_col + 1;
5947 foundcol = curwin->w_cursor.col;
5948 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005949 break;
5950 }
5951#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00005952 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005953 {
5954 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005955 if (curwin->w_cursor.col != startcol)
5956 {
5957#ifdef FEAT_COMMENTS
5958 /* Don't break until after the comment leader */
5959 if (curwin->w_cursor.col < leader_len)
5960 break;
5961#endif
5962 col = curwin->w_cursor.col;
5963 inc_cursor();
5964 /* Don't change end_foundcol if already set. */
5965 if (foundcol != curwin->w_cursor.col)
5966 {
5967 foundcol = curwin->w_cursor.col;
5968 end_foundcol = foundcol;
5969 if (curwin->w_cursor.col <= (colnr_T)wantcol)
5970 break;
5971 }
5972 curwin->w_cursor.col = col;
5973 }
5974
5975 if (curwin->w_cursor.col == 0)
5976 break;
5977
5978 col = curwin->w_cursor.col;
5979
5980 dec_cursor();
5981 cc = gchar_cursor();
5982
5983 if (WHITECHAR(cc))
5984 continue; /* break with space */
5985#ifdef FEAT_COMMENTS
5986 /* Don't break until after the comment leader */
5987 if (curwin->w_cursor.col < leader_len)
5988 break;
5989#endif
5990
5991 curwin->w_cursor.col = col;
5992
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005993 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005994 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005995 if (curwin->w_cursor.col <= (colnr_T)wantcol)
5996 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005997 }
5998#endif
5999 if (curwin->w_cursor.col == 0)
6000 break;
6001 dec_cursor();
6002 }
6003
6004 if (foundcol == 0) /* no spaces, cannot break line */
6005 {
6006 curwin->w_cursor.col = startcol;
6007 break;
6008 }
6009
6010 /* Going to break the line, remove any "$" now. */
6011 undisplay_dollar();
6012
6013 /*
6014 * Offset between cursor position and line break is used by replace
6015 * stack functions. VREPLACE does not use this, and backspaces
6016 * over the text instead.
6017 */
6018#ifdef FEAT_VREPLACE
6019 if (State & VREPLACE_FLAG)
6020 orig_col = startcol; /* Will start backspacing from here */
6021 else
6022#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006023 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006024
6025 /*
6026 * adjust startcol for spaces that will be deleted and
6027 * characters that will remain on top line
6028 */
6029 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006030 while ((cc = gchar_cursor(), WHITECHAR(cc))
6031 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006032 inc_cursor();
6033 startcol -= curwin->w_cursor.col;
6034 if (startcol < 0)
6035 startcol = 0;
6036
6037#ifdef FEAT_VREPLACE
6038 if (State & VREPLACE_FLAG)
6039 {
6040 /*
6041 * In VREPLACE mode, we will backspace over the text to be
6042 * wrapped, so save a copy now to put on the next line.
6043 */
6044 saved_text = vim_strsave(ml_get_cursor());
6045 curwin->w_cursor.col = orig_col;
6046 if (saved_text == NULL)
6047 break; /* Can't do it, out of memory */
6048 saved_text[startcol] = NUL;
6049
6050 /* Backspace over characters that will move to the next line */
6051 if (!fo_white_par)
6052 backspace_until_column(foundcol);
6053 }
6054 else
6055#endif
6056 {
6057 /* put cursor after pos. to break line */
6058 if (!fo_white_par)
6059 curwin->w_cursor.col = foundcol;
6060 }
6061
6062 /*
6063 * Split the line just before the margin.
6064 * Only insert/delete lines, but don't really redraw the window.
6065 */
6066 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6067 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6068#ifdef FEAT_COMMENTS
6069 + (do_comments ? OPENLINE_DO_COM : 0)
6070#endif
6071 , old_indent);
6072 old_indent = 0;
6073
6074 replace_offset = 0;
6075 if (first_line)
6076 {
6077 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6078 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6079 if (second_indent >= 0)
6080 {
6081#ifdef FEAT_VREPLACE
6082 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006083 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006084 else
6085#endif
6086 (void)set_indent(second_indent, SIN_CHANGED);
6087 }
6088 first_line = FALSE;
6089 }
6090
6091#ifdef FEAT_VREPLACE
6092 if (State & VREPLACE_FLAG)
6093 {
6094 /*
6095 * In VREPLACE mode we have backspaced over the text to be
6096 * moved, now we re-insert it into the new line.
6097 */
6098 ins_bytes(saved_text);
6099 vim_free(saved_text);
6100 }
6101 else
6102#endif
6103 {
6104 /*
6105 * Check if cursor is not past the NUL off the line, cindent
6106 * may have added or removed indent.
6107 */
6108 curwin->w_cursor.col += startcol;
6109 len = (colnr_T)STRLEN(ml_get_curline());
6110 if (curwin->w_cursor.col > len)
6111 curwin->w_cursor.col = len;
6112 }
6113
6114 haveto_redraw = TRUE;
6115#ifdef FEAT_CINDENT
6116 can_cindent = TRUE;
6117#endif
6118 /* moved the cursor, don't autoindent or cindent now */
6119 did_ai = FALSE;
6120#ifdef FEAT_SMARTINDENT
6121 did_si = FALSE;
6122 can_si = FALSE;
6123 can_si_back = FALSE;
6124#endif
6125 line_breakcheck();
6126 }
6127
6128 if (save_char != NUL) /* put back space after cursor */
6129 pchar_cursor(save_char);
6130
6131 if (!format_only && haveto_redraw)
6132 {
6133 update_topline();
6134 redraw_curbuf_later(VALID);
6135 }
6136}
6137
6138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006139 * Called after inserting or deleting text: When 'formatoptions' includes the
6140 * 'a' flag format from the current line until the end of the paragraph.
6141 * Keep the cursor at the same position relative to the text.
6142 * The caller must have saved the cursor line for undo, following ones will be
6143 * saved here.
6144 */
6145 void
6146auto_format(trailblank, prev_line)
6147 int trailblank; /* when TRUE also format with trailing blank */
6148 int prev_line; /* may start in previous line */
6149{
6150 pos_T pos;
6151 colnr_T len;
6152 char_u *old;
6153 char_u *new, *pnew;
6154 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006155 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006156
6157 if (!has_format_option(FO_AUTO))
6158 return;
6159
6160 pos = curwin->w_cursor;
6161 old = ml_get_curline();
6162
6163 /* may remove added space */
6164 check_auto_format(FALSE);
6165
6166 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6167 * user might insert normal text next. Also skip formatting when "1" is
6168 * in 'formatoptions' and there is a single character before the cursor.
6169 * Otherwise the line would be broken and when typing another non-white
6170 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006171 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006172 if (*old != NUL && !trailblank && wasatend)
6173 {
6174 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006175 cc = gchar_cursor();
6176 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6177 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006178 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006179 cc = gchar_cursor();
6180 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006181 {
6182 curwin->w_cursor = pos;
6183 return;
6184 }
6185 curwin->w_cursor = pos;
6186 }
6187
6188#ifdef FEAT_COMMENTS
6189 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6190 * comments. */
6191 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6192 && get_leader_len(old, NULL, FALSE) == 0)
6193 return;
6194#endif
6195
6196 /*
6197 * May start formatting in a previous line, so that after "x" a word is
6198 * moved to the previous line if it fits there now. Only when this is not
6199 * the start of a paragraph.
6200 */
6201 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6202 {
6203 --curwin->w_cursor.lnum;
6204 if (u_save_cursor() == FAIL)
6205 return;
6206 }
6207
6208 /*
6209 * Do the formatting and restore the cursor position. "saved_cursor" will
6210 * be adjusted for the text formatting.
6211 */
6212 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006213 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006214 curwin->w_cursor = saved_cursor;
6215 saved_cursor.lnum = 0;
6216
6217 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6218 {
6219 /* "cannot happen" */
6220 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6221 coladvance((colnr_T)MAXCOL);
6222 }
6223 else
6224 check_cursor_col();
6225
6226 /* Insert mode: If the cursor is now after the end of the line while it
6227 * previously wasn't, the line was broken. Because of the rule above we
6228 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6229 * formatted. */
6230 if (!wasatend && has_format_option(FO_WHITE_PAR))
6231 {
6232 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006233 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006234 if (curwin->w_cursor.col == len)
6235 {
6236 pnew = vim_strnsave(new, len + 2);
6237 pnew[len] = ' ';
6238 pnew[len + 1] = NUL;
6239 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6240 /* remove the space later */
6241 did_add_space = TRUE;
6242 }
6243 else
6244 /* may remove added space */
6245 check_auto_format(FALSE);
6246 }
6247
6248 check_cursor();
6249}
6250
6251/*
6252 * When an extra space was added to continue a paragraph for auto-formatting,
6253 * delete it now. The space must be under the cursor, just after the insert
6254 * position.
6255 */
6256 static void
6257check_auto_format(end_insert)
6258 int end_insert; /* TRUE when ending Insert mode */
6259{
6260 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006261 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262
6263 if (did_add_space)
6264 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006265 cc = gchar_cursor();
6266 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006267 /* Somehow the space was removed already. */
6268 did_add_space = FALSE;
6269 else
6270 {
6271 if (!end_insert)
6272 {
6273 inc_cursor();
6274 c = gchar_cursor();
6275 dec_cursor();
6276 }
6277 if (c != NUL)
6278 {
6279 /* The space is no longer at the end of the line, delete it. */
6280 del_char(FALSE);
6281 did_add_space = FALSE;
6282 }
6283 }
6284 }
6285}
6286
6287/*
6288 * Find out textwidth to be used for formatting:
6289 * if 'textwidth' option is set, use it
6290 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6291 * if invalid value, use 0.
6292 * Set default to window width (maximum 79) for "gq" operator.
6293 */
6294 int
6295comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006296 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006297{
6298 int textwidth;
6299
6300 textwidth = curbuf->b_p_tw;
6301 if (textwidth == 0 && curbuf->b_p_wm)
6302 {
6303 /* The width is the window width minus 'wrapmargin' minus all the
6304 * things that add to the margin. */
6305 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6306#ifdef FEAT_CMDWIN
6307 if (cmdwin_type != 0)
6308 textwidth -= 1;
6309#endif
6310#ifdef FEAT_FOLDING
6311 textwidth -= curwin->w_p_fdc;
6312#endif
6313#ifdef FEAT_SIGNS
6314 if (curwin->w_buffer->b_signlist != NULL
6315# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006316 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006317# endif
6318 )
6319 textwidth -= 1;
6320#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006321 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322 textwidth -= 8;
6323 }
6324 if (textwidth < 0)
6325 textwidth = 0;
6326 if (ff && textwidth == 0)
6327 {
6328 textwidth = W_WIDTH(curwin) - 1;
6329 if (textwidth > 79)
6330 textwidth = 79;
6331 }
6332 return textwidth;
6333}
6334
6335/*
6336 * Put a character in the redo buffer, for when just after a CTRL-V.
6337 */
6338 static void
6339redo_literal(c)
6340 int c;
6341{
6342 char_u buf[10];
6343
6344 /* Only digits need special treatment. Translate them into a string of
6345 * three digits. */
6346 if (VIM_ISDIGIT(c))
6347 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006348 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006349 AppendToRedobuff(buf);
6350 }
6351 else
6352 AppendCharToRedobuff(c);
6353}
6354
6355/*
6356 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006357 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358 */
6359 static void
6360start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006361 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362{
6363 if (!arrow_used) /* something has been inserted */
6364 {
6365 AppendToRedobuff(ESC_STR);
6366 stop_insert(end_insert_pos, FALSE);
6367 arrow_used = TRUE; /* this means we stopped the current insert */
6368 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006369#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006370 check_spell_redraw();
6371#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006372}
6373
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006374#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006375/*
6376 * If we skipped highlighting word at cursor, do it now.
6377 * It may be skipped again, thus reset spell_redraw_lnum first.
6378 */
6379 static void
6380check_spell_redraw()
6381{
6382 if (spell_redraw_lnum != 0)
6383 {
6384 linenr_T lnum = spell_redraw_lnum;
6385
6386 spell_redraw_lnum = 0;
6387 redrawWinline(lnum, FALSE);
6388 }
6389}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006390
6391/*
6392 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6393 * spelled word, if there is one.
6394 */
6395 static void
6396spell_back_to_badword()
6397{
6398 pos_T tpos = curwin->w_cursor;
6399
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006400 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006401 if (curwin->w_cursor.col != tpos.col)
6402 start_arrow(&tpos);
6403}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006404#endif
6405
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406/*
6407 * stop_arrow() is called before a change is made in insert mode.
6408 * If an arrow key has been used, start a new insertion.
6409 * Returns FAIL if undo is impossible, shouldn't insert then.
6410 */
6411 int
6412stop_arrow()
6413{
6414 if (arrow_used)
6415 {
6416 if (u_save_cursor() == OK)
6417 {
6418 arrow_used = FALSE;
6419 ins_need_undo = FALSE;
6420 }
6421 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006422 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006423 ai_col = 0;
6424#ifdef FEAT_VREPLACE
6425 if (State & VREPLACE_FLAG)
6426 {
6427 orig_line_count = curbuf->b_ml.ml_line_count;
6428 vr_lines_changed = 1;
6429 }
6430#endif
6431 ResetRedobuff();
6432 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006433 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006434 }
6435 else if (ins_need_undo)
6436 {
6437 if (u_save_cursor() == OK)
6438 ins_need_undo = FALSE;
6439 }
6440
6441#ifdef FEAT_FOLDING
6442 /* Always open fold at the cursor line when inserting something. */
6443 foldOpenCursor();
6444#endif
6445
6446 return (arrow_used || ins_need_undo ? FAIL : OK);
6447}
6448
6449/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006450 * Do a few things to stop inserting.
6451 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6452 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 */
6454 static void
6455stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006456 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006457 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006458{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006459 int cc;
6460 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461
6462 stop_redo_ins();
6463 replace_flush(); /* abandon replace stack */
6464
6465 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006466 * Save the inserted text for later redo with ^@ and CTRL-A.
6467 * Don't do it when "restart_edit" was set and nothing was inserted,
6468 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006469 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006470 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006471 if (did_restart_edit == 0 || (ptr != NULL
6472 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006473 {
6474 vim_free(last_insert);
6475 last_insert = ptr;
6476 last_insert_skip = new_insert_skip;
6477 }
6478 else
6479 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006480
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006481 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482 {
6483 /* Auto-format now. It may seem strange to do this when stopping an
6484 * insertion (or moving the cursor), but it's required when appending
6485 * a line and having it end in a space. But only do it when something
6486 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006487 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006488 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006489 pos_T tpos = curwin->w_cursor;
6490
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 /* When the cursor is at the end of the line after a space the
6492 * formatting will move it to the following word. Avoid that by
6493 * moving the cursor onto the space. */
6494 cc = 'x';
6495 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6496 {
6497 dec_cursor();
6498 cc = gchar_cursor();
6499 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006500 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501 }
6502
6503 auto_format(TRUE, FALSE);
6504
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006505 if (vim_iswhite(cc))
6506 {
6507 if (gchar_cursor() != NUL)
6508 inc_cursor();
6509#ifdef FEAT_VIRTUALEDIT
6510 /* If the cursor is still at the same character, also keep
6511 * the "coladd". */
6512 if (gchar_cursor() == NUL
6513 && curwin->w_cursor.lnum == tpos.lnum
6514 && curwin->w_cursor.col == tpos.col)
6515 curwin->w_cursor.coladd = tpos.coladd;
6516#endif
6517 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006518 }
6519
6520 /* If a space was inserted for auto-formatting, remove it now. */
6521 check_auto_format(TRUE);
6522
6523 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006524 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006525 * Do this when ESC was used or moving the cursor up/down.
6526 * Check for the old position still being valid, just in case the text
6527 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006528 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006529 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6530 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006531 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006532 pos_T tpos = curwin->w_cursor;
6533
6534 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006535 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006536 for (;;)
6537 {
6538 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6539 --curwin->w_cursor.col;
6540 cc = gchar_cursor();
6541 if (!vim_iswhite(cc))
6542 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006543 if (del_char(TRUE) == FAIL)
6544 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006545 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006546 if (curwin->w_cursor.lnum != tpos.lnum)
6547 curwin->w_cursor = tpos;
6548 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006549 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6550
6551#ifdef FEAT_VISUAL
6552 /* <C-S-Right> may have started Visual mode, adjust the position for
6553 * deleted characters. */
6554 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6555 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006556 int len = (int)STRLEN(ml_get_curline());
6557
6558 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006559 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006560 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561# ifdef FEAT_VIRTUALEDIT
6562 VIsual.coladd = 0;
6563# endif
6564 }
6565 }
6566#endif
6567 }
6568 }
6569 did_ai = FALSE;
6570#ifdef FEAT_SMARTINDENT
6571 did_si = FALSE;
6572 can_si = FALSE;
6573 can_si_back = FALSE;
6574#endif
6575
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006576 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6577 * now in a different buffer. */
6578 if (end_insert_pos != NULL)
6579 {
6580 curbuf->b_op_start = Insstart;
6581 curbuf->b_op_end = *end_insert_pos;
6582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006583}
6584
6585/*
6586 * Set the last inserted text to a single character.
6587 * Used for the replace command.
6588 */
6589 void
6590set_last_insert(c)
6591 int c;
6592{
6593 char_u *s;
6594
6595 vim_free(last_insert);
6596#ifdef FEAT_MBYTE
6597 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6598#else
6599 last_insert = alloc(6);
6600#endif
6601 if (last_insert != NULL)
6602 {
6603 s = last_insert;
6604 /* Use the CTRL-V only when entering a special char */
6605 if (c < ' ' || c == DEL)
6606 *s++ = Ctrl_V;
6607 s = add_char2buf(c, s);
6608 *s++ = ESC;
6609 *s++ = NUL;
6610 last_insert_skip = 0;
6611 }
6612}
6613
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006614#if defined(EXITFREE) || defined(PROTO)
6615 void
6616free_last_insert()
6617{
6618 vim_free(last_insert);
6619 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006620# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006621 vim_free(compl_orig_text);
6622 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006623# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006624}
6625#endif
6626
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627/*
6628 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6629 * and CSI. Handle multi-byte characters.
6630 * Returns a pointer to after the added bytes.
6631 */
6632 char_u *
6633add_char2buf(c, s)
6634 int c;
6635 char_u *s;
6636{
6637#ifdef FEAT_MBYTE
6638 char_u temp[MB_MAXBYTES];
6639 int i;
6640 int len;
6641
6642 len = (*mb_char2bytes)(c, temp);
6643 for (i = 0; i < len; ++i)
6644 {
6645 c = temp[i];
6646#endif
6647 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6648 if (c == K_SPECIAL)
6649 {
6650 *s++ = K_SPECIAL;
6651 *s++ = KS_SPECIAL;
6652 *s++ = KE_FILLER;
6653 }
6654#ifdef FEAT_GUI
6655 else if (c == CSI)
6656 {
6657 *s++ = CSI;
6658 *s++ = KS_EXTRA;
6659 *s++ = (int)KE_CSI;
6660 }
6661#endif
6662 else
6663 *s++ = c;
6664#ifdef FEAT_MBYTE
6665 }
6666#endif
6667 return s;
6668}
6669
6670/*
6671 * move cursor to start of line
6672 * if flags & BL_WHITE move to first non-white
6673 * if flags & BL_SOL move to first non-white if startofline is set,
6674 * otherwise keep "curswant" column
6675 * if flags & BL_FIX don't leave the cursor on a NUL.
6676 */
6677 void
6678beginline(flags)
6679 int flags;
6680{
6681 if ((flags & BL_SOL) && !p_sol)
6682 coladvance(curwin->w_curswant);
6683 else
6684 {
6685 curwin->w_cursor.col = 0;
6686#ifdef FEAT_VIRTUALEDIT
6687 curwin->w_cursor.coladd = 0;
6688#endif
6689
6690 if (flags & (BL_WHITE | BL_SOL))
6691 {
6692 char_u *ptr;
6693
6694 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6695 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6696 ++curwin->w_cursor.col;
6697 }
6698 curwin->w_set_curswant = TRUE;
6699 }
6700}
6701
6702/*
6703 * oneright oneleft cursor_down cursor_up
6704 *
6705 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006706 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006707 * Return OK when successful, FAIL when we hit a line of file boundary.
6708 */
6709
6710 int
6711oneright()
6712{
6713 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006714 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006715
6716#ifdef FEAT_VIRTUALEDIT
6717 if (virtual_active())
6718 {
6719 pos_T prevpos = curwin->w_cursor;
6720
6721 /* Adjust for multi-wide char (excluding TAB) */
6722 ptr = ml_get_cursor();
6723 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006724# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006725 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006726# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006728# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 ))
6730 ? ptr2cells(ptr) : 1));
6731 curwin->w_set_curswant = TRUE;
6732 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6733 return (prevpos.col != curwin->w_cursor.col
6734 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6735 }
6736#endif
6737
6738 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006739 if (*ptr == NUL)
6740 return FAIL; /* already at the very end */
6741
Bram Moolenaar071d4272004-06-13 20:20:40 +00006742#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006743 if (has_mbyte)
6744 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006745 else
6746#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006747 l = 1;
6748
6749 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6750 * contains "onemore". */
6751 if (ptr[l] == NUL
6752#ifdef FEAT_VIRTUALEDIT
6753 && (ve_flags & VE_ONEMORE) == 0
6754#endif
6755 )
6756 return FAIL;
6757 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758
6759 curwin->w_set_curswant = TRUE;
6760 return OK;
6761}
6762
6763 int
6764oneleft()
6765{
6766#ifdef FEAT_VIRTUALEDIT
6767 if (virtual_active())
6768 {
6769 int width;
6770 int v = getviscol();
6771
6772 if (v == 0)
6773 return FAIL;
6774
6775# ifdef FEAT_LINEBREAK
6776 /* We might get stuck on 'showbreak', skip over it. */
6777 width = 1;
6778 for (;;)
6779 {
6780 coladvance(v - width);
6781 /* getviscol() is slow, skip it when 'showbreak' is empty and
6782 * there are no multi-byte characters */
6783 if ((*p_sbr == NUL
6784# ifdef FEAT_MBYTE
6785 && !has_mbyte
6786# endif
6787 ) || getviscol() < v)
6788 break;
6789 ++width;
6790 }
6791# else
6792 coladvance(v - 1);
6793# endif
6794
6795 if (curwin->w_cursor.coladd == 1)
6796 {
6797 char_u *ptr;
6798
6799 /* Adjust for multi-wide char (not a TAB) */
6800 ptr = ml_get_cursor();
6801 if (*ptr != TAB && vim_isprintc(
6802# ifdef FEAT_MBYTE
6803 (*mb_ptr2char)(ptr)
6804# else
6805 *ptr
6806# endif
6807 ) && ptr2cells(ptr) > 1)
6808 curwin->w_cursor.coladd = 0;
6809 }
6810
6811 curwin->w_set_curswant = TRUE;
6812 return OK;
6813 }
6814#endif
6815
6816 if (curwin->w_cursor.col == 0)
6817 return FAIL;
6818
6819 curwin->w_set_curswant = TRUE;
6820 --curwin->w_cursor.col;
6821
6822#ifdef FEAT_MBYTE
6823 /* if the character on the left of the current cursor is a multi-byte
6824 * character, move to its first byte */
6825 if (has_mbyte)
6826 mb_adjust_cursor();
6827#endif
6828 return OK;
6829}
6830
6831 int
6832cursor_up(n, upd_topline)
6833 long n;
6834 int upd_topline; /* When TRUE: update topline */
6835{
6836 linenr_T lnum;
6837
6838 if (n > 0)
6839 {
6840 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006841 /* This fails if the cursor is already in the first line or the count
6842 * is larger than the line number and '-' is in 'cpoptions' */
6843 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006844 return FAIL;
6845 if (n >= lnum)
6846 lnum = 1;
6847 else
6848#ifdef FEAT_FOLDING
6849 if (hasAnyFolding(curwin))
6850 {
6851 /*
6852 * Count each sequence of folded lines as one logical line.
6853 */
6854 /* go to the the start of the current fold */
6855 (void)hasFolding(lnum, &lnum, NULL);
6856
6857 while (n--)
6858 {
6859 /* move up one line */
6860 --lnum;
6861 if (lnum <= 1)
6862 break;
6863 /* If we entered a fold, move to the beginning, unless in
6864 * Insert mode or when 'foldopen' contains "all": it will open
6865 * in a moment. */
6866 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6867 (void)hasFolding(lnum, &lnum, NULL);
6868 }
6869 if (lnum < 1)
6870 lnum = 1;
6871 }
6872 else
6873#endif
6874 lnum -= n;
6875 curwin->w_cursor.lnum = lnum;
6876 }
6877
6878 /* try to advance to the column we want to be at */
6879 coladvance(curwin->w_curswant);
6880
6881 if (upd_topline)
6882 update_topline(); /* make sure curwin->w_topline is valid */
6883
6884 return OK;
6885}
6886
6887/*
6888 * Cursor down a number of logical lines.
6889 */
6890 int
6891cursor_down(n, upd_topline)
6892 long n;
6893 int upd_topline; /* When TRUE: update topline */
6894{
6895 linenr_T lnum;
6896
6897 if (n > 0)
6898 {
6899 lnum = curwin->w_cursor.lnum;
6900#ifdef FEAT_FOLDING
6901 /* Move to last line of fold, will fail if it's the end-of-file. */
6902 (void)hasFolding(lnum, NULL, &lnum);
6903#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006904 /* This fails if the cursor is already in the last line or would move
6905 * beyound the last line and '-' is in 'cpoptions' */
6906 if (lnum >= curbuf->b_ml.ml_line_count
6907 || (lnum + n > curbuf->b_ml.ml_line_count
6908 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006909 return FAIL;
6910 if (lnum + n >= curbuf->b_ml.ml_line_count)
6911 lnum = curbuf->b_ml.ml_line_count;
6912 else
6913#ifdef FEAT_FOLDING
6914 if (hasAnyFolding(curwin))
6915 {
6916 linenr_T last;
6917
6918 /* count each sequence of folded lines as one logical line */
6919 while (n--)
6920 {
6921 if (hasFolding(lnum, NULL, &last))
6922 lnum = last + 1;
6923 else
6924 ++lnum;
6925 if (lnum >= curbuf->b_ml.ml_line_count)
6926 break;
6927 }
6928 if (lnum > curbuf->b_ml.ml_line_count)
6929 lnum = curbuf->b_ml.ml_line_count;
6930 }
6931 else
6932#endif
6933 lnum += n;
6934 curwin->w_cursor.lnum = lnum;
6935 }
6936
6937 /* try to advance to the column we want to be at */
6938 coladvance(curwin->w_curswant);
6939
6940 if (upd_topline)
6941 update_topline(); /* make sure curwin->w_topline is valid */
6942
6943 return OK;
6944}
6945
6946/*
6947 * Stuff the last inserted text in the read buffer.
6948 * Last_insert actually is a copy of the redo buffer, so we
6949 * first have to remove the command.
6950 */
6951 int
6952stuff_inserted(c, count, no_esc)
6953 int c; /* Command character to be inserted */
6954 long count; /* Repeat this many times */
6955 int no_esc; /* Don't add an ESC at the end */
6956{
6957 char_u *esc_ptr;
6958 char_u *ptr;
6959 char_u *last_ptr;
6960 char_u last = NUL;
6961
6962 ptr = get_last_insert();
6963 if (ptr == NULL)
6964 {
6965 EMSG(_(e_noinstext));
6966 return FAIL;
6967 }
6968
6969 /* may want to stuff the command character, to start Insert mode */
6970 if (c != NUL)
6971 stuffcharReadbuff(c);
6972 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6973 *esc_ptr = NUL; /* remove the ESC */
6974
6975 /* when the last char is either "0" or "^" it will be quoted if no ESC
6976 * comes after it OR if it will inserted more than once and "ptr"
6977 * starts with ^D. -- Acevedo
6978 */
6979 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6980 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6981 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6982 {
6983 last = *last_ptr;
6984 *last_ptr = NUL;
6985 }
6986
6987 do
6988 {
6989 stuffReadbuff(ptr);
6990 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6991 if (last)
6992 stuffReadbuff((char_u *)(last == '0'
6993 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6994 : IF_EB("\026^", CTRL_V_STR "^")));
6995 }
6996 while (--count > 0);
6997
6998 if (last)
6999 *last_ptr = last;
7000
7001 if (esc_ptr != NULL)
7002 *esc_ptr = ESC; /* put the ESC back */
7003
7004 /* may want to stuff a trailing ESC, to get out of Insert mode */
7005 if (!no_esc)
7006 stuffcharReadbuff(ESC);
7007
7008 return OK;
7009}
7010
7011 char_u *
7012get_last_insert()
7013{
7014 if (last_insert == NULL)
7015 return NULL;
7016 return last_insert + last_insert_skip;
7017}
7018
7019/*
7020 * Get last inserted string, and remove trailing <Esc>.
7021 * Returns pointer to allocated memory (must be freed) or NULL.
7022 */
7023 char_u *
7024get_last_insert_save()
7025{
7026 char_u *s;
7027 int len;
7028
7029 if (last_insert == NULL)
7030 return NULL;
7031 s = vim_strsave(last_insert + last_insert_skip);
7032 if (s != NULL)
7033 {
7034 len = (int)STRLEN(s);
7035 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7036 s[len - 1] = NUL;
7037 }
7038 return s;
7039}
7040
7041/*
7042 * Check the word in front of the cursor for an abbreviation.
7043 * Called when the non-id character "c" has been entered.
7044 * When an abbreviation is recognized it is removed from the text and
7045 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7046 */
7047 static int
7048echeck_abbr(c)
7049 int c;
7050{
7051 /* Don't check for abbreviation in paste mode, when disabled and just
7052 * after moving around with cursor keys. */
7053 if (p_paste || no_abbr || arrow_used)
7054 return FALSE;
7055
7056 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7057 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7058}
7059
7060/*
7061 * replace-stack functions
7062 *
7063 * When replacing characters, the replaced characters are remembered for each
7064 * new character. This is used to re-insert the old text when backspacing.
7065 *
7066 * There is a NUL headed list of characters for each character that is
7067 * currently in the file after the insertion point. When BS is used, one NUL
7068 * headed list is put back for the deleted character.
7069 *
7070 * For a newline, there are two NUL headed lists. One contains the characters
7071 * that the NL replaced. The extra one stores the characters after the cursor
7072 * that were deleted (always white space).
7073 *
7074 * Replace_offset is normally 0, in which case replace_push will add a new
7075 * character at the end of the stack. If replace_offset is not 0, that many
7076 * characters will be left on the stack above the newly inserted character.
7077 */
7078
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007079static char_u *replace_stack = NULL;
7080static long replace_stack_nr = 0; /* next entry in replace stack */
7081static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007082
7083 void
7084replace_push(c)
7085 int c; /* character that is replaced (NUL is none) */
7086{
7087 char_u *p;
7088
7089 if (replace_stack_nr < replace_offset) /* nothing to do */
7090 return;
7091 if (replace_stack_len <= replace_stack_nr)
7092 {
7093 replace_stack_len += 50;
7094 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7095 if (p == NULL) /* out of memory */
7096 {
7097 replace_stack_len -= 50;
7098 return;
7099 }
7100 if (replace_stack != NULL)
7101 {
7102 mch_memmove(p, replace_stack,
7103 (size_t)(replace_stack_nr * sizeof(char_u)));
7104 vim_free(replace_stack);
7105 }
7106 replace_stack = p;
7107 }
7108 p = replace_stack + replace_stack_nr - replace_offset;
7109 if (replace_offset)
7110 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7111 *p = c;
7112 ++replace_stack_nr;
7113}
7114
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007115#if defined(FEAT_MBYTE) || defined(PROTO)
7116/*
7117 * Push a character onto the replace stack. Handles a multi-byte character in
7118 * reverse byte order, so that the first byte is popped off first.
7119 * Return the number of bytes done (includes composing characters).
7120 */
7121 int
7122replace_push_mb(p)
7123 char_u *p;
7124{
7125 int l = (*mb_ptr2len)(p);
7126 int j;
7127
7128 for (j = l - 1; j >= 0; --j)
7129 replace_push(p[j]);
7130 return l;
7131}
7132#endif
7133
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007134#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007135/*
7136 * call replace_push(c) with replace_offset set to the first NUL.
7137 */
7138 static void
7139replace_push_off(c)
7140 int c;
7141{
7142 char_u *p;
7143
7144 p = replace_stack + replace_stack_nr;
7145 for (replace_offset = 1; replace_offset < replace_stack_nr;
7146 ++replace_offset)
7147 if (*--p == NUL)
7148 break;
7149 replace_push(c);
7150 replace_offset = 0;
7151}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007153
7154/*
7155 * Pop one item from the replace stack.
7156 * return -1 if stack empty
7157 * return replaced character or NUL otherwise
7158 */
7159 static int
7160replace_pop()
7161{
7162 if (replace_stack_nr == 0)
7163 return -1;
7164 return (int)replace_stack[--replace_stack_nr];
7165}
7166
7167/*
7168 * Join the top two items on the replace stack. This removes to "off"'th NUL
7169 * encountered.
7170 */
7171 static void
7172replace_join(off)
7173 int off; /* offset for which NUL to remove */
7174{
7175 int i;
7176
7177 for (i = replace_stack_nr; --i >= 0; )
7178 if (replace_stack[i] == NUL && off-- <= 0)
7179 {
7180 --replace_stack_nr;
7181 mch_memmove(replace_stack + i, replace_stack + i + 1,
7182 (size_t)(replace_stack_nr - i));
7183 return;
7184 }
7185}
7186
7187/*
7188 * Pop bytes from the replace stack until a NUL is found, and insert them
7189 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7190 */
7191 static void
7192replace_pop_ins()
7193{
7194 int cc;
7195 int oldState = State;
7196
7197 State = NORMAL; /* don't want REPLACE here */
7198 while ((cc = replace_pop()) > 0)
7199 {
7200#ifdef FEAT_MBYTE
7201 mb_replace_pop_ins(cc);
7202#else
7203 ins_char(cc);
7204#endif
7205 dec_cursor();
7206 }
7207 State = oldState;
7208}
7209
7210#ifdef FEAT_MBYTE
7211/*
7212 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7213 * indicates a multi-byte char, pop the other bytes too.
7214 */
7215 static void
7216mb_replace_pop_ins(cc)
7217 int cc;
7218{
7219 int n;
7220 char_u buf[MB_MAXBYTES];
7221 int i;
7222 int c;
7223
7224 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7225 {
7226 buf[0] = cc;
7227 for (i = 1; i < n; ++i)
7228 buf[i] = replace_pop();
7229 ins_bytes_len(buf, n);
7230 }
7231 else
7232 ins_char(cc);
7233
7234 if (enc_utf8)
7235 /* Handle composing chars. */
7236 for (;;)
7237 {
7238 c = replace_pop();
7239 if (c == -1) /* stack empty */
7240 break;
7241 if ((n = MB_BYTE2LEN(c)) == 1)
7242 {
7243 /* Not a multi-byte char, put it back. */
7244 replace_push(c);
7245 break;
7246 }
7247 else
7248 {
7249 buf[0] = c;
7250 for (i = 1; i < n; ++i)
7251 buf[i] = replace_pop();
7252 if (utf_iscomposing(utf_ptr2char(buf)))
7253 ins_bytes_len(buf, n);
7254 else
7255 {
7256 /* Not a composing char, put it back. */
7257 for (i = n - 1; i >= 0; --i)
7258 replace_push(buf[i]);
7259 break;
7260 }
7261 }
7262 }
7263}
7264#endif
7265
7266/*
7267 * make the replace stack empty
7268 * (called when exiting replace mode)
7269 */
7270 static void
7271replace_flush()
7272{
7273 vim_free(replace_stack);
7274 replace_stack = NULL;
7275 replace_stack_len = 0;
7276 replace_stack_nr = 0;
7277}
7278
7279/*
7280 * Handle doing a BS for one character.
7281 * cc < 0: replace stack empty, just move cursor
7282 * cc == 0: character was inserted, delete it
7283 * cc > 0: character was replaced, put cc (first byte of original char) back
7284 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007285 * When "limit_col" is >= 0, don't delete before this column. Matters when
7286 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007287 */
7288 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007289replace_do_bs(limit_col)
7290 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007291{
7292 int cc;
7293#ifdef FEAT_VREPLACE
7294 int orig_len = 0;
7295 int ins_len;
7296 int orig_vcols = 0;
7297 colnr_T start_vcol;
7298 char_u *p;
7299 int i;
7300 int vcol;
7301#endif
7302
7303 cc = replace_pop();
7304 if (cc > 0)
7305 {
7306#ifdef FEAT_VREPLACE
7307 if (State & VREPLACE_FLAG)
7308 {
7309 /* Get the number of screen cells used by the character we are
7310 * going to delete. */
7311 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7312 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7313 }
7314#endif
7315#ifdef FEAT_MBYTE
7316 if (has_mbyte)
7317 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007318 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007319# ifdef FEAT_VREPLACE
7320 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007321 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007322# endif
7323 replace_push(cc);
7324 }
7325 else
7326#endif
7327 {
7328 pchar_cursor(cc);
7329#ifdef FEAT_VREPLACE
7330 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007331 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007332#endif
7333 }
7334 replace_pop_ins();
7335
7336#ifdef FEAT_VREPLACE
7337 if (State & VREPLACE_FLAG)
7338 {
7339 /* Get the number of screen cells used by the inserted characters */
7340 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007341 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007342 vcol = start_vcol;
7343 for (i = 0; i < ins_len; ++i)
7344 {
7345 vcol += chartabsize(p + i, vcol);
7346#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007347 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007348#endif
7349 }
7350 vcol -= start_vcol;
7351
7352 /* Delete spaces that were inserted after the cursor to keep the
7353 * text aligned. */
7354 curwin->w_cursor.col += ins_len;
7355 while (vcol > orig_vcols && gchar_cursor() == ' ')
7356 {
7357 del_char(FALSE);
7358 ++orig_vcols;
7359 }
7360 curwin->w_cursor.col -= ins_len;
7361 }
7362#endif
7363
7364 /* mark the buffer as changed and prepare for displaying */
7365 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7366 }
7367 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007368 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369}
7370
7371#ifdef FEAT_CINDENT
7372/*
7373 * Return TRUE if C-indenting is on.
7374 */
7375 static int
7376cindent_on()
7377{
7378 return (!p_paste && (curbuf->b_p_cin
7379# ifdef FEAT_EVAL
7380 || *curbuf->b_p_inde != NUL
7381# endif
7382 ));
7383}
7384#endif
7385
7386#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7387/*
7388 * Re-indent the current line, based on the current contents of it and the
7389 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7390 * confused what all the part that handles Control-T is doing that I'm not.
7391 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7392 */
7393
7394 void
7395fixthisline(get_the_indent)
7396 int (*get_the_indent) __ARGS((void));
7397{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007398 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007399 if (linewhite(curwin->w_cursor.lnum))
7400 did_ai = TRUE; /* delete the indent if the line stays empty */
7401}
7402
7403 void
7404fix_indent()
7405{
7406 if (p_paste)
7407 return;
7408# ifdef FEAT_LISP
7409 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7410 fixthisline(get_lisp_indent);
7411# endif
7412# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7413 else
7414# endif
7415# ifdef FEAT_CINDENT
7416 if (cindent_on())
7417 do_c_expr_indent();
7418# endif
7419}
7420
7421#endif
7422
7423#ifdef FEAT_CINDENT
7424/*
7425 * return TRUE if 'cinkeys' contains the key "keytyped",
7426 * when == '*': Only if key is preceded with '*' (indent before insert)
7427 * when == '!': Only if key is prededed with '!' (don't insert)
7428 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7429 *
7430 * "keytyped" can have a few special values:
7431 * KEY_OPEN_FORW
7432 * KEY_OPEN_BACK
7433 * KEY_COMPLETE just finished completion.
7434 *
7435 * If line_is_empty is TRUE accept keys with '0' before them.
7436 */
7437 int
7438in_cinkeys(keytyped, when, line_is_empty)
7439 int keytyped;
7440 int when;
7441 int line_is_empty;
7442{
7443 char_u *look;
7444 int try_match;
7445 int try_match_word;
7446 char_u *p;
7447 char_u *line;
7448 int icase;
7449 int i;
7450
Bram Moolenaar30848942009-12-24 14:46:12 +00007451 if (keytyped == NUL)
7452 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7453 return FALSE;
7454
Bram Moolenaar071d4272004-06-13 20:20:40 +00007455#ifdef FEAT_EVAL
7456 if (*curbuf->b_p_inde != NUL)
7457 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7458 else
7459#endif
7460 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7461 while (*look)
7462 {
7463 /*
7464 * Find out if we want to try a match with this key, depending on
7465 * 'when' and a '*' or '!' before the key.
7466 */
7467 switch (when)
7468 {
7469 case '*': try_match = (*look == '*'); break;
7470 case '!': try_match = (*look == '!'); break;
7471 default: try_match = (*look != '*'); break;
7472 }
7473 if (*look == '*' || *look == '!')
7474 ++look;
7475
7476 /*
7477 * If there is a '0', only accept a match if the line is empty.
7478 * But may still match when typing last char of a word.
7479 */
7480 if (*look == '0')
7481 {
7482 try_match_word = try_match;
7483 if (!line_is_empty)
7484 try_match = FALSE;
7485 ++look;
7486 }
7487 else
7488 try_match_word = FALSE;
7489
7490 /*
7491 * does it look like a control character?
7492 */
7493 if (*look == '^'
7494#ifdef EBCDIC
7495 && (Ctrl_chr(look[1]) != 0)
7496#else
7497 && look[1] >= '?' && look[1] <= '_'
7498#endif
7499 )
7500 {
7501 if (try_match && keytyped == Ctrl_chr(look[1]))
7502 return TRUE;
7503 look += 2;
7504 }
7505 /*
7506 * 'o' means "o" command, open forward.
7507 * 'O' means "O" command, open backward.
7508 */
7509 else if (*look == 'o')
7510 {
7511 if (try_match && keytyped == KEY_OPEN_FORW)
7512 return TRUE;
7513 ++look;
7514 }
7515 else if (*look == 'O')
7516 {
7517 if (try_match && keytyped == KEY_OPEN_BACK)
7518 return TRUE;
7519 ++look;
7520 }
7521
7522 /*
7523 * 'e' means to check for "else" at start of line and just before the
7524 * cursor.
7525 */
7526 else if (*look == 'e')
7527 {
7528 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7529 {
7530 p = ml_get_curline();
7531 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7532 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7533 return TRUE;
7534 }
7535 ++look;
7536 }
7537
7538 /*
7539 * ':' only causes an indent if it is at the end of a label or case
7540 * statement, or when it was before typing the ':' (to fix
7541 * class::method for C++).
7542 */
7543 else if (*look == ':')
7544 {
7545 if (try_match && keytyped == ':')
7546 {
7547 p = ml_get_curline();
7548 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7549 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007550 /* Need to get the line again after cin_islabel(). */
7551 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 if (curwin->w_cursor.col > 2
7553 && p[curwin->w_cursor.col - 1] == ':'
7554 && p[curwin->w_cursor.col - 2] == ':')
7555 {
7556 p[curwin->w_cursor.col - 1] = ' ';
7557 i = (cin_iscase(p) || cin_isscopedecl(p)
7558 || cin_islabel(30));
7559 p = ml_get_curline();
7560 p[curwin->w_cursor.col - 1] = ':';
7561 if (i)
7562 return TRUE;
7563 }
7564 }
7565 ++look;
7566 }
7567
7568
7569 /*
7570 * Is it a key in <>, maybe?
7571 */
7572 else if (*look == '<')
7573 {
7574 if (try_match)
7575 {
7576 /*
7577 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7578 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7579 * >, *, : and ! keys if they really really want to.
7580 */
7581 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7582 && keytyped == look[1])
7583 return TRUE;
7584
7585 if (keytyped == get_special_key_code(look + 1))
7586 return TRUE;
7587 }
7588 while (*look && *look != '>')
7589 look++;
7590 while (*look == '>')
7591 look++;
7592 }
7593
7594 /*
7595 * Is it a word: "=word"?
7596 */
7597 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7598 {
7599 ++look;
7600 if (*look == '~')
7601 {
7602 icase = TRUE;
7603 ++look;
7604 }
7605 else
7606 icase = FALSE;
7607 p = vim_strchr(look, ',');
7608 if (p == NULL)
7609 p = look + STRLEN(look);
7610 if ((try_match || try_match_word)
7611 && curwin->w_cursor.col >= (colnr_T)(p - look))
7612 {
7613 int match = FALSE;
7614
7615#ifdef FEAT_INS_EXPAND
7616 if (keytyped == KEY_COMPLETE)
7617 {
7618 char_u *s;
7619
7620 /* Just completed a word, check if it starts with "look".
7621 * search back for the start of a word. */
7622 line = ml_get_curline();
7623# ifdef FEAT_MBYTE
7624 if (has_mbyte)
7625 {
7626 char_u *n;
7627
7628 for (s = line + curwin->w_cursor.col; s > line; s = n)
7629 {
7630 n = mb_prevptr(line, s);
7631 if (!vim_iswordp(n))
7632 break;
7633 }
7634 }
7635 else
7636# endif
7637 for (s = line + curwin->w_cursor.col; s > line; --s)
7638 if (!vim_iswordc(s[-1]))
7639 break;
7640 if (s + (p - look) <= line + curwin->w_cursor.col
7641 && (icase
7642 ? MB_STRNICMP(s, look, p - look)
7643 : STRNCMP(s, look, p - look)) == 0)
7644 match = TRUE;
7645 }
7646 else
7647#endif
7648 /* TODO: multi-byte */
7649 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7650 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7651 {
7652 line = ml_get_cursor();
7653 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7654 || !vim_iswordc(line[-(p - look) - 1]))
7655 && (icase
7656 ? MB_STRNICMP(line - (p - look), look, p - look)
7657 : STRNCMP(line - (p - look), look, p - look))
7658 == 0)
7659 match = TRUE;
7660 }
7661 if (match && try_match_word && !try_match)
7662 {
7663 /* "0=word": Check if there are only blanks before the
7664 * word. */
7665 line = ml_get_curline();
7666 if ((int)(skipwhite(line) - line) !=
7667 (int)(curwin->w_cursor.col - (p - look)))
7668 match = FALSE;
7669 }
7670 if (match)
7671 return TRUE;
7672 }
7673 look = p;
7674 }
7675
7676 /*
7677 * ok, it's a boring generic character.
7678 */
7679 else
7680 {
7681 if (try_match && *look == keytyped)
7682 return TRUE;
7683 ++look;
7684 }
7685
7686 /*
7687 * Skip over ", ".
7688 */
7689 look = skip_to_option_part(look);
7690 }
7691 return FALSE;
7692}
7693#endif /* FEAT_CINDENT */
7694
7695#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7696/*
7697 * Map Hebrew keyboard when in hkmap mode.
7698 */
7699 int
7700hkmap(c)
7701 int c;
7702{
7703 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7704 {
7705 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7706 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7707 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7708 static char_u map[26] =
7709 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7710 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7711 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7712 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7713 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7714 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7715 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7716 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7717 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7718
7719 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7720 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7721 /* '-1'='sofit' */
7722 else if (c == 'x')
7723 return 'X';
7724 else if (c == 'q')
7725 return '\''; /* {geresh}={'} */
7726 else if (c == 246)
7727 return ' '; /* \"o --> ' ' for a german keyboard */
7728 else if (c == 228)
7729 return ' '; /* \"a --> ' ' -- / -- */
7730 else if (c == 252)
7731 return ' '; /* \"u --> ' ' -- / -- */
7732#ifdef EBCDIC
7733 else if (islower(c))
7734#else
7735 /* NOTE: islower() does not do the right thing for us on Linux so we
7736 * do this the same was as 5.7 and previous, so it works correctly on
7737 * all systems. Specifically, the e.g. Delete and Arrow keys are
7738 * munged and won't work if e.g. searching for Hebrew text.
7739 */
7740 else if (c >= 'a' && c <= 'z')
7741#endif
7742 return (int)(map[CharOrdLow(c)] + p_aleph);
7743 else
7744 return c;
7745 }
7746 else
7747 {
7748 switch (c)
7749 {
7750 case '`': return ';';
7751 case '/': return '.';
7752 case '\'': return ',';
7753 case 'q': return '/';
7754 case 'w': return '\'';
7755
7756 /* Hebrew letters - set offset from 'a' */
7757 case ',': c = '{'; break;
7758 case '.': c = 'v'; break;
7759 case ';': c = 't'; break;
7760 default: {
7761 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7762
7763#ifdef EBCDIC
7764 /* see note about islower() above */
7765 if (!islower(c))
7766#else
7767 if (c < 'a' || c > 'z')
7768#endif
7769 return c;
7770 c = str[CharOrdLow(c)];
7771 break;
7772 }
7773 }
7774
7775 return (int)(CharOrdLow(c) + p_aleph);
7776 }
7777}
7778#endif
7779
7780 static void
7781ins_reg()
7782{
7783 int need_redraw = FALSE;
7784 int regname;
7785 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007786#ifdef FEAT_VISUAL
7787 int vis_active = VIsual_active;
7788#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007789
7790 /*
7791 * If we are going to wait for a character, show a '"'.
7792 */
7793 pc_status = PC_STATUS_UNSET;
7794 if (redrawing() && !char_avail())
7795 {
7796 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007797 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007798
7799 edit_putchar('"', TRUE);
7800#ifdef FEAT_CMDL_INFO
7801 add_to_showcmd_c(Ctrl_R);
7802#endif
7803 }
7804
7805#ifdef USE_ON_FLY_SCROLL
7806 dont_scroll = TRUE; /* disallow scrolling here */
7807#endif
7808
7809 /*
7810 * Don't map the register name. This also prevents the mode message to be
7811 * deleted when ESC is hit.
7812 */
7813 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007814 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007815 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007816 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7817 {
7818 /* Get a third key for literal register insertion */
7819 literally = regname;
7820#ifdef FEAT_CMDL_INFO
7821 add_to_showcmd_c(literally);
7822#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007823 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007825 }
7826 --no_mapping;
7827
7828#ifdef FEAT_EVAL
7829 /*
7830 * Don't call u_sync() while getting the expression,
7831 * evaluating it or giving an error message for it!
7832 */
7833 ++no_u_sync;
7834 if (regname == '=')
7835 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007836# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007838# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007839 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007840# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 /* Restore the Input Method. */
7842 if (im_on)
7843 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007844# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007845 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007846 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7847 {
7848 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007851 else
7852 {
7853#endif
7854 if (literally == Ctrl_O || literally == Ctrl_P)
7855 {
7856 /* Append the command to the redo buffer. */
7857 AppendCharToRedobuff(Ctrl_R);
7858 AppendCharToRedobuff(literally);
7859 AppendCharToRedobuff(regname);
7860
7861 do_put(regname, BACKWARD, 1L,
7862 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7863 }
7864 else if (insert_reg(regname, literally) == FAIL)
7865 {
7866 vim_beep();
7867 need_redraw = TRUE; /* remove the '"' */
7868 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007869 else if (stop_insert_mode)
7870 /* When the '=' register was used and a function was invoked that
7871 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7872 * insert anything, need to remove the '"' */
7873 need_redraw = TRUE;
7874
Bram Moolenaar071d4272004-06-13 20:20:40 +00007875#ifdef FEAT_EVAL
7876 }
7877 --no_u_sync;
7878#endif
7879#ifdef FEAT_CMDL_INFO
7880 clear_showcmd();
7881#endif
7882
7883 /* If the inserted register is empty, we need to remove the '"' */
7884 if (need_redraw || stuff_empty())
7885 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007886
7887#ifdef FEAT_VISUAL
7888 /* Disallow starting Visual mode here, would get a weird mode. */
7889 if (!vis_active && VIsual_active)
7890 end_visual_mode();
7891#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007892}
7893
7894/*
7895 * CTRL-G commands in Insert mode.
7896 */
7897 static void
7898ins_ctrl_g()
7899{
7900 int c;
7901
7902#ifdef FEAT_INS_EXPAND
7903 /* Right after CTRL-X the cursor will be after the ruler. */
7904 setcursor();
7905#endif
7906
7907 /*
7908 * Don't map the second key. This also prevents the mode message to be
7909 * deleted when ESC is hit.
7910 */
7911 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007912 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913 --no_mapping;
7914 switch (c)
7915 {
7916 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7917 case K_UP:
7918 case Ctrl_K:
7919 case 'k': ins_up(TRUE);
7920 break;
7921
7922 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7923 case K_DOWN:
7924 case Ctrl_J:
7925 case 'j': ins_down(TRUE);
7926 break;
7927
7928 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007929 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007930 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007931
7932 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007933 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007934 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 break;
7936
7937 /* Unknown CTRL-G command, reserved for future expansion. */
7938 default: vim_beep();
7939 }
7940}
7941
7942/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007943 * CTRL-^ in Insert mode.
7944 */
7945 static void
7946ins_ctrl_hat()
7947{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007948 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007949 {
7950 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7951 if (State & LANGMAP)
7952 {
7953 curbuf->b_p_iminsert = B_IMODE_NONE;
7954 State &= ~LANGMAP;
7955 }
7956 else
7957 {
7958 curbuf->b_p_iminsert = B_IMODE_LMAP;
7959 State |= LANGMAP;
7960#ifdef USE_IM_CONTROL
7961 im_set_active(FALSE);
7962#endif
7963 }
7964 }
7965#ifdef USE_IM_CONTROL
7966 else
7967 {
7968 /* There are no ":lmap" mappings, toggle IM */
7969 if (im_get_status())
7970 {
7971 curbuf->b_p_iminsert = B_IMODE_NONE;
7972 im_set_active(FALSE);
7973 }
7974 else
7975 {
7976 curbuf->b_p_iminsert = B_IMODE_IM;
7977 State &= ~LANGMAP;
7978 im_set_active(TRUE);
7979 }
7980 }
7981#endif
7982 set_iminsert_global();
7983 showmode();
7984#ifdef FEAT_GUI
7985 /* may show different cursor shape or color */
7986 if (gui.in_use)
7987 gui_update_cursor(TRUE, FALSE);
7988#endif
7989#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7990 /* Show/unshow value of 'keymap' in status lines. */
7991 status_redraw_curbuf();
7992#endif
7993}
7994
7995/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007996 * Handle ESC in insert mode.
7997 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7998 * insert.
7999 */
8000 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008001ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008002 long *count;
8003 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008004 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005{
8006 int temp;
8007 static int disabled_redraw = FALSE;
8008
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008009#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008010 check_spell_redraw();
8011#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008012#if defined(FEAT_HANGULIN)
8013# if defined(ESC_CHG_TO_ENG_MODE)
8014 hangul_input_state_set(0);
8015# endif
8016 if (composing_hangul)
8017 {
8018 push_raw_key(composing_hangul_buffer, 2);
8019 composing_hangul = 0;
8020 }
8021#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022
8023 temp = curwin->w_cursor.col;
8024 if (disabled_redraw)
8025 {
8026 --RedrawingDisabled;
8027 disabled_redraw = FALSE;
8028 }
8029 if (!arrow_used)
8030 {
8031 /*
8032 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008033 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8034 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008035 */
8036 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008037 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038
8039 /*
8040 * Repeating insert may take a long time. Check for
8041 * interrupt now and then.
8042 */
8043 if (*count > 0)
8044 {
8045 line_breakcheck();
8046 if (got_int)
8047 *count = 0;
8048 }
8049
8050 if (--*count > 0) /* repeat what was typed */
8051 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008052 /* Vi repeats the insert without replacing characters. */
8053 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8054 State &= ~REPLACE_FLAG;
8055
Bram Moolenaar071d4272004-06-13 20:20:40 +00008056 (void)start_redo_ins();
8057 if (cmdchar == 'r' || cmdchar == 'v')
8058 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8059 ++RedrawingDisabled;
8060 disabled_redraw = TRUE;
8061 return FALSE; /* repeat the insert */
8062 }
8063 stop_insert(&curwin->w_cursor, TRUE);
8064 undisplay_dollar();
8065 }
8066
8067 /* When an autoindent was removed, curswant stays after the
8068 * indent */
8069 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8070 curwin->w_set_curswant = TRUE;
8071
8072 /* Remember the last Insert position in the '^ mark. */
8073 if (!cmdmod.keepjumps)
8074 curbuf->b_last_insert = curwin->w_cursor;
8075
8076 /*
8077 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008078 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008080 if (!nomove
8081 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082#ifdef FEAT_VIRTUALEDIT
8083 || curwin->w_cursor.coladd > 0
8084#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008085 )
8086 && (restart_edit == NUL
8087 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008089 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008090#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008091 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008092#ifdef FEAT_RIGHTLEFT
8093 && !revins_on
8094#endif
8095 )
8096 {
8097#ifdef FEAT_VIRTUALEDIT
8098 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8099 {
8100 oneleft();
8101 if (restart_edit != NUL)
8102 ++curwin->w_cursor.coladd;
8103 }
8104 else
8105#endif
8106 {
8107 --curwin->w_cursor.col;
8108#ifdef FEAT_MBYTE
8109 /* Correct cursor for multi-byte character. */
8110 if (has_mbyte)
8111 mb_adjust_cursor();
8112#endif
8113 }
8114 }
8115
8116#ifdef USE_IM_CONTROL
8117 /* Disable IM to allow typing English directly for Normal mode commands.
8118 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8119 * well). */
8120 if (!(State & LANGMAP))
8121 im_save_status(&curbuf->b_p_iminsert);
8122 im_set_active(FALSE);
8123#endif
8124
8125 State = NORMAL;
8126 /* need to position cursor again (e.g. when on a TAB ) */
8127 changed_cline_bef_curs();
8128
8129#ifdef FEAT_MOUSE
8130 setmouse();
8131#endif
8132#ifdef CURSOR_SHAPE
8133 ui_cursor_shape(); /* may show different cursor shape */
8134#endif
8135
8136 /*
8137 * When recording or for CTRL-O, need to display the new mode.
8138 * Otherwise remove the mode message.
8139 */
8140 if (Recording || restart_edit != NUL)
8141 showmode();
8142 else if (p_smd)
8143 MSG("");
8144
8145 return TRUE; /* exit Insert mode */
8146}
8147
8148#ifdef FEAT_RIGHTLEFT
8149/*
8150 * Toggle language: hkmap and revins_on.
8151 * Move to end of reverse inserted text.
8152 */
8153 static void
8154ins_ctrl_()
8155{
8156 if (revins_on && revins_chars && revins_scol >= 0)
8157 {
8158 while (gchar_cursor() != NUL && revins_chars--)
8159 ++curwin->w_cursor.col;
8160 }
8161 p_ri = !p_ri;
8162 revins_on = (State == INSERT && p_ri);
8163 if (revins_on)
8164 {
8165 revins_scol = curwin->w_cursor.col;
8166 revins_legal++;
8167 revins_chars = 0;
8168 undisplay_dollar();
8169 }
8170 else
8171 revins_scol = -1;
8172#ifdef FEAT_FKMAP
8173 if (p_altkeymap)
8174 {
8175 /*
8176 * to be consistent also for redo command, using '.'
8177 * set arrow_used to true and stop it - causing to redo
8178 * characters entered in one mode (normal/reverse insert).
8179 */
8180 arrow_used = TRUE;
8181 (void)stop_arrow();
8182 p_fkmap = curwin->w_p_rl ^ p_ri;
8183 if (p_fkmap && p_ri)
8184 State = INSERT;
8185 }
8186 else
8187#endif
8188 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8189 showmode();
8190}
8191#endif
8192
8193#ifdef FEAT_VISUAL
8194/*
8195 * If 'keymodel' contains "startsel", may start selection.
8196 * Returns TRUE when a CTRL-O and other keys stuffed.
8197 */
8198 static int
8199ins_start_select(c)
8200 int c;
8201{
8202 if (km_startsel)
8203 switch (c)
8204 {
8205 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 case K_PAGEUP:
8208 case K_KPAGEUP:
8209 case K_PAGEDOWN:
8210 case K_KPAGEDOWN:
8211# ifdef MACOS
8212 case K_LEFT:
8213 case K_RIGHT:
8214 case K_UP:
8215 case K_DOWN:
8216 case K_END:
8217 case K_HOME:
8218# endif
8219 if (!(mod_mask & MOD_MASK_SHIFT))
8220 break;
8221 /* FALLTHROUGH */
8222 case K_S_LEFT:
8223 case K_S_RIGHT:
8224 case K_S_UP:
8225 case K_S_DOWN:
8226 case K_S_END:
8227 case K_S_HOME:
8228 /* Start selection right away, the cursor can move with
8229 * CTRL-O when beyond the end of the line. */
8230 start_selection();
8231
8232 /* Execute the key in (insert) Select mode. */
8233 stuffcharReadbuff(Ctrl_O);
8234 if (mod_mask)
8235 {
8236 char_u buf[4];
8237
8238 buf[0] = K_SPECIAL;
8239 buf[1] = KS_MODIFIER;
8240 buf[2] = mod_mask;
8241 buf[3] = NUL;
8242 stuffReadbuff(buf);
8243 }
8244 stuffcharReadbuff(c);
8245 return TRUE;
8246 }
8247 return FALSE;
8248}
8249#endif
8250
8251/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008252 * <Insert> key in Insert mode: toggle insert/remplace mode.
8253 */
8254 static void
8255ins_insert(replaceState)
8256 int replaceState;
8257{
8258#ifdef FEAT_FKMAP
8259 if (p_fkmap && p_ri)
8260 {
8261 beep_flush();
8262 EMSG(farsi_text_3); /* encoded in Farsi */
8263 return;
8264 }
8265#endif
8266
8267#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008268# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008269 set_vim_var_string(VV_INSERTMODE,
8270 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008271# ifdef FEAT_VREPLACE
8272 replaceState == VREPLACE ? "v" :
8273# endif
8274 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008275# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008276 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8277#endif
8278 if (State & REPLACE_FLAG)
8279 State = INSERT | (State & LANGMAP);
8280 else
8281 State = replaceState | (State & LANGMAP);
8282 AppendCharToRedobuff(K_INS);
8283 showmode();
8284#ifdef CURSOR_SHAPE
8285 ui_cursor_shape(); /* may show different cursor shape */
8286#endif
8287}
8288
8289/*
8290 * Pressed CTRL-O in Insert mode.
8291 */
8292 static void
8293ins_ctrl_o()
8294{
8295#ifdef FEAT_VREPLACE
8296 if (State & VREPLACE_FLAG)
8297 restart_edit = 'V';
8298 else
8299#endif
8300 if (State & REPLACE_FLAG)
8301 restart_edit = 'R';
8302 else
8303 restart_edit = 'I';
8304#ifdef FEAT_VIRTUALEDIT
8305 if (virtual_active())
8306 ins_at_eol = FALSE; /* cursor always keeps its column */
8307 else
8308#endif
8309 ins_at_eol = (gchar_cursor() == NUL);
8310}
8311
8312/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 * If the cursor is on an indent, ^T/^D insert/delete one
8314 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008315 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 * with vi. But vi only supports ^T and ^D after an
8317 * autoindent, we support it everywhere.
8318 */
8319 static void
8320ins_shift(c, lastc)
8321 int c;
8322 int lastc;
8323{
8324 if (stop_arrow() == FAIL)
8325 return;
8326 AppendCharToRedobuff(c);
8327
8328 /*
8329 * 0^D and ^^D: remove all indent.
8330 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008331 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8332 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {
8334 --curwin->w_cursor.col;
8335 (void)del_char(FALSE); /* delete the '^' or '0' */
8336 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8337 if (State & REPLACE_FLAG)
8338 replace_pop_ins();
8339 if (lastc == '^')
8340 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008341 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008342 }
8343 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008344 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345
8346 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8347 did_ai = FALSE;
8348#ifdef FEAT_SMARTINDENT
8349 did_si = FALSE;
8350 can_si = FALSE;
8351 can_si_back = FALSE;
8352#endif
8353#ifdef FEAT_CINDENT
8354 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8355#endif
8356}
8357
8358 static void
8359ins_del()
8360{
8361 int temp;
8362
8363 if (stop_arrow() == FAIL)
8364 return;
8365 if (gchar_cursor() == NUL) /* delete newline */
8366 {
8367 temp = curwin->w_cursor.col;
8368 if (!can_bs(BS_EOL) /* only if "eol" included */
8369 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8370 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8371 || do_join(FALSE) == FAIL)
8372 vim_beep();
8373 else
8374 curwin->w_cursor.col = temp;
8375 }
8376 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8377 vim_beep();
8378 did_ai = FALSE;
8379#ifdef FEAT_SMARTINDENT
8380 did_si = FALSE;
8381 can_si = FALSE;
8382 can_si_back = FALSE;
8383#endif
8384 AppendCharToRedobuff(K_DEL);
8385}
8386
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008387static void ins_bs_one __ARGS((colnr_T *vcolp));
8388
8389/*
8390 * Delete one character for ins_bs().
8391 */
8392 static void
8393ins_bs_one(vcolp)
8394 colnr_T *vcolp;
8395{
8396 dec_cursor();
8397 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8398 if (State & REPLACE_FLAG)
8399 {
8400 /* Don't delete characters before the insert point when in
8401 * Replace mode */
8402 if (curwin->w_cursor.lnum != Insstart.lnum
8403 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008404 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008405 }
8406 else
8407 (void)del_char(FALSE);
8408}
8409
Bram Moolenaar071d4272004-06-13 20:20:40 +00008410/*
8411 * Handle Backspace, delete-word and delete-line in Insert mode.
8412 * Return TRUE when backspace was actually used.
8413 */
8414 static int
8415ins_bs(c, mode, inserted_space_p)
8416 int c;
8417 int mode;
8418 int *inserted_space_p;
8419{
8420 linenr_T lnum;
8421 int cc;
8422 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008423 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 colnr_T mincol;
8425 int did_backspace = FALSE;
8426 int in_indent;
8427 int oldState;
8428#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008429 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430#endif
8431
8432 /*
8433 * can't delete anything in an empty file
8434 * can't backup past first character in buffer
8435 * can't backup past starting point unless 'backspace' > 1
8436 * can backup to a previous line if 'backspace' == 0
8437 */
8438 if ( bufempty()
8439 || (
8440#ifdef FEAT_RIGHTLEFT
8441 !revins_on &&
8442#endif
8443 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8444 || (!can_bs(BS_START)
8445 && (arrow_used
8446 || (curwin->w_cursor.lnum == Insstart.lnum
8447 && curwin->w_cursor.col <= Insstart.col)))
8448 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8449 && curwin->w_cursor.col <= ai_col)
8450 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8451 {
8452 vim_beep();
8453 return FALSE;
8454 }
8455
8456 if (stop_arrow() == FAIL)
8457 return FALSE;
8458 in_indent = inindent(0);
8459#ifdef FEAT_CINDENT
8460 if (in_indent)
8461 can_cindent = FALSE;
8462#endif
8463#ifdef FEAT_COMMENTS
8464 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8465#endif
8466#ifdef FEAT_RIGHTLEFT
8467 if (revins_on) /* put cursor after last inserted char */
8468 inc_cursor();
8469#endif
8470
8471#ifdef FEAT_VIRTUALEDIT
8472 /* Virtualedit:
8473 * BACKSPACE_CHAR eats a virtual space
8474 * BACKSPACE_WORD eats all coladd
8475 * BACKSPACE_LINE eats all coladd and keeps going
8476 */
8477 if (curwin->w_cursor.coladd > 0)
8478 {
8479 if (mode == BACKSPACE_CHAR)
8480 {
8481 --curwin->w_cursor.coladd;
8482 return TRUE;
8483 }
8484 if (mode == BACKSPACE_WORD)
8485 {
8486 curwin->w_cursor.coladd = 0;
8487 return TRUE;
8488 }
8489 curwin->w_cursor.coladd = 0;
8490 }
8491#endif
8492
8493 /*
8494 * delete newline!
8495 */
8496 if (curwin->w_cursor.col == 0)
8497 {
8498 lnum = Insstart.lnum;
8499 if (curwin->w_cursor.lnum == Insstart.lnum
8500#ifdef FEAT_RIGHTLEFT
8501 || revins_on
8502#endif
8503 )
8504 {
8505 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8506 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8507 return FALSE;
8508 --Insstart.lnum;
8509 Insstart.col = MAXCOL;
8510 }
8511 /*
8512 * In replace mode:
8513 * cc < 0: NL was inserted, delete it
8514 * cc >= 0: NL was replaced, put original characters back
8515 */
8516 cc = -1;
8517 if (State & REPLACE_FLAG)
8518 cc = replace_pop(); /* returns -1 if NL was inserted */
8519 /*
8520 * In replace mode, in the line we started replacing, we only move the
8521 * cursor.
8522 */
8523 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8524 {
8525 dec_cursor();
8526 }
8527 else
8528 {
8529#ifdef FEAT_VREPLACE
8530 if (!(State & VREPLACE_FLAG)
8531 || curwin->w_cursor.lnum > orig_line_count)
8532#endif
8533 {
8534 temp = gchar_cursor(); /* remember current char */
8535 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008536
8537 /* When "aw" is in 'formatoptions' we must delete the space at
8538 * the end of the line, otherwise the line will be broken
8539 * again when auto-formatting. */
8540 if (has_format_option(FO_AUTO)
8541 && has_format_option(FO_WHITE_PAR))
8542 {
8543 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8544 TRUE);
8545 int len;
8546
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008547 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008548 if (len > 0 && ptr[len - 1] == ' ')
8549 ptr[len - 1] = NUL;
8550 }
8551
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552 (void)do_join(FALSE);
8553 if (temp == NUL && gchar_cursor() != NUL)
8554 inc_cursor();
8555 }
8556#ifdef FEAT_VREPLACE
8557 else
8558 dec_cursor();
8559#endif
8560
8561 /*
8562 * In REPLACE mode we have to put back the text that was replaced
8563 * by the NL. On the replace stack is first a NUL-terminated
8564 * sequence of characters that were deleted and then the
8565 * characters that NL replaced.
8566 */
8567 if (State & REPLACE_FLAG)
8568 {
8569 /*
8570 * Do the next ins_char() in NORMAL state, to
8571 * prevent ins_char() from replacing characters and
8572 * avoiding showmatch().
8573 */
8574 oldState = State;
8575 State = NORMAL;
8576 /*
8577 * restore characters (blanks) deleted after cursor
8578 */
8579 while (cc > 0)
8580 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008581 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582#ifdef FEAT_MBYTE
8583 mb_replace_pop_ins(cc);
8584#else
8585 ins_char(cc);
8586#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008587 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008588 cc = replace_pop();
8589 }
8590 /* restore the characters that NL replaced */
8591 replace_pop_ins();
8592 State = oldState;
8593 }
8594 }
8595 did_ai = FALSE;
8596 }
8597 else
8598 {
8599 /*
8600 * Delete character(s) before the cursor.
8601 */
8602#ifdef FEAT_RIGHTLEFT
8603 if (revins_on) /* put cursor on last inserted char */
8604 dec_cursor();
8605#endif
8606 mincol = 0;
8607 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008608 if (mode == BACKSPACE_LINE
8609 && (curbuf->b_p_ai
8610#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008611 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008612#endif
8613 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008614#ifdef FEAT_RIGHTLEFT
8615 && !revins_on
8616#endif
8617 )
8618 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008619 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008620 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008621 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008623 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 }
8625
8626 /*
8627 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8628 */
8629 if ( mode == BACKSPACE_CHAR
8630 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008631 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008632 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633 && (*(ml_get_cursor() - 1) == TAB
8634 || (*(ml_get_cursor() - 1) == ' '
8635 && (!*inserted_space_p
8636 || arrow_used))))))
8637 {
8638 int ts;
8639 colnr_T vcol;
8640 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008641 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642
8643 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008644 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 ts = curbuf->b_p_sw;
8646 else
8647 ts = curbuf->b_p_sts;
8648 /* Compute the virtual column where we want to be. Since
8649 * 'showbreak' may get in the way, need to get the last column of
8650 * the previous character. */
8651 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008652 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 dec_cursor();
8654 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8655 inc_cursor();
8656 want_vcol = (want_vcol / ts) * ts;
8657
8658 /* delete characters until we are at or before want_vcol */
8659 while (vcol > want_vcol
8660 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008661 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
8663 /* insert extra spaces until we are at want_vcol */
8664 while (vcol < want_vcol)
8665 {
8666 /* Remember the first char we inserted */
8667 if (curwin->w_cursor.lnum == Insstart.lnum
8668 && curwin->w_cursor.col < Insstart.col)
8669 Insstart.col = curwin->w_cursor.col;
8670
8671#ifdef FEAT_VREPLACE
8672 if (State & VREPLACE_FLAG)
8673 ins_char(' ');
8674 else
8675#endif
8676 {
8677 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008678 if ((State & REPLACE_FLAG))
8679 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008680 }
8681 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8682 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008683
8684 /* If we are now back where we started delete one character. Can
8685 * happen when using 'sts' and 'linebreak'. */
8686 if (vcol >= start_vcol)
8687 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 }
8689
8690 /*
8691 * Delete upto starting point, start of line or previous word.
8692 */
8693 else do
8694 {
8695#ifdef FEAT_RIGHTLEFT
8696 if (!revins_on) /* put cursor on char to be deleted */
8697#endif
8698 dec_cursor();
8699
8700 /* start of word? */
8701 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8702 {
8703 mode = BACKSPACE_WORD_NOT_SPACE;
8704 temp = vim_iswordc(gchar_cursor());
8705 }
8706 /* end of word? */
8707 else if (mode == BACKSPACE_WORD_NOT_SPACE
8708 && (vim_isspace(cc = gchar_cursor())
8709 || vim_iswordc(cc) != temp))
8710 {
8711#ifdef FEAT_RIGHTLEFT
8712 if (!revins_on)
8713#endif
8714 inc_cursor();
8715#ifdef FEAT_RIGHTLEFT
8716 else if (State & REPLACE_FLAG)
8717 dec_cursor();
8718#endif
8719 break;
8720 }
8721 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008722 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 else
8724 {
8725#ifdef FEAT_MBYTE
8726 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008727 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728#endif
8729 (void)del_char(FALSE);
8730#ifdef FEAT_MBYTE
8731 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008732 * If there are combining characters and 'delcombine' is set
8733 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 * character.
8735 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008736 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 inc_cursor();
8738#endif
8739#ifdef FEAT_RIGHTLEFT
8740 if (revins_chars)
8741 {
8742 revins_chars--;
8743 revins_legal++;
8744 }
8745 if (revins_on && gchar_cursor() == NUL)
8746 break;
8747#endif
8748 }
8749 /* Just a single backspace?: */
8750 if (mode == BACKSPACE_CHAR)
8751 break;
8752 } while (
8753#ifdef FEAT_RIGHTLEFT
8754 revins_on ||
8755#endif
8756 (curwin->w_cursor.col > mincol
8757 && (curwin->w_cursor.lnum != Insstart.lnum
8758 || curwin->w_cursor.col != Insstart.col)));
8759 did_backspace = TRUE;
8760 }
8761#ifdef FEAT_SMARTINDENT
8762 did_si = FALSE;
8763 can_si = FALSE;
8764 can_si_back = FALSE;
8765#endif
8766 if (curwin->w_cursor.col <= 1)
8767 did_ai = FALSE;
8768 /*
8769 * It's a little strange to put backspaces into the redo
8770 * buffer, but it makes auto-indent a lot easier to deal
8771 * with.
8772 */
8773 AppendCharToRedobuff(c);
8774
8775 /* If deleted before the insertion point, adjust it */
8776 if (curwin->w_cursor.lnum == Insstart.lnum
8777 && curwin->w_cursor.col < Insstart.col)
8778 Insstart.col = curwin->w_cursor.col;
8779
8780 /* vi behaviour: the cursor moves backward but the character that
8781 * was there remains visible
8782 * Vim behaviour: the cursor moves backward and the character that
8783 * was there is erased from the screen.
8784 * We can emulate the vi behaviour by pretending there is a dollar
8785 * displayed even when there isn't.
8786 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8787 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8788 dollar_vcol = curwin->w_virtcol;
8789
Bram Moolenaarce3be472008-01-14 19:12:28 +00008790#ifdef FEAT_FOLDING
8791 /* When deleting a char the cursor line must never be in a closed fold.
8792 * E.g., when 'foldmethod' is indent and deleting the first non-white
8793 * char before a Tab. */
8794 if (did_backspace)
8795 foldOpenCursor();
8796#endif
8797
Bram Moolenaar071d4272004-06-13 20:20:40 +00008798 return did_backspace;
8799}
8800
8801#ifdef FEAT_MOUSE
8802 static void
8803ins_mouse(c)
8804 int c;
8805{
8806 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008807 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808
8809# ifdef FEAT_GUI
8810 /* When GUI is active, also move/paste when 'mouse' is empty */
8811 if (!gui.in_use)
8812# endif
8813 if (!mouse_has(MOUSE_INSERT))
8814 return;
8815
8816 undisplay_dollar();
8817 tpos = curwin->w_cursor;
8818 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8819 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008820#ifdef FEAT_WINDOWS
8821 win_T *new_curwin = curwin;
8822
8823 if (curwin != old_curwin && win_valid(old_curwin))
8824 {
8825 /* Mouse took us to another window. We need to go back to the
8826 * previous one to stop insert there properly. */
8827 curwin = old_curwin;
8828 curbuf = curwin->w_buffer;
8829 }
8830#endif
8831 start_arrow(curwin == old_curwin ? &tpos : NULL);
8832#ifdef FEAT_WINDOWS
8833 if (curwin != new_curwin && win_valid(new_curwin))
8834 {
8835 curwin = new_curwin;
8836 curbuf = curwin->w_buffer;
8837 }
8838#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839# ifdef FEAT_CINDENT
8840 can_cindent = TRUE;
8841# endif
8842 }
8843
8844#ifdef FEAT_WINDOWS
8845 /* redraw status lines (in case another window became active) */
8846 redraw_statuslines();
8847#endif
8848}
8849
8850 static void
8851ins_mousescroll(up)
8852 int up;
8853{
8854 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008855# if defined(FEAT_WINDOWS)
8856 win_T *old_curwin = curwin;
8857# endif
8858# ifdef FEAT_INS_EXPAND
8859 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860# endif
8861
8862 tpos = curwin->w_cursor;
8863
8864# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 /* Currently the mouse coordinates are only known in the GUI. */
8866 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8867 {
8868 int row, col;
8869
8870 row = mouse_row;
8871 col = mouse_col;
8872
8873 /* find the window at the pointer coordinates */
8874 curwin = mouse_find_win(&row, &col);
8875 curbuf = curwin->w_buffer;
8876 }
8877 if (curwin == old_curwin)
8878# endif
8879 undisplay_dollar();
8880
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008881# ifdef FEAT_INS_EXPAND
8882 /* Don't scroll the window in which completion is being done. */
8883 if (!pum_visible()
8884# if defined(FEAT_WINDOWS)
8885 || curwin != old_curwin
8886# endif
8887 )
8888# endif
8889 {
8890 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8891 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8892 else
8893 scroll_redraw(up, 3L);
8894# ifdef FEAT_INS_EXPAND
8895 did_scroll = TRUE;
8896# endif
8897 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008898
8899# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8900 curwin->w_redr_status = TRUE;
8901
8902 curwin = old_curwin;
8903 curbuf = curwin->w_buffer;
8904# endif
8905
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008906# ifdef FEAT_INS_EXPAND
8907 /* The popup menu may overlay the window, need to redraw it.
8908 * TODO: Would be more efficient to only redraw the windows that are
8909 * overlapped by the popup menu. */
8910 if (pum_visible() && did_scroll)
8911 {
8912 redraw_all_later(NOT_VALID);
8913 ins_compl_show_pum();
8914 }
8915# endif
8916
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 if (!equalpos(curwin->w_cursor, tpos))
8918 {
8919 start_arrow(&tpos);
8920# ifdef FEAT_CINDENT
8921 can_cindent = TRUE;
8922# endif
8923 }
8924}
8925#endif
8926
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008927#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008928 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008929ins_tabline(c)
8930 int c;
8931{
8932 /* We will be leaving the current window, unless closing another tab. */
8933 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8934 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8935 {
8936 undisplay_dollar();
8937 start_arrow(&curwin->w_cursor);
8938# ifdef FEAT_CINDENT
8939 can_cindent = TRUE;
8940# endif
8941 }
8942
8943 if (c == K_TABLINE)
8944 goto_tabpage(current_tab);
8945 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008946 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008947 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008948 redraw_statuslines(); /* will redraw the tabline when needed */
8949 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008950}
8951#endif
8952
8953#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 void
8955ins_scroll()
8956{
8957 pos_T tpos;
8958
8959 undisplay_dollar();
8960 tpos = curwin->w_cursor;
8961 if (gui_do_scroll())
8962 {
8963 start_arrow(&tpos);
8964# ifdef FEAT_CINDENT
8965 can_cindent = TRUE;
8966# endif
8967 }
8968}
8969
8970 void
8971ins_horscroll()
8972{
8973 pos_T tpos;
8974
8975 undisplay_dollar();
8976 tpos = curwin->w_cursor;
8977 if (gui_do_horiz_scroll())
8978 {
8979 start_arrow(&tpos);
8980# ifdef FEAT_CINDENT
8981 can_cindent = TRUE;
8982# endif
8983 }
8984}
8985#endif
8986
8987 static void
8988ins_left()
8989{
8990 pos_T tpos;
8991
8992#ifdef FEAT_FOLDING
8993 if ((fdo_flags & FDO_HOR) && KeyTyped)
8994 foldOpenCursor();
8995#endif
8996 undisplay_dollar();
8997 tpos = curwin->w_cursor;
8998 if (oneleft() == OK)
8999 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009000#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9001 /* Only call start_arrow() when not busy with preediting, it will
9002 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9003 if (!im_is_preediting())
9004#endif
9005 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009006#ifdef FEAT_RIGHTLEFT
9007 /* If exit reversed string, position is fixed */
9008 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9009 revins_legal++;
9010 revins_chars++;
9011#endif
9012 }
9013
9014 /*
9015 * if 'whichwrap' set for cursor in insert mode may go to
9016 * previous line
9017 */
9018 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9019 {
9020 start_arrow(&tpos);
9021 --(curwin->w_cursor.lnum);
9022 coladvance((colnr_T)MAXCOL);
9023 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9024 }
9025 else
9026 vim_beep();
9027}
9028
9029 static void
9030ins_home(c)
9031 int c;
9032{
9033 pos_T tpos;
9034
9035#ifdef FEAT_FOLDING
9036 if ((fdo_flags & FDO_HOR) && KeyTyped)
9037 foldOpenCursor();
9038#endif
9039 undisplay_dollar();
9040 tpos = curwin->w_cursor;
9041 if (c == K_C_HOME)
9042 curwin->w_cursor.lnum = 1;
9043 curwin->w_cursor.col = 0;
9044#ifdef FEAT_VIRTUALEDIT
9045 curwin->w_cursor.coladd = 0;
9046#endif
9047 curwin->w_curswant = 0;
9048 start_arrow(&tpos);
9049}
9050
9051 static void
9052ins_end(c)
9053 int c;
9054{
9055 pos_T tpos;
9056
9057#ifdef FEAT_FOLDING
9058 if ((fdo_flags & FDO_HOR) && KeyTyped)
9059 foldOpenCursor();
9060#endif
9061 undisplay_dollar();
9062 tpos = curwin->w_cursor;
9063 if (c == K_C_END)
9064 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9065 coladvance((colnr_T)MAXCOL);
9066 curwin->w_curswant = MAXCOL;
9067
9068 start_arrow(&tpos);
9069}
9070
9071 static void
9072ins_s_left()
9073{
9074#ifdef FEAT_FOLDING
9075 if ((fdo_flags & FDO_HOR) && KeyTyped)
9076 foldOpenCursor();
9077#endif
9078 undisplay_dollar();
9079 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9080 {
9081 start_arrow(&curwin->w_cursor);
9082 (void)bck_word(1L, FALSE, FALSE);
9083 curwin->w_set_curswant = TRUE;
9084 }
9085 else
9086 vim_beep();
9087}
9088
9089 static void
9090ins_right()
9091{
9092#ifdef FEAT_FOLDING
9093 if ((fdo_flags & FDO_HOR) && KeyTyped)
9094 foldOpenCursor();
9095#endif
9096 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009097 if (gchar_cursor() != NUL
9098#ifdef FEAT_VIRTUALEDIT
9099 || virtual_active()
9100#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009101 )
9102 {
9103 start_arrow(&curwin->w_cursor);
9104 curwin->w_set_curswant = TRUE;
9105#ifdef FEAT_VIRTUALEDIT
9106 if (virtual_active())
9107 oneright();
9108 else
9109#endif
9110 {
9111#ifdef FEAT_MBYTE
9112 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009113 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009114 else
9115#endif
9116 ++curwin->w_cursor.col;
9117 }
9118
9119#ifdef FEAT_RIGHTLEFT
9120 revins_legal++;
9121 if (revins_chars)
9122 revins_chars--;
9123#endif
9124 }
9125 /* if 'whichwrap' set for cursor in insert mode, may move the
9126 * cursor to the next line */
9127 else if (vim_strchr(p_ww, ']') != NULL
9128 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9129 {
9130 start_arrow(&curwin->w_cursor);
9131 curwin->w_set_curswant = TRUE;
9132 ++curwin->w_cursor.lnum;
9133 curwin->w_cursor.col = 0;
9134 }
9135 else
9136 vim_beep();
9137}
9138
9139 static void
9140ins_s_right()
9141{
9142#ifdef FEAT_FOLDING
9143 if ((fdo_flags & FDO_HOR) && KeyTyped)
9144 foldOpenCursor();
9145#endif
9146 undisplay_dollar();
9147 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9148 || gchar_cursor() != NUL)
9149 {
9150 start_arrow(&curwin->w_cursor);
9151 (void)fwd_word(1L, FALSE, 0);
9152 curwin->w_set_curswant = TRUE;
9153 }
9154 else
9155 vim_beep();
9156}
9157
9158 static void
9159ins_up(startcol)
9160 int startcol; /* when TRUE move to Insstart.col */
9161{
9162 pos_T tpos;
9163 linenr_T old_topline = curwin->w_topline;
9164#ifdef FEAT_DIFF
9165 int old_topfill = curwin->w_topfill;
9166#endif
9167
9168 undisplay_dollar();
9169 tpos = curwin->w_cursor;
9170 if (cursor_up(1L, TRUE) == OK)
9171 {
9172 if (startcol)
9173 coladvance(getvcol_nolist(&Insstart));
9174 if (old_topline != curwin->w_topline
9175#ifdef FEAT_DIFF
9176 || old_topfill != curwin->w_topfill
9177#endif
9178 )
9179 redraw_later(VALID);
9180 start_arrow(&tpos);
9181#ifdef FEAT_CINDENT
9182 can_cindent = TRUE;
9183#endif
9184 }
9185 else
9186 vim_beep();
9187}
9188
9189 static void
9190ins_pageup()
9191{
9192 pos_T tpos;
9193
9194 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009195
9196#ifdef FEAT_WINDOWS
9197 if (mod_mask & MOD_MASK_CTRL)
9198 {
9199 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009200 if (first_tabpage->tp_next != NULL)
9201 {
9202 start_arrow(&curwin->w_cursor);
9203 goto_tabpage(-1);
9204 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009205 return;
9206 }
9207#endif
9208
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209 tpos = curwin->w_cursor;
9210 if (onepage(BACKWARD, 1L) == OK)
9211 {
9212 start_arrow(&tpos);
9213#ifdef FEAT_CINDENT
9214 can_cindent = TRUE;
9215#endif
9216 }
9217 else
9218 vim_beep();
9219}
9220
9221 static void
9222ins_down(startcol)
9223 int startcol; /* when TRUE move to Insstart.col */
9224{
9225 pos_T tpos;
9226 linenr_T old_topline = curwin->w_topline;
9227#ifdef FEAT_DIFF
9228 int old_topfill = curwin->w_topfill;
9229#endif
9230
9231 undisplay_dollar();
9232 tpos = curwin->w_cursor;
9233 if (cursor_down(1L, TRUE) == OK)
9234 {
9235 if (startcol)
9236 coladvance(getvcol_nolist(&Insstart));
9237 if (old_topline != curwin->w_topline
9238#ifdef FEAT_DIFF
9239 || old_topfill != curwin->w_topfill
9240#endif
9241 )
9242 redraw_later(VALID);
9243 start_arrow(&tpos);
9244#ifdef FEAT_CINDENT
9245 can_cindent = TRUE;
9246#endif
9247 }
9248 else
9249 vim_beep();
9250}
9251
9252 static void
9253ins_pagedown()
9254{
9255 pos_T tpos;
9256
9257 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009258
9259#ifdef FEAT_WINDOWS
9260 if (mod_mask & MOD_MASK_CTRL)
9261 {
9262 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009263 if (first_tabpage->tp_next != NULL)
9264 {
9265 start_arrow(&curwin->w_cursor);
9266 goto_tabpage(0);
9267 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009268 return;
9269 }
9270#endif
9271
Bram Moolenaar071d4272004-06-13 20:20:40 +00009272 tpos = curwin->w_cursor;
9273 if (onepage(FORWARD, 1L) == OK)
9274 {
9275 start_arrow(&tpos);
9276#ifdef FEAT_CINDENT
9277 can_cindent = TRUE;
9278#endif
9279 }
9280 else
9281 vim_beep();
9282}
9283
9284#ifdef FEAT_DND
9285 static void
9286ins_drop()
9287{
9288 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9289}
9290#endif
9291
9292/*
9293 * Handle TAB in Insert or Replace mode.
9294 * Return TRUE when the TAB needs to be inserted like a normal character.
9295 */
9296 static int
9297ins_tab()
9298{
9299 int ind;
9300 int i;
9301 int temp;
9302
9303 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9304 Insstart_blank_vcol = get_nolist_virtcol();
9305 if (echeck_abbr(TAB + ABBR_OFF))
9306 return FALSE;
9307
9308 ind = inindent(0);
9309#ifdef FEAT_CINDENT
9310 if (ind)
9311 can_cindent = FALSE;
9312#endif
9313
9314 /*
9315 * When nothing special, insert TAB like a normal character
9316 */
9317 if (!curbuf->b_p_et
9318 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9319 && curbuf->b_p_sts == 0)
9320 return TRUE;
9321
9322 if (stop_arrow() == FAIL)
9323 return TRUE;
9324
9325 did_ai = FALSE;
9326#ifdef FEAT_SMARTINDENT
9327 did_si = FALSE;
9328 can_si = FALSE;
9329 can_si_back = FALSE;
9330#endif
9331 AppendToRedobuff((char_u *)"\t");
9332
9333 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9334 temp = (int)curbuf->b_p_sw;
9335 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9336 temp = (int)curbuf->b_p_sts;
9337 else /* otherwise use 'tabstop' */
9338 temp = (int)curbuf->b_p_ts;
9339 temp -= get_nolist_virtcol() % temp;
9340
9341 /*
9342 * Insert the first space with ins_char(). It will delete one char in
9343 * replace mode. Insert the rest with ins_str(); it will not delete any
9344 * chars. For VREPLACE mode, we use ins_char() for all characters.
9345 */
9346 ins_char(' ');
9347 while (--temp > 0)
9348 {
9349#ifdef FEAT_VREPLACE
9350 if (State & VREPLACE_FLAG)
9351 ins_char(' ');
9352 else
9353#endif
9354 {
9355 ins_str((char_u *)" ");
9356 if (State & REPLACE_FLAG) /* no char replaced */
9357 replace_push(NUL);
9358 }
9359 }
9360
9361 /*
9362 * When 'expandtab' not set: Replace spaces by TABs where possible.
9363 */
9364 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9365 {
9366 char_u *ptr;
9367#ifdef FEAT_VREPLACE
9368 char_u *saved_line = NULL; /* init for GCC */
9369 pos_T pos;
9370#endif
9371 pos_T fpos;
9372 pos_T *cursor;
9373 colnr_T want_vcol, vcol;
9374 int change_col = -1;
9375 int save_list = curwin->w_p_list;
9376
9377 /*
9378 * Get the current line. For VREPLACE mode, don't make real changes
9379 * yet, just work on a copy of the line.
9380 */
9381#ifdef FEAT_VREPLACE
9382 if (State & VREPLACE_FLAG)
9383 {
9384 pos = curwin->w_cursor;
9385 cursor = &pos;
9386 saved_line = vim_strsave(ml_get_curline());
9387 if (saved_line == NULL)
9388 return FALSE;
9389 ptr = saved_line + pos.col;
9390 }
9391 else
9392#endif
9393 {
9394 ptr = ml_get_cursor();
9395 cursor = &curwin->w_cursor;
9396 }
9397
9398 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9399 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9400 curwin->w_p_list = FALSE;
9401
9402 /* Find first white before the cursor */
9403 fpos = curwin->w_cursor;
9404 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9405 {
9406 --fpos.col;
9407 --ptr;
9408 }
9409
9410 /* In Replace mode, don't change characters before the insert point. */
9411 if ((State & REPLACE_FLAG)
9412 && fpos.lnum == Insstart.lnum
9413 && fpos.col < Insstart.col)
9414 {
9415 ptr += Insstart.col - fpos.col;
9416 fpos.col = Insstart.col;
9417 }
9418
9419 /* compute virtual column numbers of first white and cursor */
9420 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9421 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9422
9423 /* Use as many TABs as possible. Beware of 'showbreak' and
9424 * 'linebreak' adding extra virtual columns. */
9425 while (vim_iswhite(*ptr))
9426 {
9427 i = lbr_chartabsize((char_u *)"\t", vcol);
9428 if (vcol + i > want_vcol)
9429 break;
9430 if (*ptr != TAB)
9431 {
9432 *ptr = TAB;
9433 if (change_col < 0)
9434 {
9435 change_col = fpos.col; /* Column of first change */
9436 /* May have to adjust Insstart */
9437 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9438 Insstart.col = fpos.col;
9439 }
9440 }
9441 ++fpos.col;
9442 ++ptr;
9443 vcol += i;
9444 }
9445
9446 if (change_col >= 0)
9447 {
9448 int repl_off = 0;
9449
9450 /* Skip over the spaces we need. */
9451 while (vcol < want_vcol && *ptr == ' ')
9452 {
9453 vcol += lbr_chartabsize(ptr, vcol);
9454 ++ptr;
9455 ++repl_off;
9456 }
9457 if (vcol > want_vcol)
9458 {
9459 /* Must have a char with 'showbreak' just before it. */
9460 --ptr;
9461 --repl_off;
9462 }
9463 fpos.col += repl_off;
9464
9465 /* Delete following spaces. */
9466 i = cursor->col - fpos.col;
9467 if (i > 0)
9468 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009469 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009470 /* correct replace stack. */
9471 if ((State & REPLACE_FLAG)
9472#ifdef FEAT_VREPLACE
9473 && !(State & VREPLACE_FLAG)
9474#endif
9475 )
9476 for (temp = i; --temp >= 0; )
9477 replace_join(repl_off);
9478 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009479#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009480 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009481 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009482 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009483 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9484 (char_u *)"\t", 1);
9485 }
9486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009487 cursor->col -= i;
9488
9489#ifdef FEAT_VREPLACE
9490 /*
9491 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9492 * backspacing over the changed spacing and then inserting the new
9493 * spacing.
9494 */
9495 if (State & VREPLACE_FLAG)
9496 {
9497 /* Backspace from real cursor to change_col */
9498 backspace_until_column(change_col);
9499
9500 /* Insert each char in saved_line from changed_col to
9501 * ptr-cursor */
9502 ins_bytes_len(saved_line + change_col,
9503 cursor->col - change_col);
9504 }
9505#endif
9506 }
9507
9508#ifdef FEAT_VREPLACE
9509 if (State & VREPLACE_FLAG)
9510 vim_free(saved_line);
9511#endif
9512 curwin->w_p_list = save_list;
9513 }
9514
9515 return FALSE;
9516}
9517
9518/*
9519 * Handle CR or NL in insert mode.
9520 * Return TRUE when out of memory or can't undo.
9521 */
9522 static int
9523ins_eol(c)
9524 int c;
9525{
9526 int i;
9527
9528 if (echeck_abbr(c + ABBR_OFF))
9529 return FALSE;
9530 if (stop_arrow() == FAIL)
9531 return TRUE;
9532 undisplay_dollar();
9533
9534 /*
9535 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9536 * character under the cursor. Only push a NUL on the replace stack,
9537 * nothing to put back when the NL is deleted.
9538 */
9539 if ((State & REPLACE_FLAG)
9540#ifdef FEAT_VREPLACE
9541 && !(State & VREPLACE_FLAG)
9542#endif
9543 )
9544 replace_push(NUL);
9545
9546 /*
9547 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9548 * replacing the next line, so we push all of the characters left on the
9549 * line onto the replace stack. This is not done here though, it is done
9550 * in open_line().
9551 */
9552
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009553#ifdef FEAT_VIRTUALEDIT
9554 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9555 * CTRL-O). */
9556 if (virtual_active() && curwin->w_cursor.coladd > 0)
9557 coladvance(getviscol());
9558#endif
9559
Bram Moolenaar071d4272004-06-13 20:20:40 +00009560#ifdef FEAT_RIGHTLEFT
9561# ifdef FEAT_FKMAP
9562 if (p_altkeymap && p_fkmap)
9563 fkmap(NL);
9564# endif
9565 /* NL in reverse insert will always start in the end of
9566 * current line. */
9567 if (revins_on)
9568 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9569#endif
9570
9571 AppendToRedobuff(NL_STR);
9572 i = open_line(FORWARD,
9573#ifdef FEAT_COMMENTS
9574 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9575#endif
9576 0, old_indent);
9577 old_indent = 0;
9578#ifdef FEAT_CINDENT
9579 can_cindent = TRUE;
9580#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009581#ifdef FEAT_FOLDING
9582 /* When inserting a line the cursor line must never be in a closed fold. */
9583 foldOpenCursor();
9584#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009585
9586 return (!i);
9587}
9588
9589#ifdef FEAT_DIGRAPHS
9590/*
9591 * Handle digraph in insert mode.
9592 * Returns character still to be inserted, or NUL when nothing remaining to be
9593 * done.
9594 */
9595 static int
9596ins_digraph()
9597{
9598 int c;
9599 int cc;
9600
9601 pc_status = PC_STATUS_UNSET;
9602 if (redrawing() && !char_avail())
9603 {
9604 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009605 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606
9607 edit_putchar('?', TRUE);
9608#ifdef FEAT_CMDL_INFO
9609 add_to_showcmd_c(Ctrl_K);
9610#endif
9611 }
9612
9613#ifdef USE_ON_FLY_SCROLL
9614 dont_scroll = TRUE; /* disallow scrolling here */
9615#endif
9616
9617 /* don't map the digraph chars. This also prevents the
9618 * mode message to be deleted when ESC is hit */
9619 ++no_mapping;
9620 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009621 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622 --no_mapping;
9623 --allow_keys;
9624 if (IS_SPECIAL(c) || mod_mask) /* special key */
9625 {
9626#ifdef FEAT_CMDL_INFO
9627 clear_showcmd();
9628#endif
9629 insert_special(c, TRUE, FALSE);
9630 return NUL;
9631 }
9632 if (c != ESC)
9633 {
9634 if (redrawing() && !char_avail())
9635 {
9636 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009637 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638
9639 if (char2cells(c) == 1)
9640 {
9641 /* first remove the '?', otherwise it's restored when typing
9642 * an ESC next */
9643 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009644 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645 edit_putchar(c, TRUE);
9646 }
9647#ifdef FEAT_CMDL_INFO
9648 add_to_showcmd_c(c);
9649#endif
9650 }
9651 ++no_mapping;
9652 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009653 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654 --no_mapping;
9655 --allow_keys;
9656 if (cc != ESC)
9657 {
9658 AppendToRedobuff((char_u *)CTRL_V_STR);
9659 c = getdigraph(c, cc, TRUE);
9660#ifdef FEAT_CMDL_INFO
9661 clear_showcmd();
9662#endif
9663 return c;
9664 }
9665 }
9666 edit_unputchar();
9667#ifdef FEAT_CMDL_INFO
9668 clear_showcmd();
9669#endif
9670 return NUL;
9671}
9672#endif
9673
9674/*
9675 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9676 * Returns the char to be inserted, or NUL if none found.
9677 */
9678 static int
9679ins_copychar(lnum)
9680 linenr_T lnum;
9681{
9682 int c;
9683 int temp;
9684 char_u *ptr, *prev_ptr;
9685
9686 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9687 {
9688 vim_beep();
9689 return NUL;
9690 }
9691
9692 /* try to advance to the cursor column */
9693 temp = 0;
9694 ptr = ml_get(lnum);
9695 prev_ptr = ptr;
9696 validate_virtcol();
9697 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9698 {
9699 prev_ptr = ptr;
9700 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9701 }
9702 if ((colnr_T)temp > curwin->w_virtcol)
9703 ptr = prev_ptr;
9704
9705#ifdef FEAT_MBYTE
9706 c = (*mb_ptr2char)(ptr);
9707#else
9708 c = *ptr;
9709#endif
9710 if (c == NUL)
9711 vim_beep();
9712 return c;
9713}
9714
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009715/*
9716 * CTRL-Y or CTRL-E typed in Insert mode.
9717 */
9718 static int
9719ins_ctrl_ey(tc)
9720 int tc;
9721{
9722 int c = tc;
9723
9724#ifdef FEAT_INS_EXPAND
9725 if (ctrl_x_mode == CTRL_X_SCROLL)
9726 {
9727 if (c == Ctrl_Y)
9728 scrolldown_clamp();
9729 else
9730 scrollup_clamp();
9731 redraw_later(VALID);
9732 }
9733 else
9734#endif
9735 {
9736 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9737 if (c != NUL)
9738 {
9739 long tw_save;
9740
9741 /* The character must be taken literally, insert like it
9742 * was typed after a CTRL-V, and pretend 'textwidth'
9743 * wasn't set. Digits, 'o' and 'x' are special after a
9744 * CTRL-V, don't use it for these. */
9745 if (c < 256 && !isalnum(c))
9746 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9747 tw_save = curbuf->b_p_tw;
9748 curbuf->b_p_tw = -1;
9749 insert_special(c, TRUE, FALSE);
9750 curbuf->b_p_tw = tw_save;
9751#ifdef FEAT_RIGHTLEFT
9752 revins_chars++;
9753 revins_legal++;
9754#endif
9755 c = Ctrl_V; /* pretend CTRL-V is last character */
9756 auto_format(FALSE, TRUE);
9757 }
9758 }
9759 return c;
9760}
9761
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762#ifdef FEAT_SMARTINDENT
9763/*
9764 * Try to do some very smart auto-indenting.
9765 * Used when inserting a "normal" character.
9766 */
9767 static void
9768ins_try_si(c)
9769 int c;
9770{
9771 pos_T *pos, old_pos;
9772 char_u *ptr;
9773 int i;
9774 int temp;
9775
9776 /*
9777 * do some very smart indenting when entering '{' or '}'
9778 */
9779 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9780 {
9781 /*
9782 * for '}' set indent equal to indent of line containing matching '{'
9783 */
9784 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9785 {
9786 old_pos = curwin->w_cursor;
9787 /*
9788 * If the matching '{' has a ')' immediately before it (ignoring
9789 * white-space), then line up with the start of the line
9790 * containing the matching '(' if there is one. This handles the
9791 * case where an "if (..\n..) {" statement continues over multiple
9792 * lines -- webb
9793 */
9794 ptr = ml_get(pos->lnum);
9795 i = pos->col;
9796 if (i > 0) /* skip blanks before '{' */
9797 while (--i > 0 && vim_iswhite(ptr[i]))
9798 ;
9799 curwin->w_cursor.lnum = pos->lnum;
9800 curwin->w_cursor.col = i;
9801 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9802 curwin->w_cursor = *pos;
9803 i = get_indent();
9804 curwin->w_cursor = old_pos;
9805#ifdef FEAT_VREPLACE
9806 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009807 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808 else
9809#endif
9810 (void)set_indent(i, SIN_CHANGED);
9811 }
9812 else if (curwin->w_cursor.col > 0)
9813 {
9814 /*
9815 * when inserting '{' after "O" reduce indent, but not
9816 * more than indent of previous line
9817 */
9818 temp = TRUE;
9819 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9820 {
9821 old_pos = curwin->w_cursor;
9822 i = get_indent();
9823 while (curwin->w_cursor.lnum > 1)
9824 {
9825 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9826
9827 /* ignore empty lines and lines starting with '#'. */
9828 if (*ptr != '#' && *ptr != NUL)
9829 break;
9830 }
9831 if (get_indent() >= i)
9832 temp = FALSE;
9833 curwin->w_cursor = old_pos;
9834 }
9835 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009836 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 }
9838 }
9839
9840 /*
9841 * set indent of '#' always to 0
9842 */
9843 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9844 {
9845 /* remember current indent for next line */
9846 old_indent = get_indent();
9847 (void)set_indent(0, SIN_CHANGED);
9848 }
9849
9850 /* Adjust ai_col, the char at this position can be deleted. */
9851 if (ai_col > curwin->w_cursor.col)
9852 ai_col = curwin->w_cursor.col;
9853}
9854#endif
9855
9856/*
9857 * Get the value that w_virtcol would have when 'list' is off.
9858 * Unless 'cpo' contains the 'L' flag.
9859 */
9860 static colnr_T
9861get_nolist_virtcol()
9862{
9863 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9864 return getvcol_nolist(&curwin->w_cursor);
9865 validate_virtcol();
9866 return curwin->w_virtcol;
9867}