blob: b998f8dfb27a2909b9661f34b36e7642e171716c [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 */
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001961/*ARGSUSED*/
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001962 static int
1963del_char_after_col(limit_col)
1964 int limit_col;
1965{
1966#ifdef FEAT_MBYTE
1967 if (enc_utf8 && limit_col >= 0)
1968 {
1969 int ecol = curwin->w_cursor.col + 1;
1970
1971 /* Make sure the cursor is at the start of a character, but
1972 * skip forward again when going too far back because of a
1973 * composing character. */
1974 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00001975 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001976 {
1977 int l = utf_ptr2len(ml_get_cursor());
1978
1979 if (l == 0) /* end of line */
1980 break;
1981 curwin->w_cursor.col += l;
1982 }
1983 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
1984 return FALSE;
1985 del_bytes((long)(ecol - curwin->w_cursor.col), FALSE, TRUE);
1986 }
1987 else
1988#endif
1989 (void)del_char(FALSE);
1990 return TRUE;
1991}
1992
Bram Moolenaar071d4272004-06-13 20:20:40 +00001993#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1994/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001995 * CTRL-X pressed in Insert mode.
1996 */
1997 static void
1998ins_ctrl_x()
1999{
2000 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2001 * CTRL-V works like CTRL-N */
2002 if (ctrl_x_mode != CTRL_X_CMDLINE)
2003 {
2004 /* if the next ^X<> won't ADD nothing, then reset
2005 * compl_cont_status */
2006 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002007 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002008 else
2009 compl_cont_status = 0;
2010 /* We're not sure which CTRL-X mode it will be yet */
2011 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2012 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2013 edit_submode_pre = NULL;
2014 showmode();
2015 }
2016}
2017
2018/*
2019 * Return TRUE if the 'dict' or 'tsr' option can be used.
2020 */
2021 static int
2022has_compl_option(dict_opt)
2023 int dict_opt;
2024{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002025 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002026# ifdef FEAT_SPELL
2027 && !curwin->w_p_spell
2028# endif
2029 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002030 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2031 {
2032 ctrl_x_mode = 0;
2033 edit_submode = NULL;
2034 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2035 : (char_u *)_("'thesaurus' option is empty"),
2036 hl_attr(HLF_E));
2037 if (emsg_silent == 0)
2038 {
2039 vim_beep();
2040 setcursor();
2041 out_flush();
2042 ui_delay(2000L, FALSE);
2043 }
2044 return FALSE;
2045 }
2046 return TRUE;
2047}
2048
2049/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002050 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2051 * This depends on the current mode.
2052 */
2053 int
2054vim_is_ctrl_x_key(c)
2055 int c;
2056{
2057 /* Always allow ^R - let it's results then be checked */
2058 if (c == Ctrl_R)
2059 return TRUE;
2060
Bram Moolenaare3226be2005-12-18 22:10:00 +00002061 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002062 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002063 return TRUE;
2064
Bram Moolenaar071d4272004-06-13 20:20:40 +00002065 switch (ctrl_x_mode)
2066 {
2067 case 0: /* Not in any CTRL-X mode */
2068 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2069 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002070 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002071 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2072 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2073 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002074 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2075 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002076 case CTRL_X_SCROLL:
2077 return (c == Ctrl_Y || c == Ctrl_E);
2078 case CTRL_X_WHOLE_LINE:
2079 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2080 case CTRL_X_FILES:
2081 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2082 case CTRL_X_DICTIONARY:
2083 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2084 case CTRL_X_THESAURUS:
2085 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2086 case CTRL_X_TAGS:
2087 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2088#ifdef FEAT_FIND_ID
2089 case CTRL_X_PATH_PATTERNS:
2090 return (c == Ctrl_P || c == Ctrl_N);
2091 case CTRL_X_PATH_DEFINES:
2092 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2093#endif
2094 case CTRL_X_CMDLINE:
2095 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2096 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002097#ifdef FEAT_COMPL_FUNC
2098 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002099 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002100 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002101 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002102#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002103 case CTRL_X_SPELL:
2104 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002105 }
2106 EMSG(_(e_internal));
2107 return FALSE;
2108}
2109
2110/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002111 * Return TRUE when character "c" is part of the item currently being
2112 * completed. Used to decide whether to abandon complete mode when the menu
2113 * is visible.
2114 */
2115 static int
2116ins_compl_accept_char(c)
2117 int c;
2118{
2119 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2120 /* When expanding an identifier only accept identifier chars. */
2121 return vim_isIDc(c);
2122
2123 switch (ctrl_x_mode)
2124 {
2125 case CTRL_X_FILES:
2126 /* When expanding file name only accept file name chars. But not
2127 * path separators, so that "proto/<Tab>" expands files in
2128 * "proto", not "proto/" as a whole */
2129 return vim_isfilec(c) && !vim_ispathsep(c);
2130
2131 case CTRL_X_CMDLINE:
2132 case CTRL_X_OMNI:
2133 /* Command line and Omni completion can work with just about any
2134 * printable character, but do stop at white space. */
2135 return vim_isprintc(c) && !vim_iswhite(c);
2136
2137 case CTRL_X_WHOLE_LINE:
2138 /* For while line completion a space can be part of the line. */
2139 return vim_isprintc(c);
2140 }
2141 return vim_iswordc(c);
2142}
2143
2144/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002145 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002146 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002147 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002148 * the rest of the word to be in -- webb
2149 */
2150 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002151ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002152 char_u *str;
2153 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002154 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002155 char_u *fname;
2156 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002157 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002158{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002159 char_u *p;
2160 int i, c;
2161 int actual_len; /* Take multi-byte characters */
2162 int actual_compl_length; /* into account. */
2163 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002164 int has_lower = FALSE;
2165 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002166
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002167 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002168 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002169 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170
Bram Moolenaara2993e12007-08-12 14:38:46 +00002171 /* Find actual length of completion. */
2172#ifdef FEAT_MBYTE
2173 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002174 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002175 p = str;
2176 actual_len = 0;
2177 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002178 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002179 mb_ptr_adv(p);
2180 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 }
2182 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002183 else
2184#endif
2185 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186
Bram Moolenaara2993e12007-08-12 14:38:46 +00002187 /* Find actual length of original text. */
2188#ifdef FEAT_MBYTE
2189 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002190 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002191 p = compl_orig_text;
2192 actual_compl_length = 0;
2193 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002195 mb_ptr_adv(p);
2196 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002197 }
2198 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002199 else
2200#endif
2201 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202
Bram Moolenaara2993e12007-08-12 14:38:46 +00002203 /* Allocate wide character array for the completion and fill it. */
2204 wca = (int *)alloc(actual_len * sizeof(int));
2205 if (wca != NULL)
2206 {
2207 p = str;
2208 for (i = 0; i < actual_len; ++i)
2209#ifdef FEAT_MBYTE
2210 if (has_mbyte)
2211 wca[i] = mb_ptr2char_adv(&p);
2212 else
2213#endif
2214 wca[i] = *(p++);
2215
2216 /* Rule 1: Were any chars converted to lower? */
2217 p = compl_orig_text;
2218 for (i = 0; i < actual_compl_length; ++i)
2219 {
2220#ifdef FEAT_MBYTE
2221 if (has_mbyte)
2222 c = mb_ptr2char_adv(&p);
2223 else
2224#endif
2225 c = *(p++);
2226 if (MB_ISLOWER(c))
2227 {
2228 has_lower = TRUE;
2229 if (MB_ISUPPER(wca[i]))
2230 {
2231 /* Rule 1 is satisfied. */
2232 for (i = actual_compl_length; i < actual_len; ++i)
2233 wca[i] = MB_TOLOWER(wca[i]);
2234 break;
2235 }
2236 }
2237 }
2238
2239 /*
2240 * Rule 2: No lower case, 2nd consecutive letter converted to
2241 * upper case.
2242 */
2243 if (!has_lower)
2244 {
2245 p = compl_orig_text;
2246 for (i = 0; i < actual_compl_length; ++i)
2247 {
2248#ifdef FEAT_MBYTE
2249 if (has_mbyte)
2250 c = mb_ptr2char_adv(&p);
2251 else
2252#endif
2253 c = *(p++);
2254 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2255 {
2256 /* Rule 2 is satisfied. */
2257 for (i = actual_compl_length; i < actual_len; ++i)
2258 wca[i] = MB_TOUPPER(wca[i]);
2259 break;
2260 }
2261 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2262 }
2263 }
2264
2265 /* Copy the original case of the part we typed. */
2266 p = compl_orig_text;
2267 for (i = 0; i < actual_compl_length; ++i)
2268 {
2269#ifdef FEAT_MBYTE
2270 if (has_mbyte)
2271 c = mb_ptr2char_adv(&p);
2272 else
2273#endif
2274 c = *(p++);
2275 if (MB_ISLOWER(c))
2276 wca[i] = MB_TOLOWER(wca[i]);
2277 else if (MB_ISUPPER(c))
2278 wca[i] = MB_TOUPPER(wca[i]);
2279 }
2280
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002281 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002282 * Generate encoding specific output from wide character array.
2283 * Multi-byte characters can occupy up to five bytes more than
2284 * ASCII characters, and we also need one byte for NUL, so stay
2285 * six bytes away from the edge of IObuff.
2286 */
2287 p = IObuff;
2288 i = 0;
2289 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2290#ifdef FEAT_MBYTE
2291 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002292 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002293 else
2294#endif
2295 *(p++) = wca[i++];
2296 *p = NUL;
2297
2298 vim_free(wca);
2299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002300
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002301 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2302 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002303 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002304 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002305}
2306
2307/*
2308 * Add a match to the list of matches.
2309 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002310 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002311 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002312 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002313 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002314ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002315 char_u *str;
2316 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002317 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002318 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002319 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002320 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002321 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002322 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002323{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002324 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002325 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002326
2327 ui_breakcheck();
2328 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002329 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002330 if (len < 0)
2331 len = (int)STRLEN(str);
2332
2333 /*
2334 * If the same match is already present, don't add it.
2335 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002336 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002337 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002338 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002339 do
2340 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002341 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002342 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002343 && match->cp_str[len] == NUL)
2344 return NOTDONE;
2345 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002346 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002347 }
2348
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002349 /* Remove any popup menu before changing the list of matches. */
2350 ins_compl_del_pum();
2351
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352 /*
2353 * Allocate a new match structure.
2354 * Copy the values to the new match structure.
2355 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002356 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002358 return FAIL;
2359 match->cp_number = -1;
2360 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002361 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002362 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002363 {
2364 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002365 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002366 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002367 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002368
Bram Moolenaar071d4272004-06-13 20:20:40 +00002369 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002370 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2372 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002373 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002374 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002375 && compl_curr_match->cp_fname != NULL
2376 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002377 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002378 else if (fname != NULL)
2379 {
2380 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002381 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002384 match->cp_fname = NULL;
2385 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002386
2387 if (cptext != NULL)
2388 {
2389 int i;
2390
2391 for (i = 0; i < CPT_COUNT; ++i)
2392 if (cptext[i] != NULL && *cptext[i] != NUL)
2393 match->cp_text[i] = vim_strsave(cptext[i]);
2394 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002395
2396 /*
2397 * Link the new match structure in the list of matches.
2398 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002399 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002400 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 else if (dir == FORWARD)
2402 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002403 match->cp_next = compl_curr_match->cp_next;
2404 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002405 }
2406 else /* BACKWARD */
2407 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002408 match->cp_next = compl_curr_match;
2409 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002411 if (match->cp_next)
2412 match->cp_next->cp_prev = match;
2413 if (match->cp_prev)
2414 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002416 compl_first_match = match;
2417 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002419 /*
2420 * Find the longest common string if still doing that.
2421 */
2422 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2423 ins_compl_longest_match(match);
2424
Bram Moolenaar071d4272004-06-13 20:20:40 +00002425 return OK;
2426}
2427
2428/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002429 * Return TRUE if "str[len]" matches with match->cp_str, considering
2430 * match->cp_icase.
2431 */
2432 static int
2433ins_compl_equal(match, str, len)
2434 compl_T *match;
2435 char_u *str;
2436 int len;
2437{
2438 if (match->cp_icase)
2439 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2440 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2441}
2442
2443/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002444 * Reduce the longest common string for match "match".
2445 */
2446 static void
2447ins_compl_longest_match(match)
2448 compl_T *match;
2449{
2450 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002451 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002452 int had_match;
2453
2454 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002455 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002456 /* First match, use it as a whole. */
2457 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002458 if (compl_leader != NULL)
2459 {
2460 had_match = (curwin->w_cursor.col > compl_col);
2461 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002462 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002463 ins_redraw(FALSE);
2464
2465 /* When the match isn't there (to avoid matching itself) remove it
2466 * again after redrawing. */
2467 if (!had_match)
2468 ins_compl_delete();
2469 compl_used_match = FALSE;
2470 }
2471 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002472 else
2473 {
2474 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002475 p = compl_leader;
2476 s = match->cp_str;
2477 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002478 {
2479#ifdef FEAT_MBYTE
2480 if (has_mbyte)
2481 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002482 c1 = mb_ptr2char(p);
2483 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002484 }
2485 else
2486#endif
2487 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002488 c1 = *p;
2489 c2 = *s;
2490 }
2491 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2492 : (c1 != c2))
2493 break;
2494#ifdef FEAT_MBYTE
2495 if (has_mbyte)
2496 {
2497 mb_ptr_adv(p);
2498 mb_ptr_adv(s);
2499 }
2500 else
2501#endif
2502 {
2503 ++p;
2504 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002505 }
2506 }
2507
2508 if (*p != NUL)
2509 {
2510 /* Leader was shortened, need to change the inserted text. */
2511 *p = NUL;
2512 had_match = (curwin->w_cursor.col > compl_col);
2513 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002514 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002515 ins_redraw(FALSE);
2516
2517 /* When the match isn't there (to avoid matching itself) remove it
2518 * again after redrawing. */
2519 if (!had_match)
2520 ins_compl_delete();
2521 }
2522
2523 compl_used_match = FALSE;
2524 }
2525}
2526
2527/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002528 * Add an array of matches to the list of matches.
2529 * Frees matches[].
2530 */
2531 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002532ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002533 int num_matches;
2534 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002535 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002536{
2537 int i;
2538 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002539 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540
Bram Moolenaar572cb562005-08-05 21:35:02 +00002541 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002542 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002543 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002544 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002545 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002546 FreeWild(num_matches, matches);
2547}
2548
2549/* Make the completion list cyclic.
2550 * Return the number of matches (excluding the original).
2551 */
2552 static int
2553ins_compl_make_cyclic()
2554{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002555 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002556 int count = 0;
2557
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002558 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 {
2560 /*
2561 * Find the end of the list.
2562 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002563 match = compl_first_match;
2564 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002565 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002567 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002568 ++count;
2569 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002570 match->cp_next = compl_first_match;
2571 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002572 }
2573 return count;
2574}
2575
Bram Moolenaara94bc432006-03-10 21:42:59 +00002576/*
2577 * Start completion for the complete() function.
2578 * "startcol" is where the matched text starts (1 is first column).
2579 * "list" is the list of matches.
2580 */
2581 void
2582set_completion(startcol, list)
2583 int startcol;
2584 list_T *list;
2585{
2586 /* If already doing completions stop it. */
2587 if (ctrl_x_mode != 0)
2588 ins_compl_prep(' ');
2589 ins_compl_clear();
2590
2591 if (stop_arrow() == FAIL)
2592 return;
2593
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002594 if (startcol > (int)curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002595 startcol = curwin->w_cursor.col;
2596 compl_col = startcol;
2597 compl_length = curwin->w_cursor.col - startcol;
2598 /* compl_pattern doesn't need to be set */
2599 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2600 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002601 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002602 return;
2603
2604 /* Handle like dictionary completion. */
2605 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2606
2607 ins_compl_add_list(list);
2608 compl_matches = ins_compl_make_cyclic();
2609 compl_started = TRUE;
2610 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002611 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002612
2613 compl_curr_match = compl_first_match;
2614 ins_complete(Ctrl_N);
2615 out_flush();
2616}
2617
2618
Bram Moolenaar9372a112005-12-06 19:59:18 +00002619/* "compl_match_array" points the currently displayed list of entries in the
2620 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002621static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002622static int compl_match_arraysize;
2623
2624/*
2625 * Update the screen and when there is any scrolling remove the popup menu.
2626 */
2627 static void
2628ins_compl_upd_pum()
2629{
2630 int h;
2631
2632 if (compl_match_array != NULL)
2633 {
2634 h = curwin->w_cline_height;
2635 update_screen(0);
2636 if (h != curwin->w_cline_height)
2637 ins_compl_del_pum();
2638 }
2639}
2640
2641/*
2642 * Remove any popup menu.
2643 */
2644 static void
2645ins_compl_del_pum()
2646{
2647 if (compl_match_array != NULL)
2648 {
2649 pum_undisplay();
2650 vim_free(compl_match_array);
2651 compl_match_array = NULL;
2652 }
2653}
2654
2655/*
2656 * Return TRUE if the popup menu should be displayed.
2657 */
2658 static int
2659pum_wanted()
2660{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002661 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002662 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002663 return FALSE;
2664
2665 /* The display looks bad on a B&W display. */
2666 if (t_colors < 8
2667#ifdef FEAT_GUI
2668 && !gui.in_use
2669#endif
2670 )
2671 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002672 return TRUE;
2673}
2674
2675/*
2676 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002677 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002678 */
2679 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002680pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002681{
2682 compl_T *compl;
2683 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002684
2685 /* Don't display the popup menu if there are no matches or there is only
2686 * one (ignoring the original text). */
2687 compl = compl_first_match;
2688 i = 0;
2689 do
2690 {
2691 if (compl == NULL
2692 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2693 break;
2694 compl = compl->cp_next;
2695 } while (compl != compl_first_match);
2696
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002697 if (strstr((char *)p_cot, "menuone") != NULL)
2698 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002699 return (i >= 2);
2700}
2701
2702/*
2703 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002704 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002705 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002706 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002707ins_compl_show_pum()
2708{
2709 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002710 compl_T *shown_compl = NULL;
2711 int did_find_shown_match = FALSE;
2712 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002713 int i;
2714 int cur = -1;
2715 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002716 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002717
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002718 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002719 return;
2720
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002721#if defined(FEAT_EVAL)
2722 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2723 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2724#endif
2725
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002726 /* Update the screen before drawing the popup menu over it. */
2727 update_screen(0);
2728
2729 if (compl_match_array == NULL)
2730 {
2731 /* Need to build the popup menu list. */
2732 compl_match_arraysize = 0;
2733 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002734 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002735 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002736 do
2737 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002738 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2739 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002740 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002741 ++compl_match_arraysize;
2742 compl = compl->cp_next;
2743 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002744 if (compl_match_arraysize == 0)
2745 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002746 compl_match_array = (pumitem_T *)alloc_clear(
2747 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002748 * compl_match_arraysize));
2749 if (compl_match_array != NULL)
2750 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002751 /* If the current match is the original text don't find the first
2752 * match after it, don't highlight anything. */
2753 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2754 shown_match_ok = TRUE;
2755
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002756 i = 0;
2757 compl = compl_first_match;
2758 do
2759 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002760 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2761 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002762 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002763 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002764 if (!shown_match_ok)
2765 {
2766 if (compl == compl_shown_match || did_find_shown_match)
2767 {
2768 /* This item is the shown match or this is the
2769 * first displayed item after the shown match. */
2770 compl_shown_match = compl;
2771 did_find_shown_match = TRUE;
2772 shown_match_ok = TRUE;
2773 }
2774 else
2775 /* Remember this displayed match for when the
2776 * shown match is just below it. */
2777 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002778 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002779 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002780
2781 if (compl->cp_text[CPT_ABBR] != NULL)
2782 compl_match_array[i].pum_text =
2783 compl->cp_text[CPT_ABBR];
2784 else
2785 compl_match_array[i].pum_text = compl->cp_str;
2786 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2787 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2788 if (compl->cp_text[CPT_MENU] != NULL)
2789 compl_match_array[i++].pum_extra =
2790 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002791 else
2792 compl_match_array[i++].pum_extra = compl->cp_fname;
2793 }
2794
2795 if (compl == compl_shown_match)
2796 {
2797 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002798
2799 /* When the original text is the shown match don't set
2800 * compl_shown_match. */
2801 if (compl->cp_flags & ORIGINAL_TEXT)
2802 shown_match_ok = TRUE;
2803
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002804 if (!shown_match_ok && shown_compl != NULL)
2805 {
2806 /* The shown match isn't displayed, set it to the
2807 * previously displayed match. */
2808 compl_shown_match = shown_compl;
2809 shown_match_ok = TRUE;
2810 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002811 }
2812 compl = compl->cp_next;
2813 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002814
2815 if (!shown_match_ok) /* no displayed match at all */
2816 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002817 }
2818 }
2819 else
2820 {
2821 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002822 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002823 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2824 || compl_match_array[i].pum_text
2825 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002826 {
2827 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002828 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002829 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002830 }
2831
2832 if (compl_match_array != NULL)
2833 {
2834 /* Compute the screen column of the start of the completed text.
2835 * Use the cursor to get all wrapping and other settings right. */
2836 col = curwin->w_cursor.col;
2837 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002838 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002839 curwin->w_cursor.col = col;
2840 }
2841}
2842
Bram Moolenaar071d4272004-06-13 20:20:40 +00002843#define DICT_FIRST (1) /* use just first element in "dict" */
2844#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002845
Bram Moolenaar071d4272004-06-13 20:20:40 +00002846/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002847 * Add any identifiers that match the given pattern in the list of dictionary
2848 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002849 */
2850 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002851ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2852 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002853 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002854 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002855 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002856{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002857 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002858 char_u *ptr;
2859 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002860 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002861 char_u **files;
2862 int count;
2863 int i;
2864 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002865 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002866
Bram Moolenaar0b238792006-03-02 22:49:12 +00002867 if (*dict == NUL)
2868 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002869#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002870 /* When 'dictionary' is empty and spell checking is enabled use
2871 * "spell". */
2872 if (!thesaurus && curwin->w_p_spell)
2873 dict = (char_u *)"spell";
2874 else
2875#endif
2876 return;
2877 }
2878
Bram Moolenaar071d4272004-06-13 20:20:40 +00002879 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002880 if (buf == NULL)
2881 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002882 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002883
Bram Moolenaar071d4272004-06-13 20:20:40 +00002884 /* If 'infercase' is set, don't use 'smartcase' here */
2885 save_p_scs = p_scs;
2886 if (curbuf->b_p_inf)
2887 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002888
2889 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2890 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002891 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002892 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2893 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002894 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
2895
2896 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002897 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002898 i = (int)STRLEN(pat_esc) + 10;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002899 ptr = alloc(i);
2900 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002901 {
2902 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002903 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002904 }
2905 vim_snprintf((char *)ptr, i, "^\\s*\\zs\\V%s", pat_esc);
2906 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2907 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002908 vim_free(ptr);
2909 }
2910 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002911 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002912 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002913 if (regmatch.regprog == NULL)
2914 goto theend;
2915 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002916
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2918 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002919 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002920 {
2921 /* copy one dictionary file name into buf */
2922 if (flags == DICT_EXACT)
2923 {
2924 count = 1;
2925 files = &dict;
2926 }
2927 else
2928 {
2929 /* Expand wildcards in the dictionary name, but do not allow
2930 * backticks (for security, the 'dict' option may have been set in
2931 * a modeline). */
2932 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002933# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002934 if (!thesaurus && STRCMP(buf, "spell") == 0)
2935 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002936 else
2937# endif
2938 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002939 || expand_wildcards(1, &buf, &count, &files,
2940 EW_FILE|EW_SILENT) != OK)
2941 count = 0;
2942 }
2943
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002944# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002945 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002946 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002947 /* Complete from active spelling. Skip "\<" in the pattern, we
2948 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002949 if (pat[0] == '\\' && pat[1] == '<')
2950 ptr = pat + 2;
2951 else
2952 ptr = pat;
2953 spell_dump_compl(curbuf, ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002954 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002955 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002956# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00002957 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002958 {
2959 ins_compl_files(count, files, thesaurus, flags,
2960 &regmatch, buf, &dir);
2961 if (flags != DICT_EXACT)
2962 FreeWild(count, files);
2963 }
2964 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002965 break;
2966 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00002967
2968theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 p_scs = save_p_scs;
2970 vim_free(regmatch.regprog);
2971 vim_free(buf);
2972}
2973
Bram Moolenaar0b238792006-03-02 22:49:12 +00002974 static void
2975ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
2976 int count;
2977 char_u **files;
2978 int thesaurus;
2979 int flags;
2980 regmatch_T *regmatch;
2981 char_u *buf;
2982 int *dir;
2983{
2984 char_u *ptr;
2985 int i;
2986 FILE *fp;
2987 int add_r;
2988
2989 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
2990 {
2991 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
2992 if (flags != DICT_EXACT)
2993 {
2994 vim_snprintf((char *)IObuff, IOSIZE,
2995 _("Scanning dictionary: %s"), (char *)files[i]);
2996 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
2997 }
2998
2999 if (fp != NULL)
3000 {
3001 /*
3002 * Read dictionary file line by line.
3003 * Check each line for a match.
3004 */
3005 while (!got_int && !compl_interrupted
3006 && !vim_fgets(buf, LSIZE, fp))
3007 {
3008 ptr = buf;
3009 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3010 {
3011 ptr = regmatch->startp[0];
3012 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3013 ptr = find_line_end(ptr);
3014 else
3015 ptr = find_word_end(ptr);
3016 add_r = ins_compl_add_infercase(regmatch->startp[0],
3017 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003018 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003019 if (thesaurus)
3020 {
3021 char_u *wstart;
3022
3023 /*
3024 * Add the other matches on the line
3025 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003026 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003027 while (!got_int)
3028 {
3029 /* Find start of the next word. Skip white
3030 * space and punctuation. */
3031 ptr = find_word_start(ptr);
3032 if (*ptr == NUL || *ptr == NL)
3033 break;
3034 wstart = ptr;
3035
Bram Moolenaara2993e12007-08-12 14:38:46 +00003036 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003037#ifdef FEAT_MBYTE
3038 if (has_mbyte)
3039 /* Japanese words may have characters in
3040 * different classes, only separate words
3041 * with single-byte non-word characters. */
3042 while (*ptr != NUL)
3043 {
3044 int l = (*mb_ptr2len)(ptr);
3045
3046 if (l < 2 && !vim_iswordc(*ptr))
3047 break;
3048 ptr += l;
3049 }
3050 else
3051#endif
3052 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003053
3054 /* Add the word. Skip the regexp match. */
3055 if (wstart != regmatch->startp[0])
3056 add_r = ins_compl_add_infercase(wstart,
3057 (int)(ptr - wstart),
3058 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003059 }
3060 }
3061 if (add_r == OK)
3062 /* if dir was BACKWARD then honor it just once */
3063 *dir = FORWARD;
3064 else if (add_r == FAIL)
3065 break;
3066 /* avoid expensive call to vim_regexec() when at end
3067 * of line */
3068 if (*ptr == '\n' || got_int)
3069 break;
3070 }
3071 line_breakcheck();
3072 ins_compl_check_keys(50);
3073 }
3074 fclose(fp);
3075 }
3076 }
3077}
3078
Bram Moolenaar071d4272004-06-13 20:20:40 +00003079/*
3080 * Find the start of the next word.
3081 * Returns a pointer to the first char of the word. Also stops at a NUL.
3082 */
3083 char_u *
3084find_word_start(ptr)
3085 char_u *ptr;
3086{
3087#ifdef FEAT_MBYTE
3088 if (has_mbyte)
3089 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003090 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003091 else
3092#endif
3093 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3094 ++ptr;
3095 return ptr;
3096}
3097
3098/*
3099 * Find the end of the word. Assumes it starts inside a word.
3100 * Returns a pointer to just after the word.
3101 */
3102 char_u *
3103find_word_end(ptr)
3104 char_u *ptr;
3105{
3106#ifdef FEAT_MBYTE
3107 int start_class;
3108
3109 if (has_mbyte)
3110 {
3111 start_class = mb_get_class(ptr);
3112 if (start_class > 1)
3113 while (*ptr != NUL)
3114 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003115 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003116 if (mb_get_class(ptr) != start_class)
3117 break;
3118 }
3119 }
3120 else
3121#endif
3122 while (vim_iswordc(*ptr))
3123 ++ptr;
3124 return ptr;
3125}
3126
3127/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003128 * Find the end of the line, omitting CR and NL at the end.
3129 * Returns a pointer to just after the line.
3130 */
3131 static char_u *
3132find_line_end(ptr)
3133 char_u *ptr;
3134{
3135 char_u *s;
3136
3137 s = ptr + STRLEN(ptr);
3138 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3139 --s;
3140 return s;
3141}
3142
3143/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003144 * Free the list of completions
3145 */
3146 static void
3147ins_compl_free()
3148{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003149 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003150 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003152 vim_free(compl_pattern);
3153 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003154 vim_free(compl_leader);
3155 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003157 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003158 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003159
3160 ins_compl_del_pum();
3161 pum_clear();
3162
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003163 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003164 do
3165 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003166 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003167 compl_curr_match = compl_curr_match->cp_next;
3168 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003169 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003170 if (match->cp_flags & FREE_FNAME)
3171 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003172 for (i = 0; i < CPT_COUNT; ++i)
3173 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003174 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003175 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3176 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003177}
3178
3179 static void
3180ins_compl_clear()
3181{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003182 compl_cont_status = 0;
3183 compl_started = FALSE;
3184 compl_matches = 0;
3185 vim_free(compl_pattern);
3186 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003187 vim_free(compl_leader);
3188 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003190 vim_free(compl_orig_text);
3191 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003192 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003193}
3194
3195/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003196 * Return TRUE when Insert completion is active.
3197 */
3198 int
3199ins_compl_active()
3200{
3201 return compl_started;
3202}
3203
3204/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003205 * Delete one character before the cursor and show the subset of the matches
3206 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003207 * Returns the character to be used, NUL if the work is done and another char
3208 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003209 */
3210 static int
3211ins_compl_bs()
3212{
3213 char_u *line;
3214 char_u *p;
3215
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003216 line = ml_get_curline();
3217 p = line + curwin->w_cursor.col;
3218 mb_ptr_back(line, p);
3219
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003220 /* Stop completion when the whole word was deleted. For Omni completion
3221 * allow the word to be deleted, we won't match everything. */
3222 if ((int)(p - line) - (int)compl_col < 0
3223 || ((int)(p - line) - (int)compl_col == 0
3224 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003225 return K_BS;
3226
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003227 /* Deleted more than what was used to find matches or didn't finish
3228 * finding all matches: need to look for matches all over again. */
3229 if (curwin->w_cursor.col <= compl_col + compl_length
3230 || compl_was_interrupted)
3231 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003232
Bram Moolenaara6557602006-02-04 22:43:20 +00003233 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003234 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003235 if (compl_leader != NULL)
3236 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003237 ins_compl_new_leader();
3238 return NUL;
3239 }
3240 return K_BS;
3241}
Bram Moolenaara6557602006-02-04 22:43:20 +00003242
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003243/*
3244 * Called after changing "compl_leader".
3245 * Show the popup menu with a different set of matches.
3246 * May also search for matches again if the previous search was interrupted.
3247 */
3248 static void
3249ins_compl_new_leader()
3250{
3251 ins_compl_del_pum();
3252 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003253 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003254 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003255
3256 if (compl_started)
3257 ins_compl_set_original_text(compl_leader);
3258 else
3259 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003260#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003261 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003262#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003263 /*
3264 * Matches were cleared, need to search for them now. First display
3265 * the changed text before the cursor. Set "compl_restarting" to
3266 * avoid that the first match is inserted.
3267 */
3268 update_screen(0);
3269#ifdef FEAT_GUI
3270 if (gui.in_use)
3271 {
3272 /* Show the cursor after the match, not after the redrawn text. */
3273 setcursor();
3274 out_flush();
3275 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003276 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003277#endif
3278 compl_restarting = TRUE;
3279 if (ins_complete(Ctrl_N) == FAIL)
3280 compl_cont_status = 0;
3281 compl_restarting = FALSE;
3282 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003283
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003284#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003285 if (!compl_used_match)
3286 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003287 /* Go to the original text, since none of the matches is inserted. */
3288 if (compl_first_match->cp_prev != NULL
3289 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3290 compl_shown_match = compl_first_match->cp_prev;
3291 else
3292 compl_shown_match = compl_first_match;
3293 compl_curr_match = compl_shown_match;
3294 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003295 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003296#endif
3297 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003298
3299 /* Show the popup menu with a different set of matches. */
3300 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003301
3302 /* Don't let Enter select the original text when there is no popup menu. */
3303 if (compl_match_array == NULL)
3304 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003305}
3306
3307/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003308 * Return the length of the completion, from the completion start column to
3309 * the cursor column. Making sure it never goes below zero.
3310 */
3311 static int
3312ins_compl_len()
3313{
3314 int off = curwin->w_cursor.col - compl_col;
3315
3316 if (off < 0)
3317 return 0;
3318 return off;
3319}
3320
3321/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003322 * Append one character to the match leader. May reduce the number of
3323 * matches.
3324 */
3325 static void
3326ins_compl_addleader(c)
3327 int c;
3328{
3329#ifdef FEAT_MBYTE
3330 int cc;
3331
3332 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3333 {
3334 char_u buf[MB_MAXBYTES + 1];
3335
3336 (*mb_char2bytes)(c, buf);
3337 buf[cc] = NUL;
3338 ins_char_bytes(buf, cc);
3339 }
3340 else
3341#endif
3342 ins_char(c);
3343
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003344 /* If we didn't complete finding matches we must search again. */
3345 if (compl_was_interrupted)
3346 ins_compl_restart();
3347
Bram Moolenaara6557602006-02-04 22:43:20 +00003348 vim_free(compl_leader);
3349 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
3350 curwin->w_cursor.col - compl_col);
3351 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003352 ins_compl_new_leader();
3353}
3354
3355/*
3356 * Setup for finding completions again without leaving CTRL-X mode. Used when
3357 * BS or a key was typed while still searching for matches.
3358 */
3359 static void
3360ins_compl_restart()
3361{
3362 ins_compl_free();
3363 compl_started = FALSE;
3364 compl_matches = 0;
3365 compl_cont_status = 0;
3366 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003367}
3368
3369/*
3370 * Set the first match, the original text.
3371 */
3372 static void
3373ins_compl_set_original_text(str)
3374 char_u *str;
3375{
3376 char_u *p;
3377
3378 /* Replace the original text entry. */
3379 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3380 {
3381 p = vim_strsave(str);
3382 if (p != NULL)
3383 {
3384 vim_free(compl_first_match->cp_str);
3385 compl_first_match->cp_str = p;
3386 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003387 }
3388}
3389
3390/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003391 * Append one character to the match leader. May reduce the number of
3392 * matches.
3393 */
3394 static void
3395ins_compl_addfrommatch()
3396{
3397 char_u *p;
3398 int len = curwin->w_cursor.col - compl_col;
3399 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003400 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003401
3402 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003403 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003404 {
3405 /* When still at the original match use the first entry that matches
3406 * the leader. */
3407 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3408 {
3409 p = NULL;
3410 for (cp = compl_shown_match->cp_next; cp != NULL
3411 && cp != compl_first_match; cp = cp->cp_next)
3412 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003413 if (compl_leader == NULL
3414 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003415 (int)STRLEN(compl_leader)))
3416 {
3417 p = cp->cp_str;
3418 break;
3419 }
3420 }
3421 if (p == NULL || (int)STRLEN(p) <= len)
3422 return;
3423 }
3424 else
3425 return;
3426 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003427 p += len;
3428#ifdef FEAT_MBYTE
3429 c = mb_ptr2char(p);
3430#else
3431 c = *p;
3432#endif
3433 ins_compl_addleader(c);
3434}
3435
3436/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003437 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003438 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003439 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003441 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442ins_compl_prep(c)
3443 int c;
3444{
3445 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003446 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003447 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448
3449 /* Forget any previous 'special' messages if this is actually
3450 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3451 */
3452 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3453 edit_submode_extra = NULL;
3454
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003455 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3456 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003457 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003459 /* Set "compl_get_longest" when finding the first matches. */
3460 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3461 || (ctrl_x_mode == 0 && !compl_started))
3462 {
3463 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3464 compl_used_match = TRUE;
3465 }
3466
Bram Moolenaar071d4272004-06-13 20:20:40 +00003467 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3468 {
3469 /*
3470 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3471 * it will be yet. Now we decide.
3472 */
3473 switch (c)
3474 {
3475 case Ctrl_E:
3476 case Ctrl_Y:
3477 ctrl_x_mode = CTRL_X_SCROLL;
3478 if (!(State & REPLACE_FLAG))
3479 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3480 else
3481 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3482 edit_submode_pre = NULL;
3483 showmode();
3484 break;
3485 case Ctrl_L:
3486 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3487 break;
3488 case Ctrl_F:
3489 ctrl_x_mode = CTRL_X_FILES;
3490 break;
3491 case Ctrl_K:
3492 ctrl_x_mode = CTRL_X_DICTIONARY;
3493 break;
3494 case Ctrl_R:
3495 /* Simply allow ^R to happen without affecting ^X mode */
3496 break;
3497 case Ctrl_T:
3498 ctrl_x_mode = CTRL_X_THESAURUS;
3499 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003500#ifdef FEAT_COMPL_FUNC
3501 case Ctrl_U:
3502 ctrl_x_mode = CTRL_X_FUNCTION;
3503 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003504 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003505 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003506 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003507#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003508 case 's':
3509 case Ctrl_S:
3510 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003511#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003512 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003513 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003514 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003515#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003516 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 case Ctrl_RSB:
3518 ctrl_x_mode = CTRL_X_TAGS;
3519 break;
3520#ifdef FEAT_FIND_ID
3521 case Ctrl_I:
3522 case K_S_TAB:
3523 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3524 break;
3525 case Ctrl_D:
3526 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3527 break;
3528#endif
3529 case Ctrl_V:
3530 case Ctrl_Q:
3531 ctrl_x_mode = CTRL_X_CMDLINE;
3532 break;
3533 case Ctrl_P:
3534 case Ctrl_N:
3535 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3536 * just started ^X mode, or there were enough ^X's to cancel
3537 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3538 * do normal expansion when interrupting a different mode (say
3539 * ^X^F^X^P or ^P^X^X^P, see below)
3540 * nothing changes if interrupting mode 0, (eg, the flag
3541 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003542 if (!(compl_cont_status & CONT_INTRPT))
3543 compl_cont_status |= CONT_LOCAL;
3544 else if (compl_cont_mode != 0)
3545 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 /* FALLTHROUGH */
3547 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003548 /* If we have typed at least 2 ^X's... for modes != 0, we set
3549 * compl_cont_status = 0 (eg, as if we had just started ^X
3550 * mode).
3551 * For mode 0, we set "compl_cont_mode" to an impossible
3552 * value, in both cases ^X^X can be used to restart the same
3553 * mode (avoiding ADDING mode).
3554 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3555 * 'complete' and local ^P expansions respectively.
3556 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3557 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (c == Ctrl_X)
3559 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003560 if (compl_cont_mode != 0)
3561 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003562 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003563 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 }
3565 ctrl_x_mode = 0;
3566 edit_submode = NULL;
3567 showmode();
3568 break;
3569 }
3570 }
3571 else if (ctrl_x_mode != 0)
3572 {
3573 /* We're already in CTRL-X mode, do we stay in it? */
3574 if (!vim_is_ctrl_x_key(c))
3575 {
3576 if (ctrl_x_mode == CTRL_X_SCROLL)
3577 ctrl_x_mode = 0;
3578 else
3579 ctrl_x_mode = CTRL_X_FINISHED;
3580 edit_submode = NULL;
3581 }
3582 showmode();
3583 }
3584
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003585 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586 {
3587 /* Show error message from attempted keyword completion (probably
3588 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003589 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003590 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003591 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3592 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003593 || ctrl_x_mode == CTRL_X_FINISHED)
3594 {
3595 /* Get here when we have finished typing a sequence of ^N and
3596 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003597 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003598 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003600 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003601 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003602
Bram Moolenaar071d4272004-06-13 20:20:40 +00003603 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003604 * If any of the original typed text has been changed, eg when
3605 * ignorecase is set, we must add back-spaces to the redo
3606 * buffer. We add as few as necessary to delete just the part
3607 * of the original text that has changed.
3608 * When using the longest match, edited the match or used
3609 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003610 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003611 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3612 ptr = compl_curr_match->cp_str;
3613 else if (compl_leader != NULL)
3614 ptr = compl_leader;
3615 else
3616 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003617 if (compl_orig_text != NULL)
3618 {
3619 p = compl_orig_text;
3620 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3621 ++temp)
3622 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003623#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003624 if (temp > 0)
3625 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003626#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003627 for (p += temp; *p != NUL; mb_ptr_adv(p))
3628 AppendCharToRedobuff(K_BS);
3629 }
3630 if (ptr != NULL)
3631 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003632 }
3633
3634#ifdef FEAT_CINDENT
3635 want_cindent = (can_cindent && cindent_on());
3636#endif
3637 /*
3638 * When completing whole lines: fix indent for 'cindent'.
3639 * Otherwise, break line if it's too long.
3640 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003641 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 {
3643#ifdef FEAT_CINDENT
3644 /* re-indent the current line */
3645 if (want_cindent)
3646 {
3647 do_c_expr_indent();
3648 want_cindent = FALSE; /* don't do it again */
3649 }
3650#endif
3651 }
3652 else
3653 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003654 int prev_col = curwin->w_cursor.col;
3655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003657 if (prev_col > 0)
3658 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003659 if (stop_arrow() == OK)
3660 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003661 if (prev_col > 0
3662 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3663 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 }
3665
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003666 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003667 * the selection without inserting anything. When
3668 * compl_enter_selects is set the Enter key does the same. */
3669 if ((c == Ctrl_Y || (compl_enter_selects
3670 && (c == CAR || c == K_KENTER || c == NL)))
3671 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003672 retval = TRUE;
3673
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003674 /* CTRL-E means completion is Ended, go back to the typed text. */
3675 if (c == Ctrl_E)
3676 {
3677 ins_compl_delete();
3678 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003679 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003680 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003681 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003682 retval = TRUE;
3683 }
3684
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003685 auto_format(FALSE, TRUE);
3686
Bram Moolenaar071d4272004-06-13 20:20:40 +00003687 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003688 compl_started = FALSE;
3689 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003690 msg_clr_cmdline(); /* necessary for "noshowmode" */
3691 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003692 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 if (edit_submode != NULL)
3694 {
3695 edit_submode = NULL;
3696 showmode();
3697 }
3698
3699#ifdef FEAT_CINDENT
3700 /*
3701 * Indent now if a key was typed that is in 'cinkeys'.
3702 */
3703 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3704 do_c_expr_indent();
3705#endif
3706 }
3707 }
3708
3709 /* reset continue_* if we left expansion-mode, if we stay they'll be
3710 * (re)set properly in ins_complete() */
3711 if (!vim_is_ctrl_x_key(c))
3712 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003713 compl_cont_status = 0;
3714 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003715 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003716
3717 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003718}
3719
3720/*
3721 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3722 * (depending on flag) starting from buf and looking for a non-scanned
3723 * buffer (other than curbuf). curbuf is special, if it is called with
3724 * buf=curbuf then it has to be the first call for a given flag/expansion.
3725 *
3726 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3727 */
3728 static buf_T *
3729ins_compl_next_buf(buf, flag)
3730 buf_T *buf;
3731 int flag;
3732{
3733#ifdef FEAT_WINDOWS
3734 static win_T *wp;
3735#endif
3736
3737 if (flag == 'w') /* just windows */
3738 {
3739#ifdef FEAT_WINDOWS
3740 if (buf == curbuf) /* first call for this flag/expansion */
3741 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003742 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 && wp->w_buffer->b_scanned)
3744 ;
3745 buf = wp->w_buffer;
3746#else
3747 buf = curbuf;
3748#endif
3749 }
3750 else
3751 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3752 * (unlisted buffers)
3753 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003754 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003755 && ((flag == 'U'
3756 ? buf->b_p_bl
3757 : (!buf->b_p_bl
3758 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003759 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003760 ;
3761 return buf;
3762}
3763
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003764#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003765static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003766
3767/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003768 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003769 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003770 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003771 static void
3772expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003773 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003774 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003775{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003776 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003777 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003778 char_u *funcname;
3779 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003780
Bram Moolenaare344bea2005-09-01 20:46:49 +00003781 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3782 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003783 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003784
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003785 /* Call 'completefunc' to obtain the list of matches. */
3786 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003787 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003788
Bram Moolenaare344bea2005-09-01 20:46:49 +00003789 pos = curwin->w_cursor;
3790 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3791 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003792 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003793 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003794
Bram Moolenaara94bc432006-03-10 21:42:59 +00003795 ins_compl_add_list(matchlist);
3796 list_unref(matchlist);
3797}
3798#endif /* FEAT_COMPL_FUNC */
3799
Bram Moolenaar39f05632006-03-19 22:15:26 +00003800#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003801/*
3802 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003803 */
3804 static void
3805ins_compl_add_list(list)
3806 list_T *list;
3807{
3808 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003809 int dir = compl_direction;
3810
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003811 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003812 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003813 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003814 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3815 /* if dir was BACKWARD then honor it just once */
3816 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003817 else if (did_emsg)
3818 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003819 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003820}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003821
3822/*
3823 * Add a match to the list of matches from a typeval_T.
3824 * If the given string is already in the list of completions, then return
3825 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3826 * maybe because alloc() returns NULL, then FAIL is returned.
3827 */
3828 int
3829ins_compl_add_tv(tv, dir)
3830 typval_T *tv;
3831 int dir;
3832{
3833 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003834 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003835 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003836 char_u *(cptext[CPT_COUNT]);
3837
3838 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3839 {
3840 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3841 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3842 (char_u *)"abbr", FALSE);
3843 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3844 (char_u *)"menu", FALSE);
3845 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3846 (char_u *)"kind", FALSE);
3847 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3848 (char_u *)"info", FALSE);
3849 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3850 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003851 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003852 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003853 }
3854 else
3855 {
3856 word = get_tv_string_chk(tv);
3857 vim_memset(cptext, 0, sizeof(cptext));
3858 }
3859 if (word == NULL || *word == NUL)
3860 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003861 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003862}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003863#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003864
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003865/*
3866 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003867 * The search starts at position "ini" in curbuf and in the direction
3868 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003869 * When "compl_started" is FALSE start at that position, otherwise continue
3870 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003871 * This may return before finding all the matches.
3872 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003873 */
3874 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003875ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003876 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003877{
3878 static pos_T first_match_pos;
3879 static pos_T last_match_pos;
3880 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003881 static int found_all = FALSE; /* Found all matches of a
3882 certain type. */
3883 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003884
Bram Moolenaar572cb562005-08-05 21:35:02 +00003885 pos_T *pos;
3886 char_u **matches;
3887 int save_p_scs;
3888 int save_p_ws;
3889 int save_p_ic;
3890 int i;
3891 int num_matches;
3892 int len;
3893 int found_new_match;
3894 int type = ctrl_x_mode;
3895 char_u *ptr;
3896 char_u *dict = NULL;
3897 int dict_f = 0;
3898 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003899
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003900 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003901 {
3902 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3903 ins_buf->b_scanned = 0;
3904 found_all = FALSE;
3905 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003906 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003907 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003908 last_match_pos = first_match_pos = *ini;
3909 }
3910
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003911 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003912 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003913 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3914 for (;;)
3915 {
3916 found_new_match = FAIL;
3917
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003918 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003919 * or if found_all says this entry is done. For ^X^L only use the
3920 * entries from 'complete' that look in loaded buffers. */
3921 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003922 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003923 {
3924 found_all = FALSE;
3925 while (*e_cpt == ',' || *e_cpt == ' ')
3926 e_cpt++;
3927 if (*e_cpt == '.' && !curbuf->b_scanned)
3928 {
3929 ins_buf = curbuf;
3930 first_match_pos = *ini;
3931 /* So that ^N can match word immediately after cursor */
3932 if (ctrl_x_mode == 0)
3933 dec(&first_match_pos);
3934 last_match_pos = first_match_pos;
3935 type = 0;
3936 }
3937 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3938 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3939 {
3940 /* Scan a buffer, but not the current one. */
3941 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3942 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003943 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003944 first_match_pos.col = last_match_pos.col = 0;
3945 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3946 last_match_pos.lnum = 0;
3947 type = 0;
3948 }
3949 else /* unloaded buffer, scan like dictionary */
3950 {
3951 found_all = TRUE;
3952 if (ins_buf->b_fname == NULL)
3953 continue;
3954 type = CTRL_X_DICTIONARY;
3955 dict = ins_buf->b_fname;
3956 dict_f = DICT_EXACT;
3957 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00003958 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959 ins_buf->b_fname == NULL
3960 ? buf_spname(ins_buf)
3961 : ins_buf->b_sfname == NULL
3962 ? (char *)ins_buf->b_fname
3963 : (char *)ins_buf->b_sfname);
3964 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3965 }
3966 else if (*e_cpt == NUL)
3967 break;
3968 else
3969 {
3970 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3971 type = -1;
3972 else if (*e_cpt == 'k' || *e_cpt == 's')
3973 {
3974 if (*e_cpt == 'k')
3975 type = CTRL_X_DICTIONARY;
3976 else
3977 type = CTRL_X_THESAURUS;
3978 if (*++e_cpt != ',' && *e_cpt != NUL)
3979 {
3980 dict = e_cpt;
3981 dict_f = DICT_FIRST;
3982 }
3983 }
3984#ifdef FEAT_FIND_ID
3985 else if (*e_cpt == 'i')
3986 type = CTRL_X_PATH_PATTERNS;
3987 else if (*e_cpt == 'd')
3988 type = CTRL_X_PATH_DEFINES;
3989#endif
3990 else if (*e_cpt == ']' || *e_cpt == 't')
3991 {
3992 type = CTRL_X_TAGS;
3993 sprintf((char*)IObuff, _("Scanning tags."));
3994 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
3995 }
3996 else
3997 type = -1;
3998
3999 /* in any case e_cpt is advanced to the next entry */
4000 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4001
4002 found_all = TRUE;
4003 if (type == -1)
4004 continue;
4005 }
4006 }
4007
4008 switch (type)
4009 {
4010 case -1:
4011 break;
4012#ifdef FEAT_FIND_ID
4013 case CTRL_X_PATH_PATTERNS:
4014 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004015 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004016 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004018 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004019 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4020 (linenr_T)1, (linenr_T)MAXLNUM);
4021 break;
4022#endif
4023
4024 case CTRL_X_DICTIONARY:
4025 case CTRL_X_THESAURUS:
4026 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004027 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 : (type == CTRL_X_THESAURUS
4029 ? (*curbuf->b_p_tsr == NUL
4030 ? p_tsr
4031 : curbuf->b_p_tsr)
4032 : (*curbuf->b_p_dict == NUL
4033 ? p_dict
4034 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004035 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004036 dict != NULL ? dict_f
4037 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 dict = NULL;
4039 break;
4040
4041 case CTRL_X_TAGS:
4042 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4043 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004044 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045
4046 /* Find up to TAG_MANY matches. Avoids that an enourmous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004047 * of matches is found when compl_pattern is empty */
4048 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4050 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4051 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4052 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004053 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004054 }
4055 p_ic = save_p_ic;
4056 break;
4057
4058 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004059 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4061 {
4062
4063 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004064 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004065 ins_compl_add_matches(num_matches, matches,
4066#ifdef CASE_INSENSITIVE_FILENAME
4067 TRUE
4068#else
4069 FALSE
4070#endif
4071 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 }
4073 break;
4074
4075 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004076 if (expand_cmdline(&compl_xp, compl_pattern,
4077 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004079 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004080 break;
4081
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004082#ifdef FEAT_COMPL_FUNC
4083 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004084 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004085 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004086 break;
4087#endif
4088
Bram Moolenaar488c6512005-08-11 20:09:58 +00004089 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004090#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004091 num_matches = expand_spelling(first_match_pos.lnum,
4092 first_match_pos.col, compl_pattern, &matches);
4093 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004094 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004095#endif
4096 break;
4097
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098 default: /* normal ^P/^N and ^X^L */
4099 /*
4100 * If 'infercase' is set, don't use 'smartcase' here
4101 */
4102 save_p_scs = p_scs;
4103 if (ins_buf->b_p_inf)
4104 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004105
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106 /* buffers other than curbuf are scanned from the beginning or the
4107 * end but never from the middle, thus setting nowrapscan in this
4108 * buffers is a good idea, on the other hand, we always set
4109 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4110 save_p_ws = p_ws;
4111 if (ins_buf != curbuf)
4112 p_ws = FALSE;
4113 else if (*e_cpt == '.')
4114 p_ws = TRUE;
4115 for (;;)
4116 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004117 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004119 ++msg_silent; /* Don't want messages for wrapscan. */
4120
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004121 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4122 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004126 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004128 found_new_match = searchit(NULL, ins_buf, pos,
4129 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004130 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004131 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004132 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004133 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004135 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004136 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 first_match_pos = *pos;
4138 last_match_pos = *pos;
4139 }
4140 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004141 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004142 found_new_match = FAIL;
4143 if (found_new_match == FAIL)
4144 {
4145 if (ins_buf == curbuf)
4146 found_all = TRUE;
4147 break;
4148 }
4149
4150 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004151 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 && ini->lnum == pos->lnum
4153 && ini->col == pos->col)
4154 continue;
4155 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4156 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4157 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004158 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 {
4160 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4161 continue;
4162 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4163 if (!p_paste)
4164 ptr = skipwhite(ptr);
4165 }
4166 len = (int)STRLEN(ptr);
4167 }
4168 else
4169 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004170 char_u *tmp_ptr = ptr;
4171
4172 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004174 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175 /* Skip if already inside a word. */
4176 if (vim_iswordp(tmp_ptr))
4177 continue;
4178 /* Find start of next word. */
4179 tmp_ptr = find_word_start(tmp_ptr);
4180 }
4181 /* Find end of this word. */
4182 tmp_ptr = find_word_end(tmp_ptr);
4183 len = (int)(tmp_ptr - ptr);
4184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185 if ((compl_cont_status & CONT_ADDING)
4186 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
4188 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4189 {
4190 /* Try next line, if any. the new word will be
4191 * "join" as if the normal command "J" was used.
4192 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004193 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004194 * works -- Acevedo */
4195 STRNCPY(IObuff, ptr, len);
4196 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4197 tmp_ptr = ptr = skipwhite(ptr);
4198 /* Find start of next word. */
4199 tmp_ptr = find_word_start(tmp_ptr);
4200 /* Find end of next word. */
4201 tmp_ptr = find_word_end(tmp_ptr);
4202 if (tmp_ptr > ptr)
4203 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004204 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004206 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 IObuff[len++] = ' ';
4208 /* IObuf =~ "\k.* ", thus len >= 2 */
4209 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004210 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004211 || (vim_strchr(p_cpo, CPO_JOINSP)
4212 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004213 && (IObuff[len - 2] == '?'
4214 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 IObuff[len++] = ' ';
4216 }
4217 /* copy as much as posible of the new word */
4218 if (tmp_ptr - ptr >= IOSIZE - len)
4219 tmp_ptr = ptr + IOSIZE - len - 1;
4220 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4221 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004222 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224 IObuff[len] = NUL;
4225 ptr = IObuff;
4226 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004227 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 continue;
4229 }
4230 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004231 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004232 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004233 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 {
4235 found_new_match = OK;
4236 break;
4237 }
4238 }
4239 p_scs = save_p_scs;
4240 p_ws = save_p_ws;
4241 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004242
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004243 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004244 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004245 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004246 found_new_match = OK;
4247
4248 /* break the loop for specialized modes (use 'complete' just for the
4249 * generic ctrl_x_mode == 0) or when we've found a new match */
4250 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004251 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004252 {
4253 if (got_int)
4254 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004255 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004256 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004257 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004258
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004259 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4260 || compl_interrupted)
4261 break;
4262 compl_started = TRUE;
4263 }
4264 else
4265 {
4266 /* Mark a buffer scanned when it has been scanned completely */
4267 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4268 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004270 compl_started = FALSE;
4271 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004272 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004273 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274
4275 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4276 && *e_cpt == NUL) /* Got to end of 'complete' */
4277 found_new_match = FAIL;
4278
4279 i = -1; /* total of matches, unknown */
4280 if (found_new_match == FAIL
4281 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4282 i = ins_compl_make_cyclic();
4283
4284 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004285 * just been made cyclic then we have to move compl_curr_match to the next
4286 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004287 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4288 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004289 if (compl_curr_match == NULL)
4290 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 return i;
4292}
4293
4294/* Delete the old text being completed. */
4295 static void
4296ins_compl_delete()
4297{
4298 int i;
4299
4300 /*
4301 * In insert mode: Delete the typed part.
4302 * In replace mode: Put the old characters back, if any.
4303 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004304 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 backspace_until_column(i);
4306 changed_cline_bef_curs();
4307}
4308
4309/* Insert the new text being completed. */
4310 static void
4311ins_compl_insert()
4312{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004313 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004314 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4315 compl_used_match = FALSE;
4316 else
4317 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318}
4319
4320/*
4321 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004322 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4323 * get more completions. If it is FALSE, then we just do nothing when there
4324 * are no more completions in a given direction. The latter case is used when
4325 * we are still in the middle of finding completions, to allow browsing
4326 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327 * Return the total number of matches, or -1 if still unknown -- webb.
4328 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004329 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4330 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 *
4332 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004333 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4334 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 */
4336 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004337ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004338 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004339 int count; /* repeat completion this many times; should
4340 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004341 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342{
4343 int num_matches = -1;
4344 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004345 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004346 compl_T *found_compl = NULL;
4347 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004348 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004350 if (compl_leader != NULL
4351 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004353 /* Set "compl_shown_match" to the actually shown match, it may differ
4354 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004355 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004356 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004357 && compl_shown_match->cp_next != NULL
4358 && compl_shown_match->cp_next != compl_first_match)
4359 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004360
4361 /* If we didn't find it searching forward, and compl_shows_dir is
4362 * backward, find the last match. */
4363 if (compl_shows_dir == BACKWARD
4364 && !ins_compl_equal(compl_shown_match,
4365 compl_leader, (int)STRLEN(compl_leader))
4366 && (compl_shown_match->cp_next == NULL
4367 || compl_shown_match->cp_next == compl_first_match))
4368 {
4369 while (!ins_compl_equal(compl_shown_match,
4370 compl_leader, (int)STRLEN(compl_leader))
4371 && compl_shown_match->cp_prev != NULL
4372 && compl_shown_match->cp_prev != compl_first_match)
4373 compl_shown_match = compl_shown_match->cp_prev;
4374 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004375 }
4376
4377 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004378 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 /* Delete old text to be replaced */
4380 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004381
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004382 /* When finding the longest common text we stick at the original text,
4383 * don't let CTRL-N or CTRL-P move to the first match. */
4384 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4385
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004386 /* When restarting the search don't insert the first match either. */
4387 if (compl_restarting)
4388 {
4389 advance = FALSE;
4390 compl_restarting = FALSE;
4391 }
4392
Bram Moolenaare3226be2005-12-18 22:10:00 +00004393 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4394 * around. */
4395 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004397 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004399 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004400 found_end = (compl_first_match != NULL
4401 && (compl_shown_match->cp_next == compl_first_match
4402 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004403 }
4404 else if (compl_shows_dir == BACKWARD
4405 && compl_shown_match->cp_prev != NULL)
4406 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004407 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004408 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004409 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 }
4411 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004412 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004413 if (!allow_get_expansion)
4414 {
4415 if (advance)
4416 {
4417 if (compl_shows_dir == BACKWARD)
4418 compl_pending -= todo + 1;
4419 else
4420 compl_pending += todo + 1;
4421 }
4422 return -1;
4423 }
4424
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004425 if (advance)
4426 {
4427 if (compl_shows_dir == BACKWARD)
4428 --compl_pending;
4429 else
4430 ++compl_pending;
4431 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004432
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004433 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004434 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004435
4436 /* handle any pending completions */
4437 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004438 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004439 {
4440 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4441 {
4442 compl_shown_match = compl_shown_match->cp_next;
4443 --compl_pending;
4444 }
4445 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4446 {
4447 compl_shown_match = compl_shown_match->cp_prev;
4448 ++compl_pending;
4449 }
4450 else
4451 break;
4452 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004453 found_end = FALSE;
4454 }
4455 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4456 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004457 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004458 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004459 ++todo;
4460 else
4461 /* Remember a matching item. */
4462 found_compl = compl_shown_match;
4463
4464 /* Stop at the end of the list when we found a usable match. */
4465 if (found_end)
4466 {
4467 if (found_compl != NULL)
4468 {
4469 compl_shown_match = found_compl;
4470 break;
4471 }
4472 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004473 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004474 }
4475
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004476 /* Insert the text of the new completion, or the compl_leader. */
4477 if (insert_match)
4478 {
4479 if (!compl_get_longest || compl_used_match)
4480 ins_compl_insert();
4481 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004482 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004483 }
4484 else
4485 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004486
4487 if (!allow_get_expansion)
4488 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004489 /* may undisplay the popup menu first */
4490 ins_compl_upd_pum();
4491
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004492 /* redraw to show the user what was inserted */
4493 update_screen(0);
4494
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004495 /* display the updated popup menu */
4496 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004497#ifdef FEAT_GUI
4498 if (gui.in_use)
4499 {
4500 /* Show the cursor after the match, not after the redrawn text. */
4501 setcursor();
4502 out_flush();
4503 gui_update_cursor(FALSE, FALSE);
4504 }
4505#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004506
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 /* Delete old text to be replaced, since we're still searching and
4508 * don't want to match ourselves! */
4509 ins_compl_delete();
4510 }
4511
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004512 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004513 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004514 compl_enter_selects = !insert_match && compl_match_array != NULL;
4515
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 /*
4517 * Show the file name for the match (if any)
4518 * Truncate the file name to avoid a wait for return.
4519 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004520 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521 {
4522 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004523 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004524 if (i <= 0)
4525 i = 0;
4526 else
4527 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004528 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004529 msg(IObuff);
4530 redraw_cmdline = FALSE; /* don't overwrite! */
4531 }
4532
4533 return num_matches;
4534}
4535
4536/*
4537 * Call this while finding completions, to check whether the user has hit a key
4538 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004539 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004540 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004541 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004542 */
4543 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004544ins_compl_check_keys(frequency)
4545 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546{
4547 static int count = 0;
4548
4549 int c;
4550
4551 /* Don't check when reading keys from a script. That would break the test
4552 * scripts */
4553 if (using_script())
4554 return;
4555
4556 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004557 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558 return;
4559 count = 0;
4560
Bram Moolenaara260a972006-06-23 19:36:29 +00004561 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4562 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004563 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004564 if (c != NUL)
4565 {
4566 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4567 {
4568 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004569 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004570 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4571 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004572 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004573 else
4574 {
4575 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004576 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004577 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004578 if (c != K_IGNORE)
4579 {
4580 /* Don't interrupt completion when the character wasn't typed,
4581 * e.g., when doing @q to replay keys. */
4582 if (c != Ctrl_R && KeyTyped)
4583 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004584
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004585 vungetc(c);
4586 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004587 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004589 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004590 {
4591 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4592
4593 compl_pending = 0;
4594 (void)ins_compl_next(FALSE, todo, TRUE);
4595 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004596}
4597
4598/*
4599 * Decide the direction of Insert mode complete from the key typed.
4600 * Returns BACKWARD or FORWARD.
4601 */
4602 static int
4603ins_compl_key2dir(c)
4604 int c;
4605{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004606 if (c == Ctrl_P || c == Ctrl_L
4607 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4608 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004609 return BACKWARD;
4610 return FORWARD;
4611}
4612
4613/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004614 * Return TRUE for keys that are used for completion only when the popup menu
4615 * is visible.
4616 */
4617 static int
4618ins_compl_pum_key(c)
4619 int c;
4620{
4621 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004622 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4623 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004624}
4625
4626/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004627 * Decide the number of completions to move forward.
4628 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4629 */
4630 static int
4631ins_compl_key2count(c)
4632 int c;
4633{
4634 int h;
4635
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004636 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004637 {
4638 h = pum_get_height();
4639 if (h > 3)
4640 h -= 2; /* keep some context */
4641 return h;
4642 }
4643 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644}
4645
4646/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004647 * Return TRUE if completion with "c" should insert the match, FALSE if only
4648 * to change the currently selected completion.
4649 */
4650 static int
4651ins_compl_use_match(c)
4652 int c;
4653{
4654 switch (c)
4655 {
4656 case K_UP:
4657 case K_DOWN:
4658 case K_PAGEDOWN:
4659 case K_KPAGEDOWN:
4660 case K_S_DOWN:
4661 case K_PAGEUP:
4662 case K_KPAGEUP:
4663 case K_S_UP:
4664 return FALSE;
4665 }
4666 return TRUE;
4667}
4668
4669/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 * Do Insert mode completion.
4671 * Called when character "c" was typed, which has a meaning for completion.
4672 * Returns OK if completion was done, FAIL if something failed (out of mem).
4673 */
4674 static int
4675ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004676 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004677{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004678 char_u *line;
4679 int startcol = 0; /* column where searched text starts */
4680 colnr_T curs_col; /* cursor column */
4681 int n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682
Bram Moolenaare3226be2005-12-18 22:10:00 +00004683 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004684 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004685 {
4686 /* First time we hit ^N or ^P (in a row, I mean) */
4687
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 did_ai = FALSE;
4689#ifdef FEAT_SMARTINDENT
4690 did_si = FALSE;
4691 can_si = FALSE;
4692 can_si_back = FALSE;
4693#endif
4694 if (stop_arrow() == FAIL)
4695 return FAIL;
4696
4697 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004698 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004699 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004701 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004702 * "compl_startpos" to the cursor as a pattern to add a new word
4703 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004704 * "compl_startpos" is not in the same line as the cursor then fix it
4705 * (the line has been split because it was longer than 'tw'). if SOL
4706 * is set then skip the previous pattern, a word at the beginning of
4707 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004708 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4709 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710 {
4711 /*
4712 * it is a continued search
4713 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004714 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4716 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4717 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004718 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004720 /* line (probably) wrapped, set compl_startpos to the
4721 * first non_blank in the line, if it is not a wordchar
4722 * include it to get a better pattern, but then we don't
4723 * want the "\\<" prefix, check it bellow */
4724 compl_col = (colnr_T)(skipwhite(line) - line);
4725 compl_startpos.col = compl_col;
4726 compl_startpos.lnum = curwin->w_cursor.lnum;
4727 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004728 }
4729 else
4730 {
4731 /* S_IPOS was set when we inserted a word that was at the
4732 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004733 * mode but first we need to redefine compl_startpos */
4734 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004735 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004736 compl_cont_status |= CONT_SOL;
4737 compl_startpos.col = (colnr_T)(skipwhite(
4738 line + compl_length
4739 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004740 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004741 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004743 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004744 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004745 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004747 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004749 compl_cont_status &= ~CONT_SOL;
4750 compl_length = (IOSIZE - MIN_SPACE);
4751 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004752 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004753 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4754 if (compl_length < 1)
4755 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004756 }
4757 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004758 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004760 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 }
4762 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004763 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004765 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004767 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004769 compl_cont_status = 0;
4770 compl_cont_status |= CONT_N_ADDS;
4771 compl_startpos = curwin->w_cursor;
4772 startcol = (int)curs_col;
4773 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004774 }
4775
4776 /* Work out completion pattern and original text -- webb */
4777 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4778 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004779 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004780 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4781 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004782 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004784 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004785 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004786 compl_col += ++startcol;
4787 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 }
4789 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004790 compl_pattern = str_foldcase(line + compl_col,
4791 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004793 compl_pattern = vim_strnsave(line + compl_col,
4794 compl_length);
4795 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 return FAIL;
4797 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004798 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
4800 char_u *prefix = (char_u *)"\\<";
4801
4802 /* we need 3 extra chars, 1 for the NUL and
4803 * 2 >= strlen(prefix) -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004804 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4805 compl_length) + 3);
4806 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004808 if (!vim_iswordp(line + compl_col)
4809 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 && (
4811#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004812 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004814 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815#endif
4816 )))
4817 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004818 STRCPY((char *)compl_pattern, prefix);
4819 (void)quote_meta(compl_pattern + STRLEN(prefix),
4820 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004822 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004824 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004826 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827#endif
4828 )
4829 {
4830 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004831 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4832 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004834 compl_col += curs_col;
4835 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004836 }
4837 else
4838 {
4839#ifdef FEAT_MBYTE
4840 /* Search the point of change class of multibyte character
4841 * or not a word single byte character backward. */
4842 if (has_mbyte)
4843 {
4844 int base_class;
4845 int head_off;
4846
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004847 startcol -= (*mb_head_off)(line, line + startcol);
4848 base_class = mb_get_class(line + startcol);
4849 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004851 head_off = (*mb_head_off)(line, line + startcol);
4852 if (base_class != mb_get_class(line + startcol
4853 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004854 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004855 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 }
4857 }
4858 else
4859#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004860 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004862 compl_col += ++startcol;
4863 compl_length = (int)curs_col - startcol;
4864 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 {
4866 /* Only match word with at least two chars -- webb
4867 * there's no need to call quote_meta,
4868 * alloc(7) is enough -- Acevedo
4869 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004870 compl_pattern = alloc(7);
4871 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004873 STRCPY((char *)compl_pattern, "\\<");
4874 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4875 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876 }
4877 else
4878 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004879 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
4880 compl_length) + 3);
4881 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004883 STRCPY((char *)compl_pattern, "\\<");
4884 (void)quote_meta(compl_pattern + 2, line + compl_col,
4885 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 }
4887 }
4888 }
4889 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4890 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004891 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004892 compl_length = (int)curs_col - (int)compl_col;
4893 if (compl_length < 0) /* cursor in indent: empty pattern */
4894 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004896 compl_pattern = str_foldcase(line + compl_col, compl_length,
4897 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004899 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4900 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 return FAIL;
4902 }
4903 else if (ctrl_x_mode == CTRL_X_FILES)
4904 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004905 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004906 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004907 compl_col += ++startcol;
4908 compl_length = (int)curs_col - startcol;
4909 compl_pattern = addstar(line + compl_col, compl_length,
4910 EXPAND_FILES);
4911 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004912 return FAIL;
4913 }
4914 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4915 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004916 compl_pattern = vim_strnsave(line, curs_col);
4917 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004919 set_cmd_context(&compl_xp, compl_pattern,
4920 (int)STRLEN(compl_pattern), curs_col);
4921 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4922 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004923 /* No completion possible, use an empty pattern to get a
4924 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004925 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004926 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004927 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4928 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004930 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004931 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004932#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004933 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004934 * Call user defined function 'completefunc' with "a:findstart"
4935 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004936 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004937 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004938 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004939 char_u *funcname;
4940 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004941
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004942 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004943 * string */
4944 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4945 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4946 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004947 {
4948 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
4949 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004950 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004951 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004952
4953 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00004954 args[1] = NULL;
4955 pos = curwin->w_cursor;
4956 col = call_func_retnr(funcname, 2, args, FALSE);
4957 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004958
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004959 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004960 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004961 compl_col = col;
4962 if ((colnr_T)compl_col > curs_col)
4963 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004964
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004965 /* Setup variables for completion. Need to obtain "line" again,
4966 * it may have become invalid. */
4967 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004968 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004969 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4970 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004971#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004972 return FAIL;
4973 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00004974 else if (ctrl_x_mode == CTRL_X_SPELL)
4975 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004976#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00004977 if (spell_bad_len > 0)
4978 compl_col = curs_col - spell_bad_len;
4979 else
4980 compl_col = spell_word_start(startcol);
4981 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004982 {
4983 compl_length = 0;
4984 compl_col = curs_col;
4985 }
4986 else
4987 {
4988 spell_expand_check_cap(compl_col);
4989 compl_length = (int)curs_col - compl_col;
4990 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00004991 /* Need to obtain "line" again, it may have become invalid. */
4992 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004993 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4994 if (compl_pattern == NULL)
4995#endif
4996 return FAIL;
4997 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004998 else
4999 {
5000 EMSG2(_(e_intern2), "ins_complete()");
5001 return FAIL;
5002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005004 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005005 {
5006 edit_submode_pre = (char_u *)_(" Adding");
5007 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5008 {
5009 /* Insert a new line, keep indentation but ignore 'comments' */
5010#ifdef FEAT_COMMENTS
5011 char_u *old = curbuf->b_p_com;
5012
5013 curbuf->b_p_com = (char_u *)"";
5014#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005015 compl_startpos.lnum = curwin->w_cursor.lnum;
5016 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 ins_eol('\r');
5018#ifdef FEAT_COMMENTS
5019 curbuf->b_p_com = old;
5020#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005021 compl_length = 0;
5022 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005023 }
5024 }
5025 else
5026 {
5027 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005028 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005031 if (compl_cont_status & CONT_LOCAL)
5032 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 else
5034 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5035
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005036 /* Always add completion for the original text. */
5037 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005038 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5039 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005040 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005041 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005042 vim_free(compl_pattern);
5043 compl_pattern = NULL;
5044 vim_free(compl_orig_text);
5045 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 return FAIL;
5047 }
5048
5049 /* showmode might reset the internal line pointers, so it must
5050 * be called before line = ml_get(), or when this address is no
5051 * longer needed. -- Acevedo.
5052 */
5053 edit_submode_extra = (char_u *)_("-- Searching...");
5054 edit_submode_highl = HLF_COUNT;
5055 showmode();
5056 edit_submode_extra = NULL;
5057 out_flush();
5058 }
5059
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005060 compl_shown_match = compl_curr_match;
5061 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062
5063 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005064 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005066 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005068 /* may undisplay the popup menu */
5069 ins_compl_upd_pum();
5070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005071 if (n > 1) /* all matches have been found */
5072 compl_matches = n;
5073 compl_curr_match = compl_shown_match;
5074 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075
Bram Moolenaard68071d2006-05-02 22:08:30 +00005076 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5077 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 if (got_int && !global_busy)
5079 {
5080 (void)vgetc();
5081 got_int = FALSE;
5082 }
5083
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005085 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005087 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5088 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5090 edit_submode_highl = HLF_E;
5091 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5092 * because we couldn't expand anything at first place, but if we used
5093 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5094 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 if ( compl_length > 1
5096 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097 || (ctrl_x_mode != 0
5098 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5099 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005100 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102
Bram Moolenaar572cb562005-08-05 21:35:02 +00005103 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005104 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005106 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107
5108 if (edit_submode_extra == NULL)
5109 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005110 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005111 {
5112 edit_submode_extra = (char_u *)_("Back at original");
5113 edit_submode_highl = HLF_W;
5114 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005115 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005116 {
5117 edit_submode_extra = (char_u *)_("Word from other line");
5118 edit_submode_highl = HLF_COUNT;
5119 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005120 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121 {
5122 edit_submode_extra = (char_u *)_("The only match");
5123 edit_submode_highl = HLF_COUNT;
5124 }
5125 else
5126 {
5127 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005128 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005130 int number = 0;
5131 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134 {
5135 /* search backwards for the first valid (!= -1) number.
5136 * This should normally succeed already at the first loop
5137 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005138 for (match = compl_curr_match->cp_prev; match != NULL
5139 && match != compl_first_match;
5140 match = match->cp_prev)
5141 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005142 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005143 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 break;
5145 }
5146 if (match != NULL)
5147 /* go up and assign all numbers which are not assigned
5148 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005149 for (match = match->cp_next;
5150 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005151 match = match->cp_next)
5152 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153 }
5154 else /* BACKWARD */
5155 {
5156 /* search forwards (upwards) for the first valid (!= -1)
5157 * number. This should normally succeed already at the
5158 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005159 for (match = compl_curr_match->cp_next; match != NULL
5160 && match != compl_first_match;
5161 match = match->cp_next)
5162 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005164 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 break;
5166 }
5167 if (match != NULL)
5168 /* go down and assign all numbers which are not
5169 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005170 for (match = match->cp_prev; match
5171 && match->cp_number == -1;
5172 match = match->cp_prev)
5173 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
5175 }
5176
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005177 /* The match should always have a sequence number now, this is
5178 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005179 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005181 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5182 * Translations may need more than twice that. */
5183 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005185 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005186 vim_snprintf((char *)match_ref, sizeof(match_ref),
5187 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005188 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005190 vim_snprintf((char *)match_ref, sizeof(match_ref),
5191 _("match %d"),
5192 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 edit_submode_extra = match_ref;
5194 edit_submode_highl = HLF_R;
5195 if (dollar_vcol)
5196 curs_columns(FALSE);
5197 }
5198 }
5199 }
5200
5201 /* Show a message about what (completion) mode we're in. */
5202 showmode();
5203 if (edit_submode_extra != NULL)
5204 {
5205 if (!p_smd)
5206 msg_attr(edit_submode_extra,
5207 edit_submode_highl < HLF_COUNT
5208 ? hl_attr(edit_submode_highl) : 0);
5209 }
5210 else
5211 msg_clr_cmdline(); /* necessary for "noshowmode" */
5212
Bram Moolenaard68071d2006-05-02 22:08:30 +00005213 /* Show the popup menu, unless we got interrupted. */
5214 if (!compl_interrupted)
5215 {
5216 /* RedrawingDisabled may be set when invoked through complete(). */
5217 n = RedrawingDisabled;
5218 RedrawingDisabled = 0;
5219 ins_compl_show_pum();
5220 setcursor();
5221 RedrawingDisabled = n;
5222 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005223 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005224 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005225
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 return OK;
5227}
5228
5229/*
5230 * Looks in the first "len" chars. of "src" for search-metachars.
5231 * If dest is not NULL the chars. are copied there quoting (with
5232 * a backslash) the metachars, and dest would be NUL terminated.
5233 * Returns the length (needed) of dest
5234 */
5235 static int
5236quote_meta(dest, src, len)
5237 char_u *dest;
5238 char_u *src;
5239 int len;
5240{
5241 int m;
5242
5243 for (m = len; --len >= 0; src++)
5244 {
5245 switch (*src)
5246 {
5247 case '.':
5248 case '*':
5249 case '[':
5250 if (ctrl_x_mode == CTRL_X_DICTIONARY
5251 || ctrl_x_mode == CTRL_X_THESAURUS)
5252 break;
5253 case '~':
5254 if (!p_magic) /* quote these only if magic is set */
5255 break;
5256 case '\\':
5257 if (ctrl_x_mode == CTRL_X_DICTIONARY
5258 || ctrl_x_mode == CTRL_X_THESAURUS)
5259 break;
5260 case '^': /* currently it's not needed. */
5261 case '$':
5262 m++;
5263 if (dest != NULL)
5264 *dest++ = '\\';
5265 break;
5266 }
5267 if (dest != NULL)
5268 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005269# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 /* Copy remaining bytes of a multibyte character. */
5271 if (has_mbyte)
5272 {
5273 int i, mb_len;
5274
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005275 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 if (mb_len > 0 && len >= mb_len)
5277 for (i = 0; i < mb_len; ++i)
5278 {
5279 --len;
5280 ++src;
5281 if (dest != NULL)
5282 *dest++ = *src;
5283 }
5284 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005285# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 }
5287 if (dest != NULL)
5288 *dest = NUL;
5289
5290 return m;
5291}
5292#endif /* FEAT_INS_EXPAND */
5293
5294/*
5295 * Next character is interpreted literally.
5296 * A one, two or three digit decimal number is interpreted as its byte value.
5297 * If one or two digits are entered, the next character is given to vungetc().
5298 * For Unicode a character > 255 may be returned.
5299 */
5300 int
5301get_literal()
5302{
5303 int cc;
5304 int nc;
5305 int i;
5306 int hex = FALSE;
5307 int octal = FALSE;
5308#ifdef FEAT_MBYTE
5309 int unicode = 0;
5310#endif
5311
5312 if (got_int)
5313 return Ctrl_C;
5314
5315#ifdef FEAT_GUI
5316 /*
5317 * In GUI there is no point inserting the internal code for a special key.
5318 * It is more useful to insert the string "<KEY>" instead. This would
5319 * probably be useful in a text window too, but it would not be
5320 * vi-compatible (maybe there should be an option for it?) -- webb
5321 */
5322 if (gui.in_use)
5323 ++allow_keys;
5324#endif
5325#ifdef USE_ON_FLY_SCROLL
5326 dont_scroll = TRUE; /* disallow scrolling here */
5327#endif
5328 ++no_mapping; /* don't map the next key hits */
5329 cc = 0;
5330 i = 0;
5331 for (;;)
5332 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005333 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005334#ifdef FEAT_CMDL_INFO
5335 if (!(State & CMDLINE)
5336# ifdef FEAT_MBYTE
5337 && MB_BYTE2LEN_CHECK(nc) == 1
5338# endif
5339 )
5340 add_to_showcmd(nc);
5341#endif
5342 if (nc == 'x' || nc == 'X')
5343 hex = TRUE;
5344 else if (nc == 'o' || nc == 'O')
5345 octal = TRUE;
5346#ifdef FEAT_MBYTE
5347 else if (nc == 'u' || nc == 'U')
5348 unicode = nc;
5349#endif
5350 else
5351 {
5352 if (hex
5353#ifdef FEAT_MBYTE
5354 || unicode != 0
5355#endif
5356 )
5357 {
5358 if (!vim_isxdigit(nc))
5359 break;
5360 cc = cc * 16 + hex2nr(nc);
5361 }
5362 else if (octal)
5363 {
5364 if (nc < '0' || nc > '7')
5365 break;
5366 cc = cc * 8 + nc - '0';
5367 }
5368 else
5369 {
5370 if (!VIM_ISDIGIT(nc))
5371 break;
5372 cc = cc * 10 + nc - '0';
5373 }
5374
5375 ++i;
5376 }
5377
5378 if (cc > 255
5379#ifdef FEAT_MBYTE
5380 && unicode == 0
5381#endif
5382 )
5383 cc = 255; /* limit range to 0-255 */
5384 nc = 0;
5385
5386 if (hex) /* hex: up to two chars */
5387 {
5388 if (i >= 2)
5389 break;
5390 }
5391#ifdef FEAT_MBYTE
5392 else if (unicode) /* Unicode: up to four or eight chars */
5393 {
5394 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5395 break;
5396 }
5397#endif
5398 else if (i >= 3) /* decimal or octal: up to three chars */
5399 break;
5400 }
5401 if (i == 0) /* no number entered */
5402 {
5403 if (nc == K_ZERO) /* NUL is stored as NL */
5404 {
5405 cc = '\n';
5406 nc = 0;
5407 }
5408 else
5409 {
5410 cc = nc;
5411 nc = 0;
5412 }
5413 }
5414
5415 if (cc == 0) /* NUL is stored as NL */
5416 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005417#ifdef FEAT_MBYTE
5418 if (enc_dbcs && (cc & 0xff) == 0)
5419 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5420 second byte will cause trouble! */
5421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005422
5423 --no_mapping;
5424#ifdef FEAT_GUI
5425 if (gui.in_use)
5426 --allow_keys;
5427#endif
5428 if (nc)
5429 vungetc(nc);
5430 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5431 return cc;
5432}
5433
5434/*
5435 * Insert character, taking care of special keys and mod_mask
5436 */
5437 static void
5438insert_special(c, allow_modmask, ctrlv)
5439 int c;
5440 int allow_modmask;
5441 int ctrlv; /* c was typed after CTRL-V */
5442{
5443 char_u *p;
5444 int len;
5445
5446 /*
5447 * Special function key, translate into "<Key>". Up to the last '>' is
5448 * inserted with ins_str(), so as not to replace characters in replace
5449 * mode.
5450 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5451 * unless 'allow_modmask' is TRUE.
5452 */
5453#ifdef MACOS
5454 /* Command-key never produces a normal key */
5455 if (mod_mask & MOD_MASK_CMD)
5456 allow_modmask = TRUE;
5457#endif
5458 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5459 {
5460 p = get_special_key_name(c, mod_mask);
5461 len = (int)STRLEN(p);
5462 c = p[len - 1];
5463 if (len > 2)
5464 {
5465 if (stop_arrow() == FAIL)
5466 return;
5467 p[len - 1] = NUL;
5468 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005469 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005470 ctrlv = FALSE;
5471 }
5472 }
5473 if (stop_arrow() == OK)
5474 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5475}
5476
5477/*
5478 * Special characters in this context are those that need processing other
5479 * than the simple insertion that can be performed here. This includes ESC
5480 * which terminates the insert, and CR/NL which need special processing to
5481 * open up a new line. This routine tries to optimize insertions performed by
5482 * the "redo", "undo" or "put" commands, so it needs to know when it should
5483 * stop and defer processing to the "normal" mechanism.
5484 * '0' and '^' are special, because they can be followed by CTRL-D.
5485 */
5486#ifdef EBCDIC
5487# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5488#else
5489# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5490#endif
5491
5492#ifdef FEAT_MBYTE
5493# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5494#else
5495# define WHITECHAR(cc) vim_iswhite(cc)
5496#endif
5497
5498 void
5499insertchar(c, flags, second_indent)
5500 int c; /* character to insert or NUL */
5501 int flags; /* INSCHAR_FORMAT, etc. */
5502 int second_indent; /* indent for second line if >= 0 */
5503{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005504 int textwidth;
5505#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005506 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005508 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005509
5510 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5511 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005512
5513 /*
5514 * Try to break the line in two or more pieces when:
5515 * - Always do this if we have been called to do formatting only.
5516 * - Always do this when 'formatoptions' has the 'a' flag and the line
5517 * ends in white space.
5518 * - Otherwise:
5519 * - Don't do this if inserting a blank
5520 * - Don't do this if an existing character is being replaced, unless
5521 * we're in VREPLACE mode.
5522 * - Do this if the cursor is not on the line where insert started
5523 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5524 * before the insert.
5525 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5526 * before 'textwidth'
5527 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005528 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005529 && ((flags & INSCHAR_FORMAT)
5530 || (!vim_iswhite(c)
5531 && !((State & REPLACE_FLAG)
5532#ifdef FEAT_VREPLACE
5533 && !(State & VREPLACE_FLAG)
5534#endif
5535 && *ml_get_cursor() != NUL)
5536 && (curwin->w_cursor.lnum != Insstart.lnum
5537 || ((!has_format_option(FO_INS_LONG)
5538 || Insstart_textlen <= (colnr_T)textwidth)
5539 && (!fo_ins_blank
5540 || Insstart_blank_vcol <= (colnr_T)textwidth
5541 ))))))
5542 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005543 /* Format with 'formatexpr' when it's set. Use internal formatting
5544 * when 'formatexpr' isn't set or it returns non-zero. */
5545#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005546 int do_internal = TRUE;
5547
Bram Moolenaar81a82092008-03-12 16:27:00 +00005548 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005549 {
5550 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5551 /* It may be required to save for undo again, e.g. when setline()
5552 * was called. */
5553 ins_need_undo = TRUE;
5554 }
5555 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005556#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005557 internal_format(textwidth, second_indent, flags, c == NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005558 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005559
Bram Moolenaar071d4272004-06-13 20:20:40 +00005560 if (c == NUL) /* only formatting was wanted */
5561 return;
5562
5563#ifdef FEAT_COMMENTS
5564 /* Check whether this character should end a comment. */
5565 if (did_ai && (int)c == end_comment_pending)
5566 {
5567 char_u *line;
5568 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5569 int middle_len, end_len;
5570 int i;
5571
5572 /*
5573 * Need to remove existing (middle) comment leader and insert end
5574 * comment leader. First, check what comment leader we can find.
5575 */
5576 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5577 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5578 {
5579 /* Skip middle-comment string */
5580 while (*p && p[-1] != ':') /* find end of middle flags */
5581 ++p;
5582 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5583 /* Don't count trailing white space for middle_len */
5584 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5585 --middle_len;
5586
5587 /* Find the end-comment string */
5588 while (*p && p[-1] != ':') /* find end of end flags */
5589 ++p;
5590 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5591
5592 /* Skip white space before the cursor */
5593 i = curwin->w_cursor.col;
5594 while (--i >= 0 && vim_iswhite(line[i]))
5595 ;
5596 i++;
5597
5598 /* Skip to before the middle leader */
5599 i -= middle_len;
5600
5601 /* Check some expected things before we go on */
5602 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5603 {
5604 /* Backspace over all the stuff we want to replace */
5605 backspace_until_column(i);
5606
5607 /*
5608 * Insert the end-comment string, except for the last
5609 * character, which will get inserted as normal later.
5610 */
5611 ins_bytes_len(lead_end, end_len - 1);
5612 }
5613 }
5614 }
5615 end_comment_pending = NUL;
5616#endif
5617
5618 did_ai = FALSE;
5619#ifdef FEAT_SMARTINDENT
5620 did_si = FALSE;
5621 can_si = FALSE;
5622 can_si_back = FALSE;
5623#endif
5624
5625 /*
5626 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5627 * This speeds up normal text input considerably.
5628 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5629 * need to re-indent at a ':', or any other character (but not what
5630 * 'paste' is set)..
5631 */
5632#ifdef USE_ON_FLY_SCROLL
5633 dont_scroll = FALSE; /* allow scrolling here */
5634#endif
5635
5636 if ( !ISSPECIAL(c)
5637#ifdef FEAT_MBYTE
5638 && (!has_mbyte || (*mb_char2len)(c) == 1)
5639#endif
5640 && vpeekc() != NUL
5641 && !(State & REPLACE_FLAG)
5642#ifdef FEAT_CINDENT
5643 && !cindent_on()
5644#endif
5645#ifdef FEAT_RIGHTLEFT
5646 && !p_ri
5647#endif
5648 )
5649 {
5650#define INPUT_BUFLEN 100
5651 char_u buf[INPUT_BUFLEN + 1];
5652 int i;
5653 colnr_T virtcol = 0;
5654
5655 buf[0] = c;
5656 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005657 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658 virtcol = get_nolist_virtcol();
5659 /*
5660 * Stop the string when:
5661 * - no more chars available
5662 * - finding a special character (command key)
5663 * - buffer is full
5664 * - running into the 'textwidth' boundary
5665 * - need to check for abbreviation: A non-word char after a word-char
5666 */
5667 while ( (c = vpeekc()) != NUL
5668 && !ISSPECIAL(c)
5669#ifdef FEAT_MBYTE
5670 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5671#endif
5672 && i < INPUT_BUFLEN
5673 && (textwidth == 0
5674 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5675 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5676 {
5677#ifdef FEAT_RIGHTLEFT
5678 c = vgetc();
5679 if (p_hkmap && KeyTyped)
5680 c = hkmap(c); /* Hebrew mode mapping */
5681# ifdef FEAT_FKMAP
5682 if (p_fkmap && KeyTyped)
5683 c = fkmap(c); /* Farsi mode mapping */
5684# endif
5685 buf[i++] = c;
5686#else
5687 buf[i++] = vgetc();
5688#endif
5689 }
5690
5691#ifdef FEAT_DIGRAPHS
5692 do_digraph(-1); /* clear digraphs */
5693 do_digraph(buf[i-1]); /* may be the start of a digraph */
5694#endif
5695 buf[i] = NUL;
5696 ins_str(buf);
5697 if (flags & INSCHAR_CTRLV)
5698 {
5699 redo_literal(*buf);
5700 i = 1;
5701 }
5702 else
5703 i = 0;
5704 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005705 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707 else
5708 {
5709#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005710 int cc;
5711
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5713 {
5714 char_u buf[MB_MAXBYTES + 1];
5715
5716 (*mb_char2bytes)(c, buf);
5717 buf[cc] = NUL;
5718 ins_char_bytes(buf, cc);
5719 AppendCharToRedobuff(c);
5720 }
5721 else
5722#endif
5723 {
5724 ins_char(c);
5725 if (flags & INSCHAR_CTRLV)
5726 redo_literal(c);
5727 else
5728 AppendCharToRedobuff(c);
5729 }
5730 }
5731}
5732
5733/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005734 * Format text at the current insert position.
5735 */
5736 static void
5737internal_format(textwidth, second_indent, flags, format_only)
5738 int textwidth;
5739 int second_indent;
5740 int flags;
5741 int format_only;
5742{
5743 int cc;
5744 int save_char = NUL;
5745 int haveto_redraw = FALSE;
5746 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5747#ifdef FEAT_MBYTE
5748 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5749#endif
5750 int fo_white_par = has_format_option(FO_WHITE_PAR);
5751 int first_line = TRUE;
5752#ifdef FEAT_COMMENTS
5753 colnr_T leader_len;
5754 int no_leader = FALSE;
5755 int do_comments = (flags & INSCHAR_DO_COM);
5756#endif
5757
5758 /*
5759 * When 'ai' is off we don't want a space under the cursor to be
5760 * deleted. Replace it with an 'x' temporarily.
5761 */
5762 if (!curbuf->b_p_ai)
5763 {
5764 cc = gchar_cursor();
5765 if (vim_iswhite(cc))
5766 {
5767 save_char = cc;
5768 pchar_cursor('x');
5769 }
5770 }
5771
5772 /*
5773 * Repeat breaking lines, until the current line is not too long.
5774 */
5775 while (!got_int)
5776 {
5777 int startcol; /* Cursor column at entry */
5778 int wantcol; /* column at textwidth border */
5779 int foundcol; /* column for start of spaces */
5780 int end_foundcol = 0; /* column for start of word */
5781 colnr_T len;
5782 colnr_T virtcol;
5783#ifdef FEAT_VREPLACE
5784 int orig_col = 0;
5785 char_u *saved_text = NULL;
5786#endif
5787 colnr_T col;
5788
5789 virtcol = get_nolist_virtcol();
5790 if (virtcol < (colnr_T)textwidth)
5791 break;
5792
5793#ifdef FEAT_COMMENTS
5794 if (no_leader)
5795 do_comments = FALSE;
5796 else if (!(flags & INSCHAR_FORMAT)
5797 && has_format_option(FO_WRAP_COMS))
5798 do_comments = TRUE;
5799
5800 /* Don't break until after the comment leader */
5801 if (do_comments)
5802 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5803 else
5804 leader_len = 0;
5805
5806 /* If the line doesn't start with a comment leader, then don't
5807 * start one in a following broken line. Avoids that a %word
5808 * moved to the start of the next line causes all following lines
5809 * to start with %. */
5810 if (leader_len == 0)
5811 no_leader = TRUE;
5812#endif
5813 if (!(flags & INSCHAR_FORMAT)
5814#ifdef FEAT_COMMENTS
5815 && leader_len == 0
5816#endif
5817 && !has_format_option(FO_WRAP))
5818
5819 {
5820 textwidth = 0;
5821 break;
5822 }
5823 if ((startcol = curwin->w_cursor.col) == 0)
5824 break;
5825
5826 /* find column of textwidth border */
5827 coladvance((colnr_T)textwidth);
5828 wantcol = curwin->w_cursor.col;
5829
5830 curwin->w_cursor.col = startcol - 1;
5831#ifdef FEAT_MBYTE
5832 /* Correct cursor for multi-byte character. */
5833 if (has_mbyte)
5834 mb_adjust_cursor();
5835#endif
5836 foundcol = 0;
5837
5838 /*
5839 * Find position to break at.
5840 * Stop at first entered white when 'formatoptions' has 'v'
5841 */
5842 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5843 || curwin->w_cursor.lnum != Insstart.lnum
5844 || curwin->w_cursor.col >= Insstart.col)
5845 {
5846 cc = gchar_cursor();
5847 if (WHITECHAR(cc))
5848 {
5849 /* remember position of blank just before text */
5850 end_foundcol = curwin->w_cursor.col;
5851
5852 /* find start of sequence of blanks */
5853 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5854 {
5855 dec_cursor();
5856 cc = gchar_cursor();
5857 }
5858 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5859 break; /* only spaces in front of text */
5860#ifdef FEAT_COMMENTS
5861 /* Don't break until after the comment leader */
5862 if (curwin->w_cursor.col < leader_len)
5863 break;
5864#endif
5865 if (has_format_option(FO_ONE_LETTER))
5866 {
5867 /* do not break after one-letter words */
5868 if (curwin->w_cursor.col == 0)
5869 break; /* one-letter word at begin */
5870
5871 col = curwin->w_cursor.col;
5872 dec_cursor();
5873 cc = gchar_cursor();
5874
5875 if (WHITECHAR(cc))
5876 continue; /* one-letter, continue */
5877 curwin->w_cursor.col = col;
5878 }
5879#ifdef FEAT_MBYTE
5880 if (has_mbyte)
5881 foundcol = curwin->w_cursor.col
5882 + (*mb_ptr2len)(ml_get_cursor());
5883 else
5884#endif
5885 foundcol = curwin->w_cursor.col + 1;
5886 if (curwin->w_cursor.col < (colnr_T)wantcol)
5887 break;
5888 }
5889#ifdef FEAT_MBYTE
5890 else if (cc >= 0x100 && fo_multibyte
5891 && curwin->w_cursor.col <= (colnr_T)wantcol)
5892 {
5893 /* Break after or before a multi-byte character. */
5894 foundcol = curwin->w_cursor.col;
5895 if (curwin->w_cursor.col < (colnr_T)wantcol)
5896 foundcol += (*mb_char2len)(cc);
5897 end_foundcol = foundcol;
5898 break;
5899 }
5900#endif
5901 if (curwin->w_cursor.col == 0)
5902 break;
5903 dec_cursor();
5904 }
5905
5906 if (foundcol == 0) /* no spaces, cannot break line */
5907 {
5908 curwin->w_cursor.col = startcol;
5909 break;
5910 }
5911
5912 /* Going to break the line, remove any "$" now. */
5913 undisplay_dollar();
5914
5915 /*
5916 * Offset between cursor position and line break is used by replace
5917 * stack functions. VREPLACE does not use this, and backspaces
5918 * over the text instead.
5919 */
5920#ifdef FEAT_VREPLACE
5921 if (State & VREPLACE_FLAG)
5922 orig_col = startcol; /* Will start backspacing from here */
5923 else
5924#endif
5925 replace_offset = startcol - end_foundcol - 1;
5926
5927 /*
5928 * adjust startcol for spaces that will be deleted and
5929 * characters that will remain on top line
5930 */
5931 curwin->w_cursor.col = foundcol;
5932 while (cc = gchar_cursor(), WHITECHAR(cc))
5933 inc_cursor();
5934 startcol -= curwin->w_cursor.col;
5935 if (startcol < 0)
5936 startcol = 0;
5937
5938#ifdef FEAT_VREPLACE
5939 if (State & VREPLACE_FLAG)
5940 {
5941 /*
5942 * In VREPLACE mode, we will backspace over the text to be
5943 * wrapped, so save a copy now to put on the next line.
5944 */
5945 saved_text = vim_strsave(ml_get_cursor());
5946 curwin->w_cursor.col = orig_col;
5947 if (saved_text == NULL)
5948 break; /* Can't do it, out of memory */
5949 saved_text[startcol] = NUL;
5950
5951 /* Backspace over characters that will move to the next line */
5952 if (!fo_white_par)
5953 backspace_until_column(foundcol);
5954 }
5955 else
5956#endif
5957 {
5958 /* put cursor after pos. to break line */
5959 if (!fo_white_par)
5960 curwin->w_cursor.col = foundcol;
5961 }
5962
5963 /*
5964 * Split the line just before the margin.
5965 * Only insert/delete lines, but don't really redraw the window.
5966 */
5967 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
5968 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
5969#ifdef FEAT_COMMENTS
5970 + (do_comments ? OPENLINE_DO_COM : 0)
5971#endif
5972 , old_indent);
5973 old_indent = 0;
5974
5975 replace_offset = 0;
5976 if (first_line)
5977 {
5978 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
5979 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
5980 if (second_indent >= 0)
5981 {
5982#ifdef FEAT_VREPLACE
5983 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00005984 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005985 else
5986#endif
5987 (void)set_indent(second_indent, SIN_CHANGED);
5988 }
5989 first_line = FALSE;
5990 }
5991
5992#ifdef FEAT_VREPLACE
5993 if (State & VREPLACE_FLAG)
5994 {
5995 /*
5996 * In VREPLACE mode we have backspaced over the text to be
5997 * moved, now we re-insert it into the new line.
5998 */
5999 ins_bytes(saved_text);
6000 vim_free(saved_text);
6001 }
6002 else
6003#endif
6004 {
6005 /*
6006 * Check if cursor is not past the NUL off the line, cindent
6007 * may have added or removed indent.
6008 */
6009 curwin->w_cursor.col += startcol;
6010 len = (colnr_T)STRLEN(ml_get_curline());
6011 if (curwin->w_cursor.col > len)
6012 curwin->w_cursor.col = len;
6013 }
6014
6015 haveto_redraw = TRUE;
6016#ifdef FEAT_CINDENT
6017 can_cindent = TRUE;
6018#endif
6019 /* moved the cursor, don't autoindent or cindent now */
6020 did_ai = FALSE;
6021#ifdef FEAT_SMARTINDENT
6022 did_si = FALSE;
6023 can_si = FALSE;
6024 can_si_back = FALSE;
6025#endif
6026 line_breakcheck();
6027 }
6028
6029 if (save_char != NUL) /* put back space after cursor */
6030 pchar_cursor(save_char);
6031
6032 if (!format_only && haveto_redraw)
6033 {
6034 update_topline();
6035 redraw_curbuf_later(VALID);
6036 }
6037}
6038
6039/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006040 * Called after inserting or deleting text: When 'formatoptions' includes the
6041 * 'a' flag format from the current line until the end of the paragraph.
6042 * Keep the cursor at the same position relative to the text.
6043 * The caller must have saved the cursor line for undo, following ones will be
6044 * saved here.
6045 */
6046 void
6047auto_format(trailblank, prev_line)
6048 int trailblank; /* when TRUE also format with trailing blank */
6049 int prev_line; /* may start in previous line */
6050{
6051 pos_T pos;
6052 colnr_T len;
6053 char_u *old;
6054 char_u *new, *pnew;
6055 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006056 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006057
6058 if (!has_format_option(FO_AUTO))
6059 return;
6060
6061 pos = curwin->w_cursor;
6062 old = ml_get_curline();
6063
6064 /* may remove added space */
6065 check_auto_format(FALSE);
6066
6067 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6068 * user might insert normal text next. Also skip formatting when "1" is
6069 * in 'formatoptions' and there is a single character before the cursor.
6070 * Otherwise the line would be broken and when typing another non-white
6071 * next they are not joined back together. */
6072 wasatend = (pos.col == STRLEN(old));
6073 if (*old != NUL && !trailblank && wasatend)
6074 {
6075 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006076 cc = gchar_cursor();
6077 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6078 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006079 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006080 cc = gchar_cursor();
6081 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006082 {
6083 curwin->w_cursor = pos;
6084 return;
6085 }
6086 curwin->w_cursor = pos;
6087 }
6088
6089#ifdef FEAT_COMMENTS
6090 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6091 * comments. */
6092 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6093 && get_leader_len(old, NULL, FALSE) == 0)
6094 return;
6095#endif
6096
6097 /*
6098 * May start formatting in a previous line, so that after "x" a word is
6099 * moved to the previous line if it fits there now. Only when this is not
6100 * the start of a paragraph.
6101 */
6102 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6103 {
6104 --curwin->w_cursor.lnum;
6105 if (u_save_cursor() == FAIL)
6106 return;
6107 }
6108
6109 /*
6110 * Do the formatting and restore the cursor position. "saved_cursor" will
6111 * be adjusted for the text formatting.
6112 */
6113 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006114 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006115 curwin->w_cursor = saved_cursor;
6116 saved_cursor.lnum = 0;
6117
6118 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6119 {
6120 /* "cannot happen" */
6121 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6122 coladvance((colnr_T)MAXCOL);
6123 }
6124 else
6125 check_cursor_col();
6126
6127 /* Insert mode: If the cursor is now after the end of the line while it
6128 * previously wasn't, the line was broken. Because of the rule above we
6129 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6130 * formatted. */
6131 if (!wasatend && has_format_option(FO_WHITE_PAR))
6132 {
6133 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006134 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006135 if (curwin->w_cursor.col == len)
6136 {
6137 pnew = vim_strnsave(new, len + 2);
6138 pnew[len] = ' ';
6139 pnew[len + 1] = NUL;
6140 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6141 /* remove the space later */
6142 did_add_space = TRUE;
6143 }
6144 else
6145 /* may remove added space */
6146 check_auto_format(FALSE);
6147 }
6148
6149 check_cursor();
6150}
6151
6152/*
6153 * When an extra space was added to continue a paragraph for auto-formatting,
6154 * delete it now. The space must be under the cursor, just after the insert
6155 * position.
6156 */
6157 static void
6158check_auto_format(end_insert)
6159 int end_insert; /* TRUE when ending Insert mode */
6160{
6161 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006162 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006163
6164 if (did_add_space)
6165 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006166 cc = gchar_cursor();
6167 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006168 /* Somehow the space was removed already. */
6169 did_add_space = FALSE;
6170 else
6171 {
6172 if (!end_insert)
6173 {
6174 inc_cursor();
6175 c = gchar_cursor();
6176 dec_cursor();
6177 }
6178 if (c != NUL)
6179 {
6180 /* The space is no longer at the end of the line, delete it. */
6181 del_char(FALSE);
6182 did_add_space = FALSE;
6183 }
6184 }
6185 }
6186}
6187
6188/*
6189 * Find out textwidth to be used for formatting:
6190 * if 'textwidth' option is set, use it
6191 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6192 * if invalid value, use 0.
6193 * Set default to window width (maximum 79) for "gq" operator.
6194 */
6195 int
6196comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006197 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006198{
6199 int textwidth;
6200
6201 textwidth = curbuf->b_p_tw;
6202 if (textwidth == 0 && curbuf->b_p_wm)
6203 {
6204 /* The width is the window width minus 'wrapmargin' minus all the
6205 * things that add to the margin. */
6206 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6207#ifdef FEAT_CMDWIN
6208 if (cmdwin_type != 0)
6209 textwidth -= 1;
6210#endif
6211#ifdef FEAT_FOLDING
6212 textwidth -= curwin->w_p_fdc;
6213#endif
6214#ifdef FEAT_SIGNS
6215 if (curwin->w_buffer->b_signlist != NULL
6216# ifdef FEAT_NETBEANS_INTG
6217 || usingNetbeans
6218# endif
6219 )
6220 textwidth -= 1;
6221#endif
6222 if (curwin->w_p_nu)
6223 textwidth -= 8;
6224 }
6225 if (textwidth < 0)
6226 textwidth = 0;
6227 if (ff && textwidth == 0)
6228 {
6229 textwidth = W_WIDTH(curwin) - 1;
6230 if (textwidth > 79)
6231 textwidth = 79;
6232 }
6233 return textwidth;
6234}
6235
6236/*
6237 * Put a character in the redo buffer, for when just after a CTRL-V.
6238 */
6239 static void
6240redo_literal(c)
6241 int c;
6242{
6243 char_u buf[10];
6244
6245 /* Only digits need special treatment. Translate them into a string of
6246 * three digits. */
6247 if (VIM_ISDIGIT(c))
6248 {
6249 sprintf((char *)buf, "%03d", c);
6250 AppendToRedobuff(buf);
6251 }
6252 else
6253 AppendCharToRedobuff(c);
6254}
6255
6256/*
6257 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006258 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006259 */
6260 static void
6261start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006262 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006263{
6264 if (!arrow_used) /* something has been inserted */
6265 {
6266 AppendToRedobuff(ESC_STR);
6267 stop_insert(end_insert_pos, FALSE);
6268 arrow_used = TRUE; /* this means we stopped the current insert */
6269 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006270#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006271 check_spell_redraw();
6272#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006273}
6274
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006275#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006276/*
6277 * If we skipped highlighting word at cursor, do it now.
6278 * It may be skipped again, thus reset spell_redraw_lnum first.
6279 */
6280 static void
6281check_spell_redraw()
6282{
6283 if (spell_redraw_lnum != 0)
6284 {
6285 linenr_T lnum = spell_redraw_lnum;
6286
6287 spell_redraw_lnum = 0;
6288 redrawWinline(lnum, FALSE);
6289 }
6290}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006291
6292/*
6293 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6294 * spelled word, if there is one.
6295 */
6296 static void
6297spell_back_to_badword()
6298{
6299 pos_T tpos = curwin->w_cursor;
6300
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006301 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006302 if (curwin->w_cursor.col != tpos.col)
6303 start_arrow(&tpos);
6304}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006305#endif
6306
Bram Moolenaar071d4272004-06-13 20:20:40 +00006307/*
6308 * stop_arrow() is called before a change is made in insert mode.
6309 * If an arrow key has been used, start a new insertion.
6310 * Returns FAIL if undo is impossible, shouldn't insert then.
6311 */
6312 int
6313stop_arrow()
6314{
6315 if (arrow_used)
6316 {
6317 if (u_save_cursor() == OK)
6318 {
6319 arrow_used = FALSE;
6320 ins_need_undo = FALSE;
6321 }
6322 Insstart = curwin->w_cursor; /* new insertion starts here */
6323 Insstart_textlen = linetabsize(ml_get_curline());
6324 ai_col = 0;
6325#ifdef FEAT_VREPLACE
6326 if (State & VREPLACE_FLAG)
6327 {
6328 orig_line_count = curbuf->b_ml.ml_line_count;
6329 vr_lines_changed = 1;
6330 }
6331#endif
6332 ResetRedobuff();
6333 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006334 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006335 }
6336 else if (ins_need_undo)
6337 {
6338 if (u_save_cursor() == OK)
6339 ins_need_undo = FALSE;
6340 }
6341
6342#ifdef FEAT_FOLDING
6343 /* Always open fold at the cursor line when inserting something. */
6344 foldOpenCursor();
6345#endif
6346
6347 return (arrow_used || ins_need_undo ? FAIL : OK);
6348}
6349
6350/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006351 * Do a few things to stop inserting.
6352 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6353 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006354 */
6355 static void
6356stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006357 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006358 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006359{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006360 int cc;
6361 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006362
6363 stop_redo_ins();
6364 replace_flush(); /* abandon replace stack */
6365
6366 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006367 * Save the inserted text for later redo with ^@ and CTRL-A.
6368 * Don't do it when "restart_edit" was set and nothing was inserted,
6369 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006371 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006372 if (did_restart_edit == 0 || (ptr != NULL
6373 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006374 {
6375 vim_free(last_insert);
6376 last_insert = ptr;
6377 last_insert_skip = new_insert_skip;
6378 }
6379 else
6380 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006381
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006382 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383 {
6384 /* Auto-format now. It may seem strange to do this when stopping an
6385 * insertion (or moving the cursor), but it's required when appending
6386 * a line and having it end in a space. But only do it when something
6387 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006388 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006389 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006390 pos_T tpos = curwin->w_cursor;
6391
Bram Moolenaar071d4272004-06-13 20:20:40 +00006392 /* When the cursor is at the end of the line after a space the
6393 * formatting will move it to the following word. Avoid that by
6394 * moving the cursor onto the space. */
6395 cc = 'x';
6396 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6397 {
6398 dec_cursor();
6399 cc = gchar_cursor();
6400 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006401 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 }
6403
6404 auto_format(TRUE, FALSE);
6405
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006406 if (vim_iswhite(cc))
6407 {
6408 if (gchar_cursor() != NUL)
6409 inc_cursor();
6410#ifdef FEAT_VIRTUALEDIT
6411 /* If the cursor is still at the same character, also keep
6412 * the "coladd". */
6413 if (gchar_cursor() == NUL
6414 && curwin->w_cursor.lnum == tpos.lnum
6415 && curwin->w_cursor.col == tpos.col)
6416 curwin->w_cursor.coladd = tpos.coladd;
6417#endif
6418 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006419 }
6420
6421 /* If a space was inserted for auto-formatting, remove it now. */
6422 check_auto_format(TRUE);
6423
6424 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006425 * of the line, and put the cursor back.
6426 * Do this when ESC was used or moving the cursor up/down. */
6427 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006428 && curwin->w_cursor.lnum != end_insert_pos->lnum)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006429 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006430 pos_T tpos = curwin->w_cursor;
6431
6432 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar39f05632006-03-19 22:15:26 +00006433 for (;;)
6434 {
6435 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6436 --curwin->w_cursor.col;
6437 cc = gchar_cursor();
6438 if (!vim_iswhite(cc))
6439 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006440 (void)del_char(TRUE);
Bram Moolenaar39f05632006-03-19 22:15:26 +00006441 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006442 if (curwin->w_cursor.lnum != tpos.lnum)
6443 curwin->w_cursor = tpos;
6444 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006445 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6446
6447#ifdef FEAT_VISUAL
6448 /* <C-S-Right> may have started Visual mode, adjust the position for
6449 * deleted characters. */
6450 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6451 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006452 cc = (int)STRLEN(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006453 if (VIsual.col > (colnr_T)cc)
6454 {
6455 VIsual.col = cc;
6456# ifdef FEAT_VIRTUALEDIT
6457 VIsual.coladd = 0;
6458# endif
6459 }
6460 }
6461#endif
6462 }
6463 }
6464 did_ai = FALSE;
6465#ifdef FEAT_SMARTINDENT
6466 did_si = FALSE;
6467 can_si = FALSE;
6468 can_si_back = FALSE;
6469#endif
6470
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006471 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6472 * now in a different buffer. */
6473 if (end_insert_pos != NULL)
6474 {
6475 curbuf->b_op_start = Insstart;
6476 curbuf->b_op_end = *end_insert_pos;
6477 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478}
6479
6480/*
6481 * Set the last inserted text to a single character.
6482 * Used for the replace command.
6483 */
6484 void
6485set_last_insert(c)
6486 int c;
6487{
6488 char_u *s;
6489
6490 vim_free(last_insert);
6491#ifdef FEAT_MBYTE
6492 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6493#else
6494 last_insert = alloc(6);
6495#endif
6496 if (last_insert != NULL)
6497 {
6498 s = last_insert;
6499 /* Use the CTRL-V only when entering a special char */
6500 if (c < ' ' || c == DEL)
6501 *s++ = Ctrl_V;
6502 s = add_char2buf(c, s);
6503 *s++ = ESC;
6504 *s++ = NUL;
6505 last_insert_skip = 0;
6506 }
6507}
6508
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006509#if defined(EXITFREE) || defined(PROTO)
6510 void
6511free_last_insert()
6512{
6513 vim_free(last_insert);
6514 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006515# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006516 vim_free(compl_orig_text);
6517 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006518# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006519}
6520#endif
6521
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522/*
6523 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6524 * and CSI. Handle multi-byte characters.
6525 * Returns a pointer to after the added bytes.
6526 */
6527 char_u *
6528add_char2buf(c, s)
6529 int c;
6530 char_u *s;
6531{
6532#ifdef FEAT_MBYTE
6533 char_u temp[MB_MAXBYTES];
6534 int i;
6535 int len;
6536
6537 len = (*mb_char2bytes)(c, temp);
6538 for (i = 0; i < len; ++i)
6539 {
6540 c = temp[i];
6541#endif
6542 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6543 if (c == K_SPECIAL)
6544 {
6545 *s++ = K_SPECIAL;
6546 *s++ = KS_SPECIAL;
6547 *s++ = KE_FILLER;
6548 }
6549#ifdef FEAT_GUI
6550 else if (c == CSI)
6551 {
6552 *s++ = CSI;
6553 *s++ = KS_EXTRA;
6554 *s++ = (int)KE_CSI;
6555 }
6556#endif
6557 else
6558 *s++ = c;
6559#ifdef FEAT_MBYTE
6560 }
6561#endif
6562 return s;
6563}
6564
6565/*
6566 * move cursor to start of line
6567 * if flags & BL_WHITE move to first non-white
6568 * if flags & BL_SOL move to first non-white if startofline is set,
6569 * otherwise keep "curswant" column
6570 * if flags & BL_FIX don't leave the cursor on a NUL.
6571 */
6572 void
6573beginline(flags)
6574 int flags;
6575{
6576 if ((flags & BL_SOL) && !p_sol)
6577 coladvance(curwin->w_curswant);
6578 else
6579 {
6580 curwin->w_cursor.col = 0;
6581#ifdef FEAT_VIRTUALEDIT
6582 curwin->w_cursor.coladd = 0;
6583#endif
6584
6585 if (flags & (BL_WHITE | BL_SOL))
6586 {
6587 char_u *ptr;
6588
6589 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6590 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6591 ++curwin->w_cursor.col;
6592 }
6593 curwin->w_set_curswant = TRUE;
6594 }
6595}
6596
6597/*
6598 * oneright oneleft cursor_down cursor_up
6599 *
6600 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006601 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006602 * Return OK when successful, FAIL when we hit a line of file boundary.
6603 */
6604
6605 int
6606oneright()
6607{
6608 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006609 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006610
6611#ifdef FEAT_VIRTUALEDIT
6612 if (virtual_active())
6613 {
6614 pos_T prevpos = curwin->w_cursor;
6615
6616 /* Adjust for multi-wide char (excluding TAB) */
6617 ptr = ml_get_cursor();
6618 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006619# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006620 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006621# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006622 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006623# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006624 ))
6625 ? ptr2cells(ptr) : 1));
6626 curwin->w_set_curswant = TRUE;
6627 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6628 return (prevpos.col != curwin->w_cursor.col
6629 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6630 }
6631#endif
6632
6633 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006634 if (*ptr == NUL)
6635 return FAIL; /* already at the very end */
6636
Bram Moolenaar071d4272004-06-13 20:20:40 +00006637#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006638 if (has_mbyte)
6639 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006640 else
6641#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006642 l = 1;
6643
6644 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6645 * contains "onemore". */
6646 if (ptr[l] == NUL
6647#ifdef FEAT_VIRTUALEDIT
6648 && (ve_flags & VE_ONEMORE) == 0
6649#endif
6650 )
6651 return FAIL;
6652 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006653
6654 curwin->w_set_curswant = TRUE;
6655 return OK;
6656}
6657
6658 int
6659oneleft()
6660{
6661#ifdef FEAT_VIRTUALEDIT
6662 if (virtual_active())
6663 {
6664 int width;
6665 int v = getviscol();
6666
6667 if (v == 0)
6668 return FAIL;
6669
6670# ifdef FEAT_LINEBREAK
6671 /* We might get stuck on 'showbreak', skip over it. */
6672 width = 1;
6673 for (;;)
6674 {
6675 coladvance(v - width);
6676 /* getviscol() is slow, skip it when 'showbreak' is empty and
6677 * there are no multi-byte characters */
6678 if ((*p_sbr == NUL
6679# ifdef FEAT_MBYTE
6680 && !has_mbyte
6681# endif
6682 ) || getviscol() < v)
6683 break;
6684 ++width;
6685 }
6686# else
6687 coladvance(v - 1);
6688# endif
6689
6690 if (curwin->w_cursor.coladd == 1)
6691 {
6692 char_u *ptr;
6693
6694 /* Adjust for multi-wide char (not a TAB) */
6695 ptr = ml_get_cursor();
6696 if (*ptr != TAB && vim_isprintc(
6697# ifdef FEAT_MBYTE
6698 (*mb_ptr2char)(ptr)
6699# else
6700 *ptr
6701# endif
6702 ) && ptr2cells(ptr) > 1)
6703 curwin->w_cursor.coladd = 0;
6704 }
6705
6706 curwin->w_set_curswant = TRUE;
6707 return OK;
6708 }
6709#endif
6710
6711 if (curwin->w_cursor.col == 0)
6712 return FAIL;
6713
6714 curwin->w_set_curswant = TRUE;
6715 --curwin->w_cursor.col;
6716
6717#ifdef FEAT_MBYTE
6718 /* if the character on the left of the current cursor is a multi-byte
6719 * character, move to its first byte */
6720 if (has_mbyte)
6721 mb_adjust_cursor();
6722#endif
6723 return OK;
6724}
6725
6726 int
6727cursor_up(n, upd_topline)
6728 long n;
6729 int upd_topline; /* When TRUE: update topline */
6730{
6731 linenr_T lnum;
6732
6733 if (n > 0)
6734 {
6735 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006736 /* This fails if the cursor is already in the first line or the count
6737 * is larger than the line number and '-' is in 'cpoptions' */
6738 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006739 return FAIL;
6740 if (n >= lnum)
6741 lnum = 1;
6742 else
6743#ifdef FEAT_FOLDING
6744 if (hasAnyFolding(curwin))
6745 {
6746 /*
6747 * Count each sequence of folded lines as one logical line.
6748 */
6749 /* go to the the start of the current fold */
6750 (void)hasFolding(lnum, &lnum, NULL);
6751
6752 while (n--)
6753 {
6754 /* move up one line */
6755 --lnum;
6756 if (lnum <= 1)
6757 break;
6758 /* If we entered a fold, move to the beginning, unless in
6759 * Insert mode or when 'foldopen' contains "all": it will open
6760 * in a moment. */
6761 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6762 (void)hasFolding(lnum, &lnum, NULL);
6763 }
6764 if (lnum < 1)
6765 lnum = 1;
6766 }
6767 else
6768#endif
6769 lnum -= n;
6770 curwin->w_cursor.lnum = lnum;
6771 }
6772
6773 /* try to advance to the column we want to be at */
6774 coladvance(curwin->w_curswant);
6775
6776 if (upd_topline)
6777 update_topline(); /* make sure curwin->w_topline is valid */
6778
6779 return OK;
6780}
6781
6782/*
6783 * Cursor down a number of logical lines.
6784 */
6785 int
6786cursor_down(n, upd_topline)
6787 long n;
6788 int upd_topline; /* When TRUE: update topline */
6789{
6790 linenr_T lnum;
6791
6792 if (n > 0)
6793 {
6794 lnum = curwin->w_cursor.lnum;
6795#ifdef FEAT_FOLDING
6796 /* Move to last line of fold, will fail if it's the end-of-file. */
6797 (void)hasFolding(lnum, NULL, &lnum);
6798#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006799 /* This fails if the cursor is already in the last line or would move
6800 * beyound the last line and '-' is in 'cpoptions' */
6801 if (lnum >= curbuf->b_ml.ml_line_count
6802 || (lnum + n > curbuf->b_ml.ml_line_count
6803 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006804 return FAIL;
6805 if (lnum + n >= curbuf->b_ml.ml_line_count)
6806 lnum = curbuf->b_ml.ml_line_count;
6807 else
6808#ifdef FEAT_FOLDING
6809 if (hasAnyFolding(curwin))
6810 {
6811 linenr_T last;
6812
6813 /* count each sequence of folded lines as one logical line */
6814 while (n--)
6815 {
6816 if (hasFolding(lnum, NULL, &last))
6817 lnum = last + 1;
6818 else
6819 ++lnum;
6820 if (lnum >= curbuf->b_ml.ml_line_count)
6821 break;
6822 }
6823 if (lnum > curbuf->b_ml.ml_line_count)
6824 lnum = curbuf->b_ml.ml_line_count;
6825 }
6826 else
6827#endif
6828 lnum += n;
6829 curwin->w_cursor.lnum = lnum;
6830 }
6831
6832 /* try to advance to the column we want to be at */
6833 coladvance(curwin->w_curswant);
6834
6835 if (upd_topline)
6836 update_topline(); /* make sure curwin->w_topline is valid */
6837
6838 return OK;
6839}
6840
6841/*
6842 * Stuff the last inserted text in the read buffer.
6843 * Last_insert actually is a copy of the redo buffer, so we
6844 * first have to remove the command.
6845 */
6846 int
6847stuff_inserted(c, count, no_esc)
6848 int c; /* Command character to be inserted */
6849 long count; /* Repeat this many times */
6850 int no_esc; /* Don't add an ESC at the end */
6851{
6852 char_u *esc_ptr;
6853 char_u *ptr;
6854 char_u *last_ptr;
6855 char_u last = NUL;
6856
6857 ptr = get_last_insert();
6858 if (ptr == NULL)
6859 {
6860 EMSG(_(e_noinstext));
6861 return FAIL;
6862 }
6863
6864 /* may want to stuff the command character, to start Insert mode */
6865 if (c != NUL)
6866 stuffcharReadbuff(c);
6867 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6868 *esc_ptr = NUL; /* remove the ESC */
6869
6870 /* when the last char is either "0" or "^" it will be quoted if no ESC
6871 * comes after it OR if it will inserted more than once and "ptr"
6872 * starts with ^D. -- Acevedo
6873 */
6874 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6875 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6876 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6877 {
6878 last = *last_ptr;
6879 *last_ptr = NUL;
6880 }
6881
6882 do
6883 {
6884 stuffReadbuff(ptr);
6885 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6886 if (last)
6887 stuffReadbuff((char_u *)(last == '0'
6888 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6889 : IF_EB("\026^", CTRL_V_STR "^")));
6890 }
6891 while (--count > 0);
6892
6893 if (last)
6894 *last_ptr = last;
6895
6896 if (esc_ptr != NULL)
6897 *esc_ptr = ESC; /* put the ESC back */
6898
6899 /* may want to stuff a trailing ESC, to get out of Insert mode */
6900 if (!no_esc)
6901 stuffcharReadbuff(ESC);
6902
6903 return OK;
6904}
6905
6906 char_u *
6907get_last_insert()
6908{
6909 if (last_insert == NULL)
6910 return NULL;
6911 return last_insert + last_insert_skip;
6912}
6913
6914/*
6915 * Get last inserted string, and remove trailing <Esc>.
6916 * Returns pointer to allocated memory (must be freed) or NULL.
6917 */
6918 char_u *
6919get_last_insert_save()
6920{
6921 char_u *s;
6922 int len;
6923
6924 if (last_insert == NULL)
6925 return NULL;
6926 s = vim_strsave(last_insert + last_insert_skip);
6927 if (s != NULL)
6928 {
6929 len = (int)STRLEN(s);
6930 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
6931 s[len - 1] = NUL;
6932 }
6933 return s;
6934}
6935
6936/*
6937 * Check the word in front of the cursor for an abbreviation.
6938 * Called when the non-id character "c" has been entered.
6939 * When an abbreviation is recognized it is removed from the text and
6940 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
6941 */
6942 static int
6943echeck_abbr(c)
6944 int c;
6945{
6946 /* Don't check for abbreviation in paste mode, when disabled and just
6947 * after moving around with cursor keys. */
6948 if (p_paste || no_abbr || arrow_used)
6949 return FALSE;
6950
6951 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
6952 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
6953}
6954
6955/*
6956 * replace-stack functions
6957 *
6958 * When replacing characters, the replaced characters are remembered for each
6959 * new character. This is used to re-insert the old text when backspacing.
6960 *
6961 * There is a NUL headed list of characters for each character that is
6962 * currently in the file after the insertion point. When BS is used, one NUL
6963 * headed list is put back for the deleted character.
6964 *
6965 * For a newline, there are two NUL headed lists. One contains the characters
6966 * that the NL replaced. The extra one stores the characters after the cursor
6967 * that were deleted (always white space).
6968 *
6969 * Replace_offset is normally 0, in which case replace_push will add a new
6970 * character at the end of the stack. If replace_offset is not 0, that many
6971 * characters will be left on the stack above the newly inserted character.
6972 */
6973
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00006974static char_u *replace_stack = NULL;
6975static long replace_stack_nr = 0; /* next entry in replace stack */
6976static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006977
6978 void
6979replace_push(c)
6980 int c; /* character that is replaced (NUL is none) */
6981{
6982 char_u *p;
6983
6984 if (replace_stack_nr < replace_offset) /* nothing to do */
6985 return;
6986 if (replace_stack_len <= replace_stack_nr)
6987 {
6988 replace_stack_len += 50;
6989 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
6990 if (p == NULL) /* out of memory */
6991 {
6992 replace_stack_len -= 50;
6993 return;
6994 }
6995 if (replace_stack != NULL)
6996 {
6997 mch_memmove(p, replace_stack,
6998 (size_t)(replace_stack_nr * sizeof(char_u)));
6999 vim_free(replace_stack);
7000 }
7001 replace_stack = p;
7002 }
7003 p = replace_stack + replace_stack_nr - replace_offset;
7004 if (replace_offset)
7005 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7006 *p = c;
7007 ++replace_stack_nr;
7008}
7009
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007010#if defined(FEAT_MBYTE) || defined(PROTO)
7011/*
7012 * Push a character onto the replace stack. Handles a multi-byte character in
7013 * reverse byte order, so that the first byte is popped off first.
7014 * Return the number of bytes done (includes composing characters).
7015 */
7016 int
7017replace_push_mb(p)
7018 char_u *p;
7019{
7020 int l = (*mb_ptr2len)(p);
7021 int j;
7022
7023 for (j = l - 1; j >= 0; --j)
7024 replace_push(p[j]);
7025 return l;
7026}
7027#endif
7028
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007029#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007030/*
7031 * call replace_push(c) with replace_offset set to the first NUL.
7032 */
7033 static void
7034replace_push_off(c)
7035 int c;
7036{
7037 char_u *p;
7038
7039 p = replace_stack + replace_stack_nr;
7040 for (replace_offset = 1; replace_offset < replace_stack_nr;
7041 ++replace_offset)
7042 if (*--p == NUL)
7043 break;
7044 replace_push(c);
7045 replace_offset = 0;
7046}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007047#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007048
7049/*
7050 * Pop one item from the replace stack.
7051 * return -1 if stack empty
7052 * return replaced character or NUL otherwise
7053 */
7054 static int
7055replace_pop()
7056{
7057 if (replace_stack_nr == 0)
7058 return -1;
7059 return (int)replace_stack[--replace_stack_nr];
7060}
7061
7062/*
7063 * Join the top two items on the replace stack. This removes to "off"'th NUL
7064 * encountered.
7065 */
7066 static void
7067replace_join(off)
7068 int off; /* offset for which NUL to remove */
7069{
7070 int i;
7071
7072 for (i = replace_stack_nr; --i >= 0; )
7073 if (replace_stack[i] == NUL && off-- <= 0)
7074 {
7075 --replace_stack_nr;
7076 mch_memmove(replace_stack + i, replace_stack + i + 1,
7077 (size_t)(replace_stack_nr - i));
7078 return;
7079 }
7080}
7081
7082/*
7083 * Pop bytes from the replace stack until a NUL is found, and insert them
7084 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7085 */
7086 static void
7087replace_pop_ins()
7088{
7089 int cc;
7090 int oldState = State;
7091
7092 State = NORMAL; /* don't want REPLACE here */
7093 while ((cc = replace_pop()) > 0)
7094 {
7095#ifdef FEAT_MBYTE
7096 mb_replace_pop_ins(cc);
7097#else
7098 ins_char(cc);
7099#endif
7100 dec_cursor();
7101 }
7102 State = oldState;
7103}
7104
7105#ifdef FEAT_MBYTE
7106/*
7107 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7108 * indicates a multi-byte char, pop the other bytes too.
7109 */
7110 static void
7111mb_replace_pop_ins(cc)
7112 int cc;
7113{
7114 int n;
7115 char_u buf[MB_MAXBYTES];
7116 int i;
7117 int c;
7118
7119 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7120 {
7121 buf[0] = cc;
7122 for (i = 1; i < n; ++i)
7123 buf[i] = replace_pop();
7124 ins_bytes_len(buf, n);
7125 }
7126 else
7127 ins_char(cc);
7128
7129 if (enc_utf8)
7130 /* Handle composing chars. */
7131 for (;;)
7132 {
7133 c = replace_pop();
7134 if (c == -1) /* stack empty */
7135 break;
7136 if ((n = MB_BYTE2LEN(c)) == 1)
7137 {
7138 /* Not a multi-byte char, put it back. */
7139 replace_push(c);
7140 break;
7141 }
7142 else
7143 {
7144 buf[0] = c;
7145 for (i = 1; i < n; ++i)
7146 buf[i] = replace_pop();
7147 if (utf_iscomposing(utf_ptr2char(buf)))
7148 ins_bytes_len(buf, n);
7149 else
7150 {
7151 /* Not a composing char, put it back. */
7152 for (i = n - 1; i >= 0; --i)
7153 replace_push(buf[i]);
7154 break;
7155 }
7156 }
7157 }
7158}
7159#endif
7160
7161/*
7162 * make the replace stack empty
7163 * (called when exiting replace mode)
7164 */
7165 static void
7166replace_flush()
7167{
7168 vim_free(replace_stack);
7169 replace_stack = NULL;
7170 replace_stack_len = 0;
7171 replace_stack_nr = 0;
7172}
7173
7174/*
7175 * Handle doing a BS for one character.
7176 * cc < 0: replace stack empty, just move cursor
7177 * cc == 0: character was inserted, delete it
7178 * cc > 0: character was replaced, put cc (first byte of original char) back
7179 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007180 * When "limit_col" is >= 0, don't delete before this column. Matters when
7181 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007182 */
7183 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007184replace_do_bs(limit_col)
7185 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007186{
7187 int cc;
7188#ifdef FEAT_VREPLACE
7189 int orig_len = 0;
7190 int ins_len;
7191 int orig_vcols = 0;
7192 colnr_T start_vcol;
7193 char_u *p;
7194 int i;
7195 int vcol;
7196#endif
7197
7198 cc = replace_pop();
7199 if (cc > 0)
7200 {
7201#ifdef FEAT_VREPLACE
7202 if (State & VREPLACE_FLAG)
7203 {
7204 /* Get the number of screen cells used by the character we are
7205 * going to delete. */
7206 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7207 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7208 }
7209#endif
7210#ifdef FEAT_MBYTE
7211 if (has_mbyte)
7212 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007213 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007214# ifdef FEAT_VREPLACE
7215 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007216 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007217# endif
7218 replace_push(cc);
7219 }
7220 else
7221#endif
7222 {
7223 pchar_cursor(cc);
7224#ifdef FEAT_VREPLACE
7225 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007226 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007227#endif
7228 }
7229 replace_pop_ins();
7230
7231#ifdef FEAT_VREPLACE
7232 if (State & VREPLACE_FLAG)
7233 {
7234 /* Get the number of screen cells used by the inserted characters */
7235 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007236 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007237 vcol = start_vcol;
7238 for (i = 0; i < ins_len; ++i)
7239 {
7240 vcol += chartabsize(p + i, vcol);
7241#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007242 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007243#endif
7244 }
7245 vcol -= start_vcol;
7246
7247 /* Delete spaces that were inserted after the cursor to keep the
7248 * text aligned. */
7249 curwin->w_cursor.col += ins_len;
7250 while (vcol > orig_vcols && gchar_cursor() == ' ')
7251 {
7252 del_char(FALSE);
7253 ++orig_vcols;
7254 }
7255 curwin->w_cursor.col -= ins_len;
7256 }
7257#endif
7258
7259 /* mark the buffer as changed and prepare for displaying */
7260 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7261 }
7262 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007263 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007264}
7265
7266#ifdef FEAT_CINDENT
7267/*
7268 * Return TRUE if C-indenting is on.
7269 */
7270 static int
7271cindent_on()
7272{
7273 return (!p_paste && (curbuf->b_p_cin
7274# ifdef FEAT_EVAL
7275 || *curbuf->b_p_inde != NUL
7276# endif
7277 ));
7278}
7279#endif
7280
7281#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7282/*
7283 * Re-indent the current line, based on the current contents of it and the
7284 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7285 * confused what all the part that handles Control-T is doing that I'm not.
7286 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7287 */
7288
7289 void
7290fixthisline(get_the_indent)
7291 int (*get_the_indent) __ARGS((void));
7292{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007293 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007294 if (linewhite(curwin->w_cursor.lnum))
7295 did_ai = TRUE; /* delete the indent if the line stays empty */
7296}
7297
7298 void
7299fix_indent()
7300{
7301 if (p_paste)
7302 return;
7303# ifdef FEAT_LISP
7304 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7305 fixthisline(get_lisp_indent);
7306# endif
7307# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7308 else
7309# endif
7310# ifdef FEAT_CINDENT
7311 if (cindent_on())
7312 do_c_expr_indent();
7313# endif
7314}
7315
7316#endif
7317
7318#ifdef FEAT_CINDENT
7319/*
7320 * return TRUE if 'cinkeys' contains the key "keytyped",
7321 * when == '*': Only if key is preceded with '*' (indent before insert)
7322 * when == '!': Only if key is prededed with '!' (don't insert)
7323 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7324 *
7325 * "keytyped" can have a few special values:
7326 * KEY_OPEN_FORW
7327 * KEY_OPEN_BACK
7328 * KEY_COMPLETE just finished completion.
7329 *
7330 * If line_is_empty is TRUE accept keys with '0' before them.
7331 */
7332 int
7333in_cinkeys(keytyped, when, line_is_empty)
7334 int keytyped;
7335 int when;
7336 int line_is_empty;
7337{
7338 char_u *look;
7339 int try_match;
7340 int try_match_word;
7341 char_u *p;
7342 char_u *line;
7343 int icase;
7344 int i;
7345
7346#ifdef FEAT_EVAL
7347 if (*curbuf->b_p_inde != NUL)
7348 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7349 else
7350#endif
7351 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7352 while (*look)
7353 {
7354 /*
7355 * Find out if we want to try a match with this key, depending on
7356 * 'when' and a '*' or '!' before the key.
7357 */
7358 switch (when)
7359 {
7360 case '*': try_match = (*look == '*'); break;
7361 case '!': try_match = (*look == '!'); break;
7362 default: try_match = (*look != '*'); break;
7363 }
7364 if (*look == '*' || *look == '!')
7365 ++look;
7366
7367 /*
7368 * If there is a '0', only accept a match if the line is empty.
7369 * But may still match when typing last char of a word.
7370 */
7371 if (*look == '0')
7372 {
7373 try_match_word = try_match;
7374 if (!line_is_empty)
7375 try_match = FALSE;
7376 ++look;
7377 }
7378 else
7379 try_match_word = FALSE;
7380
7381 /*
7382 * does it look like a control character?
7383 */
7384 if (*look == '^'
7385#ifdef EBCDIC
7386 && (Ctrl_chr(look[1]) != 0)
7387#else
7388 && look[1] >= '?' && look[1] <= '_'
7389#endif
7390 )
7391 {
7392 if (try_match && keytyped == Ctrl_chr(look[1]))
7393 return TRUE;
7394 look += 2;
7395 }
7396 /*
7397 * 'o' means "o" command, open forward.
7398 * 'O' means "O" command, open backward.
7399 */
7400 else if (*look == 'o')
7401 {
7402 if (try_match && keytyped == KEY_OPEN_FORW)
7403 return TRUE;
7404 ++look;
7405 }
7406 else if (*look == 'O')
7407 {
7408 if (try_match && keytyped == KEY_OPEN_BACK)
7409 return TRUE;
7410 ++look;
7411 }
7412
7413 /*
7414 * 'e' means to check for "else" at start of line and just before the
7415 * cursor.
7416 */
7417 else if (*look == 'e')
7418 {
7419 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7420 {
7421 p = ml_get_curline();
7422 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7423 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7424 return TRUE;
7425 }
7426 ++look;
7427 }
7428
7429 /*
7430 * ':' only causes an indent if it is at the end of a label or case
7431 * statement, or when it was before typing the ':' (to fix
7432 * class::method for C++).
7433 */
7434 else if (*look == ':')
7435 {
7436 if (try_match && keytyped == ':')
7437 {
7438 p = ml_get_curline();
7439 if (cin_iscase(p) || cin_isscopedecl(p) || cin_islabel(30))
7440 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007441 /* Need to get the line again after cin_islabel(). */
7442 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007443 if (curwin->w_cursor.col > 2
7444 && p[curwin->w_cursor.col - 1] == ':'
7445 && p[curwin->w_cursor.col - 2] == ':')
7446 {
7447 p[curwin->w_cursor.col - 1] = ' ';
7448 i = (cin_iscase(p) || cin_isscopedecl(p)
7449 || cin_islabel(30));
7450 p = ml_get_curline();
7451 p[curwin->w_cursor.col - 1] = ':';
7452 if (i)
7453 return TRUE;
7454 }
7455 }
7456 ++look;
7457 }
7458
7459
7460 /*
7461 * Is it a key in <>, maybe?
7462 */
7463 else if (*look == '<')
7464 {
7465 if (try_match)
7466 {
7467 /*
7468 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7469 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7470 * >, *, : and ! keys if they really really want to.
7471 */
7472 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7473 && keytyped == look[1])
7474 return TRUE;
7475
7476 if (keytyped == get_special_key_code(look + 1))
7477 return TRUE;
7478 }
7479 while (*look && *look != '>')
7480 look++;
7481 while (*look == '>')
7482 look++;
7483 }
7484
7485 /*
7486 * Is it a word: "=word"?
7487 */
7488 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7489 {
7490 ++look;
7491 if (*look == '~')
7492 {
7493 icase = TRUE;
7494 ++look;
7495 }
7496 else
7497 icase = FALSE;
7498 p = vim_strchr(look, ',');
7499 if (p == NULL)
7500 p = look + STRLEN(look);
7501 if ((try_match || try_match_word)
7502 && curwin->w_cursor.col >= (colnr_T)(p - look))
7503 {
7504 int match = FALSE;
7505
7506#ifdef FEAT_INS_EXPAND
7507 if (keytyped == KEY_COMPLETE)
7508 {
7509 char_u *s;
7510
7511 /* Just completed a word, check if it starts with "look".
7512 * search back for the start of a word. */
7513 line = ml_get_curline();
7514# ifdef FEAT_MBYTE
7515 if (has_mbyte)
7516 {
7517 char_u *n;
7518
7519 for (s = line + curwin->w_cursor.col; s > line; s = n)
7520 {
7521 n = mb_prevptr(line, s);
7522 if (!vim_iswordp(n))
7523 break;
7524 }
7525 }
7526 else
7527# endif
7528 for (s = line + curwin->w_cursor.col; s > line; --s)
7529 if (!vim_iswordc(s[-1]))
7530 break;
7531 if (s + (p - look) <= line + curwin->w_cursor.col
7532 && (icase
7533 ? MB_STRNICMP(s, look, p - look)
7534 : STRNCMP(s, look, p - look)) == 0)
7535 match = TRUE;
7536 }
7537 else
7538#endif
7539 /* TODO: multi-byte */
7540 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7541 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7542 {
7543 line = ml_get_cursor();
7544 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7545 || !vim_iswordc(line[-(p - look) - 1]))
7546 && (icase
7547 ? MB_STRNICMP(line - (p - look), look, p - look)
7548 : STRNCMP(line - (p - look), look, p - look))
7549 == 0)
7550 match = TRUE;
7551 }
7552 if (match && try_match_word && !try_match)
7553 {
7554 /* "0=word": Check if there are only blanks before the
7555 * word. */
7556 line = ml_get_curline();
7557 if ((int)(skipwhite(line) - line) !=
7558 (int)(curwin->w_cursor.col - (p - look)))
7559 match = FALSE;
7560 }
7561 if (match)
7562 return TRUE;
7563 }
7564 look = p;
7565 }
7566
7567 /*
7568 * ok, it's a boring generic character.
7569 */
7570 else
7571 {
7572 if (try_match && *look == keytyped)
7573 return TRUE;
7574 ++look;
7575 }
7576
7577 /*
7578 * Skip over ", ".
7579 */
7580 look = skip_to_option_part(look);
7581 }
7582 return FALSE;
7583}
7584#endif /* FEAT_CINDENT */
7585
7586#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7587/*
7588 * Map Hebrew keyboard when in hkmap mode.
7589 */
7590 int
7591hkmap(c)
7592 int c;
7593{
7594 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7595 {
7596 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7597 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7598 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7599 static char_u map[26] =
7600 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7601 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7602 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7603 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7604 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7605 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7606 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7607 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7608 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7609
7610 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7611 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7612 /* '-1'='sofit' */
7613 else if (c == 'x')
7614 return 'X';
7615 else if (c == 'q')
7616 return '\''; /* {geresh}={'} */
7617 else if (c == 246)
7618 return ' '; /* \"o --> ' ' for a german keyboard */
7619 else if (c == 228)
7620 return ' '; /* \"a --> ' ' -- / -- */
7621 else if (c == 252)
7622 return ' '; /* \"u --> ' ' -- / -- */
7623#ifdef EBCDIC
7624 else if (islower(c))
7625#else
7626 /* NOTE: islower() does not do the right thing for us on Linux so we
7627 * do this the same was as 5.7 and previous, so it works correctly on
7628 * all systems. Specifically, the e.g. Delete and Arrow keys are
7629 * munged and won't work if e.g. searching for Hebrew text.
7630 */
7631 else if (c >= 'a' && c <= 'z')
7632#endif
7633 return (int)(map[CharOrdLow(c)] + p_aleph);
7634 else
7635 return c;
7636 }
7637 else
7638 {
7639 switch (c)
7640 {
7641 case '`': return ';';
7642 case '/': return '.';
7643 case '\'': return ',';
7644 case 'q': return '/';
7645 case 'w': return '\'';
7646
7647 /* Hebrew letters - set offset from 'a' */
7648 case ',': c = '{'; break;
7649 case '.': c = 'v'; break;
7650 case ';': c = 't'; break;
7651 default: {
7652 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7653
7654#ifdef EBCDIC
7655 /* see note about islower() above */
7656 if (!islower(c))
7657#else
7658 if (c < 'a' || c > 'z')
7659#endif
7660 return c;
7661 c = str[CharOrdLow(c)];
7662 break;
7663 }
7664 }
7665
7666 return (int)(CharOrdLow(c) + p_aleph);
7667 }
7668}
7669#endif
7670
7671 static void
7672ins_reg()
7673{
7674 int need_redraw = FALSE;
7675 int regname;
7676 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007677#ifdef FEAT_VISUAL
7678 int vis_active = VIsual_active;
7679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007680
7681 /*
7682 * If we are going to wait for a character, show a '"'.
7683 */
7684 pc_status = PC_STATUS_UNSET;
7685 if (redrawing() && !char_avail())
7686 {
7687 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007688 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007689
7690 edit_putchar('"', TRUE);
7691#ifdef FEAT_CMDL_INFO
7692 add_to_showcmd_c(Ctrl_R);
7693#endif
7694 }
7695
7696#ifdef USE_ON_FLY_SCROLL
7697 dont_scroll = TRUE; /* disallow scrolling here */
7698#endif
7699
7700 /*
7701 * Don't map the register name. This also prevents the mode message to be
7702 * deleted when ESC is hit.
7703 */
7704 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007705 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007706 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007707 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7708 {
7709 /* Get a third key for literal register insertion */
7710 literally = regname;
7711#ifdef FEAT_CMDL_INFO
7712 add_to_showcmd_c(literally);
7713#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007714 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007715 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007716 }
7717 --no_mapping;
7718
7719#ifdef FEAT_EVAL
7720 /*
7721 * Don't call u_sync() while getting the expression,
7722 * evaluating it or giving an error message for it!
7723 */
7724 ++no_u_sync;
7725 if (regname == '=')
7726 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007727# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007728 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007729# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007730 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007731# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007732 /* Restore the Input Method. */
7733 if (im_on)
7734 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007735# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007736 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007737 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7738 {
7739 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007740 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007742 else
7743 {
7744#endif
7745 if (literally == Ctrl_O || literally == Ctrl_P)
7746 {
7747 /* Append the command to the redo buffer. */
7748 AppendCharToRedobuff(Ctrl_R);
7749 AppendCharToRedobuff(literally);
7750 AppendCharToRedobuff(regname);
7751
7752 do_put(regname, BACKWARD, 1L,
7753 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7754 }
7755 else if (insert_reg(regname, literally) == FAIL)
7756 {
7757 vim_beep();
7758 need_redraw = TRUE; /* remove the '"' */
7759 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007760 else if (stop_insert_mode)
7761 /* When the '=' register was used and a function was invoked that
7762 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7763 * insert anything, need to remove the '"' */
7764 need_redraw = TRUE;
7765
Bram Moolenaar071d4272004-06-13 20:20:40 +00007766#ifdef FEAT_EVAL
7767 }
7768 --no_u_sync;
7769#endif
7770#ifdef FEAT_CMDL_INFO
7771 clear_showcmd();
7772#endif
7773
7774 /* If the inserted register is empty, we need to remove the '"' */
7775 if (need_redraw || stuff_empty())
7776 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007777
7778#ifdef FEAT_VISUAL
7779 /* Disallow starting Visual mode here, would get a weird mode. */
7780 if (!vis_active && VIsual_active)
7781 end_visual_mode();
7782#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007783}
7784
7785/*
7786 * CTRL-G commands in Insert mode.
7787 */
7788 static void
7789ins_ctrl_g()
7790{
7791 int c;
7792
7793#ifdef FEAT_INS_EXPAND
7794 /* Right after CTRL-X the cursor will be after the ruler. */
7795 setcursor();
7796#endif
7797
7798 /*
7799 * Don't map the second key. This also prevents the mode message to be
7800 * deleted when ESC is hit.
7801 */
7802 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007803 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007804 --no_mapping;
7805 switch (c)
7806 {
7807 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7808 case K_UP:
7809 case Ctrl_K:
7810 case 'k': ins_up(TRUE);
7811 break;
7812
7813 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7814 case K_DOWN:
7815 case Ctrl_J:
7816 case 'j': ins_down(TRUE);
7817 break;
7818
7819 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007820 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007821 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007822
7823 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007824 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007825 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007826 break;
7827
7828 /* Unknown CTRL-G command, reserved for future expansion. */
7829 default: vim_beep();
7830 }
7831}
7832
7833/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007834 * CTRL-^ in Insert mode.
7835 */
7836 static void
7837ins_ctrl_hat()
7838{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007839 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007840 {
7841 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7842 if (State & LANGMAP)
7843 {
7844 curbuf->b_p_iminsert = B_IMODE_NONE;
7845 State &= ~LANGMAP;
7846 }
7847 else
7848 {
7849 curbuf->b_p_iminsert = B_IMODE_LMAP;
7850 State |= LANGMAP;
7851#ifdef USE_IM_CONTROL
7852 im_set_active(FALSE);
7853#endif
7854 }
7855 }
7856#ifdef USE_IM_CONTROL
7857 else
7858 {
7859 /* There are no ":lmap" mappings, toggle IM */
7860 if (im_get_status())
7861 {
7862 curbuf->b_p_iminsert = B_IMODE_NONE;
7863 im_set_active(FALSE);
7864 }
7865 else
7866 {
7867 curbuf->b_p_iminsert = B_IMODE_IM;
7868 State &= ~LANGMAP;
7869 im_set_active(TRUE);
7870 }
7871 }
7872#endif
7873 set_iminsert_global();
7874 showmode();
7875#ifdef FEAT_GUI
7876 /* may show different cursor shape or color */
7877 if (gui.in_use)
7878 gui_update_cursor(TRUE, FALSE);
7879#endif
7880#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7881 /* Show/unshow value of 'keymap' in status lines. */
7882 status_redraw_curbuf();
7883#endif
7884}
7885
7886/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007887 * Handle ESC in insert mode.
7888 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
7889 * insert.
7890 */
7891 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00007892ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007893 long *count;
7894 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00007895 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007896{
7897 int temp;
7898 static int disabled_redraw = FALSE;
7899
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00007900#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007901 check_spell_redraw();
7902#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007903#if defined(FEAT_HANGULIN)
7904# if defined(ESC_CHG_TO_ENG_MODE)
7905 hangul_input_state_set(0);
7906# endif
7907 if (composing_hangul)
7908 {
7909 push_raw_key(composing_hangul_buffer, 2);
7910 composing_hangul = 0;
7911 }
7912#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007913
7914 temp = curwin->w_cursor.col;
7915 if (disabled_redraw)
7916 {
7917 --RedrawingDisabled;
7918 disabled_redraw = FALSE;
7919 }
7920 if (!arrow_used)
7921 {
7922 /*
7923 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00007924 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
7925 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007926 */
7927 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00007928 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007929
7930 /*
7931 * Repeating insert may take a long time. Check for
7932 * interrupt now and then.
7933 */
7934 if (*count > 0)
7935 {
7936 line_breakcheck();
7937 if (got_int)
7938 *count = 0;
7939 }
7940
7941 if (--*count > 0) /* repeat what was typed */
7942 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00007943 /* Vi repeats the insert without replacing characters. */
7944 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
7945 State &= ~REPLACE_FLAG;
7946
Bram Moolenaar071d4272004-06-13 20:20:40 +00007947 (void)start_redo_ins();
7948 if (cmdchar == 'r' || cmdchar == 'v')
7949 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
7950 ++RedrawingDisabled;
7951 disabled_redraw = TRUE;
7952 return FALSE; /* repeat the insert */
7953 }
7954 stop_insert(&curwin->w_cursor, TRUE);
7955 undisplay_dollar();
7956 }
7957
7958 /* When an autoindent was removed, curswant stays after the
7959 * indent */
7960 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
7961 curwin->w_set_curswant = TRUE;
7962
7963 /* Remember the last Insert position in the '^ mark. */
7964 if (!cmdmod.keepjumps)
7965 curbuf->b_last_insert = curwin->w_cursor;
7966
7967 /*
7968 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00007969 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00007970 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00007971 if (!nomove
7972 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007973#ifdef FEAT_VIRTUALEDIT
7974 || curwin->w_cursor.coladd > 0
7975#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007976 )
7977 && (restart_edit == NUL
7978 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007979#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00007980 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00007981#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00007982 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007983#ifdef FEAT_RIGHTLEFT
7984 && !revins_on
7985#endif
7986 )
7987 {
7988#ifdef FEAT_VIRTUALEDIT
7989 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
7990 {
7991 oneleft();
7992 if (restart_edit != NUL)
7993 ++curwin->w_cursor.coladd;
7994 }
7995 else
7996#endif
7997 {
7998 --curwin->w_cursor.col;
7999#ifdef FEAT_MBYTE
8000 /* Correct cursor for multi-byte character. */
8001 if (has_mbyte)
8002 mb_adjust_cursor();
8003#endif
8004 }
8005 }
8006
8007#ifdef USE_IM_CONTROL
8008 /* Disable IM to allow typing English directly for Normal mode commands.
8009 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8010 * well). */
8011 if (!(State & LANGMAP))
8012 im_save_status(&curbuf->b_p_iminsert);
8013 im_set_active(FALSE);
8014#endif
8015
8016 State = NORMAL;
8017 /* need to position cursor again (e.g. when on a TAB ) */
8018 changed_cline_bef_curs();
8019
8020#ifdef FEAT_MOUSE
8021 setmouse();
8022#endif
8023#ifdef CURSOR_SHAPE
8024 ui_cursor_shape(); /* may show different cursor shape */
8025#endif
8026
8027 /*
8028 * When recording or for CTRL-O, need to display the new mode.
8029 * Otherwise remove the mode message.
8030 */
8031 if (Recording || restart_edit != NUL)
8032 showmode();
8033 else if (p_smd)
8034 MSG("");
8035
8036 return TRUE; /* exit Insert mode */
8037}
8038
8039#ifdef FEAT_RIGHTLEFT
8040/*
8041 * Toggle language: hkmap and revins_on.
8042 * Move to end of reverse inserted text.
8043 */
8044 static void
8045ins_ctrl_()
8046{
8047 if (revins_on && revins_chars && revins_scol >= 0)
8048 {
8049 while (gchar_cursor() != NUL && revins_chars--)
8050 ++curwin->w_cursor.col;
8051 }
8052 p_ri = !p_ri;
8053 revins_on = (State == INSERT && p_ri);
8054 if (revins_on)
8055 {
8056 revins_scol = curwin->w_cursor.col;
8057 revins_legal++;
8058 revins_chars = 0;
8059 undisplay_dollar();
8060 }
8061 else
8062 revins_scol = -1;
8063#ifdef FEAT_FKMAP
8064 if (p_altkeymap)
8065 {
8066 /*
8067 * to be consistent also for redo command, using '.'
8068 * set arrow_used to true and stop it - causing to redo
8069 * characters entered in one mode (normal/reverse insert).
8070 */
8071 arrow_used = TRUE;
8072 (void)stop_arrow();
8073 p_fkmap = curwin->w_p_rl ^ p_ri;
8074 if (p_fkmap && p_ri)
8075 State = INSERT;
8076 }
8077 else
8078#endif
8079 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8080 showmode();
8081}
8082#endif
8083
8084#ifdef FEAT_VISUAL
8085/*
8086 * If 'keymodel' contains "startsel", may start selection.
8087 * Returns TRUE when a CTRL-O and other keys stuffed.
8088 */
8089 static int
8090ins_start_select(c)
8091 int c;
8092{
8093 if (km_startsel)
8094 switch (c)
8095 {
8096 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 case K_PAGEUP:
8099 case K_KPAGEUP:
8100 case K_PAGEDOWN:
8101 case K_KPAGEDOWN:
8102# ifdef MACOS
8103 case K_LEFT:
8104 case K_RIGHT:
8105 case K_UP:
8106 case K_DOWN:
8107 case K_END:
8108 case K_HOME:
8109# endif
8110 if (!(mod_mask & MOD_MASK_SHIFT))
8111 break;
8112 /* FALLTHROUGH */
8113 case K_S_LEFT:
8114 case K_S_RIGHT:
8115 case K_S_UP:
8116 case K_S_DOWN:
8117 case K_S_END:
8118 case K_S_HOME:
8119 /* Start selection right away, the cursor can move with
8120 * CTRL-O when beyond the end of the line. */
8121 start_selection();
8122
8123 /* Execute the key in (insert) Select mode. */
8124 stuffcharReadbuff(Ctrl_O);
8125 if (mod_mask)
8126 {
8127 char_u buf[4];
8128
8129 buf[0] = K_SPECIAL;
8130 buf[1] = KS_MODIFIER;
8131 buf[2] = mod_mask;
8132 buf[3] = NUL;
8133 stuffReadbuff(buf);
8134 }
8135 stuffcharReadbuff(c);
8136 return TRUE;
8137 }
8138 return FALSE;
8139}
8140#endif
8141
8142/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008143 * <Insert> key in Insert mode: toggle insert/remplace mode.
8144 */
8145 static void
8146ins_insert(replaceState)
8147 int replaceState;
8148{
8149#ifdef FEAT_FKMAP
8150 if (p_fkmap && p_ri)
8151 {
8152 beep_flush();
8153 EMSG(farsi_text_3); /* encoded in Farsi */
8154 return;
8155 }
8156#endif
8157
8158#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008159# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008160 set_vim_var_string(VV_INSERTMODE,
8161 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008162# ifdef FEAT_VREPLACE
8163 replaceState == VREPLACE ? "v" :
8164# endif
8165 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008166# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008167 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8168#endif
8169 if (State & REPLACE_FLAG)
8170 State = INSERT | (State & LANGMAP);
8171 else
8172 State = replaceState | (State & LANGMAP);
8173 AppendCharToRedobuff(K_INS);
8174 showmode();
8175#ifdef CURSOR_SHAPE
8176 ui_cursor_shape(); /* may show different cursor shape */
8177#endif
8178}
8179
8180/*
8181 * Pressed CTRL-O in Insert mode.
8182 */
8183 static void
8184ins_ctrl_o()
8185{
8186#ifdef FEAT_VREPLACE
8187 if (State & VREPLACE_FLAG)
8188 restart_edit = 'V';
8189 else
8190#endif
8191 if (State & REPLACE_FLAG)
8192 restart_edit = 'R';
8193 else
8194 restart_edit = 'I';
8195#ifdef FEAT_VIRTUALEDIT
8196 if (virtual_active())
8197 ins_at_eol = FALSE; /* cursor always keeps its column */
8198 else
8199#endif
8200 ins_at_eol = (gchar_cursor() == NUL);
8201}
8202
8203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 * If the cursor is on an indent, ^T/^D insert/delete one
8205 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008206 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008207 * with vi. But vi only supports ^T and ^D after an
8208 * autoindent, we support it everywhere.
8209 */
8210 static void
8211ins_shift(c, lastc)
8212 int c;
8213 int lastc;
8214{
8215 if (stop_arrow() == FAIL)
8216 return;
8217 AppendCharToRedobuff(c);
8218
8219 /*
8220 * 0^D and ^^D: remove all indent.
8221 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008222 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8223 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008224 {
8225 --curwin->w_cursor.col;
8226 (void)del_char(FALSE); /* delete the '^' or '0' */
8227 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8228 if (State & REPLACE_FLAG)
8229 replace_pop_ins();
8230 if (lastc == '^')
8231 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008232 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008233 }
8234 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008235 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236
8237 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8238 did_ai = FALSE;
8239#ifdef FEAT_SMARTINDENT
8240 did_si = FALSE;
8241 can_si = FALSE;
8242 can_si_back = FALSE;
8243#endif
8244#ifdef FEAT_CINDENT
8245 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8246#endif
8247}
8248
8249 static void
8250ins_del()
8251{
8252 int temp;
8253
8254 if (stop_arrow() == FAIL)
8255 return;
8256 if (gchar_cursor() == NUL) /* delete newline */
8257 {
8258 temp = curwin->w_cursor.col;
8259 if (!can_bs(BS_EOL) /* only if "eol" included */
8260 || u_save((linenr_T)(curwin->w_cursor.lnum - 1),
8261 (linenr_T)(curwin->w_cursor.lnum + 2)) == FAIL
8262 || do_join(FALSE) == FAIL)
8263 vim_beep();
8264 else
8265 curwin->w_cursor.col = temp;
8266 }
8267 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8268 vim_beep();
8269 did_ai = FALSE;
8270#ifdef FEAT_SMARTINDENT
8271 did_si = FALSE;
8272 can_si = FALSE;
8273 can_si_back = FALSE;
8274#endif
8275 AppendCharToRedobuff(K_DEL);
8276}
8277
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008278static void ins_bs_one __ARGS((colnr_T *vcolp));
8279
8280/*
8281 * Delete one character for ins_bs().
8282 */
8283 static void
8284ins_bs_one(vcolp)
8285 colnr_T *vcolp;
8286{
8287 dec_cursor();
8288 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8289 if (State & REPLACE_FLAG)
8290 {
8291 /* Don't delete characters before the insert point when in
8292 * Replace mode */
8293 if (curwin->w_cursor.lnum != Insstart.lnum
8294 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008295 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008296 }
8297 else
8298 (void)del_char(FALSE);
8299}
8300
Bram Moolenaar071d4272004-06-13 20:20:40 +00008301/*
8302 * Handle Backspace, delete-word and delete-line in Insert mode.
8303 * Return TRUE when backspace was actually used.
8304 */
8305 static int
8306ins_bs(c, mode, inserted_space_p)
8307 int c;
8308 int mode;
8309 int *inserted_space_p;
8310{
8311 linenr_T lnum;
8312 int cc;
8313 int temp = 0; /* init for GCC */
8314 colnr_T mincol;
8315 int did_backspace = FALSE;
8316 int in_indent;
8317 int oldState;
8318#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008319 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320#endif
8321
8322 /*
8323 * can't delete anything in an empty file
8324 * can't backup past first character in buffer
8325 * can't backup past starting point unless 'backspace' > 1
8326 * can backup to a previous line if 'backspace' == 0
8327 */
8328 if ( bufempty()
8329 || (
8330#ifdef FEAT_RIGHTLEFT
8331 !revins_on &&
8332#endif
8333 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8334 || (!can_bs(BS_START)
8335 && (arrow_used
8336 || (curwin->w_cursor.lnum == Insstart.lnum
8337 && curwin->w_cursor.col <= Insstart.col)))
8338 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8339 && curwin->w_cursor.col <= ai_col)
8340 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8341 {
8342 vim_beep();
8343 return FALSE;
8344 }
8345
8346 if (stop_arrow() == FAIL)
8347 return FALSE;
8348 in_indent = inindent(0);
8349#ifdef FEAT_CINDENT
8350 if (in_indent)
8351 can_cindent = FALSE;
8352#endif
8353#ifdef FEAT_COMMENTS
8354 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8355#endif
8356#ifdef FEAT_RIGHTLEFT
8357 if (revins_on) /* put cursor after last inserted char */
8358 inc_cursor();
8359#endif
8360
8361#ifdef FEAT_VIRTUALEDIT
8362 /* Virtualedit:
8363 * BACKSPACE_CHAR eats a virtual space
8364 * BACKSPACE_WORD eats all coladd
8365 * BACKSPACE_LINE eats all coladd and keeps going
8366 */
8367 if (curwin->w_cursor.coladd > 0)
8368 {
8369 if (mode == BACKSPACE_CHAR)
8370 {
8371 --curwin->w_cursor.coladd;
8372 return TRUE;
8373 }
8374 if (mode == BACKSPACE_WORD)
8375 {
8376 curwin->w_cursor.coladd = 0;
8377 return TRUE;
8378 }
8379 curwin->w_cursor.coladd = 0;
8380 }
8381#endif
8382
8383 /*
8384 * delete newline!
8385 */
8386 if (curwin->w_cursor.col == 0)
8387 {
8388 lnum = Insstart.lnum;
8389 if (curwin->w_cursor.lnum == Insstart.lnum
8390#ifdef FEAT_RIGHTLEFT
8391 || revins_on
8392#endif
8393 )
8394 {
8395 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8396 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8397 return FALSE;
8398 --Insstart.lnum;
8399 Insstart.col = MAXCOL;
8400 }
8401 /*
8402 * In replace mode:
8403 * cc < 0: NL was inserted, delete it
8404 * cc >= 0: NL was replaced, put original characters back
8405 */
8406 cc = -1;
8407 if (State & REPLACE_FLAG)
8408 cc = replace_pop(); /* returns -1 if NL was inserted */
8409 /*
8410 * In replace mode, in the line we started replacing, we only move the
8411 * cursor.
8412 */
8413 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8414 {
8415 dec_cursor();
8416 }
8417 else
8418 {
8419#ifdef FEAT_VREPLACE
8420 if (!(State & VREPLACE_FLAG)
8421 || curwin->w_cursor.lnum > orig_line_count)
8422#endif
8423 {
8424 temp = gchar_cursor(); /* remember current char */
8425 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008426
8427 /* When "aw" is in 'formatoptions' we must delete the space at
8428 * the end of the line, otherwise the line will be broken
8429 * again when auto-formatting. */
8430 if (has_format_option(FO_AUTO)
8431 && has_format_option(FO_WHITE_PAR))
8432 {
8433 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8434 TRUE);
8435 int len;
8436
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008437 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008438 if (len > 0 && ptr[len - 1] == ' ')
8439 ptr[len - 1] = NUL;
8440 }
8441
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 (void)do_join(FALSE);
8443 if (temp == NUL && gchar_cursor() != NUL)
8444 inc_cursor();
8445 }
8446#ifdef FEAT_VREPLACE
8447 else
8448 dec_cursor();
8449#endif
8450
8451 /*
8452 * In REPLACE mode we have to put back the text that was replaced
8453 * by the NL. On the replace stack is first a NUL-terminated
8454 * sequence of characters that were deleted and then the
8455 * characters that NL replaced.
8456 */
8457 if (State & REPLACE_FLAG)
8458 {
8459 /*
8460 * Do the next ins_char() in NORMAL state, to
8461 * prevent ins_char() from replacing characters and
8462 * avoiding showmatch().
8463 */
8464 oldState = State;
8465 State = NORMAL;
8466 /*
8467 * restore characters (blanks) deleted after cursor
8468 */
8469 while (cc > 0)
8470 {
8471 temp = curwin->w_cursor.col;
8472#ifdef FEAT_MBYTE
8473 mb_replace_pop_ins(cc);
8474#else
8475 ins_char(cc);
8476#endif
8477 curwin->w_cursor.col = temp;
8478 cc = replace_pop();
8479 }
8480 /* restore the characters that NL replaced */
8481 replace_pop_ins();
8482 State = oldState;
8483 }
8484 }
8485 did_ai = FALSE;
8486 }
8487 else
8488 {
8489 /*
8490 * Delete character(s) before the cursor.
8491 */
8492#ifdef FEAT_RIGHTLEFT
8493 if (revins_on) /* put cursor on last inserted char */
8494 dec_cursor();
8495#endif
8496 mincol = 0;
8497 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008498 if (mode == BACKSPACE_LINE
8499 && (curbuf->b_p_ai
8500#ifdef FEAT_CINDENT
8501 || cindent_on()
8502#endif
8503 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008504#ifdef FEAT_RIGHTLEFT
8505 && !revins_on
8506#endif
8507 )
8508 {
8509 temp = curwin->w_cursor.col;
8510 beginline(BL_WHITE);
8511 if (curwin->w_cursor.col < (colnr_T)temp)
8512 mincol = curwin->w_cursor.col;
8513 curwin->w_cursor.col = temp;
8514 }
8515
8516 /*
8517 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8518 */
8519 if ( mode == BACKSPACE_CHAR
8520 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008521 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008522 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 && (*(ml_get_cursor() - 1) == TAB
8524 || (*(ml_get_cursor() - 1) == ' '
8525 && (!*inserted_space_p
8526 || arrow_used))))))
8527 {
8528 int ts;
8529 colnr_T vcol;
8530 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008531 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008532
8533 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008534 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 ts = curbuf->b_p_sw;
8536 else
8537 ts = curbuf->b_p_sts;
8538 /* Compute the virtual column where we want to be. Since
8539 * 'showbreak' may get in the way, need to get the last column of
8540 * the previous character. */
8541 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008542 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 dec_cursor();
8544 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8545 inc_cursor();
8546 want_vcol = (want_vcol / ts) * ts;
8547
8548 /* delete characters until we are at or before want_vcol */
8549 while (vcol > want_vcol
8550 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008551 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008552
8553 /* insert extra spaces until we are at want_vcol */
8554 while (vcol < want_vcol)
8555 {
8556 /* Remember the first char we inserted */
8557 if (curwin->w_cursor.lnum == Insstart.lnum
8558 && curwin->w_cursor.col < Insstart.col)
8559 Insstart.col = curwin->w_cursor.col;
8560
8561#ifdef FEAT_VREPLACE
8562 if (State & VREPLACE_FLAG)
8563 ins_char(' ');
8564 else
8565#endif
8566 {
8567 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008568 if ((State & REPLACE_FLAG))
8569 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008570 }
8571 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8572 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008573
8574 /* If we are now back where we started delete one character. Can
8575 * happen when using 'sts' and 'linebreak'. */
8576 if (vcol >= start_vcol)
8577 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 }
8579
8580 /*
8581 * Delete upto starting point, start of line or previous word.
8582 */
8583 else do
8584 {
8585#ifdef FEAT_RIGHTLEFT
8586 if (!revins_on) /* put cursor on char to be deleted */
8587#endif
8588 dec_cursor();
8589
8590 /* start of word? */
8591 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8592 {
8593 mode = BACKSPACE_WORD_NOT_SPACE;
8594 temp = vim_iswordc(gchar_cursor());
8595 }
8596 /* end of word? */
8597 else if (mode == BACKSPACE_WORD_NOT_SPACE
8598 && (vim_isspace(cc = gchar_cursor())
8599 || vim_iswordc(cc) != temp))
8600 {
8601#ifdef FEAT_RIGHTLEFT
8602 if (!revins_on)
8603#endif
8604 inc_cursor();
8605#ifdef FEAT_RIGHTLEFT
8606 else if (State & REPLACE_FLAG)
8607 dec_cursor();
8608#endif
8609 break;
8610 }
8611 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008612 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613 else
8614 {
8615#ifdef FEAT_MBYTE
8616 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008617 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618#endif
8619 (void)del_char(FALSE);
8620#ifdef FEAT_MBYTE
8621 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008622 * If there are combining characters and 'delcombine' is set
8623 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008624 * character.
8625 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008626 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008627 inc_cursor();
8628#endif
8629#ifdef FEAT_RIGHTLEFT
8630 if (revins_chars)
8631 {
8632 revins_chars--;
8633 revins_legal++;
8634 }
8635 if (revins_on && gchar_cursor() == NUL)
8636 break;
8637#endif
8638 }
8639 /* Just a single backspace?: */
8640 if (mode == BACKSPACE_CHAR)
8641 break;
8642 } while (
8643#ifdef FEAT_RIGHTLEFT
8644 revins_on ||
8645#endif
8646 (curwin->w_cursor.col > mincol
8647 && (curwin->w_cursor.lnum != Insstart.lnum
8648 || curwin->w_cursor.col != Insstart.col)));
8649 did_backspace = TRUE;
8650 }
8651#ifdef FEAT_SMARTINDENT
8652 did_si = FALSE;
8653 can_si = FALSE;
8654 can_si_back = FALSE;
8655#endif
8656 if (curwin->w_cursor.col <= 1)
8657 did_ai = FALSE;
8658 /*
8659 * It's a little strange to put backspaces into the redo
8660 * buffer, but it makes auto-indent a lot easier to deal
8661 * with.
8662 */
8663 AppendCharToRedobuff(c);
8664
8665 /* If deleted before the insertion point, adjust it */
8666 if (curwin->w_cursor.lnum == Insstart.lnum
8667 && curwin->w_cursor.col < Insstart.col)
8668 Insstart.col = curwin->w_cursor.col;
8669
8670 /* vi behaviour: the cursor moves backward but the character that
8671 * was there remains visible
8672 * Vim behaviour: the cursor moves backward and the character that
8673 * was there is erased from the screen.
8674 * We can emulate the vi behaviour by pretending there is a dollar
8675 * displayed even when there isn't.
8676 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8677 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8678 dollar_vcol = curwin->w_virtcol;
8679
Bram Moolenaarce3be472008-01-14 19:12:28 +00008680#ifdef FEAT_FOLDING
8681 /* When deleting a char the cursor line must never be in a closed fold.
8682 * E.g., when 'foldmethod' is indent and deleting the first non-white
8683 * char before a Tab. */
8684 if (did_backspace)
8685 foldOpenCursor();
8686#endif
8687
Bram Moolenaar071d4272004-06-13 20:20:40 +00008688 return did_backspace;
8689}
8690
8691#ifdef FEAT_MOUSE
8692 static void
8693ins_mouse(c)
8694 int c;
8695{
8696 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008697 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008698
8699# ifdef FEAT_GUI
8700 /* When GUI is active, also move/paste when 'mouse' is empty */
8701 if (!gui.in_use)
8702# endif
8703 if (!mouse_has(MOUSE_INSERT))
8704 return;
8705
8706 undisplay_dollar();
8707 tpos = curwin->w_cursor;
8708 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8709 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008710#ifdef FEAT_WINDOWS
8711 win_T *new_curwin = curwin;
8712
8713 if (curwin != old_curwin && win_valid(old_curwin))
8714 {
8715 /* Mouse took us to another window. We need to go back to the
8716 * previous one to stop insert there properly. */
8717 curwin = old_curwin;
8718 curbuf = curwin->w_buffer;
8719 }
8720#endif
8721 start_arrow(curwin == old_curwin ? &tpos : NULL);
8722#ifdef FEAT_WINDOWS
8723 if (curwin != new_curwin && win_valid(new_curwin))
8724 {
8725 curwin = new_curwin;
8726 curbuf = curwin->w_buffer;
8727 }
8728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729# ifdef FEAT_CINDENT
8730 can_cindent = TRUE;
8731# endif
8732 }
8733
8734#ifdef FEAT_WINDOWS
8735 /* redraw status lines (in case another window became active) */
8736 redraw_statuslines();
8737#endif
8738}
8739
8740 static void
8741ins_mousescroll(up)
8742 int up;
8743{
8744 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008745# if defined(FEAT_WINDOWS)
8746 win_T *old_curwin = curwin;
8747# endif
8748# ifdef FEAT_INS_EXPAND
8749 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008750# endif
8751
8752 tpos = curwin->w_cursor;
8753
8754# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755 /* Currently the mouse coordinates are only known in the GUI. */
8756 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8757 {
8758 int row, col;
8759
8760 row = mouse_row;
8761 col = mouse_col;
8762
8763 /* find the window at the pointer coordinates */
8764 curwin = mouse_find_win(&row, &col);
8765 curbuf = curwin->w_buffer;
8766 }
8767 if (curwin == old_curwin)
8768# endif
8769 undisplay_dollar();
8770
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008771# ifdef FEAT_INS_EXPAND
8772 /* Don't scroll the window in which completion is being done. */
8773 if (!pum_visible()
8774# if defined(FEAT_WINDOWS)
8775 || curwin != old_curwin
8776# endif
8777 )
8778# endif
8779 {
8780 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8781 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8782 else
8783 scroll_redraw(up, 3L);
8784# ifdef FEAT_INS_EXPAND
8785 did_scroll = TRUE;
8786# endif
8787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008788
8789# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8790 curwin->w_redr_status = TRUE;
8791
8792 curwin = old_curwin;
8793 curbuf = curwin->w_buffer;
8794# endif
8795
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008796# ifdef FEAT_INS_EXPAND
8797 /* The popup menu may overlay the window, need to redraw it.
8798 * TODO: Would be more efficient to only redraw the windows that are
8799 * overlapped by the popup menu. */
8800 if (pum_visible() && did_scroll)
8801 {
8802 redraw_all_later(NOT_VALID);
8803 ins_compl_show_pum();
8804 }
8805# endif
8806
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807 if (!equalpos(curwin->w_cursor, tpos))
8808 {
8809 start_arrow(&tpos);
8810# ifdef FEAT_CINDENT
8811 can_cindent = TRUE;
8812# endif
8813 }
8814}
8815#endif
8816
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008817#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008818 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008819ins_tabline(c)
8820 int c;
8821{
8822 /* We will be leaving the current window, unless closing another tab. */
8823 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8824 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8825 {
8826 undisplay_dollar();
8827 start_arrow(&curwin->w_cursor);
8828# ifdef FEAT_CINDENT
8829 can_cindent = TRUE;
8830# endif
8831 }
8832
8833 if (c == K_TABLINE)
8834 goto_tabpage(current_tab);
8835 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008836 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008837 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008838 redraw_statuslines(); /* will redraw the tabline when needed */
8839 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008840}
8841#endif
8842
8843#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844 void
8845ins_scroll()
8846{
8847 pos_T tpos;
8848
8849 undisplay_dollar();
8850 tpos = curwin->w_cursor;
8851 if (gui_do_scroll())
8852 {
8853 start_arrow(&tpos);
8854# ifdef FEAT_CINDENT
8855 can_cindent = TRUE;
8856# endif
8857 }
8858}
8859
8860 void
8861ins_horscroll()
8862{
8863 pos_T tpos;
8864
8865 undisplay_dollar();
8866 tpos = curwin->w_cursor;
8867 if (gui_do_horiz_scroll())
8868 {
8869 start_arrow(&tpos);
8870# ifdef FEAT_CINDENT
8871 can_cindent = TRUE;
8872# endif
8873 }
8874}
8875#endif
8876
8877 static void
8878ins_left()
8879{
8880 pos_T tpos;
8881
8882#ifdef FEAT_FOLDING
8883 if ((fdo_flags & FDO_HOR) && KeyTyped)
8884 foldOpenCursor();
8885#endif
8886 undisplay_dollar();
8887 tpos = curwin->w_cursor;
8888 if (oneleft() == OK)
8889 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00008890#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
8891 /* Only call start_arrow() when not busy with preediting, it will
8892 * break undo. K_LEFT is inserted in im_correct_cursor(). */
8893 if (!im_is_preediting())
8894#endif
8895 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896#ifdef FEAT_RIGHTLEFT
8897 /* If exit reversed string, position is fixed */
8898 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
8899 revins_legal++;
8900 revins_chars++;
8901#endif
8902 }
8903
8904 /*
8905 * if 'whichwrap' set for cursor in insert mode may go to
8906 * previous line
8907 */
8908 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
8909 {
8910 start_arrow(&tpos);
8911 --(curwin->w_cursor.lnum);
8912 coladvance((colnr_T)MAXCOL);
8913 curwin->w_set_curswant = TRUE; /* so we stay at the end */
8914 }
8915 else
8916 vim_beep();
8917}
8918
8919 static void
8920ins_home(c)
8921 int c;
8922{
8923 pos_T tpos;
8924
8925#ifdef FEAT_FOLDING
8926 if ((fdo_flags & FDO_HOR) && KeyTyped)
8927 foldOpenCursor();
8928#endif
8929 undisplay_dollar();
8930 tpos = curwin->w_cursor;
8931 if (c == K_C_HOME)
8932 curwin->w_cursor.lnum = 1;
8933 curwin->w_cursor.col = 0;
8934#ifdef FEAT_VIRTUALEDIT
8935 curwin->w_cursor.coladd = 0;
8936#endif
8937 curwin->w_curswant = 0;
8938 start_arrow(&tpos);
8939}
8940
8941 static void
8942ins_end(c)
8943 int c;
8944{
8945 pos_T tpos;
8946
8947#ifdef FEAT_FOLDING
8948 if ((fdo_flags & FDO_HOR) && KeyTyped)
8949 foldOpenCursor();
8950#endif
8951 undisplay_dollar();
8952 tpos = curwin->w_cursor;
8953 if (c == K_C_END)
8954 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
8955 coladvance((colnr_T)MAXCOL);
8956 curwin->w_curswant = MAXCOL;
8957
8958 start_arrow(&tpos);
8959}
8960
8961 static void
8962ins_s_left()
8963{
8964#ifdef FEAT_FOLDING
8965 if ((fdo_flags & FDO_HOR) && KeyTyped)
8966 foldOpenCursor();
8967#endif
8968 undisplay_dollar();
8969 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
8970 {
8971 start_arrow(&curwin->w_cursor);
8972 (void)bck_word(1L, FALSE, FALSE);
8973 curwin->w_set_curswant = TRUE;
8974 }
8975 else
8976 vim_beep();
8977}
8978
8979 static void
8980ins_right()
8981{
8982#ifdef FEAT_FOLDING
8983 if ((fdo_flags & FDO_HOR) && KeyTyped)
8984 foldOpenCursor();
8985#endif
8986 undisplay_dollar();
8987 if (gchar_cursor() != NUL || virtual_active()
8988 )
8989 {
8990 start_arrow(&curwin->w_cursor);
8991 curwin->w_set_curswant = TRUE;
8992#ifdef FEAT_VIRTUALEDIT
8993 if (virtual_active())
8994 oneright();
8995 else
8996#endif
8997 {
8998#ifdef FEAT_MBYTE
8999 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009000 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009001 else
9002#endif
9003 ++curwin->w_cursor.col;
9004 }
9005
9006#ifdef FEAT_RIGHTLEFT
9007 revins_legal++;
9008 if (revins_chars)
9009 revins_chars--;
9010#endif
9011 }
9012 /* if 'whichwrap' set for cursor in insert mode, may move the
9013 * cursor to the next line */
9014 else if (vim_strchr(p_ww, ']') != NULL
9015 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9016 {
9017 start_arrow(&curwin->w_cursor);
9018 curwin->w_set_curswant = TRUE;
9019 ++curwin->w_cursor.lnum;
9020 curwin->w_cursor.col = 0;
9021 }
9022 else
9023 vim_beep();
9024}
9025
9026 static void
9027ins_s_right()
9028{
9029#ifdef FEAT_FOLDING
9030 if ((fdo_flags & FDO_HOR) && KeyTyped)
9031 foldOpenCursor();
9032#endif
9033 undisplay_dollar();
9034 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9035 || gchar_cursor() != NUL)
9036 {
9037 start_arrow(&curwin->w_cursor);
9038 (void)fwd_word(1L, FALSE, 0);
9039 curwin->w_set_curswant = TRUE;
9040 }
9041 else
9042 vim_beep();
9043}
9044
9045 static void
9046ins_up(startcol)
9047 int startcol; /* when TRUE move to Insstart.col */
9048{
9049 pos_T tpos;
9050 linenr_T old_topline = curwin->w_topline;
9051#ifdef FEAT_DIFF
9052 int old_topfill = curwin->w_topfill;
9053#endif
9054
9055 undisplay_dollar();
9056 tpos = curwin->w_cursor;
9057 if (cursor_up(1L, TRUE) == OK)
9058 {
9059 if (startcol)
9060 coladvance(getvcol_nolist(&Insstart));
9061 if (old_topline != curwin->w_topline
9062#ifdef FEAT_DIFF
9063 || old_topfill != curwin->w_topfill
9064#endif
9065 )
9066 redraw_later(VALID);
9067 start_arrow(&tpos);
9068#ifdef FEAT_CINDENT
9069 can_cindent = TRUE;
9070#endif
9071 }
9072 else
9073 vim_beep();
9074}
9075
9076 static void
9077ins_pageup()
9078{
9079 pos_T tpos;
9080
9081 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009082
9083#ifdef FEAT_WINDOWS
9084 if (mod_mask & MOD_MASK_CTRL)
9085 {
9086 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009087 if (first_tabpage->tp_next != NULL)
9088 {
9089 start_arrow(&curwin->w_cursor);
9090 goto_tabpage(-1);
9091 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009092 return;
9093 }
9094#endif
9095
Bram Moolenaar071d4272004-06-13 20:20:40 +00009096 tpos = curwin->w_cursor;
9097 if (onepage(BACKWARD, 1L) == OK)
9098 {
9099 start_arrow(&tpos);
9100#ifdef FEAT_CINDENT
9101 can_cindent = TRUE;
9102#endif
9103 }
9104 else
9105 vim_beep();
9106}
9107
9108 static void
9109ins_down(startcol)
9110 int startcol; /* when TRUE move to Insstart.col */
9111{
9112 pos_T tpos;
9113 linenr_T old_topline = curwin->w_topline;
9114#ifdef FEAT_DIFF
9115 int old_topfill = curwin->w_topfill;
9116#endif
9117
9118 undisplay_dollar();
9119 tpos = curwin->w_cursor;
9120 if (cursor_down(1L, TRUE) == OK)
9121 {
9122 if (startcol)
9123 coladvance(getvcol_nolist(&Insstart));
9124 if (old_topline != curwin->w_topline
9125#ifdef FEAT_DIFF
9126 || old_topfill != curwin->w_topfill
9127#endif
9128 )
9129 redraw_later(VALID);
9130 start_arrow(&tpos);
9131#ifdef FEAT_CINDENT
9132 can_cindent = TRUE;
9133#endif
9134 }
9135 else
9136 vim_beep();
9137}
9138
9139 static void
9140ins_pagedown()
9141{
9142 pos_T tpos;
9143
9144 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009145
9146#ifdef FEAT_WINDOWS
9147 if (mod_mask & MOD_MASK_CTRL)
9148 {
9149 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009150 if (first_tabpage->tp_next != NULL)
9151 {
9152 start_arrow(&curwin->w_cursor);
9153 goto_tabpage(0);
9154 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009155 return;
9156 }
9157#endif
9158
Bram Moolenaar071d4272004-06-13 20:20:40 +00009159 tpos = curwin->w_cursor;
9160 if (onepage(FORWARD, 1L) == OK)
9161 {
9162 start_arrow(&tpos);
9163#ifdef FEAT_CINDENT
9164 can_cindent = TRUE;
9165#endif
9166 }
9167 else
9168 vim_beep();
9169}
9170
9171#ifdef FEAT_DND
9172 static void
9173ins_drop()
9174{
9175 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9176}
9177#endif
9178
9179/*
9180 * Handle TAB in Insert or Replace mode.
9181 * Return TRUE when the TAB needs to be inserted like a normal character.
9182 */
9183 static int
9184ins_tab()
9185{
9186 int ind;
9187 int i;
9188 int temp;
9189
9190 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9191 Insstart_blank_vcol = get_nolist_virtcol();
9192 if (echeck_abbr(TAB + ABBR_OFF))
9193 return FALSE;
9194
9195 ind = inindent(0);
9196#ifdef FEAT_CINDENT
9197 if (ind)
9198 can_cindent = FALSE;
9199#endif
9200
9201 /*
9202 * When nothing special, insert TAB like a normal character
9203 */
9204 if (!curbuf->b_p_et
9205 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9206 && curbuf->b_p_sts == 0)
9207 return TRUE;
9208
9209 if (stop_arrow() == FAIL)
9210 return TRUE;
9211
9212 did_ai = FALSE;
9213#ifdef FEAT_SMARTINDENT
9214 did_si = FALSE;
9215 can_si = FALSE;
9216 can_si_back = FALSE;
9217#endif
9218 AppendToRedobuff((char_u *)"\t");
9219
9220 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9221 temp = (int)curbuf->b_p_sw;
9222 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9223 temp = (int)curbuf->b_p_sts;
9224 else /* otherwise use 'tabstop' */
9225 temp = (int)curbuf->b_p_ts;
9226 temp -= get_nolist_virtcol() % temp;
9227
9228 /*
9229 * Insert the first space with ins_char(). It will delete one char in
9230 * replace mode. Insert the rest with ins_str(); it will not delete any
9231 * chars. For VREPLACE mode, we use ins_char() for all characters.
9232 */
9233 ins_char(' ');
9234 while (--temp > 0)
9235 {
9236#ifdef FEAT_VREPLACE
9237 if (State & VREPLACE_FLAG)
9238 ins_char(' ');
9239 else
9240#endif
9241 {
9242 ins_str((char_u *)" ");
9243 if (State & REPLACE_FLAG) /* no char replaced */
9244 replace_push(NUL);
9245 }
9246 }
9247
9248 /*
9249 * When 'expandtab' not set: Replace spaces by TABs where possible.
9250 */
9251 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9252 {
9253 char_u *ptr;
9254#ifdef FEAT_VREPLACE
9255 char_u *saved_line = NULL; /* init for GCC */
9256 pos_T pos;
9257#endif
9258 pos_T fpos;
9259 pos_T *cursor;
9260 colnr_T want_vcol, vcol;
9261 int change_col = -1;
9262 int save_list = curwin->w_p_list;
9263
9264 /*
9265 * Get the current line. For VREPLACE mode, don't make real changes
9266 * yet, just work on a copy of the line.
9267 */
9268#ifdef FEAT_VREPLACE
9269 if (State & VREPLACE_FLAG)
9270 {
9271 pos = curwin->w_cursor;
9272 cursor = &pos;
9273 saved_line = vim_strsave(ml_get_curline());
9274 if (saved_line == NULL)
9275 return FALSE;
9276 ptr = saved_line + pos.col;
9277 }
9278 else
9279#endif
9280 {
9281 ptr = ml_get_cursor();
9282 cursor = &curwin->w_cursor;
9283 }
9284
9285 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9286 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9287 curwin->w_p_list = FALSE;
9288
9289 /* Find first white before the cursor */
9290 fpos = curwin->w_cursor;
9291 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9292 {
9293 --fpos.col;
9294 --ptr;
9295 }
9296
9297 /* In Replace mode, don't change characters before the insert point. */
9298 if ((State & REPLACE_FLAG)
9299 && fpos.lnum == Insstart.lnum
9300 && fpos.col < Insstart.col)
9301 {
9302 ptr += Insstart.col - fpos.col;
9303 fpos.col = Insstart.col;
9304 }
9305
9306 /* compute virtual column numbers of first white and cursor */
9307 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9308 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9309
9310 /* Use as many TABs as possible. Beware of 'showbreak' and
9311 * 'linebreak' adding extra virtual columns. */
9312 while (vim_iswhite(*ptr))
9313 {
9314 i = lbr_chartabsize((char_u *)"\t", vcol);
9315 if (vcol + i > want_vcol)
9316 break;
9317 if (*ptr != TAB)
9318 {
9319 *ptr = TAB;
9320 if (change_col < 0)
9321 {
9322 change_col = fpos.col; /* Column of first change */
9323 /* May have to adjust Insstart */
9324 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9325 Insstart.col = fpos.col;
9326 }
9327 }
9328 ++fpos.col;
9329 ++ptr;
9330 vcol += i;
9331 }
9332
9333 if (change_col >= 0)
9334 {
9335 int repl_off = 0;
9336
9337 /* Skip over the spaces we need. */
9338 while (vcol < want_vcol && *ptr == ' ')
9339 {
9340 vcol += lbr_chartabsize(ptr, vcol);
9341 ++ptr;
9342 ++repl_off;
9343 }
9344 if (vcol > want_vcol)
9345 {
9346 /* Must have a char with 'showbreak' just before it. */
9347 --ptr;
9348 --repl_off;
9349 }
9350 fpos.col += repl_off;
9351
9352 /* Delete following spaces. */
9353 i = cursor->col - fpos.col;
9354 if (i > 0)
9355 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009356 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009357 /* correct replace stack. */
9358 if ((State & REPLACE_FLAG)
9359#ifdef FEAT_VREPLACE
9360 && !(State & VREPLACE_FLAG)
9361#endif
9362 )
9363 for (temp = i; --temp >= 0; )
9364 replace_join(repl_off);
9365 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009366#ifdef FEAT_NETBEANS_INTG
9367 if (usingNetbeans)
9368 {
9369 netbeans_removed(curbuf, fpos.lnum, cursor->col,
9370 (long)(i + 1));
9371 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9372 (char_u *)"\t", 1);
9373 }
9374#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009375 cursor->col -= i;
9376
9377#ifdef FEAT_VREPLACE
9378 /*
9379 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9380 * backspacing over the changed spacing and then inserting the new
9381 * spacing.
9382 */
9383 if (State & VREPLACE_FLAG)
9384 {
9385 /* Backspace from real cursor to change_col */
9386 backspace_until_column(change_col);
9387
9388 /* Insert each char in saved_line from changed_col to
9389 * ptr-cursor */
9390 ins_bytes_len(saved_line + change_col,
9391 cursor->col - change_col);
9392 }
9393#endif
9394 }
9395
9396#ifdef FEAT_VREPLACE
9397 if (State & VREPLACE_FLAG)
9398 vim_free(saved_line);
9399#endif
9400 curwin->w_p_list = save_list;
9401 }
9402
9403 return FALSE;
9404}
9405
9406/*
9407 * Handle CR or NL in insert mode.
9408 * Return TRUE when out of memory or can't undo.
9409 */
9410 static int
9411ins_eol(c)
9412 int c;
9413{
9414 int i;
9415
9416 if (echeck_abbr(c + ABBR_OFF))
9417 return FALSE;
9418 if (stop_arrow() == FAIL)
9419 return TRUE;
9420 undisplay_dollar();
9421
9422 /*
9423 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9424 * character under the cursor. Only push a NUL on the replace stack,
9425 * nothing to put back when the NL is deleted.
9426 */
9427 if ((State & REPLACE_FLAG)
9428#ifdef FEAT_VREPLACE
9429 && !(State & VREPLACE_FLAG)
9430#endif
9431 )
9432 replace_push(NUL);
9433
9434 /*
9435 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9436 * replacing the next line, so we push all of the characters left on the
9437 * line onto the replace stack. This is not done here though, it is done
9438 * in open_line().
9439 */
9440
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009441#ifdef FEAT_VIRTUALEDIT
9442 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9443 * CTRL-O). */
9444 if (virtual_active() && curwin->w_cursor.coladd > 0)
9445 coladvance(getviscol());
9446#endif
9447
Bram Moolenaar071d4272004-06-13 20:20:40 +00009448#ifdef FEAT_RIGHTLEFT
9449# ifdef FEAT_FKMAP
9450 if (p_altkeymap && p_fkmap)
9451 fkmap(NL);
9452# endif
9453 /* NL in reverse insert will always start in the end of
9454 * current line. */
9455 if (revins_on)
9456 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9457#endif
9458
9459 AppendToRedobuff(NL_STR);
9460 i = open_line(FORWARD,
9461#ifdef FEAT_COMMENTS
9462 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9463#endif
9464 0, old_indent);
9465 old_indent = 0;
9466#ifdef FEAT_CINDENT
9467 can_cindent = TRUE;
9468#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009469#ifdef FEAT_FOLDING
9470 /* When inserting a line the cursor line must never be in a closed fold. */
9471 foldOpenCursor();
9472#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009473
9474 return (!i);
9475}
9476
9477#ifdef FEAT_DIGRAPHS
9478/*
9479 * Handle digraph in insert mode.
9480 * Returns character still to be inserted, or NUL when nothing remaining to be
9481 * done.
9482 */
9483 static int
9484ins_digraph()
9485{
9486 int c;
9487 int cc;
9488
9489 pc_status = PC_STATUS_UNSET;
9490 if (redrawing() && !char_avail())
9491 {
9492 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009493 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009494
9495 edit_putchar('?', TRUE);
9496#ifdef FEAT_CMDL_INFO
9497 add_to_showcmd_c(Ctrl_K);
9498#endif
9499 }
9500
9501#ifdef USE_ON_FLY_SCROLL
9502 dont_scroll = TRUE; /* disallow scrolling here */
9503#endif
9504
9505 /* don't map the digraph chars. This also prevents the
9506 * mode message to be deleted when ESC is hit */
9507 ++no_mapping;
9508 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009509 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 --no_mapping;
9511 --allow_keys;
9512 if (IS_SPECIAL(c) || mod_mask) /* special key */
9513 {
9514#ifdef FEAT_CMDL_INFO
9515 clear_showcmd();
9516#endif
9517 insert_special(c, TRUE, FALSE);
9518 return NUL;
9519 }
9520 if (c != ESC)
9521 {
9522 if (redrawing() && !char_avail())
9523 {
9524 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009525 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009526
9527 if (char2cells(c) == 1)
9528 {
9529 /* first remove the '?', otherwise it's restored when typing
9530 * an ESC next */
9531 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009532 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533 edit_putchar(c, TRUE);
9534 }
9535#ifdef FEAT_CMDL_INFO
9536 add_to_showcmd_c(c);
9537#endif
9538 }
9539 ++no_mapping;
9540 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009541 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542 --no_mapping;
9543 --allow_keys;
9544 if (cc != ESC)
9545 {
9546 AppendToRedobuff((char_u *)CTRL_V_STR);
9547 c = getdigraph(c, cc, TRUE);
9548#ifdef FEAT_CMDL_INFO
9549 clear_showcmd();
9550#endif
9551 return c;
9552 }
9553 }
9554 edit_unputchar();
9555#ifdef FEAT_CMDL_INFO
9556 clear_showcmd();
9557#endif
9558 return NUL;
9559}
9560#endif
9561
9562/*
9563 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9564 * Returns the char to be inserted, or NUL if none found.
9565 */
9566 static int
9567ins_copychar(lnum)
9568 linenr_T lnum;
9569{
9570 int c;
9571 int temp;
9572 char_u *ptr, *prev_ptr;
9573
9574 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9575 {
9576 vim_beep();
9577 return NUL;
9578 }
9579
9580 /* try to advance to the cursor column */
9581 temp = 0;
9582 ptr = ml_get(lnum);
9583 prev_ptr = ptr;
9584 validate_virtcol();
9585 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9586 {
9587 prev_ptr = ptr;
9588 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9589 }
9590 if ((colnr_T)temp > curwin->w_virtcol)
9591 ptr = prev_ptr;
9592
9593#ifdef FEAT_MBYTE
9594 c = (*mb_ptr2char)(ptr);
9595#else
9596 c = *ptr;
9597#endif
9598 if (c == NUL)
9599 vim_beep();
9600 return c;
9601}
9602
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009603/*
9604 * CTRL-Y or CTRL-E typed in Insert mode.
9605 */
9606 static int
9607ins_ctrl_ey(tc)
9608 int tc;
9609{
9610 int c = tc;
9611
9612#ifdef FEAT_INS_EXPAND
9613 if (ctrl_x_mode == CTRL_X_SCROLL)
9614 {
9615 if (c == Ctrl_Y)
9616 scrolldown_clamp();
9617 else
9618 scrollup_clamp();
9619 redraw_later(VALID);
9620 }
9621 else
9622#endif
9623 {
9624 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9625 if (c != NUL)
9626 {
9627 long tw_save;
9628
9629 /* The character must be taken literally, insert like it
9630 * was typed after a CTRL-V, and pretend 'textwidth'
9631 * wasn't set. Digits, 'o' and 'x' are special after a
9632 * CTRL-V, don't use it for these. */
9633 if (c < 256 && !isalnum(c))
9634 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9635 tw_save = curbuf->b_p_tw;
9636 curbuf->b_p_tw = -1;
9637 insert_special(c, TRUE, FALSE);
9638 curbuf->b_p_tw = tw_save;
9639#ifdef FEAT_RIGHTLEFT
9640 revins_chars++;
9641 revins_legal++;
9642#endif
9643 c = Ctrl_V; /* pretend CTRL-V is last character */
9644 auto_format(FALSE, TRUE);
9645 }
9646 }
9647 return c;
9648}
9649
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650#ifdef FEAT_SMARTINDENT
9651/*
9652 * Try to do some very smart auto-indenting.
9653 * Used when inserting a "normal" character.
9654 */
9655 static void
9656ins_try_si(c)
9657 int c;
9658{
9659 pos_T *pos, old_pos;
9660 char_u *ptr;
9661 int i;
9662 int temp;
9663
9664 /*
9665 * do some very smart indenting when entering '{' or '}'
9666 */
9667 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9668 {
9669 /*
9670 * for '}' set indent equal to indent of line containing matching '{'
9671 */
9672 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9673 {
9674 old_pos = curwin->w_cursor;
9675 /*
9676 * If the matching '{' has a ')' immediately before it (ignoring
9677 * white-space), then line up with the start of the line
9678 * containing the matching '(' if there is one. This handles the
9679 * case where an "if (..\n..) {" statement continues over multiple
9680 * lines -- webb
9681 */
9682 ptr = ml_get(pos->lnum);
9683 i = pos->col;
9684 if (i > 0) /* skip blanks before '{' */
9685 while (--i > 0 && vim_iswhite(ptr[i]))
9686 ;
9687 curwin->w_cursor.lnum = pos->lnum;
9688 curwin->w_cursor.col = i;
9689 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9690 curwin->w_cursor = *pos;
9691 i = get_indent();
9692 curwin->w_cursor = old_pos;
9693#ifdef FEAT_VREPLACE
9694 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009695 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696 else
9697#endif
9698 (void)set_indent(i, SIN_CHANGED);
9699 }
9700 else if (curwin->w_cursor.col > 0)
9701 {
9702 /*
9703 * when inserting '{' after "O" reduce indent, but not
9704 * more than indent of previous line
9705 */
9706 temp = TRUE;
9707 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9708 {
9709 old_pos = curwin->w_cursor;
9710 i = get_indent();
9711 while (curwin->w_cursor.lnum > 1)
9712 {
9713 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9714
9715 /* ignore empty lines and lines starting with '#'. */
9716 if (*ptr != '#' && *ptr != NUL)
9717 break;
9718 }
9719 if (get_indent() >= i)
9720 temp = FALSE;
9721 curwin->w_cursor = old_pos;
9722 }
9723 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009724 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725 }
9726 }
9727
9728 /*
9729 * set indent of '#' always to 0
9730 */
9731 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9732 {
9733 /* remember current indent for next line */
9734 old_indent = get_indent();
9735 (void)set_indent(0, SIN_CHANGED);
9736 }
9737
9738 /* Adjust ai_col, the char at this position can be deleted. */
9739 if (ai_col > curwin->w_cursor.col)
9740 ai_col = curwin->w_cursor.col;
9741}
9742#endif
9743
9744/*
9745 * Get the value that w_virtcol would have when 'list' is off.
9746 * Unless 'cpo' contains the 'L' flag.
9747 */
9748 static colnr_T
9749get_nolist_virtcol()
9750{
9751 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9752 return getvcol_nolist(&curwin->w_cursor);
9753 validate_virtcol();
9754 return curwin->w_virtcol;
9755}