blob: ad146c86cb8d17721558d3d051fb629669fd9346 [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 Moolenaar37dd0182010-11-10 16:54:20 +010061#ifdef FEAT_COMPL_FUNC
62static char e_complwin[] = N_("E839: Completion function changed window");
63static char e_compldel[] = N_("E840: Completion function deleted text");
64#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000065
66/*
67 * Structure used to store one match for insert completion.
68 */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000069typedef struct compl_S compl_T;
70struct compl_S
Bram Moolenaar071d4272004-06-13 20:20:40 +000071{
Bram Moolenaar572cb562005-08-05 21:35:02 +000072 compl_T *cp_next;
73 compl_T *cp_prev;
74 char_u *cp_str; /* matched text */
Bram Moolenaard1f56e62006-02-22 21:25:37 +000075 char cp_icase; /* TRUE or FALSE: ignore case */
Bram Moolenaar39f05632006-03-19 22:15:26 +000076 char_u *(cp_text[CPT_COUNT]); /* text for the menu */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +000077 char_u *cp_fname; /* file containing the match, allocated when
78 * cp_flags has FREE_FNAME */
Bram Moolenaar572cb562005-08-05 21:35:02 +000079 int cp_flags; /* ORIGINAL_TEXT, CONT_S_IPOS or FREE_FNAME */
80 int cp_number; /* sequence number */
Bram Moolenaar071d4272004-06-13 20:20:40 +000081};
82
Bram Moolenaar572cb562005-08-05 21:35:02 +000083#define ORIGINAL_TEXT (1) /* the original text when the expansion begun */
Bram Moolenaar071d4272004-06-13 20:20:40 +000084#define FREE_FNAME (2)
85
86/*
87 * All the current matches are stored in a list.
Bram Moolenaar4be06f92005-07-29 22:36:03 +000088 * "compl_first_match" points to the start of the list.
89 * "compl_curr_match" points to the currently selected entry.
90 * "compl_shown_match" is different from compl_curr_match during
91 * ins_compl_get_exp().
Bram Moolenaar071d4272004-06-13 20:20:40 +000092 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000093static compl_T *compl_first_match = NULL;
94static compl_T *compl_curr_match = NULL;
95static compl_T *compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000096
Bram Moolenaar779b74b2006-04-10 14:55:34 +000097/* After using a cursor key <Enter> selects a match in the popup menu,
98 * otherwise it inserts a line break. */
99static int compl_enter_selects = FALSE;
100
Bram Moolenaara6557602006-02-04 22:43:20 +0000101/* When "compl_leader" is not NULL only matches that start with this string
102 * are used. */
103static char_u *compl_leader = NULL;
104
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000105static int compl_get_longest = FALSE; /* put longest common string
106 in compl_leader */
107
Bram Moolenaara6557602006-02-04 22:43:20 +0000108static int compl_used_match; /* Selected one of the matches. When
109 FALSE the match was edited or using
110 the longest common string. */
111
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000112static int compl_was_interrupted = FALSE; /* didn't finish finding
113 completions. */
114
115static int compl_restarting = FALSE; /* don't insert match */
116
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000117/* When the first completion is done "compl_started" is set. When it's
118 * FALSE the word to be completed must be located. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000119static int compl_started = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000120
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000121/* Set when doing something for completion that may call edit() recursively,
122 * which is not allowed. */
123static int compl_busy = FALSE;
124
Bram Moolenaar572cb562005-08-05 21:35:02 +0000125static int compl_matches = 0;
126static char_u *compl_pattern = NULL;
127static int compl_direction = FORWARD;
128static int compl_shows_dir = FORWARD;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +0000129static int compl_pending = 0; /* > 1 for postponed CTRL-N */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000130static pos_T compl_startpos;
131static colnr_T compl_col = 0; /* column where the text starts
132 * that is being completed */
Bram Moolenaar572cb562005-08-05 21:35:02 +0000133static char_u *compl_orig_text = NULL; /* text as it was before
134 * completion started */
135static int compl_cont_mode = 0;
136static expand_T compl_xp;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000137
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000138static void ins_ctrl_x __ARGS((void));
139static int has_compl_option __ARGS((int dict_opt));
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000140static int ins_compl_accept_char __ARGS((int c));
Bram Moolenaar89d40322006-08-29 15:30:07 +0000141static 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 +0000142static int ins_compl_equal __ARGS((compl_T *match, char_u *str, int len));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000143static void ins_compl_longest_match __ARGS((compl_T *match));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000144static void ins_compl_add_matches __ARGS((int num_matches, char_u **matches, int icase));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000145static int ins_compl_make_cyclic __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000146static void ins_compl_upd_pum __ARGS((void));
147static void ins_compl_del_pum __ARGS((void));
Bram Moolenaar280f1262006-01-30 00:14:18 +0000148static int pum_wanted __ARGS((void));
Bram Moolenaar65c923a2006-03-03 22:56:30 +0000149static int pum_enough_matches __ARGS((void));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000150static void ins_compl_dictionaries __ARGS((char_u *dict, char_u *pat, int flags, int thesaurus));
Bram Moolenaar0b238792006-03-02 22:49:12 +0000151static 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 +0000152static char_u *find_line_end __ARGS((char_u *ptr));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000153static void ins_compl_free __ARGS((void));
154static void ins_compl_clear __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000155static int ins_compl_bs __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000156static void ins_compl_new_leader __ARGS((void));
Bram Moolenaara6557602006-02-04 22:43:20 +0000157static void ins_compl_addleader __ARGS((int c));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000158static int ins_compl_len __ARGS((void));
Bram Moolenaar1423b9d2006-05-07 15:16:06 +0000159static void ins_compl_restart __ARGS((void));
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +0000160static void ins_compl_set_original_text __ARGS((char_u *str));
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000161static void ins_compl_addfrommatch __ARGS((void));
Bram Moolenaar1c7715d2005-10-03 22:02:18 +0000162static int ins_compl_prep __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000163static buf_T *ins_compl_next_buf __ARGS((buf_T *buf, int flag));
Bram Moolenaara94bc432006-03-10 21:42:59 +0000164#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL)
165static void ins_compl_add_list __ARGS((list_T *list));
166#endif
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000167static int ins_compl_get_exp __ARGS((pos_T *ini));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000168static void ins_compl_delete __ARGS((void));
169static void ins_compl_insert __ARGS((void));
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000170static int ins_compl_next __ARGS((int allow_get_expansion, int count, int insert_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000171static int ins_compl_key2dir __ARGS((int c));
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000172static int ins_compl_pum_key __ARGS((int c));
Bram Moolenaare3226be2005-12-18 22:10:00 +0000173static int ins_compl_key2count __ARGS((int c));
Bram Moolenaard1f56e62006-02-22 21:25:37 +0000174static int ins_compl_use_match __ARGS((int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000175static int ins_complete __ARGS((int c));
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000176static unsigned quote_meta __ARGS((char_u *dest, char_u *str, int len));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000177#endif /* FEAT_INS_EXPAND */
178
179#define BACKSPACE_CHAR 1
180#define BACKSPACE_WORD 2
181#define BACKSPACE_WORD_NOT_SPACE 3
182#define BACKSPACE_LINE 4
183
Bram Moolenaar754b5602006-02-09 23:53:20 +0000184static void ins_redraw __ARGS((int ready));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000185static void ins_ctrl_v __ARGS((void));
186static void undisplay_dollar __ARGS((void));
187static void insert_special __ARGS((int, int, int));
Bram Moolenaar97b98102009-11-17 16:41:01 +0000188static void internal_format __ARGS((int textwidth, int second_indent, int flags, int format_only, int c));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000189static void check_auto_format __ARGS((int));
190static void redo_literal __ARGS((int c));
191static void start_arrow __ARGS((pos_T *end_insert_pos));
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +0000192#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +0000193static void check_spell_redraw __ARGS((void));
Bram Moolenaar8aff23a2005-08-19 20:40:30 +0000194static void spell_back_to_badword __ARGS((void));
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +0000195static int spell_bad_len = 0; /* length of located bad word */
Bram Moolenaar217ad922005-03-20 22:37:15 +0000196#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197static void stop_insert __ARGS((pos_T *end_insert_pos, int esc));
198static int echeck_abbr __ARGS((int));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000199static int replace_pop __ARGS((void));
200static void replace_join __ARGS((int off));
201static void replace_pop_ins __ARGS((void));
202#ifdef FEAT_MBYTE
203static void mb_replace_pop_ins __ARGS((int cc));
204#endif
205static void replace_flush __ARGS((void));
Bram Moolenaar0f6c9482009-01-13 11:29:48 +0000206static void replace_do_bs __ARGS((int limit_col));
207static int del_char_after_col __ARGS((int limit_col));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208#ifdef FEAT_CINDENT
209static int cindent_on __ARGS((void));
210#endif
211static void ins_reg __ARGS((void));
212static void ins_ctrl_g __ARGS((void));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000213static void ins_ctrl_hat __ARGS((void));
Bram Moolenaar488c6512005-08-11 20:09:58 +0000214static int ins_esc __ARGS((long *count, int cmdchar, int nomove));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215#ifdef FEAT_RIGHTLEFT
216static void ins_ctrl_ __ARGS((void));
217#endif
218#ifdef FEAT_VISUAL
219static int ins_start_select __ARGS((int c));
220#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000221static void ins_insert __ARGS((int replaceState));
222static void ins_ctrl_o __ARGS((void));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000223static void ins_shift __ARGS((int c, int lastc));
224static void ins_del __ARGS((void));
225static int ins_bs __ARGS((int c, int mode, int *inserted_space_p));
226#ifdef FEAT_MOUSE
227static void ins_mouse __ARGS((int c));
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +0200228static void ins_mousescroll __ARGS((int dir));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000230#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
231static void ins_tabline __ARGS((int c));
232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233static void ins_left __ARGS((void));
234static void ins_home __ARGS((int c));
235static void ins_end __ARGS((int c));
236static void ins_s_left __ARGS((void));
237static void ins_right __ARGS((void));
238static void ins_s_right __ARGS((void));
239static void ins_up __ARGS((int startcol));
240static void ins_pageup __ARGS((void));
241static void ins_down __ARGS((int startcol));
242static void ins_pagedown __ARGS((void));
243#ifdef FEAT_DND
244static void ins_drop __ARGS((void));
245#endif
246static int ins_tab __ARGS((void));
247static int ins_eol __ARGS((int c));
248#ifdef FEAT_DIGRAPHS
249static int ins_digraph __ARGS((void));
250#endif
251static int ins_copychar __ARGS((linenr_T lnum));
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000252static int ins_ctrl_ey __ARGS((int tc));
Bram Moolenaar071d4272004-06-13 20:20:40 +0000253#ifdef FEAT_SMARTINDENT
254static void ins_try_si __ARGS((int c));
255#endif
256static colnr_T get_nolist_virtcol __ARGS((void));
257
258static colnr_T Insstart_textlen; /* length of line when insert started */
259static colnr_T Insstart_blank_vcol; /* vcol for first inserted blank */
260
261static char_u *last_insert = NULL; /* the text of the previous insert,
262 K_SPECIAL and CSI are escaped */
263static int last_insert_skip; /* nr of chars in front of previous insert */
264static int new_insert_skip; /* nr of chars in front of current insert */
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000265static int did_restart_edit; /* "restart_edit" when calling edit() */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
267#ifdef FEAT_CINDENT
268static int can_cindent; /* may do cindenting on this line */
269#endif
270
271static int old_indent = 0; /* for ^^D command in insert mode */
272
273#ifdef FEAT_RIGHTLEFT
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +0000274static int revins_on; /* reverse insert mode on */
275static int revins_chars; /* how much to skip after edit */
276static int revins_legal; /* was the last char 'legal'? */
277static int revins_scol; /* start column of revins session */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000278#endif
279
Bram Moolenaar071d4272004-06-13 20:20:40 +0000280static int ins_need_undo; /* call u_save() before inserting a
281 char. Set when edit() is called.
282 after that arrow_used is used. */
283
284static int did_add_space = FALSE; /* auto_format() added an extra space
285 under the cursor */
286
287/*
288 * edit(): Start inserting text.
289 *
290 * "cmdchar" can be:
291 * 'i' normal insert command
292 * 'a' normal append command
293 * 'R' replace command
294 * 'r' "r<CR>" command: insert one <CR>. Note: count can be > 1, for redo,
295 * but still only one <CR> is inserted. The <Esc> is not used for redo.
296 * 'g' "gI" command.
297 * 'V' "gR" command for Virtual Replace mode.
298 * 'v' "gr" command for single character Virtual Replace mode.
299 *
300 * This function is not called recursively. For CTRL-O commands, it returns
301 * and lets the caller handle the Normal-mode command.
302 *
303 * Return TRUE if a CTRL-O command caused the return (insert mode pending).
304 */
305 int
306edit(cmdchar, startln, count)
307 int cmdchar;
308 int startln; /* if set, insert at start of line */
309 long count;
310{
311 int c = 0;
312 char_u *ptr;
313 int lastc;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000314 int mincol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000315 static linenr_T o_lnum = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000316 int i;
317 int did_backspace = TRUE; /* previous char was backspace */
318#ifdef FEAT_CINDENT
319 int line_is_white = FALSE; /* line is empty before insert */
320#endif
321 linenr_T old_topline = 0; /* topline before insertion */
322#ifdef FEAT_DIFF
323 int old_topfill = -1;
324#endif
325 int inserted_space = FALSE; /* just inserted a space */
326 int replaceState = REPLACE;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000327 int nomove = FALSE; /* don't move cursor on return */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000328
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000329 /* Remember whether editing was restarted after CTRL-O. */
330 did_restart_edit = restart_edit;
331
Bram Moolenaar071d4272004-06-13 20:20:40 +0000332 /* sleep before redrawing, needed for "CTRL-O :" that results in an
333 * error message */
334 check_for_delay(TRUE);
335
336#ifdef HAVE_SANDBOX
337 /* Don't allow inserting in the sandbox. */
338 if (sandbox != 0)
339 {
340 EMSG(_(e_sandbox));
341 return FALSE;
342 }
343#endif
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000344 /* Don't allow changes in the buffer while editing the cmdline. The
345 * caller of getcmdline() may get confused. */
Bram Moolenaarb71eaae2006-01-20 23:10:18 +0000346 if (textlock != 0)
Bram Moolenaar8ada17c2006-01-19 22:16:24 +0000347 {
348 EMSG(_(e_secure));
349 return FALSE;
350 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351
352#ifdef FEAT_INS_EXPAND
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000353 /* Don't allow recursive insert mode when busy with completion. */
Bram Moolenaar031e0dd2009-07-09 16:15:16 +0000354 if (compl_started || compl_busy || pum_visible())
Bram Moolenaarf193fff2006-04-27 00:02:13 +0000355 {
356 EMSG(_(e_secure));
357 return FALSE;
358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000359 ins_compl_clear(); /* clear stuff for CTRL-X mode */
360#endif
361
Bram Moolenaar843ee412004-06-30 16:16:41 +0000362#ifdef FEAT_AUTOCMD
363 /*
364 * Trigger InsertEnter autocommands. Do not do this for "r<CR>" or "grx".
365 */
366 if (cmdchar != 'r' && cmdchar != 'v')
367 {
Bram Moolenaar1e015462005-09-25 22:16:38 +0000368# ifdef FEAT_EVAL
Bram Moolenaar843ee412004-06-30 16:16:41 +0000369 if (cmdchar == 'R')
370 ptr = (char_u *)"r";
371 else if (cmdchar == 'V')
372 ptr = (char_u *)"v";
373 else
374 ptr = (char_u *)"i";
375 set_vim_var_string(VV_INSERTMODE, ptr, 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +0000376# endif
Bram Moolenaar843ee412004-06-30 16:16:41 +0000377 apply_autocmds(EVENT_INSERTENTER, NULL, NULL, FALSE, curbuf);
378 }
379#endif
380
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200381#ifdef FEAT_CONCEAL
382 /* Check if the cursor line needs redrawing before changing State. If
383 * 'concealcursor' is "n" it needs to be redrawn without concealing. */
Bram Moolenaar8e469272010-07-28 19:38:16 +0200384 conceal_check_cursur_line();
Bram Moolenaarf5963f72010-07-23 22:10:27 +0200385#endif
386
Bram Moolenaar071d4272004-06-13 20:20:40 +0000387#ifdef FEAT_MOUSE
388 /*
389 * When doing a paste with the middle mouse button, Insstart is set to
390 * where the paste started.
391 */
392 if (where_paste_started.lnum != 0)
393 Insstart = where_paste_started;
394 else
395#endif
396 {
397 Insstart = curwin->w_cursor;
398 if (startln)
399 Insstart.col = 0;
400 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000401 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +0000402 Insstart_blank_vcol = MAXCOL;
403 if (!did_ai)
404 ai_col = 0;
405
406 if (cmdchar != NUL && restart_edit == 0)
407 {
408 ResetRedobuff();
409 AppendNumberToRedobuff(count);
410#ifdef FEAT_VREPLACE
411 if (cmdchar == 'V' || cmdchar == 'v')
412 {
413 /* "gR" or "gr" command */
414 AppendCharToRedobuff('g');
415 AppendCharToRedobuff((cmdchar == 'v') ? 'r' : 'R');
416 }
417 else
418#endif
419 {
420 AppendCharToRedobuff(cmdchar);
421 if (cmdchar == 'g') /* "gI" command */
422 AppendCharToRedobuff('I');
423 else if (cmdchar == 'r') /* "r<CR>" command */
424 count = 1; /* insert only one <CR> */
425 }
426 }
427
428 if (cmdchar == 'R')
429 {
430#ifdef FEAT_FKMAP
431 if (p_fkmap && p_ri)
432 {
433 beep_flush();
434 EMSG(farsi_text_3); /* encoded in Farsi */
435 State = INSERT;
436 }
437 else
438#endif
439 State = REPLACE;
440 }
441#ifdef FEAT_VREPLACE
442 else if (cmdchar == 'V' || cmdchar == 'v')
443 {
444 State = VREPLACE;
445 replaceState = VREPLACE;
446 orig_line_count = curbuf->b_ml.ml_line_count;
447 vr_lines_changed = 1;
448 }
449#endif
450 else
451 State = INSERT;
452
453 stop_insert_mode = FALSE;
454
455 /*
456 * Need to recompute the cursor position, it might move when the cursor is
457 * on a TAB or special character.
458 */
459 curs_columns(TRUE);
460
461 /*
462 * Enable langmap or IME, indicated by 'iminsert'.
463 * Note that IME may enabled/disabled without us noticing here, thus the
464 * 'iminsert' value may not reflect what is actually used. It is updated
465 * when hitting <Esc>.
466 */
467 if (curbuf->b_p_iminsert == B_IMODE_LMAP)
468 State |= LANGMAP;
469#ifdef USE_IM_CONTROL
470 im_set_active(curbuf->b_p_iminsert == B_IMODE_IM);
471#endif
472
Bram Moolenaar071d4272004-06-13 20:20:40 +0000473#ifdef FEAT_MOUSE
474 setmouse();
475#endif
476#ifdef FEAT_CMDL_INFO
477 clear_showcmd();
478#endif
479#ifdef FEAT_RIGHTLEFT
480 /* there is no reverse replace mode */
481 revins_on = (State == INSERT && p_ri);
482 if (revins_on)
483 undisplay_dollar();
484 revins_chars = 0;
485 revins_legal = 0;
486 revins_scol = -1;
487#endif
488
489 /*
490 * Handle restarting Insert mode.
491 * Don't do this for "CTRL-O ." (repeat an insert): we get here with
492 * restart_edit non-zero, and something in the stuff buffer.
493 */
494 if (restart_edit != 0 && stuff_empty())
495 {
496#ifdef FEAT_MOUSE
497 /*
498 * After a paste we consider text typed to be part of the insert for
499 * the pasted text. You can backspace over the pasted text too.
500 */
501 if (where_paste_started.lnum)
502 arrow_used = FALSE;
503 else
504#endif
505 arrow_used = TRUE;
506 restart_edit = 0;
507
508 /*
509 * If the cursor was after the end-of-line before the CTRL-O and it is
510 * now at the end-of-line, put it after the end-of-line (this is not
511 * correct in very rare cases).
512 * Also do this if curswant is greater than the current virtual
513 * column. Eg after "^O$" or "^O80|".
514 */
515 validate_virtcol();
516 update_curswant();
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000517 if (((ins_at_eol && curwin->w_cursor.lnum == o_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 || curwin->w_curswant > curwin->w_virtcol)
519 && *(ptr = ml_get_curline() + curwin->w_cursor.col) != NUL)
520 {
521 if (ptr[1] == NUL)
522 ++curwin->w_cursor.col;
523#ifdef FEAT_MBYTE
524 else if (has_mbyte)
525 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000526 i = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527 if (ptr[i] == NUL)
528 curwin->w_cursor.col += i;
529 }
530#endif
531 }
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000532 ins_at_eol = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000533 }
534 else
535 arrow_used = FALSE;
536
537 /* we are in insert mode now, don't need to start it anymore */
538 need_start_insertmode = FALSE;
539
540 /* Need to save the line for undo before inserting the first char. */
541 ins_need_undo = TRUE;
542
543#ifdef FEAT_MOUSE
544 where_paste_started.lnum = 0;
545#endif
546#ifdef FEAT_CINDENT
547 can_cindent = TRUE;
548#endif
549#ifdef FEAT_FOLDING
550 /* The cursor line is not in a closed fold, unless 'insertmode' is set or
551 * restarting. */
552 if (!p_im && did_restart_edit == 0)
553 foldOpenCursor();
554#endif
555
556 /*
557 * If 'showmode' is set, show the current (insert/replace/..) mode.
558 * A warning message for changing a readonly file is given here, before
559 * actually changing anything. It's put after the mode, if any.
560 */
561 i = 0;
Bram Moolenaard12f5c12006-01-25 22:10:52 +0000562 if (p_smd && msg_silent == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000563 i = showmode();
564
565 if (!p_im && did_restart_edit == 0)
Bram Moolenaar21af89e2008-01-02 21:09:33 +0000566 change_warning(i == 0 ? 0 : i + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000567
568#ifdef CURSOR_SHAPE
569 ui_cursor_shape(); /* may show different cursor shape */
570#endif
571#ifdef FEAT_DIGRAPHS
572 do_digraph(-1); /* clear digraphs */
573#endif
574
Bram Moolenaar83c465c2005-12-16 21:53:56 +0000575 /*
576 * Get the current length of the redo buffer, those characters have to be
577 * skipped if we want to get to the inserted characters.
578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000579 ptr = get_inserted();
580 if (ptr == NULL)
581 new_insert_skip = 0;
582 else
583 {
584 new_insert_skip = (int)STRLEN(ptr);
585 vim_free(ptr);
586 }
587
588 old_indent = 0;
589
590 /*
591 * Main loop in Insert mode: repeat until Insert mode is left.
592 */
593 for (;;)
594 {
595#ifdef FEAT_RIGHTLEFT
596 if (!revins_legal)
597 revins_scol = -1; /* reset on illegal motions */
598 else
599 revins_legal = 0;
600#endif
601 if (arrow_used) /* don't repeat insert when arrow key used */
602 count = 0;
603
604 if (stop_insert_mode)
605 {
606 /* ":stopinsert" used or 'insertmode' reset */
607 count = 0;
608 goto doESCkey;
609 }
610
611 /* set curwin->w_curswant for next K_DOWN or K_UP */
612 if (!arrow_used)
613 curwin->w_set_curswant = TRUE;
614
615 /* If there is no typeahead may check for timestamps (e.g., for when a
616 * menu invoked a shell command). */
617 if (stuff_empty())
618 {
619 did_check_timestamps = FALSE;
620 if (need_check_timestamps)
621 check_timestamps(FALSE);
622 }
623
624 /*
625 * When emsg() was called msg_scroll will have been set.
626 */
627 msg_scroll = FALSE;
628
629#ifdef FEAT_GUI
630 /* When 'mousefocus' is set a mouse movement may have taken us to
631 * another window. "need_mouse_correct" may then be set because of an
632 * autocommand. */
633 if (need_mouse_correct)
634 gui_mouse_correct();
635#endif
636
637#ifdef FEAT_FOLDING
638 /* Open fold at the cursor line, according to 'foldopen'. */
639 if (fdo_flags & FDO_INSERT)
640 foldOpenCursor();
641 /* Close folds where the cursor isn't, according to 'foldclose' */
642 if (!char_avail())
643 foldCheckClose();
644#endif
645
646 /*
647 * If we inserted a character at the last position of the last line in
648 * the window, scroll the window one line up. This avoids an extra
649 * redraw.
650 * This is detected when the cursor column is smaller after inserting
651 * something.
652 * Don't do this when the topline changed already, it has
653 * already been adjusted (by insertchar() calling open_line())).
654 */
655 if (curbuf->b_mod_set
656 && curwin->w_p_wrap
657 && !did_backspace
658 && curwin->w_topline == old_topline
659#ifdef FEAT_DIFF
660 && curwin->w_topfill == old_topfill
661#endif
662 )
663 {
664 mincol = curwin->w_wcol;
665 validate_cursor_col();
666
Bram Moolenaar0ab2a882009-05-13 10:51:08 +0000667 if ((int)curwin->w_wcol < mincol - curbuf->b_p_ts
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 && curwin->w_wrow == W_WINROW(curwin)
669 + curwin->w_height - 1 - p_so
670 && (curwin->w_cursor.lnum != curwin->w_topline
671#ifdef FEAT_DIFF
672 || curwin->w_topfill > 0
673#endif
674 ))
675 {
676#ifdef FEAT_DIFF
677 if (curwin->w_topfill > 0)
678 --curwin->w_topfill;
679 else
680#endif
681#ifdef FEAT_FOLDING
682 if (hasFolding(curwin->w_topline, NULL, &old_topline))
683 set_topline(curwin, old_topline + 1);
684 else
685#endif
686 set_topline(curwin, curwin->w_topline + 1);
687 }
688 }
689
690 /* May need to adjust w_topline to show the cursor. */
691 update_topline();
692
693 did_backspace = FALSE;
694
695 validate_cursor(); /* may set must_redraw */
696
697 /*
698 * Redraw the display when no characters are waiting.
699 * Also shows mode, ruler and positions cursor.
700 */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000701 ins_redraw(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702
703#ifdef FEAT_SCROLLBIND
704 if (curwin->w_p_scb)
705 do_check_scrollbind(TRUE);
706#endif
707
Bram Moolenaar860cae12010-06-05 23:22:07 +0200708#ifdef FEAT_CURSORBIND
709 if (curwin->w_p_crb)
710 do_check_cursorbind();
711#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000712 update_curswant();
713 old_topline = curwin->w_topline;
714#ifdef FEAT_DIFF
715 old_topfill = curwin->w_topfill;
716#endif
717
718#ifdef USE_ON_FLY_SCROLL
719 dont_scroll = FALSE; /* allow scrolling here */
720#endif
721
722 /*
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000723 * Get a character for Insert mode. Ignore K_IGNORE.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 */
725 lastc = c; /* remember previous char for CTRL-D */
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000726 do
727 {
728 c = safe_vgetc();
729 } while (c == K_IGNORE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730
Bram Moolenaard29a9ee2006-09-14 09:07:34 +0000731#ifdef FEAT_AUTOCMD
732 /* Don't want K_CURSORHOLD for the second key, e.g., after CTRL-V. */
733 did_cursorhold = TRUE;
734#endif
735
Bram Moolenaar071d4272004-06-13 20:20:40 +0000736#ifdef FEAT_RIGHTLEFT
737 if (p_hkmap && KeyTyped)
738 c = hkmap(c); /* Hebrew mode mapping */
739#endif
740#ifdef FEAT_FKMAP
741 if (p_fkmap && KeyTyped)
742 c = fkmap(c); /* Farsi mode mapping */
743#endif
744
745#ifdef FEAT_INS_EXPAND
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000746 /*
747 * Special handling of keys while the popup menu is visible or wanted
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000748 * and the cursor is still in the completed word. Only when there is
749 * a match, skip this when no matches were found.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000750 */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +0000751 if (compl_started
752 && pum_wanted()
753 && curwin->w_cursor.col >= compl_col
754 && (compl_shown_match == NULL
755 || compl_shown_match != compl_shown_match->cp_next))
Bram Moolenaara6557602006-02-04 22:43:20 +0000756 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000757 /* BS: Delete one character from "compl_leader". */
758 if ((c == K_BS || c == Ctrl_H)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000759 && curwin->w_cursor.col > compl_col
760 && (c = ins_compl_bs()) == NUL)
Bram Moolenaara6557602006-02-04 22:43:20 +0000761 continue;
762
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000763 /* When no match was selected or it was edited. */
764 if (!compl_used_match)
Bram Moolenaara6557602006-02-04 22:43:20 +0000765 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000766 /* CTRL-L: Add one character from the current match to
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000767 * "compl_leader". Except when at the original match and
768 * there is nothing to add, CTRL-L works like CTRL-P then. */
769 if (c == Ctrl_L
770 && (ctrl_x_mode != CTRL_X_WHOLE_LINE
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +0000771 || (int)STRLEN(compl_shown_match->cp_str)
Bram Moolenaarc1e37902006-04-18 21:55:01 +0000772 > curwin->w_cursor.col - compl_col))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000773 {
774 ins_compl_addfrommatch();
775 continue;
776 }
777
Bram Moolenaar711d5b52007-10-19 18:40:51 +0000778 /* A non-white character that fits in with the current
779 * completion: Add to "compl_leader". */
780 if (ins_compl_accept_char(c))
Bram Moolenaar8b6144b2006-02-08 09:20:24 +0000781 {
782 ins_compl_addleader(c);
783 continue;
784 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000785
Bram Moolenaar0440ca32006-05-13 13:24:33 +0000786 /* Pressing CTRL-Y selects the current match. When
Bram Moolenaar779b74b2006-04-10 14:55:34 +0000787 * compl_enter_selects is set the Enter key does the same. */
788 if (c == Ctrl_Y || (compl_enter_selects
789 && (c == CAR || c == K_KENTER || c == NL)))
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000790 {
791 ins_compl_delete();
792 ins_compl_insert();
793 }
Bram Moolenaara6557602006-02-04 22:43:20 +0000794 }
795 }
796
Bram Moolenaar071d4272004-06-13 20:20:40 +0000797 /* Prepare for or stop CTRL-X mode. This doesn't do completion, but
798 * it does fix up the text when finishing completion. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +0000799 compl_get_longest = FALSE;
Bram Moolenaard4e20a72008-01-22 16:50:03 +0000800 if (ins_compl_prep(c))
Bram Moolenaara6557602006-02-04 22:43:20 +0000801 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000802#endif
803
Bram Moolenaar488c6512005-08-11 20:09:58 +0000804 /* CTRL-\ CTRL-N goes to Normal mode,
805 * CTRL-\ CTRL-G goes to mode selected with 'insertmode',
806 * CTRL-\ CTRL-O is like CTRL-O but without moving the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000807 if (c == Ctrl_BSL)
808 {
809 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +0000810 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 ++no_mapping;
812 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +0000813 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +0000814 --no_mapping;
815 --allow_keys;
Bram Moolenaar488c6512005-08-11 20:09:58 +0000816 if (c != Ctrl_N && c != Ctrl_G && c != Ctrl_O)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000817 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000818 /* it's something else */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 vungetc(c);
820 c = Ctrl_BSL;
821 }
822 else if (c == Ctrl_G && p_im)
823 continue;
824 else
825 {
Bram Moolenaar488c6512005-08-11 20:09:58 +0000826 if (c == Ctrl_O)
827 {
828 ins_ctrl_o();
829 ins_at_eol = FALSE; /* cursor keeps its column */
830 nomove = TRUE;
831 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000832 count = 0;
833 goto doESCkey;
834 }
835 }
836
837#ifdef FEAT_DIGRAPHS
838 c = do_digraph(c);
839#endif
840
841#ifdef FEAT_INS_EXPAND
842 if ((c == Ctrl_V || c == Ctrl_Q) && ctrl_x_mode == CTRL_X_CMDLINE)
843 goto docomplete;
844#endif
845 if (c == Ctrl_V || c == Ctrl_Q)
846 {
847 ins_ctrl_v();
848 c = Ctrl_V; /* pretend CTRL-V is last typed character */
849 continue;
850 }
851
852#ifdef FEAT_CINDENT
853 if (cindent_on()
854# ifdef FEAT_INS_EXPAND
855 && ctrl_x_mode == 0
856# endif
857 )
858 {
859 /* A key name preceded by a bang means this key is not to be
860 * inserted. Skip ahead to the re-indenting below.
861 * A key name preceded by a star means that indenting has to be
862 * done before inserting the key. */
863 line_is_white = inindent(0);
864 if (in_cinkeys(c, '!', line_is_white))
865 goto force_cindent;
866 if (can_cindent && in_cinkeys(c, '*', line_is_white)
867 && stop_arrow() == OK)
868 do_c_expr_indent();
869 }
870#endif
871
872#ifdef FEAT_RIGHTLEFT
873 if (curwin->w_p_rl)
874 switch (c)
875 {
876 case K_LEFT: c = K_RIGHT; break;
877 case K_S_LEFT: c = K_S_RIGHT; break;
878 case K_C_LEFT: c = K_C_RIGHT; break;
879 case K_RIGHT: c = K_LEFT; break;
880 case K_S_RIGHT: c = K_S_LEFT; break;
881 case K_C_RIGHT: c = K_C_LEFT; break;
882 }
883#endif
884
885#ifdef FEAT_VISUAL
886 /*
887 * If 'keymodel' contains "startsel", may start selection. If it
888 * does, a CTRL-O and c will be stuffed, we need to get these
889 * characters.
890 */
891 if (ins_start_select(c))
892 continue;
893#endif
894
895 /*
896 * The big switch to handle a character in insert mode.
897 */
898 switch (c)
899 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000900 case ESC: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000901 if (echeck_abbr(ESC + ABBR_OFF))
902 break;
903 /*FALLTHROUGH*/
904
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000905 case Ctrl_C: /* End input mode */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906#ifdef FEAT_CMDWIN
907 if (c == Ctrl_C && cmdwin_type != 0)
908 {
909 /* Close the cmdline window. */
910 cmdwin_result = K_IGNORE;
911 got_int = FALSE; /* don't stop executing autocommands et al. */
Bram Moolenaar5495cc92006-08-16 14:23:04 +0000912 nomove = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000913 goto doESCkey;
914 }
915#endif
916
917#ifdef UNIX
918do_intr:
919#endif
920 /* when 'insertmode' set, and not halfway a mapping, don't leave
921 * Insert mode */
922 if (goto_im())
923 {
924 if (got_int)
925 {
926 (void)vgetc(); /* flush all buffers */
927 got_int = FALSE;
928 }
929 else
930 vim_beep();
931 break;
932 }
933doESCkey:
934 /*
935 * This is the ONLY return from edit()!
936 */
937 /* Always update o_lnum, so that a "CTRL-O ." that adds a line
938 * still puts the cursor back after the inserted text. */
Bram Moolenaar68b76a62005-03-25 21:53:48 +0000939 if (ins_at_eol && gchar_cursor() == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000940 o_lnum = curwin->w_cursor.lnum;
941
Bram Moolenaar488c6512005-08-11 20:09:58 +0000942 if (ins_esc(&count, cmdchar, nomove))
Bram Moolenaar843ee412004-06-30 16:16:41 +0000943 {
944#ifdef FEAT_AUTOCMD
945 if (cmdchar != 'r' && cmdchar != 'v')
946 apply_autocmds(EVENT_INSERTLEAVE, NULL, NULL,
947 FALSE, curbuf);
Bram Moolenaarc0a0ab52006-10-06 18:39:58 +0000948 did_cursorhold = FALSE;
Bram Moolenaar843ee412004-06-30 16:16:41 +0000949#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000950 return (c == Ctrl_O);
Bram Moolenaar843ee412004-06-30 16:16:41 +0000951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000952 continue;
953
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000954 case Ctrl_Z: /* suspend when 'insertmode' set */
955 if (!p_im)
956 goto normalchar; /* insert CTRL-Z as normal char */
957 stuffReadbuff((char_u *)":st\r");
958 c = Ctrl_O;
959 /*FALLTHROUGH*/
960
961 case Ctrl_O: /* execute one command */
Bram Moolenaare344bea2005-09-01 20:46:49 +0000962#ifdef FEAT_COMPL_FUNC
Bram Moolenaarf75a9632005-09-13 21:20:47 +0000963 if (ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000964 goto docomplete;
965#endif
966 if (echeck_abbr(Ctrl_O + ABBR_OFF))
967 break;
968 ins_ctrl_o();
Bram Moolenaar06a89a52006-04-29 22:01:03 +0000969
970#ifdef FEAT_VIRTUALEDIT
971 /* don't move the cursor left when 'virtualedit' has "onemore". */
972 if (ve_flags & VE_ONEMORE)
973 {
974 ins_at_eol = FALSE;
975 nomove = TRUE;
976 }
977#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000978 count = 0;
979 goto doESCkey;
980
Bram Moolenaar572cb562005-08-05 21:35:02 +0000981 case K_INS: /* toggle insert/replace mode */
982 case K_KINS:
983 ins_insert(replaceState);
984 break;
985
986 case K_SELECT: /* end of Select mode mapping - ignore */
987 break;
988
Bram Moolenaar4be06f92005-07-29 22:36:03 +0000989#ifdef FEAT_SNIFF
990 case K_SNIFF: /* Sniff command received */
991 stuffcharReadbuff(K_SNIFF);
992 goto doESCkey;
993#endif
994
995 case K_HELP: /* Help key works like <ESC> <Help> */
996 case K_F1:
997 case K_XF1:
998 stuffcharReadbuff(K_HELP);
999 if (p_im)
1000 need_start_insertmode = TRUE;
1001 goto doESCkey;
1002
1003#ifdef FEAT_NETBEANS_INTG
1004 case K_F21: /* NetBeans command */
1005 ++no_mapping; /* don't map the next key hits */
Bram Moolenaar61abfd12007-09-13 16:26:47 +00001006 i = plain_vgetc();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001007 --no_mapping;
1008 netbeans_keycommand(i);
1009 break;
1010#endif
1011
1012 case K_ZERO: /* Insert the previously inserted text. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001013 case NUL:
1014 case Ctrl_A:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001015 /* For ^@ the trailing ESC will end the insert, unless there is an
1016 * error. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001017 if (stuff_inserted(NUL, 1L, (c == Ctrl_A)) == FAIL
1018 && c != Ctrl_A && !p_im)
1019 goto doESCkey; /* quit insert mode */
1020 inserted_space = FALSE;
1021 break;
1022
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001023 case Ctrl_R: /* insert the contents of a register */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 ins_reg();
1025 auto_format(FALSE, TRUE);
1026 inserted_space = FALSE;
1027 break;
1028
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001029 case Ctrl_G: /* commands starting with CTRL-G */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030 ins_ctrl_g();
1031 break;
1032
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001033 case Ctrl_HAT: /* switch input mode and/or langmap */
1034 ins_ctrl_hat();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035 break;
1036
1037#ifdef FEAT_RIGHTLEFT
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001038 case Ctrl__: /* switch between languages */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039 if (!p_ari)
1040 goto normalchar;
1041 ins_ctrl_();
1042 break;
1043#endif
1044
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001045 case Ctrl_D: /* Make indent one shiftwidth smaller. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1047 if (ctrl_x_mode == CTRL_X_PATH_DEFINES)
1048 goto docomplete;
1049#endif
1050 /* FALLTHROUGH */
1051
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001052 case Ctrl_T: /* Make indent one shiftwidth greater. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053# ifdef FEAT_INS_EXPAND
1054 if (c == Ctrl_T && ctrl_x_mode == CTRL_X_THESAURUS)
1055 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001056 if (has_compl_option(FALSE))
1057 goto docomplete;
1058 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059 }
1060# endif
1061 ins_shift(c, lastc);
1062 auto_format(FALSE, TRUE);
1063 inserted_space = FALSE;
1064 break;
1065
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001066 case K_DEL: /* delete character under the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067 case K_KDEL:
1068 ins_del();
1069 auto_format(FALSE, TRUE);
1070 break;
1071
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001072 case K_BS: /* delete character before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001073 case Ctrl_H:
1074 did_backspace = ins_bs(c, BACKSPACE_CHAR, &inserted_space);
1075 auto_format(FALSE, TRUE);
1076 break;
1077
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001078 case Ctrl_W: /* delete word before the cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079 did_backspace = ins_bs(c, BACKSPACE_WORD, &inserted_space);
1080 auto_format(FALSE, TRUE);
1081 break;
1082
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001083 case Ctrl_U: /* delete all inserted text in current line */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001084# ifdef FEAT_COMPL_FUNC
1085 /* CTRL-X CTRL-U completes with 'completefunc'. */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001086 if (ctrl_x_mode == CTRL_X_FUNCTION)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001087 goto docomplete;
1088# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089 did_backspace = ins_bs(c, BACKSPACE_LINE, &inserted_space);
1090 auto_format(FALSE, TRUE);
1091 inserted_space = FALSE;
1092 break;
1093
1094#ifdef FEAT_MOUSE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001095 case K_LEFTMOUSE: /* mouse keys */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 case K_LEFTMOUSE_NM:
1097 case K_LEFTDRAG:
1098 case K_LEFTRELEASE:
1099 case K_LEFTRELEASE_NM:
1100 case K_MIDDLEMOUSE:
1101 case K_MIDDLEDRAG:
1102 case K_MIDDLERELEASE:
1103 case K_RIGHTMOUSE:
1104 case K_RIGHTDRAG:
1105 case K_RIGHTRELEASE:
1106 case K_X1MOUSE:
1107 case K_X1DRAG:
1108 case K_X1RELEASE:
1109 case K_X2MOUSE:
1110 case K_X2DRAG:
1111 case K_X2RELEASE:
1112 ins_mouse(c);
1113 break;
1114
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001115 case K_MOUSEDOWN: /* Default action for scroll wheel up: scroll up */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001116 ins_mousescroll(MSCR_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001117 break;
1118
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001119 case K_MOUSEUP: /* Default action for scroll wheel down: scroll down */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02001120 ins_mousescroll(MSCR_UP);
1121 break;
1122
1123 case K_MOUSELEFT: /* Scroll wheel left */
1124 ins_mousescroll(MSCR_LEFT);
1125 break;
1126
1127 case K_MOUSERIGHT: /* Scroll wheel right */
1128 ins_mousescroll(MSCR_RIGHT);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001129 break;
1130#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +00001131#ifdef FEAT_GUI_TABLINE
1132 case K_TABLINE:
1133 case K_TABMENU:
1134 ins_tabline(c);
1135 break;
1136#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001137
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001138 case K_IGNORE: /* Something mapped to nothing */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001139 break;
1140
Bram Moolenaar754b5602006-02-09 23:53:20 +00001141#ifdef FEAT_AUTOCMD
1142 case K_CURSORHOLD: /* Didn't type something for a while. */
1143 apply_autocmds(EVENT_CURSORHOLDI, NULL, NULL, FALSE, curbuf);
1144 did_cursorhold = TRUE;
1145 break;
1146#endif
1147
Bram Moolenaar4770d092006-01-12 23:22:24 +00001148#ifdef FEAT_GUI_W32
1149 /* On Win32 ignore <M-F4>, we get it when closing the window was
1150 * cancelled. */
1151 case K_F4:
1152 if (mod_mask != MOD_MASK_ALT)
1153 goto normalchar;
1154 break;
1155#endif
1156
Bram Moolenaar071d4272004-06-13 20:20:40 +00001157#ifdef FEAT_GUI
1158 case K_VER_SCROLLBAR:
1159 ins_scroll();
1160 break;
1161
1162 case K_HOR_SCROLLBAR:
1163 ins_horscroll();
1164 break;
1165#endif
1166
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001167 case K_HOME: /* <Home> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001168 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 case K_S_HOME:
1170 case K_C_HOME:
1171 ins_home(c);
1172 break;
1173
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001174 case K_END: /* <End> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001175 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00001176 case K_S_END:
1177 case K_C_END:
1178 ins_end(c);
1179 break;
1180
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001181 case K_LEFT: /* <Left> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001182 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1183 ins_s_left();
1184 else
1185 ins_left();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001186 break;
1187
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001188 case K_S_LEFT: /* <S-Left> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001189 case K_C_LEFT:
1190 ins_s_left();
1191 break;
1192
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001193 case K_RIGHT: /* <Right> */
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001194 if (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))
1195 ins_s_right();
1196 else
1197 ins_right();
Bram Moolenaar071d4272004-06-13 20:20:40 +00001198 break;
1199
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001200 case K_S_RIGHT: /* <S-Right> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001201 case K_C_RIGHT:
1202 ins_s_right();
1203 break;
1204
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001205 case K_UP: /* <Up> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001206#ifdef FEAT_INS_EXPAND
1207 if (pum_visible())
1208 goto docomplete;
1209#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001210 if (mod_mask & MOD_MASK_SHIFT)
1211 ins_pageup();
1212 else
1213 ins_up(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001214 break;
1215
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001216 case K_S_UP: /* <S-Up> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001217 case K_PAGEUP:
1218 case K_KPAGEUP:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001219#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001220 if (pum_visible())
1221 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001223 ins_pageup();
1224 break;
1225
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001226 case K_DOWN: /* <Down> */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00001227#ifdef FEAT_INS_EXPAND
1228 if (pum_visible())
1229 goto docomplete;
1230#endif
Bram Moolenaarbc7aa852005-03-06 23:38:09 +00001231 if (mod_mask & MOD_MASK_SHIFT)
1232 ins_pagedown();
1233 else
1234 ins_down(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001235 break;
1236
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001237 case K_S_DOWN: /* <S-Down> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001238 case K_PAGEDOWN:
1239 case K_KPAGEDOWN:
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001240#ifdef FEAT_INS_EXPAND
Bram Moolenaare3226be2005-12-18 22:10:00 +00001241 if (pum_visible())
1242 goto docomplete;
Bram Moolenaara9b1e742005-12-19 22:14:58 +00001243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244 ins_pagedown();
1245 break;
1246
1247#ifdef FEAT_DND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001248 case K_DROP: /* drag-n-drop event */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249 ins_drop();
1250 break;
1251#endif
1252
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001253 case K_S_TAB: /* When not mapped, use like a normal TAB */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254 c = TAB;
1255 /* FALLTHROUGH */
1256
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001257 case TAB: /* TAB or Complete patterns along path */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001258#if defined(FEAT_INS_EXPAND) && defined(FEAT_FIND_ID)
1259 if (ctrl_x_mode == CTRL_X_PATH_PATTERNS)
1260 goto docomplete;
1261#endif
1262 inserted_space = FALSE;
1263 if (ins_tab())
1264 goto normalchar; /* insert TAB as a normal char */
1265 auto_format(FALSE, TRUE);
1266 break;
1267
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001268 case K_KENTER: /* <Enter> */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001269 c = CAR;
1270 /* FALLTHROUGH */
1271 case CAR:
1272 case NL:
1273#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
1274 /* In a quickfix window a <CR> jumps to the error under the
1275 * cursor. */
1276 if (bt_quickfix(curbuf) && c == CAR)
1277 {
Bram Moolenaard12f5c12006-01-25 22:10:52 +00001278 if (curwin->w_llist_ref == NULL) /* quickfix window */
1279 do_cmdline_cmd((char_u *)".cc");
1280 else /* location list window */
1281 do_cmdline_cmd((char_u *)".ll");
Bram Moolenaar071d4272004-06-13 20:20:40 +00001282 break;
1283 }
1284#endif
1285#ifdef FEAT_CMDWIN
1286 if (cmdwin_type != 0)
1287 {
1288 /* Execute the command in the cmdline window. */
1289 cmdwin_result = CAR;
1290 goto doESCkey;
1291 }
1292#endif
1293 if (ins_eol(c) && !p_im)
1294 goto doESCkey; /* out of memory */
1295 auto_format(FALSE, FALSE);
1296 inserted_space = FALSE;
1297 break;
1298
Bram Moolenaar860cae12010-06-05 23:22:07 +02001299#if defined(FEAT_DIGRAPHS) || defined(FEAT_INS_EXPAND)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001300 case Ctrl_K: /* digraph or keyword completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001301# ifdef FEAT_INS_EXPAND
1302 if (ctrl_x_mode == CTRL_X_DICTIONARY)
1303 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001304 if (has_compl_option(TRUE))
1305 goto docomplete;
1306 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001307 }
1308# endif
1309# ifdef FEAT_DIGRAPHS
1310 c = ins_digraph();
1311 if (c == NUL)
1312 break;
1313# endif
1314 goto normalchar;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001315#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316
1317#ifdef FEAT_INS_EXPAND
Bram Moolenaar572cb562005-08-05 21:35:02 +00001318 case Ctrl_X: /* Enter CTRL-X mode */
1319 ins_ctrl_x();
1320 break;
1321
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001322 case Ctrl_RSB: /* Tag name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001323 if (ctrl_x_mode != CTRL_X_TAGS)
1324 goto normalchar;
1325 goto docomplete;
1326
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001327 case Ctrl_F: /* File name completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001328 if (ctrl_x_mode != CTRL_X_FILES)
1329 goto normalchar;
1330 goto docomplete;
Bram Moolenaar488c6512005-08-11 20:09:58 +00001331
1332 case 's': /* Spelling completion after ^X */
1333 case Ctrl_S:
1334 if (ctrl_x_mode != CTRL_X_SPELL)
1335 goto normalchar;
1336 goto docomplete;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337#endif
1338
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001339 case Ctrl_L: /* Whole line completion after ^X */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340#ifdef FEAT_INS_EXPAND
1341 if (ctrl_x_mode != CTRL_X_WHOLE_LINE)
1342#endif
1343 {
1344 /* CTRL-L with 'insertmode' set: Leave Insert mode */
1345 if (p_im)
1346 {
1347 if (echeck_abbr(Ctrl_L + ABBR_OFF))
1348 break;
1349 goto doESCkey;
1350 }
1351 goto normalchar;
1352 }
1353#ifdef FEAT_INS_EXPAND
1354 /* FALLTHROUGH */
1355
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001356 case Ctrl_P: /* Do previous/next pattern completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 case Ctrl_N:
1358 /* if 'complete' is empty then plain ^P is no longer special,
1359 * but it is under other ^X modes */
1360 if (*curbuf->b_p_cpt == NUL
1361 && ctrl_x_mode != 0
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001362 && !(compl_cont_status & CONT_LOCAL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001363 goto normalchar;
1364
1365docomplete:
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001366 compl_busy = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001367 if (ins_complete(c) == FAIL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001368 compl_cont_status = 0;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00001369 compl_busy = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370 break;
1371#endif /* FEAT_INS_EXPAND */
1372
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001373 case Ctrl_Y: /* copy from previous line or scroll down */
1374 case Ctrl_E: /* copy from next line or scroll up */
1375 c = ins_ctrl_ey(c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376 break;
1377
1378 default:
1379#ifdef UNIX
1380 if (c == intr_char) /* special interrupt char */
1381 goto do_intr;
1382#endif
1383
1384 /*
1385 * Insert a nomal character.
1386 */
1387normalchar:
1388#ifdef FEAT_SMARTINDENT
1389 /* Try to perform smart-indenting. */
1390 ins_try_si(c);
1391#endif
1392
1393 if (c == ' ')
1394 {
1395 inserted_space = TRUE;
1396#ifdef FEAT_CINDENT
1397 if (inindent(0))
1398 can_cindent = FALSE;
1399#endif
1400 if (Insstart_blank_vcol == MAXCOL
1401 && curwin->w_cursor.lnum == Insstart.lnum)
1402 Insstart_blank_vcol = get_nolist_virtcol();
1403 }
1404
1405 if (vim_iswordc(c) || !echeck_abbr(
1406#ifdef FEAT_MBYTE
1407 /* Add ABBR_OFF for characters above 0x100, this is
1408 * what check_abbr() expects. */
1409 (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) :
1410#endif
1411 c))
1412 {
1413 insert_special(c, FALSE, FALSE);
1414#ifdef FEAT_RIGHTLEFT
1415 revins_legal++;
1416 revins_chars++;
1417#endif
1418 }
1419
1420 auto_format(FALSE, TRUE);
1421
1422#ifdef FEAT_FOLDING
1423 /* When inserting a character the cursor line must never be in a
1424 * closed fold. */
1425 foldOpenCursor();
1426#endif
1427 break;
1428 } /* end of switch (c) */
1429
Bram Moolenaard29a9ee2006-09-14 09:07:34 +00001430#ifdef FEAT_AUTOCMD
1431 /* If typed something may trigger CursorHoldI again. */
1432 if (c != K_CURSORHOLD)
1433 did_cursorhold = FALSE;
1434#endif
1435
Bram Moolenaar071d4272004-06-13 20:20:40 +00001436 /* If the cursor was moved we didn't just insert a space */
1437 if (arrow_used)
1438 inserted_space = FALSE;
1439
1440#ifdef FEAT_CINDENT
1441 if (can_cindent && cindent_on()
1442# ifdef FEAT_INS_EXPAND
1443 && ctrl_x_mode == 0
1444# endif
1445 )
1446 {
1447force_cindent:
1448 /*
1449 * Indent now if a key was typed that is in 'cinkeys'.
1450 */
1451 if (in_cinkeys(c, ' ', line_is_white))
1452 {
1453 if (stop_arrow() == OK)
1454 /* re-indent the current line */
1455 do_c_expr_indent();
1456 }
1457 }
1458#endif /* FEAT_CINDENT */
1459
1460 } /* for (;;) */
1461 /* NOTREACHED */
1462}
1463
1464/*
1465 * Redraw for Insert mode.
1466 * This is postponed until getting the next character to make '$' in the 'cpo'
1467 * option work correctly.
1468 * Only redraw when there are no characters available. This speeds up
1469 * inserting sequences of characters (e.g., for CTRL-R).
1470 */
1471 static void
Bram Moolenaar754b5602006-02-09 23:53:20 +00001472ins_redraw(ready)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00001473 int ready UNUSED; /* not busy with something */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001475#ifdef FEAT_CONCEAL
1476 linenr_T conceal_old_cursor_line = 0;
1477 linenr_T conceal_new_cursor_line = 0;
1478 int conceal_update_lines = FALSE;
1479#endif
1480
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 if (!char_avail())
1482 {
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001483#if defined(FEAT_AUTOCMD) || defined(FEAT_CONCEAL)
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001484 /* Trigger CursorMoved if the cursor moved. Not when the popup menu is
1485 * visible, the command might delete it. */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001486 if (ready && (
1487# ifdef FEAT_AUTOCMD
1488 has_cursormovedI()
Bram Moolenaar433f7c82006-03-21 21:29:36 +00001489# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001490# if defined(FEAT_AUTOCMD) && defined(FEAT_CONCEAL)
1491 ||
1492# endif
1493# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001494 curwin->w_p_cole > 0
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001495# endif
1496 )
1497 && !equalpos(last_cursormoved, curwin->w_cursor)
1498# ifdef FEAT_INS_EXPAND
1499 && !pum_visible()
1500# endif
1501 )
Bram Moolenaar754b5602006-02-09 23:53:20 +00001502 {
Bram Moolenaare77c7602008-01-12 17:13:50 +00001503# ifdef FEAT_SYN_HL
1504 /* Need to update the screen first, to make sure syntax
1505 * highlighting is correct after making a change (e.g., inserting
1506 * a "(". The autocommand may also require a redraw, so it's done
1507 * again below, unfortunately. */
Bram Moolenaar860cae12010-06-05 23:22:07 +02001508 if (syntax_present(curwin) && must_redraw)
Bram Moolenaare77c7602008-01-12 17:13:50 +00001509 update_screen(0);
1510# endif
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001511# ifdef FEAT_AUTOCMD
1512 if (has_cursormovedI())
1513 apply_autocmds(EVENT_CURSORMOVEDI, NULL, NULL, FALSE, curbuf);
1514# endif
1515# ifdef FEAT_CONCEAL
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001516 if (curwin->w_p_cole > 0)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001517 {
1518 conceal_old_cursor_line = last_cursormoved.lnum;
1519 conceal_new_cursor_line = curwin->w_cursor.lnum;
1520 conceal_update_lines = TRUE;
1521 }
1522# endif
Bram Moolenaar754b5602006-02-09 23:53:20 +00001523 last_cursormoved = curwin->w_cursor;
1524 }
1525#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (must_redraw)
1527 update_screen(0);
1528 else if (clear_cmdline || redraw_cmdline)
1529 showmode(); /* clear cmdline and show mode */
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001530# if defined(FEAT_CONCEAL)
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001531 if ((conceal_update_lines
1532 && (conceal_old_cursor_line != conceal_new_cursor_line
1533 || conceal_cursor_line(curwin)))
1534 || need_cursor_line_redraw)
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001535 {
Bram Moolenaarf5963f72010-07-23 22:10:27 +02001536 if (conceal_old_cursor_line != conceal_new_cursor_line)
1537 update_single_line(curwin, conceal_old_cursor_line);
1538 update_single_line(curwin, conceal_new_cursor_line == 0
1539 ? curwin->w_cursor.lnum : conceal_new_cursor_line);
Bram Moolenaarb2c03502010-07-02 20:20:09 +02001540 curwin->w_valid &= ~VALID_CROW;
1541 }
1542# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001543 showruler(FALSE);
1544 setcursor();
1545 emsg_on_display = FALSE; /* may remove error message now */
1546 }
1547}
1548
1549/*
1550 * Handle a CTRL-V or CTRL-Q typed in Insert mode.
1551 */
1552 static void
1553ins_ctrl_v()
1554{
1555 int c;
1556
1557 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00001558 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001559
1560 if (redrawing() && !char_avail())
1561 edit_putchar('^', TRUE);
1562 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
1563
1564#ifdef FEAT_CMDL_INFO
1565 add_to_showcmd_c(Ctrl_V);
1566#endif
1567
1568 c = get_literal();
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02001569 edit_unputchar(); /* when line fits in 'columns' the '^' is at the start
1570 of the next line and will not be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001571#ifdef FEAT_CMDL_INFO
1572 clear_showcmd();
1573#endif
1574 insert_special(c, FALSE, TRUE);
1575#ifdef FEAT_RIGHTLEFT
1576 revins_chars++;
1577 revins_legal++;
1578#endif
1579}
1580
1581/*
1582 * Put a character directly onto the screen. It's not stored in a buffer.
1583 * Used while handling CTRL-K, CTRL-V, etc. in Insert mode.
1584 */
1585static int pc_status;
1586#define PC_STATUS_UNSET 0 /* pc_bytes was not set */
1587#define PC_STATUS_RIGHT 1 /* right halve of double-wide char */
1588#define PC_STATUS_LEFT 2 /* left halve of double-wide char */
1589#define PC_STATUS_SET 3 /* pc_bytes was filled */
1590#ifdef FEAT_MBYTE
1591static char_u pc_bytes[MB_MAXBYTES + 1]; /* saved bytes */
1592#else
1593static char_u pc_bytes[2]; /* saved bytes */
1594#endif
1595static int pc_attr;
1596static int pc_row;
1597static int pc_col;
1598
1599 void
1600edit_putchar(c, highlight)
1601 int c;
1602 int highlight;
1603{
1604 int attr;
1605
1606 if (ScreenLines != NULL)
1607 {
1608 update_topline(); /* just in case w_topline isn't valid */
1609 validate_cursor();
1610 if (highlight)
1611 attr = hl_attr(HLF_8);
1612 else
1613 attr = 0;
1614 pc_row = W_WINROW(curwin) + curwin->w_wrow;
1615 pc_col = W_WINCOL(curwin);
1616#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1617 pc_status = PC_STATUS_UNSET;
1618#endif
1619#ifdef FEAT_RIGHTLEFT
1620 if (curwin->w_p_rl)
1621 {
1622 pc_col += W_WIDTH(curwin) - 1 - curwin->w_wcol;
1623# ifdef FEAT_MBYTE
1624 if (has_mbyte)
1625 {
1626 int fix_col = mb_fix_col(pc_col, pc_row);
1627
1628 if (fix_col != pc_col)
1629 {
1630 screen_putchar(' ', pc_row, fix_col, attr);
1631 --curwin->w_wcol;
1632 pc_status = PC_STATUS_RIGHT;
1633 }
1634 }
1635# endif
1636 }
1637 else
1638#endif
1639 {
1640 pc_col += curwin->w_wcol;
1641#ifdef FEAT_MBYTE
1642 if (mb_lefthalve(pc_row, pc_col))
1643 pc_status = PC_STATUS_LEFT;
1644#endif
1645 }
1646
1647 /* save the character to be able to put it back */
1648#if defined(FEAT_RIGHTLEFT) || defined(FEAT_MBYTE)
1649 if (pc_status == PC_STATUS_UNSET)
1650#endif
1651 {
1652 screen_getbytes(pc_row, pc_col, pc_bytes, &pc_attr);
1653 pc_status = PC_STATUS_SET;
1654 }
1655 screen_putchar(c, pc_row, pc_col, attr);
1656 }
1657}
1658
1659/*
1660 * Undo the previous edit_putchar().
1661 */
1662 void
1663edit_unputchar()
1664{
1665 if (pc_status != PC_STATUS_UNSET && pc_row >= msg_scrolled)
1666 {
1667#if defined(FEAT_MBYTE)
1668 if (pc_status == PC_STATUS_RIGHT)
1669 ++curwin->w_wcol;
1670 if (pc_status == PC_STATUS_RIGHT || pc_status == PC_STATUS_LEFT)
1671 redrawWinline(curwin->w_cursor.lnum, FALSE);
1672 else
1673#endif
1674 screen_puts(pc_bytes, pc_row - msg_scrolled, pc_col, pc_attr);
1675 }
1676}
1677
1678/*
1679 * Called when p_dollar is set: display a '$' at the end of the changed text
1680 * Only works when cursor is in the line that changes.
1681 */
1682 void
1683display_dollar(col)
1684 colnr_T col;
1685{
1686 colnr_T save_col;
1687
1688 if (!redrawing())
1689 return;
1690
1691 cursor_off();
1692 save_col = curwin->w_cursor.col;
1693 curwin->w_cursor.col = col;
1694#ifdef FEAT_MBYTE
1695 if (has_mbyte)
1696 {
1697 char_u *p;
1698
1699 /* If on the last byte of a multi-byte move to the first byte. */
1700 p = ml_get_curline();
1701 curwin->w_cursor.col -= (*mb_head_off)(p, p + col);
1702 }
1703#endif
1704 curs_columns(FALSE); /* recompute w_wrow and w_wcol */
1705 if (curwin->w_wcol < W_WIDTH(curwin))
1706 {
1707 edit_putchar('$', FALSE);
1708 dollar_vcol = curwin->w_virtcol;
1709 }
1710 curwin->w_cursor.col = save_col;
1711}
1712
1713/*
1714 * Call this function before moving the cursor from the normal insert position
1715 * in insert mode.
1716 */
1717 static void
1718undisplay_dollar()
1719{
1720 if (dollar_vcol)
1721 {
1722 dollar_vcol = 0;
1723 redrawWinline(curwin->w_cursor.lnum, FALSE);
1724 }
1725}
1726
1727/*
1728 * Insert an indent (for <Tab> or CTRL-T) or delete an indent (for CTRL-D).
1729 * Keep the cursor on the same character.
1730 * type == INDENT_INC increase indent (for CTRL-T or <Tab>)
1731 * type == INDENT_DEC decrease indent (for CTRL-D)
1732 * type == INDENT_SET set indent to "amount"
1733 * if round is TRUE, round the indent to 'shiftwidth' (only with _INC and _Dec).
1734 */
1735 void
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001736change_indent(type, amount, round, replaced, call_changed_bytes)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001737 int type;
1738 int amount;
1739 int round;
1740 int replaced; /* replaced character, put on replace stack */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001741 int call_changed_bytes; /* call changed_bytes() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742{
1743 int vcol;
1744 int last_vcol;
1745 int insstart_less; /* reduction for Insstart.col */
1746 int new_cursor_col;
1747 int i;
1748 char_u *ptr;
1749 int save_p_list;
1750 int start_col;
1751 colnr_T vc;
1752#ifdef FEAT_VREPLACE
1753 colnr_T orig_col = 0; /* init for GCC */
1754 char_u *new_line, *orig_line = NULL; /* init for GCC */
1755
1756 /* VREPLACE mode needs to know what the line was like before changing */
1757 if (State & VREPLACE_FLAG)
1758 {
1759 orig_line = vim_strsave(ml_get_curline()); /* Deal with NULL below */
1760 orig_col = curwin->w_cursor.col;
1761 }
1762#endif
1763
1764 /* for the following tricks we don't want list mode */
1765 save_p_list = curwin->w_p_list;
1766 curwin->w_p_list = FALSE;
1767 vc = getvcol_nolist(&curwin->w_cursor);
1768 vcol = vc;
1769
1770 /*
1771 * For Replace mode we need to fix the replace stack later, which is only
1772 * possible when the cursor is in the indent. Remember the number of
1773 * characters before the cursor if it's possible.
1774 */
1775 start_col = curwin->w_cursor.col;
1776
1777 /* determine offset from first non-blank */
1778 new_cursor_col = curwin->w_cursor.col;
1779 beginline(BL_WHITE);
1780 new_cursor_col -= curwin->w_cursor.col;
1781
1782 insstart_less = curwin->w_cursor.col;
1783
1784 /*
1785 * If the cursor is in the indent, compute how many screen columns the
1786 * cursor is to the left of the first non-blank.
1787 */
1788 if (new_cursor_col < 0)
1789 vcol = get_indent() - vcol;
1790
1791 if (new_cursor_col > 0) /* can't fix replace stack */
1792 start_col = -1;
1793
1794 /*
1795 * Set the new indent. The cursor will be put on the first non-blank.
1796 */
1797 if (type == INDENT_SET)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001798 (void)set_indent(amount, call_changed_bytes ? SIN_CHANGED : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799 else
1800 {
1801#ifdef FEAT_VREPLACE
1802 int save_State = State;
1803
1804 /* Avoid being called recursively. */
1805 if (State & VREPLACE_FLAG)
1806 State = INSERT;
1807#endif
Bram Moolenaar21b17e72008-01-16 19:03:13 +00001808 shift_line(type == INDENT_DEC, round, 1, call_changed_bytes);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001809#ifdef FEAT_VREPLACE
1810 State = save_State;
1811#endif
1812 }
1813 insstart_less -= curwin->w_cursor.col;
1814
1815 /*
1816 * Try to put cursor on same character.
1817 * If the cursor is at or after the first non-blank in the line,
1818 * compute the cursor column relative to the column of the first
1819 * non-blank character.
1820 * If we are not in insert mode, leave the cursor on the first non-blank.
1821 * If the cursor is before the first non-blank, position it relative
1822 * to the first non-blank, counted in screen columns.
1823 */
1824 if (new_cursor_col >= 0)
1825 {
1826 /*
1827 * When changing the indent while the cursor is touching it, reset
1828 * Insstart_col to 0.
1829 */
1830 if (new_cursor_col == 0)
1831 insstart_less = MAXCOL;
1832 new_cursor_col += curwin->w_cursor.col;
1833 }
1834 else if (!(State & INSERT))
1835 new_cursor_col = curwin->w_cursor.col;
1836 else
1837 {
1838 /*
1839 * Compute the screen column where the cursor should be.
1840 */
1841 vcol = get_indent() - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001842 curwin->w_virtcol = (colnr_T)((vcol < 0) ? 0 : vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843
1844 /*
1845 * Advance the cursor until we reach the right screen column.
1846 */
1847 vcol = last_vcol = 0;
1848 new_cursor_col = -1;
1849 ptr = ml_get_curline();
1850 while (vcol <= (int)curwin->w_virtcol)
1851 {
1852 last_vcol = vcol;
1853#ifdef FEAT_MBYTE
1854 if (has_mbyte && new_cursor_col >= 0)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001855 new_cursor_col += (*mb_ptr2len)(ptr + new_cursor_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 else
1857#endif
1858 ++new_cursor_col;
1859 vcol += lbr_chartabsize(ptr + new_cursor_col, (colnr_T)vcol);
1860 }
1861 vcol = last_vcol;
1862
1863 /*
1864 * May need to insert spaces to be able to position the cursor on
1865 * the right screen column.
1866 */
1867 if (vcol != (int)curwin->w_virtcol)
1868 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001869 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 i = (int)curwin->w_virtcol - vcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001871 ptr = alloc((unsigned)(i + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 if (ptr != NULL)
1873 {
1874 new_cursor_col += i;
1875 ptr[i] = NUL;
1876 while (--i >= 0)
1877 ptr[i] = ' ';
1878 ins_str(ptr);
1879 vim_free(ptr);
1880 }
1881 }
1882
1883 /*
1884 * When changing the indent while the cursor is in it, reset
1885 * Insstart_col to 0.
1886 */
1887 insstart_less = MAXCOL;
1888 }
1889
1890 curwin->w_p_list = save_p_list;
1891
1892 if (new_cursor_col <= 0)
1893 curwin->w_cursor.col = 0;
1894 else
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00001895 curwin->w_cursor.col = (colnr_T)new_cursor_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 curwin->w_set_curswant = TRUE;
1897 changed_cline_bef_curs();
1898
1899 /*
1900 * May have to adjust the start of the insert.
1901 */
1902 if (State & INSERT)
1903 {
1904 if (curwin->w_cursor.lnum == Insstart.lnum && Insstart.col != 0)
1905 {
1906 if ((int)Insstart.col <= insstart_less)
1907 Insstart.col = 0;
1908 else
1909 Insstart.col -= insstart_less;
1910 }
1911 if ((int)ai_col <= insstart_less)
1912 ai_col = 0;
1913 else
1914 ai_col -= insstart_less;
1915 }
1916
1917 /*
1918 * For REPLACE mode, may have to fix the replace stack, if it's possible.
1919 * If the number of characters before the cursor decreased, need to pop a
1920 * few characters from the replace stack.
1921 * If the number of characters before the cursor increased, need to push a
1922 * few NULs onto the replace stack.
1923 */
1924 if (REPLACE_NORMAL(State) && start_col >= 0)
1925 {
1926 while (start_col > (int)curwin->w_cursor.col)
1927 {
1928 replace_join(0); /* remove a NUL from the replace stack */
1929 --start_col;
1930 }
1931 while (start_col < (int)curwin->w_cursor.col || replaced)
1932 {
1933 replace_push(NUL);
1934 if (replaced)
1935 {
1936 replace_push(replaced);
1937 replaced = NUL;
1938 }
1939 ++start_col;
1940 }
1941 }
1942
1943#ifdef FEAT_VREPLACE
1944 /*
1945 * For VREPLACE mode, we also have to fix the replace stack. In this case
1946 * it is always possible because we backspace over the whole line and then
1947 * put it back again the way we wanted it.
1948 */
1949 if (State & VREPLACE_FLAG)
1950 {
1951 /* If orig_line didn't allocate, just return. At least we did the job,
1952 * even if you can't backspace. */
1953 if (orig_line == NULL)
1954 return;
1955
1956 /* Save new line */
1957 new_line = vim_strsave(ml_get_curline());
1958 if (new_line == NULL)
1959 return;
1960
1961 /* We only put back the new line up to the cursor */
1962 new_line[curwin->w_cursor.col] = NUL;
1963
1964 /* Put back original line */
1965 ml_replace(curwin->w_cursor.lnum, orig_line, FALSE);
1966 curwin->w_cursor.col = orig_col;
1967
1968 /* Backspace from cursor to start of line */
1969 backspace_until_column(0);
1970
1971 /* Insert new stuff into line again */
1972 ins_bytes(new_line);
1973
1974 vim_free(new_line);
1975 }
1976#endif
1977}
1978
1979/*
1980 * Truncate the space at the end of a line. This is to be used only in an
1981 * insert mode. It handles fixing the replace stack for REPLACE and VREPLACE
1982 * modes.
1983 */
1984 void
1985truncate_spaces(line)
1986 char_u *line;
1987{
1988 int i;
1989
1990 /* find start of trailing white space */
1991 for (i = (int)STRLEN(line) - 1; i >= 0 && vim_iswhite(line[i]); i--)
1992 {
1993 if (State & REPLACE_FLAG)
1994 replace_join(0); /* remove a NUL from the replace stack */
1995 }
1996 line[i + 1] = NUL;
1997}
1998
1999#if defined(FEAT_VREPLACE) || defined(FEAT_INS_EXPAND) \
2000 || defined(FEAT_COMMENTS) || defined(PROTO)
2001/*
2002 * Backspace the cursor until the given column. Handles REPLACE and VREPLACE
2003 * modes correctly. May also be used when not in insert mode at all.
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002004 * Will attempt not to go before "col" even when there is a composing
2005 * character.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 */
2007 void
2008backspace_until_column(col)
2009 int col;
2010{
2011 while ((int)curwin->w_cursor.col > col)
2012 {
2013 curwin->w_cursor.col--;
2014 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002015 replace_do_bs(col);
2016 else if (!del_char_after_col(col))
2017 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002018 }
2019}
2020#endif
2021
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002022/*
2023 * Like del_char(), but make sure not to go before column "limit_col".
2024 * Only matters when there are composing characters.
2025 * Return TRUE when something was deleted.
2026 */
2027 static int
2028del_char_after_col(limit_col)
Bram Moolenaar0c094b92009-05-14 20:20:33 +00002029 int limit_col UNUSED;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002030{
2031#ifdef FEAT_MBYTE
2032 if (enc_utf8 && limit_col >= 0)
2033 {
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002034 colnr_T ecol = curwin->w_cursor.col + 1;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002035
2036 /* Make sure the cursor is at the start of a character, but
2037 * skip forward again when going too far back because of a
2038 * composing character. */
2039 mb_adjust_cursor();
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00002040 while (curwin->w_cursor.col < (colnr_T)limit_col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002041 {
2042 int l = utf_ptr2len(ml_get_cursor());
2043
2044 if (l == 0) /* end of line */
2045 break;
2046 curwin->w_cursor.col += l;
2047 }
2048 if (*ml_get_cursor() == NUL || curwin->w_cursor.col == ecol)
2049 return FALSE;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002050 del_bytes((long)((int)ecol - curwin->w_cursor.col), FALSE, TRUE);
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002051 }
2052 else
2053#endif
2054 (void)del_char(FALSE);
2055 return TRUE;
2056}
2057
Bram Moolenaar071d4272004-06-13 20:20:40 +00002058#if defined(FEAT_INS_EXPAND) || defined(PROTO)
2059/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002060 * CTRL-X pressed in Insert mode.
2061 */
2062 static void
2063ins_ctrl_x()
2064{
2065 /* CTRL-X after CTRL-X CTRL-V doesn't do anything, so that CTRL-X
2066 * CTRL-V works like CTRL-N */
2067 if (ctrl_x_mode != CTRL_X_CMDLINE)
2068 {
2069 /* if the next ^X<> won't ADD nothing, then reset
2070 * compl_cont_status */
2071 if (compl_cont_status & CONT_N_ADDS)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002072 compl_cont_status |= CONT_INTRPT;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002073 else
2074 compl_cont_status = 0;
2075 /* We're not sure which CTRL-X mode it will be yet */
2076 ctrl_x_mode = CTRL_X_NOT_DEFINED_YET;
2077 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
2078 edit_submode_pre = NULL;
2079 showmode();
2080 }
2081}
2082
2083/*
2084 * Return TRUE if the 'dict' or 'tsr' option can be used.
2085 */
2086 static int
2087has_compl_option(dict_opt)
2088 int dict_opt;
2089{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002090 if (dict_opt ? (*curbuf->b_p_dict == NUL && *p_dict == NUL
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002091# ifdef FEAT_SPELL
2092 && !curwin->w_p_spell
2093# endif
2094 )
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002095 : (*curbuf->b_p_tsr == NUL && *p_tsr == NUL))
2096 {
2097 ctrl_x_mode = 0;
2098 edit_submode = NULL;
2099 msg_attr(dict_opt ? (char_u *)_("'dictionary' option is empty")
2100 : (char_u *)_("'thesaurus' option is empty"),
2101 hl_attr(HLF_E));
2102 if (emsg_silent == 0)
2103 {
2104 vim_beep();
2105 setcursor();
2106 out_flush();
2107 ui_delay(2000L, FALSE);
2108 }
2109 return FALSE;
2110 }
2111 return TRUE;
2112}
2113
2114/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002115 * Is the character 'c' a valid key to go to or keep us in CTRL-X mode?
2116 * This depends on the current mode.
2117 */
2118 int
2119vim_is_ctrl_x_key(c)
2120 int c;
2121{
2122 /* Always allow ^R - let it's results then be checked */
2123 if (c == Ctrl_R)
2124 return TRUE;
2125
Bram Moolenaare3226be2005-12-18 22:10:00 +00002126 /* Accept <PageUp> and <PageDown> if the popup menu is visible. */
Bram Moolenaard12f5c12006-01-25 22:10:52 +00002127 if (ins_compl_pum_key(c))
Bram Moolenaare3226be2005-12-18 22:10:00 +00002128 return TRUE;
2129
Bram Moolenaar071d4272004-06-13 20:20:40 +00002130 switch (ctrl_x_mode)
2131 {
2132 case 0: /* Not in any CTRL-X mode */
2133 return (c == Ctrl_N || c == Ctrl_P || c == Ctrl_X);
2134 case CTRL_X_NOT_DEFINED_YET:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002135 return ( c == Ctrl_X || c == Ctrl_Y || c == Ctrl_E
Bram Moolenaar071d4272004-06-13 20:20:40 +00002136 || c == Ctrl_L || c == Ctrl_F || c == Ctrl_RSB
2137 || c == Ctrl_I || c == Ctrl_D || c == Ctrl_P
2138 || c == Ctrl_N || c == Ctrl_T || c == Ctrl_V
Bram Moolenaar488c6512005-08-11 20:09:58 +00002139 || c == Ctrl_Q || c == Ctrl_U || c == Ctrl_O
2140 || c == Ctrl_S || c == 's');
Bram Moolenaar071d4272004-06-13 20:20:40 +00002141 case CTRL_X_SCROLL:
2142 return (c == Ctrl_Y || c == Ctrl_E);
2143 case CTRL_X_WHOLE_LINE:
2144 return (c == Ctrl_L || c == Ctrl_P || c == Ctrl_N);
2145 case CTRL_X_FILES:
2146 return (c == Ctrl_F || c == Ctrl_P || c == Ctrl_N);
2147 case CTRL_X_DICTIONARY:
2148 return (c == Ctrl_K || c == Ctrl_P || c == Ctrl_N);
2149 case CTRL_X_THESAURUS:
2150 return (c == Ctrl_T || c == Ctrl_P || c == Ctrl_N);
2151 case CTRL_X_TAGS:
2152 return (c == Ctrl_RSB || c == Ctrl_P || c == Ctrl_N);
2153#ifdef FEAT_FIND_ID
2154 case CTRL_X_PATH_PATTERNS:
2155 return (c == Ctrl_P || c == Ctrl_N);
2156 case CTRL_X_PATH_DEFINES:
2157 return (c == Ctrl_D || c == Ctrl_P || c == Ctrl_N);
2158#endif
2159 case CTRL_X_CMDLINE:
2160 return (c == Ctrl_V || c == Ctrl_Q || c == Ctrl_P || c == Ctrl_N
2161 || c == Ctrl_X);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00002162#ifdef FEAT_COMPL_FUNC
2163 case CTRL_X_FUNCTION:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002164 return (c == Ctrl_U || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaarf75a9632005-09-13 21:20:47 +00002165 case CTRL_X_OMNI:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002166 return (c == Ctrl_O || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaare344bea2005-09-01 20:46:49 +00002167#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00002168 case CTRL_X_SPELL:
2169 return (c == Ctrl_S || c == Ctrl_P || c == Ctrl_N);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002170 }
2171 EMSG(_(e_internal));
2172 return FALSE;
2173}
2174
2175/*
Bram Moolenaar711d5b52007-10-19 18:40:51 +00002176 * Return TRUE when character "c" is part of the item currently being
2177 * completed. Used to decide whether to abandon complete mode when the menu
2178 * is visible.
2179 */
2180 static int
2181ins_compl_accept_char(c)
2182 int c;
2183{
2184 if (ctrl_x_mode & CTRL_X_WANT_IDENT)
2185 /* When expanding an identifier only accept identifier chars. */
2186 return vim_isIDc(c);
2187
2188 switch (ctrl_x_mode)
2189 {
2190 case CTRL_X_FILES:
2191 /* When expanding file name only accept file name chars. But not
2192 * path separators, so that "proto/<Tab>" expands files in
2193 * "proto", not "proto/" as a whole */
2194 return vim_isfilec(c) && !vim_ispathsep(c);
2195
2196 case CTRL_X_CMDLINE:
2197 case CTRL_X_OMNI:
2198 /* Command line and Omni completion can work with just about any
2199 * printable character, but do stop at white space. */
2200 return vim_isprintc(c) && !vim_iswhite(c);
2201
2202 case CTRL_X_WHOLE_LINE:
2203 /* For while line completion a space can be part of the line. */
2204 return vim_isprintc(c);
2205 }
2206 return vim_iswordc(c);
2207}
2208
2209/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002210 * This is like ins_compl_add(), but if 'ic' and 'inf' are set, then the
Bram Moolenaar071d4272004-06-13 20:20:40 +00002211 * case of the originally typed text is used, and the case of the completed
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00002212 * text is inferred, ie this tries to work out what case you probably wanted
Bram Moolenaar071d4272004-06-13 20:20:40 +00002213 * the rest of the word to be in -- webb
2214 */
2215 int
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002216ins_compl_add_infercase(str, len, icase, fname, dir, flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002217 char_u *str;
2218 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002219 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002220 char_u *fname;
2221 int dir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002222 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002223{
Bram Moolenaara2993e12007-08-12 14:38:46 +00002224 char_u *p;
2225 int i, c;
2226 int actual_len; /* Take multi-byte characters */
2227 int actual_compl_length; /* into account. */
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002228 int min_len;
Bram Moolenaar97b98102009-11-17 16:41:01 +00002229 int *wca; /* Wide character array. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002230 int has_lower = FALSE;
2231 int was_letter = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002232
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002233 if (p_ic && curbuf->b_p_inf && len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002234 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002235 /* Infer case of completed part. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236
Bram Moolenaara2993e12007-08-12 14:38:46 +00002237 /* Find actual length of completion. */
2238#ifdef FEAT_MBYTE
2239 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002240 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002241 p = str;
2242 actual_len = 0;
2243 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002244 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002245 mb_ptr_adv(p);
2246 ++actual_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002247 }
2248 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002249 else
2250#endif
2251 actual_len = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002252
Bram Moolenaara2993e12007-08-12 14:38:46 +00002253 /* Find actual length of original text. */
2254#ifdef FEAT_MBYTE
2255 if (has_mbyte)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002256 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002257 p = compl_orig_text;
2258 actual_compl_length = 0;
2259 while (*p != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 {
Bram Moolenaara2993e12007-08-12 14:38:46 +00002261 mb_ptr_adv(p);
2262 ++actual_compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002263 }
2264 }
Bram Moolenaara2993e12007-08-12 14:38:46 +00002265 else
2266#endif
2267 actual_compl_length = compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002268
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002269 /* "actual_len" may be smaller than "actual_compl_length" when using
2270 * thesaurus, only use the minimum when comparing. */
2271 min_len = actual_len < actual_compl_length
2272 ? actual_len : actual_compl_length;
2273
Bram Moolenaara2993e12007-08-12 14:38:46 +00002274 /* Allocate wide character array for the completion and fill it. */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002275 wca = (int *)alloc((unsigned)(actual_len * sizeof(int)));
Bram Moolenaara2993e12007-08-12 14:38:46 +00002276 if (wca != NULL)
2277 {
2278 p = str;
2279 for (i = 0; i < actual_len; ++i)
2280#ifdef FEAT_MBYTE
2281 if (has_mbyte)
2282 wca[i] = mb_ptr2char_adv(&p);
2283 else
2284#endif
2285 wca[i] = *(p++);
2286
2287 /* Rule 1: Were any chars converted to lower? */
2288 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002289 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002290 {
2291#ifdef FEAT_MBYTE
2292 if (has_mbyte)
2293 c = mb_ptr2char_adv(&p);
2294 else
2295#endif
2296 c = *(p++);
2297 if (MB_ISLOWER(c))
2298 {
2299 has_lower = TRUE;
2300 if (MB_ISUPPER(wca[i]))
2301 {
2302 /* Rule 1 is satisfied. */
2303 for (i = actual_compl_length; i < actual_len; ++i)
2304 wca[i] = MB_TOLOWER(wca[i]);
2305 break;
2306 }
2307 }
2308 }
2309
2310 /*
2311 * Rule 2: No lower case, 2nd consecutive letter converted to
2312 * upper case.
2313 */
2314 if (!has_lower)
2315 {
2316 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002317 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002318 {
2319#ifdef FEAT_MBYTE
2320 if (has_mbyte)
2321 c = mb_ptr2char_adv(&p);
2322 else
2323#endif
2324 c = *(p++);
2325 if (was_letter && MB_ISUPPER(c) && MB_ISLOWER(wca[i]))
2326 {
2327 /* Rule 2 is satisfied. */
2328 for (i = actual_compl_length; i < actual_len; ++i)
2329 wca[i] = MB_TOUPPER(wca[i]);
2330 break;
2331 }
2332 was_letter = MB_ISLOWER(c) || MB_ISUPPER(c);
2333 }
2334 }
2335
2336 /* Copy the original case of the part we typed. */
2337 p = compl_orig_text;
Bram Moolenaar04fa5422010-05-28 21:31:58 +02002338 for (i = 0; i < min_len; ++i)
Bram Moolenaara2993e12007-08-12 14:38:46 +00002339 {
2340#ifdef FEAT_MBYTE
2341 if (has_mbyte)
2342 c = mb_ptr2char_adv(&p);
2343 else
2344#endif
2345 c = *(p++);
2346 if (MB_ISLOWER(c))
2347 wca[i] = MB_TOLOWER(wca[i]);
2348 else if (MB_ISUPPER(c))
2349 wca[i] = MB_TOUPPER(wca[i]);
2350 }
2351
Bram Moolenaare40e57c2007-11-08 12:04:26 +00002352 /*
Bram Moolenaara2993e12007-08-12 14:38:46 +00002353 * Generate encoding specific output from wide character array.
2354 * Multi-byte characters can occupy up to five bytes more than
2355 * ASCII characters, and we also need one byte for NUL, so stay
2356 * six bytes away from the edge of IObuff.
2357 */
2358 p = IObuff;
2359 i = 0;
2360 while (i < actual_len && (p - IObuff + 6) < IOSIZE)
2361#ifdef FEAT_MBYTE
2362 if (has_mbyte)
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00002363 p += (*mb_char2bytes)(wca[i++], p);
Bram Moolenaara2993e12007-08-12 14:38:46 +00002364 else
2365#endif
2366 *(p++) = wca[i++];
2367 *p = NUL;
2368
2369 vim_free(wca);
2370 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002371
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002372 return ins_compl_add(IObuff, len, icase, fname, NULL, dir,
2373 flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002374 }
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002375 return ins_compl_add(str, len, icase, fname, NULL, dir, flags, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002376}
2377
2378/*
2379 * Add a match to the list of matches.
2380 * If the given string is already in the list of completions, then return
Bram Moolenaar572cb562005-08-05 21:35:02 +00002381 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002382 * maybe because alloc() returns NULL, then FAIL is returned.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002383 */
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002384 static int
Bram Moolenaar89d40322006-08-29 15:30:07 +00002385ins_compl_add(str, len, icase, fname, cptext, cdir, flags, adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002386 char_u *str;
2387 int len;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002388 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002389 char_u *fname;
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002390 char_u **cptext; /* extra text for popup menu or NULL */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002391 int cdir;
Bram Moolenaar572cb562005-08-05 21:35:02 +00002392 int flags;
Bram Moolenaar89d40322006-08-29 15:30:07 +00002393 int adup; /* accept duplicate match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002394{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002395 compl_T *match;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002396 int dir = (cdir == 0 ? compl_direction : cdir);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002397
2398 ui_breakcheck();
2399 if (got_int)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002400 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002401 if (len < 0)
2402 len = (int)STRLEN(str);
2403
2404 /*
2405 * If the same match is already present, don't add it.
2406 */
Bram Moolenaar89d40322006-08-29 15:30:07 +00002407 if (compl_first_match != NULL && !adup)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002409 match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002410 do
2411 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002412 if ( !(match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar5948a572006-10-03 13:49:29 +00002413 && STRNCMP(match->cp_str, str, len) == 0
Bram Moolenaar572cb562005-08-05 21:35:02 +00002414 && match->cp_str[len] == NUL)
2415 return NOTDONE;
2416 match = match->cp_next;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002417 } while (match != NULL && match != compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002418 }
2419
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002420 /* Remove any popup menu before changing the list of matches. */
2421 ins_compl_del_pum();
2422
Bram Moolenaar071d4272004-06-13 20:20:40 +00002423 /*
2424 * Allocate a new match structure.
2425 * Copy the values to the new match structure.
2426 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002427 match = (compl_T *)alloc_clear((unsigned)sizeof(compl_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +00002428 if (match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002429 return FAIL;
2430 match->cp_number = -1;
2431 if (flags & ORIGINAL_TEXT)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002432 match->cp_number = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00002433 if ((match->cp_str = vim_strnsave(str, len)) == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002434 {
2435 vim_free(match);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002436 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437 }
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002438 match->cp_icase = icase;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002439
Bram Moolenaar071d4272004-06-13 20:20:40 +00002440 /* match-fname is:
Bram Moolenaar572cb562005-08-05 21:35:02 +00002441 * - compl_curr_match->cp_fname if it is a string equal to fname.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002442 * - a copy of fname, FREE_FNAME is set to free later THE allocated mem.
2443 * - NULL otherwise. --Acevedo */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002444 if (fname != NULL
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002445 && compl_curr_match != NULL
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002446 && compl_curr_match->cp_fname != NULL
2447 && STRCMP(fname, compl_curr_match->cp_fname) == 0)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002448 match->cp_fname = compl_curr_match->cp_fname;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002449 else if (fname != NULL)
2450 {
2451 match->cp_fname = vim_strsave(fname);
Bram Moolenaar572cb562005-08-05 21:35:02 +00002452 flags |= FREE_FNAME;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002453 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002454 else
Bram Moolenaar572cb562005-08-05 21:35:02 +00002455 match->cp_fname = NULL;
2456 match->cp_flags = flags;
Bram Moolenaar39f05632006-03-19 22:15:26 +00002457
2458 if (cptext != NULL)
2459 {
2460 int i;
2461
2462 for (i = 0; i < CPT_COUNT; ++i)
2463 if (cptext[i] != NULL && *cptext[i] != NUL)
2464 match->cp_text[i] = vim_strsave(cptext[i]);
2465 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002466
2467 /*
2468 * Link the new match structure in the list of matches.
2469 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002470 if (compl_first_match == NULL)
Bram Moolenaar572cb562005-08-05 21:35:02 +00002471 match->cp_next = match->cp_prev = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002472 else if (dir == FORWARD)
2473 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002474 match->cp_next = compl_curr_match->cp_next;
2475 match->cp_prev = compl_curr_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002476 }
2477 else /* BACKWARD */
2478 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002479 match->cp_next = compl_curr_match;
2480 match->cp_prev = compl_curr_match->cp_prev;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002481 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002482 if (match->cp_next)
2483 match->cp_next->cp_prev = match;
2484 if (match->cp_prev)
2485 match->cp_prev->cp_next = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002486 else /* if there's nothing before, it is the first match */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002487 compl_first_match = match;
2488 compl_curr_match = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002489
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002490 /*
2491 * Find the longest common string if still doing that.
2492 */
2493 if (compl_get_longest && (flags & ORIGINAL_TEXT) == 0)
2494 ins_compl_longest_match(match);
2495
Bram Moolenaar071d4272004-06-13 20:20:40 +00002496 return OK;
2497}
2498
2499/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002500 * Return TRUE if "str[len]" matches with match->cp_str, considering
2501 * match->cp_icase.
2502 */
2503 static int
2504ins_compl_equal(match, str, len)
2505 compl_T *match;
2506 char_u *str;
2507 int len;
2508{
2509 if (match->cp_icase)
2510 return STRNICMP(match->cp_str, str, (size_t)len) == 0;
2511 return STRNCMP(match->cp_str, str, (size_t)len) == 0;
2512}
2513
2514/*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002515 * Reduce the longest common string for match "match".
2516 */
2517 static void
2518ins_compl_longest_match(match)
2519 compl_T *match;
2520{
2521 char_u *p, *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002522 int c1, c2;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002523 int had_match;
2524
2525 if (compl_leader == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002526 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002527 /* First match, use it as a whole. */
2528 compl_leader = vim_strsave(match->cp_str);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002529 if (compl_leader != NULL)
2530 {
2531 had_match = (curwin->w_cursor.col > compl_col);
2532 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002533 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002534 ins_redraw(FALSE);
2535
2536 /* When the match isn't there (to avoid matching itself) remove it
2537 * again after redrawing. */
2538 if (!had_match)
2539 ins_compl_delete();
2540 compl_used_match = FALSE;
2541 }
2542 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002543 else
2544 {
2545 /* Reduce the text if this match differs from compl_leader. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002546 p = compl_leader;
2547 s = match->cp_str;
2548 while (*p != NUL)
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002549 {
2550#ifdef FEAT_MBYTE
2551 if (has_mbyte)
2552 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002553 c1 = mb_ptr2char(p);
2554 c2 = mb_ptr2char(s);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002555 }
2556 else
2557#endif
2558 {
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002559 c1 = *p;
2560 c2 = *s;
2561 }
2562 if (match->cp_icase ? (MB_TOLOWER(c1) != MB_TOLOWER(c2))
2563 : (c1 != c2))
2564 break;
2565#ifdef FEAT_MBYTE
2566 if (has_mbyte)
2567 {
2568 mb_ptr_adv(p);
2569 mb_ptr_adv(s);
2570 }
2571 else
2572#endif
2573 {
2574 ++p;
2575 ++s;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002576 }
2577 }
2578
2579 if (*p != NUL)
2580 {
2581 /* Leader was shortened, need to change the inserted text. */
2582 *p = NUL;
2583 had_match = (curwin->w_cursor.col > compl_col);
2584 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00002585 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002586 ins_redraw(FALSE);
2587
2588 /* When the match isn't there (to avoid matching itself) remove it
2589 * again after redrawing. */
2590 if (!had_match)
2591 ins_compl_delete();
2592 }
2593
2594 compl_used_match = FALSE;
2595 }
2596}
2597
2598/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00002599 * Add an array of matches to the list of matches.
2600 * Frees matches[].
2601 */
2602 static void
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002603ins_compl_add_matches(num_matches, matches, icase)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002604 int num_matches;
2605 char_u **matches;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002606 int icase;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002607{
2608 int i;
2609 int add_r = OK;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002610 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002611
Bram Moolenaar572cb562005-08-05 21:35:02 +00002612 for (i = 0; i < num_matches && add_r != FAIL; i++)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002613 if ((add_r = ins_compl_add(matches[i], -1, icase,
Bram Moolenaar4a85b412006-04-23 22:40:29 +00002614 NULL, NULL, dir, 0, FALSE)) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 /* if dir was BACKWARD then honor it just once */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002616 dir = FORWARD;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002617 FreeWild(num_matches, matches);
2618}
2619
2620/* Make the completion list cyclic.
2621 * Return the number of matches (excluding the original).
2622 */
2623 static int
2624ins_compl_make_cyclic()
2625{
Bram Moolenaar572cb562005-08-05 21:35:02 +00002626 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002627 int count = 0;
2628
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002629 if (compl_first_match != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002630 {
2631 /*
2632 * Find the end of the list.
2633 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00002634 match = compl_first_match;
2635 /* there's always an entry for the compl_orig_text, it doesn't count. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00002636 while (match->cp_next != NULL && match->cp_next != compl_first_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002637 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00002638 match = match->cp_next;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002639 ++count;
2640 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00002641 match->cp_next = compl_first_match;
2642 compl_first_match->cp_prev = match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002643 }
2644 return count;
2645}
2646
Bram Moolenaara94bc432006-03-10 21:42:59 +00002647/*
2648 * Start completion for the complete() function.
2649 * "startcol" is where the matched text starts (1 is first column).
2650 * "list" is the list of matches.
2651 */
2652 void
2653set_completion(startcol, list)
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002654 colnr_T startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002655 list_T *list;
2656{
2657 /* If already doing completions stop it. */
2658 if (ctrl_x_mode != 0)
2659 ins_compl_prep(' ');
2660 ins_compl_clear();
2661
2662 if (stop_arrow() == FAIL)
2663 return;
2664
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01002665 compl_direction = FORWARD;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002666 if (startcol > curwin->w_cursor.col)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002667 startcol = curwin->w_cursor.col;
2668 compl_col = startcol;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002669 compl_length = (int)curwin->w_cursor.col - (int)startcol;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002670 /* compl_pattern doesn't need to be set */
2671 compl_orig_text = vim_strnsave(ml_get_curline() + compl_col, compl_length);
2672 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00002673 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaara94bc432006-03-10 21:42:59 +00002674 return;
2675
2676 /* Handle like dictionary completion. */
2677 ctrl_x_mode = CTRL_X_WHOLE_LINE;
2678
2679 ins_compl_add_list(list);
2680 compl_matches = ins_compl_make_cyclic();
2681 compl_started = TRUE;
2682 compl_used_match = TRUE;
Bram Moolenaar5495cc92006-08-16 14:23:04 +00002683 compl_cont_status = 0;
Bram Moolenaara94bc432006-03-10 21:42:59 +00002684
2685 compl_curr_match = compl_first_match;
2686 ins_complete(Ctrl_N);
2687 out_flush();
2688}
2689
2690
Bram Moolenaar9372a112005-12-06 19:59:18 +00002691/* "compl_match_array" points the currently displayed list of entries in the
2692 * popup menu. It is NULL when there is no popup menu. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002693static pumitem_T *compl_match_array = NULL;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002694static int compl_match_arraysize;
2695
2696/*
2697 * Update the screen and when there is any scrolling remove the popup menu.
2698 */
2699 static void
2700ins_compl_upd_pum()
2701{
2702 int h;
2703
2704 if (compl_match_array != NULL)
2705 {
2706 h = curwin->w_cline_height;
2707 update_screen(0);
2708 if (h != curwin->w_cline_height)
2709 ins_compl_del_pum();
2710 }
2711}
2712
2713/*
2714 * Remove any popup menu.
2715 */
2716 static void
2717ins_compl_del_pum()
2718{
2719 if (compl_match_array != NULL)
2720 {
2721 pum_undisplay();
2722 vim_free(compl_match_array);
2723 compl_match_array = NULL;
2724 }
2725}
2726
2727/*
2728 * Return TRUE if the popup menu should be displayed.
2729 */
2730 static int
2731pum_wanted()
2732{
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002733 /* 'completeopt' must contain "menu" or "menuone" */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00002734 if (vim_strchr(p_cot, 'm') == NULL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002735 return FALSE;
2736
2737 /* The display looks bad on a B&W display. */
2738 if (t_colors < 8
2739#ifdef FEAT_GUI
2740 && !gui.in_use
2741#endif
2742 )
2743 return FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00002744 return TRUE;
2745}
2746
2747/*
2748 * Return TRUE if there are two or more matches to be shown in the popup menu.
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002749 * One if 'completopt' contains "menuone".
Bram Moolenaara6557602006-02-04 22:43:20 +00002750 */
2751 static int
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002752pum_enough_matches()
Bram Moolenaara6557602006-02-04 22:43:20 +00002753{
2754 compl_T *compl;
2755 int i;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002756
2757 /* Don't display the popup menu if there are no matches or there is only
2758 * one (ignoring the original text). */
2759 compl = compl_first_match;
2760 i = 0;
2761 do
2762 {
2763 if (compl == NULL
2764 || ((compl->cp_flags & ORIGINAL_TEXT) == 0 && ++i == 2))
2765 break;
2766 compl = compl->cp_next;
2767 } while (compl != compl_first_match);
2768
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002769 if (strstr((char *)p_cot, "menuone") != NULL)
2770 return (i >= 1);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002771 return (i >= 2);
2772}
2773
2774/*
2775 * Show the popup menu for the list of matches.
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002776 * Also adjusts "compl_shown_match" to an entry that is actually displayed.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002777 */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002778 void
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002779ins_compl_show_pum()
2780{
2781 compl_T *compl;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002782 compl_T *shown_compl = NULL;
2783 int did_find_shown_match = FALSE;
2784 int shown_match_ok = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002785 int i;
2786 int cur = -1;
2787 colnr_T col;
Bram Moolenaara6557602006-02-04 22:43:20 +00002788 int lead_len = 0;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002789
Bram Moolenaar65c923a2006-03-03 22:56:30 +00002790 if (!pum_wanted() || !pum_enough_matches())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002791 return;
2792
Bram Moolenaar433f7c82006-03-21 21:29:36 +00002793#if defined(FEAT_EVAL)
2794 /* Dirty hard-coded hack: remove any matchparen highlighting. */
2795 do_cmdline_cmd((char_u *)"if exists('g:loaded_matchparen')|3match none|endif");
2796#endif
2797
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002798 /* Update the screen before drawing the popup menu over it. */
2799 update_screen(0);
2800
2801 if (compl_match_array == NULL)
2802 {
2803 /* Need to build the popup menu list. */
2804 compl_match_arraysize = 0;
2805 compl = compl_first_match;
Bram Moolenaara6557602006-02-04 22:43:20 +00002806 if (compl_leader != NULL)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002807 lead_len = (int)STRLEN(compl_leader);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002808 do
2809 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002810 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2811 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002812 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002813 ++compl_match_arraysize;
2814 compl = compl->cp_next;
2815 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaara6557602006-02-04 22:43:20 +00002816 if (compl_match_arraysize == 0)
2817 return;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002818 compl_match_array = (pumitem_T *)alloc_clear(
2819 (unsigned)(sizeof(pumitem_T)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002820 * compl_match_arraysize));
2821 if (compl_match_array != NULL)
2822 {
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002823 /* If the current match is the original text don't find the first
2824 * match after it, don't highlight anything. */
2825 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
2826 shown_match_ok = TRUE;
2827
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002828 i = 0;
2829 compl = compl_first_match;
2830 do
2831 {
Bram Moolenaara6557602006-02-04 22:43:20 +00002832 if ((compl->cp_flags & ORIGINAL_TEXT) == 0
2833 && (compl_leader == NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00002834 || ins_compl_equal(compl, compl_leader, lead_len)))
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002835 {
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002836 if (!shown_match_ok)
2837 {
2838 if (compl == compl_shown_match || did_find_shown_match)
2839 {
2840 /* This item is the shown match or this is the
2841 * first displayed item after the shown match. */
2842 compl_shown_match = compl;
2843 did_find_shown_match = TRUE;
2844 shown_match_ok = TRUE;
2845 }
2846 else
2847 /* Remember this displayed match for when the
2848 * shown match is just below it. */
2849 shown_compl = compl;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002850 cur = i;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002851 }
Bram Moolenaar39f05632006-03-19 22:15:26 +00002852
2853 if (compl->cp_text[CPT_ABBR] != NULL)
2854 compl_match_array[i].pum_text =
2855 compl->cp_text[CPT_ABBR];
2856 else
2857 compl_match_array[i].pum_text = compl->cp_str;
2858 compl_match_array[i].pum_kind = compl->cp_text[CPT_KIND];
2859 compl_match_array[i].pum_info = compl->cp_text[CPT_INFO];
2860 if (compl->cp_text[CPT_MENU] != NULL)
2861 compl_match_array[i++].pum_extra =
2862 compl->cp_text[CPT_MENU];
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002863 else
2864 compl_match_array[i++].pum_extra = compl->cp_fname;
2865 }
2866
2867 if (compl == compl_shown_match)
2868 {
2869 did_find_shown_match = TRUE;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00002870
2871 /* When the original text is the shown match don't set
2872 * compl_shown_match. */
2873 if (compl->cp_flags & ORIGINAL_TEXT)
2874 shown_match_ok = TRUE;
2875
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002876 if (!shown_match_ok && shown_compl != NULL)
2877 {
2878 /* The shown match isn't displayed, set it to the
2879 * previously displayed match. */
2880 compl_shown_match = shown_compl;
2881 shown_match_ok = TRUE;
2882 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002883 }
2884 compl = compl->cp_next;
2885 } while (compl != NULL && compl != compl_first_match);
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002886
2887 if (!shown_match_ok) /* no displayed match at all */
2888 cur = -1;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002889 }
2890 }
2891 else
2892 {
2893 /* popup menu already exists, only need to find the current item.*/
Bram Moolenaara6557602006-02-04 22:43:20 +00002894 for (i = 0; i < compl_match_arraysize; ++i)
Bram Moolenaar39f05632006-03-19 22:15:26 +00002895 if (compl_match_array[i].pum_text == compl_shown_match->cp_str
2896 || compl_match_array[i].pum_text
2897 == compl_shown_match->cp_text[CPT_ABBR])
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002898 {
2899 cur = i;
Bram Moolenaara6557602006-02-04 22:43:20 +00002900 break;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00002901 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002902 }
2903
2904 if (compl_match_array != NULL)
2905 {
2906 /* Compute the screen column of the start of the completed text.
2907 * Use the cursor to get all wrapping and other settings right. */
2908 col = curwin->w_cursor.col;
2909 curwin->w_cursor.col = compl_col;
Bram Moolenaard289f132006-03-11 21:30:53 +00002910 pum_display(compl_match_array, compl_match_arraysize, cur);
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00002911 curwin->w_cursor.col = col;
2912 }
2913}
2914
Bram Moolenaar071d4272004-06-13 20:20:40 +00002915#define DICT_FIRST (1) /* use just first element in "dict" */
2916#define DICT_EXACT (2) /* "dict" is the exact name of a file */
Bram Moolenaar280f1262006-01-30 00:14:18 +00002917
Bram Moolenaar071d4272004-06-13 20:20:40 +00002918/*
Bram Moolenaar0b238792006-03-02 22:49:12 +00002919 * Add any identifiers that match the given pattern in the list of dictionary
2920 * files "dict_start" to the list of completions.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002921 */
2922 static void
Bram Moolenaar0b238792006-03-02 22:49:12 +00002923ins_compl_dictionaries(dict_start, pat, flags, thesaurus)
2924 char_u *dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002925 char_u *pat;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002926 int flags; /* DICT_FIRST and/or DICT_EXACT */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002927 int thesaurus; /* Thesaurus completion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002928{
Bram Moolenaar0b238792006-03-02 22:49:12 +00002929 char_u *dict = dict_start;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002930 char_u *ptr;
2931 char_u *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002932 regmatch_T regmatch;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002933 char_u **files;
2934 int count;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002935 int save_p_scs;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00002936 int dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002937
Bram Moolenaar0b238792006-03-02 22:49:12 +00002938 if (*dict == NUL)
2939 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00002940#ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00002941 /* When 'dictionary' is empty and spell checking is enabled use
2942 * "spell". */
2943 if (!thesaurus && curwin->w_p_spell)
2944 dict = (char_u *)"spell";
2945 else
2946#endif
2947 return;
2948 }
2949
Bram Moolenaar071d4272004-06-13 20:20:40 +00002950 buf = alloc(LSIZE);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002951 if (buf == NULL)
2952 return;
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002953 regmatch.regprog = NULL; /* so that we can goto theend */
Bram Moolenaar0b238792006-03-02 22:49:12 +00002954
Bram Moolenaar071d4272004-06-13 20:20:40 +00002955 /* If 'infercase' is set, don't use 'smartcase' here */
2956 save_p_scs = p_scs;
2957 if (curbuf->b_p_inf)
2958 p_scs = FALSE;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002959
2960 /* When invoked to match whole lines for CTRL-X CTRL-L adjust the pattern
2961 * to only match at the start of a line. Otherwise just match the
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002962 * pattern. Also need to double backslashes. */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002963 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
2964 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002965 char_u *pat_esc = vim_strsave_escaped(pat, (char_u *)"\\");
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002966 size_t len;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002967
2968 if (pat_esc == NULL)
Bram Moolenaar6519ac82007-05-06 13:45:52 +00002969 goto theend;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002970 len = STRLEN(pat_esc) + 10;
2971 ptr = alloc((unsigned)len);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002972 if (ptr == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002973 {
2974 vim_free(pat_esc);
Bram Moolenaarfa3491a2007-02-20 02:49:19 +00002975 goto theend;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002976 }
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00002977 vim_snprintf((char *)ptr, len, "^\\s*\\zs\\V%s", pat_esc);
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002978 regmatch.regprog = vim_regcomp(ptr, RE_MAGIC);
2979 vim_free(pat_esc);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002980 vim_free(ptr);
2981 }
2982 else
Bram Moolenaar0b238792006-03-02 22:49:12 +00002983 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002984 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002985 if (regmatch.regprog == NULL)
2986 goto theend;
2987 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00002988
Bram Moolenaar071d4272004-06-13 20:20:40 +00002989 /* ignore case depends on 'ignorecase', 'smartcase' and "pat" */
2990 regmatch.rm_ic = ignorecase(pat);
Bram Moolenaar0b238792006-03-02 22:49:12 +00002991 while (*dict != NUL && !got_int && !compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00002992 {
2993 /* copy one dictionary file name into buf */
2994 if (flags == DICT_EXACT)
2995 {
2996 count = 1;
2997 files = &dict;
2998 }
2999 else
3000 {
3001 /* Expand wildcards in the dictionary name, but do not allow
3002 * backticks (for security, the 'dict' option may have been set in
3003 * a modeline). */
3004 copy_option_part(&dict, buf, LSIZE, ",");
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003005# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003006 if (!thesaurus && STRCMP(buf, "spell") == 0)
3007 count = -1;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003008 else
3009# endif
3010 if (vim_strchr(buf, '`') != NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00003011 || expand_wildcards(1, &buf, &count, &files,
3012 EW_FILE|EW_SILENT) != OK)
3013 count = 0;
3014 }
3015
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003016# ifdef FEAT_SPELL
Bram Moolenaar0b238792006-03-02 22:49:12 +00003017 if (count == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003018 {
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00003019 /* Complete from active spelling. Skip "\<" in the pattern, we
3020 * don't use it as a RE. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003021 if (pat[0] == '\\' && pat[1] == '<')
3022 ptr = pat + 2;
3023 else
3024 ptr = pat;
Bram Moolenaar860cae12010-06-05 23:22:07 +02003025 spell_dump_compl(ptr, regmatch.rm_ic, &dir, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003026 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003027 else
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003028# endif
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003029 if (count > 0) /* avoid warning for using "files" uninit */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003030 {
3031 ins_compl_files(count, files, thesaurus, flags,
3032 &regmatch, buf, &dir);
3033 if (flags != DICT_EXACT)
3034 FreeWild(count, files);
3035 }
3036 if (flags != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003037 break;
3038 }
Bram Moolenaar0b238792006-03-02 22:49:12 +00003039
3040theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +00003041 p_scs = save_p_scs;
3042 vim_free(regmatch.regprog);
3043 vim_free(buf);
3044}
3045
Bram Moolenaar0b238792006-03-02 22:49:12 +00003046 static void
3047ins_compl_files(count, files, thesaurus, flags, regmatch, buf, dir)
3048 int count;
3049 char_u **files;
3050 int thesaurus;
3051 int flags;
3052 regmatch_T *regmatch;
3053 char_u *buf;
3054 int *dir;
3055{
3056 char_u *ptr;
3057 int i;
3058 FILE *fp;
3059 int add_r;
3060
3061 for (i = 0; i < count && !got_int && !compl_interrupted; i++)
3062 {
3063 fp = mch_fopen((char *)files[i], "r"); /* open dictionary file */
3064 if (flags != DICT_EXACT)
3065 {
3066 vim_snprintf((char *)IObuff, IOSIZE,
3067 _("Scanning dictionary: %s"), (char *)files[i]);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003068 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar0b238792006-03-02 22:49:12 +00003069 }
3070
3071 if (fp != NULL)
3072 {
3073 /*
3074 * Read dictionary file line by line.
3075 * Check each line for a match.
3076 */
3077 while (!got_int && !compl_interrupted
3078 && !vim_fgets(buf, LSIZE, fp))
3079 {
3080 ptr = buf;
3081 while (vim_regexec(regmatch, buf, (colnr_T)(ptr - buf)))
3082 {
3083 ptr = regmatch->startp[0];
3084 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
3085 ptr = find_line_end(ptr);
3086 else
3087 ptr = find_word_end(ptr);
3088 add_r = ins_compl_add_infercase(regmatch->startp[0],
3089 (int)(ptr - regmatch->startp[0]),
Bram Moolenaare8c3a142006-08-29 14:30:35 +00003090 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003091 if (thesaurus)
3092 {
3093 char_u *wstart;
3094
3095 /*
3096 * Add the other matches on the line
3097 */
Bram Moolenaara2993e12007-08-12 14:38:46 +00003098 ptr = buf;
Bram Moolenaar0b238792006-03-02 22:49:12 +00003099 while (!got_int)
3100 {
3101 /* Find start of the next word. Skip white
3102 * space and punctuation. */
3103 ptr = find_word_start(ptr);
3104 if (*ptr == NUL || *ptr == NL)
3105 break;
3106 wstart = ptr;
3107
Bram Moolenaara2993e12007-08-12 14:38:46 +00003108 /* Find end of the word. */
Bram Moolenaar0b238792006-03-02 22:49:12 +00003109#ifdef FEAT_MBYTE
3110 if (has_mbyte)
3111 /* Japanese words may have characters in
3112 * different classes, only separate words
3113 * with single-byte non-word characters. */
3114 while (*ptr != NUL)
3115 {
3116 int l = (*mb_ptr2len)(ptr);
3117
3118 if (l < 2 && !vim_iswordc(*ptr))
3119 break;
3120 ptr += l;
3121 }
3122 else
3123#endif
3124 ptr = find_word_end(ptr);
Bram Moolenaara2993e12007-08-12 14:38:46 +00003125
3126 /* Add the word. Skip the regexp match. */
3127 if (wstart != regmatch->startp[0])
3128 add_r = ins_compl_add_infercase(wstart,
3129 (int)(ptr - wstart),
3130 p_ic, files[i], *dir, 0);
Bram Moolenaar0b238792006-03-02 22:49:12 +00003131 }
3132 }
3133 if (add_r == OK)
3134 /* if dir was BACKWARD then honor it just once */
3135 *dir = FORWARD;
3136 else if (add_r == FAIL)
3137 break;
3138 /* avoid expensive call to vim_regexec() when at end
3139 * of line */
3140 if (*ptr == '\n' || got_int)
3141 break;
3142 }
3143 line_breakcheck();
3144 ins_compl_check_keys(50);
3145 }
3146 fclose(fp);
3147 }
3148 }
3149}
3150
Bram Moolenaar071d4272004-06-13 20:20:40 +00003151/*
3152 * Find the start of the next word.
3153 * Returns a pointer to the first char of the word. Also stops at a NUL.
3154 */
3155 char_u *
3156find_word_start(ptr)
3157 char_u *ptr;
3158{
3159#ifdef FEAT_MBYTE
3160 if (has_mbyte)
3161 while (*ptr != NUL && *ptr != '\n' && mb_get_class(ptr) <= 1)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003162 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003163 else
3164#endif
3165 while (*ptr != NUL && *ptr != '\n' && !vim_iswordc(*ptr))
3166 ++ptr;
3167 return ptr;
3168}
3169
3170/*
3171 * Find the end of the word. Assumes it starts inside a word.
3172 * Returns a pointer to just after the word.
3173 */
3174 char_u *
3175find_word_end(ptr)
3176 char_u *ptr;
3177{
3178#ifdef FEAT_MBYTE
3179 int start_class;
3180
3181 if (has_mbyte)
3182 {
3183 start_class = mb_get_class(ptr);
3184 if (start_class > 1)
3185 while (*ptr != NUL)
3186 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00003187 ptr += (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003188 if (mb_get_class(ptr) != start_class)
3189 break;
3190 }
3191 }
3192 else
3193#endif
3194 while (vim_iswordc(*ptr))
3195 ++ptr;
3196 return ptr;
3197}
3198
3199/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003200 * Find the end of the line, omitting CR and NL at the end.
3201 * Returns a pointer to just after the line.
3202 */
3203 static char_u *
3204find_line_end(ptr)
3205 char_u *ptr;
3206{
3207 char_u *s;
3208
3209 s = ptr + STRLEN(ptr);
3210 while (s > ptr && (s[-1] == CAR || s[-1] == NL))
3211 --s;
3212 return s;
3213}
3214
3215/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003216 * Free the list of completions
3217 */
3218 static void
3219ins_compl_free()
3220{
Bram Moolenaar572cb562005-08-05 21:35:02 +00003221 compl_T *match;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003222 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003223
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003224 vim_free(compl_pattern);
3225 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003226 vim_free(compl_leader);
3227 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003229 if (compl_first_match == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003230 return;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003231
3232 ins_compl_del_pum();
3233 pum_clear();
3234
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003235 compl_curr_match = compl_first_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 do
3237 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003238 match = compl_curr_match;
Bram Moolenaar572cb562005-08-05 21:35:02 +00003239 compl_curr_match = compl_curr_match->cp_next;
3240 vim_free(match->cp_str);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003241 /* several entries may use the same fname, free it just once. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00003242 if (match->cp_flags & FREE_FNAME)
3243 vim_free(match->cp_fname);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003244 for (i = 0; i < CPT_COUNT; ++i)
3245 vim_free(match->cp_text[i]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003246 vim_free(match);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003247 } while (compl_curr_match != NULL && compl_curr_match != compl_first_match);
3248 compl_first_match = compl_curr_match = NULL;
Bram Moolenaar031e0dd2009-07-09 16:15:16 +00003249 compl_shown_match = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003250}
3251
3252 static void
3253ins_compl_clear()
3254{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003255 compl_cont_status = 0;
3256 compl_started = FALSE;
3257 compl_matches = 0;
3258 vim_free(compl_pattern);
3259 compl_pattern = NULL;
Bram Moolenaara6557602006-02-04 22:43:20 +00003260 vim_free(compl_leader);
3261 compl_leader = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003262 edit_submode_extra = NULL;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003263 vim_free(compl_orig_text);
3264 compl_orig_text = NULL;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003265 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003266}
3267
3268/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00003269 * Return TRUE when Insert completion is active.
3270 */
3271 int
3272ins_compl_active()
3273{
3274 return compl_started;
3275}
3276
3277/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003278 * Delete one character before the cursor and show the subset of the matches
3279 * that match the word that is now before the cursor.
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003280 * Returns the character to be used, NUL if the work is done and another char
3281 * to be got from the user.
Bram Moolenaara6557602006-02-04 22:43:20 +00003282 */
3283 static int
3284ins_compl_bs()
3285{
3286 char_u *line;
3287 char_u *p;
3288
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003289 line = ml_get_curline();
3290 p = line + curwin->w_cursor.col;
3291 mb_ptr_back(line, p);
3292
Bram Moolenaar711d5b52007-10-19 18:40:51 +00003293 /* Stop completion when the whole word was deleted. For Omni completion
3294 * allow the word to be deleted, we won't match everything. */
3295 if ((int)(p - line) - (int)compl_col < 0
3296 || ((int)(p - line) - (int)compl_col == 0
3297 && (ctrl_x_mode & CTRL_X_OMNI) == 0))
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003298 return K_BS;
3299
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003300 /* Deleted more than what was used to find matches or didn't finish
3301 * finding all matches: need to look for matches all over again. */
3302 if (curwin->w_cursor.col <= compl_col + compl_length
3303 || compl_was_interrupted)
3304 ins_compl_restart();
Bram Moolenaara6557602006-02-04 22:43:20 +00003305
Bram Moolenaara6557602006-02-04 22:43:20 +00003306 vim_free(compl_leader);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003307 compl_leader = vim_strnsave(line + compl_col, (int)(p - line) - compl_col);
Bram Moolenaara6557602006-02-04 22:43:20 +00003308 if (compl_leader != NULL)
3309 {
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003310 ins_compl_new_leader();
3311 return NUL;
3312 }
3313 return K_BS;
3314}
Bram Moolenaara6557602006-02-04 22:43:20 +00003315
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003316/*
3317 * Called after changing "compl_leader".
3318 * Show the popup menu with a different set of matches.
3319 * May also search for matches again if the previous search was interrupted.
3320 */
3321 static void
3322ins_compl_new_leader()
3323{
3324 ins_compl_del_pum();
3325 ins_compl_delete();
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003326 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003327 compl_used_match = FALSE;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003328
3329 if (compl_started)
3330 ins_compl_set_original_text(compl_leader);
3331 else
3332 {
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003333#ifdef FEAT_SPELL
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003334 spell_bad_len = 0; /* need to redetect bad word */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +00003335#endif
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003336 /*
3337 * Matches were cleared, need to search for them now. First display
3338 * the changed text before the cursor. Set "compl_restarting" to
3339 * avoid that the first match is inserted.
3340 */
3341 update_screen(0);
3342#ifdef FEAT_GUI
3343 if (gui.in_use)
3344 {
3345 /* Show the cursor after the match, not after the redrawn text. */
3346 setcursor();
3347 out_flush();
3348 gui_update_cursor(FALSE, FALSE);
Bram Moolenaara6557602006-02-04 22:43:20 +00003349 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003350#endif
3351 compl_restarting = TRUE;
3352 if (ins_complete(Ctrl_N) == FAIL)
3353 compl_cont_status = 0;
3354 compl_restarting = FALSE;
3355 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003356
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003357 compl_enter_selects = !compl_used_match;
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003358
3359 /* Show the popup menu with a different set of matches. */
3360 ins_compl_show_pum();
Bram Moolenaar7073cc82006-08-29 16:33:06 +00003361
3362 /* Don't let Enter select the original text when there is no popup menu. */
3363 if (compl_match_array == NULL)
3364 compl_enter_selects = FALSE;
Bram Moolenaara6557602006-02-04 22:43:20 +00003365}
3366
3367/*
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003368 * Return the length of the completion, from the completion start column to
3369 * the cursor column. Making sure it never goes below zero.
3370 */
3371 static int
3372ins_compl_len()
3373{
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003374 int off = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003375
3376 if (off < 0)
3377 return 0;
3378 return off;
3379}
3380
3381/*
Bram Moolenaara6557602006-02-04 22:43:20 +00003382 * Append one character to the match leader. May reduce the number of
3383 * matches.
3384 */
3385 static void
3386ins_compl_addleader(c)
3387 int c;
3388{
3389#ifdef FEAT_MBYTE
3390 int cc;
3391
3392 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
3393 {
3394 char_u buf[MB_MAXBYTES + 1];
3395
3396 (*mb_char2bytes)(c, buf);
3397 buf[cc] = NUL;
3398 ins_char_bytes(buf, cc);
3399 }
3400 else
3401#endif
3402 ins_char(c);
3403
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003404 /* If we didn't complete finding matches we must search again. */
3405 if (compl_was_interrupted)
3406 ins_compl_restart();
3407
Bram Moolenaara6557602006-02-04 22:43:20 +00003408 vim_free(compl_leader);
3409 compl_leader = vim_strnsave(ml_get_curline() + compl_col,
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003410 (int)(curwin->w_cursor.col - compl_col));
Bram Moolenaara6557602006-02-04 22:43:20 +00003411 if (compl_leader != NULL)
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00003412 ins_compl_new_leader();
3413}
3414
3415/*
3416 * Setup for finding completions again without leaving CTRL-X mode. Used when
3417 * BS or a key was typed while still searching for matches.
3418 */
3419 static void
3420ins_compl_restart()
3421{
3422 ins_compl_free();
3423 compl_started = FALSE;
3424 compl_matches = 0;
3425 compl_cont_status = 0;
3426 compl_cont_mode = 0;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00003427}
3428
3429/*
3430 * Set the first match, the original text.
3431 */
3432 static void
3433ins_compl_set_original_text(str)
3434 char_u *str;
3435{
3436 char_u *p;
3437
3438 /* Replace the original text entry. */
3439 if (compl_first_match->cp_flags & ORIGINAL_TEXT) /* safety check */
3440 {
3441 p = vim_strsave(str);
3442 if (p != NULL)
3443 {
3444 vim_free(compl_first_match->cp_str);
3445 compl_first_match->cp_str = p;
3446 }
Bram Moolenaara6557602006-02-04 22:43:20 +00003447 }
3448}
3449
3450/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003451 * Append one character to the match leader. May reduce the number of
3452 * matches.
3453 */
3454 static void
3455ins_compl_addfrommatch()
3456{
3457 char_u *p;
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00003458 int len = (int)curwin->w_cursor.col - (int)compl_col;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003459 int c;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003460 compl_T *cp;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003461
3462 p = compl_shown_match->cp_str;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00003463 if ((int)STRLEN(p) <= len) /* the match is too short */
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003464 {
3465 /* When still at the original match use the first entry that matches
3466 * the leader. */
3467 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
3468 {
3469 p = NULL;
3470 for (cp = compl_shown_match->cp_next; cp != NULL
3471 && cp != compl_first_match; cp = cp->cp_next)
3472 {
Bram Moolenaar132283f2006-10-03 13:22:23 +00003473 if (compl_leader == NULL
3474 || ins_compl_equal(cp, compl_leader,
Bram Moolenaar0440ca32006-05-13 13:24:33 +00003475 (int)STRLEN(compl_leader)))
3476 {
3477 p = cp->cp_str;
3478 break;
3479 }
3480 }
3481 if (p == NULL || (int)STRLEN(p) <= len)
3482 return;
3483 }
3484 else
3485 return;
3486 }
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003487 p += len;
3488#ifdef FEAT_MBYTE
3489 c = mb_ptr2char(p);
3490#else
3491 c = *p;
3492#endif
3493 ins_compl_addleader(c);
3494}
3495
3496/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497 * Prepare for Insert mode completion, or stop it.
Bram Moolenaar572cb562005-08-05 21:35:02 +00003498 * Called just after typing a character in Insert mode.
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003499 * Returns TRUE when the character is not to be inserted;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003501 static int
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502ins_compl_prep(c)
3503 int c;
3504{
3505 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 int want_cindent;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003507 int retval = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508
3509 /* Forget any previous 'special' messages if this is actually
3510 * a ^X mode key - bar ^R, in which case we wait to see what it gives us.
3511 */
3512 if (c != Ctrl_R && vim_is_ctrl_x_key(c))
3513 edit_submode_extra = NULL;
3514
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00003515 /* Ignore end of Select mode mapping and mouse scroll buttons. */
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02003516 if (c == K_SELECT || c == K_MOUSEDOWN || c == K_MOUSEUP
3517 || c == K_MOUSELEFT || c == K_MOUSERIGHT)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003518 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519
Bram Moolenaarc7453f52006-02-10 23:20:28 +00003520 /* Set "compl_get_longest" when finding the first matches. */
3521 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET
3522 || (ctrl_x_mode == 0 && !compl_started))
3523 {
3524 compl_get_longest = (vim_strchr(p_cot, 'l') != NULL);
3525 compl_used_match = TRUE;
3526 }
3527
Bram Moolenaar071d4272004-06-13 20:20:40 +00003528 if (ctrl_x_mode == CTRL_X_NOT_DEFINED_YET)
3529 {
3530 /*
3531 * We have just typed CTRL-X and aren't quite sure which CTRL-X mode
3532 * it will be yet. Now we decide.
3533 */
3534 switch (c)
3535 {
3536 case Ctrl_E:
3537 case Ctrl_Y:
3538 ctrl_x_mode = CTRL_X_SCROLL;
3539 if (!(State & REPLACE_FLAG))
3540 edit_submode = (char_u *)_(" (insert) Scroll (^E/^Y)");
3541 else
3542 edit_submode = (char_u *)_(" (replace) Scroll (^E/^Y)");
3543 edit_submode_pre = NULL;
3544 showmode();
3545 break;
3546 case Ctrl_L:
3547 ctrl_x_mode = CTRL_X_WHOLE_LINE;
3548 break;
3549 case Ctrl_F:
3550 ctrl_x_mode = CTRL_X_FILES;
3551 break;
3552 case Ctrl_K:
3553 ctrl_x_mode = CTRL_X_DICTIONARY;
3554 break;
3555 case Ctrl_R:
3556 /* Simply allow ^R to happen without affecting ^X mode */
3557 break;
3558 case Ctrl_T:
3559 ctrl_x_mode = CTRL_X_THESAURUS;
3560 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003561#ifdef FEAT_COMPL_FUNC
3562 case Ctrl_U:
3563 ctrl_x_mode = CTRL_X_FUNCTION;
3564 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003565 case Ctrl_O:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003566 ctrl_x_mode = CTRL_X_OMNI;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003567 break;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003568#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003569 case 's':
3570 case Ctrl_S:
3571 ctrl_x_mode = CTRL_X_SPELL;
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00003572#ifdef FEAT_SPELL
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003573 ++emsg_off; /* Avoid getting the E756 error twice. */
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003574 spell_back_to_badword();
Bram Moolenaar910f66f2006-04-05 20:41:53 +00003575 --emsg_off;
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00003576#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00003577 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 case Ctrl_RSB:
3579 ctrl_x_mode = CTRL_X_TAGS;
3580 break;
3581#ifdef FEAT_FIND_ID
3582 case Ctrl_I:
3583 case K_S_TAB:
3584 ctrl_x_mode = CTRL_X_PATH_PATTERNS;
3585 break;
3586 case Ctrl_D:
3587 ctrl_x_mode = CTRL_X_PATH_DEFINES;
3588 break;
3589#endif
3590 case Ctrl_V:
3591 case Ctrl_Q:
3592 ctrl_x_mode = CTRL_X_CMDLINE;
3593 break;
3594 case Ctrl_P:
3595 case Ctrl_N:
3596 /* ^X^P means LOCAL expansion if nothing interrupted (eg we
3597 * just started ^X mode, or there were enough ^X's to cancel
3598 * the previous mode, say ^X^F^X^X^P or ^P^X^X^X^P, see below)
3599 * do normal expansion when interrupting a different mode (say
3600 * ^X^F^X^P or ^P^X^X^P, see below)
3601 * nothing changes if interrupting mode 0, (eg, the flag
3602 * doesn't change when going to ADDING mode -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003603 if (!(compl_cont_status & CONT_INTRPT))
3604 compl_cont_status |= CONT_LOCAL;
3605 else if (compl_cont_mode != 0)
3606 compl_cont_status &= ~CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 /* FALLTHROUGH */
3608 default:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003609 /* If we have typed at least 2 ^X's... for modes != 0, we set
3610 * compl_cont_status = 0 (eg, as if we had just started ^X
3611 * mode).
3612 * For mode 0, we set "compl_cont_mode" to an impossible
3613 * value, in both cases ^X^X can be used to restart the same
3614 * mode (avoiding ADDING mode).
3615 * Undocumented feature: In a mode != 0 ^X^P and ^X^X^P start
3616 * 'complete' and local ^P expansions respectively.
3617 * In mode 0 an extra ^X is needed since ^X^P goes to ADDING
3618 * mode -- Acevedo */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (c == Ctrl_X)
3620 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003621 if (compl_cont_mode != 0)
3622 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003624 compl_cont_mode = CTRL_X_NOT_DEFINED_YET;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 }
3626 ctrl_x_mode = 0;
3627 edit_submode = NULL;
3628 showmode();
3629 break;
3630 }
3631 }
3632 else if (ctrl_x_mode != 0)
3633 {
3634 /* We're already in CTRL-X mode, do we stay in it? */
3635 if (!vim_is_ctrl_x_key(c))
3636 {
3637 if (ctrl_x_mode == CTRL_X_SCROLL)
3638 ctrl_x_mode = 0;
3639 else
3640 ctrl_x_mode = CTRL_X_FINISHED;
3641 edit_submode = NULL;
3642 }
3643 showmode();
3644 }
3645
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003646 if (compl_started || ctrl_x_mode == CTRL_X_FINISHED)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 {
3648 /* Show error message from attempted keyword completion (probably
3649 * 'Pattern not found') until another key is hit, then go back to
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003650 * showing what mode we are in. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 showmode();
Bram Moolenaard12f5c12006-01-25 22:10:52 +00003652 if ((ctrl_x_mode == 0 && c != Ctrl_N && c != Ctrl_P && c != Ctrl_R
3653 && !ins_compl_pum_key(c))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 || ctrl_x_mode == CTRL_X_FINISHED)
3655 {
3656 /* Get here when we have finished typing a sequence of ^N and
3657 * ^P or other completion characters in CTRL-X mode. Free up
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003658 * memory that was used, and make sure we can redo the insert. */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003659 if (compl_curr_match != NULL || compl_leader != NULL || c == Ctrl_E)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003661 char_u *p;
Bram Moolenaard7fd0c42006-08-22 17:55:55 +00003662 int temp = 0;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003663
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664 /*
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003665 * If any of the original typed text has been changed, eg when
3666 * ignorecase is set, we must add back-spaces to the redo
3667 * buffer. We add as few as necessary to delete just the part
3668 * of the original text that has changed.
3669 * When using the longest match, edited the match or used
3670 * CTRL-E then don't use the current match.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003671 */
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003672 if (compl_curr_match != NULL && compl_used_match && c != Ctrl_E)
3673 ptr = compl_curr_match->cp_str;
3674 else if (compl_leader != NULL)
3675 ptr = compl_leader;
3676 else
3677 ptr = compl_orig_text;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003678 if (compl_orig_text != NULL)
3679 {
3680 p = compl_orig_text;
3681 for (temp = 0; p[temp] != NUL && p[temp] == ptr[temp];
3682 ++temp)
3683 ;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003684#ifdef FEAT_MBYTE
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003685 if (temp > 0)
3686 temp -= (*mb_head_off)(compl_orig_text, p + temp);
Bram Moolenaarc1e37902006-04-18 21:55:01 +00003687#endif
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00003688 for (p += temp; *p != NUL; mb_ptr_adv(p))
3689 AppendCharToRedobuff(K_BS);
3690 }
3691 if (ptr != NULL)
3692 AppendToRedobuffLit(ptr + temp, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003693 }
3694
3695#ifdef FEAT_CINDENT
3696 want_cindent = (can_cindent && cindent_on());
3697#endif
3698 /*
3699 * When completing whole lines: fix indent for 'cindent'.
3700 * Otherwise, break line if it's too long.
3701 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003702 if (compl_cont_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003703 {
3704#ifdef FEAT_CINDENT
3705 /* re-indent the current line */
3706 if (want_cindent)
3707 {
3708 do_c_expr_indent();
3709 want_cindent = FALSE; /* don't do it again */
3710 }
3711#endif
3712 }
3713 else
3714 {
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003715 int prev_col = curwin->w_cursor.col;
3716
Bram Moolenaar071d4272004-06-13 20:20:40 +00003717 /* put the cursor on the last char, for 'tw' formatting */
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003718 if (prev_col > 0)
3719 dec_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003720 if (stop_arrow() == OK)
3721 insertchar(NUL, 0, -1);
Bram Moolenaar09a16b52007-02-20 02:31:20 +00003722 if (prev_col > 0
3723 && ml_get_curline()[curwin->w_cursor.col] != NUL)
3724 inc_cursor();
Bram Moolenaar071d4272004-06-13 20:20:40 +00003725 }
3726
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003727 /* If the popup menu is displayed pressing CTRL-Y means accepting
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003728 * the selection without inserting anything. When
3729 * compl_enter_selects is set the Enter key does the same. */
3730 if ((c == Ctrl_Y || (compl_enter_selects
3731 && (c == CAR || c == K_KENTER || c == NL)))
3732 && pum_visible())
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003733 retval = TRUE;
3734
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003735 /* CTRL-E means completion is Ended, go back to the typed text. */
3736 if (c == Ctrl_E)
3737 {
3738 ins_compl_delete();
3739 if (compl_leader != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003740 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003741 else if (compl_first_match != NULL)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00003742 ins_bytes(compl_orig_text + ins_compl_len());
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00003743 retval = TRUE;
3744 }
3745
Bram Moolenaare37d50a2008-08-06 17:06:04 +00003746 auto_format(FALSE, TRUE);
3747
Bram Moolenaar071d4272004-06-13 20:20:40 +00003748 ins_compl_free();
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003749 compl_started = FALSE;
3750 compl_matches = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003751 msg_clr_cmdline(); /* necessary for "noshowmode" */
3752 ctrl_x_mode = 0;
Bram Moolenaar779b74b2006-04-10 14:55:34 +00003753 compl_enter_selects = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003754 if (edit_submode != NULL)
3755 {
3756 edit_submode = NULL;
3757 showmode();
3758 }
3759
3760#ifdef FEAT_CINDENT
3761 /*
3762 * Indent now if a key was typed that is in 'cinkeys'.
3763 */
3764 if (want_cindent && in_cinkeys(KEY_COMPLETE, ' ', inindent(0)))
3765 do_c_expr_indent();
3766#endif
3767 }
3768 }
3769
3770 /* reset continue_* if we left expansion-mode, if we stay they'll be
3771 * (re)set properly in ins_complete() */
3772 if (!vim_is_ctrl_x_key(c))
3773 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003774 compl_cont_status = 0;
3775 compl_cont_mode = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00003777
3778 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779}
3780
3781/*
3782 * Loops through the list of windows, loaded-buffers or non-loaded-buffers
3783 * (depending on flag) starting from buf and looking for a non-scanned
3784 * buffer (other than curbuf). curbuf is special, if it is called with
3785 * buf=curbuf then it has to be the first call for a given flag/expansion.
3786 *
3787 * Returns the buffer to scan, if any, otherwise returns curbuf -- Acevedo
3788 */
3789 static buf_T *
3790ins_compl_next_buf(buf, flag)
3791 buf_T *buf;
3792 int flag;
3793{
3794#ifdef FEAT_WINDOWS
3795 static win_T *wp;
3796#endif
3797
3798 if (flag == 'w') /* just windows */
3799 {
3800#ifdef FEAT_WINDOWS
3801 if (buf == curbuf) /* first call for this flag/expansion */
3802 wp = curwin;
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003803 while ((wp = (wp->w_next != NULL ? wp->w_next : firstwin)) != curwin
Bram Moolenaar071d4272004-06-13 20:20:40 +00003804 && wp->w_buffer->b_scanned)
3805 ;
3806 buf = wp->w_buffer;
3807#else
3808 buf = curbuf;
3809#endif
3810 }
3811 else
3812 /* 'b' (just loaded buffers), 'u' (just non-loaded buffers) or 'U'
3813 * (unlisted buffers)
3814 * When completing whole lines skip unloaded buffers. */
Bram Moolenaar1f8a5f02005-07-01 22:41:52 +00003815 while ((buf = (buf->b_next != NULL ? buf->b_next : firstbuf)) != curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00003816 && ((flag == 'U'
3817 ? buf->b_p_bl
3818 : (!buf->b_p_bl
3819 || (buf->b_ml.ml_mfp == NULL) != (flag == 'u')))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003820 || buf->b_scanned))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003821 ;
3822 return buf;
3823}
3824
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003825#ifdef FEAT_COMPL_FUNC
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003826static void expand_by_function __ARGS((int type, char_u *base));
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003827
3828/*
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003829 * Execute user defined complete function 'completefunc' or 'omnifunc', and
Bram Moolenaare344bea2005-09-01 20:46:49 +00003830 * get matches in "matches".
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003831 */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003832 static void
3833expand_by_function(type, base)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00003834 int type; /* CTRL_X_OMNI or CTRL_X_FUNCTION */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003835 char_u *base;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003836{
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003837 list_T *matchlist;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003838 char_u *args[2];
Bram Moolenaare344bea2005-09-01 20:46:49 +00003839 char_u *funcname;
3840 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003841 win_T *curwin_save;
3842 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003843
Bram Moolenaare344bea2005-09-01 20:46:49 +00003844 funcname = (type == CTRL_X_FUNCTION) ? curbuf->b_p_cfu : curbuf->b_p_ofu;
3845 if (*funcname == NUL)
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003846 return;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003847
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003848 /* Call 'completefunc' to obtain the list of matches. */
3849 args[0] = (char_u *)"0";
Bram Moolenaare344bea2005-09-01 20:46:49 +00003850 args[1] = base;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003851
Bram Moolenaare344bea2005-09-01 20:46:49 +00003852 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003853 curwin_save = curwin;
3854 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00003855 matchlist = call_func_retlist(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003856 if (curwin_save != curwin || curbuf_save != curbuf)
3857 {
3858 EMSG(_(e_complwin));
3859 goto theend;
3860 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00003861 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003862 check_cursor();
3863 if (!equalpos(curwin->w_cursor, pos))
3864 {
3865 EMSG(_(e_compldel));
3866 goto theend;
3867 }
3868 if (matchlist != NULL)
3869 ins_compl_add_list(matchlist);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00003870
Bram Moolenaar37dd0182010-11-10 16:54:20 +01003871theend:
3872 if (matchlist != NULL)
3873 list_unref(matchlist);
Bram Moolenaara94bc432006-03-10 21:42:59 +00003874}
3875#endif /* FEAT_COMPL_FUNC */
3876
Bram Moolenaar39f05632006-03-19 22:15:26 +00003877#if defined(FEAT_COMPL_FUNC) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00003878/*
3879 * Add completions from a list.
Bram Moolenaara94bc432006-03-10 21:42:59 +00003880 */
3881 static void
3882ins_compl_add_list(list)
3883 list_T *list;
3884{
3885 listitem_T *li;
Bram Moolenaara94bc432006-03-10 21:42:59 +00003886 int dir = compl_direction;
3887
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003888 /* Go through the List with matches and add each of them. */
Bram Moolenaara94bc432006-03-10 21:42:59 +00003889 for (li = list->lv_first; li != NULL; li = li->li_next)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003890 {
Bram Moolenaar39f05632006-03-19 22:15:26 +00003891 if (ins_compl_add_tv(&li->li_tv, dir) == OK)
3892 /* if dir was BACKWARD then honor it just once */
3893 dir = FORWARD;
Bram Moolenaar280f1262006-01-30 00:14:18 +00003894 else if (did_emsg)
3895 break;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003896 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003897}
Bram Moolenaar39f05632006-03-19 22:15:26 +00003898
3899/*
3900 * Add a match to the list of matches from a typeval_T.
3901 * If the given string is already in the list of completions, then return
3902 * NOTDONE, otherwise add it to the list and return OK. If there is an error,
3903 * maybe because alloc() returns NULL, then FAIL is returned.
3904 */
3905 int
3906ins_compl_add_tv(tv, dir)
3907 typval_T *tv;
3908 int dir;
3909{
3910 char_u *word;
Bram Moolenaar91170f82006-05-05 21:15:17 +00003911 int icase = FALSE;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003912 int adup = FALSE;
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01003913 int aempty = FALSE;
Bram Moolenaar39f05632006-03-19 22:15:26 +00003914 char_u *(cptext[CPT_COUNT]);
3915
3916 if (tv->v_type == VAR_DICT && tv->vval.v_dict != NULL)
3917 {
3918 word = get_dict_string(tv->vval.v_dict, (char_u *)"word", FALSE);
3919 cptext[CPT_ABBR] = get_dict_string(tv->vval.v_dict,
3920 (char_u *)"abbr", FALSE);
3921 cptext[CPT_MENU] = get_dict_string(tv->vval.v_dict,
3922 (char_u *)"menu", FALSE);
3923 cptext[CPT_KIND] = get_dict_string(tv->vval.v_dict,
3924 (char_u *)"kind", FALSE);
3925 cptext[CPT_INFO] = get_dict_string(tv->vval.v_dict,
3926 (char_u *)"info", FALSE);
3927 if (get_dict_string(tv->vval.v_dict, (char_u *)"icase", FALSE) != NULL)
3928 icase = get_dict_number(tv->vval.v_dict, (char_u *)"icase");
Bram Moolenaar4a85b412006-04-23 22:40:29 +00003929 if (get_dict_string(tv->vval.v_dict, (char_u *)"dup", FALSE) != NULL)
Bram Moolenaar89d40322006-08-29 15:30:07 +00003930 adup = get_dict_number(tv->vval.v_dict, (char_u *)"dup");
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01003931 if (get_dict_string(tv->vval.v_dict, (char_u *)"empty", FALSE) != NULL)
3932 aempty = get_dict_number(tv->vval.v_dict, (char_u *)"empty");
Bram Moolenaar39f05632006-03-19 22:15:26 +00003933 }
3934 else
3935 {
3936 word = get_tv_string_chk(tv);
3937 vim_memset(cptext, 0, sizeof(cptext));
3938 }
Bram Moolenaar2a8caa42010-11-10 17:11:33 +01003939 if (word == NULL || (!aempty && *word == NUL))
Bram Moolenaar39f05632006-03-19 22:15:26 +00003940 return FAIL;
Bram Moolenaar89d40322006-08-29 15:30:07 +00003941 return ins_compl_add(word, -1, icase, NULL, cptext, dir, 0, adup);
Bram Moolenaar39f05632006-03-19 22:15:26 +00003942}
Bram Moolenaara94bc432006-03-10 21:42:59 +00003943#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003944
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003945/*
3946 * Get the next expansion(s), using "compl_pattern".
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003947 * The search starts at position "ini" in curbuf and in the direction
3948 * compl_direction.
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003949 * When "compl_started" is FALSE start at that position, otherwise continue
3950 * where we stopped searching before.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003951 * This may return before finding all the matches.
3952 * Return the total number of matches or -1 if still unknown -- Acevedo
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953 */
3954 static int
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003955ins_compl_get_exp(ini)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956 pos_T *ini;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003957{
3958 static pos_T first_match_pos;
3959 static pos_T last_match_pos;
3960 static char_u *e_cpt = (char_u *)""; /* curr. entry in 'complete' */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003961 static int found_all = FALSE; /* Found all matches of a
3962 certain type. */
3963 static buf_T *ins_buf = NULL; /* buffer being scanned */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964
Bram Moolenaar572cb562005-08-05 21:35:02 +00003965 pos_T *pos;
3966 char_u **matches;
3967 int save_p_scs;
3968 int save_p_ws;
3969 int save_p_ic;
3970 int i;
3971 int num_matches;
3972 int len;
3973 int found_new_match;
3974 int type = ctrl_x_mode;
3975 char_u *ptr;
3976 char_u *dict = NULL;
3977 int dict_f = 0;
3978 compl_T *old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003979
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003980 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981 {
3982 for (ins_buf = firstbuf; ins_buf != NULL; ins_buf = ins_buf->b_next)
3983 ins_buf->b_scanned = 0;
3984 found_all = FALSE;
3985 ins_buf = curbuf;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003986 e_cpt = (compl_cont_status & CONT_LOCAL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003987 ? (char_u *)"." : curbuf->b_p_cpt;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003988 last_match_pos = first_match_pos = *ini;
3989 }
3990
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003991 old_match = compl_curr_match; /* remember the last current match */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00003992 pos = (compl_direction == FORWARD) ? &last_match_pos : &first_match_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993 /* For ^N/^P loop over all the flags/windows/buffers in 'complete' */
3994 for (;;)
3995 {
3996 found_new_match = FAIL;
3997
Bram Moolenaar4be06f92005-07-29 22:36:03 +00003998 /* For ^N/^P pick a new entry from e_cpt if compl_started is off,
Bram Moolenaar071d4272004-06-13 20:20:40 +00003999 * or if found_all says this entry is done. For ^X^L only use the
4000 * entries from 'complete' that look in loaded buffers. */
4001 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004002 && (!compl_started || found_all))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 {
4004 found_all = FALSE;
4005 while (*e_cpt == ',' || *e_cpt == ' ')
4006 e_cpt++;
4007 if (*e_cpt == '.' && !curbuf->b_scanned)
4008 {
4009 ins_buf = curbuf;
4010 first_match_pos = *ini;
4011 /* So that ^N can match word immediately after cursor */
4012 if (ctrl_x_mode == 0)
4013 dec(&first_match_pos);
4014 last_match_pos = first_match_pos;
4015 type = 0;
4016 }
4017 else if (vim_strchr((char_u *)"buwU", *e_cpt) != NULL
4018 && (ins_buf = ins_compl_next_buf(ins_buf, *e_cpt)) != curbuf)
4019 {
4020 /* Scan a buffer, but not the current one. */
4021 if (ins_buf->b_ml.ml_mfp != NULL) /* loaded buffer */
4022 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004023 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024 first_match_pos.col = last_match_pos.col = 0;
4025 first_match_pos.lnum = ins_buf->b_ml.ml_line_count + 1;
4026 last_match_pos.lnum = 0;
4027 type = 0;
4028 }
4029 else /* unloaded buffer, scan like dictionary */
4030 {
4031 found_all = TRUE;
4032 if (ins_buf->b_fname == NULL)
4033 continue;
4034 type = CTRL_X_DICTIONARY;
4035 dict = ins_buf->b_fname;
4036 dict_f = DICT_EXACT;
4037 }
Bram Moolenaar555b2802005-05-19 21:08:39 +00004038 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning: %s"),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004039 ins_buf->b_fname == NULL
4040 ? buf_spname(ins_buf)
4041 : ins_buf->b_sfname == NULL
4042 ? (char *)ins_buf->b_fname
4043 : (char *)ins_buf->b_sfname);
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004044 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 }
4046 else if (*e_cpt == NUL)
4047 break;
4048 else
4049 {
4050 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4051 type = -1;
4052 else if (*e_cpt == 'k' || *e_cpt == 's')
4053 {
4054 if (*e_cpt == 'k')
4055 type = CTRL_X_DICTIONARY;
4056 else
4057 type = CTRL_X_THESAURUS;
4058 if (*++e_cpt != ',' && *e_cpt != NUL)
4059 {
4060 dict = e_cpt;
4061 dict_f = DICT_FIRST;
4062 }
4063 }
4064#ifdef FEAT_FIND_ID
4065 else if (*e_cpt == 'i')
4066 type = CTRL_X_PATH_PATTERNS;
4067 else if (*e_cpt == 'd')
4068 type = CTRL_X_PATH_DEFINES;
4069#endif
4070 else if (*e_cpt == ']' || *e_cpt == 't')
4071 {
4072 type = CTRL_X_TAGS;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004073 vim_snprintf((char *)IObuff, IOSIZE, _("Scanning tags."));
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00004074 (void)msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
4076 else
4077 type = -1;
4078
4079 /* in any case e_cpt is advanced to the next entry */
4080 (void)copy_option_part(&e_cpt, IObuff, IOSIZE, ",");
4081
4082 found_all = TRUE;
4083 if (type == -1)
4084 continue;
4085 }
4086 }
4087
4088 switch (type)
4089 {
4090 case -1:
4091 break;
4092#ifdef FEAT_FIND_ID
4093 case CTRL_X_PATH_PATTERNS:
4094 case CTRL_X_PATH_DEFINES:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004095 find_pattern_in_path(compl_pattern, compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004096 (int)STRLEN(compl_pattern), FALSE, FALSE,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004097 (type == CTRL_X_PATH_DEFINES
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004098 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099 ? FIND_DEFINE : FIND_ANY, 1L, ACTION_EXPAND,
4100 (linenr_T)1, (linenr_T)MAXLNUM);
4101 break;
4102#endif
4103
4104 case CTRL_X_DICTIONARY:
4105 case CTRL_X_THESAURUS:
4106 ins_compl_dictionaries(
Bram Moolenaar0b238792006-03-02 22:49:12 +00004107 dict != NULL ? dict
Bram Moolenaar071d4272004-06-13 20:20:40 +00004108 : (type == CTRL_X_THESAURUS
4109 ? (*curbuf->b_p_tsr == NUL
4110 ? p_tsr
4111 : curbuf->b_p_tsr)
4112 : (*curbuf->b_p_dict == NUL
4113 ? p_dict
4114 : curbuf->b_p_dict)),
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004115 compl_pattern,
Bram Moolenaar0b238792006-03-02 22:49:12 +00004116 dict != NULL ? dict_f
4117 : 0, type == CTRL_X_THESAURUS);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004118 dict = NULL;
4119 break;
4120
4121 case CTRL_X_TAGS:
4122 /* set p_ic according to p_ic, p_scs and pat for find_tags(). */
4123 save_p_ic = p_ic;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004124 p_ic = ignorecase(compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004125
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004126 /* Find up to TAG_MANY matches. Avoids that an enormous number
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004127 * of matches is found when compl_pattern is empty */
4128 if (find_tags(compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 TAG_REGEXP | TAG_NAMES | TAG_NOIC |
4130 TAG_INS_COMP | (ctrl_x_mode ? TAG_VERBOSE : 0),
4131 TAG_MANY, curbuf->b_ffname) == OK && num_matches > 0)
4132 {
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004133 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004134 }
4135 p_ic = save_p_ic;
4136 break;
4137
4138 case CTRL_X_FILES:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004139 if (expand_wildcards(1, &compl_pattern, &num_matches, &matches,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004140 EW_FILE|EW_DIR|EW_ADDSLASH|EW_SILENT) == OK)
4141 {
4142
4143 /* May change home directory back to "~". */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004144 tilde_replace(compl_pattern, num_matches, matches);
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004145 ins_compl_add_matches(num_matches, matches,
4146#ifdef CASE_INSENSITIVE_FILENAME
4147 TRUE
4148#else
4149 FALSE
4150#endif
4151 );
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 }
4153 break;
4154
4155 case CTRL_X_CMDLINE:
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004156 if (expand_cmdline(&compl_xp, compl_pattern,
4157 (int)STRLEN(compl_pattern),
Bram Moolenaar071d4272004-06-13 20:20:40 +00004158 &num_matches, &matches) == EXPAND_OK)
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004159 ins_compl_add_matches(num_matches, matches, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004160 break;
4161
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004162#ifdef FEAT_COMPL_FUNC
4163 case CTRL_X_FUNCTION:
Bram Moolenaarf75a9632005-09-13 21:20:47 +00004164 case CTRL_X_OMNI:
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004165 expand_by_function(type, compl_pattern);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004166 break;
4167#endif
4168
Bram Moolenaar488c6512005-08-11 20:09:58 +00004169 case CTRL_X_SPELL:
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00004170#ifdef FEAT_SPELL
Bram Moolenaar488c6512005-08-11 20:09:58 +00004171 num_matches = expand_spelling(first_match_pos.lnum,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004172 compl_pattern, &matches);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004173 if (num_matches > 0)
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004174 ins_compl_add_matches(num_matches, matches, p_ic);
Bram Moolenaar488c6512005-08-11 20:09:58 +00004175#endif
4176 break;
4177
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178 default: /* normal ^P/^N and ^X^L */
4179 /*
4180 * If 'infercase' is set, don't use 'smartcase' here
4181 */
4182 save_p_scs = p_scs;
4183 if (ins_buf->b_p_inf)
4184 p_scs = FALSE;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004185
Bram Moolenaar071d4272004-06-13 20:20:40 +00004186 /* buffers other than curbuf are scanned from the beginning or the
4187 * end but never from the middle, thus setting nowrapscan in this
4188 * buffers is a good idea, on the other hand, we always set
4189 * wrapscan for curbuf to avoid missing matches -- Acevedo,Webb */
4190 save_p_ws = p_ws;
4191 if (ins_buf != curbuf)
4192 p_ws = FALSE;
4193 else if (*e_cpt == '.')
4194 p_ws = TRUE;
4195 for (;;)
4196 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00004197 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004199 ++msg_silent; /* Don't want messages for wrapscan. */
4200
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004201 /* ctrl_x_mode == CTRL_X_WHOLE_LINE || word-wise search that
4202 * has added a word that was at the beginning of the line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 if ( ctrl_x_mode == CTRL_X_WHOLE_LINE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004204 || (compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004205 found_new_match = search_for_exact_line(ins_buf, pos,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004206 compl_direction, compl_pattern);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004207 else
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004208 found_new_match = searchit(NULL, ins_buf, pos,
4209 compl_direction,
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004210 compl_pattern, 1L, SEARCH_KEEP + SEARCH_NFMSG,
Bram Moolenaar76929292008-01-06 19:07:36 +00004211 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004212 --msg_silent;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004213 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00004215 /* set "compl_started" even on fail */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004216 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004217 first_match_pos = *pos;
4218 last_match_pos = *pos;
4219 }
4220 else if (first_match_pos.lnum == last_match_pos.lnum
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004221 && first_match_pos.col == last_match_pos.col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222 found_new_match = FAIL;
4223 if (found_new_match == FAIL)
4224 {
4225 if (ins_buf == curbuf)
4226 found_all = TRUE;
4227 break;
4228 }
4229
4230 /* when ADDING, the text before the cursor matches, skip it */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004231 if ( (compl_cont_status & CONT_ADDING) && ins_buf == curbuf
Bram Moolenaar071d4272004-06-13 20:20:40 +00004232 && ini->lnum == pos->lnum
4233 && ini->col == pos->col)
4234 continue;
4235 ptr = ml_get_buf(ins_buf, pos->lnum, FALSE) + pos->col;
4236 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4237 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004238 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239 {
4240 if (pos->lnum >= ins_buf->b_ml.ml_line_count)
4241 continue;
4242 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4243 if (!p_paste)
4244 ptr = skipwhite(ptr);
4245 }
4246 len = (int)STRLEN(ptr);
4247 }
4248 else
4249 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004250 char_u *tmp_ptr = ptr;
4251
4252 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004254 tmp_ptr += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 /* Skip if already inside a word. */
4256 if (vim_iswordp(tmp_ptr))
4257 continue;
4258 /* Find start of next word. */
4259 tmp_ptr = find_word_start(tmp_ptr);
4260 }
4261 /* Find end of this word. */
4262 tmp_ptr = find_word_end(tmp_ptr);
4263 len = (int)(tmp_ptr - ptr);
4264
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004265 if ((compl_cont_status & CONT_ADDING)
4266 && len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 {
4268 if (pos->lnum < ins_buf->b_ml.ml_line_count)
4269 {
4270 /* Try next line, if any. the new word will be
4271 * "join" as if the normal command "J" was used.
4272 * IOSIZE is always greater than
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004273 * compl_length, so the next STRNCPY always
Bram Moolenaar071d4272004-06-13 20:20:40 +00004274 * works -- Acevedo */
4275 STRNCPY(IObuff, ptr, len);
4276 ptr = ml_get_buf(ins_buf, pos->lnum + 1, FALSE);
4277 tmp_ptr = ptr = skipwhite(ptr);
4278 /* Find start of next word. */
4279 tmp_ptr = find_word_start(tmp_ptr);
4280 /* Find end of next word. */
4281 tmp_ptr = find_word_end(tmp_ptr);
4282 if (tmp_ptr > ptr)
4283 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004284 if (*ptr != ')' && IObuff[len - 1] != TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 {
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004286 if (IObuff[len - 1] != ' ')
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 IObuff[len++] = ' ';
4288 /* IObuf =~ "\k.* ", thus len >= 2 */
4289 if (p_js
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004290 && (IObuff[len - 2] == '.'
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 || (vim_strchr(p_cpo, CPO_JOINSP)
4292 == NULL
Bram Moolenaarce0842a2005-07-18 21:58:11 +00004293 && (IObuff[len - 2] == '?'
4294 || IObuff[len - 2] == '!'))))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004295 IObuff[len++] = ' ';
4296 }
Bram Moolenaar2660c0e2010-01-19 14:59:56 +01004297 /* copy as much as possible of the new word */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 if (tmp_ptr - ptr >= IOSIZE - len)
4299 tmp_ptr = ptr + IOSIZE - len - 1;
4300 STRNCPY(IObuff + len, ptr, tmp_ptr - ptr);
4301 len += (int)(tmp_ptr - ptr);
Bram Moolenaar572cb562005-08-05 21:35:02 +00004302 flags |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004303 }
4304 IObuff[len] = NUL;
4305 ptr = IObuff;
4306 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004307 if (len == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004308 continue;
4309 }
4310 }
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004311 if (ins_compl_add_infercase(ptr, len, p_ic,
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004312 ins_buf == curbuf ? NULL : ins_buf->b_sfname,
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004313 0, flags) != NOTDONE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004314 {
4315 found_new_match = OK;
4316 break;
4317 }
4318 }
4319 p_scs = save_p_scs;
4320 p_ws = save_p_ws;
4321 }
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004322
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004323 /* check if compl_curr_match has changed, (e.g. other type of
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004324 * expansion added something) */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004325 if (type != 0 && compl_curr_match != old_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004326 found_new_match = OK;
4327
4328 /* break the loop for specialized modes (use 'complete' just for the
4329 * generic ctrl_x_mode == 0) or when we've found a new match */
4330 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004331 || found_new_match != FAIL)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004332 {
4333 if (got_int)
4334 break;
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004335 /* Fill the popup menu as soon as possible. */
Bram Moolenaar5948a572006-10-03 13:49:29 +00004336 if (type != -1)
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004337 ins_compl_check_keys(0);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004338
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004339 if ((ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE)
4340 || compl_interrupted)
4341 break;
4342 compl_started = TRUE;
4343 }
4344 else
4345 {
4346 /* Mark a buffer scanned when it has been scanned completely */
4347 if (type == 0 || type == CTRL_X_PATH_PATTERNS)
4348 ins_buf->b_scanned = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004350 compl_started = FALSE;
4351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004352 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004353 compl_started = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004354
4355 if ((ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_WHOLE_LINE)
4356 && *e_cpt == NUL) /* Got to end of 'complete' */
4357 found_new_match = FAIL;
4358
4359 i = -1; /* total of matches, unknown */
4360 if (found_new_match == FAIL
4361 || (ctrl_x_mode != 0 && ctrl_x_mode != CTRL_X_WHOLE_LINE))
4362 i = ins_compl_make_cyclic();
4363
4364 /* If several matches were added (FORWARD) or the search failed and has
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004365 * just been made cyclic then we have to move compl_curr_match to the next
4366 * or previous entry (if any) -- Acevedo */
Bram Moolenaara94bc432006-03-10 21:42:59 +00004367 compl_curr_match = compl_direction == FORWARD ? old_match->cp_next
4368 : old_match->cp_prev;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004369 if (compl_curr_match == NULL)
4370 compl_curr_match = old_match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004371 return i;
4372}
4373
4374/* Delete the old text being completed. */
4375 static void
4376ins_compl_delete()
4377{
4378 int i;
4379
4380 /*
4381 * In insert mode: Delete the typed part.
4382 * In replace mode: Put the old characters back, if any.
4383 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004384 i = compl_col + (compl_cont_status & CONT_ADDING ? compl_length : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004385 backspace_until_column(i);
4386 changed_cline_bef_curs();
4387}
4388
4389/* Insert the new text being completed. */
4390 static void
4391ins_compl_insert()
4392{
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004393 ins_bytes(compl_shown_match->cp_str + ins_compl_len());
Bram Moolenaardf1bdc92006-02-23 21:32:16 +00004394 if (compl_shown_match->cp_flags & ORIGINAL_TEXT)
4395 compl_used_match = FALSE;
4396 else
4397 compl_used_match = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398}
4399
4400/*
4401 * Fill in the next completion in the current direction.
Bram Moolenaar572cb562005-08-05 21:35:02 +00004402 * If "allow_get_expansion" is TRUE, then we may call ins_compl_get_exp() to
4403 * get more completions. If it is FALSE, then we just do nothing when there
4404 * are no more completions in a given direction. The latter case is used when
4405 * we are still in the middle of finding completions, to allow browsing
4406 * through the ones found so far.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004407 * Return the total number of matches, or -1 if still unknown -- webb.
4408 *
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004409 * compl_curr_match is currently being used by ins_compl_get_exp(), so we use
4410 * compl_shown_match here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004411 *
4412 * Note that this function may be called recursively once only. First with
Bram Moolenaar572cb562005-08-05 21:35:02 +00004413 * "allow_get_expansion" TRUE, which calls ins_compl_get_exp(), which in turn
4414 * calls this function with "allow_get_expansion" FALSE.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004415 */
4416 static int
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004417ins_compl_next(allow_get_expansion, count, insert_match)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004418 int allow_get_expansion;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004419 int count; /* repeat completion this many times; should
4420 be at least 1 */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004421 int insert_match; /* Insert the newly selected match */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004422{
4423 int num_matches = -1;
4424 int i;
Bram Moolenaare3226be2005-12-18 22:10:00 +00004425 int todo = count;
Bram Moolenaara6557602006-02-04 22:43:20 +00004426 compl_T *found_compl = NULL;
4427 int found_end = FALSE;
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004428 int advance;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004429
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004430 if (compl_leader != NULL
4431 && (compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004432 {
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004433 /* Set "compl_shown_match" to the actually shown match, it may differ
4434 * when "compl_leader" is used to omit some of the matches. */
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004435 while (!ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004436 compl_leader, (int)STRLEN(compl_leader))
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004437 && compl_shown_match->cp_next != NULL
4438 && compl_shown_match->cp_next != compl_first_match)
4439 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaar0440ca32006-05-13 13:24:33 +00004440
4441 /* If we didn't find it searching forward, and compl_shows_dir is
4442 * backward, find the last match. */
4443 if (compl_shows_dir == BACKWARD
4444 && !ins_compl_equal(compl_shown_match,
4445 compl_leader, (int)STRLEN(compl_leader))
4446 && (compl_shown_match->cp_next == NULL
4447 || compl_shown_match->cp_next == compl_first_match))
4448 {
4449 while (!ins_compl_equal(compl_shown_match,
4450 compl_leader, (int)STRLEN(compl_leader))
4451 && compl_shown_match->cp_prev != NULL
4452 && compl_shown_match->cp_prev != compl_first_match)
4453 compl_shown_match = compl_shown_match->cp_prev;
4454 }
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004455 }
4456
4457 if (allow_get_expansion && insert_match
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004458 && (!(compl_get_longest || compl_restarting) || compl_used_match))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004459 /* Delete old text to be replaced */
4460 ins_compl_delete();
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004461
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004462 /* When finding the longest common text we stick at the original text,
4463 * don't let CTRL-N or CTRL-P move to the first match. */
4464 advance = count != 1 || !allow_get_expansion || !compl_get_longest;
4465
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004466 /* When restarting the search don't insert the first match either. */
4467 if (compl_restarting)
4468 {
4469 advance = FALSE;
4470 compl_restarting = FALSE;
4471 }
4472
Bram Moolenaare3226be2005-12-18 22:10:00 +00004473 /* Repeat this for when <PageUp> or <PageDown> is typed. But don't wrap
4474 * around. */
4475 while (--todo >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004476 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004477 if (compl_shows_dir == FORWARD && compl_shown_match->cp_next != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004478 {
Bram Moolenaare3226be2005-12-18 22:10:00 +00004479 compl_shown_match = compl_shown_match->cp_next;
Bram Moolenaara6557602006-02-04 22:43:20 +00004480 found_end = (compl_first_match != NULL
4481 && (compl_shown_match->cp_next == compl_first_match
4482 || compl_shown_match == compl_first_match));
Bram Moolenaare3226be2005-12-18 22:10:00 +00004483 }
4484 else if (compl_shows_dir == BACKWARD
4485 && compl_shown_match->cp_prev != NULL)
4486 {
Bram Moolenaara6557602006-02-04 22:43:20 +00004487 found_end = (compl_shown_match == compl_first_match);
Bram Moolenaare3226be2005-12-18 22:10:00 +00004488 compl_shown_match = compl_shown_match->cp_prev;
Bram Moolenaara6557602006-02-04 22:43:20 +00004489 found_end |= (compl_shown_match == compl_first_match);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 }
4491 else
Bram Moolenaare3226be2005-12-18 22:10:00 +00004492 {
Bram Moolenaara260a972006-06-23 19:36:29 +00004493 if (!allow_get_expansion)
4494 {
4495 if (advance)
4496 {
4497 if (compl_shows_dir == BACKWARD)
4498 compl_pending -= todo + 1;
4499 else
4500 compl_pending += todo + 1;
4501 }
4502 return -1;
4503 }
4504
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004505 if (advance)
4506 {
4507 if (compl_shows_dir == BACKWARD)
4508 --compl_pending;
4509 else
4510 ++compl_pending;
4511 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004512
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00004513 /* Find matches. */
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00004514 num_matches = ins_compl_get_exp(&compl_startpos);
Bram Moolenaara260a972006-06-23 19:36:29 +00004515
4516 /* handle any pending completions */
4517 while (compl_pending != 0 && compl_direction == compl_shows_dir
Bram Moolenaarc1e37902006-04-18 21:55:01 +00004518 && advance)
Bram Moolenaara260a972006-06-23 19:36:29 +00004519 {
4520 if (compl_pending > 0 && compl_shown_match->cp_next != NULL)
4521 {
4522 compl_shown_match = compl_shown_match->cp_next;
4523 --compl_pending;
4524 }
4525 if (compl_pending < 0 && compl_shown_match->cp_prev != NULL)
4526 {
4527 compl_shown_match = compl_shown_match->cp_prev;
4528 ++compl_pending;
4529 }
4530 else
4531 break;
4532 }
Bram Moolenaara6557602006-02-04 22:43:20 +00004533 found_end = FALSE;
4534 }
4535 if ((compl_shown_match->cp_flags & ORIGINAL_TEXT) == 0
4536 && compl_leader != NULL
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004537 && !ins_compl_equal(compl_shown_match,
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004538 compl_leader, (int)STRLEN(compl_leader)))
Bram Moolenaara6557602006-02-04 22:43:20 +00004539 ++todo;
4540 else
4541 /* Remember a matching item. */
4542 found_compl = compl_shown_match;
4543
4544 /* Stop at the end of the list when we found a usable match. */
4545 if (found_end)
4546 {
4547 if (found_compl != NULL)
4548 {
4549 compl_shown_match = found_compl;
4550 break;
4551 }
4552 todo = 1; /* use first usable match after wrapping around */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004554 }
4555
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004556 /* Insert the text of the new completion, or the compl_leader. */
4557 if (insert_match)
4558 {
4559 if (!compl_get_longest || compl_used_match)
4560 ins_compl_insert();
4561 else
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00004562 ins_bytes(compl_leader + ins_compl_len());
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004563 }
4564 else
4565 compl_used_match = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004566
4567 if (!allow_get_expansion)
4568 {
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004569 /* may undisplay the popup menu first */
4570 ins_compl_upd_pum();
4571
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004572 /* redraw to show the user what was inserted */
4573 update_screen(0);
4574
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004575 /* display the updated popup menu */
4576 ins_compl_show_pum();
Bram Moolenaar14716812006-05-04 21:54:08 +00004577#ifdef FEAT_GUI
4578 if (gui.in_use)
4579 {
4580 /* Show the cursor after the match, not after the redrawn text. */
4581 setcursor();
4582 out_flush();
4583 gui_update_cursor(FALSE, FALSE);
4584 }
4585#endif
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00004586
Bram Moolenaar071d4272004-06-13 20:20:40 +00004587 /* Delete old text to be replaced, since we're still searching and
4588 * don't want to match ourselves! */
4589 ins_compl_delete();
4590 }
4591
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004592 /* Enter will select a match when the match wasn't inserted and the popup
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00004593 * menu is visible. */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00004594 compl_enter_selects = !insert_match && compl_match_array != NULL;
4595
Bram Moolenaar071d4272004-06-13 20:20:40 +00004596 /*
4597 * Show the file name for the match (if any)
4598 * Truncate the file name to avoid a wait for return.
4599 */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004600 if (compl_shown_match->cp_fname != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004601 {
4602 STRCPY(IObuff, "match in file ");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004603 i = (vim_strsize(compl_shown_match->cp_fname) + 16) - sc_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004604 if (i <= 0)
4605 i = 0;
4606 else
4607 STRCAT(IObuff, "<");
Bram Moolenaar572cb562005-08-05 21:35:02 +00004608 STRCAT(IObuff, compl_shown_match->cp_fname + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004609 msg(IObuff);
4610 redraw_cmdline = FALSE; /* don't overwrite! */
4611 }
4612
4613 return num_matches;
4614}
4615
4616/*
4617 * Call this while finding completions, to check whether the user has hit a key
4618 * that should change the currently displayed completion, or exit completion
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004619 * mode. Also, when compl_pending is not zero, show a completion as soon as
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 * possible. -- webb
Bram Moolenaar572cb562005-08-05 21:35:02 +00004621 * "frequency" specifies out of how many calls we actually check.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 */
4623 void
Bram Moolenaar572cb562005-08-05 21:35:02 +00004624ins_compl_check_keys(frequency)
4625 int frequency;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004626{
4627 static int count = 0;
4628
4629 int c;
4630
4631 /* Don't check when reading keys from a script. That would break the test
4632 * scripts */
4633 if (using_script())
4634 return;
4635
4636 /* Only do this at regular intervals */
Bram Moolenaar572cb562005-08-05 21:35:02 +00004637 if (++count < frequency)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 return;
4639 count = 0;
4640
Bram Moolenaara260a972006-06-23 19:36:29 +00004641 /* Check for a typed key. Do use mappings, otherwise vim_is_ctrl_x_key()
4642 * can't do its work correctly. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 c = vpeekc_any();
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 if (c != NUL)
4645 {
4646 if (vim_is_ctrl_x_key(c) && c != Ctrl_X && c != Ctrl_R)
4647 {
4648 c = safe_vgetc(); /* Eat the character */
Bram Moolenaare3226be2005-12-18 22:10:00 +00004649 compl_shows_dir = ins_compl_key2dir(c);
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004650 (void)ins_compl_next(FALSE, ins_compl_key2count(c),
4651 c != K_UP && c != K_DOWN);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004653 else
4654 {
4655 /* Need to get the character to have KeyTyped set. We'll put it
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004656 * back with vungetc() below. But skip K_IGNORE. */
Bram Moolenaara260a972006-06-23 19:36:29 +00004657 c = safe_vgetc();
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004658 if (c != K_IGNORE)
4659 {
4660 /* Don't interrupt completion when the character wasn't typed,
4661 * e.g., when doing @q to replay keys. */
4662 if (c != Ctrl_R && KeyTyped)
4663 compl_interrupted = TRUE;
Bram Moolenaara260a972006-06-23 19:36:29 +00004664
Bram Moolenaard4e20a72008-01-22 16:50:03 +00004665 vungetc(c);
4666 }
Bram Moolenaara260a972006-06-23 19:36:29 +00004667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004668 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004669 if (compl_pending != 0 && !got_int)
Bram Moolenaara260a972006-06-23 19:36:29 +00004670 {
4671 int todo = compl_pending > 0 ? compl_pending : -compl_pending;
4672
4673 compl_pending = 0;
4674 (void)ins_compl_next(FALSE, todo, TRUE);
4675 }
Bram Moolenaare3226be2005-12-18 22:10:00 +00004676}
4677
4678/*
4679 * Decide the direction of Insert mode complete from the key typed.
4680 * Returns BACKWARD or FORWARD.
4681 */
4682 static int
4683ins_compl_key2dir(c)
4684 int c;
4685{
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004686 if (c == Ctrl_P || c == Ctrl_L
4687 || (pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP
4688 || c == K_S_UP || c == K_UP)))
Bram Moolenaare3226be2005-12-18 22:10:00 +00004689 return BACKWARD;
4690 return FORWARD;
4691}
4692
4693/*
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004694 * Return TRUE for keys that are used for completion only when the popup menu
4695 * is visible.
4696 */
4697 static int
4698ins_compl_pum_key(c)
4699 int c;
4700{
4701 return pum_visible() && (c == K_PAGEUP || c == K_KPAGEUP || c == K_S_UP
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004702 || c == K_PAGEDOWN || c == K_KPAGEDOWN || c == K_S_DOWN
4703 || c == K_UP || c == K_DOWN);
Bram Moolenaard12f5c12006-01-25 22:10:52 +00004704}
4705
4706/*
Bram Moolenaare3226be2005-12-18 22:10:00 +00004707 * Decide the number of completions to move forward.
4708 * Returns 1 for most keys, height of the popup menu for page-up/down keys.
4709 */
4710 static int
4711ins_compl_key2count(c)
4712 int c;
4713{
4714 int h;
4715
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004716 if (ins_compl_pum_key(c) && c != K_UP && c != K_DOWN)
Bram Moolenaare3226be2005-12-18 22:10:00 +00004717 {
4718 h = pum_get_height();
4719 if (h > 3)
4720 h -= 2; /* keep some context */
4721 return h;
4722 }
4723 return 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724}
4725
4726/*
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004727 * Return TRUE if completion with "c" should insert the match, FALSE if only
4728 * to change the currently selected completion.
4729 */
4730 static int
4731ins_compl_use_match(c)
4732 int c;
4733{
4734 switch (c)
4735 {
4736 case K_UP:
4737 case K_DOWN:
4738 case K_PAGEDOWN:
4739 case K_KPAGEDOWN:
4740 case K_S_DOWN:
4741 case K_PAGEUP:
4742 case K_KPAGEUP:
4743 case K_S_UP:
4744 return FALSE;
4745 }
4746 return TRUE;
4747}
4748
4749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004750 * Do Insert mode completion.
4751 * Called when character "c" was typed, which has a meaning for completion.
4752 * Returns OK if completion was done, FAIL if something failed (out of mem).
4753 */
4754 static int
4755ins_complete(c)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004756 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757{
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004758 char_u *line;
4759 int startcol = 0; /* column where searched text starts */
4760 colnr_T curs_col; /* cursor column */
4761 int n;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01004762 int save_w_wrow;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763
Bram Moolenaare3226be2005-12-18 22:10:00 +00004764 compl_direction = ins_compl_key2dir(c);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004765 if (!compl_started)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004766 {
4767 /* First time we hit ^N or ^P (in a row, I mean) */
4768
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 did_ai = FALSE;
4770#ifdef FEAT_SMARTINDENT
4771 did_si = FALSE;
4772 can_si = FALSE;
4773 can_si_back = FALSE;
4774#endif
4775 if (stop_arrow() == FAIL)
4776 return FAIL;
4777
4778 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004779 curs_col = curwin->w_cursor.col;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00004780 compl_pending = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004781
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004782 /* If this same ctrl_x_mode has been interrupted use the text from
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004783 * "compl_startpos" to the cursor as a pattern to add a new word
4784 * instead of expand the one before the cursor, in word-wise if
Bram Moolenaar711d5b52007-10-19 18:40:51 +00004785 * "compl_startpos" is not in the same line as the cursor then fix it
4786 * (the line has been split because it was longer than 'tw'). if SOL
4787 * is set then skip the previous pattern, a word at the beginning of
4788 * the line has been inserted, we'll look for that -- Acevedo. */
Bram Moolenaarc7453f52006-02-10 23:20:28 +00004789 if ((compl_cont_status & CONT_INTRPT) == CONT_INTRPT
4790 && compl_cont_mode == ctrl_x_mode)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 {
4792 /*
4793 * it is a continued search
4794 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004795 compl_cont_status &= ~CONT_INTRPT; /* remove INTRPT */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004796 if (ctrl_x_mode == 0 || ctrl_x_mode == CTRL_X_PATH_PATTERNS
4797 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4798 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004799 if (compl_startpos.lnum != curwin->w_cursor.lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004801 /* line (probably) wrapped, set compl_startpos to the
4802 * first non_blank in the line, if it is not a wordchar
4803 * include it to get a better pattern, but then we don't
4804 * want the "\\<" prefix, check it bellow */
4805 compl_col = (colnr_T)(skipwhite(line) - line);
4806 compl_startpos.col = compl_col;
4807 compl_startpos.lnum = curwin->w_cursor.lnum;
4808 compl_cont_status &= ~CONT_SOL; /* clear SOL if present */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 }
4810 else
4811 {
4812 /* S_IPOS was set when we inserted a word that was at the
4813 * beginning of the line, which means that we'll go to SOL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004814 * mode but first we need to redefine compl_startpos */
4815 if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004816 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004817 compl_cont_status |= CONT_SOL;
4818 compl_startpos.col = (colnr_T)(skipwhite(
4819 line + compl_length
4820 + compl_startpos.col) - line);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004821 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004822 compl_col = compl_startpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004823 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004824 compl_length = curwin->w_cursor.col - (int)compl_col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00004825 /* IObuff is used to add a "word from the next line" would we
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00004826 * have enough space? just being paranoid */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004827#define MIN_SPACE 75
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004828 if (compl_length > (IOSIZE - MIN_SPACE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004829 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004830 compl_cont_status &= ~CONT_SOL;
4831 compl_length = (IOSIZE - MIN_SPACE);
4832 compl_col = curwin->w_cursor.col - compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004833 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004834 compl_cont_status |= CONT_ADDING | CONT_N_ADDS;
4835 if (compl_length < 1)
4836 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004837 }
4838 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004839 compl_cont_status = CONT_ADDING | CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004840 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004841 compl_cont_status = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 }
4843 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004844 compl_cont_status &= CONT_LOCAL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004845
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004846 if (!(compl_cont_status & CONT_ADDING)) /* normal expansion */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004847 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004848 compl_cont_mode = ctrl_x_mode;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 if (ctrl_x_mode != 0) /* Remove LOCAL if ctrl_x_mode != 0 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004850 compl_cont_status = 0;
4851 compl_cont_status |= CONT_N_ADDS;
4852 compl_startpos = curwin->w_cursor;
4853 startcol = (int)curs_col;
4854 compl_col = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856
4857 /* Work out completion pattern and original text -- webb */
4858 if (ctrl_x_mode == 0 || (ctrl_x_mode & CTRL_X_WANT_IDENT))
4859 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004860 if ((compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 || ctrl_x_mode == CTRL_X_PATH_DEFINES)
4862 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004863 if (!(compl_cont_status & CONT_ADDING))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004864 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004865 while (--startcol >= 0 && vim_isIDc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004866 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004867 compl_col += ++startcol;
4868 compl_length = curs_col - startcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004869 }
4870 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004871 compl_pattern = str_foldcase(line + compl_col,
4872 compl_length, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004873 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004874 compl_pattern = vim_strnsave(line + compl_col,
4875 compl_length);
4876 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 return FAIL;
4878 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004879 else if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 {
4881 char_u *prefix = (char_u *)"\\<";
4882
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004883 /* we need up to 2 extra chars for the prefix */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004884 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004885 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004886 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004888 if (!vim_iswordp(line + compl_col)
4889 || (compl_col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00004890 && (
4891#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004892 vim_iswordp(mb_prevptr(line, line + compl_col))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004893#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004894 vim_iswordc(line[compl_col - 1])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004895#endif
4896 )))
4897 prefix = (char_u *)"";
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004898 STRCPY((char *)compl_pattern, prefix);
4899 (void)quote_meta(compl_pattern + STRLEN(prefix),
4900 line + compl_col, compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004902 else if (--startcol < 0 ||
Bram Moolenaar071d4272004-06-13 20:20:40 +00004903#ifdef FEAT_MBYTE
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004904 !vim_iswordp(mb_prevptr(line, line + startcol + 1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905#else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004906 !vim_iswordc(line[startcol])
Bram Moolenaar071d4272004-06-13 20:20:40 +00004907#endif
4908 )
4909 {
4910 /* Match any word of at least two chars */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004911 compl_pattern = vim_strsave((char_u *)"\\<\\k\\k");
4912 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004914 compl_col += curs_col;
4915 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 }
4917 else
4918 {
4919#ifdef FEAT_MBYTE
4920 /* Search the point of change class of multibyte character
4921 * or not a word single byte character backward. */
4922 if (has_mbyte)
4923 {
4924 int base_class;
4925 int head_off;
4926
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004927 startcol -= (*mb_head_off)(line, line + startcol);
4928 base_class = mb_get_class(line + startcol);
4929 while (--startcol >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004931 head_off = (*mb_head_off)(line, line + startcol);
4932 if (base_class != mb_get_class(line + startcol
4933 - head_off))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 break;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004935 startcol -= head_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 }
4938 else
4939#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004940 while (--startcol >= 0 && vim_iswordc(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004941 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004942 compl_col += ++startcol;
4943 compl_length = (int)curs_col - startcol;
4944 if (compl_length == 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 {
4946 /* Only match word with at least two chars -- webb
4947 * there's no need to call quote_meta,
4948 * alloc(7) is enough -- Acevedo
4949 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004950 compl_pattern = alloc(7);
4951 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004952 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004953 STRCPY((char *)compl_pattern, "\\<");
4954 (void)quote_meta(compl_pattern + 2, line + compl_col, 1);
4955 STRCAT((char *)compl_pattern, "\\k");
Bram Moolenaar071d4272004-06-13 20:20:40 +00004956 }
4957 else
4958 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004959 compl_pattern = alloc(quote_meta(NULL, line + compl_col,
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00004960 compl_length) + 2);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004961 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004962 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004963 STRCPY((char *)compl_pattern, "\\<");
4964 (void)quote_meta(compl_pattern + 2, line + compl_col,
4965 compl_length);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 }
4967 }
4968 }
4969 else if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
4970 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004971 compl_col = (colnr_T)(skipwhite(line) - line);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004972 compl_length = (int)curs_col - (int)compl_col;
4973 if (compl_length < 0) /* cursor in indent: empty pattern */
4974 compl_length = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975 if (p_ic)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004976 compl_pattern = str_foldcase(line + compl_col, compl_length,
4977 NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004979 compl_pattern = vim_strnsave(line + compl_col, compl_length);
4980 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004981 return FAIL;
4982 }
4983 else if (ctrl_x_mode == CTRL_X_FILES)
4984 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004985 while (--startcol >= 0 && vim_isfilec(line[startcol]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004986 ;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004987 compl_col += ++startcol;
4988 compl_length = (int)curs_col - startcol;
4989 compl_pattern = addstar(line + compl_col, compl_length,
4990 EXPAND_FILES);
4991 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 return FAIL;
4993 }
4994 else if (ctrl_x_mode == CTRL_X_CMDLINE)
4995 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004996 compl_pattern = vim_strnsave(line, curs_col);
4997 if (compl_pattern == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004998 return FAIL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004999 set_cmd_context(&compl_xp, compl_pattern,
5000 (int)STRLEN(compl_pattern), curs_col);
5001 if (compl_xp.xp_context == EXPAND_UNSUCCESSFUL
5002 || compl_xp.xp_context == EXPAND_NOTHING)
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005003 /* No completion possible, use an empty pattern to get a
5004 * "pattern not found" message. */
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005005 compl_col = curs_col;
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005006 else
Bram Moolenaarf83c5c02006-08-16 19:24:22 +00005007 compl_col = (int)(compl_xp.xp_pattern - compl_pattern);
5008 compl_length = curs_col - compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 }
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005010 else if (ctrl_x_mode == CTRL_X_FUNCTION || ctrl_x_mode == CTRL_X_OMNI)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005011 {
Bram Moolenaare344bea2005-09-01 20:46:49 +00005012#ifdef FEAT_COMPL_FUNC
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005013 /*
Bram Moolenaare344bea2005-09-01 20:46:49 +00005014 * Call user defined function 'completefunc' with "a:findstart"
5015 * set to 1 to obtain the length of text to use for completion.
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005016 */
Bram Moolenaare344bea2005-09-01 20:46:49 +00005017 char_u *args[2];
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005018 int col;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005019 char_u *funcname;
5020 pos_T pos;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005021 win_T *curwin_save;
5022 buf_T *curbuf_save;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005023
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005024 /* Call 'completefunc' or 'omnifunc' and get pattern length as a
Bram Moolenaare344bea2005-09-01 20:46:49 +00005025 * string */
5026 funcname = ctrl_x_mode == CTRL_X_FUNCTION
5027 ? curbuf->b_p_cfu : curbuf->b_p_ofu;
5028 if (*funcname == NUL)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005029 {
5030 EMSG2(_(e_notset), ctrl_x_mode == CTRL_X_FUNCTION
5031 ? "completefunc" : "omnifunc");
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005032 return FAIL;
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005033 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005034
5035 args[0] = (char_u *)"1";
Bram Moolenaare344bea2005-09-01 20:46:49 +00005036 args[1] = NULL;
5037 pos = curwin->w_cursor;
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005038 curwin_save = curwin;
5039 curbuf_save = curbuf;
Bram Moolenaare344bea2005-09-01 20:46:49 +00005040 col = call_func_retnr(funcname, 2, args, FALSE);
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005041 if (curwin_save != curwin || curbuf_save != curbuf)
5042 {
5043 EMSG(_(e_complwin));
5044 return FAIL;
5045 }
Bram Moolenaare344bea2005-09-01 20:46:49 +00005046 curwin->w_cursor = pos; /* restore the cursor position */
Bram Moolenaar37dd0182010-11-10 16:54:20 +01005047 check_cursor();
5048 if (!equalpos(curwin->w_cursor, pos))
5049 {
5050 EMSG(_(e_compldel));
5051 return FAIL;
5052 }
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005053
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005054 if (col < 0)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00005055 col = curs_col;
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005056 compl_col = col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005057 if (compl_col > curs_col)
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005058 compl_col = curs_col;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005059
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005060 /* Setup variables for completion. Need to obtain "line" again,
5061 * it may have become invalid. */
5062 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar5a8684e2005-07-30 22:43:24 +00005063 compl_length = curs_col - compl_col;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005064 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5065 if (compl_pattern == NULL)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00005066#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005067 return FAIL;
5068 }
Bram Moolenaar488c6512005-08-11 20:09:58 +00005069 else if (ctrl_x_mode == CTRL_X_SPELL)
5070 {
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00005071#ifdef FEAT_SPELL
Bram Moolenaar6e7c7f32005-08-24 22:16:11 +00005072 if (spell_bad_len > 0)
5073 compl_col = curs_col - spell_bad_len;
5074 else
5075 compl_col = spell_word_start(startcol);
5076 if (compl_col >= (colnr_T)startcol)
Bram Moolenaarbe46a1e2006-06-22 15:13:21 +00005077 {
5078 compl_length = 0;
5079 compl_col = curs_col;
5080 }
5081 else
5082 {
5083 spell_expand_check_cap(compl_col);
5084 compl_length = (int)curs_col - compl_col;
5085 }
Bram Moolenaare2f98b92006-03-29 21:18:24 +00005086 /* Need to obtain "line" again, it may have become invalid. */
5087 line = ml_get(curwin->w_cursor.lnum);
Bram Moolenaar488c6512005-08-11 20:09:58 +00005088 compl_pattern = vim_strnsave(line + compl_col, compl_length);
5089 if (compl_pattern == NULL)
5090#endif
5091 return FAIL;
5092 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005093 else
5094 {
5095 EMSG2(_(e_intern2), "ins_complete()");
5096 return FAIL;
5097 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005098
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005099 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005100 {
5101 edit_submode_pre = (char_u *)_(" Adding");
5102 if (ctrl_x_mode == CTRL_X_WHOLE_LINE)
5103 {
5104 /* Insert a new line, keep indentation but ignore 'comments' */
5105#ifdef FEAT_COMMENTS
5106 char_u *old = curbuf->b_p_com;
5107
5108 curbuf->b_p_com = (char_u *)"";
5109#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005110 compl_startpos.lnum = curwin->w_cursor.lnum;
5111 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005112 ins_eol('\r');
5113#ifdef FEAT_COMMENTS
5114 curbuf->b_p_com = old;
5115#endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005116 compl_length = 0;
5117 compl_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005118 }
5119 }
5120 else
5121 {
5122 edit_submode_pre = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005123 compl_startpos.col = compl_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005124 }
5125
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005126 if (compl_cont_status & CONT_LOCAL)
5127 edit_submode = (char_u *)_(ctrl_x_msgs[CTRL_X_LOCAL_MSG]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005128 else
5129 edit_submode = (char_u *)_(CTRL_X_MSG(ctrl_x_mode));
5130
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00005131 /* Always add completion for the original text. */
5132 vim_free(compl_orig_text);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 compl_orig_text = vim_strnsave(line + compl_col, compl_length);
5134 if (compl_orig_text == NULL || ins_compl_add(compl_orig_text,
Bram Moolenaare8c3a142006-08-29 14:30:35 +00005135 -1, p_ic, NULL, NULL, 0, ORIGINAL_TEXT, FALSE) != OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005136 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005137 vim_free(compl_pattern);
5138 compl_pattern = NULL;
5139 vim_free(compl_orig_text);
5140 compl_orig_text = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005141 return FAIL;
5142 }
5143
5144 /* showmode might reset the internal line pointers, so it must
5145 * be called before line = ml_get(), or when this address is no
5146 * longer needed. -- Acevedo.
5147 */
5148 edit_submode_extra = (char_u *)_("-- Searching...");
5149 edit_submode_highl = HLF_COUNT;
5150 showmode();
5151 edit_submode_extra = NULL;
5152 out_flush();
5153 }
5154
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005155 compl_shown_match = compl_curr_match;
5156 compl_shows_dir = compl_direction;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005157
5158 /*
Bram Moolenaarc7453f52006-02-10 23:20:28 +00005159 * Find next match (and following matches).
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 */
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005161 save_w_wrow = curwin->w_wrow;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00005162 n = ins_compl_next(TRUE, ins_compl_key2count(c), ins_compl_use_match(c));
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005164 /* may undisplay the popup menu */
5165 ins_compl_upd_pum();
5166
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005167 if (n > 1) /* all matches have been found */
5168 compl_matches = n;
5169 compl_curr_match = compl_shown_match;
5170 compl_direction = compl_shows_dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171
Bram Moolenaard68071d2006-05-02 22:08:30 +00005172 /* Eat the ESC that vgetc() returns after a CTRL-C to avoid leaving Insert
5173 * mode. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 if (got_int && !global_busy)
5175 {
5176 (void)vgetc();
5177 got_int = FALSE;
5178 }
5179
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005180 /* we found no match if the list has only the "compl_orig_text"-entry */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005181 if (compl_first_match == compl_first_match->cp_next)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005183 edit_submode_extra = (compl_cont_status & CONT_ADDING)
5184 && compl_length > 1
Bram Moolenaar071d4272004-06-13 20:20:40 +00005185 ? (char_u *)_(e_hitend) : (char_u *)_(e_patnotf);
5186 edit_submode_highl = HLF_E;
5187 /* remove N_ADDS flag, so next ^X<> won't try to go to ADDING mode,
5188 * because we couldn't expand anything at first place, but if we used
5189 * ^P, ^N, ^X^I or ^X^D we might want to add-expand a single-char-word
5190 * (such as M in M'exico) if not tried already. -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005191 if ( compl_length > 1
5192 || (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 || (ctrl_x_mode != 0
5194 && ctrl_x_mode != CTRL_X_PATH_PATTERNS
5195 && ctrl_x_mode != CTRL_X_PATH_DEFINES))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005196 compl_cont_status &= ~CONT_N_ADDS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005197 }
5198
Bram Moolenaar572cb562005-08-05 21:35:02 +00005199 if (compl_curr_match->cp_flags & CONT_S_IPOS)
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005200 compl_cont_status |= CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 else
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005202 compl_cont_status &= ~CONT_S_IPOS;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203
5204 if (edit_submode_extra == NULL)
5205 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005206 if (compl_curr_match->cp_flags & ORIGINAL_TEXT)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 {
5208 edit_submode_extra = (char_u *)_("Back at original");
5209 edit_submode_highl = HLF_W;
5210 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005211 else if (compl_cont_status & CONT_S_IPOS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 {
5213 edit_submode_extra = (char_u *)_("Word from other line");
5214 edit_submode_highl = HLF_COUNT;
5215 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005216 else if (compl_curr_match->cp_next == compl_curr_match->cp_prev)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 {
5218 edit_submode_extra = (char_u *)_("The only match");
5219 edit_submode_highl = HLF_COUNT;
5220 }
5221 else
5222 {
5223 /* Update completion sequence number when needed. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005224 if (compl_curr_match->cp_number == -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005226 int number = 0;
5227 compl_T *match;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005229 if (compl_direction == FORWARD)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005230 {
5231 /* search backwards for the first valid (!= -1) number.
5232 * This should normally succeed already at the first loop
5233 * cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005234 for (match = compl_curr_match->cp_prev; match != NULL
5235 && match != compl_first_match;
5236 match = match->cp_prev)
5237 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005238 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005239 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005240 break;
5241 }
5242 if (match != NULL)
5243 /* go up and assign all numbers which are not assigned
5244 * yet */
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005245 for (match = match->cp_next;
5246 match != NULL && match->cp_number == -1;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005247 match = match->cp_next)
5248 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005249 }
5250 else /* BACKWARD */
5251 {
5252 /* search forwards (upwards) for the first valid (!= -1)
5253 * number. This should normally succeed already at the
5254 * first loop cycle, so it's fast! */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005255 for (match = compl_curr_match->cp_next; match != NULL
5256 && match != compl_first_match;
5257 match = match->cp_next)
5258 if (match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005259 {
Bram Moolenaar572cb562005-08-05 21:35:02 +00005260 number = match->cp_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005261 break;
5262 }
5263 if (match != NULL)
5264 /* go down and assign all numbers which are not
5265 * assigned yet */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005266 for (match = match->cp_prev; match
5267 && match->cp_number == -1;
5268 match = match->cp_prev)
5269 match->cp_number = ++number;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 }
5271 }
5272
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005273 /* The match should always have a sequence number now, this is
5274 * just a safety check. */
Bram Moolenaar572cb562005-08-05 21:35:02 +00005275 if (compl_curr_match->cp_number != -1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 {
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005277 /* Space for 10 text chars. + 2x10-digit no.s = 31.
5278 * Translations may need more than twice that. */
5279 static char_u match_ref[81];
Bram Moolenaar071d4272004-06-13 20:20:40 +00005280
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005281 if (compl_matches > 0)
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005282 vim_snprintf((char *)match_ref, sizeof(match_ref),
5283 _("match %d of %d"),
Bram Moolenaar572cb562005-08-05 21:35:02 +00005284 compl_curr_match->cp_number, compl_matches);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005285 else
Bram Moolenaar0739a1e2007-02-04 01:37:39 +00005286 vim_snprintf((char *)match_ref, sizeof(match_ref),
5287 _("match %d"),
5288 compl_curr_match->cp_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005289 edit_submode_extra = match_ref;
5290 edit_submode_highl = HLF_R;
5291 if (dollar_vcol)
5292 curs_columns(FALSE);
5293 }
5294 }
5295 }
5296
5297 /* Show a message about what (completion) mode we're in. */
5298 showmode();
5299 if (edit_submode_extra != NULL)
5300 {
5301 if (!p_smd)
5302 msg_attr(edit_submode_extra,
5303 edit_submode_highl < HLF_COUNT
5304 ? hl_attr(edit_submode_highl) : 0);
5305 }
5306 else
5307 msg_clr_cmdline(); /* necessary for "noshowmode" */
5308
Bram Moolenaard68071d2006-05-02 22:08:30 +00005309 /* Show the popup menu, unless we got interrupted. */
5310 if (!compl_interrupted)
5311 {
5312 /* RedrawingDisabled may be set when invoked through complete(). */
5313 n = RedrawingDisabled;
5314 RedrawingDisabled = 0;
Bram Moolenaarbe678f82010-03-10 14:15:54 +01005315
5316 /* If the cursor moved we need to remove the pum first. */
5317 setcursor();
5318 if (save_w_wrow != curwin->w_wrow)
5319 ins_compl_del_pum();
5320
Bram Moolenaard68071d2006-05-02 22:08:30 +00005321 ins_compl_show_pum();
5322 setcursor();
5323 RedrawingDisabled = n;
5324 }
Bram Moolenaar1423b9d2006-05-07 15:16:06 +00005325 compl_was_interrupted = compl_interrupted;
Bram Moolenaard68071d2006-05-02 22:08:30 +00005326 compl_interrupted = FALSE;
Bram Moolenaar1c7715d2005-10-03 22:02:18 +00005327
Bram Moolenaar071d4272004-06-13 20:20:40 +00005328 return OK;
5329}
5330
5331/*
5332 * Looks in the first "len" chars. of "src" for search-metachars.
5333 * If dest is not NULL the chars. are copied there quoting (with
5334 * a backslash) the metachars, and dest would be NUL terminated.
5335 * Returns the length (needed) of dest
5336 */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005337 static unsigned
Bram Moolenaar071d4272004-06-13 20:20:40 +00005338quote_meta(dest, src, len)
5339 char_u *dest;
5340 char_u *src;
5341 int len;
5342{
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005343 unsigned m = (unsigned)len + 1; /* one extra for the NUL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005344
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00005345 for ( ; --len >= 0; src++)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005346 {
5347 switch (*src)
5348 {
5349 case '.':
5350 case '*':
5351 case '[':
5352 if (ctrl_x_mode == CTRL_X_DICTIONARY
5353 || ctrl_x_mode == CTRL_X_THESAURUS)
5354 break;
5355 case '~':
5356 if (!p_magic) /* quote these only if magic is set */
5357 break;
5358 case '\\':
5359 if (ctrl_x_mode == CTRL_X_DICTIONARY
5360 || ctrl_x_mode == CTRL_X_THESAURUS)
5361 break;
5362 case '^': /* currently it's not needed. */
5363 case '$':
5364 m++;
5365 if (dest != NULL)
5366 *dest++ = '\\';
5367 break;
5368 }
5369 if (dest != NULL)
5370 *dest++ = *src;
Bram Moolenaar572cb562005-08-05 21:35:02 +00005371# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00005372 /* Copy remaining bytes of a multibyte character. */
5373 if (has_mbyte)
5374 {
5375 int i, mb_len;
5376
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00005377 mb_len = (*mb_ptr2len)(src) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005378 if (mb_len > 0 && len >= mb_len)
5379 for (i = 0; i < mb_len; ++i)
5380 {
5381 --len;
5382 ++src;
5383 if (dest != NULL)
5384 *dest++ = *src;
5385 }
5386 }
Bram Moolenaar572cb562005-08-05 21:35:02 +00005387# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005388 }
5389 if (dest != NULL)
5390 *dest = NUL;
5391
5392 return m;
5393}
5394#endif /* FEAT_INS_EXPAND */
5395
5396/*
5397 * Next character is interpreted literally.
5398 * A one, two or three digit decimal number is interpreted as its byte value.
5399 * If one or two digits are entered, the next character is given to vungetc().
5400 * For Unicode a character > 255 may be returned.
5401 */
5402 int
5403get_literal()
5404{
5405 int cc;
5406 int nc;
5407 int i;
5408 int hex = FALSE;
5409 int octal = FALSE;
5410#ifdef FEAT_MBYTE
5411 int unicode = 0;
5412#endif
5413
5414 if (got_int)
5415 return Ctrl_C;
5416
5417#ifdef FEAT_GUI
5418 /*
5419 * In GUI there is no point inserting the internal code for a special key.
5420 * It is more useful to insert the string "<KEY>" instead. This would
5421 * probably be useful in a text window too, but it would not be
5422 * vi-compatible (maybe there should be an option for it?) -- webb
5423 */
5424 if (gui.in_use)
5425 ++allow_keys;
5426#endif
5427#ifdef USE_ON_FLY_SCROLL
5428 dont_scroll = TRUE; /* disallow scrolling here */
5429#endif
5430 ++no_mapping; /* don't map the next key hits */
5431 cc = 0;
5432 i = 0;
5433 for (;;)
5434 {
Bram Moolenaar61abfd12007-09-13 16:26:47 +00005435 nc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00005436#ifdef FEAT_CMDL_INFO
5437 if (!(State & CMDLINE)
5438# ifdef FEAT_MBYTE
5439 && MB_BYTE2LEN_CHECK(nc) == 1
5440# endif
5441 )
5442 add_to_showcmd(nc);
5443#endif
5444 if (nc == 'x' || nc == 'X')
5445 hex = TRUE;
5446 else if (nc == 'o' || nc == 'O')
5447 octal = TRUE;
5448#ifdef FEAT_MBYTE
5449 else if (nc == 'u' || nc == 'U')
5450 unicode = nc;
5451#endif
5452 else
5453 {
5454 if (hex
5455#ifdef FEAT_MBYTE
5456 || unicode != 0
5457#endif
5458 )
5459 {
5460 if (!vim_isxdigit(nc))
5461 break;
5462 cc = cc * 16 + hex2nr(nc);
5463 }
5464 else if (octal)
5465 {
5466 if (nc < '0' || nc > '7')
5467 break;
5468 cc = cc * 8 + nc - '0';
5469 }
5470 else
5471 {
5472 if (!VIM_ISDIGIT(nc))
5473 break;
5474 cc = cc * 10 + nc - '0';
5475 }
5476
5477 ++i;
5478 }
5479
5480 if (cc > 255
5481#ifdef FEAT_MBYTE
5482 && unicode == 0
5483#endif
5484 )
5485 cc = 255; /* limit range to 0-255 */
5486 nc = 0;
5487
5488 if (hex) /* hex: up to two chars */
5489 {
5490 if (i >= 2)
5491 break;
5492 }
5493#ifdef FEAT_MBYTE
5494 else if (unicode) /* Unicode: up to four or eight chars */
5495 {
5496 if ((unicode == 'u' && i >= 4) || (unicode == 'U' && i >= 8))
5497 break;
5498 }
5499#endif
5500 else if (i >= 3) /* decimal or octal: up to three chars */
5501 break;
5502 }
5503 if (i == 0) /* no number entered */
5504 {
5505 if (nc == K_ZERO) /* NUL is stored as NL */
5506 {
5507 cc = '\n';
5508 nc = 0;
5509 }
5510 else
5511 {
5512 cc = nc;
5513 nc = 0;
5514 }
5515 }
5516
5517 if (cc == 0) /* NUL is stored as NL */
5518 cc = '\n';
Bram Moolenaar217ad922005-03-20 22:37:15 +00005519#ifdef FEAT_MBYTE
5520 if (enc_dbcs && (cc & 0xff) == 0)
5521 cc = '?'; /* don't accept an illegal DBCS char, the NUL in the
5522 second byte will cause trouble! */
5523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005524
5525 --no_mapping;
5526#ifdef FEAT_GUI
5527 if (gui.in_use)
5528 --allow_keys;
5529#endif
5530 if (nc)
5531 vungetc(nc);
5532 got_int = FALSE; /* CTRL-C typed after CTRL-V is not an interrupt */
5533 return cc;
5534}
5535
5536/*
5537 * Insert character, taking care of special keys and mod_mask
5538 */
5539 static void
5540insert_special(c, allow_modmask, ctrlv)
5541 int c;
5542 int allow_modmask;
5543 int ctrlv; /* c was typed after CTRL-V */
5544{
5545 char_u *p;
5546 int len;
5547
5548 /*
5549 * Special function key, translate into "<Key>". Up to the last '>' is
5550 * inserted with ins_str(), so as not to replace characters in replace
5551 * mode.
5552 * Only use mod_mask for special keys, to avoid things like <S-Space>,
5553 * unless 'allow_modmask' is TRUE.
5554 */
5555#ifdef MACOS
5556 /* Command-key never produces a normal key */
5557 if (mod_mask & MOD_MASK_CMD)
5558 allow_modmask = TRUE;
5559#endif
5560 if (IS_SPECIAL(c) || (mod_mask && allow_modmask))
5561 {
5562 p = get_special_key_name(c, mod_mask);
5563 len = (int)STRLEN(p);
5564 c = p[len - 1];
5565 if (len > 2)
5566 {
5567 if (stop_arrow() == FAIL)
5568 return;
5569 p[len - 1] = NUL;
5570 ins_str(p);
Bram Moolenaarebefac62005-12-28 22:39:57 +00005571 AppendToRedobuffLit(p, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005572 ctrlv = FALSE;
5573 }
5574 }
5575 if (stop_arrow() == OK)
5576 insertchar(c, ctrlv ? INSCHAR_CTRLV : 0, -1);
5577}
5578
5579/*
5580 * Special characters in this context are those that need processing other
5581 * than the simple insertion that can be performed here. This includes ESC
5582 * which terminates the insert, and CR/NL which need special processing to
5583 * open up a new line. This routine tries to optimize insertions performed by
5584 * the "redo", "undo" or "put" commands, so it needs to know when it should
5585 * stop and defer processing to the "normal" mechanism.
5586 * '0' and '^' are special, because they can be followed by CTRL-D.
5587 */
5588#ifdef EBCDIC
5589# define ISSPECIAL(c) ((c) < ' ' || (c) == '0' || (c) == '^')
5590#else
5591# define ISSPECIAL(c) ((c) < ' ' || (c) >= DEL || (c) == '0' || (c) == '^')
5592#endif
5593
5594#ifdef FEAT_MBYTE
5595# define WHITECHAR(cc) (vim_iswhite(cc) && (!enc_utf8 || !utf_iscomposing(utf_ptr2char(ml_get_cursor() + 1))))
5596#else
5597# define WHITECHAR(cc) vim_iswhite(cc)
5598#endif
5599
5600 void
5601insertchar(c, flags, second_indent)
5602 int c; /* character to insert or NUL */
5603 int flags; /* INSCHAR_FORMAT, etc. */
5604 int second_indent; /* indent for second line if >= 0 */
5605{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005606 int textwidth;
5607#ifdef FEAT_COMMENTS
Bram Moolenaar071d4272004-06-13 20:20:40 +00005608 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005609#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005610 int fo_ins_blank;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005611
5612 textwidth = comp_textwidth(flags & INSCHAR_FORMAT);
5613 fo_ins_blank = has_format_option(FO_INS_BLANK);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005614
5615 /*
5616 * Try to break the line in two or more pieces when:
5617 * - Always do this if we have been called to do formatting only.
5618 * - Always do this when 'formatoptions' has the 'a' flag and the line
5619 * ends in white space.
5620 * - Otherwise:
5621 * - Don't do this if inserting a blank
5622 * - Don't do this if an existing character is being replaced, unless
5623 * we're in VREPLACE mode.
5624 * - Do this if the cursor is not on the line where insert started
5625 * or - 'formatoptions' doesn't have 'l' or the line was not too long
5626 * before the insert.
5627 * - 'formatoptions' doesn't have 'b' or a blank was inserted at or
5628 * before 'textwidth'
5629 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005630 if (textwidth > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00005631 && ((flags & INSCHAR_FORMAT)
5632 || (!vim_iswhite(c)
5633 && !((State & REPLACE_FLAG)
5634#ifdef FEAT_VREPLACE
5635 && !(State & VREPLACE_FLAG)
5636#endif
5637 && *ml_get_cursor() != NUL)
5638 && (curwin->w_cursor.lnum != Insstart.lnum
5639 || ((!has_format_option(FO_INS_LONG)
5640 || Insstart_textlen <= (colnr_T)textwidth)
5641 && (!fo_ins_blank
5642 || Insstart_blank_vcol <= (colnr_T)textwidth
5643 ))))))
5644 {
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005645 /* Format with 'formatexpr' when it's set. Use internal formatting
5646 * when 'formatexpr' isn't set or it returns non-zero. */
5647#if defined(FEAT_EVAL)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005648 int do_internal = TRUE;
5649
Bram Moolenaar81a82092008-03-12 16:27:00 +00005650 if (*curbuf->b_p_fex != NUL && (flags & INSCHAR_NO_FEX) == 0)
Bram Moolenaarf3442e72006-10-10 13:49:10 +00005651 {
5652 do_internal = (fex_format(curwin->w_cursor.lnum, 1L, c) != 0);
5653 /* It may be required to save for undo again, e.g. when setline()
5654 * was called. */
5655 ins_need_undo = TRUE;
5656 }
5657 if (do_internal)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005658#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00005659 internal_format(textwidth, second_indent, flags, c == NUL, c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005660 }
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005661
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662 if (c == NUL) /* only formatting was wanted */
5663 return;
5664
5665#ifdef FEAT_COMMENTS
5666 /* Check whether this character should end a comment. */
5667 if (did_ai && (int)c == end_comment_pending)
5668 {
5669 char_u *line;
5670 char_u lead_end[COM_MAX_LEN]; /* end-comment string */
5671 int middle_len, end_len;
5672 int i;
5673
5674 /*
5675 * Need to remove existing (middle) comment leader and insert end
5676 * comment leader. First, check what comment leader we can find.
5677 */
5678 i = get_leader_len(line = ml_get_curline(), &p, FALSE);
5679 if (i > 0 && vim_strchr(p, COM_MIDDLE) != NULL) /* Just checking */
5680 {
5681 /* Skip middle-comment string */
5682 while (*p && p[-1] != ':') /* find end of middle flags */
5683 ++p;
5684 middle_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5685 /* Don't count trailing white space for middle_len */
5686 while (middle_len > 0 && vim_iswhite(lead_end[middle_len - 1]))
5687 --middle_len;
5688
5689 /* Find the end-comment string */
5690 while (*p && p[-1] != ':') /* find end of end flags */
5691 ++p;
5692 end_len = copy_option_part(&p, lead_end, COM_MAX_LEN, ",");
5693
5694 /* Skip white space before the cursor */
5695 i = curwin->w_cursor.col;
5696 while (--i >= 0 && vim_iswhite(line[i]))
5697 ;
5698 i++;
5699
5700 /* Skip to before the middle leader */
5701 i -= middle_len;
5702
5703 /* Check some expected things before we go on */
5704 if (i >= 0 && lead_end[end_len - 1] == end_comment_pending)
5705 {
5706 /* Backspace over all the stuff we want to replace */
5707 backspace_until_column(i);
5708
5709 /*
5710 * Insert the end-comment string, except for the last
5711 * character, which will get inserted as normal later.
5712 */
5713 ins_bytes_len(lead_end, end_len - 1);
5714 }
5715 }
5716 }
5717 end_comment_pending = NUL;
5718#endif
5719
5720 did_ai = FALSE;
5721#ifdef FEAT_SMARTINDENT
5722 did_si = FALSE;
5723 can_si = FALSE;
5724 can_si_back = FALSE;
5725#endif
5726
5727 /*
5728 * If there's any pending input, grab up to INPUT_BUFLEN at once.
5729 * This speeds up normal text input considerably.
5730 * Don't do this when 'cindent' or 'indentexpr' is set, because we might
5731 * need to re-indent at a ':', or any other character (but not what
5732 * 'paste' is set)..
5733 */
5734#ifdef USE_ON_FLY_SCROLL
5735 dont_scroll = FALSE; /* allow scrolling here */
5736#endif
5737
5738 if ( !ISSPECIAL(c)
5739#ifdef FEAT_MBYTE
5740 && (!has_mbyte || (*mb_char2len)(c) == 1)
5741#endif
5742 && vpeekc() != NUL
5743 && !(State & REPLACE_FLAG)
5744#ifdef FEAT_CINDENT
5745 && !cindent_on()
5746#endif
5747#ifdef FEAT_RIGHTLEFT
5748 && !p_ri
5749#endif
5750 )
5751 {
5752#define INPUT_BUFLEN 100
5753 char_u buf[INPUT_BUFLEN + 1];
5754 int i;
5755 colnr_T virtcol = 0;
5756
5757 buf[0] = c;
5758 i = 1;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005759 if (textwidth > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 virtcol = get_nolist_virtcol();
5761 /*
5762 * Stop the string when:
5763 * - no more chars available
5764 * - finding a special character (command key)
5765 * - buffer is full
5766 * - running into the 'textwidth' boundary
5767 * - need to check for abbreviation: A non-word char after a word-char
5768 */
5769 while ( (c = vpeekc()) != NUL
5770 && !ISSPECIAL(c)
5771#ifdef FEAT_MBYTE
5772 && (!has_mbyte || MB_BYTE2LEN_CHECK(c) == 1)
5773#endif
5774 && i < INPUT_BUFLEN
5775 && (textwidth == 0
5776 || (virtcol += byte2cells(buf[i - 1])) < (colnr_T)textwidth)
5777 && !(!no_abbr && !vim_iswordc(c) && vim_iswordc(buf[i - 1])))
5778 {
5779#ifdef FEAT_RIGHTLEFT
5780 c = vgetc();
5781 if (p_hkmap && KeyTyped)
5782 c = hkmap(c); /* Hebrew mode mapping */
5783# ifdef FEAT_FKMAP
5784 if (p_fkmap && KeyTyped)
5785 c = fkmap(c); /* Farsi mode mapping */
5786# endif
5787 buf[i++] = c;
5788#else
5789 buf[i++] = vgetc();
5790#endif
5791 }
5792
5793#ifdef FEAT_DIGRAPHS
5794 do_digraph(-1); /* clear digraphs */
5795 do_digraph(buf[i-1]); /* may be the start of a digraph */
5796#endif
5797 buf[i] = NUL;
5798 ins_str(buf);
5799 if (flags & INSCHAR_CTRLV)
5800 {
5801 redo_literal(*buf);
5802 i = 1;
5803 }
5804 else
5805 i = 0;
5806 if (buf[i] != NUL)
Bram Moolenaarebefac62005-12-28 22:39:57 +00005807 AppendToRedobuffLit(buf + i, -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005808 }
5809 else
5810 {
5811#ifdef FEAT_MBYTE
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005812 int cc;
5813
Bram Moolenaar071d4272004-06-13 20:20:40 +00005814 if (has_mbyte && (cc = (*mb_char2len)(c)) > 1)
5815 {
5816 char_u buf[MB_MAXBYTES + 1];
5817
5818 (*mb_char2bytes)(c, buf);
5819 buf[cc] = NUL;
5820 ins_char_bytes(buf, cc);
5821 AppendCharToRedobuff(c);
5822 }
5823 else
5824#endif
5825 {
5826 ins_char(c);
5827 if (flags & INSCHAR_CTRLV)
5828 redo_literal(c);
5829 else
5830 AppendCharToRedobuff(c);
5831 }
5832 }
5833}
5834
5835/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005836 * Format text at the current insert position.
5837 */
5838 static void
Bram Moolenaar97b98102009-11-17 16:41:01 +00005839internal_format(textwidth, second_indent, flags, format_only, c)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005840 int textwidth;
5841 int second_indent;
5842 int flags;
5843 int format_only;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005844 int c; /* character to be inserted (can be NUL) */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005845{
5846 int cc;
5847 int save_char = NUL;
5848 int haveto_redraw = FALSE;
5849 int fo_ins_blank = has_format_option(FO_INS_BLANK);
5850#ifdef FEAT_MBYTE
5851 int fo_multibyte = has_format_option(FO_MBYTE_BREAK);
5852#endif
5853 int fo_white_par = has_format_option(FO_WHITE_PAR);
5854 int first_line = TRUE;
5855#ifdef FEAT_COMMENTS
5856 colnr_T leader_len;
5857 int no_leader = FALSE;
5858 int do_comments = (flags & INSCHAR_DO_COM);
5859#endif
5860
5861 /*
5862 * When 'ai' is off we don't want a space under the cursor to be
5863 * deleted. Replace it with an 'x' temporarily.
5864 */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005865 if (!curbuf->b_p_ai
5866#ifdef FEAT_VREPLACE
5867 && !(State & VREPLACE_FLAG)
5868#endif
5869 )
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005870 {
5871 cc = gchar_cursor();
5872 if (vim_iswhite(cc))
5873 {
5874 save_char = cc;
5875 pchar_cursor('x');
5876 }
5877 }
5878
5879 /*
5880 * Repeat breaking lines, until the current line is not too long.
5881 */
5882 while (!got_int)
5883 {
5884 int startcol; /* Cursor column at entry */
5885 int wantcol; /* column at textwidth border */
5886 int foundcol; /* column for start of spaces */
5887 int end_foundcol = 0; /* column for start of word */
5888 colnr_T len;
5889 colnr_T virtcol;
5890#ifdef FEAT_VREPLACE
5891 int orig_col = 0;
5892 char_u *saved_text = NULL;
5893#endif
5894 colnr_T col;
Bram Moolenaar97b98102009-11-17 16:41:01 +00005895 colnr_T end_col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005896
Bram Moolenaar97b98102009-11-17 16:41:01 +00005897 virtcol = get_nolist_virtcol()
5898 + char2cells(c != NUL ? c : gchar_cursor());
5899 if (virtcol <= (colnr_T)textwidth)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005900 break;
5901
5902#ifdef FEAT_COMMENTS
5903 if (no_leader)
5904 do_comments = FALSE;
5905 else if (!(flags & INSCHAR_FORMAT)
5906 && has_format_option(FO_WRAP_COMS))
5907 do_comments = TRUE;
5908
5909 /* Don't break until after the comment leader */
5910 if (do_comments)
5911 leader_len = get_leader_len(ml_get_curline(), NULL, FALSE);
5912 else
5913 leader_len = 0;
5914
5915 /* If the line doesn't start with a comment leader, then don't
5916 * start one in a following broken line. Avoids that a %word
5917 * moved to the start of the next line causes all following lines
5918 * to start with %. */
5919 if (leader_len == 0)
5920 no_leader = TRUE;
5921#endif
5922 if (!(flags & INSCHAR_FORMAT)
5923#ifdef FEAT_COMMENTS
5924 && leader_len == 0
5925#endif
5926 && !has_format_option(FO_WRAP))
5927
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005928 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005929 if ((startcol = curwin->w_cursor.col) == 0)
5930 break;
5931
5932 /* find column of textwidth border */
5933 coladvance((colnr_T)textwidth);
5934 wantcol = curwin->w_cursor.col;
5935
Bram Moolenaar97b98102009-11-17 16:41:01 +00005936 curwin->w_cursor.col = startcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005937 foundcol = 0;
5938
5939 /*
5940 * Find position to break at.
5941 * Stop at first entered white when 'formatoptions' has 'v'
5942 */
5943 while ((!fo_ins_blank && !has_format_option(FO_INS_VI))
5944 || curwin->w_cursor.lnum != Insstart.lnum
5945 || curwin->w_cursor.col >= Insstart.col)
5946 {
Bram Moolenaar97b98102009-11-17 16:41:01 +00005947 if (curwin->w_cursor.col == startcol && c != NUL)
5948 cc = c;
5949 else
5950 cc = gchar_cursor();
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005951 if (WHITECHAR(cc))
5952 {
5953 /* remember position of blank just before text */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005954 end_col = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005955
5956 /* find start of sequence of blanks */
5957 while (curwin->w_cursor.col > 0 && WHITECHAR(cc))
5958 {
5959 dec_cursor();
5960 cc = gchar_cursor();
5961 }
5962 if (curwin->w_cursor.col == 0 && WHITECHAR(cc))
5963 break; /* only spaces in front of text */
5964#ifdef FEAT_COMMENTS
5965 /* Don't break until after the comment leader */
5966 if (curwin->w_cursor.col < leader_len)
5967 break;
5968#endif
5969 if (has_format_option(FO_ONE_LETTER))
5970 {
5971 /* do not break after one-letter words */
5972 if (curwin->w_cursor.col == 0)
5973 break; /* one-letter word at begin */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005974#ifdef FEAT_COMMENTS
5975 /* do not break "#a b" when 'tw' is 2 */
5976 if (curwin->w_cursor.col <= leader_len)
5977 break;
5978#endif
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005979 col = curwin->w_cursor.col;
5980 dec_cursor();
5981 cc = gchar_cursor();
5982
5983 if (WHITECHAR(cc))
5984 continue; /* one-letter, continue */
5985 curwin->w_cursor.col = col;
5986 }
Bram Moolenaar97b98102009-11-17 16:41:01 +00005987
5988 inc_cursor();
5989
5990 end_foundcol = end_col + 1;
5991 foundcol = curwin->w_cursor.col;
5992 if (curwin->w_cursor.col <= (colnr_T)wantcol)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005993 break;
5994 }
5995#ifdef FEAT_MBYTE
Bram Moolenaar97b98102009-11-17 16:41:01 +00005996 else if (cc >= 0x100 && fo_multibyte)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00005997 {
5998 /* Break after or before a multi-byte character. */
Bram Moolenaar97b98102009-11-17 16:41:01 +00005999 if (curwin->w_cursor.col != startcol)
6000 {
6001#ifdef FEAT_COMMENTS
6002 /* Don't break until after the comment leader */
6003 if (curwin->w_cursor.col < leader_len)
6004 break;
6005#endif
6006 col = curwin->w_cursor.col;
6007 inc_cursor();
6008 /* Don't change end_foundcol if already set. */
6009 if (foundcol != curwin->w_cursor.col)
6010 {
6011 foundcol = curwin->w_cursor.col;
6012 end_foundcol = foundcol;
6013 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6014 break;
6015 }
6016 curwin->w_cursor.col = col;
6017 }
6018
6019 if (curwin->w_cursor.col == 0)
6020 break;
6021
6022 col = curwin->w_cursor.col;
6023
6024 dec_cursor();
6025 cc = gchar_cursor();
6026
6027 if (WHITECHAR(cc))
6028 continue; /* break with space */
6029#ifdef FEAT_COMMENTS
6030 /* Don't break until after the comment leader */
6031 if (curwin->w_cursor.col < leader_len)
6032 break;
6033#endif
6034
6035 curwin->w_cursor.col = col;
6036
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006037 foundcol = curwin->w_cursor.col;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006038 end_foundcol = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006039 if (curwin->w_cursor.col <= (colnr_T)wantcol)
6040 break;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006041 }
6042#endif
6043 if (curwin->w_cursor.col == 0)
6044 break;
6045 dec_cursor();
6046 }
6047
6048 if (foundcol == 0) /* no spaces, cannot break line */
6049 {
6050 curwin->w_cursor.col = startcol;
6051 break;
6052 }
6053
6054 /* Going to break the line, remove any "$" now. */
6055 undisplay_dollar();
6056
6057 /*
6058 * Offset between cursor position and line break is used by replace
6059 * stack functions. VREPLACE does not use this, and backspaces
6060 * over the text instead.
6061 */
6062#ifdef FEAT_VREPLACE
6063 if (State & VREPLACE_FLAG)
6064 orig_col = startcol; /* Will start backspacing from here */
6065 else
6066#endif
Bram Moolenaar97b98102009-11-17 16:41:01 +00006067 replace_offset = startcol - end_foundcol;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006068
6069 /*
6070 * adjust startcol for spaces that will be deleted and
6071 * characters that will remain on top line
6072 */
6073 curwin->w_cursor.col = foundcol;
Bram Moolenaar97b98102009-11-17 16:41:01 +00006074 while ((cc = gchar_cursor(), WHITECHAR(cc))
6075 && (!fo_white_par || curwin->w_cursor.col < startcol))
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006076 inc_cursor();
6077 startcol -= curwin->w_cursor.col;
6078 if (startcol < 0)
6079 startcol = 0;
6080
6081#ifdef FEAT_VREPLACE
6082 if (State & VREPLACE_FLAG)
6083 {
6084 /*
6085 * In VREPLACE mode, we will backspace over the text to be
6086 * wrapped, so save a copy now to put on the next line.
6087 */
6088 saved_text = vim_strsave(ml_get_cursor());
6089 curwin->w_cursor.col = orig_col;
6090 if (saved_text == NULL)
6091 break; /* Can't do it, out of memory */
6092 saved_text[startcol] = NUL;
6093
6094 /* Backspace over characters that will move to the next line */
6095 if (!fo_white_par)
6096 backspace_until_column(foundcol);
6097 }
6098 else
6099#endif
6100 {
6101 /* put cursor after pos. to break line */
6102 if (!fo_white_par)
6103 curwin->w_cursor.col = foundcol;
6104 }
6105
6106 /*
6107 * Split the line just before the margin.
6108 * Only insert/delete lines, but don't really redraw the window.
6109 */
6110 open_line(FORWARD, OPENLINE_DELSPACES + OPENLINE_MARKFIX
6111 + (fo_white_par ? OPENLINE_KEEPTRAIL : 0)
6112#ifdef FEAT_COMMENTS
6113 + (do_comments ? OPENLINE_DO_COM : 0)
6114#endif
6115 , old_indent);
6116 old_indent = 0;
6117
6118 replace_offset = 0;
6119 if (first_line)
6120 {
6121 if (second_indent < 0 && has_format_option(FO_Q_NUMBER))
6122 second_indent = get_number_indent(curwin->w_cursor.lnum -1);
6123 if (second_indent >= 0)
6124 {
6125#ifdef FEAT_VREPLACE
6126 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00006127 change_indent(INDENT_SET, second_indent, FALSE, NUL, TRUE);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00006128 else
6129#endif
6130 (void)set_indent(second_indent, SIN_CHANGED);
6131 }
6132 first_line = FALSE;
6133 }
6134
6135#ifdef FEAT_VREPLACE
6136 if (State & VREPLACE_FLAG)
6137 {
6138 /*
6139 * In VREPLACE mode we have backspaced over the text to be
6140 * moved, now we re-insert it into the new line.
6141 */
6142 ins_bytes(saved_text);
6143 vim_free(saved_text);
6144 }
6145 else
6146#endif
6147 {
6148 /*
6149 * Check if cursor is not past the NUL off the line, cindent
6150 * may have added or removed indent.
6151 */
6152 curwin->w_cursor.col += startcol;
6153 len = (colnr_T)STRLEN(ml_get_curline());
6154 if (curwin->w_cursor.col > len)
6155 curwin->w_cursor.col = len;
6156 }
6157
6158 haveto_redraw = TRUE;
6159#ifdef FEAT_CINDENT
6160 can_cindent = TRUE;
6161#endif
6162 /* moved the cursor, don't autoindent or cindent now */
6163 did_ai = FALSE;
6164#ifdef FEAT_SMARTINDENT
6165 did_si = FALSE;
6166 can_si = FALSE;
6167 can_si_back = FALSE;
6168#endif
6169 line_breakcheck();
6170 }
6171
6172 if (save_char != NUL) /* put back space after cursor */
6173 pchar_cursor(save_char);
6174
6175 if (!format_only && haveto_redraw)
6176 {
6177 update_topline();
6178 redraw_curbuf_later(VALID);
6179 }
6180}
6181
6182/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00006183 * Called after inserting or deleting text: When 'formatoptions' includes the
6184 * 'a' flag format from the current line until the end of the paragraph.
6185 * Keep the cursor at the same position relative to the text.
6186 * The caller must have saved the cursor line for undo, following ones will be
6187 * saved here.
6188 */
6189 void
6190auto_format(trailblank, prev_line)
6191 int trailblank; /* when TRUE also format with trailing blank */
6192 int prev_line; /* may start in previous line */
6193{
6194 pos_T pos;
6195 colnr_T len;
6196 char_u *old;
6197 char_u *new, *pnew;
6198 int wasatend;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006199 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006200
6201 if (!has_format_option(FO_AUTO))
6202 return;
6203
6204 pos = curwin->w_cursor;
6205 old = ml_get_curline();
6206
6207 /* may remove added space */
6208 check_auto_format(FALSE);
6209
6210 /* Don't format in Insert mode when the cursor is on a trailing blank, the
6211 * user might insert normal text next. Also skip formatting when "1" is
6212 * in 'formatoptions' and there is a single character before the cursor.
6213 * Otherwise the line would be broken and when typing another non-white
6214 * next they are not joined back together. */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006215 wasatend = (pos.col == (colnr_T)STRLEN(old));
Bram Moolenaar071d4272004-06-13 20:20:40 +00006216 if (*old != NUL && !trailblank && wasatend)
6217 {
6218 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006219 cc = gchar_cursor();
6220 if (!WHITECHAR(cc) && curwin->w_cursor.col > 0
6221 && has_format_option(FO_ONE_LETTER))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006222 dec_cursor();
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006223 cc = gchar_cursor();
6224 if (WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006225 {
6226 curwin->w_cursor = pos;
6227 return;
6228 }
6229 curwin->w_cursor = pos;
6230 }
6231
6232#ifdef FEAT_COMMENTS
6233 /* With the 'c' flag in 'formatoptions' and 't' missing: only format
6234 * comments. */
6235 if (has_format_option(FO_WRAP_COMS) && !has_format_option(FO_WRAP)
6236 && get_leader_len(old, NULL, FALSE) == 0)
6237 return;
6238#endif
6239
6240 /*
6241 * May start formatting in a previous line, so that after "x" a word is
6242 * moved to the previous line if it fits there now. Only when this is not
6243 * the start of a paragraph.
6244 */
6245 if (prev_line && !paragraph_start(curwin->w_cursor.lnum))
6246 {
6247 --curwin->w_cursor.lnum;
6248 if (u_save_cursor() == FAIL)
6249 return;
6250 }
6251
6252 /*
6253 * Do the formatting and restore the cursor position. "saved_cursor" will
6254 * be adjusted for the text formatting.
6255 */
6256 saved_cursor = pos;
Bram Moolenaar81a82092008-03-12 16:27:00 +00006257 format_lines((linenr_T)-1, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006258 curwin->w_cursor = saved_cursor;
6259 saved_cursor.lnum = 0;
6260
6261 if (curwin->w_cursor.lnum > curbuf->b_ml.ml_line_count)
6262 {
6263 /* "cannot happen" */
6264 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
6265 coladvance((colnr_T)MAXCOL);
6266 }
6267 else
6268 check_cursor_col();
6269
6270 /* Insert mode: If the cursor is now after the end of the line while it
6271 * previously wasn't, the line was broken. Because of the rule above we
6272 * need to add a space when 'w' is in 'formatoptions' to keep a paragraph
6273 * formatted. */
6274 if (!wasatend && has_format_option(FO_WHITE_PAR))
6275 {
6276 new = ml_get_curline();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006277 len = (colnr_T)STRLEN(new);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006278 if (curwin->w_cursor.col == len)
6279 {
6280 pnew = vim_strnsave(new, len + 2);
6281 pnew[len] = ' ';
6282 pnew[len + 1] = NUL;
6283 ml_replace(curwin->w_cursor.lnum, pnew, FALSE);
6284 /* remove the space later */
6285 did_add_space = TRUE;
6286 }
6287 else
6288 /* may remove added space */
6289 check_auto_format(FALSE);
6290 }
6291
6292 check_cursor();
6293}
6294
6295/*
6296 * When an extra space was added to continue a paragraph for auto-formatting,
6297 * delete it now. The space must be under the cursor, just after the insert
6298 * position.
6299 */
6300 static void
6301check_auto_format(end_insert)
6302 int end_insert; /* TRUE when ending Insert mode */
6303{
6304 int c = ' ';
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006305 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006306
6307 if (did_add_space)
6308 {
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006309 cc = gchar_cursor();
6310 if (!WHITECHAR(cc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006311 /* Somehow the space was removed already. */
6312 did_add_space = FALSE;
6313 else
6314 {
6315 if (!end_insert)
6316 {
6317 inc_cursor();
6318 c = gchar_cursor();
6319 dec_cursor();
6320 }
6321 if (c != NUL)
6322 {
6323 /* The space is no longer at the end of the line, delete it. */
6324 del_char(FALSE);
6325 did_add_space = FALSE;
6326 }
6327 }
6328 }
6329}
6330
6331/*
6332 * Find out textwidth to be used for formatting:
6333 * if 'textwidth' option is set, use it
6334 * else if 'wrapmargin' option is set, use W_WIDTH(curwin) - 'wrapmargin'
6335 * if invalid value, use 0.
6336 * Set default to window width (maximum 79) for "gq" operator.
6337 */
6338 int
6339comp_textwidth(ff)
Bram Moolenaar91170f82006-05-05 21:15:17 +00006340 int ff; /* force formatting (for "gq" command) */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006341{
6342 int textwidth;
6343
6344 textwidth = curbuf->b_p_tw;
6345 if (textwidth == 0 && curbuf->b_p_wm)
6346 {
6347 /* The width is the window width minus 'wrapmargin' minus all the
6348 * things that add to the margin. */
6349 textwidth = W_WIDTH(curwin) - curbuf->b_p_wm;
6350#ifdef FEAT_CMDWIN
6351 if (cmdwin_type != 0)
6352 textwidth -= 1;
6353#endif
6354#ifdef FEAT_FOLDING
6355 textwidth -= curwin->w_p_fdc;
6356#endif
6357#ifdef FEAT_SIGNS
6358 if (curwin->w_buffer->b_signlist != NULL
6359# ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02006360 || netbeans_active()
Bram Moolenaar071d4272004-06-13 20:20:40 +00006361# endif
6362 )
6363 textwidth -= 1;
6364#endif
Bram Moolenaar64486672010-05-16 15:46:46 +02006365 if (curwin->w_p_nu || curwin->w_p_rnu)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006366 textwidth -= 8;
6367 }
6368 if (textwidth < 0)
6369 textwidth = 0;
6370 if (ff && textwidth == 0)
6371 {
6372 textwidth = W_WIDTH(curwin) - 1;
6373 if (textwidth > 79)
6374 textwidth = 79;
6375 }
6376 return textwidth;
6377}
6378
6379/*
6380 * Put a character in the redo buffer, for when just after a CTRL-V.
6381 */
6382 static void
6383redo_literal(c)
6384 int c;
6385{
6386 char_u buf[10];
6387
6388 /* Only digits need special treatment. Translate them into a string of
6389 * three digits. */
6390 if (VIM_ISDIGIT(c))
6391 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006392 vim_snprintf((char *)buf, sizeof(buf), "%03d", c);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006393 AppendToRedobuff(buf);
6394 }
6395 else
6396 AppendCharToRedobuff(c);
6397}
6398
6399/*
6400 * start_arrow() is called when an arrow key is used in insert mode.
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006401 * For undo/redo it resembles hitting the <ESC> key.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006402 */
6403 static void
6404start_arrow(end_insert_pos)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006405 pos_T *end_insert_pos; /* can be NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006406{
6407 if (!arrow_used) /* something has been inserted */
6408 {
6409 AppendToRedobuff(ESC_STR);
6410 stop_insert(end_insert_pos, FALSE);
6411 arrow_used = TRUE; /* this means we stopped the current insert */
6412 }
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006413#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006414 check_spell_redraw();
6415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006416}
6417
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00006418#ifdef FEAT_SPELL
Bram Moolenaar217ad922005-03-20 22:37:15 +00006419/*
6420 * If we skipped highlighting word at cursor, do it now.
6421 * It may be skipped again, thus reset spell_redraw_lnum first.
6422 */
6423 static void
6424check_spell_redraw()
6425{
6426 if (spell_redraw_lnum != 0)
6427 {
6428 linenr_T lnum = spell_redraw_lnum;
6429
6430 spell_redraw_lnum = 0;
6431 redrawWinline(lnum, FALSE);
6432 }
6433}
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006434
6435/*
6436 * Called when starting CTRL_X_SPELL mode: Move backwards to a previous badly
6437 * spelled word, if there is one.
6438 */
6439 static void
6440spell_back_to_badword()
6441{
6442 pos_T tpos = curwin->w_cursor;
6443
Bram Moolenaar81f1ecb2005-08-25 21:27:31 +00006444 spell_bad_len = spell_move_to(curwin, BACKWARD, TRUE, TRUE, NULL);
Bram Moolenaar8aff23a2005-08-19 20:40:30 +00006445 if (curwin->w_cursor.col != tpos.col)
6446 start_arrow(&tpos);
6447}
Bram Moolenaar217ad922005-03-20 22:37:15 +00006448#endif
6449
Bram Moolenaar071d4272004-06-13 20:20:40 +00006450/*
6451 * stop_arrow() is called before a change is made in insert mode.
6452 * If an arrow key has been used, start a new insertion.
6453 * Returns FAIL if undo is impossible, shouldn't insert then.
6454 */
6455 int
6456stop_arrow()
6457{
6458 if (arrow_used)
6459 {
6460 if (u_save_cursor() == OK)
6461 {
6462 arrow_used = FALSE;
6463 ins_need_undo = FALSE;
6464 }
6465 Insstart = curwin->w_cursor; /* new insertion starts here */
Bram Moolenaar0ab2a882009-05-13 10:51:08 +00006466 Insstart_textlen = (colnr_T)linetabsize(ml_get_curline());
Bram Moolenaar071d4272004-06-13 20:20:40 +00006467 ai_col = 0;
6468#ifdef FEAT_VREPLACE
6469 if (State & VREPLACE_FLAG)
6470 {
6471 orig_line_count = curbuf->b_ml.ml_line_count;
6472 vr_lines_changed = 1;
6473 }
6474#endif
6475 ResetRedobuff();
6476 AppendToRedobuff((char_u *)"1i"); /* pretend we start an insertion */
Bram Moolenaara9b1e742005-12-19 22:14:58 +00006477 new_insert_skip = 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006478 }
6479 else if (ins_need_undo)
6480 {
6481 if (u_save_cursor() == OK)
6482 ins_need_undo = FALSE;
6483 }
6484
6485#ifdef FEAT_FOLDING
6486 /* Always open fold at the cursor line when inserting something. */
6487 foldOpenCursor();
6488#endif
6489
6490 return (arrow_used || ins_need_undo ? FAIL : OK);
6491}
6492
6493/*
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006494 * Do a few things to stop inserting.
6495 * "end_insert_pos" is where insert ended. It is NULL when we already jumped
6496 * to another window/buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006497 */
6498 static void
6499stop_insert(end_insert_pos, esc)
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006500 pos_T *end_insert_pos;
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006501 int esc; /* called by ins_esc() */
Bram Moolenaar071d4272004-06-13 20:20:40 +00006502{
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006503 int cc;
6504 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006505
6506 stop_redo_ins();
6507 replace_flush(); /* abandon replace stack */
6508
6509 /*
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006510 * Save the inserted text for later redo with ^@ and CTRL-A.
6511 * Don't do it when "restart_edit" was set and nothing was inserted,
6512 * otherwise CTRL-O w and then <Left> will clear "last_insert".
Bram Moolenaar071d4272004-06-13 20:20:40 +00006513 */
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006514 ptr = get_inserted();
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +00006515 if (did_restart_edit == 0 || (ptr != NULL
6516 && (int)STRLEN(ptr) > new_insert_skip))
Bram Moolenaar83c465c2005-12-16 21:53:56 +00006517 {
6518 vim_free(last_insert);
6519 last_insert = ptr;
6520 last_insert_skip = new_insert_skip;
6521 }
6522 else
6523 vim_free(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006524
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006525 if (!arrow_used && end_insert_pos != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006526 {
6527 /* Auto-format now. It may seem strange to do this when stopping an
6528 * insertion (or moving the cursor), but it's required when appending
6529 * a line and having it end in a space. But only do it when something
6530 * was actually inserted, otherwise undo won't work. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006531 if (!ins_need_undo && has_format_option(FO_AUTO))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006532 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006533 pos_T tpos = curwin->w_cursor;
6534
Bram Moolenaar071d4272004-06-13 20:20:40 +00006535 /* When the cursor is at the end of the line after a space the
6536 * formatting will move it to the following word. Avoid that by
6537 * moving the cursor onto the space. */
6538 cc = 'x';
6539 if (curwin->w_cursor.col > 0 && gchar_cursor() == NUL)
6540 {
6541 dec_cursor();
6542 cc = gchar_cursor();
6543 if (!vim_iswhite(cc))
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006544 curwin->w_cursor = tpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006545 }
6546
6547 auto_format(TRUE, FALSE);
6548
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006549 if (vim_iswhite(cc))
6550 {
6551 if (gchar_cursor() != NUL)
6552 inc_cursor();
6553#ifdef FEAT_VIRTUALEDIT
6554 /* If the cursor is still at the same character, also keep
6555 * the "coladd". */
6556 if (gchar_cursor() == NUL
6557 && curwin->w_cursor.lnum == tpos.lnum
6558 && curwin->w_cursor.col == tpos.col)
6559 curwin->w_cursor.coladd = tpos.coladd;
6560#endif
6561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006562 }
6563
6564 /* If a space was inserted for auto-formatting, remove it now. */
6565 check_auto_format(TRUE);
6566
6567 /* If we just did an auto-indent, remove the white space from the end
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006568 * of the line, and put the cursor back.
Bram Moolenaar4be50682009-05-26 09:02:10 +00006569 * Do this when ESC was used or moving the cursor up/down.
6570 * Check for the old position still being valid, just in case the text
6571 * got changed unexpectedly. */
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006572 if (did_ai && (esc || (vim_strchr(p_cpo, CPO_INDENT) == NULL
Bram Moolenaar4be50682009-05-26 09:02:10 +00006573 && curwin->w_cursor.lnum != end_insert_pos->lnum))
6574 && end_insert_pos->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006575 {
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006576 pos_T tpos = curwin->w_cursor;
6577
6578 curwin->w_cursor = *end_insert_pos;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006579 check_cursor_col(); /* make sure it is not past the line */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006580 for (;;)
6581 {
6582 if (gchar_cursor() == NUL && curwin->w_cursor.col > 0)
6583 --curwin->w_cursor.col;
6584 cc = gchar_cursor();
6585 if (!vim_iswhite(cc))
6586 break;
Bram Moolenaar4be50682009-05-26 09:02:10 +00006587 if (del_char(TRUE) == FAIL)
6588 break; /* should not happen */
Bram Moolenaar39f05632006-03-19 22:15:26 +00006589 }
Bram Moolenaarf4b8e572004-06-24 15:53:16 +00006590 if (curwin->w_cursor.lnum != tpos.lnum)
6591 curwin->w_cursor = tpos;
6592 else if (cc != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006593 ++curwin->w_cursor.col; /* put cursor back on the NUL */
6594
6595#ifdef FEAT_VISUAL
6596 /* <C-S-Right> may have started Visual mode, adjust the position for
6597 * deleted characters. */
6598 if (VIsual_active && VIsual.lnum == curwin->w_cursor.lnum)
6599 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006600 int len = (int)STRLEN(ml_get_curline());
6601
6602 if (VIsual.col > len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00006603 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00006604 VIsual.col = len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006605# ifdef FEAT_VIRTUALEDIT
6606 VIsual.coladd = 0;
6607# endif
6608 }
6609 }
6610#endif
6611 }
6612 }
6613 did_ai = FALSE;
6614#ifdef FEAT_SMARTINDENT
6615 did_si = FALSE;
6616 can_si = FALSE;
6617 can_si_back = FALSE;
6618#endif
6619
Bram Moolenaareb3593b2006-04-22 22:33:57 +00006620 /* Set '[ and '] to the inserted text. When end_insert_pos is NULL we are
6621 * now in a different buffer. */
6622 if (end_insert_pos != NULL)
6623 {
6624 curbuf->b_op_start = Insstart;
6625 curbuf->b_op_end = *end_insert_pos;
6626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00006627}
6628
6629/*
6630 * Set the last inserted text to a single character.
6631 * Used for the replace command.
6632 */
6633 void
6634set_last_insert(c)
6635 int c;
6636{
6637 char_u *s;
6638
6639 vim_free(last_insert);
6640#ifdef FEAT_MBYTE
6641 last_insert = alloc(MB_MAXBYTES * 3 + 5);
6642#else
6643 last_insert = alloc(6);
6644#endif
6645 if (last_insert != NULL)
6646 {
6647 s = last_insert;
6648 /* Use the CTRL-V only when entering a special char */
6649 if (c < ' ' || c == DEL)
6650 *s++ = Ctrl_V;
6651 s = add_char2buf(c, s);
6652 *s++ = ESC;
6653 *s++ = NUL;
6654 last_insert_skip = 0;
6655 }
6656}
6657
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006658#if defined(EXITFREE) || defined(PROTO)
6659 void
6660free_last_insert()
6661{
6662 vim_free(last_insert);
6663 last_insert = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006664# ifdef FEAT_INS_EXPAND
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +00006665 vim_free(compl_orig_text);
6666 compl_orig_text = NULL;
Bram Moolenaare0ca7b22007-11-24 20:28:24 +00006667# endif
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00006668}
6669#endif
6670
Bram Moolenaar071d4272004-06-13 20:20:40 +00006671/*
6672 * Add character "c" to buffer "s". Escape the special meaning of K_SPECIAL
6673 * and CSI. Handle multi-byte characters.
6674 * Returns a pointer to after the added bytes.
6675 */
6676 char_u *
6677add_char2buf(c, s)
6678 int c;
6679 char_u *s;
6680{
6681#ifdef FEAT_MBYTE
6682 char_u temp[MB_MAXBYTES];
6683 int i;
6684 int len;
6685
6686 len = (*mb_char2bytes)(c, temp);
6687 for (i = 0; i < len; ++i)
6688 {
6689 c = temp[i];
6690#endif
6691 /* Need to escape K_SPECIAL and CSI like in the typeahead buffer. */
6692 if (c == K_SPECIAL)
6693 {
6694 *s++ = K_SPECIAL;
6695 *s++ = KS_SPECIAL;
6696 *s++ = KE_FILLER;
6697 }
6698#ifdef FEAT_GUI
6699 else if (c == CSI)
6700 {
6701 *s++ = CSI;
6702 *s++ = KS_EXTRA;
6703 *s++ = (int)KE_CSI;
6704 }
6705#endif
6706 else
6707 *s++ = c;
6708#ifdef FEAT_MBYTE
6709 }
6710#endif
6711 return s;
6712}
6713
6714/*
6715 * move cursor to start of line
6716 * if flags & BL_WHITE move to first non-white
6717 * if flags & BL_SOL move to first non-white if startofline is set,
6718 * otherwise keep "curswant" column
6719 * if flags & BL_FIX don't leave the cursor on a NUL.
6720 */
6721 void
6722beginline(flags)
6723 int flags;
6724{
6725 if ((flags & BL_SOL) && !p_sol)
6726 coladvance(curwin->w_curswant);
6727 else
6728 {
6729 curwin->w_cursor.col = 0;
6730#ifdef FEAT_VIRTUALEDIT
6731 curwin->w_cursor.coladd = 0;
6732#endif
6733
6734 if (flags & (BL_WHITE | BL_SOL))
6735 {
6736 char_u *ptr;
6737
6738 for (ptr = ml_get_curline(); vim_iswhite(*ptr)
6739 && !((flags & BL_FIX) && ptr[1] == NUL); ++ptr)
6740 ++curwin->w_cursor.col;
6741 }
6742 curwin->w_set_curswant = TRUE;
6743 }
6744}
6745
6746/*
6747 * oneright oneleft cursor_down cursor_up
6748 *
6749 * Move one char {right,left,down,up}.
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006750 * Doesn't move onto the NUL past the end of the line, unless it is allowed.
Bram Moolenaar071d4272004-06-13 20:20:40 +00006751 * Return OK when successful, FAIL when we hit a line of file boundary.
6752 */
6753
6754 int
6755oneright()
6756{
6757 char_u *ptr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006758 int l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006759
6760#ifdef FEAT_VIRTUALEDIT
6761 if (virtual_active())
6762 {
6763 pos_T prevpos = curwin->w_cursor;
6764
6765 /* Adjust for multi-wide char (excluding TAB) */
6766 ptr = ml_get_cursor();
6767 coladvance(getviscol() + ((*ptr != TAB && vim_isprintc(
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006768# ifdef FEAT_MBYTE
Bram Moolenaar071d4272004-06-13 20:20:40 +00006769 (*mb_ptr2char)(ptr)
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006770# else
Bram Moolenaar071d4272004-06-13 20:20:40 +00006771 *ptr
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006772# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00006773 ))
6774 ? ptr2cells(ptr) : 1));
6775 curwin->w_set_curswant = TRUE;
6776 /* Return OK if the cursor moved, FAIL otherwise (at window edge). */
6777 return (prevpos.col != curwin->w_cursor.col
6778 || prevpos.coladd != curwin->w_cursor.coladd) ? OK : FAIL;
6779 }
6780#endif
6781
6782 ptr = ml_get_cursor();
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006783 if (*ptr == NUL)
6784 return FAIL; /* already at the very end */
6785
Bram Moolenaar071d4272004-06-13 20:20:40 +00006786#ifdef FEAT_MBYTE
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006787 if (has_mbyte)
6788 l = (*mb_ptr2len)(ptr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00006789 else
6790#endif
Bram Moolenaar2eb25da2006-03-16 21:43:34 +00006791 l = 1;
6792
6793 /* move "l" bytes right, but don't end up on the NUL, unless 'virtualedit'
6794 * contains "onemore". */
6795 if (ptr[l] == NUL
6796#ifdef FEAT_VIRTUALEDIT
6797 && (ve_flags & VE_ONEMORE) == 0
6798#endif
6799 )
6800 return FAIL;
6801 curwin->w_cursor.col += l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00006802
6803 curwin->w_set_curswant = TRUE;
6804 return OK;
6805}
6806
6807 int
6808oneleft()
6809{
6810#ifdef FEAT_VIRTUALEDIT
6811 if (virtual_active())
6812 {
6813 int width;
6814 int v = getviscol();
6815
6816 if (v == 0)
6817 return FAIL;
6818
6819# ifdef FEAT_LINEBREAK
6820 /* We might get stuck on 'showbreak', skip over it. */
6821 width = 1;
6822 for (;;)
6823 {
6824 coladvance(v - width);
6825 /* getviscol() is slow, skip it when 'showbreak' is empty and
6826 * there are no multi-byte characters */
6827 if ((*p_sbr == NUL
6828# ifdef FEAT_MBYTE
6829 && !has_mbyte
6830# endif
6831 ) || getviscol() < v)
6832 break;
6833 ++width;
6834 }
6835# else
6836 coladvance(v - 1);
6837# endif
6838
6839 if (curwin->w_cursor.coladd == 1)
6840 {
6841 char_u *ptr;
6842
6843 /* Adjust for multi-wide char (not a TAB) */
6844 ptr = ml_get_cursor();
6845 if (*ptr != TAB && vim_isprintc(
6846# ifdef FEAT_MBYTE
6847 (*mb_ptr2char)(ptr)
6848# else
6849 *ptr
6850# endif
6851 ) && ptr2cells(ptr) > 1)
6852 curwin->w_cursor.coladd = 0;
6853 }
6854
6855 curwin->w_set_curswant = TRUE;
6856 return OK;
6857 }
6858#endif
6859
6860 if (curwin->w_cursor.col == 0)
6861 return FAIL;
6862
6863 curwin->w_set_curswant = TRUE;
6864 --curwin->w_cursor.col;
6865
6866#ifdef FEAT_MBYTE
6867 /* if the character on the left of the current cursor is a multi-byte
6868 * character, move to its first byte */
6869 if (has_mbyte)
6870 mb_adjust_cursor();
6871#endif
6872 return OK;
6873}
6874
6875 int
6876cursor_up(n, upd_topline)
6877 long n;
6878 int upd_topline; /* When TRUE: update topline */
6879{
6880 linenr_T lnum;
6881
6882 if (n > 0)
6883 {
6884 lnum = curwin->w_cursor.lnum;
Bram Moolenaar7c626922005-02-07 22:01:03 +00006885 /* This fails if the cursor is already in the first line or the count
6886 * is larger than the line number and '-' is in 'cpoptions' */
6887 if (lnum <= 1 || (n >= lnum && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006888 return FAIL;
6889 if (n >= lnum)
6890 lnum = 1;
6891 else
6892#ifdef FEAT_FOLDING
6893 if (hasAnyFolding(curwin))
6894 {
6895 /*
6896 * Count each sequence of folded lines as one logical line.
6897 */
6898 /* go to the the start of the current fold */
6899 (void)hasFolding(lnum, &lnum, NULL);
6900
6901 while (n--)
6902 {
6903 /* move up one line */
6904 --lnum;
6905 if (lnum <= 1)
6906 break;
6907 /* If we entered a fold, move to the beginning, unless in
6908 * Insert mode or when 'foldopen' contains "all": it will open
6909 * in a moment. */
6910 if (n > 0 || !((State & INSERT) || (fdo_flags & FDO_ALL)))
6911 (void)hasFolding(lnum, &lnum, NULL);
6912 }
6913 if (lnum < 1)
6914 lnum = 1;
6915 }
6916 else
6917#endif
6918 lnum -= n;
6919 curwin->w_cursor.lnum = lnum;
6920 }
6921
6922 /* try to advance to the column we want to be at */
6923 coladvance(curwin->w_curswant);
6924
6925 if (upd_topline)
6926 update_topline(); /* make sure curwin->w_topline is valid */
6927
6928 return OK;
6929}
6930
6931/*
6932 * Cursor down a number of logical lines.
6933 */
6934 int
6935cursor_down(n, upd_topline)
6936 long n;
6937 int upd_topline; /* When TRUE: update topline */
6938{
6939 linenr_T lnum;
6940
6941 if (n > 0)
6942 {
6943 lnum = curwin->w_cursor.lnum;
6944#ifdef FEAT_FOLDING
6945 /* Move to last line of fold, will fail if it's the end-of-file. */
6946 (void)hasFolding(lnum, NULL, &lnum);
6947#endif
Bram Moolenaar7c626922005-02-07 22:01:03 +00006948 /* This fails if the cursor is already in the last line or would move
6949 * beyound the last line and '-' is in 'cpoptions' */
6950 if (lnum >= curbuf->b_ml.ml_line_count
6951 || (lnum + n > curbuf->b_ml.ml_line_count
6952 && vim_strchr(p_cpo, CPO_MINUS) != NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00006953 return FAIL;
6954 if (lnum + n >= curbuf->b_ml.ml_line_count)
6955 lnum = curbuf->b_ml.ml_line_count;
6956 else
6957#ifdef FEAT_FOLDING
6958 if (hasAnyFolding(curwin))
6959 {
6960 linenr_T last;
6961
6962 /* count each sequence of folded lines as one logical line */
6963 while (n--)
6964 {
6965 if (hasFolding(lnum, NULL, &last))
6966 lnum = last + 1;
6967 else
6968 ++lnum;
6969 if (lnum >= curbuf->b_ml.ml_line_count)
6970 break;
6971 }
6972 if (lnum > curbuf->b_ml.ml_line_count)
6973 lnum = curbuf->b_ml.ml_line_count;
6974 }
6975 else
6976#endif
6977 lnum += n;
6978 curwin->w_cursor.lnum = lnum;
6979 }
6980
6981 /* try to advance to the column we want to be at */
6982 coladvance(curwin->w_curswant);
6983
6984 if (upd_topline)
6985 update_topline(); /* make sure curwin->w_topline is valid */
6986
6987 return OK;
6988}
6989
6990/*
6991 * Stuff the last inserted text in the read buffer.
6992 * Last_insert actually is a copy of the redo buffer, so we
6993 * first have to remove the command.
6994 */
6995 int
6996stuff_inserted(c, count, no_esc)
6997 int c; /* Command character to be inserted */
6998 long count; /* Repeat this many times */
6999 int no_esc; /* Don't add an ESC at the end */
7000{
7001 char_u *esc_ptr;
7002 char_u *ptr;
7003 char_u *last_ptr;
7004 char_u last = NUL;
7005
7006 ptr = get_last_insert();
7007 if (ptr == NULL)
7008 {
7009 EMSG(_(e_noinstext));
7010 return FAIL;
7011 }
7012
7013 /* may want to stuff the command character, to start Insert mode */
7014 if (c != NUL)
7015 stuffcharReadbuff(c);
7016 if ((esc_ptr = (char_u *)vim_strrchr(ptr, ESC)) != NULL)
7017 *esc_ptr = NUL; /* remove the ESC */
7018
7019 /* when the last char is either "0" or "^" it will be quoted if no ESC
7020 * comes after it OR if it will inserted more than once and "ptr"
7021 * starts with ^D. -- Acevedo
7022 */
7023 last_ptr = (esc_ptr ? esc_ptr : ptr + STRLEN(ptr)) - 1;
7024 if (last_ptr >= ptr && (*last_ptr == '0' || *last_ptr == '^')
7025 && (no_esc || (*ptr == Ctrl_D && count > 1)))
7026 {
7027 last = *last_ptr;
7028 *last_ptr = NUL;
7029 }
7030
7031 do
7032 {
7033 stuffReadbuff(ptr);
7034 /* a trailing "0" is inserted as "<C-V>048", "^" as "<C-V>^" */
7035 if (last)
7036 stuffReadbuff((char_u *)(last == '0'
7037 ? IF_EB("\026\060\064\070", CTRL_V_STR "xf0")
7038 : IF_EB("\026^", CTRL_V_STR "^")));
7039 }
7040 while (--count > 0);
7041
7042 if (last)
7043 *last_ptr = last;
7044
7045 if (esc_ptr != NULL)
7046 *esc_ptr = ESC; /* put the ESC back */
7047
7048 /* may want to stuff a trailing ESC, to get out of Insert mode */
7049 if (!no_esc)
7050 stuffcharReadbuff(ESC);
7051
7052 return OK;
7053}
7054
7055 char_u *
7056get_last_insert()
7057{
7058 if (last_insert == NULL)
7059 return NULL;
7060 return last_insert + last_insert_skip;
7061}
7062
7063/*
7064 * Get last inserted string, and remove trailing <Esc>.
7065 * Returns pointer to allocated memory (must be freed) or NULL.
7066 */
7067 char_u *
7068get_last_insert_save()
7069{
7070 char_u *s;
7071 int len;
7072
7073 if (last_insert == NULL)
7074 return NULL;
7075 s = vim_strsave(last_insert + last_insert_skip);
7076 if (s != NULL)
7077 {
7078 len = (int)STRLEN(s);
7079 if (len > 0 && s[len - 1] == ESC) /* remove trailing ESC */
7080 s[len - 1] = NUL;
7081 }
7082 return s;
7083}
7084
7085/*
7086 * Check the word in front of the cursor for an abbreviation.
7087 * Called when the non-id character "c" has been entered.
7088 * When an abbreviation is recognized it is removed from the text and
7089 * the replacement string is inserted in typebuf.tb_buf[], followed by "c".
7090 */
7091 static int
7092echeck_abbr(c)
7093 int c;
7094{
7095 /* Don't check for abbreviation in paste mode, when disabled and just
7096 * after moving around with cursor keys. */
7097 if (p_paste || no_abbr || arrow_used)
7098 return FALSE;
7099
7100 return check_abbr(c, ml_get_curline(), curwin->w_cursor.col,
7101 curwin->w_cursor.lnum == Insstart.lnum ? Insstart.col : 0);
7102}
7103
7104/*
7105 * replace-stack functions
7106 *
7107 * When replacing characters, the replaced characters are remembered for each
7108 * new character. This is used to re-insert the old text when backspacing.
7109 *
7110 * There is a NUL headed list of characters for each character that is
7111 * currently in the file after the insertion point. When BS is used, one NUL
7112 * headed list is put back for the deleted character.
7113 *
7114 * For a newline, there are two NUL headed lists. One contains the characters
7115 * that the NL replaced. The extra one stores the characters after the cursor
7116 * that were deleted (always white space).
7117 *
7118 * Replace_offset is normally 0, in which case replace_push will add a new
7119 * character at the end of the stack. If replace_offset is not 0, that many
7120 * characters will be left on the stack above the newly inserted character.
7121 */
7122
Bram Moolenaar6c0b44b2005-06-01 21:56:33 +00007123static char_u *replace_stack = NULL;
7124static long replace_stack_nr = 0; /* next entry in replace stack */
7125static long replace_stack_len = 0; /* max. number of entries */
Bram Moolenaar071d4272004-06-13 20:20:40 +00007126
7127 void
7128replace_push(c)
7129 int c; /* character that is replaced (NUL is none) */
7130{
7131 char_u *p;
7132
7133 if (replace_stack_nr < replace_offset) /* nothing to do */
7134 return;
7135 if (replace_stack_len <= replace_stack_nr)
7136 {
7137 replace_stack_len += 50;
7138 p = lalloc(sizeof(char_u) * replace_stack_len, TRUE);
7139 if (p == NULL) /* out of memory */
7140 {
7141 replace_stack_len -= 50;
7142 return;
7143 }
7144 if (replace_stack != NULL)
7145 {
7146 mch_memmove(p, replace_stack,
7147 (size_t)(replace_stack_nr * sizeof(char_u)));
7148 vim_free(replace_stack);
7149 }
7150 replace_stack = p;
7151 }
7152 p = replace_stack + replace_stack_nr - replace_offset;
7153 if (replace_offset)
7154 mch_memmove(p + 1, p, (size_t)(replace_offset * sizeof(char_u)));
7155 *p = c;
7156 ++replace_stack_nr;
7157}
7158
Bram Moolenaar2c994e82008-01-02 16:49:36 +00007159#if defined(FEAT_MBYTE) || defined(PROTO)
7160/*
7161 * Push a character onto the replace stack. Handles a multi-byte character in
7162 * reverse byte order, so that the first byte is popped off first.
7163 * Return the number of bytes done (includes composing characters).
7164 */
7165 int
7166replace_push_mb(p)
7167 char_u *p;
7168{
7169 int l = (*mb_ptr2len)(p);
7170 int j;
7171
7172 for (j = l - 1; j >= 0; --j)
7173 replace_push(p[j]);
7174 return l;
7175}
7176#endif
7177
Bram Moolenaar071d4272004-06-13 20:20:40 +00007178/*
7179 * Pop one item from the replace stack.
7180 * return -1 if stack empty
7181 * return replaced character or NUL otherwise
7182 */
7183 static int
7184replace_pop()
7185{
7186 if (replace_stack_nr == 0)
7187 return -1;
7188 return (int)replace_stack[--replace_stack_nr];
7189}
7190
7191/*
7192 * Join the top two items on the replace stack. This removes to "off"'th NUL
7193 * encountered.
7194 */
7195 static void
7196replace_join(off)
7197 int off; /* offset for which NUL to remove */
7198{
7199 int i;
7200
7201 for (i = replace_stack_nr; --i >= 0; )
7202 if (replace_stack[i] == NUL && off-- <= 0)
7203 {
7204 --replace_stack_nr;
7205 mch_memmove(replace_stack + i, replace_stack + i + 1,
7206 (size_t)(replace_stack_nr - i));
7207 return;
7208 }
7209}
7210
7211/*
7212 * Pop bytes from the replace stack until a NUL is found, and insert them
7213 * before the cursor. Can only be used in REPLACE or VREPLACE mode.
7214 */
7215 static void
7216replace_pop_ins()
7217{
7218 int cc;
7219 int oldState = State;
7220
7221 State = NORMAL; /* don't want REPLACE here */
7222 while ((cc = replace_pop()) > 0)
7223 {
7224#ifdef FEAT_MBYTE
7225 mb_replace_pop_ins(cc);
7226#else
7227 ins_char(cc);
7228#endif
7229 dec_cursor();
7230 }
7231 State = oldState;
7232}
7233
7234#ifdef FEAT_MBYTE
7235/*
7236 * Insert bytes popped from the replace stack. "cc" is the first byte. If it
7237 * indicates a multi-byte char, pop the other bytes too.
7238 */
7239 static void
7240mb_replace_pop_ins(cc)
7241 int cc;
7242{
7243 int n;
7244 char_u buf[MB_MAXBYTES];
7245 int i;
7246 int c;
7247
7248 if (has_mbyte && (n = MB_BYTE2LEN(cc)) > 1)
7249 {
7250 buf[0] = cc;
7251 for (i = 1; i < n; ++i)
7252 buf[i] = replace_pop();
7253 ins_bytes_len(buf, n);
7254 }
7255 else
7256 ins_char(cc);
7257
7258 if (enc_utf8)
7259 /* Handle composing chars. */
7260 for (;;)
7261 {
7262 c = replace_pop();
7263 if (c == -1) /* stack empty */
7264 break;
7265 if ((n = MB_BYTE2LEN(c)) == 1)
7266 {
7267 /* Not a multi-byte char, put it back. */
7268 replace_push(c);
7269 break;
7270 }
7271 else
7272 {
7273 buf[0] = c;
7274 for (i = 1; i < n; ++i)
7275 buf[i] = replace_pop();
7276 if (utf_iscomposing(utf_ptr2char(buf)))
7277 ins_bytes_len(buf, n);
7278 else
7279 {
7280 /* Not a composing char, put it back. */
7281 for (i = n - 1; i >= 0; --i)
7282 replace_push(buf[i]);
7283 break;
7284 }
7285 }
7286 }
7287}
7288#endif
7289
7290/*
7291 * make the replace stack empty
7292 * (called when exiting replace mode)
7293 */
7294 static void
7295replace_flush()
7296{
7297 vim_free(replace_stack);
7298 replace_stack = NULL;
7299 replace_stack_len = 0;
7300 replace_stack_nr = 0;
7301}
7302
7303/*
7304 * Handle doing a BS for one character.
7305 * cc < 0: replace stack empty, just move cursor
7306 * cc == 0: character was inserted, delete it
7307 * cc > 0: character was replaced, put cc (first byte of original char) back
7308 * and check for more characters to be put back
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007309 * When "limit_col" is >= 0, don't delete before this column. Matters when
7310 * using composing characters, use del_char_after_col() instead of del_char().
Bram Moolenaar071d4272004-06-13 20:20:40 +00007311 */
7312 static void
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007313replace_do_bs(limit_col)
7314 int limit_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007315{
7316 int cc;
7317#ifdef FEAT_VREPLACE
7318 int orig_len = 0;
7319 int ins_len;
7320 int orig_vcols = 0;
7321 colnr_T start_vcol;
7322 char_u *p;
7323 int i;
7324 int vcol;
7325#endif
7326
7327 cc = replace_pop();
7328 if (cc > 0)
7329 {
7330#ifdef FEAT_VREPLACE
7331 if (State & VREPLACE_FLAG)
7332 {
7333 /* Get the number of screen cells used by the character we are
7334 * going to delete. */
7335 getvcol(curwin, &curwin->w_cursor, NULL, &start_vcol, NULL);
7336 orig_vcols = chartabsize(ml_get_cursor(), start_vcol);
7337 }
7338#endif
7339#ifdef FEAT_MBYTE
7340 if (has_mbyte)
7341 {
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007342 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007343# ifdef FEAT_VREPLACE
7344 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007345 orig_len = (int)STRLEN(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00007346# endif
7347 replace_push(cc);
7348 }
7349 else
7350#endif
7351 {
7352 pchar_cursor(cc);
7353#ifdef FEAT_VREPLACE
7354 if (State & VREPLACE_FLAG)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007355 orig_len = (int)STRLEN(ml_get_cursor()) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007356#endif
7357 }
7358 replace_pop_ins();
7359
7360#ifdef FEAT_VREPLACE
7361 if (State & VREPLACE_FLAG)
7362 {
7363 /* Get the number of screen cells used by the inserted characters */
7364 p = ml_get_cursor();
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007365 ins_len = (int)STRLEN(p) - orig_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007366 vcol = start_vcol;
7367 for (i = 0; i < ins_len; ++i)
7368 {
7369 vcol += chartabsize(p + i, vcol);
7370#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00007371 i += (*mb_ptr2len)(p) - 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007372#endif
7373 }
7374 vcol -= start_vcol;
7375
7376 /* Delete spaces that were inserted after the cursor to keep the
7377 * text aligned. */
7378 curwin->w_cursor.col += ins_len;
7379 while (vcol > orig_vcols && gchar_cursor() == ' ')
7380 {
7381 del_char(FALSE);
7382 ++orig_vcols;
7383 }
7384 curwin->w_cursor.col -= ins_len;
7385 }
7386#endif
7387
7388 /* mark the buffer as changed and prepare for displaying */
7389 changed_bytes(curwin->w_cursor.lnum, curwin->w_cursor.col);
7390 }
7391 else if (cc == 0)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00007392 (void)del_char_after_col(limit_col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007393}
7394
7395#ifdef FEAT_CINDENT
7396/*
7397 * Return TRUE if C-indenting is on.
7398 */
7399 static int
7400cindent_on()
7401{
7402 return (!p_paste && (curbuf->b_p_cin
7403# ifdef FEAT_EVAL
7404 || *curbuf->b_p_inde != NUL
7405# endif
7406 ));
7407}
7408#endif
7409
7410#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(PROTO)
7411/*
7412 * Re-indent the current line, based on the current contents of it and the
7413 * surrounding lines. Fixing the cursor position seems really easy -- I'm very
7414 * confused what all the part that handles Control-T is doing that I'm not.
7415 * "get_the_indent" should be get_c_indent, get_expr_indent or get_lisp_indent.
7416 */
7417
7418 void
7419fixthisline(get_the_indent)
7420 int (*get_the_indent) __ARGS((void));
7421{
Bram Moolenaar21b17e72008-01-16 19:03:13 +00007422 change_indent(INDENT_SET, get_the_indent(), FALSE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007423 if (linewhite(curwin->w_cursor.lnum))
7424 did_ai = TRUE; /* delete the indent if the line stays empty */
7425}
7426
7427 void
7428fix_indent()
7429{
7430 if (p_paste)
7431 return;
7432# ifdef FEAT_LISP
7433 if (curbuf->b_p_lisp && curbuf->b_p_ai)
7434 fixthisline(get_lisp_indent);
7435# endif
7436# if defined(FEAT_LISP) && defined(FEAT_CINDENT)
7437 else
7438# endif
7439# ifdef FEAT_CINDENT
7440 if (cindent_on())
7441 do_c_expr_indent();
7442# endif
7443}
7444
7445#endif
7446
7447#ifdef FEAT_CINDENT
7448/*
7449 * return TRUE if 'cinkeys' contains the key "keytyped",
7450 * when == '*': Only if key is preceded with '*' (indent before insert)
7451 * when == '!': Only if key is prededed with '!' (don't insert)
7452 * when == ' ': Only if key is not preceded with '*'(indent afterwards)
7453 *
7454 * "keytyped" can have a few special values:
7455 * KEY_OPEN_FORW
7456 * KEY_OPEN_BACK
7457 * KEY_COMPLETE just finished completion.
7458 *
7459 * If line_is_empty is TRUE accept keys with '0' before them.
7460 */
7461 int
7462in_cinkeys(keytyped, when, line_is_empty)
7463 int keytyped;
7464 int when;
7465 int line_is_empty;
7466{
7467 char_u *look;
7468 int try_match;
7469 int try_match_word;
7470 char_u *p;
7471 char_u *line;
7472 int icase;
7473 int i;
7474
Bram Moolenaar30848942009-12-24 14:46:12 +00007475 if (keytyped == NUL)
7476 /* Can happen with CTRL-Y and CTRL-E on a short line. */
7477 return FALSE;
7478
Bram Moolenaar071d4272004-06-13 20:20:40 +00007479#ifdef FEAT_EVAL
7480 if (*curbuf->b_p_inde != NUL)
7481 look = curbuf->b_p_indk; /* 'indentexpr' set: use 'indentkeys' */
7482 else
7483#endif
7484 look = curbuf->b_p_cink; /* 'indentexpr' empty: use 'cinkeys' */
7485 while (*look)
7486 {
7487 /*
7488 * Find out if we want to try a match with this key, depending on
7489 * 'when' and a '*' or '!' before the key.
7490 */
7491 switch (when)
7492 {
7493 case '*': try_match = (*look == '*'); break;
7494 case '!': try_match = (*look == '!'); break;
7495 default: try_match = (*look != '*'); break;
7496 }
7497 if (*look == '*' || *look == '!')
7498 ++look;
7499
7500 /*
7501 * If there is a '0', only accept a match if the line is empty.
7502 * But may still match when typing last char of a word.
7503 */
7504 if (*look == '0')
7505 {
7506 try_match_word = try_match;
7507 if (!line_is_empty)
7508 try_match = FALSE;
7509 ++look;
7510 }
7511 else
7512 try_match_word = FALSE;
7513
7514 /*
7515 * does it look like a control character?
7516 */
7517 if (*look == '^'
7518#ifdef EBCDIC
7519 && (Ctrl_chr(look[1]) != 0)
7520#else
7521 && look[1] >= '?' && look[1] <= '_'
7522#endif
7523 )
7524 {
7525 if (try_match && keytyped == Ctrl_chr(look[1]))
7526 return TRUE;
7527 look += 2;
7528 }
7529 /*
7530 * 'o' means "o" command, open forward.
7531 * 'O' means "O" command, open backward.
7532 */
7533 else if (*look == 'o')
7534 {
7535 if (try_match && keytyped == KEY_OPEN_FORW)
7536 return TRUE;
7537 ++look;
7538 }
7539 else if (*look == 'O')
7540 {
7541 if (try_match && keytyped == KEY_OPEN_BACK)
7542 return TRUE;
7543 ++look;
7544 }
7545
7546 /*
7547 * 'e' means to check for "else" at start of line and just before the
7548 * cursor.
7549 */
7550 else if (*look == 'e')
7551 {
7552 if (try_match && keytyped == 'e' && curwin->w_cursor.col >= 4)
7553 {
7554 p = ml_get_curline();
7555 if (skipwhite(p) == p + curwin->w_cursor.col - 4 &&
7556 STRNCMP(p + curwin->w_cursor.col - 4, "else", 4) == 0)
7557 return TRUE;
7558 }
7559 ++look;
7560 }
7561
7562 /*
7563 * ':' only causes an indent if it is at the end of a label or case
7564 * statement, or when it was before typing the ':' (to fix
7565 * class::method for C++).
7566 */
7567 else if (*look == ':')
7568 {
7569 if (try_match && keytyped == ':')
7570 {
7571 p = ml_get_curline();
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007572 if (cin_iscase(p, FALSE) || cin_isscopedecl(p)
7573 || cin_islabel(30))
Bram Moolenaar071d4272004-06-13 20:20:40 +00007574 return TRUE;
Bram Moolenaar30118152007-06-28 10:49:22 +00007575 /* Need to get the line again after cin_islabel(). */
7576 p = ml_get_curline();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007577 if (curwin->w_cursor.col > 2
7578 && p[curwin->w_cursor.col - 1] == ':'
7579 && p[curwin->w_cursor.col - 2] == ':')
7580 {
7581 p[curwin->w_cursor.col - 1] = ' ';
Bram Moolenaar3acfc302010-07-11 17:23:02 +02007582 i = (cin_iscase(p, FALSE) || cin_isscopedecl(p)
Bram Moolenaar071d4272004-06-13 20:20:40 +00007583 || cin_islabel(30));
7584 p = ml_get_curline();
7585 p[curwin->w_cursor.col - 1] = ':';
7586 if (i)
7587 return TRUE;
7588 }
7589 }
7590 ++look;
7591 }
7592
7593
7594 /*
7595 * Is it a key in <>, maybe?
7596 */
7597 else if (*look == '<')
7598 {
7599 if (try_match)
7600 {
7601 /*
7602 * make up some named keys <o>, <O>, <e>, <0>, <>>, <<>, <*>,
7603 * <:> and <!> so that people can re-indent on o, O, e, 0, <,
7604 * >, *, : and ! keys if they really really want to.
7605 */
7606 if (vim_strchr((char_u *)"<>!*oOe0:", look[1]) != NULL
7607 && keytyped == look[1])
7608 return TRUE;
7609
7610 if (keytyped == get_special_key_code(look + 1))
7611 return TRUE;
7612 }
7613 while (*look && *look != '>')
7614 look++;
7615 while (*look == '>')
7616 look++;
7617 }
7618
7619 /*
7620 * Is it a word: "=word"?
7621 */
7622 else if (*look == '=' && look[1] != ',' && look[1] != NUL)
7623 {
7624 ++look;
7625 if (*look == '~')
7626 {
7627 icase = TRUE;
7628 ++look;
7629 }
7630 else
7631 icase = FALSE;
7632 p = vim_strchr(look, ',');
7633 if (p == NULL)
7634 p = look + STRLEN(look);
7635 if ((try_match || try_match_word)
7636 && curwin->w_cursor.col >= (colnr_T)(p - look))
7637 {
7638 int match = FALSE;
7639
7640#ifdef FEAT_INS_EXPAND
7641 if (keytyped == KEY_COMPLETE)
7642 {
7643 char_u *s;
7644
7645 /* Just completed a word, check if it starts with "look".
7646 * search back for the start of a word. */
7647 line = ml_get_curline();
7648# ifdef FEAT_MBYTE
7649 if (has_mbyte)
7650 {
7651 char_u *n;
7652
7653 for (s = line + curwin->w_cursor.col; s > line; s = n)
7654 {
7655 n = mb_prevptr(line, s);
7656 if (!vim_iswordp(n))
7657 break;
7658 }
7659 }
7660 else
7661# endif
7662 for (s = line + curwin->w_cursor.col; s > line; --s)
7663 if (!vim_iswordc(s[-1]))
7664 break;
7665 if (s + (p - look) <= line + curwin->w_cursor.col
7666 && (icase
7667 ? MB_STRNICMP(s, look, p - look)
7668 : STRNCMP(s, look, p - look)) == 0)
7669 match = TRUE;
7670 }
7671 else
7672#endif
7673 /* TODO: multi-byte */
7674 if (keytyped == (int)p[-1] || (icase && keytyped < 256
7675 && TOLOWER_LOC(keytyped) == TOLOWER_LOC((int)p[-1])))
7676 {
7677 line = ml_get_cursor();
7678 if ((curwin->w_cursor.col == (colnr_T)(p - look)
7679 || !vim_iswordc(line[-(p - look) - 1]))
7680 && (icase
7681 ? MB_STRNICMP(line - (p - look), look, p - look)
7682 : STRNCMP(line - (p - look), look, p - look))
7683 == 0)
7684 match = TRUE;
7685 }
7686 if (match && try_match_word && !try_match)
7687 {
7688 /* "0=word": Check if there are only blanks before the
7689 * word. */
7690 line = ml_get_curline();
7691 if ((int)(skipwhite(line) - line) !=
7692 (int)(curwin->w_cursor.col - (p - look)))
7693 match = FALSE;
7694 }
7695 if (match)
7696 return TRUE;
7697 }
7698 look = p;
7699 }
7700
7701 /*
7702 * ok, it's a boring generic character.
7703 */
7704 else
7705 {
7706 if (try_match && *look == keytyped)
7707 return TRUE;
7708 ++look;
7709 }
7710
7711 /*
7712 * Skip over ", ".
7713 */
7714 look = skip_to_option_part(look);
7715 }
7716 return FALSE;
7717}
7718#endif /* FEAT_CINDENT */
7719
7720#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
7721/*
7722 * Map Hebrew keyboard when in hkmap mode.
7723 */
7724 int
7725hkmap(c)
7726 int c;
7727{
7728 if (p_hkmapp) /* phonetic mapping, by Ilya Dogolazky */
7729 {
7730 enum {hALEF=0, BET, GIMEL, DALET, HEI, VAV, ZAIN, HET, TET, IUD,
7731 KAFsofit, hKAF, LAMED, MEMsofit, MEM, NUNsofit, NUN, SAMEH, AIN,
7732 PEIsofit, PEI, ZADIsofit, ZADI, KOF, RESH, hSHIN, TAV};
7733 static char_u map[26] =
7734 {(char_u)hALEF/*a*/, (char_u)BET /*b*/, (char_u)hKAF /*c*/,
7735 (char_u)DALET/*d*/, (char_u)-1 /*e*/, (char_u)PEIsofit/*f*/,
7736 (char_u)GIMEL/*g*/, (char_u)HEI /*h*/, (char_u)IUD /*i*/,
7737 (char_u)HET /*j*/, (char_u)KOF /*k*/, (char_u)LAMED /*l*/,
7738 (char_u)MEM /*m*/, (char_u)NUN /*n*/, (char_u)SAMEH /*o*/,
7739 (char_u)PEI /*p*/, (char_u)-1 /*q*/, (char_u)RESH /*r*/,
7740 (char_u)ZAIN /*s*/, (char_u)TAV /*t*/, (char_u)TET /*u*/,
7741 (char_u)VAV /*v*/, (char_u)hSHIN/*w*/, (char_u)-1 /*x*/,
7742 (char_u)AIN /*y*/, (char_u)ZADI /*z*/};
7743
7744 if (c == 'N' || c == 'M' || c == 'P' || c == 'C' || c == 'Z')
7745 return (int)(map[CharOrd(c)] - 1 + p_aleph);
7746 /* '-1'='sofit' */
7747 else if (c == 'x')
7748 return 'X';
7749 else if (c == 'q')
7750 return '\''; /* {geresh}={'} */
7751 else if (c == 246)
7752 return ' '; /* \"o --> ' ' for a german keyboard */
7753 else if (c == 228)
7754 return ' '; /* \"a --> ' ' -- / -- */
7755 else if (c == 252)
7756 return ' '; /* \"u --> ' ' -- / -- */
7757#ifdef EBCDIC
7758 else if (islower(c))
7759#else
7760 /* NOTE: islower() does not do the right thing for us on Linux so we
7761 * do this the same was as 5.7 and previous, so it works correctly on
7762 * all systems. Specifically, the e.g. Delete and Arrow keys are
7763 * munged and won't work if e.g. searching for Hebrew text.
7764 */
7765 else if (c >= 'a' && c <= 'z')
7766#endif
7767 return (int)(map[CharOrdLow(c)] + p_aleph);
7768 else
7769 return c;
7770 }
7771 else
7772 {
7773 switch (c)
7774 {
7775 case '`': return ';';
7776 case '/': return '.';
7777 case '\'': return ',';
7778 case 'q': return '/';
7779 case 'w': return '\'';
7780
7781 /* Hebrew letters - set offset from 'a' */
7782 case ',': c = '{'; break;
7783 case '.': c = 'v'; break;
7784 case ';': c = 't'; break;
7785 default: {
7786 static char str[] = "zqbcxlsjphmkwonu ydafe rig";
7787
7788#ifdef EBCDIC
7789 /* see note about islower() above */
7790 if (!islower(c))
7791#else
7792 if (c < 'a' || c > 'z')
7793#endif
7794 return c;
7795 c = str[CharOrdLow(c)];
7796 break;
7797 }
7798 }
7799
7800 return (int)(CharOrdLow(c) + p_aleph);
7801 }
7802}
7803#endif
7804
7805 static void
7806ins_reg()
7807{
7808 int need_redraw = FALSE;
7809 int regname;
7810 int literally = 0;
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007811#ifdef FEAT_VISUAL
7812 int vis_active = VIsual_active;
7813#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007814
7815 /*
7816 * If we are going to wait for a character, show a '"'.
7817 */
7818 pc_status = PC_STATUS_UNSET;
7819 if (redrawing() && !char_avail())
7820 {
7821 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00007822 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007823
7824 edit_putchar('"', TRUE);
7825#ifdef FEAT_CMDL_INFO
7826 add_to_showcmd_c(Ctrl_R);
7827#endif
7828 }
7829
7830#ifdef USE_ON_FLY_SCROLL
7831 dont_scroll = TRUE; /* disallow scrolling here */
7832#endif
7833
7834 /*
7835 * Don't map the register name. This also prevents the mode message to be
7836 * deleted when ESC is hit.
7837 */
7838 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007839 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007840 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007841 if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
7842 {
7843 /* Get a third key for literal register insertion */
7844 literally = regname;
7845#ifdef FEAT_CMDL_INFO
7846 add_to_showcmd_c(literally);
7847#endif
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007848 regname = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007849 LANGMAP_ADJUST(regname, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007850 }
7851 --no_mapping;
7852
7853#ifdef FEAT_EVAL
7854 /*
7855 * Don't call u_sync() while getting the expression,
7856 * evaluating it or giving an error message for it!
7857 */
7858 ++no_u_sync;
7859 if (regname == '=')
7860 {
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007861# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007862 int im_on = im_get_status();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007863# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007864 regname = get_expr_register();
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007865# ifdef USE_IM_CONTROL
Bram Moolenaar071d4272004-06-13 20:20:40 +00007866 /* Restore the Input Method. */
7867 if (im_on)
7868 im_set_active(TRUE);
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007869# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007870 }
Bram Moolenaar677ee682005-01-27 14:41:15 +00007871 if (regname == NUL || !valid_yank_reg(regname, FALSE))
7872 {
7873 vim_beep();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007874 need_redraw = TRUE; /* remove the '"' */
Bram Moolenaar677ee682005-01-27 14:41:15 +00007875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00007876 else
7877 {
7878#endif
7879 if (literally == Ctrl_O || literally == Ctrl_P)
7880 {
7881 /* Append the command to the redo buffer. */
7882 AppendCharToRedobuff(Ctrl_R);
7883 AppendCharToRedobuff(literally);
7884 AppendCharToRedobuff(regname);
7885
7886 do_put(regname, BACKWARD, 1L,
7887 (literally == Ctrl_P ? PUT_FIXINDENT : 0) | PUT_CURSEND);
7888 }
7889 else if (insert_reg(regname, literally) == FAIL)
7890 {
7891 vim_beep();
7892 need_redraw = TRUE; /* remove the '"' */
7893 }
Bram Moolenaar8f999f12005-01-25 22:12:55 +00007894 else if (stop_insert_mode)
7895 /* When the '=' register was used and a function was invoked that
7896 * did ":stopinsert" then stuff_empty() returns FALSE but we won't
7897 * insert anything, need to remove the '"' */
7898 need_redraw = TRUE;
7899
Bram Moolenaar071d4272004-06-13 20:20:40 +00007900#ifdef FEAT_EVAL
7901 }
7902 --no_u_sync;
7903#endif
7904#ifdef FEAT_CMDL_INFO
7905 clear_showcmd();
7906#endif
7907
7908 /* If the inserted register is empty, we need to remove the '"' */
7909 if (need_redraw || stuff_empty())
7910 edit_unputchar();
Bram Moolenaarf193fff2006-04-27 00:02:13 +00007911
7912#ifdef FEAT_VISUAL
7913 /* Disallow starting Visual mode here, would get a weird mode. */
7914 if (!vis_active && VIsual_active)
7915 end_visual_mode();
7916#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00007917}
7918
7919/*
7920 * CTRL-G commands in Insert mode.
7921 */
7922 static void
7923ins_ctrl_g()
7924{
7925 int c;
7926
7927#ifdef FEAT_INS_EXPAND
7928 /* Right after CTRL-X the cursor will be after the ruler. */
7929 setcursor();
7930#endif
7931
7932 /*
7933 * Don't map the second key. This also prevents the mode message to be
7934 * deleted when ESC is hit.
7935 */
7936 ++no_mapping;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00007937 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00007938 --no_mapping;
7939 switch (c)
7940 {
7941 /* CTRL-G k and CTRL-G <Up>: cursor up to Insstart.col */
7942 case K_UP:
7943 case Ctrl_K:
7944 case 'k': ins_up(TRUE);
7945 break;
7946
7947 /* CTRL-G j and CTRL-G <Down>: cursor down to Insstart.col */
7948 case K_DOWN:
7949 case Ctrl_J:
7950 case 'j': ins_down(TRUE);
7951 break;
7952
7953 /* CTRL-G u: start new undoable edit */
Bram Moolenaar779b74b2006-04-10 14:55:34 +00007954 case 'u': u_sync(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00007955 ins_need_undo = TRUE;
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007956
7957 /* Need to reset Insstart, esp. because a BS that joins
Bram Moolenaar34e0bfa2007-05-10 18:44:18 +00007958 * a line to the previous one must save for undo. */
Bram Moolenaara40ceaf2006-01-13 22:35:40 +00007959 Insstart = curwin->w_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +00007960 break;
7961
7962 /* Unknown CTRL-G command, reserved for future expansion. */
7963 default: vim_beep();
7964 }
7965}
7966
7967/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007968 * CTRL-^ in Insert mode.
7969 */
7970 static void
7971ins_ctrl_hat()
7972{
Bram Moolenaar97b2ad32006-03-18 21:40:56 +00007973 if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE))
Bram Moolenaar4be06f92005-07-29 22:36:03 +00007974 {
7975 /* ":lmap" mappings exists, Toggle use of ":lmap" mappings. */
7976 if (State & LANGMAP)
7977 {
7978 curbuf->b_p_iminsert = B_IMODE_NONE;
7979 State &= ~LANGMAP;
7980 }
7981 else
7982 {
7983 curbuf->b_p_iminsert = B_IMODE_LMAP;
7984 State |= LANGMAP;
7985#ifdef USE_IM_CONTROL
7986 im_set_active(FALSE);
7987#endif
7988 }
7989 }
7990#ifdef USE_IM_CONTROL
7991 else
7992 {
7993 /* There are no ":lmap" mappings, toggle IM */
7994 if (im_get_status())
7995 {
7996 curbuf->b_p_iminsert = B_IMODE_NONE;
7997 im_set_active(FALSE);
7998 }
7999 else
8000 {
8001 curbuf->b_p_iminsert = B_IMODE_IM;
8002 State &= ~LANGMAP;
8003 im_set_active(TRUE);
8004 }
8005 }
8006#endif
8007 set_iminsert_global();
8008 showmode();
8009#ifdef FEAT_GUI
8010 /* may show different cursor shape or color */
8011 if (gui.in_use)
8012 gui_update_cursor(TRUE, FALSE);
8013#endif
8014#if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP)
8015 /* Show/unshow value of 'keymap' in status lines. */
8016 status_redraw_curbuf();
8017#endif
8018}
8019
8020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008021 * Handle ESC in insert mode.
8022 * Returns TRUE when leaving insert mode, FALSE when going to repeat the
8023 * insert.
8024 */
8025 static int
Bram Moolenaar488c6512005-08-11 20:09:58 +00008026ins_esc(count, cmdchar, nomove)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008027 long *count;
8028 int cmdchar;
Bram Moolenaar488c6512005-08-11 20:09:58 +00008029 int nomove; /* don't move cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008030{
8031 int temp;
8032 static int disabled_redraw = FALSE;
8033
Bram Moolenaarb9a02fc2006-03-12 22:08:12 +00008034#ifdef FEAT_SPELL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008035 check_spell_redraw();
8036#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008037#if defined(FEAT_HANGULIN)
8038# if defined(ESC_CHG_TO_ENG_MODE)
8039 hangul_input_state_set(0);
8040# endif
8041 if (composing_hangul)
8042 {
8043 push_raw_key(composing_hangul_buffer, 2);
8044 composing_hangul = 0;
8045 }
8046#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008047
8048 temp = curwin->w_cursor.col;
8049 if (disabled_redraw)
8050 {
8051 --RedrawingDisabled;
8052 disabled_redraw = FALSE;
8053 }
8054 if (!arrow_used)
8055 {
8056 /*
8057 * Don't append the ESC for "r<CR>" and "grx".
Bram Moolenaar12805862005-01-05 22:16:17 +00008058 * When 'insertmode' is set only CTRL-L stops Insert mode. Needed for
8059 * when "count" is non-zero.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008060 */
8061 if (cmdchar != 'r' && cmdchar != 'v')
Bram Moolenaar12805862005-01-05 22:16:17 +00008062 AppendToRedobuff(p_im ? (char_u *)"\014" : ESC_STR);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008063
8064 /*
8065 * Repeating insert may take a long time. Check for
8066 * interrupt now and then.
8067 */
8068 if (*count > 0)
8069 {
8070 line_breakcheck();
8071 if (got_int)
8072 *count = 0;
8073 }
8074
8075 if (--*count > 0) /* repeat what was typed */
8076 {
Bram Moolenaar4399ef42005-02-12 14:29:27 +00008077 /* Vi repeats the insert without replacing characters. */
8078 if (vim_strchr(p_cpo, CPO_REPLCNT) != NULL)
8079 State &= ~REPLACE_FLAG;
8080
Bram Moolenaar071d4272004-06-13 20:20:40 +00008081 (void)start_redo_ins();
8082 if (cmdchar == 'r' || cmdchar == 'v')
8083 stuffReadbuff(ESC_STR); /* no ESC in redo buffer */
8084 ++RedrawingDisabled;
8085 disabled_redraw = TRUE;
8086 return FALSE; /* repeat the insert */
8087 }
8088 stop_insert(&curwin->w_cursor, TRUE);
8089 undisplay_dollar();
8090 }
8091
8092 /* When an autoindent was removed, curswant stays after the
8093 * indent */
8094 if (restart_edit == NUL && (colnr_T)temp == curwin->w_cursor.col)
8095 curwin->w_set_curswant = TRUE;
8096
8097 /* Remember the last Insert position in the '^ mark. */
8098 if (!cmdmod.keepjumps)
8099 curbuf->b_last_insert = curwin->w_cursor;
8100
8101 /*
8102 * The cursor should end up on the last inserted character.
Bram Moolenaar488c6512005-08-11 20:09:58 +00008103 * Don't do it for CTRL-O, unless past the end of the line.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104 */
Bram Moolenaar488c6512005-08-11 20:09:58 +00008105 if (!nomove
8106 && (curwin->w_cursor.col != 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107#ifdef FEAT_VIRTUALEDIT
8108 || curwin->w_cursor.coladd > 0
8109#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008110 )
8111 && (restart_edit == NUL
8112 || (gchar_cursor() == NUL
Bram Moolenaar071d4272004-06-13 20:20:40 +00008113#ifdef FEAT_VISUAL
Bram Moolenaar488c6512005-08-11 20:09:58 +00008114 && !VIsual_active
Bram Moolenaar071d4272004-06-13 20:20:40 +00008115#endif
Bram Moolenaar488c6512005-08-11 20:09:58 +00008116 ))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008117#ifdef FEAT_RIGHTLEFT
8118 && !revins_on
8119#endif
8120 )
8121 {
8122#ifdef FEAT_VIRTUALEDIT
8123 if (curwin->w_cursor.coladd > 0 || ve_flags == VE_ALL)
8124 {
8125 oneleft();
8126 if (restart_edit != NUL)
8127 ++curwin->w_cursor.coladd;
8128 }
8129 else
8130#endif
8131 {
8132 --curwin->w_cursor.col;
8133#ifdef FEAT_MBYTE
8134 /* Correct cursor for multi-byte character. */
8135 if (has_mbyte)
8136 mb_adjust_cursor();
8137#endif
8138 }
8139 }
8140
8141#ifdef USE_IM_CONTROL
8142 /* Disable IM to allow typing English directly for Normal mode commands.
8143 * When ":lmap" is enabled don't change 'iminsert' (IM can be enabled as
8144 * well). */
8145 if (!(State & LANGMAP))
8146 im_save_status(&curbuf->b_p_iminsert);
8147 im_set_active(FALSE);
8148#endif
8149
8150 State = NORMAL;
8151 /* need to position cursor again (e.g. when on a TAB ) */
8152 changed_cline_bef_curs();
8153
8154#ifdef FEAT_MOUSE
8155 setmouse();
8156#endif
8157#ifdef CURSOR_SHAPE
8158 ui_cursor_shape(); /* may show different cursor shape */
8159#endif
8160
8161 /*
8162 * When recording or for CTRL-O, need to display the new mode.
8163 * Otherwise remove the mode message.
8164 */
8165 if (Recording || restart_edit != NUL)
8166 showmode();
8167 else if (p_smd)
8168 MSG("");
8169
8170 return TRUE; /* exit Insert mode */
8171}
8172
8173#ifdef FEAT_RIGHTLEFT
8174/*
8175 * Toggle language: hkmap and revins_on.
8176 * Move to end of reverse inserted text.
8177 */
8178 static void
8179ins_ctrl_()
8180{
8181 if (revins_on && revins_chars && revins_scol >= 0)
8182 {
8183 while (gchar_cursor() != NUL && revins_chars--)
8184 ++curwin->w_cursor.col;
8185 }
8186 p_ri = !p_ri;
8187 revins_on = (State == INSERT && p_ri);
8188 if (revins_on)
8189 {
8190 revins_scol = curwin->w_cursor.col;
8191 revins_legal++;
8192 revins_chars = 0;
8193 undisplay_dollar();
8194 }
8195 else
8196 revins_scol = -1;
8197#ifdef FEAT_FKMAP
8198 if (p_altkeymap)
8199 {
8200 /*
8201 * to be consistent also for redo command, using '.'
8202 * set arrow_used to true and stop it - causing to redo
8203 * characters entered in one mode (normal/reverse insert).
8204 */
8205 arrow_used = TRUE;
8206 (void)stop_arrow();
8207 p_fkmap = curwin->w_p_rl ^ p_ri;
8208 if (p_fkmap && p_ri)
8209 State = INSERT;
8210 }
8211 else
8212#endif
8213 p_hkmap = curwin->w_p_rl ^ p_ri; /* be consistent! */
8214 showmode();
8215}
8216#endif
8217
8218#ifdef FEAT_VISUAL
8219/*
8220 * If 'keymodel' contains "startsel", may start selection.
8221 * Returns TRUE when a CTRL-O and other keys stuffed.
8222 */
8223 static int
8224ins_start_select(c)
8225 int c;
8226{
8227 if (km_startsel)
8228 switch (c)
8229 {
8230 case K_KHOME:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 case K_KEND:
Bram Moolenaar071d4272004-06-13 20:20:40 +00008232 case K_PAGEUP:
8233 case K_KPAGEUP:
8234 case K_PAGEDOWN:
8235 case K_KPAGEDOWN:
8236# ifdef MACOS
8237 case K_LEFT:
8238 case K_RIGHT:
8239 case K_UP:
8240 case K_DOWN:
8241 case K_END:
8242 case K_HOME:
8243# endif
8244 if (!(mod_mask & MOD_MASK_SHIFT))
8245 break;
8246 /* FALLTHROUGH */
8247 case K_S_LEFT:
8248 case K_S_RIGHT:
8249 case K_S_UP:
8250 case K_S_DOWN:
8251 case K_S_END:
8252 case K_S_HOME:
8253 /* Start selection right away, the cursor can move with
8254 * CTRL-O when beyond the end of the line. */
8255 start_selection();
8256
8257 /* Execute the key in (insert) Select mode. */
8258 stuffcharReadbuff(Ctrl_O);
8259 if (mod_mask)
8260 {
8261 char_u buf[4];
8262
8263 buf[0] = K_SPECIAL;
8264 buf[1] = KS_MODIFIER;
8265 buf[2] = mod_mask;
8266 buf[3] = NUL;
8267 stuffReadbuff(buf);
8268 }
8269 stuffcharReadbuff(c);
8270 return TRUE;
8271 }
8272 return FALSE;
8273}
8274#endif
8275
8276/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008277 * <Insert> key in Insert mode: toggle insert/remplace mode.
8278 */
8279 static void
8280ins_insert(replaceState)
8281 int replaceState;
8282{
8283#ifdef FEAT_FKMAP
8284 if (p_fkmap && p_ri)
8285 {
8286 beep_flush();
8287 EMSG(farsi_text_3); /* encoded in Farsi */
8288 return;
8289 }
8290#endif
8291
8292#ifdef FEAT_AUTOCMD
Bram Moolenaar1e015462005-09-25 22:16:38 +00008293# ifdef FEAT_EVAL
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008294 set_vim_var_string(VV_INSERTMODE,
8295 (char_u *)((State & REPLACE_FLAG) ? "i" :
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008296# ifdef FEAT_VREPLACE
8297 replaceState == VREPLACE ? "v" :
8298# endif
8299 "r"), 1);
Bram Moolenaar1e015462005-09-25 22:16:38 +00008300# endif
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008301 apply_autocmds(EVENT_INSERTCHANGE, NULL, NULL, FALSE, curbuf);
8302#endif
8303 if (State & REPLACE_FLAG)
8304 State = INSERT | (State & LANGMAP);
8305 else
8306 State = replaceState | (State & LANGMAP);
8307 AppendCharToRedobuff(K_INS);
8308 showmode();
8309#ifdef CURSOR_SHAPE
8310 ui_cursor_shape(); /* may show different cursor shape */
8311#endif
8312}
8313
8314/*
8315 * Pressed CTRL-O in Insert mode.
8316 */
8317 static void
8318ins_ctrl_o()
8319{
8320#ifdef FEAT_VREPLACE
8321 if (State & VREPLACE_FLAG)
8322 restart_edit = 'V';
8323 else
8324#endif
8325 if (State & REPLACE_FLAG)
8326 restart_edit = 'R';
8327 else
8328 restart_edit = 'I';
8329#ifdef FEAT_VIRTUALEDIT
8330 if (virtual_active())
8331 ins_at_eol = FALSE; /* cursor always keeps its column */
8332 else
8333#endif
8334 ins_at_eol = (gchar_cursor() == NUL);
8335}
8336
8337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 * If the cursor is on an indent, ^T/^D insert/delete one
8339 * shiftwidth. Otherwise ^T/^D behave like a "<<" or ">>".
Bram Moolenaar5b3e4602009-02-04 10:20:58 +00008340 * Always round the indent to 'shiftwidth', this is compatible
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 * with vi. But vi only supports ^T and ^D after an
8342 * autoindent, we support it everywhere.
8343 */
8344 static void
8345ins_shift(c, lastc)
8346 int c;
8347 int lastc;
8348{
8349 if (stop_arrow() == FAIL)
8350 return;
8351 AppendCharToRedobuff(c);
8352
8353 /*
8354 * 0^D and ^^D: remove all indent.
8355 */
Bram Moolenaar0cbac5b2007-07-29 13:03:35 +00008356 if (c == Ctrl_D && (lastc == '0' || lastc == '^')
8357 && curwin->w_cursor.col > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {
8359 --curwin->w_cursor.col;
8360 (void)del_char(FALSE); /* delete the '^' or '0' */
8361 /* In Replace mode, restore the characters that '^' or '0' replaced. */
8362 if (State & REPLACE_FLAG)
8363 replace_pop_ins();
8364 if (lastc == '^')
8365 old_indent = get_indent(); /* remember curr. indent */
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008366 change_indent(INDENT_SET, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008367 }
8368 else
Bram Moolenaar21b17e72008-01-16 19:03:13 +00008369 change_indent(c == Ctrl_D ? INDENT_DEC : INDENT_INC, 0, TRUE, 0, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008370
8371 if (did_ai && *skipwhite(ml_get_curline()) != NUL)
8372 did_ai = FALSE;
8373#ifdef FEAT_SMARTINDENT
8374 did_si = FALSE;
8375 can_si = FALSE;
8376 can_si_back = FALSE;
8377#endif
8378#ifdef FEAT_CINDENT
8379 can_cindent = FALSE; /* no cindenting after ^D or ^T */
8380#endif
8381}
8382
8383 static void
8384ins_del()
8385{
8386 int temp;
8387
8388 if (stop_arrow() == FAIL)
8389 return;
8390 if (gchar_cursor() == NUL) /* delete newline */
8391 {
8392 temp = curwin->w_cursor.col;
8393 if (!can_bs(BS_EOL) /* only if "eol" included */
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008394 || do_join(2, FALSE, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008395 vim_beep();
8396 else
8397 curwin->w_cursor.col = temp;
8398 }
8399 else if (del_char(FALSE) == FAIL) /* delete char under cursor */
8400 vim_beep();
8401 did_ai = FALSE;
8402#ifdef FEAT_SMARTINDENT
8403 did_si = FALSE;
8404 can_si = FALSE;
8405 can_si_back = FALSE;
8406#endif
8407 AppendCharToRedobuff(K_DEL);
8408}
8409
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008410static void ins_bs_one __ARGS((colnr_T *vcolp));
8411
8412/*
8413 * Delete one character for ins_bs().
8414 */
8415 static void
8416ins_bs_one(vcolp)
8417 colnr_T *vcolp;
8418{
8419 dec_cursor();
8420 getvcol(curwin, &curwin->w_cursor, vcolp, NULL, NULL);
8421 if (State & REPLACE_FLAG)
8422 {
8423 /* Don't delete characters before the insert point when in
8424 * Replace mode */
8425 if (curwin->w_cursor.lnum != Insstart.lnum
8426 || curwin->w_cursor.col >= Insstart.col)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008427 replace_do_bs(-1);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008428 }
8429 else
8430 (void)del_char(FALSE);
8431}
8432
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433/*
8434 * Handle Backspace, delete-word and delete-line in Insert mode.
8435 * Return TRUE when backspace was actually used.
8436 */
8437 static int
8438ins_bs(c, mode, inserted_space_p)
8439 int c;
8440 int mode;
8441 int *inserted_space_p;
8442{
8443 linenr_T lnum;
8444 int cc;
8445 int temp = 0; /* init for GCC */
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008446 colnr_T save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 colnr_T mincol;
8448 int did_backspace = FALSE;
8449 int in_indent;
8450 int oldState;
8451#ifdef FEAT_MBYTE
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008452 int cpc[MAX_MCO]; /* composing characters */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008453#endif
8454
8455 /*
8456 * can't delete anything in an empty file
8457 * can't backup past first character in buffer
8458 * can't backup past starting point unless 'backspace' > 1
8459 * can backup to a previous line if 'backspace' == 0
8460 */
8461 if ( bufempty()
8462 || (
8463#ifdef FEAT_RIGHTLEFT
8464 !revins_on &&
8465#endif
8466 ((curwin->w_cursor.lnum == 1 && curwin->w_cursor.col == 0)
8467 || (!can_bs(BS_START)
8468 && (arrow_used
8469 || (curwin->w_cursor.lnum == Insstart.lnum
8470 && curwin->w_cursor.col <= Insstart.col)))
8471 || (!can_bs(BS_INDENT) && !arrow_used && ai_col > 0
8472 && curwin->w_cursor.col <= ai_col)
8473 || (!can_bs(BS_EOL) && curwin->w_cursor.col == 0))))
8474 {
8475 vim_beep();
8476 return FALSE;
8477 }
8478
8479 if (stop_arrow() == FAIL)
8480 return FALSE;
8481 in_indent = inindent(0);
8482#ifdef FEAT_CINDENT
8483 if (in_indent)
8484 can_cindent = FALSE;
8485#endif
8486#ifdef FEAT_COMMENTS
8487 end_comment_pending = NUL; /* After BS, don't auto-end comment */
8488#endif
8489#ifdef FEAT_RIGHTLEFT
8490 if (revins_on) /* put cursor after last inserted char */
8491 inc_cursor();
8492#endif
8493
8494#ifdef FEAT_VIRTUALEDIT
8495 /* Virtualedit:
8496 * BACKSPACE_CHAR eats a virtual space
8497 * BACKSPACE_WORD eats all coladd
8498 * BACKSPACE_LINE eats all coladd and keeps going
8499 */
8500 if (curwin->w_cursor.coladd > 0)
8501 {
8502 if (mode == BACKSPACE_CHAR)
8503 {
8504 --curwin->w_cursor.coladd;
8505 return TRUE;
8506 }
8507 if (mode == BACKSPACE_WORD)
8508 {
8509 curwin->w_cursor.coladd = 0;
8510 return TRUE;
8511 }
8512 curwin->w_cursor.coladd = 0;
8513 }
8514#endif
8515
8516 /*
8517 * delete newline!
8518 */
8519 if (curwin->w_cursor.col == 0)
8520 {
8521 lnum = Insstart.lnum;
8522 if (curwin->w_cursor.lnum == Insstart.lnum
8523#ifdef FEAT_RIGHTLEFT
8524 || revins_on
8525#endif
8526 )
8527 {
8528 if (u_save((linenr_T)(curwin->w_cursor.lnum - 2),
8529 (linenr_T)(curwin->w_cursor.lnum + 1)) == FAIL)
8530 return FALSE;
8531 --Insstart.lnum;
8532 Insstart.col = MAXCOL;
8533 }
8534 /*
8535 * In replace mode:
8536 * cc < 0: NL was inserted, delete it
8537 * cc >= 0: NL was replaced, put original characters back
8538 */
8539 cc = -1;
8540 if (State & REPLACE_FLAG)
8541 cc = replace_pop(); /* returns -1 if NL was inserted */
8542 /*
8543 * In replace mode, in the line we started replacing, we only move the
8544 * cursor.
8545 */
8546 if ((State & REPLACE_FLAG) && curwin->w_cursor.lnum <= lnum)
8547 {
8548 dec_cursor();
8549 }
8550 else
8551 {
8552#ifdef FEAT_VREPLACE
8553 if (!(State & VREPLACE_FLAG)
8554 || curwin->w_cursor.lnum > orig_line_count)
8555#endif
8556 {
8557 temp = gchar_cursor(); /* remember current char */
8558 --curwin->w_cursor.lnum;
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008559
8560 /* When "aw" is in 'formatoptions' we must delete the space at
8561 * the end of the line, otherwise the line will be broken
8562 * again when auto-formatting. */
8563 if (has_format_option(FO_AUTO)
8564 && has_format_option(FO_WHITE_PAR))
8565 {
8566 char_u *ptr = ml_get_buf(curbuf, curwin->w_cursor.lnum,
8567 TRUE);
8568 int len;
8569
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008570 len = (int)STRLEN(ptr);
Bram Moolenaarc930a3c2005-05-20 21:27:20 +00008571 if (len > 0 && ptr[len - 1] == ' ')
8572 ptr[len - 1] = NUL;
8573 }
8574
Bram Moolenaar893eaab2010-07-10 17:51:46 +02008575 (void)do_join(2, FALSE, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 if (temp == NUL && gchar_cursor() != NUL)
8577 inc_cursor();
8578 }
8579#ifdef FEAT_VREPLACE
8580 else
8581 dec_cursor();
8582#endif
8583
8584 /*
8585 * In REPLACE mode we have to put back the text that was replaced
8586 * by the NL. On the replace stack is first a NUL-terminated
8587 * sequence of characters that were deleted and then the
8588 * characters that NL replaced.
8589 */
8590 if (State & REPLACE_FLAG)
8591 {
8592 /*
8593 * Do the next ins_char() in NORMAL state, to
8594 * prevent ins_char() from replacing characters and
8595 * avoiding showmatch().
8596 */
8597 oldState = State;
8598 State = NORMAL;
8599 /*
8600 * restore characters (blanks) deleted after cursor
8601 */
8602 while (cc > 0)
8603 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008604 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008605#ifdef FEAT_MBYTE
8606 mb_replace_pop_ins(cc);
8607#else
8608 ins_char(cc);
8609#endif
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008610 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008611 cc = replace_pop();
8612 }
8613 /* restore the characters that NL replaced */
8614 replace_pop_ins();
8615 State = oldState;
8616 }
8617 }
8618 did_ai = FALSE;
8619 }
8620 else
8621 {
8622 /*
8623 * Delete character(s) before the cursor.
8624 */
8625#ifdef FEAT_RIGHTLEFT
8626 if (revins_on) /* put cursor on last inserted char */
8627 dec_cursor();
8628#endif
8629 mincol = 0;
8630 /* keep indent */
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008631 if (mode == BACKSPACE_LINE
8632 && (curbuf->b_p_ai
8633#ifdef FEAT_CINDENT
Bram Moolenaar97b98102009-11-17 16:41:01 +00008634 || cindent_on()
Bram Moolenaar9248e6e2007-03-08 12:10:13 +00008635#endif
8636 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637#ifdef FEAT_RIGHTLEFT
8638 && !revins_on
8639#endif
8640 )
8641 {
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008642 save_col = curwin->w_cursor.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008643 beginline(BL_WHITE);
Bram Moolenaar76675562009-11-11 12:22:32 +00008644 if (curwin->w_cursor.col < save_col)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008645 mincol = curwin->w_cursor.col;
Bram Moolenaar5fd0ca72009-05-13 16:56:33 +00008646 curwin->w_cursor.col = save_col;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008647 }
8648
8649 /*
8650 * Handle deleting one 'shiftwidth' or 'softtabstop'.
8651 */
8652 if ( mode == BACKSPACE_CHAR
8653 && ((p_sta && in_indent)
Bram Moolenaar280f1262006-01-30 00:14:18 +00008654 || (curbuf->b_p_sts != 0
Bram Moolenaar7b88a0e2008-01-09 09:14:13 +00008655 && curwin->w_cursor.col > 0
Bram Moolenaar071d4272004-06-13 20:20:40 +00008656 && (*(ml_get_cursor() - 1) == TAB
8657 || (*(ml_get_cursor() - 1) == ' '
8658 && (!*inserted_space_p
8659 || arrow_used))))))
8660 {
8661 int ts;
8662 colnr_T vcol;
8663 colnr_T want_vcol;
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008664 colnr_T start_vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665
8666 *inserted_space_p = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +00008667 if (p_sta && in_indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008668 ts = curbuf->b_p_sw;
8669 else
8670 ts = curbuf->b_p_sts;
8671 /* Compute the virtual column where we want to be. Since
8672 * 'showbreak' may get in the way, need to get the last column of
8673 * the previous character. */
8674 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008675 start_vcol = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008676 dec_cursor();
8677 getvcol(curwin, &curwin->w_cursor, NULL, NULL, &want_vcol);
8678 inc_cursor();
8679 want_vcol = (want_vcol / ts) * ts;
8680
8681 /* delete characters until we are at or before want_vcol */
8682 while (vcol > want_vcol
8683 && (cc = *(ml_get_cursor() - 1), vim_iswhite(cc)))
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008684 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685
8686 /* insert extra spaces until we are at want_vcol */
8687 while (vcol < want_vcol)
8688 {
8689 /* Remember the first char we inserted */
8690 if (curwin->w_cursor.lnum == Insstart.lnum
8691 && curwin->w_cursor.col < Insstart.col)
8692 Insstart.col = curwin->w_cursor.col;
8693
8694#ifdef FEAT_VREPLACE
8695 if (State & VREPLACE_FLAG)
8696 ins_char(' ');
8697 else
8698#endif
8699 {
8700 ins_str((char_u *)" ");
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008701 if ((State & REPLACE_FLAG))
8702 replace_push(NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008703 }
8704 getvcol(curwin, &curwin->w_cursor, &vcol, NULL, NULL);
8705 }
Bram Moolenaarf5dcf7c2007-12-09 19:26:44 +00008706
8707 /* If we are now back where we started delete one character. Can
8708 * happen when using 'sts' and 'linebreak'. */
8709 if (vcol >= start_vcol)
8710 ins_bs_one(&vcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 }
8712
8713 /*
8714 * Delete upto starting point, start of line or previous word.
8715 */
8716 else do
8717 {
8718#ifdef FEAT_RIGHTLEFT
8719 if (!revins_on) /* put cursor on char to be deleted */
8720#endif
8721 dec_cursor();
8722
8723 /* start of word? */
8724 if (mode == BACKSPACE_WORD && !vim_isspace(gchar_cursor()))
8725 {
8726 mode = BACKSPACE_WORD_NOT_SPACE;
8727 temp = vim_iswordc(gchar_cursor());
8728 }
8729 /* end of word? */
8730 else if (mode == BACKSPACE_WORD_NOT_SPACE
8731 && (vim_isspace(cc = gchar_cursor())
8732 || vim_iswordc(cc) != temp))
8733 {
8734#ifdef FEAT_RIGHTLEFT
8735 if (!revins_on)
8736#endif
8737 inc_cursor();
8738#ifdef FEAT_RIGHTLEFT
8739 else if (State & REPLACE_FLAG)
8740 dec_cursor();
8741#endif
8742 break;
8743 }
8744 if (State & REPLACE_FLAG)
Bram Moolenaar0f6c9482009-01-13 11:29:48 +00008745 replace_do_bs(-1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 else
8747 {
8748#ifdef FEAT_MBYTE
8749 if (enc_utf8 && p_deco)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008750 (void)utfc_ptr2char(ml_get_cursor(), cpc);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751#endif
8752 (void)del_char(FALSE);
8753#ifdef FEAT_MBYTE
8754 /*
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008755 * If there are combining characters and 'delcombine' is set
8756 * move the cursor back. Don't back up before the base
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757 * character.
8758 */
Bram Moolenaar362e1a32006-03-06 23:29:24 +00008759 if (enc_utf8 && p_deco && cpc[0] != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 inc_cursor();
8761#endif
8762#ifdef FEAT_RIGHTLEFT
8763 if (revins_chars)
8764 {
8765 revins_chars--;
8766 revins_legal++;
8767 }
8768 if (revins_on && gchar_cursor() == NUL)
8769 break;
8770#endif
8771 }
8772 /* Just a single backspace?: */
8773 if (mode == BACKSPACE_CHAR)
8774 break;
8775 } while (
8776#ifdef FEAT_RIGHTLEFT
8777 revins_on ||
8778#endif
8779 (curwin->w_cursor.col > mincol
8780 && (curwin->w_cursor.lnum != Insstart.lnum
8781 || curwin->w_cursor.col != Insstart.col)));
8782 did_backspace = TRUE;
8783 }
8784#ifdef FEAT_SMARTINDENT
8785 did_si = FALSE;
8786 can_si = FALSE;
8787 can_si_back = FALSE;
8788#endif
8789 if (curwin->w_cursor.col <= 1)
8790 did_ai = FALSE;
8791 /*
8792 * It's a little strange to put backspaces into the redo
8793 * buffer, but it makes auto-indent a lot easier to deal
8794 * with.
8795 */
8796 AppendCharToRedobuff(c);
8797
8798 /* If deleted before the insertion point, adjust it */
8799 if (curwin->w_cursor.lnum == Insstart.lnum
8800 && curwin->w_cursor.col < Insstart.col)
8801 Insstart.col = curwin->w_cursor.col;
8802
8803 /* vi behaviour: the cursor moves backward but the character that
8804 * was there remains visible
8805 * Vim behaviour: the cursor moves backward and the character that
8806 * was there is erased from the screen.
8807 * We can emulate the vi behaviour by pretending there is a dollar
8808 * displayed even when there isn't.
8809 * --pkv Sun Jan 19 01:56:40 EST 2003 */
8810 if (vim_strchr(p_cpo, CPO_BACKSPACE) != NULL && dollar_vcol == 0)
8811 dollar_vcol = curwin->w_virtcol;
8812
Bram Moolenaarce3be472008-01-14 19:12:28 +00008813#ifdef FEAT_FOLDING
8814 /* When deleting a char the cursor line must never be in a closed fold.
8815 * E.g., when 'foldmethod' is indent and deleting the first non-white
8816 * char before a Tab. */
8817 if (did_backspace)
8818 foldOpenCursor();
8819#endif
8820
Bram Moolenaar071d4272004-06-13 20:20:40 +00008821 return did_backspace;
8822}
8823
8824#ifdef FEAT_MOUSE
8825 static void
8826ins_mouse(c)
8827 int c;
8828{
8829 pos_T tpos;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008830 win_T *old_curwin = curwin;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831
8832# ifdef FEAT_GUI
8833 /* When GUI is active, also move/paste when 'mouse' is empty */
8834 if (!gui.in_use)
8835# endif
8836 if (!mouse_has(MOUSE_INSERT))
8837 return;
8838
8839 undisplay_dollar();
8840 tpos = curwin->w_cursor;
8841 if (do_mouse(NULL, c, BACKWARD, 1L, 0))
8842 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008843#ifdef FEAT_WINDOWS
8844 win_T *new_curwin = curwin;
8845
8846 if (curwin != old_curwin && win_valid(old_curwin))
8847 {
8848 /* Mouse took us to another window. We need to go back to the
8849 * previous one to stop insert there properly. */
8850 curwin = old_curwin;
8851 curbuf = curwin->w_buffer;
8852 }
8853#endif
8854 start_arrow(curwin == old_curwin ? &tpos : NULL);
8855#ifdef FEAT_WINDOWS
8856 if (curwin != new_curwin && win_valid(new_curwin))
8857 {
8858 curwin = new_curwin;
8859 curbuf = curwin->w_buffer;
8860 }
8861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862# ifdef FEAT_CINDENT
8863 can_cindent = TRUE;
8864# endif
8865 }
8866
8867#ifdef FEAT_WINDOWS
8868 /* redraw status lines (in case another window became active) */
8869 redraw_statuslines();
8870#endif
8871}
8872
8873 static void
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02008874ins_mousescroll(dir)
8875 int dir;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876{
8877 pos_T tpos;
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008878# if defined(FEAT_WINDOWS)
8879 win_T *old_curwin = curwin;
8880# endif
8881# ifdef FEAT_INS_EXPAND
8882 int did_scroll = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008883# endif
8884
8885 tpos = curwin->w_cursor;
8886
8887# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008888 /* Currently the mouse coordinates are only known in the GUI. */
8889 if (gui.in_use && mouse_row >= 0 && mouse_col >= 0)
8890 {
8891 int row, col;
8892
8893 row = mouse_row;
8894 col = mouse_col;
8895
8896 /* find the window at the pointer coordinates */
8897 curwin = mouse_find_win(&row, &col);
8898 curbuf = curwin->w_buffer;
8899 }
8900 if (curwin == old_curwin)
8901# endif
8902 undisplay_dollar();
8903
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008904# ifdef FEAT_INS_EXPAND
8905 /* Don't scroll the window in which completion is being done. */
8906 if (!pum_visible()
8907# if defined(FEAT_WINDOWS)
8908 || curwin != old_curwin
8909# endif
8910 )
8911# endif
8912 {
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02008913 if (dir == MSCR_DOWN || dir == MSCR_UP)
8914 {
8915 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8916 scroll_redraw(dir,
8917 (long)(curwin->w_botline - curwin->w_topline));
8918 else
8919 scroll_redraw(dir, 3L);
8920 }
8921#ifdef FEAT_GUI
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008922 else
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02008923 {
8924 int val, step = 6;
8925
8926 if (mod_mask & (MOD_MASK_SHIFT | MOD_MASK_CTRL))
8927 step = W_WIDTH(curwin);
8928 val = curwin->w_leftcol + (dir == MSCR_RIGHT ? -step : step);
8929 if (val < 0)
8930 val = 0;
8931 gui_do_horiz_scroll(val, TRUE);
8932 }
8933#endif
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008934# ifdef FEAT_INS_EXPAND
8935 did_scroll = TRUE;
8936# endif
8937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008938
8939# if defined(FEAT_GUI) && defined(FEAT_WINDOWS)
8940 curwin->w_redr_status = TRUE;
8941
8942 curwin = old_curwin;
8943 curbuf = curwin->w_buffer;
8944# endif
8945
Bram Moolenaar9b25ffb2007-11-06 21:27:31 +00008946# ifdef FEAT_INS_EXPAND
8947 /* The popup menu may overlay the window, need to redraw it.
8948 * TODO: Would be more efficient to only redraw the windows that are
8949 * overlapped by the popup menu. */
8950 if (pum_visible() && did_scroll)
8951 {
8952 redraw_all_later(NOT_VALID);
8953 ins_compl_show_pum();
8954 }
8955# endif
8956
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957 if (!equalpos(curwin->w_cursor, tpos))
8958 {
8959 start_arrow(&tpos);
8960# ifdef FEAT_CINDENT
8961 can_cindent = TRUE;
8962# endif
8963 }
8964}
8965#endif
8966
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008967#if defined(FEAT_GUI_TABLINE) || defined(PROTO)
Bram Moolenaara94bc432006-03-10 21:42:59 +00008968 static void
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008969ins_tabline(c)
8970 int c;
8971{
8972 /* We will be leaving the current window, unless closing another tab. */
8973 if (c != K_TABMENU || current_tabmenu != TABLINE_MENU_CLOSE
8974 || (current_tab != 0 && current_tab != tabpage_index(curtab)))
8975 {
8976 undisplay_dollar();
8977 start_arrow(&curwin->w_cursor);
8978# ifdef FEAT_CINDENT
8979 can_cindent = TRUE;
8980# endif
8981 }
8982
8983 if (c == K_TABLINE)
8984 goto_tabpage(current_tab);
8985 else
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008986 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008987 handle_tabmenu();
Bram Moolenaar437df8f2006-04-27 21:47:44 +00008988 redraw_statuslines(); /* will redraw the tabline when needed */
8989 }
Bram Moolenaara23ccb82006-02-27 00:08:02 +00008990}
8991#endif
8992
8993#if defined(FEAT_GUI) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008994 void
8995ins_scroll()
8996{
8997 pos_T tpos;
8998
8999 undisplay_dollar();
9000 tpos = curwin->w_cursor;
9001 if (gui_do_scroll())
9002 {
9003 start_arrow(&tpos);
9004# ifdef FEAT_CINDENT
9005 can_cindent = TRUE;
9006# endif
9007 }
9008}
9009
9010 void
9011ins_horscroll()
9012{
9013 pos_T tpos;
9014
9015 undisplay_dollar();
9016 tpos = curwin->w_cursor;
Bram Moolenaar8d9b40e2010-07-25 15:49:07 +02009017 if (gui_do_horiz_scroll(scrollbar_value, FALSE))
Bram Moolenaar071d4272004-06-13 20:20:40 +00009018 {
9019 start_arrow(&tpos);
9020# ifdef FEAT_CINDENT
9021 can_cindent = TRUE;
9022# endif
9023 }
9024}
9025#endif
9026
9027 static void
9028ins_left()
9029{
9030 pos_T tpos;
9031
9032#ifdef FEAT_FOLDING
9033 if ((fdo_flags & FDO_HOR) && KeyTyped)
9034 foldOpenCursor();
9035#endif
9036 undisplay_dollar();
9037 tpos = curwin->w_cursor;
9038 if (oneleft() == OK)
9039 {
Bram Moolenaar39fecab2006-08-29 14:07:36 +00009040#if defined(FEAT_XIM) && defined(FEAT_GUI_GTK)
9041 /* Only call start_arrow() when not busy with preediting, it will
9042 * break undo. K_LEFT is inserted in im_correct_cursor(). */
9043 if (!im_is_preediting())
9044#endif
9045 start_arrow(&tpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009046#ifdef FEAT_RIGHTLEFT
9047 /* If exit reversed string, position is fixed */
9048 if (revins_scol != -1 && (int)curwin->w_cursor.col >= revins_scol)
9049 revins_legal++;
9050 revins_chars++;
9051#endif
9052 }
9053
9054 /*
9055 * if 'whichwrap' set for cursor in insert mode may go to
9056 * previous line
9057 */
9058 else if (vim_strchr(p_ww, '[') != NULL && curwin->w_cursor.lnum > 1)
9059 {
9060 start_arrow(&tpos);
9061 --(curwin->w_cursor.lnum);
9062 coladvance((colnr_T)MAXCOL);
9063 curwin->w_set_curswant = TRUE; /* so we stay at the end */
9064 }
9065 else
9066 vim_beep();
9067}
9068
9069 static void
9070ins_home(c)
9071 int c;
9072{
9073 pos_T tpos;
9074
9075#ifdef FEAT_FOLDING
9076 if ((fdo_flags & FDO_HOR) && KeyTyped)
9077 foldOpenCursor();
9078#endif
9079 undisplay_dollar();
9080 tpos = curwin->w_cursor;
9081 if (c == K_C_HOME)
9082 curwin->w_cursor.lnum = 1;
9083 curwin->w_cursor.col = 0;
9084#ifdef FEAT_VIRTUALEDIT
9085 curwin->w_cursor.coladd = 0;
9086#endif
9087 curwin->w_curswant = 0;
9088 start_arrow(&tpos);
9089}
9090
9091 static void
9092ins_end(c)
9093 int c;
9094{
9095 pos_T tpos;
9096
9097#ifdef FEAT_FOLDING
9098 if ((fdo_flags & FDO_HOR) && KeyTyped)
9099 foldOpenCursor();
9100#endif
9101 undisplay_dollar();
9102 tpos = curwin->w_cursor;
9103 if (c == K_C_END)
9104 curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count;
9105 coladvance((colnr_T)MAXCOL);
9106 curwin->w_curswant = MAXCOL;
9107
9108 start_arrow(&tpos);
9109}
9110
9111 static void
9112ins_s_left()
9113{
9114#ifdef FEAT_FOLDING
9115 if ((fdo_flags & FDO_HOR) && KeyTyped)
9116 foldOpenCursor();
9117#endif
9118 undisplay_dollar();
9119 if (curwin->w_cursor.lnum > 1 || curwin->w_cursor.col > 0)
9120 {
9121 start_arrow(&curwin->w_cursor);
9122 (void)bck_word(1L, FALSE, FALSE);
9123 curwin->w_set_curswant = TRUE;
9124 }
9125 else
9126 vim_beep();
9127}
9128
9129 static void
9130ins_right()
9131{
9132#ifdef FEAT_FOLDING
9133 if ((fdo_flags & FDO_HOR) && KeyTyped)
9134 foldOpenCursor();
9135#endif
9136 undisplay_dollar();
Bram Moolenaar78a15312009-05-15 19:33:18 +00009137 if (gchar_cursor() != NUL
9138#ifdef FEAT_VIRTUALEDIT
9139 || virtual_active()
9140#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009141 )
9142 {
9143 start_arrow(&curwin->w_cursor);
9144 curwin->w_set_curswant = TRUE;
9145#ifdef FEAT_VIRTUALEDIT
9146 if (virtual_active())
9147 oneright();
9148 else
9149#endif
9150 {
9151#ifdef FEAT_MBYTE
9152 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00009153 curwin->w_cursor.col += (*mb_ptr2len)(ml_get_cursor());
Bram Moolenaar071d4272004-06-13 20:20:40 +00009154 else
9155#endif
9156 ++curwin->w_cursor.col;
9157 }
9158
9159#ifdef FEAT_RIGHTLEFT
9160 revins_legal++;
9161 if (revins_chars)
9162 revins_chars--;
9163#endif
9164 }
9165 /* if 'whichwrap' set for cursor in insert mode, may move the
9166 * cursor to the next line */
9167 else if (vim_strchr(p_ww, ']') != NULL
9168 && curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count)
9169 {
9170 start_arrow(&curwin->w_cursor);
9171 curwin->w_set_curswant = TRUE;
9172 ++curwin->w_cursor.lnum;
9173 curwin->w_cursor.col = 0;
9174 }
9175 else
9176 vim_beep();
9177}
9178
9179 static void
9180ins_s_right()
9181{
9182#ifdef FEAT_FOLDING
9183 if ((fdo_flags & FDO_HOR) && KeyTyped)
9184 foldOpenCursor();
9185#endif
9186 undisplay_dollar();
9187 if (curwin->w_cursor.lnum < curbuf->b_ml.ml_line_count
9188 || gchar_cursor() != NUL)
9189 {
9190 start_arrow(&curwin->w_cursor);
9191 (void)fwd_word(1L, FALSE, 0);
9192 curwin->w_set_curswant = TRUE;
9193 }
9194 else
9195 vim_beep();
9196}
9197
9198 static void
9199ins_up(startcol)
9200 int startcol; /* when TRUE move to Insstart.col */
9201{
9202 pos_T tpos;
9203 linenr_T old_topline = curwin->w_topline;
9204#ifdef FEAT_DIFF
9205 int old_topfill = curwin->w_topfill;
9206#endif
9207
9208 undisplay_dollar();
9209 tpos = curwin->w_cursor;
9210 if (cursor_up(1L, TRUE) == OK)
9211 {
9212 if (startcol)
9213 coladvance(getvcol_nolist(&Insstart));
9214 if (old_topline != curwin->w_topline
9215#ifdef FEAT_DIFF
9216 || old_topfill != curwin->w_topfill
9217#endif
9218 )
9219 redraw_later(VALID);
9220 start_arrow(&tpos);
9221#ifdef FEAT_CINDENT
9222 can_cindent = TRUE;
9223#endif
9224 }
9225 else
9226 vim_beep();
9227}
9228
9229 static void
9230ins_pageup()
9231{
9232 pos_T tpos;
9233
9234 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009235
9236#ifdef FEAT_WINDOWS
9237 if (mod_mask & MOD_MASK_CTRL)
9238 {
9239 /* <C-PageUp>: tab page back */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009240 if (first_tabpage->tp_next != NULL)
9241 {
9242 start_arrow(&curwin->w_cursor);
9243 goto_tabpage(-1);
9244 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009245 return;
9246 }
9247#endif
9248
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249 tpos = curwin->w_cursor;
9250 if (onepage(BACKWARD, 1L) == OK)
9251 {
9252 start_arrow(&tpos);
9253#ifdef FEAT_CINDENT
9254 can_cindent = TRUE;
9255#endif
9256 }
9257 else
9258 vim_beep();
9259}
9260
9261 static void
9262ins_down(startcol)
9263 int startcol; /* when TRUE move to Insstart.col */
9264{
9265 pos_T tpos;
9266 linenr_T old_topline = curwin->w_topline;
9267#ifdef FEAT_DIFF
9268 int old_topfill = curwin->w_topfill;
9269#endif
9270
9271 undisplay_dollar();
9272 tpos = curwin->w_cursor;
9273 if (cursor_down(1L, TRUE) == OK)
9274 {
9275 if (startcol)
9276 coladvance(getvcol_nolist(&Insstart));
9277 if (old_topline != curwin->w_topline
9278#ifdef FEAT_DIFF
9279 || old_topfill != curwin->w_topfill
9280#endif
9281 )
9282 redraw_later(VALID);
9283 start_arrow(&tpos);
9284#ifdef FEAT_CINDENT
9285 can_cindent = TRUE;
9286#endif
9287 }
9288 else
9289 vim_beep();
9290}
9291
9292 static void
9293ins_pagedown()
9294{
9295 pos_T tpos;
9296
9297 undisplay_dollar();
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009298
9299#ifdef FEAT_WINDOWS
9300 if (mod_mask & MOD_MASK_CTRL)
9301 {
9302 /* <C-PageDown>: tab page forward */
Bram Moolenaarbc444822006-10-17 11:37:50 +00009303 if (first_tabpage->tp_next != NULL)
9304 {
9305 start_arrow(&curwin->w_cursor);
9306 goto_tabpage(0);
9307 }
Bram Moolenaar7fc904b2006-04-13 20:37:35 +00009308 return;
9309 }
9310#endif
9311
Bram Moolenaar071d4272004-06-13 20:20:40 +00009312 tpos = curwin->w_cursor;
9313 if (onepage(FORWARD, 1L) == OK)
9314 {
9315 start_arrow(&tpos);
9316#ifdef FEAT_CINDENT
9317 can_cindent = TRUE;
9318#endif
9319 }
9320 else
9321 vim_beep();
9322}
9323
9324#ifdef FEAT_DND
9325 static void
9326ins_drop()
9327{
9328 do_put('~', BACKWARD, 1L, PUT_CURSEND);
9329}
9330#endif
9331
9332/*
9333 * Handle TAB in Insert or Replace mode.
9334 * Return TRUE when the TAB needs to be inserted like a normal character.
9335 */
9336 static int
9337ins_tab()
9338{
9339 int ind;
9340 int i;
9341 int temp;
9342
9343 if (Insstart_blank_vcol == MAXCOL && curwin->w_cursor.lnum == Insstart.lnum)
9344 Insstart_blank_vcol = get_nolist_virtcol();
9345 if (echeck_abbr(TAB + ABBR_OFF))
9346 return FALSE;
9347
9348 ind = inindent(0);
9349#ifdef FEAT_CINDENT
9350 if (ind)
9351 can_cindent = FALSE;
9352#endif
9353
9354 /*
9355 * When nothing special, insert TAB like a normal character
9356 */
9357 if (!curbuf->b_p_et
9358 && !(p_sta && ind && curbuf->b_p_ts != curbuf->b_p_sw)
9359 && curbuf->b_p_sts == 0)
9360 return TRUE;
9361
9362 if (stop_arrow() == FAIL)
9363 return TRUE;
9364
9365 did_ai = FALSE;
9366#ifdef FEAT_SMARTINDENT
9367 did_si = FALSE;
9368 can_si = FALSE;
9369 can_si_back = FALSE;
9370#endif
9371 AppendToRedobuff((char_u *)"\t");
9372
9373 if (p_sta && ind) /* insert tab in indent, use 'shiftwidth' */
9374 temp = (int)curbuf->b_p_sw;
9375 else if (curbuf->b_p_sts > 0) /* use 'softtabstop' when set */
9376 temp = (int)curbuf->b_p_sts;
9377 else /* otherwise use 'tabstop' */
9378 temp = (int)curbuf->b_p_ts;
9379 temp -= get_nolist_virtcol() % temp;
9380
9381 /*
9382 * Insert the first space with ins_char(). It will delete one char in
9383 * replace mode. Insert the rest with ins_str(); it will not delete any
9384 * chars. For VREPLACE mode, we use ins_char() for all characters.
9385 */
9386 ins_char(' ');
9387 while (--temp > 0)
9388 {
9389#ifdef FEAT_VREPLACE
9390 if (State & VREPLACE_FLAG)
9391 ins_char(' ');
9392 else
9393#endif
9394 {
9395 ins_str((char_u *)" ");
9396 if (State & REPLACE_FLAG) /* no char replaced */
9397 replace_push(NUL);
9398 }
9399 }
9400
9401 /*
9402 * When 'expandtab' not set: Replace spaces by TABs where possible.
9403 */
9404 if (!curbuf->b_p_et && (curbuf->b_p_sts || (p_sta && ind)))
9405 {
9406 char_u *ptr;
9407#ifdef FEAT_VREPLACE
9408 char_u *saved_line = NULL; /* init for GCC */
9409 pos_T pos;
9410#endif
9411 pos_T fpos;
9412 pos_T *cursor;
9413 colnr_T want_vcol, vcol;
9414 int change_col = -1;
9415 int save_list = curwin->w_p_list;
9416
9417 /*
9418 * Get the current line. For VREPLACE mode, don't make real changes
9419 * yet, just work on a copy of the line.
9420 */
9421#ifdef FEAT_VREPLACE
9422 if (State & VREPLACE_FLAG)
9423 {
9424 pos = curwin->w_cursor;
9425 cursor = &pos;
9426 saved_line = vim_strsave(ml_get_curline());
9427 if (saved_line == NULL)
9428 return FALSE;
9429 ptr = saved_line + pos.col;
9430 }
9431 else
9432#endif
9433 {
9434 ptr = ml_get_cursor();
9435 cursor = &curwin->w_cursor;
9436 }
9437
9438 /* When 'L' is not in 'cpoptions' a tab always takes up 'ts' spaces. */
9439 if (vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9440 curwin->w_p_list = FALSE;
9441
9442 /* Find first white before the cursor */
9443 fpos = curwin->w_cursor;
9444 while (fpos.col > 0 && vim_iswhite(ptr[-1]))
9445 {
9446 --fpos.col;
9447 --ptr;
9448 }
9449
9450 /* In Replace mode, don't change characters before the insert point. */
9451 if ((State & REPLACE_FLAG)
9452 && fpos.lnum == Insstart.lnum
9453 && fpos.col < Insstart.col)
9454 {
9455 ptr += Insstart.col - fpos.col;
9456 fpos.col = Insstart.col;
9457 }
9458
9459 /* compute virtual column numbers of first white and cursor */
9460 getvcol(curwin, &fpos, &vcol, NULL, NULL);
9461 getvcol(curwin, cursor, &want_vcol, NULL, NULL);
9462
9463 /* Use as many TABs as possible. Beware of 'showbreak' and
9464 * 'linebreak' adding extra virtual columns. */
9465 while (vim_iswhite(*ptr))
9466 {
9467 i = lbr_chartabsize((char_u *)"\t", vcol);
9468 if (vcol + i > want_vcol)
9469 break;
9470 if (*ptr != TAB)
9471 {
9472 *ptr = TAB;
9473 if (change_col < 0)
9474 {
9475 change_col = fpos.col; /* Column of first change */
9476 /* May have to adjust Insstart */
9477 if (fpos.lnum == Insstart.lnum && fpos.col < Insstart.col)
9478 Insstart.col = fpos.col;
9479 }
9480 }
9481 ++fpos.col;
9482 ++ptr;
9483 vcol += i;
9484 }
9485
9486 if (change_col >= 0)
9487 {
9488 int repl_off = 0;
9489
9490 /* Skip over the spaces we need. */
9491 while (vcol < want_vcol && *ptr == ' ')
9492 {
9493 vcol += lbr_chartabsize(ptr, vcol);
9494 ++ptr;
9495 ++repl_off;
9496 }
9497 if (vcol > want_vcol)
9498 {
9499 /* Must have a char with 'showbreak' just before it. */
9500 --ptr;
9501 --repl_off;
9502 }
9503 fpos.col += repl_off;
9504
9505 /* Delete following spaces. */
9506 i = cursor->col - fpos.col;
9507 if (i > 0)
9508 {
Bram Moolenaarc1a11ed2008-06-24 22:09:24 +00009509 STRMOVE(ptr, ptr + i);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009510 /* correct replace stack. */
9511 if ((State & REPLACE_FLAG)
9512#ifdef FEAT_VREPLACE
9513 && !(State & VREPLACE_FLAG)
9514#endif
9515 )
9516 for (temp = i; --temp >= 0; )
9517 replace_join(repl_off);
9518 }
Bram Moolenaar009b2592004-10-24 19:18:58 +00009519#ifdef FEAT_NETBEANS_INTG
Bram Moolenaarb26e6322010-05-22 21:34:09 +02009520 if (netbeans_active())
Bram Moolenaar009b2592004-10-24 19:18:58 +00009521 {
Bram Moolenaar67c53842010-05-22 18:28:27 +02009522 netbeans_removed(curbuf, fpos.lnum, cursor->col, (long)(i + 1));
Bram Moolenaar009b2592004-10-24 19:18:58 +00009523 netbeans_inserted(curbuf, fpos.lnum, cursor->col,
9524 (char_u *)"\t", 1);
9525 }
9526#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009527 cursor->col -= i;
9528
9529#ifdef FEAT_VREPLACE
9530 /*
9531 * In VREPLACE mode, we haven't changed anything yet. Do it now by
9532 * backspacing over the changed spacing and then inserting the new
9533 * spacing.
9534 */
9535 if (State & VREPLACE_FLAG)
9536 {
9537 /* Backspace from real cursor to change_col */
9538 backspace_until_column(change_col);
9539
9540 /* Insert each char in saved_line from changed_col to
9541 * ptr-cursor */
9542 ins_bytes_len(saved_line + change_col,
9543 cursor->col - change_col);
9544 }
9545#endif
9546 }
9547
9548#ifdef FEAT_VREPLACE
9549 if (State & VREPLACE_FLAG)
9550 vim_free(saved_line);
9551#endif
9552 curwin->w_p_list = save_list;
9553 }
9554
9555 return FALSE;
9556}
9557
9558/*
9559 * Handle CR or NL in insert mode.
9560 * Return TRUE when out of memory or can't undo.
9561 */
9562 static int
9563ins_eol(c)
9564 int c;
9565{
9566 int i;
9567
9568 if (echeck_abbr(c + ABBR_OFF))
9569 return FALSE;
9570 if (stop_arrow() == FAIL)
9571 return TRUE;
9572 undisplay_dollar();
9573
9574 /*
9575 * Strange Vi behaviour: In Replace mode, typing a NL will not delete the
9576 * character under the cursor. Only push a NUL on the replace stack,
9577 * nothing to put back when the NL is deleted.
9578 */
9579 if ((State & REPLACE_FLAG)
9580#ifdef FEAT_VREPLACE
9581 && !(State & VREPLACE_FLAG)
9582#endif
9583 )
9584 replace_push(NUL);
9585
9586 /*
9587 * In VREPLACE mode, a NL replaces the rest of the line, and starts
9588 * replacing the next line, so we push all of the characters left on the
9589 * line onto the replace stack. This is not done here though, it is done
9590 * in open_line().
9591 */
9592
Bram Moolenaarf193fff2006-04-27 00:02:13 +00009593#ifdef FEAT_VIRTUALEDIT
9594 /* Put cursor on NUL if on the last char and coladd is 1 (happens after
9595 * CTRL-O). */
9596 if (virtual_active() && curwin->w_cursor.coladd > 0)
9597 coladvance(getviscol());
9598#endif
9599
Bram Moolenaar071d4272004-06-13 20:20:40 +00009600#ifdef FEAT_RIGHTLEFT
9601# ifdef FEAT_FKMAP
9602 if (p_altkeymap && p_fkmap)
9603 fkmap(NL);
9604# endif
9605 /* NL in reverse insert will always start in the end of
9606 * current line. */
9607 if (revins_on)
9608 curwin->w_cursor.col += (colnr_T)STRLEN(ml_get_cursor());
9609#endif
9610
9611 AppendToRedobuff(NL_STR);
9612 i = open_line(FORWARD,
9613#ifdef FEAT_COMMENTS
9614 has_format_option(FO_RET_COMS) ? OPENLINE_DO_COM :
9615#endif
9616 0, old_indent);
9617 old_indent = 0;
9618#ifdef FEAT_CINDENT
9619 can_cindent = TRUE;
9620#endif
Bram Moolenaar6ae133b2006-11-01 20:25:45 +00009621#ifdef FEAT_FOLDING
9622 /* When inserting a line the cursor line must never be in a closed fold. */
9623 foldOpenCursor();
9624#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00009625
9626 return (!i);
9627}
9628
9629#ifdef FEAT_DIGRAPHS
9630/*
9631 * Handle digraph in insert mode.
9632 * Returns character still to be inserted, or NUL when nothing remaining to be
9633 * done.
9634 */
9635 static int
9636ins_digraph()
9637{
9638 int c;
9639 int cc;
9640
9641 pc_status = PC_STATUS_UNSET;
9642 if (redrawing() && !char_avail())
9643 {
9644 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009645 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646
9647 edit_putchar('?', TRUE);
9648#ifdef FEAT_CMDL_INFO
9649 add_to_showcmd_c(Ctrl_K);
9650#endif
9651 }
9652
9653#ifdef USE_ON_FLY_SCROLL
9654 dont_scroll = TRUE; /* disallow scrolling here */
9655#endif
9656
9657 /* don't map the digraph chars. This also prevents the
9658 * mode message to be deleted when ESC is hit */
9659 ++no_mapping;
9660 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009661 c = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662 --no_mapping;
9663 --allow_keys;
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009664 edit_unputchar(); /* when line fits in 'columns' the '?' is at the start
9665 of the next line and will not be redrawn */
9666
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667 if (IS_SPECIAL(c) || mod_mask) /* special key */
9668 {
9669#ifdef FEAT_CMDL_INFO
9670 clear_showcmd();
9671#endif
9672 insert_special(c, TRUE, FALSE);
9673 return NUL;
9674 }
9675 if (c != ESC)
9676 {
9677 if (redrawing() && !char_avail())
9678 {
9679 /* may need to redraw when no more chars available now */
Bram Moolenaar754b5602006-02-09 23:53:20 +00009680 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009681
9682 if (char2cells(c) == 1)
9683 {
9684 /* first remove the '?', otherwise it's restored when typing
9685 * an ESC next */
9686 edit_unputchar();
Bram Moolenaar754b5602006-02-09 23:53:20 +00009687 ins_redraw(FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009688 edit_putchar(c, TRUE);
9689 }
9690#ifdef FEAT_CMDL_INFO
9691 add_to_showcmd_c(c);
9692#endif
9693 }
9694 ++no_mapping;
9695 ++allow_keys;
Bram Moolenaar61abfd12007-09-13 16:26:47 +00009696 cc = plain_vgetc();
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697 --no_mapping;
9698 --allow_keys;
Bram Moolenaar26dcc7e2010-07-14 22:35:55 +02009699 edit_unputchar(); /* when line fits in 'columns' the '?' is at the
9700 start of the next line and will not be redrawn */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009701 if (cc != ESC)
9702 {
9703 AppendToRedobuff((char_u *)CTRL_V_STR);
9704 c = getdigraph(c, cc, TRUE);
9705#ifdef FEAT_CMDL_INFO
9706 clear_showcmd();
9707#endif
9708 return c;
9709 }
9710 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711#ifdef FEAT_CMDL_INFO
9712 clear_showcmd();
9713#endif
9714 return NUL;
9715}
9716#endif
9717
9718/*
9719 * Handle CTRL-E and CTRL-Y in Insert mode: copy char from other line.
9720 * Returns the char to be inserted, or NUL if none found.
9721 */
9722 static int
9723ins_copychar(lnum)
9724 linenr_T lnum;
9725{
9726 int c;
9727 int temp;
9728 char_u *ptr, *prev_ptr;
9729
9730 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
9731 {
9732 vim_beep();
9733 return NUL;
9734 }
9735
9736 /* try to advance to the cursor column */
9737 temp = 0;
9738 ptr = ml_get(lnum);
9739 prev_ptr = ptr;
9740 validate_virtcol();
9741 while ((colnr_T)temp < curwin->w_virtcol && *ptr != NUL)
9742 {
9743 prev_ptr = ptr;
9744 temp += lbr_chartabsize_adv(&ptr, (colnr_T)temp);
9745 }
9746 if ((colnr_T)temp > curwin->w_virtcol)
9747 ptr = prev_ptr;
9748
9749#ifdef FEAT_MBYTE
9750 c = (*mb_ptr2char)(ptr);
9751#else
9752 c = *ptr;
9753#endif
9754 if (c == NUL)
9755 vim_beep();
9756 return c;
9757}
9758
Bram Moolenaar4be06f92005-07-29 22:36:03 +00009759/*
9760 * CTRL-Y or CTRL-E typed in Insert mode.
9761 */
9762 static int
9763ins_ctrl_ey(tc)
9764 int tc;
9765{
9766 int c = tc;
9767
9768#ifdef FEAT_INS_EXPAND
9769 if (ctrl_x_mode == CTRL_X_SCROLL)
9770 {
9771 if (c == Ctrl_Y)
9772 scrolldown_clamp();
9773 else
9774 scrollup_clamp();
9775 redraw_later(VALID);
9776 }
9777 else
9778#endif
9779 {
9780 c = ins_copychar(curwin->w_cursor.lnum + (c == Ctrl_Y ? -1 : 1));
9781 if (c != NUL)
9782 {
9783 long tw_save;
9784
9785 /* The character must be taken literally, insert like it
9786 * was typed after a CTRL-V, and pretend 'textwidth'
9787 * wasn't set. Digits, 'o' and 'x' are special after a
9788 * CTRL-V, don't use it for these. */
9789 if (c < 256 && !isalnum(c))
9790 AppendToRedobuff((char_u *)CTRL_V_STR); /* CTRL-V */
9791 tw_save = curbuf->b_p_tw;
9792 curbuf->b_p_tw = -1;
9793 insert_special(c, TRUE, FALSE);
9794 curbuf->b_p_tw = tw_save;
9795#ifdef FEAT_RIGHTLEFT
9796 revins_chars++;
9797 revins_legal++;
9798#endif
9799 c = Ctrl_V; /* pretend CTRL-V is last character */
9800 auto_format(FALSE, TRUE);
9801 }
9802 }
9803 return c;
9804}
9805
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806#ifdef FEAT_SMARTINDENT
9807/*
9808 * Try to do some very smart auto-indenting.
9809 * Used when inserting a "normal" character.
9810 */
9811 static void
9812ins_try_si(c)
9813 int c;
9814{
9815 pos_T *pos, old_pos;
9816 char_u *ptr;
9817 int i;
9818 int temp;
9819
9820 /*
9821 * do some very smart indenting when entering '{' or '}'
9822 */
9823 if (((did_si || can_si_back) && c == '{') || (can_si && c == '}'))
9824 {
9825 /*
9826 * for '}' set indent equal to indent of line containing matching '{'
9827 */
9828 if (c == '}' && (pos = findmatch(NULL, '{')) != NULL)
9829 {
9830 old_pos = curwin->w_cursor;
9831 /*
9832 * If the matching '{' has a ')' immediately before it (ignoring
9833 * white-space), then line up with the start of the line
9834 * containing the matching '(' if there is one. This handles the
9835 * case where an "if (..\n..) {" statement continues over multiple
9836 * lines -- webb
9837 */
9838 ptr = ml_get(pos->lnum);
9839 i = pos->col;
9840 if (i > 0) /* skip blanks before '{' */
9841 while (--i > 0 && vim_iswhite(ptr[i]))
9842 ;
9843 curwin->w_cursor.lnum = pos->lnum;
9844 curwin->w_cursor.col = i;
9845 if (ptr[i] == ')' && (pos = findmatch(NULL, '(')) != NULL)
9846 curwin->w_cursor = *pos;
9847 i = get_indent();
9848 curwin->w_cursor = old_pos;
9849#ifdef FEAT_VREPLACE
9850 if (State & VREPLACE_FLAG)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009851 change_indent(INDENT_SET, i, FALSE, NUL, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009852 else
9853#endif
9854 (void)set_indent(i, SIN_CHANGED);
9855 }
9856 else if (curwin->w_cursor.col > 0)
9857 {
9858 /*
9859 * when inserting '{' after "O" reduce indent, but not
9860 * more than indent of previous line
9861 */
9862 temp = TRUE;
9863 if (c == '{' && can_si_back && curwin->w_cursor.lnum > 1)
9864 {
9865 old_pos = curwin->w_cursor;
9866 i = get_indent();
9867 while (curwin->w_cursor.lnum > 1)
9868 {
9869 ptr = skipwhite(ml_get(--(curwin->w_cursor.lnum)));
9870
9871 /* ignore empty lines and lines starting with '#'. */
9872 if (*ptr != '#' && *ptr != NUL)
9873 break;
9874 }
9875 if (get_indent() >= i)
9876 temp = FALSE;
9877 curwin->w_cursor = old_pos;
9878 }
9879 if (temp)
Bram Moolenaar21b17e72008-01-16 19:03:13 +00009880 shift_line(TRUE, FALSE, 1, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881 }
9882 }
9883
9884 /*
9885 * set indent of '#' always to 0
9886 */
9887 if (curwin->w_cursor.col > 0 && can_si && c == '#')
9888 {
9889 /* remember current indent for next line */
9890 old_indent = get_indent();
9891 (void)set_indent(0, SIN_CHANGED);
9892 }
9893
9894 /* Adjust ai_col, the char at this position can be deleted. */
9895 if (ai_col > curwin->w_cursor.col)
9896 ai_col = curwin->w_cursor.col;
9897}
9898#endif
9899
9900/*
9901 * Get the value that w_virtcol would have when 'list' is off.
9902 * Unless 'cpo' contains the 'L' flag.
9903 */
9904 static colnr_T
9905get_nolist_virtcol()
9906{
9907 if (curwin->w_p_list && vim_strchr(p_cpo, CPO_LISTWM) == NULL)
9908 return getvcol_nolist(&curwin->w_cursor);
9909 validate_virtcol();
9910 return curwin->w_virtcol;
9911}