blob: e5f6b3312b0efd35707e04260f464d4ab386c73a [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * edit.c: functions for Insert mode
12 */
13
14#include "vim.h"
15
16#ifdef FEAT_INS_EXPAND
17/*
18 * definitions used for CTRL-X submode
19 */
20#define CTRL_X_WANT_IDENT 0x100
21
22#define CTRL_X_NOT_DEFINED_YET 1
23#define CTRL_X_SCROLL 2
24#define CTRL_X_WHOLE_LINE 3
25#define CTRL_X_FILES 4
26#define CTRL_X_TAGS (5 + CTRL_X_WANT_IDENT)
27#define CTRL_X_PATH_PATTERNS (6 + CTRL_X_WANT_IDENT)
28#define CTRL_X_PATH_DEFINES (7 + CTRL_X_WANT_IDENT)
29#define CTRL_X_FINISHED 8
30#define CTRL_X_DICTIONARY (9 + CTRL_X_WANT_IDENT)
31#define CTRL_X_THESAURUS (10 + CTRL_X_WANT_IDENT)
32#define CTRL_X_CMDLINE 11
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000033#define CTRL_X_FUNCTION 12
Bram Moolenaarf75a9632005-09-13 21:20:47 +000034#define CTRL_X_OMNI 13
Bram Moolenaar488c6512005-08-11 20:09:58 +000035#define CTRL_X_SPELL 14
36#define CTRL_X_LOCAL_MSG 15 /* only used in "ctrl_x_msgs" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000037
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#define CTRL_X_MSG(i) ctrl_x_msgs[(i) & ~CTRL_X_WANT_IDENT]
39
40static char *ctrl_x_msgs[] =
41{
42 N_(" Keyword completion (^N^P)"), /* ctrl_x_mode == 0, ^P/^N compl. */
Bram Moolenaar910f66f2006-04-05 20:41:53 +000043 N_(" ^X mode (^]^D^E^F^I^K^L^N^O^Ps^U^V^Y)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000044 NULL,
Bram Moolenaar071d4272004-06-13 20:20:40 +000045 N_(" Whole line completion (^L^N^P)"),
46 N_(" File name completion (^F^N^P)"),
47 N_(" Tag completion (^]^N^P)"),
48 N_(" Path pattern completion (^N^P)"),
49 N_(" Definition completion (^D^N^P)"),
50 NULL,
51 N_(" Dictionary completion (^K^N^P)"),
52 N_(" Thesaurus completion (^T^N^P)"),
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +000053 N_(" Command-line completion (^V^N^P)"),
54 N_(" User defined completion (^U^N^P)"),
Bram Moolenaarf75a9632005-09-13 21:20:47 +000055 N_(" Omni completion (^O^N^P)"),
Bram Moolenaar910f66f2006-04-05 20:41:53 +000056 N_(" Spelling suggestion (s^N^P)"),
Bram Moolenaar4be06f92005-07-29 22:36:03 +000057 N_(" Keyword Local completion (^N^P)"),
Bram Moolenaar071d4272004-06-13 20:20:40 +000058};
59
Bram Moolenaar0ab2a882009-05-13 10:51:08 +000060static char e_hitend[] = N_("Hit end of paragraph");
Bram Moolenaar071d4272004-06-13 20:20:40 +000061
62/*
63 * Structure used to store one match for insert completion.
64 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000065typedef struct compl_S compl_T;
66struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000067{
Bram Moolenaar572cb562005-08-05 21:35:02 +000068 compl_T *cp_next;
69 compl_T *cp_prev;
70 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000071 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000072 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000073 char_u *cp_fname; /* file containing the match, allocated when
74 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000075 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
76 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000077};
78
Bram Moolenaar572cb562005-08-05 21:35:02 +000079#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000080#define FREE_FNAME (2)
81
82/*
83 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000084 * "compl_first_match" points to the start of the list.
85 * "compl_curr_match" points to the currently selected entry.
86 * "compl_shown_match" is different from compl_curr_match during
87 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000088 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000089static compl_T *compl_first_match = NULL;
90static compl_T *compl_curr_match = NULL;
91static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
Bram Moolenaar779b74b2006-04-10 14:55:34 +000093/* After using a cursor key <Enter> selects a match in the popup menu,
94 * otherwise it inserts a line break. */
95static int compl_enter_selects = FALSE;
96
Bram Moolenaara6557602006-02-04 22:43:20 +000097/* When "compl_leader" is not NULL only matches that start with this string
98 * are used. */
99static char_u *compl_leader = NULL;
100
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000101static int compl_get_longest = FALSE; /* put longest common string
102 in compl_leader */
103
Bram Moolenaara6557602006-02-04 22:43:20 +0000104static int compl_used_match; /* Selected one of the matches. When
105 FALSE the match was edited or using
106 the longest common string. */
107
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000108static int compl_was_interrupted = FALSE; /* didn't finish finding
109 completions. */
110
111static int compl_restarting = FALSE; /* don't insert match */
112
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000113/* When the first completion is done "compl_started" is set. When it's
114 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000115static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000116
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000117/* Set when doing something for completion that may call edit() recursively,
118 * which is not allowed. */
119static int compl_busy = FALSE;
120
Bram Moolenaar572cb562005-08-05 21:35:02 +0000121static int compl_matches = 0;
122static char_u *compl_pattern = NULL;
123static int compl_direction = FORWARD;
124static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000125static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000126static pos_T compl_startpos;
127static colnr_T compl_col = 0; /* column where the text starts
128 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000129static char_u *compl_orig_text = NULL; /* text as it was before
130 * completion started */
131static int compl_cont_mode = 0;
132static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000133
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000134static void ins_ctrl_x __ARGS((void));
135static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000136static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000137static int ins_compl_add __ARGS((char_u *str, int len, int icase, char_u *fname, char_u **cptext, int cdir, int flags, int adup));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000138static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000139static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000140static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000141static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000142static void ins_compl_upd_pum __ARGS((void));
143static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000144static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000145static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000146static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000147static void ins_compl_files __ARGS((int count, char_u **files, int thesaurus, int flags, regmatch_T *regmatch, char_u *buf, int *dir));
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +0000148static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149static void ins_compl_free __ARGS((void));
150static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000151static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000152static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000153static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000154static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000155static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000156static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000157static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000158static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000160#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
161static void ins_compl_add_list __ARGS((list_T *list));
162#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000163static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000164static void ins_compl_delete __ARGS((void));
165static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000166static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000167static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000168static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000169static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000170static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000172static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000173#endif /* FEAT_INS_EXPAND */
174
175#define BACKSPACE_CHAR 1
176#define BACKSPACE_WORD 2
177#define BACKSPACE_WORD_NOT_SPACE 3
178#define BACKSPACE_LINE 4
179
Bram Moolenaar754b5602006-02-09 23:53:20 +0000180static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000181static void ins_ctrl_v __ARGS((void));
182static void undisplay_dollar __ARGS((void));
183static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000184static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185static void check_auto_format __ARGS((int));
186static void redo_literal __ARGS((int c));
187static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000188#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000189static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000190static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000191static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000192#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000193static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
194static int echeck_abbr __ARGS((int));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000195#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +0000196static void replace_push_off __ARGS((int c));
Bram Moolenaareb3593b2006-04-22 22:33:57 +0000197#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000198static int replace_pop __ARGS((void));
199static void replace_join __ARGS((int off));
200static void replace_pop_ins __ARGS((void));
201#ifdef FEAT_MBYTE
202static void mb_replace_pop_ins __ARGS((int cc));
203#endif
204static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000205static void replace_do_bs __ARGS((int limit_col));
206static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000207#ifdef FEAT_CINDENT
208static int cindent_on __ARGS((void));
209#endif
210static void ins_reg __ARGS((void));
211static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000212static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000213static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000214#ifdef FEAT_RIGHTLEFT
215static void ins_ctrl_ __ARGS((void));
216#endif
217#ifdef FEAT_VISUAL
218static int ins_start_select __ARGS((int c));
219#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000220static void ins_insert __ARGS((int replaceState));
221static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000222static void ins_shift __ARGS((int c, int lastc));
223static void ins_del __ARGS((void));
224static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
225#ifdef FEAT_MOUSE
226static void ins_mouse __ARGS((int c));
227static void ins_mousescroll __ARGS((int up));
228#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000229#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
230static void ins_tabline __ARGS((int c));
231#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000232static void ins_left __ARGS((void));
233static void ins_home __ARGS((int c));
234static void ins_end __ARGS((int c));
235static void ins_s_left __ARGS((void));
236static void ins_right __ARGS((void));
237static void ins_s_right __ARGS((void));
238static void ins_up __ARGS((int startcol));
239static void ins_pageup __ARGS((void));
240static void ins_down __ARGS((int startcol));
241static void ins_pagedown __ARGS((void));
242#ifdef FEAT_DND
243static void ins_drop __ARGS((void));
244#endif
245static int ins_tab __ARGS((void));
246static int ins_eol __ARGS((int c));
247#ifdef FEAT_DIGRAPHS
248static int ins_digraph __ARGS((void));
249#endif
250static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000251static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000252#ifdef FEAT_SMARTINDENT
253static void ins_try_si __ARGS((int c));
254#endif
255static colnr_T get_nolist_virtcol __ARGS((void));
256
257static colnr_T Insstart_textlen; /* length of line when insert started */
258static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
259
260static char_u *last_insert = NULL; /* the text of the previous insert,
261 K_SPECIAL and CSI are escaped */
262static int last_insert_skip; /* nr of chars in front of previous insert */
263static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000264static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000265
266#ifdef FEAT_CINDENT
267static int can_cindent; /* may do cindenting on this line */
268#endif
269
270static int old_indent = 0; /* for ^^D command in insert mode */
271
272#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000273static int revins_on; /* reverse insert mode on */
274static int revins_chars; /* how much to skip after edit */
275static int revins_legal; /* was the last char 'legal'? */
276static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000277#endif
278
Bram Moolenaar071d4272004-06-13 20:20:40 +0000279static int ins_need_undo; /* call u_save() before inserting a
280 char. Set when edit() is called.
281 after that arrow_used is used. */
282
283static int did_add_space = FALSE; /* auto_format() added an extra space
284 under the cursor */
285
286/*
287 * edit(): Start inserting text.
288 *
289 * "cmdchar" can be:
290 * 'i' normal insert command
291 * 'a' normal append command
292 * 'R' replace command
293 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
294 * but still only one <CR> is inserted. The <Esc> is not used for redo.
295 * 'g' "gI" command.
296 * 'V' "gR" command for Virtual Replace mode.
297 * 'v' "gr" command for single character Virtual Replace mode.
298 *
299 * This function is not called recursively. For CTRL-O commands, it returns
300 * and lets the caller handle the Normal-mode command.
301 *
302 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
303 */
304 int
305edit(cmdchar, startln, count)
306 int cmdchar;
307 int startln; /* if set, insert at start of line */
308 long count;
309{
310 int c = 0;
311 char_u *ptr;
312 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000313 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000314 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 int i;
316 int did_backspace = TRUE; /* previous char was backspace */
317#ifdef FEAT_CINDENT
318 int line_is_white = FALSE; /* line is empty before insert */
319#endif
320 linenr_T old_topline = 0; /* topline before insertion */
321#ifdef FEAT_DIFF
322 int old_topfill = -1;
323#endif
324 int inserted_space = FALSE; /* just inserted a space */
325 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000326 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000327
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000328 /* Remember whether editing was restarted after CTRL-O. */
329 did_restart_edit = restart_edit;
330
Bram Moolenaar071d4272004-06-13 20:20:40 +0000331 /* sleep before redrawing, needed for "CTRL-O :" that results in an
332 * error message */
333 check_for_delay(TRUE);
334
335#ifdef HAVE_SANDBOX
336 /* Don't allow inserting in the sandbox. */
337 if (sandbox != 0)
338 {
339 EMSG(_(e_sandbox));
340 return FALSE;
341 }
342#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000343 /* Don't allow changes in the buffer while editing the cmdline. The
344 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000345 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000346 {
347 EMSG(_(e_secure));
348 return FALSE;
349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000350
351#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000352 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000353 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000354 {
355 EMSG(_(e_secure));
356 return FALSE;
357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000358 ins_compl_clear(); /* clear stuff for CTRL-X mode */
359#endif
360
Bram Moolenaar843ee412004-06-30 16:16:41 +0000361#ifdef FEAT_AUTOCMD
362 /*
363 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
364 */
365 if (cmdchar != 'r' && cmdchar != 'v')
366 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000367# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000368 if (cmdchar == 'R')
369 ptr = (char_u *)"r";
370 else if (cmdchar == 'V')
371 ptr = (char_u *)"v";
372 else
373 ptr = (char_u *)"i";
374 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000375# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000376 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
377 }
378#endif
379
Bram Moolenaar071d4272004-06-13 20:20:40 +0000380#ifdef FEAT_MOUSE
381 /*
382 * When doing a paste with the middle mouse button, Insstart is set to
383 * where the paste started.
384 */
385 if (where_paste_started.lnum != 0)
386 Insstart = where_paste_started;
387 else
388#endif
389 {
390 Insstart = curwin->w_cursor;
391 if (startln)
392 Insstart.col = 0;
393 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000394 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 Insstart_blank_vcol = MAXCOL;
396 if (!did_ai)
397 ai_col = 0;
398
399 if (cmdchar != NUL && restart_edit == 0)
400 {
401 ResetRedobuff();
402 AppendNumberToRedobuff(count);
403#ifdef FEAT_VREPLACE
404 if (cmdchar == 'V' || cmdchar == 'v')
405 {
406 /* "gR" or "gr" command */
407 AppendCharToRedobuff('g');
408 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
409 }
410 else
411#endif
412 {
413 AppendCharToRedobuff(cmdchar);
414 if (cmdchar == 'g') /* "gI" command */
415 AppendCharToRedobuff('I');
416 else if (cmdchar == 'r') /* "r<CR>" command */
417 count = 1; /* insert only one <CR> */
418 }
419 }
420
421 if (cmdchar == 'R')
422 {
423#ifdef FEAT_FKMAP
424 if (p_fkmap && p_ri)
425 {
426 beep_flush();
427 EMSG(farsi_text_3); /* encoded in Farsi */
428 State = INSERT;
429 }
430 else
431#endif
432 State = REPLACE;
433 }
434#ifdef FEAT_VREPLACE
435 else if (cmdchar == 'V' || cmdchar == 'v')
436 {
437 State = VREPLACE;
438 replaceState = VREPLACE;
439 orig_line_count = curbuf->b_ml.ml_line_count;
440 vr_lines_changed = 1;
441 }
442#endif
443 else
444 State = INSERT;
445
446 stop_insert_mode = FALSE;
447
448 /*
449 * Need to recompute the cursor position, it might move when the cursor is
450 * on a TAB or special character.
451 */
452 curs_columns(TRUE);
453
454 /*
455 * Enable langmap or IME, indicated by 'iminsert'.
456 * Note that IME may enabled/disabled without us noticing here, thus the
457 * 'iminsert' value may not reflect what is actually used. It is updated
458 * when hitting <Esc>.
459 */
460 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
461 State |= LANGMAP;
462#ifdef USE_IM_CONTROL
463 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
464#endif
465
Bram Moolenaar071d4272004-06-13 20:20:40 +0000466#ifdef FEAT_MOUSE
467 setmouse();
468#endif
469#ifdef FEAT_CMDL_INFO
470 clear_showcmd();
471#endif
472#ifdef FEAT_RIGHTLEFT
473 /* there is no reverse replace mode */
474 revins_on = (State == INSERT && p_ri);
475 if (revins_on)
476 undisplay_dollar();
477 revins_chars = 0;
478 revins_legal = 0;
479 revins_scol = -1;
480#endif
481
482 /*
483 * Handle restarting Insert mode.
484 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
485 * restart_edit non-zero, and something in the stuff buffer.
486 */
487 if (restart_edit != 0 && stuff_empty())
488 {
489#ifdef FEAT_MOUSE
490 /*
491 * After a paste we consider text typed to be part of the insert for
492 * the pasted text. You can backspace over the pasted text too.
493 */
494 if (where_paste_started.lnum)
495 arrow_used = FALSE;
496 else
497#endif
498 arrow_used = TRUE;
499 restart_edit = 0;
500
501 /*
502 * If the cursor was after the end-of-line before the CTRL-O and it is
503 * now at the end-of-line, put it after the end-of-line (this is not
504 * correct in very rare cases).
505 * Also do this if curswant is greater than the current virtual
506 * column. Eg after "^O$" or "^O80|".
507 */
508 validate_virtcol();
509 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000510 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511 || curwin->w_curswant > curwin->w_virtcol)
512 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
513 {
514 if (ptr[1] == NUL)
515 ++curwin->w_cursor.col;
516#ifdef FEAT_MBYTE
517 else if (has_mbyte)
518 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000519 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 if (ptr[i] == NUL)
521 curwin->w_cursor.col += i;
522 }
523#endif
524 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000525 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 }
527 else
528 arrow_used = FALSE;
529
530 /* we are in insert mode now, don't need to start it anymore */
531 need_start_insertmode = FALSE;
532
533 /* Need to save the line for undo before inserting the first char. */
534 ins_need_undo = TRUE;
535
536#ifdef FEAT_MOUSE
537 where_paste_started.lnum = 0;
538#endif
539#ifdef FEAT_CINDENT
540 can_cindent = TRUE;
541#endif
542#ifdef FEAT_FOLDING
543 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
544 * restarting. */
545 if (!p_im && did_restart_edit == 0)
546 foldOpenCursor();
547#endif
548
549 /*
550 * If 'showmode' is set, show the current (insert/replace/..) mode.
551 * A warning message for changing a readonly file is given here, before
552 * actually changing anything. It's put after the mode, if any.
553 */
554 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000555 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000556 i = showmode();
557
558 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000559 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000560
561#ifdef CURSOR_SHAPE
562 ui_cursor_shape(); /* may show different cursor shape */
563#endif
564#ifdef FEAT_DIGRAPHS
565 do_digraph(-1); /* clear digraphs */
566#endif
567
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000568 /*
569 * Get the current length of the redo buffer, those characters have to be
570 * skipped if we want to get to the inserted characters.
571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000572 ptr = get_inserted();
573 if (ptr == NULL)
574 new_insert_skip = 0;
575 else
576 {
577 new_insert_skip = (int)STRLEN(ptr);
578 vim_free(ptr);
579 }
580
581 old_indent = 0;
582
583 /*
584 * Main loop in Insert mode: repeat until Insert mode is left.
585 */
586 for (;;)
587 {
588#ifdef FEAT_RIGHTLEFT
589 if (!revins_legal)
590 revins_scol = -1; /* reset on illegal motions */
591 else
592 revins_legal = 0;
593#endif
594 if (arrow_used) /* don't repeat insert when arrow key used */
595 count = 0;
596
597 if (stop_insert_mode)
598 {
599 /* ":stopinsert" used or 'insertmode' reset */
600 count = 0;
601 goto doESCkey;
602 }
603
604 /* set curwin->w_curswant for next K_DOWN or K_UP */
605 if (!arrow_used)
606 curwin->w_set_curswant = TRUE;
607
608 /* If there is no typeahead may check for timestamps (e.g., for when a
609 * menu invoked a shell command). */
610 if (stuff_empty())
611 {
612 did_check_timestamps = FALSE;
613 if (need_check_timestamps)
614 check_timestamps(FALSE);
615 }
616
617 /*
618 * When emsg() was called msg_scroll will have been set.
619 */
620 msg_scroll = FALSE;
621
622#ifdef FEAT_GUI
623 /* When 'mousefocus' is set a mouse movement may have taken us to
624 * another window. "need_mouse_correct" may then be set because of an
625 * autocommand. */
626 if (need_mouse_correct)
627 gui_mouse_correct();
628#endif
629
630#ifdef FEAT_FOLDING
631 /* Open fold at the cursor line, according to 'foldopen'. */
632 if (fdo_flags & FDO_INSERT)
633 foldOpenCursor();
634 /* Close folds where the cursor isn't, according to 'foldclose' */
635 if (!char_avail())
636 foldCheckClose();
637#endif
638
639 /*
640 * If we inserted a character at the last position of the last line in
641 * the window, scroll the window one line up. This avoids an extra
642 * redraw.
643 * This is detected when the cursor column is smaller after inserting
644 * something.
645 * Don't do this when the topline changed already, it has
646 * already been adjusted (by insertchar() calling open_line())).
647 */
648 if (curbuf->b_mod_set
649 && curwin->w_p_wrap
650 && !did_backspace
651 && curwin->w_topline == old_topline
652#ifdef FEAT_DIFF
653 && curwin->w_topfill == old_topfill
654#endif
655 )
656 {
657 mincol = curwin->w_wcol;
658 validate_cursor_col();
659
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000660 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000661 && curwin->w_wrow == W_WINROW(curwin)
662 + curwin->w_height - 1 - p_so
663 && (curwin->w_cursor.lnum != curwin->w_topline
664#ifdef FEAT_DIFF
665 || curwin->w_topfill > 0
666#endif
667 ))
668 {
669#ifdef FEAT_DIFF
670 if (curwin->w_topfill > 0)
671 --curwin->w_topfill;
672 else
673#endif
674#ifdef FEAT_FOLDING
675 if (hasFolding(curwin->w_topline, NULL, &old_topline))
676 set_topline(curwin, old_topline + 1);
677 else
678#endif
679 set_topline(curwin, curwin->w_topline + 1);
680 }
681 }
682
683 /* May need to adjust w_topline to show the cursor. */
684 update_topline();
685
686 did_backspace = FALSE;
687
688 validate_cursor(); /* may set must_redraw */
689
690 /*
691 * Redraw the display when no characters are waiting.
692 * Also shows mode, ruler and positions cursor.
693 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000694 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000695
696#ifdef FEAT_SCROLLBIND
697 if (curwin->w_p_scb)
698 do_check_scrollbind(TRUE);
699#endif
700
Bram Moolenaar860cae12010-06-05 23:22:07 +0200701#ifdef FEAT_CURSORBIND
702 if (curwin->w_p_crb)
703 do_check_cursorbind();
704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000705 update_curswant();
706 old_topline = curwin->w_topline;
707#ifdef FEAT_DIFF
708 old_topfill = curwin->w_topfill;
709#endif
710
711#ifdef USE_ON_FLY_SCROLL
712 dont_scroll = FALSE; /* allow scrolling here */
713#endif
714
715 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000716 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000717 */
718 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000719 do
720 {
721 c = safe_vgetc();
722 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000724#ifdef FEAT_AUTOCMD
725 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
726 did_cursorhold = TRUE;
727#endif
728
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729#ifdef FEAT_RIGHTLEFT
730 if (p_hkmap && KeyTyped)
731 c = hkmap(c); /* Hebrew mode mapping */
732#endif
733#ifdef FEAT_FKMAP
734 if (p_fkmap && KeyTyped)
735 c = fkmap(c); /* Farsi mode mapping */
736#endif
737
738#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000739 /*
740 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000741 * and the cursor is still in the completed word. Only when there is
742 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000743 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000744 if (compl_started
745 && pum_wanted()
746 && curwin->w_cursor.col >= compl_col
747 && (compl_shown_match == NULL
748 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000749 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000750 /* BS: Delete one character from "compl_leader". */
751 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000752 && curwin->w_cursor.col > compl_col
753 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000754 continue;
755
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000756 /* When no match was selected or it was edited. */
757 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000758 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000759 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000760 * "compl_leader". Except when at the original match and
761 * there is nothing to add, CTRL-L works like CTRL-P then. */
762 if (c == Ctrl_L
763 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000764 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000765 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000766 {
767 ins_compl_addfrommatch();
768 continue;
769 }
770
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000771 /* A non-white character that fits in with the current
772 * completion: Add to "compl_leader". */
773 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000774 {
775 ins_compl_addleader(c);
776 continue;
777 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000778
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000779 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000780 * compl_enter_selects is set the Enter key does the same. */
781 if (c == Ctrl_Y || (compl_enter_selects
782 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000783 {
784 ins_compl_delete();
785 ins_compl_insert();
786 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000787 }
788 }
789
Bram Moolenaar071d4272004-06-13 20:20:40 +0000790 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
791 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000792 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000793 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000794 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000795#endif
796
Bram Moolenaar488c6512005-08-11 20:09:58 +0000797 /* CTRL-\ CTRL-N goes to Normal mode,
798 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
799 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (c == Ctrl_BSL)
801 {
802 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000803 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 ++no_mapping;
805 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000806 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 --no_mapping;
808 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000809 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000811 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 vungetc(c);
813 c = Ctrl_BSL;
814 }
815 else if (c == Ctrl_G && p_im)
816 continue;
817 else
818 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000819 if (c == Ctrl_O)
820 {
821 ins_ctrl_o();
822 ins_at_eol = FALSE; /* cursor keeps its column */
823 nomove = TRUE;
824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000825 count = 0;
826 goto doESCkey;
827 }
828 }
829
830#ifdef FEAT_DIGRAPHS
831 c = do_digraph(c);
832#endif
833
834#ifdef FEAT_INS_EXPAND
835 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
836 goto docomplete;
837#endif
838 if (c == Ctrl_V || c == Ctrl_Q)
839 {
840 ins_ctrl_v();
841 c = Ctrl_V; /* pretend CTRL-V is last typed character */
842 continue;
843 }
844
845#ifdef FEAT_CINDENT
846 if (cindent_on()
847# ifdef FEAT_INS_EXPAND
848 && ctrl_x_mode == 0
849# endif
850 )
851 {
852 /* A key name preceded by a bang means this key is not to be
853 * inserted. Skip ahead to the re-indenting below.
854 * A key name preceded by a star means that indenting has to be
855 * done before inserting the key. */
856 line_is_white = inindent(0);
857 if (in_cinkeys(c, '!', line_is_white))
858 goto force_cindent;
859 if (can_cindent && in_cinkeys(c, '*', line_is_white)
860 && stop_arrow() == OK)
861 do_c_expr_indent();
862 }
863#endif
864
865#ifdef FEAT_RIGHTLEFT
866 if (curwin->w_p_rl)
867 switch (c)
868 {
869 case K_LEFT: c = K_RIGHT; break;
870 case K_S_LEFT: c = K_S_RIGHT; break;
871 case K_C_LEFT: c = K_C_RIGHT; break;
872 case K_RIGHT: c = K_LEFT; break;
873 case K_S_RIGHT: c = K_S_LEFT; break;
874 case K_C_RIGHT: c = K_C_LEFT; break;
875 }
876#endif
877
878#ifdef FEAT_VISUAL
879 /*
880 * If 'keymodel' contains "startsel", may start selection. If it
881 * does, a CTRL-O and c will be stuffed, we need to get these
882 * characters.
883 */
884 if (ins_start_select(c))
885 continue;
886#endif
887
888 /*
889 * The big switch to handle a character in insert mode.
890 */
891 switch (c)
892 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000893 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 if (echeck_abbr(ESC + ABBR_OFF))
895 break;
896 /*FALLTHROUGH*/
897
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000898 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000899#ifdef FEAT_CMDWIN
900 if (c == Ctrl_C && cmdwin_type != 0)
901 {
902 /* Close the cmdline window. */
903 cmdwin_result = K_IGNORE;
904 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000905 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 goto doESCkey;
907 }
908#endif
909
910#ifdef UNIX
911do_intr:
912#endif
913 /* when 'insertmode' set, and not halfway a mapping, don't leave
914 * Insert mode */
915 if (goto_im())
916 {
917 if (got_int)
918 {
919 (void)vgetc(); /* flush all buffers */
920 got_int = FALSE;
921 }
922 else
923 vim_beep();
924 break;
925 }
926doESCkey:
927 /*
928 * This is the ONLY return from edit()!
929 */
930 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
931 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000932 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000933 o_lnum = curwin->w_cursor.lnum;
934
Bram Moolenaar488c6512005-08-11 20:09:58 +0000935 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000936 {
937#ifdef FEAT_AUTOCMD
938 if (cmdchar != 'r' && cmdchar != 'v')
939 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
940 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000941 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000942#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000943 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000944 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000945 continue;
946
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000947 case Ctrl_Z: /* suspend when 'insertmode' set */
948 if (!p_im)
949 goto normalchar; /* insert CTRL-Z as normal char */
950 stuffReadbuff((char_u *)":st\r");
951 c = Ctrl_O;
952 /*FALLTHROUGH*/
953
954 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000955#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000956 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000957 goto docomplete;
958#endif
959 if (echeck_abbr(Ctrl_O + ABBR_OFF))
960 break;
961 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000962
963#ifdef FEAT_VIRTUALEDIT
964 /* don't move the cursor left when 'virtualedit' has "onemore". */
965 if (ve_flags & VE_ONEMORE)
966 {
967 ins_at_eol = FALSE;
968 nomove = TRUE;
969 }
970#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000971 count = 0;
972 goto doESCkey;
973
Bram Moolenaar572cb562005-08-05 21:35:02 +0000974 case K_INS: /* toggle insert/replace mode */
975 case K_KINS:
976 ins_insert(replaceState);
977 break;
978
979 case K_SELECT: /* end of Select mode mapping - ignore */
980 break;
981
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000982#ifdef FEAT_SNIFF
983 case K_SNIFF: /* Sniff command received */
984 stuffcharReadbuff(K_SNIFF);
985 goto doESCkey;
986#endif
987
988 case K_HELP: /* Help key works like <ESC> <Help> */
989 case K_F1:
990 case K_XF1:
991 stuffcharReadbuff(K_HELP);
992 if (p_im)
993 need_start_insertmode = TRUE;
994 goto doESCkey;
995
996#ifdef FEAT_NETBEANS_INTG
997 case K_F21: /* NetBeans command */
998 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000999 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001000 --no_mapping;
1001 netbeans_keycommand(i);
1002 break;
1003#endif
1004
1005 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001006 case NUL:
1007 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001008 /* For ^@ the trailing ESC will end the insert, unless there is an
1009 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001010 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1011 && c != Ctrl_A && !p_im)
1012 goto doESCkey; /* quit insert mode */
1013 inserted_space = FALSE;
1014 break;
1015
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001016 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 ins_reg();
1018 auto_format(FALSE, TRUE);
1019 inserted_space = FALSE;
1020 break;
1021
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 ins_ctrl_g();
1024 break;
1025
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001026 case Ctrl_HAT: /* switch input mode and/or langmap */
1027 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 break;
1029
1030#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001031 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032 if (!p_ari)
1033 goto normalchar;
1034 ins_ctrl_();
1035 break;
1036#endif
1037
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1040 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1041 goto docomplete;
1042#endif
1043 /* FALLTHROUGH */
1044
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046# ifdef FEAT_INS_EXPAND
1047 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1048 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001049 if (has_compl_option(FALSE))
1050 goto docomplete;
1051 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052 }
1053# endif
1054 ins_shift(c, lastc);
1055 auto_format(FALSE, TRUE);
1056 inserted_space = FALSE;
1057 break;
1058
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001059 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060 case K_KDEL:
1061 ins_del();
1062 auto_format(FALSE, TRUE);
1063 break;
1064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 case Ctrl_H:
1067 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1068 auto_format(FALSE, TRUE);
1069 break;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1073 auto_format(FALSE, TRUE);
1074 break;
1075
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001076 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001077# ifdef FEAT_COMPL_FUNC
1078 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001079 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001080 goto docomplete;
1081# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1083 auto_format(FALSE, TRUE);
1084 inserted_space = FALSE;
1085 break;
1086
1087#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001088 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 case K_LEFTMOUSE_NM:
1090 case K_LEFTDRAG:
1091 case K_LEFTRELEASE:
1092 case K_LEFTRELEASE_NM:
1093 case K_MIDDLEMOUSE:
1094 case K_MIDDLEDRAG:
1095 case K_MIDDLERELEASE:
1096 case K_RIGHTMOUSE:
1097 case K_RIGHTDRAG:
1098 case K_RIGHTRELEASE:
1099 case K_X1MOUSE:
1100 case K_X1DRAG:
1101 case K_X1RELEASE:
1102 case K_X2MOUSE:
1103 case K_X2DRAG:
1104 case K_X2RELEASE:
1105 ins_mouse(c);
1106 break;
1107
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001108 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001109 ins_mousescroll(FALSE);
1110 break;
1111
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001112 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001113 ins_mousescroll(TRUE);
1114 break;
1115#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001116#ifdef FEAT_GUI_TABLINE
1117 case K_TABLINE:
1118 case K_TABMENU:
1119 ins_tabline(c);
1120 break;
1121#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001122
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001123 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001124 break;
1125
Bram Moolenaar754b5602006-02-09 23:53:20 +00001126#ifdef FEAT_AUTOCMD
1127 case K_CURSORHOLD: /* Didn't type something for a while. */
1128 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1129 did_cursorhold = TRUE;
1130 break;
1131#endif
1132
Bram Moolenaar4770d092006-01-12 23:22:24 +00001133#ifdef FEAT_GUI_W32
1134 /* On Win32 ignore <M-F4>, we get it when closing the window was
1135 * cancelled. */
1136 case K_F4:
1137 if (mod_mask != MOD_MASK_ALT)
1138 goto normalchar;
1139 break;
1140#endif
1141
Bram Moolenaar071d4272004-06-13 20:20:40 +00001142#ifdef FEAT_GUI
1143 case K_VER_SCROLLBAR:
1144 ins_scroll();
1145 break;
1146
1147 case K_HOR_SCROLLBAR:
1148 ins_horscroll();
1149 break;
1150#endif
1151
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001152 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001153 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001154 case K_S_HOME:
1155 case K_C_HOME:
1156 ins_home(c);
1157 break;
1158
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001159 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001160 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001161 case K_S_END:
1162 case K_C_END:
1163 ins_end(c);
1164 break;
1165
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001166 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001167 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1168 ins_s_left();
1169 else
1170 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001171 break;
1172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001173 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 case K_C_LEFT:
1175 ins_s_left();
1176 break;
1177
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001178 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001179 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1180 ins_s_right();
1181 else
1182 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001183 break;
1184
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001185 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 case K_C_RIGHT:
1187 ins_s_right();
1188 break;
1189
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001190 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001191#ifdef FEAT_INS_EXPAND
1192 if (pum_visible())
1193 goto docomplete;
1194#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001195 if (mod_mask & MOD_MASK_SHIFT)
1196 ins_pageup();
1197 else
1198 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001199 break;
1200
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001201 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001202 case K_PAGEUP:
1203 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001204#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001205 if (pum_visible())
1206 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001207#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001208 ins_pageup();
1209 break;
1210
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001211 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001212#ifdef FEAT_INS_EXPAND
1213 if (pum_visible())
1214 goto docomplete;
1215#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001216 if (mod_mask & MOD_MASK_SHIFT)
1217 ins_pagedown();
1218 else
1219 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 break;
1221
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001222 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 case K_PAGEDOWN:
1224 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001225#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001226 if (pum_visible())
1227 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001229 ins_pagedown();
1230 break;
1231
1232#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001233 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 ins_drop();
1235 break;
1236#endif
1237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001238 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 c = TAB;
1240 /* FALLTHROUGH */
1241
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001242 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1244 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1245 goto docomplete;
1246#endif
1247 inserted_space = FALSE;
1248 if (ins_tab())
1249 goto normalchar; /* insert TAB as a normal char */
1250 auto_format(FALSE, TRUE);
1251 break;
1252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 c = CAR;
1255 /* FALLTHROUGH */
1256 case CAR:
1257 case NL:
1258#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1259 /* In a quickfix window a <CR> jumps to the error under the
1260 * cursor. */
1261 if (bt_quickfix(curbuf) && c == CAR)
1262 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001263 if (curwin->w_llist_ref == NULL) /* quickfix window */
1264 do_cmdline_cmd((char_u *)".cc");
1265 else /* location list window */
1266 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001267 break;
1268 }
1269#endif
1270#ifdef FEAT_CMDWIN
1271 if (cmdwin_type != 0)
1272 {
1273 /* Execute the command in the cmdline window. */
1274 cmdwin_result = CAR;
1275 goto doESCkey;
1276 }
1277#endif
1278 if (ins_eol(c) && !p_im)
1279 goto doESCkey; /* out of memory */
1280 auto_format(FALSE, FALSE);
1281 inserted_space = FALSE;
1282 break;
1283
Bram Moolenaar860cae12010-06-05 23:22:07 +02001284#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001285 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001286# ifdef FEAT_INS_EXPAND
1287 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1288 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001289 if (has_compl_option(TRUE))
1290 goto docomplete;
1291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 }
1293# endif
1294# ifdef FEAT_DIGRAPHS
1295 c = ins_digraph();
1296 if (c == NUL)
1297 break;
1298# endif
1299 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301
1302#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001303 case Ctrl_X: /* Enter CTRL-X mode */
1304 ins_ctrl_x();
1305 break;
1306
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001307 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001308 if (ctrl_x_mode != CTRL_X_TAGS)
1309 goto normalchar;
1310 goto docomplete;
1311
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001312 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313 if (ctrl_x_mode != CTRL_X_FILES)
1314 goto normalchar;
1315 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001316
1317 case 's': /* Spelling completion after ^X */
1318 case Ctrl_S:
1319 if (ctrl_x_mode != CTRL_X_SPELL)
1320 goto normalchar;
1321 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322#endif
1323
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001324 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001325#ifdef FEAT_INS_EXPAND
1326 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1327#endif
1328 {
1329 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1330 if (p_im)
1331 {
1332 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1333 break;
1334 goto doESCkey;
1335 }
1336 goto normalchar;
1337 }
1338#ifdef FEAT_INS_EXPAND
1339 /* FALLTHROUGH */
1340
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001341 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 case Ctrl_N:
1343 /* if 'complete' is empty then plain ^P is no longer special,
1344 * but it is under other ^X modes */
1345 if (*curbuf->b_p_cpt == NUL
1346 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001347 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 goto normalchar;
1349
1350docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001351 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001353 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001354 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 break;
1356#endif /* FEAT_INS_EXPAND */
1357
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001358 case Ctrl_Y: /* copy from previous line or scroll down */
1359 case Ctrl_E: /* copy from next line or scroll up */
1360 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001361 break;
1362
1363 default:
1364#ifdef UNIX
1365 if (c == intr_char) /* special interrupt char */
1366 goto do_intr;
1367#endif
1368
1369 /*
1370 * Insert a nomal character.
1371 */
1372normalchar:
1373#ifdef FEAT_SMARTINDENT
1374 /* Try to perform smart-indenting. */
1375 ins_try_si(c);
1376#endif
1377
1378 if (c == ' ')
1379 {
1380 inserted_space = TRUE;
1381#ifdef FEAT_CINDENT
1382 if (inindent(0))
1383 can_cindent = FALSE;
1384#endif
1385 if (Insstart_blank_vcol == MAXCOL
1386 && curwin->w_cursor.lnum == Insstart.lnum)
1387 Insstart_blank_vcol = get_nolist_virtcol();
1388 }
1389
1390 if (vim_iswordc(c) || !echeck_abbr(
1391#ifdef FEAT_MBYTE
1392 /* Add ABBR_OFF for characters above 0x100, this is
1393 * what check_abbr() expects. */
1394 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1395#endif
1396 c))
1397 {
1398 insert_special(c, FALSE, FALSE);
1399#ifdef FEAT_RIGHTLEFT
1400 revins_legal++;
1401 revins_chars++;
1402#endif
1403 }
1404
1405 auto_format(FALSE, TRUE);
1406
1407#ifdef FEAT_FOLDING
1408 /* When inserting a character the cursor line must never be in a
1409 * closed fold. */
1410 foldOpenCursor();
1411#endif
1412 break;
1413 } /* end of switch (c) */
1414
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001415#ifdef FEAT_AUTOCMD
1416 /* If typed something may trigger CursorHoldI again. */
1417 if (c != K_CURSORHOLD)
1418 did_cursorhold = FALSE;
1419#endif
1420
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 /* If the cursor was moved we didn't just insert a space */
1422 if (arrow_used)
1423 inserted_space = FALSE;
1424
1425#ifdef FEAT_CINDENT
1426 if (can_cindent && cindent_on()
1427# ifdef FEAT_INS_EXPAND
1428 && ctrl_x_mode == 0
1429# endif
1430 )
1431 {
1432force_cindent:
1433 /*
1434 * Indent now if a key was typed that is in 'cinkeys'.
1435 */
1436 if (in_cinkeys(c, ' ', line_is_white))
1437 {
1438 if (stop_arrow() == OK)
1439 /* re-indent the current line */
1440 do_c_expr_indent();
1441 }
1442 }
1443#endif /* FEAT_CINDENT */
1444
1445 } /* for (;;) */
1446 /* NOTREACHED */
1447}
1448
1449/*
1450 * Redraw for Insert mode.
1451 * This is postponed until getting the next character to make '$' in the 'cpo'
1452 * option work correctly.
1453 * Only redraw when there are no characters available. This speeds up
1454 * inserting sequences of characters (e.g., for CTRL-R).
1455 */
1456 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001457ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001458 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001459{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001460#ifdef FEAT_CONCEAL
1461 linenr_T conceal_old_cursor_line = 0;
1462 linenr_T conceal_new_cursor_line = 0;
1463 int conceal_update_lines = FALSE;
1464#endif
1465
Bram Moolenaar071d4272004-06-13 20:20:40 +00001466 if (!char_avail())
1467 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001468#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001469 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1470 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001471 if (ready && (
1472# ifdef FEAT_AUTOCMD
1473 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001474# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001475# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1476 ||
1477# endif
1478# ifdef FEAT_CONCEAL
1479 curwin->w_p_conceal
1480# endif
1481 )
1482 && !equalpos(last_cursormoved, curwin->w_cursor)
1483# ifdef FEAT_INS_EXPAND
1484 && !pum_visible()
1485# endif
1486 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001487 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001488# ifdef FEAT_SYN_HL
1489 /* Need to update the screen first, to make sure syntax
1490 * highlighting is correct after making a change (e.g., inserting
1491 * a "(". The autocommand may also require a redraw, so it's done
1492 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001493 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001494 update_screen(0);
1495# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001496# ifdef FEAT_AUTOCMD
1497 if (has_cursormovedI())
1498 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1499# endif
1500# ifdef FEAT_CONCEAL
1501 if (curwin->w_p_conceal)
1502 {
1503 conceal_old_cursor_line = last_cursormoved.lnum;
1504 conceal_new_cursor_line = curwin->w_cursor.lnum;
1505 conceal_update_lines = TRUE;
1506 }
1507# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001508 last_cursormoved = curwin->w_cursor;
1509 }
1510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001511 if (must_redraw)
1512 update_screen(0);
1513 else if (clear_cmdline || redraw_cmdline)
1514 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001515# if defined(FEAT_CONCEAL)
1516 if (conceal_update_lines
1517 && conceal_old_cursor_line != conceal_new_cursor_line)
1518 {
1519 update_single_line(curwin, conceal_old_cursor_line);
1520 update_single_line(curwin, conceal_new_cursor_line);
1521 curwin->w_valid &= ~VALID_CROW;
1522 }
1523# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001524 showruler(FALSE);
1525 setcursor();
1526 emsg_on_display = FALSE; /* may remove error message now */
1527 }
1528}
1529
1530/*
1531 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1532 */
1533 static void
1534ins_ctrl_v()
1535{
1536 int c;
1537
1538 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001539 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001540
1541 if (redrawing() && !char_avail())
1542 edit_putchar('^', TRUE);
1543 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1544
1545#ifdef FEAT_CMDL_INFO
1546 add_to_showcmd_c(Ctrl_V);
1547#endif
1548
1549 c = get_literal();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02001550 edit_unputchar(); /* when line fits in 'columns' the '^' is at the start
1551 of the next line and will not be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001552#ifdef FEAT_CMDL_INFO
1553 clear_showcmd();
1554#endif
1555 insert_special(c, FALSE, TRUE);
1556#ifdef FEAT_RIGHTLEFT
1557 revins_chars++;
1558 revins_legal++;
1559#endif
1560}
1561
1562/*
1563 * Put a character directly onto the screen. It's not stored in a buffer.
1564 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1565 */
1566static int pc_status;
1567#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1568#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1569#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1570#define PC_STATUS_SET 3 /* pc_bytes was filled */
1571#ifdef FEAT_MBYTE
1572static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1573#else
1574static char_u pc_bytes[2]; /* saved bytes */
1575#endif
1576static int pc_attr;
1577static int pc_row;
1578static int pc_col;
1579
1580 void
1581edit_putchar(c, highlight)
1582 int c;
1583 int highlight;
1584{
1585 int attr;
1586
1587 if (ScreenLines != NULL)
1588 {
1589 update_topline(); /* just in case w_topline isn't valid */
1590 validate_cursor();
1591 if (highlight)
1592 attr = hl_attr(HLF_8);
1593 else
1594 attr = 0;
1595 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1596 pc_col = W_WINCOL(curwin);
1597#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1598 pc_status = PC_STATUS_UNSET;
1599#endif
1600#ifdef FEAT_RIGHTLEFT
1601 if (curwin->w_p_rl)
1602 {
1603 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1604# ifdef FEAT_MBYTE
1605 if (has_mbyte)
1606 {
1607 int fix_col = mb_fix_col(pc_col, pc_row);
1608
1609 if (fix_col != pc_col)
1610 {
1611 screen_putchar(' ', pc_row, fix_col, attr);
1612 --curwin->w_wcol;
1613 pc_status = PC_STATUS_RIGHT;
1614 }
1615 }
1616# endif
1617 }
1618 else
1619#endif
1620 {
1621 pc_col += curwin->w_wcol;
1622#ifdef FEAT_MBYTE
1623 if (mb_lefthalve(pc_row, pc_col))
1624 pc_status = PC_STATUS_LEFT;
1625#endif
1626 }
1627
1628 /* save the character to be able to put it back */
1629#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1630 if (pc_status == PC_STATUS_UNSET)
1631#endif
1632 {
1633 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1634 pc_status = PC_STATUS_SET;
1635 }
1636 screen_putchar(c, pc_row, pc_col, attr);
1637 }
1638}
1639
1640/*
1641 * Undo the previous edit_putchar().
1642 */
1643 void
1644edit_unputchar()
1645{
1646 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1647 {
1648#if defined(FEAT_MBYTE)
1649 if (pc_status == PC_STATUS_RIGHT)
1650 ++curwin->w_wcol;
1651 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1652 redrawWinline(curwin->w_cursor.lnum, FALSE);
1653 else
1654#endif
1655 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1656 }
1657}
1658
1659/*
1660 * Called when p_dollar is set: display a '$' at the end of the changed text
1661 * Only works when cursor is in the line that changes.
1662 */
1663 void
1664display_dollar(col)
1665 colnr_T col;
1666{
1667 colnr_T save_col;
1668
1669 if (!redrawing())
1670 return;
1671
1672 cursor_off();
1673 save_col = curwin->w_cursor.col;
1674 curwin->w_cursor.col = col;
1675#ifdef FEAT_MBYTE
1676 if (has_mbyte)
1677 {
1678 char_u *p;
1679
1680 /* If on the last byte of a multi-byte move to the first byte. */
1681 p = ml_get_curline();
1682 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1683 }
1684#endif
1685 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1686 if (curwin->w_wcol < W_WIDTH(curwin))
1687 {
1688 edit_putchar('$', FALSE);
1689 dollar_vcol = curwin->w_virtcol;
1690 }
1691 curwin->w_cursor.col = save_col;
1692}
1693
1694/*
1695 * Call this function before moving the cursor from the normal insert position
1696 * in insert mode.
1697 */
1698 static void
1699undisplay_dollar()
1700{
1701 if (dollar_vcol)
1702 {
1703 dollar_vcol = 0;
1704 redrawWinline(curwin->w_cursor.lnum, FALSE);
1705 }
1706}
1707
1708/*
1709 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1710 * Keep the cursor on the same character.
1711 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1712 * type == INDENT_DEC decrease indent (for CTRL-D)
1713 * type == INDENT_SET set indent to "amount"
1714 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1715 */
1716 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001717change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001718 int type;
1719 int amount;
1720 int round;
1721 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001722 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001723{
1724 int vcol;
1725 int last_vcol;
1726 int insstart_less; /* reduction for Insstart.col */
1727 int new_cursor_col;
1728 int i;
1729 char_u *ptr;
1730 int save_p_list;
1731 int start_col;
1732 colnr_T vc;
1733#ifdef FEAT_VREPLACE
1734 colnr_T orig_col = 0; /* init for GCC */
1735 char_u *new_line, *orig_line = NULL; /* init for GCC */
1736
1737 /* VREPLACE mode needs to know what the line was like before changing */
1738 if (State & VREPLACE_FLAG)
1739 {
1740 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1741 orig_col = curwin->w_cursor.col;
1742 }
1743#endif
1744
1745 /* for the following tricks we don't want list mode */
1746 save_p_list = curwin->w_p_list;
1747 curwin->w_p_list = FALSE;
1748 vc = getvcol_nolist(&curwin->w_cursor);
1749 vcol = vc;
1750
1751 /*
1752 * For Replace mode we need to fix the replace stack later, which is only
1753 * possible when the cursor is in the indent. Remember the number of
1754 * characters before the cursor if it's possible.
1755 */
1756 start_col = curwin->w_cursor.col;
1757
1758 /* determine offset from first non-blank */
1759 new_cursor_col = curwin->w_cursor.col;
1760 beginline(BL_WHITE);
1761 new_cursor_col -= curwin->w_cursor.col;
1762
1763 insstart_less = curwin->w_cursor.col;
1764
1765 /*
1766 * If the cursor is in the indent, compute how many screen columns the
1767 * cursor is to the left of the first non-blank.
1768 */
1769 if (new_cursor_col < 0)
1770 vcol = get_indent() - vcol;
1771
1772 if (new_cursor_col > 0) /* can't fix replace stack */
1773 start_col = -1;
1774
1775 /*
1776 * Set the new indent. The cursor will be put on the first non-blank.
1777 */
1778 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001779 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780 else
1781 {
1782#ifdef FEAT_VREPLACE
1783 int save_State = State;
1784
1785 /* Avoid being called recursively. */
1786 if (State & VREPLACE_FLAG)
1787 State = INSERT;
1788#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001789 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790#ifdef FEAT_VREPLACE
1791 State = save_State;
1792#endif
1793 }
1794 insstart_less -= curwin->w_cursor.col;
1795
1796 /*
1797 * Try to put cursor on same character.
1798 * If the cursor is at or after the first non-blank in the line,
1799 * compute the cursor column relative to the column of the first
1800 * non-blank character.
1801 * If we are not in insert mode, leave the cursor on the first non-blank.
1802 * If the cursor is before the first non-blank, position it relative
1803 * to the first non-blank, counted in screen columns.
1804 */
1805 if (new_cursor_col >= 0)
1806 {
1807 /*
1808 * When changing the indent while the cursor is touching it, reset
1809 * Insstart_col to 0.
1810 */
1811 if (new_cursor_col == 0)
1812 insstart_less = MAXCOL;
1813 new_cursor_col += curwin->w_cursor.col;
1814 }
1815 else if (!(State & INSERT))
1816 new_cursor_col = curwin->w_cursor.col;
1817 else
1818 {
1819 /*
1820 * Compute the screen column where the cursor should be.
1821 */
1822 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001823 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001824
1825 /*
1826 * Advance the cursor until we reach the right screen column.
1827 */
1828 vcol = last_vcol = 0;
1829 new_cursor_col = -1;
1830 ptr = ml_get_curline();
1831 while (vcol <= (int)curwin->w_virtcol)
1832 {
1833 last_vcol = vcol;
1834#ifdef FEAT_MBYTE
1835 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001836 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837 else
1838#endif
1839 ++new_cursor_col;
1840 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1841 }
1842 vcol = last_vcol;
1843
1844 /*
1845 * May need to insert spaces to be able to position the cursor on
1846 * the right screen column.
1847 */
1848 if (vcol != (int)curwin->w_virtcol)
1849 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001850 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001852 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853 if (ptr != NULL)
1854 {
1855 new_cursor_col += i;
1856 ptr[i] = NUL;
1857 while (--i >= 0)
1858 ptr[i] = ' ';
1859 ins_str(ptr);
1860 vim_free(ptr);
1861 }
1862 }
1863
1864 /*
1865 * When changing the indent while the cursor is in it, reset
1866 * Insstart_col to 0.
1867 */
1868 insstart_less = MAXCOL;
1869 }
1870
1871 curwin->w_p_list = save_p_list;
1872
1873 if (new_cursor_col <= 0)
1874 curwin->w_cursor.col = 0;
1875 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001876 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877 curwin->w_set_curswant = TRUE;
1878 changed_cline_bef_curs();
1879
1880 /*
1881 * May have to adjust the start of the insert.
1882 */
1883 if (State & INSERT)
1884 {
1885 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1886 {
1887 if ((int)Insstart.col <= insstart_less)
1888 Insstart.col = 0;
1889 else
1890 Insstart.col -= insstart_less;
1891 }
1892 if ((int)ai_col <= insstart_less)
1893 ai_col = 0;
1894 else
1895 ai_col -= insstart_less;
1896 }
1897
1898 /*
1899 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1900 * If the number of characters before the cursor decreased, need to pop a
1901 * few characters from the replace stack.
1902 * If the number of characters before the cursor increased, need to push a
1903 * few NULs onto the replace stack.
1904 */
1905 if (REPLACE_NORMAL(State) && start_col >= 0)
1906 {
1907 while (start_col > (int)curwin->w_cursor.col)
1908 {
1909 replace_join(0); /* remove a NUL from the replace stack */
1910 --start_col;
1911 }
1912 while (start_col < (int)curwin->w_cursor.col || replaced)
1913 {
1914 replace_push(NUL);
1915 if (replaced)
1916 {
1917 replace_push(replaced);
1918 replaced = NUL;
1919 }
1920 ++start_col;
1921 }
1922 }
1923
1924#ifdef FEAT_VREPLACE
1925 /*
1926 * For VREPLACE mode, we also have to fix the replace stack. In this case
1927 * it is always possible because we backspace over the whole line and then
1928 * put it back again the way we wanted it.
1929 */
1930 if (State & VREPLACE_FLAG)
1931 {
1932 /* If orig_line didn't allocate, just return. At least we did the job,
1933 * even if you can't backspace. */
1934 if (orig_line == NULL)
1935 return;
1936
1937 /* Save new line */
1938 new_line = vim_strsave(ml_get_curline());
1939 if (new_line == NULL)
1940 return;
1941
1942 /* We only put back the new line up to the cursor */
1943 new_line[curwin->w_cursor.col] = NUL;
1944
1945 /* Put back original line */
1946 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1947 curwin->w_cursor.col = orig_col;
1948
1949 /* Backspace from cursor to start of line */
1950 backspace_until_column(0);
1951
1952 /* Insert new stuff into line again */
1953 ins_bytes(new_line);
1954
1955 vim_free(new_line);
1956 }
1957#endif
1958}
1959
1960/*
1961 * Truncate the space at the end of a line. This is to be used only in an
1962 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1963 * modes.
1964 */
1965 void
1966truncate_spaces(line)
1967 char_u *line;
1968{
1969 int i;
1970
1971 /* find start of trailing white space */
1972 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1973 {
1974 if (State & REPLACE_FLAG)
1975 replace_join(0); /* remove a NUL from the replace stack */
1976 }
1977 line[i + 1] = NUL;
1978}
1979
1980#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1981 || defined(FEAT_COMMENTS) || defined(PROTO)
1982/*
1983 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
1984 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001985 * Will attempt not to go before "col" even when there is a composing
1986 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 */
1988 void
1989backspace_until_column(col)
1990 int col;
1991{
1992 while ((int)curwin->w_cursor.col > col)
1993 {
1994 curwin->w_cursor.col--;
1995 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00001996 replace_do_bs(col);
1997 else if (!del_char_after_col(col))
1998 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001999 }
2000}
2001#endif
2002
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002003/*
2004 * Like del_char(), but make sure not to go before column "limit_col".
2005 * Only matters when there are composing characters.
2006 * Return TRUE when something was deleted.
2007 */
2008 static int
2009del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002010 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002011{
2012#ifdef FEAT_MBYTE
2013 if (enc_utf8 && limit_col >= 0)
2014 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002015 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002016
2017 /* Make sure the cursor is at the start of a character, but
2018 * skip forward again when going too far back because of a
2019 * composing character. */
2020 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002021 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002022 {
2023 int l = utf_ptr2len(ml_get_cursor());
2024
2025 if (l == 0) /* end of line */
2026 break;
2027 curwin->w_cursor.col += l;
2028 }
2029 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2030 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002031 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002032 }
2033 else
2034#endif
2035 (void)del_char(FALSE);
2036 return TRUE;
2037}
2038
Bram Moolenaar071d4272004-06-13 20:20:40 +00002039#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2040/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002041 * CTRL-X pressed in Insert mode.
2042 */
2043 static void
2044ins_ctrl_x()
2045{
2046 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2047 * CTRL-V works like CTRL-N */
2048 if (ctrl_x_mode != CTRL_X_CMDLINE)
2049 {
2050 /* if the next ^X<> won't ADD nothing, then reset
2051 * compl_cont_status */
2052 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002053 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002054 else
2055 compl_cont_status = 0;
2056 /* We're not sure which CTRL-X mode it will be yet */
2057 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2058 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2059 edit_submode_pre = NULL;
2060 showmode();
2061 }
2062}
2063
2064/*
2065 * Return TRUE if the 'dict' or 'tsr' option can be used.
2066 */
2067 static int
2068has_compl_option(dict_opt)
2069 int dict_opt;
2070{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002071 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002072# ifdef FEAT_SPELL
2073 && !curwin->w_p_spell
2074# endif
2075 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002076 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2077 {
2078 ctrl_x_mode = 0;
2079 edit_submode = NULL;
2080 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2081 : (char_u *)_("'thesaurus' option is empty"),
2082 hl_attr(HLF_E));
2083 if (emsg_silent == 0)
2084 {
2085 vim_beep();
2086 setcursor();
2087 out_flush();
2088 ui_delay(2000L, FALSE);
2089 }
2090 return FALSE;
2091 }
2092 return TRUE;
2093}
2094
2095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002096 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2097 * This depends on the current mode.
2098 */
2099 int
2100vim_is_ctrl_x_key(c)
2101 int c;
2102{
2103 /* Always allow ^R - let it's results then be checked */
2104 if (c == Ctrl_R)
2105 return TRUE;
2106
Bram Moolenaare3226be2005-12-18 22:10:00 +00002107 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002108 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002109 return TRUE;
2110
Bram Moolenaar071d4272004-06-13 20:20:40 +00002111 switch (ctrl_x_mode)
2112 {
2113 case 0: /* Not in any CTRL-X mode */
2114 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2115 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002116 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002117 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2118 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2119 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002120 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2121 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002122 case CTRL_X_SCROLL:
2123 return (c == Ctrl_Y || c == Ctrl_E);
2124 case CTRL_X_WHOLE_LINE:
2125 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2126 case CTRL_X_FILES:
2127 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2128 case CTRL_X_DICTIONARY:
2129 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2130 case CTRL_X_THESAURUS:
2131 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2132 case CTRL_X_TAGS:
2133 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2134#ifdef FEAT_FIND_ID
2135 case CTRL_X_PATH_PATTERNS:
2136 return (c == Ctrl_P || c == Ctrl_N);
2137 case CTRL_X_PATH_DEFINES:
2138 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2139#endif
2140 case CTRL_X_CMDLINE:
2141 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2142 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002143#ifdef FEAT_COMPL_FUNC
2144 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002145 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002146 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002147 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002148#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002149 case CTRL_X_SPELL:
2150 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002151 }
2152 EMSG(_(e_internal));
2153 return FALSE;
2154}
2155
2156/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002157 * Return TRUE when character "c" is part of the item currently being
2158 * completed. Used to decide whether to abandon complete mode when the menu
2159 * is visible.
2160 */
2161 static int
2162ins_compl_accept_char(c)
2163 int c;
2164{
2165 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2166 /* When expanding an identifier only accept identifier chars. */
2167 return vim_isIDc(c);
2168
2169 switch (ctrl_x_mode)
2170 {
2171 case CTRL_X_FILES:
2172 /* When expanding file name only accept file name chars. But not
2173 * path separators, so that "proto/<Tab>" expands files in
2174 * "proto", not "proto/" as a whole */
2175 return vim_isfilec(c) && !vim_ispathsep(c);
2176
2177 case CTRL_X_CMDLINE:
2178 case CTRL_X_OMNI:
2179 /* Command line and Omni completion can work with just about any
2180 * printable character, but do stop at white space. */
2181 return vim_isprintc(c) && !vim_iswhite(c);
2182
2183 case CTRL_X_WHOLE_LINE:
2184 /* For while line completion a space can be part of the line. */
2185 return vim_isprintc(c);
2186 }
2187 return vim_iswordc(c);
2188}
2189
2190/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002191 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002192 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002193 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002194 * the rest of the word to be in -- webb
2195 */
2196 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002197ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002198 char_u *str;
2199 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002200 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002201 char_u *fname;
2202 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002203 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002204{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002205 char_u *p;
2206 int i, c;
2207 int actual_len; /* Take multi-byte characters */
2208 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002209 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002210 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 int has_lower = FALSE;
2212 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002214 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002216 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217
Bram Moolenaara2993e12007-08-12 14:38:46 +00002218 /* Find actual length of completion. */
2219#ifdef FEAT_MBYTE
2220 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002221 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002222 p = str;
2223 actual_len = 0;
2224 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002225 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002226 mb_ptr_adv(p);
2227 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002228 }
2229 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002230 else
2231#endif
2232 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233
Bram Moolenaara2993e12007-08-12 14:38:46 +00002234 /* Find actual length of original text. */
2235#ifdef FEAT_MBYTE
2236 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002237 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002238 p = compl_orig_text;
2239 actual_compl_length = 0;
2240 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002242 mb_ptr_adv(p);
2243 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 }
2245 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002246 else
2247#endif
2248 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002249
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002250 /* "actual_len" may be smaller than "actual_compl_length" when using
2251 * thesaurus, only use the minimum when comparing. */
2252 min_len = actual_len < actual_compl_length
2253 ? actual_len : actual_compl_length;
2254
Bram Moolenaara2993e12007-08-12 14:38:46 +00002255 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002256 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002257 if (wca != NULL)
2258 {
2259 p = str;
2260 for (i = 0; i < actual_len; ++i)
2261#ifdef FEAT_MBYTE
2262 if (has_mbyte)
2263 wca[i] = mb_ptr2char_adv(&p);
2264 else
2265#endif
2266 wca[i] = *(p++);
2267
2268 /* Rule 1: Were any chars converted to lower? */
2269 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002270 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002271 {
2272#ifdef FEAT_MBYTE
2273 if (has_mbyte)
2274 c = mb_ptr2char_adv(&p);
2275 else
2276#endif
2277 c = *(p++);
2278 if (MB_ISLOWER(c))
2279 {
2280 has_lower = TRUE;
2281 if (MB_ISUPPER(wca[i]))
2282 {
2283 /* Rule 1 is satisfied. */
2284 for (i = actual_compl_length; i < actual_len; ++i)
2285 wca[i] = MB_TOLOWER(wca[i]);
2286 break;
2287 }
2288 }
2289 }
2290
2291 /*
2292 * Rule 2: No lower case, 2nd consecutive letter converted to
2293 * upper case.
2294 */
2295 if (!has_lower)
2296 {
2297 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002298 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002299 {
2300#ifdef FEAT_MBYTE
2301 if (has_mbyte)
2302 c = mb_ptr2char_adv(&p);
2303 else
2304#endif
2305 c = *(p++);
2306 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2307 {
2308 /* Rule 2 is satisfied. */
2309 for (i = actual_compl_length; i < actual_len; ++i)
2310 wca[i] = MB_TOUPPER(wca[i]);
2311 break;
2312 }
2313 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2314 }
2315 }
2316
2317 /* Copy the original case of the part we typed. */
2318 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002319 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002320 {
2321#ifdef FEAT_MBYTE
2322 if (has_mbyte)
2323 c = mb_ptr2char_adv(&p);
2324 else
2325#endif
2326 c = *(p++);
2327 if (MB_ISLOWER(c))
2328 wca[i] = MB_TOLOWER(wca[i]);
2329 else if (MB_ISUPPER(c))
2330 wca[i] = MB_TOUPPER(wca[i]);
2331 }
2332
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002333 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002334 * Generate encoding specific output from wide character array.
2335 * Multi-byte characters can occupy up to five bytes more than
2336 * ASCII characters, and we also need one byte for NUL, so stay
2337 * six bytes away from the edge of IObuff.
2338 */
2339 p = IObuff;
2340 i = 0;
2341 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2342#ifdef FEAT_MBYTE
2343 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002344 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002345 else
2346#endif
2347 *(p++) = wca[i++];
2348 *p = NUL;
2349
2350 vim_free(wca);
2351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002352
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002353 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2354 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002355 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002356 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002357}
2358
2359/*
2360 * Add a match to the list of matches.
2361 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002362 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002363 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002364 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002365 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002366ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002367 char_u *str;
2368 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002369 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002371 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002372 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002373 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002374 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002376 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002377 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002378
2379 ui_breakcheck();
2380 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002381 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 if (len < 0)
2383 len = (int)STRLEN(str);
2384
2385 /*
2386 * If the same match is already present, don't add it.
2387 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002388 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002390 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002391 do
2392 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002393 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002394 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002395 && match->cp_str[len] == NUL)
2396 return NOTDONE;
2397 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002398 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002399 }
2400
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002401 /* Remove any popup menu before changing the list of matches. */
2402 ins_compl_del_pum();
2403
Bram Moolenaar071d4272004-06-13 20:20:40 +00002404 /*
2405 * Allocate a new match structure.
2406 * Copy the values to the new match structure.
2407 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002408 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002410 return FAIL;
2411 match->cp_number = -1;
2412 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002413 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002414 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002415 {
2416 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002417 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002419 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002420
Bram Moolenaar071d4272004-06-13 20:20:40 +00002421 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002422 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2424 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002425 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002426 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002427 && compl_curr_match->cp_fname != NULL
2428 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002429 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002430 else if (fname != NULL)
2431 {
2432 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002433 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002435 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002436 match->cp_fname = NULL;
2437 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002438
2439 if (cptext != NULL)
2440 {
2441 int i;
2442
2443 for (i = 0; i < CPT_COUNT; ++i)
2444 if (cptext[i] != NULL && *cptext[i] != NUL)
2445 match->cp_text[i] = vim_strsave(cptext[i]);
2446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002447
2448 /*
2449 * Link the new match structure in the list of matches.
2450 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002451 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002452 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 else if (dir == FORWARD)
2454 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002455 match->cp_next = compl_curr_match->cp_next;
2456 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002457 }
2458 else /* BACKWARD */
2459 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002460 match->cp_next = compl_curr_match;
2461 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002462 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002463 if (match->cp_next)
2464 match->cp_next->cp_prev = match;
2465 if (match->cp_prev)
2466 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002468 compl_first_match = match;
2469 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002470
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002471 /*
2472 * Find the longest common string if still doing that.
2473 */
2474 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2475 ins_compl_longest_match(match);
2476
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 return OK;
2478}
2479
2480/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002481 * Return TRUE if "str[len]" matches with match->cp_str, considering
2482 * match->cp_icase.
2483 */
2484 static int
2485ins_compl_equal(match, str, len)
2486 compl_T *match;
2487 char_u *str;
2488 int len;
2489{
2490 if (match->cp_icase)
2491 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2492 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2493}
2494
2495/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002496 * Reduce the longest common string for match "match".
2497 */
2498 static void
2499ins_compl_longest_match(match)
2500 compl_T *match;
2501{
2502 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002503 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002504 int had_match;
2505
2506 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002507 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002508 /* First match, use it as a whole. */
2509 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002510 if (compl_leader != NULL)
2511 {
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 Moolenaarf9393ef2006-04-24 19:47:27 +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 compl_used_match = FALSE;
2522 }
2523 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002524 else
2525 {
2526 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002527 p = compl_leader;
2528 s = match->cp_str;
2529 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002530 {
2531#ifdef FEAT_MBYTE
2532 if (has_mbyte)
2533 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002534 c1 = mb_ptr2char(p);
2535 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002536 }
2537 else
2538#endif
2539 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002540 c1 = *p;
2541 c2 = *s;
2542 }
2543 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2544 : (c1 != c2))
2545 break;
2546#ifdef FEAT_MBYTE
2547 if (has_mbyte)
2548 {
2549 mb_ptr_adv(p);
2550 mb_ptr_adv(s);
2551 }
2552 else
2553#endif
2554 {
2555 ++p;
2556 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002557 }
2558 }
2559
2560 if (*p != NUL)
2561 {
2562 /* Leader was shortened, need to change the inserted text. */
2563 *p = NUL;
2564 had_match = (curwin->w_cursor.col > compl_col);
2565 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002566 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002567 ins_redraw(FALSE);
2568
2569 /* When the match isn't there (to avoid matching itself) remove it
2570 * again after redrawing. */
2571 if (!had_match)
2572 ins_compl_delete();
2573 }
2574
2575 compl_used_match = FALSE;
2576 }
2577}
2578
2579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002580 * Add an array of matches to the list of matches.
2581 * Frees matches[].
2582 */
2583 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002584ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002585 int num_matches;
2586 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002587 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002588{
2589 int i;
2590 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002591 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002592
Bram Moolenaar572cb562005-08-05 21:35:02 +00002593 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002594 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002595 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002596 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002597 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 FreeWild(num_matches, matches);
2599}
2600
2601/* Make the completion list cyclic.
2602 * Return the number of matches (excluding the original).
2603 */
2604 static int
2605ins_compl_make_cyclic()
2606{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002607 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608 int count = 0;
2609
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002610 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611 {
2612 /*
2613 * Find the end of the list.
2614 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002615 match = compl_first_match;
2616 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002617 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002618 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002619 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002620 ++count;
2621 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002622 match->cp_next = compl_first_match;
2623 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002624 }
2625 return count;
2626}
2627
Bram Moolenaara94bc432006-03-10 21:42:59 +00002628/*
2629 * Start completion for the complete() function.
2630 * "startcol" is where the matched text starts (1 is first column).
2631 * "list" is the list of matches.
2632 */
2633 void
2634set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002635 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002636 list_T *list;
2637{
2638 /* If already doing completions stop it. */
2639 if (ctrl_x_mode != 0)
2640 ins_compl_prep(' ');
2641 ins_compl_clear();
2642
2643 if (stop_arrow() == FAIL)
2644 return;
2645
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002646 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002647 startcol = curwin->w_cursor.col;
2648 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002649 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002650 /* compl_pattern doesn't need to be set */
2651 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2652 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002653 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002654 return;
2655
2656 /* Handle like dictionary completion. */
2657 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2658
2659 ins_compl_add_list(list);
2660 compl_matches = ins_compl_make_cyclic();
2661 compl_started = TRUE;
2662 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002663 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002664
2665 compl_curr_match = compl_first_match;
2666 ins_complete(Ctrl_N);
2667 out_flush();
2668}
2669
2670
Bram Moolenaar9372a112005-12-06 19:59:18 +00002671/* "compl_match_array" points the currently displayed list of entries in the
2672 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002673static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002674static int compl_match_arraysize;
2675
2676/*
2677 * Update the screen and when there is any scrolling remove the popup menu.
2678 */
2679 static void
2680ins_compl_upd_pum()
2681{
2682 int h;
2683
2684 if (compl_match_array != NULL)
2685 {
2686 h = curwin->w_cline_height;
2687 update_screen(0);
2688 if (h != curwin->w_cline_height)
2689 ins_compl_del_pum();
2690 }
2691}
2692
2693/*
2694 * Remove any popup menu.
2695 */
2696 static void
2697ins_compl_del_pum()
2698{
2699 if (compl_match_array != NULL)
2700 {
2701 pum_undisplay();
2702 vim_free(compl_match_array);
2703 compl_match_array = NULL;
2704 }
2705}
2706
2707/*
2708 * Return TRUE if the popup menu should be displayed.
2709 */
2710 static int
2711pum_wanted()
2712{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002713 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002714 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002715 return FALSE;
2716
2717 /* The display looks bad on a B&W display. */
2718 if (t_colors < 8
2719#ifdef FEAT_GUI
2720 && !gui.in_use
2721#endif
2722 )
2723 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002724 return TRUE;
2725}
2726
2727/*
2728 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002729 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002730 */
2731 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002732pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002733{
2734 compl_T *compl;
2735 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002736
2737 /* Don't display the popup menu if there are no matches or there is only
2738 * one (ignoring the original text). */
2739 compl = compl_first_match;
2740 i = 0;
2741 do
2742 {
2743 if (compl == NULL
2744 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2745 break;
2746 compl = compl->cp_next;
2747 } while (compl != compl_first_match);
2748
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002749 if (strstr((char *)p_cot, "menuone") != NULL)
2750 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002751 return (i >= 2);
2752}
2753
2754/*
2755 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002756 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002757 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002758 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002759ins_compl_show_pum()
2760{
2761 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002762 compl_T *shown_compl = NULL;
2763 int did_find_shown_match = FALSE;
2764 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002765 int i;
2766 int cur = -1;
2767 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002768 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002769
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002770 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002771 return;
2772
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002773#if defined(FEAT_EVAL)
2774 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2775 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2776#endif
2777
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002778 /* Update the screen before drawing the popup menu over it. */
2779 update_screen(0);
2780
2781 if (compl_match_array == NULL)
2782 {
2783 /* Need to build the popup menu list. */
2784 compl_match_arraysize = 0;
2785 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002786 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002787 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002788 do
2789 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002790 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2791 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002792 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002793 ++compl_match_arraysize;
2794 compl = compl->cp_next;
2795 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002796 if (compl_match_arraysize == 0)
2797 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002798 compl_match_array = (pumitem_T *)alloc_clear(
2799 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002800 * compl_match_arraysize));
2801 if (compl_match_array != NULL)
2802 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002803 /* If the current match is the original text don't find the first
2804 * match after it, don't highlight anything. */
2805 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2806 shown_match_ok = TRUE;
2807
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002808 i = 0;
2809 compl = compl_first_match;
2810 do
2811 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002812 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2813 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002814 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002815 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002816 if (!shown_match_ok)
2817 {
2818 if (compl == compl_shown_match || did_find_shown_match)
2819 {
2820 /* This item is the shown match or this is the
2821 * first displayed item after the shown match. */
2822 compl_shown_match = compl;
2823 did_find_shown_match = TRUE;
2824 shown_match_ok = TRUE;
2825 }
2826 else
2827 /* Remember this displayed match for when the
2828 * shown match is just below it. */
2829 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002830 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002831 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002832
2833 if (compl->cp_text[CPT_ABBR] != NULL)
2834 compl_match_array[i].pum_text =
2835 compl->cp_text[CPT_ABBR];
2836 else
2837 compl_match_array[i].pum_text = compl->cp_str;
2838 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2839 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2840 if (compl->cp_text[CPT_MENU] != NULL)
2841 compl_match_array[i++].pum_extra =
2842 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002843 else
2844 compl_match_array[i++].pum_extra = compl->cp_fname;
2845 }
2846
2847 if (compl == compl_shown_match)
2848 {
2849 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002850
2851 /* When the original text is the shown match don't set
2852 * compl_shown_match. */
2853 if (compl->cp_flags & ORIGINAL_TEXT)
2854 shown_match_ok = TRUE;
2855
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002856 if (!shown_match_ok && shown_compl != NULL)
2857 {
2858 /* The shown match isn't displayed, set it to the
2859 * previously displayed match. */
2860 compl_shown_match = shown_compl;
2861 shown_match_ok = TRUE;
2862 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002863 }
2864 compl = compl->cp_next;
2865 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002866
2867 if (!shown_match_ok) /* no displayed match at all */
2868 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002869 }
2870 }
2871 else
2872 {
2873 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002874 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002875 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2876 || compl_match_array[i].pum_text
2877 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002878 {
2879 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002880 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002881 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002882 }
2883
2884 if (compl_match_array != NULL)
2885 {
2886 /* Compute the screen column of the start of the completed text.
2887 * Use the cursor to get all wrapping and other settings right. */
2888 col = curwin->w_cursor.col;
2889 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002890 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002891 curwin->w_cursor.col = col;
2892 }
2893}
2894
Bram Moolenaar071d4272004-06-13 20:20:40 +00002895#define DICT_FIRST (1) /* use just first element in "dict" */
2896#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002897
Bram Moolenaar071d4272004-06-13 20:20:40 +00002898/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002899 * Add any identifiers that match the given pattern in the list of dictionary
2900 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002901 */
2902 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002903ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2904 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002905 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002906 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002907 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002908{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002909 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002910 char_u *ptr;
2911 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002912 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913 char_u **files;
2914 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002916 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002917
Bram Moolenaar0b238792006-03-02 22:49:12 +00002918 if (*dict == NUL)
2919 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002920#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002921 /* When 'dictionary' is empty and spell checking is enabled use
2922 * "spell". */
2923 if (!thesaurus && curwin->w_p_spell)
2924 dict = (char_u *)"spell";
2925 else
2926#endif
2927 return;
2928 }
2929
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002931 if (buf == NULL)
2932 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002933 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002934
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 /* If 'infercase' is set, don't use 'smartcase' here */
2936 save_p_scs = p_scs;
2937 if (curbuf->b_p_inf)
2938 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002939
2940 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2941 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002942 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002943 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2944 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002945 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002946 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002947
2948 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002949 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002950 len = STRLEN(pat_esc) + 10;
2951 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002952 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002953 {
2954 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002955 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002956 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002957 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002958 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2959 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002960 vim_free(ptr);
2961 }
2962 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002963 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002964 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002965 if (regmatch.regprog == NULL)
2966 goto theend;
2967 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002968
Bram Moolenaar071d4272004-06-13 20:20:40 +00002969 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2970 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002971 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002972 {
2973 /* copy one dictionary file name into buf */
2974 if (flags == DICT_EXACT)
2975 {
2976 count = 1;
2977 files = &dict;
2978 }
2979 else
2980 {
2981 /* Expand wildcards in the dictionary name, but do not allow
2982 * backticks (for security, the 'dict' option may have been set in
2983 * a modeline). */
2984 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002985# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002986 if (!thesaurus && STRCMP(buf, "spell") == 0)
2987 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002988 else
2989# endif
2990 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00002991 || expand_wildcards(1, &buf, &count, &files,
2992 EW_FILE|EW_SILENT) != OK)
2993 count = 0;
2994 }
2995
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002996# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002997 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002998 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00002999 /* Complete from active spelling. Skip "\<" in the pattern, we
3000 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003001 if (pat[0] == '\\' && pat[1] == '<')
3002 ptr = pat + 2;
3003 else
3004 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003005 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003006 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003007 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003008# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003009 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003010 {
3011 ins_compl_files(count, files, thesaurus, flags,
3012 &regmatch, buf, &dir);
3013 if (flags != DICT_EXACT)
3014 FreeWild(count, files);
3015 }
3016 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003017 break;
3018 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003019
3020theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003021 p_scs = save_p_scs;
3022 vim_free(regmatch.regprog);
3023 vim_free(buf);
3024}
3025
Bram Moolenaar0b238792006-03-02 22:49:12 +00003026 static void
3027ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3028 int count;
3029 char_u **files;
3030 int thesaurus;
3031 int flags;
3032 regmatch_T *regmatch;
3033 char_u *buf;
3034 int *dir;
3035{
3036 char_u *ptr;
3037 int i;
3038 FILE *fp;
3039 int add_r;
3040
3041 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3042 {
3043 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3044 if (flags != DICT_EXACT)
3045 {
3046 vim_snprintf((char *)IObuff, IOSIZE,
3047 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003048 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003049 }
3050
3051 if (fp != NULL)
3052 {
3053 /*
3054 * Read dictionary file line by line.
3055 * Check each line for a match.
3056 */
3057 while (!got_int && !compl_interrupted
3058 && !vim_fgets(buf, LSIZE, fp))
3059 {
3060 ptr = buf;
3061 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3062 {
3063 ptr = regmatch->startp[0];
3064 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3065 ptr = find_line_end(ptr);
3066 else
3067 ptr = find_word_end(ptr);
3068 add_r = ins_compl_add_infercase(regmatch->startp[0],
3069 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003070 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003071 if (thesaurus)
3072 {
3073 char_u *wstart;
3074
3075 /*
3076 * Add the other matches on the line
3077 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003078 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003079 while (!got_int)
3080 {
3081 /* Find start of the next word. Skip white
3082 * space and punctuation. */
3083 ptr = find_word_start(ptr);
3084 if (*ptr == NUL || *ptr == NL)
3085 break;
3086 wstart = ptr;
3087
Bram Moolenaara2993e12007-08-12 14:38:46 +00003088 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003089#ifdef FEAT_MBYTE
3090 if (has_mbyte)
3091 /* Japanese words may have characters in
3092 * different classes, only separate words
3093 * with single-byte non-word characters. */
3094 while (*ptr != NUL)
3095 {
3096 int l = (*mb_ptr2len)(ptr);
3097
3098 if (l < 2 && !vim_iswordc(*ptr))
3099 break;
3100 ptr += l;
3101 }
3102 else
3103#endif
3104 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003105
3106 /* Add the word. Skip the regexp match. */
3107 if (wstart != regmatch->startp[0])
3108 add_r = ins_compl_add_infercase(wstart,
3109 (int)(ptr - wstart),
3110 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003111 }
3112 }
3113 if (add_r == OK)
3114 /* if dir was BACKWARD then honor it just once */
3115 *dir = FORWARD;
3116 else if (add_r == FAIL)
3117 break;
3118 /* avoid expensive call to vim_regexec() when at end
3119 * of line */
3120 if (*ptr == '\n' || got_int)
3121 break;
3122 }
3123 line_breakcheck();
3124 ins_compl_check_keys(50);
3125 }
3126 fclose(fp);
3127 }
3128 }
3129}
3130
Bram Moolenaar071d4272004-06-13 20:20:40 +00003131/*
3132 * Find the start of the next word.
3133 * Returns a pointer to the first char of the word. Also stops at a NUL.
3134 */
3135 char_u *
3136find_word_start(ptr)
3137 char_u *ptr;
3138{
3139#ifdef FEAT_MBYTE
3140 if (has_mbyte)
3141 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003142 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003143 else
3144#endif
3145 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3146 ++ptr;
3147 return ptr;
3148}
3149
3150/*
3151 * Find the end of the word. Assumes it starts inside a word.
3152 * Returns a pointer to just after the word.
3153 */
3154 char_u *
3155find_word_end(ptr)
3156 char_u *ptr;
3157{
3158#ifdef FEAT_MBYTE
3159 int start_class;
3160
3161 if (has_mbyte)
3162 {
3163 start_class = mb_get_class(ptr);
3164 if (start_class > 1)
3165 while (*ptr != NUL)
3166 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003167 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 if (mb_get_class(ptr) != start_class)
3169 break;
3170 }
3171 }
3172 else
3173#endif
3174 while (vim_iswordc(*ptr))
3175 ++ptr;
3176 return ptr;
3177}
3178
3179/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003180 * Find the end of the line, omitting CR and NL at the end.
3181 * Returns a pointer to just after the line.
3182 */
3183 static char_u *
3184find_line_end(ptr)
3185 char_u *ptr;
3186{
3187 char_u *s;
3188
3189 s = ptr + STRLEN(ptr);
3190 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3191 --s;
3192 return s;
3193}
3194
3195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003196 * Free the list of completions
3197 */
3198 static void
3199ins_compl_free()
3200{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003201 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003202 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003203
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003204 vim_free(compl_pattern);
3205 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003206 vim_free(compl_leader);
3207 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003208
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003209 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003210 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003211
3212 ins_compl_del_pum();
3213 pum_clear();
3214
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003215 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 do
3217 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003218 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003219 compl_curr_match = compl_curr_match->cp_next;
3220 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003222 if (match->cp_flags & FREE_FNAME)
3223 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003224 for (i = 0; i < CPT_COUNT; ++i)
3225 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003227 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3228 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003229 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230}
3231
3232 static void
3233ins_compl_clear()
3234{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003235 compl_cont_status = 0;
3236 compl_started = FALSE;
3237 compl_matches = 0;
3238 vim_free(compl_pattern);
3239 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003240 vim_free(compl_leader);
3241 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003242 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003243 vim_free(compl_orig_text);
3244 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003245 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246}
3247
3248/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003249 * Return TRUE when Insert completion is active.
3250 */
3251 int
3252ins_compl_active()
3253{
3254 return compl_started;
3255}
3256
3257/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003258 * Delete one character before the cursor and show the subset of the matches
3259 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003260 * Returns the character to be used, NUL if the work is done and another char
3261 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003262 */
3263 static int
3264ins_compl_bs()
3265{
3266 char_u *line;
3267 char_u *p;
3268
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003269 line = ml_get_curline();
3270 p = line + curwin->w_cursor.col;
3271 mb_ptr_back(line, p);
3272
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003273 /* Stop completion when the whole word was deleted. For Omni completion
3274 * allow the word to be deleted, we won't match everything. */
3275 if ((int)(p - line) - (int)compl_col < 0
3276 || ((int)(p - line) - (int)compl_col == 0
3277 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003278 return K_BS;
3279
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003280 /* Deleted more than what was used to find matches or didn't finish
3281 * finding all matches: need to look for matches all over again. */
3282 if (curwin->w_cursor.col <= compl_col + compl_length
3283 || compl_was_interrupted)
3284 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003285
Bram Moolenaara6557602006-02-04 22:43:20 +00003286 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003287 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003288 if (compl_leader != NULL)
3289 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003290 ins_compl_new_leader();
3291 return NUL;
3292 }
3293 return K_BS;
3294}
Bram Moolenaara6557602006-02-04 22:43:20 +00003295
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003296/*
3297 * Called after changing "compl_leader".
3298 * Show the popup menu with a different set of matches.
3299 * May also search for matches again if the previous search was interrupted.
3300 */
3301 static void
3302ins_compl_new_leader()
3303{
3304 ins_compl_del_pum();
3305 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003306 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003307 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003308
3309 if (compl_started)
3310 ins_compl_set_original_text(compl_leader);
3311 else
3312 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003313#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003314 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003315#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003316 /*
3317 * Matches were cleared, need to search for them now. First display
3318 * the changed text before the cursor. Set "compl_restarting" to
3319 * avoid that the first match is inserted.
3320 */
3321 update_screen(0);
3322#ifdef FEAT_GUI
3323 if (gui.in_use)
3324 {
3325 /* Show the cursor after the match, not after the redrawn text. */
3326 setcursor();
3327 out_flush();
3328 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003329 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003330#endif
3331 compl_restarting = TRUE;
3332 if (ins_complete(Ctrl_N) == FAIL)
3333 compl_cont_status = 0;
3334 compl_restarting = FALSE;
3335 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003336
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003337#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003338 if (!compl_used_match)
3339 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003340 /* Go to the original text, since none of the matches is inserted. */
3341 if (compl_first_match->cp_prev != NULL
3342 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3343 compl_shown_match = compl_first_match->cp_prev;
3344 else
3345 compl_shown_match = compl_first_match;
3346 compl_curr_match = compl_shown_match;
3347 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003348 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003349#endif
3350 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003351
3352 /* Show the popup menu with a different set of matches. */
3353 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003354
3355 /* Don't let Enter select the original text when there is no popup menu. */
3356 if (compl_match_array == NULL)
3357 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003358}
3359
3360/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003361 * Return the length of the completion, from the completion start column to
3362 * the cursor column. Making sure it never goes below zero.
3363 */
3364 static int
3365ins_compl_len()
3366{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003367 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003368
3369 if (off < 0)
3370 return 0;
3371 return off;
3372}
3373
3374/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003375 * Append one character to the match leader. May reduce the number of
3376 * matches.
3377 */
3378 static void
3379ins_compl_addleader(c)
3380 int c;
3381{
3382#ifdef FEAT_MBYTE
3383 int cc;
3384
3385 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3386 {
3387 char_u buf[MB_MAXBYTES + 1];
3388
3389 (*mb_char2bytes)(c, buf);
3390 buf[cc] = NUL;
3391 ins_char_bytes(buf, cc);
3392 }
3393 else
3394#endif
3395 ins_char(c);
3396
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003397 /* If we didn't complete finding matches we must search again. */
3398 if (compl_was_interrupted)
3399 ins_compl_restart();
3400
Bram Moolenaara6557602006-02-04 22:43:20 +00003401 vim_free(compl_leader);
3402 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003403 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003404 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003405 ins_compl_new_leader();
3406}
3407
3408/*
3409 * Setup for finding completions again without leaving CTRL-X mode. Used when
3410 * BS or a key was typed while still searching for matches.
3411 */
3412 static void
3413ins_compl_restart()
3414{
3415 ins_compl_free();
3416 compl_started = FALSE;
3417 compl_matches = 0;
3418 compl_cont_status = 0;
3419 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003420}
3421
3422/*
3423 * Set the first match, the original text.
3424 */
3425 static void
3426ins_compl_set_original_text(str)
3427 char_u *str;
3428{
3429 char_u *p;
3430
3431 /* Replace the original text entry. */
3432 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3433 {
3434 p = vim_strsave(str);
3435 if (p != NULL)
3436 {
3437 vim_free(compl_first_match->cp_str);
3438 compl_first_match->cp_str = p;
3439 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003440 }
3441}
3442
3443/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003444 * Append one character to the match leader. May reduce the number of
3445 * matches.
3446 */
3447 static void
3448ins_compl_addfrommatch()
3449{
3450 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003451 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003452 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003453 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003454
3455 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003456 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003457 {
3458 /* When still at the original match use the first entry that matches
3459 * the leader. */
3460 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3461 {
3462 p = NULL;
3463 for (cp = compl_shown_match->cp_next; cp != NULL
3464 && cp != compl_first_match; cp = cp->cp_next)
3465 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003466 if (compl_leader == NULL
3467 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003468 (int)STRLEN(compl_leader)))
3469 {
3470 p = cp->cp_str;
3471 break;
3472 }
3473 }
3474 if (p == NULL || (int)STRLEN(p) <= len)
3475 return;
3476 }
3477 else
3478 return;
3479 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003480 p += len;
3481#ifdef FEAT_MBYTE
3482 c = mb_ptr2char(p);
3483#else
3484 c = *p;
3485#endif
3486 ins_compl_addleader(c);
3487}
3488
3489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003490 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003491 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003492 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003493 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003494 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495ins_compl_prep(c)
3496 int c;
3497{
3498 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003500 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501
3502 /* Forget any previous 'special' messages if this is actually
3503 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3504 */
3505 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3506 edit_submode_extra = NULL;
3507
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003508 /* Ignore end of Select mode mapping and mouse scroll buttons. */
3509 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003510 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003512 /* Set "compl_get_longest" when finding the first matches. */
3513 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3514 || (ctrl_x_mode == 0 && !compl_started))
3515 {
3516 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3517 compl_used_match = TRUE;
3518 }
3519
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3521 {
3522 /*
3523 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3524 * it will be yet. Now we decide.
3525 */
3526 switch (c)
3527 {
3528 case Ctrl_E:
3529 case Ctrl_Y:
3530 ctrl_x_mode = CTRL_X_SCROLL;
3531 if (!(State & REPLACE_FLAG))
3532 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3533 else
3534 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3535 edit_submode_pre = NULL;
3536 showmode();
3537 break;
3538 case Ctrl_L:
3539 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3540 break;
3541 case Ctrl_F:
3542 ctrl_x_mode = CTRL_X_FILES;
3543 break;
3544 case Ctrl_K:
3545 ctrl_x_mode = CTRL_X_DICTIONARY;
3546 break;
3547 case Ctrl_R:
3548 /* Simply allow ^R to happen without affecting ^X mode */
3549 break;
3550 case Ctrl_T:
3551 ctrl_x_mode = CTRL_X_THESAURUS;
3552 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003553#ifdef FEAT_COMPL_FUNC
3554 case Ctrl_U:
3555 ctrl_x_mode = CTRL_X_FUNCTION;
3556 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003557 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003558 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003559 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003560#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003561 case 's':
3562 case Ctrl_S:
3563 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003564#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003565 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003566 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003567 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003568#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003569 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 case Ctrl_RSB:
3571 ctrl_x_mode = CTRL_X_TAGS;
3572 break;
3573#ifdef FEAT_FIND_ID
3574 case Ctrl_I:
3575 case K_S_TAB:
3576 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3577 break;
3578 case Ctrl_D:
3579 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3580 break;
3581#endif
3582 case Ctrl_V:
3583 case Ctrl_Q:
3584 ctrl_x_mode = CTRL_X_CMDLINE;
3585 break;
3586 case Ctrl_P:
3587 case Ctrl_N:
3588 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3589 * just started ^X mode, or there were enough ^X's to cancel
3590 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3591 * do normal expansion when interrupting a different mode (say
3592 * ^X^F^X^P or ^P^X^X^P, see below)
3593 * nothing changes if interrupting mode 0, (eg, the flag
3594 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003595 if (!(compl_cont_status & CONT_INTRPT))
3596 compl_cont_status |= CONT_LOCAL;
3597 else if (compl_cont_mode != 0)
3598 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 /* FALLTHROUGH */
3600 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003601 /* If we have typed at least 2 ^X's... for modes != 0, we set
3602 * compl_cont_status = 0 (eg, as if we had just started ^X
3603 * mode).
3604 * For mode 0, we set "compl_cont_mode" to an impossible
3605 * value, in both cases ^X^X can be used to restart the same
3606 * mode (avoiding ADDING mode).
3607 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3608 * 'complete' and local ^P expansions respectively.
3609 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3610 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003611 if (c == Ctrl_X)
3612 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003613 if (compl_cont_mode != 0)
3614 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003616 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003617 }
3618 ctrl_x_mode = 0;
3619 edit_submode = NULL;
3620 showmode();
3621 break;
3622 }
3623 }
3624 else if (ctrl_x_mode != 0)
3625 {
3626 /* We're already in CTRL-X mode, do we stay in it? */
3627 if (!vim_is_ctrl_x_key(c))
3628 {
3629 if (ctrl_x_mode == CTRL_X_SCROLL)
3630 ctrl_x_mode = 0;
3631 else
3632 ctrl_x_mode = CTRL_X_FINISHED;
3633 edit_submode = NULL;
3634 }
3635 showmode();
3636 }
3637
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003638 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
3640 /* Show error message from attempted keyword completion (probably
3641 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003642 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003644 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3645 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003646 || ctrl_x_mode == CTRL_X_FINISHED)
3647 {
3648 /* Get here when we have finished typing a sequence of ^N and
3649 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003650 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003651 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003652 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003653 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003654 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003655
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003657 * If any of the original typed text has been changed, eg when
3658 * ignorecase is set, we must add back-spaces to the redo
3659 * buffer. We add as few as necessary to delete just the part
3660 * of the original text that has changed.
3661 * When using the longest match, edited the match or used
3662 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003663 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003664 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3665 ptr = compl_curr_match->cp_str;
3666 else if (compl_leader != NULL)
3667 ptr = compl_leader;
3668 else
3669 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003670 if (compl_orig_text != NULL)
3671 {
3672 p = compl_orig_text;
3673 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3674 ++temp)
3675 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003676#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003677 if (temp > 0)
3678 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003679#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003680 for (p += temp; *p != NUL; mb_ptr_adv(p))
3681 AppendCharToRedobuff(K_BS);
3682 }
3683 if (ptr != NULL)
3684 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003685 }
3686
3687#ifdef FEAT_CINDENT
3688 want_cindent = (can_cindent && cindent_on());
3689#endif
3690 /*
3691 * When completing whole lines: fix indent for 'cindent'.
3692 * Otherwise, break line if it's too long.
3693 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003694 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003695 {
3696#ifdef FEAT_CINDENT
3697 /* re-indent the current line */
3698 if (want_cindent)
3699 {
3700 do_c_expr_indent();
3701 want_cindent = FALSE; /* don't do it again */
3702 }
3703#endif
3704 }
3705 else
3706 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003707 int prev_col = curwin->w_cursor.col;
3708
Bram Moolenaar071d4272004-06-13 20:20:40 +00003709 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003710 if (prev_col > 0)
3711 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003712 if (stop_arrow() == OK)
3713 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003714 if (prev_col > 0
3715 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3716 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 }
3718
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003719 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003720 * the selection without inserting anything. When
3721 * compl_enter_selects is set the Enter key does the same. */
3722 if ((c == Ctrl_Y || (compl_enter_selects
3723 && (c == CAR || c == K_KENTER || c == NL)))
3724 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003725 retval = TRUE;
3726
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003727 /* CTRL-E means completion is Ended, go back to the typed text. */
3728 if (c == Ctrl_E)
3729 {
3730 ins_compl_delete();
3731 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003732 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003733 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003734 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003735 retval = TRUE;
3736 }
3737
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003738 auto_format(FALSE, TRUE);
3739
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003741 compl_started = FALSE;
3742 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743 msg_clr_cmdline(); /* necessary for "noshowmode" */
3744 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003745 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 if (edit_submode != NULL)
3747 {
3748 edit_submode = NULL;
3749 showmode();
3750 }
3751
3752#ifdef FEAT_CINDENT
3753 /*
3754 * Indent now if a key was typed that is in 'cinkeys'.
3755 */
3756 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3757 do_c_expr_indent();
3758#endif
3759 }
3760 }
3761
3762 /* reset continue_* if we left expansion-mode, if we stay they'll be
3763 * (re)set properly in ins_complete() */
3764 if (!vim_is_ctrl_x_key(c))
3765 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003766 compl_cont_status = 0;
3767 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003769
3770 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003771}
3772
3773/*
3774 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3775 * (depending on flag) starting from buf and looking for a non-scanned
3776 * buffer (other than curbuf). curbuf is special, if it is called with
3777 * buf=curbuf then it has to be the first call for a given flag/expansion.
3778 *
3779 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3780 */
3781 static buf_T *
3782ins_compl_next_buf(buf, flag)
3783 buf_T *buf;
3784 int flag;
3785{
3786#ifdef FEAT_WINDOWS
3787 static win_T *wp;
3788#endif
3789
3790 if (flag == 'w') /* just windows */
3791 {
3792#ifdef FEAT_WINDOWS
3793 if (buf == curbuf) /* first call for this flag/expansion */
3794 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003795 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 && wp->w_buffer->b_scanned)
3797 ;
3798 buf = wp->w_buffer;
3799#else
3800 buf = curbuf;
3801#endif
3802 }
3803 else
3804 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3805 * (unlisted buffers)
3806 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003807 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003808 && ((flag == 'U'
3809 ? buf->b_p_bl
3810 : (!buf->b_p_bl
3811 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003812 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003813 ;
3814 return buf;
3815}
3816
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003817#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003818static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003819
3820/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003821 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003822 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003823 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003824 static void
3825expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003826 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003827 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003828{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003829 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003830 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003831 char_u *funcname;
3832 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003833
Bram Moolenaare344bea2005-09-01 20:46:49 +00003834 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3835 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003836 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003837
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003838 /* Call 'completefunc' to obtain the list of matches. */
3839 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003840 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003841
Bram Moolenaare344bea2005-09-01 20:46:49 +00003842 pos = curwin->w_cursor;
3843 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3844 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003845 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003846 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003847
Bram Moolenaara94bc432006-03-10 21:42:59 +00003848 ins_compl_add_list(matchlist);
3849 list_unref(matchlist);
3850}
3851#endif /* FEAT_COMPL_FUNC */
3852
Bram Moolenaar39f05632006-03-19 22:15:26 +00003853#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003854/*
3855 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003856 */
3857 static void
3858ins_compl_add_list(list)
3859 list_T *list;
3860{
3861 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003862 int dir = compl_direction;
3863
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003864 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003865 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003866 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003867 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3868 /* if dir was BACKWARD then honor it just once */
3869 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003870 else if (did_emsg)
3871 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003872 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003873}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003874
3875/*
3876 * Add a match to the list of matches from a typeval_T.
3877 * If the given string is already in the list of completions, then return
3878 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3879 * maybe because alloc() returns NULL, then FAIL is returned.
3880 */
3881 int
3882ins_compl_add_tv(tv, dir)
3883 typval_T *tv;
3884 int dir;
3885{
3886 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003887 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003888 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003889 char_u *(cptext[CPT_COUNT]);
3890
3891 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3892 {
3893 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3894 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3895 (char_u *)"abbr", FALSE);
3896 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3897 (char_u *)"menu", FALSE);
3898 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3899 (char_u *)"kind", FALSE);
3900 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3901 (char_u *)"info", FALSE);
3902 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3903 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003904 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003905 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003906 }
3907 else
3908 {
3909 word = get_tv_string_chk(tv);
3910 vim_memset(cptext, 0, sizeof(cptext));
3911 }
3912 if (word == NULL || *word == NUL)
3913 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003914 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003915}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003916#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003917
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003918/*
3919 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003920 * The search starts at position "ini" in curbuf and in the direction
3921 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003922 * When "compl_started" is FALSE start at that position, otherwise continue
3923 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003924 * This may return before finding all the matches.
3925 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003926 */
3927 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003928ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003929 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003930{
3931 static pos_T first_match_pos;
3932 static pos_T last_match_pos;
3933 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003934 static int found_all = FALSE; /* Found all matches of a
3935 certain type. */
3936 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003937
Bram Moolenaar572cb562005-08-05 21:35:02 +00003938 pos_T *pos;
3939 char_u **matches;
3940 int save_p_scs;
3941 int save_p_ws;
3942 int save_p_ic;
3943 int i;
3944 int num_matches;
3945 int len;
3946 int found_new_match;
3947 int type = ctrl_x_mode;
3948 char_u *ptr;
3949 char_u *dict = NULL;
3950 int dict_f = 0;
3951 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003953 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003954 {
3955 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3956 ins_buf->b_scanned = 0;
3957 found_all = FALSE;
3958 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003959 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003960 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003961 last_match_pos = first_match_pos = *ini;
3962 }
3963
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003964 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003965 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003966 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3967 for (;;)
3968 {
3969 found_new_match = FAIL;
3970
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003971 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003972 * or if found_all says this entry is done. For ^X^L only use the
3973 * entries from 'complete' that look in loaded buffers. */
3974 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003975 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976 {
3977 found_all = FALSE;
3978 while (*e_cpt == ',' || *e_cpt == ' ')
3979 e_cpt++;
3980 if (*e_cpt == '.' && !curbuf->b_scanned)
3981 {
3982 ins_buf = curbuf;
3983 first_match_pos = *ini;
3984 /* So that ^N can match word immediately after cursor */
3985 if (ctrl_x_mode == 0)
3986 dec(&first_match_pos);
3987 last_match_pos = first_match_pos;
3988 type = 0;
3989 }
3990 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
3991 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
3992 {
3993 /* Scan a buffer, but not the current one. */
3994 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
3995 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003996 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003997 first_match_pos.col = last_match_pos.col = 0;
3998 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
3999 last_match_pos.lnum = 0;
4000 type = 0;
4001 }
4002 else /* unloaded buffer, scan like dictionary */
4003 {
4004 found_all = TRUE;
4005 if (ins_buf->b_fname == NULL)
4006 continue;
4007 type = CTRL_X_DICTIONARY;
4008 dict = ins_buf->b_fname;
4009 dict_f = DICT_EXACT;
4010 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004011 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004012 ins_buf->b_fname == NULL
4013 ? buf_spname(ins_buf)
4014 : ins_buf->b_sfname == NULL
4015 ? (char *)ins_buf->b_fname
4016 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004017 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 }
4019 else if (*e_cpt == NUL)
4020 break;
4021 else
4022 {
4023 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4024 type = -1;
4025 else if (*e_cpt == 'k' || *e_cpt == 's')
4026 {
4027 if (*e_cpt == 'k')
4028 type = CTRL_X_DICTIONARY;
4029 else
4030 type = CTRL_X_THESAURUS;
4031 if (*++e_cpt != ',' && *e_cpt != NUL)
4032 {
4033 dict = e_cpt;
4034 dict_f = DICT_FIRST;
4035 }
4036 }
4037#ifdef FEAT_FIND_ID
4038 else if (*e_cpt == 'i')
4039 type = CTRL_X_PATH_PATTERNS;
4040 else if (*e_cpt == 'd')
4041 type = CTRL_X_PATH_DEFINES;
4042#endif
4043 else if (*e_cpt == ']' || *e_cpt == 't')
4044 {
4045 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004046 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004047 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
4049 else
4050 type = -1;
4051
4052 /* in any case e_cpt is advanced to the next entry */
4053 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4054
4055 found_all = TRUE;
4056 if (type == -1)
4057 continue;
4058 }
4059 }
4060
4061 switch (type)
4062 {
4063 case -1:
4064 break;
4065#ifdef FEAT_FIND_ID
4066 case CTRL_X_PATH_PATTERNS:
4067 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004068 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004069 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004071 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4073 (linenr_T)1, (linenr_T)MAXLNUM);
4074 break;
4075#endif
4076
4077 case CTRL_X_DICTIONARY:
4078 case CTRL_X_THESAURUS:
4079 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004080 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004081 : (type == CTRL_X_THESAURUS
4082 ? (*curbuf->b_p_tsr == NUL
4083 ? p_tsr
4084 : curbuf->b_p_tsr)
4085 : (*curbuf->b_p_dict == NUL
4086 ? p_dict
4087 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004088 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004089 dict != NULL ? dict_f
4090 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 dict = NULL;
4092 break;
4093
4094 case CTRL_X_TAGS:
4095 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4096 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004097 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004098
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004099 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004100 * of matches is found when compl_pattern is empty */
4101 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4103 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4104 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4105 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004106 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004107 }
4108 p_ic = save_p_ic;
4109 break;
4110
4111 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004112 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004113 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4114 {
4115
4116 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004117 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004118 ins_compl_add_matches(num_matches, matches,
4119#ifdef CASE_INSENSITIVE_FILENAME
4120 TRUE
4121#else
4122 FALSE
4123#endif
4124 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125 }
4126 break;
4127
4128 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004129 if (expand_cmdline(&compl_xp, compl_pattern,
4130 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004132 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004133 break;
4134
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004135#ifdef FEAT_COMPL_FUNC
4136 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004137 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004138 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004139 break;
4140#endif
4141
Bram Moolenaar488c6512005-08-11 20:09:58 +00004142 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004143#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004144 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004145 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004146 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004147 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004148#endif
4149 break;
4150
Bram Moolenaar071d4272004-06-13 20:20:40 +00004151 default: /* normal ^P/^N and ^X^L */
4152 /*
4153 * If 'infercase' is set, don't use 'smartcase' here
4154 */
4155 save_p_scs = p_scs;
4156 if (ins_buf->b_p_inf)
4157 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004158
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 /* buffers other than curbuf are scanned from the beginning or the
4160 * end but never from the middle, thus setting nowrapscan in this
4161 * buffers is a good idea, on the other hand, we always set
4162 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4163 save_p_ws = p_ws;
4164 if (ins_buf != curbuf)
4165 p_ws = FALSE;
4166 else if (*e_cpt == '.')
4167 p_ws = TRUE;
4168 for (;;)
4169 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004170 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004172 ++msg_silent; /* Don't want messages for wrapscan. */
4173
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004174 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4175 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004176 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004177 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004179 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004181 found_new_match = searchit(NULL, ins_buf, pos,
4182 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004183 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004184 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004185 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004186 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004188 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004189 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190 first_match_pos = *pos;
4191 last_match_pos = *pos;
4192 }
4193 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004194 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 found_new_match = FAIL;
4196 if (found_new_match == FAIL)
4197 {
4198 if (ins_buf == curbuf)
4199 found_all = TRUE;
4200 break;
4201 }
4202
4203 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004204 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 && ini->lnum == pos->lnum
4206 && ini->col == pos->col)
4207 continue;
4208 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4209 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4210 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004211 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004212 {
4213 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4214 continue;
4215 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4216 if (!p_paste)
4217 ptr = skipwhite(ptr);
4218 }
4219 len = (int)STRLEN(ptr);
4220 }
4221 else
4222 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223 char_u *tmp_ptr = ptr;
4224
4225 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004226 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004227 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004228 /* Skip if already inside a word. */
4229 if (vim_iswordp(tmp_ptr))
4230 continue;
4231 /* Find start of next word. */
4232 tmp_ptr = find_word_start(tmp_ptr);
4233 }
4234 /* Find end of this word. */
4235 tmp_ptr = find_word_end(tmp_ptr);
4236 len = (int)(tmp_ptr - ptr);
4237
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 if ((compl_cont_status & CONT_ADDING)
4239 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004240 {
4241 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4242 {
4243 /* Try next line, if any. the new word will be
4244 * "join" as if the normal command "J" was used.
4245 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 * works -- Acevedo */
4248 STRNCPY(IObuff, ptr, len);
4249 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4250 tmp_ptr = ptr = skipwhite(ptr);
4251 /* Find start of next word. */
4252 tmp_ptr = find_word_start(tmp_ptr);
4253 /* Find end of next word. */
4254 tmp_ptr = find_word_end(tmp_ptr);
4255 if (tmp_ptr > ptr)
4256 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004257 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004259 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004260 IObuff[len++] = ' ';
4261 /* IObuf =~ "\k.* ", thus len >= 2 */
4262 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004263 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 || (vim_strchr(p_cpo, CPO_JOINSP)
4265 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004266 && (IObuff[len - 2] == '?'
4267 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004268 IObuff[len++] = ' ';
4269 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004270 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 if (tmp_ptr - ptr >= IOSIZE - len)
4272 tmp_ptr = ptr + IOSIZE - len - 1;
4273 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4274 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004275 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 }
4277 IObuff[len] = NUL;
4278 ptr = IObuff;
4279 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004280 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 continue;
4282 }
4283 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004284 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004285 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004286 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 {
4288 found_new_match = OK;
4289 break;
4290 }
4291 }
4292 p_scs = save_p_scs;
4293 p_ws = save_p_ws;
4294 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004295
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004296 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004297 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004298 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 found_new_match = OK;
4300
4301 /* break the loop for specialized modes (use 'complete' just for the
4302 * generic ctrl_x_mode == 0) or when we've found a new match */
4303 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004304 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004305 {
4306 if (got_int)
4307 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004308 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004309 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004310 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004311
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004312 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4313 || compl_interrupted)
4314 break;
4315 compl_started = TRUE;
4316 }
4317 else
4318 {
4319 /* Mark a buffer scanned when it has been scanned completely */
4320 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4321 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004323 compl_started = FALSE;
4324 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004326 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4329 && *e_cpt == NUL) /* Got to end of 'complete' */
4330 found_new_match = FAIL;
4331
4332 i = -1; /* total of matches, unknown */
4333 if (found_new_match == FAIL
4334 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4335 i = ins_compl_make_cyclic();
4336
4337 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004338 * just been made cyclic then we have to move compl_curr_match to the next
4339 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004340 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4341 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004342 if (compl_curr_match == NULL)
4343 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 return i;
4345}
4346
4347/* Delete the old text being completed. */
4348 static void
4349ins_compl_delete()
4350{
4351 int i;
4352
4353 /*
4354 * In insert mode: Delete the typed part.
4355 * In replace mode: Put the old characters back, if any.
4356 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004357 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 backspace_until_column(i);
4359 changed_cline_bef_curs();
4360}
4361
4362/* Insert the new text being completed. */
4363 static void
4364ins_compl_insert()
4365{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004366 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004367 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4368 compl_used_match = FALSE;
4369 else
4370 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371}
4372
4373/*
4374 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004375 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4376 * get more completions. If it is FALSE, then we just do nothing when there
4377 * are no more completions in a given direction. The latter case is used when
4378 * we are still in the middle of finding completions, to allow browsing
4379 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004380 * Return the total number of matches, or -1 if still unknown -- webb.
4381 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004382 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4383 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 *
4385 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004386 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4387 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 */
4389 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004390ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004392 int count; /* repeat completion this many times; should
4393 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004394 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004395{
4396 int num_matches = -1;
4397 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004398 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004399 compl_T *found_compl = NULL;
4400 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004401 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004403 if (compl_leader != NULL
4404 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004406 /* Set "compl_shown_match" to the actually shown match, it may differ
4407 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004408 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004409 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004410 && compl_shown_match->cp_next != NULL
4411 && compl_shown_match->cp_next != compl_first_match)
4412 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004413
4414 /* If we didn't find it searching forward, and compl_shows_dir is
4415 * backward, find the last match. */
4416 if (compl_shows_dir == BACKWARD
4417 && !ins_compl_equal(compl_shown_match,
4418 compl_leader, (int)STRLEN(compl_leader))
4419 && (compl_shown_match->cp_next == NULL
4420 || compl_shown_match->cp_next == compl_first_match))
4421 {
4422 while (!ins_compl_equal(compl_shown_match,
4423 compl_leader, (int)STRLEN(compl_leader))
4424 && compl_shown_match->cp_prev != NULL
4425 && compl_shown_match->cp_prev != compl_first_match)
4426 compl_shown_match = compl_shown_match->cp_prev;
4427 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004428 }
4429
4430 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004431 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 /* Delete old text to be replaced */
4433 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004434
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004435 /* When finding the longest common text we stick at the original text,
4436 * don't let CTRL-N or CTRL-P move to the first match. */
4437 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4438
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004439 /* When restarting the search don't insert the first match either. */
4440 if (compl_restarting)
4441 {
4442 advance = FALSE;
4443 compl_restarting = FALSE;
4444 }
4445
Bram Moolenaare3226be2005-12-18 22:10:00 +00004446 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4447 * around. */
4448 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004450 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004452 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004453 found_end = (compl_first_match != NULL
4454 && (compl_shown_match->cp_next == compl_first_match
4455 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004456 }
4457 else if (compl_shows_dir == BACKWARD
4458 && compl_shown_match->cp_prev != NULL)
4459 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004460 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004461 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004462 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004463 }
4464 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004465 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004466 if (!allow_get_expansion)
4467 {
4468 if (advance)
4469 {
4470 if (compl_shows_dir == BACKWARD)
4471 compl_pending -= todo + 1;
4472 else
4473 compl_pending += todo + 1;
4474 }
4475 return -1;
4476 }
4477
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004478 if (advance)
4479 {
4480 if (compl_shows_dir == BACKWARD)
4481 --compl_pending;
4482 else
4483 ++compl_pending;
4484 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004485
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004486 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004487 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004488
4489 /* handle any pending completions */
4490 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004491 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004492 {
4493 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4494 {
4495 compl_shown_match = compl_shown_match->cp_next;
4496 --compl_pending;
4497 }
4498 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4499 {
4500 compl_shown_match = compl_shown_match->cp_prev;
4501 ++compl_pending;
4502 }
4503 else
4504 break;
4505 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004506 found_end = FALSE;
4507 }
4508 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4509 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004510 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004511 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004512 ++todo;
4513 else
4514 /* Remember a matching item. */
4515 found_compl = compl_shown_match;
4516
4517 /* Stop at the end of the list when we found a usable match. */
4518 if (found_end)
4519 {
4520 if (found_compl != NULL)
4521 {
4522 compl_shown_match = found_compl;
4523 break;
4524 }
4525 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004526 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 }
4528
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004529 /* Insert the text of the new completion, or the compl_leader. */
4530 if (insert_match)
4531 {
4532 if (!compl_get_longest || compl_used_match)
4533 ins_compl_insert();
4534 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004535 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004536 }
4537 else
4538 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004539
4540 if (!allow_get_expansion)
4541 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004542 /* may undisplay the popup menu first */
4543 ins_compl_upd_pum();
4544
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004545 /* redraw to show the user what was inserted */
4546 update_screen(0);
4547
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004548 /* display the updated popup menu */
4549 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004550#ifdef FEAT_GUI
4551 if (gui.in_use)
4552 {
4553 /* Show the cursor after the match, not after the redrawn text. */
4554 setcursor();
4555 out_flush();
4556 gui_update_cursor(FALSE, FALSE);
4557 }
4558#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004559
Bram Moolenaar071d4272004-06-13 20:20:40 +00004560 /* Delete old text to be replaced, since we're still searching and
4561 * don't want to match ourselves! */
4562 ins_compl_delete();
4563 }
4564
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004565 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004566 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004567 compl_enter_selects = !insert_match && compl_match_array != NULL;
4568
Bram Moolenaar071d4272004-06-13 20:20:40 +00004569 /*
4570 * Show the file name for the match (if any)
4571 * Truncate the file name to avoid a wait for return.
4572 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004573 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004574 {
4575 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004576 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004577 if (i <= 0)
4578 i = 0;
4579 else
4580 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004581 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004582 msg(IObuff);
4583 redraw_cmdline = FALSE; /* don't overwrite! */
4584 }
4585
4586 return num_matches;
4587}
4588
4589/*
4590 * Call this while finding completions, to check whether the user has hit a key
4591 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004592 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004594 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004595 */
4596 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004597ins_compl_check_keys(frequency)
4598 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004599{
4600 static int count = 0;
4601
4602 int c;
4603
4604 /* Don't check when reading keys from a script. That would break the test
4605 * scripts */
4606 if (using_script())
4607 return;
4608
4609 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004610 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004611 return;
4612 count = 0;
4613
Bram Moolenaara260a972006-06-23 19:36:29 +00004614 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4615 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004617 if (c != NUL)
4618 {
4619 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4620 {
4621 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004622 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004623 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4624 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004625 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004626 else
4627 {
4628 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004629 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004630 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004631 if (c != K_IGNORE)
4632 {
4633 /* Don't interrupt completion when the character wasn't typed,
4634 * e.g., when doing @q to replay keys. */
4635 if (c != Ctrl_R && KeyTyped)
4636 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004637
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004638 vungetc(c);
4639 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004642 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004643 {
4644 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4645
4646 compl_pending = 0;
4647 (void)ins_compl_next(FALSE, todo, TRUE);
4648 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004649}
4650
4651/*
4652 * Decide the direction of Insert mode complete from the key typed.
4653 * Returns BACKWARD or FORWARD.
4654 */
4655 static int
4656ins_compl_key2dir(c)
4657 int c;
4658{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004659 if (c == Ctrl_P || c == Ctrl_L
4660 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4661 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004662 return BACKWARD;
4663 return FORWARD;
4664}
4665
4666/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004667 * Return TRUE for keys that are used for completion only when the popup menu
4668 * is visible.
4669 */
4670 static int
4671ins_compl_pum_key(c)
4672 int c;
4673{
4674 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004675 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4676 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004677}
4678
4679/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004680 * Decide the number of completions to move forward.
4681 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4682 */
4683 static int
4684ins_compl_key2count(c)
4685 int c;
4686{
4687 int h;
4688
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004689 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004690 {
4691 h = pum_get_height();
4692 if (h > 3)
4693 h -= 2; /* keep some context */
4694 return h;
4695 }
4696 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697}
4698
4699/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004700 * Return TRUE if completion with "c" should insert the match, FALSE if only
4701 * to change the currently selected completion.
4702 */
4703 static int
4704ins_compl_use_match(c)
4705 int c;
4706{
4707 switch (c)
4708 {
4709 case K_UP:
4710 case K_DOWN:
4711 case K_PAGEDOWN:
4712 case K_KPAGEDOWN:
4713 case K_S_DOWN:
4714 case K_PAGEUP:
4715 case K_KPAGEUP:
4716 case K_S_UP:
4717 return FALSE;
4718 }
4719 return TRUE;
4720}
4721
4722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004723 * Do Insert mode completion.
4724 * Called when character "c" was typed, which has a meaning for completion.
4725 * Returns OK if completion was done, FAIL if something failed (out of mem).
4726 */
4727 static int
4728ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004729 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004730{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004731 char_u *line;
4732 int startcol = 0; /* column where searched text starts */
4733 colnr_T curs_col; /* cursor column */
4734 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004735 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004736
Bram Moolenaare3226be2005-12-18 22:10:00 +00004737 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004738 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004739 {
4740 /* First time we hit ^N or ^P (in a row, I mean) */
4741
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 did_ai = FALSE;
4743#ifdef FEAT_SMARTINDENT
4744 did_si = FALSE;
4745 can_si = FALSE;
4746 can_si_back = FALSE;
4747#endif
4748 if (stop_arrow() == FAIL)
4749 return FAIL;
4750
4751 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004752 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004753 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004754
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004755 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004756 * "compl_startpos" to the cursor as a pattern to add a new word
4757 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004758 * "compl_startpos" is not in the same line as the cursor then fix it
4759 * (the line has been split because it was longer than 'tw'). if SOL
4760 * is set then skip the previous pattern, a word at the beginning of
4761 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004762 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4763 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 {
4765 /*
4766 * it is a continued search
4767 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004768 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4770 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4771 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004772 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004774 /* line (probably) wrapped, set compl_startpos to the
4775 * first non_blank in the line, if it is not a wordchar
4776 * include it to get a better pattern, but then we don't
4777 * want the "\\<" prefix, check it bellow */
4778 compl_col = (colnr_T)(skipwhite(line) - line);
4779 compl_startpos.col = compl_col;
4780 compl_startpos.lnum = curwin->w_cursor.lnum;
4781 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 }
4783 else
4784 {
4785 /* S_IPOS was set when we inserted a word that was at the
4786 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004787 * mode but first we need to redefine compl_startpos */
4788 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004790 compl_cont_status |= CONT_SOL;
4791 compl_startpos.col = (colnr_T)(skipwhite(
4792 line + compl_length
4793 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004795 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004797 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004798 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004799 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004801 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004803 compl_cont_status &= ~CONT_SOL;
4804 compl_length = (IOSIZE - MIN_SPACE);
4805 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004806 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004807 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4808 if (compl_length < 1)
4809 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004810 }
4811 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004812 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004814 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
4816 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004817 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004818
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004819 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004820 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004821 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004823 compl_cont_status = 0;
4824 compl_cont_status |= CONT_N_ADDS;
4825 compl_startpos = curwin->w_cursor;
4826 startcol = (int)curs_col;
4827 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004828 }
4829
4830 /* Work out completion pattern and original text -- webb */
4831 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4832 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004833 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4835 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004836 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004838 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004840 compl_col += ++startcol;
4841 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 }
4843 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004844 compl_pattern = str_foldcase(line + compl_col,
4845 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004847 compl_pattern = vim_strnsave(line + compl_col,
4848 compl_length);
4849 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004850 return FAIL;
4851 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004852 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
4854 char_u *prefix = (char_u *)"\\<";
4855
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004856 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004857 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004858 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004859 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004860 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004861 if (!vim_iswordp(line + compl_col)
4862 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 && (
4864#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004865 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004867 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868#endif
4869 )))
4870 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004871 STRCPY((char *)compl_pattern, prefix);
4872 (void)quote_meta(compl_pattern + STRLEN(prefix),
4873 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004875 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004876#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004877 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004879 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880#endif
4881 )
4882 {
4883 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004884 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4885 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004887 compl_col += curs_col;
4888 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004889 }
4890 else
4891 {
4892#ifdef FEAT_MBYTE
4893 /* Search the point of change class of multibyte character
4894 * or not a word single byte character backward. */
4895 if (has_mbyte)
4896 {
4897 int base_class;
4898 int head_off;
4899
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004900 startcol -= (*mb_head_off)(line, line + startcol);
4901 base_class = mb_get_class(line + startcol);
4902 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004904 head_off = (*mb_head_off)(line, line + startcol);
4905 if (base_class != mb_get_class(line + startcol
4906 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004908 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004909 }
4910 }
4911 else
4912#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004913 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004914 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004915 compl_col += ++startcol;
4916 compl_length = (int)curs_col - startcol;
4917 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004918 {
4919 /* Only match word with at least two chars -- webb
4920 * there's no need to call quote_meta,
4921 * alloc(7) is enough -- Acevedo
4922 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004923 compl_pattern = alloc(7);
4924 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004926 STRCPY((char *)compl_pattern, "\\<");
4927 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4928 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004929 }
4930 else
4931 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004932 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004933 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004934 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004935 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004936 STRCPY((char *)compl_pattern, "\\<");
4937 (void)quote_meta(compl_pattern + 2, line + compl_col,
4938 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 }
4941 }
4942 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4943 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004944 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004945 compl_length = (int)curs_col - (int)compl_col;
4946 if (compl_length < 0) /* cursor in indent: empty pattern */
4947 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004949 compl_pattern = str_foldcase(line + compl_col, compl_length,
4950 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004952 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4953 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 return FAIL;
4955 }
4956 else if (ctrl_x_mode == CTRL_X_FILES)
4957 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004958 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004960 compl_col += ++startcol;
4961 compl_length = (int)curs_col - startcol;
4962 compl_pattern = addstar(line + compl_col, compl_length,
4963 EXPAND_FILES);
4964 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 return FAIL;
4966 }
4967 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4968 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004969 compl_pattern = vim_strnsave(line, curs_col);
4970 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004971 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004972 set_cmd_context(&compl_xp, compl_pattern,
4973 (int)STRLEN(compl_pattern), curs_col);
4974 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4975 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004976 /* No completion possible, use an empty pattern to get a
4977 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004978 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004979 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004980 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
4981 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004983 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004984 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00004985#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004986 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00004987 * Call user defined function 'completefunc' with "a:findstart"
4988 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004989 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00004990 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00004991 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004992 char_u *funcname;
4993 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004994
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004995 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00004996 * string */
4997 funcname = ctrl_x_mode == CTRL_X_FUNCTION
4998 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
4999 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005000 {
5001 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5002 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005003 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005004 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005005
5006 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005007 args[1] = NULL;
5008 pos = curwin->w_cursor;
5009 col = call_func_retnr(funcname, 2, args, FALSE);
5010 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005011
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005012 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005013 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005014 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005015 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005016 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005017
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005018 /* Setup variables for completion. Need to obtain "line" again,
5019 * it may have become invalid. */
5020 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005021 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005022 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5023 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005024#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005025 return FAIL;
5026 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005027 else if (ctrl_x_mode == CTRL_X_SPELL)
5028 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005029#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005030 if (spell_bad_len > 0)
5031 compl_col = curs_col - spell_bad_len;
5032 else
5033 compl_col = spell_word_start(startcol);
5034 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005035 {
5036 compl_length = 0;
5037 compl_col = curs_col;
5038 }
5039 else
5040 {
5041 spell_expand_check_cap(compl_col);
5042 compl_length = (int)curs_col - compl_col;
5043 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005044 /* Need to obtain "line" again, it may have become invalid. */
5045 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005046 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5047 if (compl_pattern == NULL)
5048#endif
5049 return FAIL;
5050 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005051 else
5052 {
5053 EMSG2(_(e_intern2), "ins_complete()");
5054 return FAIL;
5055 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005057 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005058 {
5059 edit_submode_pre = (char_u *)_(" Adding");
5060 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5061 {
5062 /* Insert a new line, keep indentation but ignore 'comments' */
5063#ifdef FEAT_COMMENTS
5064 char_u *old = curbuf->b_p_com;
5065
5066 curbuf->b_p_com = (char_u *)"";
5067#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005068 compl_startpos.lnum = curwin->w_cursor.lnum;
5069 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 ins_eol('\r');
5071#ifdef FEAT_COMMENTS
5072 curbuf->b_p_com = old;
5073#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005074 compl_length = 0;
5075 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 }
5077 }
5078 else
5079 {
5080 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005081 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 }
5083
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005084 if (compl_cont_status & CONT_LOCAL)
5085 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005086 else
5087 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5088
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005089 /* Always add completion for the original text. */
5090 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005091 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5092 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005093 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005095 vim_free(compl_pattern);
5096 compl_pattern = NULL;
5097 vim_free(compl_orig_text);
5098 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005099 return FAIL;
5100 }
5101
5102 /* showmode might reset the internal line pointers, so it must
5103 * be called before line = ml_get(), or when this address is no
5104 * longer needed. -- Acevedo.
5105 */
5106 edit_submode_extra = (char_u *)_("-- Searching...");
5107 edit_submode_highl = HLF_COUNT;
5108 showmode();
5109 edit_submode_extra = NULL;
5110 out_flush();
5111 }
5112
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005113 compl_shown_match = compl_curr_match;
5114 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115
5116 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005117 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005119 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005120 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005121
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005122 /* may undisplay the popup menu */
5123 ins_compl_upd_pum();
5124
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005125 if (n > 1) /* all matches have been found */
5126 compl_matches = n;
5127 compl_curr_match = compl_shown_match;
5128 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129
Bram Moolenaard68071d2006-05-02 22:08:30 +00005130 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5131 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 if (got_int && !global_busy)
5133 {
5134 (void)vgetc();
5135 got_int = FALSE;
5136 }
5137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005138 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005139 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005141 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5142 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005143 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5144 edit_submode_highl = HLF_E;
5145 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5146 * because we couldn't expand anything at first place, but if we used
5147 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5148 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005149 if ( compl_length > 1
5150 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 || (ctrl_x_mode != 0
5152 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5153 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005154 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 }
5156
Bram Moolenaar572cb562005-08-05 21:35:02 +00005157 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005158 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005161
5162 if (edit_submode_extra == NULL)
5163 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005164 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005165 {
5166 edit_submode_extra = (char_u *)_("Back at original");
5167 edit_submode_highl = HLF_W;
5168 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005169 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 {
5171 edit_submode_extra = (char_u *)_("Word from other line");
5172 edit_submode_highl = HLF_COUNT;
5173 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005174 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 {
5176 edit_submode_extra = (char_u *)_("The only match");
5177 edit_submode_highl = HLF_COUNT;
5178 }
5179 else
5180 {
5181 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005182 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005184 int number = 0;
5185 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005187 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 {
5189 /* search backwards for the first valid (!= -1) number.
5190 * This should normally succeed already at the first loop
5191 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005192 for (match = compl_curr_match->cp_prev; match != NULL
5193 && match != compl_first_match;
5194 match = match->cp_prev)
5195 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005197 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199 }
5200 if (match != NULL)
5201 /* go up and assign all numbers which are not assigned
5202 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005203 for (match = match->cp_next;
5204 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005205 match = match->cp_next)
5206 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 }
5208 else /* BACKWARD */
5209 {
5210 /* search forwards (upwards) for the first valid (!= -1)
5211 * number. This should normally succeed already at the
5212 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005213 for (match = compl_curr_match->cp_next; match != NULL
5214 && match != compl_first_match;
5215 match = match->cp_next)
5216 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005218 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005219 break;
5220 }
5221 if (match != NULL)
5222 /* go down and assign all numbers which are not
5223 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005224 for (match = match->cp_prev; match
5225 && match->cp_number == -1;
5226 match = match->cp_prev)
5227 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 }
5229 }
5230
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005231 /* The match should always have a sequence number now, this is
5232 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005233 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005235 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5236 * Translations may need more than twice that. */
5237 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005239 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005240 vim_snprintf((char *)match_ref, sizeof(match_ref),
5241 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005242 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005243 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005244 vim_snprintf((char *)match_ref, sizeof(match_ref),
5245 _("match %d"),
5246 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 edit_submode_extra = match_ref;
5248 edit_submode_highl = HLF_R;
5249 if (dollar_vcol)
5250 curs_columns(FALSE);
5251 }
5252 }
5253 }
5254
5255 /* Show a message about what (completion) mode we're in. */
5256 showmode();
5257 if (edit_submode_extra != NULL)
5258 {
5259 if (!p_smd)
5260 msg_attr(edit_submode_extra,
5261 edit_submode_highl < HLF_COUNT
5262 ? hl_attr(edit_submode_highl) : 0);
5263 }
5264 else
5265 msg_clr_cmdline(); /* necessary for "noshowmode" */
5266
Bram Moolenaard68071d2006-05-02 22:08:30 +00005267 /* Show the popup menu, unless we got interrupted. */
5268 if (!compl_interrupted)
5269 {
5270 /* RedrawingDisabled may be set when invoked through complete(). */
5271 n = RedrawingDisabled;
5272 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005273
5274 /* If the cursor moved we need to remove the pum first. */
5275 setcursor();
5276 if (save_w_wrow != curwin->w_wrow)
5277 ins_compl_del_pum();
5278
Bram Moolenaard68071d2006-05-02 22:08:30 +00005279 ins_compl_show_pum();
5280 setcursor();
5281 RedrawingDisabled = n;
5282 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005283 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005284 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005285
Bram Moolenaar071d4272004-06-13 20:20:40 +00005286 return OK;
5287}
5288
5289/*
5290 * Looks in the first "len" chars. of "src" for search-metachars.
5291 * If dest is not NULL the chars. are copied there quoting (with
5292 * a backslash) the metachars, and dest would be NUL terminated.
5293 * Returns the length (needed) of dest
5294 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005295 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296quote_meta(dest, src, len)
5297 char_u *dest;
5298 char_u *src;
5299 int len;
5300{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005301 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005303 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 {
5305 switch (*src)
5306 {
5307 case '.':
5308 case '*':
5309 case '[':
5310 if (ctrl_x_mode == CTRL_X_DICTIONARY
5311 || ctrl_x_mode == CTRL_X_THESAURUS)
5312 break;
5313 case '~':
5314 if (!p_magic) /* quote these only if magic is set */
5315 break;
5316 case '\\':
5317 if (ctrl_x_mode == CTRL_X_DICTIONARY
5318 || ctrl_x_mode == CTRL_X_THESAURUS)
5319 break;
5320 case '^': /* currently it's not needed. */
5321 case '$':
5322 m++;
5323 if (dest != NULL)
5324 *dest++ = '\\';
5325 break;
5326 }
5327 if (dest != NULL)
5328 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005329# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005330 /* Copy remaining bytes of a multibyte character. */
5331 if (has_mbyte)
5332 {
5333 int i, mb_len;
5334
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005335 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005336 if (mb_len > 0 && len >= mb_len)
5337 for (i = 0; i < mb_len; ++i)
5338 {
5339 --len;
5340 ++src;
5341 if (dest != NULL)
5342 *dest++ = *src;
5343 }
5344 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005345# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 }
5347 if (dest != NULL)
5348 *dest = NUL;
5349
5350 return m;
5351}
5352#endif /* FEAT_INS_EXPAND */
5353
5354/*
5355 * Next character is interpreted literally.
5356 * A one, two or three digit decimal number is interpreted as its byte value.
5357 * If one or two digits are entered, the next character is given to vungetc().
5358 * For Unicode a character > 255 may be returned.
5359 */
5360 int
5361get_literal()
5362{
5363 int cc;
5364 int nc;
5365 int i;
5366 int hex = FALSE;
5367 int octal = FALSE;
5368#ifdef FEAT_MBYTE
5369 int unicode = 0;
5370#endif
5371
5372 if (got_int)
5373 return Ctrl_C;
5374
5375#ifdef FEAT_GUI
5376 /*
5377 * In GUI there is no point inserting the internal code for a special key.
5378 * It is more useful to insert the string "<KEY>" instead. This would
5379 * probably be useful in a text window too, but it would not be
5380 * vi-compatible (maybe there should be an option for it?) -- webb
5381 */
5382 if (gui.in_use)
5383 ++allow_keys;
5384#endif
5385#ifdef USE_ON_FLY_SCROLL
5386 dont_scroll = TRUE; /* disallow scrolling here */
5387#endif
5388 ++no_mapping; /* don't map the next key hits */
5389 cc = 0;
5390 i = 0;
5391 for (;;)
5392 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005393 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005394#ifdef FEAT_CMDL_INFO
5395 if (!(State & CMDLINE)
5396# ifdef FEAT_MBYTE
5397 && MB_BYTE2LEN_CHECK(nc) == 1
5398# endif
5399 )
5400 add_to_showcmd(nc);
5401#endif
5402 if (nc == 'x' || nc == 'X')
5403 hex = TRUE;
5404 else if (nc == 'o' || nc == 'O')
5405 octal = TRUE;
5406#ifdef FEAT_MBYTE
5407 else if (nc == 'u' || nc == 'U')
5408 unicode = nc;
5409#endif
5410 else
5411 {
5412 if (hex
5413#ifdef FEAT_MBYTE
5414 || unicode != 0
5415#endif
5416 )
5417 {
5418 if (!vim_isxdigit(nc))
5419 break;
5420 cc = cc * 16 + hex2nr(nc);
5421 }
5422 else if (octal)
5423 {
5424 if (nc < '0' || nc > '7')
5425 break;
5426 cc = cc * 8 + nc - '0';
5427 }
5428 else
5429 {
5430 if (!VIM_ISDIGIT(nc))
5431 break;
5432 cc = cc * 10 + nc - '0';
5433 }
5434
5435 ++i;
5436 }
5437
5438 if (cc > 255
5439#ifdef FEAT_MBYTE
5440 && unicode == 0
5441#endif
5442 )
5443 cc = 255; /* limit range to 0-255 */
5444 nc = 0;
5445
5446 if (hex) /* hex: up to two chars */
5447 {
5448 if (i >= 2)
5449 break;
5450 }
5451#ifdef FEAT_MBYTE
5452 else if (unicode) /* Unicode: up to four or eight chars */
5453 {
5454 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5455 break;
5456 }
5457#endif
5458 else if (i >= 3) /* decimal or octal: up to three chars */
5459 break;
5460 }
5461 if (i == 0) /* no number entered */
5462 {
5463 if (nc == K_ZERO) /* NUL is stored as NL */
5464 {
5465 cc = '\n';
5466 nc = 0;
5467 }
5468 else
5469 {
5470 cc = nc;
5471 nc = 0;
5472 }
5473 }
5474
5475 if (cc == 0) /* NUL is stored as NL */
5476 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005477#ifdef FEAT_MBYTE
5478 if (enc_dbcs && (cc & 0xff) == 0)
5479 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5480 second byte will cause trouble! */
5481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005482
5483 --no_mapping;
5484#ifdef FEAT_GUI
5485 if (gui.in_use)
5486 --allow_keys;
5487#endif
5488 if (nc)
5489 vungetc(nc);
5490 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5491 return cc;
5492}
5493
5494/*
5495 * Insert character, taking care of special keys and mod_mask
5496 */
5497 static void
5498insert_special(c, allow_modmask, ctrlv)
5499 int c;
5500 int allow_modmask;
5501 int ctrlv; /* c was typed after CTRL-V */
5502{
5503 char_u *p;
5504 int len;
5505
5506 /*
5507 * Special function key, translate into "<Key>". Up to the last '>' is
5508 * inserted with ins_str(), so as not to replace characters in replace
5509 * mode.
5510 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5511 * unless 'allow_modmask' is TRUE.
5512 */
5513#ifdef MACOS
5514 /* Command-key never produces a normal key */
5515 if (mod_mask & MOD_MASK_CMD)
5516 allow_modmask = TRUE;
5517#endif
5518 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5519 {
5520 p = get_special_key_name(c, mod_mask);
5521 len = (int)STRLEN(p);
5522 c = p[len - 1];
5523 if (len > 2)
5524 {
5525 if (stop_arrow() == FAIL)
5526 return;
5527 p[len - 1] = NUL;
5528 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005529 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005530 ctrlv = FALSE;
5531 }
5532 }
5533 if (stop_arrow() == OK)
5534 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5535}
5536
5537/*
5538 * Special characters in this context are those that need processing other
5539 * than the simple insertion that can be performed here. This includes ESC
5540 * which terminates the insert, and CR/NL which need special processing to
5541 * open up a new line. This routine tries to optimize insertions performed by
5542 * the "redo", "undo" or "put" commands, so it needs to know when it should
5543 * stop and defer processing to the "normal" mechanism.
5544 * '0' and '^' are special, because they can be followed by CTRL-D.
5545 */
5546#ifdef EBCDIC
5547# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5548#else
5549# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5550#endif
5551
5552#ifdef FEAT_MBYTE
5553# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5554#else
5555# define WHITECHAR(cc) vim_iswhite(cc)
5556#endif
5557
5558 void
5559insertchar(c, flags, second_indent)
5560 int c; /* character to insert or NUL */
5561 int flags; /* INSCHAR_FORMAT, etc. */
5562 int second_indent; /* indent for second line if >= 0 */
5563{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005564 int textwidth;
5565#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005566 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005567#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005568 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005569
5570 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5571 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572
5573 /*
5574 * Try to break the line in two or more pieces when:
5575 * - Always do this if we have been called to do formatting only.
5576 * - Always do this when 'formatoptions' has the 'a' flag and the line
5577 * ends in white space.
5578 * - Otherwise:
5579 * - Don't do this if inserting a blank
5580 * - Don't do this if an existing character is being replaced, unless
5581 * we're in VREPLACE mode.
5582 * - Do this if the cursor is not on the line where insert started
5583 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5584 * before the insert.
5585 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5586 * before 'textwidth'
5587 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005588 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005589 && ((flags & INSCHAR_FORMAT)
5590 || (!vim_iswhite(c)
5591 && !((State & REPLACE_FLAG)
5592#ifdef FEAT_VREPLACE
5593 && !(State & VREPLACE_FLAG)
5594#endif
5595 && *ml_get_cursor() != NUL)
5596 && (curwin->w_cursor.lnum != Insstart.lnum
5597 || ((!has_format_option(FO_INS_LONG)
5598 || Insstart_textlen <= (colnr_T)textwidth)
5599 && (!fo_ins_blank
5600 || Insstart_blank_vcol <= (colnr_T)textwidth
5601 ))))))
5602 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005603 /* Format with 'formatexpr' when it's set. Use internal formatting
5604 * when 'formatexpr' isn't set or it returns non-zero. */
5605#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005606 int do_internal = TRUE;
5607
Bram Moolenaar81a82092008-03-12 16:27:00 +00005608 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005609 {
5610 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5611 /* It may be required to save for undo again, e.g. when setline()
5612 * was called. */
5613 ins_need_undo = TRUE;
5614 }
5615 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005616#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005617 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005618 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005619
Bram Moolenaar071d4272004-06-13 20:20:40 +00005620 if (c == NUL) /* only formatting was wanted */
5621 return;
5622
5623#ifdef FEAT_COMMENTS
5624 /* Check whether this character should end a comment. */
5625 if (did_ai && (int)c == end_comment_pending)
5626 {
5627 char_u *line;
5628 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5629 int middle_len, end_len;
5630 int i;
5631
5632 /*
5633 * Need to remove existing (middle) comment leader and insert end
5634 * comment leader. First, check what comment leader we can find.
5635 */
5636 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5637 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5638 {
5639 /* Skip middle-comment string */
5640 while (*p && p[-1] != ':') /* find end of middle flags */
5641 ++p;
5642 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5643 /* Don't count trailing white space for middle_len */
5644 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5645 --middle_len;
5646
5647 /* Find the end-comment string */
5648 while (*p && p[-1] != ':') /* find end of end flags */
5649 ++p;
5650 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5651
5652 /* Skip white space before the cursor */
5653 i = curwin->w_cursor.col;
5654 while (--i >= 0 && vim_iswhite(line[i]))
5655 ;
5656 i++;
5657
5658 /* Skip to before the middle leader */
5659 i -= middle_len;
5660
5661 /* Check some expected things before we go on */
5662 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5663 {
5664 /* Backspace over all the stuff we want to replace */
5665 backspace_until_column(i);
5666
5667 /*
5668 * Insert the end-comment string, except for the last
5669 * character, which will get inserted as normal later.
5670 */
5671 ins_bytes_len(lead_end, end_len - 1);
5672 }
5673 }
5674 }
5675 end_comment_pending = NUL;
5676#endif
5677
5678 did_ai = FALSE;
5679#ifdef FEAT_SMARTINDENT
5680 did_si = FALSE;
5681 can_si = FALSE;
5682 can_si_back = FALSE;
5683#endif
5684
5685 /*
5686 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5687 * This speeds up normal text input considerably.
5688 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5689 * need to re-indent at a ':', or any other character (but not what
5690 * 'paste' is set)..
5691 */
5692#ifdef USE_ON_FLY_SCROLL
5693 dont_scroll = FALSE; /* allow scrolling here */
5694#endif
5695
5696 if ( !ISSPECIAL(c)
5697#ifdef FEAT_MBYTE
5698 && (!has_mbyte || (*mb_char2len)(c) == 1)
5699#endif
5700 && vpeekc() != NUL
5701 && !(State & REPLACE_FLAG)
5702#ifdef FEAT_CINDENT
5703 && !cindent_on()
5704#endif
5705#ifdef FEAT_RIGHTLEFT
5706 && !p_ri
5707#endif
5708 )
5709 {
5710#define INPUT_BUFLEN 100
5711 char_u buf[INPUT_BUFLEN + 1];
5712 int i;
5713 colnr_T virtcol = 0;
5714
5715 buf[0] = c;
5716 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005717 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 virtcol = get_nolist_virtcol();
5719 /*
5720 * Stop the string when:
5721 * - no more chars available
5722 * - finding a special character (command key)
5723 * - buffer is full
5724 * - running into the 'textwidth' boundary
5725 * - need to check for abbreviation: A non-word char after a word-char
5726 */
5727 while ( (c = vpeekc()) != NUL
5728 && !ISSPECIAL(c)
5729#ifdef FEAT_MBYTE
5730 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5731#endif
5732 && i < INPUT_BUFLEN
5733 && (textwidth == 0
5734 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5735 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5736 {
5737#ifdef FEAT_RIGHTLEFT
5738 c = vgetc();
5739 if (p_hkmap && KeyTyped)
5740 c = hkmap(c); /* Hebrew mode mapping */
5741# ifdef FEAT_FKMAP
5742 if (p_fkmap && KeyTyped)
5743 c = fkmap(c); /* Farsi mode mapping */
5744# endif
5745 buf[i++] = c;
5746#else
5747 buf[i++] = vgetc();
5748#endif
5749 }
5750
5751#ifdef FEAT_DIGRAPHS
5752 do_digraph(-1); /* clear digraphs */
5753 do_digraph(buf[i-1]); /* may be the start of a digraph */
5754#endif
5755 buf[i] = NUL;
5756 ins_str(buf);
5757 if (flags & INSCHAR_CTRLV)
5758 {
5759 redo_literal(*buf);
5760 i = 1;
5761 }
5762 else
5763 i = 0;
5764 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005765 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005766 }
5767 else
5768 {
5769#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005770 int cc;
5771
Bram Moolenaar071d4272004-06-13 20:20:40 +00005772 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5773 {
5774 char_u buf[MB_MAXBYTES + 1];
5775
5776 (*mb_char2bytes)(c, buf);
5777 buf[cc] = NUL;
5778 ins_char_bytes(buf, cc);
5779 AppendCharToRedobuff(c);
5780 }
5781 else
5782#endif
5783 {
5784 ins_char(c);
5785 if (flags & INSCHAR_CTRLV)
5786 redo_literal(c);
5787 else
5788 AppendCharToRedobuff(c);
5789 }
5790 }
5791}
5792
5793/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005794 * Format text at the current insert position.
5795 */
5796 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00005797internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005798 int textwidth;
5799 int second_indent;
5800 int flags;
5801 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005802 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005803{
5804 int cc;
5805 int save_char = NUL;
5806 int haveto_redraw = FALSE;
5807 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5808#ifdef FEAT_MBYTE
5809 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5810#endif
5811 int fo_white_par = has_format_option(FO_WHITE_PAR);
5812 int first_line = TRUE;
5813#ifdef FEAT_COMMENTS
5814 colnr_T leader_len;
5815 int no_leader = FALSE;
5816 int do_comments = (flags & INSCHAR_DO_COM);
5817#endif
5818
5819 /*
5820 * When 'ai' is off we don't want a space under the cursor to be
5821 * deleted. Replace it with an 'x' temporarily.
5822 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005823 if (!curbuf->b_p_ai
5824#ifdef FEAT_VREPLACE
5825 && !(State & VREPLACE_FLAG)
5826#endif
5827 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005828 {
5829 cc = gchar_cursor();
5830 if (vim_iswhite(cc))
5831 {
5832 save_char = cc;
5833 pchar_cursor('x');
5834 }
5835 }
5836
5837 /*
5838 * Repeat breaking lines, until the current line is not too long.
5839 */
5840 while (!got_int)
5841 {
5842 int startcol; /* Cursor column at entry */
5843 int wantcol; /* column at textwidth border */
5844 int foundcol; /* column for start of spaces */
5845 int end_foundcol = 0; /* column for start of word */
5846 colnr_T len;
5847 colnr_T virtcol;
5848#ifdef FEAT_VREPLACE
5849 int orig_col = 0;
5850 char_u *saved_text = NULL;
5851#endif
5852 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005853 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005854
Bram Moolenaar97b98102009-11-17 16:41:01 +00005855 virtcol = get_nolist_virtcol()
5856 + char2cells(c != NUL ? c : gchar_cursor());
5857 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005858 break;
5859
5860#ifdef FEAT_COMMENTS
5861 if (no_leader)
5862 do_comments = FALSE;
5863 else if (!(flags & INSCHAR_FORMAT)
5864 && has_format_option(FO_WRAP_COMS))
5865 do_comments = TRUE;
5866
5867 /* Don't break until after the comment leader */
5868 if (do_comments)
5869 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5870 else
5871 leader_len = 0;
5872
5873 /* If the line doesn't start with a comment leader, then don't
5874 * start one in a following broken line. Avoids that a %word
5875 * moved to the start of the next line causes all following lines
5876 * to start with %. */
5877 if (leader_len == 0)
5878 no_leader = TRUE;
5879#endif
5880 if (!(flags & INSCHAR_FORMAT)
5881#ifdef FEAT_COMMENTS
5882 && leader_len == 0
5883#endif
5884 && !has_format_option(FO_WRAP))
5885
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005886 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005887 if ((startcol = curwin->w_cursor.col) == 0)
5888 break;
5889
5890 /* find column of textwidth border */
5891 coladvance((colnr_T)textwidth);
5892 wantcol = curwin->w_cursor.col;
5893
Bram Moolenaar97b98102009-11-17 16:41:01 +00005894 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005895 foundcol = 0;
5896
5897 /*
5898 * Find position to break at.
5899 * Stop at first entered white when 'formatoptions' has 'v'
5900 */
5901 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5902 || curwin->w_cursor.lnum != Insstart.lnum
5903 || curwin->w_cursor.col >= Insstart.col)
5904 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00005905 if (curwin->w_cursor.col == startcol && c != NUL)
5906 cc = c;
5907 else
5908 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005909 if (WHITECHAR(cc))
5910 {
5911 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005912 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005913
5914 /* find start of sequence of blanks */
5915 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5916 {
5917 dec_cursor();
5918 cc = gchar_cursor();
5919 }
5920 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5921 break; /* only spaces in front of text */
5922#ifdef FEAT_COMMENTS
5923 /* Don't break until after the comment leader */
5924 if (curwin->w_cursor.col < leader_len)
5925 break;
5926#endif
5927 if (has_format_option(FO_ONE_LETTER))
5928 {
5929 /* do not break after one-letter words */
5930 if (curwin->w_cursor.col == 0)
5931 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005932#ifdef FEAT_COMMENTS
5933 /* do not break "#a b" when 'tw' is 2 */
5934 if (curwin->w_cursor.col <= leader_len)
5935 break;
5936#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005937 col = curwin->w_cursor.col;
5938 dec_cursor();
5939 cc = gchar_cursor();
5940
5941 if (WHITECHAR(cc))
5942 continue; /* one-letter, continue */
5943 curwin->w_cursor.col = col;
5944 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00005945
5946 inc_cursor();
5947
5948 end_foundcol = end_col + 1;
5949 foundcol = curwin->w_cursor.col;
5950 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005951 break;
5952 }
5953#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00005954 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005955 {
5956 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005957 if (curwin->w_cursor.col != startcol)
5958 {
5959#ifdef FEAT_COMMENTS
5960 /* Don't break until after the comment leader */
5961 if (curwin->w_cursor.col < leader_len)
5962 break;
5963#endif
5964 col = curwin->w_cursor.col;
5965 inc_cursor();
5966 /* Don't change end_foundcol if already set. */
5967 if (foundcol != curwin->w_cursor.col)
5968 {
5969 foundcol = curwin->w_cursor.col;
5970 end_foundcol = foundcol;
5971 if (curwin->w_cursor.col <= (colnr_T)wantcol)
5972 break;
5973 }
5974 curwin->w_cursor.col = col;
5975 }
5976
5977 if (curwin->w_cursor.col == 0)
5978 break;
5979
5980 col = curwin->w_cursor.col;
5981
5982 dec_cursor();
5983 cc = gchar_cursor();
5984
5985 if (WHITECHAR(cc))
5986 continue; /* break with space */
5987#ifdef FEAT_COMMENTS
5988 /* Don't break until after the comment leader */
5989 if (curwin->w_cursor.col < leader_len)
5990 break;
5991#endif
5992
5993 curwin->w_cursor.col = col;
5994
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005995 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005996 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005997 if (curwin->w_cursor.col <= (colnr_T)wantcol)
5998 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005999 }
6000#endif
6001 if (curwin->w_cursor.col == 0)
6002 break;
6003 dec_cursor();
6004 }
6005
6006 if (foundcol == 0) /* no spaces, cannot break line */
6007 {
6008 curwin->w_cursor.col = startcol;
6009 break;
6010 }
6011
6012 /* Going to break the line, remove any "$" now. */
6013 undisplay_dollar();
6014
6015 /*
6016 * Offset between cursor position and line break is used by replace
6017 * stack functions. VREPLACE does not use this, and backspaces
6018 * over the text instead.
6019 */
6020#ifdef FEAT_VREPLACE
6021 if (State & VREPLACE_FLAG)
6022 orig_col = startcol; /* Will start backspacing from here */
6023 else
6024#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006025 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006026
6027 /*
6028 * adjust startcol for spaces that will be deleted and
6029 * characters that will remain on top line
6030 */
6031 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006032 while ((cc = gchar_cursor(), WHITECHAR(cc))
6033 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006034 inc_cursor();
6035 startcol -= curwin->w_cursor.col;
6036 if (startcol < 0)
6037 startcol = 0;
6038
6039#ifdef FEAT_VREPLACE
6040 if (State & VREPLACE_FLAG)
6041 {
6042 /*
6043 * In VREPLACE mode, we will backspace over the text to be
6044 * wrapped, so save a copy now to put on the next line.
6045 */
6046 saved_text = vim_strsave(ml_get_cursor());
6047 curwin->w_cursor.col = orig_col;
6048 if (saved_text == NULL)
6049 break; /* Can't do it, out of memory */
6050 saved_text[startcol] = NUL;
6051
6052 /* Backspace over characters that will move to the next line */
6053 if (!fo_white_par)
6054 backspace_until_column(foundcol);
6055 }
6056 else
6057#endif
6058 {
6059 /* put cursor after pos. to break line */
6060 if (!fo_white_par)
6061 curwin->w_cursor.col = foundcol;
6062 }
6063
6064 /*
6065 * Split the line just before the margin.
6066 * Only insert/delete lines, but don't really redraw the window.
6067 */
6068 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6069 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6070#ifdef FEAT_COMMENTS
6071 + (do_comments ? OPENLINE_DO_COM : 0)
6072#endif
6073 , old_indent);
6074 old_indent = 0;
6075
6076 replace_offset = 0;
6077 if (first_line)
6078 {
6079 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6080 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6081 if (second_indent >= 0)
6082 {
6083#ifdef FEAT_VREPLACE
6084 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006085 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006086 else
6087#endif
6088 (void)set_indent(second_indent, SIN_CHANGED);
6089 }
6090 first_line = FALSE;
6091 }
6092
6093#ifdef FEAT_VREPLACE
6094 if (State & VREPLACE_FLAG)
6095 {
6096 /*
6097 * In VREPLACE mode we have backspaced over the text to be
6098 * moved, now we re-insert it into the new line.
6099 */
6100 ins_bytes(saved_text);
6101 vim_free(saved_text);
6102 }
6103 else
6104#endif
6105 {
6106 /*
6107 * Check if cursor is not past the NUL off the line, cindent
6108 * may have added or removed indent.
6109 */
6110 curwin->w_cursor.col += startcol;
6111 len = (colnr_T)STRLEN(ml_get_curline());
6112 if (curwin->w_cursor.col > len)
6113 curwin->w_cursor.col = len;
6114 }
6115
6116 haveto_redraw = TRUE;
6117#ifdef FEAT_CINDENT
6118 can_cindent = TRUE;
6119#endif
6120 /* moved the cursor, don't autoindent or cindent now */
6121 did_ai = FALSE;
6122#ifdef FEAT_SMARTINDENT
6123 did_si = FALSE;
6124 can_si = FALSE;
6125 can_si_back = FALSE;
6126#endif
6127 line_breakcheck();
6128 }
6129
6130 if (save_char != NUL) /* put back space after cursor */
6131 pchar_cursor(save_char);
6132
6133 if (!format_only && haveto_redraw)
6134 {
6135 update_topline();
6136 redraw_curbuf_later(VALID);
6137 }
6138}
6139
6140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006141 * Called after inserting or deleting text: When 'formatoptions' includes the
6142 * 'a' flag format from the current line until the end of the paragraph.
6143 * Keep the cursor at the same position relative to the text.
6144 * The caller must have saved the cursor line for undo, following ones will be
6145 * saved here.
6146 */
6147 void
6148auto_format(trailblank, prev_line)
6149 int trailblank; /* when TRUE also format with trailing blank */
6150 int prev_line; /* may start in previous line */
6151{
6152 pos_T pos;
6153 colnr_T len;
6154 char_u *old;
6155 char_u *new, *pnew;
6156 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006157 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006158
6159 if (!has_format_option(FO_AUTO))
6160 return;
6161
6162 pos = curwin->w_cursor;
6163 old = ml_get_curline();
6164
6165 /* may remove added space */
6166 check_auto_format(FALSE);
6167
6168 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6169 * user might insert normal text next. Also skip formatting when "1" is
6170 * in 'formatoptions' and there is a single character before the cursor.
6171 * Otherwise the line would be broken and when typing another non-white
6172 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006173 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006174 if (*old != NUL && !trailblank && wasatend)
6175 {
6176 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006177 cc = gchar_cursor();
6178 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6179 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006180 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006181 cc = gchar_cursor();
6182 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 {
6184 curwin->w_cursor = pos;
6185 return;
6186 }
6187 curwin->w_cursor = pos;
6188 }
6189
6190#ifdef FEAT_COMMENTS
6191 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6192 * comments. */
6193 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6194 && get_leader_len(old, NULL, FALSE) == 0)
6195 return;
6196#endif
6197
6198 /*
6199 * May start formatting in a previous line, so that after "x" a word is
6200 * moved to the previous line if it fits there now. Only when this is not
6201 * the start of a paragraph.
6202 */
6203 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6204 {
6205 --curwin->w_cursor.lnum;
6206 if (u_save_cursor() == FAIL)
6207 return;
6208 }
6209
6210 /*
6211 * Do the formatting and restore the cursor position. "saved_cursor" will
6212 * be adjusted for the text formatting.
6213 */
6214 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006215 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 curwin->w_cursor = saved_cursor;
6217 saved_cursor.lnum = 0;
6218
6219 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6220 {
6221 /* "cannot happen" */
6222 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6223 coladvance((colnr_T)MAXCOL);
6224 }
6225 else
6226 check_cursor_col();
6227
6228 /* Insert mode: If the cursor is now after the end of the line while it
6229 * previously wasn't, the line was broken. Because of the rule above we
6230 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6231 * formatted. */
6232 if (!wasatend && has_format_option(FO_WHITE_PAR))
6233 {
6234 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006235 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006236 if (curwin->w_cursor.col == len)
6237 {
6238 pnew = vim_strnsave(new, len + 2);
6239 pnew[len] = ' ';
6240 pnew[len + 1] = NUL;
6241 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6242 /* remove the space later */
6243 did_add_space = TRUE;
6244 }
6245 else
6246 /* may remove added space */
6247 check_auto_format(FALSE);
6248 }
6249
6250 check_cursor();
6251}
6252
6253/*
6254 * When an extra space was added to continue a paragraph for auto-formatting,
6255 * delete it now. The space must be under the cursor, just after the insert
6256 * position.
6257 */
6258 static void
6259check_auto_format(end_insert)
6260 int end_insert; /* TRUE when ending Insert mode */
6261{
6262 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006263 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006264
6265 if (did_add_space)
6266 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006267 cc = gchar_cursor();
6268 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006269 /* Somehow the space was removed already. */
6270 did_add_space = FALSE;
6271 else
6272 {
6273 if (!end_insert)
6274 {
6275 inc_cursor();
6276 c = gchar_cursor();
6277 dec_cursor();
6278 }
6279 if (c != NUL)
6280 {
6281 /* The space is no longer at the end of the line, delete it. */
6282 del_char(FALSE);
6283 did_add_space = FALSE;
6284 }
6285 }
6286 }
6287}
6288
6289/*
6290 * Find out textwidth to be used for formatting:
6291 * if 'textwidth' option is set, use it
6292 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6293 * if invalid value, use 0.
6294 * Set default to window width (maximum 79) for "gq" operator.
6295 */
6296 int
6297comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006298 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006299{
6300 int textwidth;
6301
6302 textwidth = curbuf->b_p_tw;
6303 if (textwidth == 0 && curbuf->b_p_wm)
6304 {
6305 /* The width is the window width minus 'wrapmargin' minus all the
6306 * things that add to the margin. */
6307 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6308#ifdef FEAT_CMDWIN
6309 if (cmdwin_type != 0)
6310 textwidth -= 1;
6311#endif
6312#ifdef FEAT_FOLDING
6313 textwidth -= curwin->w_p_fdc;
6314#endif
6315#ifdef FEAT_SIGNS
6316 if (curwin->w_buffer->b_signlist != NULL
6317# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006318 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006319# endif
6320 )
6321 textwidth -= 1;
6322#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006323 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006324 textwidth -= 8;
6325 }
6326 if (textwidth < 0)
6327 textwidth = 0;
6328 if (ff && textwidth == 0)
6329 {
6330 textwidth = W_WIDTH(curwin) - 1;
6331 if (textwidth > 79)
6332 textwidth = 79;
6333 }
6334 return textwidth;
6335}
6336
6337/*
6338 * Put a character in the redo buffer, for when just after a CTRL-V.
6339 */
6340 static void
6341redo_literal(c)
6342 int c;
6343{
6344 char_u buf[10];
6345
6346 /* Only digits need special treatment. Translate them into a string of
6347 * three digits. */
6348 if (VIM_ISDIGIT(c))
6349 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006350 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006351 AppendToRedobuff(buf);
6352 }
6353 else
6354 AppendCharToRedobuff(c);
6355}
6356
6357/*
6358 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006359 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006360 */
6361 static void
6362start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006363 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006364{
6365 if (!arrow_used) /* something has been inserted */
6366 {
6367 AppendToRedobuff(ESC_STR);
6368 stop_insert(end_insert_pos, FALSE);
6369 arrow_used = TRUE; /* this means we stopped the current insert */
6370 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006371#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006372 check_spell_redraw();
6373#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006374}
6375
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006376#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006377/*
6378 * If we skipped highlighting word at cursor, do it now.
6379 * It may be skipped again, thus reset spell_redraw_lnum first.
6380 */
6381 static void
6382check_spell_redraw()
6383{
6384 if (spell_redraw_lnum != 0)
6385 {
6386 linenr_T lnum = spell_redraw_lnum;
6387
6388 spell_redraw_lnum = 0;
6389 redrawWinline(lnum, FALSE);
6390 }
6391}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006392
6393/*
6394 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6395 * spelled word, if there is one.
6396 */
6397 static void
6398spell_back_to_badword()
6399{
6400 pos_T tpos = curwin->w_cursor;
6401
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006402 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006403 if (curwin->w_cursor.col != tpos.col)
6404 start_arrow(&tpos);
6405}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006406#endif
6407
Bram Moolenaar071d4272004-06-13 20:20:40 +00006408/*
6409 * stop_arrow() is called before a change is made in insert mode.
6410 * If an arrow key has been used, start a new insertion.
6411 * Returns FAIL if undo is impossible, shouldn't insert then.
6412 */
6413 int
6414stop_arrow()
6415{
6416 if (arrow_used)
6417 {
6418 if (u_save_cursor() == OK)
6419 {
6420 arrow_used = FALSE;
6421 ins_need_undo = FALSE;
6422 }
6423 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006424 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006425 ai_col = 0;
6426#ifdef FEAT_VREPLACE
6427 if (State & VREPLACE_FLAG)
6428 {
6429 orig_line_count = curbuf->b_ml.ml_line_count;
6430 vr_lines_changed = 1;
6431 }
6432#endif
6433 ResetRedobuff();
6434 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006435 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006436 }
6437 else if (ins_need_undo)
6438 {
6439 if (u_save_cursor() == OK)
6440 ins_need_undo = FALSE;
6441 }
6442
6443#ifdef FEAT_FOLDING
6444 /* Always open fold at the cursor line when inserting something. */
6445 foldOpenCursor();
6446#endif
6447
6448 return (arrow_used || ins_need_undo ? FAIL : OK);
6449}
6450
6451/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006452 * Do a few things to stop inserting.
6453 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6454 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 */
6456 static void
6457stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006458 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006459 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006460{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006461 int cc;
6462 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006463
6464 stop_redo_ins();
6465 replace_flush(); /* abandon replace stack */
6466
6467 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006468 * Save the inserted text for later redo with ^@ and CTRL-A.
6469 * Don't do it when "restart_edit" was set and nothing was inserted,
6470 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006471 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006472 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006473 if (did_restart_edit == 0 || (ptr != NULL
6474 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006475 {
6476 vim_free(last_insert);
6477 last_insert = ptr;
6478 last_insert_skip = new_insert_skip;
6479 }
6480 else
6481 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006483 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006484 {
6485 /* Auto-format now. It may seem strange to do this when stopping an
6486 * insertion (or moving the cursor), but it's required when appending
6487 * a line and having it end in a space. But only do it when something
6488 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006489 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006491 pos_T tpos = curwin->w_cursor;
6492
Bram Moolenaar071d4272004-06-13 20:20:40 +00006493 /* When the cursor is at the end of the line after a space the
6494 * formatting will move it to the following word. Avoid that by
6495 * moving the cursor onto the space. */
6496 cc = 'x';
6497 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6498 {
6499 dec_cursor();
6500 cc = gchar_cursor();
6501 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006502 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 }
6504
6505 auto_format(TRUE, FALSE);
6506
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006507 if (vim_iswhite(cc))
6508 {
6509 if (gchar_cursor() != NUL)
6510 inc_cursor();
6511#ifdef FEAT_VIRTUALEDIT
6512 /* If the cursor is still at the same character, also keep
6513 * the "coladd". */
6514 if (gchar_cursor() == NUL
6515 && curwin->w_cursor.lnum == tpos.lnum
6516 && curwin->w_cursor.col == tpos.col)
6517 curwin->w_cursor.coladd = tpos.coladd;
6518#endif
6519 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006520 }
6521
6522 /* If a space was inserted for auto-formatting, remove it now. */
6523 check_auto_format(TRUE);
6524
6525 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006526 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006527 * Do this when ESC was used or moving the cursor up/down.
6528 * Check for the old position still being valid, just in case the text
6529 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006530 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006531 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6532 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006533 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006534 pos_T tpos = curwin->w_cursor;
6535
6536 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006537 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006538 for (;;)
6539 {
6540 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6541 --curwin->w_cursor.col;
6542 cc = gchar_cursor();
6543 if (!vim_iswhite(cc))
6544 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006545 if (del_char(TRUE) == FAIL)
6546 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006547 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006548 if (curwin->w_cursor.lnum != tpos.lnum)
6549 curwin->w_cursor = tpos;
6550 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006551 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6552
6553#ifdef FEAT_VISUAL
6554 /* <C-S-Right> may have started Visual mode, adjust the position for
6555 * deleted characters. */
6556 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6557 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006558 int len = (int)STRLEN(ml_get_curline());
6559
6560 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006561 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006562 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006563# ifdef FEAT_VIRTUALEDIT
6564 VIsual.coladd = 0;
6565# endif
6566 }
6567 }
6568#endif
6569 }
6570 }
6571 did_ai = FALSE;
6572#ifdef FEAT_SMARTINDENT
6573 did_si = FALSE;
6574 can_si = FALSE;
6575 can_si_back = FALSE;
6576#endif
6577
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006578 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6579 * now in a different buffer. */
6580 if (end_insert_pos != NULL)
6581 {
6582 curbuf->b_op_start = Insstart;
6583 curbuf->b_op_end = *end_insert_pos;
6584 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006585}
6586
6587/*
6588 * Set the last inserted text to a single character.
6589 * Used for the replace command.
6590 */
6591 void
6592set_last_insert(c)
6593 int c;
6594{
6595 char_u *s;
6596
6597 vim_free(last_insert);
6598#ifdef FEAT_MBYTE
6599 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6600#else
6601 last_insert = alloc(6);
6602#endif
6603 if (last_insert != NULL)
6604 {
6605 s = last_insert;
6606 /* Use the CTRL-V only when entering a special char */
6607 if (c < ' ' || c == DEL)
6608 *s++ = Ctrl_V;
6609 s = add_char2buf(c, s);
6610 *s++ = ESC;
6611 *s++ = NUL;
6612 last_insert_skip = 0;
6613 }
6614}
6615
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006616#if defined(EXITFREE) || defined(PROTO)
6617 void
6618free_last_insert()
6619{
6620 vim_free(last_insert);
6621 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006622# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006623 vim_free(compl_orig_text);
6624 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006625# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006626}
6627#endif
6628
Bram Moolenaar071d4272004-06-13 20:20:40 +00006629/*
6630 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6631 * and CSI. Handle multi-byte characters.
6632 * Returns a pointer to after the added bytes.
6633 */
6634 char_u *
6635add_char2buf(c, s)
6636 int c;
6637 char_u *s;
6638{
6639#ifdef FEAT_MBYTE
6640 char_u temp[MB_MAXBYTES];
6641 int i;
6642 int len;
6643
6644 len = (*mb_char2bytes)(c, temp);
6645 for (i = 0; i < len; ++i)
6646 {
6647 c = temp[i];
6648#endif
6649 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6650 if (c == K_SPECIAL)
6651 {
6652 *s++ = K_SPECIAL;
6653 *s++ = KS_SPECIAL;
6654 *s++ = KE_FILLER;
6655 }
6656#ifdef FEAT_GUI
6657 else if (c == CSI)
6658 {
6659 *s++ = CSI;
6660 *s++ = KS_EXTRA;
6661 *s++ = (int)KE_CSI;
6662 }
6663#endif
6664 else
6665 *s++ = c;
6666#ifdef FEAT_MBYTE
6667 }
6668#endif
6669 return s;
6670}
6671
6672/*
6673 * move cursor to start of line
6674 * if flags & BL_WHITE move to first non-white
6675 * if flags & BL_SOL move to first non-white if startofline is set,
6676 * otherwise keep "curswant" column
6677 * if flags & BL_FIX don't leave the cursor on a NUL.
6678 */
6679 void
6680beginline(flags)
6681 int flags;
6682{
6683 if ((flags & BL_SOL) && !p_sol)
6684 coladvance(curwin->w_curswant);
6685 else
6686 {
6687 curwin->w_cursor.col = 0;
6688#ifdef FEAT_VIRTUALEDIT
6689 curwin->w_cursor.coladd = 0;
6690#endif
6691
6692 if (flags & (BL_WHITE | BL_SOL))
6693 {
6694 char_u *ptr;
6695
6696 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6697 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6698 ++curwin->w_cursor.col;
6699 }
6700 curwin->w_set_curswant = TRUE;
6701 }
6702}
6703
6704/*
6705 * oneright oneleft cursor_down cursor_up
6706 *
6707 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006708 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006709 * Return OK when successful, FAIL when we hit a line of file boundary.
6710 */
6711
6712 int
6713oneright()
6714{
6715 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006716 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006717
6718#ifdef FEAT_VIRTUALEDIT
6719 if (virtual_active())
6720 {
6721 pos_T prevpos = curwin->w_cursor;
6722
6723 /* Adjust for multi-wide char (excluding TAB) */
6724 ptr = ml_get_cursor();
6725 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006726# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006727 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006728# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006729 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006730# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006731 ))
6732 ? ptr2cells(ptr) : 1));
6733 curwin->w_set_curswant = TRUE;
6734 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6735 return (prevpos.col != curwin->w_cursor.col
6736 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6737 }
6738#endif
6739
6740 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006741 if (*ptr == NUL)
6742 return FAIL; /* already at the very end */
6743
Bram Moolenaar071d4272004-06-13 20:20:40 +00006744#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006745 if (has_mbyte)
6746 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006747 else
6748#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006749 l = 1;
6750
6751 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6752 * contains "onemore". */
6753 if (ptr[l] == NUL
6754#ifdef FEAT_VIRTUALEDIT
6755 && (ve_flags & VE_ONEMORE) == 0
6756#endif
6757 )
6758 return FAIL;
6759 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006760
6761 curwin->w_set_curswant = TRUE;
6762 return OK;
6763}
6764
6765 int
6766oneleft()
6767{
6768#ifdef FEAT_VIRTUALEDIT
6769 if (virtual_active())
6770 {
6771 int width;
6772 int v = getviscol();
6773
6774 if (v == 0)
6775 return FAIL;
6776
6777# ifdef FEAT_LINEBREAK
6778 /* We might get stuck on 'showbreak', skip over it. */
6779 width = 1;
6780 for (;;)
6781 {
6782 coladvance(v - width);
6783 /* getviscol() is slow, skip it when 'showbreak' is empty and
6784 * there are no multi-byte characters */
6785 if ((*p_sbr == NUL
6786# ifdef FEAT_MBYTE
6787 && !has_mbyte
6788# endif
6789 ) || getviscol() < v)
6790 break;
6791 ++width;
6792 }
6793# else
6794 coladvance(v - 1);
6795# endif
6796
6797 if (curwin->w_cursor.coladd == 1)
6798 {
6799 char_u *ptr;
6800
6801 /* Adjust for multi-wide char (not a TAB) */
6802 ptr = ml_get_cursor();
6803 if (*ptr != TAB && vim_isprintc(
6804# ifdef FEAT_MBYTE
6805 (*mb_ptr2char)(ptr)
6806# else
6807 *ptr
6808# endif
6809 ) && ptr2cells(ptr) > 1)
6810 curwin->w_cursor.coladd = 0;
6811 }
6812
6813 curwin->w_set_curswant = TRUE;
6814 return OK;
6815 }
6816#endif
6817
6818 if (curwin->w_cursor.col == 0)
6819 return FAIL;
6820
6821 curwin->w_set_curswant = TRUE;
6822 --curwin->w_cursor.col;
6823
6824#ifdef FEAT_MBYTE
6825 /* if the character on the left of the current cursor is a multi-byte
6826 * character, move to its first byte */
6827 if (has_mbyte)
6828 mb_adjust_cursor();
6829#endif
6830 return OK;
6831}
6832
6833 int
6834cursor_up(n, upd_topline)
6835 long n;
6836 int upd_topline; /* When TRUE: update topline */
6837{
6838 linenr_T lnum;
6839
6840 if (n > 0)
6841 {
6842 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006843 /* This fails if the cursor is already in the first line or the count
6844 * is larger than the line number and '-' is in 'cpoptions' */
6845 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006846 return FAIL;
6847 if (n >= lnum)
6848 lnum = 1;
6849 else
6850#ifdef FEAT_FOLDING
6851 if (hasAnyFolding(curwin))
6852 {
6853 /*
6854 * Count each sequence of folded lines as one logical line.
6855 */
6856 /* go to the the start of the current fold */
6857 (void)hasFolding(lnum, &lnum, NULL);
6858
6859 while (n--)
6860 {
6861 /* move up one line */
6862 --lnum;
6863 if (lnum <= 1)
6864 break;
6865 /* If we entered a fold, move to the beginning, unless in
6866 * Insert mode or when 'foldopen' contains "all": it will open
6867 * in a moment. */
6868 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6869 (void)hasFolding(lnum, &lnum, NULL);
6870 }
6871 if (lnum < 1)
6872 lnum = 1;
6873 }
6874 else
6875#endif
6876 lnum -= n;
6877 curwin->w_cursor.lnum = lnum;
6878 }
6879
6880 /* try to advance to the column we want to be at */
6881 coladvance(curwin->w_curswant);
6882
6883 if (upd_topline)
6884 update_topline(); /* make sure curwin->w_topline is valid */
6885
6886 return OK;
6887}
6888
6889/*
6890 * Cursor down a number of logical lines.
6891 */
6892 int
6893cursor_down(n, upd_topline)
6894 long n;
6895 int upd_topline; /* When TRUE: update topline */
6896{
6897 linenr_T lnum;
6898
6899 if (n > 0)
6900 {
6901 lnum = curwin->w_cursor.lnum;
6902#ifdef FEAT_FOLDING
6903 /* Move to last line of fold, will fail if it's the end-of-file. */
6904 (void)hasFolding(lnum, NULL, &lnum);
6905#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006906 /* This fails if the cursor is already in the last line or would move
6907 * beyound the last line and '-' is in 'cpoptions' */
6908 if (lnum >= curbuf->b_ml.ml_line_count
6909 || (lnum + n > curbuf->b_ml.ml_line_count
6910 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006911 return FAIL;
6912 if (lnum + n >= curbuf->b_ml.ml_line_count)
6913 lnum = curbuf->b_ml.ml_line_count;
6914 else
6915#ifdef FEAT_FOLDING
6916 if (hasAnyFolding(curwin))
6917 {
6918 linenr_T last;
6919
6920 /* count each sequence of folded lines as one logical line */
6921 while (n--)
6922 {
6923 if (hasFolding(lnum, NULL, &last))
6924 lnum = last + 1;
6925 else
6926 ++lnum;
6927 if (lnum >= curbuf->b_ml.ml_line_count)
6928 break;
6929 }
6930 if (lnum > curbuf->b_ml.ml_line_count)
6931 lnum = curbuf->b_ml.ml_line_count;
6932 }
6933 else
6934#endif
6935 lnum += n;
6936 curwin->w_cursor.lnum = lnum;
6937 }
6938
6939 /* try to advance to the column we want to be at */
6940 coladvance(curwin->w_curswant);
6941
6942 if (upd_topline)
6943 update_topline(); /* make sure curwin->w_topline is valid */
6944
6945 return OK;
6946}
6947
6948/*
6949 * Stuff the last inserted text in the read buffer.
6950 * Last_insert actually is a copy of the redo buffer, so we
6951 * first have to remove the command.
6952 */
6953 int
6954stuff_inserted(c, count, no_esc)
6955 int c; /* Command character to be inserted */
6956 long count; /* Repeat this many times */
6957 int no_esc; /* Don't add an ESC at the end */
6958{
6959 char_u *esc_ptr;
6960 char_u *ptr;
6961 char_u *last_ptr;
6962 char_u last = NUL;
6963
6964 ptr = get_last_insert();
6965 if (ptr == NULL)
6966 {
6967 EMSG(_(e_noinstext));
6968 return FAIL;
6969 }
6970
6971 /* may want to stuff the command character, to start Insert mode */
6972 if (c != NUL)
6973 stuffcharReadbuff(c);
6974 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6975 *esc_ptr = NUL; /* remove the ESC */
6976
6977 /* when the last char is either "0" or "^" it will be quoted if no ESC
6978 * comes after it OR if it will inserted more than once and "ptr"
6979 * starts with ^D. -- Acevedo
6980 */
6981 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
6982 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
6983 && (no_esc || (*ptr == Ctrl_D && count > 1)))
6984 {
6985 last = *last_ptr;
6986 *last_ptr = NUL;
6987 }
6988
6989 do
6990 {
6991 stuffReadbuff(ptr);
6992 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
6993 if (last)
6994 stuffReadbuff((char_u *)(last == '0'
6995 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
6996 : IF_EB("\026^", CTRL_V_STR "^")));
6997 }
6998 while (--count > 0);
6999
7000 if (last)
7001 *last_ptr = last;
7002
7003 if (esc_ptr != NULL)
7004 *esc_ptr = ESC; /* put the ESC back */
7005
7006 /* may want to stuff a trailing ESC, to get out of Insert mode */
7007 if (!no_esc)
7008 stuffcharReadbuff(ESC);
7009
7010 return OK;
7011}
7012
7013 char_u *
7014get_last_insert()
7015{
7016 if (last_insert == NULL)
7017 return NULL;
7018 return last_insert + last_insert_skip;
7019}
7020
7021/*
7022 * Get last inserted string, and remove trailing <Esc>.
7023 * Returns pointer to allocated memory (must be freed) or NULL.
7024 */
7025 char_u *
7026get_last_insert_save()
7027{
7028 char_u *s;
7029 int len;
7030
7031 if (last_insert == NULL)
7032 return NULL;
7033 s = vim_strsave(last_insert + last_insert_skip);
7034 if (s != NULL)
7035 {
7036 len = (int)STRLEN(s);
7037 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7038 s[len - 1] = NUL;
7039 }
7040 return s;
7041}
7042
7043/*
7044 * Check the word in front of the cursor for an abbreviation.
7045 * Called when the non-id character "c" has been entered.
7046 * When an abbreviation is recognized it is removed from the text and
7047 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7048 */
7049 static int
7050echeck_abbr(c)
7051 int c;
7052{
7053 /* Don't check for abbreviation in paste mode, when disabled and just
7054 * after moving around with cursor keys. */
7055 if (p_paste || no_abbr || arrow_used)
7056 return FALSE;
7057
7058 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7059 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7060}
7061
7062/*
7063 * replace-stack functions
7064 *
7065 * When replacing characters, the replaced characters are remembered for each
7066 * new character. This is used to re-insert the old text when backspacing.
7067 *
7068 * There is a NUL headed list of characters for each character that is
7069 * currently in the file after the insertion point. When BS is used, one NUL
7070 * headed list is put back for the deleted character.
7071 *
7072 * For a newline, there are two NUL headed lists. One contains the characters
7073 * that the NL replaced. The extra one stores the characters after the cursor
7074 * that were deleted (always white space).
7075 *
7076 * Replace_offset is normally 0, in which case replace_push will add a new
7077 * character at the end of the stack. If replace_offset is not 0, that many
7078 * characters will be left on the stack above the newly inserted character.
7079 */
7080
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007081static char_u *replace_stack = NULL;
7082static long replace_stack_nr = 0; /* next entry in replace stack */
7083static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007084
7085 void
7086replace_push(c)
7087 int c; /* character that is replaced (NUL is none) */
7088{
7089 char_u *p;
7090
7091 if (replace_stack_nr < replace_offset) /* nothing to do */
7092 return;
7093 if (replace_stack_len <= replace_stack_nr)
7094 {
7095 replace_stack_len += 50;
7096 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7097 if (p == NULL) /* out of memory */
7098 {
7099 replace_stack_len -= 50;
7100 return;
7101 }
7102 if (replace_stack != NULL)
7103 {
7104 mch_memmove(p, replace_stack,
7105 (size_t)(replace_stack_nr * sizeof(char_u)));
7106 vim_free(replace_stack);
7107 }
7108 replace_stack = p;
7109 }
7110 p = replace_stack + replace_stack_nr - replace_offset;
7111 if (replace_offset)
7112 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7113 *p = c;
7114 ++replace_stack_nr;
7115}
7116
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007117#if defined(FEAT_MBYTE) || defined(PROTO)
7118/*
7119 * Push a character onto the replace stack. Handles a multi-byte character in
7120 * reverse byte order, so that the first byte is popped off first.
7121 * Return the number of bytes done (includes composing characters).
7122 */
7123 int
7124replace_push_mb(p)
7125 char_u *p;
7126{
7127 int l = (*mb_ptr2len)(p);
7128 int j;
7129
7130 for (j = l - 1; j >= 0; --j)
7131 replace_push(p[j]);
7132 return l;
7133}
7134#endif
7135
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007136#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007137/*
7138 * call replace_push(c) with replace_offset set to the first NUL.
7139 */
7140 static void
7141replace_push_off(c)
7142 int c;
7143{
7144 char_u *p;
7145
7146 p = replace_stack + replace_stack_nr;
7147 for (replace_offset = 1; replace_offset < replace_stack_nr;
7148 ++replace_offset)
7149 if (*--p == NUL)
7150 break;
7151 replace_push(c);
7152 replace_offset = 0;
7153}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007154#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007155
7156/*
7157 * Pop one item from the replace stack.
7158 * return -1 if stack empty
7159 * return replaced character or NUL otherwise
7160 */
7161 static int
7162replace_pop()
7163{
7164 if (replace_stack_nr == 0)
7165 return -1;
7166 return (int)replace_stack[--replace_stack_nr];
7167}
7168
7169/*
7170 * Join the top two items on the replace stack. This removes to "off"'th NUL
7171 * encountered.
7172 */
7173 static void
7174replace_join(off)
7175 int off; /* offset for which NUL to remove */
7176{
7177 int i;
7178
7179 for (i = replace_stack_nr; --i >= 0; )
7180 if (replace_stack[i] == NUL && off-- <= 0)
7181 {
7182 --replace_stack_nr;
7183 mch_memmove(replace_stack + i, replace_stack + i + 1,
7184 (size_t)(replace_stack_nr - i));
7185 return;
7186 }
7187}
7188
7189/*
7190 * Pop bytes from the replace stack until a NUL is found, and insert them
7191 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7192 */
7193 static void
7194replace_pop_ins()
7195{
7196 int cc;
7197 int oldState = State;
7198
7199 State = NORMAL; /* don't want REPLACE here */
7200 while ((cc = replace_pop()) > 0)
7201 {
7202#ifdef FEAT_MBYTE
7203 mb_replace_pop_ins(cc);
7204#else
7205 ins_char(cc);
7206#endif
7207 dec_cursor();
7208 }
7209 State = oldState;
7210}
7211
7212#ifdef FEAT_MBYTE
7213/*
7214 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7215 * indicates a multi-byte char, pop the other bytes too.
7216 */
7217 static void
7218mb_replace_pop_ins(cc)
7219 int cc;
7220{
7221 int n;
7222 char_u buf[MB_MAXBYTES];
7223 int i;
7224 int c;
7225
7226 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7227 {
7228 buf[0] = cc;
7229 for (i = 1; i < n; ++i)
7230 buf[i] = replace_pop();
7231 ins_bytes_len(buf, n);
7232 }
7233 else
7234 ins_char(cc);
7235
7236 if (enc_utf8)
7237 /* Handle composing chars. */
7238 for (;;)
7239 {
7240 c = replace_pop();
7241 if (c == -1) /* stack empty */
7242 break;
7243 if ((n = MB_BYTE2LEN(c)) == 1)
7244 {
7245 /* Not a multi-byte char, put it back. */
7246 replace_push(c);
7247 break;
7248 }
7249 else
7250 {
7251 buf[0] = c;
7252 for (i = 1; i < n; ++i)
7253 buf[i] = replace_pop();
7254 if (utf_iscomposing(utf_ptr2char(buf)))
7255 ins_bytes_len(buf, n);
7256 else
7257 {
7258 /* Not a composing char, put it back. */
7259 for (i = n - 1; i >= 0; --i)
7260 replace_push(buf[i]);
7261 break;
7262 }
7263 }
7264 }
7265}
7266#endif
7267
7268/*
7269 * make the replace stack empty
7270 * (called when exiting replace mode)
7271 */
7272 static void
7273replace_flush()
7274{
7275 vim_free(replace_stack);
7276 replace_stack = NULL;
7277 replace_stack_len = 0;
7278 replace_stack_nr = 0;
7279}
7280
7281/*
7282 * Handle doing a BS for one character.
7283 * cc < 0: replace stack empty, just move cursor
7284 * cc == 0: character was inserted, delete it
7285 * cc > 0: character was replaced, put cc (first byte of original char) back
7286 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007287 * When "limit_col" is >= 0, don't delete before this column. Matters when
7288 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007289 */
7290 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007291replace_do_bs(limit_col)
7292 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007293{
7294 int cc;
7295#ifdef FEAT_VREPLACE
7296 int orig_len = 0;
7297 int ins_len;
7298 int orig_vcols = 0;
7299 colnr_T start_vcol;
7300 char_u *p;
7301 int i;
7302 int vcol;
7303#endif
7304
7305 cc = replace_pop();
7306 if (cc > 0)
7307 {
7308#ifdef FEAT_VREPLACE
7309 if (State & VREPLACE_FLAG)
7310 {
7311 /* Get the number of screen cells used by the character we are
7312 * going to delete. */
7313 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7314 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7315 }
7316#endif
7317#ifdef FEAT_MBYTE
7318 if (has_mbyte)
7319 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007320 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007321# ifdef FEAT_VREPLACE
7322 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007323 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007324# endif
7325 replace_push(cc);
7326 }
7327 else
7328#endif
7329 {
7330 pchar_cursor(cc);
7331#ifdef FEAT_VREPLACE
7332 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007333 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007334#endif
7335 }
7336 replace_pop_ins();
7337
7338#ifdef FEAT_VREPLACE
7339 if (State & VREPLACE_FLAG)
7340 {
7341 /* Get the number of screen cells used by the inserted characters */
7342 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007343 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007344 vcol = start_vcol;
7345 for (i = 0; i < ins_len; ++i)
7346 {
7347 vcol += chartabsize(p + i, vcol);
7348#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007349 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007350#endif
7351 }
7352 vcol -= start_vcol;
7353
7354 /* Delete spaces that were inserted after the cursor to keep the
7355 * text aligned. */
7356 curwin->w_cursor.col += ins_len;
7357 while (vcol > orig_vcols && gchar_cursor() == ' ')
7358 {
7359 del_char(FALSE);
7360 ++orig_vcols;
7361 }
7362 curwin->w_cursor.col -= ins_len;
7363 }
7364#endif
7365
7366 /* mark the buffer as changed and prepare for displaying */
7367 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7368 }
7369 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007370 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007371}
7372
7373#ifdef FEAT_CINDENT
7374/*
7375 * Return TRUE if C-indenting is on.
7376 */
7377 static int
7378cindent_on()
7379{
7380 return (!p_paste && (curbuf->b_p_cin
7381# ifdef FEAT_EVAL
7382 || *curbuf->b_p_inde != NUL
7383# endif
7384 ));
7385}
7386#endif
7387
7388#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7389/*
7390 * Re-indent the current line, based on the current contents of it and the
7391 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7392 * confused what all the part that handles Control-T is doing that I'm not.
7393 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7394 */
7395
7396 void
7397fixthisline(get_the_indent)
7398 int (*get_the_indent) __ARGS((void));
7399{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007400 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007401 if (linewhite(curwin->w_cursor.lnum))
7402 did_ai = TRUE; /* delete the indent if the line stays empty */
7403}
7404
7405 void
7406fix_indent()
7407{
7408 if (p_paste)
7409 return;
7410# ifdef FEAT_LISP
7411 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7412 fixthisline(get_lisp_indent);
7413# endif
7414# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7415 else
7416# endif
7417# ifdef FEAT_CINDENT
7418 if (cindent_on())
7419 do_c_expr_indent();
7420# endif
7421}
7422
7423#endif
7424
7425#ifdef FEAT_CINDENT
7426/*
7427 * return TRUE if 'cinkeys' contains the key "keytyped",
7428 * when == '*': Only if key is preceded with '*' (indent before insert)
7429 * when == '!': Only if key is prededed with '!' (don't insert)
7430 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7431 *
7432 * "keytyped" can have a few special values:
7433 * KEY_OPEN_FORW
7434 * KEY_OPEN_BACK
7435 * KEY_COMPLETE just finished completion.
7436 *
7437 * If line_is_empty is TRUE accept keys with '0' before them.
7438 */
7439 int
7440in_cinkeys(keytyped, when, line_is_empty)
7441 int keytyped;
7442 int when;
7443 int line_is_empty;
7444{
7445 char_u *look;
7446 int try_match;
7447 int try_match_word;
7448 char_u *p;
7449 char_u *line;
7450 int icase;
7451 int i;
7452
Bram Moolenaar30848942009-12-24 14:46:12 +00007453 if (keytyped == NUL)
7454 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7455 return FALSE;
7456
Bram Moolenaar071d4272004-06-13 20:20:40 +00007457#ifdef FEAT_EVAL
7458 if (*curbuf->b_p_inde != NUL)
7459 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7460 else
7461#endif
7462 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7463 while (*look)
7464 {
7465 /*
7466 * Find out if we want to try a match with this key, depending on
7467 * 'when' and a '*' or '!' before the key.
7468 */
7469 switch (when)
7470 {
7471 case '*': try_match = (*look == '*'); break;
7472 case '!': try_match = (*look == '!'); break;
7473 default: try_match = (*look != '*'); break;
7474 }
7475 if (*look == '*' || *look == '!')
7476 ++look;
7477
7478 /*
7479 * If there is a '0', only accept a match if the line is empty.
7480 * But may still match when typing last char of a word.
7481 */
7482 if (*look == '0')
7483 {
7484 try_match_word = try_match;
7485 if (!line_is_empty)
7486 try_match = FALSE;
7487 ++look;
7488 }
7489 else
7490 try_match_word = FALSE;
7491
7492 /*
7493 * does it look like a control character?
7494 */
7495 if (*look == '^'
7496#ifdef EBCDIC
7497 && (Ctrl_chr(look[1]) != 0)
7498#else
7499 && look[1] >= '?' && look[1] <= '_'
7500#endif
7501 )
7502 {
7503 if (try_match && keytyped == Ctrl_chr(look[1]))
7504 return TRUE;
7505 look += 2;
7506 }
7507 /*
7508 * 'o' means "o" command, open forward.
7509 * 'O' means "O" command, open backward.
7510 */
7511 else if (*look == 'o')
7512 {
7513 if (try_match && keytyped == KEY_OPEN_FORW)
7514 return TRUE;
7515 ++look;
7516 }
7517 else if (*look == 'O')
7518 {
7519 if (try_match && keytyped == KEY_OPEN_BACK)
7520 return TRUE;
7521 ++look;
7522 }
7523
7524 /*
7525 * 'e' means to check for "else" at start of line and just before the
7526 * cursor.
7527 */
7528 else if (*look == 'e')
7529 {
7530 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7531 {
7532 p = ml_get_curline();
7533 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7534 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7535 return TRUE;
7536 }
7537 ++look;
7538 }
7539
7540 /*
7541 * ':' only causes an indent if it is at the end of a label or case
7542 * statement, or when it was before typing the ':' (to fix
7543 * class::method for C++).
7544 */
7545 else if (*look == ':')
7546 {
7547 if (try_match && keytyped == ':')
7548 {
7549 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007550 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7551 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007552 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007553 /* Need to get the line again after cin_islabel(). */
7554 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007555 if (curwin->w_cursor.col > 2
7556 && p[curwin->w_cursor.col - 1] == ':'
7557 && p[curwin->w_cursor.col - 2] == ':')
7558 {
7559 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007560 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007561 || cin_islabel(30));
7562 p = ml_get_curline();
7563 p[curwin->w_cursor.col - 1] = ':';
7564 if (i)
7565 return TRUE;
7566 }
7567 }
7568 ++look;
7569 }
7570
7571
7572 /*
7573 * Is it a key in <>, maybe?
7574 */
7575 else if (*look == '<')
7576 {
7577 if (try_match)
7578 {
7579 /*
7580 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7581 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7582 * >, *, : and ! keys if they really really want to.
7583 */
7584 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7585 && keytyped == look[1])
7586 return TRUE;
7587
7588 if (keytyped == get_special_key_code(look + 1))
7589 return TRUE;
7590 }
7591 while (*look && *look != '>')
7592 look++;
7593 while (*look == '>')
7594 look++;
7595 }
7596
7597 /*
7598 * Is it a word: "=word"?
7599 */
7600 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7601 {
7602 ++look;
7603 if (*look == '~')
7604 {
7605 icase = TRUE;
7606 ++look;
7607 }
7608 else
7609 icase = FALSE;
7610 p = vim_strchr(look, ',');
7611 if (p == NULL)
7612 p = look + STRLEN(look);
7613 if ((try_match || try_match_word)
7614 && curwin->w_cursor.col >= (colnr_T)(p - look))
7615 {
7616 int match = FALSE;
7617
7618#ifdef FEAT_INS_EXPAND
7619 if (keytyped == KEY_COMPLETE)
7620 {
7621 char_u *s;
7622
7623 /* Just completed a word, check if it starts with "look".
7624 * search back for the start of a word. */
7625 line = ml_get_curline();
7626# ifdef FEAT_MBYTE
7627 if (has_mbyte)
7628 {
7629 char_u *n;
7630
7631 for (s = line + curwin->w_cursor.col; s > line; s = n)
7632 {
7633 n = mb_prevptr(line, s);
7634 if (!vim_iswordp(n))
7635 break;
7636 }
7637 }
7638 else
7639# endif
7640 for (s = line + curwin->w_cursor.col; s > line; --s)
7641 if (!vim_iswordc(s[-1]))
7642 break;
7643 if (s + (p - look) <= line + curwin->w_cursor.col
7644 && (icase
7645 ? MB_STRNICMP(s, look, p - look)
7646 : STRNCMP(s, look, p - look)) == 0)
7647 match = TRUE;
7648 }
7649 else
7650#endif
7651 /* TODO: multi-byte */
7652 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7653 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7654 {
7655 line = ml_get_cursor();
7656 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7657 || !vim_iswordc(line[-(p - look) - 1]))
7658 && (icase
7659 ? MB_STRNICMP(line - (p - look), look, p - look)
7660 : STRNCMP(line - (p - look), look, p - look))
7661 == 0)
7662 match = TRUE;
7663 }
7664 if (match && try_match_word && !try_match)
7665 {
7666 /* "0=word": Check if there are only blanks before the
7667 * word. */
7668 line = ml_get_curline();
7669 if ((int)(skipwhite(line) - line) !=
7670 (int)(curwin->w_cursor.col - (p - look)))
7671 match = FALSE;
7672 }
7673 if (match)
7674 return TRUE;
7675 }
7676 look = p;
7677 }
7678
7679 /*
7680 * ok, it's a boring generic character.
7681 */
7682 else
7683 {
7684 if (try_match && *look == keytyped)
7685 return TRUE;
7686 ++look;
7687 }
7688
7689 /*
7690 * Skip over ", ".
7691 */
7692 look = skip_to_option_part(look);
7693 }
7694 return FALSE;
7695}
7696#endif /* FEAT_CINDENT */
7697
7698#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7699/*
7700 * Map Hebrew keyboard when in hkmap mode.
7701 */
7702 int
7703hkmap(c)
7704 int c;
7705{
7706 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7707 {
7708 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7709 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7710 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7711 static char_u map[26] =
7712 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7713 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7714 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7715 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7716 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7717 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7718 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7719 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7720 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7721
7722 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7723 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7724 /* '-1'='sofit' */
7725 else if (c == 'x')
7726 return 'X';
7727 else if (c == 'q')
7728 return '\''; /* {geresh}={'} */
7729 else if (c == 246)
7730 return ' '; /* \"o --> ' ' for a german keyboard */
7731 else if (c == 228)
7732 return ' '; /* \"a --> ' ' -- / -- */
7733 else if (c == 252)
7734 return ' '; /* \"u --> ' ' -- / -- */
7735#ifdef EBCDIC
7736 else if (islower(c))
7737#else
7738 /* NOTE: islower() does not do the right thing for us on Linux so we
7739 * do this the same was as 5.7 and previous, so it works correctly on
7740 * all systems. Specifically, the e.g. Delete and Arrow keys are
7741 * munged and won't work if e.g. searching for Hebrew text.
7742 */
7743 else if (c >= 'a' && c <= 'z')
7744#endif
7745 return (int)(map[CharOrdLow(c)] + p_aleph);
7746 else
7747 return c;
7748 }
7749 else
7750 {
7751 switch (c)
7752 {
7753 case '`': return ';';
7754 case '/': return '.';
7755 case '\'': return ',';
7756 case 'q': return '/';
7757 case 'w': return '\'';
7758
7759 /* Hebrew letters - set offset from 'a' */
7760 case ',': c = '{'; break;
7761 case '.': c = 'v'; break;
7762 case ';': c = 't'; break;
7763 default: {
7764 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7765
7766#ifdef EBCDIC
7767 /* see note about islower() above */
7768 if (!islower(c))
7769#else
7770 if (c < 'a' || c > 'z')
7771#endif
7772 return c;
7773 c = str[CharOrdLow(c)];
7774 break;
7775 }
7776 }
7777
7778 return (int)(CharOrdLow(c) + p_aleph);
7779 }
7780}
7781#endif
7782
7783 static void
7784ins_reg()
7785{
7786 int need_redraw = FALSE;
7787 int regname;
7788 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007789#ifdef FEAT_VISUAL
7790 int vis_active = VIsual_active;
7791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007792
7793 /*
7794 * If we are going to wait for a character, show a '"'.
7795 */
7796 pc_status = PC_STATUS_UNSET;
7797 if (redrawing() && !char_avail())
7798 {
7799 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007800 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007801
7802 edit_putchar('"', TRUE);
7803#ifdef FEAT_CMDL_INFO
7804 add_to_showcmd_c(Ctrl_R);
7805#endif
7806 }
7807
7808#ifdef USE_ON_FLY_SCROLL
7809 dont_scroll = TRUE; /* disallow scrolling here */
7810#endif
7811
7812 /*
7813 * Don't map the register name. This also prevents the mode message to be
7814 * deleted when ESC is hit.
7815 */
7816 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007817 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007818 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007819 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7820 {
7821 /* Get a third key for literal register insertion */
7822 literally = regname;
7823#ifdef FEAT_CMDL_INFO
7824 add_to_showcmd_c(literally);
7825#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007826 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007827 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007828 }
7829 --no_mapping;
7830
7831#ifdef FEAT_EVAL
7832 /*
7833 * Don't call u_sync() while getting the expression,
7834 * evaluating it or giving an error message for it!
7835 */
7836 ++no_u_sync;
7837 if (regname == '=')
7838 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007839# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007841# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007842 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007843# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007844 /* Restore the Input Method. */
7845 if (im_on)
7846 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007847# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007848 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007849 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7850 {
7851 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007852 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007854 else
7855 {
7856#endif
7857 if (literally == Ctrl_O || literally == Ctrl_P)
7858 {
7859 /* Append the command to the redo buffer. */
7860 AppendCharToRedobuff(Ctrl_R);
7861 AppendCharToRedobuff(literally);
7862 AppendCharToRedobuff(regname);
7863
7864 do_put(regname, BACKWARD, 1L,
7865 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7866 }
7867 else if (insert_reg(regname, literally) == FAIL)
7868 {
7869 vim_beep();
7870 need_redraw = TRUE; /* remove the '"' */
7871 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007872 else if (stop_insert_mode)
7873 /* When the '=' register was used and a function was invoked that
7874 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7875 * insert anything, need to remove the '"' */
7876 need_redraw = TRUE;
7877
Bram Moolenaar071d4272004-06-13 20:20:40 +00007878#ifdef FEAT_EVAL
7879 }
7880 --no_u_sync;
7881#endif
7882#ifdef FEAT_CMDL_INFO
7883 clear_showcmd();
7884#endif
7885
7886 /* If the inserted register is empty, we need to remove the '"' */
7887 if (need_redraw || stuff_empty())
7888 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007889
7890#ifdef FEAT_VISUAL
7891 /* Disallow starting Visual mode here, would get a weird mode. */
7892 if (!vis_active && VIsual_active)
7893 end_visual_mode();
7894#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007895}
7896
7897/*
7898 * CTRL-G commands in Insert mode.
7899 */
7900 static void
7901ins_ctrl_g()
7902{
7903 int c;
7904
7905#ifdef FEAT_INS_EXPAND
7906 /* Right after CTRL-X the cursor will be after the ruler. */
7907 setcursor();
7908#endif
7909
7910 /*
7911 * Don't map the second key. This also prevents the mode message to be
7912 * deleted when ESC is hit.
7913 */
7914 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007915 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007916 --no_mapping;
7917 switch (c)
7918 {
7919 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7920 case K_UP:
7921 case Ctrl_K:
7922 case 'k': ins_up(TRUE);
7923 break;
7924
7925 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7926 case K_DOWN:
7927 case Ctrl_J:
7928 case 'j': ins_down(TRUE);
7929 break;
7930
7931 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007932 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007933 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007934
7935 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007936 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007937 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 break;
7939
7940 /* Unknown CTRL-G command, reserved for future expansion. */
7941 default: vim_beep();
7942 }
7943}
7944
7945/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007946 * CTRL-^ in Insert mode.
7947 */
7948 static void
7949ins_ctrl_hat()
7950{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007951 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007952 {
7953 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7954 if (State & LANGMAP)
7955 {
7956 curbuf->b_p_iminsert = B_IMODE_NONE;
7957 State &= ~LANGMAP;
7958 }
7959 else
7960 {
7961 curbuf->b_p_iminsert = B_IMODE_LMAP;
7962 State |= LANGMAP;
7963#ifdef USE_IM_CONTROL
7964 im_set_active(FALSE);
7965#endif
7966 }
7967 }
7968#ifdef USE_IM_CONTROL
7969 else
7970 {
7971 /* There are no ":lmap" mappings, toggle IM */
7972 if (im_get_status())
7973 {
7974 curbuf->b_p_iminsert = B_IMODE_NONE;
7975 im_set_active(FALSE);
7976 }
7977 else
7978 {
7979 curbuf->b_p_iminsert = B_IMODE_IM;
7980 State &= ~LANGMAP;
7981 im_set_active(TRUE);
7982 }
7983 }
7984#endif
7985 set_iminsert_global();
7986 showmode();
7987#ifdef FEAT_GUI
7988 /* may show different cursor shape or color */
7989 if (gui.in_use)
7990 gui_update_cursor(TRUE, FALSE);
7991#endif
7992#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
7993 /* Show/unshow value of 'keymap' in status lines. */
7994 status_redraw_curbuf();
7995#endif
7996}
7997
7998/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00007999 * Handle ESC in insert mode.
8000 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8001 * insert.
8002 */
8003 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008004ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008005 long *count;
8006 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008007 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008008{
8009 int temp;
8010 static int disabled_redraw = FALSE;
8011
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008012#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008013 check_spell_redraw();
8014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008015#if defined(FEAT_HANGULIN)
8016# if defined(ESC_CHG_TO_ENG_MODE)
8017 hangul_input_state_set(0);
8018# endif
8019 if (composing_hangul)
8020 {
8021 push_raw_key(composing_hangul_buffer, 2);
8022 composing_hangul = 0;
8023 }
8024#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008025
8026 temp = curwin->w_cursor.col;
8027 if (disabled_redraw)
8028 {
8029 --RedrawingDisabled;
8030 disabled_redraw = FALSE;
8031 }
8032 if (!arrow_used)
8033 {
8034 /*
8035 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008036 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8037 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008038 */
8039 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008040 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008041
8042 /*
8043 * Repeating insert may take a long time. Check for
8044 * interrupt now and then.
8045 */
8046 if (*count > 0)
8047 {
8048 line_breakcheck();
8049 if (got_int)
8050 *count = 0;
8051 }
8052
8053 if (--*count > 0) /* repeat what was typed */
8054 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008055 /* Vi repeats the insert without replacing characters. */
8056 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8057 State &= ~REPLACE_FLAG;
8058
Bram Moolenaar071d4272004-06-13 20:20:40 +00008059 (void)start_redo_ins();
8060 if (cmdchar == 'r' || cmdchar == 'v')
8061 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8062 ++RedrawingDisabled;
8063 disabled_redraw = TRUE;
8064 return FALSE; /* repeat the insert */
8065 }
8066 stop_insert(&curwin->w_cursor, TRUE);
8067 undisplay_dollar();
8068 }
8069
8070 /* When an autoindent was removed, curswant stays after the
8071 * indent */
8072 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8073 curwin->w_set_curswant = TRUE;
8074
8075 /* Remember the last Insert position in the '^ mark. */
8076 if (!cmdmod.keepjumps)
8077 curbuf->b_last_insert = curwin->w_cursor;
8078
8079 /*
8080 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008081 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008083 if (!nomove
8084 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008085#ifdef FEAT_VIRTUALEDIT
8086 || curwin->w_cursor.coladd > 0
8087#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008088 )
8089 && (restart_edit == NUL
8090 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008092 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008094 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095#ifdef FEAT_RIGHTLEFT
8096 && !revins_on
8097#endif
8098 )
8099 {
8100#ifdef FEAT_VIRTUALEDIT
8101 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8102 {
8103 oneleft();
8104 if (restart_edit != NUL)
8105 ++curwin->w_cursor.coladd;
8106 }
8107 else
8108#endif
8109 {
8110 --curwin->w_cursor.col;
8111#ifdef FEAT_MBYTE
8112 /* Correct cursor for multi-byte character. */
8113 if (has_mbyte)
8114 mb_adjust_cursor();
8115#endif
8116 }
8117 }
8118
8119#ifdef USE_IM_CONTROL
8120 /* Disable IM to allow typing English directly for Normal mode commands.
8121 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8122 * well). */
8123 if (!(State & LANGMAP))
8124 im_save_status(&curbuf->b_p_iminsert);
8125 im_set_active(FALSE);
8126#endif
8127
8128 State = NORMAL;
8129 /* need to position cursor again (e.g. when on a TAB ) */
8130 changed_cline_bef_curs();
8131
8132#ifdef FEAT_MOUSE
8133 setmouse();
8134#endif
8135#ifdef CURSOR_SHAPE
8136 ui_cursor_shape(); /* may show different cursor shape */
8137#endif
8138
8139 /*
8140 * When recording or for CTRL-O, need to display the new mode.
8141 * Otherwise remove the mode message.
8142 */
8143 if (Recording || restart_edit != NUL)
8144 showmode();
8145 else if (p_smd)
8146 MSG("");
8147
8148 return TRUE; /* exit Insert mode */
8149}
8150
8151#ifdef FEAT_RIGHTLEFT
8152/*
8153 * Toggle language: hkmap and revins_on.
8154 * Move to end of reverse inserted text.
8155 */
8156 static void
8157ins_ctrl_()
8158{
8159 if (revins_on && revins_chars && revins_scol >= 0)
8160 {
8161 while (gchar_cursor() != NUL && revins_chars--)
8162 ++curwin->w_cursor.col;
8163 }
8164 p_ri = !p_ri;
8165 revins_on = (State == INSERT && p_ri);
8166 if (revins_on)
8167 {
8168 revins_scol = curwin->w_cursor.col;
8169 revins_legal++;
8170 revins_chars = 0;
8171 undisplay_dollar();
8172 }
8173 else
8174 revins_scol = -1;
8175#ifdef FEAT_FKMAP
8176 if (p_altkeymap)
8177 {
8178 /*
8179 * to be consistent also for redo command, using '.'
8180 * set arrow_used to true and stop it - causing to redo
8181 * characters entered in one mode (normal/reverse insert).
8182 */
8183 arrow_used = TRUE;
8184 (void)stop_arrow();
8185 p_fkmap = curwin->w_p_rl ^ p_ri;
8186 if (p_fkmap && p_ri)
8187 State = INSERT;
8188 }
8189 else
8190#endif
8191 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8192 showmode();
8193}
8194#endif
8195
8196#ifdef FEAT_VISUAL
8197/*
8198 * If 'keymodel' contains "startsel", may start selection.
8199 * Returns TRUE when a CTRL-O and other keys stuffed.
8200 */
8201 static int
8202ins_start_select(c)
8203 int c;
8204{
8205 if (km_startsel)
8206 switch (c)
8207 {
8208 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008209 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 case K_PAGEUP:
8211 case K_KPAGEUP:
8212 case K_PAGEDOWN:
8213 case K_KPAGEDOWN:
8214# ifdef MACOS
8215 case K_LEFT:
8216 case K_RIGHT:
8217 case K_UP:
8218 case K_DOWN:
8219 case K_END:
8220 case K_HOME:
8221# endif
8222 if (!(mod_mask & MOD_MASK_SHIFT))
8223 break;
8224 /* FALLTHROUGH */
8225 case K_S_LEFT:
8226 case K_S_RIGHT:
8227 case K_S_UP:
8228 case K_S_DOWN:
8229 case K_S_END:
8230 case K_S_HOME:
8231 /* Start selection right away, the cursor can move with
8232 * CTRL-O when beyond the end of the line. */
8233 start_selection();
8234
8235 /* Execute the key in (insert) Select mode. */
8236 stuffcharReadbuff(Ctrl_O);
8237 if (mod_mask)
8238 {
8239 char_u buf[4];
8240
8241 buf[0] = K_SPECIAL;
8242 buf[1] = KS_MODIFIER;
8243 buf[2] = mod_mask;
8244 buf[3] = NUL;
8245 stuffReadbuff(buf);
8246 }
8247 stuffcharReadbuff(c);
8248 return TRUE;
8249 }
8250 return FALSE;
8251}
8252#endif
8253
8254/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008255 * <Insert> key in Insert mode: toggle insert/remplace mode.
8256 */
8257 static void
8258ins_insert(replaceState)
8259 int replaceState;
8260{
8261#ifdef FEAT_FKMAP
8262 if (p_fkmap && p_ri)
8263 {
8264 beep_flush();
8265 EMSG(farsi_text_3); /* encoded in Farsi */
8266 return;
8267 }
8268#endif
8269
8270#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008271# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008272 set_vim_var_string(VV_INSERTMODE,
8273 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008274# ifdef FEAT_VREPLACE
8275 replaceState == VREPLACE ? "v" :
8276# endif
8277 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008278# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008279 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8280#endif
8281 if (State & REPLACE_FLAG)
8282 State = INSERT | (State & LANGMAP);
8283 else
8284 State = replaceState | (State & LANGMAP);
8285 AppendCharToRedobuff(K_INS);
8286 showmode();
8287#ifdef CURSOR_SHAPE
8288 ui_cursor_shape(); /* may show different cursor shape */
8289#endif
8290}
8291
8292/*
8293 * Pressed CTRL-O in Insert mode.
8294 */
8295 static void
8296ins_ctrl_o()
8297{
8298#ifdef FEAT_VREPLACE
8299 if (State & VREPLACE_FLAG)
8300 restart_edit = 'V';
8301 else
8302#endif
8303 if (State & REPLACE_FLAG)
8304 restart_edit = 'R';
8305 else
8306 restart_edit = 'I';
8307#ifdef FEAT_VIRTUALEDIT
8308 if (virtual_active())
8309 ins_at_eol = FALSE; /* cursor always keeps its column */
8310 else
8311#endif
8312 ins_at_eol = (gchar_cursor() == NUL);
8313}
8314
8315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 * If the cursor is on an indent, ^T/^D insert/delete one
8317 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008318 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008319 * with vi. But vi only supports ^T and ^D after an
8320 * autoindent, we support it everywhere.
8321 */
8322 static void
8323ins_shift(c, lastc)
8324 int c;
8325 int lastc;
8326{
8327 if (stop_arrow() == FAIL)
8328 return;
8329 AppendCharToRedobuff(c);
8330
8331 /*
8332 * 0^D and ^^D: remove all indent.
8333 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008334 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8335 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {
8337 --curwin->w_cursor.col;
8338 (void)del_char(FALSE); /* delete the '^' or '0' */
8339 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8340 if (State & REPLACE_FLAG)
8341 replace_pop_ins();
8342 if (lastc == '^')
8343 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008344 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008345 }
8346 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008347 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008348
8349 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8350 did_ai = FALSE;
8351#ifdef FEAT_SMARTINDENT
8352 did_si = FALSE;
8353 can_si = FALSE;
8354 can_si_back = FALSE;
8355#endif
8356#ifdef FEAT_CINDENT
8357 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8358#endif
8359}
8360
8361 static void
8362ins_del()
8363{
8364 int temp;
8365
8366 if (stop_arrow() == FAIL)
8367 return;
8368 if (gchar_cursor() == NUL) /* delete newline */
8369 {
8370 temp = curwin->w_cursor.col;
8371 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008372 || do_join(2, FALSE, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008373 vim_beep();
8374 else
8375 curwin->w_cursor.col = temp;
8376 }
8377 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8378 vim_beep();
8379 did_ai = FALSE;
8380#ifdef FEAT_SMARTINDENT
8381 did_si = FALSE;
8382 can_si = FALSE;
8383 can_si_back = FALSE;
8384#endif
8385 AppendCharToRedobuff(K_DEL);
8386}
8387
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008388static void ins_bs_one __ARGS((colnr_T *vcolp));
8389
8390/*
8391 * Delete one character for ins_bs().
8392 */
8393 static void
8394ins_bs_one(vcolp)
8395 colnr_T *vcolp;
8396{
8397 dec_cursor();
8398 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8399 if (State & REPLACE_FLAG)
8400 {
8401 /* Don't delete characters before the insert point when in
8402 * Replace mode */
8403 if (curwin->w_cursor.lnum != Insstart.lnum
8404 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008405 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008406 }
8407 else
8408 (void)del_char(FALSE);
8409}
8410
Bram Moolenaar071d4272004-06-13 20:20:40 +00008411/*
8412 * Handle Backspace, delete-word and delete-line in Insert mode.
8413 * Return TRUE when backspace was actually used.
8414 */
8415 static int
8416ins_bs(c, mode, inserted_space_p)
8417 int c;
8418 int mode;
8419 int *inserted_space_p;
8420{
8421 linenr_T lnum;
8422 int cc;
8423 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008424 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 colnr_T mincol;
8426 int did_backspace = FALSE;
8427 int in_indent;
8428 int oldState;
8429#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008430 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431#endif
8432
8433 /*
8434 * can't delete anything in an empty file
8435 * can't backup past first character in buffer
8436 * can't backup past starting point unless 'backspace' > 1
8437 * can backup to a previous line if 'backspace' == 0
8438 */
8439 if ( bufempty()
8440 || (
8441#ifdef FEAT_RIGHTLEFT
8442 !revins_on &&
8443#endif
8444 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8445 || (!can_bs(BS_START)
8446 && (arrow_used
8447 || (curwin->w_cursor.lnum == Insstart.lnum
8448 && curwin->w_cursor.col <= Insstart.col)))
8449 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8450 && curwin->w_cursor.col <= ai_col)
8451 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8452 {
8453 vim_beep();
8454 return FALSE;
8455 }
8456
8457 if (stop_arrow() == FAIL)
8458 return FALSE;
8459 in_indent = inindent(0);
8460#ifdef FEAT_CINDENT
8461 if (in_indent)
8462 can_cindent = FALSE;
8463#endif
8464#ifdef FEAT_COMMENTS
8465 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8466#endif
8467#ifdef FEAT_RIGHTLEFT
8468 if (revins_on) /* put cursor after last inserted char */
8469 inc_cursor();
8470#endif
8471
8472#ifdef FEAT_VIRTUALEDIT
8473 /* Virtualedit:
8474 * BACKSPACE_CHAR eats a virtual space
8475 * BACKSPACE_WORD eats all coladd
8476 * BACKSPACE_LINE eats all coladd and keeps going
8477 */
8478 if (curwin->w_cursor.coladd > 0)
8479 {
8480 if (mode == BACKSPACE_CHAR)
8481 {
8482 --curwin->w_cursor.coladd;
8483 return TRUE;
8484 }
8485 if (mode == BACKSPACE_WORD)
8486 {
8487 curwin->w_cursor.coladd = 0;
8488 return TRUE;
8489 }
8490 curwin->w_cursor.coladd = 0;
8491 }
8492#endif
8493
8494 /*
8495 * delete newline!
8496 */
8497 if (curwin->w_cursor.col == 0)
8498 {
8499 lnum = Insstart.lnum;
8500 if (curwin->w_cursor.lnum == Insstart.lnum
8501#ifdef FEAT_RIGHTLEFT
8502 || revins_on
8503#endif
8504 )
8505 {
8506 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8507 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8508 return FALSE;
8509 --Insstart.lnum;
8510 Insstart.col = MAXCOL;
8511 }
8512 /*
8513 * In replace mode:
8514 * cc < 0: NL was inserted, delete it
8515 * cc >= 0: NL was replaced, put original characters back
8516 */
8517 cc = -1;
8518 if (State & REPLACE_FLAG)
8519 cc = replace_pop(); /* returns -1 if NL was inserted */
8520 /*
8521 * In replace mode, in the line we started replacing, we only move the
8522 * cursor.
8523 */
8524 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8525 {
8526 dec_cursor();
8527 }
8528 else
8529 {
8530#ifdef FEAT_VREPLACE
8531 if (!(State & VREPLACE_FLAG)
8532 || curwin->w_cursor.lnum > orig_line_count)
8533#endif
8534 {
8535 temp = gchar_cursor(); /* remember current char */
8536 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008537
8538 /* When "aw" is in 'formatoptions' we must delete the space at
8539 * the end of the line, otherwise the line will be broken
8540 * again when auto-formatting. */
8541 if (has_format_option(FO_AUTO)
8542 && has_format_option(FO_WHITE_PAR))
8543 {
8544 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8545 TRUE);
8546 int len;
8547
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008548 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008549 if (len > 0 && ptr[len - 1] == ' ')
8550 ptr[len - 1] = NUL;
8551 }
8552
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008553 (void)do_join(2, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008554 if (temp == NUL && gchar_cursor() != NUL)
8555 inc_cursor();
8556 }
8557#ifdef FEAT_VREPLACE
8558 else
8559 dec_cursor();
8560#endif
8561
8562 /*
8563 * In REPLACE mode we have to put back the text that was replaced
8564 * by the NL. On the replace stack is first a NUL-terminated
8565 * sequence of characters that were deleted and then the
8566 * characters that NL replaced.
8567 */
8568 if (State & REPLACE_FLAG)
8569 {
8570 /*
8571 * Do the next ins_char() in NORMAL state, to
8572 * prevent ins_char() from replacing characters and
8573 * avoiding showmatch().
8574 */
8575 oldState = State;
8576 State = NORMAL;
8577 /*
8578 * restore characters (blanks) deleted after cursor
8579 */
8580 while (cc > 0)
8581 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008582 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008583#ifdef FEAT_MBYTE
8584 mb_replace_pop_ins(cc);
8585#else
8586 ins_char(cc);
8587#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008588 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008589 cc = replace_pop();
8590 }
8591 /* restore the characters that NL replaced */
8592 replace_pop_ins();
8593 State = oldState;
8594 }
8595 }
8596 did_ai = FALSE;
8597 }
8598 else
8599 {
8600 /*
8601 * Delete character(s) before the cursor.
8602 */
8603#ifdef FEAT_RIGHTLEFT
8604 if (revins_on) /* put cursor on last inserted char */
8605 dec_cursor();
8606#endif
8607 mincol = 0;
8608 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008609 if (mode == BACKSPACE_LINE
8610 && (curbuf->b_p_ai
8611#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008612 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008613#endif
8614 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008615#ifdef FEAT_RIGHTLEFT
8616 && !revins_on
8617#endif
8618 )
8619 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008620 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008621 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008622 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008624 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625 }
8626
8627 /*
8628 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8629 */
8630 if ( mode == BACKSPACE_CHAR
8631 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008632 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008633 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634 && (*(ml_get_cursor() - 1) == TAB
8635 || (*(ml_get_cursor() - 1) == ' '
8636 && (!*inserted_space_p
8637 || arrow_used))))))
8638 {
8639 int ts;
8640 colnr_T vcol;
8641 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008642 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643
8644 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008645 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008646 ts = curbuf->b_p_sw;
8647 else
8648 ts = curbuf->b_p_sts;
8649 /* Compute the virtual column where we want to be. Since
8650 * 'showbreak' may get in the way, need to get the last column of
8651 * the previous character. */
8652 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008653 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008654 dec_cursor();
8655 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8656 inc_cursor();
8657 want_vcol = (want_vcol / ts) * ts;
8658
8659 /* delete characters until we are at or before want_vcol */
8660 while (vcol > want_vcol
8661 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008662 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008663
8664 /* insert extra spaces until we are at want_vcol */
8665 while (vcol < want_vcol)
8666 {
8667 /* Remember the first char we inserted */
8668 if (curwin->w_cursor.lnum == Insstart.lnum
8669 && curwin->w_cursor.col < Insstart.col)
8670 Insstart.col = curwin->w_cursor.col;
8671
8672#ifdef FEAT_VREPLACE
8673 if (State & VREPLACE_FLAG)
8674 ins_char(' ');
8675 else
8676#endif
8677 {
8678 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008679 if ((State & REPLACE_FLAG))
8680 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008681 }
8682 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8683 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008684
8685 /* If we are now back where we started delete one character. Can
8686 * happen when using 'sts' and 'linebreak'. */
8687 if (vcol >= start_vcol)
8688 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 }
8690
8691 /*
8692 * Delete upto starting point, start of line or previous word.
8693 */
8694 else do
8695 {
8696#ifdef FEAT_RIGHTLEFT
8697 if (!revins_on) /* put cursor on char to be deleted */
8698#endif
8699 dec_cursor();
8700
8701 /* start of word? */
8702 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8703 {
8704 mode = BACKSPACE_WORD_NOT_SPACE;
8705 temp = vim_iswordc(gchar_cursor());
8706 }
8707 /* end of word? */
8708 else if (mode == BACKSPACE_WORD_NOT_SPACE
8709 && (vim_isspace(cc = gchar_cursor())
8710 || vim_iswordc(cc) != temp))
8711 {
8712#ifdef FEAT_RIGHTLEFT
8713 if (!revins_on)
8714#endif
8715 inc_cursor();
8716#ifdef FEAT_RIGHTLEFT
8717 else if (State & REPLACE_FLAG)
8718 dec_cursor();
8719#endif
8720 break;
8721 }
8722 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008723 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 else
8725 {
8726#ifdef FEAT_MBYTE
8727 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008728 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008729#endif
8730 (void)del_char(FALSE);
8731#ifdef FEAT_MBYTE
8732 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008733 * If there are combining characters and 'delcombine' is set
8734 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008735 * character.
8736 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008737 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008738 inc_cursor();
8739#endif
8740#ifdef FEAT_RIGHTLEFT
8741 if (revins_chars)
8742 {
8743 revins_chars--;
8744 revins_legal++;
8745 }
8746 if (revins_on && gchar_cursor() == NUL)
8747 break;
8748#endif
8749 }
8750 /* Just a single backspace?: */
8751 if (mode == BACKSPACE_CHAR)
8752 break;
8753 } while (
8754#ifdef FEAT_RIGHTLEFT
8755 revins_on ||
8756#endif
8757 (curwin->w_cursor.col > mincol
8758 && (curwin->w_cursor.lnum != Insstart.lnum
8759 || curwin->w_cursor.col != Insstart.col)));
8760 did_backspace = TRUE;
8761 }
8762#ifdef FEAT_SMARTINDENT
8763 did_si = FALSE;
8764 can_si = FALSE;
8765 can_si_back = FALSE;
8766#endif
8767 if (curwin->w_cursor.col <= 1)
8768 did_ai = FALSE;
8769 /*
8770 * It's a little strange to put backspaces into the redo
8771 * buffer, but it makes auto-indent a lot easier to deal
8772 * with.
8773 */
8774 AppendCharToRedobuff(c);
8775
8776 /* If deleted before the insertion point, adjust it */
8777 if (curwin->w_cursor.lnum == Insstart.lnum
8778 && curwin->w_cursor.col < Insstart.col)
8779 Insstart.col = curwin->w_cursor.col;
8780
8781 /* vi behaviour: the cursor moves backward but the character that
8782 * was there remains visible
8783 * Vim behaviour: the cursor moves backward and the character that
8784 * was there is erased from the screen.
8785 * We can emulate the vi behaviour by pretending there is a dollar
8786 * displayed even when there isn't.
8787 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8788 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8789 dollar_vcol = curwin->w_virtcol;
8790
Bram Moolenaarce3be472008-01-14 19:12:28 +00008791#ifdef FEAT_FOLDING
8792 /* When deleting a char the cursor line must never be in a closed fold.
8793 * E.g., when 'foldmethod' is indent and deleting the first non-white
8794 * char before a Tab. */
8795 if (did_backspace)
8796 foldOpenCursor();
8797#endif
8798
Bram Moolenaar071d4272004-06-13 20:20:40 +00008799 return did_backspace;
8800}
8801
8802#ifdef FEAT_MOUSE
8803 static void
8804ins_mouse(c)
8805 int c;
8806{
8807 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008808 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008809
8810# ifdef FEAT_GUI
8811 /* When GUI is active, also move/paste when 'mouse' is empty */
8812 if (!gui.in_use)
8813# endif
8814 if (!mouse_has(MOUSE_INSERT))
8815 return;
8816
8817 undisplay_dollar();
8818 tpos = curwin->w_cursor;
8819 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8820 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008821#ifdef FEAT_WINDOWS
8822 win_T *new_curwin = curwin;
8823
8824 if (curwin != old_curwin && win_valid(old_curwin))
8825 {
8826 /* Mouse took us to another window. We need to go back to the
8827 * previous one to stop insert there properly. */
8828 curwin = old_curwin;
8829 curbuf = curwin->w_buffer;
8830 }
8831#endif
8832 start_arrow(curwin == old_curwin ? &tpos : NULL);
8833#ifdef FEAT_WINDOWS
8834 if (curwin != new_curwin && win_valid(new_curwin))
8835 {
8836 curwin = new_curwin;
8837 curbuf = curwin->w_buffer;
8838 }
8839#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840# ifdef FEAT_CINDENT
8841 can_cindent = TRUE;
8842# endif
8843 }
8844
8845#ifdef FEAT_WINDOWS
8846 /* redraw status lines (in case another window became active) */
8847 redraw_statuslines();
8848#endif
8849}
8850
8851 static void
8852ins_mousescroll(up)
8853 int up;
8854{
8855 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008856# if defined(FEAT_WINDOWS)
8857 win_T *old_curwin = curwin;
8858# endif
8859# ifdef FEAT_INS_EXPAND
8860 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861# endif
8862
8863 tpos = curwin->w_cursor;
8864
8865# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008866 /* Currently the mouse coordinates are only known in the GUI. */
8867 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8868 {
8869 int row, col;
8870
8871 row = mouse_row;
8872 col = mouse_col;
8873
8874 /* find the window at the pointer coordinates */
8875 curwin = mouse_find_win(&row, &col);
8876 curbuf = curwin->w_buffer;
8877 }
8878 if (curwin == old_curwin)
8879# endif
8880 undisplay_dollar();
8881
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008882# ifdef FEAT_INS_EXPAND
8883 /* Don't scroll the window in which completion is being done. */
8884 if (!pum_visible()
8885# if defined(FEAT_WINDOWS)
8886 || curwin != old_curwin
8887# endif
8888 )
8889# endif
8890 {
8891 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8892 scroll_redraw(up, (long)(curwin->w_botline - curwin->w_topline));
8893 else
8894 scroll_redraw(up, 3L);
8895# ifdef FEAT_INS_EXPAND
8896 did_scroll = TRUE;
8897# endif
8898 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008899
8900# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8901 curwin->w_redr_status = TRUE;
8902
8903 curwin = old_curwin;
8904 curbuf = curwin->w_buffer;
8905# endif
8906
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008907# ifdef FEAT_INS_EXPAND
8908 /* The popup menu may overlay the window, need to redraw it.
8909 * TODO: Would be more efficient to only redraw the windows that are
8910 * overlapped by the popup menu. */
8911 if (pum_visible() && did_scroll)
8912 {
8913 redraw_all_later(NOT_VALID);
8914 ins_compl_show_pum();
8915 }
8916# endif
8917
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 if (!equalpos(curwin->w_cursor, tpos))
8919 {
8920 start_arrow(&tpos);
8921# ifdef FEAT_CINDENT
8922 can_cindent = TRUE;
8923# endif
8924 }
8925}
8926#endif
8927
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008928#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008929 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008930ins_tabline(c)
8931 int c;
8932{
8933 /* We will be leaving the current window, unless closing another tab. */
8934 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8935 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8936 {
8937 undisplay_dollar();
8938 start_arrow(&curwin->w_cursor);
8939# ifdef FEAT_CINDENT
8940 can_cindent = TRUE;
8941# endif
8942 }
8943
8944 if (c == K_TABLINE)
8945 goto_tabpage(current_tab);
8946 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008947 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008948 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008949 redraw_statuslines(); /* will redraw the tabline when needed */
8950 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008951}
8952#endif
8953
8954#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008955 void
8956ins_scroll()
8957{
8958 pos_T tpos;
8959
8960 undisplay_dollar();
8961 tpos = curwin->w_cursor;
8962 if (gui_do_scroll())
8963 {
8964 start_arrow(&tpos);
8965# ifdef FEAT_CINDENT
8966 can_cindent = TRUE;
8967# endif
8968 }
8969}
8970
8971 void
8972ins_horscroll()
8973{
8974 pos_T tpos;
8975
8976 undisplay_dollar();
8977 tpos = curwin->w_cursor;
8978 if (gui_do_horiz_scroll())
8979 {
8980 start_arrow(&tpos);
8981# ifdef FEAT_CINDENT
8982 can_cindent = TRUE;
8983# endif
8984 }
8985}
8986#endif
8987
8988 static void
8989ins_left()
8990{
8991 pos_T tpos;
8992
8993#ifdef FEAT_FOLDING
8994 if ((fdo_flags & FDO_HOR) && KeyTyped)
8995 foldOpenCursor();
8996#endif
8997 undisplay_dollar();
8998 tpos = curwin->w_cursor;
8999 if (oneleft() == OK)
9000 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009001#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9002 /* Only call start_arrow() when not busy with preediting, it will
9003 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9004 if (!im_is_preediting())
9005#endif
9006 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007#ifdef FEAT_RIGHTLEFT
9008 /* If exit reversed string, position is fixed */
9009 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9010 revins_legal++;
9011 revins_chars++;
9012#endif
9013 }
9014
9015 /*
9016 * if 'whichwrap' set for cursor in insert mode may go to
9017 * previous line
9018 */
9019 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9020 {
9021 start_arrow(&tpos);
9022 --(curwin->w_cursor.lnum);
9023 coladvance((colnr_T)MAXCOL);
9024 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9025 }
9026 else
9027 vim_beep();
9028}
9029
9030 static void
9031ins_home(c)
9032 int c;
9033{
9034 pos_T tpos;
9035
9036#ifdef FEAT_FOLDING
9037 if ((fdo_flags & FDO_HOR) && KeyTyped)
9038 foldOpenCursor();
9039#endif
9040 undisplay_dollar();
9041 tpos = curwin->w_cursor;
9042 if (c == K_C_HOME)
9043 curwin->w_cursor.lnum = 1;
9044 curwin->w_cursor.col = 0;
9045#ifdef FEAT_VIRTUALEDIT
9046 curwin->w_cursor.coladd = 0;
9047#endif
9048 curwin->w_curswant = 0;
9049 start_arrow(&tpos);
9050}
9051
9052 static void
9053ins_end(c)
9054 int c;
9055{
9056 pos_T tpos;
9057
9058#ifdef FEAT_FOLDING
9059 if ((fdo_flags & FDO_HOR) && KeyTyped)
9060 foldOpenCursor();
9061#endif
9062 undisplay_dollar();
9063 tpos = curwin->w_cursor;
9064 if (c == K_C_END)
9065 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9066 coladvance((colnr_T)MAXCOL);
9067 curwin->w_curswant = MAXCOL;
9068
9069 start_arrow(&tpos);
9070}
9071
9072 static void
9073ins_s_left()
9074{
9075#ifdef FEAT_FOLDING
9076 if ((fdo_flags & FDO_HOR) && KeyTyped)
9077 foldOpenCursor();
9078#endif
9079 undisplay_dollar();
9080 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9081 {
9082 start_arrow(&curwin->w_cursor);
9083 (void)bck_word(1L, FALSE, FALSE);
9084 curwin->w_set_curswant = TRUE;
9085 }
9086 else
9087 vim_beep();
9088}
9089
9090 static void
9091ins_right()
9092{
9093#ifdef FEAT_FOLDING
9094 if ((fdo_flags & FDO_HOR) && KeyTyped)
9095 foldOpenCursor();
9096#endif
9097 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009098 if (gchar_cursor() != NUL
9099#ifdef FEAT_VIRTUALEDIT
9100 || virtual_active()
9101#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009102 )
9103 {
9104 start_arrow(&curwin->w_cursor);
9105 curwin->w_set_curswant = TRUE;
9106#ifdef FEAT_VIRTUALEDIT
9107 if (virtual_active())
9108 oneright();
9109 else
9110#endif
9111 {
9112#ifdef FEAT_MBYTE
9113 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009114 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009115 else
9116#endif
9117 ++curwin->w_cursor.col;
9118 }
9119
9120#ifdef FEAT_RIGHTLEFT
9121 revins_legal++;
9122 if (revins_chars)
9123 revins_chars--;
9124#endif
9125 }
9126 /* if 'whichwrap' set for cursor in insert mode, may move the
9127 * cursor to the next line */
9128 else if (vim_strchr(p_ww, ']') != NULL
9129 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9130 {
9131 start_arrow(&curwin->w_cursor);
9132 curwin->w_set_curswant = TRUE;
9133 ++curwin->w_cursor.lnum;
9134 curwin->w_cursor.col = 0;
9135 }
9136 else
9137 vim_beep();
9138}
9139
9140 static void
9141ins_s_right()
9142{
9143#ifdef FEAT_FOLDING
9144 if ((fdo_flags & FDO_HOR) && KeyTyped)
9145 foldOpenCursor();
9146#endif
9147 undisplay_dollar();
9148 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9149 || gchar_cursor() != NUL)
9150 {
9151 start_arrow(&curwin->w_cursor);
9152 (void)fwd_word(1L, FALSE, 0);
9153 curwin->w_set_curswant = TRUE;
9154 }
9155 else
9156 vim_beep();
9157}
9158
9159 static void
9160ins_up(startcol)
9161 int startcol; /* when TRUE move to Insstart.col */
9162{
9163 pos_T tpos;
9164 linenr_T old_topline = curwin->w_topline;
9165#ifdef FEAT_DIFF
9166 int old_topfill = curwin->w_topfill;
9167#endif
9168
9169 undisplay_dollar();
9170 tpos = curwin->w_cursor;
9171 if (cursor_up(1L, TRUE) == OK)
9172 {
9173 if (startcol)
9174 coladvance(getvcol_nolist(&Insstart));
9175 if (old_topline != curwin->w_topline
9176#ifdef FEAT_DIFF
9177 || old_topfill != curwin->w_topfill
9178#endif
9179 )
9180 redraw_later(VALID);
9181 start_arrow(&tpos);
9182#ifdef FEAT_CINDENT
9183 can_cindent = TRUE;
9184#endif
9185 }
9186 else
9187 vim_beep();
9188}
9189
9190 static void
9191ins_pageup()
9192{
9193 pos_T tpos;
9194
9195 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009196
9197#ifdef FEAT_WINDOWS
9198 if (mod_mask & MOD_MASK_CTRL)
9199 {
9200 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009201 if (first_tabpage->tp_next != NULL)
9202 {
9203 start_arrow(&curwin->w_cursor);
9204 goto_tabpage(-1);
9205 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009206 return;
9207 }
9208#endif
9209
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210 tpos = curwin->w_cursor;
9211 if (onepage(BACKWARD, 1L) == OK)
9212 {
9213 start_arrow(&tpos);
9214#ifdef FEAT_CINDENT
9215 can_cindent = TRUE;
9216#endif
9217 }
9218 else
9219 vim_beep();
9220}
9221
9222 static void
9223ins_down(startcol)
9224 int startcol; /* when TRUE move to Insstart.col */
9225{
9226 pos_T tpos;
9227 linenr_T old_topline = curwin->w_topline;
9228#ifdef FEAT_DIFF
9229 int old_topfill = curwin->w_topfill;
9230#endif
9231
9232 undisplay_dollar();
9233 tpos = curwin->w_cursor;
9234 if (cursor_down(1L, TRUE) == OK)
9235 {
9236 if (startcol)
9237 coladvance(getvcol_nolist(&Insstart));
9238 if (old_topline != curwin->w_topline
9239#ifdef FEAT_DIFF
9240 || old_topfill != curwin->w_topfill
9241#endif
9242 )
9243 redraw_later(VALID);
9244 start_arrow(&tpos);
9245#ifdef FEAT_CINDENT
9246 can_cindent = TRUE;
9247#endif
9248 }
9249 else
9250 vim_beep();
9251}
9252
9253 static void
9254ins_pagedown()
9255{
9256 pos_T tpos;
9257
9258 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009259
9260#ifdef FEAT_WINDOWS
9261 if (mod_mask & MOD_MASK_CTRL)
9262 {
9263 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009264 if (first_tabpage->tp_next != NULL)
9265 {
9266 start_arrow(&curwin->w_cursor);
9267 goto_tabpage(0);
9268 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009269 return;
9270 }
9271#endif
9272
Bram Moolenaar071d4272004-06-13 20:20:40 +00009273 tpos = curwin->w_cursor;
9274 if (onepage(FORWARD, 1L) == OK)
9275 {
9276 start_arrow(&tpos);
9277#ifdef FEAT_CINDENT
9278 can_cindent = TRUE;
9279#endif
9280 }
9281 else
9282 vim_beep();
9283}
9284
9285#ifdef FEAT_DND
9286 static void
9287ins_drop()
9288{
9289 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9290}
9291#endif
9292
9293/*
9294 * Handle TAB in Insert or Replace mode.
9295 * Return TRUE when the TAB needs to be inserted like a normal character.
9296 */
9297 static int
9298ins_tab()
9299{
9300 int ind;
9301 int i;
9302 int temp;
9303
9304 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9305 Insstart_blank_vcol = get_nolist_virtcol();
9306 if (echeck_abbr(TAB + ABBR_OFF))
9307 return FALSE;
9308
9309 ind = inindent(0);
9310#ifdef FEAT_CINDENT
9311 if (ind)
9312 can_cindent = FALSE;
9313#endif
9314
9315 /*
9316 * When nothing special, insert TAB like a normal character
9317 */
9318 if (!curbuf->b_p_et
9319 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9320 && curbuf->b_p_sts == 0)
9321 return TRUE;
9322
9323 if (stop_arrow() == FAIL)
9324 return TRUE;
9325
9326 did_ai = FALSE;
9327#ifdef FEAT_SMARTINDENT
9328 did_si = FALSE;
9329 can_si = FALSE;
9330 can_si_back = FALSE;
9331#endif
9332 AppendToRedobuff((char_u *)"\t");
9333
9334 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9335 temp = (int)curbuf->b_p_sw;
9336 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9337 temp = (int)curbuf->b_p_sts;
9338 else /* otherwise use 'tabstop' */
9339 temp = (int)curbuf->b_p_ts;
9340 temp -= get_nolist_virtcol() % temp;
9341
9342 /*
9343 * Insert the first space with ins_char(). It will delete one char in
9344 * replace mode. Insert the rest with ins_str(); it will not delete any
9345 * chars. For VREPLACE mode, we use ins_char() for all characters.
9346 */
9347 ins_char(' ');
9348 while (--temp > 0)
9349 {
9350#ifdef FEAT_VREPLACE
9351 if (State & VREPLACE_FLAG)
9352 ins_char(' ');
9353 else
9354#endif
9355 {
9356 ins_str((char_u *)" ");
9357 if (State & REPLACE_FLAG) /* no char replaced */
9358 replace_push(NUL);
9359 }
9360 }
9361
9362 /*
9363 * When 'expandtab' not set: Replace spaces by TABs where possible.
9364 */
9365 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9366 {
9367 char_u *ptr;
9368#ifdef FEAT_VREPLACE
9369 char_u *saved_line = NULL; /* init for GCC */
9370 pos_T pos;
9371#endif
9372 pos_T fpos;
9373 pos_T *cursor;
9374 colnr_T want_vcol, vcol;
9375 int change_col = -1;
9376 int save_list = curwin->w_p_list;
9377
9378 /*
9379 * Get the current line. For VREPLACE mode, don't make real changes
9380 * yet, just work on a copy of the line.
9381 */
9382#ifdef FEAT_VREPLACE
9383 if (State & VREPLACE_FLAG)
9384 {
9385 pos = curwin->w_cursor;
9386 cursor = &pos;
9387 saved_line = vim_strsave(ml_get_curline());
9388 if (saved_line == NULL)
9389 return FALSE;
9390 ptr = saved_line + pos.col;
9391 }
9392 else
9393#endif
9394 {
9395 ptr = ml_get_cursor();
9396 cursor = &curwin->w_cursor;
9397 }
9398
9399 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9400 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9401 curwin->w_p_list = FALSE;
9402
9403 /* Find first white before the cursor */
9404 fpos = curwin->w_cursor;
9405 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9406 {
9407 --fpos.col;
9408 --ptr;
9409 }
9410
9411 /* In Replace mode, don't change characters before the insert point. */
9412 if ((State & REPLACE_FLAG)
9413 && fpos.lnum == Insstart.lnum
9414 && fpos.col < Insstart.col)
9415 {
9416 ptr += Insstart.col - fpos.col;
9417 fpos.col = Insstart.col;
9418 }
9419
9420 /* compute virtual column numbers of first white and cursor */
9421 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9422 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9423
9424 /* Use as many TABs as possible. Beware of 'showbreak' and
9425 * 'linebreak' adding extra virtual columns. */
9426 while (vim_iswhite(*ptr))
9427 {
9428 i = lbr_chartabsize((char_u *)"\t", vcol);
9429 if (vcol + i > want_vcol)
9430 break;
9431 if (*ptr != TAB)
9432 {
9433 *ptr = TAB;
9434 if (change_col < 0)
9435 {
9436 change_col = fpos.col; /* Column of first change */
9437 /* May have to adjust Insstart */
9438 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9439 Insstart.col = fpos.col;
9440 }
9441 }
9442 ++fpos.col;
9443 ++ptr;
9444 vcol += i;
9445 }
9446
9447 if (change_col >= 0)
9448 {
9449 int repl_off = 0;
9450
9451 /* Skip over the spaces we need. */
9452 while (vcol < want_vcol && *ptr == ' ')
9453 {
9454 vcol += lbr_chartabsize(ptr, vcol);
9455 ++ptr;
9456 ++repl_off;
9457 }
9458 if (vcol > want_vcol)
9459 {
9460 /* Must have a char with 'showbreak' just before it. */
9461 --ptr;
9462 --repl_off;
9463 }
9464 fpos.col += repl_off;
9465
9466 /* Delete following spaces. */
9467 i = cursor->col - fpos.col;
9468 if (i > 0)
9469 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009470 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009471 /* correct replace stack. */
9472 if ((State & REPLACE_FLAG)
9473#ifdef FEAT_VREPLACE
9474 && !(State & VREPLACE_FLAG)
9475#endif
9476 )
9477 for (temp = i; --temp >= 0; )
9478 replace_join(repl_off);
9479 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009480#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009481 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009482 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009483 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009484 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9485 (char_u *)"\t", 1);
9486 }
9487#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009488 cursor->col -= i;
9489
9490#ifdef FEAT_VREPLACE
9491 /*
9492 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9493 * backspacing over the changed spacing and then inserting the new
9494 * spacing.
9495 */
9496 if (State & VREPLACE_FLAG)
9497 {
9498 /* Backspace from real cursor to change_col */
9499 backspace_until_column(change_col);
9500
9501 /* Insert each char in saved_line from changed_col to
9502 * ptr-cursor */
9503 ins_bytes_len(saved_line + change_col,
9504 cursor->col - change_col);
9505 }
9506#endif
9507 }
9508
9509#ifdef FEAT_VREPLACE
9510 if (State & VREPLACE_FLAG)
9511 vim_free(saved_line);
9512#endif
9513 curwin->w_p_list = save_list;
9514 }
9515
9516 return FALSE;
9517}
9518
9519/*
9520 * Handle CR or NL in insert mode.
9521 * Return TRUE when out of memory or can't undo.
9522 */
9523 static int
9524ins_eol(c)
9525 int c;
9526{
9527 int i;
9528
9529 if (echeck_abbr(c + ABBR_OFF))
9530 return FALSE;
9531 if (stop_arrow() == FAIL)
9532 return TRUE;
9533 undisplay_dollar();
9534
9535 /*
9536 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9537 * character under the cursor. Only push a NUL on the replace stack,
9538 * nothing to put back when the NL is deleted.
9539 */
9540 if ((State & REPLACE_FLAG)
9541#ifdef FEAT_VREPLACE
9542 && !(State & VREPLACE_FLAG)
9543#endif
9544 )
9545 replace_push(NUL);
9546
9547 /*
9548 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9549 * replacing the next line, so we push all of the characters left on the
9550 * line onto the replace stack. This is not done here though, it is done
9551 * in open_line().
9552 */
9553
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009554#ifdef FEAT_VIRTUALEDIT
9555 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9556 * CTRL-O). */
9557 if (virtual_active() && curwin->w_cursor.coladd > 0)
9558 coladvance(getviscol());
9559#endif
9560
Bram Moolenaar071d4272004-06-13 20:20:40 +00009561#ifdef FEAT_RIGHTLEFT
9562# ifdef FEAT_FKMAP
9563 if (p_altkeymap && p_fkmap)
9564 fkmap(NL);
9565# endif
9566 /* NL in reverse insert will always start in the end of
9567 * current line. */
9568 if (revins_on)
9569 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9570#endif
9571
9572 AppendToRedobuff(NL_STR);
9573 i = open_line(FORWARD,
9574#ifdef FEAT_COMMENTS
9575 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9576#endif
9577 0, old_indent);
9578 old_indent = 0;
9579#ifdef FEAT_CINDENT
9580 can_cindent = TRUE;
9581#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009582#ifdef FEAT_FOLDING
9583 /* When inserting a line the cursor line must never be in a closed fold. */
9584 foldOpenCursor();
9585#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586
9587 return (!i);
9588}
9589
9590#ifdef FEAT_DIGRAPHS
9591/*
9592 * Handle digraph in insert mode.
9593 * Returns character still to be inserted, or NUL when nothing remaining to be
9594 * done.
9595 */
9596 static int
9597ins_digraph()
9598{
9599 int c;
9600 int cc;
9601
9602 pc_status = PC_STATUS_UNSET;
9603 if (redrawing() && !char_avail())
9604 {
9605 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009606 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607
9608 edit_putchar('?', TRUE);
9609#ifdef FEAT_CMDL_INFO
9610 add_to_showcmd_c(Ctrl_K);
9611#endif
9612 }
9613
9614#ifdef USE_ON_FLY_SCROLL
9615 dont_scroll = TRUE; /* disallow scrolling here */
9616#endif
9617
9618 /* don't map the digraph chars. This also prevents the
9619 * mode message to be deleted when ESC is hit */
9620 ++no_mapping;
9621 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009622 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 --no_mapping;
9624 --allow_keys;
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009625 edit_unputchar(); /* when line fits in 'columns' the '?' is at the start
9626 of the next line and will not be redrawn */
9627
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628 if (IS_SPECIAL(c) || mod_mask) /* special key */
9629 {
9630#ifdef FEAT_CMDL_INFO
9631 clear_showcmd();
9632#endif
9633 insert_special(c, TRUE, FALSE);
9634 return NUL;
9635 }
9636 if (c != ESC)
9637 {
9638 if (redrawing() && !char_avail())
9639 {
9640 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009641 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642
9643 if (char2cells(c) == 1)
9644 {
9645 /* first remove the '?', otherwise it's restored when typing
9646 * an ESC next */
9647 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009648 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649 edit_putchar(c, TRUE);
9650 }
9651#ifdef FEAT_CMDL_INFO
9652 add_to_showcmd_c(c);
9653#endif
9654 }
9655 ++no_mapping;
9656 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009657 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658 --no_mapping;
9659 --allow_keys;
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009660 edit_unputchar(); /* when line fits in 'columns' the '?' is at the
9661 start of the next line and will not be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 if (cc != ESC)
9663 {
9664 AppendToRedobuff((char_u *)CTRL_V_STR);
9665 c = getdigraph(c, cc, TRUE);
9666#ifdef FEAT_CMDL_INFO
9667 clear_showcmd();
9668#endif
9669 return c;
9670 }
9671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672#ifdef FEAT_CMDL_INFO
9673 clear_showcmd();
9674#endif
9675 return NUL;
9676}
9677#endif
9678
9679/*
9680 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9681 * Returns the char to be inserted, or NUL if none found.
9682 */
9683 static int
9684ins_copychar(lnum)
9685 linenr_T lnum;
9686{
9687 int c;
9688 int temp;
9689 char_u *ptr, *prev_ptr;
9690
9691 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9692 {
9693 vim_beep();
9694 return NUL;
9695 }
9696
9697 /* try to advance to the cursor column */
9698 temp = 0;
9699 ptr = ml_get(lnum);
9700 prev_ptr = ptr;
9701 validate_virtcol();
9702 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9703 {
9704 prev_ptr = ptr;
9705 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9706 }
9707 if ((colnr_T)temp > curwin->w_virtcol)
9708 ptr = prev_ptr;
9709
9710#ifdef FEAT_MBYTE
9711 c = (*mb_ptr2char)(ptr);
9712#else
9713 c = *ptr;
9714#endif
9715 if (c == NUL)
9716 vim_beep();
9717 return c;
9718}
9719
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009720/*
9721 * CTRL-Y or CTRL-E typed in Insert mode.
9722 */
9723 static int
9724ins_ctrl_ey(tc)
9725 int tc;
9726{
9727 int c = tc;
9728
9729#ifdef FEAT_INS_EXPAND
9730 if (ctrl_x_mode == CTRL_X_SCROLL)
9731 {
9732 if (c == Ctrl_Y)
9733 scrolldown_clamp();
9734 else
9735 scrollup_clamp();
9736 redraw_later(VALID);
9737 }
9738 else
9739#endif
9740 {
9741 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9742 if (c != NUL)
9743 {
9744 long tw_save;
9745
9746 /* The character must be taken literally, insert like it
9747 * was typed after a CTRL-V, and pretend 'textwidth'
9748 * wasn't set. Digits, 'o' and 'x' are special after a
9749 * CTRL-V, don't use it for these. */
9750 if (c < 256 && !isalnum(c))
9751 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9752 tw_save = curbuf->b_p_tw;
9753 curbuf->b_p_tw = -1;
9754 insert_special(c, TRUE, FALSE);
9755 curbuf->b_p_tw = tw_save;
9756#ifdef FEAT_RIGHTLEFT
9757 revins_chars++;
9758 revins_legal++;
9759#endif
9760 c = Ctrl_V; /* pretend CTRL-V is last character */
9761 auto_format(FALSE, TRUE);
9762 }
9763 }
9764 return c;
9765}
9766
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767#ifdef FEAT_SMARTINDENT
9768/*
9769 * Try to do some very smart auto-indenting.
9770 * Used when inserting a "normal" character.
9771 */
9772 static void
9773ins_try_si(c)
9774 int c;
9775{
9776 pos_T *pos, old_pos;
9777 char_u *ptr;
9778 int i;
9779 int temp;
9780
9781 /*
9782 * do some very smart indenting when entering '{' or '}'
9783 */
9784 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9785 {
9786 /*
9787 * for '}' set indent equal to indent of line containing matching '{'
9788 */
9789 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9790 {
9791 old_pos = curwin->w_cursor;
9792 /*
9793 * If the matching '{' has a ')' immediately before it (ignoring
9794 * white-space), then line up with the start of the line
9795 * containing the matching '(' if there is one. This handles the
9796 * case where an "if (..\n..) {" statement continues over multiple
9797 * lines -- webb
9798 */
9799 ptr = ml_get(pos->lnum);
9800 i = pos->col;
9801 if (i > 0) /* skip blanks before '{' */
9802 while (--i > 0 && vim_iswhite(ptr[i]))
9803 ;
9804 curwin->w_cursor.lnum = pos->lnum;
9805 curwin->w_cursor.col = i;
9806 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9807 curwin->w_cursor = *pos;
9808 i = get_indent();
9809 curwin->w_cursor = old_pos;
9810#ifdef FEAT_VREPLACE
9811 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009812 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 else
9814#endif
9815 (void)set_indent(i, SIN_CHANGED);
9816 }
9817 else if (curwin->w_cursor.col > 0)
9818 {
9819 /*
9820 * when inserting '{' after "O" reduce indent, but not
9821 * more than indent of previous line
9822 */
9823 temp = TRUE;
9824 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9825 {
9826 old_pos = curwin->w_cursor;
9827 i = get_indent();
9828 while (curwin->w_cursor.lnum > 1)
9829 {
9830 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9831
9832 /* ignore empty lines and lines starting with '#'. */
9833 if (*ptr != '#' && *ptr != NUL)
9834 break;
9835 }
9836 if (get_indent() >= i)
9837 temp = FALSE;
9838 curwin->w_cursor = old_pos;
9839 }
9840 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009841 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842 }
9843 }
9844
9845 /*
9846 * set indent of '#' always to 0
9847 */
9848 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9849 {
9850 /* remember current indent for next line */
9851 old_indent = get_indent();
9852 (void)set_indent(0, SIN_CHANGED);
9853 }
9854
9855 /* Adjust ai_col, the char at this position can be deleted. */
9856 if (ai_col > curwin->w_cursor.col)
9857 ai_col = curwin->w_cursor.col;
9858}
9859#endif
9860
9861/*
9862 * Get the value that w_virtcol would have when 'list' is off.
9863 * Unless 'cpo' contains the 'L' flag.
9864 */
9865 static colnr_T
9866get_nolist_virtcol()
9867{
9868 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9869 return getvcol_nolist(&curwin->w_cursor);
9870 validate_virtcol();
9871 return curwin->w_virtcol;
9872}