blob: 4754fd9bde8cf3c543fdcd703f5d80ba4eefe87e [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));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200227static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000228#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 Moolenaarf5963f72010-07-23 22:10:27 +0200380#ifdef FEAT_CONCEAL
381 /* Check if the cursor line needs redrawing before changing State. If
382 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
383 conceal_check_cursur_line_redraw();
384#endif
385
Bram Moolenaar071d4272004-06-13 20:20:40 +0000386#ifdef FEAT_MOUSE
387 /*
388 * When doing a paste with the middle mouse button, Insstart is set to
389 * where the paste started.
390 */
391 if (where_paste_started.lnum != 0)
392 Insstart = where_paste_started;
393 else
394#endif
395 {
396 Insstart = curwin->w_cursor;
397 if (startln)
398 Insstart.col = 0;
399 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000400 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000401 Insstart_blank_vcol = MAXCOL;
402 if (!did_ai)
403 ai_col = 0;
404
405 if (cmdchar != NUL && restart_edit == 0)
406 {
407 ResetRedobuff();
408 AppendNumberToRedobuff(count);
409#ifdef FEAT_VREPLACE
410 if (cmdchar == 'V' || cmdchar == 'v')
411 {
412 /* "gR" or "gr" command */
413 AppendCharToRedobuff('g');
414 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
415 }
416 else
417#endif
418 {
419 AppendCharToRedobuff(cmdchar);
420 if (cmdchar == 'g') /* "gI" command */
421 AppendCharToRedobuff('I');
422 else if (cmdchar == 'r') /* "r<CR>" command */
423 count = 1; /* insert only one <CR> */
424 }
425 }
426
427 if (cmdchar == 'R')
428 {
429#ifdef FEAT_FKMAP
430 if (p_fkmap && p_ri)
431 {
432 beep_flush();
433 EMSG(farsi_text_3); /* encoded in Farsi */
434 State = INSERT;
435 }
436 else
437#endif
438 State = REPLACE;
439 }
440#ifdef FEAT_VREPLACE
441 else if (cmdchar == 'V' || cmdchar == 'v')
442 {
443 State = VREPLACE;
444 replaceState = VREPLACE;
445 orig_line_count = curbuf->b_ml.ml_line_count;
446 vr_lines_changed = 1;
447 }
448#endif
449 else
450 State = INSERT;
451
452 stop_insert_mode = FALSE;
453
454 /*
455 * Need to recompute the cursor position, it might move when the cursor is
456 * on a TAB or special character.
457 */
458 curs_columns(TRUE);
459
460 /*
461 * Enable langmap or IME, indicated by 'iminsert'.
462 * Note that IME may enabled/disabled without us noticing here, thus the
463 * 'iminsert' value may not reflect what is actually used. It is updated
464 * when hitting <Esc>.
465 */
466 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
467 State |= LANGMAP;
468#ifdef USE_IM_CONTROL
469 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
470#endif
471
Bram Moolenaar071d4272004-06-13 20:20:40 +0000472#ifdef FEAT_MOUSE
473 setmouse();
474#endif
475#ifdef FEAT_CMDL_INFO
476 clear_showcmd();
477#endif
478#ifdef FEAT_RIGHTLEFT
479 /* there is no reverse replace mode */
480 revins_on = (State == INSERT && p_ri);
481 if (revins_on)
482 undisplay_dollar();
483 revins_chars = 0;
484 revins_legal = 0;
485 revins_scol = -1;
486#endif
487
488 /*
489 * Handle restarting Insert mode.
490 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
491 * restart_edit non-zero, and something in the stuff buffer.
492 */
493 if (restart_edit != 0 && stuff_empty())
494 {
495#ifdef FEAT_MOUSE
496 /*
497 * After a paste we consider text typed to be part of the insert for
498 * the pasted text. You can backspace over the pasted text too.
499 */
500 if (where_paste_started.lnum)
501 arrow_used = FALSE;
502 else
503#endif
504 arrow_used = TRUE;
505 restart_edit = 0;
506
507 /*
508 * If the cursor was after the end-of-line before the CTRL-O and it is
509 * now at the end-of-line, put it after the end-of-line (this is not
510 * correct in very rare cases).
511 * Also do this if curswant is greater than the current virtual
512 * column. Eg after "^O$" or "^O80|".
513 */
514 validate_virtcol();
515 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000516 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 || curwin->w_curswant > curwin->w_virtcol)
518 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
519 {
520 if (ptr[1] == NUL)
521 ++curwin->w_cursor.col;
522#ifdef FEAT_MBYTE
523 else if (has_mbyte)
524 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000525 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000526 if (ptr[i] == NUL)
527 curwin->w_cursor.col += i;
528 }
529#endif
530 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000531 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000532 }
533 else
534 arrow_used = FALSE;
535
536 /* we are in insert mode now, don't need to start it anymore */
537 need_start_insertmode = FALSE;
538
539 /* Need to save the line for undo before inserting the first char. */
540 ins_need_undo = TRUE;
541
542#ifdef FEAT_MOUSE
543 where_paste_started.lnum = 0;
544#endif
545#ifdef FEAT_CINDENT
546 can_cindent = TRUE;
547#endif
548#ifdef FEAT_FOLDING
549 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
550 * restarting. */
551 if (!p_im && did_restart_edit == 0)
552 foldOpenCursor();
553#endif
554
555 /*
556 * If 'showmode' is set, show the current (insert/replace/..) mode.
557 * A warning message for changing a readonly file is given here, before
558 * actually changing anything. It's put after the mode, if any.
559 */
560 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000561 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000562 i = showmode();
563
564 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000565 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000566
567#ifdef CURSOR_SHAPE
568 ui_cursor_shape(); /* may show different cursor shape */
569#endif
570#ifdef FEAT_DIGRAPHS
571 do_digraph(-1); /* clear digraphs */
572#endif
573
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000574 /*
575 * Get the current length of the redo buffer, those characters have to be
576 * skipped if we want to get to the inserted characters.
577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000578 ptr = get_inserted();
579 if (ptr == NULL)
580 new_insert_skip = 0;
581 else
582 {
583 new_insert_skip = (int)STRLEN(ptr);
584 vim_free(ptr);
585 }
586
587 old_indent = 0;
588
589 /*
590 * Main loop in Insert mode: repeat until Insert mode is left.
591 */
592 for (;;)
593 {
594#ifdef FEAT_RIGHTLEFT
595 if (!revins_legal)
596 revins_scol = -1; /* reset on illegal motions */
597 else
598 revins_legal = 0;
599#endif
600 if (arrow_used) /* don't repeat insert when arrow key used */
601 count = 0;
602
603 if (stop_insert_mode)
604 {
605 /* ":stopinsert" used or 'insertmode' reset */
606 count = 0;
607 goto doESCkey;
608 }
609
610 /* set curwin->w_curswant for next K_DOWN or K_UP */
611 if (!arrow_used)
612 curwin->w_set_curswant = TRUE;
613
614 /* If there is no typeahead may check for timestamps (e.g., for when a
615 * menu invoked a shell command). */
616 if (stuff_empty())
617 {
618 did_check_timestamps = FALSE;
619 if (need_check_timestamps)
620 check_timestamps(FALSE);
621 }
622
623 /*
624 * When emsg() was called msg_scroll will have been set.
625 */
626 msg_scroll = FALSE;
627
628#ifdef FEAT_GUI
629 /* When 'mousefocus' is set a mouse movement may have taken us to
630 * another window. "need_mouse_correct" may then be set because of an
631 * autocommand. */
632 if (need_mouse_correct)
633 gui_mouse_correct();
634#endif
635
636#ifdef FEAT_FOLDING
637 /* Open fold at the cursor line, according to 'foldopen'. */
638 if (fdo_flags & FDO_INSERT)
639 foldOpenCursor();
640 /* Close folds where the cursor isn't, according to 'foldclose' */
641 if (!char_avail())
642 foldCheckClose();
643#endif
644
645 /*
646 * If we inserted a character at the last position of the last line in
647 * the window, scroll the window one line up. This avoids an extra
648 * redraw.
649 * This is detected when the cursor column is smaller after inserting
650 * something.
651 * Don't do this when the topline changed already, it has
652 * already been adjusted (by insertchar() calling open_line())).
653 */
654 if (curbuf->b_mod_set
655 && curwin->w_p_wrap
656 && !did_backspace
657 && curwin->w_topline == old_topline
658#ifdef FEAT_DIFF
659 && curwin->w_topfill == old_topfill
660#endif
661 )
662 {
663 mincol = curwin->w_wcol;
664 validate_cursor_col();
665
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000666 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000667 && curwin->w_wrow == W_WINROW(curwin)
668 + curwin->w_height - 1 - p_so
669 && (curwin->w_cursor.lnum != curwin->w_topline
670#ifdef FEAT_DIFF
671 || curwin->w_topfill > 0
672#endif
673 ))
674 {
675#ifdef FEAT_DIFF
676 if (curwin->w_topfill > 0)
677 --curwin->w_topfill;
678 else
679#endif
680#ifdef FEAT_FOLDING
681 if (hasFolding(curwin->w_topline, NULL, &old_topline))
682 set_topline(curwin, old_topline + 1);
683 else
684#endif
685 set_topline(curwin, curwin->w_topline + 1);
686 }
687 }
688
689 /* May need to adjust w_topline to show the cursor. */
690 update_topline();
691
692 did_backspace = FALSE;
693
694 validate_cursor(); /* may set must_redraw */
695
696 /*
697 * Redraw the display when no characters are waiting.
698 * Also shows mode, ruler and positions cursor.
699 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000700 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000701
702#ifdef FEAT_SCROLLBIND
703 if (curwin->w_p_scb)
704 do_check_scrollbind(TRUE);
705#endif
706
Bram Moolenaar860cae12010-06-05 23:22:07 +0200707#ifdef FEAT_CURSORBIND
708 if (curwin->w_p_crb)
709 do_check_cursorbind();
710#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000711 update_curswant();
712 old_topline = curwin->w_topline;
713#ifdef FEAT_DIFF
714 old_topfill = curwin->w_topfill;
715#endif
716
717#ifdef USE_ON_FLY_SCROLL
718 dont_scroll = FALSE; /* allow scrolling here */
719#endif
720
721 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000722 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 */
724 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000725 do
726 {
727 c = safe_vgetc();
728 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000729
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000730#ifdef FEAT_AUTOCMD
731 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
732 did_cursorhold = TRUE;
733#endif
734
Bram Moolenaar071d4272004-06-13 20:20:40 +0000735#ifdef FEAT_RIGHTLEFT
736 if (p_hkmap && KeyTyped)
737 c = hkmap(c); /* Hebrew mode mapping */
738#endif
739#ifdef FEAT_FKMAP
740 if (p_fkmap && KeyTyped)
741 c = fkmap(c); /* Farsi mode mapping */
742#endif
743
744#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000745 /*
746 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000747 * and the cursor is still in the completed word. Only when there is
748 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000749 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000750 if (compl_started
751 && pum_wanted()
752 && curwin->w_cursor.col >= compl_col
753 && (compl_shown_match == NULL
754 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000755 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000756 /* BS: Delete one character from "compl_leader". */
757 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000758 && curwin->w_cursor.col > compl_col
759 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000760 continue;
761
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000762 /* When no match was selected or it was edited. */
763 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000764 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000765 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000766 * "compl_leader". Except when at the original match and
767 * there is nothing to add, CTRL-L works like CTRL-P then. */
768 if (c == Ctrl_L
769 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000770 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000771 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000772 {
773 ins_compl_addfrommatch();
774 continue;
775 }
776
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000777 /* A non-white character that fits in with the current
778 * completion: Add to "compl_leader". */
779 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000780 {
781 ins_compl_addleader(c);
782 continue;
783 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000784
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000785 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000786 * compl_enter_selects is set the Enter key does the same. */
787 if (c == Ctrl_Y || (compl_enter_selects
788 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000789 {
790 ins_compl_delete();
791 ins_compl_insert();
792 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000793 }
794 }
795
Bram Moolenaar071d4272004-06-13 20:20:40 +0000796 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
797 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000798 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000799 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000800 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000801#endif
802
Bram Moolenaar488c6512005-08-11 20:09:58 +0000803 /* CTRL-\ CTRL-N goes to Normal mode,
804 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
805 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 if (c == Ctrl_BSL)
807 {
808 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000809 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000810 ++no_mapping;
811 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000812 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000813 --no_mapping;
814 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000815 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000816 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000817 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000818 vungetc(c);
819 c = Ctrl_BSL;
820 }
821 else if (c == Ctrl_G && p_im)
822 continue;
823 else
824 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000825 if (c == Ctrl_O)
826 {
827 ins_ctrl_o();
828 ins_at_eol = FALSE; /* cursor keeps its column */
829 nomove = TRUE;
830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000831 count = 0;
832 goto doESCkey;
833 }
834 }
835
836#ifdef FEAT_DIGRAPHS
837 c = do_digraph(c);
838#endif
839
840#ifdef FEAT_INS_EXPAND
841 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
842 goto docomplete;
843#endif
844 if (c == Ctrl_V || c == Ctrl_Q)
845 {
846 ins_ctrl_v();
847 c = Ctrl_V; /* pretend CTRL-V is last typed character */
848 continue;
849 }
850
851#ifdef FEAT_CINDENT
852 if (cindent_on()
853# ifdef FEAT_INS_EXPAND
854 && ctrl_x_mode == 0
855# endif
856 )
857 {
858 /* A key name preceded by a bang means this key is not to be
859 * inserted. Skip ahead to the re-indenting below.
860 * A key name preceded by a star means that indenting has to be
861 * done before inserting the key. */
862 line_is_white = inindent(0);
863 if (in_cinkeys(c, '!', line_is_white))
864 goto force_cindent;
865 if (can_cindent && in_cinkeys(c, '*', line_is_white)
866 && stop_arrow() == OK)
867 do_c_expr_indent();
868 }
869#endif
870
871#ifdef FEAT_RIGHTLEFT
872 if (curwin->w_p_rl)
873 switch (c)
874 {
875 case K_LEFT: c = K_RIGHT; break;
876 case K_S_LEFT: c = K_S_RIGHT; break;
877 case K_C_LEFT: c = K_C_RIGHT; break;
878 case K_RIGHT: c = K_LEFT; break;
879 case K_S_RIGHT: c = K_S_LEFT; break;
880 case K_C_RIGHT: c = K_C_LEFT; break;
881 }
882#endif
883
884#ifdef FEAT_VISUAL
885 /*
886 * If 'keymodel' contains "startsel", may start selection. If it
887 * does, a CTRL-O and c will be stuffed, we need to get these
888 * characters.
889 */
890 if (ins_start_select(c))
891 continue;
892#endif
893
894 /*
895 * The big switch to handle a character in insert mode.
896 */
897 switch (c)
898 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000899 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000900 if (echeck_abbr(ESC + ABBR_OFF))
901 break;
902 /*FALLTHROUGH*/
903
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000904 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000905#ifdef FEAT_CMDWIN
906 if (c == Ctrl_C && cmdwin_type != 0)
907 {
908 /* Close the cmdline window. */
909 cmdwin_result = K_IGNORE;
910 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000911 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 goto doESCkey;
913 }
914#endif
915
916#ifdef UNIX
917do_intr:
918#endif
919 /* when 'insertmode' set, and not halfway a mapping, don't leave
920 * Insert mode */
921 if (goto_im())
922 {
923 if (got_int)
924 {
925 (void)vgetc(); /* flush all buffers */
926 got_int = FALSE;
927 }
928 else
929 vim_beep();
930 break;
931 }
932doESCkey:
933 /*
934 * This is the ONLY return from edit()!
935 */
936 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
937 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000938 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000939 o_lnum = curwin->w_cursor.lnum;
940
Bram Moolenaar488c6512005-08-11 20:09:58 +0000941 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000942 {
943#ifdef FEAT_AUTOCMD
944 if (cmdchar != 'r' && cmdchar != 'v')
945 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
946 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000947 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000948#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000950 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000951 continue;
952
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000953 case Ctrl_Z: /* suspend when 'insertmode' set */
954 if (!p_im)
955 goto normalchar; /* insert CTRL-Z as normal char */
956 stuffReadbuff((char_u *)":st\r");
957 c = Ctrl_O;
958 /*FALLTHROUGH*/
959
960 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000961#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000962 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000963 goto docomplete;
964#endif
965 if (echeck_abbr(Ctrl_O + ABBR_OFF))
966 break;
967 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000968
969#ifdef FEAT_VIRTUALEDIT
970 /* don't move the cursor left when 'virtualedit' has "onemore". */
971 if (ve_flags & VE_ONEMORE)
972 {
973 ins_at_eol = FALSE;
974 nomove = TRUE;
975 }
976#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000977 count = 0;
978 goto doESCkey;
979
Bram Moolenaar572cb562005-08-05 21:35:02 +0000980 case K_INS: /* toggle insert/replace mode */
981 case K_KINS:
982 ins_insert(replaceState);
983 break;
984
985 case K_SELECT: /* end of Select mode mapping - ignore */
986 break;
987
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000988#ifdef FEAT_SNIFF
989 case K_SNIFF: /* Sniff command received */
990 stuffcharReadbuff(K_SNIFF);
991 goto doESCkey;
992#endif
993
994 case K_HELP: /* Help key works like <ESC> <Help> */
995 case K_F1:
996 case K_XF1:
997 stuffcharReadbuff(K_HELP);
998 if (p_im)
999 need_start_insertmode = TRUE;
1000 goto doESCkey;
1001
1002#ifdef FEAT_NETBEANS_INTG
1003 case K_F21: /* NetBeans command */
1004 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001005 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001006 --no_mapping;
1007 netbeans_keycommand(i);
1008 break;
1009#endif
1010
1011 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001012 case NUL:
1013 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001014 /* For ^@ the trailing ESC will end the insert, unless there is an
1015 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001016 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1017 && c != Ctrl_A && !p_im)
1018 goto doESCkey; /* quit insert mode */
1019 inserted_space = FALSE;
1020 break;
1021
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001022 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001023 ins_reg();
1024 auto_format(FALSE, TRUE);
1025 inserted_space = FALSE;
1026 break;
1027
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001028 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 ins_ctrl_g();
1030 break;
1031
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001032 case Ctrl_HAT: /* switch input mode and/or langmap */
1033 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 break;
1035
1036#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001037 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038 if (!p_ari)
1039 goto normalchar;
1040 ins_ctrl_();
1041 break;
1042#endif
1043
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001044 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1046 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1047 goto docomplete;
1048#endif
1049 /* FALLTHROUGH */
1050
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001051 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052# ifdef FEAT_INS_EXPAND
1053 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1054 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001055 if (has_compl_option(FALSE))
1056 goto docomplete;
1057 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058 }
1059# endif
1060 ins_shift(c, lastc);
1061 auto_format(FALSE, TRUE);
1062 inserted_space = FALSE;
1063 break;
1064
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001065 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066 case K_KDEL:
1067 ins_del();
1068 auto_format(FALSE, TRUE);
1069 break;
1070
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001071 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001072 case Ctrl_H:
1073 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1074 auto_format(FALSE, TRUE);
1075 break;
1076
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001077 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001078 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1079 auto_format(FALSE, TRUE);
1080 break;
1081
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001082 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001083# ifdef FEAT_COMPL_FUNC
1084 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001085 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001086 goto docomplete;
1087# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1089 auto_format(FALSE, TRUE);
1090 inserted_space = FALSE;
1091 break;
1092
1093#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001094 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 case K_LEFTMOUSE_NM:
1096 case K_LEFTDRAG:
1097 case K_LEFTRELEASE:
1098 case K_LEFTRELEASE_NM:
1099 case K_MIDDLEMOUSE:
1100 case K_MIDDLEDRAG:
1101 case K_MIDDLERELEASE:
1102 case K_RIGHTMOUSE:
1103 case K_RIGHTDRAG:
1104 case K_RIGHTRELEASE:
1105 case K_X1MOUSE:
1106 case K_X1DRAG:
1107 case K_X1RELEASE:
1108 case K_X2MOUSE:
1109 case K_X2DRAG:
1110 case K_X2RELEASE:
1111 ins_mouse(c);
1112 break;
1113
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001114 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001115 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001116 break;
1117
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001118 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001119 ins_mousescroll(MSCR_UP);
1120 break;
1121
1122 case K_MOUSELEFT: /* Scroll wheel left */
1123 ins_mousescroll(MSCR_LEFT);
1124 break;
1125
1126 case K_MOUSERIGHT: /* Scroll wheel right */
1127 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001128 break;
1129#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001130#ifdef FEAT_GUI_TABLINE
1131 case K_TABLINE:
1132 case K_TABMENU:
1133 ins_tabline(c);
1134 break;
1135#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001136
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001137 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001138 break;
1139
Bram Moolenaar754b5602006-02-09 23:53:20 +00001140#ifdef FEAT_AUTOCMD
1141 case K_CURSORHOLD: /* Didn't type something for a while. */
1142 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1143 did_cursorhold = TRUE;
1144 break;
1145#endif
1146
Bram Moolenaar4770d092006-01-12 23:22:24 +00001147#ifdef FEAT_GUI_W32
1148 /* On Win32 ignore <M-F4>, we get it when closing the window was
1149 * cancelled. */
1150 case K_F4:
1151 if (mod_mask != MOD_MASK_ALT)
1152 goto normalchar;
1153 break;
1154#endif
1155
Bram Moolenaar071d4272004-06-13 20:20:40 +00001156#ifdef FEAT_GUI
1157 case K_VER_SCROLLBAR:
1158 ins_scroll();
1159 break;
1160
1161 case K_HOR_SCROLLBAR:
1162 ins_horscroll();
1163 break;
1164#endif
1165
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001166 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001167 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 case K_S_HOME:
1169 case K_C_HOME:
1170 ins_home(c);
1171 break;
1172
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001173 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001174 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 case K_S_END:
1176 case K_C_END:
1177 ins_end(c);
1178 break;
1179
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001180 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001181 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1182 ins_s_left();
1183 else
1184 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001185 break;
1186
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001187 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001188 case K_C_LEFT:
1189 ins_s_left();
1190 break;
1191
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001192 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001193 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1194 ins_s_right();
1195 else
1196 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001197 break;
1198
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001199 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200 case K_C_RIGHT:
1201 ins_s_right();
1202 break;
1203
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001204 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001205#ifdef FEAT_INS_EXPAND
1206 if (pum_visible())
1207 goto docomplete;
1208#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001209 if (mod_mask & MOD_MASK_SHIFT)
1210 ins_pageup();
1211 else
1212 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001213 break;
1214
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001215 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001216 case K_PAGEUP:
1217 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001218#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001219 if (pum_visible())
1220 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001221#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001222 ins_pageup();
1223 break;
1224
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001225 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001226#ifdef FEAT_INS_EXPAND
1227 if (pum_visible())
1228 goto docomplete;
1229#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001230 if (mod_mask & MOD_MASK_SHIFT)
1231 ins_pagedown();
1232 else
1233 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001234 break;
1235
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001236 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001237 case K_PAGEDOWN:
1238 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001239#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001240 if (pum_visible())
1241 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001242#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 ins_pagedown();
1244 break;
1245
1246#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001247 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248 ins_drop();
1249 break;
1250#endif
1251
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001252 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001253 c = TAB;
1254 /* FALLTHROUGH */
1255
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001256 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001257#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1258 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1259 goto docomplete;
1260#endif
1261 inserted_space = FALSE;
1262 if (ins_tab())
1263 goto normalchar; /* insert TAB as a normal char */
1264 auto_format(FALSE, TRUE);
1265 break;
1266
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001267 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001268 c = CAR;
1269 /* FALLTHROUGH */
1270 case CAR:
1271 case NL:
1272#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1273 /* In a quickfix window a <CR> jumps to the error under the
1274 * cursor. */
1275 if (bt_quickfix(curbuf) && c == CAR)
1276 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001277 if (curwin->w_llist_ref == NULL) /* quickfix window */
1278 do_cmdline_cmd((char_u *)".cc");
1279 else /* location list window */
1280 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001281 break;
1282 }
1283#endif
1284#ifdef FEAT_CMDWIN
1285 if (cmdwin_type != 0)
1286 {
1287 /* Execute the command in the cmdline window. */
1288 cmdwin_result = CAR;
1289 goto doESCkey;
1290 }
1291#endif
1292 if (ins_eol(c) && !p_im)
1293 goto doESCkey; /* out of memory */
1294 auto_format(FALSE, FALSE);
1295 inserted_space = FALSE;
1296 break;
1297
Bram Moolenaar860cae12010-06-05 23:22:07 +02001298#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001299 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001300# ifdef FEAT_INS_EXPAND
1301 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1302 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001303 if (has_compl_option(TRUE))
1304 goto docomplete;
1305 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001306 }
1307# endif
1308# ifdef FEAT_DIGRAPHS
1309 c = ins_digraph();
1310 if (c == NUL)
1311 break;
1312# endif
1313 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001314#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001315
1316#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001317 case Ctrl_X: /* Enter CTRL-X mode */
1318 ins_ctrl_x();
1319 break;
1320
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001321 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001322 if (ctrl_x_mode != CTRL_X_TAGS)
1323 goto normalchar;
1324 goto docomplete;
1325
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001326 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001327 if (ctrl_x_mode != CTRL_X_FILES)
1328 goto normalchar;
1329 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001330
1331 case 's': /* Spelling completion after ^X */
1332 case Ctrl_S:
1333 if (ctrl_x_mode != CTRL_X_SPELL)
1334 goto normalchar;
1335 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001336#endif
1337
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001338 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339#ifdef FEAT_INS_EXPAND
1340 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1341#endif
1342 {
1343 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1344 if (p_im)
1345 {
1346 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1347 break;
1348 goto doESCkey;
1349 }
1350 goto normalchar;
1351 }
1352#ifdef FEAT_INS_EXPAND
1353 /* FALLTHROUGH */
1354
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001355 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001356 case Ctrl_N:
1357 /* if 'complete' is empty then plain ^P is no longer special,
1358 * but it is under other ^X modes */
1359 if (*curbuf->b_p_cpt == NUL
1360 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001361 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001362 goto normalchar;
1363
1364docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001365 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001366 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001367 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001368 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001369 break;
1370#endif /* FEAT_INS_EXPAND */
1371
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001372 case Ctrl_Y: /* copy from previous line or scroll down */
1373 case Ctrl_E: /* copy from next line or scroll up */
1374 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 break;
1376
1377 default:
1378#ifdef UNIX
1379 if (c == intr_char) /* special interrupt char */
1380 goto do_intr;
1381#endif
1382
1383 /*
1384 * Insert a nomal character.
1385 */
1386normalchar:
1387#ifdef FEAT_SMARTINDENT
1388 /* Try to perform smart-indenting. */
1389 ins_try_si(c);
1390#endif
1391
1392 if (c == ' ')
1393 {
1394 inserted_space = TRUE;
1395#ifdef FEAT_CINDENT
1396 if (inindent(0))
1397 can_cindent = FALSE;
1398#endif
1399 if (Insstart_blank_vcol == MAXCOL
1400 && curwin->w_cursor.lnum == Insstart.lnum)
1401 Insstart_blank_vcol = get_nolist_virtcol();
1402 }
1403
1404 if (vim_iswordc(c) || !echeck_abbr(
1405#ifdef FEAT_MBYTE
1406 /* Add ABBR_OFF for characters above 0x100, this is
1407 * what check_abbr() expects. */
1408 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1409#endif
1410 c))
1411 {
1412 insert_special(c, FALSE, FALSE);
1413#ifdef FEAT_RIGHTLEFT
1414 revins_legal++;
1415 revins_chars++;
1416#endif
1417 }
1418
1419 auto_format(FALSE, TRUE);
1420
1421#ifdef FEAT_FOLDING
1422 /* When inserting a character the cursor line must never be in a
1423 * closed fold. */
1424 foldOpenCursor();
1425#endif
1426 break;
1427 } /* end of switch (c) */
1428
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001429#ifdef FEAT_AUTOCMD
1430 /* If typed something may trigger CursorHoldI again. */
1431 if (c != K_CURSORHOLD)
1432 did_cursorhold = FALSE;
1433#endif
1434
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 /* If the cursor was moved we didn't just insert a space */
1436 if (arrow_used)
1437 inserted_space = FALSE;
1438
1439#ifdef FEAT_CINDENT
1440 if (can_cindent && cindent_on()
1441# ifdef FEAT_INS_EXPAND
1442 && ctrl_x_mode == 0
1443# endif
1444 )
1445 {
1446force_cindent:
1447 /*
1448 * Indent now if a key was typed that is in 'cinkeys'.
1449 */
1450 if (in_cinkeys(c, ' ', line_is_white))
1451 {
1452 if (stop_arrow() == OK)
1453 /* re-indent the current line */
1454 do_c_expr_indent();
1455 }
1456 }
1457#endif /* FEAT_CINDENT */
1458
1459 } /* for (;;) */
1460 /* NOTREACHED */
1461}
1462
1463/*
1464 * Redraw for Insert mode.
1465 * This is postponed until getting the next character to make '$' in the 'cpo'
1466 * option work correctly.
1467 * Only redraw when there are no characters available. This speeds up
1468 * inserting sequences of characters (e.g., for CTRL-R).
1469 */
1470 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001471ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001472 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001474#ifdef FEAT_CONCEAL
1475 linenr_T conceal_old_cursor_line = 0;
1476 linenr_T conceal_new_cursor_line = 0;
1477 int conceal_update_lines = FALSE;
1478#endif
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 if (!char_avail())
1481 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001482#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001483 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1484 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001485 if (ready && (
1486# ifdef FEAT_AUTOCMD
1487 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001488# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001489# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1490 ||
1491# endif
1492# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001493 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001494# endif
1495 )
1496 && !equalpos(last_cursormoved, curwin->w_cursor)
1497# ifdef FEAT_INS_EXPAND
1498 && !pum_visible()
1499# endif
1500 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001501 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001502# ifdef FEAT_SYN_HL
1503 /* Need to update the screen first, to make sure syntax
1504 * highlighting is correct after making a change (e.g., inserting
1505 * a "(". The autocommand may also require a redraw, so it's done
1506 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001507 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001508 update_screen(0);
1509# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001510# ifdef FEAT_AUTOCMD
1511 if (has_cursormovedI())
1512 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1513# endif
1514# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001515 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001516 {
1517 conceal_old_cursor_line = last_cursormoved.lnum;
1518 conceal_new_cursor_line = curwin->w_cursor.lnum;
1519 conceal_update_lines = TRUE;
1520 }
1521# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001522 last_cursormoved = curwin->w_cursor;
1523 }
1524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001525 if (must_redraw)
1526 update_screen(0);
1527 else if (clear_cmdline || redraw_cmdline)
1528 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001529# if defined(FEAT_CONCEAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001530 if ((conceal_update_lines
1531 && (conceal_old_cursor_line != conceal_new_cursor_line
1532 || conceal_cursor_line(curwin)))
1533 || need_cursor_line_redraw)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001534 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001535 if (conceal_old_cursor_line != conceal_new_cursor_line)
1536 update_single_line(curwin, conceal_old_cursor_line);
1537 update_single_line(curwin, conceal_new_cursor_line == 0
1538 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001539 curwin->w_valid &= ~VALID_CROW;
1540 }
1541# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001542 showruler(FALSE);
1543 setcursor();
1544 emsg_on_display = FALSE; /* may remove error message now */
1545 }
1546}
1547
1548/*
1549 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1550 */
1551 static void
1552ins_ctrl_v()
1553{
1554 int c;
1555
1556 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001557 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001558
1559 if (redrawing() && !char_avail())
1560 edit_putchar('^', TRUE);
1561 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1562
1563#ifdef FEAT_CMDL_INFO
1564 add_to_showcmd_c(Ctrl_V);
1565#endif
1566
1567 c = get_literal();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02001568 edit_unputchar(); /* when line fits in 'columns' the '^' is at the start
1569 of the next line and will not be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001570#ifdef FEAT_CMDL_INFO
1571 clear_showcmd();
1572#endif
1573 insert_special(c, FALSE, TRUE);
1574#ifdef FEAT_RIGHTLEFT
1575 revins_chars++;
1576 revins_legal++;
1577#endif
1578}
1579
1580/*
1581 * Put a character directly onto the screen. It's not stored in a buffer.
1582 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1583 */
1584static int pc_status;
1585#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1586#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1587#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1588#define PC_STATUS_SET 3 /* pc_bytes was filled */
1589#ifdef FEAT_MBYTE
1590static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1591#else
1592static char_u pc_bytes[2]; /* saved bytes */
1593#endif
1594static int pc_attr;
1595static int pc_row;
1596static int pc_col;
1597
1598 void
1599edit_putchar(c, highlight)
1600 int c;
1601 int highlight;
1602{
1603 int attr;
1604
1605 if (ScreenLines != NULL)
1606 {
1607 update_topline(); /* just in case w_topline isn't valid */
1608 validate_cursor();
1609 if (highlight)
1610 attr = hl_attr(HLF_8);
1611 else
1612 attr = 0;
1613 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1614 pc_col = W_WINCOL(curwin);
1615#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1616 pc_status = PC_STATUS_UNSET;
1617#endif
1618#ifdef FEAT_RIGHTLEFT
1619 if (curwin->w_p_rl)
1620 {
1621 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1622# ifdef FEAT_MBYTE
1623 if (has_mbyte)
1624 {
1625 int fix_col = mb_fix_col(pc_col, pc_row);
1626
1627 if (fix_col != pc_col)
1628 {
1629 screen_putchar(' ', pc_row, fix_col, attr);
1630 --curwin->w_wcol;
1631 pc_status = PC_STATUS_RIGHT;
1632 }
1633 }
1634# endif
1635 }
1636 else
1637#endif
1638 {
1639 pc_col += curwin->w_wcol;
1640#ifdef FEAT_MBYTE
1641 if (mb_lefthalve(pc_row, pc_col))
1642 pc_status = PC_STATUS_LEFT;
1643#endif
1644 }
1645
1646 /* save the character to be able to put it back */
1647#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1648 if (pc_status == PC_STATUS_UNSET)
1649#endif
1650 {
1651 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1652 pc_status = PC_STATUS_SET;
1653 }
1654 screen_putchar(c, pc_row, pc_col, attr);
1655 }
1656}
1657
1658/*
1659 * Undo the previous edit_putchar().
1660 */
1661 void
1662edit_unputchar()
1663{
1664 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1665 {
1666#if defined(FEAT_MBYTE)
1667 if (pc_status == PC_STATUS_RIGHT)
1668 ++curwin->w_wcol;
1669 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1670 redrawWinline(curwin->w_cursor.lnum, FALSE);
1671 else
1672#endif
1673 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1674 }
1675}
1676
1677/*
1678 * Called when p_dollar is set: display a '$' at the end of the changed text
1679 * Only works when cursor is in the line that changes.
1680 */
1681 void
1682display_dollar(col)
1683 colnr_T col;
1684{
1685 colnr_T save_col;
1686
1687 if (!redrawing())
1688 return;
1689
1690 cursor_off();
1691 save_col = curwin->w_cursor.col;
1692 curwin->w_cursor.col = col;
1693#ifdef FEAT_MBYTE
1694 if (has_mbyte)
1695 {
1696 char_u *p;
1697
1698 /* If on the last byte of a multi-byte move to the first byte. */
1699 p = ml_get_curline();
1700 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1701 }
1702#endif
1703 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1704 if (curwin->w_wcol < W_WIDTH(curwin))
1705 {
1706 edit_putchar('$', FALSE);
1707 dollar_vcol = curwin->w_virtcol;
1708 }
1709 curwin->w_cursor.col = save_col;
1710}
1711
1712/*
1713 * Call this function before moving the cursor from the normal insert position
1714 * in insert mode.
1715 */
1716 static void
1717undisplay_dollar()
1718{
1719 if (dollar_vcol)
1720 {
1721 dollar_vcol = 0;
1722 redrawWinline(curwin->w_cursor.lnum, FALSE);
1723 }
1724}
1725
1726/*
1727 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1728 * Keep the cursor on the same character.
1729 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1730 * type == INDENT_DEC decrease indent (for CTRL-D)
1731 * type == INDENT_SET set indent to "amount"
1732 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1733 */
1734 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001735change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001736 int type;
1737 int amount;
1738 int round;
1739 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001740 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001741{
1742 int vcol;
1743 int last_vcol;
1744 int insstart_less; /* reduction for Insstart.col */
1745 int new_cursor_col;
1746 int i;
1747 char_u *ptr;
1748 int save_p_list;
1749 int start_col;
1750 colnr_T vc;
1751#ifdef FEAT_VREPLACE
1752 colnr_T orig_col = 0; /* init for GCC */
1753 char_u *new_line, *orig_line = NULL; /* init for GCC */
1754
1755 /* VREPLACE mode needs to know what the line was like before changing */
1756 if (State & VREPLACE_FLAG)
1757 {
1758 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1759 orig_col = curwin->w_cursor.col;
1760 }
1761#endif
1762
1763 /* for the following tricks we don't want list mode */
1764 save_p_list = curwin->w_p_list;
1765 curwin->w_p_list = FALSE;
1766 vc = getvcol_nolist(&curwin->w_cursor);
1767 vcol = vc;
1768
1769 /*
1770 * For Replace mode we need to fix the replace stack later, which is only
1771 * possible when the cursor is in the indent. Remember the number of
1772 * characters before the cursor if it's possible.
1773 */
1774 start_col = curwin->w_cursor.col;
1775
1776 /* determine offset from first non-blank */
1777 new_cursor_col = curwin->w_cursor.col;
1778 beginline(BL_WHITE);
1779 new_cursor_col -= curwin->w_cursor.col;
1780
1781 insstart_less = curwin->w_cursor.col;
1782
1783 /*
1784 * If the cursor is in the indent, compute how many screen columns the
1785 * cursor is to the left of the first non-blank.
1786 */
1787 if (new_cursor_col < 0)
1788 vcol = get_indent() - vcol;
1789
1790 if (new_cursor_col > 0) /* can't fix replace stack */
1791 start_col = -1;
1792
1793 /*
1794 * Set the new indent. The cursor will be put on the first non-blank.
1795 */
1796 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001797 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001798 else
1799 {
1800#ifdef FEAT_VREPLACE
1801 int save_State = State;
1802
1803 /* Avoid being called recursively. */
1804 if (State & VREPLACE_FLAG)
1805 State = INSERT;
1806#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001807 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001808#ifdef FEAT_VREPLACE
1809 State = save_State;
1810#endif
1811 }
1812 insstart_less -= curwin->w_cursor.col;
1813
1814 /*
1815 * Try to put cursor on same character.
1816 * If the cursor is at or after the first non-blank in the line,
1817 * compute the cursor column relative to the column of the first
1818 * non-blank character.
1819 * If we are not in insert mode, leave the cursor on the first non-blank.
1820 * If the cursor is before the first non-blank, position it relative
1821 * to the first non-blank, counted in screen columns.
1822 */
1823 if (new_cursor_col >= 0)
1824 {
1825 /*
1826 * When changing the indent while the cursor is touching it, reset
1827 * Insstart_col to 0.
1828 */
1829 if (new_cursor_col == 0)
1830 insstart_less = MAXCOL;
1831 new_cursor_col += curwin->w_cursor.col;
1832 }
1833 else if (!(State & INSERT))
1834 new_cursor_col = curwin->w_cursor.col;
1835 else
1836 {
1837 /*
1838 * Compute the screen column where the cursor should be.
1839 */
1840 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001841 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842
1843 /*
1844 * Advance the cursor until we reach the right screen column.
1845 */
1846 vcol = last_vcol = 0;
1847 new_cursor_col = -1;
1848 ptr = ml_get_curline();
1849 while (vcol <= (int)curwin->w_virtcol)
1850 {
1851 last_vcol = vcol;
1852#ifdef FEAT_MBYTE
1853 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001854 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 else
1856#endif
1857 ++new_cursor_col;
1858 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1859 }
1860 vcol = last_vcol;
1861
1862 /*
1863 * May need to insert spaces to be able to position the cursor on
1864 * the right screen column.
1865 */
1866 if (vcol != (int)curwin->w_virtcol)
1867 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001868 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001870 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 if (ptr != NULL)
1872 {
1873 new_cursor_col += i;
1874 ptr[i] = NUL;
1875 while (--i >= 0)
1876 ptr[i] = ' ';
1877 ins_str(ptr);
1878 vim_free(ptr);
1879 }
1880 }
1881
1882 /*
1883 * When changing the indent while the cursor is in it, reset
1884 * Insstart_col to 0.
1885 */
1886 insstart_less = MAXCOL;
1887 }
1888
1889 curwin->w_p_list = save_p_list;
1890
1891 if (new_cursor_col <= 0)
1892 curwin->w_cursor.col = 0;
1893 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001894 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895 curwin->w_set_curswant = TRUE;
1896 changed_cline_bef_curs();
1897
1898 /*
1899 * May have to adjust the start of the insert.
1900 */
1901 if (State & INSERT)
1902 {
1903 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1904 {
1905 if ((int)Insstart.col <= insstart_less)
1906 Insstart.col = 0;
1907 else
1908 Insstart.col -= insstart_less;
1909 }
1910 if ((int)ai_col <= insstart_less)
1911 ai_col = 0;
1912 else
1913 ai_col -= insstart_less;
1914 }
1915
1916 /*
1917 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1918 * If the number of characters before the cursor decreased, need to pop a
1919 * few characters from the replace stack.
1920 * If the number of characters before the cursor increased, need to push a
1921 * few NULs onto the replace stack.
1922 */
1923 if (REPLACE_NORMAL(State) && start_col >= 0)
1924 {
1925 while (start_col > (int)curwin->w_cursor.col)
1926 {
1927 replace_join(0); /* remove a NUL from the replace stack */
1928 --start_col;
1929 }
1930 while (start_col < (int)curwin->w_cursor.col || replaced)
1931 {
1932 replace_push(NUL);
1933 if (replaced)
1934 {
1935 replace_push(replaced);
1936 replaced = NUL;
1937 }
1938 ++start_col;
1939 }
1940 }
1941
1942#ifdef FEAT_VREPLACE
1943 /*
1944 * For VREPLACE mode, we also have to fix the replace stack. In this case
1945 * it is always possible because we backspace over the whole line and then
1946 * put it back again the way we wanted it.
1947 */
1948 if (State & VREPLACE_FLAG)
1949 {
1950 /* If orig_line didn't allocate, just return. At least we did the job,
1951 * even if you can't backspace. */
1952 if (orig_line == NULL)
1953 return;
1954
1955 /* Save new line */
1956 new_line = vim_strsave(ml_get_curline());
1957 if (new_line == NULL)
1958 return;
1959
1960 /* We only put back the new line up to the cursor */
1961 new_line[curwin->w_cursor.col] = NUL;
1962
1963 /* Put back original line */
1964 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1965 curwin->w_cursor.col = orig_col;
1966
1967 /* Backspace from cursor to start of line */
1968 backspace_until_column(0);
1969
1970 /* Insert new stuff into line again */
1971 ins_bytes(new_line);
1972
1973 vim_free(new_line);
1974 }
1975#endif
1976}
1977
1978/*
1979 * Truncate the space at the end of a line. This is to be used only in an
1980 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1981 * modes.
1982 */
1983 void
1984truncate_spaces(line)
1985 char_u *line;
1986{
1987 int i;
1988
1989 /* find start of trailing white space */
1990 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1991 {
1992 if (State & REPLACE_FLAG)
1993 replace_join(0); /* remove a NUL from the replace stack */
1994 }
1995 line[i + 1] = NUL;
1996}
1997
1998#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
1999 || defined(FEAT_COMMENTS) || defined(PROTO)
2000/*
2001 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2002 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002003 * Will attempt not to go before "col" even when there is a composing
2004 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002005 */
2006 void
2007backspace_until_column(col)
2008 int col;
2009{
2010 while ((int)curwin->w_cursor.col > col)
2011 {
2012 curwin->w_cursor.col--;
2013 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002014 replace_do_bs(col);
2015 else if (!del_char_after_col(col))
2016 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002017 }
2018}
2019#endif
2020
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002021/*
2022 * Like del_char(), but make sure not to go before column "limit_col".
2023 * Only matters when there are composing characters.
2024 * Return TRUE when something was deleted.
2025 */
2026 static int
2027del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002028 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002029{
2030#ifdef FEAT_MBYTE
2031 if (enc_utf8 && limit_col >= 0)
2032 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002033 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002034
2035 /* Make sure the cursor is at the start of a character, but
2036 * skip forward again when going too far back because of a
2037 * composing character. */
2038 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002039 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002040 {
2041 int l = utf_ptr2len(ml_get_cursor());
2042
2043 if (l == 0) /* end of line */
2044 break;
2045 curwin->w_cursor.col += l;
2046 }
2047 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2048 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002049 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002050 }
2051 else
2052#endif
2053 (void)del_char(FALSE);
2054 return TRUE;
2055}
2056
Bram Moolenaar071d4272004-06-13 20:20:40 +00002057#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2058/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002059 * CTRL-X pressed in Insert mode.
2060 */
2061 static void
2062ins_ctrl_x()
2063{
2064 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2065 * CTRL-V works like CTRL-N */
2066 if (ctrl_x_mode != CTRL_X_CMDLINE)
2067 {
2068 /* if the next ^X<> won't ADD nothing, then reset
2069 * compl_cont_status */
2070 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002071 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002072 else
2073 compl_cont_status = 0;
2074 /* We're not sure which CTRL-X mode it will be yet */
2075 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2076 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2077 edit_submode_pre = NULL;
2078 showmode();
2079 }
2080}
2081
2082/*
2083 * Return TRUE if the 'dict' or 'tsr' option can be used.
2084 */
2085 static int
2086has_compl_option(dict_opt)
2087 int dict_opt;
2088{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002089 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002090# ifdef FEAT_SPELL
2091 && !curwin->w_p_spell
2092# endif
2093 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002094 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2095 {
2096 ctrl_x_mode = 0;
2097 edit_submode = NULL;
2098 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2099 : (char_u *)_("'thesaurus' option is empty"),
2100 hl_attr(HLF_E));
2101 if (emsg_silent == 0)
2102 {
2103 vim_beep();
2104 setcursor();
2105 out_flush();
2106 ui_delay(2000L, FALSE);
2107 }
2108 return FALSE;
2109 }
2110 return TRUE;
2111}
2112
2113/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002114 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2115 * This depends on the current mode.
2116 */
2117 int
2118vim_is_ctrl_x_key(c)
2119 int c;
2120{
2121 /* Always allow ^R - let it's results then be checked */
2122 if (c == Ctrl_R)
2123 return TRUE;
2124
Bram Moolenaare3226be2005-12-18 22:10:00 +00002125 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002126 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002127 return TRUE;
2128
Bram Moolenaar071d4272004-06-13 20:20:40 +00002129 switch (ctrl_x_mode)
2130 {
2131 case 0: /* Not in any CTRL-X mode */
2132 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2133 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002134 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002135 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2136 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2137 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002138 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2139 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 case CTRL_X_SCROLL:
2141 return (c == Ctrl_Y || c == Ctrl_E);
2142 case CTRL_X_WHOLE_LINE:
2143 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2144 case CTRL_X_FILES:
2145 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2146 case CTRL_X_DICTIONARY:
2147 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2148 case CTRL_X_THESAURUS:
2149 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2150 case CTRL_X_TAGS:
2151 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2152#ifdef FEAT_FIND_ID
2153 case CTRL_X_PATH_PATTERNS:
2154 return (c == Ctrl_P || c == Ctrl_N);
2155 case CTRL_X_PATH_DEFINES:
2156 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2157#endif
2158 case CTRL_X_CMDLINE:
2159 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2160 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002161#ifdef FEAT_COMPL_FUNC
2162 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002163 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002164 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002165 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002166#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002167 case CTRL_X_SPELL:
2168 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002169 }
2170 EMSG(_(e_internal));
2171 return FALSE;
2172}
2173
2174/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002175 * Return TRUE when character "c" is part of the item currently being
2176 * completed. Used to decide whether to abandon complete mode when the menu
2177 * is visible.
2178 */
2179 static int
2180ins_compl_accept_char(c)
2181 int c;
2182{
2183 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2184 /* When expanding an identifier only accept identifier chars. */
2185 return vim_isIDc(c);
2186
2187 switch (ctrl_x_mode)
2188 {
2189 case CTRL_X_FILES:
2190 /* When expanding file name only accept file name chars. But not
2191 * path separators, so that "proto/<Tab>" expands files in
2192 * "proto", not "proto/" as a whole */
2193 return vim_isfilec(c) && !vim_ispathsep(c);
2194
2195 case CTRL_X_CMDLINE:
2196 case CTRL_X_OMNI:
2197 /* Command line and Omni completion can work with just about any
2198 * printable character, but do stop at white space. */
2199 return vim_isprintc(c) && !vim_iswhite(c);
2200
2201 case CTRL_X_WHOLE_LINE:
2202 /* For while line completion a space can be part of the line. */
2203 return vim_isprintc(c);
2204 }
2205 return vim_iswordc(c);
2206}
2207
2208/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002209 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002211 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002212 * the rest of the word to be in -- webb
2213 */
2214 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002215ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002216 char_u *str;
2217 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002218 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002219 char_u *fname;
2220 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002221 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002222{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002223 char_u *p;
2224 int i, c;
2225 int actual_len; /* Take multi-byte characters */
2226 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002227 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002228 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002229 int has_lower = FALSE;
2230 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002231
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002232 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002233 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002234 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002235
Bram Moolenaara2993e12007-08-12 14:38:46 +00002236 /* Find actual length of completion. */
2237#ifdef FEAT_MBYTE
2238 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002239 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002240 p = str;
2241 actual_len = 0;
2242 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002243 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002244 mb_ptr_adv(p);
2245 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002246 }
2247 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002248 else
2249#endif
2250 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002251
Bram Moolenaara2993e12007-08-12 14:38:46 +00002252 /* Find actual length of original text. */
2253#ifdef FEAT_MBYTE
2254 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002256 p = compl_orig_text;
2257 actual_compl_length = 0;
2258 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002259 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002260 mb_ptr_adv(p);
2261 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 }
2263 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002264 else
2265#endif
2266 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002267
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002268 /* "actual_len" may be smaller than "actual_compl_length" when using
2269 * thesaurus, only use the minimum when comparing. */
2270 min_len = actual_len < actual_compl_length
2271 ? actual_len : actual_compl_length;
2272
Bram Moolenaara2993e12007-08-12 14:38:46 +00002273 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002274 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002275 if (wca != NULL)
2276 {
2277 p = str;
2278 for (i = 0; i < actual_len; ++i)
2279#ifdef FEAT_MBYTE
2280 if (has_mbyte)
2281 wca[i] = mb_ptr2char_adv(&p);
2282 else
2283#endif
2284 wca[i] = *(p++);
2285
2286 /* Rule 1: Were any chars converted to lower? */
2287 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002288 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002289 {
2290#ifdef FEAT_MBYTE
2291 if (has_mbyte)
2292 c = mb_ptr2char_adv(&p);
2293 else
2294#endif
2295 c = *(p++);
2296 if (MB_ISLOWER(c))
2297 {
2298 has_lower = TRUE;
2299 if (MB_ISUPPER(wca[i]))
2300 {
2301 /* Rule 1 is satisfied. */
2302 for (i = actual_compl_length; i < actual_len; ++i)
2303 wca[i] = MB_TOLOWER(wca[i]);
2304 break;
2305 }
2306 }
2307 }
2308
2309 /*
2310 * Rule 2: No lower case, 2nd consecutive letter converted to
2311 * upper case.
2312 */
2313 if (!has_lower)
2314 {
2315 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002316 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002317 {
2318#ifdef FEAT_MBYTE
2319 if (has_mbyte)
2320 c = mb_ptr2char_adv(&p);
2321 else
2322#endif
2323 c = *(p++);
2324 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2325 {
2326 /* Rule 2 is satisfied. */
2327 for (i = actual_compl_length; i < actual_len; ++i)
2328 wca[i] = MB_TOUPPER(wca[i]);
2329 break;
2330 }
2331 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2332 }
2333 }
2334
2335 /* Copy the original case of the part we typed. */
2336 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002337 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002338 {
2339#ifdef FEAT_MBYTE
2340 if (has_mbyte)
2341 c = mb_ptr2char_adv(&p);
2342 else
2343#endif
2344 c = *(p++);
2345 if (MB_ISLOWER(c))
2346 wca[i] = MB_TOLOWER(wca[i]);
2347 else if (MB_ISUPPER(c))
2348 wca[i] = MB_TOUPPER(wca[i]);
2349 }
2350
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002351 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002352 * Generate encoding specific output from wide character array.
2353 * Multi-byte characters can occupy up to five bytes more than
2354 * ASCII characters, and we also need one byte for NUL, so stay
2355 * six bytes away from the edge of IObuff.
2356 */
2357 p = IObuff;
2358 i = 0;
2359 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2360#ifdef FEAT_MBYTE
2361 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002362 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002363 else
2364#endif
2365 *(p++) = wca[i++];
2366 *p = NUL;
2367
2368 vim_free(wca);
2369 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002370
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002371 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2372 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002373 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002374 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002375}
2376
2377/*
2378 * Add a match to the list of matches.
2379 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002380 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002381 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002382 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002383 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002384ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002385 char_u *str;
2386 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002387 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002388 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002389 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002390 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002391 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002392 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002393{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002394 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002395 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002396
2397 ui_breakcheck();
2398 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002399 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002400 if (len < 0)
2401 len = (int)STRLEN(str);
2402
2403 /*
2404 * If the same match is already present, don't add it.
2405 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002406 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002407 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002408 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002409 do
2410 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002411 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002412 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002413 && match->cp_str[len] == NUL)
2414 return NOTDONE;
2415 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002416 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002417 }
2418
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002419 /* Remove any popup menu before changing the list of matches. */
2420 ins_compl_del_pum();
2421
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 /*
2423 * Allocate a new match structure.
2424 * Copy the values to the new match structure.
2425 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002426 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002427 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002428 return FAIL;
2429 match->cp_number = -1;
2430 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002431 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002432 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002433 {
2434 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002435 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002437 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002438
Bram Moolenaar071d4272004-06-13 20:20:40 +00002439 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002440 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002441 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2442 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002443 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002444 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002445 && compl_curr_match->cp_fname != NULL
2446 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002447 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002448 else if (fname != NULL)
2449 {
2450 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002451 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002452 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002453 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002454 match->cp_fname = NULL;
2455 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002456
2457 if (cptext != NULL)
2458 {
2459 int i;
2460
2461 for (i = 0; i < CPT_COUNT; ++i)
2462 if (cptext[i] != NULL && *cptext[i] != NUL)
2463 match->cp_text[i] = vim_strsave(cptext[i]);
2464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 /*
2467 * Link the new match structure in the list of matches.
2468 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002469 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002470 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002471 else if (dir == FORWARD)
2472 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002473 match->cp_next = compl_curr_match->cp_next;
2474 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 }
2476 else /* BACKWARD */
2477 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002478 match->cp_next = compl_curr_match;
2479 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002480 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002481 if (match->cp_next)
2482 match->cp_next->cp_prev = match;
2483 if (match->cp_prev)
2484 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002486 compl_first_match = match;
2487 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002488
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002489 /*
2490 * Find the longest common string if still doing that.
2491 */
2492 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2493 ins_compl_longest_match(match);
2494
Bram Moolenaar071d4272004-06-13 20:20:40 +00002495 return OK;
2496}
2497
2498/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002499 * Return TRUE if "str[len]" matches with match->cp_str, considering
2500 * match->cp_icase.
2501 */
2502 static int
2503ins_compl_equal(match, str, len)
2504 compl_T *match;
2505 char_u *str;
2506 int len;
2507{
2508 if (match->cp_icase)
2509 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2510 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2511}
2512
2513/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002514 * Reduce the longest common string for match "match".
2515 */
2516 static void
2517ins_compl_longest_match(match)
2518 compl_T *match;
2519{
2520 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002521 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002522 int had_match;
2523
2524 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002525 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002526 /* First match, use it as a whole. */
2527 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002528 if (compl_leader != NULL)
2529 {
2530 had_match = (curwin->w_cursor.col > compl_col);
2531 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002532 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002533 ins_redraw(FALSE);
2534
2535 /* When the match isn't there (to avoid matching itself) remove it
2536 * again after redrawing. */
2537 if (!had_match)
2538 ins_compl_delete();
2539 compl_used_match = FALSE;
2540 }
2541 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002542 else
2543 {
2544 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002545 p = compl_leader;
2546 s = match->cp_str;
2547 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002548 {
2549#ifdef FEAT_MBYTE
2550 if (has_mbyte)
2551 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002552 c1 = mb_ptr2char(p);
2553 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002554 }
2555 else
2556#endif
2557 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002558 c1 = *p;
2559 c2 = *s;
2560 }
2561 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2562 : (c1 != c2))
2563 break;
2564#ifdef FEAT_MBYTE
2565 if (has_mbyte)
2566 {
2567 mb_ptr_adv(p);
2568 mb_ptr_adv(s);
2569 }
2570 else
2571#endif
2572 {
2573 ++p;
2574 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002575 }
2576 }
2577
2578 if (*p != NUL)
2579 {
2580 /* Leader was shortened, need to change the inserted text. */
2581 *p = NUL;
2582 had_match = (curwin->w_cursor.col > compl_col);
2583 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002584 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002585 ins_redraw(FALSE);
2586
2587 /* When the match isn't there (to avoid matching itself) remove it
2588 * again after redrawing. */
2589 if (!had_match)
2590 ins_compl_delete();
2591 }
2592
2593 compl_used_match = FALSE;
2594 }
2595}
2596
2597/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 * Add an array of matches to the list of matches.
2599 * Frees matches[].
2600 */
2601 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002602ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002603 int num_matches;
2604 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002605 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002606{
2607 int i;
2608 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002609 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002610
Bram Moolenaar572cb562005-08-05 21:35:02 +00002611 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002612 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002613 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002614 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002615 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002616 FreeWild(num_matches, matches);
2617}
2618
2619/* Make the completion list cyclic.
2620 * Return the number of matches (excluding the original).
2621 */
2622 static int
2623ins_compl_make_cyclic()
2624{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002625 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 int count = 0;
2627
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002628 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002629 {
2630 /*
2631 * Find the end of the list.
2632 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002633 match = compl_first_match;
2634 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002635 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002636 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002637 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002638 ++count;
2639 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002640 match->cp_next = compl_first_match;
2641 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002642 }
2643 return count;
2644}
2645
Bram Moolenaara94bc432006-03-10 21:42:59 +00002646/*
2647 * Start completion for the complete() function.
2648 * "startcol" is where the matched text starts (1 is first column).
2649 * "list" is the list of matches.
2650 */
2651 void
2652set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002653 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002654 list_T *list;
2655{
2656 /* If already doing completions stop it. */
2657 if (ctrl_x_mode != 0)
2658 ins_compl_prep(' ');
2659 ins_compl_clear();
2660
2661 if (stop_arrow() == FAIL)
2662 return;
2663
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002664 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002665 startcol = curwin->w_cursor.col;
2666 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002667 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002668 /* compl_pattern doesn't need to be set */
2669 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2670 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002671 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002672 return;
2673
2674 /* Handle like dictionary completion. */
2675 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2676
2677 ins_compl_add_list(list);
2678 compl_matches = ins_compl_make_cyclic();
2679 compl_started = TRUE;
2680 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002681 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002682
2683 compl_curr_match = compl_first_match;
2684 ins_complete(Ctrl_N);
2685 out_flush();
2686}
2687
2688
Bram Moolenaar9372a112005-12-06 19:59:18 +00002689/* "compl_match_array" points the currently displayed list of entries in the
2690 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002691static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002692static int compl_match_arraysize;
2693
2694/*
2695 * Update the screen and when there is any scrolling remove the popup menu.
2696 */
2697 static void
2698ins_compl_upd_pum()
2699{
2700 int h;
2701
2702 if (compl_match_array != NULL)
2703 {
2704 h = curwin->w_cline_height;
2705 update_screen(0);
2706 if (h != curwin->w_cline_height)
2707 ins_compl_del_pum();
2708 }
2709}
2710
2711/*
2712 * Remove any popup menu.
2713 */
2714 static void
2715ins_compl_del_pum()
2716{
2717 if (compl_match_array != NULL)
2718 {
2719 pum_undisplay();
2720 vim_free(compl_match_array);
2721 compl_match_array = NULL;
2722 }
2723}
2724
2725/*
2726 * Return TRUE if the popup menu should be displayed.
2727 */
2728 static int
2729pum_wanted()
2730{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002731 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002732 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002733 return FALSE;
2734
2735 /* The display looks bad on a B&W display. */
2736 if (t_colors < 8
2737#ifdef FEAT_GUI
2738 && !gui.in_use
2739#endif
2740 )
2741 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002742 return TRUE;
2743}
2744
2745/*
2746 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002747 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002748 */
2749 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002750pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002751{
2752 compl_T *compl;
2753 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002754
2755 /* Don't display the popup menu if there are no matches or there is only
2756 * one (ignoring the original text). */
2757 compl = compl_first_match;
2758 i = 0;
2759 do
2760 {
2761 if (compl == NULL
2762 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2763 break;
2764 compl = compl->cp_next;
2765 } while (compl != compl_first_match);
2766
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002767 if (strstr((char *)p_cot, "menuone") != NULL)
2768 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002769 return (i >= 2);
2770}
2771
2772/*
2773 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002774 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002775 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002776 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002777ins_compl_show_pum()
2778{
2779 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002780 compl_T *shown_compl = NULL;
2781 int did_find_shown_match = FALSE;
2782 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002783 int i;
2784 int cur = -1;
2785 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002786 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002787
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002788 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002789 return;
2790
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002791#if defined(FEAT_EVAL)
2792 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2793 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2794#endif
2795
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002796 /* Update the screen before drawing the popup menu over it. */
2797 update_screen(0);
2798
2799 if (compl_match_array == NULL)
2800 {
2801 /* Need to build the popup menu list. */
2802 compl_match_arraysize = 0;
2803 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002804 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002805 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002806 do
2807 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002808 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2809 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002810 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002811 ++compl_match_arraysize;
2812 compl = compl->cp_next;
2813 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002814 if (compl_match_arraysize == 0)
2815 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002816 compl_match_array = (pumitem_T *)alloc_clear(
2817 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002818 * compl_match_arraysize));
2819 if (compl_match_array != NULL)
2820 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002821 /* If the current match is the original text don't find the first
2822 * match after it, don't highlight anything. */
2823 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2824 shown_match_ok = TRUE;
2825
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002826 i = 0;
2827 compl = compl_first_match;
2828 do
2829 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002830 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2831 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002832 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002833 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002834 if (!shown_match_ok)
2835 {
2836 if (compl == compl_shown_match || did_find_shown_match)
2837 {
2838 /* This item is the shown match or this is the
2839 * first displayed item after the shown match. */
2840 compl_shown_match = compl;
2841 did_find_shown_match = TRUE;
2842 shown_match_ok = TRUE;
2843 }
2844 else
2845 /* Remember this displayed match for when the
2846 * shown match is just below it. */
2847 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002848 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002849 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002850
2851 if (compl->cp_text[CPT_ABBR] != NULL)
2852 compl_match_array[i].pum_text =
2853 compl->cp_text[CPT_ABBR];
2854 else
2855 compl_match_array[i].pum_text = compl->cp_str;
2856 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2857 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2858 if (compl->cp_text[CPT_MENU] != NULL)
2859 compl_match_array[i++].pum_extra =
2860 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002861 else
2862 compl_match_array[i++].pum_extra = compl->cp_fname;
2863 }
2864
2865 if (compl == compl_shown_match)
2866 {
2867 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002868
2869 /* When the original text is the shown match don't set
2870 * compl_shown_match. */
2871 if (compl->cp_flags & ORIGINAL_TEXT)
2872 shown_match_ok = TRUE;
2873
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002874 if (!shown_match_ok && shown_compl != NULL)
2875 {
2876 /* The shown match isn't displayed, set it to the
2877 * previously displayed match. */
2878 compl_shown_match = shown_compl;
2879 shown_match_ok = TRUE;
2880 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002881 }
2882 compl = compl->cp_next;
2883 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002884
2885 if (!shown_match_ok) /* no displayed match at all */
2886 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002887 }
2888 }
2889 else
2890 {
2891 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002892 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002893 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2894 || compl_match_array[i].pum_text
2895 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002896 {
2897 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002898 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002899 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002900 }
2901
2902 if (compl_match_array != NULL)
2903 {
2904 /* Compute the screen column of the start of the completed text.
2905 * Use the cursor to get all wrapping and other settings right. */
2906 col = curwin->w_cursor.col;
2907 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002908 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002909 curwin->w_cursor.col = col;
2910 }
2911}
2912
Bram Moolenaar071d4272004-06-13 20:20:40 +00002913#define DICT_FIRST (1) /* use just first element in "dict" */
2914#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002915
Bram Moolenaar071d4272004-06-13 20:20:40 +00002916/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002917 * Add any identifiers that match the given pattern in the list of dictionary
2918 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002919 */
2920 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002921ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2922 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002923 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002924 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002925 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002926{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002927 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928 char_u *ptr;
2929 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002931 char_u **files;
2932 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002934 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935
Bram Moolenaar0b238792006-03-02 22:49:12 +00002936 if (*dict == NUL)
2937 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002938#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002939 /* When 'dictionary' is empty and spell checking is enabled use
2940 * "spell". */
2941 if (!thesaurus && curwin->w_p_spell)
2942 dict = (char_u *)"spell";
2943 else
2944#endif
2945 return;
2946 }
2947
Bram Moolenaar071d4272004-06-13 20:20:40 +00002948 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002949 if (buf == NULL)
2950 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002951 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002952
Bram Moolenaar071d4272004-06-13 20:20:40 +00002953 /* If 'infercase' is set, don't use 'smartcase' here */
2954 save_p_scs = p_scs;
2955 if (curbuf->b_p_inf)
2956 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002957
2958 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2959 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002960 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002961 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2962 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002963 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002964 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002965
2966 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002967 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002968 len = STRLEN(pat_esc) + 10;
2969 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002970 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002971 {
2972 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002973 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002974 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002975 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002976 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2977 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002978 vim_free(ptr);
2979 }
2980 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002981 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002982 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002983 if (regmatch.regprog == NULL)
2984 goto theend;
2985 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002986
Bram Moolenaar071d4272004-06-13 20:20:40 +00002987 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2988 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002989 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002990 {
2991 /* copy one dictionary file name into buf */
2992 if (flags == DICT_EXACT)
2993 {
2994 count = 1;
2995 files = &dict;
2996 }
2997 else
2998 {
2999 /* Expand wildcards in the dictionary name, but do not allow
3000 * backticks (for security, the 'dict' option may have been set in
3001 * a modeline). */
3002 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003003# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003004 if (!thesaurus && STRCMP(buf, "spell") == 0)
3005 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003006 else
3007# endif
3008 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003009 || expand_wildcards(1, &buf, &count, &files,
3010 EW_FILE|EW_SILENT) != OK)
3011 count = 0;
3012 }
3013
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003014# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003015 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003016 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003017 /* Complete from active spelling. Skip "\<" in the pattern, we
3018 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003019 if (pat[0] == '\\' && pat[1] == '<')
3020 ptr = pat + 2;
3021 else
3022 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003023 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003024 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003025 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003026# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003027 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003028 {
3029 ins_compl_files(count, files, thesaurus, flags,
3030 &regmatch, buf, &dir);
3031 if (flags != DICT_EXACT)
3032 FreeWild(count, files);
3033 }
3034 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003035 break;
3036 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003037
3038theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003039 p_scs = save_p_scs;
3040 vim_free(regmatch.regprog);
3041 vim_free(buf);
3042}
3043
Bram Moolenaar0b238792006-03-02 22:49:12 +00003044 static void
3045ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3046 int count;
3047 char_u **files;
3048 int thesaurus;
3049 int flags;
3050 regmatch_T *regmatch;
3051 char_u *buf;
3052 int *dir;
3053{
3054 char_u *ptr;
3055 int i;
3056 FILE *fp;
3057 int add_r;
3058
3059 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3060 {
3061 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3062 if (flags != DICT_EXACT)
3063 {
3064 vim_snprintf((char *)IObuff, IOSIZE,
3065 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003066 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003067 }
3068
3069 if (fp != NULL)
3070 {
3071 /*
3072 * Read dictionary file line by line.
3073 * Check each line for a match.
3074 */
3075 while (!got_int && !compl_interrupted
3076 && !vim_fgets(buf, LSIZE, fp))
3077 {
3078 ptr = buf;
3079 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3080 {
3081 ptr = regmatch->startp[0];
3082 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3083 ptr = find_line_end(ptr);
3084 else
3085 ptr = find_word_end(ptr);
3086 add_r = ins_compl_add_infercase(regmatch->startp[0],
3087 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003088 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003089 if (thesaurus)
3090 {
3091 char_u *wstart;
3092
3093 /*
3094 * Add the other matches on the line
3095 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003096 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003097 while (!got_int)
3098 {
3099 /* Find start of the next word. Skip white
3100 * space and punctuation. */
3101 ptr = find_word_start(ptr);
3102 if (*ptr == NUL || *ptr == NL)
3103 break;
3104 wstart = ptr;
3105
Bram Moolenaara2993e12007-08-12 14:38:46 +00003106 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003107#ifdef FEAT_MBYTE
3108 if (has_mbyte)
3109 /* Japanese words may have characters in
3110 * different classes, only separate words
3111 * with single-byte non-word characters. */
3112 while (*ptr != NUL)
3113 {
3114 int l = (*mb_ptr2len)(ptr);
3115
3116 if (l < 2 && !vim_iswordc(*ptr))
3117 break;
3118 ptr += l;
3119 }
3120 else
3121#endif
3122 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003123
3124 /* Add the word. Skip the regexp match. */
3125 if (wstart != regmatch->startp[0])
3126 add_r = ins_compl_add_infercase(wstart,
3127 (int)(ptr - wstart),
3128 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003129 }
3130 }
3131 if (add_r == OK)
3132 /* if dir was BACKWARD then honor it just once */
3133 *dir = FORWARD;
3134 else if (add_r == FAIL)
3135 break;
3136 /* avoid expensive call to vim_regexec() when at end
3137 * of line */
3138 if (*ptr == '\n' || got_int)
3139 break;
3140 }
3141 line_breakcheck();
3142 ins_compl_check_keys(50);
3143 }
3144 fclose(fp);
3145 }
3146 }
3147}
3148
Bram Moolenaar071d4272004-06-13 20:20:40 +00003149/*
3150 * Find the start of the next word.
3151 * Returns a pointer to the first char of the word. Also stops at a NUL.
3152 */
3153 char_u *
3154find_word_start(ptr)
3155 char_u *ptr;
3156{
3157#ifdef FEAT_MBYTE
3158 if (has_mbyte)
3159 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003160 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 else
3162#endif
3163 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3164 ++ptr;
3165 return ptr;
3166}
3167
3168/*
3169 * Find the end of the word. Assumes it starts inside a word.
3170 * Returns a pointer to just after the word.
3171 */
3172 char_u *
3173find_word_end(ptr)
3174 char_u *ptr;
3175{
3176#ifdef FEAT_MBYTE
3177 int start_class;
3178
3179 if (has_mbyte)
3180 {
3181 start_class = mb_get_class(ptr);
3182 if (start_class > 1)
3183 while (*ptr != NUL)
3184 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003185 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003186 if (mb_get_class(ptr) != start_class)
3187 break;
3188 }
3189 }
3190 else
3191#endif
3192 while (vim_iswordc(*ptr))
3193 ++ptr;
3194 return ptr;
3195}
3196
3197/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003198 * Find the end of the line, omitting CR and NL at the end.
3199 * Returns a pointer to just after the line.
3200 */
3201 static char_u *
3202find_line_end(ptr)
3203 char_u *ptr;
3204{
3205 char_u *s;
3206
3207 s = ptr + STRLEN(ptr);
3208 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3209 --s;
3210 return s;
3211}
3212
3213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003214 * Free the list of completions
3215 */
3216 static void
3217ins_compl_free()
3218{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003219 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003220 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003221
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003222 vim_free(compl_pattern);
3223 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003224 vim_free(compl_leader);
3225 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003226
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003227 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003229
3230 ins_compl_del_pum();
3231 pum_clear();
3232
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003233 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 do
3235 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003236 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003237 compl_curr_match = compl_curr_match->cp_next;
3238 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003239 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003240 if (match->cp_flags & FREE_FNAME)
3241 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003242 for (i = 0; i < CPT_COUNT; ++i)
3243 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003244 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003245 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3246 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003247 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248}
3249
3250 static void
3251ins_compl_clear()
3252{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003253 compl_cont_status = 0;
3254 compl_started = FALSE;
3255 compl_matches = 0;
3256 vim_free(compl_pattern);
3257 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003258 vim_free(compl_leader);
3259 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003260 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003261 vim_free(compl_orig_text);
3262 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003263 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003264}
3265
3266/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003267 * Return TRUE when Insert completion is active.
3268 */
3269 int
3270ins_compl_active()
3271{
3272 return compl_started;
3273}
3274
3275/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003276 * Delete one character before the cursor and show the subset of the matches
3277 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003278 * Returns the character to be used, NUL if the work is done and another char
3279 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003280 */
3281 static int
3282ins_compl_bs()
3283{
3284 char_u *line;
3285 char_u *p;
3286
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003287 line = ml_get_curline();
3288 p = line + curwin->w_cursor.col;
3289 mb_ptr_back(line, p);
3290
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003291 /* Stop completion when the whole word was deleted. For Omni completion
3292 * allow the word to be deleted, we won't match everything. */
3293 if ((int)(p - line) - (int)compl_col < 0
3294 || ((int)(p - line) - (int)compl_col == 0
3295 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003296 return K_BS;
3297
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003298 /* Deleted more than what was used to find matches or didn't finish
3299 * finding all matches: need to look for matches all over again. */
3300 if (curwin->w_cursor.col <= compl_col + compl_length
3301 || compl_was_interrupted)
3302 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003303
Bram Moolenaara6557602006-02-04 22:43:20 +00003304 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003305 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003306 if (compl_leader != NULL)
3307 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003308 ins_compl_new_leader();
3309 return NUL;
3310 }
3311 return K_BS;
3312}
Bram Moolenaara6557602006-02-04 22:43:20 +00003313
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003314/*
3315 * Called after changing "compl_leader".
3316 * Show the popup menu with a different set of matches.
3317 * May also search for matches again if the previous search was interrupted.
3318 */
3319 static void
3320ins_compl_new_leader()
3321{
3322 ins_compl_del_pum();
3323 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003324 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003325 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003326
3327 if (compl_started)
3328 ins_compl_set_original_text(compl_leader);
3329 else
3330 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003331#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003332 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003333#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003334 /*
3335 * Matches were cleared, need to search for them now. First display
3336 * the changed text before the cursor. Set "compl_restarting" to
3337 * avoid that the first match is inserted.
3338 */
3339 update_screen(0);
3340#ifdef FEAT_GUI
3341 if (gui.in_use)
3342 {
3343 /* Show the cursor after the match, not after the redrawn text. */
3344 setcursor();
3345 out_flush();
3346 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003347 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003348#endif
3349 compl_restarting = TRUE;
3350 if (ins_complete(Ctrl_N) == FAIL)
3351 compl_cont_status = 0;
3352 compl_restarting = FALSE;
3353 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003354
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003355#if 0 /* disabled, made CTRL-L, BS and typing char jump to original text. */
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003356 if (!compl_used_match)
3357 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00003358 /* Go to the original text, since none of the matches is inserted. */
3359 if (compl_first_match->cp_prev != NULL
3360 && (compl_first_match->cp_prev->cp_flags & ORIGINAL_TEXT))
3361 compl_shown_match = compl_first_match->cp_prev;
3362 else
3363 compl_shown_match = compl_first_match;
3364 compl_curr_match = compl_shown_match;
3365 compl_shows_dir = compl_direction;
Bram Moolenaara6557602006-02-04 22:43:20 +00003366 }
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003367#endif
3368 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003369
3370 /* Show the popup menu with a different set of matches. */
3371 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003372
3373 /* Don't let Enter select the original text when there is no popup menu. */
3374 if (compl_match_array == NULL)
3375 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003376}
3377
3378/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003379 * Return the length of the completion, from the completion start column to
3380 * the cursor column. Making sure it never goes below zero.
3381 */
3382 static int
3383ins_compl_len()
3384{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003385 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003386
3387 if (off < 0)
3388 return 0;
3389 return off;
3390}
3391
3392/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003393 * Append one character to the match leader. May reduce the number of
3394 * matches.
3395 */
3396 static void
3397ins_compl_addleader(c)
3398 int c;
3399{
3400#ifdef FEAT_MBYTE
3401 int cc;
3402
3403 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3404 {
3405 char_u buf[MB_MAXBYTES + 1];
3406
3407 (*mb_char2bytes)(c, buf);
3408 buf[cc] = NUL;
3409 ins_char_bytes(buf, cc);
3410 }
3411 else
3412#endif
3413 ins_char(c);
3414
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003415 /* If we didn't complete finding matches we must search again. */
3416 if (compl_was_interrupted)
3417 ins_compl_restart();
3418
Bram Moolenaara6557602006-02-04 22:43:20 +00003419 vim_free(compl_leader);
3420 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003421 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003422 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003423 ins_compl_new_leader();
3424}
3425
3426/*
3427 * Setup for finding completions again without leaving CTRL-X mode. Used when
3428 * BS or a key was typed while still searching for matches.
3429 */
3430 static void
3431ins_compl_restart()
3432{
3433 ins_compl_free();
3434 compl_started = FALSE;
3435 compl_matches = 0;
3436 compl_cont_status = 0;
3437 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003438}
3439
3440/*
3441 * Set the first match, the original text.
3442 */
3443 static void
3444ins_compl_set_original_text(str)
3445 char_u *str;
3446{
3447 char_u *p;
3448
3449 /* Replace the original text entry. */
3450 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3451 {
3452 p = vim_strsave(str);
3453 if (p != NULL)
3454 {
3455 vim_free(compl_first_match->cp_str);
3456 compl_first_match->cp_str = p;
3457 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003458 }
3459}
3460
3461/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003462 * Append one character to the match leader. May reduce the number of
3463 * matches.
3464 */
3465 static void
3466ins_compl_addfrommatch()
3467{
3468 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003469 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003470 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003471 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003472
3473 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003474 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003475 {
3476 /* When still at the original match use the first entry that matches
3477 * the leader. */
3478 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3479 {
3480 p = NULL;
3481 for (cp = compl_shown_match->cp_next; cp != NULL
3482 && cp != compl_first_match; cp = cp->cp_next)
3483 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003484 if (compl_leader == NULL
3485 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003486 (int)STRLEN(compl_leader)))
3487 {
3488 p = cp->cp_str;
3489 break;
3490 }
3491 }
3492 if (p == NULL || (int)STRLEN(p) <= len)
3493 return;
3494 }
3495 else
3496 return;
3497 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003498 p += len;
3499#ifdef FEAT_MBYTE
3500 c = mb_ptr2char(p);
3501#else
3502 c = *p;
3503#endif
3504 ins_compl_addleader(c);
3505}
3506
3507/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003509 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003510 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003512 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003513ins_compl_prep(c)
3514 int c;
3515{
3516 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003517 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003518 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
3520 /* Forget any previous 'special' messages if this is actually
3521 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3522 */
3523 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3524 edit_submode_extra = NULL;
3525
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003526 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003527 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3528 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003529 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003531 /* Set "compl_get_longest" when finding the first matches. */
3532 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3533 || (ctrl_x_mode == 0 && !compl_started))
3534 {
3535 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3536 compl_used_match = TRUE;
3537 }
3538
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3540 {
3541 /*
3542 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3543 * it will be yet. Now we decide.
3544 */
3545 switch (c)
3546 {
3547 case Ctrl_E:
3548 case Ctrl_Y:
3549 ctrl_x_mode = CTRL_X_SCROLL;
3550 if (!(State & REPLACE_FLAG))
3551 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3552 else
3553 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3554 edit_submode_pre = NULL;
3555 showmode();
3556 break;
3557 case Ctrl_L:
3558 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3559 break;
3560 case Ctrl_F:
3561 ctrl_x_mode = CTRL_X_FILES;
3562 break;
3563 case Ctrl_K:
3564 ctrl_x_mode = CTRL_X_DICTIONARY;
3565 break;
3566 case Ctrl_R:
3567 /* Simply allow ^R to happen without affecting ^X mode */
3568 break;
3569 case Ctrl_T:
3570 ctrl_x_mode = CTRL_X_THESAURUS;
3571 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003572#ifdef FEAT_COMPL_FUNC
3573 case Ctrl_U:
3574 ctrl_x_mode = CTRL_X_FUNCTION;
3575 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003576 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003577 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003578 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003579#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003580 case 's':
3581 case Ctrl_S:
3582 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003583#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003584 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003585 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003586 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003587#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003588 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589 case Ctrl_RSB:
3590 ctrl_x_mode = CTRL_X_TAGS;
3591 break;
3592#ifdef FEAT_FIND_ID
3593 case Ctrl_I:
3594 case K_S_TAB:
3595 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3596 break;
3597 case Ctrl_D:
3598 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3599 break;
3600#endif
3601 case Ctrl_V:
3602 case Ctrl_Q:
3603 ctrl_x_mode = CTRL_X_CMDLINE;
3604 break;
3605 case Ctrl_P:
3606 case Ctrl_N:
3607 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3608 * just started ^X mode, or there were enough ^X's to cancel
3609 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3610 * do normal expansion when interrupting a different mode (say
3611 * ^X^F^X^P or ^P^X^X^P, see below)
3612 * nothing changes if interrupting mode 0, (eg, the flag
3613 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003614 if (!(compl_cont_status & CONT_INTRPT))
3615 compl_cont_status |= CONT_LOCAL;
3616 else if (compl_cont_mode != 0)
3617 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 /* FALLTHROUGH */
3619 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003620 /* If we have typed at least 2 ^X's... for modes != 0, we set
3621 * compl_cont_status = 0 (eg, as if we had just started ^X
3622 * mode).
3623 * For mode 0, we set "compl_cont_mode" to an impossible
3624 * value, in both cases ^X^X can be used to restart the same
3625 * mode (avoiding ADDING mode).
3626 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3627 * 'complete' and local ^P expansions respectively.
3628 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3629 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 if (c == Ctrl_X)
3631 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003632 if (compl_cont_mode != 0)
3633 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003634 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003635 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 }
3637 ctrl_x_mode = 0;
3638 edit_submode = NULL;
3639 showmode();
3640 break;
3641 }
3642 }
3643 else if (ctrl_x_mode != 0)
3644 {
3645 /* We're already in CTRL-X mode, do we stay in it? */
3646 if (!vim_is_ctrl_x_key(c))
3647 {
3648 if (ctrl_x_mode == CTRL_X_SCROLL)
3649 ctrl_x_mode = 0;
3650 else
3651 ctrl_x_mode = CTRL_X_FINISHED;
3652 edit_submode = NULL;
3653 }
3654 showmode();
3655 }
3656
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003657 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658 {
3659 /* Show error message from attempted keyword completion (probably
3660 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003661 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003663 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3664 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 || ctrl_x_mode == CTRL_X_FINISHED)
3666 {
3667 /* Get here when we have finished typing a sequence of ^N and
3668 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003669 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003670 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003672 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003673 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003674
Bram Moolenaar071d4272004-06-13 20:20:40 +00003675 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003676 * If any of the original typed text has been changed, eg when
3677 * ignorecase is set, we must add back-spaces to the redo
3678 * buffer. We add as few as necessary to delete just the part
3679 * of the original text that has changed.
3680 * When using the longest match, edited the match or used
3681 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003682 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003683 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3684 ptr = compl_curr_match->cp_str;
3685 else if (compl_leader != NULL)
3686 ptr = compl_leader;
3687 else
3688 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003689 if (compl_orig_text != NULL)
3690 {
3691 p = compl_orig_text;
3692 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3693 ++temp)
3694 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003695#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003696 if (temp > 0)
3697 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003698#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003699 for (p += temp; *p != NUL; mb_ptr_adv(p))
3700 AppendCharToRedobuff(K_BS);
3701 }
3702 if (ptr != NULL)
3703 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003704 }
3705
3706#ifdef FEAT_CINDENT
3707 want_cindent = (can_cindent && cindent_on());
3708#endif
3709 /*
3710 * When completing whole lines: fix indent for 'cindent'.
3711 * Otherwise, break line if it's too long.
3712 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003713 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003714 {
3715#ifdef FEAT_CINDENT
3716 /* re-indent the current line */
3717 if (want_cindent)
3718 {
3719 do_c_expr_indent();
3720 want_cindent = FALSE; /* don't do it again */
3721 }
3722#endif
3723 }
3724 else
3725 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003726 int prev_col = curwin->w_cursor.col;
3727
Bram Moolenaar071d4272004-06-13 20:20:40 +00003728 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003729 if (prev_col > 0)
3730 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731 if (stop_arrow() == OK)
3732 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003733 if (prev_col > 0
3734 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3735 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003736 }
3737
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003738 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003739 * the selection without inserting anything. When
3740 * compl_enter_selects is set the Enter key does the same. */
3741 if ((c == Ctrl_Y || (compl_enter_selects
3742 && (c == CAR || c == K_KENTER || c == NL)))
3743 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003744 retval = TRUE;
3745
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003746 /* CTRL-E means completion is Ended, go back to the typed text. */
3747 if (c == Ctrl_E)
3748 {
3749 ins_compl_delete();
3750 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003751 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003752 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003753 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003754 retval = TRUE;
3755 }
3756
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003757 auto_format(FALSE, TRUE);
3758
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003760 compl_started = FALSE;
3761 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 msg_clr_cmdline(); /* necessary for "noshowmode" */
3763 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003764 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003765 if (edit_submode != NULL)
3766 {
3767 edit_submode = NULL;
3768 showmode();
3769 }
3770
3771#ifdef FEAT_CINDENT
3772 /*
3773 * Indent now if a key was typed that is in 'cinkeys'.
3774 */
3775 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3776 do_c_expr_indent();
3777#endif
3778 }
3779 }
3780
3781 /* reset continue_* if we left expansion-mode, if we stay they'll be
3782 * (re)set properly in ins_complete() */
3783 if (!vim_is_ctrl_x_key(c))
3784 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003785 compl_cont_status = 0;
3786 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003787 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003788
3789 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790}
3791
3792/*
3793 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3794 * (depending on flag) starting from buf and looking for a non-scanned
3795 * buffer (other than curbuf). curbuf is special, if it is called with
3796 * buf=curbuf then it has to be the first call for a given flag/expansion.
3797 *
3798 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3799 */
3800 static buf_T *
3801ins_compl_next_buf(buf, flag)
3802 buf_T *buf;
3803 int flag;
3804{
3805#ifdef FEAT_WINDOWS
3806 static win_T *wp;
3807#endif
3808
3809 if (flag == 'w') /* just windows */
3810 {
3811#ifdef FEAT_WINDOWS
3812 if (buf == curbuf) /* first call for this flag/expansion */
3813 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003814 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003815 && wp->w_buffer->b_scanned)
3816 ;
3817 buf = wp->w_buffer;
3818#else
3819 buf = curbuf;
3820#endif
3821 }
3822 else
3823 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3824 * (unlisted buffers)
3825 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003826 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003827 && ((flag == 'U'
3828 ? buf->b_p_bl
3829 : (!buf->b_p_bl
3830 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003831 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003832 ;
3833 return buf;
3834}
3835
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003836#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003837static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003838
3839/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003840 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003841 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003842 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003843 static void
3844expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003845 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003846 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003847{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003848 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003849 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003850 char_u *funcname;
3851 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003852
Bram Moolenaare344bea2005-09-01 20:46:49 +00003853 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3854 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003855 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003856
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003857 /* Call 'completefunc' to obtain the list of matches. */
3858 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003859 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003860
Bram Moolenaare344bea2005-09-01 20:46:49 +00003861 pos = curwin->w_cursor;
3862 matchlist = call_func_retlist(funcname, 2, args, FALSE);
3863 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003864 if (matchlist == NULL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003865 return;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003866
Bram Moolenaara94bc432006-03-10 21:42:59 +00003867 ins_compl_add_list(matchlist);
3868 list_unref(matchlist);
3869}
3870#endif /* FEAT_COMPL_FUNC */
3871
Bram Moolenaar39f05632006-03-19 22:15:26 +00003872#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003873/*
3874 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003875 */
3876 static void
3877ins_compl_add_list(list)
3878 list_T *list;
3879{
3880 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003881 int dir = compl_direction;
3882
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003883 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003884 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003885 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003886 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3887 /* if dir was BACKWARD then honor it just once */
3888 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003889 else if (did_emsg)
3890 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003891 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003892}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003893
3894/*
3895 * Add a match to the list of matches from a typeval_T.
3896 * If the given string is already in the list of completions, then return
3897 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3898 * maybe because alloc() returns NULL, then FAIL is returned.
3899 */
3900 int
3901ins_compl_add_tv(tv, dir)
3902 typval_T *tv;
3903 int dir;
3904{
3905 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003906 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003907 int adup = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003908 char_u *(cptext[CPT_COUNT]);
3909
3910 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3911 {
3912 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3913 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3914 (char_u *)"abbr", FALSE);
3915 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3916 (char_u *)"menu", FALSE);
3917 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3918 (char_u *)"kind", FALSE);
3919 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3920 (char_u *)"info", FALSE);
3921 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3922 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003923 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003924 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003925 }
3926 else
3927 {
3928 word = get_tv_string_chk(tv);
3929 vim_memset(cptext, 0, sizeof(cptext));
3930 }
3931 if (word == NULL || *word == NUL)
3932 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003933 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003934}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003935#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003936
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003937/*
3938 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003939 * The search starts at position "ini" in curbuf and in the direction
3940 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003941 * When "compl_started" is FALSE start at that position, otherwise continue
3942 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003943 * This may return before finding all the matches.
3944 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003945 */
3946 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003947ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949{
3950 static pos_T first_match_pos;
3951 static pos_T last_match_pos;
3952 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003953 static int found_all = FALSE; /* Found all matches of a
3954 certain type. */
3955 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956
Bram Moolenaar572cb562005-08-05 21:35:02 +00003957 pos_T *pos;
3958 char_u **matches;
3959 int save_p_scs;
3960 int save_p_ws;
3961 int save_p_ic;
3962 int i;
3963 int num_matches;
3964 int len;
3965 int found_new_match;
3966 int type = ctrl_x_mode;
3967 char_u *ptr;
3968 char_u *dict = NULL;
3969 int dict_f = 0;
3970 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003972 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003973 {
3974 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3975 ins_buf->b_scanned = 0;
3976 found_all = FALSE;
3977 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003978 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003979 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980 last_match_pos = first_match_pos = *ini;
3981 }
3982
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003983 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003984 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3986 for (;;)
3987 {
3988 found_new_match = FAIL;
3989
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003990 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003991 * or if found_all says this entry is done. For ^X^L only use the
3992 * entries from 'complete' that look in loaded buffers. */
3993 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003994 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003995 {
3996 found_all = FALSE;
3997 while (*e_cpt == ',' || *e_cpt == ' ')
3998 e_cpt++;
3999 if (*e_cpt == '.' && !curbuf->b_scanned)
4000 {
4001 ins_buf = curbuf;
4002 first_match_pos = *ini;
4003 /* So that ^N can match word immediately after cursor */
4004 if (ctrl_x_mode == 0)
4005 dec(&first_match_pos);
4006 last_match_pos = first_match_pos;
4007 type = 0;
4008 }
4009 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4010 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4011 {
4012 /* Scan a buffer, but not the current one. */
4013 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4014 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004015 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004016 first_match_pos.col = last_match_pos.col = 0;
4017 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4018 last_match_pos.lnum = 0;
4019 type = 0;
4020 }
4021 else /* unloaded buffer, scan like dictionary */
4022 {
4023 found_all = TRUE;
4024 if (ins_buf->b_fname == NULL)
4025 continue;
4026 type = CTRL_X_DICTIONARY;
4027 dict = ins_buf->b_fname;
4028 dict_f = DICT_EXACT;
4029 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004030 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004031 ins_buf->b_fname == NULL
4032 ? buf_spname(ins_buf)
4033 : ins_buf->b_sfname == NULL
4034 ? (char *)ins_buf->b_fname
4035 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004036 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037 }
4038 else if (*e_cpt == NUL)
4039 break;
4040 else
4041 {
4042 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4043 type = -1;
4044 else if (*e_cpt == 'k' || *e_cpt == 's')
4045 {
4046 if (*e_cpt == 'k')
4047 type = CTRL_X_DICTIONARY;
4048 else
4049 type = CTRL_X_THESAURUS;
4050 if (*++e_cpt != ',' && *e_cpt != NUL)
4051 {
4052 dict = e_cpt;
4053 dict_f = DICT_FIRST;
4054 }
4055 }
4056#ifdef FEAT_FIND_ID
4057 else if (*e_cpt == 'i')
4058 type = CTRL_X_PATH_PATTERNS;
4059 else if (*e_cpt == 'd')
4060 type = CTRL_X_PATH_DEFINES;
4061#endif
4062 else if (*e_cpt == ']' || *e_cpt == 't')
4063 {
4064 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004065 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004066 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
4068 else
4069 type = -1;
4070
4071 /* in any case e_cpt is advanced to the next entry */
4072 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4073
4074 found_all = TRUE;
4075 if (type == -1)
4076 continue;
4077 }
4078 }
4079
4080 switch (type)
4081 {
4082 case -1:
4083 break;
4084#ifdef FEAT_FIND_ID
4085 case CTRL_X_PATH_PATTERNS:
4086 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004087 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004088 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004089 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004090 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004091 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4092 (linenr_T)1, (linenr_T)MAXLNUM);
4093 break;
4094#endif
4095
4096 case CTRL_X_DICTIONARY:
4097 case CTRL_X_THESAURUS:
4098 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004099 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004100 : (type == CTRL_X_THESAURUS
4101 ? (*curbuf->b_p_tsr == NUL
4102 ? p_tsr
4103 : curbuf->b_p_tsr)
4104 : (*curbuf->b_p_dict == NUL
4105 ? p_dict
4106 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004107 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004108 dict != NULL ? dict_f
4109 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004110 dict = NULL;
4111 break;
4112
4113 case CTRL_X_TAGS:
4114 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4115 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004116 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004117
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004118 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004119 * of matches is found when compl_pattern is empty */
4120 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4122 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4123 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4124 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004125 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004126 }
4127 p_ic = save_p_ic;
4128 break;
4129
4130 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004131 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004132 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4133 {
4134
4135 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004136 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004137 ins_compl_add_matches(num_matches, matches,
4138#ifdef CASE_INSENSITIVE_FILENAME
4139 TRUE
4140#else
4141 FALSE
4142#endif
4143 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004144 }
4145 break;
4146
4147 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004148 if (expand_cmdline(&compl_xp, compl_pattern,
4149 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004150 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004151 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 break;
4153
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004154#ifdef FEAT_COMPL_FUNC
4155 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004156 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004157 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004158 break;
4159#endif
4160
Bram Moolenaar488c6512005-08-11 20:09:58 +00004161 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004162#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004163 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004164 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004165 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004166 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004167#endif
4168 break;
4169
Bram Moolenaar071d4272004-06-13 20:20:40 +00004170 default: /* normal ^P/^N and ^X^L */
4171 /*
4172 * If 'infercase' is set, don't use 'smartcase' here
4173 */
4174 save_p_scs = p_scs;
4175 if (ins_buf->b_p_inf)
4176 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 /* buffers other than curbuf are scanned from the beginning or the
4179 * end but never from the middle, thus setting nowrapscan in this
4180 * buffers is a good idea, on the other hand, we always set
4181 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4182 save_p_ws = p_ws;
4183 if (ins_buf != curbuf)
4184 p_ws = FALSE;
4185 else if (*e_cpt == '.')
4186 p_ws = TRUE;
4187 for (;;)
4188 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004189 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004190
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004191 ++msg_silent; /* Don't want messages for wrapscan. */
4192
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004193 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4194 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004196 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004198 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004200 found_new_match = searchit(NULL, ins_buf, pos,
4201 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004202 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004203 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004204 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004205 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004207 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004208 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 first_match_pos = *pos;
4210 last_match_pos = *pos;
4211 }
4212 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004213 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 found_new_match = FAIL;
4215 if (found_new_match == FAIL)
4216 {
4217 if (ins_buf == curbuf)
4218 found_all = TRUE;
4219 break;
4220 }
4221
4222 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004223 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004224 && ini->lnum == pos->lnum
4225 && ini->col == pos->col)
4226 continue;
4227 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4228 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4229 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004230 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 {
4232 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4233 continue;
4234 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4235 if (!p_paste)
4236 ptr = skipwhite(ptr);
4237 }
4238 len = (int)STRLEN(ptr);
4239 }
4240 else
4241 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004242 char_u *tmp_ptr = ptr;
4243
4244 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004246 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 /* Skip if already inside a word. */
4248 if (vim_iswordp(tmp_ptr))
4249 continue;
4250 /* Find start of next word. */
4251 tmp_ptr = find_word_start(tmp_ptr);
4252 }
4253 /* Find end of this word. */
4254 tmp_ptr = find_word_end(tmp_ptr);
4255 len = (int)(tmp_ptr - ptr);
4256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004257 if ((compl_cont_status & CONT_ADDING)
4258 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259 {
4260 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4261 {
4262 /* Try next line, if any. the new word will be
4263 * "join" as if the normal command "J" was used.
4264 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004265 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004266 * works -- Acevedo */
4267 STRNCPY(IObuff, ptr, len);
4268 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4269 tmp_ptr = ptr = skipwhite(ptr);
4270 /* Find start of next word. */
4271 tmp_ptr = find_word_start(tmp_ptr);
4272 /* Find end of next word. */
4273 tmp_ptr = find_word_end(tmp_ptr);
4274 if (tmp_ptr > ptr)
4275 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004276 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004278 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004279 IObuff[len++] = ' ';
4280 /* IObuf =~ "\k.* ", thus len >= 2 */
4281 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004282 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283 || (vim_strchr(p_cpo, CPO_JOINSP)
4284 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004285 && (IObuff[len - 2] == '?'
4286 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 IObuff[len++] = ' ';
4288 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004289 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 if (tmp_ptr - ptr >= IOSIZE - len)
4291 tmp_ptr = ptr + IOSIZE - len - 1;
4292 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4293 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004294 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 }
4296 IObuff[len] = NUL;
4297 ptr = IObuff;
4298 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004299 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 continue;
4301 }
4302 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004303 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004304 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004305 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004306 {
4307 found_new_match = OK;
4308 break;
4309 }
4310 }
4311 p_scs = save_p_scs;
4312 p_ws = save_p_ws;
4313 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004314
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004315 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004316 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004317 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318 found_new_match = OK;
4319
4320 /* break the loop for specialized modes (use 'complete' just for the
4321 * generic ctrl_x_mode == 0) or when we've found a new match */
4322 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004324 {
4325 if (got_int)
4326 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004327 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004328 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004329 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004330
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004331 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4332 || compl_interrupted)
4333 break;
4334 compl_started = TRUE;
4335 }
4336 else
4337 {
4338 /* Mark a buffer scanned when it has been scanned completely */
4339 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4340 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004342 compl_started = FALSE;
4343 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004345 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004346
4347 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4348 && *e_cpt == NUL) /* Got to end of 'complete' */
4349 found_new_match = FAIL;
4350
4351 i = -1; /* total of matches, unknown */
4352 if (found_new_match == FAIL
4353 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4354 i = ins_compl_make_cyclic();
4355
4356 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004357 * just been made cyclic then we have to move compl_curr_match to the next
4358 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004359 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4360 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004361 if (compl_curr_match == NULL)
4362 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 return i;
4364}
4365
4366/* Delete the old text being completed. */
4367 static void
4368ins_compl_delete()
4369{
4370 int i;
4371
4372 /*
4373 * In insert mode: Delete the typed part.
4374 * In replace mode: Put the old characters back, if any.
4375 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004376 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 backspace_until_column(i);
4378 changed_cline_bef_curs();
4379}
4380
4381/* Insert the new text being completed. */
4382 static void
4383ins_compl_insert()
4384{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004385 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004386 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4387 compl_used_match = FALSE;
4388 else
4389 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390}
4391
4392/*
4393 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004394 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4395 * get more completions. If it is FALSE, then we just do nothing when there
4396 * are no more completions in a given direction. The latter case is used when
4397 * we are still in the middle of finding completions, to allow browsing
4398 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 * Return the total number of matches, or -1 if still unknown -- webb.
4400 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004401 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4402 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 *
4404 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004405 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4406 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 */
4408 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004409ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004411 int count; /* repeat completion this many times; should
4412 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004413 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414{
4415 int num_matches = -1;
4416 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004417 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004418 compl_T *found_compl = NULL;
4419 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004420 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004421
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004422 if (compl_leader != NULL
4423 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004424 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004425 /* Set "compl_shown_match" to the actually shown match, it may differ
4426 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004427 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004428 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004429 && compl_shown_match->cp_next != NULL
4430 && compl_shown_match->cp_next != compl_first_match)
4431 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004432
4433 /* If we didn't find it searching forward, and compl_shows_dir is
4434 * backward, find the last match. */
4435 if (compl_shows_dir == BACKWARD
4436 && !ins_compl_equal(compl_shown_match,
4437 compl_leader, (int)STRLEN(compl_leader))
4438 && (compl_shown_match->cp_next == NULL
4439 || compl_shown_match->cp_next == compl_first_match))
4440 {
4441 while (!ins_compl_equal(compl_shown_match,
4442 compl_leader, (int)STRLEN(compl_leader))
4443 && compl_shown_match->cp_prev != NULL
4444 && compl_shown_match->cp_prev != compl_first_match)
4445 compl_shown_match = compl_shown_match->cp_prev;
4446 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004447 }
4448
4449 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004450 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 /* Delete old text to be replaced */
4452 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004453
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004454 /* When finding the longest common text we stick at the original text,
4455 * don't let CTRL-N or CTRL-P move to the first match. */
4456 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4457
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004458 /* When restarting the search don't insert the first match either. */
4459 if (compl_restarting)
4460 {
4461 advance = FALSE;
4462 compl_restarting = FALSE;
4463 }
4464
Bram Moolenaare3226be2005-12-18 22:10:00 +00004465 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4466 * around. */
4467 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004468 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004469 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004470 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004471 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004472 found_end = (compl_first_match != NULL
4473 && (compl_shown_match->cp_next == compl_first_match
4474 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004475 }
4476 else if (compl_shows_dir == BACKWARD
4477 && compl_shown_match->cp_prev != NULL)
4478 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004479 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004480 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004481 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004482 }
4483 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004484 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004485 if (!allow_get_expansion)
4486 {
4487 if (advance)
4488 {
4489 if (compl_shows_dir == BACKWARD)
4490 compl_pending -= todo + 1;
4491 else
4492 compl_pending += todo + 1;
4493 }
4494 return -1;
4495 }
4496
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004497 if (advance)
4498 {
4499 if (compl_shows_dir == BACKWARD)
4500 --compl_pending;
4501 else
4502 ++compl_pending;
4503 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004504
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004505 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004506 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004507
4508 /* handle any pending completions */
4509 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004510 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004511 {
4512 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4513 {
4514 compl_shown_match = compl_shown_match->cp_next;
4515 --compl_pending;
4516 }
4517 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4518 {
4519 compl_shown_match = compl_shown_match->cp_prev;
4520 ++compl_pending;
4521 }
4522 else
4523 break;
4524 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004525 found_end = FALSE;
4526 }
4527 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4528 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004529 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004530 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004531 ++todo;
4532 else
4533 /* Remember a matching item. */
4534 found_compl = compl_shown_match;
4535
4536 /* Stop at the end of the list when we found a usable match. */
4537 if (found_end)
4538 {
4539 if (found_compl != NULL)
4540 {
4541 compl_shown_match = found_compl;
4542 break;
4543 }
4544 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 }
4547
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004548 /* Insert the text of the new completion, or the compl_leader. */
4549 if (insert_match)
4550 {
4551 if (!compl_get_longest || compl_used_match)
4552 ins_compl_insert();
4553 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004554 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004555 }
4556 else
4557 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004558
4559 if (!allow_get_expansion)
4560 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004561 /* may undisplay the popup menu first */
4562 ins_compl_upd_pum();
4563
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004564 /* redraw to show the user what was inserted */
4565 update_screen(0);
4566
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004567 /* display the updated popup menu */
4568 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004569#ifdef FEAT_GUI
4570 if (gui.in_use)
4571 {
4572 /* Show the cursor after the match, not after the redrawn text. */
4573 setcursor();
4574 out_flush();
4575 gui_update_cursor(FALSE, FALSE);
4576 }
4577#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004578
Bram Moolenaar071d4272004-06-13 20:20:40 +00004579 /* Delete old text to be replaced, since we're still searching and
4580 * don't want to match ourselves! */
4581 ins_compl_delete();
4582 }
4583
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004584 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004585 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004586 compl_enter_selects = !insert_match && compl_match_array != NULL;
4587
Bram Moolenaar071d4272004-06-13 20:20:40 +00004588 /*
4589 * Show the file name for the match (if any)
4590 * Truncate the file name to avoid a wait for return.
4591 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004592 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004593 {
4594 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004595 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 if (i <= 0)
4597 i = 0;
4598 else
4599 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004600 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 msg(IObuff);
4602 redraw_cmdline = FALSE; /* don't overwrite! */
4603 }
4604
4605 return num_matches;
4606}
4607
4608/*
4609 * Call this while finding completions, to check whether the user has hit a key
4610 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004611 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004613 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004614 */
4615 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004616ins_compl_check_keys(frequency)
4617 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618{
4619 static int count = 0;
4620
4621 int c;
4622
4623 /* Don't check when reading keys from a script. That would break the test
4624 * scripts */
4625 if (using_script())
4626 return;
4627
4628 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004629 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004630 return;
4631 count = 0;
4632
Bram Moolenaara260a972006-06-23 19:36:29 +00004633 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4634 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004635 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004636 if (c != NUL)
4637 {
4638 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4639 {
4640 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004641 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004642 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4643 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004645 else
4646 {
4647 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004648 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004649 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004650 if (c != K_IGNORE)
4651 {
4652 /* Don't interrupt completion when the character wasn't typed,
4653 * e.g., when doing @q to replay keys. */
4654 if (c != Ctrl_R && KeyTyped)
4655 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004656
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004657 vungetc(c);
4658 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004659 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004660 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004661 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004662 {
4663 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4664
4665 compl_pending = 0;
4666 (void)ins_compl_next(FALSE, todo, TRUE);
4667 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004668}
4669
4670/*
4671 * Decide the direction of Insert mode complete from the key typed.
4672 * Returns BACKWARD or FORWARD.
4673 */
4674 static int
4675ins_compl_key2dir(c)
4676 int c;
4677{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004678 if (c == Ctrl_P || c == Ctrl_L
4679 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4680 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004681 return BACKWARD;
4682 return FORWARD;
4683}
4684
4685/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004686 * Return TRUE for keys that are used for completion only when the popup menu
4687 * is visible.
4688 */
4689 static int
4690ins_compl_pum_key(c)
4691 int c;
4692{
4693 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004694 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4695 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004696}
4697
4698/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004699 * Decide the number of completions to move forward.
4700 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4701 */
4702 static int
4703ins_compl_key2count(c)
4704 int c;
4705{
4706 int h;
4707
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004708 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004709 {
4710 h = pum_get_height();
4711 if (h > 3)
4712 h -= 2; /* keep some context */
4713 return h;
4714 }
4715 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716}
4717
4718/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004719 * Return TRUE if completion with "c" should insert the match, FALSE if only
4720 * to change the currently selected completion.
4721 */
4722 static int
4723ins_compl_use_match(c)
4724 int c;
4725{
4726 switch (c)
4727 {
4728 case K_UP:
4729 case K_DOWN:
4730 case K_PAGEDOWN:
4731 case K_KPAGEDOWN:
4732 case K_S_DOWN:
4733 case K_PAGEUP:
4734 case K_KPAGEUP:
4735 case K_S_UP:
4736 return FALSE;
4737 }
4738 return TRUE;
4739}
4740
4741/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004742 * Do Insert mode completion.
4743 * Called when character "c" was typed, which has a meaning for completion.
4744 * Returns OK if completion was done, FAIL if something failed (out of mem).
4745 */
4746 static int
4747ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004748 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004750 char_u *line;
4751 int startcol = 0; /* column where searched text starts */
4752 colnr_T curs_col; /* cursor column */
4753 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004754 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004755
Bram Moolenaare3226be2005-12-18 22:10:00 +00004756 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004757 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004758 {
4759 /* First time we hit ^N or ^P (in a row, I mean) */
4760
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 did_ai = FALSE;
4762#ifdef FEAT_SMARTINDENT
4763 did_si = FALSE;
4764 can_si = FALSE;
4765 can_si_back = FALSE;
4766#endif
4767 if (stop_arrow() == FAIL)
4768 return FAIL;
4769
4770 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004771 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004772 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004774 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004775 * "compl_startpos" to the cursor as a pattern to add a new word
4776 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004777 * "compl_startpos" is not in the same line as the cursor then fix it
4778 * (the line has been split because it was longer than 'tw'). if SOL
4779 * is set then skip the previous pattern, a word at the beginning of
4780 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004781 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4782 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004783 {
4784 /*
4785 * it is a continued search
4786 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004787 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4789 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4790 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004791 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004793 /* line (probably) wrapped, set compl_startpos to the
4794 * first non_blank in the line, if it is not a wordchar
4795 * include it to get a better pattern, but then we don't
4796 * want the "\\<" prefix, check it bellow */
4797 compl_col = (colnr_T)(skipwhite(line) - line);
4798 compl_startpos.col = compl_col;
4799 compl_startpos.lnum = curwin->w_cursor.lnum;
4800 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 }
4802 else
4803 {
4804 /* S_IPOS was set when we inserted a word that was at the
4805 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004806 * mode but first we need to redefine compl_startpos */
4807 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004808 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004809 compl_cont_status |= CONT_SOL;
4810 compl_startpos.col = (colnr_T)(skipwhite(
4811 line + compl_length
4812 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004813 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004814 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004815 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004816 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004817 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004818 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004819#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004820 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004822 compl_cont_status &= ~CONT_SOL;
4823 compl_length = (IOSIZE - MIN_SPACE);
4824 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004825 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004826 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4827 if (compl_length < 1)
4828 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 }
4830 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004831 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004832 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004833 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004834 }
4835 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004836 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004838 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004839 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004840 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004841 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004842 compl_cont_status = 0;
4843 compl_cont_status |= CONT_N_ADDS;
4844 compl_startpos = curwin->w_cursor;
4845 startcol = (int)curs_col;
4846 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 }
4848
4849 /* Work out completion pattern and original text -- webb */
4850 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4851 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004852 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4854 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004855 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004857 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004858 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004859 compl_col += ++startcol;
4860 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
4862 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004863 compl_pattern = str_foldcase(line + compl_col,
4864 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004866 compl_pattern = vim_strnsave(line + compl_col,
4867 compl_length);
4868 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 return FAIL;
4870 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004871 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
4873 char_u *prefix = (char_u *)"\\<";
4874
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004875 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004876 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004877 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004878 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004880 if (!vim_iswordp(line + compl_col)
4881 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 && (
4883#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004884 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004886 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887#endif
4888 )))
4889 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004890 STRCPY((char *)compl_pattern, prefix);
4891 (void)quote_meta(compl_pattern + STRLEN(prefix),
4892 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004894 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004896 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004898 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899#endif
4900 )
4901 {
4902 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004903 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4904 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004906 compl_col += curs_col;
4907 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908 }
4909 else
4910 {
4911#ifdef FEAT_MBYTE
4912 /* Search the point of change class of multibyte character
4913 * or not a word single byte character backward. */
4914 if (has_mbyte)
4915 {
4916 int base_class;
4917 int head_off;
4918
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004919 startcol -= (*mb_head_off)(line, line + startcol);
4920 base_class = mb_get_class(line + startcol);
4921 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004922 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004923 head_off = (*mb_head_off)(line, line + startcol);
4924 if (base_class != mb_get_class(line + startcol
4925 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004927 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004928 }
4929 }
4930 else
4931#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004932 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004933 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004934 compl_col += ++startcol;
4935 compl_length = (int)curs_col - startcol;
4936 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004937 {
4938 /* Only match word with at least two chars -- webb
4939 * there's no need to call quote_meta,
4940 * alloc(7) is enough -- Acevedo
4941 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004942 compl_pattern = alloc(7);
4943 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004945 STRCPY((char *)compl_pattern, "\\<");
4946 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4947 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 }
4949 else
4950 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004951 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004952 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004953 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004954 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004955 STRCPY((char *)compl_pattern, "\\<");
4956 (void)quote_meta(compl_pattern + 2, line + compl_col,
4957 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004958 }
4959 }
4960 }
4961 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4962 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004963 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004964 compl_length = (int)curs_col - (int)compl_col;
4965 if (compl_length < 0) /* cursor in indent: empty pattern */
4966 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004967 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004968 compl_pattern = str_foldcase(line + compl_col, compl_length,
4969 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004970 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004971 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4972 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004973 return FAIL;
4974 }
4975 else if (ctrl_x_mode == CTRL_X_FILES)
4976 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004977 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004979 compl_col += ++startcol;
4980 compl_length = (int)curs_col - startcol;
4981 compl_pattern = addstar(line + compl_col, compl_length,
4982 EXPAND_FILES);
4983 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 return FAIL;
4985 }
4986 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4987 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004988 compl_pattern = vim_strnsave(line, curs_col);
4989 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004991 set_cmd_context(&compl_xp, compl_pattern,
4992 (int)STRLEN(compl_pattern), curs_col);
4993 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
4994 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004995 /* No completion possible, use an empty pattern to get a
4996 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004997 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00004998 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00004999 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5000 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005001 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005002 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005003 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005004#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005005 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005006 * Call user defined function 'completefunc' with "a:findstart"
5007 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005008 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005009 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005010 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005011 char_u *funcname;
5012 pos_T pos;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005013
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005014 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005015 * string */
5016 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5017 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5018 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005019 {
5020 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5021 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005022 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005023 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005024
5025 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005026 args[1] = NULL;
5027 pos = curwin->w_cursor;
5028 col = call_func_retnr(funcname, 2, args, FALSE);
5029 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005030
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005031 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005032 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005033 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005034 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005035 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005036
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005037 /* Setup variables for completion. Need to obtain "line" again,
5038 * it may have become invalid. */
5039 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005040 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005041 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5042 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005043#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005044 return FAIL;
5045 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005046 else if (ctrl_x_mode == CTRL_X_SPELL)
5047 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005048#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005049 if (spell_bad_len > 0)
5050 compl_col = curs_col - spell_bad_len;
5051 else
5052 compl_col = spell_word_start(startcol);
5053 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005054 {
5055 compl_length = 0;
5056 compl_col = curs_col;
5057 }
5058 else
5059 {
5060 spell_expand_check_cap(compl_col);
5061 compl_length = (int)curs_col - compl_col;
5062 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005063 /* Need to obtain "line" again, it may have become invalid. */
5064 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005065 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5066 if (compl_pattern == NULL)
5067#endif
5068 return FAIL;
5069 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005070 else
5071 {
5072 EMSG2(_(e_intern2), "ins_complete()");
5073 return FAIL;
5074 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005075
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005076 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005077 {
5078 edit_submode_pre = (char_u *)_(" Adding");
5079 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5080 {
5081 /* Insert a new line, keep indentation but ignore 'comments' */
5082#ifdef FEAT_COMMENTS
5083 char_u *old = curbuf->b_p_com;
5084
5085 curbuf->b_p_com = (char_u *)"";
5086#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005087 compl_startpos.lnum = curwin->w_cursor.lnum;
5088 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 ins_eol('\r');
5090#ifdef FEAT_COMMENTS
5091 curbuf->b_p_com = old;
5092#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005093 compl_length = 0;
5094 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 }
5096 }
5097 else
5098 {
5099 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005100 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005101 }
5102
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005103 if (compl_cont_status & CONT_LOCAL)
5104 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005105 else
5106 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5107
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005108 /* Always add completion for the original text. */
5109 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005110 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5111 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005112 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005114 vim_free(compl_pattern);
5115 compl_pattern = NULL;
5116 vim_free(compl_orig_text);
5117 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 return FAIL;
5119 }
5120
5121 /* showmode might reset the internal line pointers, so it must
5122 * be called before line = ml_get(), or when this address is no
5123 * longer needed. -- Acevedo.
5124 */
5125 edit_submode_extra = (char_u *)_("-- Searching...");
5126 edit_submode_highl = HLF_COUNT;
5127 showmode();
5128 edit_submode_extra = NULL;
5129 out_flush();
5130 }
5131
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005132 compl_shown_match = compl_curr_match;
5133 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134
5135 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005136 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005137 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005138 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005139 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005140
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005141 /* may undisplay the popup menu */
5142 ins_compl_upd_pum();
5143
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005144 if (n > 1) /* all matches have been found */
5145 compl_matches = n;
5146 compl_curr_match = compl_shown_match;
5147 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148
Bram Moolenaard68071d2006-05-02 22:08:30 +00005149 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5150 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005151 if (got_int && !global_busy)
5152 {
5153 (void)vgetc();
5154 got_int = FALSE;
5155 }
5156
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005157 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005158 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005159 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005160 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5161 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5163 edit_submode_highl = HLF_E;
5164 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5165 * because we couldn't expand anything at first place, but if we used
5166 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5167 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005168 if ( compl_length > 1
5169 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 || (ctrl_x_mode != 0
5171 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5172 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005173 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 }
5175
Bram Moolenaar572cb562005-08-05 21:35:02 +00005176 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005177 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005179 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180
5181 if (edit_submode_extra == NULL)
5182 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005183 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 {
5185 edit_submode_extra = (char_u *)_("Back at original");
5186 edit_submode_highl = HLF_W;
5187 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005188 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 {
5190 edit_submode_extra = (char_u *)_("Word from other line");
5191 edit_submode_highl = HLF_COUNT;
5192 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005193 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005194 {
5195 edit_submode_extra = (char_u *)_("The only match");
5196 edit_submode_highl = HLF_COUNT;
5197 }
5198 else
5199 {
5200 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005201 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005202 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005203 int number = 0;
5204 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005205
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005206 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
5208 /* search backwards for the first valid (!= -1) number.
5209 * This should normally succeed already at the first loop
5210 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005211 for (match = compl_curr_match->cp_prev; match != NULL
5212 && match != compl_first_match;
5213 match = match->cp_prev)
5214 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005216 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 break;
5218 }
5219 if (match != NULL)
5220 /* go up and assign all numbers which are not assigned
5221 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005222 for (match = match->cp_next;
5223 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005224 match = match->cp_next)
5225 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005226 }
5227 else /* BACKWARD */
5228 {
5229 /* search forwards (upwards) for the first valid (!= -1)
5230 * number. This should normally succeed already at the
5231 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005232 for (match = compl_curr_match->cp_next; match != NULL
5233 && match != compl_first_match;
5234 match = match->cp_next)
5235 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005237 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 break;
5239 }
5240 if (match != NULL)
5241 /* go down and assign all numbers which are not
5242 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005243 for (match = match->cp_prev; match
5244 && match->cp_number == -1;
5245 match = match->cp_prev)
5246 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 }
5248 }
5249
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005250 /* The match should always have a sequence number now, this is
5251 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005252 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005253 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005254 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5255 * Translations may need more than twice that. */
5256 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005257
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005258 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005259 vim_snprintf((char *)match_ref, sizeof(match_ref),
5260 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005261 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005262 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005263 vim_snprintf((char *)match_ref, sizeof(match_ref),
5264 _("match %d"),
5265 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 edit_submode_extra = match_ref;
5267 edit_submode_highl = HLF_R;
5268 if (dollar_vcol)
5269 curs_columns(FALSE);
5270 }
5271 }
5272 }
5273
5274 /* Show a message about what (completion) mode we're in. */
5275 showmode();
5276 if (edit_submode_extra != NULL)
5277 {
5278 if (!p_smd)
5279 msg_attr(edit_submode_extra,
5280 edit_submode_highl < HLF_COUNT
5281 ? hl_attr(edit_submode_highl) : 0);
5282 }
5283 else
5284 msg_clr_cmdline(); /* necessary for "noshowmode" */
5285
Bram Moolenaard68071d2006-05-02 22:08:30 +00005286 /* Show the popup menu, unless we got interrupted. */
5287 if (!compl_interrupted)
5288 {
5289 /* RedrawingDisabled may be set when invoked through complete(). */
5290 n = RedrawingDisabled;
5291 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005292
5293 /* If the cursor moved we need to remove the pum first. */
5294 setcursor();
5295 if (save_w_wrow != curwin->w_wrow)
5296 ins_compl_del_pum();
5297
Bram Moolenaard68071d2006-05-02 22:08:30 +00005298 ins_compl_show_pum();
5299 setcursor();
5300 RedrawingDisabled = n;
5301 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005302 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005303 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005304
Bram Moolenaar071d4272004-06-13 20:20:40 +00005305 return OK;
5306}
5307
5308/*
5309 * Looks in the first "len" chars. of "src" for search-metachars.
5310 * If dest is not NULL the chars. are copied there quoting (with
5311 * a backslash) the metachars, and dest would be NUL terminated.
5312 * Returns the length (needed) of dest
5313 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005314 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005315quote_meta(dest, src, len)
5316 char_u *dest;
5317 char_u *src;
5318 int len;
5319{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005320 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005321
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005322 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 {
5324 switch (*src)
5325 {
5326 case '.':
5327 case '*':
5328 case '[':
5329 if (ctrl_x_mode == CTRL_X_DICTIONARY
5330 || ctrl_x_mode == CTRL_X_THESAURUS)
5331 break;
5332 case '~':
5333 if (!p_magic) /* quote these only if magic is set */
5334 break;
5335 case '\\':
5336 if (ctrl_x_mode == CTRL_X_DICTIONARY
5337 || ctrl_x_mode == CTRL_X_THESAURUS)
5338 break;
5339 case '^': /* currently it's not needed. */
5340 case '$':
5341 m++;
5342 if (dest != NULL)
5343 *dest++ = '\\';
5344 break;
5345 }
5346 if (dest != NULL)
5347 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005348# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005349 /* Copy remaining bytes of a multibyte character. */
5350 if (has_mbyte)
5351 {
5352 int i, mb_len;
5353
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005354 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005355 if (mb_len > 0 && len >= mb_len)
5356 for (i = 0; i < mb_len; ++i)
5357 {
5358 --len;
5359 ++src;
5360 if (dest != NULL)
5361 *dest++ = *src;
5362 }
5363 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005364# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005365 }
5366 if (dest != NULL)
5367 *dest = NUL;
5368
5369 return m;
5370}
5371#endif /* FEAT_INS_EXPAND */
5372
5373/*
5374 * Next character is interpreted literally.
5375 * A one, two or three digit decimal number is interpreted as its byte value.
5376 * If one or two digits are entered, the next character is given to vungetc().
5377 * For Unicode a character > 255 may be returned.
5378 */
5379 int
5380get_literal()
5381{
5382 int cc;
5383 int nc;
5384 int i;
5385 int hex = FALSE;
5386 int octal = FALSE;
5387#ifdef FEAT_MBYTE
5388 int unicode = 0;
5389#endif
5390
5391 if (got_int)
5392 return Ctrl_C;
5393
5394#ifdef FEAT_GUI
5395 /*
5396 * In GUI there is no point inserting the internal code for a special key.
5397 * It is more useful to insert the string "<KEY>" instead. This would
5398 * probably be useful in a text window too, but it would not be
5399 * vi-compatible (maybe there should be an option for it?) -- webb
5400 */
5401 if (gui.in_use)
5402 ++allow_keys;
5403#endif
5404#ifdef USE_ON_FLY_SCROLL
5405 dont_scroll = TRUE; /* disallow scrolling here */
5406#endif
5407 ++no_mapping; /* don't map the next key hits */
5408 cc = 0;
5409 i = 0;
5410 for (;;)
5411 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005412 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005413#ifdef FEAT_CMDL_INFO
5414 if (!(State & CMDLINE)
5415# ifdef FEAT_MBYTE
5416 && MB_BYTE2LEN_CHECK(nc) == 1
5417# endif
5418 )
5419 add_to_showcmd(nc);
5420#endif
5421 if (nc == 'x' || nc == 'X')
5422 hex = TRUE;
5423 else if (nc == 'o' || nc == 'O')
5424 octal = TRUE;
5425#ifdef FEAT_MBYTE
5426 else if (nc == 'u' || nc == 'U')
5427 unicode = nc;
5428#endif
5429 else
5430 {
5431 if (hex
5432#ifdef FEAT_MBYTE
5433 || unicode != 0
5434#endif
5435 )
5436 {
5437 if (!vim_isxdigit(nc))
5438 break;
5439 cc = cc * 16 + hex2nr(nc);
5440 }
5441 else if (octal)
5442 {
5443 if (nc < '0' || nc > '7')
5444 break;
5445 cc = cc * 8 + nc - '0';
5446 }
5447 else
5448 {
5449 if (!VIM_ISDIGIT(nc))
5450 break;
5451 cc = cc * 10 + nc - '0';
5452 }
5453
5454 ++i;
5455 }
5456
5457 if (cc > 255
5458#ifdef FEAT_MBYTE
5459 && unicode == 0
5460#endif
5461 )
5462 cc = 255; /* limit range to 0-255 */
5463 nc = 0;
5464
5465 if (hex) /* hex: up to two chars */
5466 {
5467 if (i >= 2)
5468 break;
5469 }
5470#ifdef FEAT_MBYTE
5471 else if (unicode) /* Unicode: up to four or eight chars */
5472 {
5473 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5474 break;
5475 }
5476#endif
5477 else if (i >= 3) /* decimal or octal: up to three chars */
5478 break;
5479 }
5480 if (i == 0) /* no number entered */
5481 {
5482 if (nc == K_ZERO) /* NUL is stored as NL */
5483 {
5484 cc = '\n';
5485 nc = 0;
5486 }
5487 else
5488 {
5489 cc = nc;
5490 nc = 0;
5491 }
5492 }
5493
5494 if (cc == 0) /* NUL is stored as NL */
5495 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005496#ifdef FEAT_MBYTE
5497 if (enc_dbcs && (cc & 0xff) == 0)
5498 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5499 second byte will cause trouble! */
5500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005501
5502 --no_mapping;
5503#ifdef FEAT_GUI
5504 if (gui.in_use)
5505 --allow_keys;
5506#endif
5507 if (nc)
5508 vungetc(nc);
5509 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5510 return cc;
5511}
5512
5513/*
5514 * Insert character, taking care of special keys and mod_mask
5515 */
5516 static void
5517insert_special(c, allow_modmask, ctrlv)
5518 int c;
5519 int allow_modmask;
5520 int ctrlv; /* c was typed after CTRL-V */
5521{
5522 char_u *p;
5523 int len;
5524
5525 /*
5526 * Special function key, translate into "<Key>". Up to the last '>' is
5527 * inserted with ins_str(), so as not to replace characters in replace
5528 * mode.
5529 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5530 * unless 'allow_modmask' is TRUE.
5531 */
5532#ifdef MACOS
5533 /* Command-key never produces a normal key */
5534 if (mod_mask & MOD_MASK_CMD)
5535 allow_modmask = TRUE;
5536#endif
5537 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5538 {
5539 p = get_special_key_name(c, mod_mask);
5540 len = (int)STRLEN(p);
5541 c = p[len - 1];
5542 if (len > 2)
5543 {
5544 if (stop_arrow() == FAIL)
5545 return;
5546 p[len - 1] = NUL;
5547 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005548 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005549 ctrlv = FALSE;
5550 }
5551 }
5552 if (stop_arrow() == OK)
5553 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5554}
5555
5556/*
5557 * Special characters in this context are those that need processing other
5558 * than the simple insertion that can be performed here. This includes ESC
5559 * which terminates the insert, and CR/NL which need special processing to
5560 * open up a new line. This routine tries to optimize insertions performed by
5561 * the "redo", "undo" or "put" commands, so it needs to know when it should
5562 * stop and defer processing to the "normal" mechanism.
5563 * '0' and '^' are special, because they can be followed by CTRL-D.
5564 */
5565#ifdef EBCDIC
5566# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5567#else
5568# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5569#endif
5570
5571#ifdef FEAT_MBYTE
5572# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5573#else
5574# define WHITECHAR(cc) vim_iswhite(cc)
5575#endif
5576
5577 void
5578insertchar(c, flags, second_indent)
5579 int c; /* character to insert or NUL */
5580 int flags; /* INSCHAR_FORMAT, etc. */
5581 int second_indent; /* indent for second line if >= 0 */
5582{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005583 int textwidth;
5584#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005585 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005587 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005588
5589 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5590 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005591
5592 /*
5593 * Try to break the line in two or more pieces when:
5594 * - Always do this if we have been called to do formatting only.
5595 * - Always do this when 'formatoptions' has the 'a' flag and the line
5596 * ends in white space.
5597 * - Otherwise:
5598 * - Don't do this if inserting a blank
5599 * - Don't do this if an existing character is being replaced, unless
5600 * we're in VREPLACE mode.
5601 * - Do this if the cursor is not on the line where insert started
5602 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5603 * before the insert.
5604 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5605 * before 'textwidth'
5606 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005607 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 && ((flags & INSCHAR_FORMAT)
5609 || (!vim_iswhite(c)
5610 && !((State & REPLACE_FLAG)
5611#ifdef FEAT_VREPLACE
5612 && !(State & VREPLACE_FLAG)
5613#endif
5614 && *ml_get_cursor() != NUL)
5615 && (curwin->w_cursor.lnum != Insstart.lnum
5616 || ((!has_format_option(FO_INS_LONG)
5617 || Insstart_textlen <= (colnr_T)textwidth)
5618 && (!fo_ins_blank
5619 || Insstart_blank_vcol <= (colnr_T)textwidth
5620 ))))))
5621 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005622 /* Format with 'formatexpr' when it's set. Use internal formatting
5623 * when 'formatexpr' isn't set or it returns non-zero. */
5624#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005625 int do_internal = TRUE;
5626
Bram Moolenaar81a82092008-03-12 16:27:00 +00005627 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005628 {
5629 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5630 /* It may be required to save for undo again, e.g. when setline()
5631 * was called. */
5632 ins_need_undo = TRUE;
5633 }
5634 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005635#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005636 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005637 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005638
Bram Moolenaar071d4272004-06-13 20:20:40 +00005639 if (c == NUL) /* only formatting was wanted */
5640 return;
5641
5642#ifdef FEAT_COMMENTS
5643 /* Check whether this character should end a comment. */
5644 if (did_ai && (int)c == end_comment_pending)
5645 {
5646 char_u *line;
5647 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5648 int middle_len, end_len;
5649 int i;
5650
5651 /*
5652 * Need to remove existing (middle) comment leader and insert end
5653 * comment leader. First, check what comment leader we can find.
5654 */
5655 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5656 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5657 {
5658 /* Skip middle-comment string */
5659 while (*p && p[-1] != ':') /* find end of middle flags */
5660 ++p;
5661 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5662 /* Don't count trailing white space for middle_len */
5663 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5664 --middle_len;
5665
5666 /* Find the end-comment string */
5667 while (*p && p[-1] != ':') /* find end of end flags */
5668 ++p;
5669 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5670
5671 /* Skip white space before the cursor */
5672 i = curwin->w_cursor.col;
5673 while (--i >= 0 && vim_iswhite(line[i]))
5674 ;
5675 i++;
5676
5677 /* Skip to before the middle leader */
5678 i -= middle_len;
5679
5680 /* Check some expected things before we go on */
5681 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5682 {
5683 /* Backspace over all the stuff we want to replace */
5684 backspace_until_column(i);
5685
5686 /*
5687 * Insert the end-comment string, except for the last
5688 * character, which will get inserted as normal later.
5689 */
5690 ins_bytes_len(lead_end, end_len - 1);
5691 }
5692 }
5693 }
5694 end_comment_pending = NUL;
5695#endif
5696
5697 did_ai = FALSE;
5698#ifdef FEAT_SMARTINDENT
5699 did_si = FALSE;
5700 can_si = FALSE;
5701 can_si_back = FALSE;
5702#endif
5703
5704 /*
5705 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5706 * This speeds up normal text input considerably.
5707 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5708 * need to re-indent at a ':', or any other character (but not what
5709 * 'paste' is set)..
5710 */
5711#ifdef USE_ON_FLY_SCROLL
5712 dont_scroll = FALSE; /* allow scrolling here */
5713#endif
5714
5715 if ( !ISSPECIAL(c)
5716#ifdef FEAT_MBYTE
5717 && (!has_mbyte || (*mb_char2len)(c) == 1)
5718#endif
5719 && vpeekc() != NUL
5720 && !(State & REPLACE_FLAG)
5721#ifdef FEAT_CINDENT
5722 && !cindent_on()
5723#endif
5724#ifdef FEAT_RIGHTLEFT
5725 && !p_ri
5726#endif
5727 )
5728 {
5729#define INPUT_BUFLEN 100
5730 char_u buf[INPUT_BUFLEN + 1];
5731 int i;
5732 colnr_T virtcol = 0;
5733
5734 buf[0] = c;
5735 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005736 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005737 virtcol = get_nolist_virtcol();
5738 /*
5739 * Stop the string when:
5740 * - no more chars available
5741 * - finding a special character (command key)
5742 * - buffer is full
5743 * - running into the 'textwidth' boundary
5744 * - need to check for abbreviation: A non-word char after a word-char
5745 */
5746 while ( (c = vpeekc()) != NUL
5747 && !ISSPECIAL(c)
5748#ifdef FEAT_MBYTE
5749 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5750#endif
5751 && i < INPUT_BUFLEN
5752 && (textwidth == 0
5753 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5754 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5755 {
5756#ifdef FEAT_RIGHTLEFT
5757 c = vgetc();
5758 if (p_hkmap && KeyTyped)
5759 c = hkmap(c); /* Hebrew mode mapping */
5760# ifdef FEAT_FKMAP
5761 if (p_fkmap && KeyTyped)
5762 c = fkmap(c); /* Farsi mode mapping */
5763# endif
5764 buf[i++] = c;
5765#else
5766 buf[i++] = vgetc();
5767#endif
5768 }
5769
5770#ifdef FEAT_DIGRAPHS
5771 do_digraph(-1); /* clear digraphs */
5772 do_digraph(buf[i-1]); /* may be the start of a digraph */
5773#endif
5774 buf[i] = NUL;
5775 ins_str(buf);
5776 if (flags & INSCHAR_CTRLV)
5777 {
5778 redo_literal(*buf);
5779 i = 1;
5780 }
5781 else
5782 i = 0;
5783 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005784 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 }
5786 else
5787 {
5788#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005789 int cc;
5790
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5792 {
5793 char_u buf[MB_MAXBYTES + 1];
5794
5795 (*mb_char2bytes)(c, buf);
5796 buf[cc] = NUL;
5797 ins_char_bytes(buf, cc);
5798 AppendCharToRedobuff(c);
5799 }
5800 else
5801#endif
5802 {
5803 ins_char(c);
5804 if (flags & INSCHAR_CTRLV)
5805 redo_literal(c);
5806 else
5807 AppendCharToRedobuff(c);
5808 }
5809 }
5810}
5811
5812/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005813 * Format text at the current insert position.
5814 */
5815 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00005816internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005817 int textwidth;
5818 int second_indent;
5819 int flags;
5820 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005821 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005822{
5823 int cc;
5824 int save_char = NUL;
5825 int haveto_redraw = FALSE;
5826 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5827#ifdef FEAT_MBYTE
5828 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5829#endif
5830 int fo_white_par = has_format_option(FO_WHITE_PAR);
5831 int first_line = TRUE;
5832#ifdef FEAT_COMMENTS
5833 colnr_T leader_len;
5834 int no_leader = FALSE;
5835 int do_comments = (flags & INSCHAR_DO_COM);
5836#endif
5837
5838 /*
5839 * When 'ai' is off we don't want a space under the cursor to be
5840 * deleted. Replace it with an 'x' temporarily.
5841 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005842 if (!curbuf->b_p_ai
5843#ifdef FEAT_VREPLACE
5844 && !(State & VREPLACE_FLAG)
5845#endif
5846 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005847 {
5848 cc = gchar_cursor();
5849 if (vim_iswhite(cc))
5850 {
5851 save_char = cc;
5852 pchar_cursor('x');
5853 }
5854 }
5855
5856 /*
5857 * Repeat breaking lines, until the current line is not too long.
5858 */
5859 while (!got_int)
5860 {
5861 int startcol; /* Cursor column at entry */
5862 int wantcol; /* column at textwidth border */
5863 int foundcol; /* column for start of spaces */
5864 int end_foundcol = 0; /* column for start of word */
5865 colnr_T len;
5866 colnr_T virtcol;
5867#ifdef FEAT_VREPLACE
5868 int orig_col = 0;
5869 char_u *saved_text = NULL;
5870#endif
5871 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005872 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005873
Bram Moolenaar97b98102009-11-17 16:41:01 +00005874 virtcol = get_nolist_virtcol()
5875 + char2cells(c != NUL ? c : gchar_cursor());
5876 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005877 break;
5878
5879#ifdef FEAT_COMMENTS
5880 if (no_leader)
5881 do_comments = FALSE;
5882 else if (!(flags & INSCHAR_FORMAT)
5883 && has_format_option(FO_WRAP_COMS))
5884 do_comments = TRUE;
5885
5886 /* Don't break until after the comment leader */
5887 if (do_comments)
5888 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5889 else
5890 leader_len = 0;
5891
5892 /* If the line doesn't start with a comment leader, then don't
5893 * start one in a following broken line. Avoids that a %word
5894 * moved to the start of the next line causes all following lines
5895 * to start with %. */
5896 if (leader_len == 0)
5897 no_leader = TRUE;
5898#endif
5899 if (!(flags & INSCHAR_FORMAT)
5900#ifdef FEAT_COMMENTS
5901 && leader_len == 0
5902#endif
5903 && !has_format_option(FO_WRAP))
5904
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005905 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005906 if ((startcol = curwin->w_cursor.col) == 0)
5907 break;
5908
5909 /* find column of textwidth border */
5910 coladvance((colnr_T)textwidth);
5911 wantcol = curwin->w_cursor.col;
5912
Bram Moolenaar97b98102009-11-17 16:41:01 +00005913 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005914 foundcol = 0;
5915
5916 /*
5917 * Find position to break at.
5918 * Stop at first entered white when 'formatoptions' has 'v'
5919 */
5920 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5921 || curwin->w_cursor.lnum != Insstart.lnum
5922 || curwin->w_cursor.col >= Insstart.col)
5923 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00005924 if (curwin->w_cursor.col == startcol && c != NUL)
5925 cc = c;
5926 else
5927 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005928 if (WHITECHAR(cc))
5929 {
5930 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005931 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005932
5933 /* find start of sequence of blanks */
5934 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5935 {
5936 dec_cursor();
5937 cc = gchar_cursor();
5938 }
5939 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5940 break; /* only spaces in front of text */
5941#ifdef FEAT_COMMENTS
5942 /* Don't break until after the comment leader */
5943 if (curwin->w_cursor.col < leader_len)
5944 break;
5945#endif
5946 if (has_format_option(FO_ONE_LETTER))
5947 {
5948 /* do not break after one-letter words */
5949 if (curwin->w_cursor.col == 0)
5950 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005951#ifdef FEAT_COMMENTS
5952 /* do not break "#a b" when 'tw' is 2 */
5953 if (curwin->w_cursor.col <= leader_len)
5954 break;
5955#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005956 col = curwin->w_cursor.col;
5957 dec_cursor();
5958 cc = gchar_cursor();
5959
5960 if (WHITECHAR(cc))
5961 continue; /* one-letter, continue */
5962 curwin->w_cursor.col = col;
5963 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00005964
5965 inc_cursor();
5966
5967 end_foundcol = end_col + 1;
5968 foundcol = curwin->w_cursor.col;
5969 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005970 break;
5971 }
5972#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00005973 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005974 {
5975 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005976 if (curwin->w_cursor.col != startcol)
5977 {
5978#ifdef FEAT_COMMENTS
5979 /* Don't break until after the comment leader */
5980 if (curwin->w_cursor.col < leader_len)
5981 break;
5982#endif
5983 col = curwin->w_cursor.col;
5984 inc_cursor();
5985 /* Don't change end_foundcol if already set. */
5986 if (foundcol != curwin->w_cursor.col)
5987 {
5988 foundcol = curwin->w_cursor.col;
5989 end_foundcol = foundcol;
5990 if (curwin->w_cursor.col <= (colnr_T)wantcol)
5991 break;
5992 }
5993 curwin->w_cursor.col = col;
5994 }
5995
5996 if (curwin->w_cursor.col == 0)
5997 break;
5998
5999 col = curwin->w_cursor.col;
6000
6001 dec_cursor();
6002 cc = gchar_cursor();
6003
6004 if (WHITECHAR(cc))
6005 continue; /* break with space */
6006#ifdef FEAT_COMMENTS
6007 /* Don't break until after the comment leader */
6008 if (curwin->w_cursor.col < leader_len)
6009 break;
6010#endif
6011
6012 curwin->w_cursor.col = col;
6013
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006014 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006015 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006016 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6017 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006018 }
6019#endif
6020 if (curwin->w_cursor.col == 0)
6021 break;
6022 dec_cursor();
6023 }
6024
6025 if (foundcol == 0) /* no spaces, cannot break line */
6026 {
6027 curwin->w_cursor.col = startcol;
6028 break;
6029 }
6030
6031 /* Going to break the line, remove any "$" now. */
6032 undisplay_dollar();
6033
6034 /*
6035 * Offset between cursor position and line break is used by replace
6036 * stack functions. VREPLACE does not use this, and backspaces
6037 * over the text instead.
6038 */
6039#ifdef FEAT_VREPLACE
6040 if (State & VREPLACE_FLAG)
6041 orig_col = startcol; /* Will start backspacing from here */
6042 else
6043#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006044 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006045
6046 /*
6047 * adjust startcol for spaces that will be deleted and
6048 * characters that will remain on top line
6049 */
6050 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006051 while ((cc = gchar_cursor(), WHITECHAR(cc))
6052 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006053 inc_cursor();
6054 startcol -= curwin->w_cursor.col;
6055 if (startcol < 0)
6056 startcol = 0;
6057
6058#ifdef FEAT_VREPLACE
6059 if (State & VREPLACE_FLAG)
6060 {
6061 /*
6062 * In VREPLACE mode, we will backspace over the text to be
6063 * wrapped, so save a copy now to put on the next line.
6064 */
6065 saved_text = vim_strsave(ml_get_cursor());
6066 curwin->w_cursor.col = orig_col;
6067 if (saved_text == NULL)
6068 break; /* Can't do it, out of memory */
6069 saved_text[startcol] = NUL;
6070
6071 /* Backspace over characters that will move to the next line */
6072 if (!fo_white_par)
6073 backspace_until_column(foundcol);
6074 }
6075 else
6076#endif
6077 {
6078 /* put cursor after pos. to break line */
6079 if (!fo_white_par)
6080 curwin->w_cursor.col = foundcol;
6081 }
6082
6083 /*
6084 * Split the line just before the margin.
6085 * Only insert/delete lines, but don't really redraw the window.
6086 */
6087 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6088 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6089#ifdef FEAT_COMMENTS
6090 + (do_comments ? OPENLINE_DO_COM : 0)
6091#endif
6092 , old_indent);
6093 old_indent = 0;
6094
6095 replace_offset = 0;
6096 if (first_line)
6097 {
6098 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6099 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6100 if (second_indent >= 0)
6101 {
6102#ifdef FEAT_VREPLACE
6103 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006104 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006105 else
6106#endif
6107 (void)set_indent(second_indent, SIN_CHANGED);
6108 }
6109 first_line = FALSE;
6110 }
6111
6112#ifdef FEAT_VREPLACE
6113 if (State & VREPLACE_FLAG)
6114 {
6115 /*
6116 * In VREPLACE mode we have backspaced over the text to be
6117 * moved, now we re-insert it into the new line.
6118 */
6119 ins_bytes(saved_text);
6120 vim_free(saved_text);
6121 }
6122 else
6123#endif
6124 {
6125 /*
6126 * Check if cursor is not past the NUL off the line, cindent
6127 * may have added or removed indent.
6128 */
6129 curwin->w_cursor.col += startcol;
6130 len = (colnr_T)STRLEN(ml_get_curline());
6131 if (curwin->w_cursor.col > len)
6132 curwin->w_cursor.col = len;
6133 }
6134
6135 haveto_redraw = TRUE;
6136#ifdef FEAT_CINDENT
6137 can_cindent = TRUE;
6138#endif
6139 /* moved the cursor, don't autoindent or cindent now */
6140 did_ai = FALSE;
6141#ifdef FEAT_SMARTINDENT
6142 did_si = FALSE;
6143 can_si = FALSE;
6144 can_si_back = FALSE;
6145#endif
6146 line_breakcheck();
6147 }
6148
6149 if (save_char != NUL) /* put back space after cursor */
6150 pchar_cursor(save_char);
6151
6152 if (!format_only && haveto_redraw)
6153 {
6154 update_topline();
6155 redraw_curbuf_later(VALID);
6156 }
6157}
6158
6159/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006160 * Called after inserting or deleting text: When 'formatoptions' includes the
6161 * 'a' flag format from the current line until the end of the paragraph.
6162 * Keep the cursor at the same position relative to the text.
6163 * The caller must have saved the cursor line for undo, following ones will be
6164 * saved here.
6165 */
6166 void
6167auto_format(trailblank, prev_line)
6168 int trailblank; /* when TRUE also format with trailing blank */
6169 int prev_line; /* may start in previous line */
6170{
6171 pos_T pos;
6172 colnr_T len;
6173 char_u *old;
6174 char_u *new, *pnew;
6175 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006176 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006177
6178 if (!has_format_option(FO_AUTO))
6179 return;
6180
6181 pos = curwin->w_cursor;
6182 old = ml_get_curline();
6183
6184 /* may remove added space */
6185 check_auto_format(FALSE);
6186
6187 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6188 * user might insert normal text next. Also skip formatting when "1" is
6189 * in 'formatoptions' and there is a single character before the cursor.
6190 * Otherwise the line would be broken and when typing another non-white
6191 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006192 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006193 if (*old != NUL && !trailblank && wasatend)
6194 {
6195 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006196 cc = gchar_cursor();
6197 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6198 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006199 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006200 cc = gchar_cursor();
6201 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006202 {
6203 curwin->w_cursor = pos;
6204 return;
6205 }
6206 curwin->w_cursor = pos;
6207 }
6208
6209#ifdef FEAT_COMMENTS
6210 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6211 * comments. */
6212 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6213 && get_leader_len(old, NULL, FALSE) == 0)
6214 return;
6215#endif
6216
6217 /*
6218 * May start formatting in a previous line, so that after "x" a word is
6219 * moved to the previous line if it fits there now. Only when this is not
6220 * the start of a paragraph.
6221 */
6222 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6223 {
6224 --curwin->w_cursor.lnum;
6225 if (u_save_cursor() == FAIL)
6226 return;
6227 }
6228
6229 /*
6230 * Do the formatting and restore the cursor position. "saved_cursor" will
6231 * be adjusted for the text formatting.
6232 */
6233 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006234 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006235 curwin->w_cursor = saved_cursor;
6236 saved_cursor.lnum = 0;
6237
6238 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6239 {
6240 /* "cannot happen" */
6241 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6242 coladvance((colnr_T)MAXCOL);
6243 }
6244 else
6245 check_cursor_col();
6246
6247 /* Insert mode: If the cursor is now after the end of the line while it
6248 * previously wasn't, the line was broken. Because of the rule above we
6249 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6250 * formatted. */
6251 if (!wasatend && has_format_option(FO_WHITE_PAR))
6252 {
6253 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006254 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006255 if (curwin->w_cursor.col == len)
6256 {
6257 pnew = vim_strnsave(new, len + 2);
6258 pnew[len] = ' ';
6259 pnew[len + 1] = NUL;
6260 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6261 /* remove the space later */
6262 did_add_space = TRUE;
6263 }
6264 else
6265 /* may remove added space */
6266 check_auto_format(FALSE);
6267 }
6268
6269 check_cursor();
6270}
6271
6272/*
6273 * When an extra space was added to continue a paragraph for auto-formatting,
6274 * delete it now. The space must be under the cursor, just after the insert
6275 * position.
6276 */
6277 static void
6278check_auto_format(end_insert)
6279 int end_insert; /* TRUE when ending Insert mode */
6280{
6281 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006282 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006283
6284 if (did_add_space)
6285 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006286 cc = gchar_cursor();
6287 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006288 /* Somehow the space was removed already. */
6289 did_add_space = FALSE;
6290 else
6291 {
6292 if (!end_insert)
6293 {
6294 inc_cursor();
6295 c = gchar_cursor();
6296 dec_cursor();
6297 }
6298 if (c != NUL)
6299 {
6300 /* The space is no longer at the end of the line, delete it. */
6301 del_char(FALSE);
6302 did_add_space = FALSE;
6303 }
6304 }
6305 }
6306}
6307
6308/*
6309 * Find out textwidth to be used for formatting:
6310 * if 'textwidth' option is set, use it
6311 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6312 * if invalid value, use 0.
6313 * Set default to window width (maximum 79) for "gq" operator.
6314 */
6315 int
6316comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006317 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006318{
6319 int textwidth;
6320
6321 textwidth = curbuf->b_p_tw;
6322 if (textwidth == 0 && curbuf->b_p_wm)
6323 {
6324 /* The width is the window width minus 'wrapmargin' minus all the
6325 * things that add to the margin. */
6326 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6327#ifdef FEAT_CMDWIN
6328 if (cmdwin_type != 0)
6329 textwidth -= 1;
6330#endif
6331#ifdef FEAT_FOLDING
6332 textwidth -= curwin->w_p_fdc;
6333#endif
6334#ifdef FEAT_SIGNS
6335 if (curwin->w_buffer->b_signlist != NULL
6336# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006337 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006338# endif
6339 )
6340 textwidth -= 1;
6341#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006342 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006343 textwidth -= 8;
6344 }
6345 if (textwidth < 0)
6346 textwidth = 0;
6347 if (ff && textwidth == 0)
6348 {
6349 textwidth = W_WIDTH(curwin) - 1;
6350 if (textwidth > 79)
6351 textwidth = 79;
6352 }
6353 return textwidth;
6354}
6355
6356/*
6357 * Put a character in the redo buffer, for when just after a CTRL-V.
6358 */
6359 static void
6360redo_literal(c)
6361 int c;
6362{
6363 char_u buf[10];
6364
6365 /* Only digits need special treatment. Translate them into a string of
6366 * three digits. */
6367 if (VIM_ISDIGIT(c))
6368 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006369 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006370 AppendToRedobuff(buf);
6371 }
6372 else
6373 AppendCharToRedobuff(c);
6374}
6375
6376/*
6377 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006378 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006379 */
6380 static void
6381start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006382 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006383{
6384 if (!arrow_used) /* something has been inserted */
6385 {
6386 AppendToRedobuff(ESC_STR);
6387 stop_insert(end_insert_pos, FALSE);
6388 arrow_used = TRUE; /* this means we stopped the current insert */
6389 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006390#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006391 check_spell_redraw();
6392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393}
6394
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006395#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006396/*
6397 * If we skipped highlighting word at cursor, do it now.
6398 * It may be skipped again, thus reset spell_redraw_lnum first.
6399 */
6400 static void
6401check_spell_redraw()
6402{
6403 if (spell_redraw_lnum != 0)
6404 {
6405 linenr_T lnum = spell_redraw_lnum;
6406
6407 spell_redraw_lnum = 0;
6408 redrawWinline(lnum, FALSE);
6409 }
6410}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006411
6412/*
6413 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6414 * spelled word, if there is one.
6415 */
6416 static void
6417spell_back_to_badword()
6418{
6419 pos_T tpos = curwin->w_cursor;
6420
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006421 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006422 if (curwin->w_cursor.col != tpos.col)
6423 start_arrow(&tpos);
6424}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006425#endif
6426
Bram Moolenaar071d4272004-06-13 20:20:40 +00006427/*
6428 * stop_arrow() is called before a change is made in insert mode.
6429 * If an arrow key has been used, start a new insertion.
6430 * Returns FAIL if undo is impossible, shouldn't insert then.
6431 */
6432 int
6433stop_arrow()
6434{
6435 if (arrow_used)
6436 {
6437 if (u_save_cursor() == OK)
6438 {
6439 arrow_used = FALSE;
6440 ins_need_undo = FALSE;
6441 }
6442 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006443 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006444 ai_col = 0;
6445#ifdef FEAT_VREPLACE
6446 if (State & VREPLACE_FLAG)
6447 {
6448 orig_line_count = curbuf->b_ml.ml_line_count;
6449 vr_lines_changed = 1;
6450 }
6451#endif
6452 ResetRedobuff();
6453 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006454 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006455 }
6456 else if (ins_need_undo)
6457 {
6458 if (u_save_cursor() == OK)
6459 ins_need_undo = FALSE;
6460 }
6461
6462#ifdef FEAT_FOLDING
6463 /* Always open fold at the cursor line when inserting something. */
6464 foldOpenCursor();
6465#endif
6466
6467 return (arrow_used || ins_need_undo ? FAIL : OK);
6468}
6469
6470/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006471 * Do a few things to stop inserting.
6472 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6473 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006474 */
6475 static void
6476stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006477 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006478 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006479{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006480 int cc;
6481 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006482
6483 stop_redo_ins();
6484 replace_flush(); /* abandon replace stack */
6485
6486 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006487 * Save the inserted text for later redo with ^@ and CTRL-A.
6488 * Don't do it when "restart_edit" was set and nothing was inserted,
6489 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006490 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006491 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006492 if (did_restart_edit == 0 || (ptr != NULL
6493 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006494 {
6495 vim_free(last_insert);
6496 last_insert = ptr;
6497 last_insert_skip = new_insert_skip;
6498 }
6499 else
6500 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006501
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006502 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006503 {
6504 /* Auto-format now. It may seem strange to do this when stopping an
6505 * insertion (or moving the cursor), but it's required when appending
6506 * a line and having it end in a space. But only do it when something
6507 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006508 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006509 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006510 pos_T tpos = curwin->w_cursor;
6511
Bram Moolenaar071d4272004-06-13 20:20:40 +00006512 /* When the cursor is at the end of the line after a space the
6513 * formatting will move it to the following word. Avoid that by
6514 * moving the cursor onto the space. */
6515 cc = 'x';
6516 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6517 {
6518 dec_cursor();
6519 cc = gchar_cursor();
6520 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006521 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006522 }
6523
6524 auto_format(TRUE, FALSE);
6525
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006526 if (vim_iswhite(cc))
6527 {
6528 if (gchar_cursor() != NUL)
6529 inc_cursor();
6530#ifdef FEAT_VIRTUALEDIT
6531 /* If the cursor is still at the same character, also keep
6532 * the "coladd". */
6533 if (gchar_cursor() == NUL
6534 && curwin->w_cursor.lnum == tpos.lnum
6535 && curwin->w_cursor.col == tpos.col)
6536 curwin->w_cursor.coladd = tpos.coladd;
6537#endif
6538 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006539 }
6540
6541 /* If a space was inserted for auto-formatting, remove it now. */
6542 check_auto_format(TRUE);
6543
6544 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006545 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006546 * Do this when ESC was used or moving the cursor up/down.
6547 * Check for the old position still being valid, just in case the text
6548 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006549 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006550 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6551 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006552 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006553 pos_T tpos = curwin->w_cursor;
6554
6555 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006556 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006557 for (;;)
6558 {
6559 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6560 --curwin->w_cursor.col;
6561 cc = gchar_cursor();
6562 if (!vim_iswhite(cc))
6563 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006564 if (del_char(TRUE) == FAIL)
6565 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006566 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006567 if (curwin->w_cursor.lnum != tpos.lnum)
6568 curwin->w_cursor = tpos;
6569 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006570 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6571
6572#ifdef FEAT_VISUAL
6573 /* <C-S-Right> may have started Visual mode, adjust the position for
6574 * deleted characters. */
6575 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6576 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006577 int len = (int)STRLEN(ml_get_curline());
6578
6579 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006580 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006581 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006582# ifdef FEAT_VIRTUALEDIT
6583 VIsual.coladd = 0;
6584# endif
6585 }
6586 }
6587#endif
6588 }
6589 }
6590 did_ai = FALSE;
6591#ifdef FEAT_SMARTINDENT
6592 did_si = FALSE;
6593 can_si = FALSE;
6594 can_si_back = FALSE;
6595#endif
6596
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006597 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6598 * now in a different buffer. */
6599 if (end_insert_pos != NULL)
6600 {
6601 curbuf->b_op_start = Insstart;
6602 curbuf->b_op_end = *end_insert_pos;
6603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006604}
6605
6606/*
6607 * Set the last inserted text to a single character.
6608 * Used for the replace command.
6609 */
6610 void
6611set_last_insert(c)
6612 int c;
6613{
6614 char_u *s;
6615
6616 vim_free(last_insert);
6617#ifdef FEAT_MBYTE
6618 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6619#else
6620 last_insert = alloc(6);
6621#endif
6622 if (last_insert != NULL)
6623 {
6624 s = last_insert;
6625 /* Use the CTRL-V only when entering a special char */
6626 if (c < ' ' || c == DEL)
6627 *s++ = Ctrl_V;
6628 s = add_char2buf(c, s);
6629 *s++ = ESC;
6630 *s++ = NUL;
6631 last_insert_skip = 0;
6632 }
6633}
6634
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006635#if defined(EXITFREE) || defined(PROTO)
6636 void
6637free_last_insert()
6638{
6639 vim_free(last_insert);
6640 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006641# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006642 vim_free(compl_orig_text);
6643 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006644# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006645}
6646#endif
6647
Bram Moolenaar071d4272004-06-13 20:20:40 +00006648/*
6649 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6650 * and CSI. Handle multi-byte characters.
6651 * Returns a pointer to after the added bytes.
6652 */
6653 char_u *
6654add_char2buf(c, s)
6655 int c;
6656 char_u *s;
6657{
6658#ifdef FEAT_MBYTE
6659 char_u temp[MB_MAXBYTES];
6660 int i;
6661 int len;
6662
6663 len = (*mb_char2bytes)(c, temp);
6664 for (i = 0; i < len; ++i)
6665 {
6666 c = temp[i];
6667#endif
6668 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6669 if (c == K_SPECIAL)
6670 {
6671 *s++ = K_SPECIAL;
6672 *s++ = KS_SPECIAL;
6673 *s++ = KE_FILLER;
6674 }
6675#ifdef FEAT_GUI
6676 else if (c == CSI)
6677 {
6678 *s++ = CSI;
6679 *s++ = KS_EXTRA;
6680 *s++ = (int)KE_CSI;
6681 }
6682#endif
6683 else
6684 *s++ = c;
6685#ifdef FEAT_MBYTE
6686 }
6687#endif
6688 return s;
6689}
6690
6691/*
6692 * move cursor to start of line
6693 * if flags & BL_WHITE move to first non-white
6694 * if flags & BL_SOL move to first non-white if startofline is set,
6695 * otherwise keep "curswant" column
6696 * if flags & BL_FIX don't leave the cursor on a NUL.
6697 */
6698 void
6699beginline(flags)
6700 int flags;
6701{
6702 if ((flags & BL_SOL) && !p_sol)
6703 coladvance(curwin->w_curswant);
6704 else
6705 {
6706 curwin->w_cursor.col = 0;
6707#ifdef FEAT_VIRTUALEDIT
6708 curwin->w_cursor.coladd = 0;
6709#endif
6710
6711 if (flags & (BL_WHITE | BL_SOL))
6712 {
6713 char_u *ptr;
6714
6715 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6716 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6717 ++curwin->w_cursor.col;
6718 }
6719 curwin->w_set_curswant = TRUE;
6720 }
6721}
6722
6723/*
6724 * oneright oneleft cursor_down cursor_up
6725 *
6726 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006727 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006728 * Return OK when successful, FAIL when we hit a line of file boundary.
6729 */
6730
6731 int
6732oneright()
6733{
6734 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006735 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006736
6737#ifdef FEAT_VIRTUALEDIT
6738 if (virtual_active())
6739 {
6740 pos_T prevpos = curwin->w_cursor;
6741
6742 /* Adjust for multi-wide char (excluding TAB) */
6743 ptr = ml_get_cursor();
6744 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006745# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006746 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006747# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006748 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006749# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006750 ))
6751 ? ptr2cells(ptr) : 1));
6752 curwin->w_set_curswant = TRUE;
6753 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6754 return (prevpos.col != curwin->w_cursor.col
6755 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6756 }
6757#endif
6758
6759 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006760 if (*ptr == NUL)
6761 return FAIL; /* already at the very end */
6762
Bram Moolenaar071d4272004-06-13 20:20:40 +00006763#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006764 if (has_mbyte)
6765 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006766 else
6767#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006768 l = 1;
6769
6770 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6771 * contains "onemore". */
6772 if (ptr[l] == NUL
6773#ifdef FEAT_VIRTUALEDIT
6774 && (ve_flags & VE_ONEMORE) == 0
6775#endif
6776 )
6777 return FAIL;
6778 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006779
6780 curwin->w_set_curswant = TRUE;
6781 return OK;
6782}
6783
6784 int
6785oneleft()
6786{
6787#ifdef FEAT_VIRTUALEDIT
6788 if (virtual_active())
6789 {
6790 int width;
6791 int v = getviscol();
6792
6793 if (v == 0)
6794 return FAIL;
6795
6796# ifdef FEAT_LINEBREAK
6797 /* We might get stuck on 'showbreak', skip over it. */
6798 width = 1;
6799 for (;;)
6800 {
6801 coladvance(v - width);
6802 /* getviscol() is slow, skip it when 'showbreak' is empty and
6803 * there are no multi-byte characters */
6804 if ((*p_sbr == NUL
6805# ifdef FEAT_MBYTE
6806 && !has_mbyte
6807# endif
6808 ) || getviscol() < v)
6809 break;
6810 ++width;
6811 }
6812# else
6813 coladvance(v - 1);
6814# endif
6815
6816 if (curwin->w_cursor.coladd == 1)
6817 {
6818 char_u *ptr;
6819
6820 /* Adjust for multi-wide char (not a TAB) */
6821 ptr = ml_get_cursor();
6822 if (*ptr != TAB && vim_isprintc(
6823# ifdef FEAT_MBYTE
6824 (*mb_ptr2char)(ptr)
6825# else
6826 *ptr
6827# endif
6828 ) && ptr2cells(ptr) > 1)
6829 curwin->w_cursor.coladd = 0;
6830 }
6831
6832 curwin->w_set_curswant = TRUE;
6833 return OK;
6834 }
6835#endif
6836
6837 if (curwin->w_cursor.col == 0)
6838 return FAIL;
6839
6840 curwin->w_set_curswant = TRUE;
6841 --curwin->w_cursor.col;
6842
6843#ifdef FEAT_MBYTE
6844 /* if the character on the left of the current cursor is a multi-byte
6845 * character, move to its first byte */
6846 if (has_mbyte)
6847 mb_adjust_cursor();
6848#endif
6849 return OK;
6850}
6851
6852 int
6853cursor_up(n, upd_topline)
6854 long n;
6855 int upd_topline; /* When TRUE: update topline */
6856{
6857 linenr_T lnum;
6858
6859 if (n > 0)
6860 {
6861 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006862 /* This fails if the cursor is already in the first line or the count
6863 * is larger than the line number and '-' is in 'cpoptions' */
6864 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006865 return FAIL;
6866 if (n >= lnum)
6867 lnum = 1;
6868 else
6869#ifdef FEAT_FOLDING
6870 if (hasAnyFolding(curwin))
6871 {
6872 /*
6873 * Count each sequence of folded lines as one logical line.
6874 */
6875 /* go to the the start of the current fold */
6876 (void)hasFolding(lnum, &lnum, NULL);
6877
6878 while (n--)
6879 {
6880 /* move up one line */
6881 --lnum;
6882 if (lnum <= 1)
6883 break;
6884 /* If we entered a fold, move to the beginning, unless in
6885 * Insert mode or when 'foldopen' contains "all": it will open
6886 * in a moment. */
6887 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6888 (void)hasFolding(lnum, &lnum, NULL);
6889 }
6890 if (lnum < 1)
6891 lnum = 1;
6892 }
6893 else
6894#endif
6895 lnum -= n;
6896 curwin->w_cursor.lnum = lnum;
6897 }
6898
6899 /* try to advance to the column we want to be at */
6900 coladvance(curwin->w_curswant);
6901
6902 if (upd_topline)
6903 update_topline(); /* make sure curwin->w_topline is valid */
6904
6905 return OK;
6906}
6907
6908/*
6909 * Cursor down a number of logical lines.
6910 */
6911 int
6912cursor_down(n, upd_topline)
6913 long n;
6914 int upd_topline; /* When TRUE: update topline */
6915{
6916 linenr_T lnum;
6917
6918 if (n > 0)
6919 {
6920 lnum = curwin->w_cursor.lnum;
6921#ifdef FEAT_FOLDING
6922 /* Move to last line of fold, will fail if it's the end-of-file. */
6923 (void)hasFolding(lnum, NULL, &lnum);
6924#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006925 /* This fails if the cursor is already in the last line or would move
6926 * beyound the last line and '-' is in 'cpoptions' */
6927 if (lnum >= curbuf->b_ml.ml_line_count
6928 || (lnum + n > curbuf->b_ml.ml_line_count
6929 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006930 return FAIL;
6931 if (lnum + n >= curbuf->b_ml.ml_line_count)
6932 lnum = curbuf->b_ml.ml_line_count;
6933 else
6934#ifdef FEAT_FOLDING
6935 if (hasAnyFolding(curwin))
6936 {
6937 linenr_T last;
6938
6939 /* count each sequence of folded lines as one logical line */
6940 while (n--)
6941 {
6942 if (hasFolding(lnum, NULL, &last))
6943 lnum = last + 1;
6944 else
6945 ++lnum;
6946 if (lnum >= curbuf->b_ml.ml_line_count)
6947 break;
6948 }
6949 if (lnum > curbuf->b_ml.ml_line_count)
6950 lnum = curbuf->b_ml.ml_line_count;
6951 }
6952 else
6953#endif
6954 lnum += n;
6955 curwin->w_cursor.lnum = lnum;
6956 }
6957
6958 /* try to advance to the column we want to be at */
6959 coladvance(curwin->w_curswant);
6960
6961 if (upd_topline)
6962 update_topline(); /* make sure curwin->w_topline is valid */
6963
6964 return OK;
6965}
6966
6967/*
6968 * Stuff the last inserted text in the read buffer.
6969 * Last_insert actually is a copy of the redo buffer, so we
6970 * first have to remove the command.
6971 */
6972 int
6973stuff_inserted(c, count, no_esc)
6974 int c; /* Command character to be inserted */
6975 long count; /* Repeat this many times */
6976 int no_esc; /* Don't add an ESC at the end */
6977{
6978 char_u *esc_ptr;
6979 char_u *ptr;
6980 char_u *last_ptr;
6981 char_u last = NUL;
6982
6983 ptr = get_last_insert();
6984 if (ptr == NULL)
6985 {
6986 EMSG(_(e_noinstext));
6987 return FAIL;
6988 }
6989
6990 /* may want to stuff the command character, to start Insert mode */
6991 if (c != NUL)
6992 stuffcharReadbuff(c);
6993 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
6994 *esc_ptr = NUL; /* remove the ESC */
6995
6996 /* when the last char is either "0" or "^" it will be quoted if no ESC
6997 * comes after it OR if it will inserted more than once and "ptr"
6998 * starts with ^D. -- Acevedo
6999 */
7000 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7001 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7002 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7003 {
7004 last = *last_ptr;
7005 *last_ptr = NUL;
7006 }
7007
7008 do
7009 {
7010 stuffReadbuff(ptr);
7011 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7012 if (last)
7013 stuffReadbuff((char_u *)(last == '0'
7014 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7015 : IF_EB("\026^", CTRL_V_STR "^")));
7016 }
7017 while (--count > 0);
7018
7019 if (last)
7020 *last_ptr = last;
7021
7022 if (esc_ptr != NULL)
7023 *esc_ptr = ESC; /* put the ESC back */
7024
7025 /* may want to stuff a trailing ESC, to get out of Insert mode */
7026 if (!no_esc)
7027 stuffcharReadbuff(ESC);
7028
7029 return OK;
7030}
7031
7032 char_u *
7033get_last_insert()
7034{
7035 if (last_insert == NULL)
7036 return NULL;
7037 return last_insert + last_insert_skip;
7038}
7039
7040/*
7041 * Get last inserted string, and remove trailing <Esc>.
7042 * Returns pointer to allocated memory (must be freed) or NULL.
7043 */
7044 char_u *
7045get_last_insert_save()
7046{
7047 char_u *s;
7048 int len;
7049
7050 if (last_insert == NULL)
7051 return NULL;
7052 s = vim_strsave(last_insert + last_insert_skip);
7053 if (s != NULL)
7054 {
7055 len = (int)STRLEN(s);
7056 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7057 s[len - 1] = NUL;
7058 }
7059 return s;
7060}
7061
7062/*
7063 * Check the word in front of the cursor for an abbreviation.
7064 * Called when the non-id character "c" has been entered.
7065 * When an abbreviation is recognized it is removed from the text and
7066 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7067 */
7068 static int
7069echeck_abbr(c)
7070 int c;
7071{
7072 /* Don't check for abbreviation in paste mode, when disabled and just
7073 * after moving around with cursor keys. */
7074 if (p_paste || no_abbr || arrow_used)
7075 return FALSE;
7076
7077 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7078 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7079}
7080
7081/*
7082 * replace-stack functions
7083 *
7084 * When replacing characters, the replaced characters are remembered for each
7085 * new character. This is used to re-insert the old text when backspacing.
7086 *
7087 * There is a NUL headed list of characters for each character that is
7088 * currently in the file after the insertion point. When BS is used, one NUL
7089 * headed list is put back for the deleted character.
7090 *
7091 * For a newline, there are two NUL headed lists. One contains the characters
7092 * that the NL replaced. The extra one stores the characters after the cursor
7093 * that were deleted (always white space).
7094 *
7095 * Replace_offset is normally 0, in which case replace_push will add a new
7096 * character at the end of the stack. If replace_offset is not 0, that many
7097 * characters will be left on the stack above the newly inserted character.
7098 */
7099
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007100static char_u *replace_stack = NULL;
7101static long replace_stack_nr = 0; /* next entry in replace stack */
7102static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007103
7104 void
7105replace_push(c)
7106 int c; /* character that is replaced (NUL is none) */
7107{
7108 char_u *p;
7109
7110 if (replace_stack_nr < replace_offset) /* nothing to do */
7111 return;
7112 if (replace_stack_len <= replace_stack_nr)
7113 {
7114 replace_stack_len += 50;
7115 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7116 if (p == NULL) /* out of memory */
7117 {
7118 replace_stack_len -= 50;
7119 return;
7120 }
7121 if (replace_stack != NULL)
7122 {
7123 mch_memmove(p, replace_stack,
7124 (size_t)(replace_stack_nr * sizeof(char_u)));
7125 vim_free(replace_stack);
7126 }
7127 replace_stack = p;
7128 }
7129 p = replace_stack + replace_stack_nr - replace_offset;
7130 if (replace_offset)
7131 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7132 *p = c;
7133 ++replace_stack_nr;
7134}
7135
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007136#if defined(FEAT_MBYTE) || defined(PROTO)
7137/*
7138 * Push a character onto the replace stack. Handles a multi-byte character in
7139 * reverse byte order, so that the first byte is popped off first.
7140 * Return the number of bytes done (includes composing characters).
7141 */
7142 int
7143replace_push_mb(p)
7144 char_u *p;
7145{
7146 int l = (*mb_ptr2len)(p);
7147 int j;
7148
7149 for (j = l - 1; j >= 0; --j)
7150 replace_push(p[j]);
7151 return l;
7152}
7153#endif
7154
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007155#if 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00007156/*
7157 * call replace_push(c) with replace_offset set to the first NUL.
7158 */
7159 static void
7160replace_push_off(c)
7161 int c;
7162{
7163 char_u *p;
7164
7165 p = replace_stack + replace_stack_nr;
7166 for (replace_offset = 1; replace_offset < replace_stack_nr;
7167 ++replace_offset)
7168 if (*--p == NUL)
7169 break;
7170 replace_push(c);
7171 replace_offset = 0;
7172}
Bram Moolenaareb3593b2006-04-22 22:33:57 +00007173#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007174
7175/*
7176 * Pop one item from the replace stack.
7177 * return -1 if stack empty
7178 * return replaced character or NUL otherwise
7179 */
7180 static int
7181replace_pop()
7182{
7183 if (replace_stack_nr == 0)
7184 return -1;
7185 return (int)replace_stack[--replace_stack_nr];
7186}
7187
7188/*
7189 * Join the top two items on the replace stack. This removes to "off"'th NUL
7190 * encountered.
7191 */
7192 static void
7193replace_join(off)
7194 int off; /* offset for which NUL to remove */
7195{
7196 int i;
7197
7198 for (i = replace_stack_nr; --i >= 0; )
7199 if (replace_stack[i] == NUL && off-- <= 0)
7200 {
7201 --replace_stack_nr;
7202 mch_memmove(replace_stack + i, replace_stack + i + 1,
7203 (size_t)(replace_stack_nr - i));
7204 return;
7205 }
7206}
7207
7208/*
7209 * Pop bytes from the replace stack until a NUL is found, and insert them
7210 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7211 */
7212 static void
7213replace_pop_ins()
7214{
7215 int cc;
7216 int oldState = State;
7217
7218 State = NORMAL; /* don't want REPLACE here */
7219 while ((cc = replace_pop()) > 0)
7220 {
7221#ifdef FEAT_MBYTE
7222 mb_replace_pop_ins(cc);
7223#else
7224 ins_char(cc);
7225#endif
7226 dec_cursor();
7227 }
7228 State = oldState;
7229}
7230
7231#ifdef FEAT_MBYTE
7232/*
7233 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7234 * indicates a multi-byte char, pop the other bytes too.
7235 */
7236 static void
7237mb_replace_pop_ins(cc)
7238 int cc;
7239{
7240 int n;
7241 char_u buf[MB_MAXBYTES];
7242 int i;
7243 int c;
7244
7245 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7246 {
7247 buf[0] = cc;
7248 for (i = 1; i < n; ++i)
7249 buf[i] = replace_pop();
7250 ins_bytes_len(buf, n);
7251 }
7252 else
7253 ins_char(cc);
7254
7255 if (enc_utf8)
7256 /* Handle composing chars. */
7257 for (;;)
7258 {
7259 c = replace_pop();
7260 if (c == -1) /* stack empty */
7261 break;
7262 if ((n = MB_BYTE2LEN(c)) == 1)
7263 {
7264 /* Not a multi-byte char, put it back. */
7265 replace_push(c);
7266 break;
7267 }
7268 else
7269 {
7270 buf[0] = c;
7271 for (i = 1; i < n; ++i)
7272 buf[i] = replace_pop();
7273 if (utf_iscomposing(utf_ptr2char(buf)))
7274 ins_bytes_len(buf, n);
7275 else
7276 {
7277 /* Not a composing char, put it back. */
7278 for (i = n - 1; i >= 0; --i)
7279 replace_push(buf[i]);
7280 break;
7281 }
7282 }
7283 }
7284}
7285#endif
7286
7287/*
7288 * make the replace stack empty
7289 * (called when exiting replace mode)
7290 */
7291 static void
7292replace_flush()
7293{
7294 vim_free(replace_stack);
7295 replace_stack = NULL;
7296 replace_stack_len = 0;
7297 replace_stack_nr = 0;
7298}
7299
7300/*
7301 * Handle doing a BS for one character.
7302 * cc < 0: replace stack empty, just move cursor
7303 * cc == 0: character was inserted, delete it
7304 * cc > 0: character was replaced, put cc (first byte of original char) back
7305 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007306 * When "limit_col" is >= 0, don't delete before this column. Matters when
7307 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007308 */
7309 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007310replace_do_bs(limit_col)
7311 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007312{
7313 int cc;
7314#ifdef FEAT_VREPLACE
7315 int orig_len = 0;
7316 int ins_len;
7317 int orig_vcols = 0;
7318 colnr_T start_vcol;
7319 char_u *p;
7320 int i;
7321 int vcol;
7322#endif
7323
7324 cc = replace_pop();
7325 if (cc > 0)
7326 {
7327#ifdef FEAT_VREPLACE
7328 if (State & VREPLACE_FLAG)
7329 {
7330 /* Get the number of screen cells used by the character we are
7331 * going to delete. */
7332 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7333 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7334 }
7335#endif
7336#ifdef FEAT_MBYTE
7337 if (has_mbyte)
7338 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007339 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007340# ifdef FEAT_VREPLACE
7341 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007342 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343# endif
7344 replace_push(cc);
7345 }
7346 else
7347#endif
7348 {
7349 pchar_cursor(cc);
7350#ifdef FEAT_VREPLACE
7351 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007352 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007353#endif
7354 }
7355 replace_pop_ins();
7356
7357#ifdef FEAT_VREPLACE
7358 if (State & VREPLACE_FLAG)
7359 {
7360 /* Get the number of screen cells used by the inserted characters */
7361 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007362 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007363 vcol = start_vcol;
7364 for (i = 0; i < ins_len; ++i)
7365 {
7366 vcol += chartabsize(p + i, vcol);
7367#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007368 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007369#endif
7370 }
7371 vcol -= start_vcol;
7372
7373 /* Delete spaces that were inserted after the cursor to keep the
7374 * text aligned. */
7375 curwin->w_cursor.col += ins_len;
7376 while (vcol > orig_vcols && gchar_cursor() == ' ')
7377 {
7378 del_char(FALSE);
7379 ++orig_vcols;
7380 }
7381 curwin->w_cursor.col -= ins_len;
7382 }
7383#endif
7384
7385 /* mark the buffer as changed and prepare for displaying */
7386 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7387 }
7388 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007389 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007390}
7391
7392#ifdef FEAT_CINDENT
7393/*
7394 * Return TRUE if C-indenting is on.
7395 */
7396 static int
7397cindent_on()
7398{
7399 return (!p_paste && (curbuf->b_p_cin
7400# ifdef FEAT_EVAL
7401 || *curbuf->b_p_inde != NUL
7402# endif
7403 ));
7404}
7405#endif
7406
7407#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7408/*
7409 * Re-indent the current line, based on the current contents of it and the
7410 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7411 * confused what all the part that handles Control-T is doing that I'm not.
7412 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7413 */
7414
7415 void
7416fixthisline(get_the_indent)
7417 int (*get_the_indent) __ARGS((void));
7418{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007419 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007420 if (linewhite(curwin->w_cursor.lnum))
7421 did_ai = TRUE; /* delete the indent if the line stays empty */
7422}
7423
7424 void
7425fix_indent()
7426{
7427 if (p_paste)
7428 return;
7429# ifdef FEAT_LISP
7430 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7431 fixthisline(get_lisp_indent);
7432# endif
7433# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7434 else
7435# endif
7436# ifdef FEAT_CINDENT
7437 if (cindent_on())
7438 do_c_expr_indent();
7439# endif
7440}
7441
7442#endif
7443
7444#ifdef FEAT_CINDENT
7445/*
7446 * return TRUE if 'cinkeys' contains the key "keytyped",
7447 * when == '*': Only if key is preceded with '*' (indent before insert)
7448 * when == '!': Only if key is prededed with '!' (don't insert)
7449 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7450 *
7451 * "keytyped" can have a few special values:
7452 * KEY_OPEN_FORW
7453 * KEY_OPEN_BACK
7454 * KEY_COMPLETE just finished completion.
7455 *
7456 * If line_is_empty is TRUE accept keys with '0' before them.
7457 */
7458 int
7459in_cinkeys(keytyped, when, line_is_empty)
7460 int keytyped;
7461 int when;
7462 int line_is_empty;
7463{
7464 char_u *look;
7465 int try_match;
7466 int try_match_word;
7467 char_u *p;
7468 char_u *line;
7469 int icase;
7470 int i;
7471
Bram Moolenaar30848942009-12-24 14:46:12 +00007472 if (keytyped == NUL)
7473 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7474 return FALSE;
7475
Bram Moolenaar071d4272004-06-13 20:20:40 +00007476#ifdef FEAT_EVAL
7477 if (*curbuf->b_p_inde != NUL)
7478 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7479 else
7480#endif
7481 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7482 while (*look)
7483 {
7484 /*
7485 * Find out if we want to try a match with this key, depending on
7486 * 'when' and a '*' or '!' before the key.
7487 */
7488 switch (when)
7489 {
7490 case '*': try_match = (*look == '*'); break;
7491 case '!': try_match = (*look == '!'); break;
7492 default: try_match = (*look != '*'); break;
7493 }
7494 if (*look == '*' || *look == '!')
7495 ++look;
7496
7497 /*
7498 * If there is a '0', only accept a match if the line is empty.
7499 * But may still match when typing last char of a word.
7500 */
7501 if (*look == '0')
7502 {
7503 try_match_word = try_match;
7504 if (!line_is_empty)
7505 try_match = FALSE;
7506 ++look;
7507 }
7508 else
7509 try_match_word = FALSE;
7510
7511 /*
7512 * does it look like a control character?
7513 */
7514 if (*look == '^'
7515#ifdef EBCDIC
7516 && (Ctrl_chr(look[1]) != 0)
7517#else
7518 && look[1] >= '?' && look[1] <= '_'
7519#endif
7520 )
7521 {
7522 if (try_match && keytyped == Ctrl_chr(look[1]))
7523 return TRUE;
7524 look += 2;
7525 }
7526 /*
7527 * 'o' means "o" command, open forward.
7528 * 'O' means "O" command, open backward.
7529 */
7530 else if (*look == 'o')
7531 {
7532 if (try_match && keytyped == KEY_OPEN_FORW)
7533 return TRUE;
7534 ++look;
7535 }
7536 else if (*look == 'O')
7537 {
7538 if (try_match && keytyped == KEY_OPEN_BACK)
7539 return TRUE;
7540 ++look;
7541 }
7542
7543 /*
7544 * 'e' means to check for "else" at start of line and just before the
7545 * cursor.
7546 */
7547 else if (*look == 'e')
7548 {
7549 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7550 {
7551 p = ml_get_curline();
7552 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7553 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7554 return TRUE;
7555 }
7556 ++look;
7557 }
7558
7559 /*
7560 * ':' only causes an indent if it is at the end of a label or case
7561 * statement, or when it was before typing the ':' (to fix
7562 * class::method for C++).
7563 */
7564 else if (*look == ':')
7565 {
7566 if (try_match && keytyped == ':')
7567 {
7568 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007569 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7570 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007571 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007572 /* Need to get the line again after cin_islabel(). */
7573 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 if (curwin->w_cursor.col > 2
7575 && p[curwin->w_cursor.col - 1] == ':'
7576 && p[curwin->w_cursor.col - 2] == ':')
7577 {
7578 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007579 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007580 || cin_islabel(30));
7581 p = ml_get_curline();
7582 p[curwin->w_cursor.col - 1] = ':';
7583 if (i)
7584 return TRUE;
7585 }
7586 }
7587 ++look;
7588 }
7589
7590
7591 /*
7592 * Is it a key in <>, maybe?
7593 */
7594 else if (*look == '<')
7595 {
7596 if (try_match)
7597 {
7598 /*
7599 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7600 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7601 * >, *, : and ! keys if they really really want to.
7602 */
7603 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7604 && keytyped == look[1])
7605 return TRUE;
7606
7607 if (keytyped == get_special_key_code(look + 1))
7608 return TRUE;
7609 }
7610 while (*look && *look != '>')
7611 look++;
7612 while (*look == '>')
7613 look++;
7614 }
7615
7616 /*
7617 * Is it a word: "=word"?
7618 */
7619 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7620 {
7621 ++look;
7622 if (*look == '~')
7623 {
7624 icase = TRUE;
7625 ++look;
7626 }
7627 else
7628 icase = FALSE;
7629 p = vim_strchr(look, ',');
7630 if (p == NULL)
7631 p = look + STRLEN(look);
7632 if ((try_match || try_match_word)
7633 && curwin->w_cursor.col >= (colnr_T)(p - look))
7634 {
7635 int match = FALSE;
7636
7637#ifdef FEAT_INS_EXPAND
7638 if (keytyped == KEY_COMPLETE)
7639 {
7640 char_u *s;
7641
7642 /* Just completed a word, check if it starts with "look".
7643 * search back for the start of a word. */
7644 line = ml_get_curline();
7645# ifdef FEAT_MBYTE
7646 if (has_mbyte)
7647 {
7648 char_u *n;
7649
7650 for (s = line + curwin->w_cursor.col; s > line; s = n)
7651 {
7652 n = mb_prevptr(line, s);
7653 if (!vim_iswordp(n))
7654 break;
7655 }
7656 }
7657 else
7658# endif
7659 for (s = line + curwin->w_cursor.col; s > line; --s)
7660 if (!vim_iswordc(s[-1]))
7661 break;
7662 if (s + (p - look) <= line + curwin->w_cursor.col
7663 && (icase
7664 ? MB_STRNICMP(s, look, p - look)
7665 : STRNCMP(s, look, p - look)) == 0)
7666 match = TRUE;
7667 }
7668 else
7669#endif
7670 /* TODO: multi-byte */
7671 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7672 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7673 {
7674 line = ml_get_cursor();
7675 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7676 || !vim_iswordc(line[-(p - look) - 1]))
7677 && (icase
7678 ? MB_STRNICMP(line - (p - look), look, p - look)
7679 : STRNCMP(line - (p - look), look, p - look))
7680 == 0)
7681 match = TRUE;
7682 }
7683 if (match && try_match_word && !try_match)
7684 {
7685 /* "0=word": Check if there are only blanks before the
7686 * word. */
7687 line = ml_get_curline();
7688 if ((int)(skipwhite(line) - line) !=
7689 (int)(curwin->w_cursor.col - (p - look)))
7690 match = FALSE;
7691 }
7692 if (match)
7693 return TRUE;
7694 }
7695 look = p;
7696 }
7697
7698 /*
7699 * ok, it's a boring generic character.
7700 */
7701 else
7702 {
7703 if (try_match && *look == keytyped)
7704 return TRUE;
7705 ++look;
7706 }
7707
7708 /*
7709 * Skip over ", ".
7710 */
7711 look = skip_to_option_part(look);
7712 }
7713 return FALSE;
7714}
7715#endif /* FEAT_CINDENT */
7716
7717#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7718/*
7719 * Map Hebrew keyboard when in hkmap mode.
7720 */
7721 int
7722hkmap(c)
7723 int c;
7724{
7725 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7726 {
7727 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7728 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7729 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7730 static char_u map[26] =
7731 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7732 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7733 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7734 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7735 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7736 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7737 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7738 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7739 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7740
7741 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7742 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7743 /* '-1'='sofit' */
7744 else if (c == 'x')
7745 return 'X';
7746 else if (c == 'q')
7747 return '\''; /* {geresh}={'} */
7748 else if (c == 246)
7749 return ' '; /* \"o --> ' ' for a german keyboard */
7750 else if (c == 228)
7751 return ' '; /* \"a --> ' ' -- / -- */
7752 else if (c == 252)
7753 return ' '; /* \"u --> ' ' -- / -- */
7754#ifdef EBCDIC
7755 else if (islower(c))
7756#else
7757 /* NOTE: islower() does not do the right thing for us on Linux so we
7758 * do this the same was as 5.7 and previous, so it works correctly on
7759 * all systems. Specifically, the e.g. Delete and Arrow keys are
7760 * munged and won't work if e.g. searching for Hebrew text.
7761 */
7762 else if (c >= 'a' && c <= 'z')
7763#endif
7764 return (int)(map[CharOrdLow(c)] + p_aleph);
7765 else
7766 return c;
7767 }
7768 else
7769 {
7770 switch (c)
7771 {
7772 case '`': return ';';
7773 case '/': return '.';
7774 case '\'': return ',';
7775 case 'q': return '/';
7776 case 'w': return '\'';
7777
7778 /* Hebrew letters - set offset from 'a' */
7779 case ',': c = '{'; break;
7780 case '.': c = 'v'; break;
7781 case ';': c = 't'; break;
7782 default: {
7783 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7784
7785#ifdef EBCDIC
7786 /* see note about islower() above */
7787 if (!islower(c))
7788#else
7789 if (c < 'a' || c > 'z')
7790#endif
7791 return c;
7792 c = str[CharOrdLow(c)];
7793 break;
7794 }
7795 }
7796
7797 return (int)(CharOrdLow(c) + p_aleph);
7798 }
7799}
7800#endif
7801
7802 static void
7803ins_reg()
7804{
7805 int need_redraw = FALSE;
7806 int regname;
7807 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007808#ifdef FEAT_VISUAL
7809 int vis_active = VIsual_active;
7810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007811
7812 /*
7813 * If we are going to wait for a character, show a '"'.
7814 */
7815 pc_status = PC_STATUS_UNSET;
7816 if (redrawing() && !char_avail())
7817 {
7818 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007819 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007820
7821 edit_putchar('"', TRUE);
7822#ifdef FEAT_CMDL_INFO
7823 add_to_showcmd_c(Ctrl_R);
7824#endif
7825 }
7826
7827#ifdef USE_ON_FLY_SCROLL
7828 dont_scroll = TRUE; /* disallow scrolling here */
7829#endif
7830
7831 /*
7832 * Don't map the register name. This also prevents the mode message to be
7833 * deleted when ESC is hit.
7834 */
7835 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007836 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007837 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007838 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7839 {
7840 /* Get a third key for literal register insertion */
7841 literally = regname;
7842#ifdef FEAT_CMDL_INFO
7843 add_to_showcmd_c(literally);
7844#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007845 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007846 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007847 }
7848 --no_mapping;
7849
7850#ifdef FEAT_EVAL
7851 /*
7852 * Don't call u_sync() while getting the expression,
7853 * evaluating it or giving an error message for it!
7854 */
7855 ++no_u_sync;
7856 if (regname == '=')
7857 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007858# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007859 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007860# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007861 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007862# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007863 /* Restore the Input Method. */
7864 if (im_on)
7865 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007866# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007867 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007868 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7869 {
7870 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007871 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007873 else
7874 {
7875#endif
7876 if (literally == Ctrl_O || literally == Ctrl_P)
7877 {
7878 /* Append the command to the redo buffer. */
7879 AppendCharToRedobuff(Ctrl_R);
7880 AppendCharToRedobuff(literally);
7881 AppendCharToRedobuff(regname);
7882
7883 do_put(regname, BACKWARD, 1L,
7884 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7885 }
7886 else if (insert_reg(regname, literally) == FAIL)
7887 {
7888 vim_beep();
7889 need_redraw = TRUE; /* remove the '"' */
7890 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007891 else if (stop_insert_mode)
7892 /* When the '=' register was used and a function was invoked that
7893 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7894 * insert anything, need to remove the '"' */
7895 need_redraw = TRUE;
7896
Bram Moolenaar071d4272004-06-13 20:20:40 +00007897#ifdef FEAT_EVAL
7898 }
7899 --no_u_sync;
7900#endif
7901#ifdef FEAT_CMDL_INFO
7902 clear_showcmd();
7903#endif
7904
7905 /* If the inserted register is empty, we need to remove the '"' */
7906 if (need_redraw || stuff_empty())
7907 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007908
7909#ifdef FEAT_VISUAL
7910 /* Disallow starting Visual mode here, would get a weird mode. */
7911 if (!vis_active && VIsual_active)
7912 end_visual_mode();
7913#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007914}
7915
7916/*
7917 * CTRL-G commands in Insert mode.
7918 */
7919 static void
7920ins_ctrl_g()
7921{
7922 int c;
7923
7924#ifdef FEAT_INS_EXPAND
7925 /* Right after CTRL-X the cursor will be after the ruler. */
7926 setcursor();
7927#endif
7928
7929 /*
7930 * Don't map the second key. This also prevents the mode message to be
7931 * deleted when ESC is hit.
7932 */
7933 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007934 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007935 --no_mapping;
7936 switch (c)
7937 {
7938 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7939 case K_UP:
7940 case Ctrl_K:
7941 case 'k': ins_up(TRUE);
7942 break;
7943
7944 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7945 case K_DOWN:
7946 case Ctrl_J:
7947 case 'j': ins_down(TRUE);
7948 break;
7949
7950 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007951 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007952 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007953
7954 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007955 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007956 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007957 break;
7958
7959 /* Unknown CTRL-G command, reserved for future expansion. */
7960 default: vim_beep();
7961 }
7962}
7963
7964/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007965 * CTRL-^ in Insert mode.
7966 */
7967 static void
7968ins_ctrl_hat()
7969{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007970 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007971 {
7972 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7973 if (State & LANGMAP)
7974 {
7975 curbuf->b_p_iminsert = B_IMODE_NONE;
7976 State &= ~LANGMAP;
7977 }
7978 else
7979 {
7980 curbuf->b_p_iminsert = B_IMODE_LMAP;
7981 State |= LANGMAP;
7982#ifdef USE_IM_CONTROL
7983 im_set_active(FALSE);
7984#endif
7985 }
7986 }
7987#ifdef USE_IM_CONTROL
7988 else
7989 {
7990 /* There are no ":lmap" mappings, toggle IM */
7991 if (im_get_status())
7992 {
7993 curbuf->b_p_iminsert = B_IMODE_NONE;
7994 im_set_active(FALSE);
7995 }
7996 else
7997 {
7998 curbuf->b_p_iminsert = B_IMODE_IM;
7999 State &= ~LANGMAP;
8000 im_set_active(TRUE);
8001 }
8002 }
8003#endif
8004 set_iminsert_global();
8005 showmode();
8006#ifdef FEAT_GUI
8007 /* may show different cursor shape or color */
8008 if (gui.in_use)
8009 gui_update_cursor(TRUE, FALSE);
8010#endif
8011#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8012 /* Show/unshow value of 'keymap' in status lines. */
8013 status_redraw_curbuf();
8014#endif
8015}
8016
8017/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008018 * Handle ESC in insert mode.
8019 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8020 * insert.
8021 */
8022 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008023ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008024 long *count;
8025 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008026 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027{
8028 int temp;
8029 static int disabled_redraw = FALSE;
8030
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008031#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008032 check_spell_redraw();
8033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008034#if defined(FEAT_HANGULIN)
8035# if defined(ESC_CHG_TO_ENG_MODE)
8036 hangul_input_state_set(0);
8037# endif
8038 if (composing_hangul)
8039 {
8040 push_raw_key(composing_hangul_buffer, 2);
8041 composing_hangul = 0;
8042 }
8043#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008044
8045 temp = curwin->w_cursor.col;
8046 if (disabled_redraw)
8047 {
8048 --RedrawingDisabled;
8049 disabled_redraw = FALSE;
8050 }
8051 if (!arrow_used)
8052 {
8053 /*
8054 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008055 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8056 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 */
8058 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008059 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060
8061 /*
8062 * Repeating insert may take a long time. Check for
8063 * interrupt now and then.
8064 */
8065 if (*count > 0)
8066 {
8067 line_breakcheck();
8068 if (got_int)
8069 *count = 0;
8070 }
8071
8072 if (--*count > 0) /* repeat what was typed */
8073 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008074 /* Vi repeats the insert without replacing characters. */
8075 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8076 State &= ~REPLACE_FLAG;
8077
Bram Moolenaar071d4272004-06-13 20:20:40 +00008078 (void)start_redo_ins();
8079 if (cmdchar == 'r' || cmdchar == 'v')
8080 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8081 ++RedrawingDisabled;
8082 disabled_redraw = TRUE;
8083 return FALSE; /* repeat the insert */
8084 }
8085 stop_insert(&curwin->w_cursor, TRUE);
8086 undisplay_dollar();
8087 }
8088
8089 /* When an autoindent was removed, curswant stays after the
8090 * indent */
8091 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8092 curwin->w_set_curswant = TRUE;
8093
8094 /* Remember the last Insert position in the '^ mark. */
8095 if (!cmdmod.keepjumps)
8096 curbuf->b_last_insert = curwin->w_cursor;
8097
8098 /*
8099 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008100 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008102 if (!nomove
8103 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104#ifdef FEAT_VIRTUALEDIT
8105 || curwin->w_cursor.coladd > 0
8106#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008107 )
8108 && (restart_edit == NUL
8109 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008110#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008111 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008113 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008114#ifdef FEAT_RIGHTLEFT
8115 && !revins_on
8116#endif
8117 )
8118 {
8119#ifdef FEAT_VIRTUALEDIT
8120 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8121 {
8122 oneleft();
8123 if (restart_edit != NUL)
8124 ++curwin->w_cursor.coladd;
8125 }
8126 else
8127#endif
8128 {
8129 --curwin->w_cursor.col;
8130#ifdef FEAT_MBYTE
8131 /* Correct cursor for multi-byte character. */
8132 if (has_mbyte)
8133 mb_adjust_cursor();
8134#endif
8135 }
8136 }
8137
8138#ifdef USE_IM_CONTROL
8139 /* Disable IM to allow typing English directly for Normal mode commands.
8140 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8141 * well). */
8142 if (!(State & LANGMAP))
8143 im_save_status(&curbuf->b_p_iminsert);
8144 im_set_active(FALSE);
8145#endif
8146
8147 State = NORMAL;
8148 /* need to position cursor again (e.g. when on a TAB ) */
8149 changed_cline_bef_curs();
8150
8151#ifdef FEAT_MOUSE
8152 setmouse();
8153#endif
8154#ifdef CURSOR_SHAPE
8155 ui_cursor_shape(); /* may show different cursor shape */
8156#endif
8157
8158 /*
8159 * When recording or for CTRL-O, need to display the new mode.
8160 * Otherwise remove the mode message.
8161 */
8162 if (Recording || restart_edit != NUL)
8163 showmode();
8164 else if (p_smd)
8165 MSG("");
8166
8167 return TRUE; /* exit Insert mode */
8168}
8169
8170#ifdef FEAT_RIGHTLEFT
8171/*
8172 * Toggle language: hkmap and revins_on.
8173 * Move to end of reverse inserted text.
8174 */
8175 static void
8176ins_ctrl_()
8177{
8178 if (revins_on && revins_chars && revins_scol >= 0)
8179 {
8180 while (gchar_cursor() != NUL && revins_chars--)
8181 ++curwin->w_cursor.col;
8182 }
8183 p_ri = !p_ri;
8184 revins_on = (State == INSERT && p_ri);
8185 if (revins_on)
8186 {
8187 revins_scol = curwin->w_cursor.col;
8188 revins_legal++;
8189 revins_chars = 0;
8190 undisplay_dollar();
8191 }
8192 else
8193 revins_scol = -1;
8194#ifdef FEAT_FKMAP
8195 if (p_altkeymap)
8196 {
8197 /*
8198 * to be consistent also for redo command, using '.'
8199 * set arrow_used to true and stop it - causing to redo
8200 * characters entered in one mode (normal/reverse insert).
8201 */
8202 arrow_used = TRUE;
8203 (void)stop_arrow();
8204 p_fkmap = curwin->w_p_rl ^ p_ri;
8205 if (p_fkmap && p_ri)
8206 State = INSERT;
8207 }
8208 else
8209#endif
8210 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8211 showmode();
8212}
8213#endif
8214
8215#ifdef FEAT_VISUAL
8216/*
8217 * If 'keymodel' contains "startsel", may start selection.
8218 * Returns TRUE when a CTRL-O and other keys stuffed.
8219 */
8220 static int
8221ins_start_select(c)
8222 int c;
8223{
8224 if (km_startsel)
8225 switch (c)
8226 {
8227 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 case K_PAGEUP:
8230 case K_KPAGEUP:
8231 case K_PAGEDOWN:
8232 case K_KPAGEDOWN:
8233# ifdef MACOS
8234 case K_LEFT:
8235 case K_RIGHT:
8236 case K_UP:
8237 case K_DOWN:
8238 case K_END:
8239 case K_HOME:
8240# endif
8241 if (!(mod_mask & MOD_MASK_SHIFT))
8242 break;
8243 /* FALLTHROUGH */
8244 case K_S_LEFT:
8245 case K_S_RIGHT:
8246 case K_S_UP:
8247 case K_S_DOWN:
8248 case K_S_END:
8249 case K_S_HOME:
8250 /* Start selection right away, the cursor can move with
8251 * CTRL-O when beyond the end of the line. */
8252 start_selection();
8253
8254 /* Execute the key in (insert) Select mode. */
8255 stuffcharReadbuff(Ctrl_O);
8256 if (mod_mask)
8257 {
8258 char_u buf[4];
8259
8260 buf[0] = K_SPECIAL;
8261 buf[1] = KS_MODIFIER;
8262 buf[2] = mod_mask;
8263 buf[3] = NUL;
8264 stuffReadbuff(buf);
8265 }
8266 stuffcharReadbuff(c);
8267 return TRUE;
8268 }
8269 return FALSE;
8270}
8271#endif
8272
8273/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008274 * <Insert> key in Insert mode: toggle insert/remplace mode.
8275 */
8276 static void
8277ins_insert(replaceState)
8278 int replaceState;
8279{
8280#ifdef FEAT_FKMAP
8281 if (p_fkmap && p_ri)
8282 {
8283 beep_flush();
8284 EMSG(farsi_text_3); /* encoded in Farsi */
8285 return;
8286 }
8287#endif
8288
8289#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008290# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008291 set_vim_var_string(VV_INSERTMODE,
8292 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008293# ifdef FEAT_VREPLACE
8294 replaceState == VREPLACE ? "v" :
8295# endif
8296 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008297# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008298 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8299#endif
8300 if (State & REPLACE_FLAG)
8301 State = INSERT | (State & LANGMAP);
8302 else
8303 State = replaceState | (State & LANGMAP);
8304 AppendCharToRedobuff(K_INS);
8305 showmode();
8306#ifdef CURSOR_SHAPE
8307 ui_cursor_shape(); /* may show different cursor shape */
8308#endif
8309}
8310
8311/*
8312 * Pressed CTRL-O in Insert mode.
8313 */
8314 static void
8315ins_ctrl_o()
8316{
8317#ifdef FEAT_VREPLACE
8318 if (State & VREPLACE_FLAG)
8319 restart_edit = 'V';
8320 else
8321#endif
8322 if (State & REPLACE_FLAG)
8323 restart_edit = 'R';
8324 else
8325 restart_edit = 'I';
8326#ifdef FEAT_VIRTUALEDIT
8327 if (virtual_active())
8328 ins_at_eol = FALSE; /* cursor always keeps its column */
8329 else
8330#endif
8331 ins_at_eol = (gchar_cursor() == NUL);
8332}
8333
8334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 * If the cursor is on an indent, ^T/^D insert/delete one
8336 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008337 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 * with vi. But vi only supports ^T and ^D after an
8339 * autoindent, we support it everywhere.
8340 */
8341 static void
8342ins_shift(c, lastc)
8343 int c;
8344 int lastc;
8345{
8346 if (stop_arrow() == FAIL)
8347 return;
8348 AppendCharToRedobuff(c);
8349
8350 /*
8351 * 0^D and ^^D: remove all indent.
8352 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008353 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8354 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {
8356 --curwin->w_cursor.col;
8357 (void)del_char(FALSE); /* delete the '^' or '0' */
8358 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8359 if (State & REPLACE_FLAG)
8360 replace_pop_ins();
8361 if (lastc == '^')
8362 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008363 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008364 }
8365 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008366 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367
8368 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8369 did_ai = FALSE;
8370#ifdef FEAT_SMARTINDENT
8371 did_si = FALSE;
8372 can_si = FALSE;
8373 can_si_back = FALSE;
8374#endif
8375#ifdef FEAT_CINDENT
8376 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8377#endif
8378}
8379
8380 static void
8381ins_del()
8382{
8383 int temp;
8384
8385 if (stop_arrow() == FAIL)
8386 return;
8387 if (gchar_cursor() == NUL) /* delete newline */
8388 {
8389 temp = curwin->w_cursor.col;
8390 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008391 || do_join(2, FALSE, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008392 vim_beep();
8393 else
8394 curwin->w_cursor.col = temp;
8395 }
8396 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8397 vim_beep();
8398 did_ai = FALSE;
8399#ifdef FEAT_SMARTINDENT
8400 did_si = FALSE;
8401 can_si = FALSE;
8402 can_si_back = FALSE;
8403#endif
8404 AppendCharToRedobuff(K_DEL);
8405}
8406
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008407static void ins_bs_one __ARGS((colnr_T *vcolp));
8408
8409/*
8410 * Delete one character for ins_bs().
8411 */
8412 static void
8413ins_bs_one(vcolp)
8414 colnr_T *vcolp;
8415{
8416 dec_cursor();
8417 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8418 if (State & REPLACE_FLAG)
8419 {
8420 /* Don't delete characters before the insert point when in
8421 * Replace mode */
8422 if (curwin->w_cursor.lnum != Insstart.lnum
8423 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008424 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008425 }
8426 else
8427 (void)del_char(FALSE);
8428}
8429
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430/*
8431 * Handle Backspace, delete-word and delete-line in Insert mode.
8432 * Return TRUE when backspace was actually used.
8433 */
8434 static int
8435ins_bs(c, mode, inserted_space_p)
8436 int c;
8437 int mode;
8438 int *inserted_space_p;
8439{
8440 linenr_T lnum;
8441 int cc;
8442 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008443 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 colnr_T mincol;
8445 int did_backspace = FALSE;
8446 int in_indent;
8447 int oldState;
8448#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008449 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450#endif
8451
8452 /*
8453 * can't delete anything in an empty file
8454 * can't backup past first character in buffer
8455 * can't backup past starting point unless 'backspace' > 1
8456 * can backup to a previous line if 'backspace' == 0
8457 */
8458 if ( bufempty()
8459 || (
8460#ifdef FEAT_RIGHTLEFT
8461 !revins_on &&
8462#endif
8463 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8464 || (!can_bs(BS_START)
8465 && (arrow_used
8466 || (curwin->w_cursor.lnum == Insstart.lnum
8467 && curwin->w_cursor.col <= Insstart.col)))
8468 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8469 && curwin->w_cursor.col <= ai_col)
8470 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8471 {
8472 vim_beep();
8473 return FALSE;
8474 }
8475
8476 if (stop_arrow() == FAIL)
8477 return FALSE;
8478 in_indent = inindent(0);
8479#ifdef FEAT_CINDENT
8480 if (in_indent)
8481 can_cindent = FALSE;
8482#endif
8483#ifdef FEAT_COMMENTS
8484 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8485#endif
8486#ifdef FEAT_RIGHTLEFT
8487 if (revins_on) /* put cursor after last inserted char */
8488 inc_cursor();
8489#endif
8490
8491#ifdef FEAT_VIRTUALEDIT
8492 /* Virtualedit:
8493 * BACKSPACE_CHAR eats a virtual space
8494 * BACKSPACE_WORD eats all coladd
8495 * BACKSPACE_LINE eats all coladd and keeps going
8496 */
8497 if (curwin->w_cursor.coladd > 0)
8498 {
8499 if (mode == BACKSPACE_CHAR)
8500 {
8501 --curwin->w_cursor.coladd;
8502 return TRUE;
8503 }
8504 if (mode == BACKSPACE_WORD)
8505 {
8506 curwin->w_cursor.coladd = 0;
8507 return TRUE;
8508 }
8509 curwin->w_cursor.coladd = 0;
8510 }
8511#endif
8512
8513 /*
8514 * delete newline!
8515 */
8516 if (curwin->w_cursor.col == 0)
8517 {
8518 lnum = Insstart.lnum;
8519 if (curwin->w_cursor.lnum == Insstart.lnum
8520#ifdef FEAT_RIGHTLEFT
8521 || revins_on
8522#endif
8523 )
8524 {
8525 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8526 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8527 return FALSE;
8528 --Insstart.lnum;
8529 Insstart.col = MAXCOL;
8530 }
8531 /*
8532 * In replace mode:
8533 * cc < 0: NL was inserted, delete it
8534 * cc >= 0: NL was replaced, put original characters back
8535 */
8536 cc = -1;
8537 if (State & REPLACE_FLAG)
8538 cc = replace_pop(); /* returns -1 if NL was inserted */
8539 /*
8540 * In replace mode, in the line we started replacing, we only move the
8541 * cursor.
8542 */
8543 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8544 {
8545 dec_cursor();
8546 }
8547 else
8548 {
8549#ifdef FEAT_VREPLACE
8550 if (!(State & VREPLACE_FLAG)
8551 || curwin->w_cursor.lnum > orig_line_count)
8552#endif
8553 {
8554 temp = gchar_cursor(); /* remember current char */
8555 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008556
8557 /* When "aw" is in 'formatoptions' we must delete the space at
8558 * the end of the line, otherwise the line will be broken
8559 * again when auto-formatting. */
8560 if (has_format_option(FO_AUTO)
8561 && has_format_option(FO_WHITE_PAR))
8562 {
8563 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8564 TRUE);
8565 int len;
8566
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008567 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008568 if (len > 0 && ptr[len - 1] == ' ')
8569 ptr[len - 1] = NUL;
8570 }
8571
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008572 (void)do_join(2, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008573 if (temp == NUL && gchar_cursor() != NUL)
8574 inc_cursor();
8575 }
8576#ifdef FEAT_VREPLACE
8577 else
8578 dec_cursor();
8579#endif
8580
8581 /*
8582 * In REPLACE mode we have to put back the text that was replaced
8583 * by the NL. On the replace stack is first a NUL-terminated
8584 * sequence of characters that were deleted and then the
8585 * characters that NL replaced.
8586 */
8587 if (State & REPLACE_FLAG)
8588 {
8589 /*
8590 * Do the next ins_char() in NORMAL state, to
8591 * prevent ins_char() from replacing characters and
8592 * avoiding showmatch().
8593 */
8594 oldState = State;
8595 State = NORMAL;
8596 /*
8597 * restore characters (blanks) deleted after cursor
8598 */
8599 while (cc > 0)
8600 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008601 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008602#ifdef FEAT_MBYTE
8603 mb_replace_pop_ins(cc);
8604#else
8605 ins_char(cc);
8606#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008607 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008608 cc = replace_pop();
8609 }
8610 /* restore the characters that NL replaced */
8611 replace_pop_ins();
8612 State = oldState;
8613 }
8614 }
8615 did_ai = FALSE;
8616 }
8617 else
8618 {
8619 /*
8620 * Delete character(s) before the cursor.
8621 */
8622#ifdef FEAT_RIGHTLEFT
8623 if (revins_on) /* put cursor on last inserted char */
8624 dec_cursor();
8625#endif
8626 mincol = 0;
8627 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008628 if (mode == BACKSPACE_LINE
8629 && (curbuf->b_p_ai
8630#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008631 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008632#endif
8633 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008634#ifdef FEAT_RIGHTLEFT
8635 && !revins_on
8636#endif
8637 )
8638 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008639 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008640 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008641 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008642 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008643 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 }
8645
8646 /*
8647 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8648 */
8649 if ( mode == BACKSPACE_CHAR
8650 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008651 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008652 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008653 && (*(ml_get_cursor() - 1) == TAB
8654 || (*(ml_get_cursor() - 1) == ' '
8655 && (!*inserted_space_p
8656 || arrow_used))))))
8657 {
8658 int ts;
8659 colnr_T vcol;
8660 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008661 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008662
8663 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008664 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 ts = curbuf->b_p_sw;
8666 else
8667 ts = curbuf->b_p_sts;
8668 /* Compute the virtual column where we want to be. Since
8669 * 'showbreak' may get in the way, need to get the last column of
8670 * the previous character. */
8671 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008672 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008673 dec_cursor();
8674 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8675 inc_cursor();
8676 want_vcol = (want_vcol / ts) * ts;
8677
8678 /* delete characters until we are at or before want_vcol */
8679 while (vcol > want_vcol
8680 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008681 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008682
8683 /* insert extra spaces until we are at want_vcol */
8684 while (vcol < want_vcol)
8685 {
8686 /* Remember the first char we inserted */
8687 if (curwin->w_cursor.lnum == Insstart.lnum
8688 && curwin->w_cursor.col < Insstart.col)
8689 Insstart.col = curwin->w_cursor.col;
8690
8691#ifdef FEAT_VREPLACE
8692 if (State & VREPLACE_FLAG)
8693 ins_char(' ');
8694 else
8695#endif
8696 {
8697 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008698 if ((State & REPLACE_FLAG))
8699 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 }
8701 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8702 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008703
8704 /* If we are now back where we started delete one character. Can
8705 * happen when using 'sts' and 'linebreak'. */
8706 if (vcol >= start_vcol)
8707 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008708 }
8709
8710 /*
8711 * Delete upto starting point, start of line or previous word.
8712 */
8713 else do
8714 {
8715#ifdef FEAT_RIGHTLEFT
8716 if (!revins_on) /* put cursor on char to be deleted */
8717#endif
8718 dec_cursor();
8719
8720 /* start of word? */
8721 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8722 {
8723 mode = BACKSPACE_WORD_NOT_SPACE;
8724 temp = vim_iswordc(gchar_cursor());
8725 }
8726 /* end of word? */
8727 else if (mode == BACKSPACE_WORD_NOT_SPACE
8728 && (vim_isspace(cc = gchar_cursor())
8729 || vim_iswordc(cc) != temp))
8730 {
8731#ifdef FEAT_RIGHTLEFT
8732 if (!revins_on)
8733#endif
8734 inc_cursor();
8735#ifdef FEAT_RIGHTLEFT
8736 else if (State & REPLACE_FLAG)
8737 dec_cursor();
8738#endif
8739 break;
8740 }
8741 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008742 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008743 else
8744 {
8745#ifdef FEAT_MBYTE
8746 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008747 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008748#endif
8749 (void)del_char(FALSE);
8750#ifdef FEAT_MBYTE
8751 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008752 * If there are combining characters and 'delcombine' is set
8753 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008754 * character.
8755 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008756 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 inc_cursor();
8758#endif
8759#ifdef FEAT_RIGHTLEFT
8760 if (revins_chars)
8761 {
8762 revins_chars--;
8763 revins_legal++;
8764 }
8765 if (revins_on && gchar_cursor() == NUL)
8766 break;
8767#endif
8768 }
8769 /* Just a single backspace?: */
8770 if (mode == BACKSPACE_CHAR)
8771 break;
8772 } while (
8773#ifdef FEAT_RIGHTLEFT
8774 revins_on ||
8775#endif
8776 (curwin->w_cursor.col > mincol
8777 && (curwin->w_cursor.lnum != Insstart.lnum
8778 || curwin->w_cursor.col != Insstart.col)));
8779 did_backspace = TRUE;
8780 }
8781#ifdef FEAT_SMARTINDENT
8782 did_si = FALSE;
8783 can_si = FALSE;
8784 can_si_back = FALSE;
8785#endif
8786 if (curwin->w_cursor.col <= 1)
8787 did_ai = FALSE;
8788 /*
8789 * It's a little strange to put backspaces into the redo
8790 * buffer, but it makes auto-indent a lot easier to deal
8791 * with.
8792 */
8793 AppendCharToRedobuff(c);
8794
8795 /* If deleted before the insertion point, adjust it */
8796 if (curwin->w_cursor.lnum == Insstart.lnum
8797 && curwin->w_cursor.col < Insstart.col)
8798 Insstart.col = curwin->w_cursor.col;
8799
8800 /* vi behaviour: the cursor moves backward but the character that
8801 * was there remains visible
8802 * Vim behaviour: the cursor moves backward and the character that
8803 * was there is erased from the screen.
8804 * We can emulate the vi behaviour by pretending there is a dollar
8805 * displayed even when there isn't.
8806 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8807 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8808 dollar_vcol = curwin->w_virtcol;
8809
Bram Moolenaarce3be472008-01-14 19:12:28 +00008810#ifdef FEAT_FOLDING
8811 /* When deleting a char the cursor line must never be in a closed fold.
8812 * E.g., when 'foldmethod' is indent and deleting the first non-white
8813 * char before a Tab. */
8814 if (did_backspace)
8815 foldOpenCursor();
8816#endif
8817
Bram Moolenaar071d4272004-06-13 20:20:40 +00008818 return did_backspace;
8819}
8820
8821#ifdef FEAT_MOUSE
8822 static void
8823ins_mouse(c)
8824 int c;
8825{
8826 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008827 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828
8829# ifdef FEAT_GUI
8830 /* When GUI is active, also move/paste when 'mouse' is empty */
8831 if (!gui.in_use)
8832# endif
8833 if (!mouse_has(MOUSE_INSERT))
8834 return;
8835
8836 undisplay_dollar();
8837 tpos = curwin->w_cursor;
8838 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8839 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008840#ifdef FEAT_WINDOWS
8841 win_T *new_curwin = curwin;
8842
8843 if (curwin != old_curwin && win_valid(old_curwin))
8844 {
8845 /* Mouse took us to another window. We need to go back to the
8846 * previous one to stop insert there properly. */
8847 curwin = old_curwin;
8848 curbuf = curwin->w_buffer;
8849 }
8850#endif
8851 start_arrow(curwin == old_curwin ? &tpos : NULL);
8852#ifdef FEAT_WINDOWS
8853 if (curwin != new_curwin && win_valid(new_curwin))
8854 {
8855 curwin = new_curwin;
8856 curbuf = curwin->w_buffer;
8857 }
8858#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859# ifdef FEAT_CINDENT
8860 can_cindent = TRUE;
8861# endif
8862 }
8863
8864#ifdef FEAT_WINDOWS
8865 /* redraw status lines (in case another window became active) */
8866 redraw_statuslines();
8867#endif
8868}
8869
8870 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02008871ins_mousescroll(dir)
8872 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008873{
8874 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008875# if defined(FEAT_WINDOWS)
8876 win_T *old_curwin = curwin;
8877# endif
8878# ifdef FEAT_INS_EXPAND
8879 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008880# endif
8881
8882 tpos = curwin->w_cursor;
8883
8884# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008885 /* Currently the mouse coordinates are only known in the GUI. */
8886 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8887 {
8888 int row, col;
8889
8890 row = mouse_row;
8891 col = mouse_col;
8892
8893 /* find the window at the pointer coordinates */
8894 curwin = mouse_find_win(&row, &col);
8895 curbuf = curwin->w_buffer;
8896 }
8897 if (curwin == old_curwin)
8898# endif
8899 undisplay_dollar();
8900
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008901# ifdef FEAT_INS_EXPAND
8902 /* Don't scroll the window in which completion is being done. */
8903 if (!pum_visible()
8904# if defined(FEAT_WINDOWS)
8905 || curwin != old_curwin
8906# endif
8907 )
8908# endif
8909 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02008910 if (dir == MSCR_DOWN || dir == MSCR_UP)
8911 {
8912 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8913 scroll_redraw(dir,
8914 (long)(curwin->w_botline - curwin->w_topline));
8915 else
8916 scroll_redraw(dir, 3L);
8917 }
8918#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008919 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02008920 {
8921 int val, step = 6;
8922
8923 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8924 step = W_WIDTH(curwin);
8925 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
8926 if (val < 0)
8927 val = 0;
8928 gui_do_horiz_scroll(val, TRUE);
8929 }
8930#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008931# ifdef FEAT_INS_EXPAND
8932 did_scroll = TRUE;
8933# endif
8934 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935
8936# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8937 curwin->w_redr_status = TRUE;
8938
8939 curwin = old_curwin;
8940 curbuf = curwin->w_buffer;
8941# endif
8942
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008943# ifdef FEAT_INS_EXPAND
8944 /* The popup menu may overlay the window, need to redraw it.
8945 * TODO: Would be more efficient to only redraw the windows that are
8946 * overlapped by the popup menu. */
8947 if (pum_visible() && did_scroll)
8948 {
8949 redraw_all_later(NOT_VALID);
8950 ins_compl_show_pum();
8951 }
8952# endif
8953
Bram Moolenaar071d4272004-06-13 20:20:40 +00008954 if (!equalpos(curwin->w_cursor, tpos))
8955 {
8956 start_arrow(&tpos);
8957# ifdef FEAT_CINDENT
8958 can_cindent = TRUE;
8959# endif
8960 }
8961}
8962#endif
8963
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008964#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008965 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008966ins_tabline(c)
8967 int c;
8968{
8969 /* We will be leaving the current window, unless closing another tab. */
8970 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8971 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8972 {
8973 undisplay_dollar();
8974 start_arrow(&curwin->w_cursor);
8975# ifdef FEAT_CINDENT
8976 can_cindent = TRUE;
8977# endif
8978 }
8979
8980 if (c == K_TABLINE)
8981 goto_tabpage(current_tab);
8982 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008983 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008984 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008985 redraw_statuslines(); /* will redraw the tabline when needed */
8986 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008987}
8988#endif
8989
8990#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 void
8992ins_scroll()
8993{
8994 pos_T tpos;
8995
8996 undisplay_dollar();
8997 tpos = curwin->w_cursor;
8998 if (gui_do_scroll())
8999 {
9000 start_arrow(&tpos);
9001# ifdef FEAT_CINDENT
9002 can_cindent = TRUE;
9003# endif
9004 }
9005}
9006
9007 void
9008ins_horscroll()
9009{
9010 pos_T tpos;
9011
9012 undisplay_dollar();
9013 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009014 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009015 {
9016 start_arrow(&tpos);
9017# ifdef FEAT_CINDENT
9018 can_cindent = TRUE;
9019# endif
9020 }
9021}
9022#endif
9023
9024 static void
9025ins_left()
9026{
9027 pos_T tpos;
9028
9029#ifdef FEAT_FOLDING
9030 if ((fdo_flags & FDO_HOR) && KeyTyped)
9031 foldOpenCursor();
9032#endif
9033 undisplay_dollar();
9034 tpos = curwin->w_cursor;
9035 if (oneleft() == OK)
9036 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009037#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9038 /* Only call start_arrow() when not busy with preediting, it will
9039 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9040 if (!im_is_preediting())
9041#endif
9042 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043#ifdef FEAT_RIGHTLEFT
9044 /* If exit reversed string, position is fixed */
9045 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9046 revins_legal++;
9047 revins_chars++;
9048#endif
9049 }
9050
9051 /*
9052 * if 'whichwrap' set for cursor in insert mode may go to
9053 * previous line
9054 */
9055 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9056 {
9057 start_arrow(&tpos);
9058 --(curwin->w_cursor.lnum);
9059 coladvance((colnr_T)MAXCOL);
9060 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9061 }
9062 else
9063 vim_beep();
9064}
9065
9066 static void
9067ins_home(c)
9068 int c;
9069{
9070 pos_T tpos;
9071
9072#ifdef FEAT_FOLDING
9073 if ((fdo_flags & FDO_HOR) && KeyTyped)
9074 foldOpenCursor();
9075#endif
9076 undisplay_dollar();
9077 tpos = curwin->w_cursor;
9078 if (c == K_C_HOME)
9079 curwin->w_cursor.lnum = 1;
9080 curwin->w_cursor.col = 0;
9081#ifdef FEAT_VIRTUALEDIT
9082 curwin->w_cursor.coladd = 0;
9083#endif
9084 curwin->w_curswant = 0;
9085 start_arrow(&tpos);
9086}
9087
9088 static void
9089ins_end(c)
9090 int c;
9091{
9092 pos_T tpos;
9093
9094#ifdef FEAT_FOLDING
9095 if ((fdo_flags & FDO_HOR) && KeyTyped)
9096 foldOpenCursor();
9097#endif
9098 undisplay_dollar();
9099 tpos = curwin->w_cursor;
9100 if (c == K_C_END)
9101 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9102 coladvance((colnr_T)MAXCOL);
9103 curwin->w_curswant = MAXCOL;
9104
9105 start_arrow(&tpos);
9106}
9107
9108 static void
9109ins_s_left()
9110{
9111#ifdef FEAT_FOLDING
9112 if ((fdo_flags & FDO_HOR) && KeyTyped)
9113 foldOpenCursor();
9114#endif
9115 undisplay_dollar();
9116 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9117 {
9118 start_arrow(&curwin->w_cursor);
9119 (void)bck_word(1L, FALSE, FALSE);
9120 curwin->w_set_curswant = TRUE;
9121 }
9122 else
9123 vim_beep();
9124}
9125
9126 static void
9127ins_right()
9128{
9129#ifdef FEAT_FOLDING
9130 if ((fdo_flags & FDO_HOR) && KeyTyped)
9131 foldOpenCursor();
9132#endif
9133 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009134 if (gchar_cursor() != NUL
9135#ifdef FEAT_VIRTUALEDIT
9136 || virtual_active()
9137#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009138 )
9139 {
9140 start_arrow(&curwin->w_cursor);
9141 curwin->w_set_curswant = TRUE;
9142#ifdef FEAT_VIRTUALEDIT
9143 if (virtual_active())
9144 oneright();
9145 else
9146#endif
9147 {
9148#ifdef FEAT_MBYTE
9149 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009150 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151 else
9152#endif
9153 ++curwin->w_cursor.col;
9154 }
9155
9156#ifdef FEAT_RIGHTLEFT
9157 revins_legal++;
9158 if (revins_chars)
9159 revins_chars--;
9160#endif
9161 }
9162 /* if 'whichwrap' set for cursor in insert mode, may move the
9163 * cursor to the next line */
9164 else if (vim_strchr(p_ww, ']') != NULL
9165 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9166 {
9167 start_arrow(&curwin->w_cursor);
9168 curwin->w_set_curswant = TRUE;
9169 ++curwin->w_cursor.lnum;
9170 curwin->w_cursor.col = 0;
9171 }
9172 else
9173 vim_beep();
9174}
9175
9176 static void
9177ins_s_right()
9178{
9179#ifdef FEAT_FOLDING
9180 if ((fdo_flags & FDO_HOR) && KeyTyped)
9181 foldOpenCursor();
9182#endif
9183 undisplay_dollar();
9184 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9185 || gchar_cursor() != NUL)
9186 {
9187 start_arrow(&curwin->w_cursor);
9188 (void)fwd_word(1L, FALSE, 0);
9189 curwin->w_set_curswant = TRUE;
9190 }
9191 else
9192 vim_beep();
9193}
9194
9195 static void
9196ins_up(startcol)
9197 int startcol; /* when TRUE move to Insstart.col */
9198{
9199 pos_T tpos;
9200 linenr_T old_topline = curwin->w_topline;
9201#ifdef FEAT_DIFF
9202 int old_topfill = curwin->w_topfill;
9203#endif
9204
9205 undisplay_dollar();
9206 tpos = curwin->w_cursor;
9207 if (cursor_up(1L, TRUE) == OK)
9208 {
9209 if (startcol)
9210 coladvance(getvcol_nolist(&Insstart));
9211 if (old_topline != curwin->w_topline
9212#ifdef FEAT_DIFF
9213 || old_topfill != curwin->w_topfill
9214#endif
9215 )
9216 redraw_later(VALID);
9217 start_arrow(&tpos);
9218#ifdef FEAT_CINDENT
9219 can_cindent = TRUE;
9220#endif
9221 }
9222 else
9223 vim_beep();
9224}
9225
9226 static void
9227ins_pageup()
9228{
9229 pos_T tpos;
9230
9231 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009232
9233#ifdef FEAT_WINDOWS
9234 if (mod_mask & MOD_MASK_CTRL)
9235 {
9236 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009237 if (first_tabpage->tp_next != NULL)
9238 {
9239 start_arrow(&curwin->w_cursor);
9240 goto_tabpage(-1);
9241 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009242 return;
9243 }
9244#endif
9245
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246 tpos = curwin->w_cursor;
9247 if (onepage(BACKWARD, 1L) == OK)
9248 {
9249 start_arrow(&tpos);
9250#ifdef FEAT_CINDENT
9251 can_cindent = TRUE;
9252#endif
9253 }
9254 else
9255 vim_beep();
9256}
9257
9258 static void
9259ins_down(startcol)
9260 int startcol; /* when TRUE move to Insstart.col */
9261{
9262 pos_T tpos;
9263 linenr_T old_topline = curwin->w_topline;
9264#ifdef FEAT_DIFF
9265 int old_topfill = curwin->w_topfill;
9266#endif
9267
9268 undisplay_dollar();
9269 tpos = curwin->w_cursor;
9270 if (cursor_down(1L, TRUE) == OK)
9271 {
9272 if (startcol)
9273 coladvance(getvcol_nolist(&Insstart));
9274 if (old_topline != curwin->w_topline
9275#ifdef FEAT_DIFF
9276 || old_topfill != curwin->w_topfill
9277#endif
9278 )
9279 redraw_later(VALID);
9280 start_arrow(&tpos);
9281#ifdef FEAT_CINDENT
9282 can_cindent = TRUE;
9283#endif
9284 }
9285 else
9286 vim_beep();
9287}
9288
9289 static void
9290ins_pagedown()
9291{
9292 pos_T tpos;
9293
9294 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009295
9296#ifdef FEAT_WINDOWS
9297 if (mod_mask & MOD_MASK_CTRL)
9298 {
9299 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009300 if (first_tabpage->tp_next != NULL)
9301 {
9302 start_arrow(&curwin->w_cursor);
9303 goto_tabpage(0);
9304 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009305 return;
9306 }
9307#endif
9308
Bram Moolenaar071d4272004-06-13 20:20:40 +00009309 tpos = curwin->w_cursor;
9310 if (onepage(FORWARD, 1L) == OK)
9311 {
9312 start_arrow(&tpos);
9313#ifdef FEAT_CINDENT
9314 can_cindent = TRUE;
9315#endif
9316 }
9317 else
9318 vim_beep();
9319}
9320
9321#ifdef FEAT_DND
9322 static void
9323ins_drop()
9324{
9325 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9326}
9327#endif
9328
9329/*
9330 * Handle TAB in Insert or Replace mode.
9331 * Return TRUE when the TAB needs to be inserted like a normal character.
9332 */
9333 static int
9334ins_tab()
9335{
9336 int ind;
9337 int i;
9338 int temp;
9339
9340 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9341 Insstart_blank_vcol = get_nolist_virtcol();
9342 if (echeck_abbr(TAB + ABBR_OFF))
9343 return FALSE;
9344
9345 ind = inindent(0);
9346#ifdef FEAT_CINDENT
9347 if (ind)
9348 can_cindent = FALSE;
9349#endif
9350
9351 /*
9352 * When nothing special, insert TAB like a normal character
9353 */
9354 if (!curbuf->b_p_et
9355 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9356 && curbuf->b_p_sts == 0)
9357 return TRUE;
9358
9359 if (stop_arrow() == FAIL)
9360 return TRUE;
9361
9362 did_ai = FALSE;
9363#ifdef FEAT_SMARTINDENT
9364 did_si = FALSE;
9365 can_si = FALSE;
9366 can_si_back = FALSE;
9367#endif
9368 AppendToRedobuff((char_u *)"\t");
9369
9370 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9371 temp = (int)curbuf->b_p_sw;
9372 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9373 temp = (int)curbuf->b_p_sts;
9374 else /* otherwise use 'tabstop' */
9375 temp = (int)curbuf->b_p_ts;
9376 temp -= get_nolist_virtcol() % temp;
9377
9378 /*
9379 * Insert the first space with ins_char(). It will delete one char in
9380 * replace mode. Insert the rest with ins_str(); it will not delete any
9381 * chars. For VREPLACE mode, we use ins_char() for all characters.
9382 */
9383 ins_char(' ');
9384 while (--temp > 0)
9385 {
9386#ifdef FEAT_VREPLACE
9387 if (State & VREPLACE_FLAG)
9388 ins_char(' ');
9389 else
9390#endif
9391 {
9392 ins_str((char_u *)" ");
9393 if (State & REPLACE_FLAG) /* no char replaced */
9394 replace_push(NUL);
9395 }
9396 }
9397
9398 /*
9399 * When 'expandtab' not set: Replace spaces by TABs where possible.
9400 */
9401 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9402 {
9403 char_u *ptr;
9404#ifdef FEAT_VREPLACE
9405 char_u *saved_line = NULL; /* init for GCC */
9406 pos_T pos;
9407#endif
9408 pos_T fpos;
9409 pos_T *cursor;
9410 colnr_T want_vcol, vcol;
9411 int change_col = -1;
9412 int save_list = curwin->w_p_list;
9413
9414 /*
9415 * Get the current line. For VREPLACE mode, don't make real changes
9416 * yet, just work on a copy of the line.
9417 */
9418#ifdef FEAT_VREPLACE
9419 if (State & VREPLACE_FLAG)
9420 {
9421 pos = curwin->w_cursor;
9422 cursor = &pos;
9423 saved_line = vim_strsave(ml_get_curline());
9424 if (saved_line == NULL)
9425 return FALSE;
9426 ptr = saved_line + pos.col;
9427 }
9428 else
9429#endif
9430 {
9431 ptr = ml_get_cursor();
9432 cursor = &curwin->w_cursor;
9433 }
9434
9435 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9436 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9437 curwin->w_p_list = FALSE;
9438
9439 /* Find first white before the cursor */
9440 fpos = curwin->w_cursor;
9441 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9442 {
9443 --fpos.col;
9444 --ptr;
9445 }
9446
9447 /* In Replace mode, don't change characters before the insert point. */
9448 if ((State & REPLACE_FLAG)
9449 && fpos.lnum == Insstart.lnum
9450 && fpos.col < Insstart.col)
9451 {
9452 ptr += Insstart.col - fpos.col;
9453 fpos.col = Insstart.col;
9454 }
9455
9456 /* compute virtual column numbers of first white and cursor */
9457 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9458 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9459
9460 /* Use as many TABs as possible. Beware of 'showbreak' and
9461 * 'linebreak' adding extra virtual columns. */
9462 while (vim_iswhite(*ptr))
9463 {
9464 i = lbr_chartabsize((char_u *)"\t", vcol);
9465 if (vcol + i > want_vcol)
9466 break;
9467 if (*ptr != TAB)
9468 {
9469 *ptr = TAB;
9470 if (change_col < 0)
9471 {
9472 change_col = fpos.col; /* Column of first change */
9473 /* May have to adjust Insstart */
9474 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9475 Insstart.col = fpos.col;
9476 }
9477 }
9478 ++fpos.col;
9479 ++ptr;
9480 vcol += i;
9481 }
9482
9483 if (change_col >= 0)
9484 {
9485 int repl_off = 0;
9486
9487 /* Skip over the spaces we need. */
9488 while (vcol < want_vcol && *ptr == ' ')
9489 {
9490 vcol += lbr_chartabsize(ptr, vcol);
9491 ++ptr;
9492 ++repl_off;
9493 }
9494 if (vcol > want_vcol)
9495 {
9496 /* Must have a char with 'showbreak' just before it. */
9497 --ptr;
9498 --repl_off;
9499 }
9500 fpos.col += repl_off;
9501
9502 /* Delete following spaces. */
9503 i = cursor->col - fpos.col;
9504 if (i > 0)
9505 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009506 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009507 /* correct replace stack. */
9508 if ((State & REPLACE_FLAG)
9509#ifdef FEAT_VREPLACE
9510 && !(State & VREPLACE_FLAG)
9511#endif
9512 )
9513 for (temp = i; --temp >= 0; )
9514 replace_join(repl_off);
9515 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009516#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009517 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009518 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009519 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009520 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9521 (char_u *)"\t", 1);
9522 }
9523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009524 cursor->col -= i;
9525
9526#ifdef FEAT_VREPLACE
9527 /*
9528 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9529 * backspacing over the changed spacing and then inserting the new
9530 * spacing.
9531 */
9532 if (State & VREPLACE_FLAG)
9533 {
9534 /* Backspace from real cursor to change_col */
9535 backspace_until_column(change_col);
9536
9537 /* Insert each char in saved_line from changed_col to
9538 * ptr-cursor */
9539 ins_bytes_len(saved_line + change_col,
9540 cursor->col - change_col);
9541 }
9542#endif
9543 }
9544
9545#ifdef FEAT_VREPLACE
9546 if (State & VREPLACE_FLAG)
9547 vim_free(saved_line);
9548#endif
9549 curwin->w_p_list = save_list;
9550 }
9551
9552 return FALSE;
9553}
9554
9555/*
9556 * Handle CR or NL in insert mode.
9557 * Return TRUE when out of memory or can't undo.
9558 */
9559 static int
9560ins_eol(c)
9561 int c;
9562{
9563 int i;
9564
9565 if (echeck_abbr(c + ABBR_OFF))
9566 return FALSE;
9567 if (stop_arrow() == FAIL)
9568 return TRUE;
9569 undisplay_dollar();
9570
9571 /*
9572 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9573 * character under the cursor. Only push a NUL on the replace stack,
9574 * nothing to put back when the NL is deleted.
9575 */
9576 if ((State & REPLACE_FLAG)
9577#ifdef FEAT_VREPLACE
9578 && !(State & VREPLACE_FLAG)
9579#endif
9580 )
9581 replace_push(NUL);
9582
9583 /*
9584 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9585 * replacing the next line, so we push all of the characters left on the
9586 * line onto the replace stack. This is not done here though, it is done
9587 * in open_line().
9588 */
9589
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009590#ifdef FEAT_VIRTUALEDIT
9591 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9592 * CTRL-O). */
9593 if (virtual_active() && curwin->w_cursor.coladd > 0)
9594 coladvance(getviscol());
9595#endif
9596
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597#ifdef FEAT_RIGHTLEFT
9598# ifdef FEAT_FKMAP
9599 if (p_altkeymap && p_fkmap)
9600 fkmap(NL);
9601# endif
9602 /* NL in reverse insert will always start in the end of
9603 * current line. */
9604 if (revins_on)
9605 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9606#endif
9607
9608 AppendToRedobuff(NL_STR);
9609 i = open_line(FORWARD,
9610#ifdef FEAT_COMMENTS
9611 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9612#endif
9613 0, old_indent);
9614 old_indent = 0;
9615#ifdef FEAT_CINDENT
9616 can_cindent = TRUE;
9617#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009618#ifdef FEAT_FOLDING
9619 /* When inserting a line the cursor line must never be in a closed fold. */
9620 foldOpenCursor();
9621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009622
9623 return (!i);
9624}
9625
9626#ifdef FEAT_DIGRAPHS
9627/*
9628 * Handle digraph in insert mode.
9629 * Returns character still to be inserted, or NUL when nothing remaining to be
9630 * done.
9631 */
9632 static int
9633ins_digraph()
9634{
9635 int c;
9636 int cc;
9637
9638 pc_status = PC_STATUS_UNSET;
9639 if (redrawing() && !char_avail())
9640 {
9641 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009642 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643
9644 edit_putchar('?', TRUE);
9645#ifdef FEAT_CMDL_INFO
9646 add_to_showcmd_c(Ctrl_K);
9647#endif
9648 }
9649
9650#ifdef USE_ON_FLY_SCROLL
9651 dont_scroll = TRUE; /* disallow scrolling here */
9652#endif
9653
9654 /* don't map the digraph chars. This also prevents the
9655 * mode message to be deleted when ESC is hit */
9656 ++no_mapping;
9657 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009658 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 --no_mapping;
9660 --allow_keys;
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009661 edit_unputchar(); /* when line fits in 'columns' the '?' is at the start
9662 of the next line and will not be redrawn */
9663
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664 if (IS_SPECIAL(c) || mod_mask) /* special key */
9665 {
9666#ifdef FEAT_CMDL_INFO
9667 clear_showcmd();
9668#endif
9669 insert_special(c, TRUE, FALSE);
9670 return NUL;
9671 }
9672 if (c != ESC)
9673 {
9674 if (redrawing() && !char_avail())
9675 {
9676 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009677 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009678
9679 if (char2cells(c) == 1)
9680 {
9681 /* first remove the '?', otherwise it's restored when typing
9682 * an ESC next */
9683 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009684 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009685 edit_putchar(c, TRUE);
9686 }
9687#ifdef FEAT_CMDL_INFO
9688 add_to_showcmd_c(c);
9689#endif
9690 }
9691 ++no_mapping;
9692 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009693 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694 --no_mapping;
9695 --allow_keys;
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009696 edit_unputchar(); /* when line fits in 'columns' the '?' is at the
9697 start of the next line and will not be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698 if (cc != ESC)
9699 {
9700 AppendToRedobuff((char_u *)CTRL_V_STR);
9701 c = getdigraph(c, cc, TRUE);
9702#ifdef FEAT_CMDL_INFO
9703 clear_showcmd();
9704#endif
9705 return c;
9706 }
9707 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009708#ifdef FEAT_CMDL_INFO
9709 clear_showcmd();
9710#endif
9711 return NUL;
9712}
9713#endif
9714
9715/*
9716 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9717 * Returns the char to be inserted, or NUL if none found.
9718 */
9719 static int
9720ins_copychar(lnum)
9721 linenr_T lnum;
9722{
9723 int c;
9724 int temp;
9725 char_u *ptr, *prev_ptr;
9726
9727 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9728 {
9729 vim_beep();
9730 return NUL;
9731 }
9732
9733 /* try to advance to the cursor column */
9734 temp = 0;
9735 ptr = ml_get(lnum);
9736 prev_ptr = ptr;
9737 validate_virtcol();
9738 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9739 {
9740 prev_ptr = ptr;
9741 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9742 }
9743 if ((colnr_T)temp > curwin->w_virtcol)
9744 ptr = prev_ptr;
9745
9746#ifdef FEAT_MBYTE
9747 c = (*mb_ptr2char)(ptr);
9748#else
9749 c = *ptr;
9750#endif
9751 if (c == NUL)
9752 vim_beep();
9753 return c;
9754}
9755
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009756/*
9757 * CTRL-Y or CTRL-E typed in Insert mode.
9758 */
9759 static int
9760ins_ctrl_ey(tc)
9761 int tc;
9762{
9763 int c = tc;
9764
9765#ifdef FEAT_INS_EXPAND
9766 if (ctrl_x_mode == CTRL_X_SCROLL)
9767 {
9768 if (c == Ctrl_Y)
9769 scrolldown_clamp();
9770 else
9771 scrollup_clamp();
9772 redraw_later(VALID);
9773 }
9774 else
9775#endif
9776 {
9777 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9778 if (c != NUL)
9779 {
9780 long tw_save;
9781
9782 /* The character must be taken literally, insert like it
9783 * was typed after a CTRL-V, and pretend 'textwidth'
9784 * wasn't set. Digits, 'o' and 'x' are special after a
9785 * CTRL-V, don't use it for these. */
9786 if (c < 256 && !isalnum(c))
9787 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9788 tw_save = curbuf->b_p_tw;
9789 curbuf->b_p_tw = -1;
9790 insert_special(c, TRUE, FALSE);
9791 curbuf->b_p_tw = tw_save;
9792#ifdef FEAT_RIGHTLEFT
9793 revins_chars++;
9794 revins_legal++;
9795#endif
9796 c = Ctrl_V; /* pretend CTRL-V is last character */
9797 auto_format(FALSE, TRUE);
9798 }
9799 }
9800 return c;
9801}
9802
Bram Moolenaar071d4272004-06-13 20:20:40 +00009803#ifdef FEAT_SMARTINDENT
9804/*
9805 * Try to do some very smart auto-indenting.
9806 * Used when inserting a "normal" character.
9807 */
9808 static void
9809ins_try_si(c)
9810 int c;
9811{
9812 pos_T *pos, old_pos;
9813 char_u *ptr;
9814 int i;
9815 int temp;
9816
9817 /*
9818 * do some very smart indenting when entering '{' or '}'
9819 */
9820 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9821 {
9822 /*
9823 * for '}' set indent equal to indent of line containing matching '{'
9824 */
9825 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9826 {
9827 old_pos = curwin->w_cursor;
9828 /*
9829 * If the matching '{' has a ')' immediately before it (ignoring
9830 * white-space), then line up with the start of the line
9831 * containing the matching '(' if there is one. This handles the
9832 * case where an "if (..\n..) {" statement continues over multiple
9833 * lines -- webb
9834 */
9835 ptr = ml_get(pos->lnum);
9836 i = pos->col;
9837 if (i > 0) /* skip blanks before '{' */
9838 while (--i > 0 && vim_iswhite(ptr[i]))
9839 ;
9840 curwin->w_cursor.lnum = pos->lnum;
9841 curwin->w_cursor.col = i;
9842 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9843 curwin->w_cursor = *pos;
9844 i = get_indent();
9845 curwin->w_cursor = old_pos;
9846#ifdef FEAT_VREPLACE
9847 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009848 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 else
9850#endif
9851 (void)set_indent(i, SIN_CHANGED);
9852 }
9853 else if (curwin->w_cursor.col > 0)
9854 {
9855 /*
9856 * when inserting '{' after "O" reduce indent, but not
9857 * more than indent of previous line
9858 */
9859 temp = TRUE;
9860 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9861 {
9862 old_pos = curwin->w_cursor;
9863 i = get_indent();
9864 while (curwin->w_cursor.lnum > 1)
9865 {
9866 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9867
9868 /* ignore empty lines and lines starting with '#'. */
9869 if (*ptr != '#' && *ptr != NUL)
9870 break;
9871 }
9872 if (get_indent() >= i)
9873 temp = FALSE;
9874 curwin->w_cursor = old_pos;
9875 }
9876 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009877 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009878 }
9879 }
9880
9881 /*
9882 * set indent of '#' always to 0
9883 */
9884 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9885 {
9886 /* remember current indent for next line */
9887 old_indent = get_indent();
9888 (void)set_indent(0, SIN_CHANGED);
9889 }
9890
9891 /* Adjust ai_col, the char at this position can be deleted. */
9892 if (ai_col > curwin->w_cursor.col)
9893 ai_col = curwin->w_cursor.col;
9894}
9895#endif
9896
9897/*
9898 * Get the value that w_virtcol would have when 'list' is off.
9899 * Unless 'cpo' contains the 'L' flag.
9900 */
9901 static colnr_T
9902get_nolist_virtcol()
9903{
9904 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9905 return getvcol_nolist(&curwin->w_cursor);
9906 validate_virtcol();
9907 return curwin->w_virtcol;
9908}