blob: a6a28cc9a3a9f19594609c36ea26e1676dd80e07 [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
701 update_curswant();
702 old_topline = curwin->w_topline;
703#ifdef FEAT_DIFF
704 old_topfill = curwin->w_topfill;
705#endif
706
707#ifdef USE_ON_FLY_SCROLL
708 dont_scroll = FALSE; /* allow scrolling here */
709#endif
710
711 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000712 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000713 */
714 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000715 do
716 {
717 c = safe_vgetc();
718 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000719
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000720#ifdef FEAT_AUTOCMD
721 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
722 did_cursorhold = TRUE;
723#endif
724
Bram Moolenaar071d4272004-06-13 20:20:40 +0000725#ifdef FEAT_RIGHTLEFT
726 if (p_hkmap && KeyTyped)
727 c = hkmap(c); /* Hebrew mode mapping */
728#endif
729#ifdef FEAT_FKMAP
730 if (p_fkmap && KeyTyped)
731 c = fkmap(c); /* Farsi mode mapping */
732#endif
733
734#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000735 /*
736 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000737 * and the cursor is still in the completed word. Only when there is
738 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000739 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000740 if (compl_started
741 && pum_wanted()
742 && curwin->w_cursor.col >= compl_col
743 && (compl_shown_match == NULL
744 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000745 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000746 /* BS: Delete one character from "compl_leader". */
747 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000748 && curwin->w_cursor.col > compl_col
749 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000750 continue;
751
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000752 /* When no match was selected or it was edited. */
753 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000754 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000755 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000756 * "compl_leader". Except when at the original match and
757 * there is nothing to add, CTRL-L works like CTRL-P then. */
758 if (c == Ctrl_L
759 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000760 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000761 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000762 {
763 ins_compl_addfrommatch();
764 continue;
765 }
766
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000767 /* A non-white character that fits in with the current
768 * completion: Add to "compl_leader". */
769 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000770 {
771 ins_compl_addleader(c);
772 continue;
773 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000774
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000775 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000776 * compl_enter_selects is set the Enter key does the same. */
777 if (c == Ctrl_Y || (compl_enter_selects
778 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000779 {
780 ins_compl_delete();
781 ins_compl_insert();
782 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000783 }
784 }
785
Bram Moolenaar071d4272004-06-13 20:20:40 +0000786 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
787 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000788 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000789 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000790 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000791#endif
792
Bram Moolenaar488c6512005-08-11 20:09:58 +0000793 /* CTRL-\ CTRL-N goes to Normal mode,
794 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
795 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 if (c == Ctrl_BSL)
797 {
798 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000799 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 ++no_mapping;
801 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000802 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 --no_mapping;
804 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000805 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000807 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 vungetc(c);
809 c = Ctrl_BSL;
810 }
811 else if (c == Ctrl_G && p_im)
812 continue;
813 else
814 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000815 if (c == Ctrl_O)
816 {
817 ins_ctrl_o();
818 ins_at_eol = FALSE; /* cursor keeps its column */
819 nomove = TRUE;
820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 count = 0;
822 goto doESCkey;
823 }
824 }
825
826#ifdef FEAT_DIGRAPHS
827 c = do_digraph(c);
828#endif
829
830#ifdef FEAT_INS_EXPAND
831 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
832 goto docomplete;
833#endif
834 if (c == Ctrl_V || c == Ctrl_Q)
835 {
836 ins_ctrl_v();
837 c = Ctrl_V; /* pretend CTRL-V is last typed character */
838 continue;
839 }
840
841#ifdef FEAT_CINDENT
842 if (cindent_on()
843# ifdef FEAT_INS_EXPAND
844 && ctrl_x_mode == 0
845# endif
846 )
847 {
848 /* A key name preceded by a bang means this key is not to be
849 * inserted. Skip ahead to the re-indenting below.
850 * A key name preceded by a star means that indenting has to be
851 * done before inserting the key. */
852 line_is_white = inindent(0);
853 if (in_cinkeys(c, '!', line_is_white))
854 goto force_cindent;
855 if (can_cindent && in_cinkeys(c, '*', line_is_white)
856 && stop_arrow() == OK)
857 do_c_expr_indent();
858 }
859#endif
860
861#ifdef FEAT_RIGHTLEFT
862 if (curwin->w_p_rl)
863 switch (c)
864 {
865 case K_LEFT: c = K_RIGHT; break;
866 case K_S_LEFT: c = K_S_RIGHT; break;
867 case K_C_LEFT: c = K_C_RIGHT; break;
868 case K_RIGHT: c = K_LEFT; break;
869 case K_S_RIGHT: c = K_S_LEFT; break;
870 case K_C_RIGHT: c = K_C_LEFT; break;
871 }
872#endif
873
874#ifdef FEAT_VISUAL
875 /*
876 * If 'keymodel' contains "startsel", may start selection. If it
877 * does, a CTRL-O and c will be stuffed, we need to get these
878 * characters.
879 */
880 if (ins_start_select(c))
881 continue;
882#endif
883
884 /*
885 * The big switch to handle a character in insert mode.
886 */
887 switch (c)
888 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000889 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 if (echeck_abbr(ESC + ABBR_OFF))
891 break;
892 /*FALLTHROUGH*/
893
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000894 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000895#ifdef FEAT_CMDWIN
896 if (c == Ctrl_C && cmdwin_type != 0)
897 {
898 /* Close the cmdline window. */
899 cmdwin_result = K_IGNORE;
900 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000901 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 goto doESCkey;
903 }
904#endif
905
906#ifdef UNIX
907do_intr:
908#endif
909 /* when 'insertmode' set, and not halfway a mapping, don't leave
910 * Insert mode */
911 if (goto_im())
912 {
913 if (got_int)
914 {
915 (void)vgetc(); /* flush all buffers */
916 got_int = FALSE;
917 }
918 else
919 vim_beep();
920 break;
921 }
922doESCkey:
923 /*
924 * This is the ONLY return from edit()!
925 */
926 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
927 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000928 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000929 o_lnum = curwin->w_cursor.lnum;
930
Bram Moolenaar488c6512005-08-11 20:09:58 +0000931 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000932 {
933#ifdef FEAT_AUTOCMD
934 if (cmdchar != 'r' && cmdchar != 'v')
935 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
936 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000937 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000941 continue;
942
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000943 case Ctrl_Z: /* suspend when 'insertmode' set */
944 if (!p_im)
945 goto normalchar; /* insert CTRL-Z as normal char */
946 stuffReadbuff((char_u *)":st\r");
947 c = Ctrl_O;
948 /*FALLTHROUGH*/
949
950 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000951#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000952 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000953 goto docomplete;
954#endif
955 if (echeck_abbr(Ctrl_O + ABBR_OFF))
956 break;
957 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000958
959#ifdef FEAT_VIRTUALEDIT
960 /* don't move the cursor left when 'virtualedit' has "onemore". */
961 if (ve_flags & VE_ONEMORE)
962 {
963 ins_at_eol = FALSE;
964 nomove = TRUE;
965 }
966#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000967 count = 0;
968 goto doESCkey;
969
Bram Moolenaar572cb562005-08-05 21:35:02 +0000970 case K_INS: /* toggle insert/replace mode */
971 case K_KINS:
972 ins_insert(replaceState);
973 break;
974
975 case K_SELECT: /* end of Select mode mapping - ignore */
976 break;
977
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000978#ifdef FEAT_SNIFF
979 case K_SNIFF: /* Sniff command received */
980 stuffcharReadbuff(K_SNIFF);
981 goto doESCkey;
982#endif
983
984 case K_HELP: /* Help key works like <ESC> <Help> */
985 case K_F1:
986 case K_XF1:
987 stuffcharReadbuff(K_HELP);
988 if (p_im)
989 need_start_insertmode = TRUE;
990 goto doESCkey;
991
992#ifdef FEAT_NETBEANS_INTG
993 case K_F21: /* NetBeans command */
994 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000995 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000996 --no_mapping;
997 netbeans_keycommand(i);
998 break;
999#endif
1000
1001 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 case NUL:
1003 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001004 /* For ^@ the trailing ESC will end the insert, unless there is an
1005 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1007 && c != Ctrl_A && !p_im)
1008 goto doESCkey; /* quit insert mode */
1009 inserted_space = FALSE;
1010 break;
1011
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001012 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 ins_reg();
1014 auto_format(FALSE, TRUE);
1015 inserted_space = FALSE;
1016 break;
1017
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001019 ins_ctrl_g();
1020 break;
1021
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 case Ctrl_HAT: /* switch input mode and/or langmap */
1023 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 break;
1025
1026#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001027 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 if (!p_ari)
1029 goto normalchar;
1030 ins_ctrl_();
1031 break;
1032#endif
1033
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001034 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1036 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1037 goto docomplete;
1038#endif
1039 /* FALLTHROUGH */
1040
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042# ifdef FEAT_INS_EXPAND
1043 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1044 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045 if (has_compl_option(FALSE))
1046 goto docomplete;
1047 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048 }
1049# endif
1050 ins_shift(c, lastc);
1051 auto_format(FALSE, TRUE);
1052 inserted_space = FALSE;
1053 break;
1054
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056 case K_KDEL:
1057 ins_del();
1058 auto_format(FALSE, TRUE);
1059 break;
1060
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001061 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062 case Ctrl_H:
1063 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1064 auto_format(FALSE, TRUE);
1065 break;
1066
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001067 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1069 auto_format(FALSE, TRUE);
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001073# ifdef FEAT_COMPL_FUNC
1074 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001075 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001076 goto docomplete;
1077# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1079 auto_format(FALSE, TRUE);
1080 inserted_space = FALSE;
1081 break;
1082
1083#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001084 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085 case K_LEFTMOUSE_NM:
1086 case K_LEFTDRAG:
1087 case K_LEFTRELEASE:
1088 case K_LEFTRELEASE_NM:
1089 case K_MIDDLEMOUSE:
1090 case K_MIDDLEDRAG:
1091 case K_MIDDLERELEASE:
1092 case K_RIGHTMOUSE:
1093 case K_RIGHTDRAG:
1094 case K_RIGHTRELEASE:
1095 case K_X1MOUSE:
1096 case K_X1DRAG:
1097 case K_X1RELEASE:
1098 case K_X2MOUSE:
1099 case K_X2DRAG:
1100 case K_X2RELEASE:
1101 ins_mouse(c);
1102 break;
1103
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ins_mousescroll(FALSE);
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_mousescroll(TRUE);
1110 break;
1111#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001112#ifdef FEAT_GUI_TABLINE
1113 case K_TABLINE:
1114 case K_TABMENU:
1115 ins_tabline(c);
1116 break;
1117#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001118
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001119 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001120 break;
1121
Bram Moolenaar754b5602006-02-09 23:53:20 +00001122#ifdef FEAT_AUTOCMD
1123 case K_CURSORHOLD: /* Didn't type something for a while. */
1124 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1125 did_cursorhold = TRUE;
1126 break;
1127#endif
1128
Bram Moolenaar4770d092006-01-12 23:22:24 +00001129#ifdef FEAT_GUI_W32
1130 /* On Win32 ignore <M-F4>, we get it when closing the window was
1131 * cancelled. */
1132 case K_F4:
1133 if (mod_mask != MOD_MASK_ALT)
1134 goto normalchar;
1135 break;
1136#endif
1137
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138#ifdef FEAT_GUI
1139 case K_VER_SCROLLBAR:
1140 ins_scroll();
1141 break;
1142
1143 case K_HOR_SCROLLBAR:
1144 ins_horscroll();
1145 break;
1146#endif
1147
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001148 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001149 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001150 case K_S_HOME:
1151 case K_C_HOME:
1152 ins_home(c);
1153 break;
1154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001155 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157 case K_S_END:
1158 case K_C_END:
1159 ins_end(c);
1160 break;
1161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001162 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001163 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1164 ins_s_left();
1165 else
1166 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 break;
1168
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001169 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001170 case K_C_LEFT:
1171 ins_s_left();
1172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001175 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1176 ins_s_right();
1177 else
1178 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001179 break;
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 case K_C_RIGHT:
1183 ins_s_right();
1184 break;
1185
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001186 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001187#ifdef FEAT_INS_EXPAND
1188 if (pum_visible())
1189 goto docomplete;
1190#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001191 if (mod_mask & MOD_MASK_SHIFT)
1192 ins_pageup();
1193 else
1194 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 break;
1196
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001197 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 case K_PAGEUP:
1199 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001200#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001201 if (pum_visible())
1202 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001203#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001204 ins_pageup();
1205 break;
1206
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001207 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001208#ifdef FEAT_INS_EXPAND
1209 if (pum_visible())
1210 goto docomplete;
1211#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001212 if (mod_mask & MOD_MASK_SHIFT)
1213 ins_pagedown();
1214 else
1215 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 break;
1217
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001218 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001219 case K_PAGEDOWN:
1220 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001221#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001222 if (pum_visible())
1223 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001224#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001225 ins_pagedown();
1226 break;
1227
1228#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001229 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001230 ins_drop();
1231 break;
1232#endif
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 c = TAB;
1236 /* FALLTHROUGH */
1237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1240 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1241 goto docomplete;
1242#endif
1243 inserted_space = FALSE;
1244 if (ins_tab())
1245 goto normalchar; /* insert TAB as a normal char */
1246 auto_format(FALSE, TRUE);
1247 break;
1248
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001249 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001250 c = CAR;
1251 /* FALLTHROUGH */
1252 case CAR:
1253 case NL:
1254#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1255 /* In a quickfix window a <CR> jumps to the error under the
1256 * cursor. */
1257 if (bt_quickfix(curbuf) && c == CAR)
1258 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001259 if (curwin->w_llist_ref == NULL) /* quickfix window */
1260 do_cmdline_cmd((char_u *)".cc");
1261 else /* location list window */
1262 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 break;
1264 }
1265#endif
1266#ifdef FEAT_CMDWIN
1267 if (cmdwin_type != 0)
1268 {
1269 /* Execute the command in the cmdline window. */
1270 cmdwin_result = CAR;
1271 goto doESCkey;
1272 }
1273#endif
1274 if (ins_eol(c) && !p_im)
1275 goto doESCkey; /* out of memory */
1276 auto_format(FALSE, FALSE);
1277 inserted_space = FALSE;
1278 break;
1279
1280#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001281 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282# ifdef FEAT_INS_EXPAND
1283 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1284 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 if (has_compl_option(TRUE))
1286 goto docomplete;
1287 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001288 }
1289# endif
1290# ifdef FEAT_DIGRAPHS
1291 c = ins_digraph();
1292 if (c == NUL)
1293 break;
1294# endif
1295 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001297
1298#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001299 case Ctrl_X: /* Enter CTRL-X mode */
1300 ins_ctrl_x();
1301 break;
1302
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304 if (ctrl_x_mode != CTRL_X_TAGS)
1305 goto normalchar;
1306 goto docomplete;
1307
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001308 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309 if (ctrl_x_mode != CTRL_X_FILES)
1310 goto normalchar;
1311 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001312
1313 case 's': /* Spelling completion after ^X */
1314 case Ctrl_S:
1315 if (ctrl_x_mode != CTRL_X_SPELL)
1316 goto normalchar;
1317 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001318#endif
1319
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001320 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321#ifdef FEAT_INS_EXPAND
1322 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1323#endif
1324 {
1325 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1326 if (p_im)
1327 {
1328 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1329 break;
1330 goto doESCkey;
1331 }
1332 goto normalchar;
1333 }
1334#ifdef FEAT_INS_EXPAND
1335 /* FALLTHROUGH */
1336
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001337 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001338 case Ctrl_N:
1339 /* if 'complete' is empty then plain ^P is no longer special,
1340 * but it is under other ^X modes */
1341 if (*curbuf->b_p_cpt == NUL
1342 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001343 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 goto normalchar;
1345
1346docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001347 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001349 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001350 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 break;
1352#endif /* FEAT_INS_EXPAND */
1353
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001354 case Ctrl_Y: /* copy from previous line or scroll down */
1355 case Ctrl_E: /* copy from next line or scroll up */
1356 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 break;
1358
1359 default:
1360#ifdef UNIX
1361 if (c == intr_char) /* special interrupt char */
1362 goto do_intr;
1363#endif
1364
1365 /*
1366 * Insert a nomal character.
1367 */
1368normalchar:
1369#ifdef FEAT_SMARTINDENT
1370 /* Try to perform smart-indenting. */
1371 ins_try_si(c);
1372#endif
1373
1374 if (c == ' ')
1375 {
1376 inserted_space = TRUE;
1377#ifdef FEAT_CINDENT
1378 if (inindent(0))
1379 can_cindent = FALSE;
1380#endif
1381 if (Insstart_blank_vcol == MAXCOL
1382 && curwin->w_cursor.lnum == Insstart.lnum)
1383 Insstart_blank_vcol = get_nolist_virtcol();
1384 }
1385
1386 if (vim_iswordc(c) || !echeck_abbr(
1387#ifdef FEAT_MBYTE
1388 /* Add ABBR_OFF for characters above 0x100, this is
1389 * what check_abbr() expects. */
1390 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1391#endif
1392 c))
1393 {
1394 insert_special(c, FALSE, FALSE);
1395#ifdef FEAT_RIGHTLEFT
1396 revins_legal++;
1397 revins_chars++;
1398#endif
1399 }
1400
1401 auto_format(FALSE, TRUE);
1402
1403#ifdef FEAT_FOLDING
1404 /* When inserting a character the cursor line must never be in a
1405 * closed fold. */
1406 foldOpenCursor();
1407#endif
1408 break;
1409 } /* end of switch (c) */
1410
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001411#ifdef FEAT_AUTOCMD
1412 /* If typed something may trigger CursorHoldI again. */
1413 if (c != K_CURSORHOLD)
1414 did_cursorhold = FALSE;
1415#endif
1416
Bram Moolenaar071d4272004-06-13 20:20:40 +00001417 /* If the cursor was moved we didn't just insert a space */
1418 if (arrow_used)
1419 inserted_space = FALSE;
1420
1421#ifdef FEAT_CINDENT
1422 if (can_cindent && cindent_on()
1423# ifdef FEAT_INS_EXPAND
1424 && ctrl_x_mode == 0
1425# endif
1426 )
1427 {
1428force_cindent:
1429 /*
1430 * Indent now if a key was typed that is in 'cinkeys'.
1431 */
1432 if (in_cinkeys(c, ' ', line_is_white))
1433 {
1434 if (stop_arrow() == OK)
1435 /* re-indent the current line */
1436 do_c_expr_indent();
1437 }
1438 }
1439#endif /* FEAT_CINDENT */
1440
1441 } /* for (;;) */
1442 /* NOTREACHED */
1443}
1444
1445/*
1446 * Redraw for Insert mode.
1447 * This is postponed until getting the next character to make '$' in the 'cpo'
1448 * option work correctly.
1449 * Only redraw when there are no characters available. This speeds up
1450 * inserting sequences of characters (e.g., for CTRL-R).
1451 */
1452 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001453ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001454 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455{
1456 if (!char_avail())
1457 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001458#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001459 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1460 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001461 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001462 && !equalpos(last_cursormoved, curwin->w_cursor)
1463# ifdef FEAT_INS_EXPAND
1464 && !pum_visible()
1465# endif
1466 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001467 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001468# ifdef FEAT_SYN_HL
1469 /* Need to update the screen first, to make sure syntax
1470 * highlighting is correct after making a change (e.g., inserting
1471 * a "(". The autocommand may also require a redraw, so it's done
1472 * again below, unfortunately. */
1473 if (syntax_present(curbuf) && must_redraw)
1474 update_screen(0);
1475# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001476 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1477 last_cursormoved = curwin->w_cursor;
1478 }
1479#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (must_redraw)
1481 update_screen(0);
1482 else if (clear_cmdline || redraw_cmdline)
1483 showmode(); /* clear cmdline and show mode */
1484 showruler(FALSE);
1485 setcursor();
1486 emsg_on_display = FALSE; /* may remove error message now */
1487 }
1488}
1489
1490/*
1491 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1492 */
1493 static void
1494ins_ctrl_v()
1495{
1496 int c;
1497
1498 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001499 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500
1501 if (redrawing() && !char_avail())
1502 edit_putchar('^', TRUE);
1503 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1504
1505#ifdef FEAT_CMDL_INFO
1506 add_to_showcmd_c(Ctrl_V);
1507#endif
1508
1509 c = get_literal();
1510#ifdef FEAT_CMDL_INFO
1511 clear_showcmd();
1512#endif
1513 insert_special(c, FALSE, TRUE);
1514#ifdef FEAT_RIGHTLEFT
1515 revins_chars++;
1516 revins_legal++;
1517#endif
1518}
1519
1520/*
1521 * Put a character directly onto the screen. It's not stored in a buffer.
1522 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1523 */
1524static int pc_status;
1525#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1526#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1527#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1528#define PC_STATUS_SET 3 /* pc_bytes was filled */
1529#ifdef FEAT_MBYTE
1530static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1531#else
1532static char_u pc_bytes[2]; /* saved bytes */
1533#endif
1534static int pc_attr;
1535static int pc_row;
1536static int pc_col;
1537
1538 void
1539edit_putchar(c, highlight)
1540 int c;
1541 int highlight;
1542{
1543 int attr;
1544
1545 if (ScreenLines != NULL)
1546 {
1547 update_topline(); /* just in case w_topline isn't valid */
1548 validate_cursor();
1549 if (highlight)
1550 attr = hl_attr(HLF_8);
1551 else
1552 attr = 0;
1553 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1554 pc_col = W_WINCOL(curwin);
1555#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1556 pc_status = PC_STATUS_UNSET;
1557#endif
1558#ifdef FEAT_RIGHTLEFT
1559 if (curwin->w_p_rl)
1560 {
1561 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1562# ifdef FEAT_MBYTE
1563 if (has_mbyte)
1564 {
1565 int fix_col = mb_fix_col(pc_col, pc_row);
1566
1567 if (fix_col != pc_col)
1568 {
1569 screen_putchar(' ', pc_row, fix_col, attr);
1570 --curwin->w_wcol;
1571 pc_status = PC_STATUS_RIGHT;
1572 }
1573 }
1574# endif
1575 }
1576 else
1577#endif
1578 {
1579 pc_col += curwin->w_wcol;
1580#ifdef FEAT_MBYTE
1581 if (mb_lefthalve(pc_row, pc_col))
1582 pc_status = PC_STATUS_LEFT;
1583#endif
1584 }
1585
1586 /* save the character to be able to put it back */
1587#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1588 if (pc_status == PC_STATUS_UNSET)
1589#endif
1590 {
1591 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1592 pc_status = PC_STATUS_SET;
1593 }
1594 screen_putchar(c, pc_row, pc_col, attr);
1595 }
1596}
1597
1598/*
1599 * Undo the previous edit_putchar().
1600 */
1601 void
1602edit_unputchar()
1603{
1604 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1605 {
1606#if defined(FEAT_MBYTE)
1607 if (pc_status == PC_STATUS_RIGHT)
1608 ++curwin->w_wcol;
1609 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1610 redrawWinline(curwin->w_cursor.lnum, FALSE);
1611 else
1612#endif
1613 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1614 }
1615}
1616
1617/*
1618 * Called when p_dollar is set: display a '$' at the end of the changed text
1619 * Only works when cursor is in the line that changes.
1620 */
1621 void
1622display_dollar(col)
1623 colnr_T col;
1624{
1625 colnr_T save_col;
1626
1627 if (!redrawing())
1628 return;
1629
1630 cursor_off();
1631 save_col = curwin->w_cursor.col;
1632 curwin->w_cursor.col = col;
1633#ifdef FEAT_MBYTE
1634 if (has_mbyte)
1635 {
1636 char_u *p;
1637
1638 /* If on the last byte of a multi-byte move to the first byte. */
1639 p = ml_get_curline();
1640 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1641 }
1642#endif
1643 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1644 if (curwin->w_wcol < W_WIDTH(curwin))
1645 {
1646 edit_putchar('$', FALSE);
1647 dollar_vcol = curwin->w_virtcol;
1648 }
1649 curwin->w_cursor.col = save_col;
1650}
1651
1652/*
1653 * Call this function before moving the cursor from the normal insert position
1654 * in insert mode.
1655 */
1656 static void
1657undisplay_dollar()
1658{
1659 if (dollar_vcol)
1660 {
1661 dollar_vcol = 0;
1662 redrawWinline(curwin->w_cursor.lnum, FALSE);
1663 }
1664}
1665
1666/*
1667 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1668 * Keep the cursor on the same character.
1669 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1670 * type == INDENT_DEC decrease indent (for CTRL-D)
1671 * type == INDENT_SET set indent to "amount"
1672 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1673 */
1674 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001675change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676 int type;
1677 int amount;
1678 int round;
1679 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001680 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681{
1682 int vcol;
1683 int last_vcol;
1684 int insstart_less; /* reduction for Insstart.col */
1685 int new_cursor_col;
1686 int i;
1687 char_u *ptr;
1688 int save_p_list;
1689 int start_col;
1690 colnr_T vc;
1691#ifdef FEAT_VREPLACE
1692 colnr_T orig_col = 0; /* init for GCC */
1693 char_u *new_line, *orig_line = NULL; /* init for GCC */
1694
1695 /* VREPLACE mode needs to know what the line was like before changing */
1696 if (State & VREPLACE_FLAG)
1697 {
1698 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1699 orig_col = curwin->w_cursor.col;
1700 }
1701#endif
1702
1703 /* for the following tricks we don't want list mode */
1704 save_p_list = curwin->w_p_list;
1705 curwin->w_p_list = FALSE;
1706 vc = getvcol_nolist(&curwin->w_cursor);
1707 vcol = vc;
1708
1709 /*
1710 * For Replace mode we need to fix the replace stack later, which is only
1711 * possible when the cursor is in the indent. Remember the number of
1712 * characters before the cursor if it's possible.
1713 */
1714 start_col = curwin->w_cursor.col;
1715
1716 /* determine offset from first non-blank */
1717 new_cursor_col = curwin->w_cursor.col;
1718 beginline(BL_WHITE);
1719 new_cursor_col -= curwin->w_cursor.col;
1720
1721 insstart_less = curwin->w_cursor.col;
1722
1723 /*
1724 * If the cursor is in the indent, compute how many screen columns the
1725 * cursor is to the left of the first non-blank.
1726 */
1727 if (new_cursor_col < 0)
1728 vcol = get_indent() - vcol;
1729
1730 if (new_cursor_col > 0) /* can't fix replace stack */
1731 start_col = -1;
1732
1733 /*
1734 * Set the new indent. The cursor will be put on the first non-blank.
1735 */
1736 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001737 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001738 else
1739 {
1740#ifdef FEAT_VREPLACE
1741 int save_State = State;
1742
1743 /* Avoid being called recursively. */
1744 if (State & VREPLACE_FLAG)
1745 State = INSERT;
1746#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001747 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748#ifdef FEAT_VREPLACE
1749 State = save_State;
1750#endif
1751 }
1752 insstart_less -= curwin->w_cursor.col;
1753
1754 /*
1755 * Try to put cursor on same character.
1756 * If the cursor is at or after the first non-blank in the line,
1757 * compute the cursor column relative to the column of the first
1758 * non-blank character.
1759 * If we are not in insert mode, leave the cursor on the first non-blank.
1760 * If the cursor is before the first non-blank, position it relative
1761 * to the first non-blank, counted in screen columns.
1762 */
1763 if (new_cursor_col >= 0)
1764 {
1765 /*
1766 * When changing the indent while the cursor is touching it, reset
1767 * Insstart_col to 0.
1768 */
1769 if (new_cursor_col == 0)
1770 insstart_less = MAXCOL;
1771 new_cursor_col += curwin->w_cursor.col;
1772 }
1773 else if (!(State & INSERT))
1774 new_cursor_col = curwin->w_cursor.col;
1775 else
1776 {
1777 /*
1778 * Compute the screen column where the cursor should be.
1779 */
1780 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001781 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
1783 /*
1784 * Advance the cursor until we reach the right screen column.
1785 */
1786 vcol = last_vcol = 0;
1787 new_cursor_col = -1;
1788 ptr = ml_get_curline();
1789 while (vcol <= (int)curwin->w_virtcol)
1790 {
1791 last_vcol = vcol;
1792#ifdef FEAT_MBYTE
1793 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001794 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795 else
1796#endif
1797 ++new_cursor_col;
1798 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1799 }
1800 vcol = last_vcol;
1801
1802 /*
1803 * May need to insert spaces to be able to position the cursor on
1804 * the right screen column.
1805 */
1806 if (vcol != (int)curwin->w_virtcol)
1807 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001808 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001810 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001811 if (ptr != NULL)
1812 {
1813 new_cursor_col += i;
1814 ptr[i] = NUL;
1815 while (--i >= 0)
1816 ptr[i] = ' ';
1817 ins_str(ptr);
1818 vim_free(ptr);
1819 }
1820 }
1821
1822 /*
1823 * When changing the indent while the cursor is in it, reset
1824 * Insstart_col to 0.
1825 */
1826 insstart_less = MAXCOL;
1827 }
1828
1829 curwin->w_p_list = save_p_list;
1830
1831 if (new_cursor_col <= 0)
1832 curwin->w_cursor.col = 0;
1833 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001834 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001835 curwin->w_set_curswant = TRUE;
1836 changed_cline_bef_curs();
1837
1838 /*
1839 * May have to adjust the start of the insert.
1840 */
1841 if (State & INSERT)
1842 {
1843 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1844 {
1845 if ((int)Insstart.col <= insstart_less)
1846 Insstart.col = 0;
1847 else
1848 Insstart.col -= insstart_less;
1849 }
1850 if ((int)ai_col <= insstart_less)
1851 ai_col = 0;
1852 else
1853 ai_col -= insstart_less;
1854 }
1855
1856 /*
1857 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1858 * If the number of characters before the cursor decreased, need to pop a
1859 * few characters from the replace stack.
1860 * If the number of characters before the cursor increased, need to push a
1861 * few NULs onto the replace stack.
1862 */
1863 if (REPLACE_NORMAL(State) && start_col >= 0)
1864 {
1865 while (start_col > (int)curwin->w_cursor.col)
1866 {
1867 replace_join(0); /* remove a NUL from the replace stack */
1868 --start_col;
1869 }
1870 while (start_col < (int)curwin->w_cursor.col || replaced)
1871 {
1872 replace_push(NUL);
1873 if (replaced)
1874 {
1875 replace_push(replaced);
1876 replaced = NUL;
1877 }
1878 ++start_col;
1879 }
1880 }
1881
1882#ifdef FEAT_VREPLACE
1883 /*
1884 * For VREPLACE mode, we also have to fix the replace stack. In this case
1885 * it is always possible because we backspace over the whole line and then
1886 * put it back again the way we wanted it.
1887 */
1888 if (State & VREPLACE_FLAG)
1889 {
1890 /* If orig_line didn't allocate, just return. At least we did the job,
1891 * even if you can't backspace. */
1892 if (orig_line == NULL)
1893 return;
1894
1895 /* Save new line */
1896 new_line = vim_strsave(ml_get_curline());
1897 if (new_line == NULL)
1898 return;
1899
1900 /* We only put back the new line up to the cursor */
1901 new_line[curwin->w_cursor.col] = NUL;
1902
1903 /* Put back original line */
1904 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1905 curwin->w_cursor.col = orig_col;
1906
1907 /* Backspace from cursor to start of line */
1908 backspace_until_column(0);
1909
1910 /* Insert new stuff into line again */
1911 ins_bytes(new_line);
1912
1913 vim_free(new_line);
1914 }
1915#endif
1916}
1917
1918/*
1919 * Truncate the space at the end of a line. This is to be used only in an
1920 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1921 * modes.
1922 */
1923 void
1924truncate_spaces(line)
1925 char_u *line;
1926{
1927 int i;
1928
1929 /* find start of trailing white space */
1930 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1931 {
1932 if (State & REPLACE_FLAG)
1933 replace_join(0); /* remove a NUL from the replace stack */
1934 }
1935 line[i + 1] = NUL;
1936}
1937
1938#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1939 || defined(FEAT_COMMENTS) || defined(PROTO)
1940/*
1941 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1942 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001943 * Will attempt not to go before "col" even when there is a composing
1944 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001945 */
1946 void
1947backspace_until_column(col)
1948 int col;
1949{
1950 while ((int)curwin->w_cursor.col > col)
1951 {
1952 curwin->w_cursor.col--;
1953 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001954 replace_do_bs(col);
1955 else if (!del_char_after_col(col))
1956 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 }
1958}
1959#endif
1960
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001961/*
1962 * Like del_char(), but make sure not to go before column "limit_col".
1963 * Only matters when there are composing characters.
1964 * Return TRUE when something was deleted.
1965 */
1966 static int
1967del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001968 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001969{
1970#ifdef FEAT_MBYTE
1971 if (enc_utf8 && limit_col >= 0)
1972 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001973 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001974
1975 /* Make sure the cursor is at the start of a character, but
1976 * skip forward again when going too far back because of a
1977 * composing character. */
1978 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001979 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001980 {
1981 int l = utf_ptr2len(ml_get_cursor());
1982
1983 if (l == 0) /* end of line */
1984 break;
1985 curwin->w_cursor.col += l;
1986 }
1987 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1988 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001989 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001990 }
1991 else
1992#endif
1993 (void)del_char(FALSE);
1994 return TRUE;
1995}
1996
Bram Moolenaar071d4272004-06-13 20:20:40 +00001997#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1998/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001999 * CTRL-X pressed in Insert mode.
2000 */
2001 static void
2002ins_ctrl_x()
2003{
2004 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2005 * CTRL-V works like CTRL-N */
2006 if (ctrl_x_mode != CTRL_X_CMDLINE)
2007 {
2008 /* if the next ^X<> won't ADD nothing, then reset
2009 * compl_cont_status */
2010 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002011 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002012 else
2013 compl_cont_status = 0;
2014 /* We're not sure which CTRL-X mode it will be yet */
2015 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2016 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2017 edit_submode_pre = NULL;
2018 showmode();
2019 }
2020}
2021
2022/*
2023 * Return TRUE if the 'dict' or 'tsr' option can be used.
2024 */
2025 static int
2026has_compl_option(dict_opt)
2027 int dict_opt;
2028{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002029 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002030# ifdef FEAT_SPELL
2031 && !curwin->w_p_spell
2032# endif
2033 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002034 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2035 {
2036 ctrl_x_mode = 0;
2037 edit_submode = NULL;
2038 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2039 : (char_u *)_("'thesaurus' option is empty"),
2040 hl_attr(HLF_E));
2041 if (emsg_silent == 0)
2042 {
2043 vim_beep();
2044 setcursor();
2045 out_flush();
2046 ui_delay(2000L, FALSE);
2047 }
2048 return FALSE;
2049 }
2050 return TRUE;
2051}
2052
2053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002054 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2055 * This depends on the current mode.
2056 */
2057 int
2058vim_is_ctrl_x_key(c)
2059 int c;
2060{
2061 /* Always allow ^R - let it's results then be checked */
2062 if (c == Ctrl_R)
2063 return TRUE;
2064
Bram Moolenaare3226be2005-12-18 22:10:00 +00002065 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002066 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002067 return TRUE;
2068
Bram Moolenaar071d4272004-06-13 20:20:40 +00002069 switch (ctrl_x_mode)
2070 {
2071 case 0: /* Not in any CTRL-X mode */
2072 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2073 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002074 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2076 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2077 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002078 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2079 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002080 case CTRL_X_SCROLL:
2081 return (c == Ctrl_Y || c == Ctrl_E);
2082 case CTRL_X_WHOLE_LINE:
2083 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2084 case CTRL_X_FILES:
2085 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2086 case CTRL_X_DICTIONARY:
2087 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2088 case CTRL_X_THESAURUS:
2089 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2090 case CTRL_X_TAGS:
2091 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2092#ifdef FEAT_FIND_ID
2093 case CTRL_X_PATH_PATTERNS:
2094 return (c == Ctrl_P || c == Ctrl_N);
2095 case CTRL_X_PATH_DEFINES:
2096 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2097#endif
2098 case CTRL_X_CMDLINE:
2099 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2100 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002101#ifdef FEAT_COMPL_FUNC
2102 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002103 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002104 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002105 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002106#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002107 case CTRL_X_SPELL:
2108 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002109 }
2110 EMSG(_(e_internal));
2111 return FALSE;
2112}
2113
2114/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002115 * Return TRUE when character "c" is part of the item currently being
2116 * completed. Used to decide whether to abandon complete mode when the menu
2117 * is visible.
2118 */
2119 static int
2120ins_compl_accept_char(c)
2121 int c;
2122{
2123 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2124 /* When expanding an identifier only accept identifier chars. */
2125 return vim_isIDc(c);
2126
2127 switch (ctrl_x_mode)
2128 {
2129 case CTRL_X_FILES:
2130 /* When expanding file name only accept file name chars. But not
2131 * path separators, so that "proto/<Tab>" expands files in
2132 * "proto", not "proto/" as a whole */
2133 return vim_isfilec(c) && !vim_ispathsep(c);
2134
2135 case CTRL_X_CMDLINE:
2136 case CTRL_X_OMNI:
2137 /* Command line and Omni completion can work with just about any
2138 * printable character, but do stop at white space. */
2139 return vim_isprintc(c) && !vim_iswhite(c);
2140
2141 case CTRL_X_WHOLE_LINE:
2142 /* For while line completion a space can be part of the line. */
2143 return vim_isprintc(c);
2144 }
2145 return vim_iswordc(c);
2146}
2147
2148/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002149 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002150 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002151 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 * the rest of the word to be in -- webb
2153 */
2154 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002155ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002156 char_u *str;
2157 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002158 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 char_u *fname;
2160 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002161 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002162{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002163 char_u *p;
2164 int i, c;
2165 int actual_len; /* Take multi-byte characters */
2166 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002167 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002168 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 int has_lower = FALSE;
2170 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002172 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002174 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002175
Bram Moolenaara2993e12007-08-12 14:38:46 +00002176 /* Find actual length of completion. */
2177#ifdef FEAT_MBYTE
2178 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002179 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002180 p = str;
2181 actual_len = 0;
2182 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002183 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002184 mb_ptr_adv(p);
2185 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 }
2187 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002188 else
2189#endif
2190 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002191
Bram Moolenaara2993e12007-08-12 14:38:46 +00002192 /* Find actual length of original text. */
2193#ifdef FEAT_MBYTE
2194 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002195 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002196 p = compl_orig_text;
2197 actual_compl_length = 0;
2198 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002199 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002200 mb_ptr_adv(p);
2201 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 }
2203 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002204 else
2205#endif
2206 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002208 /* "actual_len" may be smaller than "actual_compl_length" when using
2209 * thesaurus, only use the minimum when comparing. */
2210 min_len = actual_len < actual_compl_length
2211 ? actual_len : actual_compl_length;
2212
Bram Moolenaara2993e12007-08-12 14:38:46 +00002213 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002214 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002215 if (wca != NULL)
2216 {
2217 p = str;
2218 for (i = 0; i < actual_len; ++i)
2219#ifdef FEAT_MBYTE
2220 if (has_mbyte)
2221 wca[i] = mb_ptr2char_adv(&p);
2222 else
2223#endif
2224 wca[i] = *(p++);
2225
2226 /* Rule 1: Were any chars converted to lower? */
2227 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002228 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002229 {
2230#ifdef FEAT_MBYTE
2231 if (has_mbyte)
2232 c = mb_ptr2char_adv(&p);
2233 else
2234#endif
2235 c = *(p++);
2236 if (MB_ISLOWER(c))
2237 {
2238 has_lower = TRUE;
2239 if (MB_ISUPPER(wca[i]))
2240 {
2241 /* Rule 1 is satisfied. */
2242 for (i = actual_compl_length; i < actual_len; ++i)
2243 wca[i] = MB_TOLOWER(wca[i]);
2244 break;
2245 }
2246 }
2247 }
2248
2249 /*
2250 * Rule 2: No lower case, 2nd consecutive letter converted to
2251 * upper case.
2252 */
2253 if (!has_lower)
2254 {
2255 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002256 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002257 {
2258#ifdef FEAT_MBYTE
2259 if (has_mbyte)
2260 c = mb_ptr2char_adv(&p);
2261 else
2262#endif
2263 c = *(p++);
2264 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2265 {
2266 /* Rule 2 is satisfied. */
2267 for (i = actual_compl_length; i < actual_len; ++i)
2268 wca[i] = MB_TOUPPER(wca[i]);
2269 break;
2270 }
2271 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2272 }
2273 }
2274
2275 /* Copy the original case of the part we typed. */
2276 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002277 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002278 {
2279#ifdef FEAT_MBYTE
2280 if (has_mbyte)
2281 c = mb_ptr2char_adv(&p);
2282 else
2283#endif
2284 c = *(p++);
2285 if (MB_ISLOWER(c))
2286 wca[i] = MB_TOLOWER(wca[i]);
2287 else if (MB_ISUPPER(c))
2288 wca[i] = MB_TOUPPER(wca[i]);
2289 }
2290
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002291 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002292 * Generate encoding specific output from wide character array.
2293 * Multi-byte characters can occupy up to five bytes more than
2294 * ASCII characters, and we also need one byte for NUL, so stay
2295 * six bytes away from the edge of IObuff.
2296 */
2297 p = IObuff;
2298 i = 0;
2299 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2300#ifdef FEAT_MBYTE
2301 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002302 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002303 else
2304#endif
2305 *(p++) = wca[i++];
2306 *p = NUL;
2307
2308 vim_free(wca);
2309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002310
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002311 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2312 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002313 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002314 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315}
2316
2317/*
2318 * Add a match to the list of matches.
2319 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002320 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002321 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002323 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002324ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325 char_u *str;
2326 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002327 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002328 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002329 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002330 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002331 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002332 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002333{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002334 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002335 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336
2337 ui_breakcheck();
2338 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002339 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002340 if (len < 0)
2341 len = (int)STRLEN(str);
2342
2343 /*
2344 * If the same match is already present, don't add it.
2345 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002346 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002348 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002349 do
2350 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002351 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002352 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002353 && match->cp_str[len] == NUL)
2354 return NOTDONE;
2355 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002356 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 }
2358
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002359 /* Remove any popup menu before changing the list of matches. */
2360 ins_compl_del_pum();
2361
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 /*
2363 * Allocate a new match structure.
2364 * Copy the values to the new match structure.
2365 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002366 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002368 return FAIL;
2369 match->cp_number = -1;
2370 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002371 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002372 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 {
2374 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002375 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002377 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002378
Bram Moolenaar071d4272004-06-13 20:20:40 +00002379 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002380 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002381 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2382 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002383 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002384 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002385 && compl_curr_match->cp_fname != NULL
2386 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002387 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002388 else if (fname != NULL)
2389 {
2390 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002391 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002394 match->cp_fname = NULL;
2395 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002396
2397 if (cptext != NULL)
2398 {
2399 int i;
2400
2401 for (i = 0; i < CPT_COUNT; ++i)
2402 if (cptext[i] != NULL && *cptext[i] != NUL)
2403 match->cp_text[i] = vim_strsave(cptext[i]);
2404 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405
2406 /*
2407 * Link the new match structure in the list of matches.
2408 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002409 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002410 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002411 else if (dir == FORWARD)
2412 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002413 match->cp_next = compl_curr_match->cp_next;
2414 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 }
2416 else /* BACKWARD */
2417 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002418 match->cp_next = compl_curr_match;
2419 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002420 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002421 if (match->cp_next)
2422 match->cp_next->cp_prev = match;
2423 if (match->cp_prev)
2424 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002426 compl_first_match = match;
2427 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002429 /*
2430 * Find the longest common string if still doing that.
2431 */
2432 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2433 ins_compl_longest_match(match);
2434
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 return OK;
2436}
2437
2438/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002439 * Return TRUE if "str[len]" matches with match->cp_str, considering
2440 * match->cp_icase.
2441 */
2442 static int
2443ins_compl_equal(match, str, len)
2444 compl_T *match;
2445 char_u *str;
2446 int len;
2447{
2448 if (match->cp_icase)
2449 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2450 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2451}
2452
2453/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002454 * Reduce the longest common string for match "match".
2455 */
2456 static void
2457ins_compl_longest_match(match)
2458 compl_T *match;
2459{
2460 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002461 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002462 int had_match;
2463
2464 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002465 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002466 /* First match, use it as a whole. */
2467 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002468 if (compl_leader != NULL)
2469 {
2470 had_match = (curwin->w_cursor.col > compl_col);
2471 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002472 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002473 ins_redraw(FALSE);
2474
2475 /* When the match isn't there (to avoid matching itself) remove it
2476 * again after redrawing. */
2477 if (!had_match)
2478 ins_compl_delete();
2479 compl_used_match = FALSE;
2480 }
2481 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002482 else
2483 {
2484 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002485 p = compl_leader;
2486 s = match->cp_str;
2487 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002488 {
2489#ifdef FEAT_MBYTE
2490 if (has_mbyte)
2491 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002492 c1 = mb_ptr2char(p);
2493 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002494 }
2495 else
2496#endif
2497 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002498 c1 = *p;
2499 c2 = *s;
2500 }
2501 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2502 : (c1 != c2))
2503 break;
2504#ifdef FEAT_MBYTE
2505 if (has_mbyte)
2506 {
2507 mb_ptr_adv(p);
2508 mb_ptr_adv(s);
2509 }
2510 else
2511#endif
2512 {
2513 ++p;
2514 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002515 }
2516 }
2517
2518 if (*p != NUL)
2519 {
2520 /* Leader was shortened, need to change the inserted text. */
2521 *p = NUL;
2522 had_match = (curwin->w_cursor.col > compl_col);
2523 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002524 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002525 ins_redraw(FALSE);
2526
2527 /* When the match isn't there (to avoid matching itself) remove it
2528 * again after redrawing. */
2529 if (!had_match)
2530 ins_compl_delete();
2531 }
2532
2533 compl_used_match = FALSE;
2534 }
2535}
2536
2537/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002538 * Add an array of matches to the list of matches.
2539 * Frees matches[].
2540 */
2541 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002542ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 int num_matches;
2544 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546{
2547 int i;
2548 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002549 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550
Bram Moolenaar572cb562005-08-05 21:35:02 +00002551 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002552 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002553 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002554 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002555 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 FreeWild(num_matches, matches);
2557}
2558
2559/* Make the completion list cyclic.
2560 * Return the number of matches (excluding the original).
2561 */
2562 static int
2563ins_compl_make_cyclic()
2564{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002565 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 int count = 0;
2567
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002568 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 {
2570 /*
2571 * Find the end of the list.
2572 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002573 match = compl_first_match;
2574 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002575 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002577 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 ++count;
2579 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002580 match->cp_next = compl_first_match;
2581 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002582 }
2583 return count;
2584}
2585
Bram Moolenaara94bc432006-03-10 21:42:59 +00002586/*
2587 * Start completion for the complete() function.
2588 * "startcol" is where the matched text starts (1 is first column).
2589 * "list" is the list of matches.
2590 */
2591 void
2592set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002593 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002594 list_T *list;
2595{
2596 /* If already doing completions stop it. */
2597 if (ctrl_x_mode != 0)
2598 ins_compl_prep(' ');
2599 ins_compl_clear();
2600
2601 if (stop_arrow() == FAIL)
2602 return;
2603
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002604 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002605 startcol = curwin->w_cursor.col;
2606 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002607 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002608 /* compl_pattern doesn't need to be set */
2609 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2610 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002611 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002612 return;
2613
2614 /* Handle like dictionary completion. */
2615 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2616
2617 ins_compl_add_list(list);
2618 compl_matches = ins_compl_make_cyclic();
2619 compl_started = TRUE;
2620 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002621 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002622
2623 compl_curr_match = compl_first_match;
2624 ins_complete(Ctrl_N);
2625 out_flush();
2626}
2627
2628
Bram Moolenaar9372a112005-12-06 19:59:18 +00002629/* "compl_match_array" points the currently displayed list of entries in the
2630 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002631static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002632static int compl_match_arraysize;
2633
2634/*
2635 * Update the screen and when there is any scrolling remove the popup menu.
2636 */
2637 static void
2638ins_compl_upd_pum()
2639{
2640 int h;
2641
2642 if (compl_match_array != NULL)
2643 {
2644 h = curwin->w_cline_height;
2645 update_screen(0);
2646 if (h != curwin->w_cline_height)
2647 ins_compl_del_pum();
2648 }
2649}
2650
2651/*
2652 * Remove any popup menu.
2653 */
2654 static void
2655ins_compl_del_pum()
2656{
2657 if (compl_match_array != NULL)
2658 {
2659 pum_undisplay();
2660 vim_free(compl_match_array);
2661 compl_match_array = NULL;
2662 }
2663}
2664
2665/*
2666 * Return TRUE if the popup menu should be displayed.
2667 */
2668 static int
2669pum_wanted()
2670{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002671 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002672 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002673 return FALSE;
2674
2675 /* The display looks bad on a B&W display. */
2676 if (t_colors < 8
2677#ifdef FEAT_GUI
2678 && !gui.in_use
2679#endif
2680 )
2681 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002682 return TRUE;
2683}
2684
2685/*
2686 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002687 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002688 */
2689 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002690pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002691{
2692 compl_T *compl;
2693 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002694
2695 /* Don't display the popup menu if there are no matches or there is only
2696 * one (ignoring the original text). */
2697 compl = compl_first_match;
2698 i = 0;
2699 do
2700 {
2701 if (compl == NULL
2702 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2703 break;
2704 compl = compl->cp_next;
2705 } while (compl != compl_first_match);
2706
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002707 if (strstr((char *)p_cot, "menuone") != NULL)
2708 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002709 return (i >= 2);
2710}
2711
2712/*
2713 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002714 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002715 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002716 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002717ins_compl_show_pum()
2718{
2719 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002720 compl_T *shown_compl = NULL;
2721 int did_find_shown_match = FALSE;
2722 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002723 int i;
2724 int cur = -1;
2725 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002726 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002727
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002728 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002729 return;
2730
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002731#if defined(FEAT_EVAL)
2732 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2733 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2734#endif
2735
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002736 /* Update the screen before drawing the popup menu over it. */
2737 update_screen(0);
2738
2739 if (compl_match_array == NULL)
2740 {
2741 /* Need to build the popup menu list. */
2742 compl_match_arraysize = 0;
2743 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002744 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002745 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002746 do
2747 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002748 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2749 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002750 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002751 ++compl_match_arraysize;
2752 compl = compl->cp_next;
2753 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002754 if (compl_match_arraysize == 0)
2755 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002756 compl_match_array = (pumitem_T *)alloc_clear(
2757 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002758 * compl_match_arraysize));
2759 if (compl_match_array != NULL)
2760 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002761 /* If the current match is the original text don't find the first
2762 * match after it, don't highlight anything. */
2763 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2764 shown_match_ok = TRUE;
2765
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002766 i = 0;
2767 compl = compl_first_match;
2768 do
2769 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002770 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2771 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002772 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002773 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002774 if (!shown_match_ok)
2775 {
2776 if (compl == compl_shown_match || did_find_shown_match)
2777 {
2778 /* This item is the shown match or this is the
2779 * first displayed item after the shown match. */
2780 compl_shown_match = compl;
2781 did_find_shown_match = TRUE;
2782 shown_match_ok = TRUE;
2783 }
2784 else
2785 /* Remember this displayed match for when the
2786 * shown match is just below it. */
2787 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002788 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002789 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002790
2791 if (compl->cp_text[CPT_ABBR] != NULL)
2792 compl_match_array[i].pum_text =
2793 compl->cp_text[CPT_ABBR];
2794 else
2795 compl_match_array[i].pum_text = compl->cp_str;
2796 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2797 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2798 if (compl->cp_text[CPT_MENU] != NULL)
2799 compl_match_array[i++].pum_extra =
2800 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002801 else
2802 compl_match_array[i++].pum_extra = compl->cp_fname;
2803 }
2804
2805 if (compl == compl_shown_match)
2806 {
2807 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002808
2809 /* When the original text is the shown match don't set
2810 * compl_shown_match. */
2811 if (compl->cp_flags & ORIGINAL_TEXT)
2812 shown_match_ok = TRUE;
2813
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002814 if (!shown_match_ok && shown_compl != NULL)
2815 {
2816 /* The shown match isn't displayed, set it to the
2817 * previously displayed match. */
2818 compl_shown_match = shown_compl;
2819 shown_match_ok = TRUE;
2820 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002821 }
2822 compl = compl->cp_next;
2823 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002824
2825 if (!shown_match_ok) /* no displayed match at all */
2826 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002827 }
2828 }
2829 else
2830 {
2831 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002832 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002833 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2834 || compl_match_array[i].pum_text
2835 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002836 {
2837 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002838 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002839 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002840 }
2841
2842 if (compl_match_array != NULL)
2843 {
2844 /* Compute the screen column of the start of the completed text.
2845 * Use the cursor to get all wrapping and other settings right. */
2846 col = curwin->w_cursor.col;
2847 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002848 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002849 curwin->w_cursor.col = col;
2850 }
2851}
2852
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853#define DICT_FIRST (1) /* use just first element in "dict" */
2854#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002855
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002857 * Add any identifiers that match the given pattern in the list of dictionary
2858 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 */
2860 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002861ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2862 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002863 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002864 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002865 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002867 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002868 char_u *ptr;
2869 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002870 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002871 char_u **files;
2872 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002873 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002874 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002875
Bram Moolenaar0b238792006-03-02 22:49:12 +00002876 if (*dict == NUL)
2877 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002878#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002879 /* When 'dictionary' is empty and spell checking is enabled use
2880 * "spell". */
2881 if (!thesaurus && curwin->w_p_spell)
2882 dict = (char_u *)"spell";
2883 else
2884#endif
2885 return;
2886 }
2887
Bram Moolenaar071d4272004-06-13 20:20:40 +00002888 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002889 if (buf == NULL)
2890 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002891 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002892
Bram Moolenaar071d4272004-06-13 20:20:40 +00002893 /* If 'infercase' is set, don't use 'smartcase' here */
2894 save_p_scs = p_scs;
2895 if (curbuf->b_p_inf)
2896 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002897
2898 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2899 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002900 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002901 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2902 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002903 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002904 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002905
2906 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002907 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002908 len = STRLEN(pat_esc) + 10;
2909 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002910 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002911 {
2912 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002913 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002914 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002915 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002916 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2917 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002918 vim_free(ptr);
2919 }
2920 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002921 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002922 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002923 if (regmatch.regprog == NULL)
2924 goto theend;
2925 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002926
Bram Moolenaar071d4272004-06-13 20:20:40 +00002927 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2928 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002929 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 {
2931 /* copy one dictionary file name into buf */
2932 if (flags == DICT_EXACT)
2933 {
2934 count = 1;
2935 files = &dict;
2936 }
2937 else
2938 {
2939 /* Expand wildcards in the dictionary name, but do not allow
2940 * backticks (for security, the 'dict' option may have been set in
2941 * a modeline). */
2942 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002943# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002944 if (!thesaurus && STRCMP(buf, "spell") == 0)
2945 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002946 else
2947# endif
2948 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002949 || expand_wildcards(1, &buf, &count, &files,
2950 EW_FILE|EW_SILENT) != OK)
2951 count = 0;
2952 }
2953
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002954# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002955 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002956 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002957 /* Complete from active spelling. Skip "\<" in the pattern, we
2958 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002959 if (pat[0] == '\\' && pat[1] == '<')
2960 ptr = pat + 2;
2961 else
2962 ptr = pat;
2963 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002965 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002966# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002967 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002968 {
2969 ins_compl_files(count, files, thesaurus, flags,
2970 &regmatch, buf, &dir);
2971 if (flags != DICT_EXACT)
2972 FreeWild(count, files);
2973 }
2974 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002975 break;
2976 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002977
2978theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002979 p_scs = save_p_scs;
2980 vim_free(regmatch.regprog);
2981 vim_free(buf);
2982}
2983
Bram Moolenaar0b238792006-03-02 22:49:12 +00002984 static void
2985ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2986 int count;
2987 char_u **files;
2988 int thesaurus;
2989 int flags;
2990 regmatch_T *regmatch;
2991 char_u *buf;
2992 int *dir;
2993{
2994 char_u *ptr;
2995 int i;
2996 FILE *fp;
2997 int add_r;
2998
2999 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3000 {
3001 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3002 if (flags != DICT_EXACT)
3003 {
3004 vim_snprintf((char *)IObuff, IOSIZE,
3005 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003006 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003007 }
3008
3009 if (fp != NULL)
3010 {
3011 /*
3012 * Read dictionary file line by line.
3013 * Check each line for a match.
3014 */
3015 while (!got_int && !compl_interrupted
3016 && !vim_fgets(buf, LSIZE, fp))
3017 {
3018 ptr = buf;
3019 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3020 {
3021 ptr = regmatch->startp[0];
3022 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3023 ptr = find_line_end(ptr);
3024 else
3025 ptr = find_word_end(ptr);
3026 add_r = ins_compl_add_infercase(regmatch->startp[0],
3027 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003028 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003029 if (thesaurus)
3030 {
3031 char_u *wstart;
3032
3033 /*
3034 * Add the other matches on the line
3035 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003036 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003037 while (!got_int)
3038 {
3039 /* Find start of the next word. Skip white
3040 * space and punctuation. */
3041 ptr = find_word_start(ptr);
3042 if (*ptr == NUL || *ptr == NL)
3043 break;
3044 wstart = ptr;
3045
Bram Moolenaara2993e12007-08-12 14:38:46 +00003046 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003047#ifdef FEAT_MBYTE
3048 if (has_mbyte)
3049 /* Japanese words may have characters in
3050 * different classes, only separate words
3051 * with single-byte non-word characters. */
3052 while (*ptr != NUL)
3053 {
3054 int l = (*mb_ptr2len)(ptr);
3055
3056 if (l < 2 && !vim_iswordc(*ptr))
3057 break;
3058 ptr += l;
3059 }
3060 else
3061#endif
3062 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003063
3064 /* Add the word. Skip the regexp match. */
3065 if (wstart != regmatch->startp[0])
3066 add_r = ins_compl_add_infercase(wstart,
3067 (int)(ptr - wstart),
3068 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003069 }
3070 }
3071 if (add_r == OK)
3072 /* if dir was BACKWARD then honor it just once */
3073 *dir = FORWARD;
3074 else if (add_r == FAIL)
3075 break;
3076 /* avoid expensive call to vim_regexec() when at end
3077 * of line */
3078 if (*ptr == '\n' || got_int)
3079 break;
3080 }
3081 line_breakcheck();
3082 ins_compl_check_keys(50);
3083 }
3084 fclose(fp);
3085 }
3086 }
3087}
3088
Bram Moolenaar071d4272004-06-13 20:20:40 +00003089/*
3090 * Find the start of the next word.
3091 * Returns a pointer to the first char of the word. Also stops at a NUL.
3092 */
3093 char_u *
3094find_word_start(ptr)
3095 char_u *ptr;
3096{
3097#ifdef FEAT_MBYTE
3098 if (has_mbyte)
3099 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003100 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003101 else
3102#endif
3103 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3104 ++ptr;
3105 return ptr;
3106}
3107
3108/*
3109 * Find the end of the word. Assumes it starts inside a word.
3110 * Returns a pointer to just after the word.
3111 */
3112 char_u *
3113find_word_end(ptr)
3114 char_u *ptr;
3115{
3116#ifdef FEAT_MBYTE
3117 int start_class;
3118
3119 if (has_mbyte)
3120 {
3121 start_class = mb_get_class(ptr);
3122 if (start_class > 1)
3123 while (*ptr != NUL)
3124 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003125 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003126 if (mb_get_class(ptr) != start_class)
3127 break;
3128 }
3129 }
3130 else
3131#endif
3132 while (vim_iswordc(*ptr))
3133 ++ptr;
3134 return ptr;
3135}
3136
3137/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003138 * Find the end of the line, omitting CR and NL at the end.
3139 * Returns a pointer to just after the line.
3140 */
3141 static char_u *
3142find_line_end(ptr)
3143 char_u *ptr;
3144{
3145 char_u *s;
3146
3147 s = ptr + STRLEN(ptr);
3148 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3149 --s;
3150 return s;
3151}
3152
3153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003154 * Free the list of completions
3155 */
3156 static void
3157ins_compl_free()
3158{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003159 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003160 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003162 vim_free(compl_pattern);
3163 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003164 vim_free(compl_leader);
3165 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003167 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003169
3170 ins_compl_del_pum();
3171 pum_clear();
3172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003173 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 do
3175 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003176 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003177 compl_curr_match = compl_curr_match->cp_next;
3178 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003179 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003180 if (match->cp_flags & FREE_FNAME)
3181 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003182 for (i = 0; i < CPT_COUNT; ++i)
3183 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003184 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003185 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3186 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003187 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188}
3189
3190 static void
3191ins_compl_clear()
3192{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003193 compl_cont_status = 0;
3194 compl_started = FALSE;
3195 compl_matches = 0;
3196 vim_free(compl_pattern);
3197 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003198 vim_free(compl_leader);
3199 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003201 vim_free(compl_orig_text);
3202 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003203 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003204}
3205
3206/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003207 * Return TRUE when Insert completion is active.
3208 */
3209 int
3210ins_compl_active()
3211{
3212 return compl_started;
3213}
3214
3215/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003216 * Delete one character before the cursor and show the subset of the matches
3217 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003218 * Returns the character to be used, NUL if the work is done and another char
3219 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003220 */
3221 static int
3222ins_compl_bs()
3223{
3224 char_u *line;
3225 char_u *p;
3226
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003227 line = ml_get_curline();
3228 p = line + curwin->w_cursor.col;
3229 mb_ptr_back(line, p);
3230
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003231 /* Stop completion when the whole word was deleted. For Omni completion
3232 * allow the word to be deleted, we won't match everything. */
3233 if ((int)(p - line) - (int)compl_col < 0
3234 || ((int)(p - line) - (int)compl_col == 0
3235 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003236 return K_BS;
3237
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003238 /* Deleted more than what was used to find matches or didn't finish
3239 * finding all matches: need to look for matches all over again. */
3240 if (curwin->w_cursor.col <= compl_col + compl_length
3241 || compl_was_interrupted)
3242 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003243
Bram Moolenaara6557602006-02-04 22:43:20 +00003244 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003245 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003246 if (compl_leader != NULL)
3247 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003248 ins_compl_new_leader();
3249 return NUL;
3250 }
3251 return K_BS;
3252}
Bram Moolenaara6557602006-02-04 22:43:20 +00003253
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003254/*
3255 * Called after changing "compl_leader".
3256 * Show the popup menu with a different set of matches.
3257 * May also search for matches again if the previous search was interrupted.
3258 */
3259 static void
3260ins_compl_new_leader()
3261{
3262 ins_compl_del_pum();
3263 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003264 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003265 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003266
3267 if (compl_started)
3268 ins_compl_set_original_text(compl_leader);
3269 else
3270 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003271#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003272 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003273#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003274 /*
3275 * Matches were cleared, need to search for them now. First display
3276 * the changed text before the cursor. Set "compl_restarting" to
3277 * avoid that the first match is inserted.
3278 */
3279 update_screen(0);
3280#ifdef FEAT_GUI
3281 if (gui.in_use)
3282 {
3283 /* Show the cursor after the match, not after the redrawn text. */
3284 setcursor();
3285 out_flush();
3286 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003287 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003288#endif
3289 compl_restarting = TRUE;
3290 if (ins_complete(Ctrl_N) == FAIL)
3291 compl_cont_status = 0;
3292 compl_restarting = FALSE;
3293 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003294
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003295#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003296 if (!compl_used_match)
3297 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003298 /* Go to the original text, since none of the matches is inserted. */
3299 if (compl_first_match->cp_prev != NULL
3300 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3301 compl_shown_match = compl_first_match->cp_prev;
3302 else
3303 compl_shown_match = compl_first_match;
3304 compl_curr_match = compl_shown_match;
3305 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003306 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003307#endif
3308 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003309
3310 /* Show the popup menu with a different set of matches. */
3311 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003312
3313 /* Don't let Enter select the original text when there is no popup menu. */
3314 if (compl_match_array == NULL)
3315 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003316}
3317
3318/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003319 * Return the length of the completion, from the completion start column to
3320 * the cursor column. Making sure it never goes below zero.
3321 */
3322 static int
3323ins_compl_len()
3324{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003325 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003326
3327 if (off < 0)
3328 return 0;
3329 return off;
3330}
3331
3332/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003333 * Append one character to the match leader. May reduce the number of
3334 * matches.
3335 */
3336 static void
3337ins_compl_addleader(c)
3338 int c;
3339{
3340#ifdef FEAT_MBYTE
3341 int cc;
3342
3343 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3344 {
3345 char_u buf[MB_MAXBYTES + 1];
3346
3347 (*mb_char2bytes)(c, buf);
3348 buf[cc] = NUL;
3349 ins_char_bytes(buf, cc);
3350 }
3351 else
3352#endif
3353 ins_char(c);
3354
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003355 /* If we didn't complete finding matches we must search again. */
3356 if (compl_was_interrupted)
3357 ins_compl_restart();
3358
Bram Moolenaara6557602006-02-04 22:43:20 +00003359 vim_free(compl_leader);
3360 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003361 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003362 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003363 ins_compl_new_leader();
3364}
3365
3366/*
3367 * Setup for finding completions again without leaving CTRL-X mode. Used when
3368 * BS or a key was typed while still searching for matches.
3369 */
3370 static void
3371ins_compl_restart()
3372{
3373 ins_compl_free();
3374 compl_started = FALSE;
3375 compl_matches = 0;
3376 compl_cont_status = 0;
3377 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003378}
3379
3380/*
3381 * Set the first match, the original text.
3382 */
3383 static void
3384ins_compl_set_original_text(str)
3385 char_u *str;
3386{
3387 char_u *p;
3388
3389 /* Replace the original text entry. */
3390 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3391 {
3392 p = vim_strsave(str);
3393 if (p != NULL)
3394 {
3395 vim_free(compl_first_match->cp_str);
3396 compl_first_match->cp_str = p;
3397 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003398 }
3399}
3400
3401/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003402 * Append one character to the match leader. May reduce the number of
3403 * matches.
3404 */
3405 static void
3406ins_compl_addfrommatch()
3407{
3408 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003409 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003410 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003411 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003412
3413 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003414 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003415 {
3416 /* When still at the original match use the first entry that matches
3417 * the leader. */
3418 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3419 {
3420 p = NULL;
3421 for (cp = compl_shown_match->cp_next; cp != NULL
3422 && cp != compl_first_match; cp = cp->cp_next)
3423 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003424 if (compl_leader == NULL
3425 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003426 (int)STRLEN(compl_leader)))
3427 {
3428 p = cp->cp_str;
3429 break;
3430 }
3431 }
3432 if (p == NULL || (int)STRLEN(p) <= len)
3433 return;
3434 }
3435 else
3436 return;
3437 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003438 p += len;
3439#ifdef FEAT_MBYTE
3440 c = mb_ptr2char(p);
3441#else
3442 c = *p;
3443#endif
3444 ins_compl_addleader(c);
3445}
3446
3447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003449 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003450 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003451 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003452 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453ins_compl_prep(c)
3454 int c;
3455{
3456 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003458 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459
3460 /* Forget any previous 'special' messages if this is actually
3461 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3462 */
3463 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3464 edit_submode_extra = NULL;
3465
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003466 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3467 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003468 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003470 /* Set "compl_get_longest" when finding the first matches. */
3471 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3472 || (ctrl_x_mode == 0 && !compl_started))
3473 {
3474 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3475 compl_used_match = TRUE;
3476 }
3477
Bram Moolenaar071d4272004-06-13 20:20:40 +00003478 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3479 {
3480 /*
3481 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3482 * it will be yet. Now we decide.
3483 */
3484 switch (c)
3485 {
3486 case Ctrl_E:
3487 case Ctrl_Y:
3488 ctrl_x_mode = CTRL_X_SCROLL;
3489 if (!(State & REPLACE_FLAG))
3490 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3491 else
3492 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3493 edit_submode_pre = NULL;
3494 showmode();
3495 break;
3496 case Ctrl_L:
3497 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3498 break;
3499 case Ctrl_F:
3500 ctrl_x_mode = CTRL_X_FILES;
3501 break;
3502 case Ctrl_K:
3503 ctrl_x_mode = CTRL_X_DICTIONARY;
3504 break;
3505 case Ctrl_R:
3506 /* Simply allow ^R to happen without affecting ^X mode */
3507 break;
3508 case Ctrl_T:
3509 ctrl_x_mode = CTRL_X_THESAURUS;
3510 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003511#ifdef FEAT_COMPL_FUNC
3512 case Ctrl_U:
3513 ctrl_x_mode = CTRL_X_FUNCTION;
3514 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003515 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003516 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003517 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003518#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003519 case 's':
3520 case Ctrl_S:
3521 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003522#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003523 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003524 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003525 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003526#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003527 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 case Ctrl_RSB:
3529 ctrl_x_mode = CTRL_X_TAGS;
3530 break;
3531#ifdef FEAT_FIND_ID
3532 case Ctrl_I:
3533 case K_S_TAB:
3534 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3535 break;
3536 case Ctrl_D:
3537 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3538 break;
3539#endif
3540 case Ctrl_V:
3541 case Ctrl_Q:
3542 ctrl_x_mode = CTRL_X_CMDLINE;
3543 break;
3544 case Ctrl_P:
3545 case Ctrl_N:
3546 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3547 * just started ^X mode, or there were enough ^X's to cancel
3548 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3549 * do normal expansion when interrupting a different mode (say
3550 * ^X^F^X^P or ^P^X^X^P, see below)
3551 * nothing changes if interrupting mode 0, (eg, the flag
3552 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003553 if (!(compl_cont_status & CONT_INTRPT))
3554 compl_cont_status |= CONT_LOCAL;
3555 else if (compl_cont_mode != 0)
3556 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 /* FALLTHROUGH */
3558 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 /* If we have typed at least 2 ^X's... for modes != 0, we set
3560 * compl_cont_status = 0 (eg, as if we had just started ^X
3561 * mode).
3562 * For mode 0, we set "compl_cont_mode" to an impossible
3563 * value, in both cases ^X^X can be used to restart the same
3564 * mode (avoiding ADDING mode).
3565 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3566 * 'complete' and local ^P expansions respectively.
3567 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3568 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003569 if (c == Ctrl_X)
3570 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003571 if (compl_cont_mode != 0)
3572 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003573 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003574 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003575 }
3576 ctrl_x_mode = 0;
3577 edit_submode = NULL;
3578 showmode();
3579 break;
3580 }
3581 }
3582 else if (ctrl_x_mode != 0)
3583 {
3584 /* We're already in CTRL-X mode, do we stay in it? */
3585 if (!vim_is_ctrl_x_key(c))
3586 {
3587 if (ctrl_x_mode == CTRL_X_SCROLL)
3588 ctrl_x_mode = 0;
3589 else
3590 ctrl_x_mode = CTRL_X_FINISHED;
3591 edit_submode = NULL;
3592 }
3593 showmode();
3594 }
3595
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003596 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003597 {
3598 /* Show error message from attempted keyword completion (probably
3599 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003600 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003601 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003602 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3603 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003604 || ctrl_x_mode == CTRL_X_FINISHED)
3605 {
3606 /* Get here when we have finished typing a sequence of ^N and
3607 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003608 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003609 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003611 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003612 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003613
Bram Moolenaar071d4272004-06-13 20:20:40 +00003614 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003615 * If any of the original typed text has been changed, eg when
3616 * ignorecase is set, we must add back-spaces to the redo
3617 * buffer. We add as few as necessary to delete just the part
3618 * of the original text that has changed.
3619 * When using the longest match, edited the match or used
3620 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003622 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3623 ptr = compl_curr_match->cp_str;
3624 else if (compl_leader != NULL)
3625 ptr = compl_leader;
3626 else
3627 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003628 if (compl_orig_text != NULL)
3629 {
3630 p = compl_orig_text;
3631 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3632 ++temp)
3633 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003634#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003635 if (temp > 0)
3636 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003637#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003638 for (p += temp; *p != NUL; mb_ptr_adv(p))
3639 AppendCharToRedobuff(K_BS);
3640 }
3641 if (ptr != NULL)
3642 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 }
3644
3645#ifdef FEAT_CINDENT
3646 want_cindent = (can_cindent && cindent_on());
3647#endif
3648 /*
3649 * When completing whole lines: fix indent for 'cindent'.
3650 * Otherwise, break line if it's too long.
3651 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003652 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003653 {
3654#ifdef FEAT_CINDENT
3655 /* re-indent the current line */
3656 if (want_cindent)
3657 {
3658 do_c_expr_indent();
3659 want_cindent = FALSE; /* don't do it again */
3660 }
3661#endif
3662 }
3663 else
3664 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003665 int prev_col = curwin->w_cursor.col;
3666
Bram Moolenaar071d4272004-06-13 20:20:40 +00003667 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003668 if (prev_col > 0)
3669 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003670 if (stop_arrow() == OK)
3671 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003672 if (prev_col > 0
3673 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3674 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 }
3676
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003677 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003678 * the selection without inserting anything. When
3679 * compl_enter_selects is set the Enter key does the same. */
3680 if ((c == Ctrl_Y || (compl_enter_selects
3681 && (c == CAR || c == K_KENTER || c == NL)))
3682 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003683 retval = TRUE;
3684
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003685 /* CTRL-E means completion is Ended, go back to the typed text. */
3686 if (c == Ctrl_E)
3687 {
3688 ins_compl_delete();
3689 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003690 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003691 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003692 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003693 retval = TRUE;
3694 }
3695
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003696 auto_format(FALSE, TRUE);
3697
Bram Moolenaar071d4272004-06-13 20:20:40 +00003698 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003699 compl_started = FALSE;
3700 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003701 msg_clr_cmdline(); /* necessary for "noshowmode" */
3702 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003703 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 if (edit_submode != NULL)
3705 {
3706 edit_submode = NULL;
3707 showmode();
3708 }
3709
3710#ifdef FEAT_CINDENT
3711 /*
3712 * Indent now if a key was typed that is in 'cinkeys'.
3713 */
3714 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3715 do_c_expr_indent();
3716#endif
3717 }
3718 }
3719
3720 /* reset continue_* if we left expansion-mode, if we stay they'll be
3721 * (re)set properly in ins_complete() */
3722 if (!vim_is_ctrl_x_key(c))
3723 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003724 compl_cont_status = 0;
3725 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003727
3728 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729}
3730
3731/*
3732 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3733 * (depending on flag) starting from buf and looking for a non-scanned
3734 * buffer (other than curbuf). curbuf is special, if it is called with
3735 * buf=curbuf then it has to be the first call for a given flag/expansion.
3736 *
3737 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3738 */
3739 static buf_T *
3740ins_compl_next_buf(buf, flag)
3741 buf_T *buf;
3742 int flag;
3743{
3744#ifdef FEAT_WINDOWS
3745 static win_T *wp;
3746#endif
3747
3748 if (flag == 'w') /* just windows */
3749 {
3750#ifdef FEAT_WINDOWS
3751 if (buf == curbuf) /* first call for this flag/expansion */
3752 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003753 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 && wp->w_buffer->b_scanned)
3755 ;
3756 buf = wp->w_buffer;
3757#else
3758 buf = curbuf;
3759#endif
3760 }
3761 else
3762 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3763 * (unlisted buffers)
3764 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003765 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003766 && ((flag == 'U'
3767 ? buf->b_p_bl
3768 : (!buf->b_p_bl
3769 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003770 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771 ;
3772 return buf;
3773}
3774
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003775#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003776static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003777
3778/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003779 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003780 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003781 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003782 static void
3783expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003784 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003785 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003786{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003787 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003788 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003789 char_u *funcname;
3790 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003791
Bram Moolenaare344bea2005-09-01 20:46:49 +00003792 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3793 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003794 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003795
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003796 /* Call 'completefunc' to obtain the list of matches. */
3797 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003798 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003799
Bram Moolenaare344bea2005-09-01 20:46:49 +00003800 pos = curwin->w_cursor;
3801 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3802 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003803 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003804 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003805
Bram Moolenaara94bc432006-03-10 21:42:59 +00003806 ins_compl_add_list(matchlist);
3807 list_unref(matchlist);
3808}
3809#endif /* FEAT_COMPL_FUNC */
3810
Bram Moolenaar39f05632006-03-19 22:15:26 +00003811#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003812/*
3813 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003814 */
3815 static void
3816ins_compl_add_list(list)
3817 list_T *list;
3818{
3819 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003820 int dir = compl_direction;
3821
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003822 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003823 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003824 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003825 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3826 /* if dir was BACKWARD then honor it just once */
3827 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003828 else if (did_emsg)
3829 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003830 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003831}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003832
3833/*
3834 * Add a match to the list of matches from a typeval_T.
3835 * If the given string is already in the list of completions, then return
3836 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3837 * maybe because alloc() returns NULL, then FAIL is returned.
3838 */
3839 int
3840ins_compl_add_tv(tv, dir)
3841 typval_T *tv;
3842 int dir;
3843{
3844 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003845 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003846 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003847 char_u *(cptext[CPT_COUNT]);
3848
3849 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3850 {
3851 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3852 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3853 (char_u *)"abbr", FALSE);
3854 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3855 (char_u *)"menu", FALSE);
3856 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3857 (char_u *)"kind", FALSE);
3858 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3859 (char_u *)"info", FALSE);
3860 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3861 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003862 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003863 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003864 }
3865 else
3866 {
3867 word = get_tv_string_chk(tv);
3868 vim_memset(cptext, 0, sizeof(cptext));
3869 }
3870 if (word == NULL || *word == NUL)
3871 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003872 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003873}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003874#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003875
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003876/*
3877 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003878 * The search starts at position "ini" in curbuf and in the direction
3879 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003880 * When "compl_started" is FALSE start at that position, otherwise continue
3881 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003882 * This may return before finding all the matches.
3883 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884 */
3885 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003886ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003887 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003888{
3889 static pos_T first_match_pos;
3890 static pos_T last_match_pos;
3891 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003892 static int found_all = FALSE; /* Found all matches of a
3893 certain type. */
3894 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003895
Bram Moolenaar572cb562005-08-05 21:35:02 +00003896 pos_T *pos;
3897 char_u **matches;
3898 int save_p_scs;
3899 int save_p_ws;
3900 int save_p_ic;
3901 int i;
3902 int num_matches;
3903 int len;
3904 int found_new_match;
3905 int type = ctrl_x_mode;
3906 char_u *ptr;
3907 char_u *dict = NULL;
3908 int dict_f = 0;
3909 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003910
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003911 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 {
3913 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3914 ins_buf->b_scanned = 0;
3915 found_all = FALSE;
3916 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003917 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003918 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 last_match_pos = first_match_pos = *ini;
3920 }
3921
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003922 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003923 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003924 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3925 for (;;)
3926 {
3927 found_new_match = FAIL;
3928
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003929 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930 * or if found_all says this entry is done. For ^X^L only use the
3931 * entries from 'complete' that look in loaded buffers. */
3932 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003933 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934 {
3935 found_all = FALSE;
3936 while (*e_cpt == ',' || *e_cpt == ' ')
3937 e_cpt++;
3938 if (*e_cpt == '.' && !curbuf->b_scanned)
3939 {
3940 ins_buf = curbuf;
3941 first_match_pos = *ini;
3942 /* So that ^N can match word immediately after cursor */
3943 if (ctrl_x_mode == 0)
3944 dec(&first_match_pos);
3945 last_match_pos = first_match_pos;
3946 type = 0;
3947 }
3948 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3949 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3950 {
3951 /* Scan a buffer, but not the current one. */
3952 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3953 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003954 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955 first_match_pos.col = last_match_pos.col = 0;
3956 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3957 last_match_pos.lnum = 0;
3958 type = 0;
3959 }
3960 else /* unloaded buffer, scan like dictionary */
3961 {
3962 found_all = TRUE;
3963 if (ins_buf->b_fname == NULL)
3964 continue;
3965 type = CTRL_X_DICTIONARY;
3966 dict = ins_buf->b_fname;
3967 dict_f = DICT_EXACT;
3968 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003969 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003970 ins_buf->b_fname == NULL
3971 ? buf_spname(ins_buf)
3972 : ins_buf->b_sfname == NULL
3973 ? (char *)ins_buf->b_fname
3974 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003975 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 }
3977 else if (*e_cpt == NUL)
3978 break;
3979 else
3980 {
3981 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3982 type = -1;
3983 else if (*e_cpt == 'k' || *e_cpt == 's')
3984 {
3985 if (*e_cpt == 'k')
3986 type = CTRL_X_DICTIONARY;
3987 else
3988 type = CTRL_X_THESAURUS;
3989 if (*++e_cpt != ',' && *e_cpt != NUL)
3990 {
3991 dict = e_cpt;
3992 dict_f = DICT_FIRST;
3993 }
3994 }
3995#ifdef FEAT_FIND_ID
3996 else if (*e_cpt == 'i')
3997 type = CTRL_X_PATH_PATTERNS;
3998 else if (*e_cpt == 'd')
3999 type = CTRL_X_PATH_DEFINES;
4000#endif
4001 else if (*e_cpt == ']' || *e_cpt == 't')
4002 {
4003 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004004 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004005 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004006 }
4007 else
4008 type = -1;
4009
4010 /* in any case e_cpt is advanced to the next entry */
4011 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4012
4013 found_all = TRUE;
4014 if (type == -1)
4015 continue;
4016 }
4017 }
4018
4019 switch (type)
4020 {
4021 case -1:
4022 break;
4023#ifdef FEAT_FIND_ID
4024 case CTRL_X_PATH_PATTERNS:
4025 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004026 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004027 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004029 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4031 (linenr_T)1, (linenr_T)MAXLNUM);
4032 break;
4033#endif
4034
4035 case CTRL_X_DICTIONARY:
4036 case CTRL_X_THESAURUS:
4037 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004038 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 : (type == CTRL_X_THESAURUS
4040 ? (*curbuf->b_p_tsr == NUL
4041 ? p_tsr
4042 : curbuf->b_p_tsr)
4043 : (*curbuf->b_p_dict == NUL
4044 ? p_dict
4045 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004046 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004047 dict != NULL ? dict_f
4048 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 dict = NULL;
4050 break;
4051
4052 case CTRL_X_TAGS:
4053 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4054 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004055 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004057 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 * of matches is found when compl_pattern is empty */
4059 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4061 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4062 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4063 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004064 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 }
4066 p_ic = save_p_ic;
4067 break;
4068
4069 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004070 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4072 {
4073
4074 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004075 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004076 ins_compl_add_matches(num_matches, matches,
4077#ifdef CASE_INSENSITIVE_FILENAME
4078 TRUE
4079#else
4080 FALSE
4081#endif
4082 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004083 }
4084 break;
4085
4086 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004087 if (expand_cmdline(&compl_xp, compl_pattern,
4088 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004090 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 break;
4092
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004093#ifdef FEAT_COMPL_FUNC
4094 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004095 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004096 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004097 break;
4098#endif
4099
Bram Moolenaar488c6512005-08-11 20:09:58 +00004100 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004101#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004102 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004103 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004104 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004105 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004106#endif
4107 break;
4108
Bram Moolenaar071d4272004-06-13 20:20:40 +00004109 default: /* normal ^P/^N and ^X^L */
4110 /*
4111 * If 'infercase' is set, don't use 'smartcase' here
4112 */
4113 save_p_scs = p_scs;
4114 if (ins_buf->b_p_inf)
4115 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004116
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117 /* buffers other than curbuf are scanned from the beginning or the
4118 * end but never from the middle, thus setting nowrapscan in this
4119 * buffers is a good idea, on the other hand, we always set
4120 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4121 save_p_ws = p_ws;
4122 if (ins_buf != curbuf)
4123 p_ws = FALSE;
4124 else if (*e_cpt == '.')
4125 p_ws = TRUE;
4126 for (;;)
4127 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004128 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004130 ++msg_silent; /* Don't want messages for wrapscan. */
4131
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004132 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4133 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004135 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004137 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004139 found_new_match = searchit(NULL, ins_buf, pos,
4140 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004141 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004142 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004143 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004144 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004146 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004147 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004148 first_match_pos = *pos;
4149 last_match_pos = *pos;
4150 }
4151 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004152 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 found_new_match = FAIL;
4154 if (found_new_match == FAIL)
4155 {
4156 if (ins_buf == curbuf)
4157 found_all = TRUE;
4158 break;
4159 }
4160
4161 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004162 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004163 && ini->lnum == pos->lnum
4164 && ini->col == pos->col)
4165 continue;
4166 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4167 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4168 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004169 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 {
4171 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4172 continue;
4173 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4174 if (!p_paste)
4175 ptr = skipwhite(ptr);
4176 }
4177 len = (int)STRLEN(ptr);
4178 }
4179 else
4180 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004181 char_u *tmp_ptr = ptr;
4182
4183 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 /* Skip if already inside a word. */
4187 if (vim_iswordp(tmp_ptr))
4188 continue;
4189 /* Find start of next word. */
4190 tmp_ptr = find_word_start(tmp_ptr);
4191 }
4192 /* Find end of this word. */
4193 tmp_ptr = find_word_end(tmp_ptr);
4194 len = (int)(tmp_ptr - ptr);
4195
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196 if ((compl_cont_status & CONT_ADDING)
4197 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 {
4199 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4200 {
4201 /* Try next line, if any. the new word will be
4202 * "join" as if the normal command "J" was used.
4203 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004204 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 * works -- Acevedo */
4206 STRNCPY(IObuff, ptr, len);
4207 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4208 tmp_ptr = ptr = skipwhite(ptr);
4209 /* Find start of next word. */
4210 tmp_ptr = find_word_start(tmp_ptr);
4211 /* Find end of next word. */
4212 tmp_ptr = find_word_end(tmp_ptr);
4213 if (tmp_ptr > ptr)
4214 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004215 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004217 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004218 IObuff[len++] = ' ';
4219 /* IObuf =~ "\k.* ", thus len >= 2 */
4220 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004221 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 || (vim_strchr(p_cpo, CPO_JOINSP)
4223 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004224 && (IObuff[len - 2] == '?'
4225 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 IObuff[len++] = ' ';
4227 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004228 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 if (tmp_ptr - ptr >= IOSIZE - len)
4230 tmp_ptr = ptr + IOSIZE - len - 1;
4231 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4232 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004233 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 }
4235 IObuff[len] = NUL;
4236 ptr = IObuff;
4237 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 continue;
4240 }
4241 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004242 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004243 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004244 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
4246 found_new_match = OK;
4247 break;
4248 }
4249 }
4250 p_scs = save_p_scs;
4251 p_ws = save_p_ws;
4252 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004253
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004254 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004255 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004256 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 found_new_match = OK;
4258
4259 /* break the loop for specialized modes (use 'complete' just for the
4260 * generic ctrl_x_mode == 0) or when we've found a new match */
4261 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004262 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004263 {
4264 if (got_int)
4265 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004266 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004267 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004268 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004269
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004270 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4271 || compl_interrupted)
4272 break;
4273 compl_started = TRUE;
4274 }
4275 else
4276 {
4277 /* Mark a buffer scanned when it has been scanned completely */
4278 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4279 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004281 compl_started = FALSE;
4282 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285
4286 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4287 && *e_cpt == NUL) /* Got to end of 'complete' */
4288 found_new_match = FAIL;
4289
4290 i = -1; /* total of matches, unknown */
4291 if (found_new_match == FAIL
4292 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4293 i = ins_compl_make_cyclic();
4294
4295 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004296 * just been made cyclic then we have to move compl_curr_match to the next
4297 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004298 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4299 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004300 if (compl_curr_match == NULL)
4301 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 return i;
4303}
4304
4305/* Delete the old text being completed. */
4306 static void
4307ins_compl_delete()
4308{
4309 int i;
4310
4311 /*
4312 * In insert mode: Delete the typed part.
4313 * In replace mode: Put the old characters back, if any.
4314 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004315 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004316 backspace_until_column(i);
4317 changed_cline_bef_curs();
4318}
4319
4320/* Insert the new text being completed. */
4321 static void
4322ins_compl_insert()
4323{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004324 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004325 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4326 compl_used_match = FALSE;
4327 else
4328 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329}
4330
4331/*
4332 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004333 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4334 * get more completions. If it is FALSE, then we just do nothing when there
4335 * are no more completions in a given direction. The latter case is used when
4336 * we are still in the middle of finding completions, to allow browsing
4337 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 * Return the total number of matches, or -1 if still unknown -- webb.
4339 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004340 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4341 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342 *
4343 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004344 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4345 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346 */
4347 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004348ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004350 int count; /* repeat completion this many times; should
4351 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004352 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353{
4354 int num_matches = -1;
4355 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004356 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004357 compl_T *found_compl = NULL;
4358 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004359 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004360
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004361 if (compl_leader != NULL
4362 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004364 /* Set "compl_shown_match" to the actually shown match, it may differ
4365 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004366 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004367 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004368 && compl_shown_match->cp_next != NULL
4369 && compl_shown_match->cp_next != compl_first_match)
4370 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004371
4372 /* If we didn't find it searching forward, and compl_shows_dir is
4373 * backward, find the last match. */
4374 if (compl_shows_dir == BACKWARD
4375 && !ins_compl_equal(compl_shown_match,
4376 compl_leader, (int)STRLEN(compl_leader))
4377 && (compl_shown_match->cp_next == NULL
4378 || compl_shown_match->cp_next == compl_first_match))
4379 {
4380 while (!ins_compl_equal(compl_shown_match,
4381 compl_leader, (int)STRLEN(compl_leader))
4382 && compl_shown_match->cp_prev != NULL
4383 && compl_shown_match->cp_prev != compl_first_match)
4384 compl_shown_match = compl_shown_match->cp_prev;
4385 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004386 }
4387
4388 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004389 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 /* Delete old text to be replaced */
4391 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004392
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004393 /* When finding the longest common text we stick at the original text,
4394 * don't let CTRL-N or CTRL-P move to the first match. */
4395 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4396
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004397 /* When restarting the search don't insert the first match either. */
4398 if (compl_restarting)
4399 {
4400 advance = FALSE;
4401 compl_restarting = FALSE;
4402 }
4403
Bram Moolenaare3226be2005-12-18 22:10:00 +00004404 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4405 * around. */
4406 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004408 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004410 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004411 found_end = (compl_first_match != NULL
4412 && (compl_shown_match->cp_next == compl_first_match
4413 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004414 }
4415 else if (compl_shows_dir == BACKWARD
4416 && compl_shown_match->cp_prev != NULL)
4417 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004418 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004419 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004420 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421 }
4422 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004423 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004424 if (!allow_get_expansion)
4425 {
4426 if (advance)
4427 {
4428 if (compl_shows_dir == BACKWARD)
4429 compl_pending -= todo + 1;
4430 else
4431 compl_pending += todo + 1;
4432 }
4433 return -1;
4434 }
4435
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004436 if (advance)
4437 {
4438 if (compl_shows_dir == BACKWARD)
4439 --compl_pending;
4440 else
4441 ++compl_pending;
4442 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004443
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004444 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004445 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004446
4447 /* handle any pending completions */
4448 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004449 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004450 {
4451 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4452 {
4453 compl_shown_match = compl_shown_match->cp_next;
4454 --compl_pending;
4455 }
4456 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4457 {
4458 compl_shown_match = compl_shown_match->cp_prev;
4459 ++compl_pending;
4460 }
4461 else
4462 break;
4463 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004464 found_end = FALSE;
4465 }
4466 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4467 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004468 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004469 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004470 ++todo;
4471 else
4472 /* Remember a matching item. */
4473 found_compl = compl_shown_match;
4474
4475 /* Stop at the end of the list when we found a usable match. */
4476 if (found_end)
4477 {
4478 if (found_compl != NULL)
4479 {
4480 compl_shown_match = found_compl;
4481 break;
4482 }
4483 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485 }
4486
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004487 /* Insert the text of the new completion, or the compl_leader. */
4488 if (insert_match)
4489 {
4490 if (!compl_get_longest || compl_used_match)
4491 ins_compl_insert();
4492 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004493 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004494 }
4495 else
4496 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497
4498 if (!allow_get_expansion)
4499 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004500 /* may undisplay the popup menu first */
4501 ins_compl_upd_pum();
4502
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004503 /* redraw to show the user what was inserted */
4504 update_screen(0);
4505
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004506 /* display the updated popup menu */
4507 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004508#ifdef FEAT_GUI
4509 if (gui.in_use)
4510 {
4511 /* Show the cursor after the match, not after the redrawn text. */
4512 setcursor();
4513 out_flush();
4514 gui_update_cursor(FALSE, FALSE);
4515 }
4516#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004517
Bram Moolenaar071d4272004-06-13 20:20:40 +00004518 /* Delete old text to be replaced, since we're still searching and
4519 * don't want to match ourselves! */
4520 ins_compl_delete();
4521 }
4522
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004523 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004524 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004525 compl_enter_selects = !insert_match && compl_match_array != NULL;
4526
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 /*
4528 * Show the file name for the match (if any)
4529 * Truncate the file name to avoid a wait for return.
4530 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004531 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004532 {
4533 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004534 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004535 if (i <= 0)
4536 i = 0;
4537 else
4538 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004539 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 msg(IObuff);
4541 redraw_cmdline = FALSE; /* don't overwrite! */
4542 }
4543
4544 return num_matches;
4545}
4546
4547/*
4548 * Call this while finding completions, to check whether the user has hit a key
4549 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004550 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004551 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004552 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004553 */
4554 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004555ins_compl_check_keys(frequency)
4556 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557{
4558 static int count = 0;
4559
4560 int c;
4561
4562 /* Don't check when reading keys from a script. That would break the test
4563 * scripts */
4564 if (using_script())
4565 return;
4566
4567 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004568 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 return;
4570 count = 0;
4571
Bram Moolenaara260a972006-06-23 19:36:29 +00004572 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4573 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004575 if (c != NUL)
4576 {
4577 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4578 {
4579 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004580 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004581 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4582 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004583 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004584 else
4585 {
4586 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004587 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004588 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004589 if (c != K_IGNORE)
4590 {
4591 /* Don't interrupt completion when the character wasn't typed,
4592 * e.g., when doing @q to replay keys. */
4593 if (c != Ctrl_R && KeyTyped)
4594 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004595
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004596 vungetc(c);
4597 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004600 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004601 {
4602 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4603
4604 compl_pending = 0;
4605 (void)ins_compl_next(FALSE, todo, TRUE);
4606 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004607}
4608
4609/*
4610 * Decide the direction of Insert mode complete from the key typed.
4611 * Returns BACKWARD or FORWARD.
4612 */
4613 static int
4614ins_compl_key2dir(c)
4615 int c;
4616{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004617 if (c == Ctrl_P || c == Ctrl_L
4618 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4619 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004620 return BACKWARD;
4621 return FORWARD;
4622}
4623
4624/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004625 * Return TRUE for keys that are used for completion only when the popup menu
4626 * is visible.
4627 */
4628 static int
4629ins_compl_pum_key(c)
4630 int c;
4631{
4632 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004633 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4634 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004635}
4636
4637/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004638 * Decide the number of completions to move forward.
4639 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4640 */
4641 static int
4642ins_compl_key2count(c)
4643 int c;
4644{
4645 int h;
4646
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004647 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004648 {
4649 h = pum_get_height();
4650 if (h > 3)
4651 h -= 2; /* keep some context */
4652 return h;
4653 }
4654 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655}
4656
4657/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004658 * Return TRUE if completion with "c" should insert the match, FALSE if only
4659 * to change the currently selected completion.
4660 */
4661 static int
4662ins_compl_use_match(c)
4663 int c;
4664{
4665 switch (c)
4666 {
4667 case K_UP:
4668 case K_DOWN:
4669 case K_PAGEDOWN:
4670 case K_KPAGEDOWN:
4671 case K_S_DOWN:
4672 case K_PAGEUP:
4673 case K_KPAGEUP:
4674 case K_S_UP:
4675 return FALSE;
4676 }
4677 return TRUE;
4678}
4679
4680/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 * Do Insert mode completion.
4682 * Called when character "c" was typed, which has a meaning for completion.
4683 * Returns OK if completion was done, FAIL if something failed (out of mem).
4684 */
4685 static int
4686ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004687 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004689 char_u *line;
4690 int startcol = 0; /* column where searched text starts */
4691 colnr_T curs_col; /* cursor column */
4692 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004693 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694
Bram Moolenaare3226be2005-12-18 22:10:00 +00004695 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004696 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 {
4698 /* First time we hit ^N or ^P (in a row, I mean) */
4699
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 did_ai = FALSE;
4701#ifdef FEAT_SMARTINDENT
4702 did_si = FALSE;
4703 can_si = FALSE;
4704 can_si_back = FALSE;
4705#endif
4706 if (stop_arrow() == FAIL)
4707 return FAIL;
4708
4709 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004710 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004711 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004712
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004713 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004714 * "compl_startpos" to the cursor as a pattern to add a new word
4715 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004716 * "compl_startpos" is not in the same line as the cursor then fix it
4717 * (the line has been split because it was longer than 'tw'). if SOL
4718 * is set then skip the previous pattern, a word at the beginning of
4719 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004720 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4721 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004722 {
4723 /*
4724 * it is a continued search
4725 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004726 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4728 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4729 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004730 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004731 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004732 /* line (probably) wrapped, set compl_startpos to the
4733 * first non_blank in the line, if it is not a wordchar
4734 * include it to get a better pattern, but then we don't
4735 * want the "\\<" prefix, check it bellow */
4736 compl_col = (colnr_T)(skipwhite(line) - line);
4737 compl_startpos.col = compl_col;
4738 compl_startpos.lnum = curwin->w_cursor.lnum;
4739 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
4741 else
4742 {
4743 /* S_IPOS was set when we inserted a word that was at the
4744 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004745 * mode but first we need to redefine compl_startpos */
4746 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004748 compl_cont_status |= CONT_SOL;
4749 compl_startpos.col = (colnr_T)(skipwhite(
4750 line + compl_length
4751 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004753 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004755 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004756 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004757 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004759 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004761 compl_cont_status &= ~CONT_SOL;
4762 compl_length = (IOSIZE - MIN_SPACE);
4763 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004765 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4766 if (compl_length < 1)
4767 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 }
4769 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004770 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004771 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004772 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
4774 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004775 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004776
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004777 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004778 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004779 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004781 compl_cont_status = 0;
4782 compl_cont_status |= CONT_N_ADDS;
4783 compl_startpos = curwin->w_cursor;
4784 startcol = (int)curs_col;
4785 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
4787
4788 /* Work out completion pattern and original text -- webb */
4789 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4790 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004791 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4793 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004794 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004796 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004797 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004798 compl_col += ++startcol;
4799 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 }
4801 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004802 compl_pattern = str_foldcase(line + compl_col,
4803 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004805 compl_pattern = vim_strnsave(line + compl_col,
4806 compl_length);
4807 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 return FAIL;
4809 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004810 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004811 {
4812 char_u *prefix = (char_u *)"\\<";
4813
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004814 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004815 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004816 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004817 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004819 if (!vim_iswordp(line + compl_col)
4820 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 && (
4822#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004825 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826#endif
4827 )))
4828 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004829 STRCPY((char *)compl_pattern, prefix);
4830 (void)quote_meta(compl_pattern + STRLEN(prefix),
4831 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004833 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004835 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004837 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004838#endif
4839 )
4840 {
4841 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004842 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4843 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004844 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004845 compl_col += curs_col;
4846 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848 else
4849 {
4850#ifdef FEAT_MBYTE
4851 /* Search the point of change class of multibyte character
4852 * or not a word single byte character backward. */
4853 if (has_mbyte)
4854 {
4855 int base_class;
4856 int head_off;
4857
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004858 startcol -= (*mb_head_off)(line, line + startcol);
4859 base_class = mb_get_class(line + startcol);
4860 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004862 head_off = (*mb_head_off)(line, line + startcol);
4863 if (base_class != mb_get_class(line + startcol
4864 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004866 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004867 }
4868 }
4869 else
4870#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004871 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004873 compl_col += ++startcol;
4874 compl_length = (int)curs_col - startcol;
4875 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 {
4877 /* Only match word with at least two chars -- webb
4878 * there's no need to call quote_meta,
4879 * alloc(7) is enough -- Acevedo
4880 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004881 compl_pattern = alloc(7);
4882 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004884 STRCPY((char *)compl_pattern, "\\<");
4885 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4886 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 }
4888 else
4889 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004890 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004891 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004892 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004894 STRCPY((char *)compl_pattern, "\\<");
4895 (void)quote_meta(compl_pattern + 2, line + compl_col,
4896 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 }
4898 }
4899 }
4900 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4901 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004902 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004903 compl_length = (int)curs_col - (int)compl_col;
4904 if (compl_length < 0) /* cursor in indent: empty pattern */
4905 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004907 compl_pattern = str_foldcase(line + compl_col, compl_length,
4908 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004910 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4911 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 return FAIL;
4913 }
4914 else if (ctrl_x_mode == CTRL_X_FILES)
4915 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004916 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004918 compl_col += ++startcol;
4919 compl_length = (int)curs_col - startcol;
4920 compl_pattern = addstar(line + compl_col, compl_length,
4921 EXPAND_FILES);
4922 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 return FAIL;
4924 }
4925 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4926 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004927 compl_pattern = vim_strnsave(line, curs_col);
4928 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004930 set_cmd_context(&compl_xp, compl_pattern,
4931 (int)STRLEN(compl_pattern), curs_col);
4932 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4933 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004934 /* No completion possible, use an empty pattern to get a
4935 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004936 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004937 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004938 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4939 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004941 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004942 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004943#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004944 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004945 * Call user defined function 'completefunc' with "a:findstart"
4946 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004947 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004948 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004949 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004950 char_u *funcname;
4951 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004952
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004953 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004954 * string */
4955 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4956 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4957 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004958 {
4959 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4960 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004961 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004962 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004963
4964 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004965 args[1] = NULL;
4966 pos = curwin->w_cursor;
4967 col = call_func_retnr(funcname, 2, args, FALSE);
4968 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004969
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004970 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004971 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004972 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004973 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004974 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004975
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004976 /* Setup variables for completion. Need to obtain "line" again,
4977 * it may have become invalid. */
4978 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004979 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004980 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4981 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004982#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004983 return FAIL;
4984 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004985 else if (ctrl_x_mode == CTRL_X_SPELL)
4986 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004987#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004988 if (spell_bad_len > 0)
4989 compl_col = curs_col - spell_bad_len;
4990 else
4991 compl_col = spell_word_start(startcol);
4992 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004993 {
4994 compl_length = 0;
4995 compl_col = curs_col;
4996 }
4997 else
4998 {
4999 spell_expand_check_cap(compl_col);
5000 compl_length = (int)curs_col - compl_col;
5001 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005002 /* Need to obtain "line" again, it may have become invalid. */
5003 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005004 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5005 if (compl_pattern == NULL)
5006#endif
5007 return FAIL;
5008 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005009 else
5010 {
5011 EMSG2(_(e_intern2), "ins_complete()");
5012 return FAIL;
5013 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005014
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005015 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
5017 edit_submode_pre = (char_u *)_(" Adding");
5018 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5019 {
5020 /* Insert a new line, keep indentation but ignore 'comments' */
5021#ifdef FEAT_COMMENTS
5022 char_u *old = curbuf->b_p_com;
5023
5024 curbuf->b_p_com = (char_u *)"";
5025#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005026 compl_startpos.lnum = curwin->w_cursor.lnum;
5027 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 ins_eol('\r');
5029#ifdef FEAT_COMMENTS
5030 curbuf->b_p_com = old;
5031#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005032 compl_length = 0;
5033 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005034 }
5035 }
5036 else
5037 {
5038 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005039 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
5041
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005042 if (compl_cont_status & CONT_LOCAL)
5043 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005044 else
5045 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5046
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005047 /* Always add completion for the original text. */
5048 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005049 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5050 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005051 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005053 vim_free(compl_pattern);
5054 compl_pattern = NULL;
5055 vim_free(compl_orig_text);
5056 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 return FAIL;
5058 }
5059
5060 /* showmode might reset the internal line pointers, so it must
5061 * be called before line = ml_get(), or when this address is no
5062 * longer needed. -- Acevedo.
5063 */
5064 edit_submode_extra = (char_u *)_("-- Searching...");
5065 edit_submode_highl = HLF_COUNT;
5066 showmode();
5067 edit_submode_extra = NULL;
5068 out_flush();
5069 }
5070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005071 compl_shown_match = compl_curr_match;
5072 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005073
5074 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005075 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005077 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005078 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005080 /* may undisplay the popup menu */
5081 ins_compl_upd_pum();
5082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005083 if (n > 1) /* all matches have been found */
5084 compl_matches = n;
5085 compl_curr_match = compl_shown_match;
5086 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005087
Bram Moolenaard68071d2006-05-02 22:08:30 +00005088 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5089 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005090 if (got_int && !global_busy)
5091 {
5092 (void)vgetc();
5093 got_int = FALSE;
5094 }
5095
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005096 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005097 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005099 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5100 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5102 edit_submode_highl = HLF_E;
5103 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5104 * because we couldn't expand anything at first place, but if we used
5105 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5106 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005107 if ( compl_length > 1
5108 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005109 || (ctrl_x_mode != 0
5110 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5111 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005112 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 }
5114
Bram Moolenaar572cb562005-08-05 21:35:02 +00005115 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005117 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005118 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005119
5120 if (edit_submode_extra == NULL)
5121 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005122 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005123 {
5124 edit_submode_extra = (char_u *)_("Back at original");
5125 edit_submode_highl = HLF_W;
5126 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005127 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
5129 edit_submode_extra = (char_u *)_("Word from other line");
5130 edit_submode_highl = HLF_COUNT;
5131 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005132 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
5134 edit_submode_extra = (char_u *)_("The only match");
5135 edit_submode_highl = HLF_COUNT;
5136 }
5137 else
5138 {
5139 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005140 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005142 int number = 0;
5143 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005145 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005146 {
5147 /* search backwards for the first valid (!= -1) number.
5148 * This should normally succeed already at the first loop
5149 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005150 for (match = compl_curr_match->cp_prev; match != NULL
5151 && match != compl_first_match;
5152 match = match->cp_prev)
5153 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005155 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 break;
5157 }
5158 if (match != NULL)
5159 /* go up and assign all numbers which are not assigned
5160 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005161 for (match = match->cp_next;
5162 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005163 match = match->cp_next)
5164 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 }
5166 else /* BACKWARD */
5167 {
5168 /* search forwards (upwards) for the first valid (!= -1)
5169 * number. This should normally succeed already at the
5170 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005171 for (match = compl_curr_match->cp_next; match != NULL
5172 && match != compl_first_match;
5173 match = match->cp_next)
5174 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005176 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178 }
5179 if (match != NULL)
5180 /* go down and assign all numbers which are not
5181 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005182 for (match = match->cp_prev; match
5183 && match->cp_number == -1;
5184 match = match->cp_prev)
5185 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 }
5187 }
5188
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005189 /* The match should always have a sequence number now, this is
5190 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005191 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005193 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5194 * Translations may need more than twice that. */
5195 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005197 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005198 vim_snprintf((char *)match_ref, sizeof(match_ref),
5199 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005200 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005202 vim_snprintf((char *)match_ref, sizeof(match_ref),
5203 _("match %d"),
5204 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205 edit_submode_extra = match_ref;
5206 edit_submode_highl = HLF_R;
5207 if (dollar_vcol)
5208 curs_columns(FALSE);
5209 }
5210 }
5211 }
5212
5213 /* Show a message about what (completion) mode we're in. */
5214 showmode();
5215 if (edit_submode_extra != NULL)
5216 {
5217 if (!p_smd)
5218 msg_attr(edit_submode_extra,
5219 edit_submode_highl < HLF_COUNT
5220 ? hl_attr(edit_submode_highl) : 0);
5221 }
5222 else
5223 msg_clr_cmdline(); /* necessary for "noshowmode" */
5224
Bram Moolenaard68071d2006-05-02 22:08:30 +00005225 /* Show the popup menu, unless we got interrupted. */
5226 if (!compl_interrupted)
5227 {
5228 /* RedrawingDisabled may be set when invoked through complete(). */
5229 n = RedrawingDisabled;
5230 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005231
5232 /* If the cursor moved we need to remove the pum first. */
5233 setcursor();
5234 if (save_w_wrow != curwin->w_wrow)
5235 ins_compl_del_pum();
5236
Bram Moolenaard68071d2006-05-02 22:08:30 +00005237 ins_compl_show_pum();
5238 setcursor();
5239 RedrawingDisabled = n;
5240 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005241 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005242 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005243
Bram Moolenaar071d4272004-06-13 20:20:40 +00005244 return OK;
5245}
5246
5247/*
5248 * Looks in the first "len" chars. of "src" for search-metachars.
5249 * If dest is not NULL the chars. are copied there quoting (with
5250 * a backslash) the metachars, and dest would be NUL terminated.
5251 * Returns the length (needed) of dest
5252 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005253 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005254quote_meta(dest, src, len)
5255 char_u *dest;
5256 char_u *src;
5257 int len;
5258{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005259 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005260
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005261 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 {
5263 switch (*src)
5264 {
5265 case '.':
5266 case '*':
5267 case '[':
5268 if (ctrl_x_mode == CTRL_X_DICTIONARY
5269 || ctrl_x_mode == CTRL_X_THESAURUS)
5270 break;
5271 case '~':
5272 if (!p_magic) /* quote these only if magic is set */
5273 break;
5274 case '\\':
5275 if (ctrl_x_mode == CTRL_X_DICTIONARY
5276 || ctrl_x_mode == CTRL_X_THESAURUS)
5277 break;
5278 case '^': /* currently it's not needed. */
5279 case '$':
5280 m++;
5281 if (dest != NULL)
5282 *dest++ = '\\';
5283 break;
5284 }
5285 if (dest != NULL)
5286 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005287# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005288 /* Copy remaining bytes of a multibyte character. */
5289 if (has_mbyte)
5290 {
5291 int i, mb_len;
5292
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005293 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 if (mb_len > 0 && len >= mb_len)
5295 for (i = 0; i < mb_len; ++i)
5296 {
5297 --len;
5298 ++src;
5299 if (dest != NULL)
5300 *dest++ = *src;
5301 }
5302 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005303# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
5305 if (dest != NULL)
5306 *dest = NUL;
5307
5308 return m;
5309}
5310#endif /* FEAT_INS_EXPAND */
5311
5312/*
5313 * Next character is interpreted literally.
5314 * A one, two or three digit decimal number is interpreted as its byte value.
5315 * If one or two digits are entered, the next character is given to vungetc().
5316 * For Unicode a character > 255 may be returned.
5317 */
5318 int
5319get_literal()
5320{
5321 int cc;
5322 int nc;
5323 int i;
5324 int hex = FALSE;
5325 int octal = FALSE;
5326#ifdef FEAT_MBYTE
5327 int unicode = 0;
5328#endif
5329
5330 if (got_int)
5331 return Ctrl_C;
5332
5333#ifdef FEAT_GUI
5334 /*
5335 * In GUI there is no point inserting the internal code for a special key.
5336 * It is more useful to insert the string "<KEY>" instead. This would
5337 * probably be useful in a text window too, but it would not be
5338 * vi-compatible (maybe there should be an option for it?) -- webb
5339 */
5340 if (gui.in_use)
5341 ++allow_keys;
5342#endif
5343#ifdef USE_ON_FLY_SCROLL
5344 dont_scroll = TRUE; /* disallow scrolling here */
5345#endif
5346 ++no_mapping; /* don't map the next key hits */
5347 cc = 0;
5348 i = 0;
5349 for (;;)
5350 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005351 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005352#ifdef FEAT_CMDL_INFO
5353 if (!(State & CMDLINE)
5354# ifdef FEAT_MBYTE
5355 && MB_BYTE2LEN_CHECK(nc) == 1
5356# endif
5357 )
5358 add_to_showcmd(nc);
5359#endif
5360 if (nc == 'x' || nc == 'X')
5361 hex = TRUE;
5362 else if (nc == 'o' || nc == 'O')
5363 octal = TRUE;
5364#ifdef FEAT_MBYTE
5365 else if (nc == 'u' || nc == 'U')
5366 unicode = nc;
5367#endif
5368 else
5369 {
5370 if (hex
5371#ifdef FEAT_MBYTE
5372 || unicode != 0
5373#endif
5374 )
5375 {
5376 if (!vim_isxdigit(nc))
5377 break;
5378 cc = cc * 16 + hex2nr(nc);
5379 }
5380 else if (octal)
5381 {
5382 if (nc < '0' || nc > '7')
5383 break;
5384 cc = cc * 8 + nc - '0';
5385 }
5386 else
5387 {
5388 if (!VIM_ISDIGIT(nc))
5389 break;
5390 cc = cc * 10 + nc - '0';
5391 }
5392
5393 ++i;
5394 }
5395
5396 if (cc > 255
5397#ifdef FEAT_MBYTE
5398 && unicode == 0
5399#endif
5400 )
5401 cc = 255; /* limit range to 0-255 */
5402 nc = 0;
5403
5404 if (hex) /* hex: up to two chars */
5405 {
5406 if (i >= 2)
5407 break;
5408 }
5409#ifdef FEAT_MBYTE
5410 else if (unicode) /* Unicode: up to four or eight chars */
5411 {
5412 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5413 break;
5414 }
5415#endif
5416 else if (i >= 3) /* decimal or octal: up to three chars */
5417 break;
5418 }
5419 if (i == 0) /* no number entered */
5420 {
5421 if (nc == K_ZERO) /* NUL is stored as NL */
5422 {
5423 cc = '\n';
5424 nc = 0;
5425 }
5426 else
5427 {
5428 cc = nc;
5429 nc = 0;
5430 }
5431 }
5432
5433 if (cc == 0) /* NUL is stored as NL */
5434 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005435#ifdef FEAT_MBYTE
5436 if (enc_dbcs && (cc & 0xff) == 0)
5437 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5438 second byte will cause trouble! */
5439#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005440
5441 --no_mapping;
5442#ifdef FEAT_GUI
5443 if (gui.in_use)
5444 --allow_keys;
5445#endif
5446 if (nc)
5447 vungetc(nc);
5448 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5449 return cc;
5450}
5451
5452/*
5453 * Insert character, taking care of special keys and mod_mask
5454 */
5455 static void
5456insert_special(c, allow_modmask, ctrlv)
5457 int c;
5458 int allow_modmask;
5459 int ctrlv; /* c was typed after CTRL-V */
5460{
5461 char_u *p;
5462 int len;
5463
5464 /*
5465 * Special function key, translate into "<Key>". Up to the last '>' is
5466 * inserted with ins_str(), so as not to replace characters in replace
5467 * mode.
5468 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5469 * unless 'allow_modmask' is TRUE.
5470 */
5471#ifdef MACOS
5472 /* Command-key never produces a normal key */
5473 if (mod_mask & MOD_MASK_CMD)
5474 allow_modmask = TRUE;
5475#endif
5476 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5477 {
5478 p = get_special_key_name(c, mod_mask);
5479 len = (int)STRLEN(p);
5480 c = p[len - 1];
5481 if (len > 2)
5482 {
5483 if (stop_arrow() == FAIL)
5484 return;
5485 p[len - 1] = NUL;
5486 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005487 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005488 ctrlv = FALSE;
5489 }
5490 }
5491 if (stop_arrow() == OK)
5492 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5493}
5494
5495/*
5496 * Special characters in this context are those that need processing other
5497 * than the simple insertion that can be performed here. This includes ESC
5498 * which terminates the insert, and CR/NL which need special processing to
5499 * open up a new line. This routine tries to optimize insertions performed by
5500 * the "redo", "undo" or "put" commands, so it needs to know when it should
5501 * stop and defer processing to the "normal" mechanism.
5502 * '0' and '^' are special, because they can be followed by CTRL-D.
5503 */
5504#ifdef EBCDIC
5505# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5506#else
5507# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5508#endif
5509
5510#ifdef FEAT_MBYTE
5511# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5512#else
5513# define WHITECHAR(cc) vim_iswhite(cc)
5514#endif
5515
5516 void
5517insertchar(c, flags, second_indent)
5518 int c; /* character to insert or NUL */
5519 int flags; /* INSCHAR_FORMAT, etc. */
5520 int second_indent; /* indent for second line if >= 0 */
5521{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005522 int textwidth;
5523#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005526 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005527
5528 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5529 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530
5531 /*
5532 * Try to break the line in two or more pieces when:
5533 * - Always do this if we have been called to do formatting only.
5534 * - Always do this when 'formatoptions' has the 'a' flag and the line
5535 * ends in white space.
5536 * - Otherwise:
5537 * - Don't do this if inserting a blank
5538 * - Don't do this if an existing character is being replaced, unless
5539 * we're in VREPLACE mode.
5540 * - Do this if the cursor is not on the line where insert started
5541 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5542 * before the insert.
5543 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5544 * before 'textwidth'
5545 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005546 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005547 && ((flags & INSCHAR_FORMAT)
5548 || (!vim_iswhite(c)
5549 && !((State & REPLACE_FLAG)
5550#ifdef FEAT_VREPLACE
5551 && !(State & VREPLACE_FLAG)
5552#endif
5553 && *ml_get_cursor() != NUL)
5554 && (curwin->w_cursor.lnum != Insstart.lnum
5555 || ((!has_format_option(FO_INS_LONG)
5556 || Insstart_textlen <= (colnr_T)textwidth)
5557 && (!fo_ins_blank
5558 || Insstart_blank_vcol <= (colnr_T)textwidth
5559 ))))))
5560 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005561 /* Format with 'formatexpr' when it's set. Use internal formatting
5562 * when 'formatexpr' isn't set or it returns non-zero. */
5563#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005564 int do_internal = TRUE;
5565
Bram Moolenaar81a82092008-03-12 16:27:00 +00005566 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005567 {
5568 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5569 /* It may be required to save for undo again, e.g. when setline()
5570 * was called. */
5571 ins_need_undo = TRUE;
5572 }
5573 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005574#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005575 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005576 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005577
Bram Moolenaar071d4272004-06-13 20:20:40 +00005578 if (c == NUL) /* only formatting was wanted */
5579 return;
5580
5581#ifdef FEAT_COMMENTS
5582 /* Check whether this character should end a comment. */
5583 if (did_ai && (int)c == end_comment_pending)
5584 {
5585 char_u *line;
5586 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5587 int middle_len, end_len;
5588 int i;
5589
5590 /*
5591 * Need to remove existing (middle) comment leader and insert end
5592 * comment leader. First, check what comment leader we can find.
5593 */
5594 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5595 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5596 {
5597 /* Skip middle-comment string */
5598 while (*p && p[-1] != ':') /* find end of middle flags */
5599 ++p;
5600 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5601 /* Don't count trailing white space for middle_len */
5602 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5603 --middle_len;
5604
5605 /* Find the end-comment string */
5606 while (*p && p[-1] != ':') /* find end of end flags */
5607 ++p;
5608 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5609
5610 /* Skip white space before the cursor */
5611 i = curwin->w_cursor.col;
5612 while (--i >= 0 && vim_iswhite(line[i]))
5613 ;
5614 i++;
5615
5616 /* Skip to before the middle leader */
5617 i -= middle_len;
5618
5619 /* Check some expected things before we go on */
5620 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5621 {
5622 /* Backspace over all the stuff we want to replace */
5623 backspace_until_column(i);
5624
5625 /*
5626 * Insert the end-comment string, except for the last
5627 * character, which will get inserted as normal later.
5628 */
5629 ins_bytes_len(lead_end, end_len - 1);
5630 }
5631 }
5632 }
5633 end_comment_pending = NUL;
5634#endif
5635
5636 did_ai = FALSE;
5637#ifdef FEAT_SMARTINDENT
5638 did_si = FALSE;
5639 can_si = FALSE;
5640 can_si_back = FALSE;
5641#endif
5642
5643 /*
5644 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5645 * This speeds up normal text input considerably.
5646 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5647 * need to re-indent at a ':', or any other character (but not what
5648 * 'paste' is set)..
5649 */
5650#ifdef USE_ON_FLY_SCROLL
5651 dont_scroll = FALSE; /* allow scrolling here */
5652#endif
5653
5654 if ( !ISSPECIAL(c)
5655#ifdef FEAT_MBYTE
5656 && (!has_mbyte || (*mb_char2len)(c) == 1)
5657#endif
5658 && vpeekc() != NUL
5659 && !(State & REPLACE_FLAG)
5660#ifdef FEAT_CINDENT
5661 && !cindent_on()
5662#endif
5663#ifdef FEAT_RIGHTLEFT
5664 && !p_ri
5665#endif
5666 )
5667 {
5668#define INPUT_BUFLEN 100
5669 char_u buf[INPUT_BUFLEN + 1];
5670 int i;
5671 colnr_T virtcol = 0;
5672
5673 buf[0] = c;
5674 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005675 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 virtcol = get_nolist_virtcol();
5677 /*
5678 * Stop the string when:
5679 * - no more chars available
5680 * - finding a special character (command key)
5681 * - buffer is full
5682 * - running into the 'textwidth' boundary
5683 * - need to check for abbreviation: A non-word char after a word-char
5684 */
5685 while ( (c = vpeekc()) != NUL
5686 && !ISSPECIAL(c)
5687#ifdef FEAT_MBYTE
5688 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5689#endif
5690 && i < INPUT_BUFLEN
5691 && (textwidth == 0
5692 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5693 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5694 {
5695#ifdef FEAT_RIGHTLEFT
5696 c = vgetc();
5697 if (p_hkmap && KeyTyped)
5698 c = hkmap(c); /* Hebrew mode mapping */
5699# ifdef FEAT_FKMAP
5700 if (p_fkmap && KeyTyped)
5701 c = fkmap(c); /* Farsi mode mapping */
5702# endif
5703 buf[i++] = c;
5704#else
5705 buf[i++] = vgetc();
5706#endif
5707 }
5708
5709#ifdef FEAT_DIGRAPHS
5710 do_digraph(-1); /* clear digraphs */
5711 do_digraph(buf[i-1]); /* may be the start of a digraph */
5712#endif
5713 buf[i] = NUL;
5714 ins_str(buf);
5715 if (flags & INSCHAR_CTRLV)
5716 {
5717 redo_literal(*buf);
5718 i = 1;
5719 }
5720 else
5721 i = 0;
5722 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005723 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
5725 else
5726 {
5727#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005728 int cc;
5729
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5731 {
5732 char_u buf[MB_MAXBYTES + 1];
5733
5734 (*mb_char2bytes)(c, buf);
5735 buf[cc] = NUL;
5736 ins_char_bytes(buf, cc);
5737 AppendCharToRedobuff(c);
5738 }
5739 else
5740#endif
5741 {
5742 ins_char(c);
5743 if (flags & INSCHAR_CTRLV)
5744 redo_literal(c);
5745 else
5746 AppendCharToRedobuff(c);
5747 }
5748 }
5749}
5750
5751/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005752 * Format text at the current insert position.
5753 */
5754 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00005755internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005756 int textwidth;
5757 int second_indent;
5758 int flags;
5759 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005760 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005761{
5762 int cc;
5763 int save_char = NUL;
5764 int haveto_redraw = FALSE;
5765 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5766#ifdef FEAT_MBYTE
5767 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5768#endif
5769 int fo_white_par = has_format_option(FO_WHITE_PAR);
5770 int first_line = TRUE;
5771#ifdef FEAT_COMMENTS
5772 colnr_T leader_len;
5773 int no_leader = FALSE;
5774 int do_comments = (flags & INSCHAR_DO_COM);
5775#endif
5776
5777 /*
5778 * When 'ai' is off we don't want a space under the cursor to be
5779 * deleted. Replace it with an 'x' temporarily.
5780 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005781 if (!curbuf->b_p_ai
5782#ifdef FEAT_VREPLACE
5783 && !(State & VREPLACE_FLAG)
5784#endif
5785 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005786 {
5787 cc = gchar_cursor();
5788 if (vim_iswhite(cc))
5789 {
5790 save_char = cc;
5791 pchar_cursor('x');
5792 }
5793 }
5794
5795 /*
5796 * Repeat breaking lines, until the current line is not too long.
5797 */
5798 while (!got_int)
5799 {
5800 int startcol; /* Cursor column at entry */
5801 int wantcol; /* column at textwidth border */
5802 int foundcol; /* column for start of spaces */
5803 int end_foundcol = 0; /* column for start of word */
5804 colnr_T len;
5805 colnr_T virtcol;
5806#ifdef FEAT_VREPLACE
5807 int orig_col = 0;
5808 char_u *saved_text = NULL;
5809#endif
5810 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005811 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005812
Bram Moolenaar97b98102009-11-17 16:41:01 +00005813 virtcol = get_nolist_virtcol()
5814 + char2cells(c != NUL ? c : gchar_cursor());
5815 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005816 break;
5817
5818#ifdef FEAT_COMMENTS
5819 if (no_leader)
5820 do_comments = FALSE;
5821 else if (!(flags & INSCHAR_FORMAT)
5822 && has_format_option(FO_WRAP_COMS))
5823 do_comments = TRUE;
5824
5825 /* Don't break until after the comment leader */
5826 if (do_comments)
5827 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5828 else
5829 leader_len = 0;
5830
5831 /* If the line doesn't start with a comment leader, then don't
5832 * start one in a following broken line. Avoids that a %word
5833 * moved to the start of the next line causes all following lines
5834 * to start with %. */
5835 if (leader_len == 0)
5836 no_leader = TRUE;
5837#endif
5838 if (!(flags & INSCHAR_FORMAT)
5839#ifdef FEAT_COMMENTS
5840 && leader_len == 0
5841#endif
5842 && !has_format_option(FO_WRAP))
5843
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005844 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005845 if ((startcol = curwin->w_cursor.col) == 0)
5846 break;
5847
5848 /* find column of textwidth border */
5849 coladvance((colnr_T)textwidth);
5850 wantcol = curwin->w_cursor.col;
5851
Bram Moolenaar97b98102009-11-17 16:41:01 +00005852 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005853 foundcol = 0;
5854
5855 /*
5856 * Find position to break at.
5857 * Stop at first entered white when 'formatoptions' has 'v'
5858 */
5859 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5860 || curwin->w_cursor.lnum != Insstart.lnum
5861 || curwin->w_cursor.col >= Insstart.col)
5862 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00005863 if (curwin->w_cursor.col == startcol && c != NUL)
5864 cc = c;
5865 else
5866 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005867 if (WHITECHAR(cc))
5868 {
5869 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005870 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005871
5872 /* find start of sequence of blanks */
5873 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5874 {
5875 dec_cursor();
5876 cc = gchar_cursor();
5877 }
5878 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5879 break; /* only spaces in front of text */
5880#ifdef FEAT_COMMENTS
5881 /* Don't break until after the comment leader */
5882 if (curwin->w_cursor.col < leader_len)
5883 break;
5884#endif
5885 if (has_format_option(FO_ONE_LETTER))
5886 {
5887 /* do not break after one-letter words */
5888 if (curwin->w_cursor.col == 0)
5889 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005890#ifdef FEAT_COMMENTS
5891 /* do not break "#a b" when 'tw' is 2 */
5892 if (curwin->w_cursor.col <= leader_len)
5893 break;
5894#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005895 col = curwin->w_cursor.col;
5896 dec_cursor();
5897 cc = gchar_cursor();
5898
5899 if (WHITECHAR(cc))
5900 continue; /* one-letter, continue */
5901 curwin->w_cursor.col = col;
5902 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00005903
5904 inc_cursor();
5905
5906 end_foundcol = end_col + 1;
5907 foundcol = curwin->w_cursor.col;
5908 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005909 break;
5910 }
5911#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00005912 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005913 {
5914 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005915 if (curwin->w_cursor.col != startcol)
5916 {
5917#ifdef FEAT_COMMENTS
5918 /* Don't break until after the comment leader */
5919 if (curwin->w_cursor.col < leader_len)
5920 break;
5921#endif
5922 col = curwin->w_cursor.col;
5923 inc_cursor();
5924 /* Don't change end_foundcol if already set. */
5925 if (foundcol != curwin->w_cursor.col)
5926 {
5927 foundcol = curwin->w_cursor.col;
5928 end_foundcol = foundcol;
5929 if (curwin->w_cursor.col <= (colnr_T)wantcol)
5930 break;
5931 }
5932 curwin->w_cursor.col = col;
5933 }
5934
5935 if (curwin->w_cursor.col == 0)
5936 break;
5937
5938 col = curwin->w_cursor.col;
5939
5940 dec_cursor();
5941 cc = gchar_cursor();
5942
5943 if (WHITECHAR(cc))
5944 continue; /* break with space */
5945#ifdef FEAT_COMMENTS
5946 /* Don't break until after the comment leader */
5947 if (curwin->w_cursor.col < leader_len)
5948 break;
5949#endif
5950
5951 curwin->w_cursor.col = col;
5952
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005953 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005954 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005955 if (curwin->w_cursor.col <= (colnr_T)wantcol)
5956 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005957 }
5958#endif
5959 if (curwin->w_cursor.col == 0)
5960 break;
5961 dec_cursor();
5962 }
5963
5964 if (foundcol == 0) /* no spaces, cannot break line */
5965 {
5966 curwin->w_cursor.col = startcol;
5967 break;
5968 }
5969
5970 /* Going to break the line, remove any "$" now. */
5971 undisplay_dollar();
5972
5973 /*
5974 * Offset between cursor position and line break is used by replace
5975 * stack functions. VREPLACE does not use this, and backspaces
5976 * over the text instead.
5977 */
5978#ifdef FEAT_VREPLACE
5979 if (State & VREPLACE_FLAG)
5980 orig_col = startcol; /* Will start backspacing from here */
5981 else
5982#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005983 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005984
5985 /*
5986 * adjust startcol for spaces that will be deleted and
5987 * characters that will remain on top line
5988 */
5989 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005990 while ((cc = gchar_cursor(), WHITECHAR(cc))
5991 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005992 inc_cursor();
5993 startcol -= curwin->w_cursor.col;
5994 if (startcol < 0)
5995 startcol = 0;
5996
5997#ifdef FEAT_VREPLACE
5998 if (State & VREPLACE_FLAG)
5999 {
6000 /*
6001 * In VREPLACE mode, we will backspace over the text to be
6002 * wrapped, so save a copy now to put on the next line.
6003 */
6004 saved_text = vim_strsave(ml_get_cursor());
6005 curwin->w_cursor.col = orig_col;
6006 if (saved_text == NULL)
6007 break; /* Can't do it, out of memory */
6008 saved_text[startcol] = NUL;
6009
6010 /* Backspace over characters that will move to the next line */
6011 if (!fo_white_par)
6012 backspace_until_column(foundcol);
6013 }
6014 else
6015#endif
6016 {
6017 /* put cursor after pos. to break line */
6018 if (!fo_white_par)
6019 curwin->w_cursor.col = foundcol;
6020 }
6021
6022 /*
6023 * Split the line just before the margin.
6024 * Only insert/delete lines, but don't really redraw the window.
6025 */
6026 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6027 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6028#ifdef FEAT_COMMENTS
6029 + (do_comments ? OPENLINE_DO_COM : 0)
6030#endif
6031 , old_indent);
6032 old_indent = 0;
6033
6034 replace_offset = 0;
6035 if (first_line)
6036 {
6037 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6038 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6039 if (second_indent >= 0)
6040 {
6041#ifdef FEAT_VREPLACE
6042 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006043 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006044 else
6045#endif
6046 (void)set_indent(second_indent, SIN_CHANGED);
6047 }
6048 first_line = FALSE;
6049 }
6050
6051#ifdef FEAT_VREPLACE
6052 if (State & VREPLACE_FLAG)
6053 {
6054 /*
6055 * In VREPLACE mode we have backspaced over the text to be
6056 * moved, now we re-insert it into the new line.
6057 */
6058 ins_bytes(saved_text);
6059 vim_free(saved_text);
6060 }
6061 else
6062#endif
6063 {
6064 /*
6065 * Check if cursor is not past the NUL off the line, cindent
6066 * may have added or removed indent.
6067 */
6068 curwin->w_cursor.col += startcol;
6069 len = (colnr_T)STRLEN(ml_get_curline());
6070 if (curwin->w_cursor.col > len)
6071 curwin->w_cursor.col = len;
6072 }
6073
6074 haveto_redraw = TRUE;
6075#ifdef FEAT_CINDENT
6076 can_cindent = TRUE;
6077#endif
6078 /* moved the cursor, don't autoindent or cindent now */
6079 did_ai = FALSE;
6080#ifdef FEAT_SMARTINDENT
6081 did_si = FALSE;
6082 can_si = FALSE;
6083 can_si_back = FALSE;
6084#endif
6085 line_breakcheck();
6086 }
6087
6088 if (save_char != NUL) /* put back space after cursor */
6089 pchar_cursor(save_char);
6090
6091 if (!format_only && haveto_redraw)
6092 {
6093 update_topline();
6094 redraw_curbuf_later(VALID);
6095 }
6096}
6097
6098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006099 * Called after inserting or deleting text: When 'formatoptions' includes the
6100 * 'a' flag format from the current line until the end of the paragraph.
6101 * Keep the cursor at the same position relative to the text.
6102 * The caller must have saved the cursor line for undo, following ones will be
6103 * saved here.
6104 */
6105 void
6106auto_format(trailblank, prev_line)
6107 int trailblank; /* when TRUE also format with trailing blank */
6108 int prev_line; /* may start in previous line */
6109{
6110 pos_T pos;
6111 colnr_T len;
6112 char_u *old;
6113 char_u *new, *pnew;
6114 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006115 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006116
6117 if (!has_format_option(FO_AUTO))
6118 return;
6119
6120 pos = curwin->w_cursor;
6121 old = ml_get_curline();
6122
6123 /* may remove added space */
6124 check_auto_format(FALSE);
6125
6126 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6127 * user might insert normal text next. Also skip formatting when "1" is
6128 * in 'formatoptions' and there is a single character before the cursor.
6129 * Otherwise the line would be broken and when typing another non-white
6130 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006131 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006132 if (*old != NUL && !trailblank && wasatend)
6133 {
6134 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006135 cc = gchar_cursor();
6136 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6137 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006138 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006139 cc = gchar_cursor();
6140 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 {
6142 curwin->w_cursor = pos;
6143 return;
6144 }
6145 curwin->w_cursor = pos;
6146 }
6147
6148#ifdef FEAT_COMMENTS
6149 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6150 * comments. */
6151 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6152 && get_leader_len(old, NULL, FALSE) == 0)
6153 return;
6154#endif
6155
6156 /*
6157 * May start formatting in a previous line, so that after "x" a word is
6158 * moved to the previous line if it fits there now. Only when this is not
6159 * the start of a paragraph.
6160 */
6161 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6162 {
6163 --curwin->w_cursor.lnum;
6164 if (u_save_cursor() == FAIL)
6165 return;
6166 }
6167
6168 /*
6169 * Do the formatting and restore the cursor position. "saved_cursor" will
6170 * be adjusted for the text formatting.
6171 */
6172 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006173 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 curwin->w_cursor = saved_cursor;
6175 saved_cursor.lnum = 0;
6176
6177 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6178 {
6179 /* "cannot happen" */
6180 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6181 coladvance((colnr_T)MAXCOL);
6182 }
6183 else
6184 check_cursor_col();
6185
6186 /* Insert mode: If the cursor is now after the end of the line while it
6187 * previously wasn't, the line was broken. Because of the rule above we
6188 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6189 * formatted. */
6190 if (!wasatend && has_format_option(FO_WHITE_PAR))
6191 {
6192 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006193 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006194 if (curwin->w_cursor.col == len)
6195 {
6196 pnew = vim_strnsave(new, len + 2);
6197 pnew[len] = ' ';
6198 pnew[len + 1] = NUL;
6199 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6200 /* remove the space later */
6201 did_add_space = TRUE;
6202 }
6203 else
6204 /* may remove added space */
6205 check_auto_format(FALSE);
6206 }
6207
6208 check_cursor();
6209}
6210
6211/*
6212 * When an extra space was added to continue a paragraph for auto-formatting,
6213 * delete it now. The space must be under the cursor, just after the insert
6214 * position.
6215 */
6216 static void
6217check_auto_format(end_insert)
6218 int end_insert; /* TRUE when ending Insert mode */
6219{
6220 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006221 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222
6223 if (did_add_space)
6224 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006225 cc = gchar_cursor();
6226 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006227 /* Somehow the space was removed already. */
6228 did_add_space = FALSE;
6229 else
6230 {
6231 if (!end_insert)
6232 {
6233 inc_cursor();
6234 c = gchar_cursor();
6235 dec_cursor();
6236 }
6237 if (c != NUL)
6238 {
6239 /* The space is no longer at the end of the line, delete it. */
6240 del_char(FALSE);
6241 did_add_space = FALSE;
6242 }
6243 }
6244 }
6245}
6246
6247/*
6248 * Find out textwidth to be used for formatting:
6249 * if 'textwidth' option is set, use it
6250 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6251 * if invalid value, use 0.
6252 * Set default to window width (maximum 79) for "gq" operator.
6253 */
6254 int
6255comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006256 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006257{
6258 int textwidth;
6259
6260 textwidth = curbuf->b_p_tw;
6261 if (textwidth == 0 && curbuf->b_p_wm)
6262 {
6263 /* The width is the window width minus 'wrapmargin' minus all the
6264 * things that add to the margin. */
6265 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6266#ifdef FEAT_CMDWIN
6267 if (cmdwin_type != 0)
6268 textwidth -= 1;
6269#endif
6270#ifdef FEAT_FOLDING
6271 textwidth -= curwin->w_p_fdc;
6272#endif
6273#ifdef FEAT_SIGNS
6274 if (curwin->w_buffer->b_signlist != NULL
6275# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006276 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006277# endif
6278 )
6279 textwidth -= 1;
6280#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006281 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006282 textwidth -= 8;
6283 }
6284 if (textwidth < 0)
6285 textwidth = 0;
6286 if (ff && textwidth == 0)
6287 {
6288 textwidth = W_WIDTH(curwin) - 1;
6289 if (textwidth > 79)
6290 textwidth = 79;
6291 }
6292 return textwidth;
6293}
6294
6295/*
6296 * Put a character in the redo buffer, for when just after a CTRL-V.
6297 */
6298 static void
6299redo_literal(c)
6300 int c;
6301{
6302 char_u buf[10];
6303
6304 /* Only digits need special treatment. Translate them into a string of
6305 * three digits. */
6306 if (VIM_ISDIGIT(c))
6307 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006308 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006309 AppendToRedobuff(buf);
6310 }
6311 else
6312 AppendCharToRedobuff(c);
6313}
6314
6315/*
6316 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006317 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318 */
6319 static void
6320start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006321 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006322{
6323 if (!arrow_used) /* something has been inserted */
6324 {
6325 AppendToRedobuff(ESC_STR);
6326 stop_insert(end_insert_pos, FALSE);
6327 arrow_used = TRUE; /* this means we stopped the current insert */
6328 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006329#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006330 check_spell_redraw();
6331#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006332}
6333
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006334#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006335/*
6336 * If we skipped highlighting word at cursor, do it now.
6337 * It may be skipped again, thus reset spell_redraw_lnum first.
6338 */
6339 static void
6340check_spell_redraw()
6341{
6342 if (spell_redraw_lnum != 0)
6343 {
6344 linenr_T lnum = spell_redraw_lnum;
6345
6346 spell_redraw_lnum = 0;
6347 redrawWinline(lnum, FALSE);
6348 }
6349}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006350
6351/*
6352 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6353 * spelled word, if there is one.
6354 */
6355 static void
6356spell_back_to_badword()
6357{
6358 pos_T tpos = curwin->w_cursor;
6359
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006360 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006361 if (curwin->w_cursor.col != tpos.col)
6362 start_arrow(&tpos);
6363}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006364#endif
6365
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366/*
6367 * stop_arrow() is called before a change is made in insert mode.
6368 * If an arrow key has been used, start a new insertion.
6369 * Returns FAIL if undo is impossible, shouldn't insert then.
6370 */
6371 int
6372stop_arrow()
6373{
6374 if (arrow_used)
6375 {
6376 if (u_save_cursor() == OK)
6377 {
6378 arrow_used = FALSE;
6379 ins_need_undo = FALSE;
6380 }
6381 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006382 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 ai_col = 0;
6384#ifdef FEAT_VREPLACE
6385 if (State & VREPLACE_FLAG)
6386 {
6387 orig_line_count = curbuf->b_ml.ml_line_count;
6388 vr_lines_changed = 1;
6389 }
6390#endif
6391 ResetRedobuff();
6392 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006393 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006394 }
6395 else if (ins_need_undo)
6396 {
6397 if (u_save_cursor() == OK)
6398 ins_need_undo = FALSE;
6399 }
6400
6401#ifdef FEAT_FOLDING
6402 /* Always open fold at the cursor line when inserting something. */
6403 foldOpenCursor();
6404#endif
6405
6406 return (arrow_used || ins_need_undo ? FAIL : OK);
6407}
6408
6409/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006410 * Do a few things to stop inserting.
6411 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6412 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006413 */
6414 static void
6415stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006416 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006417 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006419 int cc;
6420 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006421
6422 stop_redo_ins();
6423 replace_flush(); /* abandon replace stack */
6424
6425 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006426 * Save the inserted text for later redo with ^@ and CTRL-A.
6427 * Don't do it when "restart_edit" was set and nothing was inserted,
6428 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006430 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006431 if (did_restart_edit == 0 || (ptr != NULL
6432 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006433 {
6434 vim_free(last_insert);
6435 last_insert = ptr;
6436 last_insert_skip = new_insert_skip;
6437 }
6438 else
6439 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006441 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006442 {
6443 /* Auto-format now. It may seem strange to do this when stopping an
6444 * insertion (or moving the cursor), but it's required when appending
6445 * a line and having it end in a space. But only do it when something
6446 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006447 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006448 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006449 pos_T tpos = curwin->w_cursor;
6450
Bram Moolenaar071d4272004-06-13 20:20:40 +00006451 /* When the cursor is at the end of the line after a space the
6452 * formatting will move it to the following word. Avoid that by
6453 * moving the cursor onto the space. */
6454 cc = 'x';
6455 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6456 {
6457 dec_cursor();
6458 cc = gchar_cursor();
6459 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006460 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006461 }
6462
6463 auto_format(TRUE, FALSE);
6464
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006465 if (vim_iswhite(cc))
6466 {
6467 if (gchar_cursor() != NUL)
6468 inc_cursor();
6469#ifdef FEAT_VIRTUALEDIT
6470 /* If the cursor is still at the same character, also keep
6471 * the "coladd". */
6472 if (gchar_cursor() == NUL
6473 && curwin->w_cursor.lnum == tpos.lnum
6474 && curwin->w_cursor.col == tpos.col)
6475 curwin->w_cursor.coladd = tpos.coladd;
6476#endif
6477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479
6480 /* If a space was inserted for auto-formatting, remove it now. */
6481 check_auto_format(TRUE);
6482
6483 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006484 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006485 * Do this when ESC was used or moving the cursor up/down.
6486 * Check for the old position still being valid, just in case the text
6487 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006488 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006489 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6490 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006491 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006492 pos_T tpos = curwin->w_cursor;
6493
6494 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006495 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006496 for (;;)
6497 {
6498 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6499 --curwin->w_cursor.col;
6500 cc = gchar_cursor();
6501 if (!vim_iswhite(cc))
6502 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006503 if (del_char(TRUE) == FAIL)
6504 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006505 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006506 if (curwin->w_cursor.lnum != tpos.lnum)
6507 curwin->w_cursor = tpos;
6508 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6510
6511#ifdef FEAT_VISUAL
6512 /* <C-S-Right> may have started Visual mode, adjust the position for
6513 * deleted characters. */
6514 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6515 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006516 int len = (int)STRLEN(ml_get_curline());
6517
6518 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006519 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006520 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521# ifdef FEAT_VIRTUALEDIT
6522 VIsual.coladd = 0;
6523# endif
6524 }
6525 }
6526#endif
6527 }
6528 }
6529 did_ai = FALSE;
6530#ifdef FEAT_SMARTINDENT
6531 did_si = FALSE;
6532 can_si = FALSE;
6533 can_si_back = FALSE;
6534#endif
6535
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006536 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6537 * now in a different buffer. */
6538 if (end_insert_pos != NULL)
6539 {
6540 curbuf->b_op_start = Insstart;
6541 curbuf->b_op_end = *end_insert_pos;
6542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006543}
6544
6545/*
6546 * Set the last inserted text to a single character.
6547 * Used for the replace command.
6548 */
6549 void
6550set_last_insert(c)
6551 int c;
6552{
6553 char_u *s;
6554
6555 vim_free(last_insert);
6556#ifdef FEAT_MBYTE
6557 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6558#else
6559 last_insert = alloc(6);
6560#endif
6561 if (last_insert != NULL)
6562 {
6563 s = last_insert;
6564 /* Use the CTRL-V only when entering a special char */
6565 if (c < ' ' || c == DEL)
6566 *s++ = Ctrl_V;
6567 s = add_char2buf(c, s);
6568 *s++ = ESC;
6569 *s++ = NUL;
6570 last_insert_skip = 0;
6571 }
6572}
6573
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006574#if defined(EXITFREE) || defined(PROTO)
6575 void
6576free_last_insert()
6577{
6578 vim_free(last_insert);
6579 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006580# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006581 vim_free(compl_orig_text);
6582 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006583# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006584}
6585#endif
6586
Bram Moolenaar071d4272004-06-13 20:20:40 +00006587/*
6588 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6589 * and CSI. Handle multi-byte characters.
6590 * Returns a pointer to after the added bytes.
6591 */
6592 char_u *
6593add_char2buf(c, s)
6594 int c;
6595 char_u *s;
6596{
6597#ifdef FEAT_MBYTE
6598 char_u temp[MB_MAXBYTES];
6599 int i;
6600 int len;
6601
6602 len = (*mb_char2bytes)(c, temp);
6603 for (i = 0; i < len; ++i)
6604 {
6605 c = temp[i];
6606#endif
6607 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6608 if (c == K_SPECIAL)
6609 {
6610 *s++ = K_SPECIAL;
6611 *s++ = KS_SPECIAL;
6612 *s++ = KE_FILLER;
6613 }
6614#ifdef FEAT_GUI
6615 else if (c == CSI)
6616 {
6617 *s++ = CSI;
6618 *s++ = KS_EXTRA;
6619 *s++ = (int)KE_CSI;
6620 }
6621#endif
6622 else
6623 *s++ = c;
6624#ifdef FEAT_MBYTE
6625 }
6626#endif
6627 return s;
6628}
6629
6630/*
6631 * move cursor to start of line
6632 * if flags & BL_WHITE move to first non-white
6633 * if flags & BL_SOL move to first non-white if startofline is set,
6634 * otherwise keep "curswant" column
6635 * if flags & BL_FIX don't leave the cursor on a NUL.
6636 */
6637 void
6638beginline(flags)
6639 int flags;
6640{
6641 if ((flags & BL_SOL) && !p_sol)
6642 coladvance(curwin->w_curswant);
6643 else
6644 {
6645 curwin->w_cursor.col = 0;
6646#ifdef FEAT_VIRTUALEDIT
6647 curwin->w_cursor.coladd = 0;
6648#endif
6649
6650 if (flags & (BL_WHITE | BL_SOL))
6651 {
6652 char_u *ptr;
6653
6654 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6655 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6656 ++curwin->w_cursor.col;
6657 }
6658 curwin->w_set_curswant = TRUE;
6659 }
6660}
6661
6662/*
6663 * oneright oneleft cursor_down cursor_up
6664 *
6665 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006666 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006667 * Return OK when successful, FAIL when we hit a line of file boundary.
6668 */
6669
6670 int
6671oneright()
6672{
6673 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006674 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006675
6676#ifdef FEAT_VIRTUALEDIT
6677 if (virtual_active())
6678 {
6679 pos_T prevpos = curwin->w_cursor;
6680
6681 /* Adjust for multi-wide char (excluding TAB) */
6682 ptr = ml_get_cursor();
6683 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006684# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006685 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006686# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006687 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006689 ))
6690 ? ptr2cells(ptr) : 1));
6691 curwin->w_set_curswant = TRUE;
6692 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6693 return (prevpos.col != curwin->w_cursor.col
6694 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6695 }
6696#endif
6697
6698 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006699 if (*ptr == NUL)
6700 return FAIL; /* already at the very end */
6701
Bram Moolenaar071d4272004-06-13 20:20:40 +00006702#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006703 if (has_mbyte)
6704 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006705 else
6706#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006707 l = 1;
6708
6709 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6710 * contains "onemore". */
6711 if (ptr[l] == NUL
6712#ifdef FEAT_VIRTUALEDIT
6713 && (ve_flags & VE_ONEMORE) == 0
6714#endif
6715 )
6716 return FAIL;
6717 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006718
6719 curwin->w_set_curswant = TRUE;
6720 return OK;
6721}
6722
6723 int
6724oneleft()
6725{
6726#ifdef FEAT_VIRTUALEDIT
6727 if (virtual_active())
6728 {
6729 int width;
6730 int v = getviscol();
6731
6732 if (v == 0)
6733 return FAIL;
6734
6735# ifdef FEAT_LINEBREAK
6736 /* We might get stuck on 'showbreak', skip over it. */
6737 width = 1;
6738 for (;;)
6739 {
6740 coladvance(v - width);
6741 /* getviscol() is slow, skip it when 'showbreak' is empty and
6742 * there are no multi-byte characters */
6743 if ((*p_sbr == NUL
6744# ifdef FEAT_MBYTE
6745 && !has_mbyte
6746# endif
6747 ) || getviscol() < v)
6748 break;
6749 ++width;
6750 }
6751# else
6752 coladvance(v - 1);
6753# endif
6754
6755 if (curwin->w_cursor.coladd == 1)
6756 {
6757 char_u *ptr;
6758
6759 /* Adjust for multi-wide char (not a TAB) */
6760 ptr = ml_get_cursor();
6761 if (*ptr != TAB && vim_isprintc(
6762# ifdef FEAT_MBYTE
6763 (*mb_ptr2char)(ptr)
6764# else
6765 *ptr
6766# endif
6767 ) && ptr2cells(ptr) > 1)
6768 curwin->w_cursor.coladd = 0;
6769 }
6770
6771 curwin->w_set_curswant = TRUE;
6772 return OK;
6773 }
6774#endif
6775
6776 if (curwin->w_cursor.col == 0)
6777 return FAIL;
6778
6779 curwin->w_set_curswant = TRUE;
6780 --curwin->w_cursor.col;
6781
6782#ifdef FEAT_MBYTE
6783 /* if the character on the left of the current cursor is a multi-byte
6784 * character, move to its first byte */
6785 if (has_mbyte)
6786 mb_adjust_cursor();
6787#endif
6788 return OK;
6789}
6790
6791 int
6792cursor_up(n, upd_topline)
6793 long n;
6794 int upd_topline; /* When TRUE: update topline */
6795{
6796 linenr_T lnum;
6797
6798 if (n > 0)
6799 {
6800 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006801 /* This fails if the cursor is already in the first line or the count
6802 * is larger than the line number and '-' is in 'cpoptions' */
6803 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 return FAIL;
6805 if (n >= lnum)
6806 lnum = 1;
6807 else
6808#ifdef FEAT_FOLDING
6809 if (hasAnyFolding(curwin))
6810 {
6811 /*
6812 * Count each sequence of folded lines as one logical line.
6813 */
6814 /* go to the the start of the current fold */
6815 (void)hasFolding(lnum, &lnum, NULL);
6816
6817 while (n--)
6818 {
6819 /* move up one line */
6820 --lnum;
6821 if (lnum <= 1)
6822 break;
6823 /* If we entered a fold, move to the beginning, unless in
6824 * Insert mode or when 'foldopen' contains "all": it will open
6825 * in a moment. */
6826 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6827 (void)hasFolding(lnum, &lnum, NULL);
6828 }
6829 if (lnum < 1)
6830 lnum = 1;
6831 }
6832 else
6833#endif
6834 lnum -= n;
6835 curwin->w_cursor.lnum = lnum;
6836 }
6837
6838 /* try to advance to the column we want to be at */
6839 coladvance(curwin->w_curswant);
6840
6841 if (upd_topline)
6842 update_topline(); /* make sure curwin->w_topline is valid */
6843
6844 return OK;
6845}
6846
6847/*
6848 * Cursor down a number of logical lines.
6849 */
6850 int
6851cursor_down(n, upd_topline)
6852 long n;
6853 int upd_topline; /* When TRUE: update topline */
6854{
6855 linenr_T lnum;
6856
6857 if (n > 0)
6858 {
6859 lnum = curwin->w_cursor.lnum;
6860#ifdef FEAT_FOLDING
6861 /* Move to last line of fold, will fail if it's the end-of-file. */
6862 (void)hasFolding(lnum, NULL, &lnum);
6863#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006864 /* This fails if the cursor is already in the last line or would move
6865 * beyound the last line and '-' is in 'cpoptions' */
6866 if (lnum >= curbuf->b_ml.ml_line_count
6867 || (lnum + n > curbuf->b_ml.ml_line_count
6868 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006869 return FAIL;
6870 if (lnum + n >= curbuf->b_ml.ml_line_count)
6871 lnum = curbuf->b_ml.ml_line_count;
6872 else
6873#ifdef FEAT_FOLDING
6874 if (hasAnyFolding(curwin))
6875 {
6876 linenr_T last;
6877
6878 /* count each sequence of folded lines as one logical line */
6879 while (n--)
6880 {
6881 if (hasFolding(lnum, NULL, &last))
6882 lnum = last + 1;
6883 else
6884 ++lnum;
6885 if (lnum >= curbuf->b_ml.ml_line_count)
6886 break;
6887 }
6888 if (lnum > curbuf->b_ml.ml_line_count)
6889 lnum = curbuf->b_ml.ml_line_count;
6890 }
6891 else
6892#endif
6893 lnum += n;
6894 curwin->w_cursor.lnum = lnum;
6895 }
6896
6897 /* try to advance to the column we want to be at */
6898 coladvance(curwin->w_curswant);
6899
6900 if (upd_topline)
6901 update_topline(); /* make sure curwin->w_topline is valid */
6902
6903 return OK;
6904}
6905
6906/*
6907 * Stuff the last inserted text in the read buffer.
6908 * Last_insert actually is a copy of the redo buffer, so we
6909 * first have to remove the command.
6910 */
6911 int
6912stuff_inserted(c, count, no_esc)
6913 int c; /* Command character to be inserted */
6914 long count; /* Repeat this many times */
6915 int no_esc; /* Don't add an ESC at the end */
6916{
6917 char_u *esc_ptr;
6918 char_u *ptr;
6919 char_u *last_ptr;
6920 char_u last = NUL;
6921
6922 ptr = get_last_insert();
6923 if (ptr == NULL)
6924 {
6925 EMSG(_(e_noinstext));
6926 return FAIL;
6927 }
6928
6929 /* may want to stuff the command character, to start Insert mode */
6930 if (c != NUL)
6931 stuffcharReadbuff(c);
6932 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6933 *esc_ptr = NUL; /* remove the ESC */
6934
6935 /* when the last char is either "0" or "^" it will be quoted if no ESC
6936 * comes after it OR if it will inserted more than once and "ptr"
6937 * starts with ^D. -- Acevedo
6938 */
6939 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6940 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6941 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6942 {
6943 last = *last_ptr;
6944 *last_ptr = NUL;
6945 }
6946
6947 do
6948 {
6949 stuffReadbuff(ptr);
6950 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6951 if (last)
6952 stuffReadbuff((char_u *)(last == '0'
6953 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6954 : IF_EB("\026^", CTRL_V_STR "^")));
6955 }
6956 while (--count > 0);
6957
6958 if (last)
6959 *last_ptr = last;
6960
6961 if (esc_ptr != NULL)
6962 *esc_ptr = ESC; /* put the ESC back */
6963
6964 /* may want to stuff a trailing ESC, to get out of Insert mode */
6965 if (!no_esc)
6966 stuffcharReadbuff(ESC);
6967
6968 return OK;
6969}
6970
6971 char_u *
6972get_last_insert()
6973{
6974 if (last_insert == NULL)
6975 return NULL;
6976 return last_insert + last_insert_skip;
6977}
6978
6979/*
6980 * Get last inserted string, and remove trailing <Esc>.
6981 * Returns pointer to allocated memory (must be freed) or NULL.
6982 */
6983 char_u *
6984get_last_insert_save()
6985{
6986 char_u *s;
6987 int len;
6988
6989 if (last_insert == NULL)
6990 return NULL;
6991 s = vim_strsave(last_insert + last_insert_skip);
6992 if (s != NULL)
6993 {
6994 len = (int)STRLEN(s);
6995 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6996 s[len - 1] = NUL;
6997 }
6998 return s;
6999}
7000
7001/*
7002 * Check the word in front of the cursor for an abbreviation.
7003 * Called when the non-id character "c" has been entered.
7004 * When an abbreviation is recognized it is removed from the text and
7005 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7006 */
7007 static int
7008echeck_abbr(c)
7009 int c;
7010{
7011 /* Don't check for abbreviation in paste mode, when disabled and just
7012 * after moving around with cursor keys. */
7013 if (p_paste || no_abbr || arrow_used)
7014 return FALSE;
7015
7016 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7017 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7018}
7019
7020/*
7021 * replace-stack functions
7022 *
7023 * When replacing characters, the replaced characters are remembered for each
7024 * new character. This is used to re-insert the old text when backspacing.
7025 *
7026 * There is a NUL headed list of characters for each character that is
7027 * currently in the file after the insertion point. When BS is used, one NUL
7028 * headed list is put back for the deleted character.
7029 *
7030 * For a newline, there are two NUL headed lists. One contains the characters
7031 * that the NL replaced. The extra one stores the characters after the cursor
7032 * that were deleted (always white space).
7033 *
7034 * Replace_offset is normally 0, in which case replace_push will add a new
7035 * character at the end of the stack. If replace_offset is not 0, that many
7036 * characters will be left on the stack above the newly inserted character.
7037 */
7038
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007039static char_u *replace_stack = NULL;
7040static long replace_stack_nr = 0; /* next entry in replace stack */
7041static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007042
7043 void
7044replace_push(c)
7045 int c; /* character that is replaced (NUL is none) */
7046{
7047 char_u *p;
7048
7049 if (replace_stack_nr < replace_offset) /* nothing to do */
7050 return;
7051 if (replace_stack_len <= replace_stack_nr)
7052 {
7053 replace_stack_len += 50;
7054 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7055 if (p == NULL) /* out of memory */
7056 {
7057 replace_stack_len -= 50;
7058 return;
7059 }
7060 if (replace_stack != NULL)
7061 {
7062 mch_memmove(p, replace_stack,
7063 (size_t)(replace_stack_nr * sizeof(char_u)));
7064 vim_free(replace_stack);
7065 }
7066 replace_stack = p;
7067 }
7068 p = replace_stack + replace_stack_nr - replace_offset;
7069 if (replace_offset)
7070 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7071 *p = c;
7072 ++replace_stack_nr;
7073}
7074
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007075#if defined(FEAT_MBYTE) || defined(PROTO)
7076/*
7077 * Push a character onto the replace stack. Handles a multi-byte character in
7078 * reverse byte order, so that the first byte is popped off first.
7079 * Return the number of bytes done (includes composing characters).
7080 */
7081 int
7082replace_push_mb(p)
7083 char_u *p;
7084{
7085 int l = (*mb_ptr2len)(p);
7086 int j;
7087
7088 for (j = l - 1; j >= 0; --j)
7089 replace_push(p[j]);
7090 return l;
7091}
7092#endif
7093
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007094#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007095/*
7096 * call replace_push(c) with replace_offset set to the first NUL.
7097 */
7098 static void
7099replace_push_off(c)
7100 int c;
7101{
7102 char_u *p;
7103
7104 p = replace_stack + replace_stack_nr;
7105 for (replace_offset = 1; replace_offset < replace_stack_nr;
7106 ++replace_offset)
7107 if (*--p == NUL)
7108 break;
7109 replace_push(c);
7110 replace_offset = 0;
7111}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007113
7114/*
7115 * Pop one item from the replace stack.
7116 * return -1 if stack empty
7117 * return replaced character or NUL otherwise
7118 */
7119 static int
7120replace_pop()
7121{
7122 if (replace_stack_nr == 0)
7123 return -1;
7124 return (int)replace_stack[--replace_stack_nr];
7125}
7126
7127/*
7128 * Join the top two items on the replace stack. This removes to "off"'th NUL
7129 * encountered.
7130 */
7131 static void
7132replace_join(off)
7133 int off; /* offset for which NUL to remove */
7134{
7135 int i;
7136
7137 for (i = replace_stack_nr; --i >= 0; )
7138 if (replace_stack[i] == NUL && off-- <= 0)
7139 {
7140 --replace_stack_nr;
7141 mch_memmove(replace_stack + i, replace_stack + i + 1,
7142 (size_t)(replace_stack_nr - i));
7143 return;
7144 }
7145}
7146
7147/*
7148 * Pop bytes from the replace stack until a NUL is found, and insert them
7149 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7150 */
7151 static void
7152replace_pop_ins()
7153{
7154 int cc;
7155 int oldState = State;
7156
7157 State = NORMAL; /* don't want REPLACE here */
7158 while ((cc = replace_pop()) > 0)
7159 {
7160#ifdef FEAT_MBYTE
7161 mb_replace_pop_ins(cc);
7162#else
7163 ins_char(cc);
7164#endif
7165 dec_cursor();
7166 }
7167 State = oldState;
7168}
7169
7170#ifdef FEAT_MBYTE
7171/*
7172 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7173 * indicates a multi-byte char, pop the other bytes too.
7174 */
7175 static void
7176mb_replace_pop_ins(cc)
7177 int cc;
7178{
7179 int n;
7180 char_u buf[MB_MAXBYTES];
7181 int i;
7182 int c;
7183
7184 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7185 {
7186 buf[0] = cc;
7187 for (i = 1; i < n; ++i)
7188 buf[i] = replace_pop();
7189 ins_bytes_len(buf, n);
7190 }
7191 else
7192 ins_char(cc);
7193
7194 if (enc_utf8)
7195 /* Handle composing chars. */
7196 for (;;)
7197 {
7198 c = replace_pop();
7199 if (c == -1) /* stack empty */
7200 break;
7201 if ((n = MB_BYTE2LEN(c)) == 1)
7202 {
7203 /* Not a multi-byte char, put it back. */
7204 replace_push(c);
7205 break;
7206 }
7207 else
7208 {
7209 buf[0] = c;
7210 for (i = 1; i < n; ++i)
7211 buf[i] = replace_pop();
7212 if (utf_iscomposing(utf_ptr2char(buf)))
7213 ins_bytes_len(buf, n);
7214 else
7215 {
7216 /* Not a composing char, put it back. */
7217 for (i = n - 1; i >= 0; --i)
7218 replace_push(buf[i]);
7219 break;
7220 }
7221 }
7222 }
7223}
7224#endif
7225
7226/*
7227 * make the replace stack empty
7228 * (called when exiting replace mode)
7229 */
7230 static void
7231replace_flush()
7232{
7233 vim_free(replace_stack);
7234 replace_stack = NULL;
7235 replace_stack_len = 0;
7236 replace_stack_nr = 0;
7237}
7238
7239/*
7240 * Handle doing a BS for one character.
7241 * cc < 0: replace stack empty, just move cursor
7242 * cc == 0: character was inserted, delete it
7243 * cc > 0: character was replaced, put cc (first byte of original char) back
7244 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007245 * When "limit_col" is >= 0, don't delete before this column. Matters when
7246 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007247 */
7248 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007249replace_do_bs(limit_col)
7250 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007251{
7252 int cc;
7253#ifdef FEAT_VREPLACE
7254 int orig_len = 0;
7255 int ins_len;
7256 int orig_vcols = 0;
7257 colnr_T start_vcol;
7258 char_u *p;
7259 int i;
7260 int vcol;
7261#endif
7262
7263 cc = replace_pop();
7264 if (cc > 0)
7265 {
7266#ifdef FEAT_VREPLACE
7267 if (State & VREPLACE_FLAG)
7268 {
7269 /* Get the number of screen cells used by the character we are
7270 * going to delete. */
7271 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7272 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7273 }
7274#endif
7275#ifdef FEAT_MBYTE
7276 if (has_mbyte)
7277 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007278 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007279# ifdef FEAT_VREPLACE
7280 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007281 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007282# endif
7283 replace_push(cc);
7284 }
7285 else
7286#endif
7287 {
7288 pchar_cursor(cc);
7289#ifdef FEAT_VREPLACE
7290 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007291 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007292#endif
7293 }
7294 replace_pop_ins();
7295
7296#ifdef FEAT_VREPLACE
7297 if (State & VREPLACE_FLAG)
7298 {
7299 /* Get the number of screen cells used by the inserted characters */
7300 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007301 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007302 vcol = start_vcol;
7303 for (i = 0; i < ins_len; ++i)
7304 {
7305 vcol += chartabsize(p + i, vcol);
7306#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007307 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308#endif
7309 }
7310 vcol -= start_vcol;
7311
7312 /* Delete spaces that were inserted after the cursor to keep the
7313 * text aligned. */
7314 curwin->w_cursor.col += ins_len;
7315 while (vcol > orig_vcols && gchar_cursor() == ' ')
7316 {
7317 del_char(FALSE);
7318 ++orig_vcols;
7319 }
7320 curwin->w_cursor.col -= ins_len;
7321 }
7322#endif
7323
7324 /* mark the buffer as changed and prepare for displaying */
7325 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7326 }
7327 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007328 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007329}
7330
7331#ifdef FEAT_CINDENT
7332/*
7333 * Return TRUE if C-indenting is on.
7334 */
7335 static int
7336cindent_on()
7337{
7338 return (!p_paste && (curbuf->b_p_cin
7339# ifdef FEAT_EVAL
7340 || *curbuf->b_p_inde != NUL
7341# endif
7342 ));
7343}
7344#endif
7345
7346#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7347/*
7348 * Re-indent the current line, based on the current contents of it and the
7349 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7350 * confused what all the part that handles Control-T is doing that I'm not.
7351 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7352 */
7353
7354 void
7355fixthisline(get_the_indent)
7356 int (*get_the_indent) __ARGS((void));
7357{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007358 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007359 if (linewhite(curwin->w_cursor.lnum))
7360 did_ai = TRUE; /* delete the indent if the line stays empty */
7361}
7362
7363 void
7364fix_indent()
7365{
7366 if (p_paste)
7367 return;
7368# ifdef FEAT_LISP
7369 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7370 fixthisline(get_lisp_indent);
7371# endif
7372# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7373 else
7374# endif
7375# ifdef FEAT_CINDENT
7376 if (cindent_on())
7377 do_c_expr_indent();
7378# endif
7379}
7380
7381#endif
7382
7383#ifdef FEAT_CINDENT
7384/*
7385 * return TRUE if 'cinkeys' contains the key "keytyped",
7386 * when == '*': Only if key is preceded with '*' (indent before insert)
7387 * when == '!': Only if key is prededed with '!' (don't insert)
7388 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7389 *
7390 * "keytyped" can have a few special values:
7391 * KEY_OPEN_FORW
7392 * KEY_OPEN_BACK
7393 * KEY_COMPLETE just finished completion.
7394 *
7395 * If line_is_empty is TRUE accept keys with '0' before them.
7396 */
7397 int
7398in_cinkeys(keytyped, when, line_is_empty)
7399 int keytyped;
7400 int when;
7401 int line_is_empty;
7402{
7403 char_u *look;
7404 int try_match;
7405 int try_match_word;
7406 char_u *p;
7407 char_u *line;
7408 int icase;
7409 int i;
7410
Bram Moolenaar30848942009-12-24 14:46:12 +00007411 if (keytyped == NUL)
7412 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7413 return FALSE;
7414
Bram Moolenaar071d4272004-06-13 20:20:40 +00007415#ifdef FEAT_EVAL
7416 if (*curbuf->b_p_inde != NUL)
7417 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7418 else
7419#endif
7420 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7421 while (*look)
7422 {
7423 /*
7424 * Find out if we want to try a match with this key, depending on
7425 * 'when' and a '*' or '!' before the key.
7426 */
7427 switch (when)
7428 {
7429 case '*': try_match = (*look == '*'); break;
7430 case '!': try_match = (*look == '!'); break;
7431 default: try_match = (*look != '*'); break;
7432 }
7433 if (*look == '*' || *look == '!')
7434 ++look;
7435
7436 /*
7437 * If there is a '0', only accept a match if the line is empty.
7438 * But may still match when typing last char of a word.
7439 */
7440 if (*look == '0')
7441 {
7442 try_match_word = try_match;
7443 if (!line_is_empty)
7444 try_match = FALSE;
7445 ++look;
7446 }
7447 else
7448 try_match_word = FALSE;
7449
7450 /*
7451 * does it look like a control character?
7452 */
7453 if (*look == '^'
7454#ifdef EBCDIC
7455 && (Ctrl_chr(look[1]) != 0)
7456#else
7457 && look[1] >= '?' && look[1] <= '_'
7458#endif
7459 )
7460 {
7461 if (try_match && keytyped == Ctrl_chr(look[1]))
7462 return TRUE;
7463 look += 2;
7464 }
7465 /*
7466 * 'o' means "o" command, open forward.
7467 * 'O' means "O" command, open backward.
7468 */
7469 else if (*look == 'o')
7470 {
7471 if (try_match && keytyped == KEY_OPEN_FORW)
7472 return TRUE;
7473 ++look;
7474 }
7475 else if (*look == 'O')
7476 {
7477 if (try_match && keytyped == KEY_OPEN_BACK)
7478 return TRUE;
7479 ++look;
7480 }
7481
7482 /*
7483 * 'e' means to check for "else" at start of line and just before the
7484 * cursor.
7485 */
7486 else if (*look == 'e')
7487 {
7488 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7489 {
7490 p = ml_get_curline();
7491 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7492 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7493 return TRUE;
7494 }
7495 ++look;
7496 }
7497
7498 /*
7499 * ':' only causes an indent if it is at the end of a label or case
7500 * statement, or when it was before typing the ':' (to fix
7501 * class::method for C++).
7502 */
7503 else if (*look == ':')
7504 {
7505 if (try_match && keytyped == ':')
7506 {
7507 p = ml_get_curline();
7508 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7509 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007510 /* Need to get the line again after cin_islabel(). */
7511 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007512 if (curwin->w_cursor.col > 2
7513 && p[curwin->w_cursor.col - 1] == ':'
7514 && p[curwin->w_cursor.col - 2] == ':')
7515 {
7516 p[curwin->w_cursor.col - 1] = ' ';
7517 i = (cin_iscase(p) || cin_isscopedecl(p)
7518 || cin_islabel(30));
7519 p = ml_get_curline();
7520 p[curwin->w_cursor.col - 1] = ':';
7521 if (i)
7522 return TRUE;
7523 }
7524 }
7525 ++look;
7526 }
7527
7528
7529 /*
7530 * Is it a key in <>, maybe?
7531 */
7532 else if (*look == '<')
7533 {
7534 if (try_match)
7535 {
7536 /*
7537 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7538 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7539 * >, *, : and ! keys if they really really want to.
7540 */
7541 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7542 && keytyped == look[1])
7543 return TRUE;
7544
7545 if (keytyped == get_special_key_code(look + 1))
7546 return TRUE;
7547 }
7548 while (*look && *look != '>')
7549 look++;
7550 while (*look == '>')
7551 look++;
7552 }
7553
7554 /*
7555 * Is it a word: "=word"?
7556 */
7557 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7558 {
7559 ++look;
7560 if (*look == '~')
7561 {
7562 icase = TRUE;
7563 ++look;
7564 }
7565 else
7566 icase = FALSE;
7567 p = vim_strchr(look, ',');
7568 if (p == NULL)
7569 p = look + STRLEN(look);
7570 if ((try_match || try_match_word)
7571 && curwin->w_cursor.col >= (colnr_T)(p - look))
7572 {
7573 int match = FALSE;
7574
7575#ifdef FEAT_INS_EXPAND
7576 if (keytyped == KEY_COMPLETE)
7577 {
7578 char_u *s;
7579
7580 /* Just completed a word, check if it starts with "look".
7581 * search back for the start of a word. */
7582 line = ml_get_curline();
7583# ifdef FEAT_MBYTE
7584 if (has_mbyte)
7585 {
7586 char_u *n;
7587
7588 for (s = line + curwin->w_cursor.col; s > line; s = n)
7589 {
7590 n = mb_prevptr(line, s);
7591 if (!vim_iswordp(n))
7592 break;
7593 }
7594 }
7595 else
7596# endif
7597 for (s = line + curwin->w_cursor.col; s > line; --s)
7598 if (!vim_iswordc(s[-1]))
7599 break;
7600 if (s + (p - look) <= line + curwin->w_cursor.col
7601 && (icase
7602 ? MB_STRNICMP(s, look, p - look)
7603 : STRNCMP(s, look, p - look)) == 0)
7604 match = TRUE;
7605 }
7606 else
7607#endif
7608 /* TODO: multi-byte */
7609 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7610 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7611 {
7612 line = ml_get_cursor();
7613 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7614 || !vim_iswordc(line[-(p - look) - 1]))
7615 && (icase
7616 ? MB_STRNICMP(line - (p - look), look, p - look)
7617 : STRNCMP(line - (p - look), look, p - look))
7618 == 0)
7619 match = TRUE;
7620 }
7621 if (match && try_match_word && !try_match)
7622 {
7623 /* "0=word": Check if there are only blanks before the
7624 * word. */
7625 line = ml_get_curline();
7626 if ((int)(skipwhite(line) - line) !=
7627 (int)(curwin->w_cursor.col - (p - look)))
7628 match = FALSE;
7629 }
7630 if (match)
7631 return TRUE;
7632 }
7633 look = p;
7634 }
7635
7636 /*
7637 * ok, it's a boring generic character.
7638 */
7639 else
7640 {
7641 if (try_match && *look == keytyped)
7642 return TRUE;
7643 ++look;
7644 }
7645
7646 /*
7647 * Skip over ", ".
7648 */
7649 look = skip_to_option_part(look);
7650 }
7651 return FALSE;
7652}
7653#endif /* FEAT_CINDENT */
7654
7655#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7656/*
7657 * Map Hebrew keyboard when in hkmap mode.
7658 */
7659 int
7660hkmap(c)
7661 int c;
7662{
7663 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7664 {
7665 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7666 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7667 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7668 static char_u map[26] =
7669 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7670 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7671 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7672 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7673 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7674 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7675 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7676 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7677 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7678
7679 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7680 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7681 /* '-1'='sofit' */
7682 else if (c == 'x')
7683 return 'X';
7684 else if (c == 'q')
7685 return '\''; /* {geresh}={'} */
7686 else if (c == 246)
7687 return ' '; /* \"o --> ' ' for a german keyboard */
7688 else if (c == 228)
7689 return ' '; /* \"a --> ' ' -- / -- */
7690 else if (c == 252)
7691 return ' '; /* \"u --> ' ' -- / -- */
7692#ifdef EBCDIC
7693 else if (islower(c))
7694#else
7695 /* NOTE: islower() does not do the right thing for us on Linux so we
7696 * do this the same was as 5.7 and previous, so it works correctly on
7697 * all systems. Specifically, the e.g. Delete and Arrow keys are
7698 * munged and won't work if e.g. searching for Hebrew text.
7699 */
7700 else if (c >= 'a' && c <= 'z')
7701#endif
7702 return (int)(map[CharOrdLow(c)] + p_aleph);
7703 else
7704 return c;
7705 }
7706 else
7707 {
7708 switch (c)
7709 {
7710 case '`': return ';';
7711 case '/': return '.';
7712 case '\'': return ',';
7713 case 'q': return '/';
7714 case 'w': return '\'';
7715
7716 /* Hebrew letters - set offset from 'a' */
7717 case ',': c = '{'; break;
7718 case '.': c = 'v'; break;
7719 case ';': c = 't'; break;
7720 default: {
7721 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7722
7723#ifdef EBCDIC
7724 /* see note about islower() above */
7725 if (!islower(c))
7726#else
7727 if (c < 'a' || c > 'z')
7728#endif
7729 return c;
7730 c = str[CharOrdLow(c)];
7731 break;
7732 }
7733 }
7734
7735 return (int)(CharOrdLow(c) + p_aleph);
7736 }
7737}
7738#endif
7739
7740 static void
7741ins_reg()
7742{
7743 int need_redraw = FALSE;
7744 int regname;
7745 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007746#ifdef FEAT_VISUAL
7747 int vis_active = VIsual_active;
7748#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007749
7750 /*
7751 * If we are going to wait for a character, show a '"'.
7752 */
7753 pc_status = PC_STATUS_UNSET;
7754 if (redrawing() && !char_avail())
7755 {
7756 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007757 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007758
7759 edit_putchar('"', TRUE);
7760#ifdef FEAT_CMDL_INFO
7761 add_to_showcmd_c(Ctrl_R);
7762#endif
7763 }
7764
7765#ifdef USE_ON_FLY_SCROLL
7766 dont_scroll = TRUE; /* disallow scrolling here */
7767#endif
7768
7769 /*
7770 * Don't map the register name. This also prevents the mode message to be
7771 * deleted when ESC is hit.
7772 */
7773 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007774 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007775 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007776 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7777 {
7778 /* Get a third key for literal register insertion */
7779 literally = regname;
7780#ifdef FEAT_CMDL_INFO
7781 add_to_showcmd_c(literally);
7782#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007783 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007784 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007785 }
7786 --no_mapping;
7787
7788#ifdef FEAT_EVAL
7789 /*
7790 * Don't call u_sync() while getting the expression,
7791 * evaluating it or giving an error message for it!
7792 */
7793 ++no_u_sync;
7794 if (regname == '=')
7795 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007796# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007797 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007798# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007799 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007800# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801 /* Restore the Input Method. */
7802 if (im_on)
7803 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007804# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007805 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007806 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7807 {
7808 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007809 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007810 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811 else
7812 {
7813#endif
7814 if (literally == Ctrl_O || literally == Ctrl_P)
7815 {
7816 /* Append the command to the redo buffer. */
7817 AppendCharToRedobuff(Ctrl_R);
7818 AppendCharToRedobuff(literally);
7819 AppendCharToRedobuff(regname);
7820
7821 do_put(regname, BACKWARD, 1L,
7822 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7823 }
7824 else if (insert_reg(regname, literally) == FAIL)
7825 {
7826 vim_beep();
7827 need_redraw = TRUE; /* remove the '"' */
7828 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007829 else if (stop_insert_mode)
7830 /* When the '=' register was used and a function was invoked that
7831 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7832 * insert anything, need to remove the '"' */
7833 need_redraw = TRUE;
7834
Bram Moolenaar071d4272004-06-13 20:20:40 +00007835#ifdef FEAT_EVAL
7836 }
7837 --no_u_sync;
7838#endif
7839#ifdef FEAT_CMDL_INFO
7840 clear_showcmd();
7841#endif
7842
7843 /* If the inserted register is empty, we need to remove the '"' */
7844 if (need_redraw || stuff_empty())
7845 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007846
7847#ifdef FEAT_VISUAL
7848 /* Disallow starting Visual mode here, would get a weird mode. */
7849 if (!vis_active && VIsual_active)
7850 end_visual_mode();
7851#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852}
7853
7854/*
7855 * CTRL-G commands in Insert mode.
7856 */
7857 static void
7858ins_ctrl_g()
7859{
7860 int c;
7861
7862#ifdef FEAT_INS_EXPAND
7863 /* Right after CTRL-X the cursor will be after the ruler. */
7864 setcursor();
7865#endif
7866
7867 /*
7868 * Don't map the second key. This also prevents the mode message to be
7869 * deleted when ESC is hit.
7870 */
7871 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007872 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 --no_mapping;
7874 switch (c)
7875 {
7876 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7877 case K_UP:
7878 case Ctrl_K:
7879 case 'k': ins_up(TRUE);
7880 break;
7881
7882 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7883 case K_DOWN:
7884 case Ctrl_J:
7885 case 'j': ins_down(TRUE);
7886 break;
7887
7888 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007889 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007891
7892 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007893 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007894 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895 break;
7896
7897 /* Unknown CTRL-G command, reserved for future expansion. */
7898 default: vim_beep();
7899 }
7900}
7901
7902/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007903 * CTRL-^ in Insert mode.
7904 */
7905 static void
7906ins_ctrl_hat()
7907{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007908 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007909 {
7910 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7911 if (State & LANGMAP)
7912 {
7913 curbuf->b_p_iminsert = B_IMODE_NONE;
7914 State &= ~LANGMAP;
7915 }
7916 else
7917 {
7918 curbuf->b_p_iminsert = B_IMODE_LMAP;
7919 State |= LANGMAP;
7920#ifdef USE_IM_CONTROL
7921 im_set_active(FALSE);
7922#endif
7923 }
7924 }
7925#ifdef USE_IM_CONTROL
7926 else
7927 {
7928 /* There are no ":lmap" mappings, toggle IM */
7929 if (im_get_status())
7930 {
7931 curbuf->b_p_iminsert = B_IMODE_NONE;
7932 im_set_active(FALSE);
7933 }
7934 else
7935 {
7936 curbuf->b_p_iminsert = B_IMODE_IM;
7937 State &= ~LANGMAP;
7938 im_set_active(TRUE);
7939 }
7940 }
7941#endif
7942 set_iminsert_global();
7943 showmode();
7944#ifdef FEAT_GUI
7945 /* may show different cursor shape or color */
7946 if (gui.in_use)
7947 gui_update_cursor(TRUE, FALSE);
7948#endif
7949#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7950 /* Show/unshow value of 'keymap' in status lines. */
7951 status_redraw_curbuf();
7952#endif
7953}
7954
7955/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007956 * Handle ESC in insert mode.
7957 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7958 * insert.
7959 */
7960 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007961ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007962 long *count;
7963 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007964 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007965{
7966 int temp;
7967 static int disabled_redraw = FALSE;
7968
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007969#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007970 check_spell_redraw();
7971#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007972#if defined(FEAT_HANGULIN)
7973# if defined(ESC_CHG_TO_ENG_MODE)
7974 hangul_input_state_set(0);
7975# endif
7976 if (composing_hangul)
7977 {
7978 push_raw_key(composing_hangul_buffer, 2);
7979 composing_hangul = 0;
7980 }
7981#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982
7983 temp = curwin->w_cursor.col;
7984 if (disabled_redraw)
7985 {
7986 --RedrawingDisabled;
7987 disabled_redraw = FALSE;
7988 }
7989 if (!arrow_used)
7990 {
7991 /*
7992 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007993 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7994 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007995 */
7996 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007997 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007998
7999 /*
8000 * Repeating insert may take a long time. Check for
8001 * interrupt now and then.
8002 */
8003 if (*count > 0)
8004 {
8005 line_breakcheck();
8006 if (got_int)
8007 *count = 0;
8008 }
8009
8010 if (--*count > 0) /* repeat what was typed */
8011 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008012 /* Vi repeats the insert without replacing characters. */
8013 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8014 State &= ~REPLACE_FLAG;
8015
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016 (void)start_redo_ins();
8017 if (cmdchar == 'r' || cmdchar == 'v')
8018 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8019 ++RedrawingDisabled;
8020 disabled_redraw = TRUE;
8021 return FALSE; /* repeat the insert */
8022 }
8023 stop_insert(&curwin->w_cursor, TRUE);
8024 undisplay_dollar();
8025 }
8026
8027 /* When an autoindent was removed, curswant stays after the
8028 * indent */
8029 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8030 curwin->w_set_curswant = TRUE;
8031
8032 /* Remember the last Insert position in the '^ mark. */
8033 if (!cmdmod.keepjumps)
8034 curbuf->b_last_insert = curwin->w_cursor;
8035
8036 /*
8037 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008038 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008040 if (!nomove
8041 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008042#ifdef FEAT_VIRTUALEDIT
8043 || curwin->w_cursor.coladd > 0
8044#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008045 )
8046 && (restart_edit == NUL
8047 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008048#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008049 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008050#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008051 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008052#ifdef FEAT_RIGHTLEFT
8053 && !revins_on
8054#endif
8055 )
8056 {
8057#ifdef FEAT_VIRTUALEDIT
8058 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8059 {
8060 oneleft();
8061 if (restart_edit != NUL)
8062 ++curwin->w_cursor.coladd;
8063 }
8064 else
8065#endif
8066 {
8067 --curwin->w_cursor.col;
8068#ifdef FEAT_MBYTE
8069 /* Correct cursor for multi-byte character. */
8070 if (has_mbyte)
8071 mb_adjust_cursor();
8072#endif
8073 }
8074 }
8075
8076#ifdef USE_IM_CONTROL
8077 /* Disable IM to allow typing English directly for Normal mode commands.
8078 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8079 * well). */
8080 if (!(State & LANGMAP))
8081 im_save_status(&curbuf->b_p_iminsert);
8082 im_set_active(FALSE);
8083#endif
8084
8085 State = NORMAL;
8086 /* need to position cursor again (e.g. when on a TAB ) */
8087 changed_cline_bef_curs();
8088
8089#ifdef FEAT_MOUSE
8090 setmouse();
8091#endif
8092#ifdef CURSOR_SHAPE
8093 ui_cursor_shape(); /* may show different cursor shape */
8094#endif
8095
8096 /*
8097 * When recording or for CTRL-O, need to display the new mode.
8098 * Otherwise remove the mode message.
8099 */
8100 if (Recording || restart_edit != NUL)
8101 showmode();
8102 else if (p_smd)
8103 MSG("");
8104
8105 return TRUE; /* exit Insert mode */
8106}
8107
8108#ifdef FEAT_RIGHTLEFT
8109/*
8110 * Toggle language: hkmap and revins_on.
8111 * Move to end of reverse inserted text.
8112 */
8113 static void
8114ins_ctrl_()
8115{
8116 if (revins_on && revins_chars && revins_scol >= 0)
8117 {
8118 while (gchar_cursor() != NUL && revins_chars--)
8119 ++curwin->w_cursor.col;
8120 }
8121 p_ri = !p_ri;
8122 revins_on = (State == INSERT && p_ri);
8123 if (revins_on)
8124 {
8125 revins_scol = curwin->w_cursor.col;
8126 revins_legal++;
8127 revins_chars = 0;
8128 undisplay_dollar();
8129 }
8130 else
8131 revins_scol = -1;
8132#ifdef FEAT_FKMAP
8133 if (p_altkeymap)
8134 {
8135 /*
8136 * to be consistent also for redo command, using '.'
8137 * set arrow_used to true and stop it - causing to redo
8138 * characters entered in one mode (normal/reverse insert).
8139 */
8140 arrow_used = TRUE;
8141 (void)stop_arrow();
8142 p_fkmap = curwin->w_p_rl ^ p_ri;
8143 if (p_fkmap && p_ri)
8144 State = INSERT;
8145 }
8146 else
8147#endif
8148 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8149 showmode();
8150}
8151#endif
8152
8153#ifdef FEAT_VISUAL
8154/*
8155 * If 'keymodel' contains "startsel", may start selection.
8156 * Returns TRUE when a CTRL-O and other keys stuffed.
8157 */
8158 static int
8159ins_start_select(c)
8160 int c;
8161{
8162 if (km_startsel)
8163 switch (c)
8164 {
8165 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 case K_PAGEUP:
8168 case K_KPAGEUP:
8169 case K_PAGEDOWN:
8170 case K_KPAGEDOWN:
8171# ifdef MACOS
8172 case K_LEFT:
8173 case K_RIGHT:
8174 case K_UP:
8175 case K_DOWN:
8176 case K_END:
8177 case K_HOME:
8178# endif
8179 if (!(mod_mask & MOD_MASK_SHIFT))
8180 break;
8181 /* FALLTHROUGH */
8182 case K_S_LEFT:
8183 case K_S_RIGHT:
8184 case K_S_UP:
8185 case K_S_DOWN:
8186 case K_S_END:
8187 case K_S_HOME:
8188 /* Start selection right away, the cursor can move with
8189 * CTRL-O when beyond the end of the line. */
8190 start_selection();
8191
8192 /* Execute the key in (insert) Select mode. */
8193 stuffcharReadbuff(Ctrl_O);
8194 if (mod_mask)
8195 {
8196 char_u buf[4];
8197
8198 buf[0] = K_SPECIAL;
8199 buf[1] = KS_MODIFIER;
8200 buf[2] = mod_mask;
8201 buf[3] = NUL;
8202 stuffReadbuff(buf);
8203 }
8204 stuffcharReadbuff(c);
8205 return TRUE;
8206 }
8207 return FALSE;
8208}
8209#endif
8210
8211/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008212 * <Insert> key in Insert mode: toggle insert/remplace mode.
8213 */
8214 static void
8215ins_insert(replaceState)
8216 int replaceState;
8217{
8218#ifdef FEAT_FKMAP
8219 if (p_fkmap && p_ri)
8220 {
8221 beep_flush();
8222 EMSG(farsi_text_3); /* encoded in Farsi */
8223 return;
8224 }
8225#endif
8226
8227#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008228# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008229 set_vim_var_string(VV_INSERTMODE,
8230 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008231# ifdef FEAT_VREPLACE
8232 replaceState == VREPLACE ? "v" :
8233# endif
8234 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008235# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008236 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8237#endif
8238 if (State & REPLACE_FLAG)
8239 State = INSERT | (State & LANGMAP);
8240 else
8241 State = replaceState | (State & LANGMAP);
8242 AppendCharToRedobuff(K_INS);
8243 showmode();
8244#ifdef CURSOR_SHAPE
8245 ui_cursor_shape(); /* may show different cursor shape */
8246#endif
8247}
8248
8249/*
8250 * Pressed CTRL-O in Insert mode.
8251 */
8252 static void
8253ins_ctrl_o()
8254{
8255#ifdef FEAT_VREPLACE
8256 if (State & VREPLACE_FLAG)
8257 restart_edit = 'V';
8258 else
8259#endif
8260 if (State & REPLACE_FLAG)
8261 restart_edit = 'R';
8262 else
8263 restart_edit = 'I';
8264#ifdef FEAT_VIRTUALEDIT
8265 if (virtual_active())
8266 ins_at_eol = FALSE; /* cursor always keeps its column */
8267 else
8268#endif
8269 ins_at_eol = (gchar_cursor() == NUL);
8270}
8271
8272/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 * If the cursor is on an indent, ^T/^D insert/delete one
8274 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008275 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008276 * with vi. But vi only supports ^T and ^D after an
8277 * autoindent, we support it everywhere.
8278 */
8279 static void
8280ins_shift(c, lastc)
8281 int c;
8282 int lastc;
8283{
8284 if (stop_arrow() == FAIL)
8285 return;
8286 AppendCharToRedobuff(c);
8287
8288 /*
8289 * 0^D and ^^D: remove all indent.
8290 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008291 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8292 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008293 {
8294 --curwin->w_cursor.col;
8295 (void)del_char(FALSE); /* delete the '^' or '0' */
8296 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8297 if (State & REPLACE_FLAG)
8298 replace_pop_ins();
8299 if (lastc == '^')
8300 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008301 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 }
8303 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008304 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305
8306 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8307 did_ai = FALSE;
8308#ifdef FEAT_SMARTINDENT
8309 did_si = FALSE;
8310 can_si = FALSE;
8311 can_si_back = FALSE;
8312#endif
8313#ifdef FEAT_CINDENT
8314 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8315#endif
8316}
8317
8318 static void
8319ins_del()
8320{
8321 int temp;
8322
8323 if (stop_arrow() == FAIL)
8324 return;
8325 if (gchar_cursor() == NUL) /* delete newline */
8326 {
8327 temp = curwin->w_cursor.col;
8328 if (!can_bs(BS_EOL) /* only if "eol" included */
8329 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8330 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8331 || do_join(FALSE) == FAIL)
8332 vim_beep();
8333 else
8334 curwin->w_cursor.col = temp;
8335 }
8336 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8337 vim_beep();
8338 did_ai = FALSE;
8339#ifdef FEAT_SMARTINDENT
8340 did_si = FALSE;
8341 can_si = FALSE;
8342 can_si_back = FALSE;
8343#endif
8344 AppendCharToRedobuff(K_DEL);
8345}
8346
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008347static void ins_bs_one __ARGS((colnr_T *vcolp));
8348
8349/*
8350 * Delete one character for ins_bs().
8351 */
8352 static void
8353ins_bs_one(vcolp)
8354 colnr_T *vcolp;
8355{
8356 dec_cursor();
8357 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8358 if (State & REPLACE_FLAG)
8359 {
8360 /* Don't delete characters before the insert point when in
8361 * Replace mode */
8362 if (curwin->w_cursor.lnum != Insstart.lnum
8363 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008364 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008365 }
8366 else
8367 (void)del_char(FALSE);
8368}
8369
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370/*
8371 * Handle Backspace, delete-word and delete-line in Insert mode.
8372 * Return TRUE when backspace was actually used.
8373 */
8374 static int
8375ins_bs(c, mode, inserted_space_p)
8376 int c;
8377 int mode;
8378 int *inserted_space_p;
8379{
8380 linenr_T lnum;
8381 int cc;
8382 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008383 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008384 colnr_T mincol;
8385 int did_backspace = FALSE;
8386 int in_indent;
8387 int oldState;
8388#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008389 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390#endif
8391
8392 /*
8393 * can't delete anything in an empty file
8394 * can't backup past first character in buffer
8395 * can't backup past starting point unless 'backspace' > 1
8396 * can backup to a previous line if 'backspace' == 0
8397 */
8398 if ( bufempty()
8399 || (
8400#ifdef FEAT_RIGHTLEFT
8401 !revins_on &&
8402#endif
8403 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8404 || (!can_bs(BS_START)
8405 && (arrow_used
8406 || (curwin->w_cursor.lnum == Insstart.lnum
8407 && curwin->w_cursor.col <= Insstart.col)))
8408 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8409 && curwin->w_cursor.col <= ai_col)
8410 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8411 {
8412 vim_beep();
8413 return FALSE;
8414 }
8415
8416 if (stop_arrow() == FAIL)
8417 return FALSE;
8418 in_indent = inindent(0);
8419#ifdef FEAT_CINDENT
8420 if (in_indent)
8421 can_cindent = FALSE;
8422#endif
8423#ifdef FEAT_COMMENTS
8424 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8425#endif
8426#ifdef FEAT_RIGHTLEFT
8427 if (revins_on) /* put cursor after last inserted char */
8428 inc_cursor();
8429#endif
8430
8431#ifdef FEAT_VIRTUALEDIT
8432 /* Virtualedit:
8433 * BACKSPACE_CHAR eats a virtual space
8434 * BACKSPACE_WORD eats all coladd
8435 * BACKSPACE_LINE eats all coladd and keeps going
8436 */
8437 if (curwin->w_cursor.coladd > 0)
8438 {
8439 if (mode == BACKSPACE_CHAR)
8440 {
8441 --curwin->w_cursor.coladd;
8442 return TRUE;
8443 }
8444 if (mode == BACKSPACE_WORD)
8445 {
8446 curwin->w_cursor.coladd = 0;
8447 return TRUE;
8448 }
8449 curwin->w_cursor.coladd = 0;
8450 }
8451#endif
8452
8453 /*
8454 * delete newline!
8455 */
8456 if (curwin->w_cursor.col == 0)
8457 {
8458 lnum = Insstart.lnum;
8459 if (curwin->w_cursor.lnum == Insstart.lnum
8460#ifdef FEAT_RIGHTLEFT
8461 || revins_on
8462#endif
8463 )
8464 {
8465 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8466 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8467 return FALSE;
8468 --Insstart.lnum;
8469 Insstart.col = MAXCOL;
8470 }
8471 /*
8472 * In replace mode:
8473 * cc < 0: NL was inserted, delete it
8474 * cc >= 0: NL was replaced, put original characters back
8475 */
8476 cc = -1;
8477 if (State & REPLACE_FLAG)
8478 cc = replace_pop(); /* returns -1 if NL was inserted */
8479 /*
8480 * In replace mode, in the line we started replacing, we only move the
8481 * cursor.
8482 */
8483 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8484 {
8485 dec_cursor();
8486 }
8487 else
8488 {
8489#ifdef FEAT_VREPLACE
8490 if (!(State & VREPLACE_FLAG)
8491 || curwin->w_cursor.lnum > orig_line_count)
8492#endif
8493 {
8494 temp = gchar_cursor(); /* remember current char */
8495 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008496
8497 /* When "aw" is in 'formatoptions' we must delete the space at
8498 * the end of the line, otherwise the line will be broken
8499 * again when auto-formatting. */
8500 if (has_format_option(FO_AUTO)
8501 && has_format_option(FO_WHITE_PAR))
8502 {
8503 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8504 TRUE);
8505 int len;
8506
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008507 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008508 if (len > 0 && ptr[len - 1] == ' ')
8509 ptr[len - 1] = NUL;
8510 }
8511
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 (void)do_join(FALSE);
8513 if (temp == NUL && gchar_cursor() != NUL)
8514 inc_cursor();
8515 }
8516#ifdef FEAT_VREPLACE
8517 else
8518 dec_cursor();
8519#endif
8520
8521 /*
8522 * In REPLACE mode we have to put back the text that was replaced
8523 * by the NL. On the replace stack is first a NUL-terminated
8524 * sequence of characters that were deleted and then the
8525 * characters that NL replaced.
8526 */
8527 if (State & REPLACE_FLAG)
8528 {
8529 /*
8530 * Do the next ins_char() in NORMAL state, to
8531 * prevent ins_char() from replacing characters and
8532 * avoiding showmatch().
8533 */
8534 oldState = State;
8535 State = NORMAL;
8536 /*
8537 * restore characters (blanks) deleted after cursor
8538 */
8539 while (cc > 0)
8540 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008541 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008542#ifdef FEAT_MBYTE
8543 mb_replace_pop_ins(cc);
8544#else
8545 ins_char(cc);
8546#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008547 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548 cc = replace_pop();
8549 }
8550 /* restore the characters that NL replaced */
8551 replace_pop_ins();
8552 State = oldState;
8553 }
8554 }
8555 did_ai = FALSE;
8556 }
8557 else
8558 {
8559 /*
8560 * Delete character(s) before the cursor.
8561 */
8562#ifdef FEAT_RIGHTLEFT
8563 if (revins_on) /* put cursor on last inserted char */
8564 dec_cursor();
8565#endif
8566 mincol = 0;
8567 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008568 if (mode == BACKSPACE_LINE
8569 && (curbuf->b_p_ai
8570#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008571 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008572#endif
8573 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574#ifdef FEAT_RIGHTLEFT
8575 && !revins_on
8576#endif
8577 )
8578 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008579 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008581 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008582 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008583 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008584 }
8585
8586 /*
8587 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8588 */
8589 if ( mode == BACKSPACE_CHAR
8590 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008591 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008592 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008593 && (*(ml_get_cursor() - 1) == TAB
8594 || (*(ml_get_cursor() - 1) == ' '
8595 && (!*inserted_space_p
8596 || arrow_used))))))
8597 {
8598 int ts;
8599 colnr_T vcol;
8600 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008601 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602
8603 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008604 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605 ts = curbuf->b_p_sw;
8606 else
8607 ts = curbuf->b_p_sts;
8608 /* Compute the virtual column where we want to be. Since
8609 * 'showbreak' may get in the way, need to get the last column of
8610 * the previous character. */
8611 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008612 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 dec_cursor();
8614 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8615 inc_cursor();
8616 want_vcol = (want_vcol / ts) * ts;
8617
8618 /* delete characters until we are at or before want_vcol */
8619 while (vcol > want_vcol
8620 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008621 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008622
8623 /* insert extra spaces until we are at want_vcol */
8624 while (vcol < want_vcol)
8625 {
8626 /* Remember the first char we inserted */
8627 if (curwin->w_cursor.lnum == Insstart.lnum
8628 && curwin->w_cursor.col < Insstart.col)
8629 Insstart.col = curwin->w_cursor.col;
8630
8631#ifdef FEAT_VREPLACE
8632 if (State & VREPLACE_FLAG)
8633 ins_char(' ');
8634 else
8635#endif
8636 {
8637 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008638 if ((State & REPLACE_FLAG))
8639 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 }
8641 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8642 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008643
8644 /* If we are now back where we started delete one character. Can
8645 * happen when using 'sts' and 'linebreak'. */
8646 if (vcol >= start_vcol)
8647 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 }
8649
8650 /*
8651 * Delete upto starting point, start of line or previous word.
8652 */
8653 else do
8654 {
8655#ifdef FEAT_RIGHTLEFT
8656 if (!revins_on) /* put cursor on char to be deleted */
8657#endif
8658 dec_cursor();
8659
8660 /* start of word? */
8661 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8662 {
8663 mode = BACKSPACE_WORD_NOT_SPACE;
8664 temp = vim_iswordc(gchar_cursor());
8665 }
8666 /* end of word? */
8667 else if (mode == BACKSPACE_WORD_NOT_SPACE
8668 && (vim_isspace(cc = gchar_cursor())
8669 || vim_iswordc(cc) != temp))
8670 {
8671#ifdef FEAT_RIGHTLEFT
8672 if (!revins_on)
8673#endif
8674 inc_cursor();
8675#ifdef FEAT_RIGHTLEFT
8676 else if (State & REPLACE_FLAG)
8677 dec_cursor();
8678#endif
8679 break;
8680 }
8681 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008682 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008683 else
8684 {
8685#ifdef FEAT_MBYTE
8686 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008687 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688#endif
8689 (void)del_char(FALSE);
8690#ifdef FEAT_MBYTE
8691 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008692 * If there are combining characters and 'delcombine' is set
8693 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008694 * character.
8695 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008696 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008697 inc_cursor();
8698#endif
8699#ifdef FEAT_RIGHTLEFT
8700 if (revins_chars)
8701 {
8702 revins_chars--;
8703 revins_legal++;
8704 }
8705 if (revins_on && gchar_cursor() == NUL)
8706 break;
8707#endif
8708 }
8709 /* Just a single backspace?: */
8710 if (mode == BACKSPACE_CHAR)
8711 break;
8712 } while (
8713#ifdef FEAT_RIGHTLEFT
8714 revins_on ||
8715#endif
8716 (curwin->w_cursor.col > mincol
8717 && (curwin->w_cursor.lnum != Insstart.lnum
8718 || curwin->w_cursor.col != Insstart.col)));
8719 did_backspace = TRUE;
8720 }
8721#ifdef FEAT_SMARTINDENT
8722 did_si = FALSE;
8723 can_si = FALSE;
8724 can_si_back = FALSE;
8725#endif
8726 if (curwin->w_cursor.col <= 1)
8727 did_ai = FALSE;
8728 /*
8729 * It's a little strange to put backspaces into the redo
8730 * buffer, but it makes auto-indent a lot easier to deal
8731 * with.
8732 */
8733 AppendCharToRedobuff(c);
8734
8735 /* If deleted before the insertion point, adjust it */
8736 if (curwin->w_cursor.lnum == Insstart.lnum
8737 && curwin->w_cursor.col < Insstart.col)
8738 Insstart.col = curwin->w_cursor.col;
8739
8740 /* vi behaviour: the cursor moves backward but the character that
8741 * was there remains visible
8742 * Vim behaviour: the cursor moves backward and the character that
8743 * was there is erased from the screen.
8744 * We can emulate the vi behaviour by pretending there is a dollar
8745 * displayed even when there isn't.
8746 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8747 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8748 dollar_vcol = curwin->w_virtcol;
8749
Bram Moolenaarce3be472008-01-14 19:12:28 +00008750#ifdef FEAT_FOLDING
8751 /* When deleting a char the cursor line must never be in a closed fold.
8752 * E.g., when 'foldmethod' is indent and deleting the first non-white
8753 * char before a Tab. */
8754 if (did_backspace)
8755 foldOpenCursor();
8756#endif
8757
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 return did_backspace;
8759}
8760
8761#ifdef FEAT_MOUSE
8762 static void
8763ins_mouse(c)
8764 int c;
8765{
8766 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008767 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768
8769# ifdef FEAT_GUI
8770 /* When GUI is active, also move/paste when 'mouse' is empty */
8771 if (!gui.in_use)
8772# endif
8773 if (!mouse_has(MOUSE_INSERT))
8774 return;
8775
8776 undisplay_dollar();
8777 tpos = curwin->w_cursor;
8778 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8779 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008780#ifdef FEAT_WINDOWS
8781 win_T *new_curwin = curwin;
8782
8783 if (curwin != old_curwin && win_valid(old_curwin))
8784 {
8785 /* Mouse took us to another window. We need to go back to the
8786 * previous one to stop insert there properly. */
8787 curwin = old_curwin;
8788 curbuf = curwin->w_buffer;
8789 }
8790#endif
8791 start_arrow(curwin == old_curwin ? &tpos : NULL);
8792#ifdef FEAT_WINDOWS
8793 if (curwin != new_curwin && win_valid(new_curwin))
8794 {
8795 curwin = new_curwin;
8796 curbuf = curwin->w_buffer;
8797 }
8798#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799# ifdef FEAT_CINDENT
8800 can_cindent = TRUE;
8801# endif
8802 }
8803
8804#ifdef FEAT_WINDOWS
8805 /* redraw status lines (in case another window became active) */
8806 redraw_statuslines();
8807#endif
8808}
8809
8810 static void
8811ins_mousescroll(up)
8812 int up;
8813{
8814 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008815# if defined(FEAT_WINDOWS)
8816 win_T *old_curwin = curwin;
8817# endif
8818# ifdef FEAT_INS_EXPAND
8819 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008820# endif
8821
8822 tpos = curwin->w_cursor;
8823
8824# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825 /* Currently the mouse coordinates are only known in the GUI. */
8826 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8827 {
8828 int row, col;
8829
8830 row = mouse_row;
8831 col = mouse_col;
8832
8833 /* find the window at the pointer coordinates */
8834 curwin = mouse_find_win(&row, &col);
8835 curbuf = curwin->w_buffer;
8836 }
8837 if (curwin == old_curwin)
8838# endif
8839 undisplay_dollar();
8840
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008841# ifdef FEAT_INS_EXPAND
8842 /* Don't scroll the window in which completion is being done. */
8843 if (!pum_visible()
8844# if defined(FEAT_WINDOWS)
8845 || curwin != old_curwin
8846# endif
8847 )
8848# endif
8849 {
8850 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8851 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8852 else
8853 scroll_redraw(up, 3L);
8854# ifdef FEAT_INS_EXPAND
8855 did_scroll = TRUE;
8856# endif
8857 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858
8859# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8860 curwin->w_redr_status = TRUE;
8861
8862 curwin = old_curwin;
8863 curbuf = curwin->w_buffer;
8864# endif
8865
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008866# ifdef FEAT_INS_EXPAND
8867 /* The popup menu may overlay the window, need to redraw it.
8868 * TODO: Would be more efficient to only redraw the windows that are
8869 * overlapped by the popup menu. */
8870 if (pum_visible() && did_scroll)
8871 {
8872 redraw_all_later(NOT_VALID);
8873 ins_compl_show_pum();
8874 }
8875# endif
8876
Bram Moolenaar071d4272004-06-13 20:20:40 +00008877 if (!equalpos(curwin->w_cursor, tpos))
8878 {
8879 start_arrow(&tpos);
8880# ifdef FEAT_CINDENT
8881 can_cindent = TRUE;
8882# endif
8883 }
8884}
8885#endif
8886
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008887#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008888 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008889ins_tabline(c)
8890 int c;
8891{
8892 /* We will be leaving the current window, unless closing another tab. */
8893 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8894 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8895 {
8896 undisplay_dollar();
8897 start_arrow(&curwin->w_cursor);
8898# ifdef FEAT_CINDENT
8899 can_cindent = TRUE;
8900# endif
8901 }
8902
8903 if (c == K_TABLINE)
8904 goto_tabpage(current_tab);
8905 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008906 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008907 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008908 redraw_statuslines(); /* will redraw the tabline when needed */
8909 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008910}
8911#endif
8912
8913#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914 void
8915ins_scroll()
8916{
8917 pos_T tpos;
8918
8919 undisplay_dollar();
8920 tpos = curwin->w_cursor;
8921 if (gui_do_scroll())
8922 {
8923 start_arrow(&tpos);
8924# ifdef FEAT_CINDENT
8925 can_cindent = TRUE;
8926# endif
8927 }
8928}
8929
8930 void
8931ins_horscroll()
8932{
8933 pos_T tpos;
8934
8935 undisplay_dollar();
8936 tpos = curwin->w_cursor;
8937 if (gui_do_horiz_scroll())
8938 {
8939 start_arrow(&tpos);
8940# ifdef FEAT_CINDENT
8941 can_cindent = TRUE;
8942# endif
8943 }
8944}
8945#endif
8946
8947 static void
8948ins_left()
8949{
8950 pos_T tpos;
8951
8952#ifdef FEAT_FOLDING
8953 if ((fdo_flags & FDO_HOR) && KeyTyped)
8954 foldOpenCursor();
8955#endif
8956 undisplay_dollar();
8957 tpos = curwin->w_cursor;
8958 if (oneleft() == OK)
8959 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008960#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8961 /* Only call start_arrow() when not busy with preediting, it will
8962 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8963 if (!im_is_preediting())
8964#endif
8965 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008966#ifdef FEAT_RIGHTLEFT
8967 /* If exit reversed string, position is fixed */
8968 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8969 revins_legal++;
8970 revins_chars++;
8971#endif
8972 }
8973
8974 /*
8975 * if 'whichwrap' set for cursor in insert mode may go to
8976 * previous line
8977 */
8978 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8979 {
8980 start_arrow(&tpos);
8981 --(curwin->w_cursor.lnum);
8982 coladvance((colnr_T)MAXCOL);
8983 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8984 }
8985 else
8986 vim_beep();
8987}
8988
8989 static void
8990ins_home(c)
8991 int c;
8992{
8993 pos_T tpos;
8994
8995#ifdef FEAT_FOLDING
8996 if ((fdo_flags & FDO_HOR) && KeyTyped)
8997 foldOpenCursor();
8998#endif
8999 undisplay_dollar();
9000 tpos = curwin->w_cursor;
9001 if (c == K_C_HOME)
9002 curwin->w_cursor.lnum = 1;
9003 curwin->w_cursor.col = 0;
9004#ifdef FEAT_VIRTUALEDIT
9005 curwin->w_cursor.coladd = 0;
9006#endif
9007 curwin->w_curswant = 0;
9008 start_arrow(&tpos);
9009}
9010
9011 static void
9012ins_end(c)
9013 int c;
9014{
9015 pos_T tpos;
9016
9017#ifdef FEAT_FOLDING
9018 if ((fdo_flags & FDO_HOR) && KeyTyped)
9019 foldOpenCursor();
9020#endif
9021 undisplay_dollar();
9022 tpos = curwin->w_cursor;
9023 if (c == K_C_END)
9024 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9025 coladvance((colnr_T)MAXCOL);
9026 curwin->w_curswant = MAXCOL;
9027
9028 start_arrow(&tpos);
9029}
9030
9031 static void
9032ins_s_left()
9033{
9034#ifdef FEAT_FOLDING
9035 if ((fdo_flags & FDO_HOR) && KeyTyped)
9036 foldOpenCursor();
9037#endif
9038 undisplay_dollar();
9039 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9040 {
9041 start_arrow(&curwin->w_cursor);
9042 (void)bck_word(1L, FALSE, FALSE);
9043 curwin->w_set_curswant = TRUE;
9044 }
9045 else
9046 vim_beep();
9047}
9048
9049 static void
9050ins_right()
9051{
9052#ifdef FEAT_FOLDING
9053 if ((fdo_flags & FDO_HOR) && KeyTyped)
9054 foldOpenCursor();
9055#endif
9056 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009057 if (gchar_cursor() != NUL
9058#ifdef FEAT_VIRTUALEDIT
9059 || virtual_active()
9060#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009061 )
9062 {
9063 start_arrow(&curwin->w_cursor);
9064 curwin->w_set_curswant = TRUE;
9065#ifdef FEAT_VIRTUALEDIT
9066 if (virtual_active())
9067 oneright();
9068 else
9069#endif
9070 {
9071#ifdef FEAT_MBYTE
9072 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009073 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009074 else
9075#endif
9076 ++curwin->w_cursor.col;
9077 }
9078
9079#ifdef FEAT_RIGHTLEFT
9080 revins_legal++;
9081 if (revins_chars)
9082 revins_chars--;
9083#endif
9084 }
9085 /* if 'whichwrap' set for cursor in insert mode, may move the
9086 * cursor to the next line */
9087 else if (vim_strchr(p_ww, ']') != NULL
9088 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9089 {
9090 start_arrow(&curwin->w_cursor);
9091 curwin->w_set_curswant = TRUE;
9092 ++curwin->w_cursor.lnum;
9093 curwin->w_cursor.col = 0;
9094 }
9095 else
9096 vim_beep();
9097}
9098
9099 static void
9100ins_s_right()
9101{
9102#ifdef FEAT_FOLDING
9103 if ((fdo_flags & FDO_HOR) && KeyTyped)
9104 foldOpenCursor();
9105#endif
9106 undisplay_dollar();
9107 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9108 || gchar_cursor() != NUL)
9109 {
9110 start_arrow(&curwin->w_cursor);
9111 (void)fwd_word(1L, FALSE, 0);
9112 curwin->w_set_curswant = TRUE;
9113 }
9114 else
9115 vim_beep();
9116}
9117
9118 static void
9119ins_up(startcol)
9120 int startcol; /* when TRUE move to Insstart.col */
9121{
9122 pos_T tpos;
9123 linenr_T old_topline = curwin->w_topline;
9124#ifdef FEAT_DIFF
9125 int old_topfill = curwin->w_topfill;
9126#endif
9127
9128 undisplay_dollar();
9129 tpos = curwin->w_cursor;
9130 if (cursor_up(1L, TRUE) == OK)
9131 {
9132 if (startcol)
9133 coladvance(getvcol_nolist(&Insstart));
9134 if (old_topline != curwin->w_topline
9135#ifdef FEAT_DIFF
9136 || old_topfill != curwin->w_topfill
9137#endif
9138 )
9139 redraw_later(VALID);
9140 start_arrow(&tpos);
9141#ifdef FEAT_CINDENT
9142 can_cindent = TRUE;
9143#endif
9144 }
9145 else
9146 vim_beep();
9147}
9148
9149 static void
9150ins_pageup()
9151{
9152 pos_T tpos;
9153
9154 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009155
9156#ifdef FEAT_WINDOWS
9157 if (mod_mask & MOD_MASK_CTRL)
9158 {
9159 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009160 if (first_tabpage->tp_next != NULL)
9161 {
9162 start_arrow(&curwin->w_cursor);
9163 goto_tabpage(-1);
9164 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009165 return;
9166 }
9167#endif
9168
Bram Moolenaar071d4272004-06-13 20:20:40 +00009169 tpos = curwin->w_cursor;
9170 if (onepage(BACKWARD, 1L) == OK)
9171 {
9172 start_arrow(&tpos);
9173#ifdef FEAT_CINDENT
9174 can_cindent = TRUE;
9175#endif
9176 }
9177 else
9178 vim_beep();
9179}
9180
9181 static void
9182ins_down(startcol)
9183 int startcol; /* when TRUE move to Insstart.col */
9184{
9185 pos_T tpos;
9186 linenr_T old_topline = curwin->w_topline;
9187#ifdef FEAT_DIFF
9188 int old_topfill = curwin->w_topfill;
9189#endif
9190
9191 undisplay_dollar();
9192 tpos = curwin->w_cursor;
9193 if (cursor_down(1L, TRUE) == OK)
9194 {
9195 if (startcol)
9196 coladvance(getvcol_nolist(&Insstart));
9197 if (old_topline != curwin->w_topline
9198#ifdef FEAT_DIFF
9199 || old_topfill != curwin->w_topfill
9200#endif
9201 )
9202 redraw_later(VALID);
9203 start_arrow(&tpos);
9204#ifdef FEAT_CINDENT
9205 can_cindent = TRUE;
9206#endif
9207 }
9208 else
9209 vim_beep();
9210}
9211
9212 static void
9213ins_pagedown()
9214{
9215 pos_T tpos;
9216
9217 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009218
9219#ifdef FEAT_WINDOWS
9220 if (mod_mask & MOD_MASK_CTRL)
9221 {
9222 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009223 if (first_tabpage->tp_next != NULL)
9224 {
9225 start_arrow(&curwin->w_cursor);
9226 goto_tabpage(0);
9227 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009228 return;
9229 }
9230#endif
9231
Bram Moolenaar071d4272004-06-13 20:20:40 +00009232 tpos = curwin->w_cursor;
9233 if (onepage(FORWARD, 1L) == OK)
9234 {
9235 start_arrow(&tpos);
9236#ifdef FEAT_CINDENT
9237 can_cindent = TRUE;
9238#endif
9239 }
9240 else
9241 vim_beep();
9242}
9243
9244#ifdef FEAT_DND
9245 static void
9246ins_drop()
9247{
9248 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9249}
9250#endif
9251
9252/*
9253 * Handle TAB in Insert or Replace mode.
9254 * Return TRUE when the TAB needs to be inserted like a normal character.
9255 */
9256 static int
9257ins_tab()
9258{
9259 int ind;
9260 int i;
9261 int temp;
9262
9263 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9264 Insstart_blank_vcol = get_nolist_virtcol();
9265 if (echeck_abbr(TAB + ABBR_OFF))
9266 return FALSE;
9267
9268 ind = inindent(0);
9269#ifdef FEAT_CINDENT
9270 if (ind)
9271 can_cindent = FALSE;
9272#endif
9273
9274 /*
9275 * When nothing special, insert TAB like a normal character
9276 */
9277 if (!curbuf->b_p_et
9278 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9279 && curbuf->b_p_sts == 0)
9280 return TRUE;
9281
9282 if (stop_arrow() == FAIL)
9283 return TRUE;
9284
9285 did_ai = FALSE;
9286#ifdef FEAT_SMARTINDENT
9287 did_si = FALSE;
9288 can_si = FALSE;
9289 can_si_back = FALSE;
9290#endif
9291 AppendToRedobuff((char_u *)"\t");
9292
9293 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9294 temp = (int)curbuf->b_p_sw;
9295 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9296 temp = (int)curbuf->b_p_sts;
9297 else /* otherwise use 'tabstop' */
9298 temp = (int)curbuf->b_p_ts;
9299 temp -= get_nolist_virtcol() % temp;
9300
9301 /*
9302 * Insert the first space with ins_char(). It will delete one char in
9303 * replace mode. Insert the rest with ins_str(); it will not delete any
9304 * chars. For VREPLACE mode, we use ins_char() for all characters.
9305 */
9306 ins_char(' ');
9307 while (--temp > 0)
9308 {
9309#ifdef FEAT_VREPLACE
9310 if (State & VREPLACE_FLAG)
9311 ins_char(' ');
9312 else
9313#endif
9314 {
9315 ins_str((char_u *)" ");
9316 if (State & REPLACE_FLAG) /* no char replaced */
9317 replace_push(NUL);
9318 }
9319 }
9320
9321 /*
9322 * When 'expandtab' not set: Replace spaces by TABs where possible.
9323 */
9324 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9325 {
9326 char_u *ptr;
9327#ifdef FEAT_VREPLACE
9328 char_u *saved_line = NULL; /* init for GCC */
9329 pos_T pos;
9330#endif
9331 pos_T fpos;
9332 pos_T *cursor;
9333 colnr_T want_vcol, vcol;
9334 int change_col = -1;
9335 int save_list = curwin->w_p_list;
9336
9337 /*
9338 * Get the current line. For VREPLACE mode, don't make real changes
9339 * yet, just work on a copy of the line.
9340 */
9341#ifdef FEAT_VREPLACE
9342 if (State & VREPLACE_FLAG)
9343 {
9344 pos = curwin->w_cursor;
9345 cursor = &pos;
9346 saved_line = vim_strsave(ml_get_curline());
9347 if (saved_line == NULL)
9348 return FALSE;
9349 ptr = saved_line + pos.col;
9350 }
9351 else
9352#endif
9353 {
9354 ptr = ml_get_cursor();
9355 cursor = &curwin->w_cursor;
9356 }
9357
9358 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9359 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9360 curwin->w_p_list = FALSE;
9361
9362 /* Find first white before the cursor */
9363 fpos = curwin->w_cursor;
9364 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9365 {
9366 --fpos.col;
9367 --ptr;
9368 }
9369
9370 /* In Replace mode, don't change characters before the insert point. */
9371 if ((State & REPLACE_FLAG)
9372 && fpos.lnum == Insstart.lnum
9373 && fpos.col < Insstart.col)
9374 {
9375 ptr += Insstart.col - fpos.col;
9376 fpos.col = Insstart.col;
9377 }
9378
9379 /* compute virtual column numbers of first white and cursor */
9380 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9381 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9382
9383 /* Use as many TABs as possible. Beware of 'showbreak' and
9384 * 'linebreak' adding extra virtual columns. */
9385 while (vim_iswhite(*ptr))
9386 {
9387 i = lbr_chartabsize((char_u *)"\t", vcol);
9388 if (vcol + i > want_vcol)
9389 break;
9390 if (*ptr != TAB)
9391 {
9392 *ptr = TAB;
9393 if (change_col < 0)
9394 {
9395 change_col = fpos.col; /* Column of first change */
9396 /* May have to adjust Insstart */
9397 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9398 Insstart.col = fpos.col;
9399 }
9400 }
9401 ++fpos.col;
9402 ++ptr;
9403 vcol += i;
9404 }
9405
9406 if (change_col >= 0)
9407 {
9408 int repl_off = 0;
9409
9410 /* Skip over the spaces we need. */
9411 while (vcol < want_vcol && *ptr == ' ')
9412 {
9413 vcol += lbr_chartabsize(ptr, vcol);
9414 ++ptr;
9415 ++repl_off;
9416 }
9417 if (vcol > want_vcol)
9418 {
9419 /* Must have a char with 'showbreak' just before it. */
9420 --ptr;
9421 --repl_off;
9422 }
9423 fpos.col += repl_off;
9424
9425 /* Delete following spaces. */
9426 i = cursor->col - fpos.col;
9427 if (i > 0)
9428 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009429 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009430 /* correct replace stack. */
9431 if ((State & REPLACE_FLAG)
9432#ifdef FEAT_VREPLACE
9433 && !(State & VREPLACE_FLAG)
9434#endif
9435 )
9436 for (temp = i; --temp >= 0; )
9437 replace_join(repl_off);
9438 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009439#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009440 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009441 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009442 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009443 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9444 (char_u *)"\t", 1);
9445 }
9446#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009447 cursor->col -= i;
9448
9449#ifdef FEAT_VREPLACE
9450 /*
9451 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9452 * backspacing over the changed spacing and then inserting the new
9453 * spacing.
9454 */
9455 if (State & VREPLACE_FLAG)
9456 {
9457 /* Backspace from real cursor to change_col */
9458 backspace_until_column(change_col);
9459
9460 /* Insert each char in saved_line from changed_col to
9461 * ptr-cursor */
9462 ins_bytes_len(saved_line + change_col,
9463 cursor->col - change_col);
9464 }
9465#endif
9466 }
9467
9468#ifdef FEAT_VREPLACE
9469 if (State & VREPLACE_FLAG)
9470 vim_free(saved_line);
9471#endif
9472 curwin->w_p_list = save_list;
9473 }
9474
9475 return FALSE;
9476}
9477
9478/*
9479 * Handle CR or NL in insert mode.
9480 * Return TRUE when out of memory or can't undo.
9481 */
9482 static int
9483ins_eol(c)
9484 int c;
9485{
9486 int i;
9487
9488 if (echeck_abbr(c + ABBR_OFF))
9489 return FALSE;
9490 if (stop_arrow() == FAIL)
9491 return TRUE;
9492 undisplay_dollar();
9493
9494 /*
9495 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9496 * character under the cursor. Only push a NUL on the replace stack,
9497 * nothing to put back when the NL is deleted.
9498 */
9499 if ((State & REPLACE_FLAG)
9500#ifdef FEAT_VREPLACE
9501 && !(State & VREPLACE_FLAG)
9502#endif
9503 )
9504 replace_push(NUL);
9505
9506 /*
9507 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9508 * replacing the next line, so we push all of the characters left on the
9509 * line onto the replace stack. This is not done here though, it is done
9510 * in open_line().
9511 */
9512
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009513#ifdef FEAT_VIRTUALEDIT
9514 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9515 * CTRL-O). */
9516 if (virtual_active() && curwin->w_cursor.coladd > 0)
9517 coladvance(getviscol());
9518#endif
9519
Bram Moolenaar071d4272004-06-13 20:20:40 +00009520#ifdef FEAT_RIGHTLEFT
9521# ifdef FEAT_FKMAP
9522 if (p_altkeymap && p_fkmap)
9523 fkmap(NL);
9524# endif
9525 /* NL in reverse insert will always start in the end of
9526 * current line. */
9527 if (revins_on)
9528 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9529#endif
9530
9531 AppendToRedobuff(NL_STR);
9532 i = open_line(FORWARD,
9533#ifdef FEAT_COMMENTS
9534 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9535#endif
9536 0, old_indent);
9537 old_indent = 0;
9538#ifdef FEAT_CINDENT
9539 can_cindent = TRUE;
9540#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009541#ifdef FEAT_FOLDING
9542 /* When inserting a line the cursor line must never be in a closed fold. */
9543 foldOpenCursor();
9544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545
9546 return (!i);
9547}
9548
9549#ifdef FEAT_DIGRAPHS
9550/*
9551 * Handle digraph in insert mode.
9552 * Returns character still to be inserted, or NUL when nothing remaining to be
9553 * done.
9554 */
9555 static int
9556ins_digraph()
9557{
9558 int c;
9559 int cc;
9560
9561 pc_status = PC_STATUS_UNSET;
9562 if (redrawing() && !char_avail())
9563 {
9564 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009565 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009566
9567 edit_putchar('?', TRUE);
9568#ifdef FEAT_CMDL_INFO
9569 add_to_showcmd_c(Ctrl_K);
9570#endif
9571 }
9572
9573#ifdef USE_ON_FLY_SCROLL
9574 dont_scroll = TRUE; /* disallow scrolling here */
9575#endif
9576
9577 /* don't map the digraph chars. This also prevents the
9578 * mode message to be deleted when ESC is hit */
9579 ++no_mapping;
9580 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009581 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582 --no_mapping;
9583 --allow_keys;
9584 if (IS_SPECIAL(c) || mod_mask) /* special key */
9585 {
9586#ifdef FEAT_CMDL_INFO
9587 clear_showcmd();
9588#endif
9589 insert_special(c, TRUE, FALSE);
9590 return NUL;
9591 }
9592 if (c != ESC)
9593 {
9594 if (redrawing() && !char_avail())
9595 {
9596 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009597 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009598
9599 if (char2cells(c) == 1)
9600 {
9601 /* first remove the '?', otherwise it's restored when typing
9602 * an ESC next */
9603 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009604 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009605 edit_putchar(c, TRUE);
9606 }
9607#ifdef FEAT_CMDL_INFO
9608 add_to_showcmd_c(c);
9609#endif
9610 }
9611 ++no_mapping;
9612 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009613 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009614 --no_mapping;
9615 --allow_keys;
9616 if (cc != ESC)
9617 {
9618 AppendToRedobuff((char_u *)CTRL_V_STR);
9619 c = getdigraph(c, cc, TRUE);
9620#ifdef FEAT_CMDL_INFO
9621 clear_showcmd();
9622#endif
9623 return c;
9624 }
9625 }
9626 edit_unputchar();
9627#ifdef FEAT_CMDL_INFO
9628 clear_showcmd();
9629#endif
9630 return NUL;
9631}
9632#endif
9633
9634/*
9635 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9636 * Returns the char to be inserted, or NUL if none found.
9637 */
9638 static int
9639ins_copychar(lnum)
9640 linenr_T lnum;
9641{
9642 int c;
9643 int temp;
9644 char_u *ptr, *prev_ptr;
9645
9646 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9647 {
9648 vim_beep();
9649 return NUL;
9650 }
9651
9652 /* try to advance to the cursor column */
9653 temp = 0;
9654 ptr = ml_get(lnum);
9655 prev_ptr = ptr;
9656 validate_virtcol();
9657 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9658 {
9659 prev_ptr = ptr;
9660 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9661 }
9662 if ((colnr_T)temp > curwin->w_virtcol)
9663 ptr = prev_ptr;
9664
9665#ifdef FEAT_MBYTE
9666 c = (*mb_ptr2char)(ptr);
9667#else
9668 c = *ptr;
9669#endif
9670 if (c == NUL)
9671 vim_beep();
9672 return c;
9673}
9674
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009675/*
9676 * CTRL-Y or CTRL-E typed in Insert mode.
9677 */
9678 static int
9679ins_ctrl_ey(tc)
9680 int tc;
9681{
9682 int c = tc;
9683
9684#ifdef FEAT_INS_EXPAND
9685 if (ctrl_x_mode == CTRL_X_SCROLL)
9686 {
9687 if (c == Ctrl_Y)
9688 scrolldown_clamp();
9689 else
9690 scrollup_clamp();
9691 redraw_later(VALID);
9692 }
9693 else
9694#endif
9695 {
9696 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9697 if (c != NUL)
9698 {
9699 long tw_save;
9700
9701 /* The character must be taken literally, insert like it
9702 * was typed after a CTRL-V, and pretend 'textwidth'
9703 * wasn't set. Digits, 'o' and 'x' are special after a
9704 * CTRL-V, don't use it for these. */
9705 if (c < 256 && !isalnum(c))
9706 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9707 tw_save = curbuf->b_p_tw;
9708 curbuf->b_p_tw = -1;
9709 insert_special(c, TRUE, FALSE);
9710 curbuf->b_p_tw = tw_save;
9711#ifdef FEAT_RIGHTLEFT
9712 revins_chars++;
9713 revins_legal++;
9714#endif
9715 c = Ctrl_V; /* pretend CTRL-V is last character */
9716 auto_format(FALSE, TRUE);
9717 }
9718 }
9719 return c;
9720}
9721
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722#ifdef FEAT_SMARTINDENT
9723/*
9724 * Try to do some very smart auto-indenting.
9725 * Used when inserting a "normal" character.
9726 */
9727 static void
9728ins_try_si(c)
9729 int c;
9730{
9731 pos_T *pos, old_pos;
9732 char_u *ptr;
9733 int i;
9734 int temp;
9735
9736 /*
9737 * do some very smart indenting when entering '{' or '}'
9738 */
9739 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9740 {
9741 /*
9742 * for '}' set indent equal to indent of line containing matching '{'
9743 */
9744 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9745 {
9746 old_pos = curwin->w_cursor;
9747 /*
9748 * If the matching '{' has a ')' immediately before it (ignoring
9749 * white-space), then line up with the start of the line
9750 * containing the matching '(' if there is one. This handles the
9751 * case where an "if (..\n..) {" statement continues over multiple
9752 * lines -- webb
9753 */
9754 ptr = ml_get(pos->lnum);
9755 i = pos->col;
9756 if (i > 0) /* skip blanks before '{' */
9757 while (--i > 0 && vim_iswhite(ptr[i]))
9758 ;
9759 curwin->w_cursor.lnum = pos->lnum;
9760 curwin->w_cursor.col = i;
9761 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9762 curwin->w_cursor = *pos;
9763 i = get_indent();
9764 curwin->w_cursor = old_pos;
9765#ifdef FEAT_VREPLACE
9766 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009767 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768 else
9769#endif
9770 (void)set_indent(i, SIN_CHANGED);
9771 }
9772 else if (curwin->w_cursor.col > 0)
9773 {
9774 /*
9775 * when inserting '{' after "O" reduce indent, but not
9776 * more than indent of previous line
9777 */
9778 temp = TRUE;
9779 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9780 {
9781 old_pos = curwin->w_cursor;
9782 i = get_indent();
9783 while (curwin->w_cursor.lnum > 1)
9784 {
9785 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9786
9787 /* ignore empty lines and lines starting with '#'. */
9788 if (*ptr != '#' && *ptr != NUL)
9789 break;
9790 }
9791 if (get_indent() >= i)
9792 temp = FALSE;
9793 curwin->w_cursor = old_pos;
9794 }
9795 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009796 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 }
9798 }
9799
9800 /*
9801 * set indent of '#' always to 0
9802 */
9803 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9804 {
9805 /* remember current indent for next line */
9806 old_indent = get_indent();
9807 (void)set_indent(0, SIN_CHANGED);
9808 }
9809
9810 /* Adjust ai_col, the char at this position can be deleted. */
9811 if (ai_col > curwin->w_cursor.col)
9812 ai_col = curwin->w_cursor.col;
9813}
9814#endif
9815
9816/*
9817 * Get the value that w_virtcol would have when 'list' is off.
9818 * Unless 'cpo' contains the 'L' flag.
9819 */
9820 static colnr_T
9821get_nolist_virtcol()
9822{
9823 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9824 return getvcol_nolist(&curwin->w_cursor);
9825 validate_virtcol();
9826 return curwin->w_virtcol;
9827}