blob: 7721259af44fcced129834dbd078d219f03316b9 [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
60static char_u e_hitend[] = N_("Hit end of paragraph");
61
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 Moolenaar572cb562005-08-05 21:35:02 +0000117static int compl_matches = 0;
118static char_u *compl_pattern = NULL;
119static int compl_direction = FORWARD;
120static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000121static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000122static pos_T compl_startpos;
123static colnr_T compl_col = 0; /* column where the text starts
124 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static char_u *compl_orig_text = NULL; /* text as it was before
126 * completion started */
127static int compl_cont_mode = 0;
128static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000129
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000130static void ins_ctrl_x __ARGS((void));
131static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000132static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000133static 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 +0000134static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000135static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000136static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000138static void ins_compl_upd_pum __ARGS((void));
139static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000140static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000141static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000142static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000143static 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 +0000144static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145static void ins_compl_free __ARGS((void));
146static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000147static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000148static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000149static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000150static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000151static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000152static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000153static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000154static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000155static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000156#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
157static void ins_compl_add_list __ARGS((list_T *list));
158#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000159static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000160static void ins_compl_delete __ARGS((void));
161static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000162static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000163static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000164static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000165static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000166static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000167static int ins_complete __ARGS((int c));
168static int quote_meta __ARGS((char_u *dest, char_u *str, int len));
169#endif /* FEAT_INS_EXPAND */
170
171#define BACKSPACE_CHAR 1
172#define BACKSPACE_WORD 2
173#define BACKSPACE_WORD_NOT_SPACE 3
174#define BACKSPACE_LINE 4
175
Bram Moolenaar754b5602006-02-09 23:53:20 +0000176static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177static void ins_ctrl_v __ARGS((void));
178static void undisplay_dollar __ARGS((void));
179static void insert_special __ARGS((int, int, int));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000180static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181static void check_auto_format __ARGS((int));
182static void redo_literal __ARGS((int c));
183static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000184#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000185static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000186static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000187static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000188#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
190static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000191#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000193#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000194static int replace_pop __ARGS((void));
195static void replace_join __ARGS((int off));
196static void replace_pop_ins __ARGS((void));
197#ifdef FEAT_MBYTE
198static void mb_replace_pop_ins __ARGS((int cc));
199#endif
200static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000201static void replace_do_bs __ARGS((int limit_col));
202static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203#ifdef FEAT_CINDENT
204static int cindent_on __ARGS((void));
205#endif
206static void ins_reg __ARGS((void));
207static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000208static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000209static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000210#ifdef FEAT_RIGHTLEFT
211static void ins_ctrl_ __ARGS((void));
212#endif
213#ifdef FEAT_VISUAL
214static int ins_start_select __ARGS((int c));
215#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000216static void ins_insert __ARGS((int replaceState));
217static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000218static void ins_shift __ARGS((int c, int lastc));
219static void ins_del __ARGS((void));
220static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
221#ifdef FEAT_MOUSE
222static void ins_mouse __ARGS((int c));
223static void ins_mousescroll __ARGS((int up));
224#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000225#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
226static void ins_tabline __ARGS((int c));
227#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228static void ins_left __ARGS((void));
229static void ins_home __ARGS((int c));
230static void ins_end __ARGS((int c));
231static void ins_s_left __ARGS((void));
232static void ins_right __ARGS((void));
233static void ins_s_right __ARGS((void));
234static void ins_up __ARGS((int startcol));
235static void ins_pageup __ARGS((void));
236static void ins_down __ARGS((int startcol));
237static void ins_pagedown __ARGS((void));
238#ifdef FEAT_DND
239static void ins_drop __ARGS((void));
240#endif
241static int ins_tab __ARGS((void));
242static int ins_eol __ARGS((int c));
243#ifdef FEAT_DIGRAPHS
244static int ins_digraph __ARGS((void));
245#endif
246static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000247static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000248#ifdef FEAT_SMARTINDENT
249static void ins_try_si __ARGS((int c));
250#endif
251static colnr_T get_nolist_virtcol __ARGS((void));
252
253static colnr_T Insstart_textlen; /* length of line when insert started */
254static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
255
256static char_u *last_insert = NULL; /* the text of the previous insert,
257 K_SPECIAL and CSI are escaped */
258static int last_insert_skip; /* nr of chars in front of previous insert */
259static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000260static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000261
262#ifdef FEAT_CINDENT
263static int can_cindent; /* may do cindenting on this line */
264#endif
265
266static int old_indent = 0; /* for ^^D command in insert mode */
267
268#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000269static int revins_on; /* reverse insert mode on */
270static int revins_chars; /* how much to skip after edit */
271static int revins_legal; /* was the last char 'legal'? */
272static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000273#endif
274
Bram Moolenaar071d4272004-06-13 20:20:40 +0000275static int ins_need_undo; /* call u_save() before inserting a
276 char. Set when edit() is called.
277 after that arrow_used is used. */
278
279static int did_add_space = FALSE; /* auto_format() added an extra space
280 under the cursor */
281
282/*
283 * edit(): Start inserting text.
284 *
285 * "cmdchar" can be:
286 * 'i' normal insert command
287 * 'a' normal append command
288 * 'R' replace command
289 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
290 * but still only one <CR> is inserted. The <Esc> is not used for redo.
291 * 'g' "gI" command.
292 * 'V' "gR" command for Virtual Replace mode.
293 * 'v' "gr" command for single character Virtual Replace mode.
294 *
295 * This function is not called recursively. For CTRL-O commands, it returns
296 * and lets the caller handle the Normal-mode command.
297 *
298 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
299 */
300 int
301edit(cmdchar, startln, count)
302 int cmdchar;
303 int startln; /* if set, insert at start of line */
304 long count;
305{
306 int c = 0;
307 char_u *ptr;
308 int lastc;
309 colnr_T mincol;
310 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000311 int i;
312 int did_backspace = TRUE; /* previous char was backspace */
313#ifdef FEAT_CINDENT
314 int line_is_white = FALSE; /* line is empty before insert */
315#endif
316 linenr_T old_topline = 0; /* topline before insertion */
317#ifdef FEAT_DIFF
318 int old_topfill = -1;
319#endif
320 int inserted_space = FALSE; /* just inserted a space */
321 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000322 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000323
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000324 /* Remember whether editing was restarted after CTRL-O. */
325 did_restart_edit = restart_edit;
326
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327 /* sleep before redrawing, needed for "CTRL-O :" that results in an
328 * error message */
329 check_for_delay(TRUE);
330
331#ifdef HAVE_SANDBOX
332 /* Don't allow inserting in the sandbox. */
333 if (sandbox != 0)
334 {
335 EMSG(_(e_sandbox));
336 return FALSE;
337 }
338#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000339 /* Don't allow changes in the buffer while editing the cmdline. The
340 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000341 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000342 {
343 EMSG(_(e_secure));
344 return FALSE;
345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000346
347#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000348 /* Don't allow recursive insert mode when busy with completion. */
349 if (compl_started || pum_visible())
350 {
351 EMSG(_(e_secure));
352 return FALSE;
353 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000354 ins_compl_clear(); /* clear stuff for CTRL-X mode */
355#endif
356
Bram Moolenaar843ee412004-06-30 16:16:41 +0000357#ifdef FEAT_AUTOCMD
358 /*
359 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
360 */
361 if (cmdchar != 'r' && cmdchar != 'v')
362 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000363# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000364 if (cmdchar == 'R')
365 ptr = (char_u *)"r";
366 else if (cmdchar == 'V')
367 ptr = (char_u *)"v";
368 else
369 ptr = (char_u *)"i";
370 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000371# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000372 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
373 }
374#endif
375
Bram Moolenaar071d4272004-06-13 20:20:40 +0000376#ifdef FEAT_MOUSE
377 /*
378 * When doing a paste with the middle mouse button, Insstart is set to
379 * where the paste started.
380 */
381 if (where_paste_started.lnum != 0)
382 Insstart = where_paste_started;
383 else
384#endif
385 {
386 Insstart = curwin->w_cursor;
387 if (startln)
388 Insstart.col = 0;
389 }
390 Insstart_textlen = linetabsize(ml_get_curline());
391 Insstart_blank_vcol = MAXCOL;
392 if (!did_ai)
393 ai_col = 0;
394
395 if (cmdchar != NUL && restart_edit == 0)
396 {
397 ResetRedobuff();
398 AppendNumberToRedobuff(count);
399#ifdef FEAT_VREPLACE
400 if (cmdchar == 'V' || cmdchar == 'v')
401 {
402 /* "gR" or "gr" command */
403 AppendCharToRedobuff('g');
404 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
405 }
406 else
407#endif
408 {
409 AppendCharToRedobuff(cmdchar);
410 if (cmdchar == 'g') /* "gI" command */
411 AppendCharToRedobuff('I');
412 else if (cmdchar == 'r') /* "r<CR>" command */
413 count = 1; /* insert only one <CR> */
414 }
415 }
416
417 if (cmdchar == 'R')
418 {
419#ifdef FEAT_FKMAP
420 if (p_fkmap && p_ri)
421 {
422 beep_flush();
423 EMSG(farsi_text_3); /* encoded in Farsi */
424 State = INSERT;
425 }
426 else
427#endif
428 State = REPLACE;
429 }
430#ifdef FEAT_VREPLACE
431 else if (cmdchar == 'V' || cmdchar == 'v')
432 {
433 State = VREPLACE;
434 replaceState = VREPLACE;
435 orig_line_count = curbuf->b_ml.ml_line_count;
436 vr_lines_changed = 1;
437 }
438#endif
439 else
440 State = INSERT;
441
442 stop_insert_mode = FALSE;
443
444 /*
445 * Need to recompute the cursor position, it might move when the cursor is
446 * on a TAB or special character.
447 */
448 curs_columns(TRUE);
449
450 /*
451 * Enable langmap or IME, indicated by 'iminsert'.
452 * Note that IME may enabled/disabled without us noticing here, thus the
453 * 'iminsert' value may not reflect what is actually used. It is updated
454 * when hitting <Esc>.
455 */
456 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
457 State |= LANGMAP;
458#ifdef USE_IM_CONTROL
459 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
460#endif
461
Bram Moolenaar071d4272004-06-13 20:20:40 +0000462#ifdef FEAT_MOUSE
463 setmouse();
464#endif
465#ifdef FEAT_CMDL_INFO
466 clear_showcmd();
467#endif
468#ifdef FEAT_RIGHTLEFT
469 /* there is no reverse replace mode */
470 revins_on = (State == INSERT && p_ri);
471 if (revins_on)
472 undisplay_dollar();
473 revins_chars = 0;
474 revins_legal = 0;
475 revins_scol = -1;
476#endif
477
478 /*
479 * Handle restarting Insert mode.
480 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
481 * restart_edit non-zero, and something in the stuff buffer.
482 */
483 if (restart_edit != 0 && stuff_empty())
484 {
485#ifdef FEAT_MOUSE
486 /*
487 * After a paste we consider text typed to be part of the insert for
488 * the pasted text. You can backspace over the pasted text too.
489 */
490 if (where_paste_started.lnum)
491 arrow_used = FALSE;
492 else
493#endif
494 arrow_used = TRUE;
495 restart_edit = 0;
496
497 /*
498 * If the cursor was after the end-of-line before the CTRL-O and it is
499 * now at the end-of-line, put it after the end-of-line (this is not
500 * correct in very rare cases).
501 * Also do this if curswant is greater than the current virtual
502 * column. Eg after "^O$" or "^O80|".
503 */
504 validate_virtcol();
505 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000506 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000507 || curwin->w_curswant > curwin->w_virtcol)
508 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
509 {
510 if (ptr[1] == NUL)
511 ++curwin->w_cursor.col;
512#ifdef FEAT_MBYTE
513 else if (has_mbyte)
514 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000515 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000516 if (ptr[i] == NUL)
517 curwin->w_cursor.col += i;
518 }
519#endif
520 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000521 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000522 }
523 else
524 arrow_used = FALSE;
525
526 /* we are in insert mode now, don't need to start it anymore */
527 need_start_insertmode = FALSE;
528
529 /* Need to save the line for undo before inserting the first char. */
530 ins_need_undo = TRUE;
531
532#ifdef FEAT_MOUSE
533 where_paste_started.lnum = 0;
534#endif
535#ifdef FEAT_CINDENT
536 can_cindent = TRUE;
537#endif
538#ifdef FEAT_FOLDING
539 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
540 * restarting. */
541 if (!p_im && did_restart_edit == 0)
542 foldOpenCursor();
543#endif
544
545 /*
546 * If 'showmode' is set, show the current (insert/replace/..) mode.
547 * A warning message for changing a readonly file is given here, before
548 * actually changing anything. It's put after the mode, if any.
549 */
550 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000551 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000552 i = showmode();
553
554 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000555 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556
557#ifdef CURSOR_SHAPE
558 ui_cursor_shape(); /* may show different cursor shape */
559#endif
560#ifdef FEAT_DIGRAPHS
561 do_digraph(-1); /* clear digraphs */
562#endif
563
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000564 /*
565 * Get the current length of the redo buffer, those characters have to be
566 * skipped if we want to get to the inserted characters.
567 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000568 ptr = get_inserted();
569 if (ptr == NULL)
570 new_insert_skip = 0;
571 else
572 {
573 new_insert_skip = (int)STRLEN(ptr);
574 vim_free(ptr);
575 }
576
577 old_indent = 0;
578
579 /*
580 * Main loop in Insert mode: repeat until Insert mode is left.
581 */
582 for (;;)
583 {
584#ifdef FEAT_RIGHTLEFT
585 if (!revins_legal)
586 revins_scol = -1; /* reset on illegal motions */
587 else
588 revins_legal = 0;
589#endif
590 if (arrow_used) /* don't repeat insert when arrow key used */
591 count = 0;
592
593 if (stop_insert_mode)
594 {
595 /* ":stopinsert" used or 'insertmode' reset */
596 count = 0;
597 goto doESCkey;
598 }
599
600 /* set curwin->w_curswant for next K_DOWN or K_UP */
601 if (!arrow_used)
602 curwin->w_set_curswant = TRUE;
603
604 /* If there is no typeahead may check for timestamps (e.g., for when a
605 * menu invoked a shell command). */
606 if (stuff_empty())
607 {
608 did_check_timestamps = FALSE;
609 if (need_check_timestamps)
610 check_timestamps(FALSE);
611 }
612
613 /*
614 * When emsg() was called msg_scroll will have been set.
615 */
616 msg_scroll = FALSE;
617
618#ifdef FEAT_GUI
619 /* When 'mousefocus' is set a mouse movement may have taken us to
620 * another window. "need_mouse_correct" may then be set because of an
621 * autocommand. */
622 if (need_mouse_correct)
623 gui_mouse_correct();
624#endif
625
626#ifdef FEAT_FOLDING
627 /* Open fold at the cursor line, according to 'foldopen'. */
628 if (fdo_flags & FDO_INSERT)
629 foldOpenCursor();
630 /* Close folds where the cursor isn't, according to 'foldclose' */
631 if (!char_avail())
632 foldCheckClose();
633#endif
634
635 /*
636 * If we inserted a character at the last position of the last line in
637 * the window, scroll the window one line up. This avoids an extra
638 * redraw.
639 * This is detected when the cursor column is smaller after inserting
640 * something.
641 * Don't do this when the topline changed already, it has
642 * already been adjusted (by insertchar() calling open_line())).
643 */
644 if (curbuf->b_mod_set
645 && curwin->w_p_wrap
646 && !did_backspace
647 && curwin->w_topline == old_topline
648#ifdef FEAT_DIFF
649 && curwin->w_topfill == old_topfill
650#endif
651 )
652 {
653 mincol = curwin->w_wcol;
654 validate_cursor_col();
655
656 if ((int)curwin->w_wcol < (int)mincol - curbuf->b_p_ts
657 && curwin->w_wrow == W_WINROW(curwin)
658 + curwin->w_height - 1 - p_so
659 && (curwin->w_cursor.lnum != curwin->w_topline
660#ifdef FEAT_DIFF
661 || curwin->w_topfill > 0
662#endif
663 ))
664 {
665#ifdef FEAT_DIFF
666 if (curwin->w_topfill > 0)
667 --curwin->w_topfill;
668 else
669#endif
670#ifdef FEAT_FOLDING
671 if (hasFolding(curwin->w_topline, NULL, &old_topline))
672 set_topline(curwin, old_topline + 1);
673 else
674#endif
675 set_topline(curwin, curwin->w_topline + 1);
676 }
677 }
678
679 /* May need to adjust w_topline to show the cursor. */
680 update_topline();
681
682 did_backspace = FALSE;
683
684 validate_cursor(); /* may set must_redraw */
685
686 /*
687 * Redraw the display when no characters are waiting.
688 * Also shows mode, ruler and positions cursor.
689 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000690 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000691
692#ifdef FEAT_SCROLLBIND
693 if (curwin->w_p_scb)
694 do_check_scrollbind(TRUE);
695#endif
696
697 update_curswant();
698 old_topline = curwin->w_topline;
699#ifdef FEAT_DIFF
700 old_topfill = curwin->w_topfill;
701#endif
702
703#ifdef USE_ON_FLY_SCROLL
704 dont_scroll = FALSE; /* allow scrolling here */
705#endif
706
707 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000708 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 */
710 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000711 do
712 {
713 c = safe_vgetc();
714 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000715
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000716#ifdef FEAT_AUTOCMD
717 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
718 did_cursorhold = TRUE;
719#endif
720
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721#ifdef FEAT_RIGHTLEFT
722 if (p_hkmap && KeyTyped)
723 c = hkmap(c); /* Hebrew mode mapping */
724#endif
725#ifdef FEAT_FKMAP
726 if (p_fkmap && KeyTyped)
727 c = fkmap(c); /* Farsi mode mapping */
728#endif
729
730#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000731 /*
732 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000733 * and the cursor is still in the completed word. Only when there is
734 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000735 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000736 if (compl_started
737 && pum_wanted()
738 && curwin->w_cursor.col >= compl_col
739 && (compl_shown_match == NULL
740 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000741 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000742 /* BS: Delete one character from "compl_leader". */
743 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000744 && curwin->w_cursor.col > compl_col
745 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000746 continue;
747
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000748 /* When no match was selected or it was edited. */
749 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000750 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000751 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000752 * "compl_leader". Except when at the original match and
753 * there is nothing to add, CTRL-L works like CTRL-P then. */
754 if (c == Ctrl_L
755 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
756 || STRLEN(compl_shown_match->cp_str)
757 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000758 {
759 ins_compl_addfrommatch();
760 continue;
761 }
762
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000763 /* A non-white character that fits in with the current
764 * completion: Add to "compl_leader". */
765 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000766 {
767 ins_compl_addleader(c);
768 continue;
769 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000770
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000771 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000772 * compl_enter_selects is set the Enter key does the same. */
773 if (c == Ctrl_Y || (compl_enter_selects
774 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000775 {
776 ins_compl_delete();
777 ins_compl_insert();
778 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000779 }
780 }
781
Bram Moolenaar071d4272004-06-13 20:20:40 +0000782 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
783 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000784 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000785 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000786 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787#endif
788
Bram Moolenaar488c6512005-08-11 20:09:58 +0000789 /* CTRL-\ CTRL-N goes to Normal mode,
790 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
791 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 if (c == Ctrl_BSL)
793 {
794 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000795 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 ++no_mapping;
797 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000798 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000799 --no_mapping;
800 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000801 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000803 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 vungetc(c);
805 c = Ctrl_BSL;
806 }
807 else if (c == Ctrl_G && p_im)
808 continue;
809 else
810 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000811 if (c == Ctrl_O)
812 {
813 ins_ctrl_o();
814 ins_at_eol = FALSE; /* cursor keeps its column */
815 nomove = TRUE;
816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 count = 0;
818 goto doESCkey;
819 }
820 }
821
822#ifdef FEAT_DIGRAPHS
823 c = do_digraph(c);
824#endif
825
826#ifdef FEAT_INS_EXPAND
827 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
828 goto docomplete;
829#endif
830 if (c == Ctrl_V || c == Ctrl_Q)
831 {
832 ins_ctrl_v();
833 c = Ctrl_V; /* pretend CTRL-V is last typed character */
834 continue;
835 }
836
837#ifdef FEAT_CINDENT
838 if (cindent_on()
839# ifdef FEAT_INS_EXPAND
840 && ctrl_x_mode == 0
841# endif
842 )
843 {
844 /* A key name preceded by a bang means this key is not to be
845 * inserted. Skip ahead to the re-indenting below.
846 * A key name preceded by a star means that indenting has to be
847 * done before inserting the key. */
848 line_is_white = inindent(0);
849 if (in_cinkeys(c, '!', line_is_white))
850 goto force_cindent;
851 if (can_cindent && in_cinkeys(c, '*', line_is_white)
852 && stop_arrow() == OK)
853 do_c_expr_indent();
854 }
855#endif
856
857#ifdef FEAT_RIGHTLEFT
858 if (curwin->w_p_rl)
859 switch (c)
860 {
861 case K_LEFT: c = K_RIGHT; break;
862 case K_S_LEFT: c = K_S_RIGHT; break;
863 case K_C_LEFT: c = K_C_RIGHT; break;
864 case K_RIGHT: c = K_LEFT; break;
865 case K_S_RIGHT: c = K_S_LEFT; break;
866 case K_C_RIGHT: c = K_C_LEFT; break;
867 }
868#endif
869
870#ifdef FEAT_VISUAL
871 /*
872 * If 'keymodel' contains "startsel", may start selection. If it
873 * does, a CTRL-O and c will be stuffed, we need to get these
874 * characters.
875 */
876 if (ins_start_select(c))
877 continue;
878#endif
879
880 /*
881 * The big switch to handle a character in insert mode.
882 */
883 switch (c)
884 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000885 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000886 if (echeck_abbr(ESC + ABBR_OFF))
887 break;
888 /*FALLTHROUGH*/
889
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000890 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000891#ifdef FEAT_CMDWIN
892 if (c == Ctrl_C && cmdwin_type != 0)
893 {
894 /* Close the cmdline window. */
895 cmdwin_result = K_IGNORE;
896 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000897 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000898 goto doESCkey;
899 }
900#endif
901
902#ifdef UNIX
903do_intr:
904#endif
905 /* when 'insertmode' set, and not halfway a mapping, don't leave
906 * Insert mode */
907 if (goto_im())
908 {
909 if (got_int)
910 {
911 (void)vgetc(); /* flush all buffers */
912 got_int = FALSE;
913 }
914 else
915 vim_beep();
916 break;
917 }
918doESCkey:
919 /*
920 * This is the ONLY return from edit()!
921 */
922 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
923 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000924 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000925 o_lnum = curwin->w_cursor.lnum;
926
Bram Moolenaar488c6512005-08-11 20:09:58 +0000927 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000928 {
929#ifdef FEAT_AUTOCMD
930 if (cmdchar != 'r' && cmdchar != 'v')
931 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
932 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000933 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000935 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000937 continue;
938
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000939 case Ctrl_Z: /* suspend when 'insertmode' set */
940 if (!p_im)
941 goto normalchar; /* insert CTRL-Z as normal char */
942 stuffReadbuff((char_u *)":st\r");
943 c = Ctrl_O;
944 /*FALLTHROUGH*/
945
946 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000947#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000948 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000949 goto docomplete;
950#endif
951 if (echeck_abbr(Ctrl_O + ABBR_OFF))
952 break;
953 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000954
955#ifdef FEAT_VIRTUALEDIT
956 /* don't move the cursor left when 'virtualedit' has "onemore". */
957 if (ve_flags & VE_ONEMORE)
958 {
959 ins_at_eol = FALSE;
960 nomove = TRUE;
961 }
962#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000963 count = 0;
964 goto doESCkey;
965
Bram Moolenaar572cb562005-08-05 21:35:02 +0000966 case K_INS: /* toggle insert/replace mode */
967 case K_KINS:
968 ins_insert(replaceState);
969 break;
970
971 case K_SELECT: /* end of Select mode mapping - ignore */
972 break;
973
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000974#ifdef FEAT_SNIFF
975 case K_SNIFF: /* Sniff command received */
976 stuffcharReadbuff(K_SNIFF);
977 goto doESCkey;
978#endif
979
980 case K_HELP: /* Help key works like <ESC> <Help> */
981 case K_F1:
982 case K_XF1:
983 stuffcharReadbuff(K_HELP);
984 if (p_im)
985 need_start_insertmode = TRUE;
986 goto doESCkey;
987
988#ifdef FEAT_NETBEANS_INTG
989 case K_F21: /* NetBeans command */
990 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000991 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000992 --no_mapping;
993 netbeans_keycommand(i);
994 break;
995#endif
996
997 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000998 case NUL:
999 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 /* For ^@ the trailing ESC will end the insert, unless there is an
1001 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001002 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1003 && c != Ctrl_A && !p_im)
1004 goto doESCkey; /* quit insert mode */
1005 inserted_space = FALSE;
1006 break;
1007
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001008 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001009 ins_reg();
1010 auto_format(FALSE, TRUE);
1011 inserted_space = FALSE;
1012 break;
1013
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001014 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001015 ins_ctrl_g();
1016 break;
1017
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001018 case Ctrl_HAT: /* switch input mode and/or langmap */
1019 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001020 break;
1021
1022#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 if (!p_ari)
1025 goto normalchar;
1026 ins_ctrl_();
1027 break;
1028#endif
1029
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001030 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1032 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1033 goto docomplete;
1034#endif
1035 /* FALLTHROUGH */
1036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038# ifdef FEAT_INS_EXPAND
1039 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1040 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001041 if (has_compl_option(FALSE))
1042 goto docomplete;
1043 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044 }
1045# endif
1046 ins_shift(c, lastc);
1047 auto_format(FALSE, TRUE);
1048 inserted_space = FALSE;
1049 break;
1050
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 case K_KDEL:
1053 ins_del();
1054 auto_format(FALSE, TRUE);
1055 break;
1056
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001057 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 case Ctrl_H:
1059 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1060 auto_format(FALSE, TRUE);
1061 break;
1062
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001063 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1065 auto_format(FALSE, TRUE);
1066 break;
1067
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001068 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001069# ifdef FEAT_COMPL_FUNC
1070 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001072 goto docomplete;
1073# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1075 auto_format(FALSE, TRUE);
1076 inserted_space = FALSE;
1077 break;
1078
1079#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001080 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 case K_LEFTMOUSE_NM:
1082 case K_LEFTDRAG:
1083 case K_LEFTRELEASE:
1084 case K_LEFTRELEASE_NM:
1085 case K_MIDDLEMOUSE:
1086 case K_MIDDLEDRAG:
1087 case K_MIDDLERELEASE:
1088 case K_RIGHTMOUSE:
1089 case K_RIGHTDRAG:
1090 case K_RIGHTRELEASE:
1091 case K_X1MOUSE:
1092 case K_X1DRAG:
1093 case K_X1RELEASE:
1094 case K_X2MOUSE:
1095 case K_X2DRAG:
1096 case K_X2RELEASE:
1097 ins_mouse(c);
1098 break;
1099
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001100 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 ins_mousescroll(FALSE);
1102 break;
1103
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001104 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001105 ins_mousescroll(TRUE);
1106 break;
1107#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001108#ifdef FEAT_GUI_TABLINE
1109 case K_TABLINE:
1110 case K_TABMENU:
1111 ins_tabline(c);
1112 break;
1113#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001114
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 break;
1117
Bram Moolenaar754b5602006-02-09 23:53:20 +00001118#ifdef FEAT_AUTOCMD
1119 case K_CURSORHOLD: /* Didn't type something for a while. */
1120 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1121 did_cursorhold = TRUE;
1122 break;
1123#endif
1124
Bram Moolenaar4770d092006-01-12 23:22:24 +00001125#ifdef FEAT_GUI_W32
1126 /* On Win32 ignore <M-F4>, we get it when closing the window was
1127 * cancelled. */
1128 case K_F4:
1129 if (mod_mask != MOD_MASK_ALT)
1130 goto normalchar;
1131 break;
1132#endif
1133
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134#ifdef FEAT_GUI
1135 case K_VER_SCROLLBAR:
1136 ins_scroll();
1137 break;
1138
1139 case K_HOR_SCROLLBAR:
1140 ins_horscroll();
1141 break;
1142#endif
1143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001144 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001145 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001146 case K_S_HOME:
1147 case K_C_HOME:
1148 ins_home(c);
1149 break;
1150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001151 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 case K_S_END:
1154 case K_C_END:
1155 ins_end(c);
1156 break;
1157
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001158 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001159 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1160 ins_s_left();
1161 else
1162 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 break;
1164
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001165 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001166 case K_C_LEFT:
1167 ins_s_left();
1168 break;
1169
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001170 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001171 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1172 ins_s_right();
1173 else
1174 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 break;
1176
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001177 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001178 case K_C_RIGHT:
1179 ins_s_right();
1180 break;
1181
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001182 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001183#ifdef FEAT_INS_EXPAND
1184 if (pum_visible())
1185 goto docomplete;
1186#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001187 if (mod_mask & MOD_MASK_SHIFT)
1188 ins_pageup();
1189 else
1190 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 break;
1192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001193 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001194 case K_PAGEUP:
1195 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001196#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001197 if (pum_visible())
1198 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001199#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 ins_pageup();
1201 break;
1202
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001203 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001204#ifdef FEAT_INS_EXPAND
1205 if (pum_visible())
1206 goto docomplete;
1207#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001208 if (mod_mask & MOD_MASK_SHIFT)
1209 ins_pagedown();
1210 else
1211 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001212 break;
1213
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001214 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001215 case K_PAGEDOWN:
1216 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001217#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001218 if (pum_visible())
1219 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001220#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001221 ins_pagedown();
1222 break;
1223
1224#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001226 ins_drop();
1227 break;
1228#endif
1229
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001230 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001231 c = TAB;
1232 /* FALLTHROUGH */
1233
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001234 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1236 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1237 goto docomplete;
1238#endif
1239 inserted_space = FALSE;
1240 if (ins_tab())
1241 goto normalchar; /* insert TAB as a normal char */
1242 auto_format(FALSE, TRUE);
1243 break;
1244
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001245 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001246 c = CAR;
1247 /* FALLTHROUGH */
1248 case CAR:
1249 case NL:
1250#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1251 /* In a quickfix window a <CR> jumps to the error under the
1252 * cursor. */
1253 if (bt_quickfix(curbuf) && c == CAR)
1254 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001255 if (curwin->w_llist_ref == NULL) /* quickfix window */
1256 do_cmdline_cmd((char_u *)".cc");
1257 else /* location list window */
1258 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001259 break;
1260 }
1261#endif
1262#ifdef FEAT_CMDWIN
1263 if (cmdwin_type != 0)
1264 {
1265 /* Execute the command in the cmdline window. */
1266 cmdwin_result = CAR;
1267 goto doESCkey;
1268 }
1269#endif
1270 if (ins_eol(c) && !p_im)
1271 goto doESCkey; /* out of memory */
1272 auto_format(FALSE, FALSE);
1273 inserted_space = FALSE;
1274 break;
1275
1276#if defined(FEAT_DIGRAPHS) || defined (FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001277 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278# ifdef FEAT_INS_EXPAND
1279 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1280 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001281 if (has_compl_option(TRUE))
1282 goto docomplete;
1283 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284 }
1285# endif
1286# ifdef FEAT_DIGRAPHS
1287 c = ins_digraph();
1288 if (c == NUL)
1289 break;
1290# endif
1291 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001293
1294#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001295 case Ctrl_X: /* Enter CTRL-X mode */
1296 ins_ctrl_x();
1297 break;
1298
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300 if (ctrl_x_mode != CTRL_X_TAGS)
1301 goto normalchar;
1302 goto docomplete;
1303
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001304 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001305 if (ctrl_x_mode != CTRL_X_FILES)
1306 goto normalchar;
1307 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001308
1309 case 's': /* Spelling completion after ^X */
1310 case Ctrl_S:
1311 if (ctrl_x_mode != CTRL_X_SPELL)
1312 goto normalchar;
1313 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001314#endif
1315
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001316 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001317#ifdef FEAT_INS_EXPAND
1318 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1319#endif
1320 {
1321 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1322 if (p_im)
1323 {
1324 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1325 break;
1326 goto doESCkey;
1327 }
1328 goto normalchar;
1329 }
1330#ifdef FEAT_INS_EXPAND
1331 /* FALLTHROUGH */
1332
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001333 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001334 case Ctrl_N:
1335 /* if 'complete' is empty then plain ^P is no longer special,
1336 * but it is under other ^X modes */
1337 if (*curbuf->b_p_cpt == NUL
1338 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340 goto normalchar;
1341
1342docomplete:
1343 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001344 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 break;
1346#endif /* FEAT_INS_EXPAND */
1347
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001348 case Ctrl_Y: /* copy from previous line or scroll down */
1349 case Ctrl_E: /* copy from next line or scroll up */
1350 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 break;
1352
1353 default:
1354#ifdef UNIX
1355 if (c == intr_char) /* special interrupt char */
1356 goto do_intr;
1357#endif
1358
1359 /*
1360 * Insert a nomal character.
1361 */
1362normalchar:
1363#ifdef FEAT_SMARTINDENT
1364 /* Try to perform smart-indenting. */
1365 ins_try_si(c);
1366#endif
1367
1368 if (c == ' ')
1369 {
1370 inserted_space = TRUE;
1371#ifdef FEAT_CINDENT
1372 if (inindent(0))
1373 can_cindent = FALSE;
1374#endif
1375 if (Insstart_blank_vcol == MAXCOL
1376 && curwin->w_cursor.lnum == Insstart.lnum)
1377 Insstart_blank_vcol = get_nolist_virtcol();
1378 }
1379
1380 if (vim_iswordc(c) || !echeck_abbr(
1381#ifdef FEAT_MBYTE
1382 /* Add ABBR_OFF for characters above 0x100, this is
1383 * what check_abbr() expects. */
1384 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1385#endif
1386 c))
1387 {
1388 insert_special(c, FALSE, FALSE);
1389#ifdef FEAT_RIGHTLEFT
1390 revins_legal++;
1391 revins_chars++;
1392#endif
1393 }
1394
1395 auto_format(FALSE, TRUE);
1396
1397#ifdef FEAT_FOLDING
1398 /* When inserting a character the cursor line must never be in a
1399 * closed fold. */
1400 foldOpenCursor();
1401#endif
1402 break;
1403 } /* end of switch (c) */
1404
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001405#ifdef FEAT_AUTOCMD
1406 /* If typed something may trigger CursorHoldI again. */
1407 if (c != K_CURSORHOLD)
1408 did_cursorhold = FALSE;
1409#endif
1410
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 /* If the cursor was moved we didn't just insert a space */
1412 if (arrow_used)
1413 inserted_space = FALSE;
1414
1415#ifdef FEAT_CINDENT
1416 if (can_cindent && cindent_on()
1417# ifdef FEAT_INS_EXPAND
1418 && ctrl_x_mode == 0
1419# endif
1420 )
1421 {
1422force_cindent:
1423 /*
1424 * Indent now if a key was typed that is in 'cinkeys'.
1425 */
1426 if (in_cinkeys(c, ' ', line_is_white))
1427 {
1428 if (stop_arrow() == OK)
1429 /* re-indent the current line */
1430 do_c_expr_indent();
1431 }
1432 }
1433#endif /* FEAT_CINDENT */
1434
1435 } /* for (;;) */
1436 /* NOTREACHED */
1437}
1438
1439/*
1440 * Redraw for Insert mode.
1441 * This is postponed until getting the next character to make '$' in the 'cpo'
1442 * option work correctly.
1443 * Only redraw when there are no characters available. This speeds up
1444 * inserting sequences of characters (e.g., for CTRL-R).
1445 */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001446/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001448ins_redraw(ready)
1449 int ready; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450{
1451 if (!char_avail())
1452 {
Bram Moolenaar754b5602006-02-09 23:53:20 +00001453#ifdef FEAT_AUTOCMD
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001454 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1455 * visible, the command might delete it. */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001456 if (ready && has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001457 && !equalpos(last_cursormoved, curwin->w_cursor)
1458# ifdef FEAT_INS_EXPAND
1459 && !pum_visible()
1460# endif
1461 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001462 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001463# ifdef FEAT_SYN_HL
1464 /* Need to update the screen first, to make sure syntax
1465 * highlighting is correct after making a change (e.g., inserting
1466 * a "(". The autocommand may also require a redraw, so it's done
1467 * again below, unfortunately. */
1468 if (syntax_present(curbuf) && must_redraw)
1469 update_screen(0);
1470# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001471 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1472 last_cursormoved = curwin->w_cursor;
1473 }
1474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001475 if (must_redraw)
1476 update_screen(0);
1477 else if (clear_cmdline || redraw_cmdline)
1478 showmode(); /* clear cmdline and show mode */
1479 showruler(FALSE);
1480 setcursor();
1481 emsg_on_display = FALSE; /* may remove error message now */
1482 }
1483}
1484
1485/*
1486 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1487 */
1488 static void
1489ins_ctrl_v()
1490{
1491 int c;
1492
1493 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001494 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
1496 if (redrawing() && !char_avail())
1497 edit_putchar('^', TRUE);
1498 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1499
1500#ifdef FEAT_CMDL_INFO
1501 add_to_showcmd_c(Ctrl_V);
1502#endif
1503
1504 c = get_literal();
1505#ifdef FEAT_CMDL_INFO
1506 clear_showcmd();
1507#endif
1508 insert_special(c, FALSE, TRUE);
1509#ifdef FEAT_RIGHTLEFT
1510 revins_chars++;
1511 revins_legal++;
1512#endif
1513}
1514
1515/*
1516 * Put a character directly onto the screen. It's not stored in a buffer.
1517 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1518 */
1519static int pc_status;
1520#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1521#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1522#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1523#define PC_STATUS_SET 3 /* pc_bytes was filled */
1524#ifdef FEAT_MBYTE
1525static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1526#else
1527static char_u pc_bytes[2]; /* saved bytes */
1528#endif
1529static int pc_attr;
1530static int pc_row;
1531static int pc_col;
1532
1533 void
1534edit_putchar(c, highlight)
1535 int c;
1536 int highlight;
1537{
1538 int attr;
1539
1540 if (ScreenLines != NULL)
1541 {
1542 update_topline(); /* just in case w_topline isn't valid */
1543 validate_cursor();
1544 if (highlight)
1545 attr = hl_attr(HLF_8);
1546 else
1547 attr = 0;
1548 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1549 pc_col = W_WINCOL(curwin);
1550#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1551 pc_status = PC_STATUS_UNSET;
1552#endif
1553#ifdef FEAT_RIGHTLEFT
1554 if (curwin->w_p_rl)
1555 {
1556 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1557# ifdef FEAT_MBYTE
1558 if (has_mbyte)
1559 {
1560 int fix_col = mb_fix_col(pc_col, pc_row);
1561
1562 if (fix_col != pc_col)
1563 {
1564 screen_putchar(' ', pc_row, fix_col, attr);
1565 --curwin->w_wcol;
1566 pc_status = PC_STATUS_RIGHT;
1567 }
1568 }
1569# endif
1570 }
1571 else
1572#endif
1573 {
1574 pc_col += curwin->w_wcol;
1575#ifdef FEAT_MBYTE
1576 if (mb_lefthalve(pc_row, pc_col))
1577 pc_status = PC_STATUS_LEFT;
1578#endif
1579 }
1580
1581 /* save the character to be able to put it back */
1582#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1583 if (pc_status == PC_STATUS_UNSET)
1584#endif
1585 {
1586 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1587 pc_status = PC_STATUS_SET;
1588 }
1589 screen_putchar(c, pc_row, pc_col, attr);
1590 }
1591}
1592
1593/*
1594 * Undo the previous edit_putchar().
1595 */
1596 void
1597edit_unputchar()
1598{
1599 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1600 {
1601#if defined(FEAT_MBYTE)
1602 if (pc_status == PC_STATUS_RIGHT)
1603 ++curwin->w_wcol;
1604 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1605 redrawWinline(curwin->w_cursor.lnum, FALSE);
1606 else
1607#endif
1608 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1609 }
1610}
1611
1612/*
1613 * Called when p_dollar is set: display a '$' at the end of the changed text
1614 * Only works when cursor is in the line that changes.
1615 */
1616 void
1617display_dollar(col)
1618 colnr_T col;
1619{
1620 colnr_T save_col;
1621
1622 if (!redrawing())
1623 return;
1624
1625 cursor_off();
1626 save_col = curwin->w_cursor.col;
1627 curwin->w_cursor.col = col;
1628#ifdef FEAT_MBYTE
1629 if (has_mbyte)
1630 {
1631 char_u *p;
1632
1633 /* If on the last byte of a multi-byte move to the first byte. */
1634 p = ml_get_curline();
1635 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1636 }
1637#endif
1638 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1639 if (curwin->w_wcol < W_WIDTH(curwin))
1640 {
1641 edit_putchar('$', FALSE);
1642 dollar_vcol = curwin->w_virtcol;
1643 }
1644 curwin->w_cursor.col = save_col;
1645}
1646
1647/*
1648 * Call this function before moving the cursor from the normal insert position
1649 * in insert mode.
1650 */
1651 static void
1652undisplay_dollar()
1653{
1654 if (dollar_vcol)
1655 {
1656 dollar_vcol = 0;
1657 redrawWinline(curwin->w_cursor.lnum, FALSE);
1658 }
1659}
1660
1661/*
1662 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1663 * Keep the cursor on the same character.
1664 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1665 * type == INDENT_DEC decrease indent (for CTRL-D)
1666 * type == INDENT_SET set indent to "amount"
1667 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1668 */
1669 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001670change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 int type;
1672 int amount;
1673 int round;
1674 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001675 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001676{
1677 int vcol;
1678 int last_vcol;
1679 int insstart_less; /* reduction for Insstart.col */
1680 int new_cursor_col;
1681 int i;
1682 char_u *ptr;
1683 int save_p_list;
1684 int start_col;
1685 colnr_T vc;
1686#ifdef FEAT_VREPLACE
1687 colnr_T orig_col = 0; /* init for GCC */
1688 char_u *new_line, *orig_line = NULL; /* init for GCC */
1689
1690 /* VREPLACE mode needs to know what the line was like before changing */
1691 if (State & VREPLACE_FLAG)
1692 {
1693 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1694 orig_col = curwin->w_cursor.col;
1695 }
1696#endif
1697
1698 /* for the following tricks we don't want list mode */
1699 save_p_list = curwin->w_p_list;
1700 curwin->w_p_list = FALSE;
1701 vc = getvcol_nolist(&curwin->w_cursor);
1702 vcol = vc;
1703
1704 /*
1705 * For Replace mode we need to fix the replace stack later, which is only
1706 * possible when the cursor is in the indent. Remember the number of
1707 * characters before the cursor if it's possible.
1708 */
1709 start_col = curwin->w_cursor.col;
1710
1711 /* determine offset from first non-blank */
1712 new_cursor_col = curwin->w_cursor.col;
1713 beginline(BL_WHITE);
1714 new_cursor_col -= curwin->w_cursor.col;
1715
1716 insstart_less = curwin->w_cursor.col;
1717
1718 /*
1719 * If the cursor is in the indent, compute how many screen columns the
1720 * cursor is to the left of the first non-blank.
1721 */
1722 if (new_cursor_col < 0)
1723 vcol = get_indent() - vcol;
1724
1725 if (new_cursor_col > 0) /* can't fix replace stack */
1726 start_col = -1;
1727
1728 /*
1729 * Set the new indent. The cursor will be put on the first non-blank.
1730 */
1731 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001732 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001733 else
1734 {
1735#ifdef FEAT_VREPLACE
1736 int save_State = State;
1737
1738 /* Avoid being called recursively. */
1739 if (State & VREPLACE_FLAG)
1740 State = INSERT;
1741#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001742 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001743#ifdef FEAT_VREPLACE
1744 State = save_State;
1745#endif
1746 }
1747 insstart_less -= curwin->w_cursor.col;
1748
1749 /*
1750 * Try to put cursor on same character.
1751 * If the cursor is at or after the first non-blank in the line,
1752 * compute the cursor column relative to the column of the first
1753 * non-blank character.
1754 * If we are not in insert mode, leave the cursor on the first non-blank.
1755 * If the cursor is before the first non-blank, position it relative
1756 * to the first non-blank, counted in screen columns.
1757 */
1758 if (new_cursor_col >= 0)
1759 {
1760 /*
1761 * When changing the indent while the cursor is touching it, reset
1762 * Insstart_col to 0.
1763 */
1764 if (new_cursor_col == 0)
1765 insstart_less = MAXCOL;
1766 new_cursor_col += curwin->w_cursor.col;
1767 }
1768 else if (!(State & INSERT))
1769 new_cursor_col = curwin->w_cursor.col;
1770 else
1771 {
1772 /*
1773 * Compute the screen column where the cursor should be.
1774 */
1775 vcol = get_indent() - vcol;
1776 curwin->w_virtcol = (vcol < 0) ? 0 : vcol;
1777
1778 /*
1779 * Advance the cursor until we reach the right screen column.
1780 */
1781 vcol = last_vcol = 0;
1782 new_cursor_col = -1;
1783 ptr = ml_get_curline();
1784 while (vcol <= (int)curwin->w_virtcol)
1785 {
1786 last_vcol = vcol;
1787#ifdef FEAT_MBYTE
1788 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001789 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 else
1791#endif
1792 ++new_cursor_col;
1793 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1794 }
1795 vcol = last_vcol;
1796
1797 /*
1798 * May need to insert spaces to be able to position the cursor on
1799 * the right screen column.
1800 */
1801 if (vcol != (int)curwin->w_virtcol)
1802 {
1803 curwin->w_cursor.col = new_cursor_col;
1804 i = (int)curwin->w_virtcol - vcol;
1805 ptr = alloc(i + 1);
1806 if (ptr != NULL)
1807 {
1808 new_cursor_col += i;
1809 ptr[i] = NUL;
1810 while (--i >= 0)
1811 ptr[i] = ' ';
1812 ins_str(ptr);
1813 vim_free(ptr);
1814 }
1815 }
1816
1817 /*
1818 * When changing the indent while the cursor is in it, reset
1819 * Insstart_col to 0.
1820 */
1821 insstart_less = MAXCOL;
1822 }
1823
1824 curwin->w_p_list = save_p_list;
1825
1826 if (new_cursor_col <= 0)
1827 curwin->w_cursor.col = 0;
1828 else
1829 curwin->w_cursor.col = new_cursor_col;
1830 curwin->w_set_curswant = TRUE;
1831 changed_cline_bef_curs();
1832
1833 /*
1834 * May have to adjust the start of the insert.
1835 */
1836 if (State & INSERT)
1837 {
1838 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1839 {
1840 if ((int)Insstart.col <= insstart_less)
1841 Insstart.col = 0;
1842 else
1843 Insstart.col -= insstart_less;
1844 }
1845 if ((int)ai_col <= insstart_less)
1846 ai_col = 0;
1847 else
1848 ai_col -= insstart_less;
1849 }
1850
1851 /*
1852 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1853 * If the number of characters before the cursor decreased, need to pop a
1854 * few characters from the replace stack.
1855 * If the number of characters before the cursor increased, need to push a
1856 * few NULs onto the replace stack.
1857 */
1858 if (REPLACE_NORMAL(State) && start_col >= 0)
1859 {
1860 while (start_col > (int)curwin->w_cursor.col)
1861 {
1862 replace_join(0); /* remove a NUL from the replace stack */
1863 --start_col;
1864 }
1865 while (start_col < (int)curwin->w_cursor.col || replaced)
1866 {
1867 replace_push(NUL);
1868 if (replaced)
1869 {
1870 replace_push(replaced);
1871 replaced = NUL;
1872 }
1873 ++start_col;
1874 }
1875 }
1876
1877#ifdef FEAT_VREPLACE
1878 /*
1879 * For VREPLACE mode, we also have to fix the replace stack. In this case
1880 * it is always possible because we backspace over the whole line and then
1881 * put it back again the way we wanted it.
1882 */
1883 if (State & VREPLACE_FLAG)
1884 {
1885 /* If orig_line didn't allocate, just return. At least we did the job,
1886 * even if you can't backspace. */
1887 if (orig_line == NULL)
1888 return;
1889
1890 /* Save new line */
1891 new_line = vim_strsave(ml_get_curline());
1892 if (new_line == NULL)
1893 return;
1894
1895 /* We only put back the new line up to the cursor */
1896 new_line[curwin->w_cursor.col] = NUL;
1897
1898 /* Put back original line */
1899 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1900 curwin->w_cursor.col = orig_col;
1901
1902 /* Backspace from cursor to start of line */
1903 backspace_until_column(0);
1904
1905 /* Insert new stuff into line again */
1906 ins_bytes(new_line);
1907
1908 vim_free(new_line);
1909 }
1910#endif
1911}
1912
1913/*
1914 * Truncate the space at the end of a line. This is to be used only in an
1915 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1916 * modes.
1917 */
1918 void
1919truncate_spaces(line)
1920 char_u *line;
1921{
1922 int i;
1923
1924 /* find start of trailing white space */
1925 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1926 {
1927 if (State & REPLACE_FLAG)
1928 replace_join(0); /* remove a NUL from the replace stack */
1929 }
1930 line[i + 1] = NUL;
1931}
1932
1933#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1934 || defined(FEAT_COMMENTS) || defined(PROTO)
1935/*
1936 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1937 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001938 * Will attempt not to go before "col" even when there is a composing
1939 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 */
1941 void
1942backspace_until_column(col)
1943 int col;
1944{
1945 while ((int)curwin->w_cursor.col > col)
1946 {
1947 curwin->w_cursor.col--;
1948 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001949 replace_do_bs(col);
1950 else if (!del_char_after_col(col))
1951 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 }
1953}
1954#endif
1955
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001956/*
1957 * Like del_char(), but make sure not to go before column "limit_col".
1958 * Only matters when there are composing characters.
1959 * Return TRUE when something was deleted.
1960 */
1961 static int
1962del_char_after_col(limit_col)
1963 int limit_col;
1964{
1965#ifdef FEAT_MBYTE
1966 if (enc_utf8 && limit_col >= 0)
1967 {
1968 int ecol = curwin->w_cursor.col + 1;
1969
1970 /* Make sure the cursor is at the start of a character, but
1971 * skip forward again when going too far back because of a
1972 * composing character. */
1973 mb_adjust_cursor();
1974 while (curwin->w_cursor.col < limit_col)
1975 {
1976 int l = utf_ptr2len(ml_get_cursor());
1977
1978 if (l == 0) /* end of line */
1979 break;
1980 curwin->w_cursor.col += l;
1981 }
1982 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1983 return FALSE;
1984 del_bytes((long)(ecol - curwin->w_cursor.col), FALSE, TRUE);
1985 }
1986 else
1987#endif
1988 (void)del_char(FALSE);
1989 return TRUE;
1990}
1991
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1993/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001994 * CTRL-X pressed in Insert mode.
1995 */
1996 static void
1997ins_ctrl_x()
1998{
1999 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2000 * CTRL-V works like CTRL-N */
2001 if (ctrl_x_mode != CTRL_X_CMDLINE)
2002 {
2003 /* if the next ^X<> won't ADD nothing, then reset
2004 * compl_cont_status */
2005 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002006 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002007 else
2008 compl_cont_status = 0;
2009 /* We're not sure which CTRL-X mode it will be yet */
2010 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2011 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2012 edit_submode_pre = NULL;
2013 showmode();
2014 }
2015}
2016
2017/*
2018 * Return TRUE if the 'dict' or 'tsr' option can be used.
2019 */
2020 static int
2021has_compl_option(dict_opt)
2022 int dict_opt;
2023{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002024 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002025# ifdef FEAT_SPELL
2026 && !curwin->w_p_spell
2027# endif
2028 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002029 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2030 {
2031 ctrl_x_mode = 0;
2032 edit_submode = NULL;
2033 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2034 : (char_u *)_("'thesaurus' option is empty"),
2035 hl_attr(HLF_E));
2036 if (emsg_silent == 0)
2037 {
2038 vim_beep();
2039 setcursor();
2040 out_flush();
2041 ui_delay(2000L, FALSE);
2042 }
2043 return FALSE;
2044 }
2045 return TRUE;
2046}
2047
2048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002049 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2050 * This depends on the current mode.
2051 */
2052 int
2053vim_is_ctrl_x_key(c)
2054 int c;
2055{
2056 /* Always allow ^R - let it's results then be checked */
2057 if (c == Ctrl_R)
2058 return TRUE;
2059
Bram Moolenaare3226be2005-12-18 22:10:00 +00002060 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002061 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002062 return TRUE;
2063
Bram Moolenaar071d4272004-06-13 20:20:40 +00002064 switch (ctrl_x_mode)
2065 {
2066 case 0: /* Not in any CTRL-X mode */
2067 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2068 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002069 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002070 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2071 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2072 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002073 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2074 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002075 case CTRL_X_SCROLL:
2076 return (c == Ctrl_Y || c == Ctrl_E);
2077 case CTRL_X_WHOLE_LINE:
2078 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2079 case CTRL_X_FILES:
2080 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2081 case CTRL_X_DICTIONARY:
2082 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2083 case CTRL_X_THESAURUS:
2084 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2085 case CTRL_X_TAGS:
2086 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2087#ifdef FEAT_FIND_ID
2088 case CTRL_X_PATH_PATTERNS:
2089 return (c == Ctrl_P || c == Ctrl_N);
2090 case CTRL_X_PATH_DEFINES:
2091 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2092#endif
2093 case CTRL_X_CMDLINE:
2094 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2095 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002096#ifdef FEAT_COMPL_FUNC
2097 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002098 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002099 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002100 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002101#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002102 case CTRL_X_SPELL:
2103 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002104 }
2105 EMSG(_(e_internal));
2106 return FALSE;
2107}
2108
2109/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002110 * Return TRUE when character "c" is part of the item currently being
2111 * completed. Used to decide whether to abandon complete mode when the menu
2112 * is visible.
2113 */
2114 static int
2115ins_compl_accept_char(c)
2116 int c;
2117{
2118 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2119 /* When expanding an identifier only accept identifier chars. */
2120 return vim_isIDc(c);
2121
2122 switch (ctrl_x_mode)
2123 {
2124 case CTRL_X_FILES:
2125 /* When expanding file name only accept file name chars. But not
2126 * path separators, so that "proto/<Tab>" expands files in
2127 * "proto", not "proto/" as a whole */
2128 return vim_isfilec(c) && !vim_ispathsep(c);
2129
2130 case CTRL_X_CMDLINE:
2131 case CTRL_X_OMNI:
2132 /* Command line and Omni completion can work with just about any
2133 * printable character, but do stop at white space. */
2134 return vim_isprintc(c) && !vim_iswhite(c);
2135
2136 case CTRL_X_WHOLE_LINE:
2137 /* For while line completion a space can be part of the line. */
2138 return vim_isprintc(c);
2139 }
2140 return vim_iswordc(c);
2141}
2142
2143/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002144 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002145 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002146 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002147 * the rest of the word to be in -- webb
2148 */
2149 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002150ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 char_u *str;
2152 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002153 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002154 char_u *fname;
2155 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002156 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002157{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002158 char_u *p;
2159 int i, c;
2160 int actual_len; /* Take multi-byte characters */
2161 int actual_compl_length; /* into account. */
2162 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002163 int has_lower = FALSE;
2164 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002165
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002166 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002167 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002168 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169
Bram Moolenaara2993e12007-08-12 14:38:46 +00002170 /* Find actual length of completion. */
2171#ifdef FEAT_MBYTE
2172 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002173 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002174 p = str;
2175 actual_len = 0;
2176 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002177 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002178 mb_ptr_adv(p);
2179 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002180 }
2181 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002182 else
2183#endif
2184 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002185
Bram Moolenaara2993e12007-08-12 14:38:46 +00002186 /* Find actual length of original text. */
2187#ifdef FEAT_MBYTE
2188 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002189 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002190 p = compl_orig_text;
2191 actual_compl_length = 0;
2192 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002193 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002194 mb_ptr_adv(p);
2195 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002196 }
2197 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002198 else
2199#endif
2200 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201
Bram Moolenaara2993e12007-08-12 14:38:46 +00002202 /* Allocate wide character array for the completion and fill it. */
2203 wca = (int *)alloc(actual_len * sizeof(int));
2204 if (wca != NULL)
2205 {
2206 p = str;
2207 for (i = 0; i < actual_len; ++i)
2208#ifdef FEAT_MBYTE
2209 if (has_mbyte)
2210 wca[i] = mb_ptr2char_adv(&p);
2211 else
2212#endif
2213 wca[i] = *(p++);
2214
2215 /* Rule 1: Were any chars converted to lower? */
2216 p = compl_orig_text;
2217 for (i = 0; i < actual_compl_length; ++i)
2218 {
2219#ifdef FEAT_MBYTE
2220 if (has_mbyte)
2221 c = mb_ptr2char_adv(&p);
2222 else
2223#endif
2224 c = *(p++);
2225 if (MB_ISLOWER(c))
2226 {
2227 has_lower = TRUE;
2228 if (MB_ISUPPER(wca[i]))
2229 {
2230 /* Rule 1 is satisfied. */
2231 for (i = actual_compl_length; i < actual_len; ++i)
2232 wca[i] = MB_TOLOWER(wca[i]);
2233 break;
2234 }
2235 }
2236 }
2237
2238 /*
2239 * Rule 2: No lower case, 2nd consecutive letter converted to
2240 * upper case.
2241 */
2242 if (!has_lower)
2243 {
2244 p = compl_orig_text;
2245 for (i = 0; i < actual_compl_length; ++i)
2246 {
2247#ifdef FEAT_MBYTE
2248 if (has_mbyte)
2249 c = mb_ptr2char_adv(&p);
2250 else
2251#endif
2252 c = *(p++);
2253 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2254 {
2255 /* Rule 2 is satisfied. */
2256 for (i = actual_compl_length; i < actual_len; ++i)
2257 wca[i] = MB_TOUPPER(wca[i]);
2258 break;
2259 }
2260 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2261 }
2262 }
2263
2264 /* Copy the original case of the part we typed. */
2265 p = compl_orig_text;
2266 for (i = 0; i < actual_compl_length; ++i)
2267 {
2268#ifdef FEAT_MBYTE
2269 if (has_mbyte)
2270 c = mb_ptr2char_adv(&p);
2271 else
2272#endif
2273 c = *(p++);
2274 if (MB_ISLOWER(c))
2275 wca[i] = MB_TOLOWER(wca[i]);
2276 else if (MB_ISUPPER(c))
2277 wca[i] = MB_TOUPPER(wca[i]);
2278 }
2279
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002280 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002281 * Generate encoding specific output from wide character array.
2282 * Multi-byte characters can occupy up to five bytes more than
2283 * ASCII characters, and we also need one byte for NUL, so stay
2284 * six bytes away from the edge of IObuff.
2285 */
2286 p = IObuff;
2287 i = 0;
2288 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2289#ifdef FEAT_MBYTE
2290 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002291 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002292 else
2293#endif
2294 *(p++) = wca[i++];
2295 *p = NUL;
2296
2297 vim_free(wca);
2298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002299
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002300 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2301 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002302 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002303 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002304}
2305
2306/*
2307 * Add a match to the list of matches.
2308 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002309 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002310 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002311 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002312 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002313ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002314 char_u *str;
2315 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002316 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002317 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002318 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002319 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002320 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002321 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002322{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002323 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002324 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002325
2326 ui_breakcheck();
2327 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002328 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002329 if (len < 0)
2330 len = (int)STRLEN(str);
2331
2332 /*
2333 * If the same match is already present, don't add it.
2334 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002335 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002336 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002337 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002338 do
2339 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002340 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002341 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002342 && match->cp_str[len] == NUL)
2343 return NOTDONE;
2344 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002345 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 }
2347
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002348 /* Remove any popup menu before changing the list of matches. */
2349 ins_compl_del_pum();
2350
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 /*
2352 * Allocate a new match structure.
2353 * Copy the values to the new match structure.
2354 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002355 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002356 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002357 return FAIL;
2358 match->cp_number = -1;
2359 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002360 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002361 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002362 {
2363 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002365 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002366 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002367
Bram Moolenaar071d4272004-06-13 20:20:40 +00002368 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002369 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2371 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002372 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002373 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002374 && compl_curr_match->cp_fname != NULL
2375 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002376 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002377 else if (fname != NULL)
2378 {
2379 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002380 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002381 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002383 match->cp_fname = NULL;
2384 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002385
2386 if (cptext != NULL)
2387 {
2388 int i;
2389
2390 for (i = 0; i < CPT_COUNT; ++i)
2391 if (cptext[i] != NULL && *cptext[i] != NUL)
2392 match->cp_text[i] = vim_strsave(cptext[i]);
2393 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394
2395 /*
2396 * Link the new match structure in the list of matches.
2397 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002398 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002399 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 else if (dir == FORWARD)
2401 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002402 match->cp_next = compl_curr_match->cp_next;
2403 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 }
2405 else /* BACKWARD */
2406 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002407 match->cp_next = compl_curr_match;
2408 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002410 if (match->cp_next)
2411 match->cp_next->cp_prev = match;
2412 if (match->cp_prev)
2413 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002415 compl_first_match = match;
2416 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002418 /*
2419 * Find the longest common string if still doing that.
2420 */
2421 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2422 ins_compl_longest_match(match);
2423
Bram Moolenaar071d4272004-06-13 20:20:40 +00002424 return OK;
2425}
2426
2427/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002428 * Return TRUE if "str[len]" matches with match->cp_str, considering
2429 * match->cp_icase.
2430 */
2431 static int
2432ins_compl_equal(match, str, len)
2433 compl_T *match;
2434 char_u *str;
2435 int len;
2436{
2437 if (match->cp_icase)
2438 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2439 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2440}
2441
2442/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002443 * Reduce the longest common string for match "match".
2444 */
2445 static void
2446ins_compl_longest_match(match)
2447 compl_T *match;
2448{
2449 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002450 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002451 int had_match;
2452
2453 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002454 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002455 /* First match, use it as a whole. */
2456 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002457 if (compl_leader != NULL)
2458 {
2459 had_match = (curwin->w_cursor.col > compl_col);
2460 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002461 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002462 ins_redraw(FALSE);
2463
2464 /* When the match isn't there (to avoid matching itself) remove it
2465 * again after redrawing. */
2466 if (!had_match)
2467 ins_compl_delete();
2468 compl_used_match = FALSE;
2469 }
2470 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002471 else
2472 {
2473 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002474 p = compl_leader;
2475 s = match->cp_str;
2476 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002477 {
2478#ifdef FEAT_MBYTE
2479 if (has_mbyte)
2480 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002481 c1 = mb_ptr2char(p);
2482 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002483 }
2484 else
2485#endif
2486 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002487 c1 = *p;
2488 c2 = *s;
2489 }
2490 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2491 : (c1 != c2))
2492 break;
2493#ifdef FEAT_MBYTE
2494 if (has_mbyte)
2495 {
2496 mb_ptr_adv(p);
2497 mb_ptr_adv(s);
2498 }
2499 else
2500#endif
2501 {
2502 ++p;
2503 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002504 }
2505 }
2506
2507 if (*p != NUL)
2508 {
2509 /* Leader was shortened, need to change the inserted text. */
2510 *p = NUL;
2511 had_match = (curwin->w_cursor.col > compl_col);
2512 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002513 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002514 ins_redraw(FALSE);
2515
2516 /* When the match isn't there (to avoid matching itself) remove it
2517 * again after redrawing. */
2518 if (!had_match)
2519 ins_compl_delete();
2520 }
2521
2522 compl_used_match = FALSE;
2523 }
2524}
2525
2526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527 * Add an array of matches to the list of matches.
2528 * Frees matches[].
2529 */
2530 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002531ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 int num_matches;
2533 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002535{
2536 int i;
2537 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002538 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002539
Bram Moolenaar572cb562005-08-05 21:35:02 +00002540 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002541 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002542 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002543 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002544 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 FreeWild(num_matches, matches);
2546}
2547
2548/* Make the completion list cyclic.
2549 * Return the number of matches (excluding the original).
2550 */
2551 static int
2552ins_compl_make_cyclic()
2553{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002554 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002555 int count = 0;
2556
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002557 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558 {
2559 /*
2560 * Find the end of the list.
2561 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002562 match = compl_first_match;
2563 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002564 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002565 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002566 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002567 ++count;
2568 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002569 match->cp_next = compl_first_match;
2570 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002571 }
2572 return count;
2573}
2574
Bram Moolenaara94bc432006-03-10 21:42:59 +00002575/*
2576 * Start completion for the complete() function.
2577 * "startcol" is where the matched text starts (1 is first column).
2578 * "list" is the list of matches.
2579 */
2580 void
2581set_completion(startcol, list)
2582 int startcol;
2583 list_T *list;
2584{
2585 /* If already doing completions stop it. */
2586 if (ctrl_x_mode != 0)
2587 ins_compl_prep(' ');
2588 ins_compl_clear();
2589
2590 if (stop_arrow() == FAIL)
2591 return;
2592
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002593 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002594 startcol = curwin->w_cursor.col;
2595 compl_col = startcol;
2596 compl_length = curwin->w_cursor.col - startcol;
2597 /* compl_pattern doesn't need to be set */
2598 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2599 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002600 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002601 return;
2602
2603 /* Handle like dictionary completion. */
2604 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2605
2606 ins_compl_add_list(list);
2607 compl_matches = ins_compl_make_cyclic();
2608 compl_started = TRUE;
2609 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002610 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002611
2612 compl_curr_match = compl_first_match;
2613 ins_complete(Ctrl_N);
2614 out_flush();
2615}
2616
2617
Bram Moolenaar9372a112005-12-06 19:59:18 +00002618/* "compl_match_array" points the currently displayed list of entries in the
2619 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002620static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002621static int compl_match_arraysize;
2622
2623/*
2624 * Update the screen and when there is any scrolling remove the popup menu.
2625 */
2626 static void
2627ins_compl_upd_pum()
2628{
2629 int h;
2630
2631 if (compl_match_array != NULL)
2632 {
2633 h = curwin->w_cline_height;
2634 update_screen(0);
2635 if (h != curwin->w_cline_height)
2636 ins_compl_del_pum();
2637 }
2638}
2639
2640/*
2641 * Remove any popup menu.
2642 */
2643 static void
2644ins_compl_del_pum()
2645{
2646 if (compl_match_array != NULL)
2647 {
2648 pum_undisplay();
2649 vim_free(compl_match_array);
2650 compl_match_array = NULL;
2651 }
2652}
2653
2654/*
2655 * Return TRUE if the popup menu should be displayed.
2656 */
2657 static int
2658pum_wanted()
2659{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002660 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002661 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002662 return FALSE;
2663
2664 /* The display looks bad on a B&W display. */
2665 if (t_colors < 8
2666#ifdef FEAT_GUI
2667 && !gui.in_use
2668#endif
2669 )
2670 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002671 return TRUE;
2672}
2673
2674/*
2675 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002676 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002677 */
2678 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002679pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002680{
2681 compl_T *compl;
2682 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002683
2684 /* Don't display the popup menu if there are no matches or there is only
2685 * one (ignoring the original text). */
2686 compl = compl_first_match;
2687 i = 0;
2688 do
2689 {
2690 if (compl == NULL
2691 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2692 break;
2693 compl = compl->cp_next;
2694 } while (compl != compl_first_match);
2695
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002696 if (strstr((char *)p_cot, "menuone") != NULL)
2697 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002698 return (i >= 2);
2699}
2700
2701/*
2702 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002703 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002704 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002705 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002706ins_compl_show_pum()
2707{
2708 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002709 compl_T *shown_compl = NULL;
2710 int did_find_shown_match = FALSE;
2711 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002712 int i;
2713 int cur = -1;
2714 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002715 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002716
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002717 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002718 return;
2719
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002720#if defined(FEAT_EVAL)
2721 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2722 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2723#endif
2724
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002725 /* Update the screen before drawing the popup menu over it. */
2726 update_screen(0);
2727
2728 if (compl_match_array == NULL)
2729 {
2730 /* Need to build the popup menu list. */
2731 compl_match_arraysize = 0;
2732 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002733 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002734 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002735 do
2736 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002737 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2738 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002739 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002740 ++compl_match_arraysize;
2741 compl = compl->cp_next;
2742 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002743 if (compl_match_arraysize == 0)
2744 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002745 compl_match_array = (pumitem_T *)alloc_clear(
2746 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002747 * compl_match_arraysize));
2748 if (compl_match_array != NULL)
2749 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002750 /* If the current match is the original text don't find the first
2751 * match after it, don't highlight anything. */
2752 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2753 shown_match_ok = TRUE;
2754
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002755 i = 0;
2756 compl = compl_first_match;
2757 do
2758 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002759 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2760 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002761 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002762 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002763 if (!shown_match_ok)
2764 {
2765 if (compl == compl_shown_match || did_find_shown_match)
2766 {
2767 /* This item is the shown match or this is the
2768 * first displayed item after the shown match. */
2769 compl_shown_match = compl;
2770 did_find_shown_match = TRUE;
2771 shown_match_ok = TRUE;
2772 }
2773 else
2774 /* Remember this displayed match for when the
2775 * shown match is just below it. */
2776 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002777 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002778 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002779
2780 if (compl->cp_text[CPT_ABBR] != NULL)
2781 compl_match_array[i].pum_text =
2782 compl->cp_text[CPT_ABBR];
2783 else
2784 compl_match_array[i].pum_text = compl->cp_str;
2785 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2786 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2787 if (compl->cp_text[CPT_MENU] != NULL)
2788 compl_match_array[i++].pum_extra =
2789 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002790 else
2791 compl_match_array[i++].pum_extra = compl->cp_fname;
2792 }
2793
2794 if (compl == compl_shown_match)
2795 {
2796 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002797
2798 /* When the original text is the shown match don't set
2799 * compl_shown_match. */
2800 if (compl->cp_flags & ORIGINAL_TEXT)
2801 shown_match_ok = TRUE;
2802
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002803 if (!shown_match_ok && shown_compl != NULL)
2804 {
2805 /* The shown match isn't displayed, set it to the
2806 * previously displayed match. */
2807 compl_shown_match = shown_compl;
2808 shown_match_ok = TRUE;
2809 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002810 }
2811 compl = compl->cp_next;
2812 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002813
2814 if (!shown_match_ok) /* no displayed match at all */
2815 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002816 }
2817 }
2818 else
2819 {
2820 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002821 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002822 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2823 || compl_match_array[i].pum_text
2824 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002825 {
2826 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002827 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002828 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002829 }
2830
2831 if (compl_match_array != NULL)
2832 {
2833 /* Compute the screen column of the start of the completed text.
2834 * Use the cursor to get all wrapping and other settings right. */
2835 col = curwin->w_cursor.col;
2836 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002837 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002838 curwin->w_cursor.col = col;
2839 }
2840}
2841
Bram Moolenaar071d4272004-06-13 20:20:40 +00002842#define DICT_FIRST (1) /* use just first element in "dict" */
2843#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002844
Bram Moolenaar071d4272004-06-13 20:20:40 +00002845/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002846 * Add any identifiers that match the given pattern in the list of dictionary
2847 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002848 */
2849 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002850ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2851 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002852 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002853 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002854 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002855{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002856 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002857 char_u *ptr;
2858 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002859 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 char_u **files;
2861 int count;
2862 int i;
2863 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002864 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002865
Bram Moolenaar0b238792006-03-02 22:49:12 +00002866 if (*dict == NUL)
2867 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002868#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002869 /* When 'dictionary' is empty and spell checking is enabled use
2870 * "spell". */
2871 if (!thesaurus && curwin->w_p_spell)
2872 dict = (char_u *)"spell";
2873 else
2874#endif
2875 return;
2876 }
2877
Bram Moolenaar071d4272004-06-13 20:20:40 +00002878 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002879 if (buf == NULL)
2880 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002881 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002882
Bram Moolenaar071d4272004-06-13 20:20:40 +00002883 /* If 'infercase' is set, don't use 'smartcase' here */
2884 save_p_scs = p_scs;
2885 if (curbuf->b_p_inf)
2886 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002887
2888 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2889 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002890 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002891 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2892 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002893 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2894
2895 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002896 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002897 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002898 ptr = alloc(i);
2899 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002900 {
2901 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002902 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002903 }
2904 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2905 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2906 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002907 vim_free(ptr);
2908 }
2909 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002910 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002911 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002912 if (regmatch.regprog == NULL)
2913 goto theend;
2914 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2917 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002918 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 {
2920 /* copy one dictionary file name into buf */
2921 if (flags == DICT_EXACT)
2922 {
2923 count = 1;
2924 files = &dict;
2925 }
2926 else
2927 {
2928 /* Expand wildcards in the dictionary name, but do not allow
2929 * backticks (for security, the 'dict' option may have been set in
2930 * a modeline). */
2931 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002932# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002933 if (!thesaurus && STRCMP(buf, "spell") == 0)
2934 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002935 else
2936# endif
2937 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002938 || expand_wildcards(1, &buf, &count, &files,
2939 EW_FILE|EW_SILENT) != OK)
2940 count = 0;
2941 }
2942
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002943# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002944 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002945 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002946 /* Complete from active spelling. Skip "\<" in the pattern, we
2947 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002948 if (pat[0] == '\\' && pat[1] == '<')
2949 ptr = pat + 2;
2950 else
2951 ptr = pat;
2952 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002954 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002955# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002956 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002957 {
2958 ins_compl_files(count, files, thesaurus, flags,
2959 &regmatch, buf, &dir);
2960 if (flags != DICT_EXACT)
2961 FreeWild(count, files);
2962 }
2963 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002964 break;
2965 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002966
2967theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002968 p_scs = save_p_scs;
2969 vim_free(regmatch.regprog);
2970 vim_free(buf);
2971}
2972
Bram Moolenaar0b238792006-03-02 22:49:12 +00002973 static void
2974ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2975 int count;
2976 char_u **files;
2977 int thesaurus;
2978 int flags;
2979 regmatch_T *regmatch;
2980 char_u *buf;
2981 int *dir;
2982{
2983 char_u *ptr;
2984 int i;
2985 FILE *fp;
2986 int add_r;
2987
2988 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2989 {
2990 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2991 if (flags != DICT_EXACT)
2992 {
2993 vim_snprintf((char *)IObuff, IOSIZE,
2994 _("Scanning dictionary: %s"), (char *)files[i]);
2995 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2996 }
2997
2998 if (fp != NULL)
2999 {
3000 /*
3001 * Read dictionary file line by line.
3002 * Check each line for a match.
3003 */
3004 while (!got_int && !compl_interrupted
3005 && !vim_fgets(buf, LSIZE, fp))
3006 {
3007 ptr = buf;
3008 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3009 {
3010 ptr = regmatch->startp[0];
3011 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3012 ptr = find_line_end(ptr);
3013 else
3014 ptr = find_word_end(ptr);
3015 add_r = ins_compl_add_infercase(regmatch->startp[0],
3016 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003017 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003018 if (thesaurus)
3019 {
3020 char_u *wstart;
3021
3022 /*
3023 * Add the other matches on the line
3024 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003025 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003026 while (!got_int)
3027 {
3028 /* Find start of the next word. Skip white
3029 * space and punctuation. */
3030 ptr = find_word_start(ptr);
3031 if (*ptr == NUL || *ptr == NL)
3032 break;
3033 wstart = ptr;
3034
Bram Moolenaara2993e12007-08-12 14:38:46 +00003035 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003036#ifdef FEAT_MBYTE
3037 if (has_mbyte)
3038 /* Japanese words may have characters in
3039 * different classes, only separate words
3040 * with single-byte non-word characters. */
3041 while (*ptr != NUL)
3042 {
3043 int l = (*mb_ptr2len)(ptr);
3044
3045 if (l < 2 && !vim_iswordc(*ptr))
3046 break;
3047 ptr += l;
3048 }
3049 else
3050#endif
3051 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003052
3053 /* Add the word. Skip the regexp match. */
3054 if (wstart != regmatch->startp[0])
3055 add_r = ins_compl_add_infercase(wstart,
3056 (int)(ptr - wstart),
3057 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003058 }
3059 }
3060 if (add_r == OK)
3061 /* if dir was BACKWARD then honor it just once */
3062 *dir = FORWARD;
3063 else if (add_r == FAIL)
3064 break;
3065 /* avoid expensive call to vim_regexec() when at end
3066 * of line */
3067 if (*ptr == '\n' || got_int)
3068 break;
3069 }
3070 line_breakcheck();
3071 ins_compl_check_keys(50);
3072 }
3073 fclose(fp);
3074 }
3075 }
3076}
3077
Bram Moolenaar071d4272004-06-13 20:20:40 +00003078/*
3079 * Find the start of the next word.
3080 * Returns a pointer to the first char of the word. Also stops at a NUL.
3081 */
3082 char_u *
3083find_word_start(ptr)
3084 char_u *ptr;
3085{
3086#ifdef FEAT_MBYTE
3087 if (has_mbyte)
3088 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003089 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003090 else
3091#endif
3092 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3093 ++ptr;
3094 return ptr;
3095}
3096
3097/*
3098 * Find the end of the word. Assumes it starts inside a word.
3099 * Returns a pointer to just after the word.
3100 */
3101 char_u *
3102find_word_end(ptr)
3103 char_u *ptr;
3104{
3105#ifdef FEAT_MBYTE
3106 int start_class;
3107
3108 if (has_mbyte)
3109 {
3110 start_class = mb_get_class(ptr);
3111 if (start_class > 1)
3112 while (*ptr != NUL)
3113 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003114 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003115 if (mb_get_class(ptr) != start_class)
3116 break;
3117 }
3118 }
3119 else
3120#endif
3121 while (vim_iswordc(*ptr))
3122 ++ptr;
3123 return ptr;
3124}
3125
3126/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003127 * Find the end of the line, omitting CR and NL at the end.
3128 * Returns a pointer to just after the line.
3129 */
3130 static char_u *
3131find_line_end(ptr)
3132 char_u *ptr;
3133{
3134 char_u *s;
3135
3136 s = ptr + STRLEN(ptr);
3137 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3138 --s;
3139 return s;
3140}
3141
3142/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 * Free the list of completions
3144 */
3145 static void
3146ins_compl_free()
3147{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003148 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003149 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003150
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003151 vim_free(compl_pattern);
3152 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003153 vim_free(compl_leader);
3154 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003155
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003156 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003157 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003158
3159 ins_compl_del_pum();
3160 pum_clear();
3161
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003162 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 do
3164 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003165 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003166 compl_curr_match = compl_curr_match->cp_next;
3167 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003169 if (match->cp_flags & FREE_FNAME)
3170 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003171 for (i = 0; i < CPT_COUNT; ++i)
3172 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003173 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003174 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3175 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176}
3177
3178 static void
3179ins_compl_clear()
3180{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003181 compl_cont_status = 0;
3182 compl_started = FALSE;
3183 compl_matches = 0;
3184 vim_free(compl_pattern);
3185 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003186 vim_free(compl_leader);
3187 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003189 vim_free(compl_orig_text);
3190 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003191 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192}
3193
3194/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003195 * Return TRUE when Insert completion is active.
3196 */
3197 int
3198ins_compl_active()
3199{
3200 return compl_started;
3201}
3202
3203/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003204 * Delete one character before the cursor and show the subset of the matches
3205 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003206 * Returns the character to be used, NUL if the work is done and another char
3207 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003208 */
3209 static int
3210ins_compl_bs()
3211{
3212 char_u *line;
3213 char_u *p;
3214
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003215 line = ml_get_curline();
3216 p = line + curwin->w_cursor.col;
3217 mb_ptr_back(line, p);
3218
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003219 /* Stop completion when the whole word was deleted. For Omni completion
3220 * allow the word to be deleted, we won't match everything. */
3221 if ((int)(p - line) - (int)compl_col < 0
3222 || ((int)(p - line) - (int)compl_col == 0
3223 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003224 return K_BS;
3225
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003226 /* Deleted more than what was used to find matches or didn't finish
3227 * finding all matches: need to look for matches all over again. */
3228 if (curwin->w_cursor.col <= compl_col + compl_length
3229 || compl_was_interrupted)
3230 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003231
Bram Moolenaara6557602006-02-04 22:43:20 +00003232 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003233 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003234 if (compl_leader != NULL)
3235 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003236 ins_compl_new_leader();
3237 return NUL;
3238 }
3239 return K_BS;
3240}
Bram Moolenaara6557602006-02-04 22:43:20 +00003241
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003242/*
3243 * Called after changing "compl_leader".
3244 * Show the popup menu with a different set of matches.
3245 * May also search for matches again if the previous search was interrupted.
3246 */
3247 static void
3248ins_compl_new_leader()
3249{
3250 ins_compl_del_pum();
3251 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003252 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003253 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003254
3255 if (compl_started)
3256 ins_compl_set_original_text(compl_leader);
3257 else
3258 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003259#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003260 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003261#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003262 /*
3263 * Matches were cleared, need to search for them now. First display
3264 * the changed text before the cursor. Set "compl_restarting" to
3265 * avoid that the first match is inserted.
3266 */
3267 update_screen(0);
3268#ifdef FEAT_GUI
3269 if (gui.in_use)
3270 {
3271 /* Show the cursor after the match, not after the redrawn text. */
3272 setcursor();
3273 out_flush();
3274 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003275 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003276#endif
3277 compl_restarting = TRUE;
3278 if (ins_complete(Ctrl_N) == FAIL)
3279 compl_cont_status = 0;
3280 compl_restarting = FALSE;
3281 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003282
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003283#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003284 if (!compl_used_match)
3285 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003286 /* Go to the original text, since none of the matches is inserted. */
3287 if (compl_first_match->cp_prev != NULL
3288 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3289 compl_shown_match = compl_first_match->cp_prev;
3290 else
3291 compl_shown_match = compl_first_match;
3292 compl_curr_match = compl_shown_match;
3293 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003294 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003295#endif
3296 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003297
3298 /* Show the popup menu with a different set of matches. */
3299 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003300
3301 /* Don't let Enter select the original text when there is no popup menu. */
3302 if (compl_match_array == NULL)
3303 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003304}
3305
3306/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003307 * Return the length of the completion, from the completion start column to
3308 * the cursor column. Making sure it never goes below zero.
3309 */
3310 static int
3311ins_compl_len()
3312{
3313 int off = curwin->w_cursor.col - compl_col;
3314
3315 if (off < 0)
3316 return 0;
3317 return off;
3318}
3319
3320/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003321 * Append one character to the match leader. May reduce the number of
3322 * matches.
3323 */
3324 static void
3325ins_compl_addleader(c)
3326 int c;
3327{
3328#ifdef FEAT_MBYTE
3329 int cc;
3330
3331 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3332 {
3333 char_u buf[MB_MAXBYTES + 1];
3334
3335 (*mb_char2bytes)(c, buf);
3336 buf[cc] = NUL;
3337 ins_char_bytes(buf, cc);
3338 }
3339 else
3340#endif
3341 ins_char(c);
3342
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003343 /* If we didn't complete finding matches we must search again. */
3344 if (compl_was_interrupted)
3345 ins_compl_restart();
3346
Bram Moolenaara6557602006-02-04 22:43:20 +00003347 vim_free(compl_leader);
3348 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3349 curwin->w_cursor.col - compl_col);
3350 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003351 ins_compl_new_leader();
3352}
3353
3354/*
3355 * Setup for finding completions again without leaving CTRL-X mode. Used when
3356 * BS or a key was typed while still searching for matches.
3357 */
3358 static void
3359ins_compl_restart()
3360{
3361 ins_compl_free();
3362 compl_started = FALSE;
3363 compl_matches = 0;
3364 compl_cont_status = 0;
3365 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003366}
3367
3368/*
3369 * Set the first match, the original text.
3370 */
3371 static void
3372ins_compl_set_original_text(str)
3373 char_u *str;
3374{
3375 char_u *p;
3376
3377 /* Replace the original text entry. */
3378 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3379 {
3380 p = vim_strsave(str);
3381 if (p != NULL)
3382 {
3383 vim_free(compl_first_match->cp_str);
3384 compl_first_match->cp_str = p;
3385 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003386 }
3387}
3388
3389/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003390 * Append one character to the match leader. May reduce the number of
3391 * matches.
3392 */
3393 static void
3394ins_compl_addfrommatch()
3395{
3396 char_u *p;
3397 int len = curwin->w_cursor.col - compl_col;
3398 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003399 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003400
3401 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003402 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003403 {
3404 /* When still at the original match use the first entry that matches
3405 * the leader. */
3406 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3407 {
3408 p = NULL;
3409 for (cp = compl_shown_match->cp_next; cp != NULL
3410 && cp != compl_first_match; cp = cp->cp_next)
3411 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003412 if (compl_leader == NULL
3413 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003414 (int)STRLEN(compl_leader)))
3415 {
3416 p = cp->cp_str;
3417 break;
3418 }
3419 }
3420 if (p == NULL || (int)STRLEN(p) <= len)
3421 return;
3422 }
3423 else
3424 return;
3425 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003426 p += len;
3427#ifdef FEAT_MBYTE
3428 c = mb_ptr2char(p);
3429#else
3430 c = *p;
3431#endif
3432 ins_compl_addleader(c);
3433}
3434
3435/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003436 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003437 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003438 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003440 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441ins_compl_prep(c)
3442 int c;
3443{
3444 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003446 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003447
3448 /* Forget any previous 'special' messages if this is actually
3449 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3450 */
3451 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3452 edit_submode_extra = NULL;
3453
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003454 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3455 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003456 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003458 /* Set "compl_get_longest" when finding the first matches. */
3459 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3460 || (ctrl_x_mode == 0 && !compl_started))
3461 {
3462 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3463 compl_used_match = TRUE;
3464 }
3465
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3467 {
3468 /*
3469 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3470 * it will be yet. Now we decide.
3471 */
3472 switch (c)
3473 {
3474 case Ctrl_E:
3475 case Ctrl_Y:
3476 ctrl_x_mode = CTRL_X_SCROLL;
3477 if (!(State & REPLACE_FLAG))
3478 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3479 else
3480 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3481 edit_submode_pre = NULL;
3482 showmode();
3483 break;
3484 case Ctrl_L:
3485 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3486 break;
3487 case Ctrl_F:
3488 ctrl_x_mode = CTRL_X_FILES;
3489 break;
3490 case Ctrl_K:
3491 ctrl_x_mode = CTRL_X_DICTIONARY;
3492 break;
3493 case Ctrl_R:
3494 /* Simply allow ^R to happen without affecting ^X mode */
3495 break;
3496 case Ctrl_T:
3497 ctrl_x_mode = CTRL_X_THESAURUS;
3498 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003499#ifdef FEAT_COMPL_FUNC
3500 case Ctrl_U:
3501 ctrl_x_mode = CTRL_X_FUNCTION;
3502 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003503 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003504 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003505 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003506#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003507 case 's':
3508 case Ctrl_S:
3509 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003510#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003511 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003512 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003513 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003514#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003515 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 case Ctrl_RSB:
3517 ctrl_x_mode = CTRL_X_TAGS;
3518 break;
3519#ifdef FEAT_FIND_ID
3520 case Ctrl_I:
3521 case K_S_TAB:
3522 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3523 break;
3524 case Ctrl_D:
3525 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3526 break;
3527#endif
3528 case Ctrl_V:
3529 case Ctrl_Q:
3530 ctrl_x_mode = CTRL_X_CMDLINE;
3531 break;
3532 case Ctrl_P:
3533 case Ctrl_N:
3534 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3535 * just started ^X mode, or there were enough ^X's to cancel
3536 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3537 * do normal expansion when interrupting a different mode (say
3538 * ^X^F^X^P or ^P^X^X^P, see below)
3539 * nothing changes if interrupting mode 0, (eg, the flag
3540 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003541 if (!(compl_cont_status & CONT_INTRPT))
3542 compl_cont_status |= CONT_LOCAL;
3543 else if (compl_cont_mode != 0)
3544 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003545 /* FALLTHROUGH */
3546 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003547 /* If we have typed at least 2 ^X's... for modes != 0, we set
3548 * compl_cont_status = 0 (eg, as if we had just started ^X
3549 * mode).
3550 * For mode 0, we set "compl_cont_mode" to an impossible
3551 * value, in both cases ^X^X can be used to restart the same
3552 * mode (avoiding ADDING mode).
3553 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3554 * 'complete' and local ^P expansions respectively.
3555 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3556 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (c == Ctrl_X)
3558 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 if (compl_cont_mode != 0)
3560 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003562 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003563 }
3564 ctrl_x_mode = 0;
3565 edit_submode = NULL;
3566 showmode();
3567 break;
3568 }
3569 }
3570 else if (ctrl_x_mode != 0)
3571 {
3572 /* We're already in CTRL-X mode, do we stay in it? */
3573 if (!vim_is_ctrl_x_key(c))
3574 {
3575 if (ctrl_x_mode == CTRL_X_SCROLL)
3576 ctrl_x_mode = 0;
3577 else
3578 ctrl_x_mode = CTRL_X_FINISHED;
3579 edit_submode = NULL;
3580 }
3581 showmode();
3582 }
3583
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003584 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585 {
3586 /* Show error message from attempted keyword completion (probably
3587 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003588 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003590 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3591 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592 || ctrl_x_mode == CTRL_X_FINISHED)
3593 {
3594 /* Get here when we have finished typing a sequence of ^N and
3595 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003596 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003597 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003598 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003599 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003600 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003601
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003603 * If any of the original typed text has been changed, eg when
3604 * ignorecase is set, we must add back-spaces to the redo
3605 * buffer. We add as few as necessary to delete just the part
3606 * of the original text that has changed.
3607 * When using the longest match, edited the match or used
3608 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003609 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003610 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3611 ptr = compl_curr_match->cp_str;
3612 else if (compl_leader != NULL)
3613 ptr = compl_leader;
3614 else
3615 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003616 if (compl_orig_text != NULL)
3617 {
3618 p = compl_orig_text;
3619 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3620 ++temp)
3621 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003622#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003623 if (temp > 0)
3624 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003625#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003626 for (p += temp; *p != NUL; mb_ptr_adv(p))
3627 AppendCharToRedobuff(K_BS);
3628 }
3629 if (ptr != NULL)
3630 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 }
3632
3633#ifdef FEAT_CINDENT
3634 want_cindent = (can_cindent && cindent_on());
3635#endif
3636 /*
3637 * When completing whole lines: fix indent for 'cindent'.
3638 * Otherwise, break line if it's too long.
3639 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003640 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 {
3642#ifdef FEAT_CINDENT
3643 /* re-indent the current line */
3644 if (want_cindent)
3645 {
3646 do_c_expr_indent();
3647 want_cindent = FALSE; /* don't do it again */
3648 }
3649#endif
3650 }
3651 else
3652 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003653 int prev_col = curwin->w_cursor.col;
3654
Bram Moolenaar071d4272004-06-13 20:20:40 +00003655 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003656 if (prev_col > 0)
3657 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 if (stop_arrow() == OK)
3659 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003660 if (prev_col > 0
3661 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3662 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 }
3664
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003665 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003666 * the selection without inserting anything. When
3667 * compl_enter_selects is set the Enter key does the same. */
3668 if ((c == Ctrl_Y || (compl_enter_selects
3669 && (c == CAR || c == K_KENTER || c == NL)))
3670 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003671 retval = TRUE;
3672
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003673 /* CTRL-E means completion is Ended, go back to the typed text. */
3674 if (c == Ctrl_E)
3675 {
3676 ins_compl_delete();
3677 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003678 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003679 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003680 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003681 retval = TRUE;
3682 }
3683
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003684 auto_format(FALSE, TRUE);
3685
Bram Moolenaar071d4272004-06-13 20:20:40 +00003686 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003687 compl_started = FALSE;
3688 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003689 msg_clr_cmdline(); /* necessary for "noshowmode" */
3690 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003691 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003692 if (edit_submode != NULL)
3693 {
3694 edit_submode = NULL;
3695 showmode();
3696 }
3697
3698#ifdef FEAT_CINDENT
3699 /*
3700 * Indent now if a key was typed that is in 'cinkeys'.
3701 */
3702 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3703 do_c_expr_indent();
3704#endif
3705 }
3706 }
3707
3708 /* reset continue_* if we left expansion-mode, if we stay they'll be
3709 * (re)set properly in ins_complete() */
3710 if (!vim_is_ctrl_x_key(c))
3711 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003712 compl_cont_status = 0;
3713 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003715
3716 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717}
3718
3719/*
3720 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3721 * (depending on flag) starting from buf and looking for a non-scanned
3722 * buffer (other than curbuf). curbuf is special, if it is called with
3723 * buf=curbuf then it has to be the first call for a given flag/expansion.
3724 *
3725 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3726 */
3727 static buf_T *
3728ins_compl_next_buf(buf, flag)
3729 buf_T *buf;
3730 int flag;
3731{
3732#ifdef FEAT_WINDOWS
3733 static win_T *wp;
3734#endif
3735
3736 if (flag == 'w') /* just windows */
3737 {
3738#ifdef FEAT_WINDOWS
3739 if (buf == curbuf) /* first call for this flag/expansion */
3740 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003741 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003742 && wp->w_buffer->b_scanned)
3743 ;
3744 buf = wp->w_buffer;
3745#else
3746 buf = curbuf;
3747#endif
3748 }
3749 else
3750 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3751 * (unlisted buffers)
3752 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003753 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 && ((flag == 'U'
3755 ? buf->b_p_bl
3756 : (!buf->b_p_bl
3757 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003758 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 ;
3760 return buf;
3761}
3762
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003763#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003764static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003765
3766/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003767 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003768 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003769 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003770 static void
3771expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003772 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003773 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003774{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003775 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003776 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003777 char_u *funcname;
3778 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003779
Bram Moolenaare344bea2005-09-01 20:46:49 +00003780 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3781 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003782 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003783
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003784 /* Call 'completefunc' to obtain the list of matches. */
3785 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003786 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003787
Bram Moolenaare344bea2005-09-01 20:46:49 +00003788 pos = curwin->w_cursor;
3789 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3790 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003791 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003792 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003793
Bram Moolenaara94bc432006-03-10 21:42:59 +00003794 ins_compl_add_list(matchlist);
3795 list_unref(matchlist);
3796}
3797#endif /* FEAT_COMPL_FUNC */
3798
Bram Moolenaar39f05632006-03-19 22:15:26 +00003799#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003800/*
3801 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003802 */
3803 static void
3804ins_compl_add_list(list)
3805 list_T *list;
3806{
3807 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003808 int dir = compl_direction;
3809
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003810 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003811 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003812 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003813 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3814 /* if dir was BACKWARD then honor it just once */
3815 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003816 else if (did_emsg)
3817 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003818 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003819}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003820
3821/*
3822 * Add a match to the list of matches from a typeval_T.
3823 * If the given string is already in the list of completions, then return
3824 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3825 * maybe because alloc() returns NULL, then FAIL is returned.
3826 */
3827 int
3828ins_compl_add_tv(tv, dir)
3829 typval_T *tv;
3830 int dir;
3831{
3832 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003833 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003834 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003835 char_u *(cptext[CPT_COUNT]);
3836
3837 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3838 {
3839 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3840 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3841 (char_u *)"abbr", FALSE);
3842 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3843 (char_u *)"menu", FALSE);
3844 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3845 (char_u *)"kind", FALSE);
3846 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3847 (char_u *)"info", FALSE);
3848 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3849 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003850 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003851 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003852 }
3853 else
3854 {
3855 word = get_tv_string_chk(tv);
3856 vim_memset(cptext, 0, sizeof(cptext));
3857 }
3858 if (word == NULL || *word == NUL)
3859 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003860 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003861}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003862#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003863
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003864/*
3865 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003866 * The search starts at position "ini" in curbuf and in the direction
3867 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003868 * When "compl_started" is FALSE start at that position, otherwise continue
3869 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003870 * This may return before finding all the matches.
3871 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003872 */
3873 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003874ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003875 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876{
3877 static pos_T first_match_pos;
3878 static pos_T last_match_pos;
3879 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003880 static int found_all = FALSE; /* Found all matches of a
3881 certain type. */
3882 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003883
Bram Moolenaar572cb562005-08-05 21:35:02 +00003884 pos_T *pos;
3885 char_u **matches;
3886 int save_p_scs;
3887 int save_p_ws;
3888 int save_p_ic;
3889 int i;
3890 int num_matches;
3891 int len;
3892 int found_new_match;
3893 int type = ctrl_x_mode;
3894 char_u *ptr;
3895 char_u *dict = NULL;
3896 int dict_f = 0;
3897 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003898
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003899 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003900 {
3901 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3902 ins_buf->b_scanned = 0;
3903 found_all = FALSE;
3904 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003905 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003906 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003907 last_match_pos = first_match_pos = *ini;
3908 }
3909
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003910 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003911 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003912 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3913 for (;;)
3914 {
3915 found_new_match = FAIL;
3916
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003917 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003918 * or if found_all says this entry is done. For ^X^L only use the
3919 * entries from 'complete' that look in loaded buffers. */
3920 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003921 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003922 {
3923 found_all = FALSE;
3924 while (*e_cpt == ',' || *e_cpt == ' ')
3925 e_cpt++;
3926 if (*e_cpt == '.' && !curbuf->b_scanned)
3927 {
3928 ins_buf = curbuf;
3929 first_match_pos = *ini;
3930 /* So that ^N can match word immediately after cursor */
3931 if (ctrl_x_mode == 0)
3932 dec(&first_match_pos);
3933 last_match_pos = first_match_pos;
3934 type = 0;
3935 }
3936 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3937 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3938 {
3939 /* Scan a buffer, but not the current one. */
3940 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3941 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003942 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943 first_match_pos.col = last_match_pos.col = 0;
3944 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3945 last_match_pos.lnum = 0;
3946 type = 0;
3947 }
3948 else /* unloaded buffer, scan like dictionary */
3949 {
3950 found_all = TRUE;
3951 if (ins_buf->b_fname == NULL)
3952 continue;
3953 type = CTRL_X_DICTIONARY;
3954 dict = ins_buf->b_fname;
3955 dict_f = DICT_EXACT;
3956 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003957 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958 ins_buf->b_fname == NULL
3959 ? buf_spname(ins_buf)
3960 : ins_buf->b_sfname == NULL
3961 ? (char *)ins_buf->b_fname
3962 : (char *)ins_buf->b_sfname);
3963 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3964 }
3965 else if (*e_cpt == NUL)
3966 break;
3967 else
3968 {
3969 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3970 type = -1;
3971 else if (*e_cpt == 'k' || *e_cpt == 's')
3972 {
3973 if (*e_cpt == 'k')
3974 type = CTRL_X_DICTIONARY;
3975 else
3976 type = CTRL_X_THESAURUS;
3977 if (*++e_cpt != ',' && *e_cpt != NUL)
3978 {
3979 dict = e_cpt;
3980 dict_f = DICT_FIRST;
3981 }
3982 }
3983#ifdef FEAT_FIND_ID
3984 else if (*e_cpt == 'i')
3985 type = CTRL_X_PATH_PATTERNS;
3986 else if (*e_cpt == 'd')
3987 type = CTRL_X_PATH_DEFINES;
3988#endif
3989 else if (*e_cpt == ']' || *e_cpt == 't')
3990 {
3991 type = CTRL_X_TAGS;
3992 sprintf((char*)IObuff, _("Scanning tags."));
3993 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3994 }
3995 else
3996 type = -1;
3997
3998 /* in any case e_cpt is advanced to the next entry */
3999 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4000
4001 found_all = TRUE;
4002 if (type == -1)
4003 continue;
4004 }
4005 }
4006
4007 switch (type)
4008 {
4009 case -1:
4010 break;
4011#ifdef FEAT_FIND_ID
4012 case CTRL_X_PATH_PATTERNS:
4013 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004014 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004015 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004017 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4019 (linenr_T)1, (linenr_T)MAXLNUM);
4020 break;
4021#endif
4022
4023 case CTRL_X_DICTIONARY:
4024 case CTRL_X_THESAURUS:
4025 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004026 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027 : (type == CTRL_X_THESAURUS
4028 ? (*curbuf->b_p_tsr == NUL
4029 ? p_tsr
4030 : curbuf->b_p_tsr)
4031 : (*curbuf->b_p_dict == NUL
4032 ? p_dict
4033 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004034 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004035 dict != NULL ? dict_f
4036 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 dict = NULL;
4038 break;
4039
4040 case CTRL_X_TAGS:
4041 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4042 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004043 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044
4045 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004046 * of matches is found when compl_pattern is empty */
4047 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4049 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4050 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4051 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004052 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 }
4054 p_ic = save_p_ic;
4055 break;
4056
4057 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004058 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4060 {
4061
4062 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004063 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004064 ins_compl_add_matches(num_matches, matches,
4065#ifdef CASE_INSENSITIVE_FILENAME
4066 TRUE
4067#else
4068 FALSE
4069#endif
4070 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 }
4072 break;
4073
4074 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004075 if (expand_cmdline(&compl_xp, compl_pattern,
4076 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004077 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004078 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 break;
4080
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004081#ifdef FEAT_COMPL_FUNC
4082 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004083 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004084 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004085 break;
4086#endif
4087
Bram Moolenaar488c6512005-08-11 20:09:58 +00004088 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004089#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004090 num_matches = expand_spelling(first_match_pos.lnum,
4091 first_match_pos.col, compl_pattern, &matches);
4092 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004093 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004094#endif
4095 break;
4096
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 default: /* normal ^P/^N and ^X^L */
4098 /*
4099 * If 'infercase' is set, don't use 'smartcase' here
4100 */
4101 save_p_scs = p_scs;
4102 if (ins_buf->b_p_inf)
4103 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004104
Bram Moolenaar071d4272004-06-13 20:20:40 +00004105 /* buffers other than curbuf are scanned from the beginning or the
4106 * end but never from the middle, thus setting nowrapscan in this
4107 * buffers is a good idea, on the other hand, we always set
4108 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4109 save_p_ws = p_ws;
4110 if (ins_buf != curbuf)
4111 p_ws = FALSE;
4112 else if (*e_cpt == '.')
4113 p_ws = TRUE;
4114 for (;;)
4115 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004116 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004118 ++msg_silent; /* Don't want messages for wrapscan. */
4119
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004120 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4121 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004122 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004123 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004124 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004125 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004127 found_new_match = searchit(NULL, ins_buf, pos,
4128 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004129 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004130 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004131 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004132 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004134 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004135 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004136 first_match_pos = *pos;
4137 last_match_pos = *pos;
4138 }
4139 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004140 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 found_new_match = FAIL;
4142 if (found_new_match == FAIL)
4143 {
4144 if (ins_buf == curbuf)
4145 found_all = TRUE;
4146 break;
4147 }
4148
4149 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004150 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 && ini->lnum == pos->lnum
4152 && ini->col == pos->col)
4153 continue;
4154 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4155 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4156 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004157 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 {
4159 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4160 continue;
4161 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4162 if (!p_paste)
4163 ptr = skipwhite(ptr);
4164 }
4165 len = (int)STRLEN(ptr);
4166 }
4167 else
4168 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004169 char_u *tmp_ptr = ptr;
4170
4171 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004173 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 /* Skip if already inside a word. */
4175 if (vim_iswordp(tmp_ptr))
4176 continue;
4177 /* Find start of next word. */
4178 tmp_ptr = find_word_start(tmp_ptr);
4179 }
4180 /* Find end of this word. */
4181 tmp_ptr = find_word_end(tmp_ptr);
4182 len = (int)(tmp_ptr - ptr);
4183
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004184 if ((compl_cont_status & CONT_ADDING)
4185 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 {
4187 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4188 {
4189 /* Try next line, if any. the new word will be
4190 * "join" as if the normal command "J" was used.
4191 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004192 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 * works -- Acevedo */
4194 STRNCPY(IObuff, ptr, len);
4195 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4196 tmp_ptr = ptr = skipwhite(ptr);
4197 /* Find start of next word. */
4198 tmp_ptr = find_word_start(tmp_ptr);
4199 /* Find end of next word. */
4200 tmp_ptr = find_word_end(tmp_ptr);
4201 if (tmp_ptr > ptr)
4202 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004203 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004205 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 IObuff[len++] = ' ';
4207 /* IObuf =~ "\k.* ", thus len >= 2 */
4208 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004209 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 || (vim_strchr(p_cpo, CPO_JOINSP)
4211 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004212 && (IObuff[len - 2] == '?'
4213 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 IObuff[len++] = ' ';
4215 }
4216 /* copy as much as posible of the new word */
4217 if (tmp_ptr - ptr >= IOSIZE - len)
4218 tmp_ptr = ptr + IOSIZE - len - 1;
4219 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4220 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004221 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 }
4223 IObuff[len] = NUL;
4224 ptr = IObuff;
4225 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004226 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 continue;
4228 }
4229 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004230 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004231 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004232 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004233 {
4234 found_new_match = OK;
4235 break;
4236 }
4237 }
4238 p_scs = save_p_scs;
4239 p_ws = save_p_ws;
4240 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004241
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 /* check if compl_curr_match has changed, (e.g. other type of
4243 * expansion added somenthing) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004244 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 found_new_match = OK;
4246
4247 /* break the loop for specialized modes (use 'complete' just for the
4248 * generic ctrl_x_mode == 0) or when we've found a new match */
4249 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004250 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004251 {
4252 if (got_int)
4253 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004254 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004255 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004256 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004257
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004258 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4259 || compl_interrupted)
4260 break;
4261 compl_started = TRUE;
4262 }
4263 else
4264 {
4265 /* Mark a buffer scanned when it has been scanned completely */
4266 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4267 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004269 compl_started = FALSE;
4270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004272 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273
4274 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4275 && *e_cpt == NUL) /* Got to end of 'complete' */
4276 found_new_match = FAIL;
4277
4278 i = -1; /* total of matches, unknown */
4279 if (found_new_match == FAIL
4280 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4281 i = ins_compl_make_cyclic();
4282
4283 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004284 * just been made cyclic then we have to move compl_curr_match to the next
4285 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004286 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4287 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004288 if (compl_curr_match == NULL)
4289 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 return i;
4291}
4292
4293/* Delete the old text being completed. */
4294 static void
4295ins_compl_delete()
4296{
4297 int i;
4298
4299 /*
4300 * In insert mode: Delete the typed part.
4301 * In replace mode: Put the old characters back, if any.
4302 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004303 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004304 backspace_until_column(i);
4305 changed_cline_bef_curs();
4306}
4307
4308/* Insert the new text being completed. */
4309 static void
4310ins_compl_insert()
4311{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004312 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004313 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4314 compl_used_match = FALSE;
4315 else
4316 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317}
4318
4319/*
4320 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004321 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4322 * get more completions. If it is FALSE, then we just do nothing when there
4323 * are no more completions in a given direction. The latter case is used when
4324 * we are still in the middle of finding completions, to allow browsing
4325 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 * Return the total number of matches, or -1 if still unknown -- webb.
4327 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004328 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4329 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004330 *
4331 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004332 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4333 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 */
4335 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004336ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004338 int count; /* repeat completion this many times; should
4339 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004340 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341{
4342 int num_matches = -1;
4343 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004344 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004345 compl_T *found_compl = NULL;
4346 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004347 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004349 if (compl_leader != NULL
4350 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004352 /* Set "compl_shown_match" to the actually shown match, it may differ
4353 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004354 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004355 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004356 && compl_shown_match->cp_next != NULL
4357 && compl_shown_match->cp_next != compl_first_match)
4358 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004359
4360 /* If we didn't find it searching forward, and compl_shows_dir is
4361 * backward, find the last match. */
4362 if (compl_shows_dir == BACKWARD
4363 && !ins_compl_equal(compl_shown_match,
4364 compl_leader, (int)STRLEN(compl_leader))
4365 && (compl_shown_match->cp_next == NULL
4366 || compl_shown_match->cp_next == compl_first_match))
4367 {
4368 while (!ins_compl_equal(compl_shown_match,
4369 compl_leader, (int)STRLEN(compl_leader))
4370 && compl_shown_match->cp_prev != NULL
4371 && compl_shown_match->cp_prev != compl_first_match)
4372 compl_shown_match = compl_shown_match->cp_prev;
4373 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004374 }
4375
4376 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004377 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004378 /* Delete old text to be replaced */
4379 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004380
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004381 /* When finding the longest common text we stick at the original text,
4382 * don't let CTRL-N or CTRL-P move to the first match. */
4383 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4384
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004385 /* When restarting the search don't insert the first match either. */
4386 if (compl_restarting)
4387 {
4388 advance = FALSE;
4389 compl_restarting = FALSE;
4390 }
4391
Bram Moolenaare3226be2005-12-18 22:10:00 +00004392 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4393 * around. */
4394 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004396 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004398 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004399 found_end = (compl_first_match != NULL
4400 && (compl_shown_match->cp_next == compl_first_match
4401 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004402 }
4403 else if (compl_shows_dir == BACKWARD
4404 && compl_shown_match->cp_prev != NULL)
4405 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004406 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004407 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004408 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004409 }
4410 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004411 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004412 if (!allow_get_expansion)
4413 {
4414 if (advance)
4415 {
4416 if (compl_shows_dir == BACKWARD)
4417 compl_pending -= todo + 1;
4418 else
4419 compl_pending += todo + 1;
4420 }
4421 return -1;
4422 }
4423
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004424 if (advance)
4425 {
4426 if (compl_shows_dir == BACKWARD)
4427 --compl_pending;
4428 else
4429 ++compl_pending;
4430 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004431
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004432 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004433 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004434
4435 /* handle any pending completions */
4436 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004437 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004438 {
4439 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4440 {
4441 compl_shown_match = compl_shown_match->cp_next;
4442 --compl_pending;
4443 }
4444 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4445 {
4446 compl_shown_match = compl_shown_match->cp_prev;
4447 ++compl_pending;
4448 }
4449 else
4450 break;
4451 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004452 found_end = FALSE;
4453 }
4454 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4455 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004456 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004457 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004458 ++todo;
4459 else
4460 /* Remember a matching item. */
4461 found_compl = compl_shown_match;
4462
4463 /* Stop at the end of the list when we found a usable match. */
4464 if (found_end)
4465 {
4466 if (found_compl != NULL)
4467 {
4468 compl_shown_match = found_compl;
4469 break;
4470 }
4471 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004472 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 }
4474
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004475 /* Insert the text of the new completion, or the compl_leader. */
4476 if (insert_match)
4477 {
4478 if (!compl_get_longest || compl_used_match)
4479 ins_compl_insert();
4480 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004481 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004482 }
4483 else
4484 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004485
4486 if (!allow_get_expansion)
4487 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004488 /* may undisplay the popup menu first */
4489 ins_compl_upd_pum();
4490
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004491 /* redraw to show the user what was inserted */
4492 update_screen(0);
4493
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004494 /* display the updated popup menu */
4495 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004496#ifdef FEAT_GUI
4497 if (gui.in_use)
4498 {
4499 /* Show the cursor after the match, not after the redrawn text. */
4500 setcursor();
4501 out_flush();
4502 gui_update_cursor(FALSE, FALSE);
4503 }
4504#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004505
Bram Moolenaar071d4272004-06-13 20:20:40 +00004506 /* Delete old text to be replaced, since we're still searching and
4507 * don't want to match ourselves! */
4508 ins_compl_delete();
4509 }
4510
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004511 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004512 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004513 compl_enter_selects = !insert_match && compl_match_array != NULL;
4514
Bram Moolenaar071d4272004-06-13 20:20:40 +00004515 /*
4516 * Show the file name for the match (if any)
4517 * Truncate the file name to avoid a wait for return.
4518 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004519 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004520 {
4521 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004522 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 if (i <= 0)
4524 i = 0;
4525 else
4526 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004527 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528 msg(IObuff);
4529 redraw_cmdline = FALSE; /* don't overwrite! */
4530 }
4531
4532 return num_matches;
4533}
4534
4535/*
4536 * Call this while finding completions, to check whether the user has hit a key
4537 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004538 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004540 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004541 */
4542 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004543ins_compl_check_keys(frequency)
4544 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004545{
4546 static int count = 0;
4547
4548 int c;
4549
4550 /* Don't check when reading keys from a script. That would break the test
4551 * scripts */
4552 if (using_script())
4553 return;
4554
4555 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004556 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004557 return;
4558 count = 0;
4559
Bram Moolenaara260a972006-06-23 19:36:29 +00004560 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4561 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004562 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 if (c != NUL)
4564 {
4565 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4566 {
4567 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004568 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004569 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4570 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004571 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004572 else
4573 {
4574 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004575 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004576 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004577 if (c != K_IGNORE)
4578 {
4579 /* Don't interrupt completion when the character wasn't typed,
4580 * e.g., when doing @q to replay keys. */
4581 if (c != Ctrl_R && KeyTyped)
4582 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004583
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004584 vungetc(c);
4585 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004586 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004588 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004589 {
4590 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4591
4592 compl_pending = 0;
4593 (void)ins_compl_next(FALSE, todo, TRUE);
4594 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004595}
4596
4597/*
4598 * Decide the direction of Insert mode complete from the key typed.
4599 * Returns BACKWARD or FORWARD.
4600 */
4601 static int
4602ins_compl_key2dir(c)
4603 int c;
4604{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004605 if (c == Ctrl_P || c == Ctrl_L
4606 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4607 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004608 return BACKWARD;
4609 return FORWARD;
4610}
4611
4612/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004613 * Return TRUE for keys that are used for completion only when the popup menu
4614 * is visible.
4615 */
4616 static int
4617ins_compl_pum_key(c)
4618 int c;
4619{
4620 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004621 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4622 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004623}
4624
4625/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004626 * Decide the number of completions to move forward.
4627 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4628 */
4629 static int
4630ins_compl_key2count(c)
4631 int c;
4632{
4633 int h;
4634
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004635 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004636 {
4637 h = pum_get_height();
4638 if (h > 3)
4639 h -= 2; /* keep some context */
4640 return h;
4641 }
4642 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643}
4644
4645/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004646 * Return TRUE if completion with "c" should insert the match, FALSE if only
4647 * to change the currently selected completion.
4648 */
4649 static int
4650ins_compl_use_match(c)
4651 int c;
4652{
4653 switch (c)
4654 {
4655 case K_UP:
4656 case K_DOWN:
4657 case K_PAGEDOWN:
4658 case K_KPAGEDOWN:
4659 case K_S_DOWN:
4660 case K_PAGEUP:
4661 case K_KPAGEUP:
4662 case K_S_UP:
4663 return FALSE;
4664 }
4665 return TRUE;
4666}
4667
4668/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 * Do Insert mode completion.
4670 * Called when character "c" was typed, which has a meaning for completion.
4671 * Returns OK if completion was done, FAIL if something failed (out of mem).
4672 */
4673 static int
4674ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004675 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004676{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004677 char_u *line;
4678 int startcol = 0; /* column where searched text starts */
4679 colnr_T curs_col; /* cursor column */
4680 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681
Bram Moolenaare3226be2005-12-18 22:10:00 +00004682 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004683 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 {
4685 /* First time we hit ^N or ^P (in a row, I mean) */
4686
Bram Moolenaar071d4272004-06-13 20:20:40 +00004687 did_ai = FALSE;
4688#ifdef FEAT_SMARTINDENT
4689 did_si = FALSE;
4690 can_si = FALSE;
4691 can_si_back = FALSE;
4692#endif
4693 if (stop_arrow() == FAIL)
4694 return FAIL;
4695
4696 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004697 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004698 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004700 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004701 * "compl_startpos" to the cursor as a pattern to add a new word
4702 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004703 * "compl_startpos" is not in the same line as the cursor then fix it
4704 * (the line has been split because it was longer than 'tw'). if SOL
4705 * is set then skip the previous pattern, a word at the beginning of
4706 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004707 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4708 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004709 {
4710 /*
4711 * it is a continued search
4712 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004713 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004714 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4715 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4716 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004717 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004719 /* line (probably) wrapped, set compl_startpos to the
4720 * first non_blank in the line, if it is not a wordchar
4721 * include it to get a better pattern, but then we don't
4722 * want the "\\<" prefix, check it bellow */
4723 compl_col = (colnr_T)(skipwhite(line) - line);
4724 compl_startpos.col = compl_col;
4725 compl_startpos.lnum = curwin->w_cursor.lnum;
4726 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004727 }
4728 else
4729 {
4730 /* S_IPOS was set when we inserted a word that was at the
4731 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004732 * mode but first we need to redefine compl_startpos */
4733 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004734 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004735 compl_cont_status |= CONT_SOL;
4736 compl_startpos.col = (colnr_T)(skipwhite(
4737 line + compl_length
4738 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004740 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004742 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004743 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 * have enough space? just being paranoic */
4745#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004746 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004747 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004748 compl_cont_status &= ~CONT_SOL;
4749 compl_length = (IOSIZE - MIN_SPACE);
4750 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004752 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4753 if (compl_length < 1)
4754 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755 }
4756 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004757 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004759 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004762 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004764 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004766 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004768 compl_cont_status = 0;
4769 compl_cont_status |= CONT_N_ADDS;
4770 compl_startpos = curwin->w_cursor;
4771 startcol = (int)curs_col;
4772 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
4774
4775 /* Work out completion pattern and original text -- webb */
4776 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4777 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004778 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004779 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4780 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004781 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004783 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004784 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004785 compl_col += ++startcol;
4786 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004787 }
4788 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004789 compl_pattern = str_foldcase(line + compl_col,
4790 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004792 compl_pattern = vim_strnsave(line + compl_col,
4793 compl_length);
4794 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004795 return FAIL;
4796 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004797 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004798 {
4799 char_u *prefix = (char_u *)"\\<";
4800
4801 /* we need 3 extra chars, 1 for the NUL and
4802 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004803 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4804 compl_length) + 3);
4805 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004807 if (!vim_iswordp(line + compl_col)
4808 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 && (
4810#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004811 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004813 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004814#endif
4815 )))
4816 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004817 STRCPY((char *)compl_pattern, prefix);
4818 (void)quote_meta(compl_pattern + STRLEN(prefix),
4819 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004821 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004824#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004825 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004826#endif
4827 )
4828 {
4829 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004830 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4831 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004833 compl_col += curs_col;
4834 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004835 }
4836 else
4837 {
4838#ifdef FEAT_MBYTE
4839 /* Search the point of change class of multibyte character
4840 * or not a word single byte character backward. */
4841 if (has_mbyte)
4842 {
4843 int base_class;
4844 int head_off;
4845
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004846 startcol -= (*mb_head_off)(line, line + startcol);
4847 base_class = mb_get_class(line + startcol);
4848 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004850 head_off = (*mb_head_off)(line, line + startcol);
4851 if (base_class != mb_get_class(line + startcol
4852 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004854 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857 else
4858#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004859 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004861 compl_col += ++startcol;
4862 compl_length = (int)curs_col - startcol;
4863 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
4865 /* Only match word with at least two chars -- webb
4866 * there's no need to call quote_meta,
4867 * alloc(7) is enough -- Acevedo
4868 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004869 compl_pattern = alloc(7);
4870 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004871 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004872 STRCPY((char *)compl_pattern, "\\<");
4873 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4874 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
4876 else
4877 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004878 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4879 compl_length) + 3);
4880 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004881 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004882 STRCPY((char *)compl_pattern, "\\<");
4883 (void)quote_meta(compl_pattern + 2, line + compl_col,
4884 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 }
4886 }
4887 }
4888 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4889 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004890 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004891 compl_length = (int)curs_col - (int)compl_col;
4892 if (compl_length < 0) /* cursor in indent: empty pattern */
4893 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004895 compl_pattern = str_foldcase(line + compl_col, compl_length,
4896 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004898 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4899 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004900 return FAIL;
4901 }
4902 else if (ctrl_x_mode == CTRL_X_FILES)
4903 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004904 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004906 compl_col += ++startcol;
4907 compl_length = (int)curs_col - startcol;
4908 compl_pattern = addstar(line + compl_col, compl_length,
4909 EXPAND_FILES);
4910 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 return FAIL;
4912 }
4913 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4914 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004915 compl_pattern = vim_strnsave(line, curs_col);
4916 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004918 set_cmd_context(&compl_xp, compl_pattern,
4919 (int)STRLEN(compl_pattern), curs_col);
4920 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4921 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004922 /* No completion possible, use an empty pattern to get a
4923 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004924 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004925 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004926 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4927 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004929 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004930 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004931#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004932 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004933 * Call user defined function 'completefunc' with "a:findstart"
4934 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004935 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004936 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004937 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004938 char_u *funcname;
4939 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004940
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004941 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004942 * string */
4943 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4944 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4945 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004946 {
4947 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4948 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004949 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004950 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004951
4952 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004953 args[1] = NULL;
4954 pos = curwin->w_cursor;
4955 col = call_func_retnr(funcname, 2, args, FALSE);
4956 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004957
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004958 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004959 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004960 compl_col = col;
4961 if ((colnr_T)compl_col > curs_col)
4962 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004963
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004964 /* Setup variables for completion. Need to obtain "line" again,
4965 * it may have become invalid. */
4966 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004967 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004968 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4969 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004970#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004971 return FAIL;
4972 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004973 else if (ctrl_x_mode == CTRL_X_SPELL)
4974 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004975#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004976 if (spell_bad_len > 0)
4977 compl_col = curs_col - spell_bad_len;
4978 else
4979 compl_col = spell_word_start(startcol);
4980 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004981 {
4982 compl_length = 0;
4983 compl_col = curs_col;
4984 }
4985 else
4986 {
4987 spell_expand_check_cap(compl_col);
4988 compl_length = (int)curs_col - compl_col;
4989 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004990 /* Need to obtain "line" again, it may have become invalid. */
4991 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004992 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4993 if (compl_pattern == NULL)
4994#endif
4995 return FAIL;
4996 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004997 else
4998 {
4999 EMSG2(_(e_intern2), "ins_complete()");
5000 return FAIL;
5001 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005003 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005004 {
5005 edit_submode_pre = (char_u *)_(" Adding");
5006 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5007 {
5008 /* Insert a new line, keep indentation but ignore 'comments' */
5009#ifdef FEAT_COMMENTS
5010 char_u *old = curbuf->b_p_com;
5011
5012 curbuf->b_p_com = (char_u *)"";
5013#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005014 compl_startpos.lnum = curwin->w_cursor.lnum;
5015 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 ins_eol('\r');
5017#ifdef FEAT_COMMENTS
5018 curbuf->b_p_com = old;
5019#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005020 compl_length = 0;
5021 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005022 }
5023 }
5024 else
5025 {
5026 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005027 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
5029
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005030 if (compl_cont_status & CONT_LOCAL)
5031 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 else
5033 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5034
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005035 /* Always add completion for the original text. */
5036 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005037 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5038 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005039 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005041 vim_free(compl_pattern);
5042 compl_pattern = NULL;
5043 vim_free(compl_orig_text);
5044 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 return FAIL;
5046 }
5047
5048 /* showmode might reset the internal line pointers, so it must
5049 * be called before line = ml_get(), or when this address is no
5050 * longer needed. -- Acevedo.
5051 */
5052 edit_submode_extra = (char_u *)_("-- Searching...");
5053 edit_submode_highl = HLF_COUNT;
5054 showmode();
5055 edit_submode_extra = NULL;
5056 out_flush();
5057 }
5058
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005059 compl_shown_match = compl_curr_match;
5060 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005061
5062 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005063 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005064 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005065 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005067 /* may undisplay the popup menu */
5068 ins_compl_upd_pum();
5069
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005070 if (n > 1) /* all matches have been found */
5071 compl_matches = n;
5072 compl_curr_match = compl_shown_match;
5073 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005074
Bram Moolenaard68071d2006-05-02 22:08:30 +00005075 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5076 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 if (got_int && !global_busy)
5078 {
5079 (void)vgetc();
5080 got_int = FALSE;
5081 }
5082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005083 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005084 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005086 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5087 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5089 edit_submode_highl = HLF_E;
5090 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5091 * because we couldn't expand anything at first place, but if we used
5092 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5093 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005094 if ( compl_length > 1
5095 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005096 || (ctrl_x_mode != 0
5097 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5098 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005099 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 }
5101
Bram Moolenaar572cb562005-08-05 21:35:02 +00005102 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005104 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005105 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005106
5107 if (edit_submode_extra == NULL)
5108 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005109 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 {
5111 edit_submode_extra = (char_u *)_("Back at original");
5112 edit_submode_highl = HLF_W;
5113 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005114 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
5116 edit_submode_extra = (char_u *)_("Word from other line");
5117 edit_submode_highl = HLF_COUNT;
5118 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005119 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 {
5121 edit_submode_extra = (char_u *)_("The only match");
5122 edit_submode_highl = HLF_COUNT;
5123 }
5124 else
5125 {
5126 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005127 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005129 int number = 0;
5130 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005132 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005133 {
5134 /* search backwards for the first valid (!= -1) number.
5135 * This should normally succeed already at the first loop
5136 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005137 for (match = compl_curr_match->cp_prev; match != NULL
5138 && match != compl_first_match;
5139 match = match->cp_prev)
5140 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005142 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 break;
5144 }
5145 if (match != NULL)
5146 /* go up and assign all numbers which are not assigned
5147 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005148 for (match = match->cp_next;
5149 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005150 match = match->cp_next)
5151 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153 else /* BACKWARD */
5154 {
5155 /* search forwards (upwards) for the first valid (!= -1)
5156 * number. This should normally succeed already at the
5157 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005158 for (match = compl_curr_match->cp_next; match != NULL
5159 && match != compl_first_match;
5160 match = match->cp_next)
5161 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005163 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165 }
5166 if (match != NULL)
5167 /* go down and assign all numbers which are not
5168 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005169 for (match = match->cp_prev; match
5170 && match->cp_number == -1;
5171 match = match->cp_prev)
5172 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 }
5174 }
5175
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005176 /* The match should always have a sequence number now, this is
5177 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005178 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005180 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5181 * Translations may need more than twice that. */
5182 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005184 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005185 vim_snprintf((char *)match_ref, sizeof(match_ref),
5186 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005187 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005189 vim_snprintf((char *)match_ref, sizeof(match_ref),
5190 _("match %d"),
5191 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005192 edit_submode_extra = match_ref;
5193 edit_submode_highl = HLF_R;
5194 if (dollar_vcol)
5195 curs_columns(FALSE);
5196 }
5197 }
5198 }
5199
5200 /* Show a message about what (completion) mode we're in. */
5201 showmode();
5202 if (edit_submode_extra != NULL)
5203 {
5204 if (!p_smd)
5205 msg_attr(edit_submode_extra,
5206 edit_submode_highl < HLF_COUNT
5207 ? hl_attr(edit_submode_highl) : 0);
5208 }
5209 else
5210 msg_clr_cmdline(); /* necessary for "noshowmode" */
5211
Bram Moolenaard68071d2006-05-02 22:08:30 +00005212 /* Show the popup menu, unless we got interrupted. */
5213 if (!compl_interrupted)
5214 {
5215 /* RedrawingDisabled may be set when invoked through complete(). */
5216 n = RedrawingDisabled;
5217 RedrawingDisabled = 0;
5218 ins_compl_show_pum();
5219 setcursor();
5220 RedrawingDisabled = n;
5221 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005222 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005223 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005224
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 return OK;
5226}
5227
5228/*
5229 * Looks in the first "len" chars. of "src" for search-metachars.
5230 * If dest is not NULL the chars. are copied there quoting (with
5231 * a backslash) the metachars, and dest would be NUL terminated.
5232 * Returns the length (needed) of dest
5233 */
5234 static int
5235quote_meta(dest, src, len)
5236 char_u *dest;
5237 char_u *src;
5238 int len;
5239{
5240 int m;
5241
5242 for (m = len; --len >= 0; src++)
5243 {
5244 switch (*src)
5245 {
5246 case '.':
5247 case '*':
5248 case '[':
5249 if (ctrl_x_mode == CTRL_X_DICTIONARY
5250 || ctrl_x_mode == CTRL_X_THESAURUS)
5251 break;
5252 case '~':
5253 if (!p_magic) /* quote these only if magic is set */
5254 break;
5255 case '\\':
5256 if (ctrl_x_mode == CTRL_X_DICTIONARY
5257 || ctrl_x_mode == CTRL_X_THESAURUS)
5258 break;
5259 case '^': /* currently it's not needed. */
5260 case '$':
5261 m++;
5262 if (dest != NULL)
5263 *dest++ = '\\';
5264 break;
5265 }
5266 if (dest != NULL)
5267 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005268# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005269 /* Copy remaining bytes of a multibyte character. */
5270 if (has_mbyte)
5271 {
5272 int i, mb_len;
5273
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005274 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005275 if (mb_len > 0 && len >= mb_len)
5276 for (i = 0; i < mb_len; ++i)
5277 {
5278 --len;
5279 ++src;
5280 if (dest != NULL)
5281 *dest++ = *src;
5282 }
5283 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005284# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 }
5286 if (dest != NULL)
5287 *dest = NUL;
5288
5289 return m;
5290}
5291#endif /* FEAT_INS_EXPAND */
5292
5293/*
5294 * Next character is interpreted literally.
5295 * A one, two or three digit decimal number is interpreted as its byte value.
5296 * If one or two digits are entered, the next character is given to vungetc().
5297 * For Unicode a character > 255 may be returned.
5298 */
5299 int
5300get_literal()
5301{
5302 int cc;
5303 int nc;
5304 int i;
5305 int hex = FALSE;
5306 int octal = FALSE;
5307#ifdef FEAT_MBYTE
5308 int unicode = 0;
5309#endif
5310
5311 if (got_int)
5312 return Ctrl_C;
5313
5314#ifdef FEAT_GUI
5315 /*
5316 * In GUI there is no point inserting the internal code for a special key.
5317 * It is more useful to insert the string "<KEY>" instead. This would
5318 * probably be useful in a text window too, but it would not be
5319 * vi-compatible (maybe there should be an option for it?) -- webb
5320 */
5321 if (gui.in_use)
5322 ++allow_keys;
5323#endif
5324#ifdef USE_ON_FLY_SCROLL
5325 dont_scroll = TRUE; /* disallow scrolling here */
5326#endif
5327 ++no_mapping; /* don't map the next key hits */
5328 cc = 0;
5329 i = 0;
5330 for (;;)
5331 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005332 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333#ifdef FEAT_CMDL_INFO
5334 if (!(State & CMDLINE)
5335# ifdef FEAT_MBYTE
5336 && MB_BYTE2LEN_CHECK(nc) == 1
5337# endif
5338 )
5339 add_to_showcmd(nc);
5340#endif
5341 if (nc == 'x' || nc == 'X')
5342 hex = TRUE;
5343 else if (nc == 'o' || nc == 'O')
5344 octal = TRUE;
5345#ifdef FEAT_MBYTE
5346 else if (nc == 'u' || nc == 'U')
5347 unicode = nc;
5348#endif
5349 else
5350 {
5351 if (hex
5352#ifdef FEAT_MBYTE
5353 || unicode != 0
5354#endif
5355 )
5356 {
5357 if (!vim_isxdigit(nc))
5358 break;
5359 cc = cc * 16 + hex2nr(nc);
5360 }
5361 else if (octal)
5362 {
5363 if (nc < '0' || nc > '7')
5364 break;
5365 cc = cc * 8 + nc - '0';
5366 }
5367 else
5368 {
5369 if (!VIM_ISDIGIT(nc))
5370 break;
5371 cc = cc * 10 + nc - '0';
5372 }
5373
5374 ++i;
5375 }
5376
5377 if (cc > 255
5378#ifdef FEAT_MBYTE
5379 && unicode == 0
5380#endif
5381 )
5382 cc = 255; /* limit range to 0-255 */
5383 nc = 0;
5384
5385 if (hex) /* hex: up to two chars */
5386 {
5387 if (i >= 2)
5388 break;
5389 }
5390#ifdef FEAT_MBYTE
5391 else if (unicode) /* Unicode: up to four or eight chars */
5392 {
5393 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5394 break;
5395 }
5396#endif
5397 else if (i >= 3) /* decimal or octal: up to three chars */
5398 break;
5399 }
5400 if (i == 0) /* no number entered */
5401 {
5402 if (nc == K_ZERO) /* NUL is stored as NL */
5403 {
5404 cc = '\n';
5405 nc = 0;
5406 }
5407 else
5408 {
5409 cc = nc;
5410 nc = 0;
5411 }
5412 }
5413
5414 if (cc == 0) /* NUL is stored as NL */
5415 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005416#ifdef FEAT_MBYTE
5417 if (enc_dbcs && (cc & 0xff) == 0)
5418 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5419 second byte will cause trouble! */
5420#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005421
5422 --no_mapping;
5423#ifdef FEAT_GUI
5424 if (gui.in_use)
5425 --allow_keys;
5426#endif
5427 if (nc)
5428 vungetc(nc);
5429 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5430 return cc;
5431}
5432
5433/*
5434 * Insert character, taking care of special keys and mod_mask
5435 */
5436 static void
5437insert_special(c, allow_modmask, ctrlv)
5438 int c;
5439 int allow_modmask;
5440 int ctrlv; /* c was typed after CTRL-V */
5441{
5442 char_u *p;
5443 int len;
5444
5445 /*
5446 * Special function key, translate into "<Key>". Up to the last '>' is
5447 * inserted with ins_str(), so as not to replace characters in replace
5448 * mode.
5449 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5450 * unless 'allow_modmask' is TRUE.
5451 */
5452#ifdef MACOS
5453 /* Command-key never produces a normal key */
5454 if (mod_mask & MOD_MASK_CMD)
5455 allow_modmask = TRUE;
5456#endif
5457 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5458 {
5459 p = get_special_key_name(c, mod_mask);
5460 len = (int)STRLEN(p);
5461 c = p[len - 1];
5462 if (len > 2)
5463 {
5464 if (stop_arrow() == FAIL)
5465 return;
5466 p[len - 1] = NUL;
5467 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005468 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005469 ctrlv = FALSE;
5470 }
5471 }
5472 if (stop_arrow() == OK)
5473 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5474}
5475
5476/*
5477 * Special characters in this context are those that need processing other
5478 * than the simple insertion that can be performed here. This includes ESC
5479 * which terminates the insert, and CR/NL which need special processing to
5480 * open up a new line. This routine tries to optimize insertions performed by
5481 * the "redo", "undo" or "put" commands, so it needs to know when it should
5482 * stop and defer processing to the "normal" mechanism.
5483 * '0' and '^' are special, because they can be followed by CTRL-D.
5484 */
5485#ifdef EBCDIC
5486# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5487#else
5488# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5489#endif
5490
5491#ifdef FEAT_MBYTE
5492# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5493#else
5494# define WHITECHAR(cc) vim_iswhite(cc)
5495#endif
5496
5497 void
5498insertchar(c, flags, second_indent)
5499 int c; /* character to insert or NUL */
5500 int flags; /* INSCHAR_FORMAT, etc. */
5501 int second_indent; /* indent for second line if >= 0 */
5502{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005503 int textwidth;
5504#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005505 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508
5509 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5510 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005511
5512 /*
5513 * Try to break the line in two or more pieces when:
5514 * - Always do this if we have been called to do formatting only.
5515 * - Always do this when 'formatoptions' has the 'a' flag and the line
5516 * ends in white space.
5517 * - Otherwise:
5518 * - Don't do this if inserting a blank
5519 * - Don't do this if an existing character is being replaced, unless
5520 * we're in VREPLACE mode.
5521 * - Do this if the cursor is not on the line where insert started
5522 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5523 * before the insert.
5524 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5525 * before 'textwidth'
5526 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005527 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005528 && ((flags & INSCHAR_FORMAT)
5529 || (!vim_iswhite(c)
5530 && !((State & REPLACE_FLAG)
5531#ifdef FEAT_VREPLACE
5532 && !(State & VREPLACE_FLAG)
5533#endif
5534 && *ml_get_cursor() != NUL)
5535 && (curwin->w_cursor.lnum != Insstart.lnum
5536 || ((!has_format_option(FO_INS_LONG)
5537 || Insstart_textlen <= (colnr_T)textwidth)
5538 && (!fo_ins_blank
5539 || Insstart_blank_vcol <= (colnr_T)textwidth
5540 ))))))
5541 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005542 /* Format with 'formatexpr' when it's set. Use internal formatting
5543 * when 'formatexpr' isn't set or it returns non-zero. */
5544#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005545 int do_internal = TRUE;
5546
Bram Moolenaar81a82092008-03-12 16:27:00 +00005547 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005548 {
5549 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5550 /* It may be required to save for undo again, e.g. when setline()
5551 * was called. */
5552 ins_need_undo = TRUE;
5553 }
5554 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005555#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005556 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005557 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005558
Bram Moolenaar071d4272004-06-13 20:20:40 +00005559 if (c == NUL) /* only formatting was wanted */
5560 return;
5561
5562#ifdef FEAT_COMMENTS
5563 /* Check whether this character should end a comment. */
5564 if (did_ai && (int)c == end_comment_pending)
5565 {
5566 char_u *line;
5567 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5568 int middle_len, end_len;
5569 int i;
5570
5571 /*
5572 * Need to remove existing (middle) comment leader and insert end
5573 * comment leader. First, check what comment leader we can find.
5574 */
5575 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5576 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5577 {
5578 /* Skip middle-comment string */
5579 while (*p && p[-1] != ':') /* find end of middle flags */
5580 ++p;
5581 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5582 /* Don't count trailing white space for middle_len */
5583 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5584 --middle_len;
5585
5586 /* Find the end-comment string */
5587 while (*p && p[-1] != ':') /* find end of end flags */
5588 ++p;
5589 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5590
5591 /* Skip white space before the cursor */
5592 i = curwin->w_cursor.col;
5593 while (--i >= 0 && vim_iswhite(line[i]))
5594 ;
5595 i++;
5596
5597 /* Skip to before the middle leader */
5598 i -= middle_len;
5599
5600 /* Check some expected things before we go on */
5601 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5602 {
5603 /* Backspace over all the stuff we want to replace */
5604 backspace_until_column(i);
5605
5606 /*
5607 * Insert the end-comment string, except for the last
5608 * character, which will get inserted as normal later.
5609 */
5610 ins_bytes_len(lead_end, end_len - 1);
5611 }
5612 }
5613 }
5614 end_comment_pending = NUL;
5615#endif
5616
5617 did_ai = FALSE;
5618#ifdef FEAT_SMARTINDENT
5619 did_si = FALSE;
5620 can_si = FALSE;
5621 can_si_back = FALSE;
5622#endif
5623
5624 /*
5625 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5626 * This speeds up normal text input considerably.
5627 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5628 * need to re-indent at a ':', or any other character (but not what
5629 * 'paste' is set)..
5630 */
5631#ifdef USE_ON_FLY_SCROLL
5632 dont_scroll = FALSE; /* allow scrolling here */
5633#endif
5634
5635 if ( !ISSPECIAL(c)
5636#ifdef FEAT_MBYTE
5637 && (!has_mbyte || (*mb_char2len)(c) == 1)
5638#endif
5639 && vpeekc() != NUL
5640 && !(State & REPLACE_FLAG)
5641#ifdef FEAT_CINDENT
5642 && !cindent_on()
5643#endif
5644#ifdef FEAT_RIGHTLEFT
5645 && !p_ri
5646#endif
5647 )
5648 {
5649#define INPUT_BUFLEN 100
5650 char_u buf[INPUT_BUFLEN + 1];
5651 int i;
5652 colnr_T virtcol = 0;
5653
5654 buf[0] = c;
5655 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005656 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 virtcol = get_nolist_virtcol();
5658 /*
5659 * Stop the string when:
5660 * - no more chars available
5661 * - finding a special character (command key)
5662 * - buffer is full
5663 * - running into the 'textwidth' boundary
5664 * - need to check for abbreviation: A non-word char after a word-char
5665 */
5666 while ( (c = vpeekc()) != NUL
5667 && !ISSPECIAL(c)
5668#ifdef FEAT_MBYTE
5669 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5670#endif
5671 && i < INPUT_BUFLEN
5672 && (textwidth == 0
5673 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5674 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5675 {
5676#ifdef FEAT_RIGHTLEFT
5677 c = vgetc();
5678 if (p_hkmap && KeyTyped)
5679 c = hkmap(c); /* Hebrew mode mapping */
5680# ifdef FEAT_FKMAP
5681 if (p_fkmap && KeyTyped)
5682 c = fkmap(c); /* Farsi mode mapping */
5683# endif
5684 buf[i++] = c;
5685#else
5686 buf[i++] = vgetc();
5687#endif
5688 }
5689
5690#ifdef FEAT_DIGRAPHS
5691 do_digraph(-1); /* clear digraphs */
5692 do_digraph(buf[i-1]); /* may be the start of a digraph */
5693#endif
5694 buf[i] = NUL;
5695 ins_str(buf);
5696 if (flags & INSCHAR_CTRLV)
5697 {
5698 redo_literal(*buf);
5699 i = 1;
5700 }
5701 else
5702 i = 0;
5703 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005704 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005705 }
5706 else
5707 {
5708#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005709 int cc;
5710
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5712 {
5713 char_u buf[MB_MAXBYTES + 1];
5714
5715 (*mb_char2bytes)(c, buf);
5716 buf[cc] = NUL;
5717 ins_char_bytes(buf, cc);
5718 AppendCharToRedobuff(c);
5719 }
5720 else
5721#endif
5722 {
5723 ins_char(c);
5724 if (flags & INSCHAR_CTRLV)
5725 redo_literal(c);
5726 else
5727 AppendCharToRedobuff(c);
5728 }
5729 }
5730}
5731
5732/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005733 * Format text at the current insert position.
5734 */
5735 static void
5736internal_format(textwidth, second_indent, flags, format_only)
5737 int textwidth;
5738 int second_indent;
5739 int flags;
5740 int format_only;
5741{
5742 int cc;
5743 int save_char = NUL;
5744 int haveto_redraw = FALSE;
5745 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5746#ifdef FEAT_MBYTE
5747 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5748#endif
5749 int fo_white_par = has_format_option(FO_WHITE_PAR);
5750 int first_line = TRUE;
5751#ifdef FEAT_COMMENTS
5752 colnr_T leader_len;
5753 int no_leader = FALSE;
5754 int do_comments = (flags & INSCHAR_DO_COM);
5755#endif
5756
5757 /*
5758 * When 'ai' is off we don't want a space under the cursor to be
5759 * deleted. Replace it with an 'x' temporarily.
5760 */
5761 if (!curbuf->b_p_ai)
5762 {
5763 cc = gchar_cursor();
5764 if (vim_iswhite(cc))
5765 {
5766 save_char = cc;
5767 pchar_cursor('x');
5768 }
5769 }
5770
5771 /*
5772 * Repeat breaking lines, until the current line is not too long.
5773 */
5774 while (!got_int)
5775 {
5776 int startcol; /* Cursor column at entry */
5777 int wantcol; /* column at textwidth border */
5778 int foundcol; /* column for start of spaces */
5779 int end_foundcol = 0; /* column for start of word */
5780 colnr_T len;
5781 colnr_T virtcol;
5782#ifdef FEAT_VREPLACE
5783 int orig_col = 0;
5784 char_u *saved_text = NULL;
5785#endif
5786 colnr_T col;
5787
5788 virtcol = get_nolist_virtcol();
5789 if (virtcol < (colnr_T)textwidth)
5790 break;
5791
5792#ifdef FEAT_COMMENTS
5793 if (no_leader)
5794 do_comments = FALSE;
5795 else if (!(flags & INSCHAR_FORMAT)
5796 && has_format_option(FO_WRAP_COMS))
5797 do_comments = TRUE;
5798
5799 /* Don't break until after the comment leader */
5800 if (do_comments)
5801 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5802 else
5803 leader_len = 0;
5804
5805 /* If the line doesn't start with a comment leader, then don't
5806 * start one in a following broken line. Avoids that a %word
5807 * moved to the start of the next line causes all following lines
5808 * to start with %. */
5809 if (leader_len == 0)
5810 no_leader = TRUE;
5811#endif
5812 if (!(flags & INSCHAR_FORMAT)
5813#ifdef FEAT_COMMENTS
5814 && leader_len == 0
5815#endif
5816 && !has_format_option(FO_WRAP))
5817
5818 {
5819 textwidth = 0;
5820 break;
5821 }
5822 if ((startcol = curwin->w_cursor.col) == 0)
5823 break;
5824
5825 /* find column of textwidth border */
5826 coladvance((colnr_T)textwidth);
5827 wantcol = curwin->w_cursor.col;
5828
5829 curwin->w_cursor.col = startcol - 1;
5830#ifdef FEAT_MBYTE
5831 /* Correct cursor for multi-byte character. */
5832 if (has_mbyte)
5833 mb_adjust_cursor();
5834#endif
5835 foundcol = 0;
5836
5837 /*
5838 * Find position to break at.
5839 * Stop at first entered white when 'formatoptions' has 'v'
5840 */
5841 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5842 || curwin->w_cursor.lnum != Insstart.lnum
5843 || curwin->w_cursor.col >= Insstart.col)
5844 {
5845 cc = gchar_cursor();
5846 if (WHITECHAR(cc))
5847 {
5848 /* remember position of blank just before text */
5849 end_foundcol = curwin->w_cursor.col;
5850
5851 /* find start of sequence of blanks */
5852 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5853 {
5854 dec_cursor();
5855 cc = gchar_cursor();
5856 }
5857 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5858 break; /* only spaces in front of text */
5859#ifdef FEAT_COMMENTS
5860 /* Don't break until after the comment leader */
5861 if (curwin->w_cursor.col < leader_len)
5862 break;
5863#endif
5864 if (has_format_option(FO_ONE_LETTER))
5865 {
5866 /* do not break after one-letter words */
5867 if (curwin->w_cursor.col == 0)
5868 break; /* one-letter word at begin */
5869
5870 col = curwin->w_cursor.col;
5871 dec_cursor();
5872 cc = gchar_cursor();
5873
5874 if (WHITECHAR(cc))
5875 continue; /* one-letter, continue */
5876 curwin->w_cursor.col = col;
5877 }
5878#ifdef FEAT_MBYTE
5879 if (has_mbyte)
5880 foundcol = curwin->w_cursor.col
5881 + (*mb_ptr2len)(ml_get_cursor());
5882 else
5883#endif
5884 foundcol = curwin->w_cursor.col + 1;
5885 if (curwin->w_cursor.col < (colnr_T)wantcol)
5886 break;
5887 }
5888#ifdef FEAT_MBYTE
5889 else if (cc >= 0x100 && fo_multibyte
5890 && curwin->w_cursor.col <= (colnr_T)wantcol)
5891 {
5892 /* Break after or before a multi-byte character. */
5893 foundcol = curwin->w_cursor.col;
5894 if (curwin->w_cursor.col < (colnr_T)wantcol)
5895 foundcol += (*mb_char2len)(cc);
5896 end_foundcol = foundcol;
5897 break;
5898 }
5899#endif
5900 if (curwin->w_cursor.col == 0)
5901 break;
5902 dec_cursor();
5903 }
5904
5905 if (foundcol == 0) /* no spaces, cannot break line */
5906 {
5907 curwin->w_cursor.col = startcol;
5908 break;
5909 }
5910
5911 /* Going to break the line, remove any "$" now. */
5912 undisplay_dollar();
5913
5914 /*
5915 * Offset between cursor position and line break is used by replace
5916 * stack functions. VREPLACE does not use this, and backspaces
5917 * over the text instead.
5918 */
5919#ifdef FEAT_VREPLACE
5920 if (State & VREPLACE_FLAG)
5921 orig_col = startcol; /* Will start backspacing from here */
5922 else
5923#endif
5924 replace_offset = startcol - end_foundcol - 1;
5925
5926 /*
5927 * adjust startcol for spaces that will be deleted and
5928 * characters that will remain on top line
5929 */
5930 curwin->w_cursor.col = foundcol;
5931 while (cc = gchar_cursor(), WHITECHAR(cc))
5932 inc_cursor();
5933 startcol -= curwin->w_cursor.col;
5934 if (startcol < 0)
5935 startcol = 0;
5936
5937#ifdef FEAT_VREPLACE
5938 if (State & VREPLACE_FLAG)
5939 {
5940 /*
5941 * In VREPLACE mode, we will backspace over the text to be
5942 * wrapped, so save a copy now to put on the next line.
5943 */
5944 saved_text = vim_strsave(ml_get_cursor());
5945 curwin->w_cursor.col = orig_col;
5946 if (saved_text == NULL)
5947 break; /* Can't do it, out of memory */
5948 saved_text[startcol] = NUL;
5949
5950 /* Backspace over characters that will move to the next line */
5951 if (!fo_white_par)
5952 backspace_until_column(foundcol);
5953 }
5954 else
5955#endif
5956 {
5957 /* put cursor after pos. to break line */
5958 if (!fo_white_par)
5959 curwin->w_cursor.col = foundcol;
5960 }
5961
5962 /*
5963 * Split the line just before the margin.
5964 * Only insert/delete lines, but don't really redraw the window.
5965 */
5966 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5967 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5968#ifdef FEAT_COMMENTS
5969 + (do_comments ? OPENLINE_DO_COM : 0)
5970#endif
5971 , old_indent);
5972 old_indent = 0;
5973
5974 replace_offset = 0;
5975 if (first_line)
5976 {
5977 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5978 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5979 if (second_indent >= 0)
5980 {
5981#ifdef FEAT_VREPLACE
5982 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00005983 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005984 else
5985#endif
5986 (void)set_indent(second_indent, SIN_CHANGED);
5987 }
5988 first_line = FALSE;
5989 }
5990
5991#ifdef FEAT_VREPLACE
5992 if (State & VREPLACE_FLAG)
5993 {
5994 /*
5995 * In VREPLACE mode we have backspaced over the text to be
5996 * moved, now we re-insert it into the new line.
5997 */
5998 ins_bytes(saved_text);
5999 vim_free(saved_text);
6000 }
6001 else
6002#endif
6003 {
6004 /*
6005 * Check if cursor is not past the NUL off the line, cindent
6006 * may have added or removed indent.
6007 */
6008 curwin->w_cursor.col += startcol;
6009 len = (colnr_T)STRLEN(ml_get_curline());
6010 if (curwin->w_cursor.col > len)
6011 curwin->w_cursor.col = len;
6012 }
6013
6014 haveto_redraw = TRUE;
6015#ifdef FEAT_CINDENT
6016 can_cindent = TRUE;
6017#endif
6018 /* moved the cursor, don't autoindent or cindent now */
6019 did_ai = FALSE;
6020#ifdef FEAT_SMARTINDENT
6021 did_si = FALSE;
6022 can_si = FALSE;
6023 can_si_back = FALSE;
6024#endif
6025 line_breakcheck();
6026 }
6027
6028 if (save_char != NUL) /* put back space after cursor */
6029 pchar_cursor(save_char);
6030
6031 if (!format_only && haveto_redraw)
6032 {
6033 update_topline();
6034 redraw_curbuf_later(VALID);
6035 }
6036}
6037
6038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006039 * Called after inserting or deleting text: When 'formatoptions' includes the
6040 * 'a' flag format from the current line until the end of the paragraph.
6041 * Keep the cursor at the same position relative to the text.
6042 * The caller must have saved the cursor line for undo, following ones will be
6043 * saved here.
6044 */
6045 void
6046auto_format(trailblank, prev_line)
6047 int trailblank; /* when TRUE also format with trailing blank */
6048 int prev_line; /* may start in previous line */
6049{
6050 pos_T pos;
6051 colnr_T len;
6052 char_u *old;
6053 char_u *new, *pnew;
6054 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006055 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006056
6057 if (!has_format_option(FO_AUTO))
6058 return;
6059
6060 pos = curwin->w_cursor;
6061 old = ml_get_curline();
6062
6063 /* may remove added space */
6064 check_auto_format(FALSE);
6065
6066 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6067 * user might insert normal text next. Also skip formatting when "1" is
6068 * in 'formatoptions' and there is a single character before the cursor.
6069 * Otherwise the line would be broken and when typing another non-white
6070 * next they are not joined back together. */
6071 wasatend = (pos.col == STRLEN(old));
6072 if (*old != NUL && !trailblank && wasatend)
6073 {
6074 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006075 cc = gchar_cursor();
6076 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6077 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006078 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006079 cc = gchar_cursor();
6080 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006081 {
6082 curwin->w_cursor = pos;
6083 return;
6084 }
6085 curwin->w_cursor = pos;
6086 }
6087
6088#ifdef FEAT_COMMENTS
6089 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6090 * comments. */
6091 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6092 && get_leader_len(old, NULL, FALSE) == 0)
6093 return;
6094#endif
6095
6096 /*
6097 * May start formatting in a previous line, so that after "x" a word is
6098 * moved to the previous line if it fits there now. Only when this is not
6099 * the start of a paragraph.
6100 */
6101 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6102 {
6103 --curwin->w_cursor.lnum;
6104 if (u_save_cursor() == FAIL)
6105 return;
6106 }
6107
6108 /*
6109 * Do the formatting and restore the cursor position. "saved_cursor" will
6110 * be adjusted for the text formatting.
6111 */
6112 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006113 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006114 curwin->w_cursor = saved_cursor;
6115 saved_cursor.lnum = 0;
6116
6117 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6118 {
6119 /* "cannot happen" */
6120 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6121 coladvance((colnr_T)MAXCOL);
6122 }
6123 else
6124 check_cursor_col();
6125
6126 /* Insert mode: If the cursor is now after the end of the line while it
6127 * previously wasn't, the line was broken. Because of the rule above we
6128 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6129 * formatted. */
6130 if (!wasatend && has_format_option(FO_WHITE_PAR))
6131 {
6132 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006133 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006134 if (curwin->w_cursor.col == len)
6135 {
6136 pnew = vim_strnsave(new, len + 2);
6137 pnew[len] = ' ';
6138 pnew[len + 1] = NUL;
6139 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6140 /* remove the space later */
6141 did_add_space = TRUE;
6142 }
6143 else
6144 /* may remove added space */
6145 check_auto_format(FALSE);
6146 }
6147
6148 check_cursor();
6149}
6150
6151/*
6152 * When an extra space was added to continue a paragraph for auto-formatting,
6153 * delete it now. The space must be under the cursor, just after the insert
6154 * position.
6155 */
6156 static void
6157check_auto_format(end_insert)
6158 int end_insert; /* TRUE when ending Insert mode */
6159{
6160 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006161 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006162
6163 if (did_add_space)
6164 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006165 cc = gchar_cursor();
6166 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006167 /* Somehow the space was removed already. */
6168 did_add_space = FALSE;
6169 else
6170 {
6171 if (!end_insert)
6172 {
6173 inc_cursor();
6174 c = gchar_cursor();
6175 dec_cursor();
6176 }
6177 if (c != NUL)
6178 {
6179 /* The space is no longer at the end of the line, delete it. */
6180 del_char(FALSE);
6181 did_add_space = FALSE;
6182 }
6183 }
6184 }
6185}
6186
6187/*
6188 * Find out textwidth to be used for formatting:
6189 * if 'textwidth' option is set, use it
6190 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6191 * if invalid value, use 0.
6192 * Set default to window width (maximum 79) for "gq" operator.
6193 */
6194 int
6195comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006196 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006197{
6198 int textwidth;
6199
6200 textwidth = curbuf->b_p_tw;
6201 if (textwidth == 0 && curbuf->b_p_wm)
6202 {
6203 /* The width is the window width minus 'wrapmargin' minus all the
6204 * things that add to the margin. */
6205 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6206#ifdef FEAT_CMDWIN
6207 if (cmdwin_type != 0)
6208 textwidth -= 1;
6209#endif
6210#ifdef FEAT_FOLDING
6211 textwidth -= curwin->w_p_fdc;
6212#endif
6213#ifdef FEAT_SIGNS
6214 if (curwin->w_buffer->b_signlist != NULL
6215# ifdef FEAT_NETBEANS_INTG
6216 || usingNetbeans
6217# endif
6218 )
6219 textwidth -= 1;
6220#endif
6221 if (curwin->w_p_nu)
6222 textwidth -= 8;
6223 }
6224 if (textwidth < 0)
6225 textwidth = 0;
6226 if (ff && textwidth == 0)
6227 {
6228 textwidth = W_WIDTH(curwin) - 1;
6229 if (textwidth > 79)
6230 textwidth = 79;
6231 }
6232 return textwidth;
6233}
6234
6235/*
6236 * Put a character in the redo buffer, for when just after a CTRL-V.
6237 */
6238 static void
6239redo_literal(c)
6240 int c;
6241{
6242 char_u buf[10];
6243
6244 /* Only digits need special treatment. Translate them into a string of
6245 * three digits. */
6246 if (VIM_ISDIGIT(c))
6247 {
6248 sprintf((char *)buf, "%03d", c);
6249 AppendToRedobuff(buf);
6250 }
6251 else
6252 AppendCharToRedobuff(c);
6253}
6254
6255/*
6256 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006257 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 */
6259 static void
6260start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006261 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006262{
6263 if (!arrow_used) /* something has been inserted */
6264 {
6265 AppendToRedobuff(ESC_STR);
6266 stop_insert(end_insert_pos, FALSE);
6267 arrow_used = TRUE; /* this means we stopped the current insert */
6268 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006269#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006270 check_spell_redraw();
6271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006272}
6273
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006274#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006275/*
6276 * If we skipped highlighting word at cursor, do it now.
6277 * It may be skipped again, thus reset spell_redraw_lnum first.
6278 */
6279 static void
6280check_spell_redraw()
6281{
6282 if (spell_redraw_lnum != 0)
6283 {
6284 linenr_T lnum = spell_redraw_lnum;
6285
6286 spell_redraw_lnum = 0;
6287 redrawWinline(lnum, FALSE);
6288 }
6289}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006290
6291/*
6292 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6293 * spelled word, if there is one.
6294 */
6295 static void
6296spell_back_to_badword()
6297{
6298 pos_T tpos = curwin->w_cursor;
6299
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006300 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006301 if (curwin->w_cursor.col != tpos.col)
6302 start_arrow(&tpos);
6303}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006304#endif
6305
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306/*
6307 * stop_arrow() is called before a change is made in insert mode.
6308 * If an arrow key has been used, start a new insertion.
6309 * Returns FAIL if undo is impossible, shouldn't insert then.
6310 */
6311 int
6312stop_arrow()
6313{
6314 if (arrow_used)
6315 {
6316 if (u_save_cursor() == OK)
6317 {
6318 arrow_used = FALSE;
6319 ins_need_undo = FALSE;
6320 }
6321 Insstart = curwin->w_cursor; /* new insertion starts here */
6322 Insstart_textlen = linetabsize(ml_get_curline());
6323 ai_col = 0;
6324#ifdef FEAT_VREPLACE
6325 if (State & VREPLACE_FLAG)
6326 {
6327 orig_line_count = curbuf->b_ml.ml_line_count;
6328 vr_lines_changed = 1;
6329 }
6330#endif
6331 ResetRedobuff();
6332 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006333 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006334 }
6335 else if (ins_need_undo)
6336 {
6337 if (u_save_cursor() == OK)
6338 ins_need_undo = FALSE;
6339 }
6340
6341#ifdef FEAT_FOLDING
6342 /* Always open fold at the cursor line when inserting something. */
6343 foldOpenCursor();
6344#endif
6345
6346 return (arrow_used || ins_need_undo ? FAIL : OK);
6347}
6348
6349/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006350 * Do a few things to stop inserting.
6351 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6352 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006353 */
6354 static void
6355stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006356 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006357 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006358{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006359 int cc;
6360 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361
6362 stop_redo_ins();
6363 replace_flush(); /* abandon replace stack */
6364
6365 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006366 * Save the inserted text for later redo with ^@ and CTRL-A.
6367 * Don't do it when "restart_edit" was set and nothing was inserted,
6368 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006369 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006370 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006371 if (did_restart_edit == 0 || (ptr != NULL
6372 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006373 {
6374 vim_free(last_insert);
6375 last_insert = ptr;
6376 last_insert_skip = new_insert_skip;
6377 }
6378 else
6379 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006380
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006381 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006382 {
6383 /* Auto-format now. It may seem strange to do this when stopping an
6384 * insertion (or moving the cursor), but it's required when appending
6385 * a line and having it end in a space. But only do it when something
6386 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006387 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006388 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006389 pos_T tpos = curwin->w_cursor;
6390
Bram Moolenaar071d4272004-06-13 20:20:40 +00006391 /* When the cursor is at the end of the line after a space the
6392 * formatting will move it to the following word. Avoid that by
6393 * moving the cursor onto the space. */
6394 cc = 'x';
6395 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6396 {
6397 dec_cursor();
6398 cc = gchar_cursor();
6399 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006400 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006401 }
6402
6403 auto_format(TRUE, FALSE);
6404
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006405 if (vim_iswhite(cc))
6406 {
6407 if (gchar_cursor() != NUL)
6408 inc_cursor();
6409#ifdef FEAT_VIRTUALEDIT
6410 /* If the cursor is still at the same character, also keep
6411 * the "coladd". */
6412 if (gchar_cursor() == NUL
6413 && curwin->w_cursor.lnum == tpos.lnum
6414 && curwin->w_cursor.col == tpos.col)
6415 curwin->w_cursor.coladd = tpos.coladd;
6416#endif
6417 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006418 }
6419
6420 /* If a space was inserted for auto-formatting, remove it now. */
6421 check_auto_format(TRUE);
6422
6423 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006424 * of the line, and put the cursor back.
6425 * Do this when ESC was used or moving the cursor up/down. */
6426 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006427 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006428 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006429 pos_T tpos = curwin->w_cursor;
6430
6431 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006432 for (;;)
6433 {
6434 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6435 --curwin->w_cursor.col;
6436 cc = gchar_cursor();
6437 if (!vim_iswhite(cc))
6438 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006439 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006440 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006441 if (curwin->w_cursor.lnum != tpos.lnum)
6442 curwin->w_cursor = tpos;
6443 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6445
6446#ifdef FEAT_VISUAL
6447 /* <C-S-Right> may have started Visual mode, adjust the position for
6448 * deleted characters. */
6449 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6450 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006451 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006452 if (VIsual.col > (colnr_T)cc)
6453 {
6454 VIsual.col = cc;
6455# ifdef FEAT_VIRTUALEDIT
6456 VIsual.coladd = 0;
6457# endif
6458 }
6459 }
6460#endif
6461 }
6462 }
6463 did_ai = FALSE;
6464#ifdef FEAT_SMARTINDENT
6465 did_si = FALSE;
6466 can_si = FALSE;
6467 can_si_back = FALSE;
6468#endif
6469
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006470 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6471 * now in a different buffer. */
6472 if (end_insert_pos != NULL)
6473 {
6474 curbuf->b_op_start = Insstart;
6475 curbuf->b_op_end = *end_insert_pos;
6476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006477}
6478
6479/*
6480 * Set the last inserted text to a single character.
6481 * Used for the replace command.
6482 */
6483 void
6484set_last_insert(c)
6485 int c;
6486{
6487 char_u *s;
6488
6489 vim_free(last_insert);
6490#ifdef FEAT_MBYTE
6491 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6492#else
6493 last_insert = alloc(6);
6494#endif
6495 if (last_insert != NULL)
6496 {
6497 s = last_insert;
6498 /* Use the CTRL-V only when entering a special char */
6499 if (c < ' ' || c == DEL)
6500 *s++ = Ctrl_V;
6501 s = add_char2buf(c, s);
6502 *s++ = ESC;
6503 *s++ = NUL;
6504 last_insert_skip = 0;
6505 }
6506}
6507
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006508#if defined(EXITFREE) || defined(PROTO)
6509 void
6510free_last_insert()
6511{
6512 vim_free(last_insert);
6513 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006514# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006515 vim_free(compl_orig_text);
6516 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006517# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006518}
6519#endif
6520
Bram Moolenaar071d4272004-06-13 20:20:40 +00006521/*
6522 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6523 * and CSI. Handle multi-byte characters.
6524 * Returns a pointer to after the added bytes.
6525 */
6526 char_u *
6527add_char2buf(c, s)
6528 int c;
6529 char_u *s;
6530{
6531#ifdef FEAT_MBYTE
6532 char_u temp[MB_MAXBYTES];
6533 int i;
6534 int len;
6535
6536 len = (*mb_char2bytes)(c, temp);
6537 for (i = 0; i < len; ++i)
6538 {
6539 c = temp[i];
6540#endif
6541 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6542 if (c == K_SPECIAL)
6543 {
6544 *s++ = K_SPECIAL;
6545 *s++ = KS_SPECIAL;
6546 *s++ = KE_FILLER;
6547 }
6548#ifdef FEAT_GUI
6549 else if (c == CSI)
6550 {
6551 *s++ = CSI;
6552 *s++ = KS_EXTRA;
6553 *s++ = (int)KE_CSI;
6554 }
6555#endif
6556 else
6557 *s++ = c;
6558#ifdef FEAT_MBYTE
6559 }
6560#endif
6561 return s;
6562}
6563
6564/*
6565 * move cursor to start of line
6566 * if flags & BL_WHITE move to first non-white
6567 * if flags & BL_SOL move to first non-white if startofline is set,
6568 * otherwise keep "curswant" column
6569 * if flags & BL_FIX don't leave the cursor on a NUL.
6570 */
6571 void
6572beginline(flags)
6573 int flags;
6574{
6575 if ((flags & BL_SOL) && !p_sol)
6576 coladvance(curwin->w_curswant);
6577 else
6578 {
6579 curwin->w_cursor.col = 0;
6580#ifdef FEAT_VIRTUALEDIT
6581 curwin->w_cursor.coladd = 0;
6582#endif
6583
6584 if (flags & (BL_WHITE | BL_SOL))
6585 {
6586 char_u *ptr;
6587
6588 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6589 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6590 ++curwin->w_cursor.col;
6591 }
6592 curwin->w_set_curswant = TRUE;
6593 }
6594}
6595
6596/*
6597 * oneright oneleft cursor_down cursor_up
6598 *
6599 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006600 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006601 * Return OK when successful, FAIL when we hit a line of file boundary.
6602 */
6603
6604 int
6605oneright()
6606{
6607 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006608 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609
6610#ifdef FEAT_VIRTUALEDIT
6611 if (virtual_active())
6612 {
6613 pos_T prevpos = curwin->w_cursor;
6614
6615 /* Adjust for multi-wide char (excluding TAB) */
6616 ptr = ml_get_cursor();
6617 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006618# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006619 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006620# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006621 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006622# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006623 ))
6624 ? ptr2cells(ptr) : 1));
6625 curwin->w_set_curswant = TRUE;
6626 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6627 return (prevpos.col != curwin->w_cursor.col
6628 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6629 }
6630#endif
6631
6632 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006633 if (*ptr == NUL)
6634 return FAIL; /* already at the very end */
6635
Bram Moolenaar071d4272004-06-13 20:20:40 +00006636#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006637 if (has_mbyte)
6638 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006639 else
6640#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006641 l = 1;
6642
6643 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6644 * contains "onemore". */
6645 if (ptr[l] == NUL
6646#ifdef FEAT_VIRTUALEDIT
6647 && (ve_flags & VE_ONEMORE) == 0
6648#endif
6649 )
6650 return FAIL;
6651 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006652
6653 curwin->w_set_curswant = TRUE;
6654 return OK;
6655}
6656
6657 int
6658oneleft()
6659{
6660#ifdef FEAT_VIRTUALEDIT
6661 if (virtual_active())
6662 {
6663 int width;
6664 int v = getviscol();
6665
6666 if (v == 0)
6667 return FAIL;
6668
6669# ifdef FEAT_LINEBREAK
6670 /* We might get stuck on 'showbreak', skip over it. */
6671 width = 1;
6672 for (;;)
6673 {
6674 coladvance(v - width);
6675 /* getviscol() is slow, skip it when 'showbreak' is empty and
6676 * there are no multi-byte characters */
6677 if ((*p_sbr == NUL
6678# ifdef FEAT_MBYTE
6679 && !has_mbyte
6680# endif
6681 ) || getviscol() < v)
6682 break;
6683 ++width;
6684 }
6685# else
6686 coladvance(v - 1);
6687# endif
6688
6689 if (curwin->w_cursor.coladd == 1)
6690 {
6691 char_u *ptr;
6692
6693 /* Adjust for multi-wide char (not a TAB) */
6694 ptr = ml_get_cursor();
6695 if (*ptr != TAB && vim_isprintc(
6696# ifdef FEAT_MBYTE
6697 (*mb_ptr2char)(ptr)
6698# else
6699 *ptr
6700# endif
6701 ) && ptr2cells(ptr) > 1)
6702 curwin->w_cursor.coladd = 0;
6703 }
6704
6705 curwin->w_set_curswant = TRUE;
6706 return OK;
6707 }
6708#endif
6709
6710 if (curwin->w_cursor.col == 0)
6711 return FAIL;
6712
6713 curwin->w_set_curswant = TRUE;
6714 --curwin->w_cursor.col;
6715
6716#ifdef FEAT_MBYTE
6717 /* if the character on the left of the current cursor is a multi-byte
6718 * character, move to its first byte */
6719 if (has_mbyte)
6720 mb_adjust_cursor();
6721#endif
6722 return OK;
6723}
6724
6725 int
6726cursor_up(n, upd_topline)
6727 long n;
6728 int upd_topline; /* When TRUE: update topline */
6729{
6730 linenr_T lnum;
6731
6732 if (n > 0)
6733 {
6734 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006735 /* This fails if the cursor is already in the first line or the count
6736 * is larger than the line number and '-' is in 'cpoptions' */
6737 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006738 return FAIL;
6739 if (n >= lnum)
6740 lnum = 1;
6741 else
6742#ifdef FEAT_FOLDING
6743 if (hasAnyFolding(curwin))
6744 {
6745 /*
6746 * Count each sequence of folded lines as one logical line.
6747 */
6748 /* go to the the start of the current fold */
6749 (void)hasFolding(lnum, &lnum, NULL);
6750
6751 while (n--)
6752 {
6753 /* move up one line */
6754 --lnum;
6755 if (lnum <= 1)
6756 break;
6757 /* If we entered a fold, move to the beginning, unless in
6758 * Insert mode or when 'foldopen' contains "all": it will open
6759 * in a moment. */
6760 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6761 (void)hasFolding(lnum, &lnum, NULL);
6762 }
6763 if (lnum < 1)
6764 lnum = 1;
6765 }
6766 else
6767#endif
6768 lnum -= n;
6769 curwin->w_cursor.lnum = lnum;
6770 }
6771
6772 /* try to advance to the column we want to be at */
6773 coladvance(curwin->w_curswant);
6774
6775 if (upd_topline)
6776 update_topline(); /* make sure curwin->w_topline is valid */
6777
6778 return OK;
6779}
6780
6781/*
6782 * Cursor down a number of logical lines.
6783 */
6784 int
6785cursor_down(n, upd_topline)
6786 long n;
6787 int upd_topline; /* When TRUE: update topline */
6788{
6789 linenr_T lnum;
6790
6791 if (n > 0)
6792 {
6793 lnum = curwin->w_cursor.lnum;
6794#ifdef FEAT_FOLDING
6795 /* Move to last line of fold, will fail if it's the end-of-file. */
6796 (void)hasFolding(lnum, NULL, &lnum);
6797#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006798 /* This fails if the cursor is already in the last line or would move
6799 * beyound the last line and '-' is in 'cpoptions' */
6800 if (lnum >= curbuf->b_ml.ml_line_count
6801 || (lnum + n > curbuf->b_ml.ml_line_count
6802 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006803 return FAIL;
6804 if (lnum + n >= curbuf->b_ml.ml_line_count)
6805 lnum = curbuf->b_ml.ml_line_count;
6806 else
6807#ifdef FEAT_FOLDING
6808 if (hasAnyFolding(curwin))
6809 {
6810 linenr_T last;
6811
6812 /* count each sequence of folded lines as one logical line */
6813 while (n--)
6814 {
6815 if (hasFolding(lnum, NULL, &last))
6816 lnum = last + 1;
6817 else
6818 ++lnum;
6819 if (lnum >= curbuf->b_ml.ml_line_count)
6820 break;
6821 }
6822 if (lnum > curbuf->b_ml.ml_line_count)
6823 lnum = curbuf->b_ml.ml_line_count;
6824 }
6825 else
6826#endif
6827 lnum += n;
6828 curwin->w_cursor.lnum = lnum;
6829 }
6830
6831 /* try to advance to the column we want to be at */
6832 coladvance(curwin->w_curswant);
6833
6834 if (upd_topline)
6835 update_topline(); /* make sure curwin->w_topline is valid */
6836
6837 return OK;
6838}
6839
6840/*
6841 * Stuff the last inserted text in the read buffer.
6842 * Last_insert actually is a copy of the redo buffer, so we
6843 * first have to remove the command.
6844 */
6845 int
6846stuff_inserted(c, count, no_esc)
6847 int c; /* Command character to be inserted */
6848 long count; /* Repeat this many times */
6849 int no_esc; /* Don't add an ESC at the end */
6850{
6851 char_u *esc_ptr;
6852 char_u *ptr;
6853 char_u *last_ptr;
6854 char_u last = NUL;
6855
6856 ptr = get_last_insert();
6857 if (ptr == NULL)
6858 {
6859 EMSG(_(e_noinstext));
6860 return FAIL;
6861 }
6862
6863 /* may want to stuff the command character, to start Insert mode */
6864 if (c != NUL)
6865 stuffcharReadbuff(c);
6866 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6867 *esc_ptr = NUL; /* remove the ESC */
6868
6869 /* when the last char is either "0" or "^" it will be quoted if no ESC
6870 * comes after it OR if it will inserted more than once and "ptr"
6871 * starts with ^D. -- Acevedo
6872 */
6873 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6874 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6875 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6876 {
6877 last = *last_ptr;
6878 *last_ptr = NUL;
6879 }
6880
6881 do
6882 {
6883 stuffReadbuff(ptr);
6884 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6885 if (last)
6886 stuffReadbuff((char_u *)(last == '0'
6887 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6888 : IF_EB("\026^", CTRL_V_STR "^")));
6889 }
6890 while (--count > 0);
6891
6892 if (last)
6893 *last_ptr = last;
6894
6895 if (esc_ptr != NULL)
6896 *esc_ptr = ESC; /* put the ESC back */
6897
6898 /* may want to stuff a trailing ESC, to get out of Insert mode */
6899 if (!no_esc)
6900 stuffcharReadbuff(ESC);
6901
6902 return OK;
6903}
6904
6905 char_u *
6906get_last_insert()
6907{
6908 if (last_insert == NULL)
6909 return NULL;
6910 return last_insert + last_insert_skip;
6911}
6912
6913/*
6914 * Get last inserted string, and remove trailing <Esc>.
6915 * Returns pointer to allocated memory (must be freed) or NULL.
6916 */
6917 char_u *
6918get_last_insert_save()
6919{
6920 char_u *s;
6921 int len;
6922
6923 if (last_insert == NULL)
6924 return NULL;
6925 s = vim_strsave(last_insert + last_insert_skip);
6926 if (s != NULL)
6927 {
6928 len = (int)STRLEN(s);
6929 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6930 s[len - 1] = NUL;
6931 }
6932 return s;
6933}
6934
6935/*
6936 * Check the word in front of the cursor for an abbreviation.
6937 * Called when the non-id character "c" has been entered.
6938 * When an abbreviation is recognized it is removed from the text and
6939 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6940 */
6941 static int
6942echeck_abbr(c)
6943 int c;
6944{
6945 /* Don't check for abbreviation in paste mode, when disabled and just
6946 * after moving around with cursor keys. */
6947 if (p_paste || no_abbr || arrow_used)
6948 return FALSE;
6949
6950 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6951 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6952}
6953
6954/*
6955 * replace-stack functions
6956 *
6957 * When replacing characters, the replaced characters are remembered for each
6958 * new character. This is used to re-insert the old text when backspacing.
6959 *
6960 * There is a NUL headed list of characters for each character that is
6961 * currently in the file after the insertion point. When BS is used, one NUL
6962 * headed list is put back for the deleted character.
6963 *
6964 * For a newline, there are two NUL headed lists. One contains the characters
6965 * that the NL replaced. The extra one stores the characters after the cursor
6966 * that were deleted (always white space).
6967 *
6968 * Replace_offset is normally 0, in which case replace_push will add a new
6969 * character at the end of the stack. If replace_offset is not 0, that many
6970 * characters will be left on the stack above the newly inserted character.
6971 */
6972
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006973static char_u *replace_stack = NULL;
6974static long replace_stack_nr = 0; /* next entry in replace stack */
6975static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006976
6977 void
6978replace_push(c)
6979 int c; /* character that is replaced (NUL is none) */
6980{
6981 char_u *p;
6982
6983 if (replace_stack_nr < replace_offset) /* nothing to do */
6984 return;
6985 if (replace_stack_len <= replace_stack_nr)
6986 {
6987 replace_stack_len += 50;
6988 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6989 if (p == NULL) /* out of memory */
6990 {
6991 replace_stack_len -= 50;
6992 return;
6993 }
6994 if (replace_stack != NULL)
6995 {
6996 mch_memmove(p, replace_stack,
6997 (size_t)(replace_stack_nr * sizeof(char_u)));
6998 vim_free(replace_stack);
6999 }
7000 replace_stack = p;
7001 }
7002 p = replace_stack + replace_stack_nr - replace_offset;
7003 if (replace_offset)
7004 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7005 *p = c;
7006 ++replace_stack_nr;
7007}
7008
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007009#if defined(FEAT_MBYTE) || defined(PROTO)
7010/*
7011 * Push a character onto the replace stack. Handles a multi-byte character in
7012 * reverse byte order, so that the first byte is popped off first.
7013 * Return the number of bytes done (includes composing characters).
7014 */
7015 int
7016replace_push_mb(p)
7017 char_u *p;
7018{
7019 int l = (*mb_ptr2len)(p);
7020 int j;
7021
7022 for (j = l - 1; j >= 0; --j)
7023 replace_push(p[j]);
7024 return l;
7025}
7026#endif
7027
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007028#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007029/*
7030 * call replace_push(c) with replace_offset set to the first NUL.
7031 */
7032 static void
7033replace_push_off(c)
7034 int c;
7035{
7036 char_u *p;
7037
7038 p = replace_stack + replace_stack_nr;
7039 for (replace_offset = 1; replace_offset < replace_stack_nr;
7040 ++replace_offset)
7041 if (*--p == NUL)
7042 break;
7043 replace_push(c);
7044 replace_offset = 0;
7045}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007047
7048/*
7049 * Pop one item from the replace stack.
7050 * return -1 if stack empty
7051 * return replaced character or NUL otherwise
7052 */
7053 static int
7054replace_pop()
7055{
7056 if (replace_stack_nr == 0)
7057 return -1;
7058 return (int)replace_stack[--replace_stack_nr];
7059}
7060
7061/*
7062 * Join the top two items on the replace stack. This removes to "off"'th NUL
7063 * encountered.
7064 */
7065 static void
7066replace_join(off)
7067 int off; /* offset for which NUL to remove */
7068{
7069 int i;
7070
7071 for (i = replace_stack_nr; --i >= 0; )
7072 if (replace_stack[i] == NUL && off-- <= 0)
7073 {
7074 --replace_stack_nr;
7075 mch_memmove(replace_stack + i, replace_stack + i + 1,
7076 (size_t)(replace_stack_nr - i));
7077 return;
7078 }
7079}
7080
7081/*
7082 * Pop bytes from the replace stack until a NUL is found, and insert them
7083 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7084 */
7085 static void
7086replace_pop_ins()
7087{
7088 int cc;
7089 int oldState = State;
7090
7091 State = NORMAL; /* don't want REPLACE here */
7092 while ((cc = replace_pop()) > 0)
7093 {
7094#ifdef FEAT_MBYTE
7095 mb_replace_pop_ins(cc);
7096#else
7097 ins_char(cc);
7098#endif
7099 dec_cursor();
7100 }
7101 State = oldState;
7102}
7103
7104#ifdef FEAT_MBYTE
7105/*
7106 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7107 * indicates a multi-byte char, pop the other bytes too.
7108 */
7109 static void
7110mb_replace_pop_ins(cc)
7111 int cc;
7112{
7113 int n;
7114 char_u buf[MB_MAXBYTES];
7115 int i;
7116 int c;
7117
7118 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7119 {
7120 buf[0] = cc;
7121 for (i = 1; i < n; ++i)
7122 buf[i] = replace_pop();
7123 ins_bytes_len(buf, n);
7124 }
7125 else
7126 ins_char(cc);
7127
7128 if (enc_utf8)
7129 /* Handle composing chars. */
7130 for (;;)
7131 {
7132 c = replace_pop();
7133 if (c == -1) /* stack empty */
7134 break;
7135 if ((n = MB_BYTE2LEN(c)) == 1)
7136 {
7137 /* Not a multi-byte char, put it back. */
7138 replace_push(c);
7139 break;
7140 }
7141 else
7142 {
7143 buf[0] = c;
7144 for (i = 1; i < n; ++i)
7145 buf[i] = replace_pop();
7146 if (utf_iscomposing(utf_ptr2char(buf)))
7147 ins_bytes_len(buf, n);
7148 else
7149 {
7150 /* Not a composing char, put it back. */
7151 for (i = n - 1; i >= 0; --i)
7152 replace_push(buf[i]);
7153 break;
7154 }
7155 }
7156 }
7157}
7158#endif
7159
7160/*
7161 * make the replace stack empty
7162 * (called when exiting replace mode)
7163 */
7164 static void
7165replace_flush()
7166{
7167 vim_free(replace_stack);
7168 replace_stack = NULL;
7169 replace_stack_len = 0;
7170 replace_stack_nr = 0;
7171}
7172
7173/*
7174 * Handle doing a BS for one character.
7175 * cc < 0: replace stack empty, just move cursor
7176 * cc == 0: character was inserted, delete it
7177 * cc > 0: character was replaced, put cc (first byte of original char) back
7178 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007179 * When "limit_col" is >= 0, don't delete before this column. Matters when
7180 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007181 */
7182 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007183replace_do_bs(limit_col)
7184 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007185{
7186 int cc;
7187#ifdef FEAT_VREPLACE
7188 int orig_len = 0;
7189 int ins_len;
7190 int orig_vcols = 0;
7191 colnr_T start_vcol;
7192 char_u *p;
7193 int i;
7194 int vcol;
7195#endif
7196
7197 cc = replace_pop();
7198 if (cc > 0)
7199 {
7200#ifdef FEAT_VREPLACE
7201 if (State & VREPLACE_FLAG)
7202 {
7203 /* Get the number of screen cells used by the character we are
7204 * going to delete. */
7205 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7206 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7207 }
7208#endif
7209#ifdef FEAT_MBYTE
7210 if (has_mbyte)
7211 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007212 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007213# ifdef FEAT_VREPLACE
7214 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007215 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007216# endif
7217 replace_push(cc);
7218 }
7219 else
7220#endif
7221 {
7222 pchar_cursor(cc);
7223#ifdef FEAT_VREPLACE
7224 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007225 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007226#endif
7227 }
7228 replace_pop_ins();
7229
7230#ifdef FEAT_VREPLACE
7231 if (State & VREPLACE_FLAG)
7232 {
7233 /* Get the number of screen cells used by the inserted characters */
7234 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007235 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007236 vcol = start_vcol;
7237 for (i = 0; i < ins_len; ++i)
7238 {
7239 vcol += chartabsize(p + i, vcol);
7240#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007241 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007242#endif
7243 }
7244 vcol -= start_vcol;
7245
7246 /* Delete spaces that were inserted after the cursor to keep the
7247 * text aligned. */
7248 curwin->w_cursor.col += ins_len;
7249 while (vcol > orig_vcols && gchar_cursor() == ' ')
7250 {
7251 del_char(FALSE);
7252 ++orig_vcols;
7253 }
7254 curwin->w_cursor.col -= ins_len;
7255 }
7256#endif
7257
7258 /* mark the buffer as changed and prepare for displaying */
7259 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7260 }
7261 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007262 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007263}
7264
7265#ifdef FEAT_CINDENT
7266/*
7267 * Return TRUE if C-indenting is on.
7268 */
7269 static int
7270cindent_on()
7271{
7272 return (!p_paste && (curbuf->b_p_cin
7273# ifdef FEAT_EVAL
7274 || *curbuf->b_p_inde != NUL
7275# endif
7276 ));
7277}
7278#endif
7279
7280#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7281/*
7282 * Re-indent the current line, based on the current contents of it and the
7283 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7284 * confused what all the part that handles Control-T is doing that I'm not.
7285 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7286 */
7287
7288 void
7289fixthisline(get_the_indent)
7290 int (*get_the_indent) __ARGS((void));
7291{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007292 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293 if (linewhite(curwin->w_cursor.lnum))
7294 did_ai = TRUE; /* delete the indent if the line stays empty */
7295}
7296
7297 void
7298fix_indent()
7299{
7300 if (p_paste)
7301 return;
7302# ifdef FEAT_LISP
7303 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7304 fixthisline(get_lisp_indent);
7305# endif
7306# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7307 else
7308# endif
7309# ifdef FEAT_CINDENT
7310 if (cindent_on())
7311 do_c_expr_indent();
7312# endif
7313}
7314
7315#endif
7316
7317#ifdef FEAT_CINDENT
7318/*
7319 * return TRUE if 'cinkeys' contains the key "keytyped",
7320 * when == '*': Only if key is preceded with '*' (indent before insert)
7321 * when == '!': Only if key is prededed with '!' (don't insert)
7322 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7323 *
7324 * "keytyped" can have a few special values:
7325 * KEY_OPEN_FORW
7326 * KEY_OPEN_BACK
7327 * KEY_COMPLETE just finished completion.
7328 *
7329 * If line_is_empty is TRUE accept keys with '0' before them.
7330 */
7331 int
7332in_cinkeys(keytyped, when, line_is_empty)
7333 int keytyped;
7334 int when;
7335 int line_is_empty;
7336{
7337 char_u *look;
7338 int try_match;
7339 int try_match_word;
7340 char_u *p;
7341 char_u *line;
7342 int icase;
7343 int i;
7344
7345#ifdef FEAT_EVAL
7346 if (*curbuf->b_p_inde != NUL)
7347 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7348 else
7349#endif
7350 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7351 while (*look)
7352 {
7353 /*
7354 * Find out if we want to try a match with this key, depending on
7355 * 'when' and a '*' or '!' before the key.
7356 */
7357 switch (when)
7358 {
7359 case '*': try_match = (*look == '*'); break;
7360 case '!': try_match = (*look == '!'); break;
7361 default: try_match = (*look != '*'); break;
7362 }
7363 if (*look == '*' || *look == '!')
7364 ++look;
7365
7366 /*
7367 * If there is a '0', only accept a match if the line is empty.
7368 * But may still match when typing last char of a word.
7369 */
7370 if (*look == '0')
7371 {
7372 try_match_word = try_match;
7373 if (!line_is_empty)
7374 try_match = FALSE;
7375 ++look;
7376 }
7377 else
7378 try_match_word = FALSE;
7379
7380 /*
7381 * does it look like a control character?
7382 */
7383 if (*look == '^'
7384#ifdef EBCDIC
7385 && (Ctrl_chr(look[1]) != 0)
7386#else
7387 && look[1] >= '?' && look[1] <= '_'
7388#endif
7389 )
7390 {
7391 if (try_match && keytyped == Ctrl_chr(look[1]))
7392 return TRUE;
7393 look += 2;
7394 }
7395 /*
7396 * 'o' means "o" command, open forward.
7397 * 'O' means "O" command, open backward.
7398 */
7399 else if (*look == 'o')
7400 {
7401 if (try_match && keytyped == KEY_OPEN_FORW)
7402 return TRUE;
7403 ++look;
7404 }
7405 else if (*look == 'O')
7406 {
7407 if (try_match && keytyped == KEY_OPEN_BACK)
7408 return TRUE;
7409 ++look;
7410 }
7411
7412 /*
7413 * 'e' means to check for "else" at start of line and just before the
7414 * cursor.
7415 */
7416 else if (*look == 'e')
7417 {
7418 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7419 {
7420 p = ml_get_curline();
7421 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7422 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7423 return TRUE;
7424 }
7425 ++look;
7426 }
7427
7428 /*
7429 * ':' only causes an indent if it is at the end of a label or case
7430 * statement, or when it was before typing the ':' (to fix
7431 * class::method for C++).
7432 */
7433 else if (*look == ':')
7434 {
7435 if (try_match && keytyped == ':')
7436 {
7437 p = ml_get_curline();
7438 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7439 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007440 /* Need to get the line again after cin_islabel(). */
7441 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007442 if (curwin->w_cursor.col > 2
7443 && p[curwin->w_cursor.col - 1] == ':'
7444 && p[curwin->w_cursor.col - 2] == ':')
7445 {
7446 p[curwin->w_cursor.col - 1] = ' ';
7447 i = (cin_iscase(p) || cin_isscopedecl(p)
7448 || cin_islabel(30));
7449 p = ml_get_curline();
7450 p[curwin->w_cursor.col - 1] = ':';
7451 if (i)
7452 return TRUE;
7453 }
7454 }
7455 ++look;
7456 }
7457
7458
7459 /*
7460 * Is it a key in <>, maybe?
7461 */
7462 else if (*look == '<')
7463 {
7464 if (try_match)
7465 {
7466 /*
7467 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7468 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7469 * >, *, : and ! keys if they really really want to.
7470 */
7471 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7472 && keytyped == look[1])
7473 return TRUE;
7474
7475 if (keytyped == get_special_key_code(look + 1))
7476 return TRUE;
7477 }
7478 while (*look && *look != '>')
7479 look++;
7480 while (*look == '>')
7481 look++;
7482 }
7483
7484 /*
7485 * Is it a word: "=word"?
7486 */
7487 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7488 {
7489 ++look;
7490 if (*look == '~')
7491 {
7492 icase = TRUE;
7493 ++look;
7494 }
7495 else
7496 icase = FALSE;
7497 p = vim_strchr(look, ',');
7498 if (p == NULL)
7499 p = look + STRLEN(look);
7500 if ((try_match || try_match_word)
7501 && curwin->w_cursor.col >= (colnr_T)(p - look))
7502 {
7503 int match = FALSE;
7504
7505#ifdef FEAT_INS_EXPAND
7506 if (keytyped == KEY_COMPLETE)
7507 {
7508 char_u *s;
7509
7510 /* Just completed a word, check if it starts with "look".
7511 * search back for the start of a word. */
7512 line = ml_get_curline();
7513# ifdef FEAT_MBYTE
7514 if (has_mbyte)
7515 {
7516 char_u *n;
7517
7518 for (s = line + curwin->w_cursor.col; s > line; s = n)
7519 {
7520 n = mb_prevptr(line, s);
7521 if (!vim_iswordp(n))
7522 break;
7523 }
7524 }
7525 else
7526# endif
7527 for (s = line + curwin->w_cursor.col; s > line; --s)
7528 if (!vim_iswordc(s[-1]))
7529 break;
7530 if (s + (p - look) <= line + curwin->w_cursor.col
7531 && (icase
7532 ? MB_STRNICMP(s, look, p - look)
7533 : STRNCMP(s, look, p - look)) == 0)
7534 match = TRUE;
7535 }
7536 else
7537#endif
7538 /* TODO: multi-byte */
7539 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7540 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7541 {
7542 line = ml_get_cursor();
7543 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7544 || !vim_iswordc(line[-(p - look) - 1]))
7545 && (icase
7546 ? MB_STRNICMP(line - (p - look), look, p - look)
7547 : STRNCMP(line - (p - look), look, p - look))
7548 == 0)
7549 match = TRUE;
7550 }
7551 if (match && try_match_word && !try_match)
7552 {
7553 /* "0=word": Check if there are only blanks before the
7554 * word. */
7555 line = ml_get_curline();
7556 if ((int)(skipwhite(line) - line) !=
7557 (int)(curwin->w_cursor.col - (p - look)))
7558 match = FALSE;
7559 }
7560 if (match)
7561 return TRUE;
7562 }
7563 look = p;
7564 }
7565
7566 /*
7567 * ok, it's a boring generic character.
7568 */
7569 else
7570 {
7571 if (try_match && *look == keytyped)
7572 return TRUE;
7573 ++look;
7574 }
7575
7576 /*
7577 * Skip over ", ".
7578 */
7579 look = skip_to_option_part(look);
7580 }
7581 return FALSE;
7582}
7583#endif /* FEAT_CINDENT */
7584
7585#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7586/*
7587 * Map Hebrew keyboard when in hkmap mode.
7588 */
7589 int
7590hkmap(c)
7591 int c;
7592{
7593 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7594 {
7595 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7596 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7597 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7598 static char_u map[26] =
7599 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7600 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7601 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7602 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7603 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7604 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7605 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7606 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7607 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7608
7609 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7610 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7611 /* '-1'='sofit' */
7612 else if (c == 'x')
7613 return 'X';
7614 else if (c == 'q')
7615 return '\''; /* {geresh}={'} */
7616 else if (c == 246)
7617 return ' '; /* \"o --> ' ' for a german keyboard */
7618 else if (c == 228)
7619 return ' '; /* \"a --> ' ' -- / -- */
7620 else if (c == 252)
7621 return ' '; /* \"u --> ' ' -- / -- */
7622#ifdef EBCDIC
7623 else if (islower(c))
7624#else
7625 /* NOTE: islower() does not do the right thing for us on Linux so we
7626 * do this the same was as 5.7 and previous, so it works correctly on
7627 * all systems. Specifically, the e.g. Delete and Arrow keys are
7628 * munged and won't work if e.g. searching for Hebrew text.
7629 */
7630 else if (c >= 'a' && c <= 'z')
7631#endif
7632 return (int)(map[CharOrdLow(c)] + p_aleph);
7633 else
7634 return c;
7635 }
7636 else
7637 {
7638 switch (c)
7639 {
7640 case '`': return ';';
7641 case '/': return '.';
7642 case '\'': return ',';
7643 case 'q': return '/';
7644 case 'w': return '\'';
7645
7646 /* Hebrew letters - set offset from 'a' */
7647 case ',': c = '{'; break;
7648 case '.': c = 'v'; break;
7649 case ';': c = 't'; break;
7650 default: {
7651 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7652
7653#ifdef EBCDIC
7654 /* see note about islower() above */
7655 if (!islower(c))
7656#else
7657 if (c < 'a' || c > 'z')
7658#endif
7659 return c;
7660 c = str[CharOrdLow(c)];
7661 break;
7662 }
7663 }
7664
7665 return (int)(CharOrdLow(c) + p_aleph);
7666 }
7667}
7668#endif
7669
7670 static void
7671ins_reg()
7672{
7673 int need_redraw = FALSE;
7674 int regname;
7675 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007676#ifdef FEAT_VISUAL
7677 int vis_active = VIsual_active;
7678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007679
7680 /*
7681 * If we are going to wait for a character, show a '"'.
7682 */
7683 pc_status = PC_STATUS_UNSET;
7684 if (redrawing() && !char_avail())
7685 {
7686 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007687 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007688
7689 edit_putchar('"', TRUE);
7690#ifdef FEAT_CMDL_INFO
7691 add_to_showcmd_c(Ctrl_R);
7692#endif
7693 }
7694
7695#ifdef USE_ON_FLY_SCROLL
7696 dont_scroll = TRUE; /* disallow scrolling here */
7697#endif
7698
7699 /*
7700 * Don't map the register name. This also prevents the mode message to be
7701 * deleted when ESC is hit.
7702 */
7703 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007704 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007705#ifdef FEAT_LANGMAP
7706 LANGMAP_ADJUST(regname, TRUE);
7707#endif
7708 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7709 {
7710 /* Get a third key for literal register insertion */
7711 literally = regname;
7712#ifdef FEAT_CMDL_INFO
7713 add_to_showcmd_c(literally);
7714#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007715 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716#ifdef FEAT_LANGMAP
7717 LANGMAP_ADJUST(regname, TRUE);
7718#endif
7719 }
7720 --no_mapping;
7721
7722#ifdef FEAT_EVAL
7723 /*
7724 * Don't call u_sync() while getting the expression,
7725 * evaluating it or giving an error message for it!
7726 */
7727 ++no_u_sync;
7728 if (regname == '=')
7729 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007730# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007731 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007732# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007733 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007734# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007735 /* Restore the Input Method. */
7736 if (im_on)
7737 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007738# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007739 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007740 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7741 {
7742 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007743 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007744 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007745 else
7746 {
7747#endif
7748 if (literally == Ctrl_O || literally == Ctrl_P)
7749 {
7750 /* Append the command to the redo buffer. */
7751 AppendCharToRedobuff(Ctrl_R);
7752 AppendCharToRedobuff(literally);
7753 AppendCharToRedobuff(regname);
7754
7755 do_put(regname, BACKWARD, 1L,
7756 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7757 }
7758 else if (insert_reg(regname, literally) == FAIL)
7759 {
7760 vim_beep();
7761 need_redraw = TRUE; /* remove the '"' */
7762 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007763 else if (stop_insert_mode)
7764 /* When the '=' register was used and a function was invoked that
7765 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7766 * insert anything, need to remove the '"' */
7767 need_redraw = TRUE;
7768
Bram Moolenaar071d4272004-06-13 20:20:40 +00007769#ifdef FEAT_EVAL
7770 }
7771 --no_u_sync;
7772#endif
7773#ifdef FEAT_CMDL_INFO
7774 clear_showcmd();
7775#endif
7776
7777 /* If the inserted register is empty, we need to remove the '"' */
7778 if (need_redraw || stuff_empty())
7779 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007780
7781#ifdef FEAT_VISUAL
7782 /* Disallow starting Visual mode here, would get a weird mode. */
7783 if (!vis_active && VIsual_active)
7784 end_visual_mode();
7785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007786}
7787
7788/*
7789 * CTRL-G commands in Insert mode.
7790 */
7791 static void
7792ins_ctrl_g()
7793{
7794 int c;
7795
7796#ifdef FEAT_INS_EXPAND
7797 /* Right after CTRL-X the cursor will be after the ruler. */
7798 setcursor();
7799#endif
7800
7801 /*
7802 * Don't map the second key. This also prevents the mode message to be
7803 * deleted when ESC is hit.
7804 */
7805 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007806 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007807 --no_mapping;
7808 switch (c)
7809 {
7810 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7811 case K_UP:
7812 case Ctrl_K:
7813 case 'k': ins_up(TRUE);
7814 break;
7815
7816 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7817 case K_DOWN:
7818 case Ctrl_J:
7819 case 'j': ins_down(TRUE);
7820 break;
7821
7822 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007823 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007824 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007825
7826 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007827 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007828 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007829 break;
7830
7831 /* Unknown CTRL-G command, reserved for future expansion. */
7832 default: vim_beep();
7833 }
7834}
7835
7836/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007837 * CTRL-^ in Insert mode.
7838 */
7839 static void
7840ins_ctrl_hat()
7841{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007842 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007843 {
7844 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7845 if (State & LANGMAP)
7846 {
7847 curbuf->b_p_iminsert = B_IMODE_NONE;
7848 State &= ~LANGMAP;
7849 }
7850 else
7851 {
7852 curbuf->b_p_iminsert = B_IMODE_LMAP;
7853 State |= LANGMAP;
7854#ifdef USE_IM_CONTROL
7855 im_set_active(FALSE);
7856#endif
7857 }
7858 }
7859#ifdef USE_IM_CONTROL
7860 else
7861 {
7862 /* There are no ":lmap" mappings, toggle IM */
7863 if (im_get_status())
7864 {
7865 curbuf->b_p_iminsert = B_IMODE_NONE;
7866 im_set_active(FALSE);
7867 }
7868 else
7869 {
7870 curbuf->b_p_iminsert = B_IMODE_IM;
7871 State &= ~LANGMAP;
7872 im_set_active(TRUE);
7873 }
7874 }
7875#endif
7876 set_iminsert_global();
7877 showmode();
7878#ifdef FEAT_GUI
7879 /* may show different cursor shape or color */
7880 if (gui.in_use)
7881 gui_update_cursor(TRUE, FALSE);
7882#endif
7883#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7884 /* Show/unshow value of 'keymap' in status lines. */
7885 status_redraw_curbuf();
7886#endif
7887}
7888
7889/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007890 * Handle ESC in insert mode.
7891 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7892 * insert.
7893 */
7894 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007895ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896 long *count;
7897 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007898 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007899{
7900 int temp;
7901 static int disabled_redraw = FALSE;
7902
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007903#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007904 check_spell_redraw();
7905#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007906#if defined(FEAT_HANGULIN)
7907# if defined(ESC_CHG_TO_ENG_MODE)
7908 hangul_input_state_set(0);
7909# endif
7910 if (composing_hangul)
7911 {
7912 push_raw_key(composing_hangul_buffer, 2);
7913 composing_hangul = 0;
7914 }
7915#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916
7917 temp = curwin->w_cursor.col;
7918 if (disabled_redraw)
7919 {
7920 --RedrawingDisabled;
7921 disabled_redraw = FALSE;
7922 }
7923 if (!arrow_used)
7924 {
7925 /*
7926 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007927 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7928 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929 */
7930 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007931 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007932
7933 /*
7934 * Repeating insert may take a long time. Check for
7935 * interrupt now and then.
7936 */
7937 if (*count > 0)
7938 {
7939 line_breakcheck();
7940 if (got_int)
7941 *count = 0;
7942 }
7943
7944 if (--*count > 0) /* repeat what was typed */
7945 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007946 /* Vi repeats the insert without replacing characters. */
7947 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7948 State &= ~REPLACE_FLAG;
7949
Bram Moolenaar071d4272004-06-13 20:20:40 +00007950 (void)start_redo_ins();
7951 if (cmdchar == 'r' || cmdchar == 'v')
7952 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7953 ++RedrawingDisabled;
7954 disabled_redraw = TRUE;
7955 return FALSE; /* repeat the insert */
7956 }
7957 stop_insert(&curwin->w_cursor, TRUE);
7958 undisplay_dollar();
7959 }
7960
7961 /* When an autoindent was removed, curswant stays after the
7962 * indent */
7963 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7964 curwin->w_set_curswant = TRUE;
7965
7966 /* Remember the last Insert position in the '^ mark. */
7967 if (!cmdmod.keepjumps)
7968 curbuf->b_last_insert = curwin->w_cursor;
7969
7970 /*
7971 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007972 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007974 if (!nomove
7975 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007976#ifdef FEAT_VIRTUALEDIT
7977 || curwin->w_cursor.coladd > 0
7978#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007979 )
7980 && (restart_edit == NUL
7981 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007982#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007983 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007984#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007985 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007986#ifdef FEAT_RIGHTLEFT
7987 && !revins_on
7988#endif
7989 )
7990 {
7991#ifdef FEAT_VIRTUALEDIT
7992 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7993 {
7994 oneleft();
7995 if (restart_edit != NUL)
7996 ++curwin->w_cursor.coladd;
7997 }
7998 else
7999#endif
8000 {
8001 --curwin->w_cursor.col;
8002#ifdef FEAT_MBYTE
8003 /* Correct cursor for multi-byte character. */
8004 if (has_mbyte)
8005 mb_adjust_cursor();
8006#endif
8007 }
8008 }
8009
8010#ifdef USE_IM_CONTROL
8011 /* Disable IM to allow typing English directly for Normal mode commands.
8012 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8013 * well). */
8014 if (!(State & LANGMAP))
8015 im_save_status(&curbuf->b_p_iminsert);
8016 im_set_active(FALSE);
8017#endif
8018
8019 State = NORMAL;
8020 /* need to position cursor again (e.g. when on a TAB ) */
8021 changed_cline_bef_curs();
8022
8023#ifdef FEAT_MOUSE
8024 setmouse();
8025#endif
8026#ifdef CURSOR_SHAPE
8027 ui_cursor_shape(); /* may show different cursor shape */
8028#endif
8029
8030 /*
8031 * When recording or for CTRL-O, need to display the new mode.
8032 * Otherwise remove the mode message.
8033 */
8034 if (Recording || restart_edit != NUL)
8035 showmode();
8036 else if (p_smd)
8037 MSG("");
8038
8039 return TRUE; /* exit Insert mode */
8040}
8041
8042#ifdef FEAT_RIGHTLEFT
8043/*
8044 * Toggle language: hkmap and revins_on.
8045 * Move to end of reverse inserted text.
8046 */
8047 static void
8048ins_ctrl_()
8049{
8050 if (revins_on && revins_chars && revins_scol >= 0)
8051 {
8052 while (gchar_cursor() != NUL && revins_chars--)
8053 ++curwin->w_cursor.col;
8054 }
8055 p_ri = !p_ri;
8056 revins_on = (State == INSERT && p_ri);
8057 if (revins_on)
8058 {
8059 revins_scol = curwin->w_cursor.col;
8060 revins_legal++;
8061 revins_chars = 0;
8062 undisplay_dollar();
8063 }
8064 else
8065 revins_scol = -1;
8066#ifdef FEAT_FKMAP
8067 if (p_altkeymap)
8068 {
8069 /*
8070 * to be consistent also for redo command, using '.'
8071 * set arrow_used to true and stop it - causing to redo
8072 * characters entered in one mode (normal/reverse insert).
8073 */
8074 arrow_used = TRUE;
8075 (void)stop_arrow();
8076 p_fkmap = curwin->w_p_rl ^ p_ri;
8077 if (p_fkmap && p_ri)
8078 State = INSERT;
8079 }
8080 else
8081#endif
8082 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8083 showmode();
8084}
8085#endif
8086
8087#ifdef FEAT_VISUAL
8088/*
8089 * If 'keymodel' contains "startsel", may start selection.
8090 * Returns TRUE when a CTRL-O and other keys stuffed.
8091 */
8092 static int
8093ins_start_select(c)
8094 int c;
8095{
8096 if (km_startsel)
8097 switch (c)
8098 {
8099 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 case K_PAGEUP:
8102 case K_KPAGEUP:
8103 case K_PAGEDOWN:
8104 case K_KPAGEDOWN:
8105# ifdef MACOS
8106 case K_LEFT:
8107 case K_RIGHT:
8108 case K_UP:
8109 case K_DOWN:
8110 case K_END:
8111 case K_HOME:
8112# endif
8113 if (!(mod_mask & MOD_MASK_SHIFT))
8114 break;
8115 /* FALLTHROUGH */
8116 case K_S_LEFT:
8117 case K_S_RIGHT:
8118 case K_S_UP:
8119 case K_S_DOWN:
8120 case K_S_END:
8121 case K_S_HOME:
8122 /* Start selection right away, the cursor can move with
8123 * CTRL-O when beyond the end of the line. */
8124 start_selection();
8125
8126 /* Execute the key in (insert) Select mode. */
8127 stuffcharReadbuff(Ctrl_O);
8128 if (mod_mask)
8129 {
8130 char_u buf[4];
8131
8132 buf[0] = K_SPECIAL;
8133 buf[1] = KS_MODIFIER;
8134 buf[2] = mod_mask;
8135 buf[3] = NUL;
8136 stuffReadbuff(buf);
8137 }
8138 stuffcharReadbuff(c);
8139 return TRUE;
8140 }
8141 return FALSE;
8142}
8143#endif
8144
8145/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008146 * <Insert> key in Insert mode: toggle insert/remplace mode.
8147 */
8148 static void
8149ins_insert(replaceState)
8150 int replaceState;
8151{
8152#ifdef FEAT_FKMAP
8153 if (p_fkmap && p_ri)
8154 {
8155 beep_flush();
8156 EMSG(farsi_text_3); /* encoded in Farsi */
8157 return;
8158 }
8159#endif
8160
8161#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008162# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008163 set_vim_var_string(VV_INSERTMODE,
8164 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008165# ifdef FEAT_VREPLACE
8166 replaceState == VREPLACE ? "v" :
8167# endif
8168 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008169# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008170 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8171#endif
8172 if (State & REPLACE_FLAG)
8173 State = INSERT | (State & LANGMAP);
8174 else
8175 State = replaceState | (State & LANGMAP);
8176 AppendCharToRedobuff(K_INS);
8177 showmode();
8178#ifdef CURSOR_SHAPE
8179 ui_cursor_shape(); /* may show different cursor shape */
8180#endif
8181}
8182
8183/*
8184 * Pressed CTRL-O in Insert mode.
8185 */
8186 static void
8187ins_ctrl_o()
8188{
8189#ifdef FEAT_VREPLACE
8190 if (State & VREPLACE_FLAG)
8191 restart_edit = 'V';
8192 else
8193#endif
8194 if (State & REPLACE_FLAG)
8195 restart_edit = 'R';
8196 else
8197 restart_edit = 'I';
8198#ifdef FEAT_VIRTUALEDIT
8199 if (virtual_active())
8200 ins_at_eol = FALSE; /* cursor always keeps its column */
8201 else
8202#endif
8203 ins_at_eol = (gchar_cursor() == NUL);
8204}
8205
8206/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 * If the cursor is on an indent, ^T/^D insert/delete one
8208 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
8209 * Always round the indent to 'shiftwith', this is compatible
8210 * with vi. But vi only supports ^T and ^D after an
8211 * autoindent, we support it everywhere.
8212 */
8213 static void
8214ins_shift(c, lastc)
8215 int c;
8216 int lastc;
8217{
8218 if (stop_arrow() == FAIL)
8219 return;
8220 AppendCharToRedobuff(c);
8221
8222 /*
8223 * 0^D and ^^D: remove all indent.
8224 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008225 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8226 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {
8228 --curwin->w_cursor.col;
8229 (void)del_char(FALSE); /* delete the '^' or '0' */
8230 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8231 if (State & REPLACE_FLAG)
8232 replace_pop_ins();
8233 if (lastc == '^')
8234 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008235 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 }
8237 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008238 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008239
8240 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8241 did_ai = FALSE;
8242#ifdef FEAT_SMARTINDENT
8243 did_si = FALSE;
8244 can_si = FALSE;
8245 can_si_back = FALSE;
8246#endif
8247#ifdef FEAT_CINDENT
8248 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8249#endif
8250}
8251
8252 static void
8253ins_del()
8254{
8255 int temp;
8256
8257 if (stop_arrow() == FAIL)
8258 return;
8259 if (gchar_cursor() == NUL) /* delete newline */
8260 {
8261 temp = curwin->w_cursor.col;
8262 if (!can_bs(BS_EOL) /* only if "eol" included */
8263 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8264 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8265 || do_join(FALSE) == FAIL)
8266 vim_beep();
8267 else
8268 curwin->w_cursor.col = temp;
8269 }
8270 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8271 vim_beep();
8272 did_ai = FALSE;
8273#ifdef FEAT_SMARTINDENT
8274 did_si = FALSE;
8275 can_si = FALSE;
8276 can_si_back = FALSE;
8277#endif
8278 AppendCharToRedobuff(K_DEL);
8279}
8280
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008281static void ins_bs_one __ARGS((colnr_T *vcolp));
8282
8283/*
8284 * Delete one character for ins_bs().
8285 */
8286 static void
8287ins_bs_one(vcolp)
8288 colnr_T *vcolp;
8289{
8290 dec_cursor();
8291 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8292 if (State & REPLACE_FLAG)
8293 {
8294 /* Don't delete characters before the insert point when in
8295 * Replace mode */
8296 if (curwin->w_cursor.lnum != Insstart.lnum
8297 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008298 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008299 }
8300 else
8301 (void)del_char(FALSE);
8302}
8303
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304/*
8305 * Handle Backspace, delete-word and delete-line in Insert mode.
8306 * Return TRUE when backspace was actually used.
8307 */
8308 static int
8309ins_bs(c, mode, inserted_space_p)
8310 int c;
8311 int mode;
8312 int *inserted_space_p;
8313{
8314 linenr_T lnum;
8315 int cc;
8316 int temp = 0; /* init for GCC */
8317 colnr_T mincol;
8318 int did_backspace = FALSE;
8319 int in_indent;
8320 int oldState;
8321#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008322 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008323#endif
8324
8325 /*
8326 * can't delete anything in an empty file
8327 * can't backup past first character in buffer
8328 * can't backup past starting point unless 'backspace' > 1
8329 * can backup to a previous line if 'backspace' == 0
8330 */
8331 if ( bufempty()
8332 || (
8333#ifdef FEAT_RIGHTLEFT
8334 !revins_on &&
8335#endif
8336 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8337 || (!can_bs(BS_START)
8338 && (arrow_used
8339 || (curwin->w_cursor.lnum == Insstart.lnum
8340 && curwin->w_cursor.col <= Insstart.col)))
8341 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8342 && curwin->w_cursor.col <= ai_col)
8343 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8344 {
8345 vim_beep();
8346 return FALSE;
8347 }
8348
8349 if (stop_arrow() == FAIL)
8350 return FALSE;
8351 in_indent = inindent(0);
8352#ifdef FEAT_CINDENT
8353 if (in_indent)
8354 can_cindent = FALSE;
8355#endif
8356#ifdef FEAT_COMMENTS
8357 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8358#endif
8359#ifdef FEAT_RIGHTLEFT
8360 if (revins_on) /* put cursor after last inserted char */
8361 inc_cursor();
8362#endif
8363
8364#ifdef FEAT_VIRTUALEDIT
8365 /* Virtualedit:
8366 * BACKSPACE_CHAR eats a virtual space
8367 * BACKSPACE_WORD eats all coladd
8368 * BACKSPACE_LINE eats all coladd and keeps going
8369 */
8370 if (curwin->w_cursor.coladd > 0)
8371 {
8372 if (mode == BACKSPACE_CHAR)
8373 {
8374 --curwin->w_cursor.coladd;
8375 return TRUE;
8376 }
8377 if (mode == BACKSPACE_WORD)
8378 {
8379 curwin->w_cursor.coladd = 0;
8380 return TRUE;
8381 }
8382 curwin->w_cursor.coladd = 0;
8383 }
8384#endif
8385
8386 /*
8387 * delete newline!
8388 */
8389 if (curwin->w_cursor.col == 0)
8390 {
8391 lnum = Insstart.lnum;
8392 if (curwin->w_cursor.lnum == Insstart.lnum
8393#ifdef FEAT_RIGHTLEFT
8394 || revins_on
8395#endif
8396 )
8397 {
8398 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8399 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8400 return FALSE;
8401 --Insstart.lnum;
8402 Insstart.col = MAXCOL;
8403 }
8404 /*
8405 * In replace mode:
8406 * cc < 0: NL was inserted, delete it
8407 * cc >= 0: NL was replaced, put original characters back
8408 */
8409 cc = -1;
8410 if (State & REPLACE_FLAG)
8411 cc = replace_pop(); /* returns -1 if NL was inserted */
8412 /*
8413 * In replace mode, in the line we started replacing, we only move the
8414 * cursor.
8415 */
8416 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8417 {
8418 dec_cursor();
8419 }
8420 else
8421 {
8422#ifdef FEAT_VREPLACE
8423 if (!(State & VREPLACE_FLAG)
8424 || curwin->w_cursor.lnum > orig_line_count)
8425#endif
8426 {
8427 temp = gchar_cursor(); /* remember current char */
8428 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008429
8430 /* When "aw" is in 'formatoptions' we must delete the space at
8431 * the end of the line, otherwise the line will be broken
8432 * again when auto-formatting. */
8433 if (has_format_option(FO_AUTO)
8434 && has_format_option(FO_WHITE_PAR))
8435 {
8436 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8437 TRUE);
8438 int len;
8439
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008440 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008441 if (len > 0 && ptr[len - 1] == ' ')
8442 ptr[len - 1] = NUL;
8443 }
8444
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 (void)do_join(FALSE);
8446 if (temp == NUL && gchar_cursor() != NUL)
8447 inc_cursor();
8448 }
8449#ifdef FEAT_VREPLACE
8450 else
8451 dec_cursor();
8452#endif
8453
8454 /*
8455 * In REPLACE mode we have to put back the text that was replaced
8456 * by the NL. On the replace stack is first a NUL-terminated
8457 * sequence of characters that were deleted and then the
8458 * characters that NL replaced.
8459 */
8460 if (State & REPLACE_FLAG)
8461 {
8462 /*
8463 * Do the next ins_char() in NORMAL state, to
8464 * prevent ins_char() from replacing characters and
8465 * avoiding showmatch().
8466 */
8467 oldState = State;
8468 State = NORMAL;
8469 /*
8470 * restore characters (blanks) deleted after cursor
8471 */
8472 while (cc > 0)
8473 {
8474 temp = curwin->w_cursor.col;
8475#ifdef FEAT_MBYTE
8476 mb_replace_pop_ins(cc);
8477#else
8478 ins_char(cc);
8479#endif
8480 curwin->w_cursor.col = temp;
8481 cc = replace_pop();
8482 }
8483 /* restore the characters that NL replaced */
8484 replace_pop_ins();
8485 State = oldState;
8486 }
8487 }
8488 did_ai = FALSE;
8489 }
8490 else
8491 {
8492 /*
8493 * Delete character(s) before the cursor.
8494 */
8495#ifdef FEAT_RIGHTLEFT
8496 if (revins_on) /* put cursor on last inserted char */
8497 dec_cursor();
8498#endif
8499 mincol = 0;
8500 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008501 if (mode == BACKSPACE_LINE
8502 && (curbuf->b_p_ai
8503#ifdef FEAT_CINDENT
8504 || cindent_on()
8505#endif
8506 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507#ifdef FEAT_RIGHTLEFT
8508 && !revins_on
8509#endif
8510 )
8511 {
8512 temp = curwin->w_cursor.col;
8513 beginline(BL_WHITE);
8514 if (curwin->w_cursor.col < (colnr_T)temp)
8515 mincol = curwin->w_cursor.col;
8516 curwin->w_cursor.col = temp;
8517 }
8518
8519 /*
8520 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8521 */
8522 if ( mode == BACKSPACE_CHAR
8523 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008524 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008525 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 && (*(ml_get_cursor() - 1) == TAB
8527 || (*(ml_get_cursor() - 1) == ' '
8528 && (!*inserted_space_p
8529 || arrow_used))))))
8530 {
8531 int ts;
8532 colnr_T vcol;
8533 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008534 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535
8536 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008537 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538 ts = curbuf->b_p_sw;
8539 else
8540 ts = curbuf->b_p_sts;
8541 /* Compute the virtual column where we want to be. Since
8542 * 'showbreak' may get in the way, need to get the last column of
8543 * the previous character. */
8544 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008545 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546 dec_cursor();
8547 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8548 inc_cursor();
8549 want_vcol = (want_vcol / ts) * ts;
8550
8551 /* delete characters until we are at or before want_vcol */
8552 while (vcol > want_vcol
8553 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008554 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008555
8556 /* insert extra spaces until we are at want_vcol */
8557 while (vcol < want_vcol)
8558 {
8559 /* Remember the first char we inserted */
8560 if (curwin->w_cursor.lnum == Insstart.lnum
8561 && curwin->w_cursor.col < Insstart.col)
8562 Insstart.col = curwin->w_cursor.col;
8563
8564#ifdef FEAT_VREPLACE
8565 if (State & VREPLACE_FLAG)
8566 ins_char(' ');
8567 else
8568#endif
8569 {
8570 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008571 if ((State & REPLACE_FLAG))
8572 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 }
8574 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8575 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008576
8577 /* If we are now back where we started delete one character. Can
8578 * happen when using 'sts' and 'linebreak'. */
8579 if (vcol >= start_vcol)
8580 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008581 }
8582
8583 /*
8584 * Delete upto starting point, start of line or previous word.
8585 */
8586 else do
8587 {
8588#ifdef FEAT_RIGHTLEFT
8589 if (!revins_on) /* put cursor on char to be deleted */
8590#endif
8591 dec_cursor();
8592
8593 /* start of word? */
8594 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8595 {
8596 mode = BACKSPACE_WORD_NOT_SPACE;
8597 temp = vim_iswordc(gchar_cursor());
8598 }
8599 /* end of word? */
8600 else if (mode == BACKSPACE_WORD_NOT_SPACE
8601 && (vim_isspace(cc = gchar_cursor())
8602 || vim_iswordc(cc) != temp))
8603 {
8604#ifdef FEAT_RIGHTLEFT
8605 if (!revins_on)
8606#endif
8607 inc_cursor();
8608#ifdef FEAT_RIGHTLEFT
8609 else if (State & REPLACE_FLAG)
8610 dec_cursor();
8611#endif
8612 break;
8613 }
8614 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008615 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616 else
8617 {
8618#ifdef FEAT_MBYTE
8619 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008620 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621#endif
8622 (void)del_char(FALSE);
8623#ifdef FEAT_MBYTE
8624 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008625 * If there are combining characters and 'delcombine' is set
8626 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 * character.
8628 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008629 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630 inc_cursor();
8631#endif
8632#ifdef FEAT_RIGHTLEFT
8633 if (revins_chars)
8634 {
8635 revins_chars--;
8636 revins_legal++;
8637 }
8638 if (revins_on && gchar_cursor() == NUL)
8639 break;
8640#endif
8641 }
8642 /* Just a single backspace?: */
8643 if (mode == BACKSPACE_CHAR)
8644 break;
8645 } while (
8646#ifdef FEAT_RIGHTLEFT
8647 revins_on ||
8648#endif
8649 (curwin->w_cursor.col > mincol
8650 && (curwin->w_cursor.lnum != Insstart.lnum
8651 || curwin->w_cursor.col != Insstart.col)));
8652 did_backspace = TRUE;
8653 }
8654#ifdef FEAT_SMARTINDENT
8655 did_si = FALSE;
8656 can_si = FALSE;
8657 can_si_back = FALSE;
8658#endif
8659 if (curwin->w_cursor.col <= 1)
8660 did_ai = FALSE;
8661 /*
8662 * It's a little strange to put backspaces into the redo
8663 * buffer, but it makes auto-indent a lot easier to deal
8664 * with.
8665 */
8666 AppendCharToRedobuff(c);
8667
8668 /* If deleted before the insertion point, adjust it */
8669 if (curwin->w_cursor.lnum == Insstart.lnum
8670 && curwin->w_cursor.col < Insstart.col)
8671 Insstart.col = curwin->w_cursor.col;
8672
8673 /* vi behaviour: the cursor moves backward but the character that
8674 * was there remains visible
8675 * Vim behaviour: the cursor moves backward and the character that
8676 * was there is erased from the screen.
8677 * We can emulate the vi behaviour by pretending there is a dollar
8678 * displayed even when there isn't.
8679 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8680 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8681 dollar_vcol = curwin->w_virtcol;
8682
Bram Moolenaarce3be472008-01-14 19:12:28 +00008683#ifdef FEAT_FOLDING
8684 /* When deleting a char the cursor line must never be in a closed fold.
8685 * E.g., when 'foldmethod' is indent and deleting the first non-white
8686 * char before a Tab. */
8687 if (did_backspace)
8688 foldOpenCursor();
8689#endif
8690
Bram Moolenaar071d4272004-06-13 20:20:40 +00008691 return did_backspace;
8692}
8693
8694#ifdef FEAT_MOUSE
8695 static void
8696ins_mouse(c)
8697 int c;
8698{
8699 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008700 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008701
8702# ifdef FEAT_GUI
8703 /* When GUI is active, also move/paste when 'mouse' is empty */
8704 if (!gui.in_use)
8705# endif
8706 if (!mouse_has(MOUSE_INSERT))
8707 return;
8708
8709 undisplay_dollar();
8710 tpos = curwin->w_cursor;
8711 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8712 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008713#ifdef FEAT_WINDOWS
8714 win_T *new_curwin = curwin;
8715
8716 if (curwin != old_curwin && win_valid(old_curwin))
8717 {
8718 /* Mouse took us to another window. We need to go back to the
8719 * previous one to stop insert there properly. */
8720 curwin = old_curwin;
8721 curbuf = curwin->w_buffer;
8722 }
8723#endif
8724 start_arrow(curwin == old_curwin ? &tpos : NULL);
8725#ifdef FEAT_WINDOWS
8726 if (curwin != new_curwin && win_valid(new_curwin))
8727 {
8728 curwin = new_curwin;
8729 curbuf = curwin->w_buffer;
8730 }
8731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732# ifdef FEAT_CINDENT
8733 can_cindent = TRUE;
8734# endif
8735 }
8736
8737#ifdef FEAT_WINDOWS
8738 /* redraw status lines (in case another window became active) */
8739 redraw_statuslines();
8740#endif
8741}
8742
8743 static void
8744ins_mousescroll(up)
8745 int up;
8746{
8747 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008748# if defined(FEAT_WINDOWS)
8749 win_T *old_curwin = curwin;
8750# endif
8751# ifdef FEAT_INS_EXPAND
8752 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753# endif
8754
8755 tpos = curwin->w_cursor;
8756
8757# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 /* Currently the mouse coordinates are only known in the GUI. */
8759 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8760 {
8761 int row, col;
8762
8763 row = mouse_row;
8764 col = mouse_col;
8765
8766 /* find the window at the pointer coordinates */
8767 curwin = mouse_find_win(&row, &col);
8768 curbuf = curwin->w_buffer;
8769 }
8770 if (curwin == old_curwin)
8771# endif
8772 undisplay_dollar();
8773
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008774# ifdef FEAT_INS_EXPAND
8775 /* Don't scroll the window in which completion is being done. */
8776 if (!pum_visible()
8777# if defined(FEAT_WINDOWS)
8778 || curwin != old_curwin
8779# endif
8780 )
8781# endif
8782 {
8783 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8784 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8785 else
8786 scroll_redraw(up, 3L);
8787# ifdef FEAT_INS_EXPAND
8788 did_scroll = TRUE;
8789# endif
8790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008791
8792# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8793 curwin->w_redr_status = TRUE;
8794
8795 curwin = old_curwin;
8796 curbuf = curwin->w_buffer;
8797# endif
8798
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008799# ifdef FEAT_INS_EXPAND
8800 /* The popup menu may overlay the window, need to redraw it.
8801 * TODO: Would be more efficient to only redraw the windows that are
8802 * overlapped by the popup menu. */
8803 if (pum_visible() && did_scroll)
8804 {
8805 redraw_all_later(NOT_VALID);
8806 ins_compl_show_pum();
8807 }
8808# endif
8809
Bram Moolenaar071d4272004-06-13 20:20:40 +00008810 if (!equalpos(curwin->w_cursor, tpos))
8811 {
8812 start_arrow(&tpos);
8813# ifdef FEAT_CINDENT
8814 can_cindent = TRUE;
8815# endif
8816 }
8817}
8818#endif
8819
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008820#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008821 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008822ins_tabline(c)
8823 int c;
8824{
8825 /* We will be leaving the current window, unless closing another tab. */
8826 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8827 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8828 {
8829 undisplay_dollar();
8830 start_arrow(&curwin->w_cursor);
8831# ifdef FEAT_CINDENT
8832 can_cindent = TRUE;
8833# endif
8834 }
8835
8836 if (c == K_TABLINE)
8837 goto_tabpage(current_tab);
8838 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008839 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008840 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008841 redraw_statuslines(); /* will redraw the tabline when needed */
8842 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008843}
8844#endif
8845
8846#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 void
8848ins_scroll()
8849{
8850 pos_T tpos;
8851
8852 undisplay_dollar();
8853 tpos = curwin->w_cursor;
8854 if (gui_do_scroll())
8855 {
8856 start_arrow(&tpos);
8857# ifdef FEAT_CINDENT
8858 can_cindent = TRUE;
8859# endif
8860 }
8861}
8862
8863 void
8864ins_horscroll()
8865{
8866 pos_T tpos;
8867
8868 undisplay_dollar();
8869 tpos = curwin->w_cursor;
8870 if (gui_do_horiz_scroll())
8871 {
8872 start_arrow(&tpos);
8873# ifdef FEAT_CINDENT
8874 can_cindent = TRUE;
8875# endif
8876 }
8877}
8878#endif
8879
8880 static void
8881ins_left()
8882{
8883 pos_T tpos;
8884
8885#ifdef FEAT_FOLDING
8886 if ((fdo_flags & FDO_HOR) && KeyTyped)
8887 foldOpenCursor();
8888#endif
8889 undisplay_dollar();
8890 tpos = curwin->w_cursor;
8891 if (oneleft() == OK)
8892 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008893#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8894 /* Only call start_arrow() when not busy with preediting, it will
8895 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8896 if (!im_is_preediting())
8897#endif
8898 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899#ifdef FEAT_RIGHTLEFT
8900 /* If exit reversed string, position is fixed */
8901 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8902 revins_legal++;
8903 revins_chars++;
8904#endif
8905 }
8906
8907 /*
8908 * if 'whichwrap' set for cursor in insert mode may go to
8909 * previous line
8910 */
8911 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8912 {
8913 start_arrow(&tpos);
8914 --(curwin->w_cursor.lnum);
8915 coladvance((colnr_T)MAXCOL);
8916 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8917 }
8918 else
8919 vim_beep();
8920}
8921
8922 static void
8923ins_home(c)
8924 int c;
8925{
8926 pos_T tpos;
8927
8928#ifdef FEAT_FOLDING
8929 if ((fdo_flags & FDO_HOR) && KeyTyped)
8930 foldOpenCursor();
8931#endif
8932 undisplay_dollar();
8933 tpos = curwin->w_cursor;
8934 if (c == K_C_HOME)
8935 curwin->w_cursor.lnum = 1;
8936 curwin->w_cursor.col = 0;
8937#ifdef FEAT_VIRTUALEDIT
8938 curwin->w_cursor.coladd = 0;
8939#endif
8940 curwin->w_curswant = 0;
8941 start_arrow(&tpos);
8942}
8943
8944 static void
8945ins_end(c)
8946 int c;
8947{
8948 pos_T tpos;
8949
8950#ifdef FEAT_FOLDING
8951 if ((fdo_flags & FDO_HOR) && KeyTyped)
8952 foldOpenCursor();
8953#endif
8954 undisplay_dollar();
8955 tpos = curwin->w_cursor;
8956 if (c == K_C_END)
8957 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8958 coladvance((colnr_T)MAXCOL);
8959 curwin->w_curswant = MAXCOL;
8960
8961 start_arrow(&tpos);
8962}
8963
8964 static void
8965ins_s_left()
8966{
8967#ifdef FEAT_FOLDING
8968 if ((fdo_flags & FDO_HOR) && KeyTyped)
8969 foldOpenCursor();
8970#endif
8971 undisplay_dollar();
8972 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8973 {
8974 start_arrow(&curwin->w_cursor);
8975 (void)bck_word(1L, FALSE, FALSE);
8976 curwin->w_set_curswant = TRUE;
8977 }
8978 else
8979 vim_beep();
8980}
8981
8982 static void
8983ins_right()
8984{
8985#ifdef FEAT_FOLDING
8986 if ((fdo_flags & FDO_HOR) && KeyTyped)
8987 foldOpenCursor();
8988#endif
8989 undisplay_dollar();
8990 if (gchar_cursor() != NUL || virtual_active()
8991 )
8992 {
8993 start_arrow(&curwin->w_cursor);
8994 curwin->w_set_curswant = TRUE;
8995#ifdef FEAT_VIRTUALEDIT
8996 if (virtual_active())
8997 oneright();
8998 else
8999#endif
9000 {
9001#ifdef FEAT_MBYTE
9002 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009003 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009004 else
9005#endif
9006 ++curwin->w_cursor.col;
9007 }
9008
9009#ifdef FEAT_RIGHTLEFT
9010 revins_legal++;
9011 if (revins_chars)
9012 revins_chars--;
9013#endif
9014 }
9015 /* if 'whichwrap' set for cursor in insert mode, may move the
9016 * cursor to the next line */
9017 else if (vim_strchr(p_ww, ']') != NULL
9018 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9019 {
9020 start_arrow(&curwin->w_cursor);
9021 curwin->w_set_curswant = TRUE;
9022 ++curwin->w_cursor.lnum;
9023 curwin->w_cursor.col = 0;
9024 }
9025 else
9026 vim_beep();
9027}
9028
9029 static void
9030ins_s_right()
9031{
9032#ifdef FEAT_FOLDING
9033 if ((fdo_flags & FDO_HOR) && KeyTyped)
9034 foldOpenCursor();
9035#endif
9036 undisplay_dollar();
9037 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9038 || gchar_cursor() != NUL)
9039 {
9040 start_arrow(&curwin->w_cursor);
9041 (void)fwd_word(1L, FALSE, 0);
9042 curwin->w_set_curswant = TRUE;
9043 }
9044 else
9045 vim_beep();
9046}
9047
9048 static void
9049ins_up(startcol)
9050 int startcol; /* when TRUE move to Insstart.col */
9051{
9052 pos_T tpos;
9053 linenr_T old_topline = curwin->w_topline;
9054#ifdef FEAT_DIFF
9055 int old_topfill = curwin->w_topfill;
9056#endif
9057
9058 undisplay_dollar();
9059 tpos = curwin->w_cursor;
9060 if (cursor_up(1L, TRUE) == OK)
9061 {
9062 if (startcol)
9063 coladvance(getvcol_nolist(&Insstart));
9064 if (old_topline != curwin->w_topline
9065#ifdef FEAT_DIFF
9066 || old_topfill != curwin->w_topfill
9067#endif
9068 )
9069 redraw_later(VALID);
9070 start_arrow(&tpos);
9071#ifdef FEAT_CINDENT
9072 can_cindent = TRUE;
9073#endif
9074 }
9075 else
9076 vim_beep();
9077}
9078
9079 static void
9080ins_pageup()
9081{
9082 pos_T tpos;
9083
9084 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009085
9086#ifdef FEAT_WINDOWS
9087 if (mod_mask & MOD_MASK_CTRL)
9088 {
9089 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009090 if (first_tabpage->tp_next != NULL)
9091 {
9092 start_arrow(&curwin->w_cursor);
9093 goto_tabpage(-1);
9094 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009095 return;
9096 }
9097#endif
9098
Bram Moolenaar071d4272004-06-13 20:20:40 +00009099 tpos = curwin->w_cursor;
9100 if (onepage(BACKWARD, 1L) == OK)
9101 {
9102 start_arrow(&tpos);
9103#ifdef FEAT_CINDENT
9104 can_cindent = TRUE;
9105#endif
9106 }
9107 else
9108 vim_beep();
9109}
9110
9111 static void
9112ins_down(startcol)
9113 int startcol; /* when TRUE move to Insstart.col */
9114{
9115 pos_T tpos;
9116 linenr_T old_topline = curwin->w_topline;
9117#ifdef FEAT_DIFF
9118 int old_topfill = curwin->w_topfill;
9119#endif
9120
9121 undisplay_dollar();
9122 tpos = curwin->w_cursor;
9123 if (cursor_down(1L, TRUE) == OK)
9124 {
9125 if (startcol)
9126 coladvance(getvcol_nolist(&Insstart));
9127 if (old_topline != curwin->w_topline
9128#ifdef FEAT_DIFF
9129 || old_topfill != curwin->w_topfill
9130#endif
9131 )
9132 redraw_later(VALID);
9133 start_arrow(&tpos);
9134#ifdef FEAT_CINDENT
9135 can_cindent = TRUE;
9136#endif
9137 }
9138 else
9139 vim_beep();
9140}
9141
9142 static void
9143ins_pagedown()
9144{
9145 pos_T tpos;
9146
9147 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009148
9149#ifdef FEAT_WINDOWS
9150 if (mod_mask & MOD_MASK_CTRL)
9151 {
9152 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009153 if (first_tabpage->tp_next != NULL)
9154 {
9155 start_arrow(&curwin->w_cursor);
9156 goto_tabpage(0);
9157 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009158 return;
9159 }
9160#endif
9161
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162 tpos = curwin->w_cursor;
9163 if (onepage(FORWARD, 1L) == OK)
9164 {
9165 start_arrow(&tpos);
9166#ifdef FEAT_CINDENT
9167 can_cindent = TRUE;
9168#endif
9169 }
9170 else
9171 vim_beep();
9172}
9173
9174#ifdef FEAT_DND
9175 static void
9176ins_drop()
9177{
9178 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9179}
9180#endif
9181
9182/*
9183 * Handle TAB in Insert or Replace mode.
9184 * Return TRUE when the TAB needs to be inserted like a normal character.
9185 */
9186 static int
9187ins_tab()
9188{
9189 int ind;
9190 int i;
9191 int temp;
9192
9193 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9194 Insstart_blank_vcol = get_nolist_virtcol();
9195 if (echeck_abbr(TAB + ABBR_OFF))
9196 return FALSE;
9197
9198 ind = inindent(0);
9199#ifdef FEAT_CINDENT
9200 if (ind)
9201 can_cindent = FALSE;
9202#endif
9203
9204 /*
9205 * When nothing special, insert TAB like a normal character
9206 */
9207 if (!curbuf->b_p_et
9208 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9209 && curbuf->b_p_sts == 0)
9210 return TRUE;
9211
9212 if (stop_arrow() == FAIL)
9213 return TRUE;
9214
9215 did_ai = FALSE;
9216#ifdef FEAT_SMARTINDENT
9217 did_si = FALSE;
9218 can_si = FALSE;
9219 can_si_back = FALSE;
9220#endif
9221 AppendToRedobuff((char_u *)"\t");
9222
9223 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9224 temp = (int)curbuf->b_p_sw;
9225 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9226 temp = (int)curbuf->b_p_sts;
9227 else /* otherwise use 'tabstop' */
9228 temp = (int)curbuf->b_p_ts;
9229 temp -= get_nolist_virtcol() % temp;
9230
9231 /*
9232 * Insert the first space with ins_char(). It will delete one char in
9233 * replace mode. Insert the rest with ins_str(); it will not delete any
9234 * chars. For VREPLACE mode, we use ins_char() for all characters.
9235 */
9236 ins_char(' ');
9237 while (--temp > 0)
9238 {
9239#ifdef FEAT_VREPLACE
9240 if (State & VREPLACE_FLAG)
9241 ins_char(' ');
9242 else
9243#endif
9244 {
9245 ins_str((char_u *)" ");
9246 if (State & REPLACE_FLAG) /* no char replaced */
9247 replace_push(NUL);
9248 }
9249 }
9250
9251 /*
9252 * When 'expandtab' not set: Replace spaces by TABs where possible.
9253 */
9254 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9255 {
9256 char_u *ptr;
9257#ifdef FEAT_VREPLACE
9258 char_u *saved_line = NULL; /* init for GCC */
9259 pos_T pos;
9260#endif
9261 pos_T fpos;
9262 pos_T *cursor;
9263 colnr_T want_vcol, vcol;
9264 int change_col = -1;
9265 int save_list = curwin->w_p_list;
9266
9267 /*
9268 * Get the current line. For VREPLACE mode, don't make real changes
9269 * yet, just work on a copy of the line.
9270 */
9271#ifdef FEAT_VREPLACE
9272 if (State & VREPLACE_FLAG)
9273 {
9274 pos = curwin->w_cursor;
9275 cursor = &pos;
9276 saved_line = vim_strsave(ml_get_curline());
9277 if (saved_line == NULL)
9278 return FALSE;
9279 ptr = saved_line + pos.col;
9280 }
9281 else
9282#endif
9283 {
9284 ptr = ml_get_cursor();
9285 cursor = &curwin->w_cursor;
9286 }
9287
9288 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9289 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9290 curwin->w_p_list = FALSE;
9291
9292 /* Find first white before the cursor */
9293 fpos = curwin->w_cursor;
9294 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9295 {
9296 --fpos.col;
9297 --ptr;
9298 }
9299
9300 /* In Replace mode, don't change characters before the insert point. */
9301 if ((State & REPLACE_FLAG)
9302 && fpos.lnum == Insstart.lnum
9303 && fpos.col < Insstart.col)
9304 {
9305 ptr += Insstart.col - fpos.col;
9306 fpos.col = Insstart.col;
9307 }
9308
9309 /* compute virtual column numbers of first white and cursor */
9310 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9311 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9312
9313 /* Use as many TABs as possible. Beware of 'showbreak' and
9314 * 'linebreak' adding extra virtual columns. */
9315 while (vim_iswhite(*ptr))
9316 {
9317 i = lbr_chartabsize((char_u *)"\t", vcol);
9318 if (vcol + i > want_vcol)
9319 break;
9320 if (*ptr != TAB)
9321 {
9322 *ptr = TAB;
9323 if (change_col < 0)
9324 {
9325 change_col = fpos.col; /* Column of first change */
9326 /* May have to adjust Insstart */
9327 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9328 Insstart.col = fpos.col;
9329 }
9330 }
9331 ++fpos.col;
9332 ++ptr;
9333 vcol += i;
9334 }
9335
9336 if (change_col >= 0)
9337 {
9338 int repl_off = 0;
9339
9340 /* Skip over the spaces we need. */
9341 while (vcol < want_vcol && *ptr == ' ')
9342 {
9343 vcol += lbr_chartabsize(ptr, vcol);
9344 ++ptr;
9345 ++repl_off;
9346 }
9347 if (vcol > want_vcol)
9348 {
9349 /* Must have a char with 'showbreak' just before it. */
9350 --ptr;
9351 --repl_off;
9352 }
9353 fpos.col += repl_off;
9354
9355 /* Delete following spaces. */
9356 i = cursor->col - fpos.col;
9357 if (i > 0)
9358 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009359 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009360 /* correct replace stack. */
9361 if ((State & REPLACE_FLAG)
9362#ifdef FEAT_VREPLACE
9363 && !(State & VREPLACE_FLAG)
9364#endif
9365 )
9366 for (temp = i; --temp >= 0; )
9367 replace_join(repl_off);
9368 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009369#ifdef FEAT_NETBEANS_INTG
9370 if (usingNetbeans)
9371 {
9372 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9373 (long)(i + 1));
9374 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9375 (char_u *)"\t", 1);
9376 }
9377#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009378 cursor->col -= i;
9379
9380#ifdef FEAT_VREPLACE
9381 /*
9382 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9383 * backspacing over the changed spacing and then inserting the new
9384 * spacing.
9385 */
9386 if (State & VREPLACE_FLAG)
9387 {
9388 /* Backspace from real cursor to change_col */
9389 backspace_until_column(change_col);
9390
9391 /* Insert each char in saved_line from changed_col to
9392 * ptr-cursor */
9393 ins_bytes_len(saved_line + change_col,
9394 cursor->col - change_col);
9395 }
9396#endif
9397 }
9398
9399#ifdef FEAT_VREPLACE
9400 if (State & VREPLACE_FLAG)
9401 vim_free(saved_line);
9402#endif
9403 curwin->w_p_list = save_list;
9404 }
9405
9406 return FALSE;
9407}
9408
9409/*
9410 * Handle CR or NL in insert mode.
9411 * Return TRUE when out of memory or can't undo.
9412 */
9413 static int
9414ins_eol(c)
9415 int c;
9416{
9417 int i;
9418
9419 if (echeck_abbr(c + ABBR_OFF))
9420 return FALSE;
9421 if (stop_arrow() == FAIL)
9422 return TRUE;
9423 undisplay_dollar();
9424
9425 /*
9426 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9427 * character under the cursor. Only push a NUL on the replace stack,
9428 * nothing to put back when the NL is deleted.
9429 */
9430 if ((State & REPLACE_FLAG)
9431#ifdef FEAT_VREPLACE
9432 && !(State & VREPLACE_FLAG)
9433#endif
9434 )
9435 replace_push(NUL);
9436
9437 /*
9438 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9439 * replacing the next line, so we push all of the characters left on the
9440 * line onto the replace stack. This is not done here though, it is done
9441 * in open_line().
9442 */
9443
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009444#ifdef FEAT_VIRTUALEDIT
9445 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9446 * CTRL-O). */
9447 if (virtual_active() && curwin->w_cursor.coladd > 0)
9448 coladvance(getviscol());
9449#endif
9450
Bram Moolenaar071d4272004-06-13 20:20:40 +00009451#ifdef FEAT_RIGHTLEFT
9452# ifdef FEAT_FKMAP
9453 if (p_altkeymap && p_fkmap)
9454 fkmap(NL);
9455# endif
9456 /* NL in reverse insert will always start in the end of
9457 * current line. */
9458 if (revins_on)
9459 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9460#endif
9461
9462 AppendToRedobuff(NL_STR);
9463 i = open_line(FORWARD,
9464#ifdef FEAT_COMMENTS
9465 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9466#endif
9467 0, old_indent);
9468 old_indent = 0;
9469#ifdef FEAT_CINDENT
9470 can_cindent = TRUE;
9471#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009472#ifdef FEAT_FOLDING
9473 /* When inserting a line the cursor line must never be in a closed fold. */
9474 foldOpenCursor();
9475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009476
9477 return (!i);
9478}
9479
9480#ifdef FEAT_DIGRAPHS
9481/*
9482 * Handle digraph in insert mode.
9483 * Returns character still to be inserted, or NUL when nothing remaining to be
9484 * done.
9485 */
9486 static int
9487ins_digraph()
9488{
9489 int c;
9490 int cc;
9491
9492 pc_status = PC_STATUS_UNSET;
9493 if (redrawing() && !char_avail())
9494 {
9495 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009496 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009497
9498 edit_putchar('?', TRUE);
9499#ifdef FEAT_CMDL_INFO
9500 add_to_showcmd_c(Ctrl_K);
9501#endif
9502 }
9503
9504#ifdef USE_ON_FLY_SCROLL
9505 dont_scroll = TRUE; /* disallow scrolling here */
9506#endif
9507
9508 /* don't map the digraph chars. This also prevents the
9509 * mode message to be deleted when ESC is hit */
9510 ++no_mapping;
9511 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009512 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009513 --no_mapping;
9514 --allow_keys;
9515 if (IS_SPECIAL(c) || mod_mask) /* special key */
9516 {
9517#ifdef FEAT_CMDL_INFO
9518 clear_showcmd();
9519#endif
9520 insert_special(c, TRUE, FALSE);
9521 return NUL;
9522 }
9523 if (c != ESC)
9524 {
9525 if (redrawing() && !char_avail())
9526 {
9527 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009528 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529
9530 if (char2cells(c) == 1)
9531 {
9532 /* first remove the '?', otherwise it's restored when typing
9533 * an ESC next */
9534 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009535 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 edit_putchar(c, TRUE);
9537 }
9538#ifdef FEAT_CMDL_INFO
9539 add_to_showcmd_c(c);
9540#endif
9541 }
9542 ++no_mapping;
9543 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009544 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545 --no_mapping;
9546 --allow_keys;
9547 if (cc != ESC)
9548 {
9549 AppendToRedobuff((char_u *)CTRL_V_STR);
9550 c = getdigraph(c, cc, TRUE);
9551#ifdef FEAT_CMDL_INFO
9552 clear_showcmd();
9553#endif
9554 return c;
9555 }
9556 }
9557 edit_unputchar();
9558#ifdef FEAT_CMDL_INFO
9559 clear_showcmd();
9560#endif
9561 return NUL;
9562}
9563#endif
9564
9565/*
9566 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9567 * Returns the char to be inserted, or NUL if none found.
9568 */
9569 static int
9570ins_copychar(lnum)
9571 linenr_T lnum;
9572{
9573 int c;
9574 int temp;
9575 char_u *ptr, *prev_ptr;
9576
9577 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9578 {
9579 vim_beep();
9580 return NUL;
9581 }
9582
9583 /* try to advance to the cursor column */
9584 temp = 0;
9585 ptr = ml_get(lnum);
9586 prev_ptr = ptr;
9587 validate_virtcol();
9588 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9589 {
9590 prev_ptr = ptr;
9591 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9592 }
9593 if ((colnr_T)temp > curwin->w_virtcol)
9594 ptr = prev_ptr;
9595
9596#ifdef FEAT_MBYTE
9597 c = (*mb_ptr2char)(ptr);
9598#else
9599 c = *ptr;
9600#endif
9601 if (c == NUL)
9602 vim_beep();
9603 return c;
9604}
9605
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009606/*
9607 * CTRL-Y or CTRL-E typed in Insert mode.
9608 */
9609 static int
9610ins_ctrl_ey(tc)
9611 int tc;
9612{
9613 int c = tc;
9614
9615#ifdef FEAT_INS_EXPAND
9616 if (ctrl_x_mode == CTRL_X_SCROLL)
9617 {
9618 if (c == Ctrl_Y)
9619 scrolldown_clamp();
9620 else
9621 scrollup_clamp();
9622 redraw_later(VALID);
9623 }
9624 else
9625#endif
9626 {
9627 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9628 if (c != NUL)
9629 {
9630 long tw_save;
9631
9632 /* The character must be taken literally, insert like it
9633 * was typed after a CTRL-V, and pretend 'textwidth'
9634 * wasn't set. Digits, 'o' and 'x' are special after a
9635 * CTRL-V, don't use it for these. */
9636 if (c < 256 && !isalnum(c))
9637 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9638 tw_save = curbuf->b_p_tw;
9639 curbuf->b_p_tw = -1;
9640 insert_special(c, TRUE, FALSE);
9641 curbuf->b_p_tw = tw_save;
9642#ifdef FEAT_RIGHTLEFT
9643 revins_chars++;
9644 revins_legal++;
9645#endif
9646 c = Ctrl_V; /* pretend CTRL-V is last character */
9647 auto_format(FALSE, TRUE);
9648 }
9649 }
9650 return c;
9651}
9652
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653#ifdef FEAT_SMARTINDENT
9654/*
9655 * Try to do some very smart auto-indenting.
9656 * Used when inserting a "normal" character.
9657 */
9658 static void
9659ins_try_si(c)
9660 int c;
9661{
9662 pos_T *pos, old_pos;
9663 char_u *ptr;
9664 int i;
9665 int temp;
9666
9667 /*
9668 * do some very smart indenting when entering '{' or '}'
9669 */
9670 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9671 {
9672 /*
9673 * for '}' set indent equal to indent of line containing matching '{'
9674 */
9675 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9676 {
9677 old_pos = curwin->w_cursor;
9678 /*
9679 * If the matching '{' has a ')' immediately before it (ignoring
9680 * white-space), then line up with the start of the line
9681 * containing the matching '(' if there is one. This handles the
9682 * case where an "if (..\n..) {" statement continues over multiple
9683 * lines -- webb
9684 */
9685 ptr = ml_get(pos->lnum);
9686 i = pos->col;
9687 if (i > 0) /* skip blanks before '{' */
9688 while (--i > 0 && vim_iswhite(ptr[i]))
9689 ;
9690 curwin->w_cursor.lnum = pos->lnum;
9691 curwin->w_cursor.col = i;
9692 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9693 curwin->w_cursor = *pos;
9694 i = get_indent();
9695 curwin->w_cursor = old_pos;
9696#ifdef FEAT_VREPLACE
9697 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009698 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009699 else
9700#endif
9701 (void)set_indent(i, SIN_CHANGED);
9702 }
9703 else if (curwin->w_cursor.col > 0)
9704 {
9705 /*
9706 * when inserting '{' after "O" reduce indent, but not
9707 * more than indent of previous line
9708 */
9709 temp = TRUE;
9710 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9711 {
9712 old_pos = curwin->w_cursor;
9713 i = get_indent();
9714 while (curwin->w_cursor.lnum > 1)
9715 {
9716 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9717
9718 /* ignore empty lines and lines starting with '#'. */
9719 if (*ptr != '#' && *ptr != NUL)
9720 break;
9721 }
9722 if (get_indent() >= i)
9723 temp = FALSE;
9724 curwin->w_cursor = old_pos;
9725 }
9726 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009727 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728 }
9729 }
9730
9731 /*
9732 * set indent of '#' always to 0
9733 */
9734 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9735 {
9736 /* remember current indent for next line */
9737 old_indent = get_indent();
9738 (void)set_indent(0, SIN_CHANGED);
9739 }
9740
9741 /* Adjust ai_col, the char at this position can be deleted. */
9742 if (ai_col > curwin->w_cursor.col)
9743 ai_col = curwin->w_cursor.col;
9744}
9745#endif
9746
9747/*
9748 * Get the value that w_virtcol would have when 'list' is off.
9749 * Unless 'cpo' contains the 'L' flag.
9750 */
9751 static colnr_T
9752get_nolist_virtcol()
9753{
9754 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9755 return getvcol_nolist(&curwin->w_cursor);
9756 validate_virtcol();
9757 return curwin->w_virtcol;
9758}